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