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