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 #if GTK_CHECK_VERSION(3,0,0)
131 static void colourchoose_response(GtkDialog *dialog,
132 gint response_id, gpointer data);
134 static void coloursel_ok(GtkButton *button, gpointer data);
135 static void coloursel_cancel(GtkButton *button, gpointer data);
137 static void window_destroy(GtkWidget *widget, gpointer data);
138 int get_listitemheight(GtkWidget *widget);
140 static int uctrl_cmp_byctrl(void *av, void *bv)
142 struct uctrl *a = (struct uctrl *)av;
143 struct uctrl *b = (struct uctrl *)bv;
144 if (a->ctrl < b->ctrl)
146 else if (a->ctrl > b->ctrl)
151 static int uctrl_cmp_byctrl_find(void *av, void *bv)
153 union control *a = (union control *)av;
154 struct uctrl *b = (struct uctrl *)bv;
157 else if (a > b->ctrl)
162 static int uctrl_cmp_bywidget(void *av, void *bv)
164 struct uctrl *a = (struct uctrl *)av;
165 struct uctrl *b = (struct uctrl *)bv;
166 if (a->toplevel < b->toplevel)
168 else if (a->toplevel > b->toplevel)
173 static int uctrl_cmp_bywidget_find(void *av, void *bv)
175 GtkWidget *a = (GtkWidget *)av;
176 struct uctrl *b = (struct uctrl *)bv;
179 else if (a > b->toplevel)
184 static void dlg_init(struct dlgparam *dp)
186 dp->byctrl = newtree234(uctrl_cmp_byctrl);
187 dp->bywidget = newtree234(uctrl_cmp_bywidget);
188 dp->coloursel_result.ok = FALSE;
189 dp->window = dp->cancelbutton = NULL;
190 #if !GTK_CHECK_VERSION(2,0,0)
191 dp->treeitems = NULL;
192 dp->currtreeitem = NULL;
195 dp->currfocus = NULL;
198 static void dlg_cleanup(struct dlgparam *dp)
202 freetree234(dp->byctrl); /* doesn't free the uctrls inside */
204 while ( (uc = index234(dp->bywidget, 0)) != NULL) {
205 del234(dp->bywidget, uc);
209 freetree234(dp->bywidget);
211 #if !GTK_CHECK_VERSION(2,0,0)
212 sfree(dp->treeitems);
216 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
218 add234(dp->byctrl, uc);
219 add234(dp->bywidget, uc);
222 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
226 return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
229 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
231 struct uctrl *ret = NULL;
235 ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
238 w = gtk_widget_get_parent(w);
243 union control *dlg_last_focused(union control *ctrl, void *dlg)
245 struct dlgparam *dp = (struct dlgparam *)dlg;
246 if (dp->currfocus != ctrl)
247 return dp->currfocus;
249 return dp->lastfocus;
252 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
254 struct dlgparam *dp = (struct dlgparam *)dlg;
255 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
256 assert(uc->ctrl->generic.type == CTRL_RADIO);
257 assert(uc->buttons != NULL);
258 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
261 int dlg_radiobutton_get(union control *ctrl, void *dlg)
263 struct dlgparam *dp = (struct dlgparam *)dlg;
264 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
267 assert(uc->ctrl->generic.type == CTRL_RADIO);
268 assert(uc->buttons != NULL);
269 for (i = 0; i < uc->nbuttons; i++)
270 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
272 return 0; /* got to return something */
275 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
277 struct dlgparam *dp = (struct dlgparam *)dlg;
278 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
279 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
280 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
283 int dlg_checkbox_get(union control *ctrl, void *dlg)
285 struct dlgparam *dp = (struct dlgparam *)dlg;
286 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
287 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
288 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
291 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
293 struct dlgparam *dp = (struct dlgparam *)dlg;
294 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
297 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
299 #if GTK_CHECK_VERSION(2,4,0)
301 entry = gtk_bin_get_child(GTK_BIN(uc->combo));
306 assert(entry != NULL);
309 * GTK 2 implements gtk_entry_set_text by means of two separate
310 * operations: first delete the previous text leaving the empty
311 * string, then insert the new text. This causes two calls to
312 * the "changed" signal.
314 * The first call to "changed", if allowed to proceed normally,
315 * will cause an EVENT_VALCHANGE event on the edit box, causing
316 * a call to dlg_editbox_get() which will read the empty string
317 * out of the GtkEntry - and promptly write it straight into the
318 * Conf structure, which is precisely where our `text' pointer
319 * is probably pointing, so the second editing operation will
320 * insert that instead of the string we originally asked for.
322 * Hence, we must take our own copy of the text before we do
325 tmpstring = dupstr(text);
326 gtk_entry_set_text(GTK_ENTRY(entry), tmpstring);
330 char *dlg_editbox_get(union control *ctrl, void *dlg)
332 struct dlgparam *dp = (struct dlgparam *)dlg;
333 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
334 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
336 #if GTK_CHECK_VERSION(2,4,0)
338 return dupstr(gtk_entry_get_text
339 (GTK_ENTRY(gtk_bin_get_child(GTK_BIN(uc->combo)))));
344 return dupstr(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
347 assert(!"We shouldn't get here");
350 #if !GTK_CHECK_VERSION(2,4,0)
351 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
353 GtkContainer *cont = GTK_CONTAINER(data);
354 /* gtk_container_remove will unref the widget for us; we need not. */
355 gtk_container_remove(cont, w);
359 /* The `listbox' functions can also apply to combo boxes. */
360 void dlg_listbox_clear(union control *ctrl, void *dlg)
362 struct dlgparam *dp = (struct dlgparam *)dlg;
363 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
365 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
366 uc->ctrl->generic.type == CTRL_LISTBOX);
368 #if !GTK_CHECK_VERSION(2,4,0)
370 gtk_container_foreach(GTK_CONTAINER(uc->menu),
371 container_remove_and_destroy,
372 GTK_CONTAINER(uc->menu));
376 gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
380 #if GTK_CHECK_VERSION(2,0,0)
382 gtk_list_store_clear(uc->listmodel);
386 assert(!"We shouldn't get here");
389 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
391 struct dlgparam *dp = (struct dlgparam *)dlg;
392 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
394 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
395 uc->ctrl->generic.type == CTRL_LISTBOX);
397 #if !GTK_CHECK_VERSION(2,4,0)
400 (GTK_CONTAINER(uc->menu),
401 g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
405 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
409 #if GTK_CHECK_VERSION(2,0,0)
413 assert(uc->listmodel != NULL);
414 path = gtk_tree_path_new_from_indices(index, -1);
415 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
416 gtk_list_store_remove(uc->listmodel, &iter);
417 gtk_tree_path_free(path);
421 assert(!"We shouldn't get here");
424 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
426 dlg_listbox_addwithid(ctrl, dlg, text, 0);
430 * Each listbox entry may have a numeric id associated with it.
431 * Note that some front ends only permit a string to be stored at
432 * each position, which means that _if_ you put two identical
433 * strings in any listbox then you MUST not assign them different
434 * IDs and expect to get meaningful results back.
436 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
437 char const *text, int id)
439 struct dlgparam *dp = (struct dlgparam *)dlg;
440 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
442 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
443 uc->ctrl->generic.type == CTRL_LISTBOX);
446 * This routine is long and complicated in both GTK 1 and 2,
447 * and completely different. Sigh.
449 dp->flags |= FLAG_UPDATING_COMBO_LIST;
451 #if !GTK_CHECK_VERSION(2,4,0)
454 * List item in a drop-down (but non-combo) list. Tabs are
455 * ignored; we just provide a standard menu item with the
458 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
460 gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
461 gtk_widget_show(menuitem);
463 g_object_set_data(G_OBJECT(menuitem), "user-data",
464 GINT_TO_POINTER(id));
465 g_signal_connect(G_OBJECT(menuitem), "activate",
466 G_CALLBACK(menuitem_activate), dp);
469 if (uc->list && uc->entry) {
471 * List item in a combo-box list, which means the sensible
472 * thing to do is make it a perfectly normal label. Hence
473 * tabs are disregarded.
475 GtkWidget *listitem = gtk_list_item_new_with_label(text);
477 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
478 gtk_widget_show(listitem);
480 g_object_set_data(G_OBJECT(listitem), "user-data",
481 GINT_TO_POINTER(id));
485 #if !GTK_CHECK_VERSION(2,0,0)
488 * List item in a non-combo-box list box. We make all of
489 * these Columns containing GtkLabels. This allows us to do
490 * the nasty force_left hack irrespective of whether there
491 * are tabs in the thing.
493 GtkWidget *listitem = gtk_list_item_new();
494 GtkWidget *cols = columns_new(10);
498 /* Count the tabs in the text, and hence determine # of columns. */
500 for (i = 0; text[i]; i++)
505 (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
506 percents = snewn(ncols, gint);
507 percents[ncols-1] = 100;
508 for (i = 0; i < ncols-1; i++) {
509 percents[i] = uc->ctrl->listbox.percentages[i];
510 percents[ncols-1] -= percents[i];
512 columns_set_cols(COLUMNS(cols), ncols, percents);
515 for (i = 0; i < ncols; i++) {
516 int len = strcspn(text, "\t");
517 char *dup = dupprintf("%.*s", len, text);
522 label = gtk_label_new(dup);
525 columns_add(COLUMNS(cols), label, i, 1);
526 columns_force_left_align(COLUMNS(cols), label);
527 gtk_widget_show(label);
529 gtk_container_add(GTK_CONTAINER(listitem), cols);
530 gtk_widget_show(cols);
531 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
532 gtk_widget_show(listitem);
534 if (ctrl->listbox.multisel) {
535 g_signal_connect(G_OBJECT(listitem), "key_press_event",
536 G_CALLBACK(listitem_multi_key), uc->adj);
538 g_signal_connect(G_OBJECT(listitem), "key_press_event",
539 G_CALLBACK(listitem_single_key), uc->adj);
541 g_signal_connect(G_OBJECT(listitem), "focus_in_event",
542 G_CALLBACK(widget_focus), dp);
543 g_signal_connect(G_OBJECT(listitem), "button_press_event",
544 G_CALLBACK(listitem_button_press), dp);
545 g_signal_connect(G_OBJECT(listitem), "button_release_event",
546 G_CALLBACK(listitem_button_release), dp);
547 g_object_set_data(G_OBJECT(listitem), "user-data",
548 GINT_TO_POINTER(id));
556 dp->flags |= FLAG_UPDATING_LISTBOX;/* inhibit drag-list update */
557 gtk_list_store_append(uc->listmodel, &iter);
558 dp->flags &= ~FLAG_UPDATING_LISTBOX;
559 gtk_list_store_set(uc->listmodel, &iter, 0, id, -1);
562 * Now go through text and divide it into columns at the tabs,
565 cols = (uc->ctrl->generic.type == CTRL_LISTBOX ? ctrl->listbox.ncols : 1);
566 cols = cols ? cols : 1;
567 for (i = 0; i < cols; i++) {
568 int collen = strcspn(text, "\t");
569 char *tmpstr = snewn(collen+1, char);
570 memcpy(tmpstr, text, collen);
571 tmpstr[collen] = '\0';
572 gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1);
580 assert(!"We shouldn't get here");
582 dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
585 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
587 struct dlgparam *dp = (struct dlgparam *)dlg;
588 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
590 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
591 uc->ctrl->generic.type == CTRL_LISTBOX);
593 #if !GTK_CHECK_VERSION(2,4,0)
594 if (uc->menu || uc->list) {
598 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
600 item = G_OBJECT(g_list_nth_data(children, index));
601 g_list_free(children);
603 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item), "user-data"));
606 #if GTK_CHECK_VERSION(2,0,0)
612 path = gtk_tree_path_new_from_indices(index, -1);
613 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
614 gtk_tree_model_get(GTK_TREE_MODEL(uc->listmodel), &iter, 0, &ret, -1);
615 gtk_tree_path_free(path);
620 assert(!"We shouldn't get here");
621 return -1; /* placate dataflow analysis */
624 /* dlg_listbox_index returns <0 if no single element is selected. */
625 int dlg_listbox_index(union control *ctrl, void *dlg)
627 struct dlgparam *dp = (struct dlgparam *)dlg;
628 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
630 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
631 uc->ctrl->generic.type == CTRL_LISTBOX);
633 #if !GTK_CHECK_VERSION(2,4,0)
634 if (uc->menu || uc->list) {
636 GtkWidget *item, *activeitem;
641 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
643 activeitem = NULL; /* unnecessarily placate gcc */
645 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
647 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
648 i++, children = children->next) {
649 if (uc->menu ? activeitem == item :
650 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
657 g_list_free(children);
658 return selected < 0 ? -1 : selected;
663 * This API function already does the right thing in the
664 * case of no current selection.
666 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo));
669 #if GTK_CHECK_VERSION(2,0,0)
671 GtkTreeSelection *treesel;
678 assert(uc->treeview != NULL);
679 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
681 if (gtk_tree_selection_count_selected_rows(treesel) != 1)
684 sellist = gtk_tree_selection_get_selected_rows(treesel, &model);
686 assert(sellist && sellist->data);
687 path = sellist->data;
689 if (gtk_tree_path_get_depth(path) != 1) {
692 indices = gtk_tree_path_get_indices(path);
700 g_list_foreach(sellist, (GFunc)gtk_tree_path_free, NULL);
701 g_list_free(sellist);
706 assert(!"We shouldn't get here");
707 return -1; /* placate dataflow analysis */
710 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
712 struct dlgparam *dp = (struct dlgparam *)dlg;
713 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
715 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
716 uc->ctrl->generic.type == CTRL_LISTBOX);
718 #if !GTK_CHECK_VERSION(2,4,0)
719 if (uc->menu || uc->list) {
721 GtkWidget *item, *activeitem;
723 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
724 uc->ctrl->generic.type == CTRL_LISTBOX);
725 assert(uc->menu != NULL || uc->list != NULL);
727 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
729 item = GTK_WIDGET(g_list_nth_data(children, index));
730 g_list_free(children);
733 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
734 return item == activeitem;
736 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
742 * This API function already does the right thing in the
743 * case of no current selection.
745 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo)) == index;
748 #if GTK_CHECK_VERSION(2,0,0)
750 GtkTreeSelection *treesel;
754 assert(uc->treeview != NULL);
755 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
757 path = gtk_tree_path_new_from_indices(index, -1);
758 ret = gtk_tree_selection_path_is_selected(treesel, path);
759 gtk_tree_path_free(path);
764 assert(!"We shouldn't get here");
765 return -1; /* placate dataflow analysis */
768 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
770 struct dlgparam *dp = (struct dlgparam *)dlg;
771 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
773 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
774 uc->ctrl->generic.type == CTRL_LISTBOX);
776 #if !GTK_CHECK_VERSION(2,4,0)
778 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
784 gdouble newtop, newbot;
786 gtk_list_select_item(GTK_LIST(uc->list), index);
789 * Scroll the list box if necessary to ensure the newly
790 * selected item is visible.
792 items = gtk_container_children(GTK_CONTAINER(uc->list));
793 nitems = g_list_length(items);
795 int modified = FALSE;
797 newtop = uc->adj->lower +
798 (uc->adj->upper - uc->adj->lower) * index / nitems;
799 newbot = uc->adj->lower +
800 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
801 if (uc->adj->value > newtop) {
803 uc->adj->value = newtop;
804 } else if (uc->adj->value < newbot - uc->adj->page_size) {
806 uc->adj->value = newbot - uc->adj->page_size;
809 gtk_adjustment_value_changed(uc->adj);
815 gtk_combo_box_set_active(GTK_COMBO_BOX(uc->combo), index);
819 #if GTK_CHECK_VERSION(2,0,0)
821 GtkTreeSelection *treesel;
824 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
826 path = gtk_tree_path_new_from_indices(index, -1);
827 gtk_tree_selection_select_path(treesel, path);
828 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc->treeview),
829 path, NULL, FALSE, 0.0, 0.0);
830 gtk_tree_path_free(path);
834 assert(!"We shouldn't get here");
837 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
839 struct dlgparam *dp = (struct dlgparam *)dlg;
840 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
842 assert(uc->ctrl->generic.type == CTRL_TEXT);
843 assert(uc->text != NULL);
845 gtk_label_set_text(GTK_LABEL(uc->text), text);
848 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
850 struct dlgparam *dp = (struct dlgparam *)dlg;
851 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
853 switch (uc->ctrl->generic.type) {
855 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
856 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
859 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
860 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
863 gtk_label_set_text(GTK_LABEL(uc->label), text);
864 shortcut_highlight(uc->label, ctrl->radio.shortcut);
867 gtk_label_set_text(GTK_LABEL(uc->label), text);
868 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
870 case CTRL_FILESELECT:
871 gtk_label_set_text(GTK_LABEL(uc->label), text);
872 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
874 case CTRL_FONTSELECT:
875 gtk_label_set_text(GTK_LABEL(uc->label), text);
876 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
879 gtk_label_set_text(GTK_LABEL(uc->label), text);
880 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
883 assert(!"This shouldn't happen");
888 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
890 struct dlgparam *dp = (struct dlgparam *)dlg;
891 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
892 /* We must copy fn->path before passing it to gtk_entry_set_text.
893 * See comment in dlg_editbox_set() for the reasons. */
894 char *duppath = dupstr(fn->path);
895 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
896 assert(uc->entry != NULL);
897 gtk_entry_set_text(GTK_ENTRY(uc->entry), duppath);
901 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
903 struct dlgparam *dp = (struct dlgparam *)dlg;
904 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
905 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
906 assert(uc->entry != NULL);
907 return filename_from_str(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
910 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
912 struct dlgparam *dp = (struct dlgparam *)dlg;
913 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
914 /* We must copy fs->name before passing it to gtk_entry_set_text.
915 * See comment in dlg_editbox_set() for the reasons. */
916 char *dupname = dupstr(fs->name);
917 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
918 assert(uc->entry != NULL);
919 gtk_entry_set_text(GTK_ENTRY(uc->entry), dupname);
923 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
925 struct dlgparam *dp = (struct dlgparam *)dlg;
926 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
927 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
928 assert(uc->entry != NULL);
929 return fontspec_new(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
933 * Bracketing a large set of updates in these two functions will
934 * cause the front end (if possible) to delay updating the screen
935 * until it's all complete, thus avoiding flicker.
937 void dlg_update_start(union control *ctrl, void *dlg)
940 * Apparently we can't do this at all in GTK. GtkCList supports
941 * freeze and thaw, but not GtkList. Bah.
945 void dlg_update_done(union control *ctrl, void *dlg)
948 * Apparently we can't do this at all in GTK. GtkCList supports
949 * freeze and thaw, but not GtkList. Bah.
953 void dlg_set_focus(union control *ctrl, void *dlg)
955 struct dlgparam *dp = (struct dlgparam *)dlg;
956 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
958 switch (ctrl->generic.type) {
961 /* Check boxes and buttons get the focus _and_ get toggled. */
962 gtk_widget_grab_focus(uc->toplevel);
964 case CTRL_FILESELECT:
965 case CTRL_FONTSELECT:
968 /* Anything containing an edit box gets that focused. */
969 gtk_widget_grab_focus(uc->entry);
971 #if GTK_CHECK_VERSION(2,4,0)
972 else if (uc->combo) {
973 /* Failing that, there'll be a combo box. */
974 gtk_widget_grab_focus(uc->combo);
980 * Radio buttons: we find the currently selected button and
985 for (i = 0; i < ctrl->radio.nbuttons; i++)
986 if (gtk_toggle_button_get_active
987 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
988 gtk_widget_grab_focus(uc->buttons[i]);
993 #if !GTK_CHECK_VERSION(2,4,0)
995 gtk_widget_grab_focus(uc->optmenu);
1000 gtk_widget_grab_focus(uc->combo);
1004 #if !GTK_CHECK_VERSION(2,0,0)
1007 * For GTK-1 style list boxes, we tell it to focus one
1008 * of its children, which appears to do the Right
1011 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
1016 gtk_widget_grab_focus(uc->treeview);
1020 assert(!"We shouldn't get here");
1026 * During event processing, you might well want to give an error
1027 * indication to the user. dlg_beep() is a quick and easy generic
1028 * error; dlg_error() puts up a message-box or equivalent.
1030 void dlg_beep(void *dlg)
1035 static void errmsg_button_clicked(GtkButton *button, gpointer data)
1037 gtk_widget_destroy(GTK_WIDGET(data));
1040 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
1042 #if !GTK_CHECK_VERSION(2,0,0)
1043 gint x, y, w, h, dx, dy;
1045 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
1046 gtk_widget_size_request(GTK_WIDGET(child), &req);
1048 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(parent)), &x, &y);
1049 gdk_window_get_size(gtk_widget_get_window(GTK_WIDGET(parent)), &w, &h);
1052 * One corner of the transient will be offset inwards, by 1/4
1053 * of the parent window's size, from the corresponding corner
1054 * of the parent window. The corner will be chosen so as to
1055 * place the transient closer to the centre of the screen; this
1056 * should avoid transients going off the edge of the screen on
1059 if (x + w/2 < gdk_screen_width() / 2)
1060 dx = x + w/4; /* work from left edges */
1062 dx = x + 3*w/4 - req.width; /* work from right edges */
1063 if (y + h/2 < gdk_screen_height() / 2)
1064 dy = y + h/4; /* work from top edges */
1066 dy = y + 3*h/4 - req.height; /* work from bottom edges */
1067 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
1071 void dlg_error_msg(void *dlg, const char *msg)
1073 struct dlgparam *dp = (struct dlgparam *)dlg;
1074 GtkWidget *window, *hbox, *text, *ok;
1076 window = gtk_dialog_new();
1077 text = gtk_label_new(msg);
1078 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1079 hbox = gtk_hbox_new(FALSE, 0);
1080 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1081 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
1082 hbox, FALSE, FALSE, 20);
1083 gtk_widget_show(text);
1084 gtk_widget_show(hbox);
1085 gtk_window_set_title(GTK_WINDOW(window), "Error");
1086 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1087 ok = gtk_button_new_with_label("OK");
1088 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(window))),
1089 ok, FALSE, FALSE, 0);
1090 gtk_widget_show(ok);
1091 gtk_widget_set_can_default(ok, TRUE);
1092 gtk_window_set_default(GTK_WINDOW(window), ok);
1093 g_signal_connect(G_OBJECT(ok), "clicked",
1094 G_CALLBACK(errmsg_button_clicked), window);
1095 g_signal_connect(G_OBJECT(window), "destroy",
1096 G_CALLBACK(window_destroy), NULL);
1097 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1098 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
1099 set_transient_window_pos(dp->window, window);
1100 gtk_widget_show(window);
1106 * This function signals to the front end that the dialog's
1107 * processing is completed, and passes an integer value (typically
1108 * a success status).
1110 void dlg_end(void *dlg, int value)
1112 struct dlgparam *dp = (struct dlgparam *)dlg;
1114 gtk_widget_destroy(dp->window);
1117 void dlg_refresh(union control *ctrl, void *dlg)
1119 struct dlgparam *dp = (struct dlgparam *)dlg;
1123 if (ctrl->generic.handler != NULL)
1124 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1128 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
1129 assert(uc->ctrl != NULL);
1130 if (uc->ctrl->generic.handler != NULL)
1131 uc->ctrl->generic.handler(uc->ctrl, dp,
1132 dp->data, EVENT_REFRESH);
1137 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
1139 struct dlgparam *dp = (struct dlgparam *)dlg;
1140 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
1142 #if GTK_CHECK_VERSION(3,0,0)
1143 GtkWidget *coloursel =
1144 gtk_color_chooser_dialog_new("Select a colour",
1145 GTK_WINDOW(dp->window));
1146 gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(coloursel), FALSE);
1148 GtkWidget *okbutton, *cancelbutton;
1149 GtkWidget *coloursel =
1150 gtk_color_selection_dialog_new("Select a colour");
1151 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
1152 GtkColorSelection *cs = GTK_COLOR_SELECTION
1153 (gtk_color_selection_dialog_get_color_selection(ccs));
1154 gtk_color_selection_set_has_opacity_control(cs, FALSE);
1157 dp->coloursel_result.ok = FALSE;
1159 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
1161 #if GTK_CHECK_VERSION(3,0,0)
1164 rgba.red = r / 255.0;
1165 rgba.green = g / 255.0;
1166 rgba.blue = b / 255.0;
1167 rgba.alpha = 1.0; /* fully opaque! */
1168 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(coloursel), &rgba);
1170 #elif GTK_CHECK_VERSION(2,0,0)
1173 col.red = r * 0x0101;
1174 col.green = g * 0x0101;
1175 col.blue = b * 0x0101;
1176 gtk_color_selection_set_current_color(cs, &col);
1181 cvals[0] = r / 255.0;
1182 cvals[1] = g / 255.0;
1183 cvals[2] = b / 255.0;
1184 cvals[3] = 1.0; /* fully opaque! */
1185 gtk_color_selection_set_color(cs, cvals);
1189 g_object_set_data(G_OBJECT(coloursel), "user-data", (gpointer)uc);
1191 #if GTK_CHECK_VERSION(3,0,0)
1192 g_signal_connect(G_OBJECT(coloursel), "response",
1193 G_CALLBACK(colourchoose_response), (gpointer)dp);
1196 #if GTK_CHECK_VERSION(2,0,0)
1197 g_object_get(G_OBJECT(ccs),
1198 "ok-button", &okbutton,
1199 "cancel-button", &cancelbutton,
1200 (const char *)NULL);
1202 okbutton = ccs->ok_button;
1203 cancelbutton = ccs->cancel_button;
1205 g_object_set_data(G_OBJECT(okbutton), "user-data",
1206 (gpointer)coloursel);
1207 g_object_set_data(G_OBJECT(cancelbutton), "user-data",
1208 (gpointer)coloursel);
1209 g_signal_connect(G_OBJECT(okbutton), "clicked",
1210 G_CALLBACK(coloursel_ok), (gpointer)dp);
1211 g_signal_connect(G_OBJECT(cancelbutton), "clicked",
1212 G_CALLBACK(coloursel_cancel), (gpointer)dp);
1213 g_signal_connect_swapped(G_OBJECT(okbutton), "clicked",
1214 G_CALLBACK(gtk_widget_destroy),
1215 (gpointer)coloursel);
1216 g_signal_connect_swapped(G_OBJECT(cancelbutton), "clicked",
1217 G_CALLBACK(gtk_widget_destroy),
1218 (gpointer)coloursel);
1220 gtk_widget_show(coloursel);
1223 int dlg_coloursel_results(union control *ctrl, void *dlg,
1224 int *r, int *g, int *b)
1226 struct dlgparam *dp = (struct dlgparam *)dlg;
1227 if (dp->coloursel_result.ok) {
1228 *r = dp->coloursel_result.r;
1229 *g = dp->coloursel_result.g;
1230 *b = dp->coloursel_result.b;
1236 /* ----------------------------------------------------------------------
1237 * Signal handlers while the dialog box is active.
1240 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
1243 struct dlgparam *dp = (struct dlgparam *)data;
1244 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1245 union control *focus;
1252 if (focus != dp->currfocus) {
1253 dp->lastfocus = dp->currfocus;
1254 dp->currfocus = focus;
1260 static void button_clicked(GtkButton *button, gpointer data)
1262 struct dlgparam *dp = (struct dlgparam *)data;
1263 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1264 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1267 static void button_toggled(GtkToggleButton *tb, gpointer data)
1269 struct dlgparam *dp = (struct dlgparam *)data;
1270 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
1271 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1274 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
1278 * GtkEntry has a nasty habit of eating the Return key, which
1279 * is unhelpful since it doesn't actually _do_ anything with it
1280 * (it calls gtk_widget_activate, but our edit boxes never need
1281 * activating). So I catch Return before GtkEntry sees it, and
1282 * pass it straight on to the parent widget. Effect: hitting
1283 * Return in an edit box will now activate the default button
1284 * in the dialog just like it will everywhere else.
1286 GtkWidget *parent = gtk_widget_get_parent(widget);
1287 if (event->keyval == GDK_KEY_Return && parent != NULL) {
1288 gboolean return_val;
1289 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
1290 g_signal_emit_by_name(G_OBJECT(parent), "key_press_event",
1291 event, &return_val);
1297 static void editbox_changed(GtkEditable *ed, gpointer data)
1299 struct dlgparam *dp = (struct dlgparam *)data;
1300 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1301 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1302 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1306 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1309 struct dlgparam *dp = (struct dlgparam *)data;
1310 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1311 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1315 #if !GTK_CHECK_VERSION(2,0,0)
1318 * GTK 1 list box event handlers.
1321 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1322 gpointer data, int multiple)
1324 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1326 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1327 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1328 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1329 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1331 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1332 * in a list box. So, if the list box is single-selection:
1334 * - if the list item in question isn't already selected,
1335 * we simply select it.
1336 * - otherwise, we find the next one (or next
1337 * however-far-away) in whichever direction we're going,
1339 * + in this case, we must also fiddle with the
1340 * scrollbar to ensure the newly selected item is
1343 * If it's multiple-selection, we do all of the above
1344 * except actually selecting anything, so we move the focus
1345 * and fiddle the scrollbar to follow it.
1347 GtkWidget *list = item->parent;
1349 g_signal_stop_emission_by_name(G_OBJECT(item), "key_press_event");
1352 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1353 gtk_list_select_child(GTK_LIST(list), item);
1356 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1357 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1360 (event->keyval==GDK_Page_Down ||
1361 event->keyval==GDK_KP_Page_Down ||
1362 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1365 GList *children, *chead;
1367 chead = children = gtk_container_children(GTK_CONTAINER(list));
1369 n = g_list_length(children);
1373 * Figure out how many list items to a screenful,
1374 * and adjust the step appropriately.
1376 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1377 step--; /* go by one less than that */
1381 while (children != NULL) {
1382 if (item == children->data)
1384 children = children->next;
1389 if (direction < 0 && i > 0)
1390 children = children->prev, i--;
1391 else if (direction > 0 && i < n-1)
1392 children = children->next, i++;
1396 if (children && children->data) {
1398 gtk_list_select_child(GTK_LIST(list),
1399 GTK_WIDGET(children->data));
1400 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1401 gtk_adjustment_clamp_page
1403 adj->lower + (adj->upper-adj->lower) * i / n,
1404 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1415 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1418 return listitem_key(item, event, data, FALSE);
1421 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1424 return listitem_key(item, event, data, TRUE);
1427 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1430 struct dlgparam *dp = (struct dlgparam *)data;
1431 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1432 switch (event->type) {
1434 case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1435 case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1436 case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1441 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1444 struct dlgparam *dp = (struct dlgparam *)data;
1445 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1446 if (uc->nclicks>1) {
1447 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1453 static void list_selchange(GtkList *list, gpointer data)
1455 struct dlgparam *dp = (struct dlgparam *)data;
1456 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1458 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1461 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1463 int index = dlg_listbox_index(uc->ctrl, dp);
1464 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1468 (index == 0 && direction < 0) ||
1469 (index == g_list_length(children)-1 && direction > 0)) {
1474 child = g_list_nth_data(children, index);
1475 gtk_widget_ref(child);
1476 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1477 g_list_free(children);
1480 children = g_list_append(children, child);
1481 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1482 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1483 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1486 static void draglist_up(GtkButton *button, gpointer data)
1488 struct dlgparam *dp = (struct dlgparam *)data;
1489 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1490 draglist_move(dp, uc, -1);
1493 static void draglist_down(GtkButton *button, gpointer data)
1495 struct dlgparam *dp = (struct dlgparam *)data;
1496 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1497 draglist_move(dp, uc, +1);
1500 #else /* !GTK_CHECK_VERSION(2,0,0) */
1503 * GTK 2 list box event handlers.
1506 static void listbox_doubleclick(GtkTreeView *treeview, GtkTreePath *path,
1507 GtkTreeViewColumn *column, gpointer data)
1509 struct dlgparam *dp = (struct dlgparam *)data;
1510 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(treeview));
1512 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1515 static void listbox_selchange(GtkTreeSelection *treeselection,
1518 struct dlgparam *dp = (struct dlgparam *)data;
1519 GtkTreeView *tree = gtk_tree_selection_get_tree_view(treeselection);
1520 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1522 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1525 struct draglist_valchange_ctx {
1527 struct dlgparam *dp;
1530 static gboolean draglist_valchange(gpointer data)
1532 struct draglist_valchange_ctx *ctx =
1533 (struct draglist_valchange_ctx *)data;
1535 ctx->uc->ctrl->generic.handler(ctx->uc->ctrl, ctx->dp,
1536 ctx->dp->data, EVENT_VALCHANGE);
1543 static void listbox_reorder(GtkTreeModel *treemodel, GtkTreePath *path,
1544 GtkTreeIter *iter, gpointer data)
1546 struct dlgparam *dp = (struct dlgparam *)data;
1550 if (dp->flags & FLAG_UPDATING_LISTBOX)
1551 return; /* not a user drag operation */
1553 tree = g_object_get_data(G_OBJECT(treemodel), "user-data");
1554 uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1557 * We should cause EVENT_VALCHANGE on the list box, now
1558 * that its rows have been reordered. However, the GTK 2
1559 * docs say that at the point this signal is received the
1560 * new row might not have actually been filled in yet.
1562 * (So what smegging use is it then, eh? Don't suppose it
1563 * occurred to you at any point that letting the
1564 * application know _after_ the reordering was compelete
1565 * might be helpful to someone?)
1567 * To get round this, I schedule an idle function, which I
1568 * hope won't be called until the main event loop is
1569 * re-entered after the drag-and-drop handler has finished
1570 * furtling with the list store.
1572 struct draglist_valchange_ctx *ctx =
1573 snew(struct draglist_valchange_ctx);
1576 g_idle_add(draglist_valchange, ctx);
1580 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1582 #if !GTK_CHECK_VERSION(2,4,0)
1584 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1586 struct dlgparam *dp = (struct dlgparam *)data;
1587 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1588 gpointer optmenu = g_object_get_data(G_OBJECT(menushell), "user-data");
1589 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1590 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1595 static void droplist_selchange(GtkComboBox *combo, gpointer data)
1597 struct dlgparam *dp = (struct dlgparam *)data;
1598 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(combo));
1600 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1603 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1605 #ifdef USE_GTK_FILE_CHOOSER_DIALOG
1606 static void filechoose_response(GtkDialog *dialog, gint response,
1609 /* struct dlgparam *dp = (struct dlgparam *)data; */
1610 struct uctrl *uc = g_object_get_data(G_OBJECT(dialog), "user-data");
1611 if (response == GTK_RESPONSE_ACCEPT) {
1612 gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1613 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1616 gtk_widget_destroy(GTK_WIDGET(dialog));
1619 static void filesel_ok(GtkButton *button, gpointer data)
1621 /* struct dlgparam *dp = (struct dlgparam *)data; */
1622 gpointer filesel = g_object_get_data(G_OBJECT(button), "user-data");
1623 struct uctrl *uc = g_object_get_data(G_OBJECT(filesel), "user-data");
1624 const char *name = gtk_file_selection_get_filename
1625 (GTK_FILE_SELECTION(filesel));
1626 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1630 static void fontsel_ok(GtkButton *button, gpointer data)
1632 /* struct dlgparam *dp = (struct dlgparam *)data; */
1634 #if !GTK_CHECK_VERSION(2,0,0)
1636 gpointer fontsel = g_object_get_data(G_OBJECT(button), "user-data");
1637 struct uctrl *uc = g_object_get_data(G_OBJECT(fontsel), "user-data");
1638 const char *name = gtk_font_selection_dialog_get_font_name
1639 (GTK_FONT_SELECTION_DIALOG(fontsel));
1640 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1644 unifontsel *fontsel = (unifontsel *)g_object_get_data
1645 (G_OBJECT(button), "user-data");
1646 struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1647 char *name = unifontsel_get_name(fontsel);
1648 assert(name); /* should always be ok after OK pressed */
1649 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1655 #if GTK_CHECK_VERSION(3,0,0)
1657 static void colourchoose_response(GtkDialog *dialog,
1658 gint response_id, gpointer data)
1660 struct dlgparam *dp = (struct dlgparam *)data;
1661 struct uctrl *uc = g_object_get_data(G_OBJECT(dialog), "user-data");
1663 if (response_id == GTK_RESPONSE_OK) {
1665 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(dialog), &rgba);
1666 dp->coloursel_result.r = (int) (255 * rgba.red);
1667 dp->coloursel_result.g = (int) (255 * rgba.green);
1668 dp->coloursel_result.b = (int) (255 * rgba.blue);
1669 dp->coloursel_result.ok = TRUE;
1671 dp->coloursel_result.ok = FALSE;
1674 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1676 gtk_widget_destroy(GTK_WIDGET(dialog));
1679 #else /* GTK 1/2 coloursel response handlers */
1681 static void coloursel_ok(GtkButton *button, gpointer data)
1683 struct dlgparam *dp = (struct dlgparam *)data;
1684 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1685 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1687 #if GTK_CHECK_VERSION(2,0,0)
1689 GtkColorSelection *cs = GTK_COLOR_SELECTION
1690 (gtk_color_selection_dialog_get_color_selection
1691 (GTK_COLOR_SELECTION_DIALOG(coloursel)));
1693 gtk_color_selection_get_current_color(cs, &col);
1694 dp->coloursel_result.r = col.red / 0x0100;
1695 dp->coloursel_result.g = col.green / 0x0100;
1696 dp->coloursel_result.b = col.blue / 0x0100;
1700 GtkColorSelection *cs = GTK_COLOR_SELECTION
1701 (gtk_color_selection_dialog_get_color_selection
1702 (GTK_COLOR_SELECTION_DIALOG(coloursel)));
1704 gtk_color_selection_get_color(cs, cvals);
1705 dp->coloursel_result.r = (int) (255 * cvals[0]);
1706 dp->coloursel_result.g = (int) (255 * cvals[1]);
1707 dp->coloursel_result.b = (int) (255 * cvals[2]);
1710 dp->coloursel_result.ok = TRUE;
1711 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1714 static void coloursel_cancel(GtkButton *button, gpointer data)
1716 struct dlgparam *dp = (struct dlgparam *)data;
1717 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1718 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1719 dp->coloursel_result.ok = FALSE;
1720 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1723 #endif /* end of coloursel response handlers */
1725 static void filefont_clicked(GtkButton *button, gpointer data)
1727 struct dlgparam *dp = (struct dlgparam *)data;
1728 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1730 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1731 #ifdef USE_GTK_FILE_CHOOSER_DIALOG
1732 GtkWidget *filechoose = gtk_file_chooser_dialog_new
1733 (uc->ctrl->fileselect.title, GTK_WINDOW(dp->window),
1734 (uc->ctrl->fileselect.for_writing ?
1735 GTK_FILE_CHOOSER_ACTION_SAVE :
1736 GTK_FILE_CHOOSER_ACTION_OPEN),
1737 STANDARD_CANCEL_LABEL, GTK_RESPONSE_CANCEL,
1738 STANDARD_OPEN_LABEL, GTK_RESPONSE_ACCEPT,
1739 (const gchar *)NULL);
1740 gtk_window_set_modal(GTK_WINDOW(filechoose), TRUE);
1741 g_object_set_data(G_OBJECT(filechoose), "user-data", (gpointer)uc);
1742 g_signal_connect(G_OBJECT(filechoose), "response",
1743 G_CALLBACK(filechoose_response), (gpointer)dp);
1744 gtk_widget_show(filechoose);
1746 GtkWidget *filesel =
1747 gtk_file_selection_new(uc->ctrl->fileselect.title);
1748 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1750 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1752 g_object_set_data(G_OBJECT(filesel), "user-data", (gpointer)uc);
1754 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1755 G_CALLBACK(filesel_ok), (gpointer)dp);
1756 g_signal_connect_swapped
1757 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1758 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1759 g_signal_connect_swapped
1760 (G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1761 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1762 gtk_widget_show(filesel);
1766 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1767 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1769 #if !GTK_CHECK_VERSION(2,0,0)
1772 * Use the GTK 1 standard font selector.
1775 gchar *spacings[] = { "c", "m", NULL };
1776 GtkWidget *fontsel =
1777 gtk_font_selection_dialog_new("Select a font");
1778 gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1779 gtk_font_selection_dialog_set_filter
1780 (GTK_FONT_SELECTION_DIALOG(fontsel),
1781 GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1782 NULL, NULL, NULL, NULL, spacings, NULL);
1783 if (!gtk_font_selection_dialog_set_font_name
1784 (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1786 * If the font name wasn't found as it was, try opening
1787 * it and extracting its FONT property. This should
1788 * have the effect of mapping short aliases into true
1791 GdkFont *font = gdk_font_load(fontname);
1793 XFontStruct *xfs = GDK_FONT_XFONT(font);
1794 Display *disp = GDK_FONT_XDISPLAY(font);
1795 Atom fontprop = XInternAtom(disp, "FONT", False);
1798 if (XGetFontProperty(xfs, fontprop, &ret)) {
1799 char *name = XGetAtomName(disp, (Atom)ret);
1801 gtk_font_selection_dialog_set_font_name
1802 (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1804 gdk_font_unref(font);
1808 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1809 "user-data", (gpointer)fontsel);
1810 g_object_set_data(G_OBJECT(fontsel), "user-data", (gpointer)uc);
1812 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1813 "clicked", G_CALLBACK(fontsel_ok), (gpointer)dp);
1814 g_signal_connect_swapped
1815 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1816 "clicked", G_CALLBACK(gtk_widget_destroy),
1818 g_signal_connect_swapped
1819 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1820 "clicked", G_CALLBACK(gtk_widget_destroy),
1822 gtk_widget_show(fontsel);
1824 #else /* !GTK_CHECK_VERSION(2,0,0) */
1827 * Use the unifontsel code provided in gtkfont.c.
1830 unifontsel *fontsel = unifontsel_new("Select a font");
1832 gtk_window_set_modal(fontsel->window, TRUE);
1833 unifontsel_set_name(fontsel, fontname);
1835 g_object_set_data(G_OBJECT(fontsel->ok_button),
1836 "user-data", (gpointer)fontsel);
1837 fontsel->user_data = uc;
1838 g_signal_connect(G_OBJECT(fontsel->ok_button), "clicked",
1839 G_CALLBACK(fontsel_ok), (gpointer)dp);
1840 g_signal_connect_swapped(G_OBJECT(fontsel->ok_button), "clicked",
1841 G_CALLBACK(unifontsel_destroy),
1843 g_signal_connect_swapped(G_OBJECT(fontsel->cancel_button),"clicked",
1844 G_CALLBACK(unifontsel_destroy),
1847 gtk_widget_show(GTK_WIDGET(fontsel->window));
1849 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1854 #if !GTK_CHECK_VERSION(3,0,0)
1855 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1858 struct dlgparam *dp = (struct dlgparam *)data;
1859 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1861 gtk_widget_set_size_request(uc->text, alloc->width, -1);
1862 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1863 g_signal_handler_disconnect(G_OBJECT(uc->text), uc->textsig);
1867 /* ----------------------------------------------------------------------
1868 * This function does the main layout work: it reads a controlset,
1869 * it creates the relevant GTK controls, and returns a GtkWidget
1870 * containing the result. (This widget might be a title of some
1871 * sort, it might be a Columns containing many controls, or it
1872 * might be a GtkFrame containing a Columns; whatever it is, it's
1873 * definitely a GtkWidget and should probably be added to a
1876 * `win' is required for setting the default button. If it is
1877 * non-NULL, all buttons created will be default-capable (so they
1878 * have extra space round them for the default highlight).
1880 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1881 struct controlset *s, GtkWindow *win)
1887 if (!s->boxname && s->boxtitle) {
1888 /* This controlset is a panel title. */
1889 return gtk_label_new(s->boxtitle);
1893 * Otherwise, we expect to be laying out actual controls, so
1894 * we'll start by creating a Columns for the purpose.
1896 cols = COLUMNS(columns_new(4));
1897 ret = GTK_WIDGET(cols);
1898 gtk_widget_show(ret);
1901 * Create a containing frame if we have a box name.
1904 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1905 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1906 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1907 gtk_widget_show(ret);
1911 * Now iterate through the controls themselves, create them,
1912 * and add them to the Columns.
1914 for (i = 0; i < s->ncontrols; i++) {
1915 union control *ctrl = s->ctrls[i];
1918 GtkWidget *w = NULL;
1920 switch (ctrl->generic.type) {
1923 static const int simplecols[1] = { 100 };
1924 columns_set_cols(cols, ctrl->columns.ncols,
1925 (ctrl->columns.percentages ?
1926 ctrl->columns.percentages : simplecols));
1928 continue; /* no actual control created */
1931 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1933 columns_taborder_last(cols, uc->toplevel);
1935 continue; /* no actual control created */
1938 uc = snew(struct uctrl);
1942 #if !GTK_CHECK_VERSION(2,4,0)
1943 uc->list = uc->menu = uc->optmenu = NULL;
1947 #if GTK_CHECK_VERSION(2,0,0)
1948 uc->treeview = NULL;
1949 uc->listmodel = NULL;
1951 uc->button = uc->text = NULL;
1955 switch (ctrl->generic.type) {
1957 w = gtk_button_new_with_label(ctrl->generic.label);
1959 gtk_widget_set_can_default(w, TRUE);
1960 if (ctrl->button.isdefault)
1961 gtk_window_set_default(win, w);
1962 if (ctrl->button.iscancel)
1963 dp->cancelbutton = w;
1965 g_signal_connect(G_OBJECT(w), "clicked",
1966 G_CALLBACK(button_clicked), dp);
1967 g_signal_connect(G_OBJECT(w), "focus_in_event",
1968 G_CALLBACK(widget_focus), dp);
1969 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1970 ctrl->button.shortcut, SHORTCUT_UCTRL, uc);
1973 w = gtk_check_button_new_with_label(ctrl->generic.label);
1974 g_signal_connect(G_OBJECT(w), "toggled",
1975 G_CALLBACK(button_toggled), dp);
1976 g_signal_connect(G_OBJECT(w), "focus_in_event",
1977 G_CALLBACK(widget_focus), dp);
1978 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1979 ctrl->checkbox.shortcut, SHORTCUT_UCTRL, uc);
1984 * Radio buttons get to go inside their own Columns, no
1988 gint i, *percentages;
1992 if (ctrl->generic.label) {
1993 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1994 columns_add(COLUMNS(w), label, 0, 1);
1995 columns_force_left_align(COLUMNS(w), label);
1996 gtk_widget_show(label);
1997 shortcut_add(scs, label, ctrl->radio.shortcut,
1998 SHORTCUT_UCTRL, uc);
2001 percentages = g_new(gint, ctrl->radio.ncolumns);
2002 for (i = 0; i < ctrl->radio.ncolumns; i++) {
2004 ((100 * (i+1) / ctrl->radio.ncolumns) -
2005 100 * i / ctrl->radio.ncolumns);
2007 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
2009 g_free(percentages);
2012 uc->nbuttons = ctrl->radio.nbuttons;
2013 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
2015 for (i = 0; i < ctrl->radio.nbuttons; i++) {
2019 b = (gtk_radio_button_new_with_label
2020 (group, ctrl->radio.buttons[i]));
2022 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(b));
2023 colstart = i % ctrl->radio.ncolumns;
2024 columns_add(COLUMNS(w), b, colstart,
2025 (i == ctrl->radio.nbuttons-1 ?
2026 ctrl->radio.ncolumns - colstart : 1));
2027 columns_force_left_align(COLUMNS(w), b);
2029 g_signal_connect(G_OBJECT(b), "toggled",
2030 G_CALLBACK(button_toggled), dp);
2031 g_signal_connect(G_OBJECT(b), "focus_in_event",
2032 G_CALLBACK(widget_focus), dp);
2033 if (ctrl->radio.shortcuts) {
2034 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(b)),
2035 ctrl->radio.shortcuts[i],
2036 SHORTCUT_UCTRL, uc);
2044 GtkWidget *signalobject;
2046 if (ctrl->editbox.has_list) {
2047 #if !GTK_CHECK_VERSION(2,4,0)
2051 w = gtk_combo_new();
2052 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
2053 uc->entry = GTK_COMBO(w)->entry;
2054 uc->list = GTK_COMBO(w)->list;
2055 signalobject = uc->entry;
2060 uc->listmodel = gtk_list_store_new(2, G_TYPE_INT,
2062 w = gtk_combo_box_new_with_model_and_entry
2063 (GTK_TREE_MODEL(uc->listmodel));
2064 /* We cannot support password combo boxes. */
2065 assert(!ctrl->editbox.password);
2067 signalobject = uc->combo;
2070 w = gtk_entry_new();
2071 if (ctrl->editbox.password)
2072 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
2077 g_signal_connect(G_OBJECT(signalobject), "changed",
2078 G_CALLBACK(editbox_changed), dp);
2079 g_signal_connect(G_OBJECT(signalobject), "key_press_event",
2080 G_CALLBACK(editbox_key), dp);
2081 g_signal_connect(G_OBJECT(signalobject), "focus_in_event",
2082 G_CALLBACK(widget_focus), dp);
2083 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
2084 G_CALLBACK(editbox_lostfocus), dp);
2085 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
2086 G_CALLBACK(editbox_lostfocus), dp);
2089 * Find out the edit box's height, which we'll need
2090 * for vertical centring below (and, in GTK2, size
2091 * tweaking as well).
2093 gtk_widget_size_request(w, &req);
2095 #if !GTK_CHECK_VERSION(3,0,0)
2097 * Edit boxes, for some strange reason, have a minimum
2098 * width of 150 in GTK 1.2. We don't want this - we'd
2099 * rather the edit boxes acquired their natural width
2100 * from the column layout of the rest of the box.
2102 gtk_widget_set_size_request(w, 10, req.height);
2105 * In GTK 3, this is still true, but there's a special
2106 * method for GtkEntry in particular to fix it.
2108 if (GTK_IS_ENTRY(w))
2109 gtk_entry_set_width_chars(GTK_ENTRY(w), 1);
2112 if (ctrl->generic.label) {
2113 GtkWidget *label, *container;
2115 label = gtk_label_new(ctrl->generic.label);
2117 shortcut_add(scs, label, ctrl->editbox.shortcut,
2118 SHORTCUT_FOCUS, uc->entry);
2120 container = columns_new(4);
2121 if (ctrl->editbox.percentwidth == 100) {
2122 columns_add(COLUMNS(container), label, 0, 1);
2123 columns_force_left_align(COLUMNS(container), label);
2124 columns_add(COLUMNS(container), w, 0, 1);
2126 gint percentages[2];
2127 percentages[1] = ctrl->editbox.percentwidth;
2128 percentages[0] = 100 - ctrl->editbox.percentwidth;
2129 columns_set_cols(COLUMNS(container), 2, percentages);
2130 columns_add(COLUMNS(container), label, 0, 1);
2131 columns_force_left_align(COLUMNS(container), label);
2132 columns_add(COLUMNS(container), w, 1, 1);
2133 /* Centre the label vertically. */
2134 gtk_widget_set_size_request(label, -1, req.height);
2135 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2137 gtk_widget_show(label);
2145 case CTRL_FILESELECT:
2146 case CTRL_FONTSELECT:
2150 const char *browsebtn =
2151 (ctrl->generic.type == CTRL_FILESELECT ?
2152 "Browse..." : "Change...");
2154 gint percentages[] = { 75, 25 };
2156 columns_set_cols(COLUMNS(w), 2, percentages);
2158 if (ctrl->generic.label) {
2159 ww = gtk_label_new(ctrl->generic.label);
2160 columns_add(COLUMNS(w), ww, 0, 2);
2161 columns_force_left_align(COLUMNS(w), ww);
2162 gtk_widget_show(ww);
2163 shortcut_add(scs, ww,
2164 (ctrl->generic.type == CTRL_FILESELECT ?
2165 ctrl->fileselect.shortcut :
2166 ctrl->fontselect.shortcut),
2167 SHORTCUT_UCTRL, uc);
2171 uc->entry = ww = gtk_entry_new();
2172 gtk_widget_size_request(ww, &req);
2173 gtk_widget_set_size_request(ww, 10, req.height);
2174 columns_add(COLUMNS(w), ww, 0, 1);
2175 gtk_widget_show(ww);
2177 uc->button = ww = gtk_button_new_with_label(browsebtn);
2178 columns_add(COLUMNS(w), ww, 1, 1);
2179 gtk_widget_show(ww);
2181 g_signal_connect(G_OBJECT(uc->entry), "key_press_event",
2182 G_CALLBACK(editbox_key), dp);
2184 g_signal_connect(G_OBJECT(uc->entry), "changed",
2185 G_CALLBACK(editbox_changed), dp);
2186 g_signal_connect(G_OBJECT(uc->entry), "focus_in_event",
2187 G_CALLBACK(widget_focus), dp);
2188 g_signal_connect(G_OBJECT(uc->button), "focus_in_event",
2189 G_CALLBACK(widget_focus), dp);
2190 g_signal_connect(G_OBJECT(ww), "clicked",
2191 G_CALLBACK(filefont_clicked), dp);
2196 #if GTK_CHECK_VERSION(2,0,0)
2198 * First construct the list data store, with the right
2199 * number of columns.
2201 # if !GTK_CHECK_VERSION(2,4,0)
2202 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2203 * because combo boxes are still done the old GTK1 way.) */
2204 if (ctrl->listbox.height > 0)
2211 cols = ctrl->listbox.ncols;
2212 cols = cols ? cols : 1;
2213 types = snewn(1 + cols, GType);
2215 types[0] = G_TYPE_INT;
2216 for (i = 0; i < cols; i++)
2217 types[i+1] = G_TYPE_STRING;
2219 uc->listmodel = gtk_list_store_newv(1 + cols, types);
2226 * See if it's a drop-down list (non-editable combo
2229 if (ctrl->listbox.height == 0) {
2230 #if !GTK_CHECK_VERSION(2,4,0)
2232 * GTK1 and early-GTK2 option-menu style of
2235 uc->optmenu = w = gtk_option_menu_new();
2236 uc->menu = gtk_menu_new();
2237 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
2238 g_object_set_data(G_OBJECT(uc->menu), "user-data",
2239 (gpointer)uc->optmenu);
2240 g_signal_connect(G_OBJECT(uc->optmenu), "focus_in_event",
2241 G_CALLBACK(widget_focus), dp);
2244 * Late-GTK2 style using a GtkComboBox.
2246 GtkCellRenderer *cr;
2249 * Create a non-editable GtkComboBox (that is, not
2250 * its subclass GtkComboBoxEntry).
2252 w = gtk_combo_box_new_with_model
2253 (GTK_TREE_MODEL(uc->listmodel));
2257 * Tell it how to render a list item (i.e. which
2258 * column to look at in the list model).
2260 cr = gtk_cell_renderer_text_new();
2261 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), cr, TRUE);
2262 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), cr,
2266 * And tell it to notify us when the selection
2269 g_signal_connect(G_OBJECT(w), "changed",
2270 G_CALLBACK(droplist_selchange), dp);
2273 #if !GTK_CHECK_VERSION(2,0,0)
2275 * GTK1-style full list box.
2277 uc->list = gtk_list_new();
2278 if (ctrl->listbox.multisel == 2) {
2279 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2280 GTK_SELECTION_EXTENDED);
2281 } else if (ctrl->listbox.multisel == 1) {
2282 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2283 GTK_SELECTION_MULTIPLE);
2285 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2286 GTK_SELECTION_SINGLE);
2288 w = gtk_scrolled_window_new(NULL, NULL);
2289 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
2291 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
2293 GTK_POLICY_AUTOMATIC);
2294 uc->adj = gtk_scrolled_window_get_vadjustment
2295 (GTK_SCROLLED_WINDOW(w));
2297 gtk_widget_show(uc->list);
2298 g_signal_connect(G_OBJECT(uc->list), "selection-changed",
2299 G_CALLBACK(list_selchange), dp);
2300 g_signal_connect(G_OBJECT(uc->list), "focus_in_event",
2301 G_CALLBACK(widget_focus), dp);
2304 * Adjust the height of the scrolled window to the
2305 * minimum given by the height parameter.
2307 * This piece of guesswork is a horrid hack based
2308 * on looking inside the GTK 1.2 sources
2309 * (specifically gtkviewport.c, which appears to be
2310 * the widget which provides the border around the
2311 * scrolling area). Anyone lets me know how I can
2312 * do this in a way which isn't at risk from GTK
2313 * upgrades, I'd be grateful.
2317 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
2318 gtk_widget_set_size_request(w, 10,
2319 2*edge + (ctrl->listbox.height *
2320 get_listitemheight(w)));
2323 if (ctrl->listbox.draglist) {
2325 * GTK doesn't appear to make it easy to
2326 * implement a proper draggable list; so
2327 * instead I'm just going to have to put an Up
2328 * and a Down button to the right of the actual
2329 * list box. Ah well.
2331 GtkWidget *cols, *button;
2332 static const gint percentages[2] = { 80, 20 };
2334 cols = columns_new(4);
2335 columns_set_cols(COLUMNS(cols), 2, percentages);
2336 columns_add(COLUMNS(cols), w, 0, 1);
2338 button = gtk_button_new_with_label("Up");
2339 columns_add(COLUMNS(cols), button, 1, 1);
2340 gtk_widget_show(button);
2341 g_signal_connect(G_OBJECT(button), "clicked",
2342 G_CALLBACK(draglist_up), dp);
2343 g_signal_connect(G_OBJECT(button), "focus_in_event",
2344 G_CALLBACK(widget_focus), dp);
2345 button = gtk_button_new_with_label("Down");
2346 columns_add(COLUMNS(cols), button, 1, 1);
2347 gtk_widget_show(button);
2348 g_signal_connect(G_OBJECT(button), "clicked",
2349 G_CALLBACK(draglist_down), dp);
2350 g_signal_connect(G_OBJECT(button), "focus_in_event",
2351 G_CALLBACK(widget_focus), dp);
2357 * GTK2 treeview-based full list box.
2359 GtkTreeSelection *sel;
2362 * Create the list box itself, its columns, and
2363 * its containing scrolled window.
2365 w = gtk_tree_view_new_with_model
2366 (GTK_TREE_MODEL(uc->listmodel));
2367 g_object_set_data(G_OBJECT(uc->listmodel), "user-data",
2369 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2370 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
2371 gtk_tree_selection_set_mode
2372 (sel, ctrl->listbox.multisel ? GTK_SELECTION_MULTIPLE :
2373 GTK_SELECTION_SINGLE);
2375 g_signal_connect(G_OBJECT(w), "row-activated",
2376 G_CALLBACK(listbox_doubleclick), dp);
2377 g_signal_connect(G_OBJECT(sel), "changed",
2378 G_CALLBACK(listbox_selchange), dp);
2380 if (ctrl->listbox.draglist) {
2381 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w), TRUE);
2382 g_signal_connect(G_OBJECT(uc->listmodel), "row-inserted",
2383 G_CALLBACK(listbox_reorder), dp);
2390 cols = ctrl->listbox.ncols;
2391 cols = cols ? cols : 1;
2392 for (i = 0; i < cols; i++) {
2393 GtkTreeViewColumn *column;
2394 GtkCellRenderer *cellrend;
2396 * It appears that GTK 2 doesn't leave us any
2397 * particularly sensible way to honour the
2398 * "percentages" specification in the ctrl
2401 cellrend = gtk_cell_renderer_text_new();
2402 if (!ctrl->listbox.hscroll) {
2403 g_object_set(G_OBJECT(cellrend),
2404 "ellipsize", PANGO_ELLIPSIZE_END,
2405 "ellipsize-set", TRUE,
2408 column = gtk_tree_view_column_new_with_attributes
2409 ("heading", cellrend, "text", i+1, (char *)NULL);
2410 gtk_tree_view_column_set_sizing
2411 (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2412 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2419 scroll = gtk_scrolled_window_new(NULL, NULL);
2420 gtk_scrolled_window_set_shadow_type
2421 (GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
2423 gtk_container_add(GTK_CONTAINER(scroll), w);
2424 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2425 GTK_POLICY_AUTOMATIC,
2427 gtk_widget_set_size_request
2429 ctrl->listbox.height * get_listitemheight(w));
2436 if (ctrl->generic.label) {
2437 GtkWidget *label, *container;
2440 label = gtk_label_new(ctrl->generic.label);
2441 gtk_label_set_width_chars(GTK_LABEL(label), 3);
2443 shortcut_add(scs, label, ctrl->listbox.shortcut,
2446 container = columns_new(4);
2447 if (ctrl->listbox.percentwidth == 100) {
2448 columns_add(COLUMNS(container), label, 0, 1);
2449 columns_force_left_align(COLUMNS(container), label);
2450 columns_add(COLUMNS(container), w, 0, 1);
2452 gint percentages[2];
2453 percentages[1] = ctrl->listbox.percentwidth;
2454 percentages[0] = 100 - ctrl->listbox.percentwidth;
2455 columns_set_cols(COLUMNS(container), 2, percentages);
2456 columns_add(COLUMNS(container), label, 0, 1);
2457 columns_force_left_align(COLUMNS(container), label);
2458 columns_add(COLUMNS(container), w, 1, 1);
2459 /* Centre the label vertically. */
2460 gtk_widget_size_request(w, &req);
2461 gtk_widget_set_size_request(label, -1, req.height);
2462 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2464 gtk_widget_show(label);
2473 #if !GTK_CHECK_VERSION(3,0,0)
2475 * Wrapping text widgets don't sit well with the GTK2
2476 * layout model, in which widgets state a minimum size
2477 * and the whole window then adjusts to the smallest
2478 * size it can sensibly take given its contents. A
2479 * wrapping text widget _has_ no clear minimum size;
2480 * instead it has a range of possibilities. It can be
2481 * one line deep but 2000 wide, or two lines deep and
2482 * 1000 pixels, or three by 867, or four by 500 and so
2483 * on. It can be as short as you like provided you
2484 * don't mind it being wide, or as narrow as you like
2485 * provided you don't mind it being tall.
2487 * Therefore, it fits very badly into the layout model.
2488 * Hence the only thing to do is pick a width and let
2489 * it choose its own number of lines. To do this I'm
2490 * going to cheat a little. All new wrapping text
2491 * widgets will be created with a minimal text content
2492 * "X"; then, after the rest of the dialog box is set
2493 * up and its size calculated, the text widgets will be
2494 * told their width and given their real text, which
2495 * will cause the size to be recomputed in the y
2496 * direction (because many of them will expand to more
2499 uc->text = w = gtk_label_new("X");
2501 g_signal_connect(G_OBJECT(w), "size-allocate",
2502 G_CALLBACK(label_sizealloc), dp);
2505 * In GTK3, this is all fixed, because the main aim of the
2506 * new 'height-for-width' geometry management is to make
2507 * wrapping labels behave sensibly. So now we can just do
2508 * the obvious thing.
2510 uc->text = w = gtk_label_new(uc->ctrl->generic.label);
2512 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
2513 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
2519 columns_add(cols, w,
2520 COLUMN_START(ctrl->generic.column),
2521 COLUMN_SPAN(ctrl->generic.column));
2523 columns_force_left_align(cols, w);
2527 dlg_add_uctrl(dp, uc);
2534 struct dlgparam *dp;
2535 GtkNotebook *panels;
2537 #if !GTK_CHECK_VERSION(2,0,0)
2538 GtkWidget *treeitem;
2541 GtkTreePath *treepath;
2543 struct Shortcuts shortcuts;
2546 #if GTK_CHECK_VERSION(2,0,0)
2547 static void treeselection_changed(GtkTreeSelection *treeselection,
2550 struct selparam *sps = (struct selparam *)data, *sp;
2551 GtkTreeModel *treemodel;
2552 GtkTreeIter treeiter;
2556 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2559 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
2562 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2563 gtk_notebook_set_current_page(sp->panels, page_num);
2565 dlg_refresh(NULL, sp->dp);
2567 sp->dp->shortcuts = &sp->shortcuts;
2570 static void treeitem_sel(GtkItem *item, gpointer data)
2572 struct selparam *sp = (struct selparam *)data;
2575 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2576 gtk_notebook_set_page(sp->panels, page_num);
2578 dlg_refresh(NULL, sp->dp);
2580 sp->dp->shortcuts = &sp->shortcuts;
2581 sp->dp->currtreeitem = sp->treeitem;
2585 static void window_destroy(GtkWidget *widget, gpointer data)
2590 #if !GTK_CHECK_VERSION(2,0,0)
2591 static int tree_grab_focus(struct dlgparam *dp)
2596 * See if any of the treeitems has the focus.
2599 for (i = 0; i < dp->ntreeitems; i++)
2600 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
2608 gtk_widget_grab_focus(dp->currtreeitem);
2613 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
2616 struct dlgparam *dp = (struct dlgparam *)data;
2618 g_signal_stop_emission_by_name(G_OBJECT(container), "focus");
2620 * If there's a focused treeitem, we return FALSE to cause the
2621 * focus to move on to some totally other control. If not, we
2622 * focus the selected one.
2624 return tree_grab_focus(dp);
2628 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2630 struct dlgparam *dp = (struct dlgparam *)data;
2632 if (event->keyval == GDK_KEY_Escape && dp->cancelbutton) {
2633 g_signal_emit_by_name(G_OBJECT(dp->cancelbutton), "clicked");
2637 if ((event->state & GDK_MOD1_MASK) &&
2638 (unsigned char)event->string[0] > 0 &&
2639 (unsigned char)event->string[0] <= 127) {
2640 int schr = (unsigned char)event->string[0];
2641 struct Shortcut *sc = &dp->shortcuts->sc[schr];
2643 switch (sc->action) {
2645 #if GTK_CHECK_VERSION(2,0,0)
2646 gtk_widget_grab_focus(sc->widget);
2648 tree_grab_focus(dp);
2651 case SHORTCUT_FOCUS:
2652 gtk_widget_grab_focus(sc->widget);
2654 case SHORTCUT_UCTRL:
2656 * We must do something sensible with a uctrl.
2657 * Precisely what this is depends on the type of
2660 switch (sc->uc->ctrl->generic.type) {
2663 /* Check boxes and buttons get the focus _and_ get toggled. */
2664 gtk_widget_grab_focus(sc->uc->toplevel);
2665 g_signal_emit_by_name(G_OBJECT(sc->uc->toplevel), "clicked");
2667 case CTRL_FILESELECT:
2668 case CTRL_FONTSELECT:
2669 /* File/font selectors have their buttons pressed (ooer),
2670 * and focus transferred to the edit box. */
2671 g_signal_emit_by_name(G_OBJECT(sc->uc->button), "clicked");
2672 gtk_widget_grab_focus(sc->uc->entry);
2676 * Radio buttons are fun, because they have
2677 * multiple shortcuts. We must find whether the
2678 * activated shortcut is the shortcut for the whole
2679 * group, or for a particular button. In the former
2680 * case, we find the currently selected button and
2681 * focus it; in the latter, we focus-and-click the
2682 * button whose shortcut was pressed.
2684 if (schr == sc->uc->ctrl->radio.shortcut) {
2686 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2687 if (gtk_toggle_button_get_active
2688 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
2689 gtk_widget_grab_focus(sc->uc->buttons[i]);
2691 } else if (sc->uc->ctrl->radio.shortcuts) {
2693 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2694 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
2695 gtk_widget_grab_focus(sc->uc->buttons[i]);
2696 g_signal_emit_by_name
2697 (G_OBJECT(sc->uc->buttons[i]), "clicked");
2703 #if !GTK_CHECK_VERSION(2,4,0)
2704 if (sc->uc->optmenu) {
2708 gtk_widget_grab_focus(sc->uc->optmenu);
2709 /* Option menus don't work using the "clicked" signal.
2710 * We need to manufacture a button press event :-/ */
2711 bev.type = GDK_BUTTON_PRESS;
2713 g_signal_emit_by_name(G_OBJECT(sc->uc->optmenu),
2714 "button_press_event",
2719 if (sc->uc->combo) {
2720 gtk_widget_grab_focus(sc->uc->combo);
2721 gtk_combo_box_popup(GTK_COMBO_BOX(sc->uc->combo));
2725 #if !GTK_CHECK_VERSION(2,0,0)
2728 * For GTK-1 style list boxes, we tell it to
2729 * focus one of its children, which appears to
2730 * do the Right Thing.
2732 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
2733 GTK_DIR_TAB_FORWARD);
2737 if (sc->uc->treeview) {
2738 gtk_widget_grab_focus(sc->uc->treeview);
2742 assert(!"We shouldn't get here");
2752 #if !GTK_CHECK_VERSION(2,0,0)
2753 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2755 struct dlgparam *dp = (struct dlgparam *)data;
2757 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2758 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2760 for (i = 0; i < dp->ntreeitems; i++)
2761 if (widget == dp->treeitems[i])
2763 if (i < dp->ntreeitems) {
2764 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2771 if (i < 0 || i >= dp->ntreeitems)
2772 break; /* nothing in that dir to select */
2774 * Determine if this tree item is visible.
2777 GtkWidget *w = dp->treeitems[i];
2779 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2780 if (!GTK_WIDGET_VISIBLE(w)) {
2787 j = i; /* got one */
2793 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2795 g_signal_emit_by_name(G_OBJECT(dp->treeitems[j]), "toggle");
2796 gtk_widget_grab_focus(dp->treeitems[j]);
2802 * It's nice for Left and Right to expand and collapse tree
2805 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2806 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2807 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2810 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2811 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2812 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2820 static void shortcut_highlight(GtkWidget *labelw, int chr)
2822 GtkLabel *label = GTK_LABEL(labelw);
2823 const gchar *currstr;
2827 #if !GTK_CHECK_VERSION(2,0,0)
2829 gchar *currstr_nonconst;
2830 gtk_label_get(label, &currstr_nonconst);
2831 currstr = currstr_nonconst;
2834 currstr = gtk_label_get_text(label);
2837 for (i = 0; currstr[i]; i++)
2838 if (tolower((unsigned char)currstr[i]) == chr) {
2841 pattern = dupprintf("%*s_", i, "");
2843 gtk_widget_size_request(GTK_WIDGET(label), &req);
2844 gtk_label_set_pattern(label, pattern);
2845 gtk_widget_set_size_request(GTK_WIDGET(label), -1, req.height);
2852 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2853 int chr, int action, void *ptr)
2855 if (chr == NO_SHORTCUT)
2858 chr = tolower((unsigned char)chr);
2860 assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2862 scs->sc[chr].action = action;
2864 if (action == SHORTCUT_FOCUS) {
2865 scs->sc[chr].uc = NULL;
2866 scs->sc[chr].widget = (GtkWidget *)ptr;
2868 scs->sc[chr].widget = NULL;
2869 scs->sc[chr].uc = (struct uctrl *)ptr;
2872 shortcut_highlight(labelw, chr);
2875 int get_listitemheight(GtkWidget *w)
2877 #if !GTK_CHECK_VERSION(2,0,0)
2878 GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2880 gtk_widget_size_request(listitem, &req);
2881 g_object_ref_sink(G_OBJECT(listitem));
2885 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
2886 #if GTK_CHECK_VERSION(3,0,0)
2890 * Since none of my list items wraps in this GUI, no
2891 * interesting width-for-height behaviour should be happening,
2892 * so I don't think it should matter here whether I ask for
2893 * the minimum or natural height.
2895 gtk_cell_renderer_get_preferred_size(cr, w, &req, NULL);
2896 height = req.height;
2899 gtk_cell_renderer_get_size(cr, w, NULL, NULL, NULL, NULL, &height);
2901 g_object_ref(G_OBJECT(cr));
2902 g_object_ref_sink(G_OBJECT(cr));
2903 g_object_unref(G_OBJECT(cr));
2908 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2910 #if !GTK_CHECK_VERSION(2,0,0)
2913 * In GTK 1, laying out the buttons at the bottom of the
2914 * configuration box is nice and easy, because a GtkDialog's
2915 * action_area is a GtkHBox which stretches to cover the full
2916 * width of the dialog. So we just put our Columns widget
2917 * straight into that hbox, and it ends up just where we want
2920 gtk_box_pack_start(GTK_BOX(dlg->action_area), w, TRUE, TRUE, 0);
2924 * In GTK 2, the action area is now a GtkHButtonBox and its
2925 * layout behaviour seems to be different: it doesn't stretch
2926 * to cover the full width of the window, but instead finds its
2927 * own preferred width and right-aligns that within the window.
2928 * This isn't what we want, because we have both left-aligned
2929 * and right-aligned buttons coming out of the above call to
2930 * layout_ctrls(), and right-aligning the whole thing will
2931 * result in the former being centred and looking weird.
2933 * So instead we abandon the dialog's action area completely:
2934 * we gtk_widget_hide() it in the below code, and we also call
2935 * gtk_dialog_set_has_separator() to remove the separator above
2936 * it. We then insert our own action area into the end of the
2937 * dialog's main vbox, and add our own separator above that.
2939 * (Ideally, if we were a native GTK app, we would use the
2940 * GtkHButtonBox's _own_ innate ability to support one set of
2941 * buttons being right-aligned and one left-aligned. But to do
2942 * that here, we would have to either (a) pick apart our cross-
2943 * platform layout structures and treat them specially for this
2944 * particular set of controls, which would be painful, or else
2945 * (b) develop a special and simpler cross-platform
2946 * representation for these particular controls, and introduce
2947 * special-case code into all the _other_ platforms to handle
2948 * it. Neither appeals. Therefore, I regretfully discard the
2949 * GTKHButtonBox and go it alone.)
2953 align = gtk_alignment_new(0, 0, 1, 1);
2954 gtk_container_add(GTK_CONTAINER(align), w);
2956 * The purpose of this GtkAlignment is to provide padding
2957 * around the buttons. The padding we use is twice the padding
2958 * used in our GtkColumns, because we nest two GtkColumns most
2959 * of the time (one separating the tree view from the main
2960 * controls, and another for the main controls themselves).
2962 #if GTK_CHECK_VERSION(2,4,0)
2963 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2965 gtk_widget_show(align);
2966 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2967 align, FALSE, TRUE, 0);
2968 w = gtk_hseparator_new();
2969 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2972 gtk_widget_hide(gtk_dialog_get_action_area(dlg));
2973 #if !GTK_CHECK_VERSION(3,0,0)
2974 /* This cosmetic property is withdrawn in GTK 3's GtkDialog */
2975 g_object_set(G_OBJECT(dlg), "has-separator", TRUE, (const char *)NULL);
2980 int do_config_box(const char *title, Conf *conf, int midsession,
2983 GtkWidget *window, *hbox, *vbox, *cols, *label,
2984 *tree, *treescroll, *panels, *panelvbox;
2985 int index, level, protocol;
2986 struct controlbox *ctrlbox;
2988 #if GTK_CHECK_VERSION(2,0,0)
2989 GtkTreeStore *treestore;
2990 GtkCellRenderer *treerenderer;
2991 GtkTreeViewColumn *treecolumn;
2992 GtkTreeSelection *treeselection;
2993 GtkTreeIter treeiterlevels[8];
2995 GtkTreeItem *treeitemlevels[8];
2996 GtkTree *treelevels[8];
2999 struct Shortcuts scs;
3001 struct selparam *selparams = NULL;
3002 int nselparams = 0, selparamsize = 0;
3006 for (index = 0; index < lenof(scs.sc); index++) {
3007 scs.sc[index].action = SHORTCUT_EMPTY;
3010 window = gtk_dialog_new();
3012 ctrlbox = ctrl_new_box();
3013 protocol = conf_get_int(conf, CONF_protocol);
3014 setup_config_box(ctrlbox, midsession, protocol, protcfginfo);
3015 unix_setup_config_box(ctrlbox, midsession, protocol);
3016 gtk_setup_config_box(ctrlbox, midsession, window);
3018 gtk_window_set_title(GTK_WINDOW(window), title);
3019 hbox = gtk_hbox_new(FALSE, 4);
3021 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3022 hbox, TRUE, TRUE, 0);
3023 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
3024 gtk_widget_show(hbox);
3025 vbox = gtk_vbox_new(FALSE, 4);
3026 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
3027 gtk_widget_show(vbox);
3028 cols = columns_new(4);
3029 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
3030 gtk_widget_show(cols);
3031 label = gtk_label_new("Category:");
3032 columns_add(COLUMNS(cols), label, 0, 1);
3033 columns_force_left_align(COLUMNS(cols), label);
3034 gtk_widget_show(label);
3035 treescroll = gtk_scrolled_window_new(NULL, NULL);
3036 #if GTK_CHECK_VERSION(2,0,0)
3037 treestore = gtk_tree_store_new
3038 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
3039 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
3040 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
3041 treerenderer = gtk_cell_renderer_text_new();
3042 treecolumn = gtk_tree_view_column_new_with_attributes
3043 ("Label", treerenderer, "text", 0, NULL);
3044 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
3045 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
3046 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
3047 gtk_container_add(GTK_CONTAINER(treescroll), tree);
3049 tree = gtk_tree_new();
3050 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
3051 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
3052 g_signal_connect(G_OBJECT(tree), "focus",
3053 G_CALLBACK(tree_focus), &dp);
3055 g_signal_connect(G_OBJECT(tree), "focus_in_event",
3056 G_CALLBACK(widget_focus), &dp);
3057 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
3058 gtk_widget_show(treescroll);
3059 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
3060 panels = gtk_notebook_new();
3061 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
3062 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
3063 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
3064 gtk_widget_show(panels);
3069 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3070 struct controlset *s = ctrlbox->ctrlsets[index];
3073 if (!*s->pathname) {
3074 w = layout_ctrls(&dp, &scs, s, GTK_WINDOW(window));
3076 set_dialog_action_area(GTK_DIALOG(window), w);
3078 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
3079 if (j != INT_MAX) { /* add to treeview, start new panel */
3081 #if GTK_CHECK_VERSION(2,0,0)
3082 GtkTreeIter treeiter;
3084 GtkWidget *treeitem;
3089 * We expect never to find an implicit path
3090 * component. For example, we expect never to see
3091 * A/B/C followed by A/D/E, because that would
3092 * _implicitly_ create A/D. All our path prefixes
3093 * are expected to contain actual controls and be
3094 * selectable in the treeview; so we would expect
3095 * to see A/D _explicitly_ before encountering
3098 assert(j == ctrl_path_elements(s->pathname) - 1);
3100 c = strrchr(s->pathname, '/');
3108 first = (panelvbox == NULL);
3110 panelvbox = gtk_vbox_new(FALSE, 4);
3111 gtk_widget_show(panelvbox);
3112 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
3117 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
3119 gtk_notebook_set_current_page(GTK_NOTEBOOK(panels),
3123 if (nselparams >= selparamsize) {
3125 selparams = sresize(selparams, selparamsize,
3128 selparams[nselparams].dp = &dp;
3129 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
3130 selparams[nselparams].panel = panelvbox;
3131 selparams[nselparams].shortcuts = scs; /* structure copy */
3133 assert(j-1 < level);
3135 #if GTK_CHECK_VERSION(2,0,0)
3137 /* treeiterlevels[j-1] will always be valid because we
3138 * don't allow implicit path components; see above.
3140 gtk_tree_store_append(treestore, &treeiter,
3141 &treeiterlevels[j-1]);
3143 gtk_tree_store_append(treestore, &treeiter, NULL);
3144 gtk_tree_store_set(treestore, &treeiter,
3146 TREESTORE_PARAMS, nselparams,
3148 treeiterlevels[j] = treeiter;
3150 selparams[nselparams].depth = j;
3152 selparams[nselparams].treepath =
3153 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
3154 &treeiterlevels[j-1]);
3156 * We are going to collapse all tree branches
3157 * at depth greater than 2, but not _yet_; see
3158 * the comment at the call to
3159 * gtk_tree_view_collapse_row below.
3161 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree),
3162 selparams[nselparams].treepath,
3165 selparams[nselparams].treepath = NULL;
3168 treeitem = gtk_tree_item_new_with_label(c);
3170 if (!treelevels[j-1]) {
3171 treelevels[j-1] = GTK_TREE(gtk_tree_new());
3172 gtk_tree_item_set_subtree
3173 (treeitemlevels[j-1],
3174 GTK_WIDGET(treelevels[j-1]));
3176 gtk_tree_item_expand(treeitemlevels[j-1]);
3178 gtk_tree_item_collapse(treeitemlevels[j-1]);
3180 gtk_tree_append(treelevels[j-1], treeitem);
3182 gtk_tree_append(GTK_TREE(tree), treeitem);
3184 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
3185 treelevels[j] = NULL;
3187 g_signal_connect(G_OBJECT(treeitem), "key_press_event",
3188 G_CALLBACK(tree_key_press), &dp);
3189 g_signal_connect(G_OBJECT(treeitem), "focus_in_event",
3190 G_CALLBACK(widget_focus), &dp);
3192 gtk_widget_show(treeitem);
3195 gtk_tree_select_child(GTK_TREE(tree), treeitem);
3196 selparams[nselparams].treeitem = treeitem;
3203 w = layout_ctrls(&dp, &selparams[nselparams-1].shortcuts, s, NULL);
3204 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
3209 #if GTK_CHECK_VERSION(2,0,0)
3215 * We want our tree view to come up with all branches at
3216 * depth 2 or more collapsed. However, if we start off
3217 * with those branches collapsed, then the tree view's
3218 * size request will be calculated based on the width of
3219 * the collapsed tree. So instead we start with them all
3220 * expanded; then we ask for the current size request,
3221 * collapse the relevant rows, and force the width to the
3222 * value we just computed. This arranges that the tree
3223 * view is wide enough to have all branches expanded
3227 gtk_widget_size_request(tree, &req);
3229 for (i = 0; i < nselparams; i++)
3230 if (selparams[i].depth >= 2)
3231 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree),
3232 selparams[i].treepath);
3234 gtk_widget_set_size_request(tree, req.width, -1);
3238 #if GTK_CHECK_VERSION(2,0,0)
3239 g_signal_connect(G_OBJECT(treeselection), "changed",
3240 G_CALLBACK(treeselection_changed), selparams);
3242 dp.ntreeitems = nselparams;
3243 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
3245 for (index = 0; index < nselparams; index++) {
3246 g_signal_connect(G_OBJECT(selparams[index].treeitem), "select",
3247 G_CALLBACK(treeitem_sel),
3249 dp.treeitems[index] = selparams[index].treeitem;
3254 dlg_refresh(NULL, &dp);
3256 dp.shortcuts = &selparams[0].shortcuts;
3257 #if !GTK_CHECK_VERSION(2,0,0)
3258 dp.currtreeitem = dp.treeitems[0];
3260 dp.lastfocus = NULL;
3266 extern void set_window_icon(GtkWidget *window,
3267 const char *const *const *icon,
3269 extern const char *const *const cfg_icon[];
3270 extern const int n_cfg_icon;
3271 set_window_icon(window, cfg_icon, n_cfg_icon);
3274 #if !GTK_CHECK_VERSION(2,0,0)
3275 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
3278 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
3280 GTK_POLICY_AUTOMATIC);
3281 gtk_widget_show(tree);
3283 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3284 gtk_widget_show(window);
3287 * Set focus into the first available control.
3289 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3290 struct controlset *s = ctrlbox->ctrlsets[index];
3295 for (j = 0; j < s->ncontrols; j++)
3296 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
3297 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
3298 s->ctrls[j]->generic.type != CTRL_TEXT) {
3299 dlg_set_focus(s->ctrls[j], &dp);
3300 dp.lastfocus = s->ctrls[j];
3309 g_signal_connect(G_OBJECT(window), "destroy",
3310 G_CALLBACK(window_destroy), NULL);
3311 g_signal_connect(G_OBJECT(window), "key_press_event",
3312 G_CALLBACK(win_key_press), &dp);
3323 static void messagebox_handler(union control *ctrl, void *dlg,
3324 void *data, int event)
3326 if (event == EVENT_ACTION)
3327 dlg_end(dlg, ctrl->generic.context.i);
3329 int messagebox(GtkWidget *parentwin, const char *title, const char *msg,
3332 GtkWidget *window, *w0, *w1;
3333 struct controlbox *ctrlbox;
3334 struct controlset *s0, *s1;
3337 struct Shortcuts scs;
3343 for (index = 0; index < lenof(scs.sc); index++) {
3344 scs.sc[index].action = SHORTCUT_EMPTY;
3347 ctrlbox = ctrl_new_box();
3350 va_start(ap, minwid);
3351 while (va_arg(ap, char *) != NULL) {
3353 (void) va_arg(ap, int); /* shortcut */
3354 (void) va_arg(ap, int); /* normal/default/cancel */
3355 (void) va_arg(ap, int); /* end value */
3359 s0 = ctrl_getset(ctrlbox, "", "", "");
3360 c = ctrl_columns(s0, 2, 50, 50);
3361 c->columns.ncols = s0->ncolumns = ncols;
3362 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
3363 for (index = 0; index < ncols; index++)
3364 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
3365 va_start(ap, minwid);
3368 char *title = va_arg(ap, char *);
3369 int shortcut, type, value;
3372 shortcut = va_arg(ap, int);
3373 type = va_arg(ap, int);
3374 value = va_arg(ap, int);
3375 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
3376 messagebox_handler, I(value));
3377 c->generic.column = index++;
3379 c->button.isdefault = TRUE;
3381 c->button.iscancel = TRUE;
3385 s1 = ctrl_getset(ctrlbox, "x", "", "");
3386 ctrl_text(s1, msg, HELPCTX(no_help));
3388 window = gtk_dialog_new();
3389 gtk_window_set_title(GTK_WINDOW(window), title);
3390 w0 = layout_ctrls(&dp, &scs, s0, GTK_WINDOW(window));
3391 set_dialog_action_area(GTK_DIALOG(window), w0);
3392 gtk_widget_show(w0);
3393 w1 = layout_ctrls(&dp, &scs, s1, GTK_WINDOW(window));
3394 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3395 gtk_widget_set_size_request(w1, minwid+20, -1);
3397 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3399 gtk_widget_show(w1);
3401 dp.shortcuts = &scs;
3402 dp.lastfocus = NULL;
3406 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3408 set_transient_window_pos(parentwin, window);
3409 gtk_window_set_transient_for(GTK_WINDOW(window),
3410 GTK_WINDOW(parentwin));
3412 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3413 gtk_widget_show(window);
3415 g_signal_connect(G_OBJECT(window), "destroy",
3416 G_CALLBACK(window_destroy), NULL);
3417 g_signal_connect(G_OBJECT(window), "key_press_event",
3418 G_CALLBACK(win_key_press), &dp);
3424 ctrl_free_box(ctrlbox);
3429 int string_width(const char *text)
3431 GtkWidget *label = gtk_label_new(text);
3433 gtk_widget_size_request(label, &req);
3434 g_object_ref_sink(G_OBJECT(label));
3438 int reallyclose(void *frontend)
3440 char *title = dupcat(appname, " Exit Confirmation", NULL);
3441 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
3442 title, "Are you sure you want to close this session?",
3443 string_width("Most of the width of the above text"),
3451 int verify_ssh_host_key(void *frontend, char *host, int port,
3452 const char *keytype, char *keystr, char *fingerprint,
3453 void (*callback)(void *ctx, int result), void *ctx)
3455 static const char absenttxt[] =
3456 "The server's host key is not cached. You have no guarantee "
3457 "that the server is the computer you think it is.\n"
3458 "The server's %s key fingerprint is:\n"
3460 "If you trust this host, press \"Accept\" to add the key to "
3461 "PuTTY's cache and carry on connecting.\n"
3462 "If you want to carry on connecting just once, without "
3463 "adding the key to the cache, press \"Connect Once\".\n"
3464 "If you do not trust this host, press \"Cancel\" to abandon the "
3466 static const char wrongtxt[] =
3467 "WARNING - POTENTIAL SECURITY BREACH!\n"
3468 "The server's host key does not match the one PuTTY has "
3469 "cached. This means that either the server administrator "
3470 "has changed the host key, or you have actually connected "
3471 "to another computer pretending to be the server.\n"
3472 "The new %s key fingerprint is:\n"
3474 "If you were expecting this change and trust the new key, "
3475 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3476 "If you want to carry on connecting but without updating "
3477 "the cache, press \"Connect Once\".\n"
3478 "If you want to abandon the connection completely, press "
3479 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3487 ret = verify_host_key(host, port, keytype, keystr);
3489 if (ret == 0) /* success - key matched OK */
3492 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
3494 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3495 "PuTTY Security Alert", text,
3496 string_width(fingerprint),
3497 "Accept", 'a', 0, 2,
3498 "Connect Once", 'o', 0, 1,
3499 "Cancel", 'c', -1, 0,
3505 store_host_key(host, port, keytype, keystr);
3506 return 1; /* continue with connection */
3507 } else if (ret == 1)
3508 return 1; /* continue with connection */
3509 return 0; /* do not continue with connection */
3513 * Ask whether the selected algorithm is acceptable (since it was
3514 * below the configured 'warn' threshold).
3516 int askalg(void *frontend, const char *algtype, const char *algname,
3517 void (*callback)(void *ctx, int result), void *ctx)
3519 static const char msg[] =
3520 "The first %s supported by the server is "
3521 "%s, which is below the configured warning threshold.\n"
3522 "Continue with connection?";
3526 text = dupprintf(msg, algtype, algname);
3527 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3528 "PuTTY Security Alert", text,
3529 string_width("Continue with connection?"),
3542 void old_keyfile_warning(void)
3545 * This should never happen on Unix. We hope.
3549 void fatal_message_box(void *window, const char *msg)
3551 messagebox(window, "PuTTY Fatal Error", msg,
3552 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3553 "OK", 'o', 1, 1, NULL);
3556 void nonfatal_message_box(void *window, const char *msg)
3558 messagebox(window, "PuTTY Error", msg,
3559 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3560 "OK", 'o', 1, 1, NULL);
3563 void fatalbox(const char *p, ...)
3568 msg = dupvprintf(p, ap);
3570 fatal_message_box(NULL, msg);
3575 void nonfatal(const char *p, ...)
3580 msg = dupvprintf(p, ap);
3582 nonfatal_message_box(NULL, msg);
3586 static GtkWidget *aboutbox = NULL;
3588 static void about_close_clicked(GtkButton *button, gpointer data)
3590 gtk_widget_destroy(aboutbox);
3594 static void licence_clicked(GtkButton *button, gpointer data)
3598 const char *licence =
3599 "Copyright 1997-2015 Simon Tatham.\n\n"
3601 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3602 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3603 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3604 "Markus Kuhn, Colin Watson, Christopher Staite, and CORE SDI S.A.\n\n"
3606 "Permission is hereby granted, free of charge, to any person "
3607 "obtaining a copy of this software and associated documentation "
3608 "files (the ""Software""), to deal in the Software without restriction, "
3609 "including without limitation the rights to use, copy, modify, merge, "
3610 "publish, distribute, sublicense, and/or sell copies of the Software, "
3611 "and to permit persons to whom the Software is furnished to do so, "
3612 "subject to the following conditions:\n\n"
3614 "The above copyright notice and this permission notice shall be "
3615 "included in all copies or substantial portions of the Software.\n\n"
3617 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3618 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3619 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3620 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3621 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3622 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3623 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3624 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3625 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3626 "OTHER DEALINGS IN THE SOFTWARE.";
3628 title = dupcat(appname, " Licence", NULL);
3629 assert(aboutbox != NULL);
3630 messagebox(aboutbox, title, licence,
3631 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3632 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3633 "OK", 'o', 1, 1, NULL);
3637 void about_box(void *window)
3643 gtk_widget_grab_focus(aboutbox);
3647 aboutbox = gtk_dialog_new();
3648 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
3649 title = dupcat("About ", appname, NULL);
3650 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
3653 w = gtk_button_new_with_label("Close");
3654 gtk_widget_set_can_default(w, TRUE);
3655 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
3656 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3657 w, FALSE, FALSE, 0);
3658 g_signal_connect(G_OBJECT(w), "clicked",
3659 G_CALLBACK(about_close_clicked), NULL);
3662 w = gtk_button_new_with_label("View Licence");
3663 gtk_widget_set_can_default(w, TRUE);
3664 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3665 w, FALSE, FALSE, 0);
3666 g_signal_connect(G_OBJECT(w), "clicked",
3667 G_CALLBACK(licence_clicked), NULL);
3670 w = gtk_label_new(appname);
3672 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3673 w, FALSE, FALSE, 0);
3676 w = gtk_label_new(ver);
3678 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3679 w, FALSE, FALSE, 5);
3682 w = gtk_label_new("Copyright 1997-2015 Simon Tatham. All rights reserved");
3684 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3685 w, FALSE, FALSE, 5);
3688 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
3689 gtk_window_set_transient_for(GTK_WINDOW(aboutbox),
3690 GTK_WINDOW(window));
3691 gtk_widget_show(aboutbox);
3694 struct eventlog_stuff {
3695 GtkWidget *parentwin, *window;
3696 struct controlbox *eventbox;
3697 struct Shortcuts scs;
3699 union control *listctrl;
3701 int nevents, negsize;
3704 int ignore_selchange;
3707 static void eventlog_destroy(GtkWidget *widget, gpointer data)
3709 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3714 dlg_cleanup(&es->dp);
3715 ctrl_free_box(es->eventbox);
3717 static void eventlog_ok_handler(union control *ctrl, void *dlg,
3718 void *data, int event)
3720 if (event == EVENT_ACTION)
3723 static void eventlog_list_handler(union control *ctrl, void *dlg,
3724 void *data, int event)
3726 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3728 if (event == EVENT_REFRESH) {
3731 dlg_update_start(ctrl, dlg);
3732 dlg_listbox_clear(ctrl, dlg);
3733 for (i = 0; i < es->nevents; i++) {
3734 dlg_listbox_add(ctrl, dlg, es->events[i]);
3736 dlg_update_done(ctrl, dlg);
3737 } else if (event == EVENT_SELCHANGE) {
3742 * If this SELCHANGE event is happening as a result of
3743 * deliberate deselection because someone else has grabbed
3744 * the selection, the last thing we want to do is pre-empt
3747 if (es->ignore_selchange)
3751 * Construct the data to use as the selection.
3756 for (i = 0; i < es->nevents; i++) {
3757 if (dlg_listbox_issel(ctrl, dlg, i)) {
3758 int extralen = strlen(es->events[i]);
3760 if (es->sellen + extralen + 2 > selsize) {
3761 selsize = es->sellen + extralen + 512;
3762 es->seldata = sresize(es->seldata, selsize, char);
3765 strcpy(es->seldata + es->sellen, es->events[i]);
3766 es->sellen += extralen;
3767 es->seldata[es->sellen++] = '\n';
3771 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
3772 GDK_CURRENT_TIME)) {
3773 extern GdkAtom compound_text_atom;
3775 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3776 GDK_SELECTION_TYPE_STRING, 1);
3777 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3778 compound_text_atom, 1);
3784 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
3785 guint info, guint time_stamp, gpointer data)
3787 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3789 gtk_selection_data_set(seldata, gtk_selection_data_get_target(seldata), 8,
3790 (unsigned char *)es->seldata, es->sellen);
3793 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
3796 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3800 * Deselect everything in the list box.
3802 uc = dlg_find_byctrl(&es->dp, es->listctrl);
3803 es->ignore_selchange = 1;
3804 #if !GTK_CHECK_VERSION(2,0,0)
3806 gtk_list_unselect_all(GTK_LIST(uc->list));
3808 assert(uc->treeview);
3809 gtk_tree_selection_unselect_all
3810 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview)));
3812 es->ignore_selchange = 0;
3820 void showeventlog(void *estuff, void *parentwin)
3822 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3823 GtkWidget *window, *w0, *w1;
3824 GtkWidget *parent = GTK_WIDGET(parentwin);
3825 struct controlset *s0, *s1;
3831 gtk_widget_grab_focus(es->window);
3837 for (index = 0; index < lenof(es->scs.sc); index++) {
3838 es->scs.sc[index].action = SHORTCUT_EMPTY;
3841 es->eventbox = ctrl_new_box();
3843 s0 = ctrl_getset(es->eventbox, "", "", "");
3844 ctrl_columns(s0, 3, 33, 34, 33);
3845 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
3846 eventlog_ok_handler, P(NULL));
3847 c->button.column = 1;
3848 c->button.isdefault = TRUE;
3850 s1 = ctrl_getset(es->eventbox, "x", "", "");
3851 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3852 eventlog_list_handler, P(es));
3853 c->listbox.height = 10;
3854 c->listbox.multisel = 2;
3855 c->listbox.ncols = 3;
3856 c->listbox.percentages = snewn(3, int);
3857 c->listbox.percentages[0] = 25;
3858 c->listbox.percentages[1] = 10;
3859 c->listbox.percentages[2] = 65;
3861 es->window = window = gtk_dialog_new();
3862 title = dupcat(appname, " Event Log", NULL);
3863 gtk_window_set_title(GTK_WINDOW(window), title);
3865 w0 = layout_ctrls(&es->dp, &es->scs, s0, GTK_WINDOW(window));
3866 set_dialog_action_area(GTK_DIALOG(window), w0);
3867 gtk_widget_show(w0);
3868 w1 = layout_ctrls(&es->dp, &es->scs, s1, GTK_WINDOW(window));
3869 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3870 gtk_widget_set_size_request(w1, 20 + string_width
3871 ("LINE OF TEXT GIVING WIDTH OF EVENT LOG IS "
3872 "QUITE LONG 'COS SSH LOG ENTRIES ARE WIDE"),
3875 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3877 gtk_widget_show(w1);
3880 es->dp.shortcuts = &es->scs;
3881 es->dp.lastfocus = NULL;
3883 es->dp.window = window;
3885 dlg_refresh(NULL, &es->dp);
3888 set_transient_window_pos(parent, window);
3889 gtk_window_set_transient_for(GTK_WINDOW(window),
3890 GTK_WINDOW(parent));
3892 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3893 gtk_widget_show(window);
3895 g_signal_connect(G_OBJECT(window), "destroy",
3896 G_CALLBACK(eventlog_destroy), es);
3897 g_signal_connect(G_OBJECT(window), "key_press_event",
3898 G_CALLBACK(win_key_press), &es->dp);
3899 g_signal_connect(G_OBJECT(window), "selection_get",
3900 G_CALLBACK(eventlog_selection_get), es);
3901 g_signal_connect(G_OBJECT(window), "selection_clear_event",
3902 G_CALLBACK(eventlog_selection_clear), es);
3905 void *eventlogstuff_new(void)
3907 struct eventlog_stuff *es;
3908 es = snew(struct eventlog_stuff);
3909 memset(es, 0, sizeof(*es));
3913 void logevent_dlg(void *estuff, const char *string)
3915 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3920 if (es->nevents >= es->negsize) {
3922 es->events = sresize(es->events, es->negsize, char *);
3926 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3928 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3929 strcpy(es->events[es->nevents], timebuf);
3930 strcat(es->events[es->nevents], string);
3932 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3937 int askappend(void *frontend, Filename *filename,
3938 void (*callback)(void *ctx, int result), void *ctx)
3940 static const char msgtemplate[] =
3941 "The session log file \"%.*s\" already exists. "
3942 "You can overwrite it with a new session log, "
3943 "append your session log to the end of it, "
3944 "or disable session logging for this session.";
3949 message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
3950 mbtitle = dupprintf("%s Log to File", appname);
3952 mbret = messagebox(get_window(frontend), mbtitle, message,
3953 string_width("LINE OF TEXT SUITABLE FOR THE"
3954 " ASKAPPEND WIDTH"),
3955 "Overwrite", 'o', 1, 2,
3956 "Append", 'a', 0, 1,
3957 "Disable", 'd', -1, 0,