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