d9b15094 |
1 | /* |
2 | * gtkdlg.c - GTK implementation of the PuTTY configuration box. |
3 | */ |
4 | |
1d0d4a3b |
5 | /* |
1c291fcf |
6 | * TODO when porting to GTK 2.0: |
7 | * |
8 | * - GtkTree is apparently deprecated and we should switch to |
9 | * GtkTreeView instead. |
10 | * - GtkLabel has a built-in mnemonic scheme, so we should at |
11 | * least consider switching to that from the current adhockery. |
1d0d4a3b |
12 | */ |
13 | |
d9b15094 |
14 | #include <assert.h> |
1c291fcf |
15 | #include <ctype.h> |
d9b15094 |
16 | #include <gtk/gtk.h> |
1c291fcf |
17 | #include <gdk/gdkkeysyms.h> |
0bd8d76d |
18 | #include <gdk/gdkx.h> |
19 | #include <X11/Xlib.h> |
20 | #include <X11/Xutil.h> |
d9b15094 |
21 | |
22 | #include "gtkcols.h" |
23 | #include "gtkpanel.h" |
24 | |
25 | #ifdef TESTMODE |
26 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
27 | #endif |
28 | |
29 | #include "putty.h" |
30 | #include "dialog.h" |
0f5e1f75 |
31 | #include "tree234.h" |
32 | |
1c291fcf |
33 | struct Shortcut { |
34 | GtkWidget *widget; |
35 | struct uctrl *uc; |
36 | int action; |
37 | }; |
38 | |
39 | struct Shortcuts { |
40 | struct Shortcut sc[128]; |
41 | }; |
42 | |
0f5e1f75 |
43 | struct uctrl { |
44 | union control *ctrl; |
45 | GtkWidget *toplevel; |
46 | void *privdata; |
47 | int privdata_needs_free; |
48 | GtkWidget **buttons; int nbuttons; /* for radio buttons */ |
49 | GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */ |
1c291fcf |
50 | GtkWidget *button; /* for filesel, fontsel */ |
0f5e1f75 |
51 | GtkWidget *list; /* for combobox, listbox */ |
52 | GtkWidget *menu; /* for optionmenu (==droplist) */ |
53 | GtkWidget *optmenu; /* also for optionmenu */ |
54 | GtkWidget *text; /* for text */ |
0bd8d76d |
55 | GtkAdjustment *adj; /* for the scrollbar in a list box */ |
c5d64b49 |
56 | guint textsig; |
0f5e1f75 |
57 | }; |
58 | |
59 | struct dlgparam { |
60 | tree234 *byctrl, *bywidget; |
61 | void *data; |
62 | struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */ |
63 | /* `flags' are set to indicate when a GTK signal handler is being called |
64 | * due to automatic processing and should not flag a user event. */ |
65 | int flags; |
1c291fcf |
66 | struct Shortcuts *shortcuts; |
23897ed0 |
67 | GtkWidget *window, *cancelbutton, *currtreeitem, **treeitems; |
0bd8d76d |
68 | union control *currfocus, *lastfocus; |
69 | int ntreeitems; |
70 | int retval; |
0f5e1f75 |
71 | }; |
72 | #define FLAG_UPDATING_COMBO_LIST 1 |
73 | |
1c291fcf |
74 | enum { /* values for Shortcut.action */ |
75 | SHORTCUT_EMPTY, /* no shortcut on this key */ |
c5d64b49 |
76 | SHORTCUT_TREE, /* focus a tree item */ |
1c291fcf |
77 | SHORTCUT_FOCUS, /* focus the supplied widget */ |
78 | SHORTCUT_UCTRL, /* do something sane with uctrl */ |
79 | SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */ |
80 | SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */ |
81 | }; |
82 | |
0f5e1f75 |
83 | /* |
84 | * Forward references. |
85 | */ |
0bd8d76d |
86 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
87 | gpointer data); |
1c291fcf |
88 | static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
89 | int chr, int action, void *ptr); |
0bd8d76d |
90 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
91 | gpointer data); |
92 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
93 | gpointer data); |
94 | static int listitem_button(GtkWidget *item, GdkEventButton *event, |
0f5e1f75 |
95 | gpointer data); |
96 | static void menuitem_activate(GtkMenuItem *item, gpointer data); |
97 | static void coloursel_ok(GtkButton *button, gpointer data); |
98 | static void coloursel_cancel(GtkButton *button, gpointer data); |
0bd8d76d |
99 | static void window_destroy(GtkWidget *widget, gpointer data); |
0f5e1f75 |
100 | |
101 | static int uctrl_cmp_byctrl(void *av, void *bv) |
102 | { |
103 | struct uctrl *a = (struct uctrl *)av; |
104 | struct uctrl *b = (struct uctrl *)bv; |
105 | if (a->ctrl < b->ctrl) |
106 | return -1; |
107 | else if (a->ctrl > b->ctrl) |
108 | return +1; |
109 | return 0; |
110 | } |
111 | |
112 | static int uctrl_cmp_byctrl_find(void *av, void *bv) |
113 | { |
114 | union control *a = (union control *)av; |
115 | struct uctrl *b = (struct uctrl *)bv; |
116 | if (a < b->ctrl) |
117 | return -1; |
118 | else if (a > b->ctrl) |
119 | return +1; |
120 | return 0; |
121 | } |
122 | |
123 | static int uctrl_cmp_bywidget(void *av, void *bv) |
124 | { |
125 | struct uctrl *a = (struct uctrl *)av; |
126 | struct uctrl *b = (struct uctrl *)bv; |
127 | if (a->toplevel < b->toplevel) |
128 | return -1; |
129 | else if (a->toplevel > b->toplevel) |
130 | return +1; |
131 | return 0; |
132 | } |
133 | |
134 | static int uctrl_cmp_bywidget_find(void *av, void *bv) |
135 | { |
136 | GtkWidget *a = (GtkWidget *)av; |
137 | struct uctrl *b = (struct uctrl *)bv; |
138 | if (a < b->toplevel) |
139 | return -1; |
140 | else if (a > b->toplevel) |
141 | return +1; |
142 | return 0; |
143 | } |
144 | |
145 | static void dlg_init(struct dlgparam *dp) |
146 | { |
147 | dp->byctrl = newtree234(uctrl_cmp_byctrl); |
148 | dp->bywidget = newtree234(uctrl_cmp_bywidget); |
149 | dp->coloursel_result.ok = FALSE; |
150 | } |
151 | |
152 | static void dlg_cleanup(struct dlgparam *dp) |
153 | { |
154 | struct uctrl *uc; |
155 | |
156 | freetree234(dp->byctrl); /* doesn't free the uctrls inside */ |
157 | while ( (uc = index234(dp->bywidget, 0)) != NULL) { |
158 | del234(dp->bywidget, uc); |
159 | if (uc->privdata_needs_free) |
160 | sfree(uc->privdata); |
161 | sfree(uc->buttons); |
162 | sfree(uc); |
163 | } |
164 | freetree234(dp->bywidget); |
0bd8d76d |
165 | sfree(dp->treeitems); |
0f5e1f75 |
166 | } |
167 | |
168 | static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc) |
169 | { |
170 | add234(dp->byctrl, uc); |
171 | add234(dp->bywidget, uc); |
172 | } |
173 | |
174 | static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl) |
175 | { |
176 | return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find); |
177 | } |
178 | |
179 | static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w) |
180 | { |
181 | struct uctrl *ret = NULL; |
182 | do { |
183 | ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find); |
184 | if (ret) |
185 | return ret; |
186 | w = w->parent; |
187 | } while (w); |
188 | return ret; |
189 | } |
d9b15094 |
190 | |
d9b15094 |
191 | void *dlg_get_privdata(union control *ctrl, void *dlg) |
192 | { |
0f5e1f75 |
193 | struct dlgparam *dp = (struct dlgparam *)dlg; |
194 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
195 | return uc->privdata; |
d9b15094 |
196 | } |
197 | |
198 | void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) |
199 | { |
0f5e1f75 |
200 | struct dlgparam *dp = (struct dlgparam *)dlg; |
201 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
202 | uc->privdata = ptr; |
203 | uc->privdata_needs_free = FALSE; |
d9b15094 |
204 | } |
205 | |
206 | void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) |
207 | { |
0f5e1f75 |
208 | struct dlgparam *dp = (struct dlgparam *)dlg; |
209 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
3d88e64d |
210 | /* |
211 | * This is an internal allocation routine, so it's allowed to |
212 | * use smalloc directly. |
213 | */ |
0f5e1f75 |
214 | uc->privdata = smalloc(size); |
215 | uc->privdata_needs_free = FALSE; |
216 | return uc->privdata; |
d9b15094 |
217 | } |
218 | |
0bd8d76d |
219 | union control *dlg_last_focused(union control *ctrl, void *dlg) |
d9b15094 |
220 | { |
0f5e1f75 |
221 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
222 | if (dp->currfocus != ctrl) |
223 | return dp->currfocus; |
224 | else |
225 | return dp->lastfocus; |
d9b15094 |
226 | } |
227 | |
0f5e1f75 |
228 | void dlg_radiobutton_set(union control *ctrl, void *dlg, int which) |
d9b15094 |
229 | { |
0f5e1f75 |
230 | struct dlgparam *dp = (struct dlgparam *)dlg; |
231 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
232 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
233 | assert(uc->buttons != NULL); |
234 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE); |
d9b15094 |
235 | } |
236 | |
237 | int dlg_radiobutton_get(union control *ctrl, void *dlg) |
238 | { |
0f5e1f75 |
239 | struct dlgparam *dp = (struct dlgparam *)dlg; |
240 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
241 | int i; |
242 | |
243 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
244 | assert(uc->buttons != NULL); |
245 | for (i = 0; i < uc->nbuttons; i++) |
246 | if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i]))) |
247 | return i; |
248 | return 0; /* got to return something */ |
d9b15094 |
249 | } |
250 | |
251 | void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) |
252 | { |
0f5e1f75 |
253 | struct dlgparam *dp = (struct dlgparam *)dlg; |
254 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
255 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
256 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked); |
d9b15094 |
257 | } |
258 | |
259 | int dlg_checkbox_get(union control *ctrl, void *dlg) |
260 | { |
0f5e1f75 |
261 | struct dlgparam *dp = (struct dlgparam *)dlg; |
262 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
263 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
264 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel)); |
d9b15094 |
265 | } |
266 | |
267 | void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) |
268 | { |
0f5e1f75 |
269 | struct dlgparam *dp = (struct dlgparam *)dlg; |
270 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
271 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
272 | assert(uc->entry != NULL); |
273 | gtk_entry_set_text(GTK_ENTRY(uc->entry), text); |
d9b15094 |
274 | } |
275 | |
276 | void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) |
277 | { |
0f5e1f75 |
278 | struct dlgparam *dp = (struct dlgparam *)dlg; |
279 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
280 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
281 | assert(uc->entry != NULL); |
282 | strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
283 | length); |
284 | buffer[length-1] = '\0'; |
285 | } |
286 | |
287 | static void container_remove_and_destroy(GtkWidget *w, gpointer data) |
288 | { |
289 | GtkContainer *cont = GTK_CONTAINER(data); |
290 | /* gtk_container_remove will unref the widget for us; we need not. */ |
291 | gtk_container_remove(cont, w); |
d9b15094 |
292 | } |
293 | |
294 | /* The `listbox' functions can also apply to combo boxes. */ |
295 | void dlg_listbox_clear(union control *ctrl, void *dlg) |
296 | { |
0f5e1f75 |
297 | struct dlgparam *dp = (struct dlgparam *)dlg; |
298 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
299 | GtkContainer *cont; |
300 | |
301 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
302 | uc->ctrl->generic.type == CTRL_LISTBOX); |
303 | assert(uc->menu != NULL || uc->list != NULL); |
304 | |
305 | cont = (uc->menu ? GTK_CONTAINER(uc->menu) : GTK_CONTAINER(uc->list)); |
306 | |
307 | gtk_container_foreach(cont, container_remove_and_destroy, cont); |
d9b15094 |
308 | } |
309 | |
310 | void dlg_listbox_del(union control *ctrl, void *dlg, int index) |
311 | { |
0f5e1f75 |
312 | struct dlgparam *dp = (struct dlgparam *)dlg; |
313 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
314 | |
315 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
316 | uc->ctrl->generic.type == CTRL_LISTBOX); |
317 | assert(uc->menu != NULL || uc->list != NULL); |
318 | |
319 | if (uc->menu) { |
320 | gtk_container_remove |
321 | (GTK_CONTAINER(uc->menu), |
322 | g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index)); |
323 | } else { |
324 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
325 | } |
d9b15094 |
326 | } |
327 | |
328 | void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) |
329 | { |
4eaff8d4 |
330 | dlg_listbox_addwithid(ctrl, dlg, text, 0); |
d9b15094 |
331 | } |
332 | |
333 | /* |
334 | * Each listbox entry may have a numeric id associated with it. |
335 | * Note that some front ends only permit a string to be stored at |
336 | * each position, which means that _if_ you put two identical |
337 | * strings in any listbox then you MUST not assign them different |
338 | * IDs and expect to get meaningful results back. |
339 | */ |
4eaff8d4 |
340 | void dlg_listbox_addwithid(union control *ctrl, void *dlg, |
341 | char const *text, int id) |
d9b15094 |
342 | { |
0f5e1f75 |
343 | struct dlgparam *dp = (struct dlgparam *)dlg; |
344 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
345 | |
346 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
347 | uc->ctrl->generic.type == CTRL_LISTBOX); |
348 | assert(uc->menu != NULL || uc->list != NULL); |
349 | |
350 | dp->flags |= FLAG_UPDATING_COMBO_LIST; |
351 | |
352 | if (uc->menu) { |
353 | /* |
354 | * List item in a drop-down (but non-combo) list. Tabs are |
355 | * ignored; we just provide a standard menu item with the |
356 | * text. |
357 | */ |
358 | GtkWidget *menuitem = gtk_menu_item_new_with_label(text); |
359 | |
360 | gtk_container_add(GTK_CONTAINER(uc->menu), menuitem); |
361 | gtk_widget_show(menuitem); |
362 | |
363 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", (gpointer)id); |
364 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
365 | GTK_SIGNAL_FUNC(menuitem_activate), dp); |
366 | } else if (!uc->entry) { |
367 | /* |
368 | * List item in a non-combo-box list box. We make all of |
369 | * these Columns containing GtkLabels. This allows us to do |
370 | * the nasty force_left hack irrespective of whether there |
371 | * are tabs in the thing. |
372 | */ |
373 | GtkWidget *listitem = gtk_list_item_new(); |
523fd1da |
374 | GtkWidget *cols = columns_new(10); |
0f5e1f75 |
375 | gint *percents; |
376 | int i, ncols; |
377 | |
378 | /* Count the tabs in the text, and hence determine # of columns. */ |
379 | ncols = 1; |
380 | for (i = 0; text[i]; i++) |
381 | if (text[i] == '\t') |
382 | ncols++; |
383 | |
384 | assert(ncols <= |
385 | (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1)); |
3d88e64d |
386 | percents = snewn(ncols, gint); |
0f5e1f75 |
387 | percents[ncols-1] = 100; |
388 | for (i = 0; i < ncols-1; i++) { |
389 | percents[i] = uc->ctrl->listbox.percentages[i]; |
390 | percents[ncols-1] -= percents[i]; |
391 | } |
392 | columns_set_cols(COLUMNS(cols), ncols, percents); |
393 | sfree(percents); |
394 | |
395 | for (i = 0; i < ncols; i++) { |
396 | int len = strcspn(text, "\t"); |
397 | char *dup = dupprintf("%.*s", len, text); |
398 | GtkWidget *label; |
399 | |
400 | text += len; |
401 | if (*text) text++; |
402 | label = gtk_label_new(dup); |
403 | sfree(dup); |
404 | |
405 | columns_add(COLUMNS(cols), label, i, 1); |
406 | columns_force_left_align(COLUMNS(cols), label); |
407 | gtk_widget_show(label); |
408 | } |
409 | gtk_container_add(GTK_CONTAINER(listitem), cols); |
410 | gtk_widget_show(cols); |
411 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
412 | gtk_widget_show(listitem); |
413 | |
0bd8d76d |
414 | if (ctrl->listbox.multisel) { |
415 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
416 | GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj); |
417 | } else { |
418 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
419 | GTK_SIGNAL_FUNC(listitem_single_key), uc->adj); |
420 | } |
421 | gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event", |
422 | GTK_SIGNAL_FUNC(widget_focus), dp); |
0f5e1f75 |
423 | gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event", |
424 | GTK_SIGNAL_FUNC(listitem_button), dp); |
425 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
426 | } else { |
427 | /* |
428 | * List item in a combo-box list, which means the sensible |
429 | * thing to do is make it a perfectly normal label. Hence |
430 | * tabs are disregarded. |
431 | */ |
432 | GtkWidget *listitem = gtk_list_item_new_with_label(text); |
433 | |
434 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
435 | gtk_widget_show(listitem); |
436 | |
437 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
438 | } |
439 | |
440 | dp->flags &= ~FLAG_UPDATING_COMBO_LIST; |
d9b15094 |
441 | } |
442 | |
443 | int dlg_listbox_getid(union control *ctrl, void *dlg, int index) |
444 | { |
0f5e1f75 |
445 | struct dlgparam *dp = (struct dlgparam *)dlg; |
446 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
447 | GList *children; |
448 | GtkObject *item; |
449 | |
450 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
451 | uc->ctrl->generic.type == CTRL_LISTBOX); |
452 | assert(uc->menu != NULL || uc->list != NULL); |
453 | |
454 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
455 | uc->list)); |
456 | item = GTK_OBJECT(g_list_nth_data(children, index)); |
0bd8d76d |
457 | g_list_free(children); |
0f5e1f75 |
458 | |
459 | return (int)gtk_object_get_data(GTK_OBJECT(item), "user-data"); |
d9b15094 |
460 | } |
461 | |
462 | /* dlg_listbox_index returns <0 if no single element is selected. */ |
463 | int dlg_listbox_index(union control *ctrl, void *dlg) |
464 | { |
0f5e1f75 |
465 | struct dlgparam *dp = (struct dlgparam *)dlg; |
466 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
467 | GList *children; |
468 | GtkWidget *item, *activeitem; |
469 | int i; |
470 | int selected = -1; |
471 | |
472 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
473 | uc->ctrl->generic.type == CTRL_LISTBOX); |
474 | assert(uc->menu != NULL || uc->list != NULL); |
475 | |
476 | if (uc->menu) |
477 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
478 | |
479 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
480 | uc->list)); |
481 | for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL; |
482 | i++, children = children->next) { |
483 | if (uc->menu ? activeitem == item : |
484 | GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) { |
0bd8d76d |
485 | if (selected == -1) |
0f5e1f75 |
486 | selected = i; |
487 | else |
0bd8d76d |
488 | selected = -2; |
0f5e1f75 |
489 | } |
490 | } |
0bd8d76d |
491 | g_list_free(children); |
492 | return selected < 0 ? -1 : selected; |
d9b15094 |
493 | } |
494 | |
495 | int dlg_listbox_issel(union control *ctrl, void *dlg, int index) |
496 | { |
0f5e1f75 |
497 | struct dlgparam *dp = (struct dlgparam *)dlg; |
498 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
499 | GList *children; |
500 | GtkWidget *item, *activeitem; |
501 | |
502 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
503 | uc->ctrl->generic.type == CTRL_LISTBOX); |
504 | assert(uc->menu != NULL || uc->list != NULL); |
505 | |
506 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
507 | uc->list)); |
508 | item = GTK_WIDGET(g_list_nth_data(children, index)); |
0bd8d76d |
509 | g_list_free(children); |
0f5e1f75 |
510 | |
511 | if (uc->menu) { |
512 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
513 | return item == activeitem; |
514 | } else { |
515 | return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED; |
516 | } |
d9b15094 |
517 | } |
518 | |
519 | void dlg_listbox_select(union control *ctrl, void *dlg, int index) |
520 | { |
0f5e1f75 |
521 | struct dlgparam *dp = (struct dlgparam *)dlg; |
522 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
523 | |
524 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
525 | uc->ctrl->generic.type == CTRL_LISTBOX); |
526 | assert(uc->optmenu != NULL || uc->list != NULL); |
527 | |
528 | if (uc->optmenu) { |
529 | gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index); |
530 | } else { |
531 | gtk_list_select_item(GTK_LIST(uc->list), index); |
532 | } |
d9b15094 |
533 | } |
534 | |
535 | void dlg_text_set(union control *ctrl, void *dlg, char const *text) |
536 | { |
0f5e1f75 |
537 | struct dlgparam *dp = (struct dlgparam *)dlg; |
538 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
539 | |
540 | assert(uc->ctrl->generic.type == CTRL_TEXT); |
541 | assert(uc->text != NULL); |
542 | |
543 | gtk_label_set_text(GTK_LABEL(uc->text), text); |
d9b15094 |
544 | } |
545 | |
546 | void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn) |
547 | { |
0f5e1f75 |
548 | struct dlgparam *dp = (struct dlgparam *)dlg; |
549 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
550 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
551 | assert(uc->entry != NULL); |
552 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path); |
d9b15094 |
553 | } |
554 | |
555 | void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn) |
556 | { |
0f5e1f75 |
557 | struct dlgparam *dp = (struct dlgparam *)dlg; |
558 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
559 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
560 | assert(uc->entry != NULL); |
561 | strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
562 | lenof(fn->path)); |
563 | fn->path[lenof(fn->path)-1] = '\0'; |
d9b15094 |
564 | } |
565 | |
566 | void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs) |
567 | { |
0f5e1f75 |
568 | struct dlgparam *dp = (struct dlgparam *)dlg; |
569 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
570 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
571 | assert(uc->entry != NULL); |
572 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name); |
d9b15094 |
573 | } |
574 | |
575 | void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs) |
576 | { |
0f5e1f75 |
577 | struct dlgparam *dp = (struct dlgparam *)dlg; |
578 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
579 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
580 | assert(uc->entry != NULL); |
581 | strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
582 | lenof(fs->name)); |
583 | fs->name[lenof(fs->name)-1] = '\0'; |
d9b15094 |
584 | } |
585 | |
586 | /* |
587 | * Bracketing a large set of updates in these two functions will |
588 | * cause the front end (if possible) to delay updating the screen |
589 | * until it's all complete, thus avoiding flicker. |
590 | */ |
591 | void dlg_update_start(union control *ctrl, void *dlg) |
592 | { |
0f5e1f75 |
593 | /* |
594 | * Apparently we can't do this at all in GTK. GtkCList supports |
595 | * freeze and thaw, but not GtkList. Bah. |
596 | */ |
d9b15094 |
597 | } |
598 | |
599 | void dlg_update_done(union control *ctrl, void *dlg) |
600 | { |
0f5e1f75 |
601 | /* |
602 | * Apparently we can't do this at all in GTK. GtkCList supports |
603 | * freeze and thaw, but not GtkList. Bah. |
604 | */ |
d9b15094 |
605 | } |
606 | |
607 | void dlg_set_focus(union control *ctrl, void *dlg) |
608 | { |
0f5e1f75 |
609 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
610 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
611 | |
612 | switch (ctrl->generic.type) { |
613 | case CTRL_CHECKBOX: |
614 | case CTRL_BUTTON: |
615 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
616 | gtk_widget_grab_focus(uc->toplevel); |
617 | break; |
618 | case CTRL_FILESELECT: |
619 | case CTRL_FONTSELECT: |
620 | case CTRL_EDITBOX: |
621 | /* Anything containing an edit box gets that focused. */ |
622 | gtk_widget_grab_focus(uc->entry); |
623 | break; |
624 | case CTRL_RADIO: |
625 | /* |
626 | * Radio buttons: we find the currently selected button and |
627 | * focus it. |
628 | */ |
629 | { |
630 | int i; |
631 | for (i = 0; i < ctrl->radio.nbuttons; i++) |
632 | if (gtk_toggle_button_get_active |
633 | (GTK_TOGGLE_BUTTON(uc->buttons[i]))) { |
634 | gtk_widget_grab_focus(uc->buttons[i]); |
635 | } |
636 | } |
637 | break; |
638 | case CTRL_LISTBOX: |
639 | /* |
640 | * If the list is really an option menu, we focus it. |
641 | * Otherwise we tell it to focus one of its children, which |
642 | * appears to do the Right Thing. |
643 | */ |
644 | if (uc->optmenu) { |
645 | gtk_widget_grab_focus(uc->optmenu); |
646 | } else { |
647 | assert(uc->list != NULL); |
648 | gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD); |
649 | } |
650 | break; |
651 | } |
d9b15094 |
652 | } |
653 | |
654 | /* |
655 | * During event processing, you might well want to give an error |
656 | * indication to the user. dlg_beep() is a quick and easy generic |
657 | * error; dlg_error() puts up a message-box or equivalent. |
658 | */ |
659 | void dlg_beep(void *dlg) |
660 | { |
0f5e1f75 |
661 | gdk_beep(); |
d9b15094 |
662 | } |
663 | |
0bd8d76d |
664 | static void errmsg_button_clicked(GtkButton *button, gpointer data) |
665 | { |
666 | gtk_widget_destroy(GTK_WIDGET(data)); |
667 | } |
668 | |
d9b15094 |
669 | void dlg_error_msg(void *dlg, char *msg) |
670 | { |
0f5e1f75 |
671 | struct dlgparam *dp = (struct dlgparam *)dlg; |
23897ed0 |
672 | GtkWidget *window, *hbox, *text, *ok; |
0bd8d76d |
673 | |
674 | window = gtk_dialog_new(); |
675 | text = gtk_label_new(msg); |
676 | gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0); |
23897ed0 |
677 | hbox = gtk_hbox_new(FALSE, 0); |
678 | gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20); |
0bd8d76d |
679 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
23897ed0 |
680 | hbox, FALSE, FALSE, 20); |
0bd8d76d |
681 | gtk_widget_show(text); |
23897ed0 |
682 | gtk_widget_show(hbox); |
683 | gtk_window_set_title(GTK_WINDOW(window), "Error"); |
0bd8d76d |
684 | gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); |
685 | ok = gtk_button_new_with_label("OK"); |
686 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), |
687 | ok, FALSE, FALSE, 0); |
688 | gtk_widget_show(ok); |
689 | GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT); |
690 | gtk_window_set_default(GTK_WINDOW(window), ok); |
691 | gtk_signal_connect(GTK_OBJECT(ok), "clicked", |
692 | GTK_SIGNAL_FUNC(errmsg_button_clicked), window); |
693 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
694 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
695 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
23897ed0 |
696 | gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window)); |
697 | { |
698 | gint x, y, w, h, dx, dy; |
699 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE); |
700 | gdk_window_get_origin(GTK_WIDGET(dp->window)->window, &x, &y); |
701 | gdk_window_get_size(GTK_WIDGET(dp->window)->window, &w, &h); |
702 | dx = x + w/4; |
703 | dy = y + h/4; |
704 | gtk_widget_set_uposition(GTK_WIDGET(window), dx, dy); |
705 | } |
0bd8d76d |
706 | gtk_widget_show(window); |
707 | gtk_main(); |
d9b15094 |
708 | } |
709 | |
710 | /* |
711 | * This function signals to the front end that the dialog's |
712 | * processing is completed, and passes an integer value (typically |
713 | * a success status). |
714 | */ |
715 | void dlg_end(void *dlg, int value) |
716 | { |
0f5e1f75 |
717 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
718 | dp->retval = value; |
0f5e1f75 |
719 | gtk_main_quit(); |
d9b15094 |
720 | } |
721 | |
722 | void dlg_refresh(union control *ctrl, void *dlg) |
723 | { |
0f5e1f75 |
724 | struct dlgparam *dp = (struct dlgparam *)dlg; |
725 | struct uctrl *uc; |
726 | |
727 | if (ctrl) { |
728 | if (ctrl->generic.handler != NULL) |
729 | ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH); |
730 | } else { |
731 | int i; |
732 | |
733 | for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) { |
734 | assert(uc->ctrl != NULL); |
735 | if (uc->ctrl->generic.handler != NULL) |
736 | uc->ctrl->generic.handler(uc->ctrl, dp, |
737 | dp->data, EVENT_REFRESH); |
738 | } |
739 | } |
d9b15094 |
740 | } |
741 | |
742 | void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b) |
743 | { |
0f5e1f75 |
744 | struct dlgparam *dp = (struct dlgparam *)dlg; |
745 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
746 | gdouble cvals[4]; |
747 | |
748 | GtkWidget *coloursel = |
749 | gtk_color_selection_dialog_new("Select a colour"); |
750 | GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel); |
751 | |
752 | dp->coloursel_result.ok = FALSE; |
753 | |
754 | gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE); |
755 | gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE); |
756 | cvals[0] = r / 255.0; |
757 | cvals[1] = g / 255.0; |
758 | cvals[2] = b / 255.0; |
759 | cvals[3] = 1.0; /* fully opaque! */ |
760 | gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals); |
761 | |
762 | gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data", |
763 | (gpointer)coloursel); |
764 | gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data", |
765 | (gpointer)coloursel); |
766 | gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc); |
767 | gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked", |
768 | GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp); |
769 | gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked", |
770 | GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp); |
771 | gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked", |
772 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
773 | (gpointer)coloursel); |
774 | gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked", |
775 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
776 | (gpointer)coloursel); |
777 | gtk_widget_show(coloursel); |
d9b15094 |
778 | } |
779 | |
780 | int dlg_coloursel_results(union control *ctrl, void *dlg, |
781 | int *r, int *g, int *b) |
782 | { |
0f5e1f75 |
783 | struct dlgparam *dp = (struct dlgparam *)dlg; |
784 | if (dp->coloursel_result.ok) { |
785 | *r = dp->coloursel_result.r; |
786 | *g = dp->coloursel_result.g; |
787 | *b = dp->coloursel_result.b; |
788 | return 1; |
789 | } else |
790 | return 0; |
d9b15094 |
791 | } |
792 | |
0f5e1f75 |
793 | /* ---------------------------------------------------------------------- |
794 | * Signal handlers while the dialog box is active. |
795 | */ |
796 | |
0bd8d76d |
797 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
798 | gpointer data) |
799 | { |
800 | struct dlgparam *dp = (struct dlgparam *)data; |
801 | struct uctrl *uc = dlg_find_bywidget(dp, widget); |
802 | union control *focus; |
803 | |
804 | if (uc && uc->ctrl) |
805 | focus = uc->ctrl; |
806 | else |
807 | focus = NULL; |
808 | |
809 | if (focus != dp->currfocus) { |
810 | dp->lastfocus = dp->currfocus; |
811 | dp->currfocus = focus; |
812 | } |
813 | |
814 | return FALSE; |
815 | } |
816 | |
0f5e1f75 |
817 | static void button_clicked(GtkButton *button, gpointer data) |
818 | { |
819 | struct dlgparam *dp = (struct dlgparam *)data; |
820 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
821 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
822 | } |
823 | |
824 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
825 | { |
826 | struct dlgparam *dp = (struct dlgparam *)data; |
827 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb)); |
828 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
829 | } |
830 | |
1c291fcf |
831 | static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) |
832 | { |
833 | /* |
834 | * GtkEntry has a nasty habit of eating the Return key, which |
835 | * is unhelpful since it doesn't actually _do_ anything with it |
836 | * (it calls gtk_widget_activate, but our edit boxes never need |
837 | * activating). So I catch Return before GtkEntry sees it, and |
838 | * pass it straight on to the parent widget. Effect: hitting |
839 | * Return in an edit box will now activate the default button |
840 | * in the dialog just like it will everywhere else. |
841 | */ |
842 | if (event->keyval == GDK_Return && widget->parent != NULL) { |
843 | gint return_val; |
0bd8d76d |
844 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); |
1c291fcf |
845 | gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", |
846 | event, &return_val); |
847 | return return_val; |
848 | } |
849 | return FALSE; |
850 | } |
851 | |
0f5e1f75 |
852 | static void editbox_changed(GtkEditable *ed, gpointer data) |
853 | { |
854 | struct dlgparam *dp = (struct dlgparam *)data; |
855 | if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) { |
856 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
857 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
858 | } |
859 | } |
860 | |
861 | static void editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event, |
862 | gpointer data) |
863 | { |
864 | struct dlgparam *dp = (struct dlgparam *)data; |
865 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
866 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH); |
867 | } |
868 | |
0bd8d76d |
869 | static int listitem_key(GtkWidget *item, GdkEventKey *event, gpointer data, |
870 | int multiple) |
871 | { |
872 | GtkAdjustment *adj = GTK_ADJUSTMENT(data); |
873 | |
874 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
875 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down || |
876 | event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up || |
877 | event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) { |
878 | /* |
879 | * Up, Down, PgUp or PgDn have been pressed on a ListItem |
880 | * in a list box. So, if the list box is single-selection: |
881 | * |
882 | * - if the list item in question isn't already selected, |
883 | * we simply select it. |
884 | * - otherwise, we find the next one (or next |
885 | * however-far-away) in whichever direction we're going, |
886 | * and select that. |
887 | * + in this case, we must also fiddle with the |
888 | * scrollbar to ensure the newly selected item is |
889 | * actually visible. |
890 | * |
891 | * If it's multiple-selection, we do all of the above |
892 | * except actually selecting anything, so we move the focus |
893 | * and fiddle the scrollbar to follow it. |
894 | */ |
895 | GtkWidget *list = item->parent; |
896 | |
897 | gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event"); |
898 | |
899 | if (!multiple && |
900 | GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) { |
901 | gtk_list_select_child(GTK_LIST(list), item); |
902 | } else { |
903 | int direction = |
904 | (event->keyval==GDK_Up || event->keyval==GDK_KP_Up || |
905 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
906 | ? -1 : +1; |
907 | int step = |
908 | (event->keyval==GDK_Page_Down || |
909 | event->keyval==GDK_KP_Page_Down || |
910 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
911 | ? 2 : 1; |
912 | int i, n; |
913 | GtkWidget *thisitem; |
914 | GList *children, *chead; |
915 | |
916 | chead = children = gtk_container_children(GTK_CONTAINER(list)); |
917 | |
918 | n = g_list_length(children); |
919 | |
920 | if (step == 2) { |
921 | /* |
922 | * Figure out how many list items to a screenful, |
923 | * and adjust the step appropriately. |
924 | */ |
925 | step = 0.5 + adj->page_size * n / (adj->upper - adj->lower); |
926 | step--; /* go by one less than that */ |
927 | } |
928 | |
929 | i = 0; |
930 | while (children != NULL) { |
931 | if (item == children->data) |
932 | break; |
933 | children = children->next; |
934 | i++; |
935 | } |
936 | |
937 | while (step > 0) { |
938 | if (direction < 0 && i > 0) |
939 | children = children->prev, i--; |
940 | else if (direction > 0 && i < n-1) |
941 | children = children->next, i++; |
942 | step--; |
943 | } |
944 | |
945 | if (children && children->data) { |
946 | if (!multiple) |
947 | gtk_list_select_child(GTK_LIST(list), |
948 | GTK_WIDGET(children->data)); |
949 | gtk_widget_grab_focus(GTK_WIDGET(children->data)); |
950 | gtk_adjustment_clamp_page |
951 | (adj, |
952 | adj->lower + (adj->upper-adj->lower) * i / n, |
953 | adj->lower + (adj->upper-adj->lower) * (i+1) / n); |
954 | } |
955 | |
956 | g_list_free(chead); |
957 | } |
958 | return TRUE; |
959 | } |
960 | |
961 | return FALSE; |
962 | } |
963 | |
964 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
965 | gpointer data) |
966 | { |
967 | listitem_key(item, event, data, FALSE); |
968 | } |
969 | |
970 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
971 | gpointer data) |
972 | { |
973 | listitem_key(item, event, data, TRUE); |
974 | } |
975 | |
976 | static int listitem_button(GtkWidget *item, GdkEventButton *event, |
0f5e1f75 |
977 | gpointer data) |
978 | { |
979 | struct dlgparam *dp = (struct dlgparam *)data; |
980 | if (event->type == GDK_2BUTTON_PRESS || |
981 | event->type == GDK_3BUTTON_PRESS) { |
982 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item)); |
983 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
0bd8d76d |
984 | return TRUE; |
0f5e1f75 |
985 | } |
0bd8d76d |
986 | return FALSE; |
0f5e1f75 |
987 | } |
988 | |
989 | static void list_selchange(GtkList *list, gpointer data) |
990 | { |
991 | struct dlgparam *dp = (struct dlgparam *)data; |
992 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list)); |
993 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
994 | } |
995 | |
996 | static void menuitem_activate(GtkMenuItem *item, gpointer data) |
997 | { |
998 | struct dlgparam *dp = (struct dlgparam *)data; |
999 | GtkWidget *menushell = GTK_WIDGET(item)->parent; |
1000 | gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data"); |
1001 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu)); |
1002 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
1003 | } |
1004 | |
1005 | static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction) |
1006 | { |
1007 | int index = dlg_listbox_index(uc->ctrl, dp); |
1008 | GList *children = gtk_container_children(GTK_CONTAINER(uc->list)); |
1009 | GtkWidget *child; |
1010 | |
1011 | if ((index < 0) || |
1012 | (index == 0 && direction < 0) || |
1013 | (index == g_list_length(children)-1 && direction > 0)) { |
1014 | gdk_beep(); |
1015 | return; |
1016 | } |
1017 | |
1018 | child = g_list_nth_data(children, index); |
1019 | gtk_widget_ref(child); |
1020 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
0bd8d76d |
1021 | g_list_free(children); |
1022 | |
0f5e1f75 |
1023 | children = NULL; |
1024 | children = g_list_append(children, child); |
1025 | gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction); |
1026 | gtk_list_select_item(GTK_LIST(uc->list), index + direction); |
1027 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
1028 | } |
1029 | |
1030 | static void draglist_up(GtkButton *button, gpointer data) |
1031 | { |
1032 | struct dlgparam *dp = (struct dlgparam *)data; |
1033 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1034 | draglist_move(dp, uc, -1); |
1035 | } |
1036 | |
1037 | static void draglist_down(GtkButton *button, gpointer data) |
1038 | { |
1039 | struct dlgparam *dp = (struct dlgparam *)data; |
1040 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1041 | draglist_move(dp, uc, +1); |
1042 | } |
1043 | |
1044 | static void filesel_ok(GtkButton *button, gpointer data) |
1045 | { |
1046 | struct dlgparam *dp = (struct dlgparam *)data; |
1047 | gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1048 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data"); |
1049 | char *name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); |
1050 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
1051 | } |
1052 | |
1053 | static void fontsel_ok(GtkButton *button, gpointer data) |
1054 | { |
1055 | struct dlgparam *dp = (struct dlgparam *)data; |
1056 | gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1057 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data"); |
1058 | char *name = gtk_font_selection_dialog_get_font_name |
1059 | (GTK_FONT_SELECTION_DIALOG(fontsel)); |
1060 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
1061 | } |
1062 | |
1063 | static void coloursel_ok(GtkButton *button, gpointer data) |
1064 | { |
1065 | struct dlgparam *dp = (struct dlgparam *)data; |
1066 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1067 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
1068 | gdouble cvals[4]; |
1069 | gtk_color_selection_get_color |
1070 | (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel), |
1071 | cvals); |
1072 | dp->coloursel_result.r = (int) (255 * cvals[0]); |
1073 | dp->coloursel_result.g = (int) (255 * cvals[1]); |
1074 | dp->coloursel_result.b = (int) (255 * cvals[2]); |
1075 | dp->coloursel_result.ok = TRUE; |
1076 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
1077 | } |
1078 | |
1079 | static void coloursel_cancel(GtkButton *button, gpointer data) |
1080 | { |
1081 | struct dlgparam *dp = (struct dlgparam *)data; |
1082 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1083 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
1084 | dp->coloursel_result.ok = FALSE; |
1085 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
1086 | } |
1087 | |
1088 | static void filefont_clicked(GtkButton *button, gpointer data) |
1089 | { |
1090 | struct dlgparam *dp = (struct dlgparam *)data; |
1091 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1092 | |
1093 | if (uc->ctrl->generic.type == CTRL_FILESELECT) { |
1094 | GtkWidget *filesel = |
1095 | gtk_file_selection_new(uc->ctrl->fileselect.title); |
1096 | gtk_window_set_modal(GTK_WINDOW(filesel), TRUE); |
1097 | gtk_object_set_data |
1098 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data", |
1099 | (gpointer)filesel); |
1100 | gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc); |
1101 | gtk_signal_connect |
1102 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1103 | GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp); |
1104 | gtk_signal_connect_object |
1105 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1106 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1107 | gtk_signal_connect_object |
1108 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked", |
1109 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1110 | gtk_widget_show(filesel); |
1111 | } |
1112 | |
1113 | if (uc->ctrl->generic.type == CTRL_FONTSELECT) { |
1114 | gchar *spacings[] = { "c", "m", NULL }; |
0bd8d76d |
1115 | gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry)); |
0f5e1f75 |
1116 | GtkWidget *fontsel = |
1117 | gtk_font_selection_dialog_new("Select a font"); |
1118 | gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE); |
1119 | gtk_font_selection_dialog_set_filter |
1120 | (GTK_FONT_SELECTION_DIALOG(fontsel), |
1121 | GTK_FONT_FILTER_BASE, GTK_FONT_ALL, |
1122 | NULL, NULL, NULL, NULL, spacings, NULL); |
0bd8d76d |
1123 | if (!gtk_font_selection_dialog_set_font_name |
1124 | (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) { |
1125 | /* |
1126 | * If the font name wasn't found as it was, try opening |
1127 | * it and extracting its FONT property. This should |
1128 | * have the effect of mapping short aliases into true |
1129 | * XLFDs. |
1130 | */ |
1131 | GdkFont *font = gdk_font_load(fontname); |
1132 | if (font) { |
1133 | XFontStruct *xfs = GDK_FONT_XFONT(font); |
1134 | Display *disp = GDK_FONT_XDISPLAY(font); |
1135 | Atom fontprop = XInternAtom(disp, "FONT", False); |
1136 | unsigned long ret; |
1137 | if (XGetFontProperty(xfs, fontprop, &ret)) { |
1138 | char *name = XGetAtomName(disp, (Atom)ret); |
1139 | if (name) |
1140 | gtk_font_selection_dialog_set_font_name |
1141 | (GTK_FONT_SELECTION_DIALOG(fontsel), name); |
1142 | } |
1143 | gdk_font_unref(font); |
1144 | } |
1145 | } |
0f5e1f75 |
1146 | gtk_object_set_data |
1147 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1148 | "user-data", (gpointer)fontsel); |
1149 | gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc); |
1150 | gtk_signal_connect |
1151 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1152 | "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp); |
1153 | gtk_signal_connect_object |
1154 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1155 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
1156 | (gpointer)fontsel); |
1157 | gtk_signal_connect_object |
1158 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button), |
1159 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
1160 | (gpointer)fontsel); |
1161 | gtk_widget_show(fontsel); |
1162 | } |
1163 | } |
1164 | |
c5d64b49 |
1165 | static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc, |
1166 | gpointer data) |
1167 | { |
1168 | struct dlgparam *dp = (struct dlgparam *)data; |
1169 | struct uctrl *uc = dlg_find_bywidget(dp, widget); |
1170 | |
1171 | gtk_widget_set_usize(uc->text, alloc->width, -1); |
1172 | gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label); |
1173 | gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig); |
1174 | } |
1175 | |
0f5e1f75 |
1176 | /* ---------------------------------------------------------------------- |
d9b15094 |
1177 | * This function does the main layout work: it reads a controlset, |
1178 | * it creates the relevant GTK controls, and returns a GtkWidget |
1179 | * containing the result. (This widget might be a title of some |
1180 | * sort, it might be a Columns containing many controls, or it |
1181 | * might be a GtkFrame containing a Columns; whatever it is, it's |
1182 | * definitely a GtkWidget and should probably be added to a |
1183 | * GtkVbox.) |
1c291fcf |
1184 | * |
1185 | * `listitemheight' is used to calculate a usize for list boxes: it |
1186 | * should be the height from the size request of a GtkListItem. |
1187 | * |
1188 | * `win' is required for setting the default button. If it is |
1189 | * non-NULL, all buttons created will be default-capable (so they |
1190 | * have extra space round them for the default highlight). |
d9b15094 |
1191 | */ |
1c291fcf |
1192 | GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs, |
1193 | struct controlset *s, int listitemheight, |
1194 | GtkWindow *win) |
d9b15094 |
1195 | { |
1196 | Columns *cols; |
1197 | GtkWidget *ret; |
1198 | int i; |
1199 | |
1200 | if (!s->boxname && s->boxtitle) { |
1201 | /* This controlset is a panel title. */ |
1202 | return gtk_label_new(s->boxtitle); |
1203 | } |
1204 | |
1205 | /* |
1206 | * Otherwise, we expect to be laying out actual controls, so |
1207 | * we'll start by creating a Columns for the purpose. |
1208 | */ |
1209 | cols = COLUMNS(columns_new(4)); |
1210 | ret = GTK_WIDGET(cols); |
1211 | gtk_widget_show(ret); |
1212 | |
1213 | /* |
1214 | * Create a containing frame if we have a box name. |
1215 | */ |
1216 | if (*s->boxname) { |
1217 | ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */ |
1218 | gtk_container_set_border_width(GTK_CONTAINER(cols), 4); |
1219 | gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols)); |
1220 | gtk_widget_show(ret); |
1221 | } |
1222 | |
1223 | /* |
1224 | * Now iterate through the controls themselves, create them, |
1225 | * and add them to the Columns. |
1226 | */ |
1227 | for (i = 0; i < s->ncontrols; i++) { |
1228 | union control *ctrl = s->ctrls[i]; |
1c291fcf |
1229 | struct uctrl *uc; |
1230 | int left = FALSE; |
d9b15094 |
1231 | GtkWidget *w = NULL; |
1232 | |
1233 | switch (ctrl->generic.type) { |
1234 | case CTRL_COLUMNS: |
1235 | { |
1236 | static const int simplecols[1] = { 100 }; |
1237 | columns_set_cols(cols, ctrl->columns.ncols, |
1238 | (ctrl->columns.percentages ? |
1239 | ctrl->columns.percentages : simplecols)); |
1240 | } |
1241 | continue; /* no actual control created */ |
1242 | case CTRL_TABDELAY: |
0f5e1f75 |
1243 | { |
1244 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl); |
1245 | if (uc) |
1246 | columns_taborder_last(cols, uc->toplevel); |
1247 | } |
d9b15094 |
1248 | continue; /* no actual control created */ |
1c291fcf |
1249 | } |
1250 | |
3d88e64d |
1251 | uc = snew(struct uctrl); |
1c291fcf |
1252 | uc->ctrl = ctrl; |
1253 | uc->privdata = NULL; |
1254 | uc->privdata_needs_free = FALSE; |
1255 | uc->buttons = NULL; |
1256 | uc->entry = uc->list = uc->menu = NULL; |
1257 | uc->button = uc->optmenu = uc->text = NULL; |
1258 | |
1259 | switch (ctrl->generic.type) { |
d9b15094 |
1260 | case CTRL_BUTTON: |
1261 | w = gtk_button_new_with_label(ctrl->generic.label); |
1c291fcf |
1262 | if (win) { |
1263 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
1264 | if (ctrl->button.isdefault) |
1265 | gtk_window_set_default(win, w); |
0bd8d76d |
1266 | if (ctrl->button.iscancel) |
1267 | dp->cancelbutton = w; |
1c291fcf |
1268 | } |
0f5e1f75 |
1269 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
1270 | GTK_SIGNAL_FUNC(button_clicked), dp); |
0bd8d76d |
1271 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
1272 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1273 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut, |
1274 | SHORTCUT_UCTRL, uc); |
d9b15094 |
1275 | break; |
1276 | case CTRL_CHECKBOX: |
1277 | w = gtk_check_button_new_with_label(ctrl->generic.label); |
0f5e1f75 |
1278 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
1279 | GTK_SIGNAL_FUNC(button_toggled), dp); |
0bd8d76d |
1280 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
1281 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1282 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut, |
1283 | SHORTCUT_UCTRL, uc); |
1284 | left = TRUE; |
d9b15094 |
1285 | break; |
1286 | case CTRL_RADIO: |
1287 | /* |
1288 | * Radio buttons get to go inside their own Columns, no |
1289 | * matter what. |
1290 | */ |
1291 | { |
1292 | gint i, *percentages; |
1293 | GSList *group; |
1294 | |
c5d64b49 |
1295 | w = columns_new(0); |
d9b15094 |
1296 | if (ctrl->generic.label) { |
1297 | GtkWidget *label = gtk_label_new(ctrl->generic.label); |
1298 | columns_add(COLUMNS(w), label, 0, 1); |
1299 | columns_force_left_align(COLUMNS(w), label); |
1300 | gtk_widget_show(label); |
1c291fcf |
1301 | shortcut_add(scs, label, ctrl->radio.shortcut, |
1302 | SHORTCUT_UCTRL, uc); |
d9b15094 |
1303 | } |
1304 | percentages = g_new(gint, ctrl->radio.ncolumns); |
1305 | for (i = 0; i < ctrl->radio.ncolumns; i++) { |
1306 | percentages[i] = |
1307 | ((100 * (i+1) / ctrl->radio.ncolumns) - |
1308 | 100 * i / ctrl->radio.ncolumns); |
1309 | } |
1310 | columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns, |
1311 | percentages); |
1312 | g_free(percentages); |
1313 | group = NULL; |
0f5e1f75 |
1314 | |
1c291fcf |
1315 | uc->nbuttons = ctrl->radio.nbuttons; |
3d88e64d |
1316 | uc->buttons = snewn(uc->nbuttons, GtkWidget *); |
0f5e1f75 |
1317 | |
d9b15094 |
1318 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
1319 | GtkWidget *b; |
1320 | gint colstart; |
1321 | |
1322 | b = (gtk_radio_button_new_with_label |
1323 | (group, ctrl->radio.buttons[i])); |
1c291fcf |
1324 | uc->buttons[i] = b; |
d9b15094 |
1325 | group = gtk_radio_button_group(GTK_RADIO_BUTTON(b)); |
1326 | colstart = i % ctrl->radio.ncolumns; |
1327 | columns_add(COLUMNS(w), b, colstart, |
1328 | (i == ctrl->radio.nbuttons-1 ? |
1329 | ctrl->radio.ncolumns - colstart : 1)); |
1c291fcf |
1330 | columns_force_left_align(COLUMNS(w), b); |
d9b15094 |
1331 | gtk_widget_show(b); |
0f5e1f75 |
1332 | gtk_signal_connect(GTK_OBJECT(b), "toggled", |
1333 | GTK_SIGNAL_FUNC(button_toggled), dp); |
0bd8d76d |
1334 | gtk_signal_connect(GTK_OBJECT(b), "focus_in_event", |
1335 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1336 | if (ctrl->radio.shortcuts) { |
1337 | shortcut_add(scs, GTK_BIN(b)->child, |
1338 | ctrl->radio.shortcuts[i], |
1339 | SHORTCUT_UCTRL, uc); |
1340 | } |
d9b15094 |
1341 | } |
1342 | } |
1343 | break; |
1344 | case CTRL_EDITBOX: |
c5d64b49 |
1345 | { |
d9b15094 |
1346 | GtkRequisition req; |
c5d64b49 |
1347 | |
1348 | if (ctrl->editbox.has_list) { |
1349 | w = gtk_combo_new(); |
1350 | gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE); |
1351 | uc->entry = GTK_COMBO(w)->entry; |
1352 | uc->list = GTK_COMBO(w)->list; |
1353 | } else { |
1354 | w = gtk_entry_new(); |
1355 | if (ctrl->editbox.password) |
1356 | gtk_entry_set_visibility(GTK_ENTRY(w), FALSE); |
1357 | uc->entry = w; |
1358 | } |
1359 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
1360 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
1361 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
1362 | GTK_SIGNAL_FUNC(editbox_key), dp); |
1363 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
1364 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1365 | /* |
1366 | * Edit boxes, for some strange reason, have a minimum |
1367 | * width of 150 in GTK 1.2. We don't want this - we'd |
1368 | * rather the edit boxes acquired their natural width |
1369 | * from the column layout of the rest of the box. |
1370 | * |
1371 | * Also, while we're here, we'll squirrel away the |
1372 | * edit box height so we can use that to centre its |
1373 | * label vertically beside it. |
1374 | */ |
d9b15094 |
1375 | gtk_widget_size_request(w, &req); |
1376 | gtk_widget_set_usize(w, 10, req.height); |
d9b15094 |
1377 | |
c5d64b49 |
1378 | if (ctrl->generic.label) { |
1379 | GtkWidget *label, *container; |
1380 | |
1381 | label = gtk_label_new(ctrl->generic.label); |
1382 | |
1383 | shortcut_add(scs, label, ctrl->editbox.shortcut, |
1384 | SHORTCUT_FOCUS, uc->entry); |
1385 | |
1386 | container = columns_new(4); |
1387 | if (ctrl->editbox.percentwidth == 100) { |
1388 | columns_add(COLUMNS(container), label, 0, 1); |
1389 | columns_force_left_align(COLUMNS(container), label); |
1390 | columns_add(COLUMNS(container), w, 0, 1); |
1391 | } else { |
1392 | gint percentages[2]; |
1393 | percentages[1] = ctrl->editbox.percentwidth; |
1394 | percentages[0] = 100 - ctrl->editbox.percentwidth; |
1395 | columns_set_cols(COLUMNS(container), 2, percentages); |
1396 | columns_add(COLUMNS(container), label, 0, 1); |
1397 | columns_force_left_align(COLUMNS(container), label); |
1398 | columns_add(COLUMNS(container), w, 1, 1); |
1399 | /* Centre the label vertically. */ |
1400 | gtk_widget_set_usize(label, -1, req.height); |
1401 | gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); |
1402 | } |
1403 | gtk_widget_show(label); |
1404 | gtk_widget_show(w); |
d9b15094 |
1405 | |
c5d64b49 |
1406 | w = container; |
1407 | } |
1408 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event", |
1409 | GTK_SIGNAL_FUNC(editbox_lostfocus), dp); |
1410 | } |
d9b15094 |
1411 | break; |
1d0d4a3b |
1412 | case CTRL_FILESELECT: |
1413 | case CTRL_FONTSELECT: |
1414 | { |
1415 | GtkWidget *ww; |
1416 | GtkRequisition req; |
1417 | char *browsebtn = |
1418 | (ctrl->generic.type == CTRL_FILESELECT ? |
1419 | "Browse..." : "Change..."); |
1420 | |
1421 | gint percentages[] = { 75, 25 }; |
1422 | w = columns_new(4); |
1423 | columns_set_cols(COLUMNS(w), 2, percentages); |
1424 | |
1425 | if (ctrl->generic.label) { |
1426 | ww = gtk_label_new(ctrl->generic.label); |
1427 | columns_add(COLUMNS(w), ww, 0, 2); |
1428 | columns_force_left_align(COLUMNS(w), ww); |
1429 | gtk_widget_show(ww); |
1c291fcf |
1430 | shortcut_add(scs, ww, |
1431 | (ctrl->generic.type == CTRL_FILESELECT ? |
1432 | ctrl->fileselect.shortcut : |
1433 | ctrl->fontselect.shortcut), |
1434 | SHORTCUT_UCTRL, uc); |
1d0d4a3b |
1435 | } |
1436 | |
1c291fcf |
1437 | uc->entry = ww = gtk_entry_new(); |
1d0d4a3b |
1438 | gtk_widget_size_request(ww, &req); |
1439 | gtk_widget_set_usize(ww, 10, req.height); |
1440 | columns_add(COLUMNS(w), ww, 0, 1); |
1441 | gtk_widget_show(ww); |
1442 | |
1c291fcf |
1443 | uc->button = ww = gtk_button_new_with_label(browsebtn); |
1d0d4a3b |
1444 | columns_add(COLUMNS(w), ww, 1, 1); |
1445 | gtk_widget_show(ww); |
0f5e1f75 |
1446 | |
1c291fcf |
1447 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
1448 | GTK_SIGNAL_FUNC(editbox_key), dp); |
1449 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
0f5e1f75 |
1450 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
0bd8d76d |
1451 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
1452 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1453 | gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event", |
1454 | GTK_SIGNAL_FUNC(widget_focus), dp); |
0f5e1f75 |
1455 | gtk_signal_connect(GTK_OBJECT(ww), "clicked", |
1456 | GTK_SIGNAL_FUNC(filefont_clicked), dp); |
1d0d4a3b |
1457 | } |
1458 | break; |
1459 | case CTRL_LISTBOX: |
1460 | if (ctrl->listbox.height == 0) { |
1c291fcf |
1461 | uc->optmenu = w = gtk_option_menu_new(); |
1462 | uc->menu = gtk_menu_new(); |
1463 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu); |
1464 | gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data", |
1465 | (gpointer)uc->optmenu); |
0bd8d76d |
1466 | gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event", |
1467 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1468 | } else { |
1c291fcf |
1469 | uc->list = gtk_list_new(); |
0bd8d76d |
1470 | if (ctrl->listbox.multisel) { |
1471 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
1472 | GTK_SELECTION_MULTIPLE); |
1473 | } else { |
1474 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
1475 | GTK_SELECTION_SINGLE); |
1476 | } |
1d0d4a3b |
1477 | w = gtk_scrolled_window_new(NULL, NULL); |
1478 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w), |
1c291fcf |
1479 | uc->list); |
1d0d4a3b |
1480 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w), |
1481 | GTK_POLICY_NEVER, |
1482 | GTK_POLICY_AUTOMATIC); |
0bd8d76d |
1483 | uc->adj = gtk_scrolled_window_get_vadjustment |
1484 | (GTK_SCROLLED_WINDOW(w)); |
1485 | |
1c291fcf |
1486 | gtk_widget_show(uc->list); |
1487 | gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed", |
0f5e1f75 |
1488 | GTK_SIGNAL_FUNC(list_selchange), dp); |
0bd8d76d |
1489 | gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event", |
1490 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1491 | |
1492 | /* |
1493 | * Adjust the height of the scrolled window to the |
1494 | * minimum given by the height parameter. |
1495 | * |
1496 | * This piece of guesswork is a horrid hack based |
1497 | * on looking inside the GTK 1.2 sources |
1498 | * (specifically gtkviewport.c, which appears to be |
1499 | * the widget which provides the border around the |
1500 | * scrolling area). Anyone lets me know how I can |
1501 | * do this in a way which isn't at risk from GTK |
1502 | * upgrades, I'd be grateful. |
1503 | */ |
1504 | { |
1c291fcf |
1505 | int edge = GTK_WIDGET(uc->list)->style->klass->ythickness; |
1d0d4a3b |
1506 | gtk_widget_set_usize(w, 10, |
1507 | 2*edge + (ctrl->listbox.height * |
1508 | listitemheight)); |
1509 | } |
1d0d4a3b |
1510 | |
1511 | if (ctrl->listbox.draglist) { |
1512 | /* |
1513 | * GTK doesn't appear to make it easy to |
1514 | * implement a proper draggable list; so |
1515 | * instead I'm just going to have to put an Up |
1516 | * and a Down button to the right of the actual |
1517 | * list box. Ah well. |
1518 | */ |
1519 | GtkWidget *cols, *button; |
1520 | static const gint percentages[2] = { 80, 20 }; |
1521 | |
1522 | cols = columns_new(4); |
1523 | columns_set_cols(COLUMNS(cols), 2, percentages); |
1524 | columns_add(COLUMNS(cols), w, 0, 1); |
1525 | gtk_widget_show(w); |
1526 | button = gtk_button_new_with_label("Up"); |
1527 | columns_add(COLUMNS(cols), button, 1, 1); |
1528 | gtk_widget_show(button); |
0f5e1f75 |
1529 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1530 | GTK_SIGNAL_FUNC(draglist_up), dp); |
0bd8d76d |
1531 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
1532 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1533 | button = gtk_button_new_with_label("Down"); |
1534 | columns_add(COLUMNS(cols), button, 1, 1); |
1535 | gtk_widget_show(button); |
0f5e1f75 |
1536 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1537 | GTK_SIGNAL_FUNC(draglist_down), dp); |
0bd8d76d |
1538 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
1539 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1540 | |
1541 | w = cols; |
1542 | } |
1543 | |
1544 | } |
1545 | if (ctrl->generic.label) { |
1546 | GtkWidget *label, *container; |
1547 | |
1548 | label = gtk_label_new(ctrl->generic.label); |
1549 | |
1550 | container = columns_new(4); |
1551 | if (ctrl->listbox.percentwidth == 100) { |
1552 | columns_add(COLUMNS(container), label, 0, 1); |
1553 | columns_force_left_align(COLUMNS(container), label); |
1554 | columns_add(COLUMNS(container), w, 0, 1); |
1555 | } else { |
1556 | gint percentages[2]; |
1557 | percentages[1] = ctrl->listbox.percentwidth; |
1558 | percentages[0] = 100 - ctrl->listbox.percentwidth; |
1559 | columns_set_cols(COLUMNS(container), 2, percentages); |
1560 | columns_add(COLUMNS(container), label, 0, 1); |
1561 | columns_force_left_align(COLUMNS(container), label); |
1562 | columns_add(COLUMNS(container), w, 1, 1); |
1563 | } |
1564 | gtk_widget_show(label); |
1565 | gtk_widget_show(w); |
1c291fcf |
1566 | shortcut_add(scs, label, ctrl->listbox.shortcut, |
1567 | SHORTCUT_UCTRL, uc); |
1d0d4a3b |
1568 | w = container; |
1569 | } |
1570 | break; |
d9b15094 |
1571 | case CTRL_TEXT: |
c5d64b49 |
1572 | /* |
1573 | * Wrapping text widgets don't sit well with the GTK |
1574 | * layout model, in which widgets state a minimum size |
1575 | * and the whole window then adjusts to the smallest |
1576 | * size it can sensibly take given its contents. A |
1577 | * wrapping text widget _has_ no clear minimum size; |
1578 | * instead it has a range of possibilities. It can be |
1579 | * one line deep but 2000 wide, or two lines deep and |
1580 | * 1000 pixels, or three by 867, or four by 500 and so |
1581 | * on. It can be as short as you like provided you |
1582 | * don't mind it being wide, or as narrow as you like |
1583 | * provided you don't mind it being tall. |
1584 | * |
1585 | * Therefore, it fits very badly into the layout model. |
1586 | * Hence the only thing to do is pick a width and let |
1587 | * it choose its own number of lines. To do this I'm |
1588 | * going to cheat a little. All new wrapping text |
1589 | * widgets will be created with a minimal text content |
1590 | * "X"; then, after the rest of the dialog box is set |
1591 | * up and its size calculated, the text widgets will be |
1592 | * told their width and given their real text, which |
1593 | * will cause the size to be recomputed in the y |
1594 | * direction (because many of them will expand to more |
1595 | * than one line). |
1596 | */ |
1597 | uc->text = w = gtk_label_new("X"); |
0bd8d76d |
1598 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0); |
d9b15094 |
1599 | gtk_label_set_line_wrap(GTK_LABEL(w), TRUE); |
c5d64b49 |
1600 | uc->textsig = |
1601 | gtk_signal_connect(GTK_OBJECT(w), "size-allocate", |
1602 | GTK_SIGNAL_FUNC(label_sizealloc), dp); |
d9b15094 |
1603 | break; |
1604 | } |
0f5e1f75 |
1605 | |
1c291fcf |
1606 | assert(w != NULL); |
0f5e1f75 |
1607 | |
1c291fcf |
1608 | columns_add(cols, w, |
1609 | COLUMN_START(ctrl->generic.column), |
1610 | COLUMN_SPAN(ctrl->generic.column)); |
1611 | if (left) |
1612 | columns_force_left_align(cols, w); |
1613 | gtk_widget_show(w); |
1614 | |
1615 | uc->toplevel = w; |
1616 | dlg_add_uctrl(dp, uc); |
d9b15094 |
1617 | } |
1618 | |
1619 | return ret; |
1620 | } |
1621 | |
1622 | struct selparam { |
1c291fcf |
1623 | struct dlgparam *dp; |
d9b15094 |
1624 | Panels *panels; |
1625 | GtkWidget *panel, *treeitem; |
1c291fcf |
1626 | struct Shortcuts shortcuts; |
d9b15094 |
1627 | }; |
1628 | |
1629 | static void treeitem_sel(GtkItem *item, gpointer data) |
1630 | { |
1631 | struct selparam *sp = (struct selparam *)data; |
1632 | |
1633 | panels_switch_to(sp->panels, sp->panel); |
1c291fcf |
1634 | |
1635 | sp->dp->shortcuts = &sp->shortcuts; |
0bd8d76d |
1636 | sp->dp->currtreeitem = sp->treeitem; |
d9b15094 |
1637 | } |
1638 | |
0bd8d76d |
1639 | static void window_destroy(GtkWidget *widget, gpointer data) |
d9b15094 |
1640 | { |
1641 | gtk_main_quit(); |
1642 | } |
1643 | |
c5d64b49 |
1644 | static int tree_grab_focus(struct dlgparam *dp) |
1645 | { |
1646 | int i, f; |
1647 | |
1648 | /* |
1649 | * See if any of the treeitems has the focus. |
1650 | */ |
1651 | f = -1; |
1652 | for (i = 0; i < dp->ntreeitems; i++) |
1653 | if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) { |
1654 | f = i; |
1655 | break; |
1656 | } |
1657 | |
1658 | if (f >= 0) |
1659 | return FALSE; |
1660 | else { |
1661 | gtk_widget_grab_focus(dp->currtreeitem); |
1662 | return TRUE; |
1663 | } |
1664 | } |
1665 | |
1666 | gint tree_focus(GtkContainer *container, GtkDirectionType direction, |
1667 | gpointer data) |
1668 | { |
1669 | struct dlgparam *dp = (struct dlgparam *)data; |
1670 | |
1671 | gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus"); |
1672 | /* |
1673 | * If there's a focused treeitem, we return FALSE to cause the |
1674 | * focus to move on to some totally other control. If not, we |
1675 | * focus the selected one. |
1676 | */ |
1677 | return tree_grab_focus(dp); |
1678 | } |
1679 | |
1c291fcf |
1680 | int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
1681 | { |
1682 | struct dlgparam *dp = (struct dlgparam *)data; |
1683 | |
0bd8d76d |
1684 | if (event->keyval == GDK_Escape && dp->cancelbutton) { |
1685 | gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked"); |
1c291fcf |
1686 | return TRUE; |
1687 | } |
1688 | |
1689 | if ((event->state & GDK_MOD1_MASK) && |
1690 | (unsigned char)event->string[0] > 0 && |
1691 | (unsigned char)event->string[0] <= 127) { |
1692 | int schr = (unsigned char)event->string[0]; |
1693 | struct Shortcut *sc = &dp->shortcuts->sc[schr]; |
1694 | |
1695 | switch (sc->action) { |
c5d64b49 |
1696 | case SHORTCUT_TREE: |
1697 | tree_grab_focus(dp); |
1698 | break; |
1c291fcf |
1699 | case SHORTCUT_FOCUS: |
1700 | gtk_widget_grab_focus(sc->widget); |
1701 | break; |
1702 | case SHORTCUT_UCTRL: |
1703 | /* |
1704 | * We must do something sensible with a uctrl. |
1705 | * Precisely what this is depends on the type of |
1706 | * control. |
1707 | */ |
1708 | switch (sc->uc->ctrl->generic.type) { |
1709 | case CTRL_CHECKBOX: |
1710 | case CTRL_BUTTON: |
1711 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
1712 | gtk_widget_grab_focus(sc->uc->toplevel); |
1713 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel), |
1714 | "clicked"); |
1715 | break; |
1716 | case CTRL_FILESELECT: |
1717 | case CTRL_FONTSELECT: |
1718 | /* File/font selectors have their buttons pressed (ooer), |
1719 | * and focus transferred to the edit box. */ |
1720 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button), |
1721 | "clicked"); |
1722 | gtk_widget_grab_focus(sc->uc->entry); |
1723 | break; |
1724 | case CTRL_RADIO: |
1725 | /* |
1726 | * Radio buttons are fun, because they have |
1727 | * multiple shortcuts. We must find whether the |
1728 | * activated shortcut is the shortcut for the whole |
1729 | * group, or for a particular button. In the former |
1730 | * case, we find the currently selected button and |
1731 | * focus it; in the latter, we focus-and-click the |
1732 | * button whose shortcut was pressed. |
1733 | */ |
1734 | if (schr == sc->uc->ctrl->radio.shortcut) { |
1735 | int i; |
1736 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
1737 | if (gtk_toggle_button_get_active |
1738 | (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) { |
1739 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
1740 | } |
1741 | } else if (sc->uc->ctrl->radio.shortcuts) { |
1742 | int i; |
1743 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
1744 | if (schr == sc->uc->ctrl->radio.shortcuts[i]) { |
1745 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
1746 | gtk_signal_emit_by_name |
1747 | (GTK_OBJECT(sc->uc->buttons[i]), "clicked"); |
1748 | } |
1749 | } |
1750 | break; |
1751 | case CTRL_LISTBOX: |
1752 | /* |
0bd8d76d |
1753 | * If the list is really an option menu, we focus |
1754 | * and click it. Otherwise we tell it to focus one |
1755 | * of its children, which appears to do the Right |
1756 | * Thing. |
1c291fcf |
1757 | */ |
1758 | if (sc->uc->optmenu) { |
1759 | GdkEventButton bev; |
1760 | gint returnval; |
1761 | |
1762 | gtk_widget_grab_focus(sc->uc->optmenu); |
1763 | /* Option menus don't work using the "clicked" signal. |
1764 | * We need to manufacture a button press event :-/ */ |
1765 | bev.type = GDK_BUTTON_PRESS; |
1766 | bev.button = 1; |
1767 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu), |
1768 | "button_press_event", |
1769 | &bev, &returnval); |
1770 | } else { |
0bd8d76d |
1771 | assert(sc->uc->list != NULL); |
1772 | |
1773 | gtk_container_focus(GTK_CONTAINER(sc->uc->list), |
1774 | GTK_DIR_TAB_FORWARD); |
1c291fcf |
1775 | } |
1776 | break; |
1777 | } |
1778 | break; |
1779 | } |
1780 | } |
1781 | |
1782 | return FALSE; |
1783 | } |
1784 | |
0bd8d76d |
1785 | int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
1786 | { |
1787 | struct dlgparam *dp = (struct dlgparam *)data; |
1788 | |
1789 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
1790 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down) { |
c5d64b49 |
1791 | int dir, i, j = -1; |
0bd8d76d |
1792 | for (i = 0; i < dp->ntreeitems; i++) |
c5d64b49 |
1793 | if (widget == dp->treeitems[i]) |
1794 | break; |
1795 | if (i < dp->ntreeitems) { |
1796 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up) |
1797 | dir = -1; |
1798 | else |
1799 | dir = +1; |
1800 | |
1801 | while (1) { |
1802 | i += dir; |
1803 | if (i < 0 || i >= dp->ntreeitems) |
1804 | break; /* nothing in that dir to select */ |
1805 | /* |
1806 | * Determine if this tree item is visible. |
1807 | */ |
1808 | { |
1809 | GtkWidget *w = dp->treeitems[i]; |
1810 | int vis = TRUE; |
1811 | while (w && GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w)) { |
1812 | if (!GTK_WIDGET_VISIBLE(w)) { |
1813 | vis = FALSE; |
1814 | break; |
1815 | } |
1816 | w = w->parent; |
1817 | } |
1818 | if (vis) { |
1819 | j = i; /* got one */ |
1820 | break; |
1821 | } |
1822 | } |
1823 | } |
1824 | } |
0bd8d76d |
1825 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
1826 | "key_press_event"); |
1827 | if (j >= 0) { |
1828 | gint return_val; |
1829 | gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle"); |
1830 | gtk_widget_grab_focus(dp->treeitems[j]); |
1831 | } |
1832 | return TRUE; |
1833 | } |
1834 | |
0bd8d76d |
1835 | /* |
c5d64b49 |
1836 | * It's nice for Left and Right to expand and collapse tree |
1837 | * branches. |
0bd8d76d |
1838 | */ |
c5d64b49 |
1839 | if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) { |
1840 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
1841 | "key_press_event"); |
1842 | gtk_tree_item_collapse(GTK_TREE_ITEM(widget)); |
1843 | return TRUE; |
1844 | } |
1845 | if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) { |
1846 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
1847 | "key_press_event"); |
1848 | gtk_tree_item_expand(GTK_TREE_ITEM(widget)); |
1849 | return TRUE; |
0bd8d76d |
1850 | } |
c5d64b49 |
1851 | |
1852 | return FALSE; |
0bd8d76d |
1853 | } |
1854 | |
1c291fcf |
1855 | void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
1856 | int chr, int action, void *ptr) |
1857 | { |
1858 | GtkLabel *label = GTK_LABEL(labelw); |
1859 | gchar *currstr, *pattern; |
1860 | int i; |
1861 | |
1862 | if (chr == NO_SHORTCUT) |
1863 | return; |
1864 | |
1865 | chr = tolower((unsigned char)chr); |
1866 | |
1867 | assert(scs->sc[chr].action == SHORTCUT_EMPTY); |
1868 | |
1869 | scs->sc[chr].action = action; |
1870 | |
1871 | if (action == SHORTCUT_FOCUS) { |
1872 | scs->sc[chr].uc = NULL; |
1873 | scs->sc[chr].widget = (GtkWidget *)ptr; |
1874 | } else { |
1875 | scs->sc[chr].widget = NULL; |
1876 | scs->sc[chr].uc = (struct uctrl *)ptr; |
1877 | } |
1878 | |
1879 | gtk_label_get(label, &currstr); |
1880 | for (i = 0; currstr[i]; i++) |
1881 | if (tolower((unsigned char)currstr[i]) == chr) { |
1882 | GtkRequisition req; |
1883 | |
1884 | pattern = dupprintf("%*s_", i, ""); |
1885 | |
1886 | gtk_widget_size_request(GTK_WIDGET(label), &req); |
1887 | gtk_label_set_pattern(label, pattern); |
1888 | gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height); |
1889 | |
1890 | sfree(pattern); |
1891 | break; |
1892 | } |
1893 | } |
1894 | |
c5d64b49 |
1895 | int do_config_box(const char *title) |
d9b15094 |
1896 | { |
1897 | GtkWidget *window, *hbox, *vbox, *cols, *label, |
1898 | *tree, *treescroll, *panels, *panelvbox; |
1d0d4a3b |
1899 | int index, level, listitemheight; |
d9b15094 |
1900 | struct controlbox *ctrlbox; |
1901 | char *path; |
1902 | GtkTreeItem *treeitemlevels[8]; |
1903 | GtkTree *treelevels[8]; |
1904 | Config cfg; |
0f5e1f75 |
1905 | struct dlgparam dp; |
1906 | struct sesslist sl; |
1c291fcf |
1907 | struct Shortcuts scs; |
d9b15094 |
1908 | |
1909 | struct selparam *selparams = NULL; |
1910 | int nselparams = 0, selparamsize = 0; |
1911 | |
1912 | do_defaults(NULL, &cfg); |
1913 | |
0f5e1f75 |
1914 | dlg_init(&dp); |
1915 | |
1d0d4a3b |
1916 | { |
1917 | GtkWidget *listitem = gtk_list_item_new_with_label("foo"); |
1918 | GtkRequisition req; |
1919 | gtk_widget_size_request(listitem, &req); |
1920 | listitemheight = req.height; |
1921 | gtk_widget_unref(listitem); |
1922 | } |
1923 | |
0f5e1f75 |
1924 | sl.nsessions = 0; |
1925 | |
1c291fcf |
1926 | for (index = 0; index < lenof(scs.sc); index++) { |
1927 | scs.sc[index].action = SHORTCUT_EMPTY; |
1928 | } |
1929 | |
d9b15094 |
1930 | ctrlbox = ctrl_new_box(); |
0f5e1f75 |
1931 | setup_config_box(ctrlbox, &sl, FALSE, 0); |
1d0d4a3b |
1932 | unix_setup_config_box(ctrlbox, FALSE); |
d9b15094 |
1933 | |
1934 | window = gtk_dialog_new(); |
c5d64b49 |
1935 | gtk_window_set_title(GTK_WINDOW(window), title); |
d9b15094 |
1936 | hbox = gtk_hbox_new(FALSE, 4); |
1937 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0); |
1d0d4a3b |
1938 | gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
d9b15094 |
1939 | gtk_widget_show(hbox); |
1940 | vbox = gtk_vbox_new(FALSE, 4); |
1941 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
1942 | gtk_widget_show(vbox); |
1943 | cols = columns_new(4); |
1944 | gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0); |
1945 | gtk_widget_show(cols); |
1946 | label = gtk_label_new("Category:"); |
1947 | columns_add(COLUMNS(cols), label, 0, 1); |
1948 | columns_force_left_align(COLUMNS(cols), label); |
1949 | gtk_widget_show(label); |
1950 | treescroll = gtk_scrolled_window_new(NULL, NULL); |
1951 | tree = gtk_tree_new(); |
0bd8d76d |
1952 | gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event", |
1953 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
c5d64b49 |
1954 | shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree); |
d9b15094 |
1955 | gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM); |
1d0d4a3b |
1956 | gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE); |
d9b15094 |
1957 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll), |
1958 | tree); |
1959 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll), |
1960 | GTK_POLICY_NEVER, |
1961 | GTK_POLICY_AUTOMATIC); |
0bd8d76d |
1962 | gtk_signal_connect(GTK_OBJECT(tree), "focus", |
1963 | GTK_SIGNAL_FUNC(tree_focus), &dp); |
d9b15094 |
1964 | gtk_widget_show(tree); |
1965 | gtk_widget_show(treescroll); |
1966 | gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0); |
1967 | panels = panels_new(); |
1968 | gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0); |
1969 | gtk_widget_show(panels); |
1970 | |
1971 | panelvbox = NULL; |
1972 | path = NULL; |
1973 | level = 0; |
1974 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
1975 | struct controlset *s = ctrlbox->ctrlsets[index]; |
1c291fcf |
1976 | GtkWidget *w; |
d9b15094 |
1977 | |
1978 | if (!*s->pathname) { |
1c291fcf |
1979 | w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window)); |
d9b15094 |
1980 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), |
1981 | w, TRUE, TRUE, 0); |
1982 | } else { |
1983 | int j = path ? ctrl_path_compare(s->pathname, path) : 0; |
1984 | if (j != INT_MAX) { /* add to treeview, start new panel */ |
1985 | char *c; |
1986 | GtkWidget *treeitem; |
1987 | int first; |
1988 | |
1989 | /* |
1990 | * We expect never to find an implicit path |
1991 | * component. For example, we expect never to see |
1992 | * A/B/C followed by A/D/E, because that would |
1993 | * _implicitly_ create A/D. All our path prefixes |
1994 | * are expected to contain actual controls and be |
1995 | * selectable in the treeview; so we would expect |
1996 | * to see A/D _explicitly_ before encountering |
1997 | * A/D/E. |
1998 | */ |
1999 | assert(j == ctrl_path_elements(s->pathname) - 1); |
2000 | |
2001 | c = strrchr(s->pathname, '/'); |
2002 | if (!c) |
2003 | c = s->pathname; |
2004 | else |
2005 | c++; |
2006 | |
2007 | treeitem = gtk_tree_item_new_with_label(c); |
2008 | assert(j-1 < level); |
2009 | if (j > 0) { |
2010 | if (!treelevels[j-1]) { |
2011 | treelevels[j-1] = GTK_TREE(gtk_tree_new()); |
2012 | gtk_tree_item_set_subtree |
2013 | (treeitemlevels[j-1], |
2014 | GTK_WIDGET(treelevels[j-1])); |
2015 | gtk_tree_item_expand(treeitemlevels[j-1]); |
2016 | } |
2017 | gtk_tree_append(treelevels[j-1], treeitem); |
2018 | } else { |
2019 | gtk_tree_append(GTK_TREE(tree), treeitem); |
2020 | } |
2021 | treeitemlevels[j] = GTK_TREE_ITEM(treeitem); |
2022 | treelevels[j] = NULL; |
2023 | level = j+1; |
2024 | |
0bd8d76d |
2025 | gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event", |
2026 | GTK_SIGNAL_FUNC(tree_key_press), &dp); |
2027 | gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event", |
2028 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
2029 | |
d9b15094 |
2030 | gtk_widget_show(treeitem); |
2031 | |
2032 | path = s->pathname; |
2033 | |
2034 | first = (panelvbox == NULL); |
2035 | |
2036 | panelvbox = gtk_vbox_new(FALSE, 4); |
2037 | gtk_container_add(GTK_CONTAINER(panels), panelvbox); |
2038 | if (first) { |
2039 | panels_switch_to(PANELS(panels), panelvbox); |
2040 | gtk_tree_select_child(GTK_TREE(tree), treeitem); |
2041 | } |
2042 | |
2043 | if (nselparams >= selparamsize) { |
2044 | selparamsize += 16; |
3d88e64d |
2045 | selparams = sresize(selparams, selparamsize, |
2046 | struct selparam); |
d9b15094 |
2047 | } |
1c291fcf |
2048 | selparams[nselparams].dp = &dp; |
d9b15094 |
2049 | selparams[nselparams].panels = PANELS(panels); |
2050 | selparams[nselparams].panel = panelvbox; |
1c291fcf |
2051 | selparams[nselparams].shortcuts = scs; /* structure copy */ |
d9b15094 |
2052 | selparams[nselparams].treeitem = treeitem; |
2053 | nselparams++; |
2054 | |
2055 | } |
2056 | |
1c291fcf |
2057 | w = layout_ctrls(&dp, |
2058 | &selparams[nselparams-1].shortcuts, |
2059 | s, listitemheight, NULL); |
d9b15094 |
2060 | gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0); |
1d0d4a3b |
2061 | gtk_widget_show(w); |
d9b15094 |
2062 | } |
2063 | } |
2064 | |
0bd8d76d |
2065 | dp.ntreeitems = nselparams; |
3d88e64d |
2066 | dp.treeitems = snewn(dp.ntreeitems, GtkWidget *); |
0bd8d76d |
2067 | |
d9b15094 |
2068 | for (index = 0; index < nselparams; index++) { |
2069 | gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select", |
2070 | GTK_SIGNAL_FUNC(treeitem_sel), |
2071 | &selparams[index]); |
0bd8d76d |
2072 | dp.treeitems[index] = selparams[index].treeitem; |
d9b15094 |
2073 | } |
2074 | |
0f5e1f75 |
2075 | dp.data = &cfg; |
2076 | dlg_refresh(NULL, &dp); |
2077 | |
1c291fcf |
2078 | dp.shortcuts = &selparams[0].shortcuts; |
0bd8d76d |
2079 | dp.currtreeitem = dp.treeitems[0]; |
2080 | dp.lastfocus = NULL; |
2081 | dp.retval = 0; |
23897ed0 |
2082 | dp.window = window; |
1c291fcf |
2083 | |
23897ed0 |
2084 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
d9b15094 |
2085 | gtk_widget_show(window); |
2086 | |
0bd8d76d |
2087 | /* |
2088 | * Set focus into the first available control. |
2089 | */ |
2090 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
2091 | struct controlset *s = ctrlbox->ctrlsets[index]; |
2092 | int done = 0; |
2093 | int j; |
2094 | |
2095 | if (*s->pathname) { |
2096 | for (j = 0; j < s->ncontrols; j++) |
2097 | if (s->ctrls[j]->generic.type != CTRL_TABDELAY && |
2098 | s->ctrls[j]->generic.type != CTRL_COLUMNS && |
2099 | s->ctrls[j]->generic.type != CTRL_TEXT) { |
2100 | dlg_set_focus(s->ctrls[j], &dp); |
2101 | dp.lastfocus = s->ctrls[j]; |
2102 | done = 1; |
2103 | break; |
2104 | } |
2105 | } |
2106 | if (done) |
2107 | break; |
2108 | } |
2109 | |
d9b15094 |
2110 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
0bd8d76d |
2111 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
1c291fcf |
2112 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
2113 | GTK_SIGNAL_FUNC(win_key_press), &dp); |
d9b15094 |
2114 | |
2115 | gtk_main(); |
2116 | |
0f5e1f75 |
2117 | dlg_cleanup(&dp); |
d9b15094 |
2118 | sfree(selparams); |
0bd8d76d |
2119 | |
2120 | return dp.retval; |
d9b15094 |
2121 | } |
2122 | |
2123 | /* ====================================================================== |
2124 | * Below here is a stub main program which allows the dialog box |
2125 | * code to be compiled and tested with a minimal amount of the rest |
2126 | * of PuTTY. |
2127 | */ |
2128 | |
2129 | #ifdef TESTMODE |
2130 | |
2131 | /* Compile command for testing: |
2132 | |
1d0d4a3b |
2133 | gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \ |
c5d64b49 |
2134 | ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \ |
2135 | -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs` |
2136 | |
d9b15094 |
2137 | */ |
2138 | |
2139 | void modalfatalbox(char *p, ...) |
2140 | { |
2141 | va_list ap; |
2142 | fprintf(stderr, "FATAL ERROR: "); |
2143 | va_start(ap, p); |
2144 | vfprintf(stderr, p, ap); |
2145 | va_end(ap); |
2146 | fputc('\n', stderr); |
2147 | exit(1); |
2148 | } |
2149 | |
2150 | char *cp_name(int codepage) |
2151 | { |
2152 | return (codepage == 123 ? "testing123" : |
2153 | codepage == 234 ? "testing234" : |
2154 | codepage == 345 ? "testing345" : |
2155 | "unknown"); |
2156 | } |
2157 | |
2158 | char *cp_enumerate(int index) |
2159 | { |
2160 | return (index == 0 ? "testing123" : |
2161 | index == 1 ? "testing234" : |
2162 | NULL); |
2163 | } |
2164 | |
2165 | int decode_codepage(char *cp_name) |
2166 | { |
2167 | return (!strcmp(cp_name, "testing123") ? 123 : |
2168 | !strcmp(cp_name, "testing234") ? 234 : |
2169 | !strcmp(cp_name, "testing345") ? 345 : |
2170 | -2); |
2171 | } |
2172 | |
2173 | struct printer_enum_tag { int dummy; } printer_test; |
2174 | |
2175 | printer_enum *printer_start_enum(int *nprinters_ptr) { |
2176 | *nprinters_ptr = 2; |
2177 | return &printer_test; |
2178 | } |
2179 | char *printer_get_name(printer_enum *pe, int i) { |
2180 | return (i==0 ? "lpr" : i==1 ? "lpr -Pfoobar" : NULL); |
2181 | } |
2182 | void printer_finish_enum(printer_enum *pe) { } |
2183 | |
2184 | char *platform_default_s(const char *name) |
2185 | { |
2186 | return NULL; |
2187 | } |
2188 | |
2189 | int platform_default_i(const char *name, int def) |
2190 | { |
2191 | return def; |
2192 | } |
2193 | |
2194 | FontSpec platform_default_fontspec(const char *name) |
2195 | { |
2196 | FontSpec ret; |
0f5e1f75 |
2197 | if (!strcmp(name, "Font")) |
2198 | strcpy(ret.name, "fixed"); |
2199 | else |
2200 | *ret.name = '\0'; |
d9b15094 |
2201 | return ret; |
2202 | } |
2203 | |
2204 | Filename platform_default_filename(const char *name) |
2205 | { |
2206 | Filename ret; |
0f5e1f75 |
2207 | if (!strcmp(name, "LogFileName")) |
2208 | strcpy(ret.path, "putty.log"); |
2209 | else |
2210 | *ret.path = '\0'; |
d9b15094 |
2211 | return ret; |
2212 | } |
2213 | |
2214 | char *x_get_default(const char *key) |
2215 | { |
2216 | return NULL; |
2217 | } |
2218 | |
2219 | int main(int argc, char **argv) |
2220 | { |
2221 | gtk_init(&argc, &argv); |
c5d64b49 |
2222 | printf("returned %d\n", do_config_box("PuTTY Configuration")); |
d9b15094 |
2223 | return 0; |
2224 | } |
2225 | |
2226 | #endif |