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