2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
10 #include <gdk/gdkkeysyms.h>
13 #include <X11/Xutil.h>
19 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
34 struct Shortcut sc[128];
41 int privdata_needs_free;
42 GtkWidget **buttons; int nbuttons; /* for radio buttons */
43 GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */
44 GtkWidget *button; /* for filesel, fontsel */
45 GtkWidget *list; /* for combobox, listbox */
46 GtkWidget *menu; /* for optionmenu (==droplist) */
47 GtkWidget *optmenu; /* also for optionmenu */
48 GtkWidget *text; /* for text */
49 GtkWidget *label; /* for dlg_label_change */
50 GtkAdjustment *adj; /* for the scrollbar in a list box */
57 tree234 *byctrl, *bywidget;
59 struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
60 /* `flags' are set to indicate when a GTK signal handler is being called
61 * due to automatic processing and should not flag a user event. */
63 struct Shortcuts *shortcuts;
64 GtkWidget *window, *cancelbutton;
65 union control *currfocus, *lastfocus;
66 #if !GTK_CHECK_VERSION(2,0,0)
67 GtkWidget *currtreeitem, **treeitems;
72 #define FLAG_UPDATING_COMBO_LIST 1
74 enum { /* values for Shortcut.action */
75 SHORTCUT_EMPTY, /* no shortcut on this key */
76 SHORTCUT_TREE, /* focus a tree item */
77 SHORTCUT_FOCUS, /* focus the supplied widget */
78 SHORTCUT_UCTRL, /* do something sane with uctrl */
79 SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */
80 SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */
83 #if GTK_CHECK_VERSION(2,0,0)
94 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
96 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
97 int chr, int action, void *ptr);
98 static void shortcut_highlight(GtkWidget *label, int chr);
99 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
101 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
103 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
105 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
107 static void menuitem_activate(GtkMenuItem *item, gpointer data);
108 static void coloursel_ok(GtkButton *button, gpointer data);
109 static void coloursel_cancel(GtkButton *button, gpointer data);
110 static void window_destroy(GtkWidget *widget, gpointer data);
112 static int uctrl_cmp_byctrl(void *av, void *bv)
114 struct uctrl *a = (struct uctrl *)av;
115 struct uctrl *b = (struct uctrl *)bv;
116 if (a->ctrl < b->ctrl)
118 else if (a->ctrl > b->ctrl)
123 static int uctrl_cmp_byctrl_find(void *av, void *bv)
125 union control *a = (union control *)av;
126 struct uctrl *b = (struct uctrl *)bv;
129 else if (a > b->ctrl)
134 static int uctrl_cmp_bywidget(void *av, void *bv)
136 struct uctrl *a = (struct uctrl *)av;
137 struct uctrl *b = (struct uctrl *)bv;
138 if (a->toplevel < b->toplevel)
140 else if (a->toplevel > b->toplevel)
145 static int uctrl_cmp_bywidget_find(void *av, void *bv)
147 GtkWidget *a = (GtkWidget *)av;
148 struct uctrl *b = (struct uctrl *)bv;
151 else if (a > b->toplevel)
156 static void dlg_init(struct dlgparam *dp)
158 dp->byctrl = newtree234(uctrl_cmp_byctrl);
159 dp->bywidget = newtree234(uctrl_cmp_bywidget);
160 dp->coloursel_result.ok = FALSE;
161 dp->window = dp->cancelbutton = NULL;
162 #if !GTK_CHECK_VERSION(2,0,0)
163 dp->treeitems = NULL;
164 dp->currtreeitem = NULL;
167 dp->currfocus = NULL;
170 static void dlg_cleanup(struct dlgparam *dp)
174 freetree234(dp->byctrl); /* doesn't free the uctrls inside */
176 while ( (uc = index234(dp->bywidget, 0)) != NULL) {
177 del234(dp->bywidget, uc);
178 if (uc->privdata_needs_free)
183 freetree234(dp->bywidget);
185 #if !GTK_CHECK_VERSION(2,0,0)
186 sfree(dp->treeitems);
190 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
192 add234(dp->byctrl, uc);
193 add234(dp->bywidget, uc);
196 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
200 return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
203 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
205 struct uctrl *ret = NULL;
209 ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
217 void *dlg_get_privdata(union control *ctrl, void *dlg)
219 struct dlgparam *dp = (struct dlgparam *)dlg;
220 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
224 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
226 struct dlgparam *dp = (struct dlgparam *)dlg;
227 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
229 uc->privdata_needs_free = FALSE;
232 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
234 struct dlgparam *dp = (struct dlgparam *)dlg;
235 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
237 * This is an internal allocation routine, so it's allowed to
238 * use smalloc directly.
240 uc->privdata = smalloc(size);
241 uc->privdata_needs_free = FALSE;
245 union control *dlg_last_focused(union control *ctrl, void *dlg)
247 struct dlgparam *dp = (struct dlgparam *)dlg;
248 if (dp->currfocus != ctrl)
249 return dp->currfocus;
251 return dp->lastfocus;
254 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
256 struct dlgparam *dp = (struct dlgparam *)dlg;
257 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
258 assert(uc->ctrl->generic.type == CTRL_RADIO);
259 assert(uc->buttons != NULL);
260 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
263 int dlg_radiobutton_get(union control *ctrl, void *dlg)
265 struct dlgparam *dp = (struct dlgparam *)dlg;
266 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
269 assert(uc->ctrl->generic.type == CTRL_RADIO);
270 assert(uc->buttons != NULL);
271 for (i = 0; i < uc->nbuttons; i++)
272 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
274 return 0; /* got to return something */
277 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
279 struct dlgparam *dp = (struct dlgparam *)dlg;
280 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
281 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
282 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
285 int dlg_checkbox_get(union control *ctrl, void *dlg)
287 struct dlgparam *dp = (struct dlgparam *)dlg;
288 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
289 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
290 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
293 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
295 struct dlgparam *dp = (struct dlgparam *)dlg;
296 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
297 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
298 assert(uc->entry != NULL);
300 * GTK 2 implements gtk_entry_set_text by means of two separate
301 * operations: first delete the previous text leaving the empty
302 * string, then insert the new text. This causes two calls to
303 * the "changed" signal.
305 * The first call to "changed", if allowed to proceed normally,
306 * will cause an EVENT_VALCHANGE event on the edit box, causing
307 * a call to dlg_editbox_get() which will read the empty string
308 * out of the GtkEntry - and promptly write it straight into
309 * the Config structure, which is precisely where our `text'
310 * pointer is probably pointing, so the second editing
311 * operation will insert that instead of the string we
312 * originally asked for.
314 * Hence, we must block our "changed" signal handler for the
315 * duration of this call to gtk_entry_set_text.
317 g_signal_handler_block(uc->entry, uc->entrysig);
318 gtk_entry_set_text(GTK_ENTRY(uc->entry), text);
319 g_signal_handler_unblock(uc->entry, uc->entrysig);
322 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
324 struct dlgparam *dp = (struct dlgparam *)dlg;
325 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
326 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
327 assert(uc->entry != NULL);
328 strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
330 buffer[length-1] = '\0';
333 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
335 GtkContainer *cont = GTK_CONTAINER(data);
336 /* gtk_container_remove will unref the widget for us; we need not. */
337 gtk_container_remove(cont, w);
340 /* The `listbox' functions can also apply to combo boxes. */
341 void dlg_listbox_clear(union control *ctrl, void *dlg)
343 struct dlgparam *dp = (struct dlgparam *)dlg;
344 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
346 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
347 uc->ctrl->generic.type == CTRL_LISTBOX);
348 assert(uc->menu != NULL || uc->list != NULL);
351 gtk_container_foreach(GTK_CONTAINER(uc->menu),
352 container_remove_and_destroy,
353 GTK_CONTAINER(uc->menu));
355 gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
359 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
361 struct dlgparam *dp = (struct dlgparam *)dlg;
362 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
364 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
365 uc->ctrl->generic.type == CTRL_LISTBOX);
366 assert(uc->menu != NULL || uc->list != NULL);
370 (GTK_CONTAINER(uc->menu),
371 g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
373 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
377 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
379 dlg_listbox_addwithid(ctrl, dlg, text, 0);
383 * Each listbox entry may have a numeric id associated with it.
384 * Note that some front ends only permit a string to be stored at
385 * each position, which means that _if_ you put two identical
386 * strings in any listbox then you MUST not assign them different
387 * IDs and expect to get meaningful results back.
389 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
390 char const *text, int id)
392 struct dlgparam *dp = (struct dlgparam *)dlg;
393 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
395 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
396 uc->ctrl->generic.type == CTRL_LISTBOX);
397 assert(uc->menu != NULL || uc->list != NULL);
399 dp->flags |= FLAG_UPDATING_COMBO_LIST;
403 * List item in a drop-down (but non-combo) list. Tabs are
404 * ignored; we just provide a standard menu item with the
407 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
409 gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
410 gtk_widget_show(menuitem);
412 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
413 GINT_TO_POINTER(id));
414 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
415 GTK_SIGNAL_FUNC(menuitem_activate), dp);
416 } else if (!uc->entry) {
418 * List item in a non-combo-box list box. We make all of
419 * these Columns containing GtkLabels. This allows us to do
420 * the nasty force_left hack irrespective of whether there
421 * are tabs in the thing.
423 GtkWidget *listitem = gtk_list_item_new();
424 GtkWidget *cols = columns_new(10);
428 /* Count the tabs in the text, and hence determine # of columns. */
430 for (i = 0; text[i]; i++)
435 (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
436 percents = snewn(ncols, gint);
437 percents[ncols-1] = 100;
438 for (i = 0; i < ncols-1; i++) {
439 percents[i] = uc->ctrl->listbox.percentages[i];
440 percents[ncols-1] -= percents[i];
442 columns_set_cols(COLUMNS(cols), ncols, percents);
445 for (i = 0; i < ncols; i++) {
446 int len = strcspn(text, "\t");
447 char *dup = dupprintf("%.*s", len, text);
452 label = gtk_label_new(dup);
455 columns_add(COLUMNS(cols), label, i, 1);
456 columns_force_left_align(COLUMNS(cols), label);
457 gtk_widget_show(label);
459 gtk_container_add(GTK_CONTAINER(listitem), cols);
460 gtk_widget_show(cols);
461 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
462 gtk_widget_show(listitem);
464 if (ctrl->listbox.multisel) {
465 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
466 GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj);
468 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
469 GTK_SIGNAL_FUNC(listitem_single_key), uc->adj);
471 gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event",
472 GTK_SIGNAL_FUNC(widget_focus), dp);
473 gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event",
474 GTK_SIGNAL_FUNC(listitem_button_press), dp);
475 gtk_signal_connect(GTK_OBJECT(listitem), "button_release_event",
476 GTK_SIGNAL_FUNC(listitem_button_release), dp);
477 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
478 GINT_TO_POINTER(id));
481 * List item in a combo-box list, which means the sensible
482 * thing to do is make it a perfectly normal label. Hence
483 * tabs are disregarded.
485 GtkWidget *listitem = gtk_list_item_new_with_label(text);
487 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
488 gtk_widget_show(listitem);
490 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
491 GINT_TO_POINTER(id));
494 dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
497 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
499 struct dlgparam *dp = (struct dlgparam *)dlg;
500 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
504 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
505 uc->ctrl->generic.type == CTRL_LISTBOX);
506 assert(uc->menu != NULL || uc->list != NULL);
508 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
510 item = GTK_OBJECT(g_list_nth_data(children, index));
511 g_list_free(children);
513 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), "user-data"));
516 /* dlg_listbox_index returns <0 if no single element is selected. */
517 int dlg_listbox_index(union control *ctrl, void *dlg)
519 struct dlgparam *dp = (struct dlgparam *)dlg;
520 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
522 GtkWidget *item, *activeitem;
526 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
527 uc->ctrl->generic.type == CTRL_LISTBOX);
528 assert(uc->menu != NULL || uc->list != NULL);
531 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
533 activeitem = NULL; /* unnecessarily placate gcc */
535 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
537 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
538 i++, children = children->next) {
539 if (uc->menu ? activeitem == item :
540 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
547 g_list_free(children);
548 return selected < 0 ? -1 : selected;
551 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
553 struct dlgparam *dp = (struct dlgparam *)dlg;
554 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
556 GtkWidget *item, *activeitem;
558 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
559 uc->ctrl->generic.type == CTRL_LISTBOX);
560 assert(uc->menu != NULL || uc->list != NULL);
562 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
564 item = GTK_WIDGET(g_list_nth_data(children, index));
565 g_list_free(children);
568 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
569 return item == activeitem;
571 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
575 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
577 struct dlgparam *dp = (struct dlgparam *)dlg;
578 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
580 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
581 uc->ctrl->generic.type == CTRL_LISTBOX);
582 assert(uc->optmenu != NULL || uc->list != NULL);
585 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
589 gdouble newtop, newbot;
591 gtk_list_select_item(GTK_LIST(uc->list), index);
594 * Scroll the list box if necessary to ensure the newly
595 * selected item is visible.
597 items = gtk_container_children(GTK_CONTAINER(uc->list));
598 nitems = g_list_length(items);
600 int modified = FALSE;
602 newtop = uc->adj->lower +
603 (uc->adj->upper - uc->adj->lower) * index / nitems;
604 newbot = uc->adj->lower +
605 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
606 if (uc->adj->value > newtop) {
608 uc->adj->value = newtop;
609 } else if (uc->adj->value < newbot - uc->adj->page_size) {
611 uc->adj->value = newbot - uc->adj->page_size;
614 gtk_adjustment_value_changed(uc->adj);
619 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
621 struct dlgparam *dp = (struct dlgparam *)dlg;
622 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
624 assert(uc->ctrl->generic.type == CTRL_TEXT);
625 assert(uc->text != NULL);
627 gtk_label_set_text(GTK_LABEL(uc->text), text);
630 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
632 struct dlgparam *dp = (struct dlgparam *)dlg;
633 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
635 switch (uc->ctrl->generic.type) {
637 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
638 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
641 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
642 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
645 gtk_label_set_text(GTK_LABEL(uc->label), text);
646 shortcut_highlight(uc->label, ctrl->radio.shortcut);
649 gtk_label_set_text(GTK_LABEL(uc->label), text);
650 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
652 case CTRL_FILESELECT:
653 gtk_label_set_text(GTK_LABEL(uc->label), text);
654 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
656 case CTRL_FONTSELECT:
657 gtk_label_set_text(GTK_LABEL(uc->label), text);
658 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
661 gtk_label_set_text(GTK_LABEL(uc->label), text);
662 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
665 assert(!"This shouldn't happen");
670 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
672 struct dlgparam *dp = (struct dlgparam *)dlg;
673 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
674 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
675 assert(uc->entry != NULL);
676 gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path);
679 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
681 struct dlgparam *dp = (struct dlgparam *)dlg;
682 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
683 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
684 assert(uc->entry != NULL);
685 strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
687 fn->path[lenof(fn->path)-1] = '\0';
690 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
692 struct dlgparam *dp = (struct dlgparam *)dlg;
693 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
694 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
695 assert(uc->entry != NULL);
696 gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name);
699 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
701 struct dlgparam *dp = (struct dlgparam *)dlg;
702 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
703 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
704 assert(uc->entry != NULL);
705 strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
707 fs->name[lenof(fs->name)-1] = '\0';
711 * Bracketing a large set of updates in these two functions will
712 * cause the front end (if possible) to delay updating the screen
713 * until it's all complete, thus avoiding flicker.
715 void dlg_update_start(union control *ctrl, void *dlg)
718 * Apparently we can't do this at all in GTK. GtkCList supports
719 * freeze and thaw, but not GtkList. Bah.
723 void dlg_update_done(union control *ctrl, void *dlg)
726 * Apparently we can't do this at all in GTK. GtkCList supports
727 * freeze and thaw, but not GtkList. Bah.
731 void dlg_set_focus(union control *ctrl, void *dlg)
733 struct dlgparam *dp = (struct dlgparam *)dlg;
734 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
736 switch (ctrl->generic.type) {
739 /* Check boxes and buttons get the focus _and_ get toggled. */
740 gtk_widget_grab_focus(uc->toplevel);
742 case CTRL_FILESELECT:
743 case CTRL_FONTSELECT:
745 /* Anything containing an edit box gets that focused. */
746 gtk_widget_grab_focus(uc->entry);
750 * Radio buttons: we find the currently selected button and
755 for (i = 0; i < ctrl->radio.nbuttons; i++)
756 if (gtk_toggle_button_get_active
757 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
758 gtk_widget_grab_focus(uc->buttons[i]);
764 * If the list is really an option menu, we focus it.
765 * Otherwise we tell it to focus one of its children, which
766 * appears to do the Right Thing.
769 gtk_widget_grab_focus(uc->optmenu);
771 assert(uc->list != NULL);
772 #if GTK_CHECK_VERSION(2,0,0)
773 gtk_widget_grab_focus(uc->list);
775 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
783 * During event processing, you might well want to give an error
784 * indication to the user. dlg_beep() is a quick and easy generic
785 * error; dlg_error() puts up a message-box or equivalent.
787 void dlg_beep(void *dlg)
792 static void errmsg_button_clicked(GtkButton *button, gpointer data)
794 gtk_widget_destroy(GTK_WIDGET(data));
797 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
799 gint x, y, w, h, dx, dy;
801 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
802 gtk_widget_size_request(GTK_WIDGET(child), &req);
804 gdk_window_get_origin(GTK_WIDGET(parent)->window, &x, &y);
805 gdk_window_get_size(GTK_WIDGET(parent)->window, &w, &h);
808 * One corner of the transient will be offset inwards, by 1/4
809 * of the parent window's size, from the corresponding corner
810 * of the parent window. The corner will be chosen so as to
811 * place the transient closer to the centre of the screen; this
812 * should avoid transients going off the edge of the screen on
815 if (x + w/2 < gdk_screen_width() / 2)
816 dx = x + w/4; /* work from left edges */
818 dx = x + 3*w/4 - req.width; /* work from right edges */
819 if (y + h/2 < gdk_screen_height() / 2)
820 dy = y + h/4; /* work from top edges */
822 dy = y + 3*h/4 - req.height; /* work from bottom edges */
823 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
826 void dlg_error_msg(void *dlg, char *msg)
828 struct dlgparam *dp = (struct dlgparam *)dlg;
829 GtkWidget *window, *hbox, *text, *ok;
831 window = gtk_dialog_new();
832 text = gtk_label_new(msg);
833 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
834 hbox = gtk_hbox_new(FALSE, 0);
835 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
836 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
837 hbox, FALSE, FALSE, 20);
838 gtk_widget_show(text);
839 gtk_widget_show(hbox);
840 gtk_window_set_title(GTK_WINDOW(window), "Error");
841 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
842 ok = gtk_button_new_with_label("OK");
843 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
844 ok, FALSE, FALSE, 0);
846 GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
847 gtk_window_set_default(GTK_WINDOW(window), ok);
848 gtk_signal_connect(GTK_OBJECT(ok), "clicked",
849 GTK_SIGNAL_FUNC(errmsg_button_clicked), window);
850 gtk_signal_connect(GTK_OBJECT(window), "destroy",
851 GTK_SIGNAL_FUNC(window_destroy), NULL);
852 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
853 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
854 set_transient_window_pos(dp->window, window);
855 gtk_widget_show(window);
860 * This function signals to the front end that the dialog's
861 * processing is completed, and passes an integer value (typically
864 void dlg_end(void *dlg, int value)
866 struct dlgparam *dp = (struct dlgparam *)dlg;
868 gtk_widget_destroy(dp->window);
871 void dlg_refresh(union control *ctrl, void *dlg)
873 struct dlgparam *dp = (struct dlgparam *)dlg;
877 if (ctrl->generic.handler != NULL)
878 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
882 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
883 assert(uc->ctrl != NULL);
884 if (uc->ctrl->generic.handler != NULL)
885 uc->ctrl->generic.handler(uc->ctrl, dp,
886 dp->data, EVENT_REFRESH);
891 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
893 struct dlgparam *dp = (struct dlgparam *)dlg;
894 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
897 GtkWidget *coloursel =
898 gtk_color_selection_dialog_new("Select a colour");
899 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
901 dp->coloursel_result.ok = FALSE;
903 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
904 #if GTK_CHECK_VERSION(2,0,0)
905 gtk_color_selection_set_has_opacity_control(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
907 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
909 cvals[0] = r / 255.0;
910 cvals[1] = g / 255.0;
911 cvals[2] = b / 255.0;
912 cvals[3] = 1.0; /* fully opaque! */
913 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals);
915 gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data",
916 (gpointer)coloursel);
917 gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data",
918 (gpointer)coloursel);
919 gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc);
920 gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked",
921 GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp);
922 gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked",
923 GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp);
924 gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked",
925 GTK_SIGNAL_FUNC(gtk_widget_destroy),
926 (gpointer)coloursel);
927 gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked",
928 GTK_SIGNAL_FUNC(gtk_widget_destroy),
929 (gpointer)coloursel);
930 gtk_widget_show(coloursel);
933 int dlg_coloursel_results(union control *ctrl, void *dlg,
934 int *r, int *g, int *b)
936 struct dlgparam *dp = (struct dlgparam *)dlg;
937 if (dp->coloursel_result.ok) {
938 *r = dp->coloursel_result.r;
939 *g = dp->coloursel_result.g;
940 *b = dp->coloursel_result.b;
946 /* ----------------------------------------------------------------------
947 * Signal handlers while the dialog box is active.
950 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
953 struct dlgparam *dp = (struct dlgparam *)data;
954 struct uctrl *uc = dlg_find_bywidget(dp, widget);
955 union control *focus;
962 if (focus != dp->currfocus) {
963 dp->lastfocus = dp->currfocus;
964 dp->currfocus = focus;
970 static void button_clicked(GtkButton *button, gpointer data)
972 struct dlgparam *dp = (struct dlgparam *)data;
973 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
974 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
977 static void button_toggled(GtkToggleButton *tb, gpointer data)
979 struct dlgparam *dp = (struct dlgparam *)data;
980 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
981 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
984 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
988 * GtkEntry has a nasty habit of eating the Return key, which
989 * is unhelpful since it doesn't actually _do_ anything with it
990 * (it calls gtk_widget_activate, but our edit boxes never need
991 * activating). So I catch Return before GtkEntry sees it, and
992 * pass it straight on to the parent widget. Effect: hitting
993 * Return in an edit box will now activate the default button
994 * in the dialog just like it will everywhere else.
996 if (event->keyval == GDK_Return && widget->parent != NULL) {
998 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
999 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
1000 event, &return_val);
1006 static void editbox_changed(GtkEditable *ed, gpointer data)
1008 struct dlgparam *dp = (struct dlgparam *)data;
1009 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1010 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1011 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1015 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1018 struct dlgparam *dp = (struct dlgparam *)data;
1019 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1020 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1024 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1025 gpointer data, int multiple)
1027 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1029 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1030 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1031 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1032 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1034 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1035 * in a list box. So, if the list box is single-selection:
1037 * - if the list item in question isn't already selected,
1038 * we simply select it.
1039 * - otherwise, we find the next one (or next
1040 * however-far-away) in whichever direction we're going,
1042 * + in this case, we must also fiddle with the
1043 * scrollbar to ensure the newly selected item is
1046 * If it's multiple-selection, we do all of the above
1047 * except actually selecting anything, so we move the focus
1048 * and fiddle the scrollbar to follow it.
1050 GtkWidget *list = item->parent;
1052 gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event");
1055 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1056 gtk_list_select_child(GTK_LIST(list), item);
1059 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1060 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1063 (event->keyval==GDK_Page_Down ||
1064 event->keyval==GDK_KP_Page_Down ||
1065 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1068 GList *children, *chead;
1070 chead = children = gtk_container_children(GTK_CONTAINER(list));
1072 n = g_list_length(children);
1076 * Figure out how many list items to a screenful,
1077 * and adjust the step appropriately.
1079 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1080 step--; /* go by one less than that */
1084 while (children != NULL) {
1085 if (item == children->data)
1087 children = children->next;
1092 if (direction < 0 && i > 0)
1093 children = children->prev, i--;
1094 else if (direction > 0 && i < n-1)
1095 children = children->next, i++;
1099 if (children && children->data) {
1101 gtk_list_select_child(GTK_LIST(list),
1102 GTK_WIDGET(children->data));
1103 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1104 gtk_adjustment_clamp_page
1106 adj->lower + (adj->upper-adj->lower) * i / n,
1107 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1118 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1121 return listitem_key(item, event, data, FALSE);
1124 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1127 return listitem_key(item, event, data, TRUE);
1130 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1133 struct dlgparam *dp = (struct dlgparam *)data;
1134 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1135 switch (event->type) {
1137 case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1138 case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1139 case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1144 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1147 struct dlgparam *dp = (struct dlgparam *)data;
1148 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1149 if (uc->nclicks>1) {
1150 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1156 static void list_selchange(GtkList *list, gpointer data)
1158 struct dlgparam *dp = (struct dlgparam *)data;
1159 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1161 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1164 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1166 struct dlgparam *dp = (struct dlgparam *)data;
1167 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1168 gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data");
1169 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1170 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1173 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1175 int index = dlg_listbox_index(uc->ctrl, dp);
1176 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1180 (index == 0 && direction < 0) ||
1181 (index == g_list_length(children)-1 && direction > 0)) {
1186 child = g_list_nth_data(children, index);
1187 gtk_widget_ref(child);
1188 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1189 g_list_free(children);
1192 children = g_list_append(children, child);
1193 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1194 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1195 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1198 static void draglist_up(GtkButton *button, gpointer data)
1200 struct dlgparam *dp = (struct dlgparam *)data;
1201 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1202 draglist_move(dp, uc, -1);
1205 static void draglist_down(GtkButton *button, gpointer data)
1207 struct dlgparam *dp = (struct dlgparam *)data;
1208 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1209 draglist_move(dp, uc, +1);
1212 static void filesel_ok(GtkButton *button, gpointer data)
1214 /* struct dlgparam *dp = (struct dlgparam *)data; */
1215 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1216 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data");
1217 const char *name = gtk_file_selection_get_filename
1218 (GTK_FILE_SELECTION(filesel));
1219 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1222 static void fontsel_ok(GtkButton *button, gpointer data)
1224 /* struct dlgparam *dp = (struct dlgparam *)data; */
1225 unifontsel *fontsel = (unifontsel *)gtk_object_get_data
1226 (GTK_OBJECT(button), "user-data");
1227 struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1228 char *name = unifontsel_get_name(fontsel);
1229 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1233 static void coloursel_ok(GtkButton *button, gpointer data)
1235 struct dlgparam *dp = (struct dlgparam *)data;
1236 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1237 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1239 gtk_color_selection_get_color
1240 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel),
1242 dp->coloursel_result.r = (int) (255 * cvals[0]);
1243 dp->coloursel_result.g = (int) (255 * cvals[1]);
1244 dp->coloursel_result.b = (int) (255 * cvals[2]);
1245 dp->coloursel_result.ok = TRUE;
1246 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1249 static void coloursel_cancel(GtkButton *button, gpointer data)
1251 struct dlgparam *dp = (struct dlgparam *)data;
1252 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1253 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1254 dp->coloursel_result.ok = FALSE;
1255 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1258 static void filefont_clicked(GtkButton *button, gpointer data)
1260 struct dlgparam *dp = (struct dlgparam *)data;
1261 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1263 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1264 GtkWidget *filesel =
1265 gtk_file_selection_new(uc->ctrl->fileselect.title);
1266 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1268 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1270 gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc);
1272 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1273 GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp);
1274 gtk_signal_connect_object
1275 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1276 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1277 gtk_signal_connect_object
1278 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1279 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1280 gtk_widget_show(filesel);
1283 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1284 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1285 unifontsel *fontsel = unifontsel_new("Select a font");
1287 gtk_window_set_modal(fontsel->window, TRUE);
1288 unifontsel_set_name(fontsel, fontname);
1290 gtk_object_set_data(GTK_OBJECT(fontsel->ok_button),
1291 "user-data", (gpointer)fontsel);
1292 fontsel->user_data = uc;
1293 gtk_signal_connect(GTK_OBJECT(fontsel->ok_button), "clicked",
1294 GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1295 gtk_signal_connect_object(GTK_OBJECT(fontsel->ok_button), "clicked",
1296 GTK_SIGNAL_FUNC(unifontsel_destroy),
1298 gtk_signal_connect_object(GTK_OBJECT(fontsel->cancel_button),"clicked",
1299 GTK_SIGNAL_FUNC(unifontsel_destroy),
1302 gtk_widget_show(GTK_WIDGET(fontsel->window));
1306 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1309 struct dlgparam *dp = (struct dlgparam *)data;
1310 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1312 gtk_widget_set_usize(uc->text, alloc->width, -1);
1313 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1314 gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig);
1317 /* ----------------------------------------------------------------------
1318 * This function does the main layout work: it reads a controlset,
1319 * it creates the relevant GTK controls, and returns a GtkWidget
1320 * containing the result. (This widget might be a title of some
1321 * sort, it might be a Columns containing many controls, or it
1322 * might be a GtkFrame containing a Columns; whatever it is, it's
1323 * definitely a GtkWidget and should probably be added to a
1326 * `listitemheight' is used to calculate a usize for list boxes: it
1327 * should be the height from the size request of a GtkListItem.
1329 * `win' is required for setting the default button. If it is
1330 * non-NULL, all buttons created will be default-capable (so they
1331 * have extra space round them for the default highlight).
1333 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1334 struct controlset *s, int listitemheight,
1341 if (!s->boxname && s->boxtitle) {
1342 /* This controlset is a panel title. */
1343 return gtk_label_new(s->boxtitle);
1347 * Otherwise, we expect to be laying out actual controls, so
1348 * we'll start by creating a Columns for the purpose.
1350 cols = COLUMNS(columns_new(4));
1351 ret = GTK_WIDGET(cols);
1352 gtk_widget_show(ret);
1355 * Create a containing frame if we have a box name.
1358 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1359 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1360 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1361 gtk_widget_show(ret);
1365 * Now iterate through the controls themselves, create them,
1366 * and add them to the Columns.
1368 for (i = 0; i < s->ncontrols; i++) {
1369 union control *ctrl = s->ctrls[i];
1372 GtkWidget *w = NULL;
1374 switch (ctrl->generic.type) {
1377 static const int simplecols[1] = { 100 };
1378 columns_set_cols(cols, ctrl->columns.ncols,
1379 (ctrl->columns.percentages ?
1380 ctrl->columns.percentages : simplecols));
1382 continue; /* no actual control created */
1385 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1387 columns_taborder_last(cols, uc->toplevel);
1389 continue; /* no actual control created */
1392 uc = snew(struct uctrl);
1394 uc->privdata = NULL;
1395 uc->privdata_needs_free = FALSE;
1397 uc->entry = uc->list = uc->menu = NULL;
1398 uc->button = uc->optmenu = uc->text = NULL;
1402 switch (ctrl->generic.type) {
1404 w = gtk_button_new_with_label(ctrl->generic.label);
1406 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
1407 if (ctrl->button.isdefault)
1408 gtk_window_set_default(win, w);
1409 if (ctrl->button.iscancel)
1410 dp->cancelbutton = w;
1412 gtk_signal_connect(GTK_OBJECT(w), "clicked",
1413 GTK_SIGNAL_FUNC(button_clicked), dp);
1414 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1415 GTK_SIGNAL_FUNC(widget_focus), dp);
1416 shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut,
1417 SHORTCUT_UCTRL, uc);
1420 w = gtk_check_button_new_with_label(ctrl->generic.label);
1421 gtk_signal_connect(GTK_OBJECT(w), "toggled",
1422 GTK_SIGNAL_FUNC(button_toggled), dp);
1423 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1424 GTK_SIGNAL_FUNC(widget_focus), dp);
1425 shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut,
1426 SHORTCUT_UCTRL, uc);
1431 * Radio buttons get to go inside their own Columns, no
1435 gint i, *percentages;
1439 if (ctrl->generic.label) {
1440 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1441 columns_add(COLUMNS(w), label, 0, 1);
1442 columns_force_left_align(COLUMNS(w), label);
1443 gtk_widget_show(label);
1444 shortcut_add(scs, label, ctrl->radio.shortcut,
1445 SHORTCUT_UCTRL, uc);
1448 percentages = g_new(gint, ctrl->radio.ncolumns);
1449 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1451 ((100 * (i+1) / ctrl->radio.ncolumns) -
1452 100 * i / ctrl->radio.ncolumns);
1454 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1456 g_free(percentages);
1459 uc->nbuttons = ctrl->radio.nbuttons;
1460 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1462 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1466 b = (gtk_radio_button_new_with_label
1467 (group, ctrl->radio.buttons[i]));
1469 group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
1470 colstart = i % ctrl->radio.ncolumns;
1471 columns_add(COLUMNS(w), b, colstart,
1472 (i == ctrl->radio.nbuttons-1 ?
1473 ctrl->radio.ncolumns - colstart : 1));
1474 columns_force_left_align(COLUMNS(w), b);
1476 gtk_signal_connect(GTK_OBJECT(b), "toggled",
1477 GTK_SIGNAL_FUNC(button_toggled), dp);
1478 gtk_signal_connect(GTK_OBJECT(b), "focus_in_event",
1479 GTK_SIGNAL_FUNC(widget_focus), dp);
1480 if (ctrl->radio.shortcuts) {
1481 shortcut_add(scs, GTK_BIN(b)->child,
1482 ctrl->radio.shortcuts[i],
1483 SHORTCUT_UCTRL, uc);
1492 if (ctrl->editbox.has_list) {
1493 w = gtk_combo_new();
1494 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1495 uc->entry = GTK_COMBO(w)->entry;
1496 uc->list = GTK_COMBO(w)->list;
1498 w = gtk_entry_new();
1499 if (ctrl->editbox.password)
1500 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
1504 gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
1505 GTK_SIGNAL_FUNC(editbox_changed), dp);
1506 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
1507 GTK_SIGNAL_FUNC(editbox_key), dp);
1508 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
1509 GTK_SIGNAL_FUNC(widget_focus), dp);
1511 * Edit boxes, for some strange reason, have a minimum
1512 * width of 150 in GTK 1.2. We don't want this - we'd
1513 * rather the edit boxes acquired their natural width
1514 * from the column layout of the rest of the box.
1516 * Also, while we're here, we'll squirrel away the
1517 * edit box height so we can use that to centre its
1518 * label vertically beside it.
1520 gtk_widget_size_request(w, &req);
1521 gtk_widget_set_usize(w, 10, req.height);
1523 if (ctrl->generic.label) {
1524 GtkWidget *label, *container;
1526 label = gtk_label_new(ctrl->generic.label);
1528 shortcut_add(scs, label, ctrl->editbox.shortcut,
1529 SHORTCUT_FOCUS, uc->entry);
1531 container = columns_new(4);
1532 if (ctrl->editbox.percentwidth == 100) {
1533 columns_add(COLUMNS(container), label, 0, 1);
1534 columns_force_left_align(COLUMNS(container), label);
1535 columns_add(COLUMNS(container), w, 0, 1);
1537 gint percentages[2];
1538 percentages[1] = ctrl->editbox.percentwidth;
1539 percentages[0] = 100 - ctrl->editbox.percentwidth;
1540 columns_set_cols(COLUMNS(container), 2, percentages);
1541 columns_add(COLUMNS(container), label, 0, 1);
1542 columns_force_left_align(COLUMNS(container), label);
1543 columns_add(COLUMNS(container), w, 1, 1);
1544 /* Centre the label vertically. */
1545 gtk_widget_set_usize(label, -1, req.height);
1546 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1548 gtk_widget_show(label);
1554 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event",
1555 GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1558 case CTRL_FILESELECT:
1559 case CTRL_FONTSELECT:
1564 (ctrl->generic.type == CTRL_FILESELECT ?
1565 "Browse..." : "Change...");
1567 gint percentages[] = { 75, 25 };
1569 columns_set_cols(COLUMNS(w), 2, percentages);
1571 if (ctrl->generic.label) {
1572 ww = gtk_label_new(ctrl->generic.label);
1573 columns_add(COLUMNS(w), ww, 0, 2);
1574 columns_force_left_align(COLUMNS(w), ww);
1575 gtk_widget_show(ww);
1576 shortcut_add(scs, ww,
1577 (ctrl->generic.type == CTRL_FILESELECT ?
1578 ctrl->fileselect.shortcut :
1579 ctrl->fontselect.shortcut),
1580 SHORTCUT_UCTRL, uc);
1584 uc->entry = ww = gtk_entry_new();
1585 gtk_widget_size_request(ww, &req);
1586 gtk_widget_set_usize(ww, 10, req.height);
1587 columns_add(COLUMNS(w), ww, 0, 1);
1588 gtk_widget_show(ww);
1590 uc->button = ww = gtk_button_new_with_label(browsebtn);
1591 columns_add(COLUMNS(w), ww, 1, 1);
1592 gtk_widget_show(ww);
1594 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
1595 GTK_SIGNAL_FUNC(editbox_key), dp);
1597 gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
1598 GTK_SIGNAL_FUNC(editbox_changed), dp);
1599 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
1600 GTK_SIGNAL_FUNC(widget_focus), dp);
1601 gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
1602 GTK_SIGNAL_FUNC(widget_focus), dp);
1603 gtk_signal_connect(GTK_OBJECT(ww), "clicked",
1604 GTK_SIGNAL_FUNC(filefont_clicked), dp);
1608 if (ctrl->listbox.height == 0) {
1609 uc->optmenu = w = gtk_option_menu_new();
1610 uc->menu = gtk_menu_new();
1611 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
1612 gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data",
1613 (gpointer)uc->optmenu);
1614 gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event",
1615 GTK_SIGNAL_FUNC(widget_focus), dp);
1617 uc->list = gtk_list_new();
1618 if (ctrl->listbox.multisel == 2) {
1619 gtk_list_set_selection_mode(GTK_LIST(uc->list),
1620 GTK_SELECTION_EXTENDED);
1621 } else if (ctrl->listbox.multisel == 1) {
1622 gtk_list_set_selection_mode(GTK_LIST(uc->list),
1623 GTK_SELECTION_MULTIPLE);
1625 gtk_list_set_selection_mode(GTK_LIST(uc->list),
1626 GTK_SELECTION_SINGLE);
1628 w = gtk_scrolled_window_new(NULL, NULL);
1629 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
1631 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
1633 GTK_POLICY_AUTOMATIC);
1634 uc->adj = gtk_scrolled_window_get_vadjustment
1635 (GTK_SCROLLED_WINDOW(w));
1637 gtk_widget_show(uc->list);
1638 gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed",
1639 GTK_SIGNAL_FUNC(list_selchange), dp);
1640 gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event",
1641 GTK_SIGNAL_FUNC(widget_focus), dp);
1644 * Adjust the height of the scrolled window to the
1645 * minimum given by the height parameter.
1647 * This piece of guesswork is a horrid hack based
1648 * on looking inside the GTK 1.2 sources
1649 * (specifically gtkviewport.c, which appears to be
1650 * the widget which provides the border around the
1651 * scrolling area). Anyone lets me know how I can
1652 * do this in a way which isn't at risk from GTK
1653 * upgrades, I'd be grateful.
1657 #if GTK_CHECK_VERSION(2,0,0)
1658 edge = GTK_WIDGET(uc->list)->style->ythickness;
1660 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
1662 gtk_widget_set_usize(w, 10,
1663 2*edge + (ctrl->listbox.height *
1667 if (ctrl->listbox.draglist) {
1669 * GTK doesn't appear to make it easy to
1670 * implement a proper draggable list; so
1671 * instead I'm just going to have to put an Up
1672 * and a Down button to the right of the actual
1673 * list box. Ah well.
1675 GtkWidget *cols, *button;
1676 static const gint percentages[2] = { 80, 20 };
1678 cols = columns_new(4);
1679 columns_set_cols(COLUMNS(cols), 2, percentages);
1680 columns_add(COLUMNS(cols), w, 0, 1);
1682 button = gtk_button_new_with_label("Up");
1683 columns_add(COLUMNS(cols), button, 1, 1);
1684 gtk_widget_show(button);
1685 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1686 GTK_SIGNAL_FUNC(draglist_up), dp);
1687 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
1688 GTK_SIGNAL_FUNC(widget_focus), dp);
1689 button = gtk_button_new_with_label("Down");
1690 columns_add(COLUMNS(cols), button, 1, 1);
1691 gtk_widget_show(button);
1692 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1693 GTK_SIGNAL_FUNC(draglist_down), dp);
1694 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
1695 GTK_SIGNAL_FUNC(widget_focus), dp);
1701 if (ctrl->generic.label) {
1702 GtkWidget *label, *container;
1704 label = gtk_label_new(ctrl->generic.label);
1706 container = columns_new(4);
1707 if (ctrl->listbox.percentwidth == 100) {
1708 columns_add(COLUMNS(container), label, 0, 1);
1709 columns_force_left_align(COLUMNS(container), label);
1710 columns_add(COLUMNS(container), w, 0, 1);
1712 gint percentages[2];
1713 percentages[1] = ctrl->listbox.percentwidth;
1714 percentages[0] = 100 - ctrl->listbox.percentwidth;
1715 columns_set_cols(COLUMNS(container), 2, percentages);
1716 columns_add(COLUMNS(container), label, 0, 1);
1717 columns_force_left_align(COLUMNS(container), label);
1718 columns_add(COLUMNS(container), w, 1, 1);
1720 gtk_widget_show(label);
1722 shortcut_add(scs, label, ctrl->listbox.shortcut,
1723 SHORTCUT_UCTRL, uc);
1730 * Wrapping text widgets don't sit well with the GTK
1731 * layout model, in which widgets state a minimum size
1732 * and the whole window then adjusts to the smallest
1733 * size it can sensibly take given its contents. A
1734 * wrapping text widget _has_ no clear minimum size;
1735 * instead it has a range of possibilities. It can be
1736 * one line deep but 2000 wide, or two lines deep and
1737 * 1000 pixels, or three by 867, or four by 500 and so
1738 * on. It can be as short as you like provided you
1739 * don't mind it being wide, or as narrow as you like
1740 * provided you don't mind it being tall.
1742 * Therefore, it fits very badly into the layout model.
1743 * Hence the only thing to do is pick a width and let
1744 * it choose its own number of lines. To do this I'm
1745 * going to cheat a little. All new wrapping text
1746 * widgets will be created with a minimal text content
1747 * "X"; then, after the rest of the dialog box is set
1748 * up and its size calculated, the text widgets will be
1749 * told their width and given their real text, which
1750 * will cause the size to be recomputed in the y
1751 * direction (because many of them will expand to more
1754 uc->text = w = gtk_label_new("X");
1755 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
1756 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
1758 gtk_signal_connect(GTK_OBJECT(w), "size-allocate",
1759 GTK_SIGNAL_FUNC(label_sizealloc), dp);
1765 columns_add(cols, w,
1766 COLUMN_START(ctrl->generic.column),
1767 COLUMN_SPAN(ctrl->generic.column));
1769 columns_force_left_align(cols, w);
1773 dlg_add_uctrl(dp, uc);
1780 struct dlgparam *dp;
1781 GtkNotebook *panels;
1783 #if !GTK_CHECK_VERSION(2,0,0)
1784 GtkWidget *treeitem;
1786 struct Shortcuts shortcuts;
1789 #if GTK_CHECK_VERSION(2,0,0)
1790 static void treeselection_changed(GtkTreeSelection *treeselection,
1793 struct selparam *sps = (struct selparam *)data, *sp;
1794 GtkTreeModel *treemodel;
1795 GtkTreeIter treeiter;
1799 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
1802 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
1805 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
1806 gtk_notebook_set_page(sp->panels, page_num);
1808 dlg_refresh(NULL, sp->dp);
1810 sp->dp->shortcuts = &sp->shortcuts;
1813 static void treeitem_sel(GtkItem *item, gpointer data)
1815 struct selparam *sp = (struct selparam *)data;
1818 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
1819 gtk_notebook_set_page(sp->panels, page_num);
1821 dlg_refresh(NULL, sp->dp);
1823 sp->dp->shortcuts = &sp->shortcuts;
1824 sp->dp->currtreeitem = sp->treeitem;
1828 static void window_destroy(GtkWidget *widget, gpointer data)
1833 #if !GTK_CHECK_VERSION(2,0,0)
1834 static int tree_grab_focus(struct dlgparam *dp)
1839 * See if any of the treeitems has the focus.
1842 for (i = 0; i < dp->ntreeitems; i++)
1843 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
1851 gtk_widget_grab_focus(dp->currtreeitem);
1856 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
1859 struct dlgparam *dp = (struct dlgparam *)data;
1861 gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus");
1863 * If there's a focused treeitem, we return FALSE to cause the
1864 * focus to move on to some totally other control. If not, we
1865 * focus the selected one.
1867 return tree_grab_focus(dp);
1871 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
1873 struct dlgparam *dp = (struct dlgparam *)data;
1875 if (event->keyval == GDK_Escape && dp->cancelbutton) {
1876 gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked");
1880 if ((event->state & GDK_MOD1_MASK) &&
1881 (unsigned char)event->string[0] > 0 &&
1882 (unsigned char)event->string[0] <= 127) {
1883 int schr = (unsigned char)event->string[0];
1884 struct Shortcut *sc = &dp->shortcuts->sc[schr];
1886 switch (sc->action) {
1888 #if GTK_CHECK_VERSION(2,0,0)
1889 gtk_widget_grab_focus(sc->widget);
1891 tree_grab_focus(dp);
1894 case SHORTCUT_FOCUS:
1895 gtk_widget_grab_focus(sc->widget);
1897 case SHORTCUT_UCTRL:
1899 * We must do something sensible with a uctrl.
1900 * Precisely what this is depends on the type of
1903 switch (sc->uc->ctrl->generic.type) {
1906 /* Check boxes and buttons get the focus _and_ get toggled. */
1907 gtk_widget_grab_focus(sc->uc->toplevel);
1908 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel),
1911 case CTRL_FILESELECT:
1912 case CTRL_FONTSELECT:
1913 /* File/font selectors have their buttons pressed (ooer),
1914 * and focus transferred to the edit box. */
1915 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button),
1917 gtk_widget_grab_focus(sc->uc->entry);
1921 * Radio buttons are fun, because they have
1922 * multiple shortcuts. We must find whether the
1923 * activated shortcut is the shortcut for the whole
1924 * group, or for a particular button. In the former
1925 * case, we find the currently selected button and
1926 * focus it; in the latter, we focus-and-click the
1927 * button whose shortcut was pressed.
1929 if (schr == sc->uc->ctrl->radio.shortcut) {
1931 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
1932 if (gtk_toggle_button_get_active
1933 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
1934 gtk_widget_grab_focus(sc->uc->buttons[i]);
1936 } else if (sc->uc->ctrl->radio.shortcuts) {
1938 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
1939 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
1940 gtk_widget_grab_focus(sc->uc->buttons[i]);
1941 gtk_signal_emit_by_name
1942 (GTK_OBJECT(sc->uc->buttons[i]), "clicked");
1948 * If the list is really an option menu, we focus
1949 * and click it. Otherwise we tell it to focus one
1950 * of its children, which appears to do the Right
1953 if (sc->uc->optmenu) {
1957 gtk_widget_grab_focus(sc->uc->optmenu);
1958 /* Option menus don't work using the "clicked" signal.
1959 * We need to manufacture a button press event :-/ */
1960 bev.type = GDK_BUTTON_PRESS;
1962 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu),
1963 "button_press_event",
1966 assert(sc->uc->list != NULL);
1968 #if GTK_CHECK_VERSION(2,0,0)
1969 gtk_widget_grab_focus(sc->uc->list);
1971 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
1972 GTK_DIR_TAB_FORWARD);
1984 #if !GTK_CHECK_VERSION(2,0,0)
1985 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
1987 struct dlgparam *dp = (struct dlgparam *)data;
1989 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1990 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
1992 for (i = 0; i < dp->ntreeitems; i++)
1993 if (widget == dp->treeitems[i])
1995 if (i < dp->ntreeitems) {
1996 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2003 if (i < 0 || i >= dp->ntreeitems)
2004 break; /* nothing in that dir to select */
2006 * Determine if this tree item is visible.
2009 GtkWidget *w = dp->treeitems[i];
2011 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2012 if (!GTK_WIDGET_VISIBLE(w)) {
2019 j = i; /* got one */
2025 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2028 gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle");
2029 gtk_widget_grab_focus(dp->treeitems[j]);
2035 * It's nice for Left and Right to expand and collapse tree
2038 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2039 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2041 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2044 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2045 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2047 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2055 static void shortcut_highlight(GtkWidget *labelw, int chr)
2057 GtkLabel *label = GTK_LABEL(labelw);
2058 gchar *currstr, *pattern;
2061 gtk_label_get(label, &currstr);
2062 for (i = 0; currstr[i]; i++)
2063 if (tolower((unsigned char)currstr[i]) == chr) {
2066 pattern = dupprintf("%*s_", i, "");
2068 gtk_widget_size_request(GTK_WIDGET(label), &req);
2069 gtk_label_set_pattern(label, pattern);
2070 gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
2077 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2078 int chr, int action, void *ptr)
2080 if (chr == NO_SHORTCUT)
2083 chr = tolower((unsigned char)chr);
2085 assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2087 scs->sc[chr].action = action;
2089 if (action == SHORTCUT_FOCUS) {
2090 scs->sc[chr].uc = NULL;
2091 scs->sc[chr].widget = (GtkWidget *)ptr;
2093 scs->sc[chr].widget = NULL;
2094 scs->sc[chr].uc = (struct uctrl *)ptr;
2097 shortcut_highlight(labelw, chr);
2100 int get_listitemheight(void)
2102 GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2104 gtk_widget_size_request(listitem, &req);
2105 gtk_object_sink(GTK_OBJECT(listitem));
2109 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2111 #if !GTK_CHECK_VERSION(2,0,0)
2114 * In GTK 1, laying out the buttons at the bottom of the
2115 * configuration box is nice and easy, because a GtkDialog's
2116 * action_area is a GtkHBox which stretches to cover the full
2117 * width of the dialog. So we just put our Columns widget
2118 * straight into that hbox, and it ends up just where we want
2121 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
2126 * In GTK 2, the action area is now a GtkHButtonBox and its
2127 * layout behaviour seems to be different: it doesn't stretch
2128 * to cover the full width of the window, but instead finds its
2129 * own preferred width and right-aligns that within the window.
2130 * This isn't what we want, because we have both left-aligned
2131 * and right-aligned buttons coming out of the above call to
2132 * layout_ctrls(), and right-aligning the whole thing will
2133 * result in the former being centred and looking weird.
2135 * So instead we abandon the dialog's action area completely:
2136 * we gtk_widget_hide() it in the below code, and we also call
2137 * gtk_dialog_set_has_separator() to remove the separator above
2138 * it. We then insert our own action area into the end of the
2139 * dialog's main vbox, and add our own separator above that.
2141 * (Ideally, if we were a native GTK app, we would use the
2142 * GtkHButtonBox's _own_ innate ability to support one set of
2143 * buttons being right-aligned and one left-aligned. But to do
2144 * that here, we would have to either (a) pick apart our cross-
2145 * platform layout structures and treat them specially for this
2146 * particular set of controls, which would be painful, or else
2147 * (b) develop a special and simpler cross-platform
2148 * representation for these particular controls, and introduce
2149 * special-case code into all the _other_ platforms to handle
2150 * it. Neither appeals. Therefore, I regretfully discard the
2151 * GTKHButtonBox and go it alone.)
2155 align = gtk_alignment_new(0, 0, 1, 1);
2156 gtk_container_add(GTK_CONTAINER(align), w);
2158 * The purpose of this GtkAlignment is to provide padding
2159 * around the buttons. The padding we use is twice the padding
2160 * used in our GtkColumns, because we nest two GtkColumns most
2161 * of the time (one separating the tree view from the main
2162 * controls, and another for the main controls themselves).
2164 #if GTK_CHECK_VERSION(2,4,0)
2165 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2167 gtk_widget_show(align);
2168 gtk_box_pack_end(GTK_BOX(dlg->vbox), align, FALSE, TRUE, 0);
2169 w = gtk_hseparator_new();
2170 gtk_box_pack_end(GTK_BOX(dlg->vbox), w, FALSE, TRUE, 0);
2172 gtk_widget_hide(dlg->action_area);
2173 gtk_dialog_set_has_separator(dlg, FALSE);
2177 int do_config_box(const char *title, Config *cfg, int midsession,
2180 GtkWidget *window, *hbox, *vbox, *cols, *label,
2181 *tree, *treescroll, *panels, *panelvbox;
2182 int index, level, listitemheight;
2183 struct controlbox *ctrlbox;
2185 #if GTK_CHECK_VERSION(2,0,0)
2186 GtkTreeStore *treestore;
2187 GtkCellRenderer *treerenderer;
2188 GtkTreeViewColumn *treecolumn;
2189 GtkTreeSelection *treeselection;
2190 GtkTreeIter treeiterlevels[8];
2192 GtkTreeItem *treeitemlevels[8];
2193 GtkTree *treelevels[8];
2196 struct Shortcuts scs;
2198 struct selparam *selparams = NULL;
2199 int nselparams = 0, selparamsize = 0;
2203 listitemheight = get_listitemheight();
2205 for (index = 0; index < lenof(scs.sc); index++) {
2206 scs.sc[index].action = SHORTCUT_EMPTY;
2209 window = gtk_dialog_new();
2211 ctrlbox = ctrl_new_box();
2212 setup_config_box(ctrlbox, midsession, cfg->protocol, protcfginfo);
2213 unix_setup_config_box(ctrlbox, midsession, cfg->protocol);
2214 gtk_setup_config_box(ctrlbox, midsession, window);
2216 gtk_window_set_title(GTK_WINDOW(window), title);
2217 hbox = gtk_hbox_new(FALSE, 4);
2218 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0);
2219 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2220 gtk_widget_show(hbox);
2221 vbox = gtk_vbox_new(FALSE, 4);
2222 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2223 gtk_widget_show(vbox);
2224 cols = columns_new(4);
2225 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2226 gtk_widget_show(cols);
2227 label = gtk_label_new("Category:");
2228 columns_add(COLUMNS(cols), label, 0, 1);
2229 columns_force_left_align(COLUMNS(cols), label);
2230 gtk_widget_show(label);
2231 treescroll = gtk_scrolled_window_new(NULL, NULL);
2232 #if GTK_CHECK_VERSION(2,0,0)
2233 treestore = gtk_tree_store_new
2234 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2235 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2236 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2237 treerenderer = gtk_cell_renderer_text_new();
2238 treecolumn = gtk_tree_view_column_new_with_attributes
2239 ("Label", treerenderer, "text", 0, NULL);
2240 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2241 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2242 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2243 gtk_container_add(GTK_CONTAINER(treescroll), tree);
2245 tree = gtk_tree_new();
2246 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2247 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2248 gtk_signal_connect(GTK_OBJECT(tree), "focus",
2249 GTK_SIGNAL_FUNC(tree_focus), &dp);
2251 gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event",
2252 GTK_SIGNAL_FUNC(widget_focus), &dp);
2253 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2254 gtk_widget_show(treescroll);
2255 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2256 panels = gtk_notebook_new();
2257 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2258 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2259 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2260 gtk_widget_show(panels);
2265 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2266 struct controlset *s = ctrlbox->ctrlsets[index];
2269 if (!*s->pathname) {
2270 w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window));
2272 set_dialog_action_area(GTK_DIALOG(window), w);
2274 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2275 if (j != INT_MAX) { /* add to treeview, start new panel */
2277 #if GTK_CHECK_VERSION(2,0,0)
2278 GtkTreeIter treeiter;
2280 GtkWidget *treeitem;
2285 * We expect never to find an implicit path
2286 * component. For example, we expect never to see
2287 * A/B/C followed by A/D/E, because that would
2288 * _implicitly_ create A/D. All our path prefixes
2289 * are expected to contain actual controls and be
2290 * selectable in the treeview; so we would expect
2291 * to see A/D _explicitly_ before encountering
2294 assert(j == ctrl_path_elements(s->pathname) - 1);
2296 c = strrchr(s->pathname, '/');
2304 first = (panelvbox == NULL);
2306 panelvbox = gtk_vbox_new(FALSE, 4);
2307 gtk_widget_show(panelvbox);
2308 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
2313 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
2315 gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num);
2318 if (nselparams >= selparamsize) {
2320 selparams = sresize(selparams, selparamsize,
2323 selparams[nselparams].dp = &dp;
2324 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
2325 selparams[nselparams].panel = panelvbox;
2326 selparams[nselparams].shortcuts = scs; /* structure copy */
2328 assert(j-1 < level);
2330 #if GTK_CHECK_VERSION(2,0,0)
2332 /* treeiterlevels[j-1] will always be valid because we
2333 * don't allow implicit path components; see above.
2335 gtk_tree_store_append(treestore, &treeiter,
2336 &treeiterlevels[j-1]);
2338 gtk_tree_store_append(treestore, &treeiter, NULL);
2339 gtk_tree_store_set(treestore, &treeiter,
2341 TREESTORE_PARAMS, nselparams,
2343 treeiterlevels[j] = treeiter;
2348 path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
2349 &treeiterlevels[j-1]);
2351 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree), path,
2354 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree), path);
2355 gtk_tree_path_free(path);
2358 treeitem = gtk_tree_item_new_with_label(c);
2360 if (!treelevels[j-1]) {
2361 treelevels[j-1] = GTK_TREE(gtk_tree_new());
2362 gtk_tree_item_set_subtree
2363 (treeitemlevels[j-1],
2364 GTK_WIDGET(treelevels[j-1]));
2366 gtk_tree_item_expand(treeitemlevels[j-1]);
2368 gtk_tree_item_collapse(treeitemlevels[j-1]);
2370 gtk_tree_append(treelevels[j-1], treeitem);
2372 gtk_tree_append(GTK_TREE(tree), treeitem);
2374 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
2375 treelevels[j] = NULL;
2377 gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event",
2378 GTK_SIGNAL_FUNC(tree_key_press), &dp);
2379 gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event",
2380 GTK_SIGNAL_FUNC(widget_focus), &dp);
2382 gtk_widget_show(treeitem);
2385 gtk_tree_select_child(GTK_TREE(tree), treeitem);
2386 selparams[nselparams].treeitem = treeitem;
2393 w = layout_ctrls(&dp,
2394 &selparams[nselparams-1].shortcuts,
2395 s, listitemheight, NULL);
2396 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
2401 #if GTK_CHECK_VERSION(2,0,0)
2402 g_signal_connect(G_OBJECT(treeselection), "changed",
2403 G_CALLBACK(treeselection_changed), selparams);
2405 dp.ntreeitems = nselparams;
2406 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
2408 for (index = 0; index < nselparams; index++) {
2409 gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
2410 GTK_SIGNAL_FUNC(treeitem_sel),
2412 dp.treeitems[index] = selparams[index].treeitem;
2417 dlg_refresh(NULL, &dp);
2419 dp.shortcuts = &selparams[0].shortcuts;
2420 #if !GTK_CHECK_VERSION(2,0,0)
2421 dp.currtreeitem = dp.treeitems[0];
2423 dp.lastfocus = NULL;
2429 extern void set_window_icon(GtkWidget *window,
2430 const char *const *const *icon,
2432 extern const char *const *const cfg_icon[];
2433 extern const int n_cfg_icon;
2434 set_window_icon(window, cfg_icon, n_cfg_icon);
2437 #if !GTK_CHECK_VERSION(2,0,0)
2438 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
2441 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
2443 GTK_POLICY_AUTOMATIC);
2444 gtk_widget_show(tree);
2446 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2447 gtk_widget_show(window);
2450 * Set focus into the first available control.
2452 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2453 struct controlset *s = ctrlbox->ctrlsets[index];
2458 for (j = 0; j < s->ncontrols; j++)
2459 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
2460 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
2461 s->ctrls[j]->generic.type != CTRL_TEXT) {
2462 dlg_set_focus(s->ctrls[j], &dp);
2463 dp.lastfocus = s->ctrls[j];
2472 gtk_signal_connect(GTK_OBJECT(window), "destroy",
2473 GTK_SIGNAL_FUNC(window_destroy), NULL);
2474 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
2475 GTK_SIGNAL_FUNC(win_key_press), &dp);
2485 static void messagebox_handler(union control *ctrl, void *dlg,
2486 void *data, int event)
2488 if (event == EVENT_ACTION)
2489 dlg_end(dlg, ctrl->generic.context.i);
2491 int messagebox(GtkWidget *parentwin, char *title, char *msg, int minwid, ...)
2493 GtkWidget *window, *w0, *w1;
2494 struct controlbox *ctrlbox;
2495 struct controlset *s0, *s1;
2498 struct Shortcuts scs;
2504 for (index = 0; index < lenof(scs.sc); index++) {
2505 scs.sc[index].action = SHORTCUT_EMPTY;
2508 ctrlbox = ctrl_new_box();
2511 va_start(ap, minwid);
2512 while (va_arg(ap, char *) != NULL) {
2514 (void) va_arg(ap, int); /* shortcut */
2515 (void) va_arg(ap, int); /* normal/default/cancel */
2516 (void) va_arg(ap, int); /* end value */
2520 s0 = ctrl_getset(ctrlbox, "", "", "");
2521 c = ctrl_columns(s0, 2, 50, 50);
2522 c->columns.ncols = s0->ncolumns = ncols;
2523 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
2524 for (index = 0; index < ncols; index++)
2525 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
2526 va_start(ap, minwid);
2529 char *title = va_arg(ap, char *);
2530 int shortcut, type, value;
2533 shortcut = va_arg(ap, int);
2534 type = va_arg(ap, int);
2535 value = va_arg(ap, int);
2536 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
2537 messagebox_handler, I(value));
2538 c->generic.column = index++;
2540 c->button.isdefault = TRUE;
2542 c->button.iscancel = TRUE;
2546 s1 = ctrl_getset(ctrlbox, "x", "", "");
2547 ctrl_text(s1, msg, HELPCTX(no_help));
2549 window = gtk_dialog_new();
2550 gtk_window_set_title(GTK_WINDOW(window), title);
2551 w0 = layout_ctrls(&dp, &scs, s0, 0, GTK_WINDOW(window));
2552 set_dialog_action_area(GTK_DIALOG(window), w0);
2553 gtk_widget_show(w0);
2554 w1 = layout_ctrls(&dp, &scs, s1, 0, GTK_WINDOW(window));
2555 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
2556 gtk_widget_set_usize(w1, minwid+20, -1);
2557 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
2559 gtk_widget_show(w1);
2561 dp.shortcuts = &scs;
2562 dp.lastfocus = NULL;
2566 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
2568 set_transient_window_pos(parentwin, window);
2569 gtk_window_set_transient_for(GTK_WINDOW(window),
2570 GTK_WINDOW(parentwin));
2572 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2573 gtk_widget_show(window);
2575 gtk_signal_connect(GTK_OBJECT(window), "destroy",
2576 GTK_SIGNAL_FUNC(window_destroy), NULL);
2577 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
2578 GTK_SIGNAL_FUNC(win_key_press), &dp);
2583 ctrl_free_box(ctrlbox);
2588 static int string_width(char *text)
2590 GtkWidget *label = gtk_label_new(text);
2592 gtk_widget_size_request(label, &req);
2593 gtk_object_sink(GTK_OBJECT(label));
2597 int reallyclose(void *frontend)
2599 char *title = dupcat(appname, " Exit Confirmation", NULL);
2600 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
2601 title, "Are you sure you want to close this session?",
2602 string_width("Most of the width of the above text"),
2610 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
2611 char *keystr, char *fingerprint,
2612 void (*callback)(void *ctx, int result), void *ctx)
2614 static const char absenttxt[] =
2615 "The server's host key is not cached. You have no guarantee "
2616 "that the server is the computer you think it is.\n"
2617 "The server's %s key fingerprint is:\n"
2619 "If you trust this host, press \"Accept\" to add the key to "
2620 "PuTTY's cache and carry on connecting.\n"
2621 "If you want to carry on connecting just once, without "
2622 "adding the key to the cache, press \"Connect Once\".\n"
2623 "If you do not trust this host, press \"Cancel\" to abandon the "
2625 static const char wrongtxt[] =
2626 "WARNING - POTENTIAL SECURITY BREACH!\n"
2627 "The server's host key does not match the one PuTTY has "
2628 "cached. This means that either the server administrator "
2629 "has changed the host key, or you have actually connected "
2630 "to another computer pretending to be the server.\n"
2631 "The new %s key fingerprint is:\n"
2633 "If you were expecting this change and trust the new key, "
2634 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2635 "If you want to carry on connecting but without updating "
2636 "the cache, press \"Connect Once\".\n"
2637 "If you want to abandon the connection completely, press "
2638 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2646 ret = verify_host_key(host, port, keytype, keystr);
2648 if (ret == 0) /* success - key matched OK */
2651 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
2653 ret = messagebox(GTK_WIDGET(get_window(frontend)),
2654 "PuTTY Security Alert", text,
2655 string_width(fingerprint),
2656 "Accept", 'a', 0, 2,
2657 "Connect Once", 'o', 0, 1,
2658 "Cancel", 'c', -1, 0,
2664 store_host_key(host, port, keytype, keystr);
2665 return 1; /* continue with connection */
2666 } else if (ret == 1)
2667 return 1; /* continue with connection */
2668 return 0; /* do not continue with connection */
2672 * Ask whether the selected algorithm is acceptable (since it was
2673 * below the configured 'warn' threshold).
2675 int askalg(void *frontend, const char *algtype, const char *algname,
2676 void (*callback)(void *ctx, int result), void *ctx)
2678 static const char msg[] =
2679 "The first %s supported by the server is "
2680 "%s, which is below the configured warning threshold.\n"
2681 "Continue with connection?";
2685 text = dupprintf(msg, algtype, algname);
2686 ret = messagebox(GTK_WIDGET(get_window(frontend)),
2687 "PuTTY Security Alert", text,
2688 string_width("Continue with connection?"),
2701 void old_keyfile_warning(void)
2704 * This should never happen on Unix. We hope.
2708 void fatal_message_box(void *window, char *msg)
2710 messagebox(window, "PuTTY Fatal Error", msg,
2711 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2712 "OK", 'o', 1, 1, NULL);
2715 void fatalbox(char *p, ...)
2720 msg = dupvprintf(p, ap);
2722 fatal_message_box(NULL, msg);
2727 static GtkWidget *aboutbox = NULL;
2729 static void about_close_clicked(GtkButton *button, gpointer data)
2731 gtk_widget_destroy(aboutbox);
2735 static void licence_clicked(GtkButton *button, gpointer data)
2740 "Copyright 1997-2008 Simon Tatham.\n\n"
2742 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2743 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2744 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
2745 "Markus Kuhn, Colin Watson, and CORE SDI S.A.\n\n"
2747 "Permission is hereby granted, free of charge, to any person "
2748 "obtaining a copy of this software and associated documentation "
2749 "files (the ""Software""), to deal in the Software without restriction, "
2750 "including without limitation the rights to use, copy, modify, merge, "
2751 "publish, distribute, sublicense, and/or sell copies of the Software, "
2752 "and to permit persons to whom the Software is furnished to do so, "
2753 "subject to the following conditions:\n\n"
2755 "The above copyright notice and this permission notice shall be "
2756 "included in all copies or substantial portions of the Software.\n\n"
2758 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2759 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2760 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2761 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2762 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
2763 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2764 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2765 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2766 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2767 "OTHER DEALINGS IN THE SOFTWARE.";
2769 title = dupcat(appname, " Licence", NULL);
2770 assert(aboutbox != NULL);
2771 messagebox(aboutbox, title, licence,
2772 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2773 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2774 "OK", 'o', 1, 1, NULL);
2778 void about_box(void *window)
2784 gtk_widget_grab_focus(aboutbox);
2788 aboutbox = gtk_dialog_new();
2789 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
2790 title = dupcat("About ", appname, NULL);
2791 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
2794 w = gtk_button_new_with_label("Close");
2795 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
2796 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
2797 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
2798 w, FALSE, FALSE, 0);
2799 gtk_signal_connect(GTK_OBJECT(w), "clicked",
2800 GTK_SIGNAL_FUNC(about_close_clicked), NULL);
2803 w = gtk_button_new_with_label("View Licence");
2804 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
2805 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
2806 w, FALSE, FALSE, 0);
2807 gtk_signal_connect(GTK_OBJECT(w), "clicked",
2808 GTK_SIGNAL_FUNC(licence_clicked), NULL);
2811 w = gtk_label_new(appname);
2812 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2813 w, FALSE, FALSE, 0);
2816 w = gtk_label_new(ver);
2817 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2818 w, FALSE, FALSE, 5);
2821 w = gtk_label_new("Copyright 1997-2008 Simon Tatham. All rights reserved");
2822 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2823 w, FALSE, FALSE, 5);
2826 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
2827 gtk_widget_show(aboutbox);
2830 struct eventlog_stuff {
2831 GtkWidget *parentwin, *window;
2832 struct controlbox *eventbox;
2833 struct Shortcuts scs;
2835 union control *listctrl;
2837 int nevents, negsize;
2840 int ignore_selchange;
2843 static void eventlog_destroy(GtkWidget *widget, gpointer data)
2845 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2849 dlg_cleanup(&es->dp);
2850 ctrl_free_box(es->eventbox);
2852 static void eventlog_ok_handler(union control *ctrl, void *dlg,
2853 void *data, int event)
2855 if (event == EVENT_ACTION)
2858 static void eventlog_list_handler(union control *ctrl, void *dlg,
2859 void *data, int event)
2861 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2863 if (event == EVENT_REFRESH) {
2866 dlg_update_start(ctrl, dlg);
2867 dlg_listbox_clear(ctrl, dlg);
2868 for (i = 0; i < es->nevents; i++) {
2869 dlg_listbox_add(ctrl, dlg, es->events[i]);
2871 dlg_update_done(ctrl, dlg);
2872 } else if (event == EVENT_SELCHANGE) {
2877 * If this SELCHANGE event is happening as a result of
2878 * deliberate deselection because someone else has grabbed
2879 * the selection, the last thing we want to do is pre-empt
2882 if (es->ignore_selchange)
2886 * Construct the data to use as the selection.
2891 for (i = 0; i < es->nevents; i++) {
2892 if (dlg_listbox_issel(ctrl, dlg, i)) {
2893 int extralen = strlen(es->events[i]);
2895 if (es->sellen + extralen + 2 > selsize) {
2896 selsize = es->sellen + extralen + 512;
2897 es->seldata = sresize(es->seldata, selsize, char);
2900 strcpy(es->seldata + es->sellen, es->events[i]);
2901 es->sellen += extralen;
2902 es->seldata[es->sellen++] = '\n';
2906 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
2907 GDK_CURRENT_TIME)) {
2908 extern GdkAtom compound_text_atom;
2910 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
2911 GDK_SELECTION_TYPE_STRING, 1);
2912 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
2913 compound_text_atom, 1);
2919 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2920 guint info, guint time_stamp, gpointer data)
2922 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2924 gtk_selection_data_set(seldata, seldata->target, 8,
2925 (unsigned char *)es->seldata, es->sellen);
2928 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2931 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2935 * Deselect everything in the list box.
2937 uc = dlg_find_byctrl(&es->dp, es->listctrl);
2938 es->ignore_selchange = 1;
2939 gtk_list_unselect_all(GTK_LIST(uc->list));
2940 es->ignore_selchange = 0;
2948 void showeventlog(void *estuff, void *parentwin)
2950 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
2951 GtkWidget *window, *w0, *w1;
2952 GtkWidget *parent = GTK_WIDGET(parentwin);
2953 struct controlset *s0, *s1;
2955 int listitemheight, index;
2959 gtk_widget_grab_focus(es->window);
2965 for (index = 0; index < lenof(es->scs.sc); index++) {
2966 es->scs.sc[index].action = SHORTCUT_EMPTY;
2969 es->eventbox = ctrl_new_box();
2971 s0 = ctrl_getset(es->eventbox, "", "", "");
2972 ctrl_columns(s0, 3, 33, 34, 33);
2973 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
2974 eventlog_ok_handler, P(NULL));
2975 c->button.column = 1;
2976 c->button.isdefault = TRUE;
2978 s1 = ctrl_getset(es->eventbox, "x", "", "");
2979 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
2980 eventlog_list_handler, P(es));
2981 c->listbox.height = 10;
2982 c->listbox.multisel = 2;
2983 c->listbox.ncols = 3;
2984 c->listbox.percentages = snewn(3, int);
2985 c->listbox.percentages[0] = 25;
2986 c->listbox.percentages[1] = 10;
2987 c->listbox.percentages[2] = 65;
2989 listitemheight = get_listitemheight();
2991 es->window = window = gtk_dialog_new();
2992 title = dupcat(appname, " Event Log", NULL);
2993 gtk_window_set_title(GTK_WINDOW(window), title);
2995 w0 = layout_ctrls(&es->dp, &es->scs, s0,
2996 listitemheight, GTK_WINDOW(window));
2997 set_dialog_action_area(GTK_DIALOG(window), w0);
2998 gtk_widget_show(w0);
2999 w1 = layout_ctrls(&es->dp, &es->scs, s1,
3000 listitemheight, GTK_WINDOW(window));
3001 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3002 gtk_widget_set_usize(w1, 20 +
3003 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3004 " IS QUITE LONG 'COS SSH LOG ENTRIES"
3006 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
3008 gtk_widget_show(w1);
3011 es->dp.shortcuts = &es->scs;
3012 es->dp.lastfocus = NULL;
3014 es->dp.window = window;
3016 dlg_refresh(NULL, &es->dp);
3019 set_transient_window_pos(parent, window);
3020 gtk_window_set_transient_for(GTK_WINDOW(window),
3021 GTK_WINDOW(parent));
3023 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3024 gtk_widget_show(window);
3026 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3027 GTK_SIGNAL_FUNC(eventlog_destroy), es);
3028 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3029 GTK_SIGNAL_FUNC(win_key_press), &es->dp);
3030 gtk_signal_connect(GTK_OBJECT(window), "selection_get",
3031 GTK_SIGNAL_FUNC(eventlog_selection_get), es);
3032 gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event",
3033 GTK_SIGNAL_FUNC(eventlog_selection_clear), es);
3036 void *eventlogstuff_new(void)
3038 struct eventlog_stuff *es;
3039 es = snew(struct eventlog_stuff);
3040 memset(es, 0, sizeof(*es));
3044 void logevent_dlg(void *estuff, const char *string)
3046 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3051 if (es->nevents >= es->negsize) {
3053 es->events = sresize(es->events, es->negsize, char *);
3057 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3059 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3060 strcpy(es->events[es->nevents], timebuf);
3061 strcat(es->events[es->nevents], string);
3063 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3068 int askappend(void *frontend, Filename filename,
3069 void (*callback)(void *ctx, int result), void *ctx)
3071 static const char msgtemplate[] =
3072 "The session log file \"%.*s\" already exists. "
3073 "You can overwrite it with a new session log, "
3074 "append your session log to the end of it, "
3075 "or disable session logging for this session.";
3080 message = dupprintf(msgtemplate, FILENAME_MAX, filename.path);
3081 mbtitle = dupprintf("%s Log to File", appname);
3083 mbret = messagebox(get_window(frontend), mbtitle, message,
3084 string_width("LINE OF TEXT SUITABLE FOR THE"
3085 " ASKAPPEND WIDTH"),
3086 "Overwrite", 'o', 1, 2,
3087 "Append", 'a', 0, 1,
3088 "Disable", 'd', -1, 0,