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