]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/gtkdlg.c
GTK 3 prep: do not include individual GTK/GDK headers.
[PuTTY.git] / unix / gtkdlg.c
1 /*
2  * gtkdlg.c - GTK implementation of the PuTTY configuration box.
3  */
4
5 #include <assert.h>
6 #include <stdarg.h>
7 #include <ctype.h>
8 #include <time.h>
9 #include <gtk/gtk.h>
10 #if !GTK_CHECK_VERSION(3,0,0)
11 #include <gdk/gdkkeysyms.h>
12 #endif
13 #include <gdk/gdkx.h>
14 #include <X11/Xlib.h>
15 #include <X11/Xutil.h>
16
17 #include "gtkcols.h"
18 #include "gtkfont.h"
19
20 #ifdef TESTMODE
21 #define PUTTY_DO_GLOBALS               /* actually _define_ globals */
22 #endif
23
24 #include "putty.h"
25 #include "storage.h"
26 #include "dialog.h"
27 #include "tree234.h"
28
29 struct Shortcut {
30     GtkWidget *widget;
31     struct uctrl *uc;
32     int action;
33 };
34
35 struct Shortcuts {
36     struct Shortcut sc[128];
37 };
38
39 struct uctrl {
40     union control *ctrl;
41     GtkWidget *toplevel;
42     GtkWidget **buttons; int nbuttons; /* for radio buttons */
43     GtkWidget *entry;         /* for editbox, filesel, fontsel */
44     GtkWidget *button;        /* for filesel, fontsel */
45 #if !GTK_CHECK_VERSION(2,4,0)
46     GtkWidget *list;          /* for listbox (in GTK1), combobox (<=GTK2.3) */
47     GtkWidget *menu;          /* for optionmenu (==droplist) */
48     GtkWidget *optmenu;       /* also for optionmenu */
49 #else
50     GtkWidget *combo;         /* for combo box (either editable or not) */
51 #endif
52 #if GTK_CHECK_VERSION(2,0,0)
53     GtkWidget *treeview;      /* for listbox (GTK2), droplist+combo (>=2.4) */
54     GtkListStore *listmodel;  /* for all types of list box */
55 #endif
56     GtkWidget *text;          /* for text */
57     GtkWidget *label;         /* for dlg_label_change */
58     GtkAdjustment *adj;       /* for the scrollbar in a list box */
59     guint entrysig;
60     guint textsig;
61     int nclicks;
62 };
63
64 struct dlgparam {
65     tree234 *byctrl, *bywidget;
66     void *data;
67     struct { unsigned char r, g, b, ok; } coloursel_result;   /* 0-255 */
68     /* `flags' are set to indicate when a GTK signal handler is being called
69      * due to automatic processing and should not flag a user event. */
70     int flags;
71     struct Shortcuts *shortcuts;
72     GtkWidget *window, *cancelbutton;
73     union control *currfocus, *lastfocus;
74 #if !GTK_CHECK_VERSION(2,0,0)
75     GtkWidget *currtreeitem, **treeitems;
76     int ntreeitems;
77 #endif
78     int retval;
79 };
80 #define FLAG_UPDATING_COMBO_LIST 1
81 #define FLAG_UPDATING_LISTBOX    2
82
83 enum {                                 /* values for Shortcut.action */
84     SHORTCUT_EMPTY,                    /* no shortcut on this key */
85     SHORTCUT_TREE,                     /* focus a tree item */
86     SHORTCUT_FOCUS,                    /* focus the supplied widget */
87     SHORTCUT_UCTRL,                    /* do something sane with uctrl */
88     SHORTCUT_UCTRL_UP,                 /* uctrl is a draglist, move Up */
89     SHORTCUT_UCTRL_DOWN,               /* uctrl is a draglist, move Down */
90 };
91
92 #if GTK_CHECK_VERSION(2,0,0)
93 enum {
94     TREESTORE_PATH,
95     TREESTORE_PARAMS,
96     TREESTORE_NUM
97 };
98 #endif
99
100 /*
101  * Forward references.
102  */
103 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
104                              gpointer data);
105 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
106                          int chr, int action, void *ptr);
107 static void shortcut_highlight(GtkWidget *label, int chr);
108 #if !GTK_CHECK_VERSION(2,0,0)
109 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
110                                     gpointer data);
111 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
112                                    gpointer data);
113 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
114                                       gpointer data);
115 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
116                                         gpointer data);
117 #endif
118 #if !GTK_CHECK_VERSION(2,4,0)
119 static void menuitem_activate(GtkMenuItem *item, gpointer data);
120 #endif
121 static void coloursel_ok(GtkButton *button, gpointer data);
122 static void coloursel_cancel(GtkButton *button, gpointer data);
123 static void window_destroy(GtkWidget *widget, gpointer data);
124 int get_listitemheight(GtkWidget *widget);
125
126 static int uctrl_cmp_byctrl(void *av, void *bv)
127 {
128     struct uctrl *a = (struct uctrl *)av;
129     struct uctrl *b = (struct uctrl *)bv;
130     if (a->ctrl < b->ctrl)
131         return -1;
132     else if (a->ctrl > b->ctrl)
133         return +1;
134     return 0;
135 }
136
137 static int uctrl_cmp_byctrl_find(void *av, void *bv)
138 {
139     union control *a = (union control *)av;
140     struct uctrl *b = (struct uctrl *)bv;
141     if (a < b->ctrl)
142         return -1;
143     else if (a > b->ctrl)
144         return +1;
145     return 0;
146 }
147
148 static int uctrl_cmp_bywidget(void *av, void *bv)
149 {
150     struct uctrl *a = (struct uctrl *)av;
151     struct uctrl *b = (struct uctrl *)bv;
152     if (a->toplevel < b->toplevel)
153         return -1;
154     else if (a->toplevel > b->toplevel)
155         return +1;
156     return 0;
157 }
158
159 static int uctrl_cmp_bywidget_find(void *av, void *bv)
160 {
161     GtkWidget *a = (GtkWidget *)av;
162     struct uctrl *b = (struct uctrl *)bv;
163     if (a < b->toplevel)
164         return -1;
165     else if (a > b->toplevel)
166         return +1;
167     return 0;
168 }
169
170 static void dlg_init(struct dlgparam *dp)
171 {
172     dp->byctrl = newtree234(uctrl_cmp_byctrl);
173     dp->bywidget = newtree234(uctrl_cmp_bywidget);
174     dp->coloursel_result.ok = FALSE;
175     dp->window = dp->cancelbutton = NULL;
176 #if !GTK_CHECK_VERSION(2,0,0)
177     dp->treeitems = NULL;
178     dp->currtreeitem = NULL;
179 #endif
180     dp->flags = 0;
181     dp->currfocus = NULL;
182 }
183
184 static void dlg_cleanup(struct dlgparam *dp)
185 {
186     struct uctrl *uc;
187
188     freetree234(dp->byctrl);           /* doesn't free the uctrls inside */
189     dp->byctrl = NULL;
190     while ( (uc = index234(dp->bywidget, 0)) != NULL) {
191         del234(dp->bywidget, uc);
192         sfree(uc->buttons);
193         sfree(uc);
194     }
195     freetree234(dp->bywidget);
196     dp->bywidget = NULL;
197 #if !GTK_CHECK_VERSION(2,0,0)
198     sfree(dp->treeitems);
199 #endif
200 }
201
202 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
203 {
204     add234(dp->byctrl, uc);
205     add234(dp->bywidget, uc);
206 }
207
208 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
209 {
210     if (!dp->byctrl)
211         return NULL;
212     return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
213 }
214
215 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
216 {
217     struct uctrl *ret = NULL;
218     if (!dp->bywidget)
219         return NULL;
220     do {
221         ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
222         if (ret)
223             return ret;
224         w = w->parent;
225     } while (w);
226     return ret;
227 }
228
229 union control *dlg_last_focused(union control *ctrl, void *dlg)
230 {
231     struct dlgparam *dp = (struct dlgparam *)dlg;
232     if (dp->currfocus != ctrl)
233         return dp->currfocus;
234     else
235         return dp->lastfocus;
236 }
237
238 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
239 {
240     struct dlgparam *dp = (struct dlgparam *)dlg;
241     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
242     assert(uc->ctrl->generic.type == CTRL_RADIO);
243     assert(uc->buttons != NULL);
244     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
245 }
246
247 int dlg_radiobutton_get(union control *ctrl, void *dlg)
248 {
249     struct dlgparam *dp = (struct dlgparam *)dlg;
250     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
251     int i;
252
253     assert(uc->ctrl->generic.type == CTRL_RADIO);
254     assert(uc->buttons != NULL);
255     for (i = 0; i < uc->nbuttons; i++)
256         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
257             return i;
258     return 0;                          /* got to return something */
259 }
260
261 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
262 {
263     struct dlgparam *dp = (struct dlgparam *)dlg;
264     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
265     assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
266     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
267 }
268
269 int dlg_checkbox_get(union control *ctrl, void *dlg)
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     return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
275 }
276
277 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
278 {
279     struct dlgparam *dp = (struct dlgparam *)dlg;
280     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
281     GtkWidget *entry;
282     char *tmpstring;
283     assert(uc->ctrl->generic.type == CTRL_EDITBOX);
284
285 #if GTK_CHECK_VERSION(2,4,0)
286     if (uc->combo)
287         entry = gtk_bin_get_child(GTK_BIN(uc->combo));
288     else
289 #endif
290     entry = uc->entry;
291
292     assert(entry != NULL);
293
294     /*
295      * GTK 2 implements gtk_entry_set_text by means of two separate
296      * operations: first delete the previous text leaving the empty
297      * string, then insert the new text. This causes two calls to
298      * the "changed" signal.
299      *
300      * The first call to "changed", if allowed to proceed normally,
301      * will cause an EVENT_VALCHANGE event on the edit box, causing
302      * a call to dlg_editbox_get() which will read the empty string
303      * out of the GtkEntry - and promptly write it straight into the
304      * Conf structure, which is precisely where our `text' pointer
305      * is probably pointing, so the second editing operation will
306      * insert that instead of the string we originally asked for.
307      *
308      * Hence, we must take our own copy of the text before we do
309      * this.
310      */
311     tmpstring = dupstr(text);
312     gtk_entry_set_text(GTK_ENTRY(entry), tmpstring);
313     sfree(tmpstring);
314 }
315
316 char *dlg_editbox_get(union control *ctrl, void *dlg)
317 {
318     struct dlgparam *dp = (struct dlgparam *)dlg;
319     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
320     assert(uc->ctrl->generic.type == CTRL_EDITBOX);
321
322 #if GTK_CHECK_VERSION(2,4,0)
323     if (uc->combo) {
324 #if GTK_CHECK_VERSION(2,6,0)
325         return dupstr(gtk_combo_box_get_active_text(GTK_COMBO_BOX(uc->combo)));
326 #else
327         return dupstr(gtk_entry_get_text
328                       (GTK_ENTRY(gtk_bin_get_child(GTK_BIN(uc->combo)))));
329 #endif
330     }
331 #endif
332
333     if (uc->entry) {
334         return dupstr(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
335     }
336
337     assert(!"We shouldn't get here");
338 }
339
340 #if !GTK_CHECK_VERSION(2,4,0)
341 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
342 {
343     GtkContainer *cont = GTK_CONTAINER(data);
344     /* gtk_container_remove will unref the widget for us; we need not. */
345     gtk_container_remove(cont, w);
346 }
347 #endif
348
349 /* The `listbox' functions can also apply to combo boxes. */
350 void dlg_listbox_clear(union control *ctrl, void *dlg)
351 {
352     struct dlgparam *dp = (struct dlgparam *)dlg;
353     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
354
355     assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
356            uc->ctrl->generic.type == CTRL_LISTBOX);
357
358 #if !GTK_CHECK_VERSION(2,4,0)
359     if (uc->menu) {
360         gtk_container_foreach(GTK_CONTAINER(uc->menu),
361                               container_remove_and_destroy,
362                               GTK_CONTAINER(uc->menu));
363         return;
364     }
365     if (uc->list) {
366         gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
367         return;
368     }
369 #endif
370 #if GTK_CHECK_VERSION(2,0,0)
371     if (uc->listmodel) {
372         gtk_list_store_clear(uc->listmodel);
373         return;
374     }
375 #endif
376     assert(!"We shouldn't get here");
377 }
378
379 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
380 {
381     struct dlgparam *dp = (struct dlgparam *)dlg;
382     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
383
384     assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
385            uc->ctrl->generic.type == CTRL_LISTBOX);
386
387 #if !GTK_CHECK_VERSION(2,4,0)
388     if (uc->menu) {
389         gtk_container_remove
390             (GTK_CONTAINER(uc->menu),
391              g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
392         return;
393     }
394     if (uc->list) {
395         gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
396         return;
397     }
398 #endif
399 #if GTK_CHECK_VERSION(2,0,0)
400     if (uc->listmodel) {
401         GtkTreePath *path;
402         GtkTreeIter iter;
403         assert(uc->listmodel != NULL);
404         path = gtk_tree_path_new_from_indices(index, -1);
405         gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
406         gtk_list_store_remove(uc->listmodel, &iter);
407         gtk_tree_path_free(path);
408         return;
409     }
410 #endif
411     assert(!"We shouldn't get here");
412 }
413
414 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
415 {
416     dlg_listbox_addwithid(ctrl, dlg, text, 0);
417 }
418
419 /*
420  * Each listbox entry may have a numeric id associated with it.
421  * Note that some front ends only permit a string to be stored at
422  * each position, which means that _if_ you put two identical
423  * strings in any listbox then you MUST not assign them different
424  * IDs and expect to get meaningful results back.
425  */
426 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
427                            char const *text, int id)
428 {
429     struct dlgparam *dp = (struct dlgparam *)dlg;
430     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
431
432     assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
433            uc->ctrl->generic.type == CTRL_LISTBOX);
434
435     /*
436      * This routine is long and complicated in both GTK 1 and 2,
437      * and completely different. Sigh.
438      */
439     dp->flags |= FLAG_UPDATING_COMBO_LIST;
440
441 #if !GTK_CHECK_VERSION(2,4,0)
442     if (uc->menu) {
443         /*
444          * List item in a drop-down (but non-combo) list. Tabs are
445          * ignored; we just provide a standard menu item with the
446          * text.
447          */
448         GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
449
450         gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
451         gtk_widget_show(menuitem);
452
453         gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
454                             GINT_TO_POINTER(id));
455         gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
456                            GTK_SIGNAL_FUNC(menuitem_activate), dp);
457         goto done;
458     }
459     if (uc->list && uc->entry) {
460         /*
461          * List item in a combo-box list, which means the sensible
462          * thing to do is make it a perfectly normal label. Hence
463          * tabs are disregarded.
464          */
465         GtkWidget *listitem = gtk_list_item_new_with_label(text);
466
467         gtk_container_add(GTK_CONTAINER(uc->list), listitem);
468         gtk_widget_show(listitem);
469
470         gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
471                             GINT_TO_POINTER(id));
472         goto done;
473     }
474 #endif
475 #if !GTK_CHECK_VERSION(2,0,0)
476     if (uc->list) {
477         /*
478          * List item in a non-combo-box list box. We make all of
479          * these Columns containing GtkLabels. This allows us to do
480          * the nasty force_left hack irrespective of whether there
481          * are tabs in the thing.
482          */
483         GtkWidget *listitem = gtk_list_item_new();
484         GtkWidget *cols = columns_new(10);
485         gint *percents;
486         int i, ncols;
487
488         /* Count the tabs in the text, and hence determine # of columns. */
489         ncols = 1;
490         for (i = 0; text[i]; i++)
491             if (text[i] == '\t')
492                 ncols++;
493
494         assert(ncols <=
495                (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
496         percents = snewn(ncols, gint);
497         percents[ncols-1] = 100;
498         for (i = 0; i < ncols-1; i++) {
499             percents[i] = uc->ctrl->listbox.percentages[i];
500             percents[ncols-1] -= percents[i];
501         }
502         columns_set_cols(COLUMNS(cols), ncols, percents);
503         sfree(percents);
504
505         for (i = 0; i < ncols; i++) {
506             int len = strcspn(text, "\t");
507             char *dup = dupprintf("%.*s", len, text);
508             GtkWidget *label;
509
510             text += len;
511             if (*text) text++;
512             label = gtk_label_new(dup);
513             sfree(dup);
514
515             columns_add(COLUMNS(cols), label, i, 1);
516             columns_force_left_align(COLUMNS(cols), label);
517             gtk_widget_show(label);
518         }
519         gtk_container_add(GTK_CONTAINER(listitem), cols);
520         gtk_widget_show(cols);
521         gtk_container_add(GTK_CONTAINER(uc->list), listitem);
522         gtk_widget_show(listitem);
523
524         if (ctrl->listbox.multisel) {
525             gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
526                                GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj);
527         } else {
528             gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
529                                GTK_SIGNAL_FUNC(listitem_single_key), uc->adj);
530         }
531         gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event",
532                            GTK_SIGNAL_FUNC(widget_focus), dp);
533         gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event",
534                            GTK_SIGNAL_FUNC(listitem_button_press), dp);
535         gtk_signal_connect(GTK_OBJECT(listitem), "button_release_event",
536                            GTK_SIGNAL_FUNC(listitem_button_release), dp);
537         gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
538                             GINT_TO_POINTER(id));
539         goto done;
540     }
541 #else
542     if (uc->listmodel) {
543         GtkTreeIter iter;
544         int i, cols;
545
546         dp->flags |= FLAG_UPDATING_LISTBOX;/* inhibit drag-list update */
547         gtk_list_store_append(uc->listmodel, &iter);
548         dp->flags &= ~FLAG_UPDATING_LISTBOX;
549         gtk_list_store_set(uc->listmodel, &iter, 0, id, -1);
550
551         /*
552          * Now go through text and divide it into columns at the tabs,
553          * as necessary.
554          */
555         cols = (uc->ctrl->generic.type == CTRL_LISTBOX ? ctrl->listbox.ncols : 1);
556         cols = cols ? cols : 1;
557         for (i = 0; i < cols; i++) {
558             int collen = strcspn(text, "\t");
559             char *tmpstr = snewn(collen+1, char);
560             memcpy(tmpstr, text, collen);
561             tmpstr[collen] = '\0';
562             gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1);
563             sfree(tmpstr);
564             text += collen;
565             if (*text) text++;
566         }
567         goto done;
568     }
569 #endif
570     assert(!"We shouldn't get here");
571     done:
572     dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
573 }
574
575 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
576 {
577     struct dlgparam *dp = (struct dlgparam *)dlg;
578     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
579
580     assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
581            uc->ctrl->generic.type == CTRL_LISTBOX);
582
583 #if !GTK_CHECK_VERSION(2,4,0)
584     if (uc->menu || uc->list) {
585         GList *children;
586         GtkObject *item;
587
588         children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
589                                                         uc->list));
590         item = GTK_OBJECT(g_list_nth_data(children, index));
591         g_list_free(children);
592
593         return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
594                                                    "user-data"));
595     }
596 #endif
597 #if GTK_CHECK_VERSION(2,0,0)
598     if (uc->listmodel) {
599         GtkTreePath *path;
600         GtkTreeIter iter;
601         int ret;
602
603         path = gtk_tree_path_new_from_indices(index, -1);
604         gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
605         gtk_tree_model_get(GTK_TREE_MODEL(uc->listmodel), &iter, 0, &ret, -1);
606         gtk_tree_path_free(path);
607
608         return ret;
609     }
610 #endif
611     assert(!"We shouldn't get here");
612     return -1;                         /* placate dataflow analysis */
613 }
614
615 /* dlg_listbox_index returns <0 if no single element is selected. */
616 int dlg_listbox_index(union control *ctrl, void *dlg)
617 {
618     struct dlgparam *dp = (struct dlgparam *)dlg;
619     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
620
621     assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
622            uc->ctrl->generic.type == CTRL_LISTBOX);
623
624 #if !GTK_CHECK_VERSION(2,4,0)
625     if (uc->menu || uc->list) {
626         GList *children;
627         GtkWidget *item, *activeitem;
628         int i;
629         int selected = -1;
630
631         if (uc->menu)
632             activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
633         else
634             activeitem = NULL;         /* unnecessarily placate gcc */
635
636         children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
637                                                         uc->list));
638         for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
639              i++, children = children->next) {
640             if (uc->menu ? activeitem == item :
641                 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
642                 if (selected == -1)
643                     selected = i;
644                 else
645                     selected = -2;
646             }
647         }
648         g_list_free(children);
649         return selected < 0 ? -1 : selected;
650     }
651 #else
652     if (uc->combo) {
653         /*
654          * This API function already does the right thing in the
655          * case of no current selection.
656          */
657         return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo));
658     }
659 #endif
660 #if GTK_CHECK_VERSION(2,0,0)
661     if (uc->treeview) {
662         GtkTreeSelection *treesel;
663         GtkTreePath *path;
664         GtkTreeModel *model;
665         GList *sellist;
666         gint *indices;
667         int ret;
668
669         assert(uc->treeview != NULL);
670         treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
671
672         if (gtk_tree_selection_count_selected_rows(treesel) != 1)
673             return -1;
674
675         sellist = gtk_tree_selection_get_selected_rows(treesel, &model);
676
677         assert(sellist && sellist->data);
678         path = sellist->data;
679
680         if (gtk_tree_path_get_depth(path) != 1) {
681             ret = -1;
682         } else {
683             indices = gtk_tree_path_get_indices(path);
684             if (!indices) {
685                 ret = -1;
686             } else {
687                 ret = indices[0];
688             }
689         }
690
691         g_list_foreach(sellist, (GFunc)gtk_tree_path_free, NULL);
692         g_list_free(sellist);
693
694         return ret;
695     }
696 #endif
697     assert(!"We shouldn't get here");
698     return -1;                         /* placate dataflow analysis */
699 }
700
701 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
702 {
703     struct dlgparam *dp = (struct dlgparam *)dlg;
704     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
705
706     assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
707            uc->ctrl->generic.type == CTRL_LISTBOX);
708
709 #if !GTK_CHECK_VERSION(2,4,0)
710     if (uc->menu || uc->list) {
711         GList *children;
712         GtkWidget *item, *activeitem;
713
714         assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
715                uc->ctrl->generic.type == CTRL_LISTBOX);
716         assert(uc->menu != NULL || uc->list != NULL);
717
718         children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
719                                                         uc->list));
720         item = GTK_WIDGET(g_list_nth_data(children, index));
721         g_list_free(children);
722
723         if (uc->menu) {
724             activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
725             return item == activeitem;
726         } else {
727             return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
728         }
729     }
730 #else
731     if (uc->combo) {
732         /*
733          * This API function already does the right thing in the
734          * case of no current selection.
735          */
736         return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo)) == index;
737     }
738 #endif
739 #if GTK_CHECK_VERSION(2,0,0)
740     if (uc->treeview) {
741         GtkTreeSelection *treesel;
742         GtkTreePath *path;
743         int ret;
744
745         assert(uc->treeview != NULL);
746         treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
747
748         path = gtk_tree_path_new_from_indices(index, -1);
749         ret = gtk_tree_selection_path_is_selected(treesel, path);
750         gtk_tree_path_free(path);
751
752         return ret;
753     }
754 #endif
755     assert(!"We shouldn't get here");
756     return -1;                         /* placate dataflow analysis */
757 }
758
759 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
760 {
761     struct dlgparam *dp = (struct dlgparam *)dlg;
762     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
763
764     assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
765            uc->ctrl->generic.type == CTRL_LISTBOX);
766
767 #if !GTK_CHECK_VERSION(2,4,0)
768     if (uc->optmenu) {
769         gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
770         return;
771     } 
772     if (uc->list) {
773         int nitems;
774         GList *items;
775         gdouble newtop, newbot;
776
777         gtk_list_select_item(GTK_LIST(uc->list), index);
778
779         /*
780          * Scroll the list box if necessary to ensure the newly
781          * selected item is visible.
782          */
783         items = gtk_container_children(GTK_CONTAINER(uc->list));
784         nitems = g_list_length(items);
785         if (nitems > 0) {
786             int modified = FALSE;
787             g_list_free(items);
788             newtop = uc->adj->lower +
789                 (uc->adj->upper - uc->adj->lower) * index / nitems;
790             newbot = uc->adj->lower +
791                 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
792             if (uc->adj->value > newtop) {
793                 modified = TRUE;
794                 uc->adj->value = newtop;
795             } else if (uc->adj->value < newbot - uc->adj->page_size) {
796                 modified = TRUE;
797                 uc->adj->value = newbot - uc->adj->page_size;
798             }
799             if (modified)
800                 gtk_adjustment_value_changed(uc->adj);
801         }
802         return;
803     }
804 #else
805     if (uc->combo) {
806         gtk_combo_box_set_active(GTK_COMBO_BOX(uc->combo), index);
807         return;
808     }
809 #endif
810 #if GTK_CHECK_VERSION(2,0,0)
811     if (uc->treeview) {
812         GtkTreeSelection *treesel;
813         GtkTreePath *path;
814
815         treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
816
817         path = gtk_tree_path_new_from_indices(index, -1);
818         gtk_tree_selection_select_path(treesel, path);
819         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc->treeview),
820                                      path, NULL, FALSE, 0.0, 0.0);
821         gtk_tree_path_free(path);
822         return;
823     }
824 #endif
825     assert(!"We shouldn't get here");
826 }
827
828 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
829 {
830     struct dlgparam *dp = (struct dlgparam *)dlg;
831     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
832
833     assert(uc->ctrl->generic.type == CTRL_TEXT);
834     assert(uc->text != NULL);
835
836     gtk_label_set_text(GTK_LABEL(uc->text), text);
837 }
838
839 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
840 {
841     struct dlgparam *dp = (struct dlgparam *)dlg;
842     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
843
844     switch (uc->ctrl->generic.type) {
845       case CTRL_BUTTON:
846         gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
847         shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
848         break;
849       case CTRL_CHECKBOX:
850         gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
851         shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
852         break;
853       case CTRL_RADIO:
854         gtk_label_set_text(GTK_LABEL(uc->label), text);
855         shortcut_highlight(uc->label, ctrl->radio.shortcut);
856         break;
857       case CTRL_EDITBOX:
858         gtk_label_set_text(GTK_LABEL(uc->label), text);
859         shortcut_highlight(uc->label, ctrl->editbox.shortcut);
860         break;
861       case CTRL_FILESELECT:
862         gtk_label_set_text(GTK_LABEL(uc->label), text);
863         shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
864         break;
865       case CTRL_FONTSELECT:
866         gtk_label_set_text(GTK_LABEL(uc->label), text);
867         shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
868         break;
869       case CTRL_LISTBOX:
870         gtk_label_set_text(GTK_LABEL(uc->label), text);
871         shortcut_highlight(uc->label, ctrl->listbox.shortcut);
872         break;
873       default:
874         assert(!"This shouldn't happen");
875         break;
876     }
877 }
878
879 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
880 {
881     struct dlgparam *dp = (struct dlgparam *)dlg;
882     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
883     /* We must copy fn->path before passing it to gtk_entry_set_text.
884      * See comment in dlg_editbox_set() for the reasons. */
885     char *duppath = dupstr(fn->path);
886     assert(uc->ctrl->generic.type == CTRL_FILESELECT);
887     assert(uc->entry != NULL);
888     gtk_entry_set_text(GTK_ENTRY(uc->entry), duppath);
889     sfree(duppath);
890 }
891
892 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
893 {
894     struct dlgparam *dp = (struct dlgparam *)dlg;
895     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
896     assert(uc->ctrl->generic.type == CTRL_FILESELECT);
897     assert(uc->entry != NULL);
898     return filename_from_str(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
899 }
900
901 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
902 {
903     struct dlgparam *dp = (struct dlgparam *)dlg;
904     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
905     /* We must copy fs->name before passing it to gtk_entry_set_text.
906      * See comment in dlg_editbox_set() for the reasons. */
907     char *dupname = dupstr(fs->name);
908     assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
909     assert(uc->entry != NULL);
910     gtk_entry_set_text(GTK_ENTRY(uc->entry), dupname);
911     sfree(dupname);
912 }
913
914 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
915 {
916     struct dlgparam *dp = (struct dlgparam *)dlg;
917     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
918     assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
919     assert(uc->entry != NULL);
920     return fontspec_new(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
921 }
922
923 /*
924  * Bracketing a large set of updates in these two functions will
925  * cause the front end (if possible) to delay updating the screen
926  * until it's all complete, thus avoiding flicker.
927  */
928 void dlg_update_start(union control *ctrl, void *dlg)
929 {
930     /*
931      * Apparently we can't do this at all in GTK. GtkCList supports
932      * freeze and thaw, but not GtkList. Bah.
933      */
934 }
935
936 void dlg_update_done(union control *ctrl, void *dlg)
937 {
938     /*
939      * Apparently we can't do this at all in GTK. GtkCList supports
940      * freeze and thaw, but not GtkList. Bah.
941      */
942 }
943
944 void dlg_set_focus(union control *ctrl, void *dlg)
945 {
946     struct dlgparam *dp = (struct dlgparam *)dlg;
947     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
948
949     switch (ctrl->generic.type) {
950       case CTRL_CHECKBOX:
951       case CTRL_BUTTON:
952         /* Check boxes and buttons get the focus _and_ get toggled. */
953         gtk_widget_grab_focus(uc->toplevel);
954         break;
955       case CTRL_FILESELECT:
956       case CTRL_FONTSELECT:
957       case CTRL_EDITBOX:
958         if (uc->entry) {
959             /* Anything containing an edit box gets that focused. */
960             gtk_widget_grab_focus(uc->entry);
961         }
962 #if GTK_CHECK_VERSION(2,4,0)
963         else if (uc->combo) {
964             /* Failing that, there'll be a combo box. */
965             gtk_widget_grab_focus(uc->combo);
966         }
967 #endif
968         break;
969       case CTRL_RADIO:
970         /*
971          * Radio buttons: we find the currently selected button and
972          * focus it.
973          */
974         {
975             int i;
976             for (i = 0; i < ctrl->radio.nbuttons; i++)
977                 if (gtk_toggle_button_get_active
978                     (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
979                     gtk_widget_grab_focus(uc->buttons[i]);
980                 }
981         }
982         break;
983       case CTRL_LISTBOX:
984 #if !GTK_CHECK_VERSION(2,4,0)
985         if (uc->optmenu) {
986             gtk_widget_grab_focus(uc->optmenu);
987             break;
988         }
989 #else
990         if (uc->combo) {
991             gtk_widget_grab_focus(uc->combo);
992             break;
993         }
994 #endif
995 #if !GTK_CHECK_VERSION(2,0,0)
996         if (uc->list) {
997             /*
998              * For GTK-1 style list boxes, we tell it to focus one
999              * of its children, which appears to do the Right
1000              * Thing.
1001              */
1002             gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
1003             break;
1004         }
1005 #else
1006         if (uc->treeview) {
1007             gtk_widget_grab_focus(uc->treeview);
1008             break;
1009         }
1010 #endif
1011         assert(!"We shouldn't get here");
1012         break;
1013     }
1014 }
1015
1016 /*
1017  * During event processing, you might well want to give an error
1018  * indication to the user. dlg_beep() is a quick and easy generic
1019  * error; dlg_error() puts up a message-box or equivalent.
1020  */
1021 void dlg_beep(void *dlg)
1022 {
1023     gdk_beep();
1024 }
1025
1026 static void errmsg_button_clicked(GtkButton *button, gpointer data)
1027 {
1028     gtk_widget_destroy(GTK_WIDGET(data));
1029 }
1030
1031 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
1032 {
1033     gint x, y, w, h, dx, dy;
1034     GtkRequisition req;
1035     gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
1036     gtk_widget_size_request(GTK_WIDGET(child), &req);
1037
1038     gdk_window_get_origin(GTK_WIDGET(parent)->window, &x, &y);
1039     gdk_window_get_size(GTK_WIDGET(parent)->window, &w, &h);
1040
1041     /*
1042      * One corner of the transient will be offset inwards, by 1/4
1043      * of the parent window's size, from the corresponding corner
1044      * of the parent window. The corner will be chosen so as to
1045      * place the transient closer to the centre of the screen; this
1046      * should avoid transients going off the edge of the screen on
1047      * a regular basis.
1048      */
1049     if (x + w/2 < gdk_screen_width() / 2)
1050         dx = x + w/4;                  /* work from left edges */
1051     else
1052         dx = x + 3*w/4 - req.width;    /* work from right edges */
1053     if (y + h/2 < gdk_screen_height() / 2)
1054         dy = y + h/4;                  /* work from top edges */
1055     else
1056         dy = y + 3*h/4 - req.height;   /* work from bottom edges */
1057     gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
1058 }
1059
1060 void dlg_error_msg(void *dlg, const char *msg)
1061 {
1062     struct dlgparam *dp = (struct dlgparam *)dlg;
1063     GtkWidget *window, *hbox, *text, *ok;
1064
1065     window = gtk_dialog_new();
1066     text = gtk_label_new(msg);
1067     gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1068     hbox = gtk_hbox_new(FALSE, 0);
1069     gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1070     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
1071                        hbox, FALSE, FALSE, 20);
1072     gtk_widget_show(text);
1073     gtk_widget_show(hbox);
1074     gtk_window_set_title(GTK_WINDOW(window), "Error");
1075     gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1076     ok = gtk_button_new_with_label("OK");
1077     gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
1078                      ok, FALSE, FALSE, 0);
1079     gtk_widget_show(ok);
1080     GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
1081     gtk_window_set_default(GTK_WINDOW(window), ok);
1082     gtk_signal_connect(GTK_OBJECT(ok), "clicked",
1083                        GTK_SIGNAL_FUNC(errmsg_button_clicked), window);
1084     gtk_signal_connect(GTK_OBJECT(window), "destroy",
1085                        GTK_SIGNAL_FUNC(window_destroy), NULL);
1086     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1087     gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
1088     set_transient_window_pos(dp->window, window);
1089     gtk_widget_show(window);
1090     gtk_main();
1091 }
1092
1093 /*
1094  * This function signals to the front end that the dialog's
1095  * processing is completed, and passes an integer value (typically
1096  * a success status).
1097  */
1098 void dlg_end(void *dlg, int value)
1099 {
1100     struct dlgparam *dp = (struct dlgparam *)dlg;
1101     dp->retval = value;
1102     gtk_widget_destroy(dp->window);
1103 }
1104
1105 void dlg_refresh(union control *ctrl, void *dlg)
1106 {
1107     struct dlgparam *dp = (struct dlgparam *)dlg;
1108     struct uctrl *uc;
1109
1110     if (ctrl) {
1111         if (ctrl->generic.handler != NULL)
1112             ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1113     } else {
1114         int i;
1115
1116         for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
1117             assert(uc->ctrl != NULL);
1118             if (uc->ctrl->generic.handler != NULL)
1119                 uc->ctrl->generic.handler(uc->ctrl, dp,
1120                                           dp->data, EVENT_REFRESH);
1121         }
1122     }
1123 }
1124
1125 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
1126 {
1127     struct dlgparam *dp = (struct dlgparam *)dlg;
1128     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
1129     gdouble cvals[4];
1130
1131     GtkWidget *coloursel =
1132         gtk_color_selection_dialog_new("Select a colour");
1133     GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
1134
1135     dp->coloursel_result.ok = FALSE;
1136
1137     gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
1138 #if GTK_CHECK_VERSION(2,0,0)
1139     gtk_color_selection_set_has_opacity_control(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
1140 #else
1141     gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
1142 #endif
1143     cvals[0] = r / 255.0;
1144     cvals[1] = g / 255.0;
1145     cvals[2] = b / 255.0;
1146     cvals[3] = 1.0;                    /* fully opaque! */
1147     gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals);
1148
1149     gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data",
1150                         (gpointer)coloursel);
1151     gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data",
1152                         (gpointer)coloursel);
1153     gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc);
1154     gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked",
1155                        GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp);
1156     gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked",
1157                        GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp);
1158     gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked",
1159                               GTK_SIGNAL_FUNC(gtk_widget_destroy),
1160                               (gpointer)coloursel);
1161     gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked",
1162                               GTK_SIGNAL_FUNC(gtk_widget_destroy),
1163                               (gpointer)coloursel);
1164     gtk_widget_show(coloursel);
1165 }
1166
1167 int dlg_coloursel_results(union control *ctrl, void *dlg,
1168                           int *r, int *g, int *b)
1169 {
1170     struct dlgparam *dp = (struct dlgparam *)dlg;
1171     if (dp->coloursel_result.ok) {
1172         *r = dp->coloursel_result.r;
1173         *g = dp->coloursel_result.g;
1174         *b = dp->coloursel_result.b;
1175         return 1;
1176     } else
1177         return 0;
1178 }
1179
1180 /* ----------------------------------------------------------------------
1181  * Signal handlers while the dialog box is active.
1182  */
1183
1184 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
1185                              gpointer data)
1186 {
1187     struct dlgparam *dp = (struct dlgparam *)data;
1188     struct uctrl *uc = dlg_find_bywidget(dp, widget);
1189     union control *focus;
1190
1191     if (uc && uc->ctrl)
1192         focus = uc->ctrl;
1193     else
1194         focus = NULL;
1195
1196     if (focus != dp->currfocus) {
1197         dp->lastfocus = dp->currfocus;
1198         dp->currfocus = focus;
1199     }
1200
1201     return FALSE;
1202 }
1203
1204 static void button_clicked(GtkButton *button, gpointer data)
1205 {
1206     struct dlgparam *dp = (struct dlgparam *)data;
1207     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1208     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1209 }
1210
1211 static void button_toggled(GtkToggleButton *tb, gpointer data)
1212 {
1213     struct dlgparam *dp = (struct dlgparam *)data;
1214     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
1215     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1216 }
1217
1218 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
1219                             gpointer data)
1220 {
1221     /*
1222      * GtkEntry has a nasty habit of eating the Return key, which
1223      * is unhelpful since it doesn't actually _do_ anything with it
1224      * (it calls gtk_widget_activate, but our edit boxes never need
1225      * activating). So I catch Return before GtkEntry sees it, and
1226      * pass it straight on to the parent widget. Effect: hitting
1227      * Return in an edit box will now activate the default button
1228      * in the dialog just like it will everywhere else.
1229      */
1230     if (event->keyval == GDK_Return && widget->parent != NULL) {
1231         gboolean return_val;
1232         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1233         gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
1234                                 event, &return_val);
1235         return return_val;
1236     }
1237     return FALSE;
1238 }
1239
1240 static void editbox_changed(GtkEditable *ed, gpointer data)
1241 {
1242     struct dlgparam *dp = (struct dlgparam *)data;
1243     if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1244         struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1245         uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1246     }
1247 }
1248
1249 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1250                                   gpointer data)
1251 {
1252     struct dlgparam *dp = (struct dlgparam *)data;
1253     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1254     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1255     return FALSE;
1256 }
1257
1258 #if !GTK_CHECK_VERSION(2,0,0)
1259
1260 /*
1261  * GTK 1 list box event handlers.
1262  */
1263
1264 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1265                              gpointer data, int multiple)
1266 {
1267     GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1268
1269     if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1270         event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1271         event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1272         event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1273         /*
1274          * Up, Down, PgUp or PgDn have been pressed on a ListItem
1275          * in a list box. So, if the list box is single-selection:
1276          * 
1277          *  - if the list item in question isn't already selected,
1278          *    we simply select it.
1279          *  - otherwise, we find the next one (or next
1280          *    however-far-away) in whichever direction we're going,
1281          *    and select that.
1282          *     + in this case, we must also fiddle with the
1283          *       scrollbar to ensure the newly selected item is
1284          *       actually visible.
1285          * 
1286          * If it's multiple-selection, we do all of the above
1287          * except actually selecting anything, so we move the focus
1288          * and fiddle the scrollbar to follow it.
1289          */
1290         GtkWidget *list = item->parent;
1291
1292         gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event");
1293
1294         if (!multiple &&
1295             GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1296                 gtk_list_select_child(GTK_LIST(list), item);
1297         } else {
1298             int direction =
1299                 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1300                  event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1301                 ? -1 : +1;
1302             int step =
1303                 (event->keyval==GDK_Page_Down || 
1304                  event->keyval==GDK_KP_Page_Down ||
1305                  event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1306                 ? 2 : 1;
1307             int i, n;
1308             GList *children, *chead;
1309
1310             chead = children = gtk_container_children(GTK_CONTAINER(list));
1311
1312             n = g_list_length(children);
1313
1314             if (step == 2) {
1315                 /*
1316                  * Figure out how many list items to a screenful,
1317                  * and adjust the step appropriately.
1318                  */
1319                 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1320                 step--;                /* go by one less than that */
1321             }
1322
1323             i = 0;
1324             while (children != NULL) {
1325                 if (item == children->data)
1326                     break;
1327                 children = children->next;
1328                 i++;
1329             }
1330
1331             while (step > 0) {
1332                 if (direction < 0 && i > 0)
1333                     children = children->prev, i--;
1334                 else if (direction > 0 && i < n-1)
1335                     children = children->next, i++;
1336                 step--;
1337             }
1338
1339             if (children && children->data) {
1340                 if (!multiple)
1341                     gtk_list_select_child(GTK_LIST(list),
1342                                           GTK_WIDGET(children->data));
1343                 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1344                 gtk_adjustment_clamp_page
1345                     (adj,
1346                      adj->lower + (adj->upper-adj->lower) * i / n,
1347                      adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1348             }
1349
1350             g_list_free(chead);
1351         }
1352         return TRUE;
1353     }
1354
1355     return FALSE;
1356 }
1357
1358 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1359                                     gpointer data)
1360 {
1361     return listitem_key(item, event, data, FALSE);
1362 }
1363
1364 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1365                                    gpointer data)
1366 {
1367     return listitem_key(item, event, data, TRUE);
1368 }
1369
1370 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1371                                       gpointer data)
1372 {
1373     struct dlgparam *dp = (struct dlgparam *)data;
1374     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1375     switch (event->type) {
1376     default:
1377     case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1378     case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1379     case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1380     }
1381     return FALSE;
1382 }
1383
1384 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1385                                         gpointer data)
1386 {
1387     struct dlgparam *dp = (struct dlgparam *)data;
1388     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1389     if (uc->nclicks>1) {
1390         uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1391         return TRUE;
1392     }
1393     return FALSE;
1394 }
1395
1396 static void list_selchange(GtkList *list, gpointer data)
1397 {
1398     struct dlgparam *dp = (struct dlgparam *)data;
1399     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1400     if (!uc) return;
1401     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1402 }
1403
1404 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1405 {
1406     int index = dlg_listbox_index(uc->ctrl, dp);
1407     GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1408     GtkWidget *child;
1409
1410     if ((index < 0) ||
1411         (index == 0 && direction < 0) ||
1412         (index == g_list_length(children)-1 && direction > 0)) {
1413         gdk_beep();
1414         return;
1415     }
1416
1417     child = g_list_nth_data(children, index);
1418     gtk_widget_ref(child);
1419     gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1420     g_list_free(children);
1421
1422     children = NULL;
1423     children = g_list_append(children, child);
1424     gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1425     gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1426     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1427 }
1428
1429 static void draglist_up(GtkButton *button, gpointer data)
1430 {
1431     struct dlgparam *dp = (struct dlgparam *)data;
1432     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1433     draglist_move(dp, uc, -1);
1434 }
1435
1436 static void draglist_down(GtkButton *button, gpointer data)
1437 {
1438     struct dlgparam *dp = (struct dlgparam *)data;
1439     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1440     draglist_move(dp, uc, +1);
1441 }
1442
1443 #else /* !GTK_CHECK_VERSION(2,0,0) */
1444
1445 /*
1446  * GTK 2 list box event handlers.
1447  */
1448
1449 static void listbox_doubleclick(GtkTreeView *treeview, GtkTreePath *path,
1450                                 GtkTreeViewColumn *column, gpointer data)
1451 {
1452     struct dlgparam *dp = (struct dlgparam *)data;
1453     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(treeview));
1454     if (uc)
1455         uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1456 }
1457
1458 static void listbox_selchange(GtkTreeSelection *treeselection,
1459                               gpointer data)
1460 {
1461     struct dlgparam *dp = (struct dlgparam *)data;
1462     GtkTreeView *tree = gtk_tree_selection_get_tree_view(treeselection);
1463     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1464     if (uc)
1465         uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1466 }
1467
1468 struct draglist_valchange_ctx {
1469     struct uctrl *uc;
1470     struct dlgparam *dp;
1471 };
1472
1473 static gboolean draglist_valchange(gpointer data)
1474 {
1475     struct draglist_valchange_ctx *ctx =
1476         (struct draglist_valchange_ctx *)data;
1477
1478     ctx->uc->ctrl->generic.handler(ctx->uc->ctrl, ctx->dp,
1479                                    ctx->dp->data, EVENT_VALCHANGE);
1480
1481     sfree(ctx);
1482
1483     return FALSE;
1484 }
1485
1486 static void listbox_reorder(GtkTreeModel *treemodel, GtkTreePath *path,
1487                             GtkTreeIter *iter, gpointer data)
1488 {
1489     struct dlgparam *dp = (struct dlgparam *)data;
1490     gpointer tree;
1491     struct uctrl *uc;
1492
1493     if (dp->flags & FLAG_UPDATING_LISTBOX)
1494         return;                        /* not a user drag operation */
1495
1496     tree = g_object_get_data(G_OBJECT(treemodel), "user-data");
1497     uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1498     if (uc) {
1499         /*
1500          * We should cause EVENT_VALCHANGE on the list box, now
1501          * that its rows have been reordered. However, the GTK 2
1502          * docs say that at the point this signal is received the
1503          * new row might not have actually been filled in yet.
1504          *
1505          * (So what smegging use is it then, eh? Don't suppose it
1506          * occurred to you at any point that letting the
1507          * application know _after_ the reordering was compelete
1508          * might be helpful to someone?)
1509          *
1510          * To get round this, I schedule an idle function, which I
1511          * hope won't be called until the main event loop is
1512          * re-entered after the drag-and-drop handler has finished
1513          * furtling with the list store.
1514          */
1515         struct draglist_valchange_ctx *ctx =
1516             snew(struct draglist_valchange_ctx);
1517         ctx->uc = uc;
1518         ctx->dp = dp;
1519         g_idle_add(draglist_valchange, ctx);
1520     }
1521 }
1522
1523 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1524
1525 #if !GTK_CHECK_VERSION(2,4,0)
1526
1527 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1528 {
1529     struct dlgparam *dp = (struct dlgparam *)data;
1530     GtkWidget *menushell = GTK_WIDGET(item)->parent;
1531     gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data");
1532     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1533     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1534 }
1535
1536 #else
1537
1538 static void droplist_selchange(GtkComboBox *combo, gpointer data)
1539 {
1540     struct dlgparam *dp = (struct dlgparam *)data;
1541     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(combo));
1542     if (uc)
1543         uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1544 }
1545
1546 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1547
1548 static void filesel_ok(GtkButton *button, gpointer data)
1549 {
1550     /* struct dlgparam *dp = (struct dlgparam *)data; */
1551     gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1552     struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data");
1553     const char *name = gtk_file_selection_get_filename
1554         (GTK_FILE_SELECTION(filesel));
1555     gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1556 }
1557
1558 static void fontsel_ok(GtkButton *button, gpointer data)
1559 {
1560     /* struct dlgparam *dp = (struct dlgparam *)data; */
1561
1562 #if !GTK_CHECK_VERSION(2,0,0)
1563
1564     gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1565     struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data");
1566     const char *name = gtk_font_selection_dialog_get_font_name
1567         (GTK_FONT_SELECTION_DIALOG(fontsel));
1568     gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1569
1570 #else
1571
1572     unifontsel *fontsel = (unifontsel *)gtk_object_get_data
1573         (GTK_OBJECT(button), "user-data");
1574     struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1575     char *name = unifontsel_get_name(fontsel);
1576     assert(name);              /* should always be ok after OK pressed */
1577     gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1578     sfree(name);
1579
1580 #endif
1581 }
1582
1583 static void coloursel_ok(GtkButton *button, gpointer data)
1584 {
1585     struct dlgparam *dp = (struct dlgparam *)data;
1586     gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1587     struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1588     gdouble cvals[4];
1589     gtk_color_selection_get_color
1590         (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel),
1591          cvals);
1592     dp->coloursel_result.r = (int) (255 * cvals[0]);
1593     dp->coloursel_result.g = (int) (255 * cvals[1]);
1594     dp->coloursel_result.b = (int) (255 * cvals[2]);
1595     dp->coloursel_result.ok = TRUE;
1596     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1597 }
1598
1599 static void coloursel_cancel(GtkButton *button, gpointer data)
1600 {
1601     struct dlgparam *dp = (struct dlgparam *)data;
1602     gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1603     struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1604     dp->coloursel_result.ok = FALSE;
1605     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1606 }
1607
1608 static void filefont_clicked(GtkButton *button, gpointer data)
1609 {
1610     struct dlgparam *dp = (struct dlgparam *)data;
1611     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1612
1613     if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1614         GtkWidget *filesel =
1615             gtk_file_selection_new(uc->ctrl->fileselect.title);
1616         gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1617         gtk_object_set_data
1618             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1619              (gpointer)filesel);
1620         gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc);
1621         gtk_signal_connect
1622             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1623              GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp);
1624         gtk_signal_connect_object
1625             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1626              GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1627         gtk_signal_connect_object
1628             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1629              GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1630         gtk_widget_show(filesel);
1631     }
1632
1633     if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1634         const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1635
1636 #if !GTK_CHECK_VERSION(2,0,0)
1637
1638         /*
1639          * Use the GTK 1 standard font selector.
1640          */
1641
1642         gchar *spacings[] = { "c", "m", NULL };
1643         GtkWidget *fontsel =
1644             gtk_font_selection_dialog_new("Select a font");
1645         gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1646         gtk_font_selection_dialog_set_filter
1647             (GTK_FONT_SELECTION_DIALOG(fontsel),
1648              GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1649              NULL, NULL, NULL, NULL, spacings, NULL);
1650         if (!gtk_font_selection_dialog_set_font_name
1651             (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1652             /*
1653              * If the font name wasn't found as it was, try opening
1654              * it and extracting its FONT property. This should
1655              * have the effect of mapping short aliases into true
1656              * XLFDs.
1657              */
1658             GdkFont *font = gdk_font_load(fontname);
1659             if (font) {
1660                 XFontStruct *xfs = GDK_FONT_XFONT(font);
1661                 Display *disp = GDK_FONT_XDISPLAY(font);
1662                 Atom fontprop = XInternAtom(disp, "FONT", False);
1663                 unsigned long ret;
1664                 gdk_font_ref(font);
1665                 if (XGetFontProperty(xfs, fontprop, &ret)) {
1666                     char *name = XGetAtomName(disp, (Atom)ret);
1667                     if (name)
1668                         gtk_font_selection_dialog_set_font_name
1669                         (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1670                 }
1671                 gdk_font_unref(font);
1672             }
1673         }
1674         gtk_object_set_data
1675             (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1676              "user-data", (gpointer)fontsel);
1677         gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc);
1678         gtk_signal_connect
1679             (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1680              "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1681         gtk_signal_connect_object
1682             (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1683              "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1684              (gpointer)fontsel);
1685         gtk_signal_connect_object
1686             (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1687              "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1688              (gpointer)fontsel);
1689         gtk_widget_show(fontsel);
1690
1691 #else /* !GTK_CHECK_VERSION(2,0,0) */
1692
1693         /*
1694          * Use the unifontsel code provided in gtkfont.c.
1695          */
1696
1697         unifontsel *fontsel = unifontsel_new("Select a font");
1698
1699         gtk_window_set_modal(fontsel->window, TRUE);
1700         unifontsel_set_name(fontsel, fontname);
1701         
1702         gtk_object_set_data(GTK_OBJECT(fontsel->ok_button),
1703                             "user-data", (gpointer)fontsel);
1704         fontsel->user_data = uc;
1705         gtk_signal_connect(GTK_OBJECT(fontsel->ok_button), "clicked",
1706                            GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1707         gtk_signal_connect_object(GTK_OBJECT(fontsel->ok_button), "clicked",
1708                                   GTK_SIGNAL_FUNC(unifontsel_destroy),
1709                                   (gpointer)fontsel);
1710         gtk_signal_connect_object(GTK_OBJECT(fontsel->cancel_button),"clicked",
1711                                   GTK_SIGNAL_FUNC(unifontsel_destroy),
1712                                   (gpointer)fontsel);
1713
1714         gtk_widget_show(GTK_WIDGET(fontsel->window));
1715
1716 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1717
1718     }
1719 }
1720
1721 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1722                             gpointer data)
1723 {
1724     struct dlgparam *dp = (struct dlgparam *)data;
1725     struct uctrl *uc = dlg_find_bywidget(dp, widget);
1726
1727     gtk_widget_set_usize(uc->text, alloc->width, -1);
1728     gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1729     gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig);
1730 }
1731
1732 /* ----------------------------------------------------------------------
1733  * This function does the main layout work: it reads a controlset,
1734  * it creates the relevant GTK controls, and returns a GtkWidget
1735  * containing the result. (This widget might be a title of some
1736  * sort, it might be a Columns containing many controls, or it
1737  * might be a GtkFrame containing a Columns; whatever it is, it's
1738  * definitely a GtkWidget and should probably be added to a
1739  * GtkVbox.)
1740  * 
1741  * `win' is required for setting the default button. If it is
1742  * non-NULL, all buttons created will be default-capable (so they
1743  * have extra space round them for the default highlight).
1744  */
1745 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1746                         struct controlset *s, GtkWindow *win)
1747 {
1748     Columns *cols;
1749     GtkWidget *ret;
1750     int i;
1751
1752     if (!s->boxname && s->boxtitle) {
1753         /* This controlset is a panel title. */
1754         return gtk_label_new(s->boxtitle);
1755     }
1756
1757     /*
1758      * Otherwise, we expect to be laying out actual controls, so
1759      * we'll start by creating a Columns for the purpose.
1760      */
1761     cols = COLUMNS(columns_new(4));
1762     ret = GTK_WIDGET(cols);
1763     gtk_widget_show(ret);
1764
1765     /*
1766      * Create a containing frame if we have a box name.
1767      */
1768     if (*s->boxname) {
1769         ret = gtk_frame_new(s->boxtitle);   /* NULL is valid here */
1770         gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1771         gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1772         gtk_widget_show(ret);
1773     }
1774
1775     /*
1776      * Now iterate through the controls themselves, create them,
1777      * and add them to the Columns.
1778      */
1779     for (i = 0; i < s->ncontrols; i++) {
1780         union control *ctrl = s->ctrls[i];
1781         struct uctrl *uc;
1782         int left = FALSE;
1783         GtkWidget *w = NULL;
1784
1785         switch (ctrl->generic.type) {
1786           case CTRL_COLUMNS:
1787             {
1788                 static const int simplecols[1] = { 100 };
1789                 columns_set_cols(cols, ctrl->columns.ncols,
1790                                  (ctrl->columns.percentages ?
1791                                   ctrl->columns.percentages : simplecols));
1792             }
1793             continue;                  /* no actual control created */
1794           case CTRL_TABDELAY:
1795             {
1796                 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1797                 if (uc)
1798                     columns_taborder_last(cols, uc->toplevel);
1799             }
1800             continue;                  /* no actual control created */
1801         }
1802
1803         uc = snew(struct uctrl);
1804         uc->ctrl = ctrl;
1805         uc->buttons = NULL;
1806         uc->entry = NULL;
1807 #if !GTK_CHECK_VERSION(2,4,0)
1808         uc->list = uc->menu = uc->optmenu = NULL;
1809 #else
1810         uc->combo = NULL;
1811 #endif
1812 #if GTK_CHECK_VERSION(2,0,0)
1813         uc->treeview = NULL;
1814         uc->listmodel = NULL;
1815 #endif
1816         uc->button = uc->text = NULL;
1817         uc->label = NULL;
1818         uc->nclicks = 0;
1819
1820         switch (ctrl->generic.type) {
1821           case CTRL_BUTTON:
1822             w = gtk_button_new_with_label(ctrl->generic.label);
1823             if (win) {
1824                 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
1825                 if (ctrl->button.isdefault)
1826                     gtk_window_set_default(win, w);
1827                 if (ctrl->button.iscancel)
1828                     dp->cancelbutton = w;
1829             }
1830             gtk_signal_connect(GTK_OBJECT(w), "clicked",
1831                                GTK_SIGNAL_FUNC(button_clicked), dp);
1832             gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1833                                GTK_SIGNAL_FUNC(widget_focus), dp);
1834             shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut,
1835                          SHORTCUT_UCTRL, uc);
1836             break;
1837           case CTRL_CHECKBOX:
1838             w = gtk_check_button_new_with_label(ctrl->generic.label);
1839             gtk_signal_connect(GTK_OBJECT(w), "toggled",
1840                                GTK_SIGNAL_FUNC(button_toggled), dp);
1841             gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1842                                GTK_SIGNAL_FUNC(widget_focus), dp);
1843             shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut,
1844                          SHORTCUT_UCTRL, uc);
1845             left = TRUE;
1846             break;
1847           case CTRL_RADIO:
1848             /*
1849              * Radio buttons get to go inside their own Columns, no
1850              * matter what.
1851              */
1852             {
1853                 gint i, *percentages;
1854                 GSList *group;
1855
1856                 w = columns_new(0);
1857                 if (ctrl->generic.label) {
1858                     GtkWidget *label = gtk_label_new(ctrl->generic.label);
1859                     columns_add(COLUMNS(w), label, 0, 1);
1860                     columns_force_left_align(COLUMNS(w), label);
1861                     gtk_widget_show(label);
1862                     shortcut_add(scs, label, ctrl->radio.shortcut,
1863                                  SHORTCUT_UCTRL, uc);
1864                     uc->label = label;
1865                 }
1866                 percentages = g_new(gint, ctrl->radio.ncolumns);
1867                 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1868                     percentages[i] =
1869                         ((100 * (i+1) / ctrl->radio.ncolumns) -
1870                          100 * i / ctrl->radio.ncolumns);
1871                 }
1872                 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1873                                  percentages);
1874                 g_free(percentages);
1875                 group = NULL;
1876
1877                 uc->nbuttons = ctrl->radio.nbuttons;
1878                 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1879
1880                 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1881                     GtkWidget *b;
1882                     gint colstart;
1883
1884                     b = (gtk_radio_button_new_with_label
1885                          (group, ctrl->radio.buttons[i]));
1886                     uc->buttons[i] = b;
1887                     group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
1888                     colstart = i % ctrl->radio.ncolumns;
1889                     columns_add(COLUMNS(w), b, colstart,
1890                                 (i == ctrl->radio.nbuttons-1 ?
1891                                  ctrl->radio.ncolumns - colstart : 1));
1892                     columns_force_left_align(COLUMNS(w), b);
1893                     gtk_widget_show(b);
1894                     gtk_signal_connect(GTK_OBJECT(b), "toggled",
1895                                        GTK_SIGNAL_FUNC(button_toggled), dp);
1896                     gtk_signal_connect(GTK_OBJECT(b), "focus_in_event",
1897                                        GTK_SIGNAL_FUNC(widget_focus), dp);
1898                     if (ctrl->radio.shortcuts) {
1899                         shortcut_add(scs, GTK_BIN(b)->child,
1900                                      ctrl->radio.shortcuts[i],
1901                                      SHORTCUT_UCTRL, uc);
1902                     }
1903                 }
1904             }
1905             break;
1906           case CTRL_EDITBOX:
1907             {
1908                 GtkRequisition req;
1909                 GtkWidget *signalobject;
1910
1911                 if (ctrl->editbox.has_list) {
1912 #if !GTK_CHECK_VERSION(2,4,0)
1913                     /*
1914                      * GTK 1 combo box.
1915                      */
1916                     w = gtk_combo_new();
1917                     gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1918                     uc->entry = GTK_COMBO(w)->entry;
1919                     uc->list = GTK_COMBO(w)->list;
1920                     signalobject = uc->entry;
1921 #else
1922                     /*
1923                      * GTK 2 combo box.
1924                      */
1925                     uc->listmodel = gtk_list_store_new(2, G_TYPE_INT,
1926                                                        G_TYPE_STRING);
1927                     w = gtk_combo_box_entry_new_with_model
1928                         (GTK_TREE_MODEL(uc->listmodel), 1);
1929                     /* We cannot support password combo boxes. */
1930                     assert(!ctrl->editbox.password);
1931                     uc->combo = w;
1932                     signalobject = uc->combo;
1933 #endif
1934                 } else {
1935                     w = gtk_entry_new();
1936                     if (ctrl->editbox.password)
1937                         gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
1938                     uc->entry = w;
1939                     signalobject = w;
1940                 }
1941                 uc->entrysig =
1942                     gtk_signal_connect(GTK_OBJECT(signalobject), "changed",
1943                                        GTK_SIGNAL_FUNC(editbox_changed), dp);
1944                 gtk_signal_connect(GTK_OBJECT(signalobject), "key_press_event",
1945                                    GTK_SIGNAL_FUNC(editbox_key), dp);
1946                 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_in_event",
1947                                    GTK_SIGNAL_FUNC(widget_focus), dp);
1948                 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_out_event",
1949                                    GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1950                 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_out_event",
1951                                    GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1952                 /*
1953                  * Edit boxes, for some strange reason, have a minimum
1954                  * width of 150 in GTK 1.2. We don't want this - we'd
1955                  * rather the edit boxes acquired their natural width
1956                  * from the column layout of the rest of the box.
1957                  *
1958                  * Also, while we're here, we'll squirrel away the
1959                  * edit box height so we can use that to centre its
1960                  * label vertically beside it.
1961                  */
1962                 gtk_widget_size_request(w, &req);
1963                 gtk_widget_set_usize(w, 10, req.height);
1964
1965                 if (ctrl->generic.label) {
1966                     GtkWidget *label, *container;
1967
1968                     label = gtk_label_new(ctrl->generic.label);
1969
1970                     shortcut_add(scs, label, ctrl->editbox.shortcut,
1971                                  SHORTCUT_FOCUS, uc->entry);
1972
1973                     container = columns_new(4);
1974                     if (ctrl->editbox.percentwidth == 100) {
1975                         columns_add(COLUMNS(container), label, 0, 1);
1976                         columns_force_left_align(COLUMNS(container), label);
1977                         columns_add(COLUMNS(container), w, 0, 1);
1978                     } else {
1979                         gint percentages[2];
1980                         percentages[1] = ctrl->editbox.percentwidth;
1981                         percentages[0] = 100 - ctrl->editbox.percentwidth;
1982                         columns_set_cols(COLUMNS(container), 2, percentages);
1983                         columns_add(COLUMNS(container), label, 0, 1);
1984                         columns_force_left_align(COLUMNS(container), label);
1985                         columns_add(COLUMNS(container), w, 1, 1);
1986                         /* Centre the label vertically. */
1987                         gtk_widget_set_usize(label, -1, req.height);
1988                         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1989                     }
1990                     gtk_widget_show(label);
1991                     gtk_widget_show(w);
1992
1993                     w = container;
1994                     uc->label = label;
1995                 }
1996             }
1997             break;
1998           case CTRL_FILESELECT:
1999           case CTRL_FONTSELECT:
2000             {
2001                 GtkWidget *ww;
2002                 GtkRequisition req;
2003                 const char *browsebtn =
2004                     (ctrl->generic.type == CTRL_FILESELECT ?
2005                      "Browse..." : "Change...");
2006
2007                 gint percentages[] = { 75, 25 };
2008                 w = columns_new(4);
2009                 columns_set_cols(COLUMNS(w), 2, percentages);
2010
2011                 if (ctrl->generic.label) {
2012                     ww = gtk_label_new(ctrl->generic.label);
2013                     columns_add(COLUMNS(w), ww, 0, 2);
2014                     columns_force_left_align(COLUMNS(w), ww);
2015                     gtk_widget_show(ww);
2016                     shortcut_add(scs, ww,
2017                                  (ctrl->generic.type == CTRL_FILESELECT ?
2018                                   ctrl->fileselect.shortcut :
2019                                   ctrl->fontselect.shortcut),
2020                                  SHORTCUT_UCTRL, uc);
2021                     uc->label = ww;
2022                 }
2023
2024                 uc->entry = ww = gtk_entry_new();
2025                 gtk_widget_size_request(ww, &req);
2026                 gtk_widget_set_usize(ww, 10, req.height);
2027                 columns_add(COLUMNS(w), ww, 0, 1);
2028                 gtk_widget_show(ww);
2029
2030                 uc->button = ww = gtk_button_new_with_label(browsebtn);
2031                 columns_add(COLUMNS(w), ww, 1, 1);
2032                 gtk_widget_show(ww);
2033
2034                 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
2035                                    GTK_SIGNAL_FUNC(editbox_key), dp);
2036                 uc->entrysig =
2037                     gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
2038                                        GTK_SIGNAL_FUNC(editbox_changed), dp);
2039                 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
2040                                    GTK_SIGNAL_FUNC(widget_focus), dp);
2041                 gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
2042                                    GTK_SIGNAL_FUNC(widget_focus), dp);
2043                 gtk_signal_connect(GTK_OBJECT(ww), "clicked",
2044                                    GTK_SIGNAL_FUNC(filefont_clicked), dp);
2045             }
2046             break;
2047           case CTRL_LISTBOX:
2048
2049 #if GTK_CHECK_VERSION(2,0,0)
2050             /*
2051              * First construct the list data store, with the right
2052              * number of columns.
2053              */
2054 #  if !GTK_CHECK_VERSION(2,4,0)
2055             /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2056              * because combo boxes are still done the old GTK1 way.) */
2057             if (ctrl->listbox.height > 0)
2058 #  endif
2059             {
2060                 GType *types;
2061                 int i;
2062                 int cols;
2063
2064                 cols = ctrl->listbox.ncols;
2065                 cols = cols ? cols : 1;
2066                 types = snewn(1 + cols, GType);
2067
2068                 types[0] = G_TYPE_INT;
2069                 for (i = 0; i < cols; i++)
2070                     types[i+1] = G_TYPE_STRING;
2071
2072                 uc->listmodel = gtk_list_store_newv(1 + cols, types);
2073
2074                 sfree(types);
2075             }
2076 #endif
2077
2078             /*
2079              * See if it's a drop-down list (non-editable combo
2080              * box).
2081              */
2082             if (ctrl->listbox.height == 0) {
2083 #if !GTK_CHECK_VERSION(2,4,0)
2084                 /*
2085                  * GTK1 and early-GTK2 option-menu style of
2086                  * drop-down list.
2087                  */
2088                 uc->optmenu = w = gtk_option_menu_new();
2089                 uc->menu = gtk_menu_new();
2090                 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
2091                 gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data",
2092                                     (gpointer)uc->optmenu);
2093                 gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event",
2094                                    GTK_SIGNAL_FUNC(widget_focus), dp);
2095 #else
2096                 /*
2097                  * Late-GTK2 style using a GtkComboBox.
2098                  */
2099                 GtkCellRenderer *cr;
2100
2101                 /*
2102                  * Create a non-editable GtkComboBox (that is, not
2103                  * its subclass GtkComboBoxEntry).
2104                  */
2105                 w = gtk_combo_box_new_with_model
2106                     (GTK_TREE_MODEL(uc->listmodel));
2107                 uc->combo = w;
2108
2109                 /*
2110                  * Tell it how to render a list item (i.e. which
2111                  * column to look at in the list model).
2112                  */
2113                 cr = gtk_cell_renderer_text_new();
2114                 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), cr, TRUE);
2115                 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), cr,
2116                                                "text", 1, NULL);
2117
2118                 /*
2119                  * And tell it to notify us when the selection
2120                  * changes.
2121                  */
2122                 g_signal_connect(G_OBJECT(w), "changed",
2123                                  G_CALLBACK(droplist_selchange), dp);
2124 #endif
2125             } else {
2126 #if !GTK_CHECK_VERSION(2,0,0)
2127                 /*
2128                  * GTK1-style full list box.
2129                  */
2130                 uc->list = gtk_list_new();
2131                 if (ctrl->listbox.multisel == 2) {
2132                     gtk_list_set_selection_mode(GTK_LIST(uc->list),
2133                                                 GTK_SELECTION_EXTENDED);
2134                 } else if (ctrl->listbox.multisel == 1) {
2135                     gtk_list_set_selection_mode(GTK_LIST(uc->list),
2136                                                 GTK_SELECTION_MULTIPLE);
2137                 } else {
2138                     gtk_list_set_selection_mode(GTK_LIST(uc->list),
2139                                                 GTK_SELECTION_SINGLE);
2140                 }
2141                 w = gtk_scrolled_window_new(NULL, NULL);
2142                 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
2143                                                       uc->list);
2144                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
2145                                                GTK_POLICY_NEVER,
2146                                                GTK_POLICY_AUTOMATIC);
2147                 uc->adj = gtk_scrolled_window_get_vadjustment
2148                     (GTK_SCROLLED_WINDOW(w));
2149
2150                 gtk_widget_show(uc->list);
2151                 gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed",
2152                                    GTK_SIGNAL_FUNC(list_selchange), dp);
2153                 gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event",
2154                                    GTK_SIGNAL_FUNC(widget_focus), dp);
2155
2156                 /*
2157                  * Adjust the height of the scrolled window to the
2158                  * minimum given by the height parameter.
2159                  * 
2160                  * This piece of guesswork is a horrid hack based
2161                  * on looking inside the GTK 1.2 sources
2162                  * (specifically gtkviewport.c, which appears to be
2163                  * the widget which provides the border around the
2164                  * scrolling area). Anyone lets me know how I can
2165                  * do this in a way which isn't at risk from GTK
2166                  * upgrades, I'd be grateful.
2167                  */
2168                 {
2169                     int edge;
2170                     edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
2171                     gtk_widget_set_usize(w, 10,
2172                                          2*edge + (ctrl->listbox.height *
2173                                                    get_listitemheight(w)));
2174                 }
2175
2176                 if (ctrl->listbox.draglist) {
2177                     /*
2178                      * GTK doesn't appear to make it easy to
2179                      * implement a proper draggable list; so
2180                      * instead I'm just going to have to put an Up
2181                      * and a Down button to the right of the actual
2182                      * list box. Ah well.
2183                      */
2184                     GtkWidget *cols, *button;
2185                     static const gint percentages[2] = { 80, 20 };
2186
2187                     cols = columns_new(4);
2188                     columns_set_cols(COLUMNS(cols), 2, percentages);
2189                     columns_add(COLUMNS(cols), w, 0, 1);
2190                     gtk_widget_show(w);
2191                     button = gtk_button_new_with_label("Up");
2192                     columns_add(COLUMNS(cols), button, 1, 1);
2193                     gtk_widget_show(button);
2194                     gtk_signal_connect(GTK_OBJECT(button), "clicked",
2195                                        GTK_SIGNAL_FUNC(draglist_up), dp);
2196                     gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
2197                                        GTK_SIGNAL_FUNC(widget_focus), dp);
2198                     button = gtk_button_new_with_label("Down");
2199                     columns_add(COLUMNS(cols), button, 1, 1);
2200                     gtk_widget_show(button);
2201                     gtk_signal_connect(GTK_OBJECT(button), "clicked",
2202                                        GTK_SIGNAL_FUNC(draglist_down), dp);
2203                     gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
2204                                        GTK_SIGNAL_FUNC(widget_focus), dp);
2205
2206                     w = cols;
2207                 }
2208 #else
2209                 /*
2210                  * GTK2 treeview-based full list box.
2211                  */
2212                 GtkTreeSelection *sel;
2213
2214                 /*
2215                  * Create the list box itself, its columns, and
2216                  * its containing scrolled window.
2217                  */
2218                 w = gtk_tree_view_new_with_model
2219                     (GTK_TREE_MODEL(uc->listmodel));
2220                 g_object_set_data(G_OBJECT(uc->listmodel), "user-data",
2221                                   (gpointer)w);
2222                 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2223                 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
2224                 gtk_tree_selection_set_mode
2225                     (sel, ctrl->listbox.multisel ? GTK_SELECTION_MULTIPLE :
2226                      GTK_SELECTION_SINGLE);
2227                 uc->treeview = w;
2228                 gtk_signal_connect(GTK_OBJECT(w), "row-activated",
2229                                    GTK_SIGNAL_FUNC(listbox_doubleclick), dp);
2230                 g_signal_connect(G_OBJECT(sel), "changed",
2231                                  G_CALLBACK(listbox_selchange), dp);
2232
2233                 if (ctrl->listbox.draglist) {
2234                     gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w), TRUE);
2235                     g_signal_connect(G_OBJECT(uc->listmodel), "row-inserted",
2236                                      G_CALLBACK(listbox_reorder), dp);
2237                 }
2238
2239                 {
2240                     int i;
2241                     int cols;
2242
2243                     cols = ctrl->listbox.ncols;
2244                     cols = cols ? cols : 1;
2245                     for (i = 0; i < cols; i++) {
2246                         GtkTreeViewColumn *column;
2247                         GtkCellRenderer *cellrend;
2248                         /*
2249                          * It appears that GTK 2 doesn't leave us any
2250                          * particularly sensible way to honour the
2251                          * "percentages" specification in the ctrl
2252                          * structure.
2253                          */
2254                         cellrend = gtk_cell_renderer_text_new();
2255                         if (!ctrl->listbox.hscroll) {
2256                             gtk_object_set(GTK_OBJECT(cellrend),
2257                                            "ellipsize", PANGO_ELLIPSIZE_END,
2258                                            "ellipsize-set", TRUE,
2259                                            NULL);
2260                         }
2261                         column = gtk_tree_view_column_new_with_attributes
2262                             ("heading", cellrend, "text", i+1, (char *)NULL);
2263                         gtk_tree_view_column_set_sizing
2264                             (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2265                         gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2266                     }
2267                 }
2268
2269                 {
2270                     GtkWidget *scroll;
2271
2272                     scroll = gtk_scrolled_window_new(NULL, NULL);
2273                     gtk_scrolled_window_set_shadow_type
2274                         (GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
2275                     gtk_widget_show(w);
2276                     gtk_container_add(GTK_CONTAINER(scroll), w);
2277                     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2278                                                    GTK_POLICY_AUTOMATIC,
2279                                                    GTK_POLICY_ALWAYS);
2280                     gtk_widget_set_size_request
2281                         (scroll, -1,
2282                          ctrl->listbox.height * get_listitemheight(w));
2283
2284                     w = scroll;
2285                 }
2286 #endif
2287             }
2288
2289             if (ctrl->generic.label) {
2290                 GtkWidget *label, *container;
2291                 GtkRequisition req;
2292
2293                 label = gtk_label_new(ctrl->generic.label);
2294
2295                 shortcut_add(scs, label, ctrl->listbox.shortcut,
2296                              SHORTCUT_FOCUS, w);
2297
2298                 container = columns_new(4);
2299                 if (ctrl->listbox.percentwidth == 100) {
2300                     columns_add(COLUMNS(container), label, 0, 1);
2301                     columns_force_left_align(COLUMNS(container), label);
2302                     columns_add(COLUMNS(container), w, 0, 1);
2303                 } else {
2304                     gint percentages[2];
2305                     percentages[1] = ctrl->listbox.percentwidth;
2306                     percentages[0] = 100 - ctrl->listbox.percentwidth;
2307                     columns_set_cols(COLUMNS(container), 2, percentages);
2308                     columns_add(COLUMNS(container), label, 0, 1);
2309                     columns_force_left_align(COLUMNS(container), label);
2310                     columns_add(COLUMNS(container), w, 1, 1);
2311                     /* Centre the label vertically. */
2312                     gtk_widget_size_request(w, &req);
2313                     gtk_widget_set_usize(label, -1, req.height);
2314                     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2315                 }
2316                 gtk_widget_show(label);
2317                 gtk_widget_show(w);
2318
2319                 w = container;
2320                 uc->label = label;
2321             }
2322
2323             break;
2324           case CTRL_TEXT:
2325             /*
2326              * Wrapping text widgets don't sit well with the GTK
2327              * layout model, in which widgets state a minimum size
2328              * and the whole window then adjusts to the smallest
2329              * size it can sensibly take given its contents. A
2330              * wrapping text widget _has_ no clear minimum size;
2331              * instead it has a range of possibilities. It can be
2332              * one line deep but 2000 wide, or two lines deep and
2333              * 1000 pixels, or three by 867, or four by 500 and so
2334              * on. It can be as short as you like provided you
2335              * don't mind it being wide, or as narrow as you like
2336              * provided you don't mind it being tall.
2337              * 
2338              * Therefore, it fits very badly into the layout model.
2339              * Hence the only thing to do is pick a width and let
2340              * it choose its own number of lines. To do this I'm
2341              * going to cheat a little. All new wrapping text
2342              * widgets will be created with a minimal text content
2343              * "X"; then, after the rest of the dialog box is set
2344              * up and its size calculated, the text widgets will be
2345              * told their width and given their real text, which
2346              * will cause the size to be recomputed in the y
2347              * direction (because many of them will expand to more
2348              * than one line).
2349              */
2350             uc->text = w = gtk_label_new("X");
2351             gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
2352             gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
2353             uc->textsig =
2354                 gtk_signal_connect(GTK_OBJECT(w), "size-allocate",
2355                                    GTK_SIGNAL_FUNC(label_sizealloc), dp);
2356             break;
2357         }
2358
2359         assert(w != NULL);
2360
2361         columns_add(cols, w,
2362                     COLUMN_START(ctrl->generic.column),
2363                     COLUMN_SPAN(ctrl->generic.column));
2364         if (left)
2365             columns_force_left_align(cols, w);
2366         gtk_widget_show(w);
2367
2368         uc->toplevel = w;
2369         dlg_add_uctrl(dp, uc);
2370     }
2371
2372     return ret;
2373 }
2374
2375 struct selparam {
2376     struct dlgparam *dp;
2377     GtkNotebook *panels;
2378     GtkWidget *panel;
2379 #if !GTK_CHECK_VERSION(2,0,0)
2380     GtkWidget *treeitem;
2381 #else
2382     int depth;
2383     GtkTreePath *treepath;
2384 #endif
2385     struct Shortcuts shortcuts;
2386 };
2387
2388 #if GTK_CHECK_VERSION(2,0,0)
2389 static void treeselection_changed(GtkTreeSelection *treeselection,
2390                                   gpointer data)
2391 {
2392     struct selparam *sps = (struct selparam *)data, *sp;
2393     GtkTreeModel *treemodel;
2394     GtkTreeIter treeiter;
2395     gint spindex;
2396     gint page_num;
2397
2398     if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2399         return;
2400
2401     gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
2402     sp = &sps[spindex];
2403
2404     page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2405     gtk_notebook_set_page(sp->panels, page_num);
2406
2407     dlg_refresh(NULL, sp->dp);
2408
2409     sp->dp->shortcuts = &sp->shortcuts;
2410 }
2411 #else
2412 static void treeitem_sel(GtkItem *item, gpointer data)
2413 {
2414     struct selparam *sp = (struct selparam *)data;
2415     gint page_num;
2416
2417     page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2418     gtk_notebook_set_page(sp->panels, page_num);
2419
2420     dlg_refresh(NULL, sp->dp);
2421
2422     sp->dp->shortcuts = &sp->shortcuts;
2423     sp->dp->currtreeitem = sp->treeitem;
2424 }
2425 #endif
2426
2427 static void window_destroy(GtkWidget *widget, gpointer data)
2428 {
2429     gtk_main_quit();
2430 }
2431
2432 #if !GTK_CHECK_VERSION(2,0,0)
2433 static int tree_grab_focus(struct dlgparam *dp)
2434 {
2435     int i, f;
2436
2437     /*
2438      * See if any of the treeitems has the focus.
2439      */
2440     f = -1;
2441     for (i = 0; i < dp->ntreeitems; i++)
2442         if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
2443             f = i;
2444             break;
2445         }
2446
2447     if (f >= 0)
2448         return FALSE;
2449     else {
2450         gtk_widget_grab_focus(dp->currtreeitem);
2451         return TRUE;
2452     }
2453 }
2454
2455 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
2456                 gpointer data)
2457 {
2458     struct dlgparam *dp = (struct dlgparam *)data;
2459
2460     gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus");
2461     /*
2462      * If there's a focused treeitem, we return FALSE to cause the
2463      * focus to move on to some totally other control. If not, we
2464      * focus the selected one.
2465      */
2466     return tree_grab_focus(dp);
2467 }
2468 #endif
2469
2470 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2471 {
2472     struct dlgparam *dp = (struct dlgparam *)data;
2473
2474     if (event->keyval == GDK_Escape && dp->cancelbutton) {
2475         gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked");
2476         return TRUE;
2477     }
2478
2479     if ((event->state & GDK_MOD1_MASK) &&
2480         (unsigned char)event->string[0] > 0 &&
2481         (unsigned char)event->string[0] <= 127) {
2482         int schr = (unsigned char)event->string[0];
2483         struct Shortcut *sc = &dp->shortcuts->sc[schr];
2484
2485         switch (sc->action) {
2486           case SHORTCUT_TREE:
2487 #if GTK_CHECK_VERSION(2,0,0)
2488             gtk_widget_grab_focus(sc->widget);
2489 #else
2490             tree_grab_focus(dp);
2491 #endif
2492             break;
2493           case SHORTCUT_FOCUS:
2494             gtk_widget_grab_focus(sc->widget);
2495             break;
2496           case SHORTCUT_UCTRL:
2497             /*
2498              * We must do something sensible with a uctrl.
2499              * Precisely what this is depends on the type of
2500              * control.
2501              */
2502             switch (sc->uc->ctrl->generic.type) {
2503               case CTRL_CHECKBOX:
2504               case CTRL_BUTTON:
2505                 /* Check boxes and buttons get the focus _and_ get toggled. */
2506                 gtk_widget_grab_focus(sc->uc->toplevel);
2507                 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel),
2508                                         "clicked");
2509                 break;
2510               case CTRL_FILESELECT:
2511               case CTRL_FONTSELECT:
2512                 /* File/font selectors have their buttons pressed (ooer),
2513                  * and focus transferred to the edit box. */
2514                 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button),
2515                                         "clicked");
2516                 gtk_widget_grab_focus(sc->uc->entry);
2517                 break;
2518               case CTRL_RADIO:
2519                 /*
2520                  * Radio buttons are fun, because they have
2521                  * multiple shortcuts. We must find whether the
2522                  * activated shortcut is the shortcut for the whole
2523                  * group, or for a particular button. In the former
2524                  * case, we find the currently selected button and
2525                  * focus it; in the latter, we focus-and-click the
2526                  * button whose shortcut was pressed.
2527                  */
2528                 if (schr == sc->uc->ctrl->radio.shortcut) {
2529                     int i;
2530                     for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2531                         if (gtk_toggle_button_get_active
2532                             (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
2533                             gtk_widget_grab_focus(sc->uc->buttons[i]);
2534                         }
2535                 } else if (sc->uc->ctrl->radio.shortcuts) {
2536                     int i;
2537                     for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2538                         if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
2539                             gtk_widget_grab_focus(sc->uc->buttons[i]);
2540                             gtk_signal_emit_by_name
2541                                 (GTK_OBJECT(sc->uc->buttons[i]), "clicked");
2542                         }
2543                 }
2544                 break;
2545               case CTRL_LISTBOX:
2546
2547 #if !GTK_CHECK_VERSION(2,4,0)
2548                 if (sc->uc->optmenu) {
2549                     GdkEventButton bev;
2550                     gint returnval;
2551
2552                     gtk_widget_grab_focus(sc->uc->optmenu);
2553                     /* Option menus don't work using the "clicked" signal.
2554                      * We need to manufacture a button press event :-/ */
2555                     bev.type = GDK_BUTTON_PRESS;
2556                     bev.button = 1;
2557                     gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu),
2558                                             "button_press_event",
2559                                             &bev, &returnval);
2560                     break;
2561                 }
2562 #else
2563                 if (sc->uc->combo) {
2564                     gtk_widget_grab_focus(sc->uc->combo);
2565                     gtk_combo_box_popup(GTK_COMBO_BOX(sc->uc->combo));
2566                     break;
2567                 }
2568 #endif
2569 #if !GTK_CHECK_VERSION(2,0,0)
2570                 if (sc->uc->list) {
2571                     /*
2572                      * For GTK-1 style list boxes, we tell it to
2573                      * focus one of its children, which appears to
2574                      * do the Right Thing.
2575                      */
2576                     gtk_container_focus(GTK_CONTAINER(sc->uc->list),
2577                                         GTK_DIR_TAB_FORWARD);
2578                     break;
2579                 }
2580 #else
2581                 if (sc->uc->treeview) {
2582                     gtk_widget_grab_focus(sc->uc->treeview);
2583                     break;
2584                 }
2585 #endif
2586                 assert(!"We shouldn't get here");
2587                 break;
2588             }
2589             break;
2590         }
2591     }
2592
2593     return FALSE;
2594 }
2595
2596 #if !GTK_CHECK_VERSION(2,0,0)
2597 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2598 {
2599     struct dlgparam *dp = (struct dlgparam *)data;
2600
2601     if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2602         event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2603         int dir, i, j = -1;
2604         for (i = 0; i < dp->ntreeitems; i++)
2605             if (widget == dp->treeitems[i])
2606                 break;
2607         if (i < dp->ntreeitems) {
2608             if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2609                 dir = -1;
2610             else
2611                 dir = +1;
2612
2613             while (1) {
2614                 i += dir;
2615                 if (i < 0 || i >= dp->ntreeitems)
2616                     break;             /* nothing in that dir to select */
2617                 /*
2618                  * Determine if this tree item is visible.
2619                  */
2620                 {
2621                     GtkWidget *w = dp->treeitems[i];
2622                     int vis = TRUE;
2623                     while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2624                         if (!GTK_WIDGET_VISIBLE(w)) {
2625                             vis = FALSE;
2626                             break;
2627                         }
2628                         w = w->parent;
2629                     }
2630                     if (vis) {
2631                         j = i;         /* got one */
2632                         break;
2633                     }
2634                 }
2635             }
2636         }
2637         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2638                                      "key_press_event");
2639         if (j >= 0) {
2640             gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle");
2641             gtk_widget_grab_focus(dp->treeitems[j]);
2642         }
2643         return TRUE;
2644     }
2645
2646     /*
2647      * It's nice for Left and Right to expand and collapse tree
2648      * branches.
2649      */
2650     if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2651         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2652                                      "key_press_event");
2653         gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2654         return TRUE;
2655     }
2656     if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2657         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2658                                      "key_press_event");
2659         gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2660         return TRUE;
2661     }
2662
2663     return FALSE;
2664 }
2665 #endif
2666
2667 static void shortcut_highlight(GtkWidget *labelw, int chr)
2668 {
2669     GtkLabel *label = GTK_LABEL(labelw);
2670     gchar *currstr, *pattern;
2671     int i;
2672
2673     gtk_label_get(label, &currstr);
2674     for (i = 0; currstr[i]; i++)
2675         if (tolower((unsigned char)currstr[i]) == chr) {
2676             GtkRequisition req;
2677
2678             pattern = dupprintf("%*s_", i, "");
2679
2680             gtk_widget_size_request(GTK_WIDGET(label), &req);
2681             gtk_label_set_pattern(label, pattern);
2682             gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
2683
2684             sfree(pattern);
2685             break;
2686         }
2687 }
2688
2689 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2690                   int chr, int action, void *ptr)
2691 {
2692     if (chr == NO_SHORTCUT)
2693         return;
2694
2695     chr = tolower((unsigned char)chr);
2696
2697     assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2698
2699     scs->sc[chr].action = action;
2700
2701     if (action == SHORTCUT_FOCUS) {
2702         scs->sc[chr].uc = NULL;
2703         scs->sc[chr].widget = (GtkWidget *)ptr;
2704     } else {
2705         scs->sc[chr].widget = NULL;
2706         scs->sc[chr].uc = (struct uctrl *)ptr;
2707     }
2708
2709     shortcut_highlight(labelw, chr);
2710 }
2711
2712 int get_listitemheight(GtkWidget *w)
2713 {
2714 #if !GTK_CHECK_VERSION(2,0,0)
2715     GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2716     GtkRequisition req;
2717     gtk_widget_size_request(listitem, &req);
2718     gtk_object_sink(GTK_OBJECT(listitem));
2719     return req.height;
2720 #else
2721     int height;
2722     GtkCellRenderer *cr = gtk_cell_renderer_text_new();
2723     gtk_cell_renderer_get_size(cr, w, NULL, NULL, NULL, NULL, &height);
2724     g_object_ref(G_OBJECT(cr));
2725     gtk_object_sink(GTK_OBJECT(cr));
2726     g_object_unref(G_OBJECT(cr));
2727     return height;
2728 #endif
2729 }
2730
2731 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2732 {
2733 #if !GTK_CHECK_VERSION(2,0,0)
2734
2735     /*
2736      * In GTK 1, laying out the buttons at the bottom of the
2737      * configuration box is nice and easy, because a GtkDialog's
2738      * action_area is a GtkHBox which stretches to cover the full
2739      * width of the dialog. So we just put our Columns widget
2740      * straight into that hbox, and it ends up just where we want
2741      * it.
2742      */
2743     gtk_box_pack_start(GTK_BOX(dlg->action_area), w, TRUE, TRUE, 0);
2744
2745 #else
2746     /*
2747      * In GTK 2, the action area is now a GtkHButtonBox and its
2748      * layout behaviour seems to be different: it doesn't stretch
2749      * to cover the full width of the window, but instead finds its
2750      * own preferred width and right-aligns that within the window.
2751      * This isn't what we want, because we have both left-aligned
2752      * and right-aligned buttons coming out of the above call to
2753      * layout_ctrls(), and right-aligning the whole thing will
2754      * result in the former being centred and looking weird.
2755      *
2756      * So instead we abandon the dialog's action area completely:
2757      * we gtk_widget_hide() it in the below code, and we also call
2758      * gtk_dialog_set_has_separator() to remove the separator above
2759      * it. We then insert our own action area into the end of the
2760      * dialog's main vbox, and add our own separator above that.
2761      *
2762      * (Ideally, if we were a native GTK app, we would use the
2763      * GtkHButtonBox's _own_ innate ability to support one set of
2764      * buttons being right-aligned and one left-aligned. But to do
2765      * that here, we would have to either (a) pick apart our cross-
2766      * platform layout structures and treat them specially for this
2767      * particular set of controls, which would be painful, or else
2768      * (b) develop a special and simpler cross-platform
2769      * representation for these particular controls, and introduce
2770      * special-case code into all the _other_ platforms to handle
2771      * it. Neither appeals. Therefore, I regretfully discard the
2772      * GTKHButtonBox and go it alone.)
2773      */
2774
2775     GtkWidget *align;
2776     align = gtk_alignment_new(0, 0, 1, 1);
2777     gtk_container_add(GTK_CONTAINER(align), w);
2778     /*
2779      * The purpose of this GtkAlignment is to provide padding
2780      * around the buttons. The padding we use is twice the padding
2781      * used in our GtkColumns, because we nest two GtkColumns most
2782      * of the time (one separating the tree view from the main
2783      * controls, and another for the main controls themselves).
2784      */
2785 #if GTK_CHECK_VERSION(2,4,0)
2786     gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2787 #endif
2788     gtk_widget_show(align);
2789     gtk_box_pack_end(GTK_BOX(dlg->vbox), align, FALSE, TRUE, 0);
2790     w = gtk_hseparator_new();
2791     gtk_box_pack_end(GTK_BOX(dlg->vbox), w, FALSE, TRUE, 0);
2792     gtk_widget_show(w);
2793     gtk_widget_hide(dlg->action_area);
2794     gtk_dialog_set_has_separator(dlg, FALSE);
2795 #endif
2796 }
2797
2798 int do_config_box(const char *title, Conf *conf, int midsession,
2799                   int protcfginfo)
2800 {
2801     GtkWidget *window, *hbox, *vbox, *cols, *label,
2802         *tree, *treescroll, *panels, *panelvbox;
2803     int index, level, protocol;
2804     struct controlbox *ctrlbox;
2805     char *path;
2806 #if GTK_CHECK_VERSION(2,0,0)
2807     GtkTreeStore *treestore;
2808     GtkCellRenderer *treerenderer;
2809     GtkTreeViewColumn *treecolumn;
2810     GtkTreeSelection *treeselection;
2811     GtkTreeIter treeiterlevels[8];
2812 #else
2813     GtkTreeItem *treeitemlevels[8];
2814     GtkTree *treelevels[8];
2815 #endif
2816     struct dlgparam dp;
2817     struct Shortcuts scs;
2818
2819     struct selparam *selparams = NULL;
2820     int nselparams = 0, selparamsize = 0;
2821
2822     dlg_init(&dp);
2823
2824     for (index = 0; index < lenof(scs.sc); index++) {
2825         scs.sc[index].action = SHORTCUT_EMPTY;
2826     }
2827
2828     window = gtk_dialog_new();
2829
2830     ctrlbox = ctrl_new_box();
2831     protocol = conf_get_int(conf, CONF_protocol);
2832     setup_config_box(ctrlbox, midsession, protocol, protcfginfo);
2833     unix_setup_config_box(ctrlbox, midsession, protocol);
2834     gtk_setup_config_box(ctrlbox, midsession, window);
2835
2836     gtk_window_set_title(GTK_WINDOW(window), title);
2837     hbox = gtk_hbox_new(FALSE, 4);
2838     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0);
2839     gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2840     gtk_widget_show(hbox);
2841     vbox = gtk_vbox_new(FALSE, 4);
2842     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2843     gtk_widget_show(vbox);
2844     cols = columns_new(4);
2845     gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2846     gtk_widget_show(cols);
2847     label = gtk_label_new("Category:");
2848     columns_add(COLUMNS(cols), label, 0, 1);
2849     columns_force_left_align(COLUMNS(cols), label);
2850     gtk_widget_show(label);
2851     treescroll = gtk_scrolled_window_new(NULL, NULL);
2852 #if GTK_CHECK_VERSION(2,0,0)
2853     treestore = gtk_tree_store_new
2854         (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2855     tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2856     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2857     treerenderer = gtk_cell_renderer_text_new();
2858     treecolumn = gtk_tree_view_column_new_with_attributes
2859         ("Label", treerenderer, "text", 0, NULL);
2860     gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2861     treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2862     gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2863     gtk_container_add(GTK_CONTAINER(treescroll), tree);
2864 #else
2865     tree = gtk_tree_new();
2866     gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2867     gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2868     gtk_signal_connect(GTK_OBJECT(tree), "focus",
2869                        GTK_SIGNAL_FUNC(tree_focus), &dp);
2870 #endif
2871     gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event",
2872                        GTK_SIGNAL_FUNC(widget_focus), &dp);
2873     shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2874     gtk_widget_show(treescroll);
2875     gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2876     panels = gtk_notebook_new();
2877     gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2878     gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2879     gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2880     gtk_widget_show(panels);
2881
2882     panelvbox = NULL;
2883     path = NULL;
2884     level = 0;
2885     for (index = 0; index < ctrlbox->nctrlsets; index++) {
2886         struct controlset *s = ctrlbox->ctrlsets[index];
2887         GtkWidget *w;
2888
2889         if (!*s->pathname) {
2890             w = layout_ctrls(&dp, &scs, s, GTK_WINDOW(window));
2891
2892             set_dialog_action_area(GTK_DIALOG(window), w);
2893         } else {
2894             int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2895             if (j != INT_MAX) {        /* add to treeview, start new panel */
2896                 char *c;
2897 #if GTK_CHECK_VERSION(2,0,0)
2898                 GtkTreeIter treeiter;
2899 #else
2900                 GtkWidget *treeitem;
2901 #endif
2902                 int first;
2903
2904                 /*
2905                  * We expect never to find an implicit path
2906                  * component. For example, we expect never to see
2907                  * A/B/C followed by A/D/E, because that would
2908                  * _implicitly_ create A/D. All our path prefixes
2909                  * are expected to contain actual controls and be
2910                  * selectable in the treeview; so we would expect
2911                  * to see A/D _explicitly_ before encountering
2912                  * A/D/E.
2913                  */
2914                 assert(j == ctrl_path_elements(s->pathname) - 1);
2915
2916                 c = strrchr(s->pathname, '/');
2917                 if (!c)
2918                     c = s->pathname;
2919                 else
2920                     c++;
2921
2922                 path = s->pathname;
2923
2924                 first = (panelvbox == NULL);
2925
2926                 panelvbox = gtk_vbox_new(FALSE, 4);
2927                 gtk_widget_show(panelvbox);
2928                 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
2929                                          NULL);
2930                 if (first) {
2931                     gint page_num;
2932
2933                     page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
2934                                                      panelvbox);
2935                     gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num);
2936                 }
2937
2938                 if (nselparams >= selparamsize) {
2939                     selparamsize += 16;
2940                     selparams = sresize(selparams, selparamsize,
2941                                         struct selparam);
2942                 }
2943                 selparams[nselparams].dp = &dp;
2944                 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
2945                 selparams[nselparams].panel = panelvbox;
2946                 selparams[nselparams].shortcuts = scs;   /* structure copy */
2947
2948                 assert(j-1 < level);
2949
2950 #if GTK_CHECK_VERSION(2,0,0)
2951                 if (j > 0)
2952                     /* treeiterlevels[j-1] will always be valid because we
2953                      * don't allow implicit path components; see above.
2954                      */
2955                     gtk_tree_store_append(treestore, &treeiter,
2956                                           &treeiterlevels[j-1]);
2957                 else
2958                     gtk_tree_store_append(treestore, &treeiter, NULL);
2959                 gtk_tree_store_set(treestore, &treeiter,
2960                                    TREESTORE_PATH, c,
2961                                    TREESTORE_PARAMS, nselparams,
2962                                    -1);
2963                 treeiterlevels[j] = treeiter;
2964
2965                 selparams[nselparams].depth = j;
2966                 if (j > 0) {
2967                     selparams[nselparams].treepath =
2968                         gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
2969                                                 &treeiterlevels[j-1]);
2970                     /*
2971                      * We are going to collapse all tree branches
2972                      * at depth greater than 2, but not _yet_; see
2973                      * the comment at the call to
2974                      * gtk_tree_view_collapse_row below.
2975                      */
2976                     gtk_tree_view_expand_row(GTK_TREE_VIEW(tree),
2977                                              selparams[nselparams].treepath,
2978                                              FALSE);
2979                 } else {
2980                     selparams[nselparams].treepath = NULL;
2981                 }
2982 #else
2983                 treeitem = gtk_tree_item_new_with_label(c);
2984                 if (j > 0) {
2985                     if (!treelevels[j-1]) {
2986                         treelevels[j-1] = GTK_TREE(gtk_tree_new());
2987                         gtk_tree_item_set_subtree
2988                             (treeitemlevels[j-1],
2989                              GTK_WIDGET(treelevels[j-1]));
2990                         if (j < 2)
2991                             gtk_tree_item_expand(treeitemlevels[j-1]);
2992                         else
2993                             gtk_tree_item_collapse(treeitemlevels[j-1]);
2994                     }
2995                     gtk_tree_append(treelevels[j-1], treeitem);
2996                 } else {
2997                     gtk_tree_append(GTK_TREE(tree), treeitem);
2998                 }
2999                 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
3000                 treelevels[j] = NULL;
3001
3002                 gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event",
3003                                    GTK_SIGNAL_FUNC(tree_key_press), &dp);
3004                 gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event",
3005                                    GTK_SIGNAL_FUNC(widget_focus), &dp);
3006
3007                 gtk_widget_show(treeitem);
3008
3009                 if (first)
3010                     gtk_tree_select_child(GTK_TREE(tree), treeitem);
3011                 selparams[nselparams].treeitem = treeitem;
3012 #endif
3013
3014                 level = j+1;
3015                 nselparams++;
3016             }
3017
3018             w = layout_ctrls(&dp, &selparams[nselparams-1].shortcuts, s, NULL);
3019             gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
3020             gtk_widget_show(w);
3021         }
3022     }
3023
3024 #if GTK_CHECK_VERSION(2,0,0)
3025     {
3026         GtkRequisition req;
3027         int i;
3028
3029         /*
3030          * We want our tree view to come up with all branches at
3031          * depth 2 or more collapsed. However, if we start off
3032          * with those branches collapsed, then the tree view's
3033          * size request will be calculated based on the width of
3034          * the collapsed tree. So instead we start with them all
3035          * expanded; then we ask for the current size request,
3036          * collapse the relevant rows, and force the width to the
3037          * value we just computed. This arranges that the tree
3038          * view is wide enough to have all branches expanded
3039          * safely.
3040          */
3041
3042         gtk_widget_size_request(tree, &req);
3043
3044         for (i = 0; i < nselparams; i++)
3045             if (selparams[i].depth >= 2)
3046                 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree),
3047                                            selparams[i].treepath);
3048
3049         gtk_widget_set_size_request(tree, req.width, -1);
3050     }
3051 #endif
3052
3053 #if GTK_CHECK_VERSION(2,0,0)
3054     g_signal_connect(G_OBJECT(treeselection), "changed",
3055                      G_CALLBACK(treeselection_changed), selparams);
3056 #else
3057     dp.ntreeitems = nselparams;
3058     dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
3059
3060     for (index = 0; index < nselparams; index++) {
3061         gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
3062                            GTK_SIGNAL_FUNC(treeitem_sel),
3063                            &selparams[index]);
3064         dp.treeitems[index] = selparams[index].treeitem;
3065     }
3066 #endif
3067
3068     dp.data = conf;
3069     dlg_refresh(NULL, &dp);
3070
3071     dp.shortcuts = &selparams[0].shortcuts;
3072 #if !GTK_CHECK_VERSION(2,0,0)
3073     dp.currtreeitem = dp.treeitems[0];
3074 #endif
3075     dp.lastfocus = NULL;
3076     dp.retval = 0;
3077     dp.window = window;
3078
3079     {
3080         /* in gtkwin.c */
3081         extern void set_window_icon(GtkWidget *window,
3082                                     const char *const *const *icon,
3083                                     int n_icon);
3084         extern const char *const *const cfg_icon[];
3085         extern const int n_cfg_icon;
3086         set_window_icon(window, cfg_icon, n_cfg_icon);
3087     }
3088
3089 #if !GTK_CHECK_VERSION(2,0,0)
3090     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
3091                                           tree);
3092 #endif
3093     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
3094                                    GTK_POLICY_NEVER,
3095                                    GTK_POLICY_AUTOMATIC);
3096     gtk_widget_show(tree);
3097
3098     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3099     gtk_widget_show(window);
3100
3101     /*
3102      * Set focus into the first available control.
3103      */
3104     for (index = 0; index < ctrlbox->nctrlsets; index++) {
3105         struct controlset *s = ctrlbox->ctrlsets[index];
3106         int done = 0;
3107         int j;
3108
3109         if (*s->pathname) {
3110             for (j = 0; j < s->ncontrols; j++)
3111                 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
3112                     s->ctrls[j]->generic.type != CTRL_COLUMNS &&
3113                     s->ctrls[j]->generic.type != CTRL_TEXT) {
3114                     dlg_set_focus(s->ctrls[j], &dp);
3115                     dp.lastfocus = s->ctrls[j];
3116                     done = 1;
3117                     break;
3118                 }
3119         }
3120         if (done)
3121             break;
3122     }
3123
3124     gtk_signal_connect(GTK_OBJECT(window), "destroy",
3125                        GTK_SIGNAL_FUNC(window_destroy), NULL);
3126     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3127                        GTK_SIGNAL_FUNC(win_key_press), &dp);
3128
3129     gtk_main();
3130
3131     dlg_cleanup(&dp);
3132     sfree(selparams);
3133
3134     return dp.retval;
3135 }
3136
3137 static void messagebox_handler(union control *ctrl, void *dlg,
3138                                void *data, int event)
3139 {
3140     if (event == EVENT_ACTION)
3141         dlg_end(dlg, ctrl->generic.context.i);
3142 }
3143 int messagebox(GtkWidget *parentwin, const char *title, const char *msg,
3144                int minwid, ...)
3145 {
3146     GtkWidget *window, *w0, *w1;
3147     struct controlbox *ctrlbox;
3148     struct controlset *s0, *s1;
3149     union control *c;
3150     struct dlgparam dp;
3151     struct Shortcuts scs;
3152     int index, ncols;
3153     va_list ap;
3154
3155     dlg_init(&dp);
3156
3157     for (index = 0; index < lenof(scs.sc); index++) {
3158         scs.sc[index].action = SHORTCUT_EMPTY;
3159     }
3160
3161     ctrlbox = ctrl_new_box();
3162
3163     ncols = 0;
3164     va_start(ap, minwid);
3165     while (va_arg(ap, char *) != NULL) {
3166         ncols++;
3167         (void) va_arg(ap, int);        /* shortcut */
3168         (void) va_arg(ap, int);        /* normal/default/cancel */
3169         (void) va_arg(ap, int);        /* end value */
3170     }
3171     va_end(ap);
3172
3173     s0 = ctrl_getset(ctrlbox, "", "", "");
3174     c = ctrl_columns(s0, 2, 50, 50);
3175     c->columns.ncols = s0->ncolumns = ncols;
3176     c->columns.percentages = sresize(c->columns.percentages, ncols, int);
3177     for (index = 0; index < ncols; index++)
3178         c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
3179     va_start(ap, minwid);
3180     index = 0;
3181     while (1) {
3182         char *title = va_arg(ap, char *);
3183         int shortcut, type, value;
3184         if (title == NULL)
3185             break;
3186         shortcut = va_arg(ap, int);
3187         type = va_arg(ap, int);
3188         value = va_arg(ap, int);
3189         c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
3190                             messagebox_handler, I(value));
3191         c->generic.column = index++;
3192         if (type > 0)
3193             c->button.isdefault = TRUE;
3194         else if (type < 0)
3195             c->button.iscancel = TRUE;
3196     }
3197     va_end(ap);
3198
3199     s1 = ctrl_getset(ctrlbox, "x", "", "");
3200     ctrl_text(s1, msg, HELPCTX(no_help));
3201
3202     window = gtk_dialog_new();
3203     gtk_window_set_title(GTK_WINDOW(window), title);
3204     w0 = layout_ctrls(&dp, &scs, s0, GTK_WINDOW(window));
3205     set_dialog_action_area(GTK_DIALOG(window), w0);
3206     gtk_widget_show(w0);
3207     w1 = layout_ctrls(&dp, &scs, s1, GTK_WINDOW(window));
3208     gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3209     gtk_widget_set_usize(w1, minwid+20, -1);
3210     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
3211                        w1, TRUE, TRUE, 0);
3212     gtk_widget_show(w1);
3213
3214     dp.shortcuts = &scs;
3215     dp.lastfocus = NULL;
3216     dp.retval = 0;
3217     dp.window = window;
3218
3219     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3220     if (parentwin) {
3221         set_transient_window_pos(parentwin, window);
3222         gtk_window_set_transient_for(GTK_WINDOW(window),
3223                                      GTK_WINDOW(parentwin));
3224     } else
3225         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3226     gtk_widget_show(window);
3227
3228     gtk_signal_connect(GTK_OBJECT(window), "destroy",
3229                        GTK_SIGNAL_FUNC(window_destroy), NULL);
3230     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3231                        GTK_SIGNAL_FUNC(win_key_press), &dp);
3232
3233     gtk_main();
3234
3235     dlg_cleanup(&dp);
3236     ctrl_free_box(ctrlbox);
3237
3238     return dp.retval;
3239 }
3240
3241 int string_width(const char *text)
3242 {
3243     GtkWidget *label = gtk_label_new(text);
3244     GtkRequisition req;
3245     gtk_widget_size_request(label, &req);
3246     gtk_object_sink(GTK_OBJECT(label));
3247     return req.width;
3248 }
3249
3250 int reallyclose(void *frontend)
3251 {
3252     char *title = dupcat(appname, " Exit Confirmation", NULL);
3253     int ret = messagebox(GTK_WIDGET(get_window(frontend)),
3254                          title, "Are you sure you want to close this session?",
3255                          string_width("Most of the width of the above text"),
3256                          "Yes", 'y', +1, 1,
3257                          "No", 'n', -1, 0,
3258                          NULL);
3259     sfree(title);
3260     return ret;
3261 }
3262
3263 int verify_ssh_host_key(void *frontend, char *host, int port,
3264                         const char *keytype, char *keystr, char *fingerprint,
3265                         void (*callback)(void *ctx, int result), void *ctx)
3266 {
3267     static const char absenttxt[] =
3268         "The server's host key is not cached. You have no guarantee "
3269         "that the server is the computer you think it is.\n"
3270         "The server's %s key fingerprint is:\n"
3271         "%s\n"
3272         "If you trust this host, press \"Accept\" to add the key to "
3273         "PuTTY's cache and carry on connecting.\n"
3274         "If you want to carry on connecting just once, without "
3275         "adding the key to the cache, press \"Connect Once\".\n"
3276         "If you do not trust this host, press \"Cancel\" to abandon the "
3277         "connection.";
3278     static const char wrongtxt[] =
3279         "WARNING - POTENTIAL SECURITY BREACH!\n"
3280         "The server's host key does not match the one PuTTY has "
3281         "cached. This means that either the server administrator "
3282         "has changed the host key, or you have actually connected "
3283         "to another computer pretending to be the server.\n"
3284         "The new %s key fingerprint is:\n"
3285         "%s\n"
3286         "If you were expecting this change and trust the new key, "
3287         "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3288         "If you want to carry on connecting but without updating "
3289         "the cache, press \"Connect Once\".\n"
3290         "If you want to abandon the connection completely, press "
3291         "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3292         "safe choice.";
3293     char *text;
3294     int ret;
3295
3296     /*
3297      * Verify the key.
3298      */
3299     ret = verify_host_key(host, port, keytype, keystr);
3300
3301     if (ret == 0)                      /* success - key matched OK */
3302         return 1;
3303
3304     text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
3305
3306     ret = messagebox(GTK_WIDGET(get_window(frontend)),
3307                      "PuTTY Security Alert", text,
3308                      string_width(fingerprint),
3309                      "Accept", 'a', 0, 2,
3310                      "Connect Once", 'o', 0, 1,
3311                      "Cancel", 'c', -1, 0,
3312                      NULL);
3313
3314     sfree(text);
3315
3316     if (ret == 2) {
3317         store_host_key(host, port, keytype, keystr);
3318         return 1;                      /* continue with connection */
3319     } else if (ret == 1)
3320         return 1;                      /* continue with connection */
3321     return 0;                          /* do not continue with connection */
3322 }
3323
3324 /*
3325  * Ask whether the selected algorithm is acceptable (since it was
3326  * below the configured 'warn' threshold).
3327  */
3328 int askalg(void *frontend, const char *algtype, const char *algname,
3329            void (*callback)(void *ctx, int result), void *ctx)
3330 {
3331     static const char msg[] =
3332         "The first %s supported by the server is "
3333         "%s, which is below the configured warning threshold.\n"
3334         "Continue with connection?";
3335     char *text;
3336     int ret;
3337
3338     text = dupprintf(msg, algtype, algname);
3339     ret = messagebox(GTK_WIDGET(get_window(frontend)),
3340                      "PuTTY Security Alert", text,
3341                      string_width("Continue with connection?"),
3342                      "Yes", 'y', 0, 1,
3343                      "No", 'n', 0, 0,
3344                      NULL);
3345     sfree(text);
3346
3347     if (ret) {
3348         return 1;
3349     } else {
3350         return 0;
3351     }
3352 }
3353
3354 void old_keyfile_warning(void)
3355 {
3356     /*
3357      * This should never happen on Unix. We hope.
3358      */
3359 }
3360
3361 void fatal_message_box(void *window, const char *msg)
3362 {
3363     messagebox(window, "PuTTY Fatal Error", msg,
3364                string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3365                "OK", 'o', 1, 1, NULL);
3366 }
3367
3368 void nonfatal_message_box(void *window, const char *msg)
3369 {
3370     messagebox(window, "PuTTY Error", msg,
3371                string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3372                "OK", 'o', 1, 1, NULL);
3373 }
3374
3375 void fatalbox(const char *p, ...)
3376 {
3377     va_list ap;
3378     char *msg;
3379     va_start(ap, p);
3380     msg = dupvprintf(p, ap);
3381     va_end(ap);
3382     fatal_message_box(NULL, msg);
3383     sfree(msg);
3384     cleanup_exit(1);
3385 }
3386
3387 void nonfatal(const char *p, ...)
3388 {
3389     va_list ap;
3390     char *msg;
3391     va_start(ap, p);
3392     msg = dupvprintf(p, ap);
3393     va_end(ap);
3394     nonfatal_message_box(NULL, msg);
3395     sfree(msg);
3396 }
3397
3398 static GtkWidget *aboutbox = NULL;
3399
3400 static void about_close_clicked(GtkButton *button, gpointer data)
3401 {
3402     gtk_widget_destroy(aboutbox);
3403     aboutbox = NULL;
3404 }
3405
3406 static void licence_clicked(GtkButton *button, gpointer data)
3407 {
3408     char *title;
3409
3410     const char *licence =
3411         "Copyright 1997-2015 Simon Tatham.\n\n"
3412
3413         "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3414         "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3415         "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3416         "Markus Kuhn, Colin Watson, Christopher Staite, and CORE SDI S.A.\n\n"
3417
3418         "Permission is hereby granted, free of charge, to any person "
3419         "obtaining a copy of this software and associated documentation "
3420         "files (the ""Software""), to deal in the Software without restriction, "
3421         "including without limitation the rights to use, copy, modify, merge, "
3422         "publish, distribute, sublicense, and/or sell copies of the Software, "
3423         "and to permit persons to whom the Software is furnished to do so, "
3424         "subject to the following conditions:\n\n"
3425
3426         "The above copyright notice and this permission notice shall be "
3427         "included in all copies or substantial portions of the Software.\n\n"
3428
3429         "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3430         "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3431         "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3432         "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3433         "PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE "
3434         "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3435         "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3436         "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3437         "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3438         "OTHER DEALINGS IN THE SOFTWARE.";
3439
3440     title = dupcat(appname, " Licence", NULL);
3441     assert(aboutbox != NULL);
3442     messagebox(aboutbox, title, licence,
3443                string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3444                             " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3445                "OK", 'o', 1, 1, NULL);
3446     sfree(title);
3447 }
3448
3449 void about_box(void *window)
3450 {
3451     GtkWidget *w;
3452     char *title;
3453
3454     if (aboutbox) {
3455         gtk_widget_grab_focus(aboutbox);
3456         return;
3457     }
3458
3459     aboutbox = gtk_dialog_new();
3460     gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
3461     title = dupcat("About ", appname, NULL);
3462     gtk_window_set_title(GTK_WINDOW(aboutbox), title);
3463     sfree(title);
3464
3465     w = gtk_button_new_with_label("Close");
3466     GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
3467     gtk_window_set_default(GTK_WINDOW(aboutbox), w);
3468     gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
3469                      w, FALSE, FALSE, 0);
3470     gtk_signal_connect(GTK_OBJECT(w), "clicked",
3471                        GTK_SIGNAL_FUNC(about_close_clicked), NULL);
3472     gtk_widget_show(w);
3473
3474     w = gtk_button_new_with_label("View Licence");
3475     GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
3476     gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
3477                      w, FALSE, FALSE, 0);
3478     gtk_signal_connect(GTK_OBJECT(w), "clicked",
3479                        GTK_SIGNAL_FUNC(licence_clicked), NULL);
3480     gtk_widget_show(w);
3481
3482     w = gtk_label_new(appname);
3483     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
3484                        w, FALSE, FALSE, 0);
3485     gtk_widget_show(w);
3486
3487     w = gtk_label_new(ver);
3488     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
3489                        w, FALSE, FALSE, 5);
3490     gtk_widget_show(w);
3491
3492     w = gtk_label_new("Copyright 1997-2015 Simon Tatham. All rights reserved");
3493     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
3494                        w, FALSE, FALSE, 5);
3495     gtk_widget_show(w);
3496
3497     set_transient_window_pos(GTK_WIDGET(window), aboutbox);
3498     gtk_window_set_transient_for(GTK_WINDOW(aboutbox),
3499                                  GTK_WINDOW(window));
3500     gtk_widget_show(aboutbox);
3501 }
3502
3503 struct eventlog_stuff {
3504     GtkWidget *parentwin, *window;
3505     struct controlbox *eventbox;
3506     struct Shortcuts scs;
3507     struct dlgparam dp;
3508     union control *listctrl;
3509     char **events;
3510     int nevents, negsize;
3511     char *seldata;
3512     int sellen;
3513     int ignore_selchange;
3514 };
3515
3516 static void eventlog_destroy(GtkWidget *widget, gpointer data)
3517 {
3518     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3519
3520     es->window = NULL;
3521     sfree(es->seldata);
3522     es->seldata = NULL;
3523     dlg_cleanup(&es->dp);
3524     ctrl_free_box(es->eventbox);
3525 }
3526 static void eventlog_ok_handler(union control *ctrl, void *dlg,
3527                                 void *data, int event)
3528 {
3529     if (event == EVENT_ACTION)
3530         dlg_end(dlg, 0);
3531 }
3532 static void eventlog_list_handler(union control *ctrl, void *dlg,
3533                                   void *data, int event)
3534 {
3535     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3536
3537     if (event == EVENT_REFRESH) {
3538         int i;
3539
3540         dlg_update_start(ctrl, dlg);
3541         dlg_listbox_clear(ctrl, dlg);
3542         for (i = 0; i < es->nevents; i++) {
3543             dlg_listbox_add(ctrl, dlg, es->events[i]);
3544         }
3545         dlg_update_done(ctrl, dlg);
3546     } else if (event == EVENT_SELCHANGE) {
3547         int i;
3548         int selsize = 0;
3549
3550         /*
3551          * If this SELCHANGE event is happening as a result of
3552          * deliberate deselection because someone else has grabbed
3553          * the selection, the last thing we want to do is pre-empt
3554          * them.
3555          */
3556         if (es->ignore_selchange)
3557             return;
3558
3559         /*
3560          * Construct the data to use as the selection.
3561          */
3562         sfree(es->seldata);
3563         es->seldata = NULL;
3564         es->sellen = 0;
3565         for (i = 0; i < es->nevents; i++) {
3566             if (dlg_listbox_issel(ctrl, dlg, i)) {
3567                 int extralen = strlen(es->events[i]);
3568
3569                 if (es->sellen + extralen + 2 > selsize) {
3570                     selsize = es->sellen + extralen + 512;
3571                     es->seldata = sresize(es->seldata, selsize, char);
3572                 }
3573
3574                 strcpy(es->seldata + es->sellen, es->events[i]);
3575                 es->sellen += extralen;
3576                 es->seldata[es->sellen++] = '\n';
3577             }
3578         }
3579
3580         if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
3581                                     GDK_CURRENT_TIME)) {
3582             extern GdkAtom compound_text_atom;
3583
3584             gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3585                                      GDK_SELECTION_TYPE_STRING, 1);
3586             gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3587                                      compound_text_atom, 1);
3588         }
3589
3590     }
3591 }
3592
3593 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
3594                             guint info, guint time_stamp, gpointer data)
3595 {
3596     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3597
3598     gtk_selection_data_set(seldata, seldata->target, 8,
3599                            (unsigned char *)es->seldata, es->sellen);
3600 }
3601
3602 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
3603                               gpointer data)
3604 {
3605     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3606     struct uctrl *uc;
3607
3608     /*
3609      * Deselect everything in the list box.
3610      */
3611     uc = dlg_find_byctrl(&es->dp, es->listctrl);
3612     es->ignore_selchange = 1;
3613 #if !GTK_CHECK_VERSION(2,0,0)
3614     assert(uc->list);
3615     gtk_list_unselect_all(GTK_LIST(uc->list));
3616 #else
3617     assert(uc->treeview);
3618     gtk_tree_selection_unselect_all
3619         (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview)));
3620 #endif
3621     es->ignore_selchange = 0;
3622
3623     sfree(es->seldata);
3624     es->sellen = 0;
3625     es->seldata = NULL;
3626     return TRUE;
3627 }
3628
3629 void showeventlog(void *estuff, void *parentwin)
3630 {
3631     struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3632     GtkWidget *window, *w0, *w1;
3633     GtkWidget *parent = GTK_WIDGET(parentwin);
3634     struct controlset *s0, *s1;
3635     union control *c;
3636     int index;
3637     char *title;
3638
3639     if (es->window) {
3640         gtk_widget_grab_focus(es->window);
3641         return;
3642     }
3643
3644     dlg_init(&es->dp);
3645
3646     for (index = 0; index < lenof(es->scs.sc); index++) {
3647         es->scs.sc[index].action = SHORTCUT_EMPTY;
3648     }
3649
3650     es->eventbox = ctrl_new_box();
3651
3652     s0 = ctrl_getset(es->eventbox, "", "", "");
3653     ctrl_columns(s0, 3, 33, 34, 33);
3654     c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
3655                         eventlog_ok_handler, P(NULL));
3656     c->button.column = 1;
3657     c->button.isdefault = TRUE;
3658
3659     s1 = ctrl_getset(es->eventbox, "x", "", "");
3660     es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3661                                     eventlog_list_handler, P(es));
3662     c->listbox.height = 10;
3663     c->listbox.multisel = 2;
3664     c->listbox.ncols = 3;
3665     c->listbox.percentages = snewn(3, int);
3666     c->listbox.percentages[0] = 25;
3667     c->listbox.percentages[1] = 10;
3668     c->listbox.percentages[2] = 65;
3669
3670     es->window = window = gtk_dialog_new();
3671     title = dupcat(appname, " Event Log", NULL);
3672     gtk_window_set_title(GTK_WINDOW(window), title);
3673     sfree(title);
3674     w0 = layout_ctrls(&es->dp, &es->scs, s0, GTK_WINDOW(window));
3675     set_dialog_action_area(GTK_DIALOG(window), w0);
3676     gtk_widget_show(w0);
3677     w1 = layout_ctrls(&es->dp, &es->scs, s1, GTK_WINDOW(window));
3678     gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3679     gtk_widget_set_usize(w1, 20 +
3680                          string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3681                                       " IS QUITE LONG 'COS SSH LOG ENTRIES"
3682                                       " ARE WIDE"), -1);
3683     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
3684                        w1, TRUE, TRUE, 0);
3685     gtk_widget_show(w1);
3686
3687     es->dp.data = es;
3688     es->dp.shortcuts = &es->scs;
3689     es->dp.lastfocus = NULL;
3690     es->dp.retval = 0;
3691     es->dp.window = window;
3692
3693     dlg_refresh(NULL, &es->dp);
3694
3695     if (parent) {
3696         set_transient_window_pos(parent, window);
3697         gtk_window_set_transient_for(GTK_WINDOW(window),
3698                                      GTK_WINDOW(parent));
3699     } else
3700         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3701     gtk_widget_show(window);
3702
3703     gtk_signal_connect(GTK_OBJECT(window), "destroy",
3704                        GTK_SIGNAL_FUNC(eventlog_destroy), es);
3705     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3706                        GTK_SIGNAL_FUNC(win_key_press), &es->dp);
3707     gtk_signal_connect(GTK_OBJECT(window), "selection_get",
3708                        GTK_SIGNAL_FUNC(eventlog_selection_get), es);
3709     gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event",
3710                        GTK_SIGNAL_FUNC(eventlog_selection_clear), es);
3711 }
3712
3713 void *eventlogstuff_new(void)
3714 {
3715     struct eventlog_stuff *es;
3716     es = snew(struct eventlog_stuff);
3717     memset(es, 0, sizeof(*es));
3718     return es;
3719 }
3720
3721 void logevent_dlg(void *estuff, const char *string)
3722 {
3723     struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3724
3725     char timebuf[40];
3726     struct tm tm;
3727
3728     if (es->nevents >= es->negsize) {
3729         es->negsize += 64;
3730         es->events = sresize(es->events, es->negsize, char *);
3731     }
3732
3733     tm=ltime();
3734     strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3735
3736     es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3737     strcpy(es->events[es->nevents], timebuf);
3738     strcat(es->events[es->nevents], string);
3739     if (es->window) {
3740         dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3741     }
3742     es->nevents++;
3743 }
3744
3745 int askappend(void *frontend, Filename *filename,
3746               void (*callback)(void *ctx, int result), void *ctx)
3747 {
3748     static const char msgtemplate[] =
3749         "The session log file \"%.*s\" already exists. "
3750         "You can overwrite it with a new session log, "
3751         "append your session log to the end of it, "
3752         "or disable session logging for this session.";
3753     char *message;
3754     char *mbtitle;
3755     int mbret;
3756
3757     message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
3758     mbtitle = dupprintf("%s Log to File", appname);
3759
3760     mbret = messagebox(get_window(frontend), mbtitle, message,
3761                        string_width("LINE OF TEXT SUITABLE FOR THE"
3762                                     " ASKAPPEND WIDTH"),
3763                        "Overwrite", 'o', 1, 2,
3764                        "Append", 'a', 0, 1,
3765                        "Disable", 'd', -1, 0,
3766                        NULL);
3767
3768     sfree(message);
3769     sfree(mbtitle);
3770
3771     return mbret;
3772 }