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