Mercurial > pidgin.yaz
comparison console/libgnt/gntcombobox.c @ 13935:cd2da4b079cf
[gaim-migrate @ 16466]
New widget GntComboBox. I have addde a test file as an example as well.
Rename gntutils.* to gntmarshal.*
I am going to have some util-functions in gntutils.* later.
committer: Tailor Script <tailor@pidgin.im>
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sat, 08 Jul 2006 23:58:20 +0000 |
parents | |
children | 669898e6aa11 |
comparison
equal
deleted
inserted
replaced
13934:ef0d515b9f97 | 13935:cd2da4b079cf |
---|---|
1 #include "gntbox.h" | |
2 #include "gntcombobox.h" | |
3 #include "gnttree.h" | |
4 #include "gntmarshal.h" | |
5 | |
6 #include <string.h> | |
7 | |
8 enum | |
9 { | |
10 SIG_SELECTION_CHANGED, | |
11 SIGS, | |
12 }; | |
13 | |
14 static GntWidgetClass *parent_class = NULL; | |
15 static guint signals[SIGS] = { 0 }; | |
16 static void (*widget_lost_focus)(GntWidget *widget); | |
17 | |
18 static void | |
19 set_selection(GntComboBox *box, gpointer key) | |
20 { | |
21 if (box->selected != key) | |
22 { | |
23 gpointer old = box->selected; | |
24 box->selected = key; | |
25 g_signal_emit(box, signals[SIG_SELECTION_CHANGED], 0, old, key); | |
26 gnt_widget_draw(GNT_WIDGET(box)); | |
27 } | |
28 } | |
29 | |
30 static void | |
31 gnt_combo_box_draw(GntWidget *widget) | |
32 { | |
33 GntComboBox *box = GNT_COMBO_BOX(widget); | |
34 const char *text = NULL; | |
35 GntColorType type; | |
36 | |
37 if (box->dropdown) | |
38 { | |
39 text = gnt_tree_get_selection_text(GNT_TREE(box->dropdown)); | |
40 box->selected = gnt_tree_get_selection_data(GNT_TREE(box->dropdown)); | |
41 } | |
42 | |
43 if (text == NULL) | |
44 text = ""; | |
45 | |
46 if (gnt_widget_has_focus(widget)) | |
47 type = GNT_COLOR_HIGHLIGHT; | |
48 else | |
49 type = GNT_COLOR_NORMAL; | |
50 | |
51 wbkgdset(widget->window, '\0' | COLOR_PAIR(type)); | |
52 mvwprintw(widget->window, 1, 1, text); | |
53 | |
54 DEBUG; | |
55 } | |
56 | |
57 static void | |
58 gnt_combo_box_size_request(GntWidget *widget) | |
59 { | |
60 widget->priv.height = 3; /* For now, a combobox will have border */ | |
61 widget->priv.width = 15; | |
62 } | |
63 | |
64 static void | |
65 gnt_combo_box_map(GntWidget *widget) | |
66 { | |
67 if (widget->priv.width == 0 || widget->priv.height == 0) | |
68 gnt_widget_size_request(widget); | |
69 DEBUG; | |
70 } | |
71 | |
72 static gboolean | |
73 gnt_combo_box_key_pressed(GntWidget *widget, const char *text) | |
74 { | |
75 GntComboBox *box = GNT_COMBO_BOX(widget); | |
76 if (GNT_WIDGET_IS_FLAG_SET(box->dropdown->parent, GNT_WIDGET_MAPPED)) | |
77 { | |
78 if (text[1] == 0) | |
79 { | |
80 switch (text[0]) | |
81 { | |
82 case '\r': | |
83 case '\t': | |
84 /* XXX: Get the selction */ | |
85 set_selection(box, gnt_tree_get_selection_data(GNT_TREE(box->dropdown))); | |
86 case 27: | |
87 gnt_widget_hide(box->dropdown->parent); | |
88 return TRUE; | |
89 break; | |
90 } | |
91 } | |
92 if (gnt_widget_key_pressed(box->dropdown, text)) | |
93 return TRUE; | |
94 } | |
95 else | |
96 { | |
97 if (text[0] == 27) | |
98 { | |
99 if (strcmp(text + 1, GNT_KEY_UP) == 0 || | |
100 strcmp(text + 1, GNT_KEY_DOWN) == 0) | |
101 { | |
102 gnt_widget_set_size(box->dropdown, widget->priv.width, 9); | |
103 gnt_widget_set_position(box->dropdown->parent, | |
104 widget->priv.x, widget->priv.y + widget->priv.height - 1); | |
105 gnt_widget_draw(box->dropdown->parent); | |
106 return TRUE; | |
107 } | |
108 } | |
109 } | |
110 | |
111 return FALSE; | |
112 } | |
113 | |
114 static void | |
115 gnt_combo_box_destroy(GntWidget *widget) | |
116 { | |
117 gnt_widget_destroy(GNT_COMBO_BOX(widget)->dropdown->parent); | |
118 } | |
119 | |
120 static void | |
121 gnt_combo_box_lost_focus(GntWidget *widget) | |
122 { | |
123 GntComboBox *combo = GNT_COMBO_BOX(widget); | |
124 if (GNT_WIDGET_IS_FLAG_SET(combo->dropdown->parent, GNT_WIDGET_MAPPED)) | |
125 gnt_widget_hide(GNT_COMBO_BOX(widget)->dropdown->parent); | |
126 widget_lost_focus(widget); | |
127 } | |
128 | |
129 static void | |
130 gnt_combo_box_class_init(GntComboBoxClass *klass) | |
131 { | |
132 parent_class = GNT_WIDGET_CLASS(klass); | |
133 | |
134 parent_class->destroy = gnt_combo_box_destroy; | |
135 parent_class->draw = gnt_combo_box_draw; | |
136 parent_class->map = gnt_combo_box_map; | |
137 parent_class->size_request = gnt_combo_box_size_request; | |
138 parent_class->key_pressed = gnt_combo_box_key_pressed; | |
139 | |
140 widget_lost_focus = parent_class->lost_focus; | |
141 parent_class->lost_focus = gnt_combo_box_lost_focus; | |
142 | |
143 signals[SIG_SELECTION_CHANGED] = | |
144 g_signal_new("selection-changed", | |
145 G_TYPE_FROM_CLASS(klass), | |
146 G_SIGNAL_RUN_LAST, | |
147 0, | |
148 NULL, NULL, | |
149 gnt_closure_marshal_VOID__POINTER_POINTER, | |
150 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER); | |
151 | |
152 DEBUG; | |
153 } | |
154 | |
155 static void | |
156 gnt_combo_box_init(GTypeInstance *instance, gpointer class) | |
157 { | |
158 GntWidget *box; | |
159 GntComboBox *combo = GNT_COMBO_BOX(instance); | |
160 | |
161 GNT_WIDGET_SET_FLAGS(GNT_WIDGET(instance), | |
162 GNT_WIDGET_GROW_X | GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_NO_SHADOW); | |
163 combo->dropdown = gnt_tree_new(); | |
164 | |
165 box = gnt_box_new(FALSE, FALSE); | |
166 GNT_WIDGET_SET_FLAGS(box, GNT_WIDGET_NO_SHADOW | GNT_WIDGET_NO_BORDER); | |
167 gnt_box_add_widget(GNT_BOX(box), combo->dropdown); | |
168 | |
169 DEBUG; | |
170 } | |
171 | |
172 /****************************************************************************** | |
173 * GntComboBox API | |
174 *****************************************************************************/ | |
175 GType | |
176 gnt_combo_box_get_gtype(void) | |
177 { | |
178 static GType type = 0; | |
179 | |
180 if(type == 0) | |
181 { | |
182 static const GTypeInfo info = { | |
183 sizeof(GntComboBoxClass), | |
184 NULL, /* base_init */ | |
185 NULL, /* base_finalize */ | |
186 (GClassInitFunc)gnt_combo_box_class_init, | |
187 NULL, /* class_finalize */ | |
188 NULL, /* class_data */ | |
189 sizeof(GntComboBox), | |
190 0, /* n_preallocs */ | |
191 gnt_combo_box_init, /* instance_init */ | |
192 }; | |
193 | |
194 type = g_type_register_static(GNT_TYPE_WIDGET, | |
195 "GntComboBox", | |
196 &info, 0); | |
197 } | |
198 | |
199 return type; | |
200 } | |
201 | |
202 GntWidget *gnt_combo_box_new() | |
203 { | |
204 GntWidget *widget = g_object_new(GNT_TYPE_COMBO_BOX, NULL); | |
205 | |
206 return widget; | |
207 } | |
208 | |
209 void gnt_combo_box_add_data(GntComboBox *box, gpointer key, const char *text) | |
210 { | |
211 gnt_tree_add_row_after(GNT_TREE(box->dropdown), key, text, NULL, NULL); | |
212 if (box->selected == NULL) | |
213 set_selection(box, key); | |
214 } | |
215 | |
216 gpointer gnt_combo_box_get_selected_data(GntComboBox *box) | |
217 { | |
218 return box->selected; | |
219 } | |
220 |