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