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