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