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