]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/gtkdlg.c
Deal with the possibility of no valid font being selected at all
[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     assert(name);              /* should always be ok after OK pressed */
1230     gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1231     sfree(name);
1232 }
1233
1234 static void coloursel_ok(GtkButton *button, gpointer data)
1235 {
1236     struct dlgparam *dp = (struct dlgparam *)data;
1237     gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1238     struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1239     gdouble cvals[4];
1240     gtk_color_selection_get_color
1241         (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel),
1242          cvals);
1243     dp->coloursel_result.r = (int) (255 * cvals[0]);
1244     dp->coloursel_result.g = (int) (255 * cvals[1]);
1245     dp->coloursel_result.b = (int) (255 * cvals[2]);
1246     dp->coloursel_result.ok = TRUE;
1247     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1248 }
1249
1250 static void coloursel_cancel(GtkButton *button, gpointer data)
1251 {
1252     struct dlgparam *dp = (struct dlgparam *)data;
1253     gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1254     struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1255     dp->coloursel_result.ok = FALSE;
1256     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1257 }
1258
1259 static void filefont_clicked(GtkButton *button, gpointer data)
1260 {
1261     struct dlgparam *dp = (struct dlgparam *)data;
1262     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1263
1264     if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1265         GtkWidget *filesel =
1266             gtk_file_selection_new(uc->ctrl->fileselect.title);
1267         gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1268         gtk_object_set_data
1269             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1270              (gpointer)filesel);
1271         gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc);
1272         gtk_signal_connect
1273             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1274              GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp);
1275         gtk_signal_connect_object
1276             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1277              GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1278         gtk_signal_connect_object
1279             (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1280              GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1281         gtk_widget_show(filesel);
1282     }
1283
1284     if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1285         const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1286         unifontsel *fontsel = unifontsel_new("Select a font");
1287
1288         gtk_window_set_modal(fontsel->window, TRUE);
1289         unifontsel_set_name(fontsel, fontname);
1290         
1291         gtk_object_set_data(GTK_OBJECT(fontsel->ok_button),
1292                             "user-data", (gpointer)fontsel);
1293         fontsel->user_data = uc;
1294         gtk_signal_connect(GTK_OBJECT(fontsel->ok_button), "clicked",
1295                            GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1296         gtk_signal_connect_object(GTK_OBJECT(fontsel->ok_button), "clicked",
1297                                   GTK_SIGNAL_FUNC(unifontsel_destroy),
1298                                   (gpointer)fontsel);
1299         gtk_signal_connect_object(GTK_OBJECT(fontsel->cancel_button),"clicked",
1300                                   GTK_SIGNAL_FUNC(unifontsel_destroy),
1301                                   (gpointer)fontsel);
1302
1303         gtk_widget_show(GTK_WIDGET(fontsel->window));
1304     }
1305 }
1306
1307 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1308                             gpointer data)
1309 {
1310     struct dlgparam *dp = (struct dlgparam *)data;
1311     struct uctrl *uc = dlg_find_bywidget(dp, widget);
1312
1313     gtk_widget_set_usize(uc->text, alloc->width, -1);
1314     gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1315     gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig);
1316 }
1317
1318 /* ----------------------------------------------------------------------
1319  * This function does the main layout work: it reads a controlset,
1320  * it creates the relevant GTK controls, and returns a GtkWidget
1321  * containing the result. (This widget might be a title of some
1322  * sort, it might be a Columns containing many controls, or it
1323  * might be a GtkFrame containing a Columns; whatever it is, it's
1324  * definitely a GtkWidget and should probably be added to a
1325  * GtkVbox.)
1326  * 
1327  * `listitemheight' is used to calculate a usize for list boxes: it
1328  * should be the height from the size request of a GtkListItem.
1329  * 
1330  * `win' is required for setting the default button. If it is
1331  * non-NULL, all buttons created will be default-capable (so they
1332  * have extra space round them for the default highlight).
1333  */
1334 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1335                         struct controlset *s, int listitemheight,
1336                         GtkWindow *win)
1337 {
1338     Columns *cols;
1339     GtkWidget *ret;
1340     int i;
1341
1342     if (!s->boxname && s->boxtitle) {
1343         /* This controlset is a panel title. */
1344         return gtk_label_new(s->boxtitle);
1345     }
1346
1347     /*
1348      * Otherwise, we expect to be laying out actual controls, so
1349      * we'll start by creating a Columns for the purpose.
1350      */
1351     cols = COLUMNS(columns_new(4));
1352     ret = GTK_WIDGET(cols);
1353     gtk_widget_show(ret);
1354
1355     /*
1356      * Create a containing frame if we have a box name.
1357      */
1358     if (*s->boxname) {
1359         ret = gtk_frame_new(s->boxtitle);   /* NULL is valid here */
1360         gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1361         gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1362         gtk_widget_show(ret);
1363     }
1364
1365     /*
1366      * Now iterate through the controls themselves, create them,
1367      * and add them to the Columns.
1368      */
1369     for (i = 0; i < s->ncontrols; i++) {
1370         union control *ctrl = s->ctrls[i];
1371         struct uctrl *uc;
1372         int left = FALSE;
1373         GtkWidget *w = NULL;
1374
1375         switch (ctrl->generic.type) {
1376           case CTRL_COLUMNS:
1377             {
1378                 static const int simplecols[1] = { 100 };
1379                 columns_set_cols(cols, ctrl->columns.ncols,
1380                                  (ctrl->columns.percentages ?
1381                                   ctrl->columns.percentages : simplecols));
1382             }
1383             continue;                  /* no actual control created */
1384           case CTRL_TABDELAY:
1385             {
1386                 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1387                 if (uc)
1388                     columns_taborder_last(cols, uc->toplevel);
1389             }
1390             continue;                  /* no actual control created */
1391         }
1392
1393         uc = snew(struct uctrl);
1394         uc->ctrl = ctrl;
1395         uc->privdata = NULL;
1396         uc->privdata_needs_free = FALSE;
1397         uc->buttons = NULL;
1398         uc->entry = uc->list = uc->menu = NULL;
1399         uc->button = uc->optmenu = uc->text = NULL;
1400         uc->label = NULL;
1401         uc->nclicks = 0;
1402
1403         switch (ctrl->generic.type) {
1404           case CTRL_BUTTON:
1405             w = gtk_button_new_with_label(ctrl->generic.label);
1406             if (win) {
1407                 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
1408                 if (ctrl->button.isdefault)
1409                     gtk_window_set_default(win, w);
1410                 if (ctrl->button.iscancel)
1411                     dp->cancelbutton = w;
1412             }
1413             gtk_signal_connect(GTK_OBJECT(w), "clicked",
1414                                GTK_SIGNAL_FUNC(button_clicked), dp);
1415             gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1416                                GTK_SIGNAL_FUNC(widget_focus), dp);
1417             shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut,
1418                          SHORTCUT_UCTRL, uc);
1419             break;
1420           case CTRL_CHECKBOX:
1421             w = gtk_check_button_new_with_label(ctrl->generic.label);
1422             gtk_signal_connect(GTK_OBJECT(w), "toggled",
1423                                GTK_SIGNAL_FUNC(button_toggled), dp);
1424             gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1425                                GTK_SIGNAL_FUNC(widget_focus), dp);
1426             shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut,
1427                          SHORTCUT_UCTRL, uc);
1428             left = TRUE;
1429             break;
1430           case CTRL_RADIO:
1431             /*
1432              * Radio buttons get to go inside their own Columns, no
1433              * matter what.
1434              */
1435             {
1436                 gint i, *percentages;
1437                 GSList *group;
1438
1439                 w = columns_new(0);
1440                 if (ctrl->generic.label) {
1441                     GtkWidget *label = gtk_label_new(ctrl->generic.label);
1442                     columns_add(COLUMNS(w), label, 0, 1);
1443                     columns_force_left_align(COLUMNS(w), label);
1444                     gtk_widget_show(label);
1445                     shortcut_add(scs, label, ctrl->radio.shortcut,
1446                                  SHORTCUT_UCTRL, uc);
1447                     uc->label = label;
1448                 }
1449                 percentages = g_new(gint, ctrl->radio.ncolumns);
1450                 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1451                     percentages[i] =
1452                         ((100 * (i+1) / ctrl->radio.ncolumns) -
1453                          100 * i / ctrl->radio.ncolumns);
1454                 }
1455                 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1456                                  percentages);
1457                 g_free(percentages);
1458                 group = NULL;
1459
1460                 uc->nbuttons = ctrl->radio.nbuttons;
1461                 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1462
1463                 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1464                     GtkWidget *b;
1465                     gint colstart;
1466
1467                     b = (gtk_radio_button_new_with_label
1468                          (group, ctrl->radio.buttons[i]));
1469                     uc->buttons[i] = b;
1470                     group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
1471                     colstart = i % ctrl->radio.ncolumns;
1472                     columns_add(COLUMNS(w), b, colstart,
1473                                 (i == ctrl->radio.nbuttons-1 ?
1474                                  ctrl->radio.ncolumns - colstart : 1));
1475                     columns_force_left_align(COLUMNS(w), b);
1476                     gtk_widget_show(b);
1477                     gtk_signal_connect(GTK_OBJECT(b), "toggled",
1478                                        GTK_SIGNAL_FUNC(button_toggled), dp);
1479                     gtk_signal_connect(GTK_OBJECT(b), "focus_in_event",
1480                                        GTK_SIGNAL_FUNC(widget_focus), dp);
1481                     if (ctrl->radio.shortcuts) {
1482                         shortcut_add(scs, GTK_BIN(b)->child,
1483                                      ctrl->radio.shortcuts[i],
1484                                      SHORTCUT_UCTRL, uc);
1485                     }
1486                 }
1487             }
1488             break;
1489           case CTRL_EDITBOX:
1490             {
1491                 GtkRequisition req;
1492
1493                 if (ctrl->editbox.has_list) {
1494                     w = gtk_combo_new();
1495                     gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1496                     uc->entry = GTK_COMBO(w)->entry;
1497                     uc->list = GTK_COMBO(w)->list;
1498                 } else {
1499                     w = gtk_entry_new();
1500                     if (ctrl->editbox.password)
1501                         gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
1502                     uc->entry = w;
1503                 }
1504                 uc->entrysig =
1505                     gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
1506                                        GTK_SIGNAL_FUNC(editbox_changed), dp);
1507                 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
1508                                    GTK_SIGNAL_FUNC(editbox_key), dp);
1509                 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
1510                                    GTK_SIGNAL_FUNC(widget_focus), dp);
1511                 /*
1512                  * Edit boxes, for some strange reason, have a minimum
1513                  * width of 150 in GTK 1.2. We don't want this - we'd
1514                  * rather the edit boxes acquired their natural width
1515                  * from the column layout of the rest of the box.
1516                  *
1517                  * Also, while we're here, we'll squirrel away the
1518                  * edit box height so we can use that to centre its
1519                  * label vertically beside it.
1520                  */
1521                 gtk_widget_size_request(w, &req);
1522                 gtk_widget_set_usize(w, 10, req.height);
1523
1524                 if (ctrl->generic.label) {
1525                     GtkWidget *label, *container;
1526
1527                     label = gtk_label_new(ctrl->generic.label);
1528
1529                     shortcut_add(scs, label, ctrl->editbox.shortcut,
1530                                  SHORTCUT_FOCUS, uc->entry);
1531
1532                     container = columns_new(4);
1533                     if (ctrl->editbox.percentwidth == 100) {
1534                         columns_add(COLUMNS(container), label, 0, 1);
1535                         columns_force_left_align(COLUMNS(container), label);
1536                         columns_add(COLUMNS(container), w, 0, 1);
1537                     } else {
1538                         gint percentages[2];
1539                         percentages[1] = ctrl->editbox.percentwidth;
1540                         percentages[0] = 100 - ctrl->editbox.percentwidth;
1541                         columns_set_cols(COLUMNS(container), 2, percentages);
1542                         columns_add(COLUMNS(container), label, 0, 1);
1543                         columns_force_left_align(COLUMNS(container), label);
1544                         columns_add(COLUMNS(container), w, 1, 1);
1545                         /* Centre the label vertically. */
1546                         gtk_widget_set_usize(label, -1, req.height);
1547                         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1548                     }
1549                     gtk_widget_show(label);
1550                     gtk_widget_show(w);
1551
1552                     w = container;
1553                     uc->label = label;
1554                 }
1555                 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event",
1556                                    GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1557             }
1558             break;
1559           case CTRL_FILESELECT:
1560           case CTRL_FONTSELECT:
1561             {
1562                 GtkWidget *ww;
1563                 GtkRequisition req;
1564                 char *browsebtn =
1565                     (ctrl->generic.type == CTRL_FILESELECT ?
1566                      "Browse..." : "Change...");
1567
1568                 gint percentages[] = { 75, 25 };
1569                 w = columns_new(4);
1570                 columns_set_cols(COLUMNS(w), 2, percentages);
1571
1572                 if (ctrl->generic.label) {
1573                     ww = gtk_label_new(ctrl->generic.label);
1574                     columns_add(COLUMNS(w), ww, 0, 2);
1575                     columns_force_left_align(COLUMNS(w), ww);
1576                     gtk_widget_show(ww);
1577                     shortcut_add(scs, ww,
1578                                  (ctrl->generic.type == CTRL_FILESELECT ?
1579                                   ctrl->fileselect.shortcut :
1580                                   ctrl->fontselect.shortcut),
1581                                  SHORTCUT_UCTRL, uc);
1582                     uc->label = ww;
1583                 }
1584
1585                 uc->entry = ww = gtk_entry_new();
1586                 gtk_widget_size_request(ww, &req);
1587                 gtk_widget_set_usize(ww, 10, req.height);
1588                 columns_add(COLUMNS(w), ww, 0, 1);
1589                 gtk_widget_show(ww);
1590
1591                 uc->button = ww = gtk_button_new_with_label(browsebtn);
1592                 columns_add(COLUMNS(w), ww, 1, 1);
1593                 gtk_widget_show(ww);
1594
1595                 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
1596                                    GTK_SIGNAL_FUNC(editbox_key), dp);
1597                 uc->entrysig =
1598                     gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
1599                                        GTK_SIGNAL_FUNC(editbox_changed), dp);
1600                 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
1601                                    GTK_SIGNAL_FUNC(widget_focus), dp);
1602                 gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
1603                                    GTK_SIGNAL_FUNC(widget_focus), dp);
1604                 gtk_signal_connect(GTK_OBJECT(ww), "clicked",
1605                                    GTK_SIGNAL_FUNC(filefont_clicked), dp);
1606             }
1607             break;
1608           case CTRL_LISTBOX:
1609             if (ctrl->listbox.height == 0) {
1610                 uc->optmenu = w = gtk_option_menu_new();
1611                 uc->menu = gtk_menu_new();
1612                 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
1613                 gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data",
1614                                     (gpointer)uc->optmenu);
1615                 gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event",
1616                                    GTK_SIGNAL_FUNC(widget_focus), dp);
1617             } else {
1618                 uc->list = gtk_list_new();
1619                 if (ctrl->listbox.multisel == 2) {
1620                     gtk_list_set_selection_mode(GTK_LIST(uc->list),
1621                                                 GTK_SELECTION_EXTENDED);
1622                 } else if (ctrl->listbox.multisel == 1) {
1623                     gtk_list_set_selection_mode(GTK_LIST(uc->list),
1624                                                 GTK_SELECTION_MULTIPLE);
1625                 } else {
1626                     gtk_list_set_selection_mode(GTK_LIST(uc->list),
1627                                                 GTK_SELECTION_SINGLE);
1628                 }
1629                 w = gtk_scrolled_window_new(NULL, NULL);
1630                 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
1631                                                       uc->list);
1632                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
1633                                                GTK_POLICY_NEVER,
1634                                                GTK_POLICY_AUTOMATIC);
1635                 uc->adj = gtk_scrolled_window_get_vadjustment
1636                     (GTK_SCROLLED_WINDOW(w));
1637
1638                 gtk_widget_show(uc->list);
1639                 gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed",
1640                                    GTK_SIGNAL_FUNC(list_selchange), dp);
1641                 gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event",
1642                                    GTK_SIGNAL_FUNC(widget_focus), dp);
1643
1644                 /*
1645                  * Adjust the height of the scrolled window to the
1646                  * minimum given by the height parameter.
1647                  * 
1648                  * This piece of guesswork is a horrid hack based
1649                  * on looking inside the GTK 1.2 sources
1650                  * (specifically gtkviewport.c, which appears to be
1651                  * the widget which provides the border around the
1652                  * scrolling area). Anyone lets me know how I can
1653                  * do this in a way which isn't at risk from GTK
1654                  * upgrades, I'd be grateful.
1655                  */
1656                 {
1657                     int edge;
1658 #if GTK_CHECK_VERSION(2,0,0)
1659                     edge = GTK_WIDGET(uc->list)->style->ythickness;
1660 #else
1661                     edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
1662 #endif
1663                     gtk_widget_set_usize(w, 10,
1664                                          2*edge + (ctrl->listbox.height *
1665                                                    listitemheight));
1666                 }
1667
1668                 if (ctrl->listbox.draglist) {
1669                     /*
1670                      * GTK doesn't appear to make it easy to
1671                      * implement a proper draggable list; so
1672                      * instead I'm just going to have to put an Up
1673                      * and a Down button to the right of the actual
1674                      * list box. Ah well.
1675                      */
1676                     GtkWidget *cols, *button;
1677                     static const gint percentages[2] = { 80, 20 };
1678
1679                     cols = columns_new(4);
1680                     columns_set_cols(COLUMNS(cols), 2, percentages);
1681                     columns_add(COLUMNS(cols), w, 0, 1);
1682                     gtk_widget_show(w);
1683                     button = gtk_button_new_with_label("Up");
1684                     columns_add(COLUMNS(cols), button, 1, 1);
1685                     gtk_widget_show(button);
1686                     gtk_signal_connect(GTK_OBJECT(button), "clicked",
1687                                        GTK_SIGNAL_FUNC(draglist_up), dp);
1688                     gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
1689                                        GTK_SIGNAL_FUNC(widget_focus), dp);
1690                     button = gtk_button_new_with_label("Down");
1691                     columns_add(COLUMNS(cols), button, 1, 1);
1692                     gtk_widget_show(button);
1693                     gtk_signal_connect(GTK_OBJECT(button), "clicked",
1694                                        GTK_SIGNAL_FUNC(draglist_down), dp);
1695                     gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
1696                                        GTK_SIGNAL_FUNC(widget_focus), dp);
1697
1698                     w = cols;
1699                 }
1700
1701             }
1702             if (ctrl->generic.label) {
1703                 GtkWidget *label, *container;
1704
1705                 label = gtk_label_new(ctrl->generic.label);
1706
1707                 container = columns_new(4);
1708                 if (ctrl->listbox.percentwidth == 100) {
1709                     columns_add(COLUMNS(container), label, 0, 1);
1710                     columns_force_left_align(COLUMNS(container), label);
1711                     columns_add(COLUMNS(container), w, 0, 1);
1712                 } else {
1713                     gint percentages[2];
1714                     percentages[1] = ctrl->listbox.percentwidth;
1715                     percentages[0] = 100 - ctrl->listbox.percentwidth;
1716                     columns_set_cols(COLUMNS(container), 2, percentages);
1717                     columns_add(COLUMNS(container), label, 0, 1);
1718                     columns_force_left_align(COLUMNS(container), label);
1719                     columns_add(COLUMNS(container), w, 1, 1);
1720                 }
1721                 gtk_widget_show(label);
1722                 gtk_widget_show(w);
1723                 shortcut_add(scs, label, ctrl->listbox.shortcut,
1724                              SHORTCUT_UCTRL, uc);
1725                 w = container;
1726                 uc->label = label;
1727             }
1728             break;
1729           case CTRL_TEXT:
1730             /*
1731              * Wrapping text widgets don't sit well with the GTK
1732              * layout model, in which widgets state a minimum size
1733              * and the whole window then adjusts to the smallest
1734              * size it can sensibly take given its contents. A
1735              * wrapping text widget _has_ no clear minimum size;
1736              * instead it has a range of possibilities. It can be
1737              * one line deep but 2000 wide, or two lines deep and
1738              * 1000 pixels, or three by 867, or four by 500 and so
1739              * on. It can be as short as you like provided you
1740              * don't mind it being wide, or as narrow as you like
1741              * provided you don't mind it being tall.
1742              * 
1743              * Therefore, it fits very badly into the layout model.
1744              * Hence the only thing to do is pick a width and let
1745              * it choose its own number of lines. To do this I'm
1746              * going to cheat a little. All new wrapping text
1747              * widgets will be created with a minimal text content
1748              * "X"; then, after the rest of the dialog box is set
1749              * up and its size calculated, the text widgets will be
1750              * told their width and given their real text, which
1751              * will cause the size to be recomputed in the y
1752              * direction (because many of them will expand to more
1753              * than one line).
1754              */
1755             uc->text = w = gtk_label_new("X");
1756             gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
1757             gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
1758             uc->textsig =
1759                 gtk_signal_connect(GTK_OBJECT(w), "size-allocate",
1760                                    GTK_SIGNAL_FUNC(label_sizealloc), dp);
1761             break;
1762         }
1763
1764         assert(w != NULL);
1765
1766         columns_add(cols, w,
1767                     COLUMN_START(ctrl->generic.column),
1768                     COLUMN_SPAN(ctrl->generic.column));
1769         if (left)
1770             columns_force_left_align(cols, w);
1771         gtk_widget_show(w);
1772
1773         uc->toplevel = w;
1774         dlg_add_uctrl(dp, uc);
1775     }
1776
1777     return ret;
1778 }
1779
1780 struct selparam {
1781     struct dlgparam *dp;
1782     GtkNotebook *panels;
1783     GtkWidget *panel;
1784 #if !GTK_CHECK_VERSION(2,0,0)
1785     GtkWidget *treeitem;
1786 #endif
1787     struct Shortcuts shortcuts;
1788 };
1789
1790 #if GTK_CHECK_VERSION(2,0,0)
1791 static void treeselection_changed(GtkTreeSelection *treeselection,
1792                                   gpointer data)
1793 {
1794     struct selparam *sps = (struct selparam *)data, *sp;
1795     GtkTreeModel *treemodel;
1796     GtkTreeIter treeiter;
1797     gint spindex;
1798     gint page_num;
1799
1800     if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
1801         return;
1802
1803     gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
1804     sp = &sps[spindex];
1805
1806     page_num = gtk_notebook_page_num(sp->panels, sp->panel);
1807     gtk_notebook_set_page(sp->panels, page_num);
1808
1809     dlg_refresh(NULL, sp->dp);
1810
1811     sp->dp->shortcuts = &sp->shortcuts;
1812 }
1813 #else
1814 static void treeitem_sel(GtkItem *item, gpointer data)
1815 {
1816     struct selparam *sp = (struct selparam *)data;
1817     gint page_num;
1818
1819     page_num = gtk_notebook_page_num(sp->panels, sp->panel);
1820     gtk_notebook_set_page(sp->panels, page_num);
1821
1822     dlg_refresh(NULL, sp->dp);
1823
1824     sp->dp->shortcuts = &sp->shortcuts;
1825     sp->dp->currtreeitem = sp->treeitem;
1826 }
1827 #endif
1828
1829 static void window_destroy(GtkWidget *widget, gpointer data)
1830 {
1831     gtk_main_quit();
1832 }
1833
1834 #if !GTK_CHECK_VERSION(2,0,0)
1835 static int tree_grab_focus(struct dlgparam *dp)
1836 {
1837     int i, f;
1838
1839     /*
1840      * See if any of the treeitems has the focus.
1841      */
1842     f = -1;
1843     for (i = 0; i < dp->ntreeitems; i++)
1844         if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
1845             f = i;
1846             break;
1847         }
1848
1849     if (f >= 0)
1850         return FALSE;
1851     else {
1852         gtk_widget_grab_focus(dp->currtreeitem);
1853         return TRUE;
1854     }
1855 }
1856
1857 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
1858                 gpointer data)
1859 {
1860     struct dlgparam *dp = (struct dlgparam *)data;
1861
1862     gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus");
1863     /*
1864      * If there's a focused treeitem, we return FALSE to cause the
1865      * focus to move on to some totally other control. If not, we
1866      * focus the selected one.
1867      */
1868     return tree_grab_focus(dp);
1869 }
1870 #endif
1871
1872 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
1873 {
1874     struct dlgparam *dp = (struct dlgparam *)data;
1875
1876     if (event->keyval == GDK_Escape && dp->cancelbutton) {
1877         gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked");
1878         return TRUE;
1879     }
1880
1881     if ((event->state & GDK_MOD1_MASK) &&
1882         (unsigned char)event->string[0] > 0 &&
1883         (unsigned char)event->string[0] <= 127) {
1884         int schr = (unsigned char)event->string[0];
1885         struct Shortcut *sc = &dp->shortcuts->sc[schr];
1886
1887         switch (sc->action) {
1888           case SHORTCUT_TREE:
1889 #if GTK_CHECK_VERSION(2,0,0)
1890             gtk_widget_grab_focus(sc->widget);
1891 #else
1892             tree_grab_focus(dp);
1893 #endif
1894             break;
1895           case SHORTCUT_FOCUS:
1896             gtk_widget_grab_focus(sc->widget);
1897             break;
1898           case SHORTCUT_UCTRL:
1899             /*
1900              * We must do something sensible with a uctrl.
1901              * Precisely what this is depends on the type of
1902              * control.
1903              */
1904             switch (sc->uc->ctrl->generic.type) {
1905               case CTRL_CHECKBOX:
1906               case CTRL_BUTTON:
1907                 /* Check boxes and buttons get the focus _and_ get toggled. */
1908                 gtk_widget_grab_focus(sc->uc->toplevel);
1909                 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel),
1910                                         "clicked");
1911                 break;
1912               case CTRL_FILESELECT:
1913               case CTRL_FONTSELECT:
1914                 /* File/font selectors have their buttons pressed (ooer),
1915                  * and focus transferred to the edit box. */
1916                 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button),
1917                                         "clicked");
1918                 gtk_widget_grab_focus(sc->uc->entry);
1919                 break;
1920               case CTRL_RADIO:
1921                 /*
1922                  * Radio buttons are fun, because they have
1923                  * multiple shortcuts. We must find whether the
1924                  * activated shortcut is the shortcut for the whole
1925                  * group, or for a particular button. In the former
1926                  * case, we find the currently selected button and
1927                  * focus it; in the latter, we focus-and-click the
1928                  * button whose shortcut was pressed.
1929                  */
1930                 if (schr == sc->uc->ctrl->radio.shortcut) {
1931                     int i;
1932                     for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
1933                         if (gtk_toggle_button_get_active
1934                             (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
1935                             gtk_widget_grab_focus(sc->uc->buttons[i]);
1936                         }
1937                 } else if (sc->uc->ctrl->radio.shortcuts) {
1938                     int i;
1939                     for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
1940                         if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
1941                             gtk_widget_grab_focus(sc->uc->buttons[i]);
1942                             gtk_signal_emit_by_name
1943                                 (GTK_OBJECT(sc->uc->buttons[i]), "clicked");
1944                         }
1945                 }
1946                 break;
1947               case CTRL_LISTBOX:
1948                 /*
1949                  * If the list is really an option menu, we focus
1950                  * and click it. Otherwise we tell it to focus one
1951                  * of its children, which appears to do the Right
1952                  * Thing.
1953                  */
1954                 if (sc->uc->optmenu) {
1955                     GdkEventButton bev;
1956                     gint returnval;
1957
1958                     gtk_widget_grab_focus(sc->uc->optmenu);
1959                     /* Option menus don't work using the "clicked" signal.
1960                      * We need to manufacture a button press event :-/ */
1961                     bev.type = GDK_BUTTON_PRESS;
1962                     bev.button = 1;
1963                     gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu),
1964                                             "button_press_event",
1965                                             &bev, &returnval);
1966                 } else {
1967                     assert(sc->uc->list != NULL);
1968
1969 #if GTK_CHECK_VERSION(2,0,0)
1970                     gtk_widget_grab_focus(sc->uc->list);
1971 #else
1972                     gtk_container_focus(GTK_CONTAINER(sc->uc->list),
1973                                         GTK_DIR_TAB_FORWARD);
1974 #endif
1975                 }
1976                 break;
1977             }
1978             break;
1979         }
1980     }
1981
1982     return FALSE;
1983 }
1984
1985 #if !GTK_CHECK_VERSION(2,0,0)
1986 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
1987 {
1988     struct dlgparam *dp = (struct dlgparam *)data;
1989
1990     if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1991         event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
1992         int dir, i, j = -1;
1993         for (i = 0; i < dp->ntreeitems; i++)
1994             if (widget == dp->treeitems[i])
1995                 break;
1996         if (i < dp->ntreeitems) {
1997             if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
1998                 dir = -1;
1999             else
2000                 dir = +1;
2001
2002             while (1) {
2003                 i += dir;
2004                 if (i < 0 || i >= dp->ntreeitems)
2005                     break;             /* nothing in that dir to select */
2006                 /*
2007                  * Determine if this tree item is visible.
2008                  */
2009                 {
2010                     GtkWidget *w = dp->treeitems[i];
2011                     int vis = TRUE;
2012                     while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2013                         if (!GTK_WIDGET_VISIBLE(w)) {
2014                             vis = FALSE;
2015                             break;
2016                         }
2017                         w = w->parent;
2018                     }
2019                     if (vis) {
2020                         j = i;         /* got one */
2021                         break;
2022                     }
2023                 }
2024             }
2025         }
2026         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2027                                      "key_press_event");
2028         if (j >= 0) {
2029             gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle");
2030             gtk_widget_grab_focus(dp->treeitems[j]);
2031         }
2032         return TRUE;
2033     }
2034
2035     /*
2036      * It's nice for Left and Right to expand and collapse tree
2037      * branches.
2038      */
2039     if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2040         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2041                                      "key_press_event");
2042         gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2043         return TRUE;
2044     }
2045     if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2046         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2047                                      "key_press_event");
2048         gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2049         return TRUE;
2050     }
2051
2052     return FALSE;
2053 }
2054 #endif
2055
2056 static void shortcut_highlight(GtkWidget *labelw, int chr)
2057 {
2058     GtkLabel *label = GTK_LABEL(labelw);
2059     gchar *currstr, *pattern;
2060     int i;
2061
2062     gtk_label_get(label, &currstr);
2063     for (i = 0; currstr[i]; i++)
2064         if (tolower((unsigned char)currstr[i]) == chr) {
2065             GtkRequisition req;
2066
2067             pattern = dupprintf("%*s_", i, "");
2068
2069             gtk_widget_size_request(GTK_WIDGET(label), &req);
2070             gtk_label_set_pattern(label, pattern);
2071             gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
2072
2073             sfree(pattern);
2074             break;
2075         }
2076 }
2077
2078 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2079                   int chr, int action, void *ptr)
2080 {
2081     if (chr == NO_SHORTCUT)
2082         return;
2083
2084     chr = tolower((unsigned char)chr);
2085
2086     assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2087
2088     scs->sc[chr].action = action;
2089
2090     if (action == SHORTCUT_FOCUS) {
2091         scs->sc[chr].uc = NULL;
2092         scs->sc[chr].widget = (GtkWidget *)ptr;
2093     } else {
2094         scs->sc[chr].widget = NULL;
2095         scs->sc[chr].uc = (struct uctrl *)ptr;
2096     }
2097
2098     shortcut_highlight(labelw, chr);
2099 }
2100
2101 int get_listitemheight(void)
2102 {
2103     GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2104     GtkRequisition req;
2105     gtk_widget_size_request(listitem, &req);
2106     gtk_object_sink(GTK_OBJECT(listitem));
2107     return req.height;
2108 }
2109
2110 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2111 {
2112 #if !GTK_CHECK_VERSION(2,0,0)
2113
2114     /*
2115      * In GTK 1, laying out the buttons at the bottom of the
2116      * configuration box is nice and easy, because a GtkDialog's
2117      * action_area is a GtkHBox which stretches to cover the full
2118      * width of the dialog. So we just put our Columns widget
2119      * straight into that hbox, and it ends up just where we want
2120      * it.
2121      */
2122     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
2123                        w, TRUE, TRUE, 0);
2124
2125 #else
2126     /*
2127      * In GTK 2, the action area is now a GtkHButtonBox and its
2128      * layout behaviour seems to be different: it doesn't stretch
2129      * to cover the full width of the window, but instead finds its
2130      * own preferred width and right-aligns that within the window.
2131      * This isn't what we want, because we have both left-aligned
2132      * and right-aligned buttons coming out of the above call to
2133      * layout_ctrls(), and right-aligning the whole thing will
2134      * result in the former being centred and looking weird.
2135      *
2136      * So instead we abandon the dialog's action area completely:
2137      * we gtk_widget_hide() it in the below code, and we also call
2138      * gtk_dialog_set_has_separator() to remove the separator above
2139      * it. We then insert our own action area into the end of the
2140      * dialog's main vbox, and add our own separator above that.
2141      *
2142      * (Ideally, if we were a native GTK app, we would use the
2143      * GtkHButtonBox's _own_ innate ability to support one set of
2144      * buttons being right-aligned and one left-aligned. But to do
2145      * that here, we would have to either (a) pick apart our cross-
2146      * platform layout structures and treat them specially for this
2147      * particular set of controls, which would be painful, or else
2148      * (b) develop a special and simpler cross-platform
2149      * representation for these particular controls, and introduce
2150      * special-case code into all the _other_ platforms to handle
2151      * it. Neither appeals. Therefore, I regretfully discard the
2152      * GTKHButtonBox and go it alone.)
2153      */
2154
2155     GtkWidget *align;
2156     align = gtk_alignment_new(0, 0, 1, 1);
2157     gtk_container_add(GTK_CONTAINER(align), w);
2158     /*
2159      * The purpose of this GtkAlignment is to provide padding
2160      * around the buttons. The padding we use is twice the padding
2161      * used in our GtkColumns, because we nest two GtkColumns most
2162      * of the time (one separating the tree view from the main
2163      * controls, and another for the main controls themselves).
2164      */
2165 #if GTK_CHECK_VERSION(2,4,0)
2166     gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2167 #endif
2168     gtk_widget_show(align);
2169     gtk_box_pack_end(GTK_BOX(dlg->vbox), align, FALSE, TRUE, 0);
2170     w = gtk_hseparator_new();
2171     gtk_box_pack_end(GTK_BOX(dlg->vbox), w, FALSE, TRUE, 0);
2172     gtk_widget_show(w);
2173     gtk_widget_hide(dlg->action_area);
2174     gtk_dialog_set_has_separator(dlg, FALSE);
2175 #endif
2176 }
2177
2178 int do_config_box(const char *title, Config *cfg, int midsession,
2179                   int protcfginfo)
2180 {
2181     GtkWidget *window, *hbox, *vbox, *cols, *label,
2182         *tree, *treescroll, *panels, *panelvbox;
2183     int index, level, listitemheight;
2184     struct controlbox *ctrlbox;
2185     char *path;
2186 #if GTK_CHECK_VERSION(2,0,0)
2187     GtkTreeStore *treestore;
2188     GtkCellRenderer *treerenderer;
2189     GtkTreeViewColumn *treecolumn;
2190     GtkTreeSelection *treeselection;
2191     GtkTreeIter treeiterlevels[8];
2192 #else
2193     GtkTreeItem *treeitemlevels[8];
2194     GtkTree *treelevels[8];
2195 #endif
2196     struct dlgparam dp;
2197     struct Shortcuts scs;
2198
2199     struct selparam *selparams = NULL;
2200     int nselparams = 0, selparamsize = 0;
2201
2202     dlg_init(&dp);
2203
2204     listitemheight = get_listitemheight();
2205
2206     for (index = 0; index < lenof(scs.sc); index++) {
2207         scs.sc[index].action = SHORTCUT_EMPTY;
2208     }
2209
2210     window = gtk_dialog_new();
2211
2212     ctrlbox = ctrl_new_box();
2213     setup_config_box(ctrlbox, midsession, cfg->protocol, protcfginfo);
2214     unix_setup_config_box(ctrlbox, midsession, cfg->protocol);
2215     gtk_setup_config_box(ctrlbox, midsession, window);
2216
2217     gtk_window_set_title(GTK_WINDOW(window), title);
2218     hbox = gtk_hbox_new(FALSE, 4);
2219     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0);
2220     gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2221     gtk_widget_show(hbox);
2222     vbox = gtk_vbox_new(FALSE, 4);
2223     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2224     gtk_widget_show(vbox);
2225     cols = columns_new(4);
2226     gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2227     gtk_widget_show(cols);
2228     label = gtk_label_new("Category:");
2229     columns_add(COLUMNS(cols), label, 0, 1);
2230     columns_force_left_align(COLUMNS(cols), label);
2231     gtk_widget_show(label);
2232     treescroll = gtk_scrolled_window_new(NULL, NULL);
2233 #if GTK_CHECK_VERSION(2,0,0)
2234     treestore = gtk_tree_store_new
2235         (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2236     tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2237     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2238     treerenderer = gtk_cell_renderer_text_new();
2239     treecolumn = gtk_tree_view_column_new_with_attributes
2240         ("Label", treerenderer, "text", 0, NULL);
2241     gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2242     treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2243     gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2244     gtk_container_add(GTK_CONTAINER(treescroll), tree);
2245 #else
2246     tree = gtk_tree_new();
2247     gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2248     gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2249     gtk_signal_connect(GTK_OBJECT(tree), "focus",
2250                        GTK_SIGNAL_FUNC(tree_focus), &dp);
2251 #endif
2252     gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event",
2253                        GTK_SIGNAL_FUNC(widget_focus), &dp);
2254     shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2255     gtk_widget_show(treescroll);
2256     gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2257     panels = gtk_notebook_new();
2258     gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2259     gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2260     gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2261     gtk_widget_show(panels);
2262
2263     panelvbox = NULL;
2264     path = NULL;
2265     level = 0;
2266     for (index = 0; index < ctrlbox->nctrlsets; index++) {
2267         struct controlset *s = ctrlbox->ctrlsets[index];
2268         GtkWidget *w;
2269
2270         if (!*s->pathname) {
2271             w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window));
2272
2273             set_dialog_action_area(GTK_DIALOG(window), w);
2274         } else {
2275             int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2276             if (j != INT_MAX) {        /* add to treeview, start new panel */
2277                 char *c;
2278 #if GTK_CHECK_VERSION(2,0,0)
2279                 GtkTreeIter treeiter;
2280 #else
2281                 GtkWidget *treeitem;
2282 #endif
2283                 int first;
2284
2285                 /*
2286                  * We expect never to find an implicit path
2287                  * component. For example, we expect never to see
2288                  * A/B/C followed by A/D/E, because that would
2289                  * _implicitly_ create A/D. All our path prefixes
2290                  * are expected to contain actual controls and be
2291                  * selectable in the treeview; so we would expect
2292                  * to see A/D _explicitly_ before encountering
2293                  * A/D/E.
2294                  */
2295                 assert(j == ctrl_path_elements(s->pathname) - 1);
2296
2297                 c = strrchr(s->pathname, '/');
2298                 if (!c)
2299                     c = s->pathname;
2300                 else
2301                     c++;
2302
2303                 path = s->pathname;
2304
2305                 first = (panelvbox == NULL);
2306
2307                 panelvbox = gtk_vbox_new(FALSE, 4);
2308                 gtk_widget_show(panelvbox);
2309                 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
2310                                          NULL);
2311                 if (first) {
2312                     gint page_num;
2313
2314                     page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
2315                                                      panelvbox);
2316                     gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num);
2317                 }
2318
2319                 if (nselparams >= selparamsize) {
2320                     selparamsize += 16;
2321                     selparams = sresize(selparams, selparamsize,
2322                                         struct selparam);
2323                 }
2324                 selparams[nselparams].dp = &dp;
2325                 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
2326                 selparams[nselparams].panel = panelvbox;
2327                 selparams[nselparams].shortcuts = scs;   /* structure copy */
2328
2329                 assert(j-1 < level);
2330
2331 #if GTK_CHECK_VERSION(2,0,0)
2332                 if (j > 0)
2333                     /* treeiterlevels[j-1] will always be valid because we
2334                      * don't allow implicit path components; see above.
2335                      */
2336                     gtk_tree_store_append(treestore, &treeiter,
2337                                           &treeiterlevels[j-1]);
2338                 else
2339                     gtk_tree_store_append(treestore, &treeiter, NULL);
2340                 gtk_tree_store_set(treestore, &treeiter,
2341                                    TREESTORE_PATH, c,
2342                                    TREESTORE_PARAMS, nselparams,
2343                                    -1);
2344                 treeiterlevels[j] = treeiter;
2345
2346                 if (j > 0) {
2347                     GtkTreePath *path;
2348
2349                     path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
2350                                                    &treeiterlevels[j-1]);
2351                     if (j < 2)
2352                         gtk_tree_view_expand_row(GTK_TREE_VIEW(tree), path,
2353                                                  FALSE);
2354                     else
2355                         gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree), path);
2356                     gtk_tree_path_free(path);
2357                 }
2358 #else
2359                 treeitem = gtk_tree_item_new_with_label(c);
2360                 if (j > 0) {
2361                     if (!treelevels[j-1]) {
2362                         treelevels[j-1] = GTK_TREE(gtk_tree_new());
2363                         gtk_tree_item_set_subtree
2364                             (treeitemlevels[j-1],
2365                              GTK_WIDGET(treelevels[j-1]));
2366                         if (j < 2)
2367                             gtk_tree_item_expand(treeitemlevels[j-1]);
2368                         else
2369                             gtk_tree_item_collapse(treeitemlevels[j-1]);
2370                     }
2371                     gtk_tree_append(treelevels[j-1], treeitem);
2372                 } else {
2373                     gtk_tree_append(GTK_TREE(tree), treeitem);
2374                 }
2375                 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
2376                 treelevels[j] = NULL;
2377
2378                 gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event",
2379                                    GTK_SIGNAL_FUNC(tree_key_press), &dp);
2380                 gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event",
2381                                    GTK_SIGNAL_FUNC(widget_focus), &dp);
2382
2383                 gtk_widget_show(treeitem);
2384
2385                 if (first)
2386                     gtk_tree_select_child(GTK_TREE(tree), treeitem);
2387                 selparams[nselparams].treeitem = treeitem;
2388 #endif
2389
2390                 level = j+1;
2391                 nselparams++;
2392             }
2393
2394             w = layout_ctrls(&dp,
2395                              &selparams[nselparams-1].shortcuts,
2396                              s, listitemheight, NULL);
2397             gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
2398             gtk_widget_show(w);
2399         }
2400     }
2401
2402 #if GTK_CHECK_VERSION(2,0,0)
2403     g_signal_connect(G_OBJECT(treeselection), "changed",
2404                      G_CALLBACK(treeselection_changed), selparams);
2405 #else
2406     dp.ntreeitems = nselparams;
2407     dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
2408
2409     for (index = 0; index < nselparams; index++) {
2410         gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
2411                            GTK_SIGNAL_FUNC(treeitem_sel),
2412                            &selparams[index]);
2413         dp.treeitems[index] = selparams[index].treeitem;
2414     }
2415 #endif
2416
2417     dp.data = cfg;
2418     dlg_refresh(NULL, &dp);
2419
2420     dp.shortcuts = &selparams[0].shortcuts;
2421 #if !GTK_CHECK_VERSION(2,0,0)
2422     dp.currtreeitem = dp.treeitems[0];
2423 #endif
2424     dp.lastfocus = NULL;
2425     dp.retval = 0;
2426     dp.window = window;
2427
2428     {
2429         /* in gtkwin.c */
2430         extern void set_window_icon(GtkWidget *window,
2431                                     const char *const *const *icon,
2432                                     int n_icon);
2433         extern const char *const *const cfg_icon[];
2434         extern const int n_cfg_icon;
2435         set_window_icon(window, cfg_icon, n_cfg_icon);
2436     }
2437
2438 #if !GTK_CHECK_VERSION(2,0,0)
2439     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
2440                                           tree);
2441 #endif
2442     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
2443                                    GTK_POLICY_NEVER,
2444                                    GTK_POLICY_AUTOMATIC);
2445     gtk_widget_show(tree);
2446
2447     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2448     gtk_widget_show(window);
2449
2450     /*
2451      * Set focus into the first available control.
2452      */
2453     for (index = 0; index < ctrlbox->nctrlsets; index++) {
2454         struct controlset *s = ctrlbox->ctrlsets[index];
2455         int done = 0;
2456         int j;
2457
2458         if (*s->pathname) {
2459             for (j = 0; j < s->ncontrols; j++)
2460                 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
2461                     s->ctrls[j]->generic.type != CTRL_COLUMNS &&
2462                     s->ctrls[j]->generic.type != CTRL_TEXT) {
2463                     dlg_set_focus(s->ctrls[j], &dp);
2464                     dp.lastfocus = s->ctrls[j];
2465                     done = 1;
2466                     break;
2467                 }
2468         }
2469         if (done)
2470             break;
2471     }
2472
2473     gtk_signal_connect(GTK_OBJECT(window), "destroy",
2474                        GTK_SIGNAL_FUNC(window_destroy), NULL);
2475     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
2476                        GTK_SIGNAL_FUNC(win_key_press), &dp);
2477
2478     gtk_main();
2479
2480     dlg_cleanup(&dp);
2481     sfree(selparams);
2482
2483     return dp.retval;
2484 }
2485
2486 static void messagebox_handler(union control *ctrl, void *dlg,
2487                                void *data, int event)
2488 {
2489     if (event == EVENT_ACTION)
2490         dlg_end(dlg, ctrl->generic.context.i);
2491 }
2492 int messagebox(GtkWidget *parentwin, char *title, char *msg, int minwid, ...)
2493 {
2494     GtkWidget *window, *w0, *w1;
2495     struct controlbox *ctrlbox;
2496     struct controlset *s0, *s1;
2497     union control *c;
2498     struct dlgparam dp;
2499     struct Shortcuts scs;
2500     int index, ncols;
2501     va_list ap;
2502
2503     dlg_init(&dp);
2504
2505     for (index = 0; index < lenof(scs.sc); index++) {
2506         scs.sc[index].action = SHORTCUT_EMPTY;
2507     }
2508
2509     ctrlbox = ctrl_new_box();
2510
2511     ncols = 0;
2512     va_start(ap, minwid);
2513     while (va_arg(ap, char *) != NULL) {
2514         ncols++;
2515         (void) va_arg(ap, int);        /* shortcut */
2516         (void) va_arg(ap, int);        /* normal/default/cancel */
2517         (void) va_arg(ap, int);        /* end value */
2518     }
2519     va_end(ap);
2520
2521     s0 = ctrl_getset(ctrlbox, "", "", "");
2522     c = ctrl_columns(s0, 2, 50, 50);
2523     c->columns.ncols = s0->ncolumns = ncols;
2524     c->columns.percentages = sresize(c->columns.percentages, ncols, int);
2525     for (index = 0; index < ncols; index++)
2526         c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
2527     va_start(ap, minwid);
2528     index = 0;
2529     while (1) {
2530         char *title = va_arg(ap, char *);
2531         int shortcut, type, value;
2532         if (title == NULL)
2533             break;
2534         shortcut = va_arg(ap, int);
2535         type = va_arg(ap, int);
2536         value = va_arg(ap, int);
2537         c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
2538                             messagebox_handler, I(value));
2539         c->generic.column = index++;
2540         if (type > 0)
2541             c->button.isdefault = TRUE;
2542         else if (type < 0)
2543             c->button.iscancel = TRUE;
2544     }
2545     va_end(ap);
2546
2547     s1 = ctrl_getset(ctrlbox, "x", "", "");
2548     ctrl_text(s1, msg, HELPCTX(no_help));
2549
2550     window = gtk_dialog_new();
2551     gtk_window_set_title(GTK_WINDOW(window), title);
2552     w0 = layout_ctrls(&dp, &scs, s0, 0, GTK_WINDOW(window));
2553     set_dialog_action_area(GTK_DIALOG(window), w0);
2554     gtk_widget_show(w0);
2555     w1 = layout_ctrls(&dp, &scs, s1, 0, GTK_WINDOW(window));
2556     gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
2557     gtk_widget_set_usize(w1, minwid+20, -1);
2558     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
2559                        w1, TRUE, TRUE, 0);
2560     gtk_widget_show(w1);
2561
2562     dp.shortcuts = &scs;
2563     dp.lastfocus = NULL;
2564     dp.retval = 0;
2565     dp.window = window;
2566
2567     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
2568     if (parentwin) {
2569         set_transient_window_pos(parentwin, window);
2570         gtk_window_set_transient_for(GTK_WINDOW(window),
2571                                      GTK_WINDOW(parentwin));
2572     } else
2573         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2574     gtk_widget_show(window);
2575
2576     gtk_signal_connect(GTK_OBJECT(window), "destroy",
2577                        GTK_SIGNAL_FUNC(window_destroy), NULL);
2578     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
2579                        GTK_SIGNAL_FUNC(win_key_press), &dp);
2580
2581     gtk_main();
2582
2583     dlg_cleanup(&dp);
2584     ctrl_free_box(ctrlbox);
2585
2586     return dp.retval;
2587 }
2588
2589 static int string_width(char *text)
2590 {
2591     GtkWidget *label = gtk_label_new(text);
2592     GtkRequisition req;
2593     gtk_widget_size_request(label, &req);
2594     gtk_object_sink(GTK_OBJECT(label));
2595     return req.width;
2596 }
2597
2598 int reallyclose(void *frontend)
2599 {
2600     char *title = dupcat(appname, " Exit Confirmation", NULL);
2601     int ret = messagebox(GTK_WIDGET(get_window(frontend)),
2602                          title, "Are you sure you want to close this session?",
2603                          string_width("Most of the width of the above text"),
2604                          "Yes", 'y', +1, 1,
2605                          "No", 'n', -1, 0,
2606                          NULL);
2607     sfree(title);
2608     return ret;
2609 }
2610
2611 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
2612                         char *keystr, char *fingerprint,
2613                         void (*callback)(void *ctx, int result), void *ctx)
2614 {
2615     static const char absenttxt[] =
2616         "The server's host key is not cached. You have no guarantee "
2617         "that the server is the computer you think it is.\n"
2618         "The server's %s key fingerprint is:\n"
2619         "%s\n"
2620         "If you trust this host, press \"Accept\" to add the key to "
2621         "PuTTY's cache and carry on connecting.\n"
2622         "If you want to carry on connecting just once, without "
2623         "adding the key to the cache, press \"Connect Once\".\n"
2624         "If you do not trust this host, press \"Cancel\" to abandon the "
2625         "connection.";
2626     static const char wrongtxt[] =
2627         "WARNING - POTENTIAL SECURITY BREACH!\n"
2628         "The server's host key does not match the one PuTTY has "
2629         "cached. This means that either the server administrator "
2630         "has changed the host key, or you have actually connected "
2631         "to another computer pretending to be the server.\n"
2632         "The new %s key fingerprint is:\n"
2633         "%s\n"
2634         "If you were expecting this change and trust the new key, "
2635         "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2636         "If you want to carry on connecting but without updating "
2637         "the cache, press \"Connect Once\".\n"
2638         "If you want to abandon the connection completely, press "
2639         "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2640         "safe choice.";
2641     char *text;
2642     int ret;
2643
2644     /*
2645      * Verify the key.
2646      */
2647     ret = verify_host_key(host, port, keytype, keystr);
2648
2649     if (ret == 0)                      /* success - key matched OK */
2650         return 1;
2651
2652     text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
2653
2654     ret = messagebox(GTK_WIDGET(get_window(frontend)),
2655                      "PuTTY Security Alert", text,
2656                      string_width(fingerprint),
2657                      "Accept", 'a', 0, 2,
2658                      "Connect Once", 'o', 0, 1,
2659                      "Cancel", 'c', -1, 0,
2660                      NULL);
2661
2662     sfree(text);
2663
2664     if (ret == 2) {
2665         store_host_key(host, port, keytype, keystr);
2666         return 1;                      /* continue with connection */
2667     } else if (ret == 1)
2668         return 1;                      /* continue with connection */
2669     return 0;                          /* do not continue with connection */
2670 }
2671
2672 /*
2673  * Ask whether the selected algorithm is acceptable (since it was
2674  * below the configured 'warn' threshold).
2675  */
2676 int askalg(void *frontend, const char *algtype, const char *algname,
2677            void (*callback)(void *ctx, int result), void *ctx)
2678 {
2679     static const char msg[] =
2680         "The first %s supported by the server is "
2681         "%s, which is below the configured warning threshold.\n"
2682         "Continue with connection?";
2683     char *text;
2684     int ret;
2685
2686     text = dupprintf(msg, algtype, algname);
2687     ret = messagebox(GTK_WIDGET(get_window(frontend)),
2688                      "PuTTY Security Alert", text,
2689                      string_width("Continue with connection?"),
2690                      "Yes", 'y', 0, 1,
2691                      "No", 'n', 0, 0,
2692                      NULL);
2693     sfree(text);
2694
2695     if (ret) {
2696         return 1;
2697     } else {
2698         return 0;
2699     }
2700 }
2701
2702 void old_keyfile_warning(void)
2703 {
2704     /*
2705      * This should never happen on Unix. We hope.
2706      */
2707 }
2708
2709 void fatal_message_box(void *window, char *msg)
2710 {
2711     messagebox(window, "PuTTY Fatal Error", msg,
2712                string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2713                "OK", 'o', 1, 1, NULL);
2714 }
2715
2716 void fatalbox(char *p, ...)
2717 {
2718     va_list ap;
2719     char *msg;
2720     va_start(ap, p);
2721     msg = dupvprintf(p, ap);
2722     va_end(ap);
2723     fatal_message_box(NULL, msg);
2724     sfree(msg);
2725     cleanup_exit(1);
2726 }
2727
2728 static GtkWidget *aboutbox = NULL;
2729
2730 static void about_close_clicked(GtkButton *button, gpointer data)
2731 {
2732     gtk_widget_destroy(aboutbox);
2733     aboutbox = NULL;
2734 }
2735
2736 static void licence_clicked(GtkButton *button, gpointer data)
2737 {
2738     char *title;
2739
2740     char *licence =
2741         "Copyright 1997-2008 Simon Tatham.\n\n"
2742
2743         "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2744         "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2745         "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
2746         "Markus Kuhn, Colin Watson, and CORE SDI S.A.\n\n"
2747
2748         "Permission is hereby granted, free of charge, to any person "
2749         "obtaining a copy of this software and associated documentation "
2750         "files (the ""Software""), to deal in the Software without restriction, "
2751         "including without limitation the rights to use, copy, modify, merge, "
2752         "publish, distribute, sublicense, and/or sell copies of the Software, "
2753         "and to permit persons to whom the Software is furnished to do so, "
2754         "subject to the following conditions:\n\n"
2755
2756         "The above copyright notice and this permission notice shall be "
2757         "included in all copies or substantial portions of the Software.\n\n"
2758
2759         "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2760         "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2761         "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2762         "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2763         "PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE "
2764         "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2765         "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2766         "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2767         "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2768         "OTHER DEALINGS IN THE SOFTWARE.";
2769
2770     title = dupcat(appname, " Licence", NULL);
2771     assert(aboutbox != NULL);
2772     messagebox(aboutbox, title, licence,
2773                string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2774                             " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2775                "OK", 'o', 1, 1, NULL);
2776     sfree(title);
2777 }
2778
2779 void about_box(void *window)
2780 {
2781     GtkWidget *w;
2782     char *title;
2783
2784     if (aboutbox) {
2785         gtk_widget_grab_focus(aboutbox);
2786         return;
2787     }
2788
2789     aboutbox = gtk_dialog_new();
2790     gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
2791     title = dupcat("About ", appname, NULL);
2792     gtk_window_set_title(GTK_WINDOW(aboutbox), title);
2793     sfree(title);
2794
2795     w = gtk_button_new_with_label("Close");
2796     GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
2797     gtk_window_set_default(GTK_WINDOW(aboutbox), w);
2798     gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
2799                      w, FALSE, FALSE, 0);
2800     gtk_signal_connect(GTK_OBJECT(w), "clicked",
2801                        GTK_SIGNAL_FUNC(about_close_clicked), NULL);
2802     gtk_widget_show(w);
2803
2804     w = gtk_button_new_with_label("View Licence");
2805     GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
2806     gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
2807                      w, FALSE, FALSE, 0);
2808     gtk_signal_connect(GTK_OBJECT(w), "clicked",
2809                        GTK_SIGNAL_FUNC(licence_clicked), NULL);
2810     gtk_widget_show(w);
2811
2812     w = gtk_label_new(appname);
2813     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2814                        w, FALSE, FALSE, 0);
2815     gtk_widget_show(w);
2816
2817     w = gtk_label_new(ver);
2818     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2819                        w, FALSE, FALSE, 5);
2820     gtk_widget_show(w);
2821
2822     w = gtk_label_new("Copyright 1997-2008 Simon Tatham. All rights reserved");
2823     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2824                        w, FALSE, FALSE, 5);
2825     gtk_widget_show(w);
2826
2827     set_transient_window_pos(GTK_WIDGET(window), aboutbox);
2828     gtk_widget_show(aboutbox);
2829 }
2830
2831 struct eventlog_stuff {
2832     GtkWidget *parentwin, *window;
2833     struct controlbox *eventbox;
2834     struct Shortcuts scs;
2835     struct dlgparam dp;
2836     union control *listctrl;
2837     char **events;
2838     int nevents, negsize;
2839     char *seldata;
2840     int sellen;
2841     int ignore_selchange;
2842 };
2843
2844 static void eventlog_destroy(GtkWidget *widget, gpointer data)
2845 {
2846     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2847
2848     es->window = NULL;
2849     sfree(es->seldata);
2850     dlg_cleanup(&es->dp);
2851     ctrl_free_box(es->eventbox);
2852 }
2853 static void eventlog_ok_handler(union control *ctrl, void *dlg,
2854                                 void *data, int event)
2855 {
2856     if (event == EVENT_ACTION)
2857         dlg_end(dlg, 0);
2858 }
2859 static void eventlog_list_handler(union control *ctrl, void *dlg,
2860                                   void *data, int event)
2861 {
2862     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2863
2864     if (event == EVENT_REFRESH) {
2865         int i;
2866
2867         dlg_update_start(ctrl, dlg);
2868         dlg_listbox_clear(ctrl, dlg);
2869         for (i = 0; i < es->nevents; i++) {
2870             dlg_listbox_add(ctrl, dlg, es->events[i]);
2871         }
2872         dlg_update_done(ctrl, dlg);
2873     } else if (event == EVENT_SELCHANGE) {
2874         int i;
2875         int selsize = 0;
2876
2877         /*
2878          * If this SELCHANGE event is happening as a result of
2879          * deliberate deselection because someone else has grabbed
2880          * the selection, the last thing we want to do is pre-empt
2881          * them.
2882          */
2883         if (es->ignore_selchange)
2884             return;
2885
2886         /*
2887          * Construct the data to use as the selection.
2888          */
2889         sfree(es->seldata);
2890         es->seldata = NULL;
2891         es->sellen = 0;
2892         for (i = 0; i < es->nevents; i++) {
2893             if (dlg_listbox_issel(ctrl, dlg, i)) {
2894                 int extralen = strlen(es->events[i]);
2895
2896                 if (es->sellen + extralen + 2 > selsize) {
2897                     selsize = es->sellen + extralen + 512;
2898                     es->seldata = sresize(es->seldata, selsize, char);
2899                 }
2900
2901                 strcpy(es->seldata + es->sellen, es->events[i]);
2902                 es->sellen += extralen;
2903                 es->seldata[es->sellen++] = '\n';
2904             }
2905         }
2906
2907         if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
2908                                     GDK_CURRENT_TIME)) {
2909             extern GdkAtom compound_text_atom;
2910
2911             gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
2912                                      GDK_SELECTION_TYPE_STRING, 1);
2913             gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
2914                                      compound_text_atom, 1);
2915         }
2916
2917     }
2918 }
2919
2920 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2921                             guint info, guint time_stamp, gpointer data)
2922 {
2923     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2924
2925     gtk_selection_data_set(seldata, seldata->target, 8,
2926                            (unsigned char *)es->seldata, es->sellen);
2927 }
2928
2929 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2930                               gpointer data)
2931 {
2932     struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2933     struct uctrl *uc;
2934
2935     /*
2936      * Deselect everything in the list box.
2937      */
2938     uc = dlg_find_byctrl(&es->dp, es->listctrl);
2939     es->ignore_selchange = 1;
2940     gtk_list_unselect_all(GTK_LIST(uc->list));
2941     es->ignore_selchange = 0;
2942
2943     sfree(es->seldata);
2944     es->sellen = 0;
2945     es->seldata = NULL;
2946     return TRUE;
2947 }
2948
2949 void showeventlog(void *estuff, void *parentwin)
2950 {
2951     struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
2952     GtkWidget *window, *w0, *w1;
2953     GtkWidget *parent = GTK_WIDGET(parentwin);
2954     struct controlset *s0, *s1;
2955     union control *c;
2956     int listitemheight, index;
2957     char *title;
2958
2959     if (es->window) {
2960         gtk_widget_grab_focus(es->window);
2961         return;
2962     }
2963
2964     dlg_init(&es->dp);
2965
2966     for (index = 0; index < lenof(es->scs.sc); index++) {
2967         es->scs.sc[index].action = SHORTCUT_EMPTY;
2968     }
2969
2970     es->eventbox = ctrl_new_box();
2971
2972     s0 = ctrl_getset(es->eventbox, "", "", "");
2973     ctrl_columns(s0, 3, 33, 34, 33);
2974     c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
2975                         eventlog_ok_handler, P(NULL));
2976     c->button.column = 1;
2977     c->button.isdefault = TRUE;
2978
2979     s1 = ctrl_getset(es->eventbox, "x", "", "");
2980     es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
2981                                     eventlog_list_handler, P(es));
2982     c->listbox.height = 10;
2983     c->listbox.multisel = 2;
2984     c->listbox.ncols = 3;
2985     c->listbox.percentages = snewn(3, int);
2986     c->listbox.percentages[0] = 25;
2987     c->listbox.percentages[1] = 10;
2988     c->listbox.percentages[2] = 65;
2989
2990     listitemheight = get_listitemheight();
2991
2992     es->window = window = gtk_dialog_new();
2993     title = dupcat(appname, " Event Log", NULL);
2994     gtk_window_set_title(GTK_WINDOW(window), title);
2995     sfree(title);
2996     w0 = layout_ctrls(&es->dp, &es->scs, s0,
2997                       listitemheight, GTK_WINDOW(window));
2998     set_dialog_action_area(GTK_DIALOG(window), w0);
2999     gtk_widget_show(w0);
3000     w1 = layout_ctrls(&es->dp, &es->scs, s1,
3001                       listitemheight, GTK_WINDOW(window));
3002     gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3003     gtk_widget_set_usize(w1, 20 +
3004                          string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3005                                       " IS QUITE LONG 'COS SSH LOG ENTRIES"
3006                                       " ARE WIDE"), -1);
3007     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
3008                        w1, TRUE, TRUE, 0);
3009     gtk_widget_show(w1);
3010
3011     es->dp.data = es;
3012     es->dp.shortcuts = &es->scs;
3013     es->dp.lastfocus = NULL;
3014     es->dp.retval = 0;
3015     es->dp.window = window;
3016
3017     dlg_refresh(NULL, &es->dp);
3018
3019     if (parent) {
3020         set_transient_window_pos(parent, window);
3021         gtk_window_set_transient_for(GTK_WINDOW(window),
3022                                      GTK_WINDOW(parent));
3023     } else
3024         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3025     gtk_widget_show(window);
3026
3027     gtk_signal_connect(GTK_OBJECT(window), "destroy",
3028                        GTK_SIGNAL_FUNC(eventlog_destroy), es);
3029     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3030                        GTK_SIGNAL_FUNC(win_key_press), &es->dp);
3031     gtk_signal_connect(GTK_OBJECT(window), "selection_get",
3032                        GTK_SIGNAL_FUNC(eventlog_selection_get), es);
3033     gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event",
3034                        GTK_SIGNAL_FUNC(eventlog_selection_clear), es);
3035 }
3036
3037 void *eventlogstuff_new(void)
3038 {
3039     struct eventlog_stuff *es;
3040     es = snew(struct eventlog_stuff);
3041     memset(es, 0, sizeof(*es));
3042     return es;
3043 }
3044
3045 void logevent_dlg(void *estuff, const char *string)
3046 {
3047     struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3048
3049     char timebuf[40];
3050     struct tm tm;
3051
3052     if (es->nevents >= es->negsize) {
3053         es->negsize += 64;
3054         es->events = sresize(es->events, es->negsize, char *);
3055     }
3056
3057     tm=ltime();
3058     strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3059
3060     es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3061     strcpy(es->events[es->nevents], timebuf);
3062     strcat(es->events[es->nevents], string);
3063     if (es->window) {
3064         dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3065     }
3066     es->nevents++;
3067 }
3068
3069 int askappend(void *frontend, Filename filename,
3070               void (*callback)(void *ctx, int result), void *ctx)
3071 {
3072     static const char msgtemplate[] =
3073         "The session log file \"%.*s\" already exists. "
3074         "You can overwrite it with a new session log, "
3075         "append your session log to the end of it, "
3076         "or disable session logging for this session.";
3077     char *message;
3078     char *mbtitle;
3079     int mbret;
3080
3081     message = dupprintf(msgtemplate, FILENAME_MAX, filename.path);
3082     mbtitle = dupprintf("%s Log to File", appname);
3083
3084     mbret = messagebox(get_window(frontend), mbtitle, message,
3085                        string_width("LINE OF TEXT SUITABLE FOR THE"
3086                                     " ASKAPPEND WIDTH"),
3087                        "Overwrite", 'o', 1, 2,
3088                        "Append", 'a', 0, 1,
3089                        "Disable", 'd', -1, 0,
3090                        NULL);
3091
3092     sfree(message);
3093     sfree(mbtitle);
3094
3095     return mbret;
3096 }