Mercurial > pidgin.yaz
annotate finch/libgnt/gntmenu.c @ 18069:08776fc5c06f
propagate from branch 'im.pidgin.finch.workspaces' (head 76cfc9565a5cdde8d2e1f2efc3282b309a55a110)
to branch 'im.pidgin.pidgin.2.1.0' (head 7369e1bc964aa300a73670872d34129642b3ec49)
author | Richard Nelson <wabz@pidgin.im> |
---|---|
date | Fri, 08 Jun 2007 10:21:50 +0000 |
parents | 1cedd520cd18 3c3fc1432a01 |
children | 9a0f99ea664d ac904659104f |
rev | line source |
---|---|
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
diff
changeset
|
1 /** |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
diff
changeset
|
3 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
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:
15931
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:
15931
diff
changeset
|
6 * source distribution. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
diff
changeset
|
7 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
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:
15931
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:
15931
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:
15931
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:
15931
diff
changeset
|
12 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
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:
15931
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:
15931
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:
15931
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:
15931
diff
changeset
|
17 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
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:
15931
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:
15931
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:
15931
diff
changeset
|
21 */ |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
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 { | |
16731
3c3fc1432a01
Let windows know when the workspace they are in is being hidden/shown
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
245 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:
15931
diff
changeset
|
246 |
3c3fc1432a01
Let windows know when the workspace they are in is being hidden/shown
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
247 while ((sub = menu->submenu)) |
3c3fc1432a01
Let windows know when the workspace they are in is being hidden/shown
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
248 gnt_widget_hide(GNT_WIDGET(sub)); |
15818 | 249 if (menu->parentmenu) |
250 menu->parentmenu->submenu = NULL; | |
251 } | |
252 | |
253 static void | |
254 gnt_menu_class_init(GntMenuClass *klass) | |
255 { | |
256 GntWidgetClass *wid_class = GNT_WIDGET_CLASS(klass); | |
257 parent_class = GNT_TREE_CLASS(klass); | |
258 | |
259 org_destroy = wid_class->destroy; | |
260 org_map = wid_class->map; | |
261 org_draw = wid_class->draw; | |
262 org_key_pressed = wid_class->key_pressed; | |
263 | |
264 wid_class->destroy = gnt_menu_destroy; | |
265 wid_class->draw = gnt_menu_draw; | |
266 wid_class->map = gnt_menu_map; | |
267 wid_class->size_request = gnt_menu_size_request; | |
268 wid_class->key_pressed = gnt_menu_key_pressed; | |
269 wid_class->activate = gnt_menu_activate; | |
270 wid_class->hide = gnt_menu_hide; | |
271 | |
272 parent_class->toggled = gnt_menu_toggled; | |
273 | |
274 GNTDEBUG; | |
275 } | |
276 | |
277 static void | |
278 gnt_menu_init(GTypeInstance *instance, gpointer class) | |
279 { | |
280 GntWidget *widget = GNT_WIDGET(instance); | |
281 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_SHADOW | GNT_WIDGET_NO_BORDER | | |
282 GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_TRANSIENT); | |
283 GNTDEBUG; | |
284 } | |
285 | |
286 /****************************************************************************** | |
287 * GntMenu API | |
288 *****************************************************************************/ | |
289 GType | |
290 gnt_menu_get_gtype(void) | |
291 { | |
292 static GType type = 0; | |
293 | |
294 if(type == 0) | |
295 { | |
296 static const GTypeInfo info = { | |
297 sizeof(GntMenuClass), | |
298 NULL, /* base_init */ | |
299 NULL, /* base_finalize */ | |
300 (GClassInitFunc)gnt_menu_class_init, | |
301 NULL, /* class_finalize */ | |
302 NULL, /* class_data */ | |
303 sizeof(GntMenu), | |
304 0, /* n_preallocs */ | |
305 gnt_menu_init, /* instance_init */ | |
306 NULL /* value_table */ | |
307 }; | |
308 | |
309 type = g_type_register_static(GNT_TYPE_TREE, | |
310 "GntMenu", | |
311 &info, 0); | |
312 } | |
313 | |
314 return type; | |
315 } | |
316 | |
317 GntWidget *gnt_menu_new(GntMenuType type) | |
318 { | |
319 GntWidget *widget = g_object_new(GNT_TYPE_MENU, NULL); | |
320 GntMenu *menu = GNT_MENU(widget); | |
321 menu->list = NULL; | |
322 menu->selected = 0; | |
323 menu->type = type; | |
324 | |
325 if (type == GNT_MENU_TOPLEVEL) { | |
326 widget->priv.x = 0; | |
327 widget->priv.y = 0; | |
328 } else { | |
329 GNT_TREE(widget)->show_separator = FALSE; | |
330 _gnt_tree_init_internals(GNT_TREE(widget), 2); | |
331 gnt_tree_set_col_width(GNT_TREE(widget), 1, 1); /* The second column is to indicate that it has a submenu */ | |
332 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_NO_BORDER); | |
333 } | |
334 | |
335 return widget; | |
336 } | |
337 | |
338 void gnt_menu_add_item(GntMenu *menu, GntMenuItem *item) | |
339 { | |
340 menu->list = g_list_append(menu->list, item); | |
341 } | |
342 |