Mercurial > pidgin
annotate finch/libgnt/gntmenu.c @ 18837:b5d6e360eca0
propagate from branch 'im.pidgin.pidgin' (head 5e5168d639fb9a3a29ab3fe44b976d6d25924403)
to branch 'libgnt.pygnt' (head ec6a81b752b20677effe5ad3da03b7a1dbc922bf)
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Tue, 07 Aug 2007 21:45:31 +0000 |
parents | da550279d390 |
children | 5dd1cfc53666 9f7587b86b0d |
rev | line source |
---|---|
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
1 /** |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
3 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
6 * source distribution. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
7 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
11 * (at your option) any later version. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
12 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
16 * GNU General Public License for more details. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
17 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
21 */ |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
22 |
15817 | 23 #include "gntmenu.h" |
24 #include "gntmenuitemcheck.h" | |
25 | |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
26 #include <ctype.h> |
15817 | 27 #include <string.h> |
28 | |
29 enum | |
30 { | |
31 SIGS = 1, | |
32 }; | |
33 | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
34 enum |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
35 { |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
36 ITEM_TEXT = 0, |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
37 ITEM_TRIGGER, |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
38 ITEM_SUBMENU, |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
39 NUM_COLUMNS |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
40 }; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
41 |
15817 | 42 static GntTreeClass *parent_class = NULL; |
43 | |
44 static void (*org_draw)(GntWidget *wid); | |
45 static void (*org_destroy)(GntWidget *wid); | |
46 static void (*org_map)(GntWidget *wid); | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
47 static void (*org_size_request)(GntWidget *wid); |
15817 | 48 static gboolean (*org_key_pressed)(GntWidget *w, const char *t); |
49 | |
50 static void | |
51 gnt_menu_draw(GntWidget *widget) | |
52 { | |
53 GntMenu *menu = GNT_MENU(widget); | |
54 GList *iter; | |
55 chtype type; | |
56 int i; | |
57 | |
58 if (menu->type == GNT_MENU_TOPLEVEL) { | |
59 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_HIGHLIGHT)); | |
60 werase(widget->window); | |
61 | |
62 for (i = 0, iter = menu->list; iter; iter = iter->next, i++) { | |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
63 GntMenuItem *item = GNT_MENU_ITEM(iter->data); |
15817 | 64 type = ' ' | COLOR_PAIR(GNT_COLOR_HIGHLIGHT); |
65 if (i == menu->selected) | |
66 type |= A_REVERSE; | |
67 item->priv.x = getcurx(widget->window) + widget->priv.x; | |
68 item->priv.y = getcury(widget->window) + widget->priv.y + 1; | |
69 wbkgdset(widget->window, type); | |
70 wprintw(widget->window, " %s ", item->text); | |
71 } | |
72 } else { | |
73 org_draw(widget); | |
74 } | |
75 | |
76 GNTDEBUG; | |
77 } | |
78 | |
79 static void | |
80 gnt_menu_size_request(GntWidget *widget) | |
81 { | |
82 GntMenu *menu = GNT_MENU(widget); | |
83 | |
84 if (menu->type == GNT_MENU_TOPLEVEL) { | |
85 widget->priv.height = 1; | |
86 widget->priv.width = getmaxx(stdscr); | |
87 } else { | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
88 org_size_request(widget); |
15817 | 89 widget->priv.height = g_list_length(menu->list) + 2; |
90 } | |
91 } | |
92 | |
93 static void | |
94 menu_tree_add(GntMenu *menu, GntMenuItem *item, GntMenuItem *parent) | |
95 { | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
96 char trigger[4] = "\0 )\0"; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
97 |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
98 if ((trigger[1] = gnt_menuitem_get_trigger(item)) && trigger[1] != ' ') |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
99 trigger[0] = '('; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
100 |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
101 if (GNT_IS_MENU_ITEM_CHECK(item)) { |
15817 | 102 gnt_tree_add_choice(GNT_TREE(menu), item, |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
103 gnt_tree_create_row(GNT_TREE(menu), item->text, trigger, " "), parent, NULL); |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
104 gnt_tree_set_choice(GNT_TREE(menu), item, gnt_menuitem_check_get_checked(GNT_MENU_ITEM_CHECK(item))); |
15817 | 105 } else |
106 gnt_tree_add_row_last(GNT_TREE(menu), item, | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
107 gnt_tree_create_row(GNT_TREE(menu), item->text, trigger, item->submenu ? ">" : " "), parent); |
15817 | 108 |
109 if (0 && item->submenu) { | |
110 GntMenu *sub = GNT_MENU(item->submenu); | |
111 GList *iter; | |
112 for (iter = sub->list; iter; iter = iter->next) { | |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
113 GntMenuItem *it = GNT_MENU_ITEM(iter->data); |
15817 | 114 menu_tree_add(menu, it, item); |
115 } | |
116 } | |
117 } | |
118 | |
18447
8fc91e437981
Possible crash fix.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18444
diff
changeset
|
119 #define GET_VAL(ch) ((ch >= '0' && ch <= '9') ? (ch - '0') : (ch >= 'a' && ch <= 'z') ? (10 + ch - 'a') : 36) |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
120 |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
121 static void |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
122 assign_triggers(GntMenu *menu) |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
123 { |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
124 GList *iter; |
18447
8fc91e437981
Possible crash fix.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18444
diff
changeset
|
125 gboolean bools[37]; |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
126 |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
127 memset(bools, 0, sizeof(bools)); |
18447
8fc91e437981
Possible crash fix.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18444
diff
changeset
|
128 bools[36] = 1; |
8fc91e437981
Possible crash fix.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18444
diff
changeset
|
129 |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
130 for (iter = menu->list; iter; iter = iter->next) { |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
131 GntMenuItem *item = iter->data; |
18447
8fc91e437981
Possible crash fix.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18444
diff
changeset
|
132 char trigger = tolower(gnt_menuitem_get_trigger(item)); |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
133 if (trigger == '\0' || trigger == ' ') |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
134 continue; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
135 bools[(int)GET_VAL(trigger)] = 1; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
136 } |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
137 |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
138 for (iter = menu->list; iter; iter = iter->next) { |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
139 GntMenuItem *item = iter->data; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
140 char trigger = gnt_menuitem_get_trigger(item); |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
141 const char *text = item->text; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
142 if (trigger != '\0') |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
143 continue; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
144 while (*text) { |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
145 char ch = tolower(*text++); |
18447
8fc91e437981
Possible crash fix.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18444
diff
changeset
|
146 if (ch == ' ' || bools[(int)GET_VAL(ch)]) |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
147 continue; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
148 trigger = ch; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
149 break; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
150 } |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
151 if (trigger == 0) |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
152 trigger = item->text[0]; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
153 gnt_menuitem_set_trigger(item, trigger); |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
154 bools[(int)GET_VAL(trigger)] = 1; |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
155 } |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
156 } |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
157 |
15817 | 158 static void |
159 gnt_menu_map(GntWidget *widget) | |
160 { | |
161 GntMenu *menu = GNT_MENU(widget); | |
162 | |
163 if (menu->type == GNT_MENU_TOPLEVEL) { | |
164 gnt_widget_size_request(widget); | |
165 } else { | |
166 /* Populate the tree */ | |
167 GList *iter; | |
168 gnt_tree_remove_all(GNT_TREE(widget)); | |
18444
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
169 /* Try to assign some trigger for the items */ |
daf41b214ffb
Try to automatically assign some hotkeys for the menuitems. Do we like this?
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18440
diff
changeset
|
170 assign_triggers(menu); |
15817 | 171 for (iter = menu->list; iter; iter = iter->next) { |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
172 GntMenuItem *item = GNT_MENU_ITEM(iter->data); |
15817 | 173 menu_tree_add(menu, item, NULL); |
174 } | |
175 org_map(widget); | |
176 gnt_tree_adjust_columns(GNT_TREE(widget)); | |
177 } | |
178 GNTDEBUG; | |
179 } | |
180 | |
181 static void | |
182 menuitem_activate(GntMenu *menu, GntMenuItem *item) | |
183 { | |
184 if (item) { | |
185 if (item->submenu) { | |
186 GntMenu *sub = GNT_MENU(item->submenu); | |
187 menu->submenu = sub; | |
188 sub->type = GNT_MENU_POPUP; /* Submenus are *never* toplevel */ | |
189 sub->parentmenu = menu; | |
190 if (menu->type != GNT_MENU_TOPLEVEL) { | |
191 GntWidget *widget = GNT_WIDGET(menu); | |
192 item->priv.x = widget->priv.x + widget->priv.width - 1; | |
193 item->priv.y = widget->priv.y + gnt_tree_get_selection_visible_line(GNT_TREE(menu)); | |
194 } | |
195 gnt_widget_set_position(GNT_WIDGET(sub), item->priv.x, item->priv.y); | |
196 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(sub), GNT_WIDGET_INVISIBLE); | |
197 gnt_widget_draw(GNT_WIDGET(sub)); | |
198 } else if (item->callback) { | |
199 item->callback(item, item->callbackdata); | |
200 while (menu) { | |
201 gnt_widget_hide(GNT_WIDGET(menu)); | |
202 menu = menu->parentmenu; | |
203 } | |
204 } | |
205 } | |
206 } | |
207 | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
208 static GList* |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
209 find_item_with_trigger(GList *start, GList *end, char trigger) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
210 { |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
211 GList *iter; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
212 for (iter = start; iter != (end ? end : NULL); iter = iter->next) { |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
213 if (gnt_menuitem_get_trigger(iter->data) == trigger) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
214 return iter; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
215 } |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
216 return NULL; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
217 } |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
218 |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
219 static gboolean |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
220 check_for_trigger(GntMenu *menu, char trigger) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
221 { |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
222 /* check for a trigger key */ |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
223 GList *iter; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
224 GList *nth = g_list_find(menu->list, gnt_tree_get_selection_data(GNT_TREE(menu))); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
225 GList *find = find_item_with_trigger(nth->next, NULL, trigger); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
226 if (!find) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
227 find = find_item_with_trigger(menu->list, nth->next, trigger); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
228 if (!find) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
229 return FALSE; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
230 if (find != nth) { |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
231 gnt_tree_set_selected(GNT_TREE(menu), find->data); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
232 iter = find_item_with_trigger(find->next, NULL, trigger); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
233 if (iter != NULL && iter != find) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
234 return TRUE; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
235 iter = find_item_with_trigger(menu->list, nth, trigger); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
236 if (iter != NULL && iter != find) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
237 return TRUE; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
238 } |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
239 gnt_widget_activate(GNT_WIDGET(menu)); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
240 return TRUE; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
241 } |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
242 |
15817 | 243 static gboolean |
244 gnt_menu_key_pressed(GntWidget *widget, const char *text) | |
245 { | |
246 GntMenu *menu = GNT_MENU(widget); | |
247 int current = menu->selected; | |
248 | |
249 if (menu->submenu) { | |
250 do menu = menu->submenu; while (menu->submenu); | |
251 return (gnt_widget_key_pressed(GNT_WIDGET(menu), text)); | |
252 } | |
253 | |
254 if (text[0] == 27 && text[1] == 0) { | |
255 /* Escape closes menu */ | |
256 GntMenu *par = menu->parentmenu; | |
257 if (par != NULL) { | |
258 par->submenu = NULL; | |
259 gnt_widget_hide(widget); | |
260 } else | |
261 gnt_widget_hide(widget); | |
262 return TRUE; | |
263 } | |
264 | |
265 if (menu->type == GNT_MENU_TOPLEVEL) { | |
266 if (strcmp(text, GNT_KEY_LEFT) == 0) { | |
267 menu->selected--; | |
268 if (menu->selected < 0) | |
269 menu->selected = g_list_length(menu->list) - 1; | |
270 } else if (strcmp(text, GNT_KEY_RIGHT) == 0) { | |
271 menu->selected++; | |
272 if (menu->selected >= g_list_length(menu->list)) | |
273 menu->selected = 0; | |
274 } else if (strcmp(text, GNT_KEY_ENTER) == 0) { | |
275 gnt_widget_activate(widget); | |
276 } | |
277 | |
278 if (current != menu->selected) { | |
279 gnt_widget_draw(widget); | |
280 return TRUE; | |
281 } | |
282 } else { | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
283 if (text[1] == '\0') { |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
284 if (check_for_trigger(menu, text[0])) |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
285 return TRUE; |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
286 } |
15817 | 287 return org_key_pressed(widget, text); |
288 } | |
289 | |
290 return FALSE; | |
291 } | |
292 | |
293 static void | |
294 gnt_menu_destroy(GntWidget *widget) | |
295 { | |
296 GntMenu *menu = GNT_MENU(widget); | |
297 g_list_foreach(menu->list, (GFunc)g_object_unref, NULL); | |
298 g_list_free(menu->list); | |
299 org_destroy(widget); | |
300 } | |
301 | |
302 static void | |
303 gnt_menu_toggled(GntTree *tree, gpointer key) | |
304 { | |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
305 GntMenuItem *item = GNT_MENU_ITEM(key); |
15817 | 306 GntMenu *menu = GNT_MENU(tree); |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
307 gboolean check = gnt_menuitem_check_get_checked(GNT_MENU_ITEM_CHECK(item)); |
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
308 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item), !check); |
15817 | 309 if (item->callback) |
310 item->callback(item, item->callbackdata); | |
311 while (menu) { | |
312 gnt_widget_hide(GNT_WIDGET(menu)); | |
313 menu = menu->parentmenu; | |
314 } | |
315 } | |
316 | |
317 static void | |
318 gnt_menu_activate(GntWidget *widget) | |
319 { | |
320 GntMenu *menu = GNT_MENU(widget); | |
321 GntMenuItem *item; | |
322 | |
323 if (menu->type == GNT_MENU_TOPLEVEL) { | |
324 item = g_list_nth_data(menu->list, menu->selected); | |
325 } else { | |
326 item = gnt_tree_get_selection_data(GNT_TREE(menu)); | |
327 } | |
328 | |
329 if (item) { | |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
330 if (GNT_IS_MENU_ITEM_CHECK(item)) |
15817 | 331 gnt_menu_toggled(GNT_TREE(widget), item); |
332 else | |
333 menuitem_activate(menu, item); | |
334 } | |
335 } | |
336 | |
337 static void | |
338 gnt_menu_hide(GntWidget *widget) | |
339 { | |
17707
3c3fc1432a01
Let windows know when the workspace they are in is being hidden/shown
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
340 GntMenu *sub, *menu = GNT_MENU(widget); |
3c3fc1432a01
Let windows know when the workspace they are in is being hidden/shown
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
341 |
3c3fc1432a01
Let windows know when the workspace they are in is being hidden/shown
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
342 while ((sub = menu->submenu)) |
3c3fc1432a01
Let windows know when the workspace they are in is being hidden/shown
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
343 gnt_widget_hide(GNT_WIDGET(sub)); |
15817 | 344 if (menu->parentmenu) |
345 menu->parentmenu->submenu = NULL; | |
346 } | |
347 | |
348 static void | |
349 gnt_menu_class_init(GntMenuClass *klass) | |
350 { | |
351 GntWidgetClass *wid_class = GNT_WIDGET_CLASS(klass); | |
352 parent_class = GNT_TREE_CLASS(klass); | |
353 | |
354 org_destroy = wid_class->destroy; | |
355 org_map = wid_class->map; | |
356 org_draw = wid_class->draw; | |
357 org_key_pressed = wid_class->key_pressed; | |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
358 org_size_request = wid_class->size_request; |
15817 | 359 |
360 wid_class->destroy = gnt_menu_destroy; | |
361 wid_class->draw = gnt_menu_draw; | |
362 wid_class->map = gnt_menu_map; | |
363 wid_class->size_request = gnt_menu_size_request; | |
364 wid_class->key_pressed = gnt_menu_key_pressed; | |
365 wid_class->activate = gnt_menu_activate; | |
366 wid_class->hide = gnt_menu_hide; | |
367 | |
368 parent_class->toggled = gnt_menu_toggled; | |
369 | |
370 GNTDEBUG; | |
371 } | |
372 | |
373 static void | |
374 gnt_menu_init(GTypeInstance *instance, gpointer class) | |
375 { | |
376 GntWidget *widget = GNT_WIDGET(instance); | |
377 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_SHADOW | GNT_WIDGET_NO_BORDER | | |
378 GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_TRANSIENT); | |
379 GNTDEBUG; | |
380 } | |
381 | |
382 /****************************************************************************** | |
383 * GntMenu API | |
384 *****************************************************************************/ | |
385 GType | |
386 gnt_menu_get_gtype(void) | |
387 { | |
388 static GType type = 0; | |
389 | |
390 if(type == 0) | |
391 { | |
392 static const GTypeInfo info = { | |
393 sizeof(GntMenuClass), | |
394 NULL, /* base_init */ | |
395 NULL, /* base_finalize */ | |
396 (GClassInitFunc)gnt_menu_class_init, | |
397 NULL, /* class_finalize */ | |
398 NULL, /* class_data */ | |
399 sizeof(GntMenu), | |
400 0, /* n_preallocs */ | |
401 gnt_menu_init, /* instance_init */ | |
402 NULL /* value_table */ | |
403 }; | |
404 | |
405 type = g_type_register_static(GNT_TYPE_TREE, | |
406 "GntMenu", | |
407 &info, 0); | |
408 } | |
409 | |
410 return type; | |
411 } | |
412 | |
413 GntWidget *gnt_menu_new(GntMenuType type) | |
414 { | |
415 GntWidget *widget = g_object_new(GNT_TYPE_MENU, NULL); | |
416 GntMenu *menu = GNT_MENU(widget); | |
417 menu->list = NULL; | |
418 menu->selected = 0; | |
419 menu->type = type; | |
420 | |
421 if (type == GNT_MENU_TOPLEVEL) { | |
422 widget->priv.x = 0; | |
423 widget->priv.y = 0; | |
424 } else { | |
425 GNT_TREE(widget)->show_separator = FALSE; | |
18531
da550279d390
Use gobject properties instead of hacks when creating columns in a tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18447
diff
changeset
|
426 g_object_set(G_OBJECT(widget), "columns", NUM_COLUMNS, NULL); |
18440
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
427 gnt_tree_set_col_width(GNT_TREE(widget), ITEM_TRIGGER, 3); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
428 gnt_tree_set_column_resizable(GNT_TREE(widget), ITEM_TRIGGER, FALSE); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
429 gnt_tree_set_col_width(GNT_TREE(widget), ITEM_SUBMENU, 1); |
be10fc22d649
Allow trigger keys for menuitems.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18404
diff
changeset
|
430 gnt_tree_set_column_resizable(GNT_TREE(widget), ITEM_SUBMENU, FALSE); |
15817 | 431 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_NO_BORDER); |
432 } | |
433 | |
434 return widget; | |
435 } | |
436 | |
437 void gnt_menu_add_item(GntMenu *menu, GntMenuItem *item) | |
438 { | |
439 menu->list = g_list_append(menu->list, item); | |
440 } | |
441 |