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