1 /*
2  * GNT - The GLib Ncurses Toolkit
3  *
4  * GNT is the legal property of its developers, whose names are too numerous
5  * to list here.  Please refer to the COPYRIGHT file distributed with this
6  * source distribution.
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
21  */
22 
23 #include "gntinternal.h"
24 #include "gntbox.h"
25 #include "gntcombobox.h"
26 #include "gnttree.h"
27 #include "gntmarshal.h"
28 #include "gntstyle.h"
29 #include "gntutils.h"
30 
31 #include <string.h>
32 
33 enum
34 {
35 	SIG_SELECTION_CHANGED,
36 	SIGS,
37 };
38 
39 static GntWidgetClass *parent_class = NULL;
40 static guint signals[SIGS] = { 0 };
41 static void (*widget_lost_focus)(GntWidget *widget);
42 
43 static void
set_selection(GntComboBox * box,gpointer key)44 set_selection(GntComboBox *box, gpointer key)
45 {
46 	if (box->selected != key)
47 	{
48 		/* XXX: make sure the key actually does exist */
49 		gpointer old = box->selected;
50 		box->selected = key;
51 		if (GNT_WIDGET(box)->window)
52 			gnt_widget_draw(GNT_WIDGET(box));
53 		if (box->dropdown)
54 			gnt_tree_set_selected(GNT_TREE(box->dropdown), key);
55 		g_signal_emit(box, signals[SIG_SELECTION_CHANGED], 0, old, key);
56 	}
57 }
58 
59 static void
hide_popup(GntComboBox * box,gboolean set)60 hide_popup(GntComboBox *box, gboolean set)
61 {
62 	gnt_widget_set_size(box->dropdown,
63 		box->dropdown->priv.width - 1, box->dropdown->priv.height);
64 	if (set)
65 		set_selection(box, gnt_tree_get_selection_data(GNT_TREE(box->dropdown)));
66 	else
67 		gnt_tree_set_selected(GNT_TREE(box->dropdown), box->selected);
68 	gnt_widget_hide(box->dropdown->parent);
69 }
70 
71 static void
gnt_combo_box_draw(GntWidget * widget)72 gnt_combo_box_draw(GntWidget *widget)
73 {
74 	GntComboBox *box = GNT_COMBO_BOX(widget);
75 	char *text = NULL, *s;
76 	GntColorType type;
77 	int len;
78 
79 	if (box->dropdown && box->selected)
80 		text = gnt_tree_get_selection_text(GNT_TREE(box->dropdown));
81 
82 	if (text == NULL)
83 		text = g_strdup("");
84 
85 	if (gnt_widget_has_focus(widget))
86 		type = GNT_COLOR_HIGHLIGHT;
87 	else
88 		type = GNT_COLOR_NORMAL;
89 
90 	wbkgdset(widget->window, '\0' | gnt_color_pair(type));
91 
92 	s = (char*)gnt_util_onscreen_width_to_pointer(text, widget->priv.width - 4, &len);
93 	*s = '\0';
94 
95 	mvwaddstr(widget->window, 1, 1, C_(text));
96 	whline(widget->window, ' ' | gnt_color_pair(type), widget->priv.width - 4 - len);
97 	mvwaddch(widget->window, 1, widget->priv.width - 3, ACS_VLINE | gnt_color_pair(GNT_COLOR_NORMAL));
98 	mvwaddch(widget->window, 1, widget->priv.width - 2, ACS_DARROW | gnt_color_pair(GNT_COLOR_NORMAL));
99 	(void)wmove(widget->window, 1, 1);
100 
101 	g_free(text);
102 	GNTDEBUG;
103 }
104 
105 static void
gnt_combo_box_size_request(GntWidget * widget)106 gnt_combo_box_size_request(GntWidget *widget)
107 {
108 	if (!gnt_widget_get_mapped(widget)) {
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 */
112 		widget->priv.width = MAX(10, dd->priv.width + 2);
113 	}
114 }
115 
116 static void
gnt_combo_box_map(GntWidget * widget)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
popup_dropdown(GntComboBox * box)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_set_visible(parent, TRUE);
145 	gnt_widget_draw(parent);
146 }
147 
148 static gboolean
gnt_combo_box_key_pressed(GntWidget * widget,const char * text)149 gnt_combo_box_key_pressed(GntWidget *widget, const char *text)
150 {
151 	GntComboBox *box = GNT_COMBO_BOX(widget);
152 	gboolean showing = gnt_widget_get_mapped(box->dropdown->parent);
153 
154 	if (showing) {
155 		if (text[1] == 0) {
156 			switch (text[0]) {
157 				case '\r':
158 				case '\t':
159 				case '\n':
160 					hide_popup(box, TRUE);
161 					return TRUE;
162 				case 27:
163 					hide_popup(box, FALSE);
164 					return TRUE;
165 			}
166 		}
167 	}
168 
169 	if (gnt_widget_key_pressed(box->dropdown, text)) {
170 		if (!showing)
171 			popup_dropdown(box);
172 		return TRUE;
173 	}
174 
175 	{
176 #define SEARCH_IN_RANGE(start, end) do { \
177 		GntTreeRow *row; \
178 		for (row = start; row != end; \
179 				row = gnt_tree_row_get_next(tree, row)) { \
180 			gpointer key = gnt_tree_row_get_key(tree, row); \
181 			GList *list = gnt_tree_get_row_text_list(tree, key); \
182 			gboolean found = FALSE; \
183 			found = (list->data && g_ascii_strncasecmp(text, list->data, len) == 0); \
184 			g_list_foreach(list, (GFunc)g_free, NULL); \
185 			g_list_free(list); \
186 			if (found) { \
187 				if (!showing) \
188 					popup_dropdown(box); \
189 				gnt_tree_set_selected(tree, key); \
190 				return TRUE; \
191 			} \
192 		} \
193 } while (0)
194 
195 		int len = strlen(text);
196 		GntTree *tree = GNT_TREE(box->dropdown);
197 		GntTreeRow *current = tree->current;
198 
199 		SEARCH_IN_RANGE(gnt_tree_row_get_next(tree, current), NULL);
200 		SEARCH_IN_RANGE(tree->top, current);
201 
202 #undef SEARCH_IN_RANGE
203 	}
204 
205 	return FALSE;
206 }
207 
208 static void
gnt_combo_box_destroy(GntWidget * widget)209 gnt_combo_box_destroy(GntWidget *widget)
210 {
211 	gnt_widget_destroy(GNT_COMBO_BOX(widget)->dropdown->parent);
212 }
213 
214 static void
gnt_combo_box_lost_focus(GntWidget * widget)215 gnt_combo_box_lost_focus(GntWidget *widget)
216 {
217 	GntComboBox *combo = GNT_COMBO_BOX(widget);
218 	if (gnt_widget_get_mapped(combo->dropdown->parent))
219 		hide_popup(combo, FALSE);
220 	widget_lost_focus(widget);
221 }
222 
223 static gboolean
gnt_combo_box_clicked(GntWidget * widget,GntMouseEvent event,int x,int y)224 gnt_combo_box_clicked(GntWidget *widget, GntMouseEvent event, int x, int y)
225 {
226 	GntComboBox *box = GNT_COMBO_BOX(widget);
227 	gboolean dshowing = gnt_widget_get_mapped(box->dropdown->parent);
228 
229 	if (event == GNT_MOUSE_SCROLL_UP) {
230 		if (dshowing)
231 			gnt_widget_key_pressed(box->dropdown, GNT_KEY_UP);
232 	} else if (event == GNT_MOUSE_SCROLL_DOWN) {
233 		if (dshowing)
234 			gnt_widget_key_pressed(box->dropdown, GNT_KEY_DOWN);
235 	} else if (event == GNT_LEFT_MOUSE_DOWN) {
236 		if (dshowing) {
237 			hide_popup(box, TRUE);
238 		} else {
239 			popup_dropdown(GNT_COMBO_BOX(widget));
240 		}
241 	} else
242 		return FALSE;
243 	return TRUE;
244 }
245 
246 static void
gnt_combo_box_size_changed(GntWidget * widget,int oldw,int oldh)247 gnt_combo_box_size_changed(GntWidget *widget, int oldw, int oldh)
248 {
249 	GntComboBox *box = GNT_COMBO_BOX(widget);
250 	gnt_widget_set_size(box->dropdown, widget->priv.width - 1, box->dropdown->priv.height);
251 }
252 
253 static gboolean
dropdown_menu(GntBindable * b,GList * null)254 dropdown_menu(GntBindable *b, GList *null)
255 {
256 	if (gnt_widget_get_mapped(GNT_COMBO_BOX(b)->dropdown->parent))
257 		return FALSE;
258 	popup_dropdown(GNT_COMBO_BOX(b));
259 	return TRUE;
260 }
261 
262 static void
gnt_combo_box_class_init(GntComboBoxClass * klass)263 gnt_combo_box_class_init(GntComboBoxClass *klass)
264 {
265 	GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass);
266 
267 	parent_class = GNT_WIDGET_CLASS(klass);
268 
269 	parent_class->destroy = gnt_combo_box_destroy;
270 	parent_class->draw = gnt_combo_box_draw;
271 	parent_class->map = gnt_combo_box_map;
272 	parent_class->size_request = gnt_combo_box_size_request;
273 	parent_class->key_pressed = gnt_combo_box_key_pressed;
274 	parent_class->clicked = gnt_combo_box_clicked;
275 	parent_class->size_changed = gnt_combo_box_size_changed;
276 
277 	widget_lost_focus = parent_class->lost_focus;
278 	parent_class->lost_focus = gnt_combo_box_lost_focus;
279 
280 	signals[SIG_SELECTION_CHANGED] =
281 		g_signal_new("selection-changed",
282 					 G_TYPE_FROM_CLASS(klass),
283 					 G_SIGNAL_RUN_LAST,
284 					 0,
285 					 NULL, NULL,
286 					 gnt_closure_marshal_VOID__POINTER_POINTER,
287 					 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
288 
289 	gnt_bindable_class_register_action(bindable, "dropdown", dropdown_menu,
290 			GNT_KEY_DOWN, NULL);
291 	gnt_bindable_register_binding(bindable, "dropdown", GNT_KEY_UP, NULL);
292 
293 	gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable);
294 
295 	GNTDEBUG;
296 }
297 
298 static void
gnt_combo_box_init(GTypeInstance * instance,gpointer class)299 gnt_combo_box_init(GTypeInstance *instance, gpointer class)
300 {
301 	GntWidget *box;
302 	GntWidget *widget = GNT_WIDGET(instance);
303 	GntComboBox *combo = GNT_COMBO_BOX(instance);
304 
305 	gnt_widget_set_grow_x(widget, TRUE);
306 	gnt_widget_set_take_focus(widget, TRUE);
307 	gnt_widget_set_has_shadow(widget, FALSE);
308 	combo->dropdown = gnt_tree_new();
309 
310 	box = gnt_box_new(FALSE, FALSE);
311 	gnt_widget_set_has_shadow(box, FALSE);
312 	gnt_widget_set_has_border(box, FALSE);
313 	gnt_widget_set_transient(box, TRUE);
314 	gnt_box_set_pad(GNT_BOX(box), 0);
315 	gnt_box_add_widget(GNT_BOX(box), combo->dropdown);
316 
317 	widget->priv.minw = 4;
318 	widget->priv.minh = 3;
319 	GNTDEBUG;
320 }
321 
322 /******************************************************************************
323  * GntComboBox API
324  *****************************************************************************/
325 GType
gnt_combo_box_get_gtype(void)326 gnt_combo_box_get_gtype(void)
327 {
328 	static GType type = 0;
329 
330 	if(type == 0)
331 	{
332 		static const GTypeInfo info = {
333 			sizeof(GntComboBoxClass),
334 			NULL,					/* base_init		*/
335 			NULL,					/* base_finalize	*/
336 			(GClassInitFunc)gnt_combo_box_class_init,
337 			NULL,					/* class_finalize	*/
338 			NULL,					/* class_data		*/
339 			sizeof(GntComboBox),
340 			0,						/* n_preallocs		*/
341 			gnt_combo_box_init,			/* instance_init	*/
342 			NULL					/* value_table		*/
343 		};
344 
345 		type = g_type_register_static(GNT_TYPE_WIDGET,
346 									  "GntComboBox",
347 									  &info, 0);
348 	}
349 
350 	return type;
351 }
352 
gnt_combo_box_new()353 GntWidget *gnt_combo_box_new()
354 {
355 	GntWidget *widget = g_object_new(GNT_TYPE_COMBO_BOX, NULL);
356 
357 	return widget;
358 }
359 
360 GntWidget *
gnt_combo_box_get_dropdown(GntComboBox * box)361 gnt_combo_box_get_dropdown(GntComboBox *box)
362 {
363 	g_return_val_if_fail(GNT_IS_COMBO_BOX(box), NULL);
364 
365 	return box->dropdown;
366 }
367 
gnt_combo_box_add_data(GntComboBox * box,gpointer key,const char * text)368 void gnt_combo_box_add_data(GntComboBox *box, gpointer key, const char *text)
369 {
370 	gnt_tree_add_row_last(GNT_TREE(box->dropdown), key,
371 			gnt_tree_create_row(GNT_TREE(box->dropdown), text), NULL);
372 	if (box->selected == NULL)
373 		set_selection(box, key);
374 }
375 
gnt_combo_box_get_selected_data(GntComboBox * box)376 gpointer gnt_combo_box_get_selected_data(GntComboBox *box)
377 {
378 	return box->selected;
379 }
380 
gnt_combo_box_set_selected(GntComboBox * box,gpointer key)381 void gnt_combo_box_set_selected(GntComboBox *box, gpointer key)
382 {
383 	set_selection(box, key);
384 }
385 
gnt_combo_box_remove(GntComboBox * box,gpointer key)386 void gnt_combo_box_remove(GntComboBox *box, gpointer key)
387 {
388 	gnt_tree_remove(GNT_TREE(box->dropdown), key);
389 	if (box->selected == key)
390 		set_selection(box, NULL);
391 }
392 
gnt_combo_box_remove_all(GntComboBox * box)393 void gnt_combo_box_remove_all(GntComboBox *box)
394 {
395 	gnt_tree_remove_all(GNT_TREE(box->dropdown));
396 	set_selection(box, NULL);
397 }
398