Mercurial > pidgin.yaz
annotate finch/libgnt/gntmenu.c @ 19105:fd27baf1711b
disapproval of revision 'c80d121e4fd54ef1ba9c1ce9e4afa059f6b777e9'
author | Eric Polino <aluink@pidgin.im> |
---|---|
date | Tue, 19 Jun 2007 19:33:16 +0000 |
parents | ac904659104f |
children | c6282dc3fb9f |
rev | line source |
---|---|
17718
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
1 /** |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
3 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
6 * source distribution. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
7 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
11 * (at your option) any later version. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
12 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
16 * GNU General Public License for more details. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
17 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
21 */ |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15931
diff
changeset
|
22 |
15818 | 23 #include "gntmenu.h" |
24 #include "gntmenuitemcheck.h" | |
25 | |
26 #include <string.h> | |
27 | |
28 enum | |
29 { | |
30 SIGS = 1, | |
31 }; | |
32 | |
33 static GntTreeClass *parent_class = NULL; | |
34 | |
35 static void (*org_draw)(GntWidget *wid); | |
36 static void (*org_destroy)(GntWidget *wid); | |
37 static void (*org_map)(GntWidget *wid); | |
38 static gboolean (*org_key_pressed)(GntWidget *w, const char *t); | |
39 | |
40 static void | |
41 gnt_menu_draw(GntWidget *widget) | |
42 { | |
43 GntMenu *menu = GNT_MENU(widget); | |
44 GList *iter; | |
45 chtype type; | |
46 int i; | |
47 | |
48 if (menu->type == GNT_MENU_TOPLEVEL) { | |
49 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_HIGHLIGHT)); | |
50 werase(widget->window); | |
51 | |
52 for (i = 0, iter = menu->list; iter; iter = iter->next, i++) { | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
53 GntMenuItem *item = GNT_MENU_ITEM(iter->data); |
15818 | 54 type = ' ' | COLOR_PAIR(GNT_COLOR_HIGHLIGHT); |
55 if (i == menu->selected) | |
56 type |= A_REVERSE; | |
57 item->priv.x = getcurx(widget->window) + widget->priv.x; | |
58 item->priv.y = getcury(widget->window) + widget->priv.y + 1; | |
59 wbkgdset(widget->window, type); | |
60 wprintw(widget->window, " %s ", item->text); | |
61 } | |
62 } else { | |
63 org_draw(widget); | |
64 } | |
65 | |
66 GNTDEBUG; | |
67 } | |
68 | |
69 static void | |
70 gnt_menu_size_request(GntWidget *widget) | |
71 { | |
72 GntMenu *menu = GNT_MENU(widget); | |
73 | |
74 if (menu->type == GNT_MENU_TOPLEVEL) { | |
75 widget->priv.height = 1; | |
76 widget->priv.width = getmaxx(stdscr); | |
77 } else { | |
78 widget->priv.height = g_list_length(menu->list) + 2; | |
79 widget->priv.width = 25; /* XXX: */ | |
80 } | |
81 } | |
82 | |
83 static void | |
84 menu_tree_add(GntMenu *menu, GntMenuItem *item, GntMenuItem *parent) | |
85 { | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
86 if (GNT_IS_MENU_ITEM_CHECK(item)) { |
15818 | 87 gnt_tree_add_choice(GNT_TREE(menu), item, |
88 gnt_tree_create_row(GNT_TREE(menu), item->text, " "), parent, NULL); | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
89 gnt_tree_set_choice(GNT_TREE(menu), item, gnt_menuitem_check_get_checked(GNT_MENU_ITEM_CHECK(item))); |
15818 | 90 } else |
91 gnt_tree_add_row_last(GNT_TREE(menu), item, | |
92 gnt_tree_create_row(GNT_TREE(menu), item->text, item->submenu ? ">" : " "), parent); | |
93 | |
94 if (0 && item->submenu) { | |
95 GntMenu *sub = GNT_MENU(item->submenu); | |
96 GList *iter; | |
97 for (iter = sub->list; iter; iter = iter->next) { | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
98 GntMenuItem *it = GNT_MENU_ITEM(iter->data); |
15818 | 99 menu_tree_add(menu, it, item); |
100 } | |
101 } | |
102 } | |
103 | |
104 static void | |
105 gnt_menu_map(GntWidget *widget) | |
106 { | |
107 GntMenu *menu = GNT_MENU(widget); | |
108 | |
109 if (menu->type == GNT_MENU_TOPLEVEL) { | |
110 gnt_widget_size_request(widget); | |
111 } else { | |
112 /* Populate the tree */ | |
113 GList *iter; | |
114 gnt_tree_remove_all(GNT_TREE(widget)); | |
115 for (iter = menu->list; iter; iter = iter->next) { | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
116 GntMenuItem *item = GNT_MENU_ITEM(iter->data); |
15818 | 117 menu_tree_add(menu, item, NULL); |
118 } | |
119 org_map(widget); | |
120 gnt_tree_adjust_columns(GNT_TREE(widget)); | |
121 } | |
122 GNTDEBUG; | |
123 } | |
124 | |
125 static void | |
126 menuitem_activate(GntMenu *menu, GntMenuItem *item) | |
127 { | |
128 if (item) { | |
129 if (item->submenu) { | |
130 GntMenu *sub = GNT_MENU(item->submenu); | |
131 menu->submenu = sub; | |
132 sub->type = GNT_MENU_POPUP; /* Submenus are *never* toplevel */ | |
133 sub->parentmenu = menu; | |
134 if (menu->type != GNT_MENU_TOPLEVEL) { | |
135 GntWidget *widget = GNT_WIDGET(menu); | |
136 item->priv.x = widget->priv.x + widget->priv.width - 1; | |
137 item->priv.y = widget->priv.y + gnt_tree_get_selection_visible_line(GNT_TREE(menu)); | |
138 } | |
139 gnt_widget_set_position(GNT_WIDGET(sub), item->priv.x, item->priv.y); | |
140 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(sub), GNT_WIDGET_INVISIBLE); | |
141 gnt_widget_draw(GNT_WIDGET(sub)); | |
142 } else if (item->callback) { | |
143 item->callback(item, item->callbackdata); | |
144 while (menu) { | |
145 gnt_widget_hide(GNT_WIDGET(menu)); | |
146 menu = menu->parentmenu; | |
147 } | |
148 } | |
149 } | |
150 } | |
151 | |
152 static gboolean | |
153 gnt_menu_key_pressed(GntWidget *widget, const char *text) | |
154 { | |
155 GntMenu *menu = GNT_MENU(widget); | |
156 int current = menu->selected; | |
157 | |
158 if (menu->submenu) { | |
159 do menu = menu->submenu; while (menu->submenu); | |
160 return (gnt_widget_key_pressed(GNT_WIDGET(menu), text)); | |
161 } | |
162 | |
163 if (text[0] == 27 && text[1] == 0) { | |
164 /* Escape closes menu */ | |
165 GntMenu *par = menu->parentmenu; | |
166 if (par != NULL) { | |
167 par->submenu = NULL; | |
168 gnt_widget_hide(widget); | |
169 } else | |
170 gnt_widget_hide(widget); | |
171 return TRUE; | |
172 } | |
173 | |
174 if (menu->type == GNT_MENU_TOPLEVEL) { | |
175 if (strcmp(text, GNT_KEY_LEFT) == 0) { | |
176 menu->selected--; | |
177 if (menu->selected < 0) | |
178 menu->selected = g_list_length(menu->list) - 1; | |
179 } else if (strcmp(text, GNT_KEY_RIGHT) == 0) { | |
180 menu->selected++; | |
181 if (menu->selected >= g_list_length(menu->list)) | |
182 menu->selected = 0; | |
183 } else if (strcmp(text, GNT_KEY_ENTER) == 0) { | |
184 gnt_widget_activate(widget); | |
185 } | |
186 | |
187 if (current != menu->selected) { | |
188 gnt_widget_draw(widget); | |
189 return TRUE; | |
190 } | |
191 } else { | |
192 return org_key_pressed(widget, text); | |
193 } | |
194 | |
195 return FALSE; | |
196 } | |
197 | |
198 static void | |
199 gnt_menu_destroy(GntWidget *widget) | |
200 { | |
201 GntMenu *menu = GNT_MENU(widget); | |
202 g_list_foreach(menu->list, (GFunc)g_object_unref, NULL); | |
203 g_list_free(menu->list); | |
204 org_destroy(widget); | |
205 } | |
206 | |
207 static void | |
208 gnt_menu_toggled(GntTree *tree, gpointer key) | |
209 { | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
210 GntMenuItem *item = GNT_MENU_ITEM(key); |
15818 | 211 GntMenu *menu = GNT_MENU(tree); |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
212 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:
15818
diff
changeset
|
213 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item), !check); |
15818 | 214 if (item->callback) |
215 item->callback(item, item->callbackdata); | |
216 while (menu) { | |
217 gnt_widget_hide(GNT_WIDGET(menu)); | |
218 menu = menu->parentmenu; | |
219 } | |
220 } | |
221 | |
222 static void | |
223 gnt_menu_activate(GntWidget *widget) | |
224 { | |
225 GntMenu *menu = GNT_MENU(widget); | |
226 GntMenuItem *item; | |
227 | |
228 if (menu->type == GNT_MENU_TOPLEVEL) { | |
229 item = g_list_nth_data(menu->list, menu->selected); | |
230 } else { | |
231 item = gnt_tree_get_selection_data(GNT_TREE(menu)); | |
232 } | |
233 | |
234 if (item) { | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
235 if (GNT_IS_MENU_ITEM_CHECK(item)) |
15818 | 236 gnt_menu_toggled(GNT_TREE(widget), item); |
237 else | |
238 menuitem_activate(menu, item); | |
239 } | |
240 } | |
241 | |
242 static void | |
243 gnt_menu_hide(GntWidget *widget) | |
244 { | |
19098
ac904659104f
propagate from branch 'im.pidgin.pidgin.2.1.0' (head 121873f517c4c5e2d65c3a1cdd152694834d7db0)
Eric Polino <aluink@pidgin.im>
parents:
18069
diff
changeset
|
245 GntMenu *menu = GNT_MENU(widget); |
15818 | 246 if (menu->parentmenu) |
247 menu->parentmenu->submenu = NULL; | |
248 } | |
249 | |
250 static void | |
251 gnt_menu_class_init(GntMenuClass *klass) | |
252 { | |
253 GntWidgetClass *wid_class = GNT_WIDGET_CLASS(klass); | |
254 parent_class = GNT_TREE_CLASS(klass); | |
255 | |
256 org_destroy = wid_class->destroy; | |
257 org_map = wid_class->map; | |
258 org_draw = wid_class->draw; | |
259 org_key_pressed = wid_class->key_pressed; | |
260 | |
261 wid_class->destroy = gnt_menu_destroy; | |
262 wid_class->draw = gnt_menu_draw; | |
263 wid_class->map = gnt_menu_map; | |
264 wid_class->size_request = gnt_menu_size_request; | |
265 wid_class->key_pressed = gnt_menu_key_pressed; | |
266 wid_class->activate = gnt_menu_activate; | |
267 wid_class->hide = gnt_menu_hide; | |
268 | |
269 parent_class->toggled = gnt_menu_toggled; | |
270 | |
271 GNTDEBUG; | |
272 } | |
273 | |
274 static void | |
275 gnt_menu_init(GTypeInstance *instance, gpointer class) | |
276 { | |
277 GntWidget *widget = GNT_WIDGET(instance); | |
278 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_SHADOW | GNT_WIDGET_NO_BORDER | | |
279 GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_TRANSIENT); | |
280 GNTDEBUG; | |
281 } | |
282 | |
283 /****************************************************************************** | |
284 * GntMenu API | |
285 *****************************************************************************/ | |
286 GType | |
287 gnt_menu_get_gtype(void) | |
288 { | |
289 static GType type = 0; | |
290 | |
291 if(type == 0) | |
292 { | |
293 static const GTypeInfo info = { | |
294 sizeof(GntMenuClass), | |
295 NULL, /* base_init */ | |
296 NULL, /* base_finalize */ | |
297 (GClassInitFunc)gnt_menu_class_init, | |
298 NULL, /* class_finalize */ | |
299 NULL, /* class_data */ | |
300 sizeof(GntMenu), | |
301 0, /* n_preallocs */ | |
302 gnt_menu_init, /* instance_init */ | |
303 NULL /* value_table */ | |
304 }; | |
305 | |
306 type = g_type_register_static(GNT_TYPE_TREE, | |
307 "GntMenu", | |
308 &info, 0); | |
309 } | |
310 | |
311 return type; | |
312 } | |
313 | |
314 GntWidget *gnt_menu_new(GntMenuType type) | |
315 { | |
316 GntWidget *widget = g_object_new(GNT_TYPE_MENU, NULL); | |
317 GntMenu *menu = GNT_MENU(widget); | |
318 menu->list = NULL; | |
319 menu->selected = 0; | |
320 menu->type = type; | |
321 | |
322 if (type == GNT_MENU_TOPLEVEL) { | |
323 widget->priv.x = 0; | |
324 widget->priv.y = 0; | |
325 } else { | |
326 GNT_TREE(widget)->show_separator = FALSE; | |
327 _gnt_tree_init_internals(GNT_TREE(widget), 2); | |
328 gnt_tree_set_col_width(GNT_TREE(widget), 1, 1); /* The second column is to indicate that it has a submenu */ | |
329 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_NO_BORDER); | |
330 } | |
331 | |
332 return widget; | |
333 } | |
334 | |
335 void gnt_menu_add_item(GntMenu *menu, GntMenuItem *item) | |
336 { | |
337 menu->list = g_list_append(menu->list, item); | |
338 } | |
339 |