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