2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
10 #if !GTK_CHECK_VERSION(3,0,0)
11 #include <gdk/gdkkeysyms.h>
16 #include <X11/Xutil.h>
19 #include "gtkcompat.h"
25 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
33 #if GTK_CHECK_VERSION(2,0,0)
34 /* Decide which of GtkFileChooserDialog and GtkFileSelection to use */
35 #define USE_GTK_FILE_CHOOSER_DIALOG
45 struct Shortcut sc[128];
51 GtkWidget **buttons; int nbuttons; /* for radio buttons */
52 GtkWidget *entry; /* for editbox, filesel, fontsel */
53 GtkWidget *button; /* for filesel, fontsel */
54 #if !GTK_CHECK_VERSION(2,4,0)
55 GtkWidget *list; /* for listbox (in GTK1), combobox (<=GTK2.3) */
56 GtkWidget *menu; /* for optionmenu (==droplist) */
57 GtkWidget *optmenu; /* also for optionmenu */
59 GtkWidget *combo; /* for combo box (either editable or not) */
61 #if GTK_CHECK_VERSION(2,0,0)
62 GtkWidget *treeview; /* for listbox (GTK2), droplist+combo (>=2.4) */
63 GtkListStore *listmodel; /* for all types of list box */
65 GtkWidget *text; /* for text */
66 GtkWidget *label; /* for dlg_label_change */
67 GtkAdjustment *adj; /* for the scrollbar in a list box */
74 tree234 *byctrl, *bywidget;
76 struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
77 /* `flags' are set to indicate when a GTK signal handler is being called
78 * due to automatic processing and should not flag a user event. */
80 struct Shortcuts *shortcuts;
81 GtkWidget *window, *cancelbutton;
82 union control *currfocus, *lastfocus;
83 #if !GTK_CHECK_VERSION(2,0,0)
84 GtkWidget *currtreeitem, **treeitems;
89 #define FLAG_UPDATING_COMBO_LIST 1
90 #define FLAG_UPDATING_LISTBOX 2
92 enum { /* values for Shortcut.action */
93 SHORTCUT_EMPTY, /* no shortcut on this key */
94 SHORTCUT_TREE, /* focus a tree item */
95 SHORTCUT_FOCUS, /* focus the supplied widget */
96 SHORTCUT_UCTRL, /* do something sane with uctrl */
97 SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */
98 SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */
101 #if GTK_CHECK_VERSION(2,0,0)
110 * Forward references.
112 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
114 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
115 int chr, int action, void *ptr);
116 static void shortcut_highlight(GtkWidget *label, int chr);
117 #if !GTK_CHECK_VERSION(2,0,0)
118 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
120 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
122 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
124 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
127 #if !GTK_CHECK_VERSION(2,4,0)
128 static void menuitem_activate(GtkMenuItem *item, gpointer data);
130 static void coloursel_ok(GtkButton *button, gpointer data);
131 static void coloursel_cancel(GtkButton *button, gpointer data);
132 static void window_destroy(GtkWidget *widget, gpointer data);
133 int get_listitemheight(GtkWidget *widget);
135 static int uctrl_cmp_byctrl(void *av, void *bv)
137 struct uctrl *a = (struct uctrl *)av;
138 struct uctrl *b = (struct uctrl *)bv;
139 if (a->ctrl < b->ctrl)
141 else if (a->ctrl > b->ctrl)
146 static int uctrl_cmp_byctrl_find(void *av, void *bv)
148 union control *a = (union control *)av;
149 struct uctrl *b = (struct uctrl *)bv;
152 else if (a > b->ctrl)
157 static int uctrl_cmp_bywidget(void *av, void *bv)
159 struct uctrl *a = (struct uctrl *)av;
160 struct uctrl *b = (struct uctrl *)bv;
161 if (a->toplevel < b->toplevel)
163 else if (a->toplevel > b->toplevel)
168 static int uctrl_cmp_bywidget_find(void *av, void *bv)
170 GtkWidget *a = (GtkWidget *)av;
171 struct uctrl *b = (struct uctrl *)bv;
174 else if (a > b->toplevel)
179 static void dlg_init(struct dlgparam *dp)
181 dp->byctrl = newtree234(uctrl_cmp_byctrl);
182 dp->bywidget = newtree234(uctrl_cmp_bywidget);
183 dp->coloursel_result.ok = FALSE;
184 dp->window = dp->cancelbutton = NULL;
185 #if !GTK_CHECK_VERSION(2,0,0)
186 dp->treeitems = NULL;
187 dp->currtreeitem = NULL;
190 dp->currfocus = NULL;
193 static void dlg_cleanup(struct dlgparam *dp)
197 freetree234(dp->byctrl); /* doesn't free the uctrls inside */
199 while ( (uc = index234(dp->bywidget, 0)) != NULL) {
200 del234(dp->bywidget, uc);
204 freetree234(dp->bywidget);
206 #if !GTK_CHECK_VERSION(2,0,0)
207 sfree(dp->treeitems);
211 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
213 add234(dp->byctrl, uc);
214 add234(dp->bywidget, uc);
217 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
221 return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
224 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
226 struct uctrl *ret = NULL;
230 ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
233 w = gtk_widget_get_parent(w);
238 union control *dlg_last_focused(union control *ctrl, void *dlg)
240 struct dlgparam *dp = (struct dlgparam *)dlg;
241 if (dp->currfocus != ctrl)
242 return dp->currfocus;
244 return dp->lastfocus;
247 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
249 struct dlgparam *dp = (struct dlgparam *)dlg;
250 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
251 assert(uc->ctrl->generic.type == CTRL_RADIO);
252 assert(uc->buttons != NULL);
253 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
256 int dlg_radiobutton_get(union control *ctrl, void *dlg)
258 struct dlgparam *dp = (struct dlgparam *)dlg;
259 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
262 assert(uc->ctrl->generic.type == CTRL_RADIO);
263 assert(uc->buttons != NULL);
264 for (i = 0; i < uc->nbuttons; i++)
265 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
267 return 0; /* got to return something */
270 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
272 struct dlgparam *dp = (struct dlgparam *)dlg;
273 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
274 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
275 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
278 int dlg_checkbox_get(union control *ctrl, void *dlg)
280 struct dlgparam *dp = (struct dlgparam *)dlg;
281 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
282 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
283 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
286 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
288 struct dlgparam *dp = (struct dlgparam *)dlg;
289 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
292 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
294 #if GTK_CHECK_VERSION(2,4,0)
296 entry = gtk_bin_get_child(GTK_BIN(uc->combo));
301 assert(entry != NULL);
304 * GTK 2 implements gtk_entry_set_text by means of two separate
305 * operations: first delete the previous text leaving the empty
306 * string, then insert the new text. This causes two calls to
307 * the "changed" signal.
309 * The first call to "changed", if allowed to proceed normally,
310 * will cause an EVENT_VALCHANGE event on the edit box, causing
311 * a call to dlg_editbox_get() which will read the empty string
312 * out of the GtkEntry - and promptly write it straight into the
313 * Conf structure, which is precisely where our `text' pointer
314 * is probably pointing, so the second editing operation will
315 * insert that instead of the string we originally asked for.
317 * Hence, we must take our own copy of the text before we do
320 tmpstring = dupstr(text);
321 gtk_entry_set_text(GTK_ENTRY(entry), tmpstring);
325 char *dlg_editbox_get(union control *ctrl, void *dlg)
327 struct dlgparam *dp = (struct dlgparam *)dlg;
328 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
329 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
331 #if GTK_CHECK_VERSION(2,4,0)
333 return dupstr(gtk_entry_get_text
334 (GTK_ENTRY(gtk_bin_get_child(GTK_BIN(uc->combo)))));
339 return dupstr(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
342 assert(!"We shouldn't get here");
345 #if !GTK_CHECK_VERSION(2,4,0)
346 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
348 GtkContainer *cont = GTK_CONTAINER(data);
349 /* gtk_container_remove will unref the widget for us; we need not. */
350 gtk_container_remove(cont, w);
354 /* The `listbox' functions can also apply to combo boxes. */
355 void dlg_listbox_clear(union control *ctrl, void *dlg)
357 struct dlgparam *dp = (struct dlgparam *)dlg;
358 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
360 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
361 uc->ctrl->generic.type == CTRL_LISTBOX);
363 #if !GTK_CHECK_VERSION(2,4,0)
365 gtk_container_foreach(GTK_CONTAINER(uc->menu),
366 container_remove_and_destroy,
367 GTK_CONTAINER(uc->menu));
371 gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
375 #if GTK_CHECK_VERSION(2,0,0)
377 gtk_list_store_clear(uc->listmodel);
381 assert(!"We shouldn't get here");
384 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
386 struct dlgparam *dp = (struct dlgparam *)dlg;
387 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
389 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
390 uc->ctrl->generic.type == CTRL_LISTBOX);
392 #if !GTK_CHECK_VERSION(2,4,0)
395 (GTK_CONTAINER(uc->menu),
396 g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
400 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
404 #if GTK_CHECK_VERSION(2,0,0)
408 assert(uc->listmodel != NULL);
409 path = gtk_tree_path_new_from_indices(index, -1);
410 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
411 gtk_list_store_remove(uc->listmodel, &iter);
412 gtk_tree_path_free(path);
416 assert(!"We shouldn't get here");
419 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
421 dlg_listbox_addwithid(ctrl, dlg, text, 0);
425 * Each listbox entry may have a numeric id associated with it.
426 * Note that some front ends only permit a string to be stored at
427 * each position, which means that _if_ you put two identical
428 * strings in any listbox then you MUST not assign them different
429 * IDs and expect to get meaningful results back.
431 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
432 char const *text, int id)
434 struct dlgparam *dp = (struct dlgparam *)dlg;
435 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
437 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
438 uc->ctrl->generic.type == CTRL_LISTBOX);
441 * This routine is long and complicated in both GTK 1 and 2,
442 * and completely different. Sigh.
444 dp->flags |= FLAG_UPDATING_COMBO_LIST;
446 #if !GTK_CHECK_VERSION(2,4,0)
449 * List item in a drop-down (but non-combo) list. Tabs are
450 * ignored; we just provide a standard menu item with the
453 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
455 gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
456 gtk_widget_show(menuitem);
458 g_object_set_data(G_OBJECT(menuitem), "user-data",
459 GINT_TO_POINTER(id));
460 g_signal_connect(G_OBJECT(menuitem), "activate",
461 G_CALLBACK(menuitem_activate), dp);
464 if (uc->list && uc->entry) {
466 * List item in a combo-box list, which means the sensible
467 * thing to do is make it a perfectly normal label. Hence
468 * tabs are disregarded.
470 GtkWidget *listitem = gtk_list_item_new_with_label(text);
472 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
473 gtk_widget_show(listitem);
475 g_object_set_data(G_OBJECT(listitem), "user-data",
476 GINT_TO_POINTER(id));
480 #if !GTK_CHECK_VERSION(2,0,0)
483 * List item in a non-combo-box list box. We make all of
484 * these Columns containing GtkLabels. This allows us to do
485 * the nasty force_left hack irrespective of whether there
486 * are tabs in the thing.
488 GtkWidget *listitem = gtk_list_item_new();
489 GtkWidget *cols = columns_new(10);
493 /* Count the tabs in the text, and hence determine # of columns. */
495 for (i = 0; text[i]; i++)
500 (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
501 percents = snewn(ncols, gint);
502 percents[ncols-1] = 100;
503 for (i = 0; i < ncols-1; i++) {
504 percents[i] = uc->ctrl->listbox.percentages[i];
505 percents[ncols-1] -= percents[i];
507 columns_set_cols(COLUMNS(cols), ncols, percents);
510 for (i = 0; i < ncols; i++) {
511 int len = strcspn(text, "\t");
512 char *dup = dupprintf("%.*s", len, text);
517 label = gtk_label_new(dup);
520 columns_add(COLUMNS(cols), label, i, 1);
521 columns_force_left_align(COLUMNS(cols), label);
522 gtk_widget_show(label);
524 gtk_container_add(GTK_CONTAINER(listitem), cols);
525 gtk_widget_show(cols);
526 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
527 gtk_widget_show(listitem);
529 if (ctrl->listbox.multisel) {
530 g_signal_connect(G_OBJECT(listitem), "key_press_event",
531 G_CALLBACK(listitem_multi_key), uc->adj);
533 g_signal_connect(G_OBJECT(listitem), "key_press_event",
534 G_CALLBACK(listitem_single_key), uc->adj);
536 g_signal_connect(G_OBJECT(listitem), "focus_in_event",
537 G_CALLBACK(widget_focus), dp);
538 g_signal_connect(G_OBJECT(listitem), "button_press_event",
539 G_CALLBACK(listitem_button_press), dp);
540 g_signal_connect(G_OBJECT(listitem), "button_release_event",
541 G_CALLBACK(listitem_button_release), dp);
542 g_object_set_data(G_OBJECT(listitem), "user-data",
543 GINT_TO_POINTER(id));
551 dp->flags |= FLAG_UPDATING_LISTBOX;/* inhibit drag-list update */
552 gtk_list_store_append(uc->listmodel, &iter);
553 dp->flags &= ~FLAG_UPDATING_LISTBOX;
554 gtk_list_store_set(uc->listmodel, &iter, 0, id, -1);
557 * Now go through text and divide it into columns at the tabs,
560 cols = (uc->ctrl->generic.type == CTRL_LISTBOX ? ctrl->listbox.ncols : 1);
561 cols = cols ? cols : 1;
562 for (i = 0; i < cols; i++) {
563 int collen = strcspn(text, "\t");
564 char *tmpstr = snewn(collen+1, char);
565 memcpy(tmpstr, text, collen);
566 tmpstr[collen] = '\0';
567 gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1);
575 assert(!"We shouldn't get here");
577 dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
580 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
582 struct dlgparam *dp = (struct dlgparam *)dlg;
583 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
585 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
586 uc->ctrl->generic.type == CTRL_LISTBOX);
588 #if !GTK_CHECK_VERSION(2,4,0)
589 if (uc->menu || uc->list) {
593 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
595 item = G_OBJECT(g_list_nth_data(children, index));
596 g_list_free(children);
598 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item), "user-data"));
601 #if GTK_CHECK_VERSION(2,0,0)
607 path = gtk_tree_path_new_from_indices(index, -1);
608 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
609 gtk_tree_model_get(GTK_TREE_MODEL(uc->listmodel), &iter, 0, &ret, -1);
610 gtk_tree_path_free(path);
615 assert(!"We shouldn't get here");
616 return -1; /* placate dataflow analysis */
619 /* dlg_listbox_index returns <0 if no single element is selected. */
620 int dlg_listbox_index(union control *ctrl, void *dlg)
622 struct dlgparam *dp = (struct dlgparam *)dlg;
623 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
625 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
626 uc->ctrl->generic.type == CTRL_LISTBOX);
628 #if !GTK_CHECK_VERSION(2,4,0)
629 if (uc->menu || uc->list) {
631 GtkWidget *item, *activeitem;
636 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
638 activeitem = NULL; /* unnecessarily placate gcc */
640 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
642 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
643 i++, children = children->next) {
644 if (uc->menu ? activeitem == item :
645 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
652 g_list_free(children);
653 return selected < 0 ? -1 : selected;
658 * This API function already does the right thing in the
659 * case of no current selection.
661 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo));
664 #if GTK_CHECK_VERSION(2,0,0)
666 GtkTreeSelection *treesel;
673 assert(uc->treeview != NULL);
674 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
676 if (gtk_tree_selection_count_selected_rows(treesel) != 1)
679 sellist = gtk_tree_selection_get_selected_rows(treesel, &model);
681 assert(sellist && sellist->data);
682 path = sellist->data;
684 if (gtk_tree_path_get_depth(path) != 1) {
687 indices = gtk_tree_path_get_indices(path);
695 g_list_foreach(sellist, (GFunc)gtk_tree_path_free, NULL);
696 g_list_free(sellist);
701 assert(!"We shouldn't get here");
702 return -1; /* placate dataflow analysis */
705 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
707 struct dlgparam *dp = (struct dlgparam *)dlg;
708 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
710 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
711 uc->ctrl->generic.type == CTRL_LISTBOX);
713 #if !GTK_CHECK_VERSION(2,4,0)
714 if (uc->menu || uc->list) {
716 GtkWidget *item, *activeitem;
718 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
719 uc->ctrl->generic.type == CTRL_LISTBOX);
720 assert(uc->menu != NULL || uc->list != NULL);
722 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
724 item = GTK_WIDGET(g_list_nth_data(children, index));
725 g_list_free(children);
728 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
729 return item == activeitem;
731 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
737 * This API function already does the right thing in the
738 * case of no current selection.
740 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo)) == index;
743 #if GTK_CHECK_VERSION(2,0,0)
745 GtkTreeSelection *treesel;
749 assert(uc->treeview != NULL);
750 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
752 path = gtk_tree_path_new_from_indices(index, -1);
753 ret = gtk_tree_selection_path_is_selected(treesel, path);
754 gtk_tree_path_free(path);
759 assert(!"We shouldn't get here");
760 return -1; /* placate dataflow analysis */
763 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
765 struct dlgparam *dp = (struct dlgparam *)dlg;
766 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
768 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
769 uc->ctrl->generic.type == CTRL_LISTBOX);
771 #if !GTK_CHECK_VERSION(2,4,0)
773 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
779 gdouble newtop, newbot;
781 gtk_list_select_item(GTK_LIST(uc->list), index);
784 * Scroll the list box if necessary to ensure the newly
785 * selected item is visible.
787 items = gtk_container_children(GTK_CONTAINER(uc->list));
788 nitems = g_list_length(items);
790 int modified = FALSE;
792 newtop = uc->adj->lower +
793 (uc->adj->upper - uc->adj->lower) * index / nitems;
794 newbot = uc->adj->lower +
795 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
796 if (uc->adj->value > newtop) {
798 uc->adj->value = newtop;
799 } else if (uc->adj->value < newbot - uc->adj->page_size) {
801 uc->adj->value = newbot - uc->adj->page_size;
804 gtk_adjustment_value_changed(uc->adj);
810 gtk_combo_box_set_active(GTK_COMBO_BOX(uc->combo), index);
814 #if GTK_CHECK_VERSION(2,0,0)
816 GtkTreeSelection *treesel;
819 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
821 path = gtk_tree_path_new_from_indices(index, -1);
822 gtk_tree_selection_select_path(treesel, path);
823 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc->treeview),
824 path, NULL, FALSE, 0.0, 0.0);
825 gtk_tree_path_free(path);
829 assert(!"We shouldn't get here");
832 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
834 struct dlgparam *dp = (struct dlgparam *)dlg;
835 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
837 assert(uc->ctrl->generic.type == CTRL_TEXT);
838 assert(uc->text != NULL);
840 gtk_label_set_text(GTK_LABEL(uc->text), text);
843 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
845 struct dlgparam *dp = (struct dlgparam *)dlg;
846 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
848 switch (uc->ctrl->generic.type) {
850 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
851 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
854 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
855 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
858 gtk_label_set_text(GTK_LABEL(uc->label), text);
859 shortcut_highlight(uc->label, ctrl->radio.shortcut);
862 gtk_label_set_text(GTK_LABEL(uc->label), text);
863 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
865 case CTRL_FILESELECT:
866 gtk_label_set_text(GTK_LABEL(uc->label), text);
867 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
869 case CTRL_FONTSELECT:
870 gtk_label_set_text(GTK_LABEL(uc->label), text);
871 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
874 gtk_label_set_text(GTK_LABEL(uc->label), text);
875 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
878 assert(!"This shouldn't happen");
883 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
885 struct dlgparam *dp = (struct dlgparam *)dlg;
886 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
887 /* We must copy fn->path before passing it to gtk_entry_set_text.
888 * See comment in dlg_editbox_set() for the reasons. */
889 char *duppath = dupstr(fn->path);
890 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
891 assert(uc->entry != NULL);
892 gtk_entry_set_text(GTK_ENTRY(uc->entry), duppath);
896 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
898 struct dlgparam *dp = (struct dlgparam *)dlg;
899 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
900 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
901 assert(uc->entry != NULL);
902 return filename_from_str(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
905 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
907 struct dlgparam *dp = (struct dlgparam *)dlg;
908 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
909 /* We must copy fs->name before passing it to gtk_entry_set_text.
910 * See comment in dlg_editbox_set() for the reasons. */
911 char *dupname = dupstr(fs->name);
912 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
913 assert(uc->entry != NULL);
914 gtk_entry_set_text(GTK_ENTRY(uc->entry), dupname);
918 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
920 struct dlgparam *dp = (struct dlgparam *)dlg;
921 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
922 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
923 assert(uc->entry != NULL);
924 return fontspec_new(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
928 * Bracketing a large set of updates in these two functions will
929 * cause the front end (if possible) to delay updating the screen
930 * until it's all complete, thus avoiding flicker.
932 void dlg_update_start(union control *ctrl, void *dlg)
935 * Apparently we can't do this at all in GTK. GtkCList supports
936 * freeze and thaw, but not GtkList. Bah.
940 void dlg_update_done(union control *ctrl, void *dlg)
943 * Apparently we can't do this at all in GTK. GtkCList supports
944 * freeze and thaw, but not GtkList. Bah.
948 void dlg_set_focus(union control *ctrl, void *dlg)
950 struct dlgparam *dp = (struct dlgparam *)dlg;
951 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
953 switch (ctrl->generic.type) {
956 /* Check boxes and buttons get the focus _and_ get toggled. */
957 gtk_widget_grab_focus(uc->toplevel);
959 case CTRL_FILESELECT:
960 case CTRL_FONTSELECT:
963 /* Anything containing an edit box gets that focused. */
964 gtk_widget_grab_focus(uc->entry);
966 #if GTK_CHECK_VERSION(2,4,0)
967 else if (uc->combo) {
968 /* Failing that, there'll be a combo box. */
969 gtk_widget_grab_focus(uc->combo);
975 * Radio buttons: we find the currently selected button and
980 for (i = 0; i < ctrl->radio.nbuttons; i++)
981 if (gtk_toggle_button_get_active
982 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
983 gtk_widget_grab_focus(uc->buttons[i]);
988 #if !GTK_CHECK_VERSION(2,4,0)
990 gtk_widget_grab_focus(uc->optmenu);
995 gtk_widget_grab_focus(uc->combo);
999 #if !GTK_CHECK_VERSION(2,0,0)
1002 * For GTK-1 style list boxes, we tell it to focus one
1003 * of its children, which appears to do the Right
1006 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
1011 gtk_widget_grab_focus(uc->treeview);
1015 assert(!"We shouldn't get here");
1021 * During event processing, you might well want to give an error
1022 * indication to the user. dlg_beep() is a quick and easy generic
1023 * error; dlg_error() puts up a message-box or equivalent.
1025 void dlg_beep(void *dlg)
1030 static void errmsg_button_clicked(GtkButton *button, gpointer data)
1032 gtk_widget_destroy(GTK_WIDGET(data));
1035 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
1037 #if !GTK_CHECK_VERSION(2,0,0)
1038 gint x, y, w, h, dx, dy;
1040 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
1041 gtk_widget_size_request(GTK_WIDGET(child), &req);
1043 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(parent)), &x, &y);
1044 gdk_window_get_size(gtk_widget_get_window(GTK_WIDGET(parent)), &w, &h);
1047 * One corner of the transient will be offset inwards, by 1/4
1048 * of the parent window's size, from the corresponding corner
1049 * of the parent window. The corner will be chosen so as to
1050 * place the transient closer to the centre of the screen; this
1051 * should avoid transients going off the edge of the screen on
1054 if (x + w/2 < gdk_screen_width() / 2)
1055 dx = x + w/4; /* work from left edges */
1057 dx = x + 3*w/4 - req.width; /* work from right edges */
1058 if (y + h/2 < gdk_screen_height() / 2)
1059 dy = y + h/4; /* work from top edges */
1061 dy = y + 3*h/4 - req.height; /* work from bottom edges */
1062 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
1066 void dlg_error_msg(void *dlg, const char *msg)
1068 struct dlgparam *dp = (struct dlgparam *)dlg;
1069 GtkWidget *window, *hbox, *text, *ok;
1071 window = gtk_dialog_new();
1072 text = gtk_label_new(msg);
1073 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1074 hbox = gtk_hbox_new(FALSE, 0);
1075 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1076 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
1077 hbox, FALSE, FALSE, 20);
1078 gtk_widget_show(text);
1079 gtk_widget_show(hbox);
1080 gtk_window_set_title(GTK_WINDOW(window), "Error");
1081 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1082 ok = gtk_button_new_with_label("OK");
1083 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(window))),
1084 ok, FALSE, FALSE, 0);
1085 gtk_widget_show(ok);
1086 gtk_widget_set_can_default(ok, TRUE);
1087 gtk_window_set_default(GTK_WINDOW(window), ok);
1088 g_signal_connect(G_OBJECT(ok), "clicked",
1089 G_CALLBACK(errmsg_button_clicked), window);
1090 g_signal_connect(G_OBJECT(window), "destroy",
1091 G_CALLBACK(window_destroy), NULL);
1092 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1093 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
1094 set_transient_window_pos(dp->window, window);
1095 gtk_widget_show(window);
1100 * This function signals to the front end that the dialog's
1101 * processing is completed, and passes an integer value (typically
1102 * a success status).
1104 void dlg_end(void *dlg, int value)
1106 struct dlgparam *dp = (struct dlgparam *)dlg;
1108 gtk_widget_destroy(dp->window);
1111 void dlg_refresh(union control *ctrl, void *dlg)
1113 struct dlgparam *dp = (struct dlgparam *)dlg;
1117 if (ctrl->generic.handler != NULL)
1118 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1122 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
1123 assert(uc->ctrl != NULL);
1124 if (uc->ctrl->generic.handler != NULL)
1125 uc->ctrl->generic.handler(uc->ctrl, dp,
1126 dp->data, EVENT_REFRESH);
1131 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
1133 struct dlgparam *dp = (struct dlgparam *)dlg;
1134 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
1135 GtkWidget *okbutton, *cancelbutton;
1137 GtkWidget *coloursel =
1138 gtk_color_selection_dialog_new("Select a colour");
1139 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
1140 GtkColorSelection *cs = GTK_COLOR_SELECTION
1141 (gtk_color_selection_dialog_get_color_selection(ccs));
1143 dp->coloursel_result.ok = FALSE;
1145 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
1146 #if GTK_CHECK_VERSION(2,0,0)
1147 gtk_color_selection_set_has_opacity_control(cs, FALSE);
1149 gtk_color_selection_set_opacity(cs, FALSE);
1152 #if GTK_CHECK_VERSION(2,0,0)
1155 col.red = r * 0x0101;
1156 col.green = g * 0x0101;
1157 col.blue = b * 0x0101;
1158 gtk_color_selection_set_current_color(cs, &col);
1163 cvals[0] = r / 255.0;
1164 cvals[1] = g / 255.0;
1165 cvals[2] = b / 255.0;
1166 cvals[3] = 1.0; /* fully opaque! */
1167 gtk_color_selection_set_color(cs, cvals);
1171 #if GTK_CHECK_VERSION(2,0,0)
1172 g_object_get(G_OBJECT(ccs),
1173 "ok-button", &okbutton,
1174 "cancel-button", &cancelbutton,
1175 (const char *)NULL);
1177 okbutton = ccs->ok_button;
1178 cancelbutton = ccs->cancel_button;
1180 g_object_set_data(G_OBJECT(okbutton), "user-data",
1181 (gpointer)coloursel);
1182 g_object_set_data(G_OBJECT(cancelbutton), "user-data",
1183 (gpointer)coloursel);
1184 g_object_set_data(G_OBJECT(coloursel), "user-data", (gpointer)uc);
1185 g_signal_connect(G_OBJECT(okbutton), "clicked",
1186 G_CALLBACK(coloursel_ok), (gpointer)dp);
1187 g_signal_connect(G_OBJECT(cancelbutton), "clicked",
1188 G_CALLBACK(coloursel_cancel), (gpointer)dp);
1189 g_signal_connect_swapped(G_OBJECT(okbutton), "clicked",
1190 G_CALLBACK(gtk_widget_destroy),
1191 (gpointer)coloursel);
1192 g_signal_connect_swapped(G_OBJECT(cancelbutton), "clicked",
1193 G_CALLBACK(gtk_widget_destroy),
1194 (gpointer)coloursel);
1195 gtk_widget_show(coloursel);
1198 int dlg_coloursel_results(union control *ctrl, void *dlg,
1199 int *r, int *g, int *b)
1201 struct dlgparam *dp = (struct dlgparam *)dlg;
1202 if (dp->coloursel_result.ok) {
1203 *r = dp->coloursel_result.r;
1204 *g = dp->coloursel_result.g;
1205 *b = dp->coloursel_result.b;
1211 /* ----------------------------------------------------------------------
1212 * Signal handlers while the dialog box is active.
1215 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
1218 struct dlgparam *dp = (struct dlgparam *)data;
1219 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1220 union control *focus;
1227 if (focus != dp->currfocus) {
1228 dp->lastfocus = dp->currfocus;
1229 dp->currfocus = focus;
1235 static void button_clicked(GtkButton *button, gpointer data)
1237 struct dlgparam *dp = (struct dlgparam *)data;
1238 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1239 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1242 static void button_toggled(GtkToggleButton *tb, gpointer data)
1244 struct dlgparam *dp = (struct dlgparam *)data;
1245 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
1246 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1249 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
1253 * GtkEntry has a nasty habit of eating the Return key, which
1254 * is unhelpful since it doesn't actually _do_ anything with it
1255 * (it calls gtk_widget_activate, but our edit boxes never need
1256 * activating). So I catch Return before GtkEntry sees it, and
1257 * pass it straight on to the parent widget. Effect: hitting
1258 * Return in an edit box will now activate the default button
1259 * in the dialog just like it will everywhere else.
1261 GtkWidget *parent = gtk_widget_get_parent(widget);
1262 if (event->keyval == GDK_KEY_Return && parent != NULL) {
1263 gboolean return_val;
1264 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
1265 g_signal_emit_by_name(G_OBJECT(parent), "key_press_event",
1266 event, &return_val);
1272 static void editbox_changed(GtkEditable *ed, gpointer data)
1274 struct dlgparam *dp = (struct dlgparam *)data;
1275 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1276 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1277 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1281 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1284 struct dlgparam *dp = (struct dlgparam *)data;
1285 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1286 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1290 #if !GTK_CHECK_VERSION(2,0,0)
1293 * GTK 1 list box event handlers.
1296 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1297 gpointer data, int multiple)
1299 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1301 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1302 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1303 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1304 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1306 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1307 * in a list box. So, if the list box is single-selection:
1309 * - if the list item in question isn't already selected,
1310 * we simply select it.
1311 * - otherwise, we find the next one (or next
1312 * however-far-away) in whichever direction we're going,
1314 * + in this case, we must also fiddle with the
1315 * scrollbar to ensure the newly selected item is
1318 * If it's multiple-selection, we do all of the above
1319 * except actually selecting anything, so we move the focus
1320 * and fiddle the scrollbar to follow it.
1322 GtkWidget *list = item->parent;
1324 g_signal_stop_emission_by_name(G_OBJECT(item), "key_press_event");
1327 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1328 gtk_list_select_child(GTK_LIST(list), item);
1331 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1332 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1335 (event->keyval==GDK_Page_Down ||
1336 event->keyval==GDK_KP_Page_Down ||
1337 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1340 GList *children, *chead;
1342 chead = children = gtk_container_children(GTK_CONTAINER(list));
1344 n = g_list_length(children);
1348 * Figure out how many list items to a screenful,
1349 * and adjust the step appropriately.
1351 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1352 step--; /* go by one less than that */
1356 while (children != NULL) {
1357 if (item == children->data)
1359 children = children->next;
1364 if (direction < 0 && i > 0)
1365 children = children->prev, i--;
1366 else if (direction > 0 && i < n-1)
1367 children = children->next, i++;
1371 if (children && children->data) {
1373 gtk_list_select_child(GTK_LIST(list),
1374 GTK_WIDGET(children->data));
1375 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1376 gtk_adjustment_clamp_page
1378 adj->lower + (adj->upper-adj->lower) * i / n,
1379 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1390 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1393 return listitem_key(item, event, data, FALSE);
1396 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1399 return listitem_key(item, event, data, TRUE);
1402 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1405 struct dlgparam *dp = (struct dlgparam *)data;
1406 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1407 switch (event->type) {
1409 case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1410 case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1411 case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1416 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1419 struct dlgparam *dp = (struct dlgparam *)data;
1420 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1421 if (uc->nclicks>1) {
1422 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1428 static void list_selchange(GtkList *list, gpointer data)
1430 struct dlgparam *dp = (struct dlgparam *)data;
1431 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1433 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1436 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1438 int index = dlg_listbox_index(uc->ctrl, dp);
1439 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1443 (index == 0 && direction < 0) ||
1444 (index == g_list_length(children)-1 && direction > 0)) {
1449 child = g_list_nth_data(children, index);
1450 gtk_widget_ref(child);
1451 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1452 g_list_free(children);
1455 children = g_list_append(children, child);
1456 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1457 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1458 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1461 static void draglist_up(GtkButton *button, gpointer data)
1463 struct dlgparam *dp = (struct dlgparam *)data;
1464 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1465 draglist_move(dp, uc, -1);
1468 static void draglist_down(GtkButton *button, gpointer data)
1470 struct dlgparam *dp = (struct dlgparam *)data;
1471 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1472 draglist_move(dp, uc, +1);
1475 #else /* !GTK_CHECK_VERSION(2,0,0) */
1478 * GTK 2 list box event handlers.
1481 static void listbox_doubleclick(GtkTreeView *treeview, GtkTreePath *path,
1482 GtkTreeViewColumn *column, gpointer data)
1484 struct dlgparam *dp = (struct dlgparam *)data;
1485 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(treeview));
1487 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1490 static void listbox_selchange(GtkTreeSelection *treeselection,
1493 struct dlgparam *dp = (struct dlgparam *)data;
1494 GtkTreeView *tree = gtk_tree_selection_get_tree_view(treeselection);
1495 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1497 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1500 struct draglist_valchange_ctx {
1502 struct dlgparam *dp;
1505 static gboolean draglist_valchange(gpointer data)
1507 struct draglist_valchange_ctx *ctx =
1508 (struct draglist_valchange_ctx *)data;
1510 ctx->uc->ctrl->generic.handler(ctx->uc->ctrl, ctx->dp,
1511 ctx->dp->data, EVENT_VALCHANGE);
1518 static void listbox_reorder(GtkTreeModel *treemodel, GtkTreePath *path,
1519 GtkTreeIter *iter, gpointer data)
1521 struct dlgparam *dp = (struct dlgparam *)data;
1525 if (dp->flags & FLAG_UPDATING_LISTBOX)
1526 return; /* not a user drag operation */
1528 tree = g_object_get_data(G_OBJECT(treemodel), "user-data");
1529 uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1532 * We should cause EVENT_VALCHANGE on the list box, now
1533 * that its rows have been reordered. However, the GTK 2
1534 * docs say that at the point this signal is received the
1535 * new row might not have actually been filled in yet.
1537 * (So what smegging use is it then, eh? Don't suppose it
1538 * occurred to you at any point that letting the
1539 * application know _after_ the reordering was compelete
1540 * might be helpful to someone?)
1542 * To get round this, I schedule an idle function, which I
1543 * hope won't be called until the main event loop is
1544 * re-entered after the drag-and-drop handler has finished
1545 * furtling with the list store.
1547 struct draglist_valchange_ctx *ctx =
1548 snew(struct draglist_valchange_ctx);
1551 g_idle_add(draglist_valchange, ctx);
1555 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1557 #if !GTK_CHECK_VERSION(2,4,0)
1559 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1561 struct dlgparam *dp = (struct dlgparam *)data;
1562 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1563 gpointer optmenu = g_object_get_data(G_OBJECT(menushell), "user-data");
1564 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1565 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1570 static void droplist_selchange(GtkComboBox *combo, gpointer data)
1572 struct dlgparam *dp = (struct dlgparam *)data;
1573 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(combo));
1575 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1578 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1580 #ifdef USE_GTK_FILE_CHOOSER_DIALOG
1581 static void filechoose_response(GtkDialog *dialog, gint response,
1584 /* struct dlgparam *dp = (struct dlgparam *)data; */
1585 struct uctrl *uc = g_object_get_data(G_OBJECT(dialog), "user-data");
1586 if (response == GTK_RESPONSE_ACCEPT) {
1587 gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1588 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1591 gtk_widget_destroy(GTK_WIDGET(dialog));
1594 static void filesel_ok(GtkButton *button, gpointer data)
1596 /* struct dlgparam *dp = (struct dlgparam *)data; */
1597 gpointer filesel = g_object_get_data(G_OBJECT(button), "user-data");
1598 struct uctrl *uc = g_object_get_data(G_OBJECT(filesel), "user-data");
1599 const char *name = gtk_file_selection_get_filename
1600 (GTK_FILE_SELECTION(filesel));
1601 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1605 static void fontsel_ok(GtkButton *button, gpointer data)
1607 /* struct dlgparam *dp = (struct dlgparam *)data; */
1609 #if !GTK_CHECK_VERSION(2,0,0)
1611 gpointer fontsel = g_object_get_data(G_OBJECT(button), "user-data");
1612 struct uctrl *uc = g_object_get_data(G_OBJECT(fontsel), "user-data");
1613 const char *name = gtk_font_selection_dialog_get_font_name
1614 (GTK_FONT_SELECTION_DIALOG(fontsel));
1615 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1619 unifontsel *fontsel = (unifontsel *)g_object_get_data
1620 (G_OBJECT(button), "user-data");
1621 struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1622 char *name = unifontsel_get_name(fontsel);
1623 assert(name); /* should always be ok after OK pressed */
1624 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1630 static void coloursel_ok(GtkButton *button, gpointer data)
1632 struct dlgparam *dp = (struct dlgparam *)data;
1633 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1634 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1635 GtkColorSelection *cs = GTK_COLOR_SELECTION
1636 (gtk_color_selection_dialog_get_color_selection
1637 (GTK_COLOR_SELECTION_DIALOG(coloursel)));
1638 #if GTK_CHECK_VERSION(2,0,0)
1641 gtk_color_selection_get_current_color(cs, &col);
1642 dp->coloursel_result.r = col.red / 0x0100;
1643 dp->coloursel_result.g = col.green / 0x0100;
1644 dp->coloursel_result.b = col.blue / 0x0100;
1649 gtk_color_selection_get_color(cs, cvals);
1650 dp->coloursel_result.r = (int) (255 * cvals[0]);
1651 dp->coloursel_result.g = (int) (255 * cvals[1]);
1652 dp->coloursel_result.b = (int) (255 * cvals[2]);
1655 dp->coloursel_result.ok = TRUE;
1656 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1659 static void coloursel_cancel(GtkButton *button, gpointer data)
1661 struct dlgparam *dp = (struct dlgparam *)data;
1662 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1663 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1664 dp->coloursel_result.ok = FALSE;
1665 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1668 static void filefont_clicked(GtkButton *button, gpointer data)
1670 struct dlgparam *dp = (struct dlgparam *)data;
1671 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1673 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1674 #ifdef USE_GTK_FILE_CHOOSER_DIALOG
1675 GtkWidget *filechoose = gtk_file_chooser_dialog_new
1676 (uc->ctrl->fileselect.title, GTK_WINDOW(dp->window),
1677 (uc->ctrl->fileselect.for_writing ?
1678 GTK_FILE_CHOOSER_ACTION_SAVE :
1679 GTK_FILE_CHOOSER_ACTION_OPEN),
1680 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1681 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1682 (const gchar *)NULL);
1683 gtk_window_set_modal(GTK_WINDOW(filechoose), TRUE);
1684 g_object_set_data(G_OBJECT(filechoose), "user-data", (gpointer)uc);
1685 g_signal_connect(G_OBJECT(filechoose), "response",
1686 G_CALLBACK(filechoose_response), (gpointer)dp);
1687 gtk_widget_show(filechoose);
1689 GtkWidget *filesel =
1690 gtk_file_selection_new(uc->ctrl->fileselect.title);
1691 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1693 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1695 g_object_set_data(G_OBJECT(filesel), "user-data", (gpointer)uc);
1697 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1698 G_CALLBACK(filesel_ok), (gpointer)dp);
1699 g_signal_connect_swapped
1700 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1701 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1702 g_signal_connect_swapped
1703 (G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1704 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1705 gtk_widget_show(filesel);
1709 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1710 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1712 #if !GTK_CHECK_VERSION(2,0,0)
1715 * Use the GTK 1 standard font selector.
1718 gchar *spacings[] = { "c", "m", NULL };
1719 GtkWidget *fontsel =
1720 gtk_font_selection_dialog_new("Select a font");
1721 gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1722 gtk_font_selection_dialog_set_filter
1723 (GTK_FONT_SELECTION_DIALOG(fontsel),
1724 GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1725 NULL, NULL, NULL, NULL, spacings, NULL);
1726 if (!gtk_font_selection_dialog_set_font_name
1727 (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1729 * If the font name wasn't found as it was, try opening
1730 * it and extracting its FONT property. This should
1731 * have the effect of mapping short aliases into true
1734 GdkFont *font = gdk_font_load(fontname);
1736 XFontStruct *xfs = GDK_FONT_XFONT(font);
1737 Display *disp = GDK_FONT_XDISPLAY(font);
1738 Atom fontprop = XInternAtom(disp, "FONT", False);
1741 if (XGetFontProperty(xfs, fontprop, &ret)) {
1742 char *name = XGetAtomName(disp, (Atom)ret);
1744 gtk_font_selection_dialog_set_font_name
1745 (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1747 gdk_font_unref(font);
1751 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1752 "user-data", (gpointer)fontsel);
1753 g_object_set_data(G_OBJECT(fontsel), "user-data", (gpointer)uc);
1755 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1756 "clicked", G_CALLBACK(fontsel_ok), (gpointer)dp);
1757 g_signal_connect_swapped
1758 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1759 "clicked", G_CALLBACK(gtk_widget_destroy),
1761 g_signal_connect_swapped
1762 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1763 "clicked", G_CALLBACK(gtk_widget_destroy),
1765 gtk_widget_show(fontsel);
1767 #else /* !GTK_CHECK_VERSION(2,0,0) */
1770 * Use the unifontsel code provided in gtkfont.c.
1773 unifontsel *fontsel = unifontsel_new("Select a font");
1775 gtk_window_set_modal(fontsel->window, TRUE);
1776 unifontsel_set_name(fontsel, fontname);
1778 g_object_set_data(G_OBJECT(fontsel->ok_button),
1779 "user-data", (gpointer)fontsel);
1780 fontsel->user_data = uc;
1781 g_signal_connect(G_OBJECT(fontsel->ok_button), "clicked",
1782 G_CALLBACK(fontsel_ok), (gpointer)dp);
1783 g_signal_connect_swapped(G_OBJECT(fontsel->ok_button), "clicked",
1784 G_CALLBACK(unifontsel_destroy),
1786 g_signal_connect_swapped(G_OBJECT(fontsel->cancel_button),"clicked",
1787 G_CALLBACK(unifontsel_destroy),
1790 gtk_widget_show(GTK_WIDGET(fontsel->window));
1792 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1797 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1800 struct dlgparam *dp = (struct dlgparam *)data;
1801 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1803 gtk_widget_set_size_request(uc->text, alloc->width, -1);
1804 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1805 g_signal_handler_disconnect(G_OBJECT(uc->text), uc->textsig);
1808 /* ----------------------------------------------------------------------
1809 * This function does the main layout work: it reads a controlset,
1810 * it creates the relevant GTK controls, and returns a GtkWidget
1811 * containing the result. (This widget might be a title of some
1812 * sort, it might be a Columns containing many controls, or it
1813 * might be a GtkFrame containing a Columns; whatever it is, it's
1814 * definitely a GtkWidget and should probably be added to a
1817 * `win' is required for setting the default button. If it is
1818 * non-NULL, all buttons created will be default-capable (so they
1819 * have extra space round them for the default highlight).
1821 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1822 struct controlset *s, GtkWindow *win)
1828 if (!s->boxname && s->boxtitle) {
1829 /* This controlset is a panel title. */
1830 return gtk_label_new(s->boxtitle);
1834 * Otherwise, we expect to be laying out actual controls, so
1835 * we'll start by creating a Columns for the purpose.
1837 cols = COLUMNS(columns_new(4));
1838 ret = GTK_WIDGET(cols);
1839 gtk_widget_show(ret);
1842 * Create a containing frame if we have a box name.
1845 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1846 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1847 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1848 gtk_widget_show(ret);
1852 * Now iterate through the controls themselves, create them,
1853 * and add them to the Columns.
1855 for (i = 0; i < s->ncontrols; i++) {
1856 union control *ctrl = s->ctrls[i];
1859 GtkWidget *w = NULL;
1861 switch (ctrl->generic.type) {
1864 static const int simplecols[1] = { 100 };
1865 columns_set_cols(cols, ctrl->columns.ncols,
1866 (ctrl->columns.percentages ?
1867 ctrl->columns.percentages : simplecols));
1869 continue; /* no actual control created */
1872 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1874 columns_taborder_last(cols, uc->toplevel);
1876 continue; /* no actual control created */
1879 uc = snew(struct uctrl);
1883 #if !GTK_CHECK_VERSION(2,4,0)
1884 uc->list = uc->menu = uc->optmenu = NULL;
1888 #if GTK_CHECK_VERSION(2,0,0)
1889 uc->treeview = NULL;
1890 uc->listmodel = NULL;
1892 uc->button = uc->text = NULL;
1896 switch (ctrl->generic.type) {
1898 w = gtk_button_new_with_label(ctrl->generic.label);
1900 gtk_widget_set_can_default(w, TRUE);
1901 if (ctrl->button.isdefault)
1902 gtk_window_set_default(win, w);
1903 if (ctrl->button.iscancel)
1904 dp->cancelbutton = w;
1906 g_signal_connect(G_OBJECT(w), "clicked",
1907 G_CALLBACK(button_clicked), dp);
1908 g_signal_connect(G_OBJECT(w), "focus_in_event",
1909 G_CALLBACK(widget_focus), dp);
1910 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1911 ctrl->button.shortcut, SHORTCUT_UCTRL, uc);
1914 w = gtk_check_button_new_with_label(ctrl->generic.label);
1915 g_signal_connect(G_OBJECT(w), "toggled",
1916 G_CALLBACK(button_toggled), dp);
1917 g_signal_connect(G_OBJECT(w), "focus_in_event",
1918 G_CALLBACK(widget_focus), dp);
1919 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1920 ctrl->checkbox.shortcut, SHORTCUT_UCTRL, uc);
1925 * Radio buttons get to go inside their own Columns, no
1929 gint i, *percentages;
1933 if (ctrl->generic.label) {
1934 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1935 columns_add(COLUMNS(w), label, 0, 1);
1936 columns_force_left_align(COLUMNS(w), label);
1937 gtk_widget_show(label);
1938 shortcut_add(scs, label, ctrl->radio.shortcut,
1939 SHORTCUT_UCTRL, uc);
1942 percentages = g_new(gint, ctrl->radio.ncolumns);
1943 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1945 ((100 * (i+1) / ctrl->radio.ncolumns) -
1946 100 * i / ctrl->radio.ncolumns);
1948 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1950 g_free(percentages);
1953 uc->nbuttons = ctrl->radio.nbuttons;
1954 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1956 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1960 b = (gtk_radio_button_new_with_label
1961 (group, ctrl->radio.buttons[i]));
1963 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(b));
1964 colstart = i % ctrl->radio.ncolumns;
1965 columns_add(COLUMNS(w), b, colstart,
1966 (i == ctrl->radio.nbuttons-1 ?
1967 ctrl->radio.ncolumns - colstart : 1));
1968 columns_force_left_align(COLUMNS(w), b);
1970 g_signal_connect(G_OBJECT(b), "toggled",
1971 G_CALLBACK(button_toggled), dp);
1972 g_signal_connect(G_OBJECT(b), "focus_in_event",
1973 G_CALLBACK(widget_focus), dp);
1974 if (ctrl->radio.shortcuts) {
1975 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(b)),
1976 ctrl->radio.shortcuts[i],
1977 SHORTCUT_UCTRL, uc);
1985 GtkWidget *signalobject;
1987 if (ctrl->editbox.has_list) {
1988 #if !GTK_CHECK_VERSION(2,4,0)
1992 w = gtk_combo_new();
1993 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1994 uc->entry = GTK_COMBO(w)->entry;
1995 uc->list = GTK_COMBO(w)->list;
1996 signalobject = uc->entry;
2001 uc->listmodel = gtk_list_store_new(2, G_TYPE_INT,
2003 w = gtk_combo_box_new_with_model_and_entry
2004 (GTK_TREE_MODEL(uc->listmodel));
2005 /* We cannot support password combo boxes. */
2006 assert(!ctrl->editbox.password);
2008 signalobject = uc->combo;
2011 w = gtk_entry_new();
2012 if (ctrl->editbox.password)
2013 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
2018 g_signal_connect(G_OBJECT(signalobject), "changed",
2019 G_CALLBACK(editbox_changed), dp);
2020 g_signal_connect(G_OBJECT(signalobject), "key_press_event",
2021 G_CALLBACK(editbox_key), dp);
2022 g_signal_connect(G_OBJECT(signalobject), "focus_in_event",
2023 G_CALLBACK(widget_focus), dp);
2024 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
2025 G_CALLBACK(editbox_lostfocus), dp);
2026 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
2027 G_CALLBACK(editbox_lostfocus), dp);
2029 * Edit boxes, for some strange reason, have a minimum
2030 * width of 150 in GTK 1.2. We don't want this - we'd
2031 * rather the edit boxes acquired their natural width
2032 * from the column layout of the rest of the box.
2034 * Also, while we're here, we'll squirrel away the
2035 * edit box height so we can use that to centre its
2036 * label vertically beside it.
2038 gtk_widget_size_request(w, &req);
2039 gtk_widget_set_size_request(w, 10, req.height);
2041 if (ctrl->generic.label) {
2042 GtkWidget *label, *container;
2044 label = gtk_label_new(ctrl->generic.label);
2046 shortcut_add(scs, label, ctrl->editbox.shortcut,
2047 SHORTCUT_FOCUS, uc->entry);
2049 container = columns_new(4);
2050 if (ctrl->editbox.percentwidth == 100) {
2051 columns_add(COLUMNS(container), label, 0, 1);
2052 columns_force_left_align(COLUMNS(container), label);
2053 columns_add(COLUMNS(container), w, 0, 1);
2055 gint percentages[2];
2056 percentages[1] = ctrl->editbox.percentwidth;
2057 percentages[0] = 100 - ctrl->editbox.percentwidth;
2058 columns_set_cols(COLUMNS(container), 2, percentages);
2059 columns_add(COLUMNS(container), label, 0, 1);
2060 columns_force_left_align(COLUMNS(container), label);
2061 columns_add(COLUMNS(container), w, 1, 1);
2062 /* Centre the label vertically. */
2063 gtk_widget_set_size_request(label, -1, req.height);
2064 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2066 gtk_widget_show(label);
2074 case CTRL_FILESELECT:
2075 case CTRL_FONTSELECT:
2079 const char *browsebtn =
2080 (ctrl->generic.type == CTRL_FILESELECT ?
2081 "Browse..." : "Change...");
2083 gint percentages[] = { 75, 25 };
2085 columns_set_cols(COLUMNS(w), 2, percentages);
2087 if (ctrl->generic.label) {
2088 ww = gtk_label_new(ctrl->generic.label);
2089 columns_add(COLUMNS(w), ww, 0, 2);
2090 columns_force_left_align(COLUMNS(w), ww);
2091 gtk_widget_show(ww);
2092 shortcut_add(scs, ww,
2093 (ctrl->generic.type == CTRL_FILESELECT ?
2094 ctrl->fileselect.shortcut :
2095 ctrl->fontselect.shortcut),
2096 SHORTCUT_UCTRL, uc);
2100 uc->entry = ww = gtk_entry_new();
2101 gtk_widget_size_request(ww, &req);
2102 gtk_widget_set_size_request(ww, 10, req.height);
2103 columns_add(COLUMNS(w), ww, 0, 1);
2104 gtk_widget_show(ww);
2106 uc->button = ww = gtk_button_new_with_label(browsebtn);
2107 columns_add(COLUMNS(w), ww, 1, 1);
2108 gtk_widget_show(ww);
2110 g_signal_connect(G_OBJECT(uc->entry), "key_press_event",
2111 G_CALLBACK(editbox_key), dp);
2113 g_signal_connect(G_OBJECT(uc->entry), "changed",
2114 G_CALLBACK(editbox_changed), dp);
2115 g_signal_connect(G_OBJECT(uc->entry), "focus_in_event",
2116 G_CALLBACK(widget_focus), dp);
2117 g_signal_connect(G_OBJECT(uc->button), "focus_in_event",
2118 G_CALLBACK(widget_focus), dp);
2119 g_signal_connect(G_OBJECT(ww), "clicked",
2120 G_CALLBACK(filefont_clicked), dp);
2125 #if GTK_CHECK_VERSION(2,0,0)
2127 * First construct the list data store, with the right
2128 * number of columns.
2130 # if !GTK_CHECK_VERSION(2,4,0)
2131 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2132 * because combo boxes are still done the old GTK1 way.) */
2133 if (ctrl->listbox.height > 0)
2140 cols = ctrl->listbox.ncols;
2141 cols = cols ? cols : 1;
2142 types = snewn(1 + cols, GType);
2144 types[0] = G_TYPE_INT;
2145 for (i = 0; i < cols; i++)
2146 types[i+1] = G_TYPE_STRING;
2148 uc->listmodel = gtk_list_store_newv(1 + cols, types);
2155 * See if it's a drop-down list (non-editable combo
2158 if (ctrl->listbox.height == 0) {
2159 #if !GTK_CHECK_VERSION(2,4,0)
2161 * GTK1 and early-GTK2 option-menu style of
2164 uc->optmenu = w = gtk_option_menu_new();
2165 uc->menu = gtk_menu_new();
2166 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
2167 g_object_set_data(G_OBJECT(uc->menu), "user-data",
2168 (gpointer)uc->optmenu);
2169 g_signal_connect(G_OBJECT(uc->optmenu), "focus_in_event",
2170 G_CALLBACK(widget_focus), dp);
2173 * Late-GTK2 style using a GtkComboBox.
2175 GtkCellRenderer *cr;
2178 * Create a non-editable GtkComboBox (that is, not
2179 * its subclass GtkComboBoxEntry).
2181 w = gtk_combo_box_new_with_model
2182 (GTK_TREE_MODEL(uc->listmodel));
2186 * Tell it how to render a list item (i.e. which
2187 * column to look at in the list model).
2189 cr = gtk_cell_renderer_text_new();
2190 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), cr, TRUE);
2191 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), cr,
2195 * And tell it to notify us when the selection
2198 g_signal_connect(G_OBJECT(w), "changed",
2199 G_CALLBACK(droplist_selchange), dp);
2202 #if !GTK_CHECK_VERSION(2,0,0)
2204 * GTK1-style full list box.
2206 uc->list = gtk_list_new();
2207 if (ctrl->listbox.multisel == 2) {
2208 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2209 GTK_SELECTION_EXTENDED);
2210 } else if (ctrl->listbox.multisel == 1) {
2211 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2212 GTK_SELECTION_MULTIPLE);
2214 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2215 GTK_SELECTION_SINGLE);
2217 w = gtk_scrolled_window_new(NULL, NULL);
2218 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
2220 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
2222 GTK_POLICY_AUTOMATIC);
2223 uc->adj = gtk_scrolled_window_get_vadjustment
2224 (GTK_SCROLLED_WINDOW(w));
2226 gtk_widget_show(uc->list);
2227 g_signal_connect(G_OBJECT(uc->list), "selection-changed",
2228 G_CALLBACK(list_selchange), dp);
2229 g_signal_connect(G_OBJECT(uc->list), "focus_in_event",
2230 G_CALLBACK(widget_focus), dp);
2233 * Adjust the height of the scrolled window to the
2234 * minimum given by the height parameter.
2236 * This piece of guesswork is a horrid hack based
2237 * on looking inside the GTK 1.2 sources
2238 * (specifically gtkviewport.c, which appears to be
2239 * the widget which provides the border around the
2240 * scrolling area). Anyone lets me know how I can
2241 * do this in a way which isn't at risk from GTK
2242 * upgrades, I'd be grateful.
2246 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
2247 gtk_widget_set_size_request(w, 10,
2248 2*edge + (ctrl->listbox.height *
2249 get_listitemheight(w)));
2252 if (ctrl->listbox.draglist) {
2254 * GTK doesn't appear to make it easy to
2255 * implement a proper draggable list; so
2256 * instead I'm just going to have to put an Up
2257 * and a Down button to the right of the actual
2258 * list box. Ah well.
2260 GtkWidget *cols, *button;
2261 static const gint percentages[2] = { 80, 20 };
2263 cols = columns_new(4);
2264 columns_set_cols(COLUMNS(cols), 2, percentages);
2265 columns_add(COLUMNS(cols), w, 0, 1);
2267 button = gtk_button_new_with_label("Up");
2268 columns_add(COLUMNS(cols), button, 1, 1);
2269 gtk_widget_show(button);
2270 g_signal_connect(G_OBJECT(button), "clicked",
2271 G_CALLBACK(draglist_up), dp);
2272 g_signal_connect(G_OBJECT(button), "focus_in_event",
2273 G_CALLBACK(widget_focus), dp);
2274 button = gtk_button_new_with_label("Down");
2275 columns_add(COLUMNS(cols), button, 1, 1);
2276 gtk_widget_show(button);
2277 g_signal_connect(G_OBJECT(button), "clicked",
2278 G_CALLBACK(draglist_down), dp);
2279 g_signal_connect(G_OBJECT(button), "focus_in_event",
2280 G_CALLBACK(widget_focus), dp);
2286 * GTK2 treeview-based full list box.
2288 GtkTreeSelection *sel;
2291 * Create the list box itself, its columns, and
2292 * its containing scrolled window.
2294 w = gtk_tree_view_new_with_model
2295 (GTK_TREE_MODEL(uc->listmodel));
2296 g_object_set_data(G_OBJECT(uc->listmodel), "user-data",
2298 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2299 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
2300 gtk_tree_selection_set_mode
2301 (sel, ctrl->listbox.multisel ? GTK_SELECTION_MULTIPLE :
2302 GTK_SELECTION_SINGLE);
2304 g_signal_connect(G_OBJECT(w), "row-activated",
2305 G_CALLBACK(listbox_doubleclick), dp);
2306 g_signal_connect(G_OBJECT(sel), "changed",
2307 G_CALLBACK(listbox_selchange), dp);
2309 if (ctrl->listbox.draglist) {
2310 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w), TRUE);
2311 g_signal_connect(G_OBJECT(uc->listmodel), "row-inserted",
2312 G_CALLBACK(listbox_reorder), dp);
2319 cols = ctrl->listbox.ncols;
2320 cols = cols ? cols : 1;
2321 for (i = 0; i < cols; i++) {
2322 GtkTreeViewColumn *column;
2323 GtkCellRenderer *cellrend;
2325 * It appears that GTK 2 doesn't leave us any
2326 * particularly sensible way to honour the
2327 * "percentages" specification in the ctrl
2330 cellrend = gtk_cell_renderer_text_new();
2331 if (!ctrl->listbox.hscroll) {
2332 g_object_set(G_OBJECT(cellrend),
2333 "ellipsize", PANGO_ELLIPSIZE_END,
2334 "ellipsize-set", TRUE,
2337 column = gtk_tree_view_column_new_with_attributes
2338 ("heading", cellrend, "text", i+1, (char *)NULL);
2339 gtk_tree_view_column_set_sizing
2340 (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2341 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2348 scroll = gtk_scrolled_window_new(NULL, NULL);
2349 gtk_scrolled_window_set_shadow_type
2350 (GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
2352 gtk_container_add(GTK_CONTAINER(scroll), w);
2353 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2354 GTK_POLICY_AUTOMATIC,
2356 gtk_widget_set_size_request
2358 ctrl->listbox.height * get_listitemheight(w));
2365 if (ctrl->generic.label) {
2366 GtkWidget *label, *container;
2369 label = gtk_label_new(ctrl->generic.label);
2371 shortcut_add(scs, label, ctrl->listbox.shortcut,
2374 container = columns_new(4);
2375 if (ctrl->listbox.percentwidth == 100) {
2376 columns_add(COLUMNS(container), label, 0, 1);
2377 columns_force_left_align(COLUMNS(container), label);
2378 columns_add(COLUMNS(container), w, 0, 1);
2380 gint percentages[2];
2381 percentages[1] = ctrl->listbox.percentwidth;
2382 percentages[0] = 100 - ctrl->listbox.percentwidth;
2383 columns_set_cols(COLUMNS(container), 2, percentages);
2384 columns_add(COLUMNS(container), label, 0, 1);
2385 columns_force_left_align(COLUMNS(container), label);
2386 columns_add(COLUMNS(container), w, 1, 1);
2387 /* Centre the label vertically. */
2388 gtk_widget_size_request(w, &req);
2389 gtk_widget_set_size_request(label, -1, req.height);
2390 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2392 gtk_widget_show(label);
2402 * Wrapping text widgets don't sit well with the GTK
2403 * layout model, in which widgets state a minimum size
2404 * and the whole window then adjusts to the smallest
2405 * size it can sensibly take given its contents. A
2406 * wrapping text widget _has_ no clear minimum size;
2407 * instead it has a range of possibilities. It can be
2408 * one line deep but 2000 wide, or two lines deep and
2409 * 1000 pixels, or three by 867, or four by 500 and so
2410 * on. It can be as short as you like provided you
2411 * don't mind it being wide, or as narrow as you like
2412 * provided you don't mind it being tall.
2414 * Therefore, it fits very badly into the layout model.
2415 * Hence the only thing to do is pick a width and let
2416 * it choose its own number of lines. To do this I'm
2417 * going to cheat a little. All new wrapping text
2418 * widgets will be created with a minimal text content
2419 * "X"; then, after the rest of the dialog box is set
2420 * up and its size calculated, the text widgets will be
2421 * told their width and given their real text, which
2422 * will cause the size to be recomputed in the y
2423 * direction (because many of them will expand to more
2426 uc->text = w = gtk_label_new("X");
2427 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
2428 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
2430 g_signal_connect(G_OBJECT(w), "size-allocate",
2431 G_CALLBACK(label_sizealloc), dp);
2437 columns_add(cols, w,
2438 COLUMN_START(ctrl->generic.column),
2439 COLUMN_SPAN(ctrl->generic.column));
2441 columns_force_left_align(cols, w);
2445 dlg_add_uctrl(dp, uc);
2452 struct dlgparam *dp;
2453 GtkNotebook *panels;
2455 #if !GTK_CHECK_VERSION(2,0,0)
2456 GtkWidget *treeitem;
2459 GtkTreePath *treepath;
2461 struct Shortcuts shortcuts;
2464 #if GTK_CHECK_VERSION(2,0,0)
2465 static void treeselection_changed(GtkTreeSelection *treeselection,
2468 struct selparam *sps = (struct selparam *)data, *sp;
2469 GtkTreeModel *treemodel;
2470 GtkTreeIter treeiter;
2474 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2477 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
2480 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2481 gtk_notebook_set_current_page(sp->panels, page_num);
2483 dlg_refresh(NULL, sp->dp);
2485 sp->dp->shortcuts = &sp->shortcuts;
2488 static void treeitem_sel(GtkItem *item, gpointer data)
2490 struct selparam *sp = (struct selparam *)data;
2493 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2494 gtk_notebook_set_page(sp->panels, page_num);
2496 dlg_refresh(NULL, sp->dp);
2498 sp->dp->shortcuts = &sp->shortcuts;
2499 sp->dp->currtreeitem = sp->treeitem;
2503 static void window_destroy(GtkWidget *widget, gpointer data)
2508 #if !GTK_CHECK_VERSION(2,0,0)
2509 static int tree_grab_focus(struct dlgparam *dp)
2514 * See if any of the treeitems has the focus.
2517 for (i = 0; i < dp->ntreeitems; i++)
2518 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
2526 gtk_widget_grab_focus(dp->currtreeitem);
2531 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
2534 struct dlgparam *dp = (struct dlgparam *)data;
2536 g_signal_stop_emission_by_name(G_OBJECT(container), "focus");
2538 * If there's a focused treeitem, we return FALSE to cause the
2539 * focus to move on to some totally other control. If not, we
2540 * focus the selected one.
2542 return tree_grab_focus(dp);
2546 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2548 struct dlgparam *dp = (struct dlgparam *)data;
2550 if (event->keyval == GDK_KEY_Escape && dp->cancelbutton) {
2551 g_signal_emit_by_name(G_OBJECT(dp->cancelbutton), "clicked");
2555 if ((event->state & GDK_MOD1_MASK) &&
2556 (unsigned char)event->string[0] > 0 &&
2557 (unsigned char)event->string[0] <= 127) {
2558 int schr = (unsigned char)event->string[0];
2559 struct Shortcut *sc = &dp->shortcuts->sc[schr];
2561 switch (sc->action) {
2563 #if GTK_CHECK_VERSION(2,0,0)
2564 gtk_widget_grab_focus(sc->widget);
2566 tree_grab_focus(dp);
2569 case SHORTCUT_FOCUS:
2570 gtk_widget_grab_focus(sc->widget);
2572 case SHORTCUT_UCTRL:
2574 * We must do something sensible with a uctrl.
2575 * Precisely what this is depends on the type of
2578 switch (sc->uc->ctrl->generic.type) {
2581 /* Check boxes and buttons get the focus _and_ get toggled. */
2582 gtk_widget_grab_focus(sc->uc->toplevel);
2583 g_signal_emit_by_name(G_OBJECT(sc->uc->toplevel), "clicked");
2585 case CTRL_FILESELECT:
2586 case CTRL_FONTSELECT:
2587 /* File/font selectors have their buttons pressed (ooer),
2588 * and focus transferred to the edit box. */
2589 g_signal_emit_by_name(G_OBJECT(sc->uc->button), "clicked");
2590 gtk_widget_grab_focus(sc->uc->entry);
2594 * Radio buttons are fun, because they have
2595 * multiple shortcuts. We must find whether the
2596 * activated shortcut is the shortcut for the whole
2597 * group, or for a particular button. In the former
2598 * case, we find the currently selected button and
2599 * focus it; in the latter, we focus-and-click the
2600 * button whose shortcut was pressed.
2602 if (schr == sc->uc->ctrl->radio.shortcut) {
2604 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2605 if (gtk_toggle_button_get_active
2606 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
2607 gtk_widget_grab_focus(sc->uc->buttons[i]);
2609 } else if (sc->uc->ctrl->radio.shortcuts) {
2611 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2612 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
2613 gtk_widget_grab_focus(sc->uc->buttons[i]);
2614 g_signal_emit_by_name
2615 (G_OBJECT(sc->uc->buttons[i]), "clicked");
2621 #if !GTK_CHECK_VERSION(2,4,0)
2622 if (sc->uc->optmenu) {
2626 gtk_widget_grab_focus(sc->uc->optmenu);
2627 /* Option menus don't work using the "clicked" signal.
2628 * We need to manufacture a button press event :-/ */
2629 bev.type = GDK_BUTTON_PRESS;
2631 g_signal_emit_by_name(G_OBJECT(sc->uc->optmenu),
2632 "button_press_event",
2637 if (sc->uc->combo) {
2638 gtk_widget_grab_focus(sc->uc->combo);
2639 gtk_combo_box_popup(GTK_COMBO_BOX(sc->uc->combo));
2643 #if !GTK_CHECK_VERSION(2,0,0)
2646 * For GTK-1 style list boxes, we tell it to
2647 * focus one of its children, which appears to
2648 * do the Right Thing.
2650 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
2651 GTK_DIR_TAB_FORWARD);
2655 if (sc->uc->treeview) {
2656 gtk_widget_grab_focus(sc->uc->treeview);
2660 assert(!"We shouldn't get here");
2670 #if !GTK_CHECK_VERSION(2,0,0)
2671 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2673 struct dlgparam *dp = (struct dlgparam *)data;
2675 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2676 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2678 for (i = 0; i < dp->ntreeitems; i++)
2679 if (widget == dp->treeitems[i])
2681 if (i < dp->ntreeitems) {
2682 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2689 if (i < 0 || i >= dp->ntreeitems)
2690 break; /* nothing in that dir to select */
2692 * Determine if this tree item is visible.
2695 GtkWidget *w = dp->treeitems[i];
2697 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2698 if (!GTK_WIDGET_VISIBLE(w)) {
2705 j = i; /* got one */
2711 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2713 g_signal_emit_by_name(G_OBJECT(dp->treeitems[j]), "toggle");
2714 gtk_widget_grab_focus(dp->treeitems[j]);
2720 * It's nice for Left and Right to expand and collapse tree
2723 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2724 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2725 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2728 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2729 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2730 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2738 static void shortcut_highlight(GtkWidget *labelw, int chr)
2740 GtkLabel *label = GTK_LABEL(labelw);
2741 const gchar *currstr;
2745 #if !GTK_CHECK_VERSION(2,0,0)
2747 gchar *currstr_nonconst;
2748 gtk_label_get(label, &currstr_nonconst);
2749 currstr = currstr_nonconst;
2752 currstr = gtk_label_get_text(label);
2755 for (i = 0; currstr[i]; i++)
2756 if (tolower((unsigned char)currstr[i]) == chr) {
2759 pattern = dupprintf("%*s_", i, "");
2761 gtk_widget_size_request(GTK_WIDGET(label), &req);
2762 gtk_label_set_pattern(label, pattern);
2763 gtk_widget_set_size_request(GTK_WIDGET(label), -1, req.height);
2770 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2771 int chr, int action, void *ptr)
2773 if (chr == NO_SHORTCUT)
2776 chr = tolower((unsigned char)chr);
2778 assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2780 scs->sc[chr].action = action;
2782 if (action == SHORTCUT_FOCUS) {
2783 scs->sc[chr].uc = NULL;
2784 scs->sc[chr].widget = (GtkWidget *)ptr;
2786 scs->sc[chr].widget = NULL;
2787 scs->sc[chr].uc = (struct uctrl *)ptr;
2790 shortcut_highlight(labelw, chr);
2793 int get_listitemheight(GtkWidget *w)
2795 #if !GTK_CHECK_VERSION(2,0,0)
2796 GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2798 gtk_widget_size_request(listitem, &req);
2799 g_object_ref_sink(G_OBJECT(listitem));
2803 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
2804 gtk_cell_renderer_get_size(cr, w, NULL, NULL, NULL, NULL, &height);
2805 g_object_ref(G_OBJECT(cr));
2806 g_object_ref_sink(G_OBJECT(cr));
2807 g_object_unref(G_OBJECT(cr));
2812 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2814 #if !GTK_CHECK_VERSION(2,0,0)
2817 * In GTK 1, laying out the buttons at the bottom of the
2818 * configuration box is nice and easy, because a GtkDialog's
2819 * action_area is a GtkHBox which stretches to cover the full
2820 * width of the dialog. So we just put our Columns widget
2821 * straight into that hbox, and it ends up just where we want
2824 gtk_box_pack_start(GTK_BOX(dlg->action_area), w, TRUE, TRUE, 0);
2828 * In GTK 2, the action area is now a GtkHButtonBox and its
2829 * layout behaviour seems to be different: it doesn't stretch
2830 * to cover the full width of the window, but instead finds its
2831 * own preferred width and right-aligns that within the window.
2832 * This isn't what we want, because we have both left-aligned
2833 * and right-aligned buttons coming out of the above call to
2834 * layout_ctrls(), and right-aligning the whole thing will
2835 * result in the former being centred and looking weird.
2837 * So instead we abandon the dialog's action area completely:
2838 * we gtk_widget_hide() it in the below code, and we also call
2839 * gtk_dialog_set_has_separator() to remove the separator above
2840 * it. We then insert our own action area into the end of the
2841 * dialog's main vbox, and add our own separator above that.
2843 * (Ideally, if we were a native GTK app, we would use the
2844 * GtkHButtonBox's _own_ innate ability to support one set of
2845 * buttons being right-aligned and one left-aligned. But to do
2846 * that here, we would have to either (a) pick apart our cross-
2847 * platform layout structures and treat them specially for this
2848 * particular set of controls, which would be painful, or else
2849 * (b) develop a special and simpler cross-platform
2850 * representation for these particular controls, and introduce
2851 * special-case code into all the _other_ platforms to handle
2852 * it. Neither appeals. Therefore, I regretfully discard the
2853 * GTKHButtonBox and go it alone.)
2857 align = gtk_alignment_new(0, 0, 1, 1);
2858 gtk_container_add(GTK_CONTAINER(align), w);
2860 * The purpose of this GtkAlignment is to provide padding
2861 * around the buttons. The padding we use is twice the padding
2862 * used in our GtkColumns, because we nest two GtkColumns most
2863 * of the time (one separating the tree view from the main
2864 * controls, and another for the main controls themselves).
2866 #if GTK_CHECK_VERSION(2,4,0)
2867 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2869 gtk_widget_show(align);
2870 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2871 align, FALSE, TRUE, 0);
2872 w = gtk_hseparator_new();
2873 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2876 gtk_widget_hide(gtk_dialog_get_action_area(dlg));
2877 g_object_set(G_OBJECT(dlg), "has-separator", TRUE, (const char *)NULL);
2881 int do_config_box(const char *title, Conf *conf, int midsession,
2884 GtkWidget *window, *hbox, *vbox, *cols, *label,
2885 *tree, *treescroll, *panels, *panelvbox;
2886 int index, level, protocol;
2887 struct controlbox *ctrlbox;
2889 #if GTK_CHECK_VERSION(2,0,0)
2890 GtkTreeStore *treestore;
2891 GtkCellRenderer *treerenderer;
2892 GtkTreeViewColumn *treecolumn;
2893 GtkTreeSelection *treeselection;
2894 GtkTreeIter treeiterlevels[8];
2896 GtkTreeItem *treeitemlevels[8];
2897 GtkTree *treelevels[8];
2900 struct Shortcuts scs;
2902 struct selparam *selparams = NULL;
2903 int nselparams = 0, selparamsize = 0;
2907 for (index = 0; index < lenof(scs.sc); index++) {
2908 scs.sc[index].action = SHORTCUT_EMPTY;
2911 window = gtk_dialog_new();
2913 ctrlbox = ctrl_new_box();
2914 protocol = conf_get_int(conf, CONF_protocol);
2915 setup_config_box(ctrlbox, midsession, protocol, protcfginfo);
2916 unix_setup_config_box(ctrlbox, midsession, protocol);
2917 gtk_setup_config_box(ctrlbox, midsession, window);
2919 gtk_window_set_title(GTK_WINDOW(window), title);
2920 hbox = gtk_hbox_new(FALSE, 4);
2922 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
2923 hbox, TRUE, TRUE, 0);
2924 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2925 gtk_widget_show(hbox);
2926 vbox = gtk_vbox_new(FALSE, 4);
2927 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2928 gtk_widget_show(vbox);
2929 cols = columns_new(4);
2930 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2931 gtk_widget_show(cols);
2932 label = gtk_label_new("Category:");
2933 columns_add(COLUMNS(cols), label, 0, 1);
2934 columns_force_left_align(COLUMNS(cols), label);
2935 gtk_widget_show(label);
2936 treescroll = gtk_scrolled_window_new(NULL, NULL);
2937 #if GTK_CHECK_VERSION(2,0,0)
2938 treestore = gtk_tree_store_new
2939 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2940 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2941 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2942 treerenderer = gtk_cell_renderer_text_new();
2943 treecolumn = gtk_tree_view_column_new_with_attributes
2944 ("Label", treerenderer, "text", 0, NULL);
2945 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2946 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2947 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2948 gtk_container_add(GTK_CONTAINER(treescroll), tree);
2950 tree = gtk_tree_new();
2951 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2952 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2953 g_signal_connect(G_OBJECT(tree), "focus",
2954 G_CALLBACK(tree_focus), &dp);
2956 g_signal_connect(G_OBJECT(tree), "focus_in_event",
2957 G_CALLBACK(widget_focus), &dp);
2958 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2959 gtk_widget_show(treescroll);
2960 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2961 panels = gtk_notebook_new();
2962 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2963 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2964 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2965 gtk_widget_show(panels);
2970 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2971 struct controlset *s = ctrlbox->ctrlsets[index];
2974 if (!*s->pathname) {
2975 w = layout_ctrls(&dp, &scs, s, GTK_WINDOW(window));
2977 set_dialog_action_area(GTK_DIALOG(window), w);
2979 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2980 if (j != INT_MAX) { /* add to treeview, start new panel */
2982 #if GTK_CHECK_VERSION(2,0,0)
2983 GtkTreeIter treeiter;
2985 GtkWidget *treeitem;
2990 * We expect never to find an implicit path
2991 * component. For example, we expect never to see
2992 * A/B/C followed by A/D/E, because that would
2993 * _implicitly_ create A/D. All our path prefixes
2994 * are expected to contain actual controls and be
2995 * selectable in the treeview; so we would expect
2996 * to see A/D _explicitly_ before encountering
2999 assert(j == ctrl_path_elements(s->pathname) - 1);
3001 c = strrchr(s->pathname, '/');
3009 first = (panelvbox == NULL);
3011 panelvbox = gtk_vbox_new(FALSE, 4);
3012 gtk_widget_show(panelvbox);
3013 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
3018 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
3020 gtk_notebook_set_current_page(GTK_NOTEBOOK(panels),
3024 if (nselparams >= selparamsize) {
3026 selparams = sresize(selparams, selparamsize,
3029 selparams[nselparams].dp = &dp;
3030 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
3031 selparams[nselparams].panel = panelvbox;
3032 selparams[nselparams].shortcuts = scs; /* structure copy */
3034 assert(j-1 < level);
3036 #if GTK_CHECK_VERSION(2,0,0)
3038 /* treeiterlevels[j-1] will always be valid because we
3039 * don't allow implicit path components; see above.
3041 gtk_tree_store_append(treestore, &treeiter,
3042 &treeiterlevels[j-1]);
3044 gtk_tree_store_append(treestore, &treeiter, NULL);
3045 gtk_tree_store_set(treestore, &treeiter,
3047 TREESTORE_PARAMS, nselparams,
3049 treeiterlevels[j] = treeiter;
3051 selparams[nselparams].depth = j;
3053 selparams[nselparams].treepath =
3054 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
3055 &treeiterlevels[j-1]);
3057 * We are going to collapse all tree branches
3058 * at depth greater than 2, but not _yet_; see
3059 * the comment at the call to
3060 * gtk_tree_view_collapse_row below.
3062 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree),
3063 selparams[nselparams].treepath,
3066 selparams[nselparams].treepath = NULL;
3069 treeitem = gtk_tree_item_new_with_label(c);
3071 if (!treelevels[j-1]) {
3072 treelevels[j-1] = GTK_TREE(gtk_tree_new());
3073 gtk_tree_item_set_subtree
3074 (treeitemlevels[j-1],
3075 GTK_WIDGET(treelevels[j-1]));
3077 gtk_tree_item_expand(treeitemlevels[j-1]);
3079 gtk_tree_item_collapse(treeitemlevels[j-1]);
3081 gtk_tree_append(treelevels[j-1], treeitem);
3083 gtk_tree_append(GTK_TREE(tree), treeitem);
3085 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
3086 treelevels[j] = NULL;
3088 g_signal_connect(G_OBJECT(treeitem), "key_press_event",
3089 G_CALLBACK(tree_key_press), &dp);
3090 g_signal_connect(G_OBJECT(treeitem), "focus_in_event",
3091 G_CALLBACK(widget_focus), &dp);
3093 gtk_widget_show(treeitem);
3096 gtk_tree_select_child(GTK_TREE(tree), treeitem);
3097 selparams[nselparams].treeitem = treeitem;
3104 w = layout_ctrls(&dp, &selparams[nselparams-1].shortcuts, s, NULL);
3105 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
3110 #if GTK_CHECK_VERSION(2,0,0)
3116 * We want our tree view to come up with all branches at
3117 * depth 2 or more collapsed. However, if we start off
3118 * with those branches collapsed, then the tree view's
3119 * size request will be calculated based on the width of
3120 * the collapsed tree. So instead we start with them all
3121 * expanded; then we ask for the current size request,
3122 * collapse the relevant rows, and force the width to the
3123 * value we just computed. This arranges that the tree
3124 * view is wide enough to have all branches expanded
3128 gtk_widget_size_request(tree, &req);
3130 for (i = 0; i < nselparams; i++)
3131 if (selparams[i].depth >= 2)
3132 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree),
3133 selparams[i].treepath);
3135 gtk_widget_set_size_request(tree, req.width, -1);
3139 #if GTK_CHECK_VERSION(2,0,0)
3140 g_signal_connect(G_OBJECT(treeselection), "changed",
3141 G_CALLBACK(treeselection_changed), selparams);
3143 dp.ntreeitems = nselparams;
3144 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
3146 for (index = 0; index < nselparams; index++) {
3147 g_signal_connect(G_OBJECT(selparams[index].treeitem), "select",
3148 G_CALLBACK(treeitem_sel),
3150 dp.treeitems[index] = selparams[index].treeitem;
3155 dlg_refresh(NULL, &dp);
3157 dp.shortcuts = &selparams[0].shortcuts;
3158 #if !GTK_CHECK_VERSION(2,0,0)
3159 dp.currtreeitem = dp.treeitems[0];
3161 dp.lastfocus = NULL;
3167 extern void set_window_icon(GtkWidget *window,
3168 const char *const *const *icon,
3170 extern const char *const *const cfg_icon[];
3171 extern const int n_cfg_icon;
3172 set_window_icon(window, cfg_icon, n_cfg_icon);
3175 #if !GTK_CHECK_VERSION(2,0,0)
3176 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
3179 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
3181 GTK_POLICY_AUTOMATIC);
3182 gtk_widget_show(tree);
3184 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3185 gtk_widget_show(window);
3188 * Set focus into the first available control.
3190 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3191 struct controlset *s = ctrlbox->ctrlsets[index];
3196 for (j = 0; j < s->ncontrols; j++)
3197 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
3198 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
3199 s->ctrls[j]->generic.type != CTRL_TEXT) {
3200 dlg_set_focus(s->ctrls[j], &dp);
3201 dp.lastfocus = s->ctrls[j];
3210 g_signal_connect(G_OBJECT(window), "destroy",
3211 G_CALLBACK(window_destroy), NULL);
3212 g_signal_connect(G_OBJECT(window), "key_press_event",
3213 G_CALLBACK(win_key_press), &dp);
3223 static void messagebox_handler(union control *ctrl, void *dlg,
3224 void *data, int event)
3226 if (event == EVENT_ACTION)
3227 dlg_end(dlg, ctrl->generic.context.i);
3229 int messagebox(GtkWidget *parentwin, const char *title, const char *msg,
3232 GtkWidget *window, *w0, *w1;
3233 struct controlbox *ctrlbox;
3234 struct controlset *s0, *s1;
3237 struct Shortcuts scs;
3243 for (index = 0; index < lenof(scs.sc); index++) {
3244 scs.sc[index].action = SHORTCUT_EMPTY;
3247 ctrlbox = ctrl_new_box();
3250 va_start(ap, minwid);
3251 while (va_arg(ap, char *) != NULL) {
3253 (void) va_arg(ap, int); /* shortcut */
3254 (void) va_arg(ap, int); /* normal/default/cancel */
3255 (void) va_arg(ap, int); /* end value */
3259 s0 = ctrl_getset(ctrlbox, "", "", "");
3260 c = ctrl_columns(s0, 2, 50, 50);
3261 c->columns.ncols = s0->ncolumns = ncols;
3262 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
3263 for (index = 0; index < ncols; index++)
3264 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
3265 va_start(ap, minwid);
3268 char *title = va_arg(ap, char *);
3269 int shortcut, type, value;
3272 shortcut = va_arg(ap, int);
3273 type = va_arg(ap, int);
3274 value = va_arg(ap, int);
3275 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
3276 messagebox_handler, I(value));
3277 c->generic.column = index++;
3279 c->button.isdefault = TRUE;
3281 c->button.iscancel = TRUE;
3285 s1 = ctrl_getset(ctrlbox, "x", "", "");
3286 ctrl_text(s1, msg, HELPCTX(no_help));
3288 window = gtk_dialog_new();
3289 gtk_window_set_title(GTK_WINDOW(window), title);
3290 w0 = layout_ctrls(&dp, &scs, s0, GTK_WINDOW(window));
3291 set_dialog_action_area(GTK_DIALOG(window), w0);
3292 gtk_widget_show(w0);
3293 w1 = layout_ctrls(&dp, &scs, s1, GTK_WINDOW(window));
3294 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3295 gtk_widget_set_size_request(w1, minwid+20, -1);
3297 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3299 gtk_widget_show(w1);
3301 dp.shortcuts = &scs;
3302 dp.lastfocus = NULL;
3306 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3308 set_transient_window_pos(parentwin, window);
3309 gtk_window_set_transient_for(GTK_WINDOW(window),
3310 GTK_WINDOW(parentwin));
3312 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3313 gtk_widget_show(window);
3315 g_signal_connect(G_OBJECT(window), "destroy",
3316 G_CALLBACK(window_destroy), NULL);
3317 g_signal_connect(G_OBJECT(window), "key_press_event",
3318 G_CALLBACK(win_key_press), &dp);
3323 ctrl_free_box(ctrlbox);
3328 int string_width(const char *text)
3330 GtkWidget *label = gtk_label_new(text);
3332 gtk_widget_size_request(label, &req);
3333 g_object_ref_sink(G_OBJECT(label));
3337 int reallyclose(void *frontend)
3339 char *title = dupcat(appname, " Exit Confirmation", NULL);
3340 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
3341 title, "Are you sure you want to close this session?",
3342 string_width("Most of the width of the above text"),
3350 int verify_ssh_host_key(void *frontend, char *host, int port,
3351 const char *keytype, char *keystr, char *fingerprint,
3352 void (*callback)(void *ctx, int result), void *ctx)
3354 static const char absenttxt[] =
3355 "The server's host key is not cached. You have no guarantee "
3356 "that the server is the computer you think it is.\n"
3357 "The server's %s key fingerprint is:\n"
3359 "If you trust this host, press \"Accept\" to add the key to "
3360 "PuTTY's cache and carry on connecting.\n"
3361 "If you want to carry on connecting just once, without "
3362 "adding the key to the cache, press \"Connect Once\".\n"
3363 "If you do not trust this host, press \"Cancel\" to abandon the "
3365 static const char wrongtxt[] =
3366 "WARNING - POTENTIAL SECURITY BREACH!\n"
3367 "The server's host key does not match the one PuTTY has "
3368 "cached. This means that either the server administrator "
3369 "has changed the host key, or you have actually connected "
3370 "to another computer pretending to be the server.\n"
3371 "The new %s key fingerprint is:\n"
3373 "If you were expecting this change and trust the new key, "
3374 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3375 "If you want to carry on connecting but without updating "
3376 "the cache, press \"Connect Once\".\n"
3377 "If you want to abandon the connection completely, press "
3378 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3386 ret = verify_host_key(host, port, keytype, keystr);
3388 if (ret == 0) /* success - key matched OK */
3391 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
3393 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3394 "PuTTY Security Alert", text,
3395 string_width(fingerprint),
3396 "Accept", 'a', 0, 2,
3397 "Connect Once", 'o', 0, 1,
3398 "Cancel", 'c', -1, 0,
3404 store_host_key(host, port, keytype, keystr);
3405 return 1; /* continue with connection */
3406 } else if (ret == 1)
3407 return 1; /* continue with connection */
3408 return 0; /* do not continue with connection */
3412 * Ask whether the selected algorithm is acceptable (since it was
3413 * below the configured 'warn' threshold).
3415 int askalg(void *frontend, const char *algtype, const char *algname,
3416 void (*callback)(void *ctx, int result), void *ctx)
3418 static const char msg[] =
3419 "The first %s supported by the server is "
3420 "%s, which is below the configured warning threshold.\n"
3421 "Continue with connection?";
3425 text = dupprintf(msg, algtype, algname);
3426 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3427 "PuTTY Security Alert", text,
3428 string_width("Continue with connection?"),
3441 void old_keyfile_warning(void)
3444 * This should never happen on Unix. We hope.
3448 void fatal_message_box(void *window, const char *msg)
3450 messagebox(window, "PuTTY Fatal Error", msg,
3451 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3452 "OK", 'o', 1, 1, NULL);
3455 void nonfatal_message_box(void *window, const char *msg)
3457 messagebox(window, "PuTTY Error", msg,
3458 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3459 "OK", 'o', 1, 1, NULL);
3462 void fatalbox(const char *p, ...)
3467 msg = dupvprintf(p, ap);
3469 fatal_message_box(NULL, msg);
3474 void nonfatal(const char *p, ...)
3479 msg = dupvprintf(p, ap);
3481 nonfatal_message_box(NULL, msg);
3485 static GtkWidget *aboutbox = NULL;
3487 static void about_close_clicked(GtkButton *button, gpointer data)
3489 gtk_widget_destroy(aboutbox);
3493 static void licence_clicked(GtkButton *button, gpointer data)
3497 const char *licence =
3498 "Copyright 1997-2015 Simon Tatham.\n\n"
3500 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3501 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3502 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3503 "Markus Kuhn, Colin Watson, Christopher Staite, and CORE SDI S.A.\n\n"
3505 "Permission is hereby granted, free of charge, to any person "
3506 "obtaining a copy of this software and associated documentation "
3507 "files (the ""Software""), to deal in the Software without restriction, "
3508 "including without limitation the rights to use, copy, modify, merge, "
3509 "publish, distribute, sublicense, and/or sell copies of the Software, "
3510 "and to permit persons to whom the Software is furnished to do so, "
3511 "subject to the following conditions:\n\n"
3513 "The above copyright notice and this permission notice shall be "
3514 "included in all copies or substantial portions of the Software.\n\n"
3516 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3517 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3518 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3519 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3520 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3521 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3522 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3523 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3524 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3525 "OTHER DEALINGS IN THE SOFTWARE.";
3527 title = dupcat(appname, " Licence", NULL);
3528 assert(aboutbox != NULL);
3529 messagebox(aboutbox, title, licence,
3530 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3531 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3532 "OK", 'o', 1, 1, NULL);
3536 void about_box(void *window)
3542 gtk_widget_grab_focus(aboutbox);
3546 aboutbox = gtk_dialog_new();
3547 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
3548 title = dupcat("About ", appname, NULL);
3549 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
3552 w = gtk_button_new_with_label("Close");
3553 gtk_widget_set_can_default(w, TRUE);
3554 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
3555 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3556 w, FALSE, FALSE, 0);
3557 g_signal_connect(G_OBJECT(w), "clicked",
3558 G_CALLBACK(about_close_clicked), NULL);
3561 w = gtk_button_new_with_label("View Licence");
3562 gtk_widget_set_can_default(w, TRUE);
3563 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3564 w, FALSE, FALSE, 0);
3565 g_signal_connect(G_OBJECT(w), "clicked",
3566 G_CALLBACK(licence_clicked), NULL);
3569 w = gtk_label_new(appname);
3571 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3572 w, FALSE, FALSE, 0);
3575 w = gtk_label_new(ver);
3577 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3578 w, FALSE, FALSE, 5);
3581 w = gtk_label_new("Copyright 1997-2015 Simon Tatham. All rights reserved");
3583 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3584 w, FALSE, FALSE, 5);
3587 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
3588 gtk_window_set_transient_for(GTK_WINDOW(aboutbox),
3589 GTK_WINDOW(window));
3590 gtk_widget_show(aboutbox);
3593 struct eventlog_stuff {
3594 GtkWidget *parentwin, *window;
3595 struct controlbox *eventbox;
3596 struct Shortcuts scs;
3598 union control *listctrl;
3600 int nevents, negsize;
3603 int ignore_selchange;
3606 static void eventlog_destroy(GtkWidget *widget, gpointer data)
3608 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3613 dlg_cleanup(&es->dp);
3614 ctrl_free_box(es->eventbox);
3616 static void eventlog_ok_handler(union control *ctrl, void *dlg,
3617 void *data, int event)
3619 if (event == EVENT_ACTION)
3622 static void eventlog_list_handler(union control *ctrl, void *dlg,
3623 void *data, int event)
3625 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3627 if (event == EVENT_REFRESH) {
3630 dlg_update_start(ctrl, dlg);
3631 dlg_listbox_clear(ctrl, dlg);
3632 for (i = 0; i < es->nevents; i++) {
3633 dlg_listbox_add(ctrl, dlg, es->events[i]);
3635 dlg_update_done(ctrl, dlg);
3636 } else if (event == EVENT_SELCHANGE) {
3641 * If this SELCHANGE event is happening as a result of
3642 * deliberate deselection because someone else has grabbed
3643 * the selection, the last thing we want to do is pre-empt
3646 if (es->ignore_selchange)
3650 * Construct the data to use as the selection.
3655 for (i = 0; i < es->nevents; i++) {
3656 if (dlg_listbox_issel(ctrl, dlg, i)) {
3657 int extralen = strlen(es->events[i]);
3659 if (es->sellen + extralen + 2 > selsize) {
3660 selsize = es->sellen + extralen + 512;
3661 es->seldata = sresize(es->seldata, selsize, char);
3664 strcpy(es->seldata + es->sellen, es->events[i]);
3665 es->sellen += extralen;
3666 es->seldata[es->sellen++] = '\n';
3670 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
3671 GDK_CURRENT_TIME)) {
3672 extern GdkAtom compound_text_atom;
3674 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3675 GDK_SELECTION_TYPE_STRING, 1);
3676 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3677 compound_text_atom, 1);
3683 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
3684 guint info, guint time_stamp, gpointer data)
3686 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3688 gtk_selection_data_set(seldata, gtk_selection_data_get_target(seldata), 8,
3689 (unsigned char *)es->seldata, es->sellen);
3692 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
3695 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3699 * Deselect everything in the list box.
3701 uc = dlg_find_byctrl(&es->dp, es->listctrl);
3702 es->ignore_selchange = 1;
3703 #if !GTK_CHECK_VERSION(2,0,0)
3705 gtk_list_unselect_all(GTK_LIST(uc->list));
3707 assert(uc->treeview);
3708 gtk_tree_selection_unselect_all
3709 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview)));
3711 es->ignore_selchange = 0;
3719 void showeventlog(void *estuff, void *parentwin)
3721 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3722 GtkWidget *window, *w0, *w1;
3723 GtkWidget *parent = GTK_WIDGET(parentwin);
3724 struct controlset *s0, *s1;
3730 gtk_widget_grab_focus(es->window);
3736 for (index = 0; index < lenof(es->scs.sc); index++) {
3737 es->scs.sc[index].action = SHORTCUT_EMPTY;
3740 es->eventbox = ctrl_new_box();
3742 s0 = ctrl_getset(es->eventbox, "", "", "");
3743 ctrl_columns(s0, 3, 33, 34, 33);
3744 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
3745 eventlog_ok_handler, P(NULL));
3746 c->button.column = 1;
3747 c->button.isdefault = TRUE;
3749 s1 = ctrl_getset(es->eventbox, "x", "", "");
3750 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3751 eventlog_list_handler, P(es));
3752 c->listbox.height = 10;
3753 c->listbox.multisel = 2;
3754 c->listbox.ncols = 3;
3755 c->listbox.percentages = snewn(3, int);
3756 c->listbox.percentages[0] = 25;
3757 c->listbox.percentages[1] = 10;
3758 c->listbox.percentages[2] = 65;
3760 es->window = window = gtk_dialog_new();
3761 title = dupcat(appname, " Event Log", NULL);
3762 gtk_window_set_title(GTK_WINDOW(window), title);
3764 w0 = layout_ctrls(&es->dp, &es->scs, s0, GTK_WINDOW(window));
3765 set_dialog_action_area(GTK_DIALOG(window), w0);
3766 gtk_widget_show(w0);
3767 w1 = layout_ctrls(&es->dp, &es->scs, s1, GTK_WINDOW(window));
3768 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3769 gtk_widget_set_size_request(w1, 20 + string_width
3770 ("LINE OF TEXT GIVING WIDTH OF EVENT LOG IS "
3771 "QUITE LONG 'COS SSH LOG ENTRIES ARE WIDE"),
3774 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3776 gtk_widget_show(w1);
3779 es->dp.shortcuts = &es->scs;
3780 es->dp.lastfocus = NULL;
3782 es->dp.window = window;
3784 dlg_refresh(NULL, &es->dp);
3787 set_transient_window_pos(parent, window);
3788 gtk_window_set_transient_for(GTK_WINDOW(window),
3789 GTK_WINDOW(parent));
3791 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3792 gtk_widget_show(window);
3794 g_signal_connect(G_OBJECT(window), "destroy",
3795 G_CALLBACK(eventlog_destroy), es);
3796 g_signal_connect(G_OBJECT(window), "key_press_event",
3797 G_CALLBACK(win_key_press), &es->dp);
3798 g_signal_connect(G_OBJECT(window), "selection_get",
3799 G_CALLBACK(eventlog_selection_get), es);
3800 g_signal_connect(G_OBJECT(window), "selection_clear_event",
3801 G_CALLBACK(eventlog_selection_clear), es);
3804 void *eventlogstuff_new(void)
3806 struct eventlog_stuff *es;
3807 es = snew(struct eventlog_stuff);
3808 memset(es, 0, sizeof(*es));
3812 void logevent_dlg(void *estuff, const char *string)
3814 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3819 if (es->nevents >= es->negsize) {
3821 es->events = sresize(es->events, es->negsize, char *);
3825 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3827 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3828 strcpy(es->events[es->nevents], timebuf);
3829 strcat(es->events[es->nevents], string);
3831 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3836 int askappend(void *frontend, Filename *filename,
3837 void (*callback)(void *ctx, int result), void *ctx)
3839 static const char msgtemplate[] =
3840 "The session log file \"%.*s\" already exists. "
3841 "You can overwrite it with a new session log, "
3842 "append your session log to the end of it, "
3843 "or disable session logging for this session.";
3848 message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
3849 mbtitle = dupprintf("%s Log to File", appname);
3851 mbret = messagebox(get_window(frontend), mbtitle, message,
3852 string_width("LINE OF TEXT SUITABLE FOR THE"
3853 " ASKAPPEND WIDTH"),
3854 "Overwrite", 'o', 1, 2,
3855 "Append", 'a', 0, 1,
3856 "Disable", 'd', -1, 0,