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