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