Mercurial > pidgin.yaz
annotate finch/libgnt/gntcombobox.c @ 30190:c9413547ee33
merged from im.pidgin.pidgin
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Fri, 23 Apr 2010 14:44:01 +0900 |
parents | a18f421696dc |
children | 6943aec8cf61 |
rev | line source |
---|---|
17718
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
1 /** |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
3 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
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:
15818
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:
15818
diff
changeset
|
6 * source distribution. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
7 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
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:
15818
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:
15818
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:
15818
diff
changeset
|
11 * (at your option) any later version. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
12 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
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:
15818
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
16 * GNU General Public License for more details. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
17 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
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:
15818
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
19680
44b4e8bd759b
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
18443
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
17718
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
21 */ |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
22 |
28984
a18f421696dc
Fix showing umlauts etc. on non-utf8 locales.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
26521
diff
changeset
|
23 #include "gntinternal.h" |
15818 | 24 #include "gntbox.h" |
25 #include "gntcombobox.h" | |
26 #include "gnttree.h" | |
27 #include "gntmarshal.h" | |
28 #include "gntutils.h" | |
29 | |
30 #include <string.h> | |
31 | |
32 enum | |
33 { | |
34 SIG_SELECTION_CHANGED, | |
35 SIGS, | |
36 }; | |
37 | |
38 static GntWidgetClass *parent_class = NULL; | |
39 static guint signals[SIGS] = { 0 }; | |
40 static void (*widget_lost_focus)(GntWidget *widget); | |
41 | |
42 static void | |
43 set_selection(GntComboBox *box, gpointer key) | |
44 { | |
45 if (box->selected != key) | |
46 { | |
47 /* XXX: make sure the key actually does exist */ | |
48 gpointer old = box->selected; | |
49 box->selected = key; | |
50 if (GNT_WIDGET(box)->window) | |
51 gnt_widget_draw(GNT_WIDGET(box)); | |
52 if (box->dropdown) | |
53 gnt_tree_set_selected(GNT_TREE(box->dropdown), key); | |
54 g_signal_emit(box, signals[SIG_SELECTION_CHANGED], 0, old, key); | |
55 } | |
56 } | |
57 | |
58 static void | |
59 hide_popup(GntComboBox *box, gboolean set) | |
60 { | |
61 gnt_widget_set_size(box->dropdown, | |
62 box->dropdown->priv.width - 1, box->dropdown->priv.height); | |
63 if (set) | |
64 set_selection(box, gnt_tree_get_selection_data(GNT_TREE(box->dropdown))); | |
65 else | |
66 gnt_tree_set_selected(GNT_TREE(box->dropdown), box->selected); | |
67 gnt_widget_hide(box->dropdown->parent); | |
68 } | |
69 | |
70 static void | |
71 gnt_combo_box_draw(GntWidget *widget) | |
72 { | |
73 GntComboBox *box = GNT_COMBO_BOX(widget); | |
74 char *text = NULL, *s; | |
75 GntColorType type; | |
76 int len; | |
26521
c67d43408daa
Update hardware cursor correctly.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
22289
diff
changeset
|
77 |
15818 | 78 if (box->dropdown && box->selected) |
79 text = gnt_tree_get_selection_text(GNT_TREE(box->dropdown)); | |
80 | |
81 if (text == NULL) | |
82 text = g_strdup(""); | |
83 | |
84 if (gnt_widget_has_focus(widget)) | |
85 type = GNT_COLOR_HIGHLIGHT; | |
86 else | |
87 type = GNT_COLOR_NORMAL; | |
88 | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
89 wbkgdset(widget->window, '\0' | gnt_color_pair(type)); |
15818 | 90 |
91 s = (char*)gnt_util_onscreen_width_to_pointer(text, widget->priv.width - 4, &len); | |
92 *s = '\0'; | |
93 | |
28984
a18f421696dc
Fix showing umlauts etc. on non-utf8 locales.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
26521
diff
changeset
|
94 mvwaddstr(widget->window, 1, 1, C_(text)); |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
95 whline(widget->window, ' ' | gnt_color_pair(type), widget->priv.width - 4 - len); |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
96 mvwaddch(widget->window, 1, widget->priv.width - 3, ACS_VLINE | gnt_color_pair(GNT_COLOR_NORMAL)); |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
97 mvwaddch(widget->window, 1, widget->priv.width - 2, ACS_DARROW | gnt_color_pair(GNT_COLOR_NORMAL)); |
26521
c67d43408daa
Update hardware cursor correctly.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
22289
diff
changeset
|
98 wmove(widget->window, 1, 1); |
15818 | 99 |
100 g_free(text); | |
101 GNTDEBUG; | |
102 } | |
103 | |
104 static void | |
105 gnt_combo_box_size_request(GntWidget *widget) | |
106 { | |
107 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_MAPPED)) | |
108 { | |
109 GntWidget *dd = GNT_COMBO_BOX(widget)->dropdown; | |
110 gnt_widget_size_request(dd); | |
111 widget->priv.height = 3; /* For now, a combobox will have border */ | |
18443
92f845cd977c
Fix the initial size of a combobox.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17718
diff
changeset
|
112 widget->priv.width = MAX(10, dd->priv.width + 2); |
15818 | 113 } |
114 } | |
115 | |
116 static void | |
117 gnt_combo_box_map(GntWidget *widget) | |
118 { | |
119 if (widget->priv.width == 0 || widget->priv.height == 0) | |
120 gnt_widget_size_request(widget); | |
121 GNTDEBUG; | |
122 } | |
123 | |
124 static void | |
125 popup_dropdown(GntComboBox *box) | |
126 { | |
127 GntWidget *widget = GNT_WIDGET(box); | |
128 GntWidget *parent = box->dropdown->parent; | |
129 int height = g_list_length(GNT_TREE(box->dropdown)->list); | |
130 int y = widget->priv.y + widget->priv.height - 1; | |
131 gnt_widget_set_size(box->dropdown, widget->priv.width, height + 2); | |
132 | |
133 if (y + height + 2 >= getmaxy(stdscr)) | |
134 y = widget->priv.y - height - 1; | |
135 gnt_widget_set_position(parent, widget->priv.x, y); | |
136 if (parent->window) | |
137 { | |
138 mvwin(parent->window, y, widget->priv.x); | |
139 wresize(parent->window, height+2, widget->priv.width); | |
140 } | |
141 parent->priv.width = widget->priv.width; | |
142 parent->priv.height = height + 2; | |
143 | |
144 GNT_WIDGET_UNSET_FLAGS(parent, GNT_WIDGET_INVISIBLE); | |
145 gnt_widget_draw(parent); | |
146 } | |
147 | |
148 static gboolean | |
149 gnt_combo_box_key_pressed(GntWidget *widget, const char *text) | |
150 { | |
151 GntComboBox *box = GNT_COMBO_BOX(widget); | |
152 if (GNT_WIDGET_IS_FLAG_SET(box->dropdown->parent, GNT_WIDGET_MAPPED)) | |
153 { | |
154 if (text[1] == 0) | |
155 { | |
156 switch (text[0]) | |
157 { | |
158 case '\r': | |
159 case '\t': | |
22289
0ba0f2804299
'\n' (ctrl-j) should behave similar to '\r' (ctrl-m). Fixes #4833.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
21250
diff
changeset
|
160 case '\n': |
15818 | 161 hide_popup(box, TRUE); |
162 return TRUE; | |
163 case 27: | |
164 hide_popup(box, FALSE); | |
165 return TRUE; | |
166 } | |
167 } | |
168 if (gnt_widget_key_pressed(box->dropdown, text)) | |
169 return TRUE; | |
170 } | |
171 else | |
172 { | |
173 if (text[0] == 27) | |
174 { | |
175 if (strcmp(text, GNT_KEY_UP) == 0 || | |
176 strcmp(text, GNT_KEY_DOWN) == 0) | |
177 { | |
178 popup_dropdown(box); | |
179 return TRUE; | |
180 } | |
181 } | |
182 } | |
183 | |
184 return FALSE; | |
185 } | |
186 | |
187 static void | |
188 gnt_combo_box_destroy(GntWidget *widget) | |
189 { | |
190 gnt_widget_destroy(GNT_COMBO_BOX(widget)->dropdown->parent); | |
191 } | |
192 | |
193 static void | |
194 gnt_combo_box_lost_focus(GntWidget *widget) | |
195 { | |
196 GntComboBox *combo = GNT_COMBO_BOX(widget); | |
197 if (GNT_WIDGET_IS_FLAG_SET(combo->dropdown->parent, GNT_WIDGET_MAPPED)) | |
198 hide_popup(combo, FALSE); | |
199 widget_lost_focus(widget); | |
200 } | |
201 | |
202 static gboolean | |
203 gnt_combo_box_clicked(GntWidget *widget, GntMouseEvent event, int x, int y) | |
204 { | |
205 GntComboBox *box = GNT_COMBO_BOX(widget); | |
206 gboolean dshowing = GNT_WIDGET_IS_FLAG_SET(box->dropdown->parent, GNT_WIDGET_MAPPED); | |
207 | |
208 if (event == GNT_MOUSE_SCROLL_UP) { | |
209 if (dshowing) | |
210 gnt_widget_key_pressed(box->dropdown, GNT_KEY_UP); | |
211 } else if (event == GNT_MOUSE_SCROLL_DOWN) { | |
212 if (dshowing) | |
213 gnt_widget_key_pressed(box->dropdown, GNT_KEY_DOWN); | |
214 } else if (event == GNT_LEFT_MOUSE_DOWN) { | |
215 if (dshowing) { | |
216 hide_popup(box, TRUE); | |
217 } else { | |
218 popup_dropdown(GNT_COMBO_BOX(widget)); | |
219 } | |
220 } else | |
221 return FALSE; | |
222 return TRUE; | |
223 } | |
224 | |
225 static void | |
226 gnt_combo_box_size_changed(GntWidget *widget, int oldw, int oldh) | |
227 { | |
228 GntComboBox *box = GNT_COMBO_BOX(widget); | |
229 gnt_widget_set_size(box->dropdown, widget->priv.width - 1, box->dropdown->priv.height); | |
230 } | |
231 | |
232 static void | |
233 gnt_combo_box_class_init(GntComboBoxClass *klass) | |
234 { | |
235 parent_class = GNT_WIDGET_CLASS(klass); | |
236 | |
237 parent_class->destroy = gnt_combo_box_destroy; | |
238 parent_class->draw = gnt_combo_box_draw; | |
239 parent_class->map = gnt_combo_box_map; | |
240 parent_class->size_request = gnt_combo_box_size_request; | |
241 parent_class->key_pressed = gnt_combo_box_key_pressed; | |
242 parent_class->clicked = gnt_combo_box_clicked; | |
243 parent_class->size_changed = gnt_combo_box_size_changed; | |
244 | |
245 widget_lost_focus = parent_class->lost_focus; | |
246 parent_class->lost_focus = gnt_combo_box_lost_focus; | |
247 | |
248 signals[SIG_SELECTION_CHANGED] = | |
249 g_signal_new("selection-changed", | |
250 G_TYPE_FROM_CLASS(klass), | |
251 G_SIGNAL_RUN_LAST, | |
252 0, | |
253 NULL, NULL, | |
254 gnt_closure_marshal_VOID__POINTER_POINTER, | |
255 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER); | |
256 | |
257 GNTDEBUG; | |
258 } | |
259 | |
260 static void | |
261 gnt_combo_box_init(GTypeInstance *instance, gpointer class) | |
262 { | |
263 GntWidget *box; | |
264 GntWidget *widget = GNT_WIDGET(instance); | |
265 GntComboBox *combo = GNT_COMBO_BOX(instance); | |
266 | |
267 GNT_WIDGET_SET_FLAGS(GNT_WIDGET(instance), | |
268 GNT_WIDGET_GROW_X | GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_NO_SHADOW); | |
269 combo->dropdown = gnt_tree_new(); | |
270 | |
271 box = gnt_box_new(FALSE, FALSE); | |
272 GNT_WIDGET_SET_FLAGS(box, GNT_WIDGET_NO_SHADOW | GNT_WIDGET_NO_BORDER | GNT_WIDGET_TRANSIENT); | |
273 gnt_box_set_pad(GNT_BOX(box), 0); | |
274 gnt_box_add_widget(GNT_BOX(box), combo->dropdown); | |
275 | |
276 widget->priv.minw = 4; | |
277 widget->priv.minh = 3; | |
278 GNTDEBUG; | |
279 } | |
280 | |
281 /****************************************************************************** | |
282 * GntComboBox API | |
283 *****************************************************************************/ | |
284 GType | |
285 gnt_combo_box_get_gtype(void) | |
286 { | |
287 static GType type = 0; | |
288 | |
289 if(type == 0) | |
290 { | |
291 static const GTypeInfo info = { | |
292 sizeof(GntComboBoxClass), | |
293 NULL, /* base_init */ | |
294 NULL, /* base_finalize */ | |
295 (GClassInitFunc)gnt_combo_box_class_init, | |
296 NULL, /* class_finalize */ | |
297 NULL, /* class_data */ | |
298 sizeof(GntComboBox), | |
299 0, /* n_preallocs */ | |
300 gnt_combo_box_init, /* instance_init */ | |
301 NULL /* value_table */ | |
302 }; | |
303 | |
304 type = g_type_register_static(GNT_TYPE_WIDGET, | |
305 "GntComboBox", | |
306 &info, 0); | |
307 } | |
308 | |
309 return type; | |
310 } | |
311 | |
312 GntWidget *gnt_combo_box_new() | |
313 { | |
314 GntWidget *widget = g_object_new(GNT_TYPE_COMBO_BOX, NULL); | |
315 | |
316 return widget; | |
317 } | |
318 | |
319 void gnt_combo_box_add_data(GntComboBox *box, gpointer key, const char *text) | |
320 { | |
321 gnt_tree_add_row_last(GNT_TREE(box->dropdown), key, | |
322 gnt_tree_create_row(GNT_TREE(box->dropdown), text), NULL); | |
323 if (box->selected == NULL) | |
324 set_selection(box, key); | |
325 } | |
326 | |
327 gpointer gnt_combo_box_get_selected_data(GntComboBox *box) | |
328 { | |
329 return box->selected; | |
330 } | |
331 | |
332 void gnt_combo_box_set_selected(GntComboBox *box, gpointer key) | |
333 { | |
334 set_selection(box, key); | |
335 } | |
336 | |
337 void gnt_combo_box_remove(GntComboBox *box, gpointer key) | |
338 { | |
339 gnt_tree_remove(GNT_TREE(box->dropdown), key); | |
340 if (box->selected == key) | |
341 set_selection(box, NULL); | |
342 } | |
343 | |
344 void gnt_combo_box_remove_all(GntComboBox *box) | |
345 { | |
346 gnt_tree_remove_all(GNT_TREE(box->dropdown)); | |
347 set_selection(box, NULL); | |
348 } | |
349 |