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