2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
11 #if !GTK_CHECK_VERSION(3,0,0)
12 #include <gdk/gdkkeysyms.h>
16 #include "gtkcompat.h"
23 #include <X11/Xutil.h>
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
34 #if GTK_CHECK_VERSION(2,0,0)
35 /* Decide which of GtkFileChooserDialog and GtkFileSelection to use */
36 #define USE_GTK_FILE_CHOOSER_DIALOG
46 struct Shortcut sc[128];
52 GtkWidget **buttons; int nbuttons; /* for radio buttons */
53 GtkWidget *entry; /* for editbox, filesel, fontsel */
54 GtkWidget *button; /* for filesel, fontsel */
55 #if !GTK_CHECK_VERSION(2,4,0)
56 GtkWidget *list; /* for listbox (in GTK1), combobox (<=GTK2.3) */
57 GtkWidget *menu; /* for optionmenu (==droplist) */
58 GtkWidget *optmenu; /* also for optionmenu */
60 GtkWidget *combo; /* for combo box (either editable or not) */
62 #if GTK_CHECK_VERSION(2,0,0)
63 GtkWidget *treeview; /* for listbox (GTK2), droplist+combo (>=2.4) */
64 GtkListStore *listmodel; /* for all types of list box */
66 GtkWidget *text; /* for text */
67 GtkWidget *label; /* for dlg_label_change */
68 GtkAdjustment *adj; /* for the scrollbar in a list box */
75 tree234 *byctrl, *bywidget;
77 struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
78 /* `flags' are set to indicate when a GTK signal handler is being called
79 * due to automatic processing and should not flag a user event. */
81 struct Shortcuts *shortcuts;
82 GtkWidget *window, *cancelbutton;
83 union control *currfocus, *lastfocus;
84 #if !GTK_CHECK_VERSION(2,0,0)
85 GtkWidget *currtreeitem, **treeitems;
89 struct selparam *selparams;
93 #define FLAG_UPDATING_COMBO_LIST 1
94 #define FLAG_UPDATING_LISTBOX 2
96 enum { /* values for Shortcut.action */
97 SHORTCUT_EMPTY, /* no shortcut on this key */
98 SHORTCUT_TREE, /* focus a tree item */
99 SHORTCUT_FOCUS, /* focus the supplied widget */
100 SHORTCUT_UCTRL, /* do something sane with uctrl */
101 SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */
102 SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */
105 #if GTK_CHECK_VERSION(2,0,0)
114 * Forward references.
116 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
118 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
119 int chr, int action, void *ptr);
120 static void shortcut_highlight(GtkWidget *label, int chr);
121 #if !GTK_CHECK_VERSION(2,0,0)
122 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
124 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
126 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
128 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
131 #if !GTK_CHECK_VERSION(2,4,0)
132 static void menuitem_activate(GtkMenuItem *item, gpointer data);
134 #if GTK_CHECK_VERSION(3,0,0)
135 static void colourchoose_response(GtkDialog *dialog,
136 gint response_id, gpointer data);
138 static void coloursel_ok(GtkButton *button, gpointer data);
139 static void coloursel_cancel(GtkButton *button, gpointer data);
141 static void window_destroy(GtkWidget *widget, gpointer data);
142 int get_listitemheight(GtkWidget *widget);
144 static int uctrl_cmp_byctrl(void *av, void *bv)
146 struct uctrl *a = (struct uctrl *)av;
147 struct uctrl *b = (struct uctrl *)bv;
148 if (a->ctrl < b->ctrl)
150 else if (a->ctrl > b->ctrl)
155 static int uctrl_cmp_byctrl_find(void *av, void *bv)
157 union control *a = (union control *)av;
158 struct uctrl *b = (struct uctrl *)bv;
161 else if (a > b->ctrl)
166 static int uctrl_cmp_bywidget(void *av, void *bv)
168 struct uctrl *a = (struct uctrl *)av;
169 struct uctrl *b = (struct uctrl *)bv;
170 if (a->toplevel < b->toplevel)
172 else if (a->toplevel > b->toplevel)
177 static int uctrl_cmp_bywidget_find(void *av, void *bv)
179 GtkWidget *a = (GtkWidget *)av;
180 struct uctrl *b = (struct uctrl *)bv;
183 else if (a > b->toplevel)
188 static void dlg_init(struct dlgparam *dp)
190 dp->byctrl = newtree234(uctrl_cmp_byctrl);
191 dp->bywidget = newtree234(uctrl_cmp_bywidget);
192 dp->coloursel_result.ok = FALSE;
193 dp->window = dp->cancelbutton = NULL;
194 #if !GTK_CHECK_VERSION(2,0,0)
195 dp->treeitems = NULL;
196 dp->currtreeitem = NULL;
199 dp->currfocus = NULL;
202 static void dlg_cleanup(struct dlgparam *dp)
206 freetree234(dp->byctrl); /* doesn't free the uctrls inside */
208 while ( (uc = index234(dp->bywidget, 0)) != NULL) {
209 del234(dp->bywidget, uc);
213 freetree234(dp->bywidget);
215 #if !GTK_CHECK_VERSION(2,0,0)
216 sfree(dp->treeitems);
220 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
222 add234(dp->byctrl, uc);
223 add234(dp->bywidget, uc);
226 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
230 return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
233 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
235 struct uctrl *ret = NULL;
239 ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
242 w = gtk_widget_get_parent(w);
247 union control *dlg_last_focused(union control *ctrl, void *dlg)
249 struct dlgparam *dp = (struct dlgparam *)dlg;
250 if (dp->currfocus != ctrl)
251 return dp->currfocus;
253 return dp->lastfocus;
256 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
258 struct dlgparam *dp = (struct dlgparam *)dlg;
259 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
260 assert(uc->ctrl->generic.type == CTRL_RADIO);
261 assert(uc->buttons != NULL);
262 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
265 int dlg_radiobutton_get(union control *ctrl, void *dlg)
267 struct dlgparam *dp = (struct dlgparam *)dlg;
268 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
271 assert(uc->ctrl->generic.type == CTRL_RADIO);
272 assert(uc->buttons != NULL);
273 for (i = 0; i < uc->nbuttons; i++)
274 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
276 return 0; /* got to return something */
279 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
281 struct dlgparam *dp = (struct dlgparam *)dlg;
282 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
283 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
284 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
287 int dlg_checkbox_get(union control *ctrl, void *dlg)
289 struct dlgparam *dp = (struct dlgparam *)dlg;
290 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
291 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
292 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
295 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
297 struct dlgparam *dp = (struct dlgparam *)dlg;
298 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
301 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
303 #if GTK_CHECK_VERSION(2,4,0)
305 entry = gtk_bin_get_child(GTK_BIN(uc->combo));
310 assert(entry != NULL);
313 * GTK 2 implements gtk_entry_set_text by means of two separate
314 * operations: first delete the previous text leaving the empty
315 * string, then insert the new text. This causes two calls to
316 * the "changed" signal.
318 * The first call to "changed", if allowed to proceed normally,
319 * will cause an EVENT_VALCHANGE event on the edit box, causing
320 * a call to dlg_editbox_get() which will read the empty string
321 * out of the GtkEntry - and promptly write it straight into the
322 * Conf structure, which is precisely where our `text' pointer
323 * is probably pointing, so the second editing operation will
324 * insert that instead of the string we originally asked for.
326 * Hence, we must take our own copy of the text before we do
329 tmpstring = dupstr(text);
330 gtk_entry_set_text(GTK_ENTRY(entry), tmpstring);
334 char *dlg_editbox_get(union control *ctrl, void *dlg)
336 struct dlgparam *dp = (struct dlgparam *)dlg;
337 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
338 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
340 #if GTK_CHECK_VERSION(2,4,0)
342 return dupstr(gtk_entry_get_text
343 (GTK_ENTRY(gtk_bin_get_child(GTK_BIN(uc->combo)))));
348 return dupstr(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
351 assert(!"We shouldn't get here");
354 #if !GTK_CHECK_VERSION(2,4,0)
355 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
357 GtkContainer *cont = GTK_CONTAINER(data);
358 /* gtk_container_remove will unref the widget for us; we need not. */
359 gtk_container_remove(cont, w);
363 /* The `listbox' functions can also apply to combo boxes. */
364 void dlg_listbox_clear(union control *ctrl, void *dlg)
366 struct dlgparam *dp = (struct dlgparam *)dlg;
367 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
369 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
370 uc->ctrl->generic.type == CTRL_LISTBOX);
372 #if !GTK_CHECK_VERSION(2,4,0)
374 gtk_container_foreach(GTK_CONTAINER(uc->menu),
375 container_remove_and_destroy,
376 GTK_CONTAINER(uc->menu));
380 gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
384 #if GTK_CHECK_VERSION(2,0,0)
386 gtk_list_store_clear(uc->listmodel);
390 assert(!"We shouldn't get here");
393 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
395 struct dlgparam *dp = (struct dlgparam *)dlg;
396 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
398 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
399 uc->ctrl->generic.type == CTRL_LISTBOX);
401 #if !GTK_CHECK_VERSION(2,4,0)
404 (GTK_CONTAINER(uc->menu),
405 g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
409 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
413 #if GTK_CHECK_VERSION(2,0,0)
417 assert(uc->listmodel != NULL);
418 path = gtk_tree_path_new_from_indices(index, -1);
419 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
420 gtk_list_store_remove(uc->listmodel, &iter);
421 gtk_tree_path_free(path);
425 assert(!"We shouldn't get here");
428 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
430 dlg_listbox_addwithid(ctrl, dlg, text, 0);
434 * Each listbox entry may have a numeric id associated with it.
435 * Note that some front ends only permit a string to be stored at
436 * each position, which means that _if_ you put two identical
437 * strings in any listbox then you MUST not assign them different
438 * IDs and expect to get meaningful results back.
440 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
441 char const *text, int id)
443 struct dlgparam *dp = (struct dlgparam *)dlg;
444 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
446 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
447 uc->ctrl->generic.type == CTRL_LISTBOX);
450 * This routine is long and complicated in both GTK 1 and 2,
451 * and completely different. Sigh.
453 dp->flags |= FLAG_UPDATING_COMBO_LIST;
455 #if !GTK_CHECK_VERSION(2,4,0)
458 * List item in a drop-down (but non-combo) list. Tabs are
459 * ignored; we just provide a standard menu item with the
462 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
464 gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
465 gtk_widget_show(menuitem);
467 g_object_set_data(G_OBJECT(menuitem), "user-data",
468 GINT_TO_POINTER(id));
469 g_signal_connect(G_OBJECT(menuitem), "activate",
470 G_CALLBACK(menuitem_activate), dp);
473 if (uc->list && uc->entry) {
475 * List item in a combo-box list, which means the sensible
476 * thing to do is make it a perfectly normal label. Hence
477 * tabs are disregarded.
479 GtkWidget *listitem = gtk_list_item_new_with_label(text);
481 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
482 gtk_widget_show(listitem);
484 g_object_set_data(G_OBJECT(listitem), "user-data",
485 GINT_TO_POINTER(id));
489 #if !GTK_CHECK_VERSION(2,0,0)
492 * List item in a non-combo-box list box. We make all of
493 * these Columns containing GtkLabels. This allows us to do
494 * the nasty force_left hack irrespective of whether there
495 * are tabs in the thing.
497 GtkWidget *listitem = gtk_list_item_new();
498 GtkWidget *cols = columns_new(10);
502 /* Count the tabs in the text, and hence determine # of columns. */
504 for (i = 0; text[i]; i++)
509 (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
510 percents = snewn(ncols, gint);
511 percents[ncols-1] = 100;
512 for (i = 0; i < ncols-1; i++) {
513 percents[i] = uc->ctrl->listbox.percentages[i];
514 percents[ncols-1] -= percents[i];
516 columns_set_cols(COLUMNS(cols), ncols, percents);
519 for (i = 0; i < ncols; i++) {
520 int len = strcspn(text, "\t");
521 char *dup = dupprintf("%.*s", len, text);
526 label = gtk_label_new(dup);
529 columns_add(COLUMNS(cols), label, i, 1);
530 columns_force_left_align(COLUMNS(cols), label);
531 gtk_widget_show(label);
533 gtk_container_add(GTK_CONTAINER(listitem), cols);
534 gtk_widget_show(cols);
535 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
536 gtk_widget_show(listitem);
538 if (ctrl->listbox.multisel) {
539 g_signal_connect(G_OBJECT(listitem), "key_press_event",
540 G_CALLBACK(listitem_multi_key), uc->adj);
542 g_signal_connect(G_OBJECT(listitem), "key_press_event",
543 G_CALLBACK(listitem_single_key), uc->adj);
545 g_signal_connect(G_OBJECT(listitem), "focus_in_event",
546 G_CALLBACK(widget_focus), dp);
547 g_signal_connect(G_OBJECT(listitem), "button_press_event",
548 G_CALLBACK(listitem_button_press), dp);
549 g_signal_connect(G_OBJECT(listitem), "button_release_event",
550 G_CALLBACK(listitem_button_release), dp);
551 g_object_set_data(G_OBJECT(listitem), "user-data",
552 GINT_TO_POINTER(id));
560 dp->flags |= FLAG_UPDATING_LISTBOX;/* inhibit drag-list update */
561 gtk_list_store_append(uc->listmodel, &iter);
562 dp->flags &= ~FLAG_UPDATING_LISTBOX;
563 gtk_list_store_set(uc->listmodel, &iter, 0, id, -1);
566 * Now go through text and divide it into columns at the tabs,
569 cols = (uc->ctrl->generic.type == CTRL_LISTBOX ? ctrl->listbox.ncols : 1);
570 cols = cols ? cols : 1;
571 for (i = 0; i < cols; i++) {
572 int collen = strcspn(text, "\t");
573 char *tmpstr = snewn(collen+1, char);
574 memcpy(tmpstr, text, collen);
575 tmpstr[collen] = '\0';
576 gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1);
584 assert(!"We shouldn't get here");
586 dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
589 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
591 struct dlgparam *dp = (struct dlgparam *)dlg;
592 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
594 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
595 uc->ctrl->generic.type == CTRL_LISTBOX);
597 #if !GTK_CHECK_VERSION(2,4,0)
598 if (uc->menu || uc->list) {
602 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
604 item = G_OBJECT(g_list_nth_data(children, index));
605 g_list_free(children);
607 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item), "user-data"));
610 #if GTK_CHECK_VERSION(2,0,0)
616 path = gtk_tree_path_new_from_indices(index, -1);
617 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
618 gtk_tree_model_get(GTK_TREE_MODEL(uc->listmodel), &iter, 0, &ret, -1);
619 gtk_tree_path_free(path);
624 assert(!"We shouldn't get here");
625 return -1; /* placate dataflow analysis */
628 /* dlg_listbox_index returns <0 if no single element is selected. */
629 int dlg_listbox_index(union control *ctrl, void *dlg)
631 struct dlgparam *dp = (struct dlgparam *)dlg;
632 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
634 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
635 uc->ctrl->generic.type == CTRL_LISTBOX);
637 #if !GTK_CHECK_VERSION(2,4,0)
638 if (uc->menu || uc->list) {
640 GtkWidget *item, *activeitem;
645 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
647 activeitem = NULL; /* unnecessarily placate gcc */
649 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
651 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
652 i++, children = children->next) {
653 if (uc->menu ? activeitem == item :
654 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
661 g_list_free(children);
662 return selected < 0 ? -1 : selected;
667 * This API function already does the right thing in the
668 * case of no current selection.
670 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo));
673 #if GTK_CHECK_VERSION(2,0,0)
675 GtkTreeSelection *treesel;
682 assert(uc->treeview != NULL);
683 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
685 if (gtk_tree_selection_count_selected_rows(treesel) != 1)
688 sellist = gtk_tree_selection_get_selected_rows(treesel, &model);
690 assert(sellist && sellist->data);
691 path = sellist->data;
693 if (gtk_tree_path_get_depth(path) != 1) {
696 indices = gtk_tree_path_get_indices(path);
704 g_list_foreach(sellist, (GFunc)gtk_tree_path_free, NULL);
705 g_list_free(sellist);
710 assert(!"We shouldn't get here");
711 return -1; /* placate dataflow analysis */
714 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
716 struct dlgparam *dp = (struct dlgparam *)dlg;
717 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
719 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
720 uc->ctrl->generic.type == CTRL_LISTBOX);
722 #if !GTK_CHECK_VERSION(2,4,0)
723 if (uc->menu || uc->list) {
725 GtkWidget *item, *activeitem;
727 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
728 uc->ctrl->generic.type == CTRL_LISTBOX);
729 assert(uc->menu != NULL || uc->list != NULL);
731 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
733 item = GTK_WIDGET(g_list_nth_data(children, index));
734 g_list_free(children);
737 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
738 return item == activeitem;
740 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
746 * This API function already does the right thing in the
747 * case of no current selection.
749 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo)) == index;
752 #if GTK_CHECK_VERSION(2,0,0)
754 GtkTreeSelection *treesel;
758 assert(uc->treeview != NULL);
759 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
761 path = gtk_tree_path_new_from_indices(index, -1);
762 ret = gtk_tree_selection_path_is_selected(treesel, path);
763 gtk_tree_path_free(path);
768 assert(!"We shouldn't get here");
769 return -1; /* placate dataflow analysis */
772 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
774 struct dlgparam *dp = (struct dlgparam *)dlg;
775 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
777 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
778 uc->ctrl->generic.type == CTRL_LISTBOX);
780 #if !GTK_CHECK_VERSION(2,4,0)
782 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
788 gdouble newtop, newbot;
790 gtk_list_select_item(GTK_LIST(uc->list), index);
793 * Scroll the list box if necessary to ensure the newly
794 * selected item is visible.
796 items = gtk_container_children(GTK_CONTAINER(uc->list));
797 nitems = g_list_length(items);
799 int modified = FALSE;
801 newtop = uc->adj->lower +
802 (uc->adj->upper - uc->adj->lower) * index / nitems;
803 newbot = uc->adj->lower +
804 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
805 if (uc->adj->value > newtop) {
807 uc->adj->value = newtop;
808 } else if (uc->adj->value < newbot - uc->adj->page_size) {
810 uc->adj->value = newbot - uc->adj->page_size;
813 gtk_adjustment_value_changed(uc->adj);
819 gtk_combo_box_set_active(GTK_COMBO_BOX(uc->combo), index);
823 #if GTK_CHECK_VERSION(2,0,0)
825 GtkTreeSelection *treesel;
828 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
830 path = gtk_tree_path_new_from_indices(index, -1);
831 gtk_tree_selection_select_path(treesel, path);
832 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc->treeview),
833 path, NULL, FALSE, 0.0, 0.0);
834 gtk_tree_path_free(path);
838 assert(!"We shouldn't get here");
841 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
843 struct dlgparam *dp = (struct dlgparam *)dlg;
844 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
846 assert(uc->ctrl->generic.type == CTRL_TEXT);
847 assert(uc->text != NULL);
849 gtk_label_set_text(GTK_LABEL(uc->text), text);
852 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
854 struct dlgparam *dp = (struct dlgparam *)dlg;
855 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
857 switch (uc->ctrl->generic.type) {
859 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
860 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
863 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
864 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
867 gtk_label_set_text(GTK_LABEL(uc->label), text);
868 shortcut_highlight(uc->label, ctrl->radio.shortcut);
871 gtk_label_set_text(GTK_LABEL(uc->label), text);
872 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
874 case CTRL_FILESELECT:
875 gtk_label_set_text(GTK_LABEL(uc->label), text);
876 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
878 case CTRL_FONTSELECT:
879 gtk_label_set_text(GTK_LABEL(uc->label), text);
880 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
883 gtk_label_set_text(GTK_LABEL(uc->label), text);
884 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
887 assert(!"This shouldn't happen");
892 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
894 struct dlgparam *dp = (struct dlgparam *)dlg;
895 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
896 /* We must copy fn->path before passing it to gtk_entry_set_text.
897 * See comment in dlg_editbox_set() for the reasons. */
898 char *duppath = dupstr(fn->path);
899 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
900 assert(uc->entry != NULL);
901 gtk_entry_set_text(GTK_ENTRY(uc->entry), duppath);
905 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
907 struct dlgparam *dp = (struct dlgparam *)dlg;
908 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
909 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
910 assert(uc->entry != NULL);
911 return filename_from_str(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
914 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
916 struct dlgparam *dp = (struct dlgparam *)dlg;
917 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
918 /* We must copy fs->name before passing it to gtk_entry_set_text.
919 * See comment in dlg_editbox_set() for the reasons. */
920 char *dupname = dupstr(fs->name);
921 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
922 assert(uc->entry != NULL);
923 gtk_entry_set_text(GTK_ENTRY(uc->entry), dupname);
927 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
929 struct dlgparam *dp = (struct dlgparam *)dlg;
930 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
931 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
932 assert(uc->entry != NULL);
933 return fontspec_new(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
937 * Bracketing a large set of updates in these two functions will
938 * cause the front end (if possible) to delay updating the screen
939 * until it's all complete, thus avoiding flicker.
941 void dlg_update_start(union control *ctrl, void *dlg)
944 * Apparently we can't do this at all in GTK. GtkCList supports
945 * freeze and thaw, but not GtkList. Bah.
949 void dlg_update_done(union control *ctrl, void *dlg)
952 * Apparently we can't do this at all in GTK. GtkCList supports
953 * freeze and thaw, but not GtkList. Bah.
957 void dlg_set_focus(union control *ctrl, void *dlg)
959 struct dlgparam *dp = (struct dlgparam *)dlg;
960 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
962 switch (ctrl->generic.type) {
965 /* Check boxes and buttons get the focus _and_ get toggled. */
966 gtk_widget_grab_focus(uc->toplevel);
968 case CTRL_FILESELECT:
969 case CTRL_FONTSELECT:
972 /* Anything containing an edit box gets that focused. */
973 gtk_widget_grab_focus(uc->entry);
975 #if GTK_CHECK_VERSION(2,4,0)
976 else if (uc->combo) {
977 /* Failing that, there'll be a combo box. */
978 gtk_widget_grab_focus(uc->combo);
984 * Radio buttons: we find the currently selected button and
989 for (i = 0; i < ctrl->radio.nbuttons; i++)
990 if (gtk_toggle_button_get_active
991 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
992 gtk_widget_grab_focus(uc->buttons[i]);
997 #if !GTK_CHECK_VERSION(2,4,0)
999 gtk_widget_grab_focus(uc->optmenu);
1004 gtk_widget_grab_focus(uc->combo);
1008 #if !GTK_CHECK_VERSION(2,0,0)
1011 * For GTK-1 style list boxes, we tell it to focus one
1012 * of its children, which appears to do the Right
1015 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
1020 gtk_widget_grab_focus(uc->treeview);
1024 assert(!"We shouldn't get here");
1030 * During event processing, you might well want to give an error
1031 * indication to the user. dlg_beep() is a quick and easy generic
1032 * error; dlg_error() puts up a message-box or equivalent.
1034 void dlg_beep(void *dlg)
1039 static void errmsg_button_clicked(GtkButton *button, gpointer data)
1041 gtk_widget_destroy(GTK_WIDGET(data));
1044 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
1046 #if !GTK_CHECK_VERSION(2,0,0)
1047 gint x, y, w, h, dx, dy;
1049 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
1050 gtk_widget_size_request(GTK_WIDGET(child), &req);
1052 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(parent)), &x, &y);
1053 gdk_window_get_size(gtk_widget_get_window(GTK_WIDGET(parent)), &w, &h);
1056 * One corner of the transient will be offset inwards, by 1/4
1057 * of the parent window's size, from the corresponding corner
1058 * of the parent window. The corner will be chosen so as to
1059 * place the transient closer to the centre of the screen; this
1060 * should avoid transients going off the edge of the screen on
1063 if (x + w/2 < gdk_screen_width() / 2)
1064 dx = x + w/4; /* work from left edges */
1066 dx = x + 3*w/4 - req.width; /* work from right edges */
1067 if (y + h/2 < gdk_screen_height() / 2)
1068 dy = y + h/4; /* work from top edges */
1070 dy = y + 3*h/4 - req.height; /* work from bottom edges */
1071 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
1075 void dlg_error_msg(void *dlg, const char *msg)
1077 struct dlgparam *dp = (struct dlgparam *)dlg;
1078 GtkWidget *window, *hbox, *text, *ok;
1080 window = gtk_dialog_new();
1081 text = gtk_label_new(msg);
1082 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1083 hbox = gtk_hbox_new(FALSE, 0);
1084 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1085 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
1086 hbox, FALSE, FALSE, 20);
1087 gtk_widget_show(text);
1088 gtk_widget_show(hbox);
1089 gtk_window_set_title(GTK_WINDOW(window), "Error");
1090 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1091 ok = gtk_button_new_with_label("OK");
1092 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(window))),
1093 ok, FALSE, FALSE, 0);
1094 gtk_widget_show(ok);
1095 gtk_widget_set_can_default(ok, TRUE);
1096 gtk_window_set_default(GTK_WINDOW(window), ok);
1097 g_signal_connect(G_OBJECT(ok), "clicked",
1098 G_CALLBACK(errmsg_button_clicked), window);
1099 g_signal_connect(G_OBJECT(window), "destroy",
1100 G_CALLBACK(window_destroy), NULL);
1101 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1102 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
1103 set_transient_window_pos(dp->window, window);
1104 gtk_widget_show(window);
1110 * This function signals to the front end that the dialog's
1111 * processing is completed, and passes an integer value (typically
1112 * a success status).
1114 void dlg_end(void *dlg, int value)
1116 struct dlgparam *dp = (struct dlgparam *)dlg;
1118 gtk_widget_destroy(dp->window);
1121 void dlg_refresh(union control *ctrl, void *dlg)
1123 struct dlgparam *dp = (struct dlgparam *)dlg;
1127 if (ctrl->generic.handler != NULL)
1128 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1132 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
1133 assert(uc->ctrl != NULL);
1134 if (uc->ctrl->generic.handler != NULL)
1135 uc->ctrl->generic.handler(uc->ctrl, dp,
1136 dp->data, EVENT_REFRESH);
1141 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
1143 struct dlgparam *dp = (struct dlgparam *)dlg;
1144 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
1146 #if GTK_CHECK_VERSION(3,0,0)
1147 GtkWidget *coloursel =
1148 gtk_color_chooser_dialog_new("Select a colour",
1149 GTK_WINDOW(dp->window));
1150 gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(coloursel), FALSE);
1152 GtkWidget *okbutton, *cancelbutton;
1153 GtkWidget *coloursel =
1154 gtk_color_selection_dialog_new("Select a colour");
1155 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
1156 GtkColorSelection *cs = GTK_COLOR_SELECTION
1157 (gtk_color_selection_dialog_get_color_selection(ccs));
1158 gtk_color_selection_set_has_opacity_control(cs, FALSE);
1161 dp->coloursel_result.ok = FALSE;
1163 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
1165 #if GTK_CHECK_VERSION(3,0,0)
1168 rgba.red = r / 255.0;
1169 rgba.green = g / 255.0;
1170 rgba.blue = b / 255.0;
1171 rgba.alpha = 1.0; /* fully opaque! */
1172 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(coloursel), &rgba);
1174 #elif GTK_CHECK_VERSION(2,0,0)
1177 col.red = r * 0x0101;
1178 col.green = g * 0x0101;
1179 col.blue = b * 0x0101;
1180 gtk_color_selection_set_current_color(cs, &col);
1185 cvals[0] = r / 255.0;
1186 cvals[1] = g / 255.0;
1187 cvals[2] = b / 255.0;
1188 cvals[3] = 1.0; /* fully opaque! */
1189 gtk_color_selection_set_color(cs, cvals);
1193 g_object_set_data(G_OBJECT(coloursel), "user-data", (gpointer)uc);
1195 #if GTK_CHECK_VERSION(3,0,0)
1196 g_signal_connect(G_OBJECT(coloursel), "response",
1197 G_CALLBACK(colourchoose_response), (gpointer)dp);
1200 #if GTK_CHECK_VERSION(2,0,0)
1201 g_object_get(G_OBJECT(ccs),
1202 "ok-button", &okbutton,
1203 "cancel-button", &cancelbutton,
1204 (const char *)NULL);
1206 okbutton = ccs->ok_button;
1207 cancelbutton = ccs->cancel_button;
1209 g_object_set_data(G_OBJECT(okbutton), "user-data",
1210 (gpointer)coloursel);
1211 g_object_set_data(G_OBJECT(cancelbutton), "user-data",
1212 (gpointer)coloursel);
1213 g_signal_connect(G_OBJECT(okbutton), "clicked",
1214 G_CALLBACK(coloursel_ok), (gpointer)dp);
1215 g_signal_connect(G_OBJECT(cancelbutton), "clicked",
1216 G_CALLBACK(coloursel_cancel), (gpointer)dp);
1217 g_signal_connect_swapped(G_OBJECT(okbutton), "clicked",
1218 G_CALLBACK(gtk_widget_destroy),
1219 (gpointer)coloursel);
1220 g_signal_connect_swapped(G_OBJECT(cancelbutton), "clicked",
1221 G_CALLBACK(gtk_widget_destroy),
1222 (gpointer)coloursel);
1224 gtk_widget_show(coloursel);
1227 int dlg_coloursel_results(union control *ctrl, void *dlg,
1228 int *r, int *g, int *b)
1230 struct dlgparam *dp = (struct dlgparam *)dlg;
1231 if (dp->coloursel_result.ok) {
1232 *r = dp->coloursel_result.r;
1233 *g = dp->coloursel_result.g;
1234 *b = dp->coloursel_result.b;
1240 /* ----------------------------------------------------------------------
1241 * Signal handlers while the dialog box is active.
1244 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
1247 struct dlgparam *dp = (struct dlgparam *)data;
1248 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1249 union control *focus;
1256 if (focus != dp->currfocus) {
1257 dp->lastfocus = dp->currfocus;
1258 dp->currfocus = focus;
1264 static void button_clicked(GtkButton *button, gpointer data)
1266 struct dlgparam *dp = (struct dlgparam *)data;
1267 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1268 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1271 static void button_toggled(GtkToggleButton *tb, gpointer data)
1273 struct dlgparam *dp = (struct dlgparam *)data;
1274 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
1275 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1278 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
1282 * GtkEntry has a nasty habit of eating the Return key, which
1283 * is unhelpful since it doesn't actually _do_ anything with it
1284 * (it calls gtk_widget_activate, but our edit boxes never need
1285 * activating). So I catch Return before GtkEntry sees it, and
1286 * pass it straight on to the parent widget. Effect: hitting
1287 * Return in an edit box will now activate the default button
1288 * in the dialog just like it will everywhere else.
1290 GtkWidget *parent = gtk_widget_get_parent(widget);
1291 if (event->keyval == GDK_KEY_Return && parent != NULL) {
1292 gboolean return_val;
1293 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
1294 g_signal_emit_by_name(G_OBJECT(parent), "key_press_event",
1295 event, &return_val);
1301 static void editbox_changed(GtkEditable *ed, gpointer data)
1303 struct dlgparam *dp = (struct dlgparam *)data;
1304 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1305 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1306 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1310 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1313 struct dlgparam *dp = (struct dlgparam *)data;
1314 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1315 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1319 #if !GTK_CHECK_VERSION(2,0,0)
1322 * GTK 1 list box event handlers.
1325 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1326 gpointer data, int multiple)
1328 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1330 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1331 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1332 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1333 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1335 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1336 * in a list box. So, if the list box is single-selection:
1338 * - if the list item in question isn't already selected,
1339 * we simply select it.
1340 * - otherwise, we find the next one (or next
1341 * however-far-away) in whichever direction we're going,
1343 * + in this case, we must also fiddle with the
1344 * scrollbar to ensure the newly selected item is
1347 * If it's multiple-selection, we do all of the above
1348 * except actually selecting anything, so we move the focus
1349 * and fiddle the scrollbar to follow it.
1351 GtkWidget *list = item->parent;
1353 g_signal_stop_emission_by_name(G_OBJECT(item), "key_press_event");
1356 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1357 gtk_list_select_child(GTK_LIST(list), item);
1360 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1361 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1364 (event->keyval==GDK_Page_Down ||
1365 event->keyval==GDK_KP_Page_Down ||
1366 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1369 GList *children, *chead;
1371 chead = children = gtk_container_children(GTK_CONTAINER(list));
1373 n = g_list_length(children);
1377 * Figure out how many list items to a screenful,
1378 * and adjust the step appropriately.
1380 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1381 step--; /* go by one less than that */
1385 while (children != NULL) {
1386 if (item == children->data)
1388 children = children->next;
1393 if (direction < 0 && i > 0)
1394 children = children->prev, i--;
1395 else if (direction > 0 && i < n-1)
1396 children = children->next, i++;
1400 if (children && children->data) {
1402 gtk_list_select_child(GTK_LIST(list),
1403 GTK_WIDGET(children->data));
1404 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1405 gtk_adjustment_clamp_page
1407 adj->lower + (adj->upper-adj->lower) * i / n,
1408 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1419 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1422 return listitem_key(item, event, data, FALSE);
1425 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1428 return listitem_key(item, event, data, TRUE);
1431 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1434 struct dlgparam *dp = (struct dlgparam *)data;
1435 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1436 switch (event->type) {
1438 case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1439 case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1440 case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1445 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1448 struct dlgparam *dp = (struct dlgparam *)data;
1449 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1450 if (uc->nclicks>1) {
1451 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1457 static void list_selchange(GtkList *list, gpointer data)
1459 struct dlgparam *dp = (struct dlgparam *)data;
1460 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1462 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1465 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1467 int index = dlg_listbox_index(uc->ctrl, dp);
1468 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1472 (index == 0 && direction < 0) ||
1473 (index == g_list_length(children)-1 && direction > 0)) {
1478 child = g_list_nth_data(children, index);
1479 gtk_widget_ref(child);
1480 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1481 g_list_free(children);
1484 children = g_list_append(children, child);
1485 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1486 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1487 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1490 static void draglist_up(GtkButton *button, gpointer data)
1492 struct dlgparam *dp = (struct dlgparam *)data;
1493 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1494 draglist_move(dp, uc, -1);
1497 static void draglist_down(GtkButton *button, gpointer data)
1499 struct dlgparam *dp = (struct dlgparam *)data;
1500 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1501 draglist_move(dp, uc, +1);
1504 #else /* !GTK_CHECK_VERSION(2,0,0) */
1507 * GTK 2 list box event handlers.
1510 static void listbox_doubleclick(GtkTreeView *treeview, GtkTreePath *path,
1511 GtkTreeViewColumn *column, gpointer data)
1513 struct dlgparam *dp = (struct dlgparam *)data;
1514 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(treeview));
1516 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1519 static void listbox_selchange(GtkTreeSelection *treeselection,
1522 struct dlgparam *dp = (struct dlgparam *)data;
1523 GtkTreeView *tree = gtk_tree_selection_get_tree_view(treeselection);
1524 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1526 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1529 struct draglist_valchange_ctx {
1531 struct dlgparam *dp;
1534 static gboolean draglist_valchange(gpointer data)
1536 struct draglist_valchange_ctx *ctx =
1537 (struct draglist_valchange_ctx *)data;
1539 ctx->uc->ctrl->generic.handler(ctx->uc->ctrl, ctx->dp,
1540 ctx->dp->data, EVENT_VALCHANGE);
1547 static void listbox_reorder(GtkTreeModel *treemodel, GtkTreePath *path,
1548 GtkTreeIter *iter, gpointer data)
1550 struct dlgparam *dp = (struct dlgparam *)data;
1554 if (dp->flags & FLAG_UPDATING_LISTBOX)
1555 return; /* not a user drag operation */
1557 tree = g_object_get_data(G_OBJECT(treemodel), "user-data");
1558 uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1561 * We should cause EVENT_VALCHANGE on the list box, now
1562 * that its rows have been reordered. However, the GTK 2
1563 * docs say that at the point this signal is received the
1564 * new row might not have actually been filled in yet.
1566 * (So what smegging use is it then, eh? Don't suppose it
1567 * occurred to you at any point that letting the
1568 * application know _after_ the reordering was compelete
1569 * might be helpful to someone?)
1571 * To get round this, I schedule an idle function, which I
1572 * hope won't be called until the main event loop is
1573 * re-entered after the drag-and-drop handler has finished
1574 * furtling with the list store.
1576 struct draglist_valchange_ctx *ctx =
1577 snew(struct draglist_valchange_ctx);
1580 g_idle_add(draglist_valchange, ctx);
1584 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1586 #if !GTK_CHECK_VERSION(2,4,0)
1588 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1590 struct dlgparam *dp = (struct dlgparam *)data;
1591 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1592 gpointer optmenu = g_object_get_data(G_OBJECT(menushell), "user-data");
1593 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1594 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1599 static void droplist_selchange(GtkComboBox *combo, gpointer data)
1601 struct dlgparam *dp = (struct dlgparam *)data;
1602 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(combo));
1604 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1607 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1609 #ifdef USE_GTK_FILE_CHOOSER_DIALOG
1610 static void filechoose_response(GtkDialog *dialog, gint response,
1613 /* struct dlgparam *dp = (struct dlgparam *)data; */
1614 struct uctrl *uc = g_object_get_data(G_OBJECT(dialog), "user-data");
1615 if (response == GTK_RESPONSE_ACCEPT) {
1616 gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1617 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1620 gtk_widget_destroy(GTK_WIDGET(dialog));
1623 static void filesel_ok(GtkButton *button, gpointer data)
1625 /* struct dlgparam *dp = (struct dlgparam *)data; */
1626 gpointer filesel = g_object_get_data(G_OBJECT(button), "user-data");
1627 struct uctrl *uc = g_object_get_data(G_OBJECT(filesel), "user-data");
1628 const char *name = gtk_file_selection_get_filename
1629 (GTK_FILE_SELECTION(filesel));
1630 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1634 static void fontsel_ok(GtkButton *button, gpointer data)
1636 /* struct dlgparam *dp = (struct dlgparam *)data; */
1638 #if !GTK_CHECK_VERSION(2,0,0)
1640 gpointer fontsel = g_object_get_data(G_OBJECT(button), "user-data");
1641 struct uctrl *uc = g_object_get_data(G_OBJECT(fontsel), "user-data");
1642 const char *name = gtk_font_selection_dialog_get_font_name
1643 (GTK_FONT_SELECTION_DIALOG(fontsel));
1644 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1648 unifontsel *fontsel = (unifontsel *)g_object_get_data
1649 (G_OBJECT(button), "user-data");
1650 struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1651 char *name = unifontsel_get_name(fontsel);
1652 assert(name); /* should always be ok after OK pressed */
1653 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1659 #if GTK_CHECK_VERSION(3,0,0)
1661 static void colourchoose_response(GtkDialog *dialog,
1662 gint response_id, gpointer data)
1664 struct dlgparam *dp = (struct dlgparam *)data;
1665 struct uctrl *uc = g_object_get_data(G_OBJECT(dialog), "user-data");
1667 if (response_id == GTK_RESPONSE_OK) {
1669 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(dialog), &rgba);
1670 dp->coloursel_result.r = (int) (255 * rgba.red);
1671 dp->coloursel_result.g = (int) (255 * rgba.green);
1672 dp->coloursel_result.b = (int) (255 * rgba.blue);
1673 dp->coloursel_result.ok = TRUE;
1675 dp->coloursel_result.ok = FALSE;
1678 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1680 gtk_widget_destroy(GTK_WIDGET(dialog));
1683 #else /* GTK 1/2 coloursel response handlers */
1685 static void coloursel_ok(GtkButton *button, gpointer data)
1687 struct dlgparam *dp = (struct dlgparam *)data;
1688 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1689 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1691 #if GTK_CHECK_VERSION(2,0,0)
1693 GtkColorSelection *cs = GTK_COLOR_SELECTION
1694 (gtk_color_selection_dialog_get_color_selection
1695 (GTK_COLOR_SELECTION_DIALOG(coloursel)));
1697 gtk_color_selection_get_current_color(cs, &col);
1698 dp->coloursel_result.r = col.red / 0x0100;
1699 dp->coloursel_result.g = col.green / 0x0100;
1700 dp->coloursel_result.b = col.blue / 0x0100;
1704 GtkColorSelection *cs = GTK_COLOR_SELECTION
1705 (gtk_color_selection_dialog_get_color_selection
1706 (GTK_COLOR_SELECTION_DIALOG(coloursel)));
1708 gtk_color_selection_get_color(cs, cvals);
1709 dp->coloursel_result.r = (int) (255 * cvals[0]);
1710 dp->coloursel_result.g = (int) (255 * cvals[1]);
1711 dp->coloursel_result.b = (int) (255 * cvals[2]);
1714 dp->coloursel_result.ok = TRUE;
1715 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1718 static void coloursel_cancel(GtkButton *button, gpointer data)
1720 struct dlgparam *dp = (struct dlgparam *)data;
1721 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1722 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1723 dp->coloursel_result.ok = FALSE;
1724 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1727 #endif /* end of coloursel response handlers */
1729 static void filefont_clicked(GtkButton *button, gpointer data)
1731 struct dlgparam *dp = (struct dlgparam *)data;
1732 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1734 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1735 #ifdef USE_GTK_FILE_CHOOSER_DIALOG
1736 GtkWidget *filechoose = gtk_file_chooser_dialog_new
1737 (uc->ctrl->fileselect.title, GTK_WINDOW(dp->window),
1738 (uc->ctrl->fileselect.for_writing ?
1739 GTK_FILE_CHOOSER_ACTION_SAVE :
1740 GTK_FILE_CHOOSER_ACTION_OPEN),
1741 STANDARD_CANCEL_LABEL, GTK_RESPONSE_CANCEL,
1742 STANDARD_OPEN_LABEL, GTK_RESPONSE_ACCEPT,
1743 (const gchar *)NULL);
1744 gtk_window_set_modal(GTK_WINDOW(filechoose), TRUE);
1745 g_object_set_data(G_OBJECT(filechoose), "user-data", (gpointer)uc);
1746 g_signal_connect(G_OBJECT(filechoose), "response",
1747 G_CALLBACK(filechoose_response), (gpointer)dp);
1748 gtk_widget_show(filechoose);
1750 GtkWidget *filesel =
1751 gtk_file_selection_new(uc->ctrl->fileselect.title);
1752 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1754 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1756 g_object_set_data(G_OBJECT(filesel), "user-data", (gpointer)uc);
1758 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1759 G_CALLBACK(filesel_ok), (gpointer)dp);
1760 g_signal_connect_swapped
1761 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1762 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1763 g_signal_connect_swapped
1764 (G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1765 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1766 gtk_widget_show(filesel);
1770 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1771 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1773 #if !GTK_CHECK_VERSION(2,0,0)
1776 * Use the GTK 1 standard font selector.
1779 gchar *spacings[] = { "c", "m", NULL };
1780 GtkWidget *fontsel =
1781 gtk_font_selection_dialog_new("Select a font");
1782 gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1783 gtk_font_selection_dialog_set_filter
1784 (GTK_FONT_SELECTION_DIALOG(fontsel),
1785 GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1786 NULL, NULL, NULL, NULL, spacings, NULL);
1787 if (!gtk_font_selection_dialog_set_font_name
1788 (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1790 * If the font name wasn't found as it was, try opening
1791 * it and extracting its FONT property. This should
1792 * have the effect of mapping short aliases into true
1795 GdkFont *font = gdk_font_load(fontname);
1797 XFontStruct *xfs = GDK_FONT_XFONT(font);
1798 Display *disp = GDK_FONT_XDISPLAY(font);
1799 Atom fontprop = XInternAtom(disp, "FONT", False);
1802 if (XGetFontProperty(xfs, fontprop, &ret)) {
1803 char *name = XGetAtomName(disp, (Atom)ret);
1805 gtk_font_selection_dialog_set_font_name
1806 (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1808 gdk_font_unref(font);
1812 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1813 "user-data", (gpointer)fontsel);
1814 g_object_set_data(G_OBJECT(fontsel), "user-data", (gpointer)uc);
1816 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1817 "clicked", G_CALLBACK(fontsel_ok), (gpointer)dp);
1818 g_signal_connect_swapped
1819 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1820 "clicked", G_CALLBACK(gtk_widget_destroy),
1822 g_signal_connect_swapped
1823 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1824 "clicked", G_CALLBACK(gtk_widget_destroy),
1826 gtk_widget_show(fontsel);
1828 #else /* !GTK_CHECK_VERSION(2,0,0) */
1831 * Use the unifontsel code provided in gtkfont.c.
1834 unifontsel *fontsel = unifontsel_new("Select a font");
1836 gtk_window_set_modal(fontsel->window, TRUE);
1837 unifontsel_set_name(fontsel, fontname);
1839 g_object_set_data(G_OBJECT(fontsel->ok_button),
1840 "user-data", (gpointer)fontsel);
1841 fontsel->user_data = uc;
1842 g_signal_connect(G_OBJECT(fontsel->ok_button), "clicked",
1843 G_CALLBACK(fontsel_ok), (gpointer)dp);
1844 g_signal_connect_swapped(G_OBJECT(fontsel->ok_button), "clicked",
1845 G_CALLBACK(unifontsel_destroy),
1847 g_signal_connect_swapped(G_OBJECT(fontsel->cancel_button),"clicked",
1848 G_CALLBACK(unifontsel_destroy),
1851 gtk_widget_show(GTK_WIDGET(fontsel->window));
1853 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1858 #if !GTK_CHECK_VERSION(3,0,0)
1859 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1862 struct dlgparam *dp = (struct dlgparam *)data;
1863 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1865 gtk_widget_set_size_request(uc->text, alloc->width, -1);
1866 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1867 g_signal_handler_disconnect(G_OBJECT(uc->text), uc->textsig);
1871 /* ----------------------------------------------------------------------
1872 * This function does the main layout work: it reads a controlset,
1873 * it creates the relevant GTK controls, and returns a GtkWidget
1874 * containing the result. (This widget might be a title of some
1875 * sort, it might be a Columns containing many controls, or it
1876 * might be a GtkFrame containing a Columns; whatever it is, it's
1877 * definitely a GtkWidget and should probably be added to a
1880 * `win' is required for setting the default button. If it is
1881 * non-NULL, all buttons created will be default-capable (so they
1882 * have extra space round them for the default highlight).
1884 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1885 struct controlset *s, GtkWindow *win)
1891 if (!s->boxname && s->boxtitle) {
1892 /* This controlset is a panel title. */
1893 return gtk_label_new(s->boxtitle);
1897 * Otherwise, we expect to be laying out actual controls, so
1898 * we'll start by creating a Columns for the purpose.
1900 cols = COLUMNS(columns_new(4));
1901 ret = GTK_WIDGET(cols);
1902 gtk_widget_show(ret);
1905 * Create a containing frame if we have a box name.
1908 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1909 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1910 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1911 gtk_widget_show(ret);
1915 * Now iterate through the controls themselves, create them,
1916 * and add them to the Columns.
1918 for (i = 0; i < s->ncontrols; i++) {
1919 union control *ctrl = s->ctrls[i];
1922 GtkWidget *w = NULL;
1924 switch (ctrl->generic.type) {
1927 static const int simplecols[1] = { 100 };
1928 columns_set_cols(cols, ctrl->columns.ncols,
1929 (ctrl->columns.percentages ?
1930 ctrl->columns.percentages : simplecols));
1932 continue; /* no actual control created */
1935 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1937 columns_taborder_last(cols, uc->toplevel);
1939 continue; /* no actual control created */
1942 uc = snew(struct uctrl);
1946 #if !GTK_CHECK_VERSION(2,4,0)
1947 uc->list = uc->menu = uc->optmenu = NULL;
1951 #if GTK_CHECK_VERSION(2,0,0)
1952 uc->treeview = NULL;
1953 uc->listmodel = NULL;
1955 uc->button = uc->text = NULL;
1959 switch (ctrl->generic.type) {
1961 w = gtk_button_new_with_label(ctrl->generic.label);
1963 gtk_widget_set_can_default(w, TRUE);
1964 if (ctrl->button.isdefault)
1965 gtk_window_set_default(win, w);
1966 if (ctrl->button.iscancel)
1967 dp->cancelbutton = w;
1969 g_signal_connect(G_OBJECT(w), "clicked",
1970 G_CALLBACK(button_clicked), dp);
1971 g_signal_connect(G_OBJECT(w), "focus_in_event",
1972 G_CALLBACK(widget_focus), dp);
1973 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1974 ctrl->button.shortcut, SHORTCUT_UCTRL, uc);
1977 w = gtk_check_button_new_with_label(ctrl->generic.label);
1978 g_signal_connect(G_OBJECT(w), "toggled",
1979 G_CALLBACK(button_toggled), dp);
1980 g_signal_connect(G_OBJECT(w), "focus_in_event",
1981 G_CALLBACK(widget_focus), dp);
1982 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1983 ctrl->checkbox.shortcut, SHORTCUT_UCTRL, uc);
1988 * Radio buttons get to go inside their own Columns, no
1992 gint i, *percentages;
1996 if (ctrl->generic.label) {
1997 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1998 columns_add(COLUMNS(w), label, 0, 1);
1999 columns_force_left_align(COLUMNS(w), label);
2000 gtk_widget_show(label);
2001 shortcut_add(scs, label, ctrl->radio.shortcut,
2002 SHORTCUT_UCTRL, uc);
2005 percentages = g_new(gint, ctrl->radio.ncolumns);
2006 for (i = 0; i < ctrl->radio.ncolumns; i++) {
2008 ((100 * (i+1) / ctrl->radio.ncolumns) -
2009 100 * i / ctrl->radio.ncolumns);
2011 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
2013 g_free(percentages);
2016 uc->nbuttons = ctrl->radio.nbuttons;
2017 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
2019 for (i = 0; i < ctrl->radio.nbuttons; i++) {
2023 b = (gtk_radio_button_new_with_label
2024 (group, ctrl->radio.buttons[i]));
2026 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(b));
2027 colstart = i % ctrl->radio.ncolumns;
2028 columns_add(COLUMNS(w), b, colstart,
2029 (i == ctrl->radio.nbuttons-1 ?
2030 ctrl->radio.ncolumns - colstart : 1));
2031 columns_force_left_align(COLUMNS(w), b);
2033 g_signal_connect(G_OBJECT(b), "toggled",
2034 G_CALLBACK(button_toggled), dp);
2035 g_signal_connect(G_OBJECT(b), "focus_in_event",
2036 G_CALLBACK(widget_focus), dp);
2037 if (ctrl->radio.shortcuts) {
2038 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(b)),
2039 ctrl->radio.shortcuts[i],
2040 SHORTCUT_UCTRL, uc);
2047 GtkWidget *signalobject;
2049 if (ctrl->editbox.has_list) {
2050 #if !GTK_CHECK_VERSION(2,4,0)
2054 w = gtk_combo_new();
2055 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
2056 uc->entry = GTK_COMBO(w)->entry;
2057 uc->list = GTK_COMBO(w)->list;
2058 signalobject = uc->entry;
2063 uc->listmodel = gtk_list_store_new(2, G_TYPE_INT,
2065 w = gtk_combo_box_new_with_model_and_entry
2066 (GTK_TREE_MODEL(uc->listmodel));
2067 /* We cannot support password combo boxes. */
2068 assert(!ctrl->editbox.password);
2070 signalobject = uc->combo;
2073 w = gtk_entry_new();
2074 if (ctrl->editbox.password)
2075 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
2080 g_signal_connect(G_OBJECT(signalobject), "changed",
2081 G_CALLBACK(editbox_changed), dp);
2082 g_signal_connect(G_OBJECT(signalobject), "key_press_event",
2083 G_CALLBACK(editbox_key), dp);
2084 g_signal_connect(G_OBJECT(signalobject), "focus_in_event",
2085 G_CALLBACK(widget_focus), dp);
2086 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
2087 G_CALLBACK(editbox_lostfocus), dp);
2088 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
2089 G_CALLBACK(editbox_lostfocus), dp);
2091 #if !GTK_CHECK_VERSION(3,0,0)
2093 * Edit boxes, for some strange reason, have a minimum
2094 * width of 150 in GTK 1.2. We don't want this - we'd
2095 * rather the edit boxes acquired their natural width
2096 * from the column layout of the rest of the box.
2100 gtk_widget_size_request(w, &req);
2101 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 columns_force_same_height(COLUMNS(container),
2136 gtk_widget_show(label);
2144 case CTRL_FILESELECT:
2145 case CTRL_FONTSELECT:
2148 const char *browsebtn =
2149 (ctrl->generic.type == CTRL_FILESELECT ?
2150 "Browse..." : "Change...");
2152 gint percentages[] = { 75, 25 };
2154 columns_set_cols(COLUMNS(w), 2, percentages);
2156 if (ctrl->generic.label) {
2157 ww = gtk_label_new(ctrl->generic.label);
2158 columns_add(COLUMNS(w), ww, 0, 2);
2159 columns_force_left_align(COLUMNS(w), ww);
2160 gtk_widget_show(ww);
2161 shortcut_add(scs, ww,
2162 (ctrl->generic.type == CTRL_FILESELECT ?
2163 ctrl->fileselect.shortcut :
2164 ctrl->fontselect.shortcut),
2165 SHORTCUT_UCTRL, uc);
2169 uc->entry = ww = gtk_entry_new();
2170 #if !GTK_CHECK_VERSION(3,0,0)
2173 gtk_widget_size_request(ww, &req);
2174 gtk_widget_set_size_request(ww, 10, req.height);
2177 gtk_entry_set_width_chars(GTK_ENTRY(ww), 1);
2179 columns_add(COLUMNS(w), ww, 0, 1);
2180 gtk_widget_show(ww);
2182 uc->button = ww = gtk_button_new_with_label(browsebtn);
2183 columns_add(COLUMNS(w), ww, 1, 1);
2184 gtk_widget_show(ww);
2186 columns_force_same_height(COLUMNS(w), uc->entry, uc->button);
2188 g_signal_connect(G_OBJECT(uc->entry), "key_press_event",
2189 G_CALLBACK(editbox_key), dp);
2191 g_signal_connect(G_OBJECT(uc->entry), "changed",
2192 G_CALLBACK(editbox_changed), dp);
2193 g_signal_connect(G_OBJECT(uc->entry), "focus_in_event",
2194 G_CALLBACK(widget_focus), dp);
2195 g_signal_connect(G_OBJECT(uc->button), "focus_in_event",
2196 G_CALLBACK(widget_focus), dp);
2197 g_signal_connect(G_OBJECT(ww), "clicked",
2198 G_CALLBACK(filefont_clicked), dp);
2203 #if GTK_CHECK_VERSION(2,0,0)
2205 * First construct the list data store, with the right
2206 * number of columns.
2208 # if !GTK_CHECK_VERSION(2,4,0)
2209 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2210 * because combo boxes are still done the old GTK1 way.) */
2211 if (ctrl->listbox.height > 0)
2218 cols = ctrl->listbox.ncols;
2219 cols = cols ? cols : 1;
2220 types = snewn(1 + cols, GType);
2222 types[0] = G_TYPE_INT;
2223 for (i = 0; i < cols; i++)
2224 types[i+1] = G_TYPE_STRING;
2226 uc->listmodel = gtk_list_store_newv(1 + cols, types);
2233 * See if it's a drop-down list (non-editable combo
2236 if (ctrl->listbox.height == 0) {
2237 #if !GTK_CHECK_VERSION(2,4,0)
2239 * GTK1 and early-GTK2 option-menu style of
2242 uc->optmenu = w = gtk_option_menu_new();
2243 uc->menu = gtk_menu_new();
2244 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
2245 g_object_set_data(G_OBJECT(uc->menu), "user-data",
2246 (gpointer)uc->optmenu);
2247 g_signal_connect(G_OBJECT(uc->optmenu), "focus_in_event",
2248 G_CALLBACK(widget_focus), dp);
2251 * Late-GTK2 style using a GtkComboBox.
2253 GtkCellRenderer *cr;
2256 * Create a non-editable GtkComboBox (that is, not
2257 * its subclass GtkComboBoxEntry).
2259 w = gtk_combo_box_new_with_model
2260 (GTK_TREE_MODEL(uc->listmodel));
2264 * Tell it how to render a list item (i.e. which
2265 * column to look at in the list model).
2267 cr = gtk_cell_renderer_text_new();
2268 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), cr, TRUE);
2269 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), cr,
2273 * And tell it to notify us when the selection
2276 g_signal_connect(G_OBJECT(w), "changed",
2277 G_CALLBACK(droplist_selchange), dp);
2280 #if !GTK_CHECK_VERSION(2,0,0)
2282 * GTK1-style full list box.
2284 uc->list = gtk_list_new();
2285 if (ctrl->listbox.multisel == 2) {
2286 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2287 GTK_SELECTION_EXTENDED);
2288 } else if (ctrl->listbox.multisel == 1) {
2289 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2290 GTK_SELECTION_MULTIPLE);
2292 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2293 GTK_SELECTION_SINGLE);
2295 w = gtk_scrolled_window_new(NULL, NULL);
2296 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
2298 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
2300 GTK_POLICY_AUTOMATIC);
2301 uc->adj = gtk_scrolled_window_get_vadjustment
2302 (GTK_SCROLLED_WINDOW(w));
2304 gtk_widget_show(uc->list);
2305 g_signal_connect(G_OBJECT(uc->list), "selection-changed",
2306 G_CALLBACK(list_selchange), dp);
2307 g_signal_connect(G_OBJECT(uc->list), "focus_in_event",
2308 G_CALLBACK(widget_focus), dp);
2311 * Adjust the height of the scrolled window to the
2312 * minimum given by the height parameter.
2314 * This piece of guesswork is a horrid hack based
2315 * on looking inside the GTK 1.2 sources
2316 * (specifically gtkviewport.c, which appears to be
2317 * the widget which provides the border around the
2318 * scrolling area). Anyone lets me know how I can
2319 * do this in a way which isn't at risk from GTK
2320 * upgrades, I'd be grateful.
2324 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
2325 gtk_widget_set_size_request(w, 10,
2326 2*edge + (ctrl->listbox.height *
2327 get_listitemheight(w)));
2330 if (ctrl->listbox.draglist) {
2332 * GTK doesn't appear to make it easy to
2333 * implement a proper draggable list; so
2334 * instead I'm just going to have to put an Up
2335 * and a Down button to the right of the actual
2336 * list box. Ah well.
2338 GtkWidget *cols, *button;
2339 static const gint percentages[2] = { 80, 20 };
2341 cols = columns_new(4);
2342 columns_set_cols(COLUMNS(cols), 2, percentages);
2343 columns_add(COLUMNS(cols), w, 0, 1);
2345 button = gtk_button_new_with_label("Up");
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_up), dp);
2350 g_signal_connect(G_OBJECT(button), "focus_in_event",
2351 G_CALLBACK(widget_focus), dp);
2352 button = gtk_button_new_with_label("Down");
2353 columns_add(COLUMNS(cols), button, 1, 1);
2354 gtk_widget_show(button);
2355 g_signal_connect(G_OBJECT(button), "clicked",
2356 G_CALLBACK(draglist_down), dp);
2357 g_signal_connect(G_OBJECT(button), "focus_in_event",
2358 G_CALLBACK(widget_focus), dp);
2364 * GTK2 treeview-based full list box.
2366 GtkTreeSelection *sel;
2369 * Create the list box itself, its columns, and
2370 * its containing scrolled window.
2372 w = gtk_tree_view_new_with_model
2373 (GTK_TREE_MODEL(uc->listmodel));
2374 g_object_set_data(G_OBJECT(uc->listmodel), "user-data",
2376 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2377 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
2378 gtk_tree_selection_set_mode
2379 (sel, ctrl->listbox.multisel ? GTK_SELECTION_MULTIPLE :
2380 GTK_SELECTION_SINGLE);
2382 g_signal_connect(G_OBJECT(w), "row-activated",
2383 G_CALLBACK(listbox_doubleclick), dp);
2384 g_signal_connect(G_OBJECT(sel), "changed",
2385 G_CALLBACK(listbox_selchange), dp);
2387 if (ctrl->listbox.draglist) {
2388 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w), TRUE);
2389 g_signal_connect(G_OBJECT(uc->listmodel), "row-inserted",
2390 G_CALLBACK(listbox_reorder), dp);
2397 cols = ctrl->listbox.ncols;
2398 cols = cols ? cols : 1;
2399 for (i = 0; i < cols; i++) {
2400 GtkTreeViewColumn *column;
2401 GtkCellRenderer *cellrend;
2403 * It appears that GTK 2 doesn't leave us any
2404 * particularly sensible way to honour the
2405 * "percentages" specification in the ctrl
2408 cellrend = gtk_cell_renderer_text_new();
2409 if (!ctrl->listbox.hscroll) {
2410 g_object_set(G_OBJECT(cellrend),
2411 "ellipsize", PANGO_ELLIPSIZE_END,
2412 "ellipsize-set", TRUE,
2415 column = gtk_tree_view_column_new_with_attributes
2416 ("heading", cellrend, "text", i+1, (char *)NULL);
2417 gtk_tree_view_column_set_sizing
2418 (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2419 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2426 scroll = gtk_scrolled_window_new(NULL, NULL);
2427 gtk_scrolled_window_set_shadow_type
2428 (GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
2430 gtk_container_add(GTK_CONTAINER(scroll), w);
2431 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2432 GTK_POLICY_AUTOMATIC,
2434 gtk_widget_set_size_request
2436 ctrl->listbox.height * get_listitemheight(w));
2443 if (ctrl->generic.label) {
2444 GtkWidget *label, *container;
2446 label = gtk_label_new(ctrl->generic.label);
2447 #if GTK_CHECK_VERSION(3,0,0)
2448 gtk_label_set_width_chars(GTK_LABEL(label), 3);
2451 shortcut_add(scs, label, ctrl->listbox.shortcut,
2454 container = columns_new(4);
2455 if (ctrl->listbox.percentwidth == 100) {
2456 columns_add(COLUMNS(container), label, 0, 1);
2457 columns_force_left_align(COLUMNS(container), label);
2458 columns_add(COLUMNS(container), w, 0, 1);
2460 gint percentages[2];
2461 percentages[1] = ctrl->listbox.percentwidth;
2462 percentages[0] = 100 - ctrl->listbox.percentwidth;
2463 columns_set_cols(COLUMNS(container), 2, percentages);
2464 columns_add(COLUMNS(container), label, 0, 1);
2465 columns_force_left_align(COLUMNS(container), label);
2466 columns_add(COLUMNS(container), w, 1, 1);
2467 columns_force_same_height(COLUMNS(container),
2470 gtk_widget_show(label);
2479 #if !GTK_CHECK_VERSION(3,0,0)
2481 * Wrapping text widgets don't sit well with the GTK2
2482 * layout model, in which widgets state a minimum size
2483 * and the whole window then adjusts to the smallest
2484 * size it can sensibly take given its contents. A
2485 * wrapping text widget _has_ no clear minimum size;
2486 * instead it has a range of possibilities. It can be
2487 * one line deep but 2000 wide, or two lines deep and
2488 * 1000 pixels, or three by 867, or four by 500 and so
2489 * on. It can be as short as you like provided you
2490 * don't mind it being wide, or as narrow as you like
2491 * provided you don't mind it being tall.
2493 * Therefore, it fits very badly into the layout model.
2494 * Hence the only thing to do is pick a width and let
2495 * it choose its own number of lines. To do this I'm
2496 * going to cheat a little. All new wrapping text
2497 * widgets will be created with a minimal text content
2498 * "X"; then, after the rest of the dialog box is set
2499 * up and its size calculated, the text widgets will be
2500 * told their width and given their real text, which
2501 * will cause the size to be recomputed in the y
2502 * direction (because many of them will expand to more
2505 uc->text = w = gtk_label_new("X");
2507 g_signal_connect(G_OBJECT(w), "size-allocate",
2508 G_CALLBACK(label_sizealloc), dp);
2511 * In GTK3, this is all fixed, because the main aim of the
2512 * new 'height-for-width' geometry management is to make
2513 * wrapping labels behave sensibly. So now we can just do
2514 * the obvious thing.
2516 uc->text = w = gtk_label_new(uc->ctrl->generic.label);
2518 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
2519 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
2525 columns_add(cols, w,
2526 COLUMN_START(ctrl->generic.column),
2527 COLUMN_SPAN(ctrl->generic.column));
2529 columns_force_left_align(cols, w);
2533 dlg_add_uctrl(dp, uc);
2540 struct dlgparam *dp;
2541 GtkNotebook *panels;
2543 #if !GTK_CHECK_VERSION(2,0,0)
2544 GtkWidget *treeitem;
2547 GtkTreePath *treepath;
2549 struct Shortcuts shortcuts;
2552 #if GTK_CHECK_VERSION(2,0,0)
2553 static void treeselection_changed(GtkTreeSelection *treeselection,
2556 struct selparam *sps = (struct selparam *)data, *sp;
2557 GtkTreeModel *treemodel;
2558 GtkTreeIter treeiter;
2562 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2565 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
2568 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2569 gtk_notebook_set_current_page(sp->panels, page_num);
2571 dlg_refresh(NULL, sp->dp);
2573 sp->dp->shortcuts = &sp->shortcuts;
2576 static void treeitem_sel(GtkItem *item, gpointer data)
2578 struct selparam *sp = (struct selparam *)data;
2581 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2582 gtk_notebook_set_page(sp->panels, page_num);
2584 dlg_refresh(NULL, sp->dp);
2586 sp->dp->shortcuts = &sp->shortcuts;
2587 sp->dp->currtreeitem = sp->treeitem;
2591 static void window_destroy(GtkWidget *widget, gpointer data)
2596 #if !GTK_CHECK_VERSION(2,0,0)
2597 static int tree_grab_focus(struct dlgparam *dp)
2602 * See if any of the treeitems has the focus.
2605 for (i = 0; i < dp->ntreeitems; i++)
2606 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
2614 gtk_widget_grab_focus(dp->currtreeitem);
2619 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
2622 struct dlgparam *dp = (struct dlgparam *)data;
2624 g_signal_stop_emission_by_name(G_OBJECT(container), "focus");
2626 * If there's a focused treeitem, we return FALSE to cause the
2627 * focus to move on to some totally other control. If not, we
2628 * focus the selected one.
2630 return tree_grab_focus(dp);
2634 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2636 struct dlgparam *dp = (struct dlgparam *)data;
2638 if (event->keyval == GDK_KEY_Escape && dp->cancelbutton) {
2639 g_signal_emit_by_name(G_OBJECT(dp->cancelbutton), "clicked");
2643 if ((event->state & GDK_MOD1_MASK) &&
2644 (unsigned char)event->string[0] > 0 &&
2645 (unsigned char)event->string[0] <= 127) {
2646 int schr = (unsigned char)event->string[0];
2647 struct Shortcut *sc = &dp->shortcuts->sc[schr];
2649 switch (sc->action) {
2651 #if GTK_CHECK_VERSION(2,0,0)
2652 gtk_widget_grab_focus(sc->widget);
2654 tree_grab_focus(dp);
2657 case SHORTCUT_FOCUS:
2658 gtk_widget_grab_focus(sc->widget);
2660 case SHORTCUT_UCTRL:
2662 * We must do something sensible with a uctrl.
2663 * Precisely what this is depends on the type of
2666 switch (sc->uc->ctrl->generic.type) {
2669 /* Check boxes and buttons get the focus _and_ get toggled. */
2670 gtk_widget_grab_focus(sc->uc->toplevel);
2671 g_signal_emit_by_name(G_OBJECT(sc->uc->toplevel), "clicked");
2673 case CTRL_FILESELECT:
2674 case CTRL_FONTSELECT:
2675 /* File/font selectors have their buttons pressed (ooer),
2676 * and focus transferred to the edit box. */
2677 g_signal_emit_by_name(G_OBJECT(sc->uc->button), "clicked");
2678 gtk_widget_grab_focus(sc->uc->entry);
2682 * Radio buttons are fun, because they have
2683 * multiple shortcuts. We must find whether the
2684 * activated shortcut is the shortcut for the whole
2685 * group, or for a particular button. In the former
2686 * case, we find the currently selected button and
2687 * focus it; in the latter, we focus-and-click the
2688 * button whose shortcut was pressed.
2690 if (schr == sc->uc->ctrl->radio.shortcut) {
2692 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2693 if (gtk_toggle_button_get_active
2694 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
2695 gtk_widget_grab_focus(sc->uc->buttons[i]);
2697 } else if (sc->uc->ctrl->radio.shortcuts) {
2699 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2700 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
2701 gtk_widget_grab_focus(sc->uc->buttons[i]);
2702 g_signal_emit_by_name
2703 (G_OBJECT(sc->uc->buttons[i]), "clicked");
2709 #if !GTK_CHECK_VERSION(2,4,0)
2710 if (sc->uc->optmenu) {
2714 gtk_widget_grab_focus(sc->uc->optmenu);
2715 /* Option menus don't work using the "clicked" signal.
2716 * We need to manufacture a button press event :-/ */
2717 bev.type = GDK_BUTTON_PRESS;
2719 g_signal_emit_by_name(G_OBJECT(sc->uc->optmenu),
2720 "button_press_event",
2725 if (sc->uc->combo) {
2726 gtk_widget_grab_focus(sc->uc->combo);
2727 gtk_combo_box_popup(GTK_COMBO_BOX(sc->uc->combo));
2731 #if !GTK_CHECK_VERSION(2,0,0)
2734 * For GTK-1 style list boxes, we tell it to
2735 * focus one of its children, which appears to
2736 * do the Right Thing.
2738 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
2739 GTK_DIR_TAB_FORWARD);
2743 if (sc->uc->treeview) {
2744 gtk_widget_grab_focus(sc->uc->treeview);
2748 assert(!"We shouldn't get here");
2758 #if !GTK_CHECK_VERSION(2,0,0)
2759 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2761 struct dlgparam *dp = (struct dlgparam *)data;
2763 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2764 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2766 for (i = 0; i < dp->ntreeitems; i++)
2767 if (widget == dp->treeitems[i])
2769 if (i < dp->ntreeitems) {
2770 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2777 if (i < 0 || i >= dp->ntreeitems)
2778 break; /* nothing in that dir to select */
2780 * Determine if this tree item is visible.
2783 GtkWidget *w = dp->treeitems[i];
2785 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2786 if (!GTK_WIDGET_VISIBLE(w)) {
2793 j = i; /* got one */
2799 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2801 g_signal_emit_by_name(G_OBJECT(dp->treeitems[j]), "toggle");
2802 gtk_widget_grab_focus(dp->treeitems[j]);
2808 * It's nice for Left and Right to expand and collapse tree
2811 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2812 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2813 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2816 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2817 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2818 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2826 static void shortcut_highlight(GtkWidget *labelw, int chr)
2828 GtkLabel *label = GTK_LABEL(labelw);
2829 const gchar *currstr;
2833 #if !GTK_CHECK_VERSION(2,0,0)
2835 gchar *currstr_nonconst;
2836 gtk_label_get(label, &currstr_nonconst);
2837 currstr = currstr_nonconst;
2840 currstr = gtk_label_get_text(label);
2843 for (i = 0; currstr[i]; i++)
2844 if (tolower((unsigned char)currstr[i]) == chr) {
2845 pattern = dupprintf("%*s_", i, "");
2846 gtk_label_set_pattern(label, pattern);
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 #if GTK_CHECK_VERSION(2,0,0)
2981 void initial_treeview_collapse(struct dlgparam *dp, GtkWidget *tree)
2984 * Collapse the deeper branches of the treeview into the state we
2985 * like them to start off in. See comment below in do_config_box.
2988 for (i = 0; i < dp->nselparams; i++)
2989 if (dp->selparams[i].depth >= 2)
2990 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree),
2991 dp->selparams[i].treepath);
2995 #if GTK_CHECK_VERSION(3,0,0)
2996 void treeview_map_event(GtkWidget *tree, gpointer data)
2998 struct dlgparam *dp = (struct dlgparam *)data;
2999 GtkAllocation alloc;
3000 gtk_widget_get_allocation(tree, &alloc);
3001 gtk_widget_set_size_request(tree, alloc.width, -1);
3002 initial_treeview_collapse(dp, tree);
3006 int do_config_box(const char *title, Conf *conf, int midsession,
3009 GtkWidget *window, *hbox, *vbox, *cols, *label,
3010 *tree, *treescroll, *panels, *panelvbox;
3011 int index, level, protocol;
3012 struct controlbox *ctrlbox;
3014 #if GTK_CHECK_VERSION(2,0,0)
3015 GtkTreeStore *treestore;
3016 GtkCellRenderer *treerenderer;
3017 GtkTreeViewColumn *treecolumn;
3018 GtkTreeSelection *treeselection;
3019 GtkTreeIter treeiterlevels[8];
3021 GtkTreeItem *treeitemlevels[8];
3022 GtkTree *treelevels[8];
3025 struct Shortcuts scs;
3027 struct selparam *selparams = NULL;
3028 int nselparams = 0, selparamsize = 0;
3032 for (index = 0; index < lenof(scs.sc); index++) {
3033 scs.sc[index].action = SHORTCUT_EMPTY;
3036 window = gtk_dialog_new();
3038 ctrlbox = ctrl_new_box();
3039 protocol = conf_get_int(conf, CONF_protocol);
3040 setup_config_box(ctrlbox, midsession, protocol, protcfginfo);
3041 unix_setup_config_box(ctrlbox, midsession, protocol);
3042 gtk_setup_config_box(ctrlbox, midsession, window);
3044 gtk_window_set_title(GTK_WINDOW(window), title);
3045 hbox = gtk_hbox_new(FALSE, 4);
3047 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3048 hbox, TRUE, TRUE, 0);
3049 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
3050 gtk_widget_show(hbox);
3051 vbox = gtk_vbox_new(FALSE, 4);
3052 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
3053 gtk_widget_show(vbox);
3054 cols = columns_new(4);
3055 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
3056 gtk_widget_show(cols);
3057 label = gtk_label_new("Category:");
3058 columns_add(COLUMNS(cols), label, 0, 1);
3059 columns_force_left_align(COLUMNS(cols), label);
3060 gtk_widget_show(label);
3061 treescroll = gtk_scrolled_window_new(NULL, NULL);
3062 #if GTK_CHECK_VERSION(2,0,0)
3063 treestore = gtk_tree_store_new
3064 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
3065 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
3066 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
3067 treerenderer = gtk_cell_renderer_text_new();
3068 treecolumn = gtk_tree_view_column_new_with_attributes
3069 ("Label", treerenderer, "text", 0, NULL);
3070 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
3071 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
3072 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
3073 gtk_container_add(GTK_CONTAINER(treescroll), tree);
3075 tree = gtk_tree_new();
3076 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
3077 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
3078 g_signal_connect(G_OBJECT(tree), "focus",
3079 G_CALLBACK(tree_focus), &dp);
3081 g_signal_connect(G_OBJECT(tree), "focus_in_event",
3082 G_CALLBACK(widget_focus), &dp);
3083 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
3084 gtk_widget_show(treescroll);
3085 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
3086 panels = gtk_notebook_new();
3087 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
3088 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
3089 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
3090 gtk_widget_show(panels);
3095 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3096 struct controlset *s = ctrlbox->ctrlsets[index];
3099 if (!*s->pathname) {
3100 w = layout_ctrls(&dp, &scs, s, GTK_WINDOW(window));
3102 set_dialog_action_area(GTK_DIALOG(window), w);
3104 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
3105 if (j != INT_MAX) { /* add to treeview, start new panel */
3107 #if GTK_CHECK_VERSION(2,0,0)
3108 GtkTreeIter treeiter;
3110 GtkWidget *treeitem;
3115 * We expect never to find an implicit path
3116 * component. For example, we expect never to see
3117 * A/B/C followed by A/D/E, because that would
3118 * _implicitly_ create A/D. All our path prefixes
3119 * are expected to contain actual controls and be
3120 * selectable in the treeview; so we would expect
3121 * to see A/D _explicitly_ before encountering
3124 assert(j == ctrl_path_elements(s->pathname) - 1);
3126 c = strrchr(s->pathname, '/');
3134 first = (panelvbox == NULL);
3136 panelvbox = gtk_vbox_new(FALSE, 4);
3137 gtk_widget_show(panelvbox);
3138 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
3143 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
3145 gtk_notebook_set_current_page(GTK_NOTEBOOK(panels),
3149 if (nselparams >= selparamsize) {
3151 selparams = sresize(selparams, selparamsize,
3154 selparams[nselparams].dp = &dp;
3155 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
3156 selparams[nselparams].panel = panelvbox;
3157 selparams[nselparams].shortcuts = scs; /* structure copy */
3159 assert(j-1 < level);
3161 #if GTK_CHECK_VERSION(2,0,0)
3163 /* treeiterlevels[j-1] will always be valid because we
3164 * don't allow implicit path components; see above.
3166 gtk_tree_store_append(treestore, &treeiter,
3167 &treeiterlevels[j-1]);
3169 gtk_tree_store_append(treestore, &treeiter, NULL);
3170 gtk_tree_store_set(treestore, &treeiter,
3172 TREESTORE_PARAMS, nselparams,
3174 treeiterlevels[j] = treeiter;
3176 selparams[nselparams].depth = j;
3178 selparams[nselparams].treepath =
3179 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
3180 &treeiterlevels[j-1]);
3182 * We are going to collapse all tree branches
3183 * at depth greater than 2, but not _yet_; see
3184 * the comment at the call to
3185 * gtk_tree_view_collapse_row below.
3187 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree),
3188 selparams[nselparams].treepath,
3191 selparams[nselparams].treepath = NULL;
3194 treeitem = gtk_tree_item_new_with_label(c);
3196 if (!treelevels[j-1]) {
3197 treelevels[j-1] = GTK_TREE(gtk_tree_new());
3198 gtk_tree_item_set_subtree
3199 (treeitemlevels[j-1],
3200 GTK_WIDGET(treelevels[j-1]));
3202 gtk_tree_item_expand(treeitemlevels[j-1]);
3204 gtk_tree_item_collapse(treeitemlevels[j-1]);
3206 gtk_tree_append(treelevels[j-1], treeitem);
3208 gtk_tree_append(GTK_TREE(tree), treeitem);
3210 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
3211 treelevels[j] = NULL;
3213 g_signal_connect(G_OBJECT(treeitem), "key_press_event",
3214 G_CALLBACK(tree_key_press), &dp);
3215 g_signal_connect(G_OBJECT(treeitem), "focus_in_event",
3216 G_CALLBACK(widget_focus), &dp);
3218 gtk_widget_show(treeitem);
3221 gtk_tree_select_child(GTK_TREE(tree), treeitem);
3222 selparams[nselparams].treeitem = treeitem;
3229 w = layout_ctrls(&dp, &selparams[nselparams-1].shortcuts, s, NULL);
3230 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
3235 #if GTK_CHECK_VERSION(2,0,0)
3237 * We want our tree view to come up with all branches at depth 2
3238 * or more collapsed. However, if we start off with those branches
3239 * collapsed, then the tree view's size request will be calculated
3240 * based on the width of the collapsed tree, and then when the
3241 * collapsed branches are expanded later, the tree view will
3242 * jarringly change size.
3244 * So instead we start with everything expanded; then, once the
3245 * tree view has computed its resulting width requirement, we
3246 * collapse the relevant rows, but force the width to be the value
3247 * we just retrieved. This arranges that the tree view is wide
3248 * enough to have all branches expanded without further resizing.
3251 dp.nselparams = nselparams;
3252 dp.selparams = selparams;
3254 #if !GTK_CHECK_VERSION(3,0,0)
3257 * In GTK2, we can just do the job right now.
3260 gtk_widget_size_request(tree, &req);
3261 initial_treeview_collapse(&dp, tree);
3262 gtk_widget_set_size_request(tree, req.width, -1);
3266 * But in GTK3, we have to wait until the widget is about to be
3267 * mapped, because the size computation won't have been done yet.
3269 g_signal_connect(G_OBJECT(tree), "map",
3270 G_CALLBACK(treeview_map_event), &dp);
3271 #endif /* GTK 2 vs 3 */
3272 #endif /* GTK 2+ vs 1 */
3274 #if GTK_CHECK_VERSION(2,0,0)
3275 g_signal_connect(G_OBJECT(treeselection), "changed",
3276 G_CALLBACK(treeselection_changed), selparams);
3278 dp.ntreeitems = nselparams;
3279 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
3280 for (index = 0; index < nselparams; index++) {
3281 g_signal_connect(G_OBJECT(selparams[index].treeitem), "select",
3282 G_CALLBACK(treeitem_sel),
3284 dp.treeitems[index] = selparams[index].treeitem;
3289 dlg_refresh(NULL, &dp);
3291 dp.shortcuts = &selparams[0].shortcuts;
3292 #if !GTK_CHECK_VERSION(2,0,0)
3293 dp.currtreeitem = dp.treeitems[0];
3295 dp.lastfocus = NULL;
3301 extern void set_window_icon(GtkWidget *window,
3302 const char *const *const *icon,
3304 extern const char *const *const cfg_icon[];
3305 extern const int n_cfg_icon;
3306 set_window_icon(window, cfg_icon, n_cfg_icon);
3309 #if !GTK_CHECK_VERSION(2,0,0)
3310 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
3313 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
3315 GTK_POLICY_AUTOMATIC);
3316 gtk_widget_show(tree);
3318 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3319 gtk_widget_show(window);
3322 * Set focus into the first available control.
3324 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3325 struct controlset *s = ctrlbox->ctrlsets[index];
3330 for (j = 0; j < s->ncontrols; j++)
3331 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
3332 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
3333 s->ctrls[j]->generic.type != CTRL_TEXT) {
3334 dlg_set_focus(s->ctrls[j], &dp);
3335 dp.lastfocus = s->ctrls[j];
3344 g_signal_connect(G_OBJECT(window), "destroy",
3345 G_CALLBACK(window_destroy), NULL);
3346 g_signal_connect(G_OBJECT(window), "key_press_event",
3347 G_CALLBACK(win_key_press), &dp);
3358 static void messagebox_handler(union control *ctrl, void *dlg,
3359 void *data, int event)
3361 if (event == EVENT_ACTION)
3362 dlg_end(dlg, ctrl->generic.context.i);
3364 int messagebox(GtkWidget *parentwin, const char *title, const char *msg,
3367 GtkWidget *window, *w0, *w1;
3368 struct controlbox *ctrlbox;
3369 struct controlset *s0, *s1;
3372 struct Shortcuts scs;
3378 for (index = 0; index < lenof(scs.sc); index++) {
3379 scs.sc[index].action = SHORTCUT_EMPTY;
3382 ctrlbox = ctrl_new_box();
3385 va_start(ap, minwid);
3386 while (va_arg(ap, char *) != NULL) {
3388 (void) va_arg(ap, int); /* shortcut */
3389 (void) va_arg(ap, int); /* normal/default/cancel */
3390 (void) va_arg(ap, int); /* end value */
3394 s0 = ctrl_getset(ctrlbox, "", "", "");
3395 c = ctrl_columns(s0, 2, 50, 50);
3396 c->columns.ncols = s0->ncolumns = ncols;
3397 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
3398 for (index = 0; index < ncols; index++)
3399 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
3400 va_start(ap, minwid);
3403 char *title = va_arg(ap, char *);
3404 int shortcut, type, value;
3407 shortcut = va_arg(ap, int);
3408 type = va_arg(ap, int);
3409 value = va_arg(ap, int);
3410 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
3411 messagebox_handler, I(value));
3412 c->generic.column = index++;
3414 c->button.isdefault = TRUE;
3416 c->button.iscancel = TRUE;
3420 s1 = ctrl_getset(ctrlbox, "x", "", "");
3421 ctrl_text(s1, msg, HELPCTX(no_help));
3423 window = gtk_dialog_new();
3424 gtk_window_set_title(GTK_WINDOW(window), title);
3425 w0 = layout_ctrls(&dp, &scs, s0, GTK_WINDOW(window));
3426 set_dialog_action_area(GTK_DIALOG(window), w0);
3427 gtk_widget_show(w0);
3428 w1 = layout_ctrls(&dp, &scs, s1, GTK_WINDOW(window));
3429 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3430 gtk_widget_set_size_request(w1, minwid+20, -1);
3432 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3434 gtk_widget_show(w1);
3436 dp.shortcuts = &scs;
3437 dp.lastfocus = NULL;
3441 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3443 set_transient_window_pos(parentwin, window);
3444 gtk_window_set_transient_for(GTK_WINDOW(window),
3445 GTK_WINDOW(parentwin));
3447 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3448 gtk_widget_show(window);
3450 g_signal_connect(G_OBJECT(window), "destroy",
3451 G_CALLBACK(window_destroy), NULL);
3452 g_signal_connect(G_OBJECT(window), "key_press_event",
3453 G_CALLBACK(win_key_press), &dp);
3459 ctrl_free_box(ctrlbox);
3464 int string_width(const char *text)
3467 get_label_text_dimensions(text, &ret, NULL);
3471 int reallyclose(void *frontend)
3473 char *title = dupcat(appname, " Exit Confirmation", NULL);
3474 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
3475 title, "Are you sure you want to close this session?",
3476 string_width("Most of the width of the above text"),
3484 int verify_ssh_host_key(void *frontend, char *host, int port,
3485 const char *keytype, char *keystr, char *fingerprint,
3486 void (*callback)(void *ctx, int result), void *ctx)
3488 static const char absenttxt[] =
3489 "The server's host key is not cached. You have no guarantee "
3490 "that the server is the computer you think it is.\n"
3491 "The server's %s key fingerprint is:\n"
3493 "If you trust this host, press \"Accept\" to add the key to "
3494 "PuTTY's cache and carry on connecting.\n"
3495 "If you want to carry on connecting just once, without "
3496 "adding the key to the cache, press \"Connect Once\".\n"
3497 "If you do not trust this host, press \"Cancel\" to abandon the "
3499 static const char wrongtxt[] =
3500 "WARNING - POTENTIAL SECURITY BREACH!\n"
3501 "The server's host key does not match the one PuTTY has "
3502 "cached. This means that either the server administrator "
3503 "has changed the host key, or you have actually connected "
3504 "to another computer pretending to be the server.\n"
3505 "The new %s key fingerprint is:\n"
3507 "If you were expecting this change and trust the new key, "
3508 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3509 "If you want to carry on connecting but without updating "
3510 "the cache, press \"Connect Once\".\n"
3511 "If you want to abandon the connection completely, press "
3512 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3520 ret = verify_host_key(host, port, keytype, keystr);
3522 if (ret == 0) /* success - key matched OK */
3525 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
3527 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3528 "PuTTY Security Alert", text,
3529 string_width(fingerprint),
3530 "Accept", 'a', 0, 2,
3531 "Connect Once", 'o', 0, 1,
3532 "Cancel", 'c', -1, 0,
3538 store_host_key(host, port, keytype, keystr);
3539 return 1; /* continue with connection */
3540 } else if (ret == 1)
3541 return 1; /* continue with connection */
3542 return 0; /* do not continue with connection */
3546 * Ask whether the selected algorithm is acceptable (since it was
3547 * below the configured 'warn' threshold).
3549 int askalg(void *frontend, const char *algtype, const char *algname,
3550 void (*callback)(void *ctx, int result), void *ctx)
3552 static const char msg[] =
3553 "The first %s supported by the server is "
3554 "%s, which is below the configured warning threshold.\n"
3555 "Continue with connection?";
3559 text = dupprintf(msg, algtype, algname);
3560 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3561 "PuTTY Security Alert", text,
3562 string_width("Continue with connection?"),
3575 void old_keyfile_warning(void)
3578 * This should never happen on Unix. We hope.
3582 void fatal_message_box(void *window, const char *msg)
3584 messagebox(window, "PuTTY Fatal Error", msg,
3585 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3586 "OK", 'o', 1, 1, NULL);
3589 void nonfatal_message_box(void *window, const char *msg)
3591 messagebox(window, "PuTTY Error", msg,
3592 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3593 "OK", 'o', 1, 1, NULL);
3596 void fatalbox(const char *p, ...)
3601 msg = dupvprintf(p, ap);
3603 fatal_message_box(NULL, msg);
3608 void nonfatal(const char *p, ...)
3613 msg = dupvprintf(p, ap);
3615 nonfatal_message_box(NULL, msg);
3619 static GtkWidget *aboutbox = NULL;
3621 static void about_close_clicked(GtkButton *button, gpointer data)
3623 gtk_widget_destroy(aboutbox);
3627 static void licence_clicked(GtkButton *button, gpointer data)
3631 const char *licence =
3632 "Copyright 1997-2015 Simon Tatham.\n\n"
3634 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3635 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3636 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3637 "Markus Kuhn, Colin Watson, Christopher Staite, and CORE SDI S.A.\n\n"
3639 "Permission is hereby granted, free of charge, to any person "
3640 "obtaining a copy of this software and associated documentation "
3641 "files (the ""Software""), to deal in the Software without restriction, "
3642 "including without limitation the rights to use, copy, modify, merge, "
3643 "publish, distribute, sublicense, and/or sell copies of the Software, "
3644 "and to permit persons to whom the Software is furnished to do so, "
3645 "subject to the following conditions:\n\n"
3647 "The above copyright notice and this permission notice shall be "
3648 "included in all copies or substantial portions of the Software.\n\n"
3650 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3651 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3652 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3653 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3654 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3655 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3656 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3657 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3658 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3659 "OTHER DEALINGS IN THE SOFTWARE.";
3661 title = dupcat(appname, " Licence", NULL);
3662 assert(aboutbox != NULL);
3663 messagebox(aboutbox, title, licence,
3664 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3665 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3666 "OK", 'o', 1, 1, NULL);
3670 void about_box(void *window)
3676 gtk_widget_grab_focus(aboutbox);
3680 aboutbox = gtk_dialog_new();
3681 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
3682 title = dupcat("About ", appname, NULL);
3683 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
3686 w = gtk_button_new_with_label("Close");
3687 gtk_widget_set_can_default(w, TRUE);
3688 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
3689 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3690 w, FALSE, FALSE, 0);
3691 g_signal_connect(G_OBJECT(w), "clicked",
3692 G_CALLBACK(about_close_clicked), NULL);
3695 w = gtk_button_new_with_label("View Licence");
3696 gtk_widget_set_can_default(w, TRUE);
3697 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3698 w, FALSE, FALSE, 0);
3699 g_signal_connect(G_OBJECT(w), "clicked",
3700 G_CALLBACK(licence_clicked), NULL);
3703 w = gtk_label_new(appname);
3705 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3706 w, FALSE, FALSE, 0);
3709 w = gtk_label_new(ver);
3711 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3712 w, FALSE, FALSE, 5);
3715 w = gtk_label_new("Copyright 1997-2015 Simon Tatham. All rights reserved");
3717 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3718 w, FALSE, FALSE, 5);
3721 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
3722 gtk_window_set_transient_for(GTK_WINDOW(aboutbox),
3723 GTK_WINDOW(window));
3724 gtk_widget_show(aboutbox);
3727 struct eventlog_stuff {
3728 GtkWidget *parentwin, *window;
3729 struct controlbox *eventbox;
3730 struct Shortcuts scs;
3732 union control *listctrl;
3734 int nevents, negsize;
3737 int ignore_selchange;
3740 static void eventlog_destroy(GtkWidget *widget, gpointer data)
3742 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3747 dlg_cleanup(&es->dp);
3748 ctrl_free_box(es->eventbox);
3750 static void eventlog_ok_handler(union control *ctrl, void *dlg,
3751 void *data, int event)
3753 if (event == EVENT_ACTION)
3756 static void eventlog_list_handler(union control *ctrl, void *dlg,
3757 void *data, int event)
3759 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3761 if (event == EVENT_REFRESH) {
3764 dlg_update_start(ctrl, dlg);
3765 dlg_listbox_clear(ctrl, dlg);
3766 for (i = 0; i < es->nevents; i++) {
3767 dlg_listbox_add(ctrl, dlg, es->events[i]);
3769 dlg_update_done(ctrl, dlg);
3770 } else if (event == EVENT_SELCHANGE) {
3775 * If this SELCHANGE event is happening as a result of
3776 * deliberate deselection because someone else has grabbed
3777 * the selection, the last thing we want to do is pre-empt
3780 if (es->ignore_selchange)
3784 * Construct the data to use as the selection.
3789 for (i = 0; i < es->nevents; i++) {
3790 if (dlg_listbox_issel(ctrl, dlg, i)) {
3791 int extralen = strlen(es->events[i]);
3793 if (es->sellen + extralen + 2 > selsize) {
3794 selsize = es->sellen + extralen + 512;
3795 es->seldata = sresize(es->seldata, selsize, char);
3798 strcpy(es->seldata + es->sellen, es->events[i]);
3799 es->sellen += extralen;
3800 es->seldata[es->sellen++] = '\n';
3804 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
3805 GDK_CURRENT_TIME)) {
3806 extern GdkAtom compound_text_atom;
3808 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3809 GDK_SELECTION_TYPE_STRING, 1);
3810 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3811 compound_text_atom, 1);
3817 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
3818 guint info, guint time_stamp, gpointer data)
3820 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3822 gtk_selection_data_set(seldata, gtk_selection_data_get_target(seldata), 8,
3823 (unsigned char *)es->seldata, es->sellen);
3826 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
3829 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3833 * Deselect everything in the list box.
3835 uc = dlg_find_byctrl(&es->dp, es->listctrl);
3836 es->ignore_selchange = 1;
3837 #if !GTK_CHECK_VERSION(2,0,0)
3839 gtk_list_unselect_all(GTK_LIST(uc->list));
3841 assert(uc->treeview);
3842 gtk_tree_selection_unselect_all
3843 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview)));
3845 es->ignore_selchange = 0;
3853 void showeventlog(void *estuff, void *parentwin)
3855 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3856 GtkWidget *window, *w0, *w1;
3857 GtkWidget *parent = GTK_WIDGET(parentwin);
3858 struct controlset *s0, *s1;
3864 gtk_widget_grab_focus(es->window);
3870 for (index = 0; index < lenof(es->scs.sc); index++) {
3871 es->scs.sc[index].action = SHORTCUT_EMPTY;
3874 es->eventbox = ctrl_new_box();
3876 s0 = ctrl_getset(es->eventbox, "", "", "");
3877 ctrl_columns(s0, 3, 33, 34, 33);
3878 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
3879 eventlog_ok_handler, P(NULL));
3880 c->button.column = 1;
3881 c->button.isdefault = TRUE;
3883 s1 = ctrl_getset(es->eventbox, "x", "", "");
3884 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3885 eventlog_list_handler, P(es));
3886 c->listbox.height = 10;
3887 c->listbox.multisel = 2;
3888 c->listbox.ncols = 3;
3889 c->listbox.percentages = snewn(3, int);
3890 c->listbox.percentages[0] = 25;
3891 c->listbox.percentages[1] = 10;
3892 c->listbox.percentages[2] = 65;
3894 es->window = window = gtk_dialog_new();
3895 title = dupcat(appname, " Event Log", NULL);
3896 gtk_window_set_title(GTK_WINDOW(window), title);
3898 w0 = layout_ctrls(&es->dp, &es->scs, s0, GTK_WINDOW(window));
3899 set_dialog_action_area(GTK_DIALOG(window), w0);
3900 gtk_widget_show(w0);
3901 w1 = layout_ctrls(&es->dp, &es->scs, s1, GTK_WINDOW(window));
3902 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3903 gtk_widget_set_size_request(w1, 20 + string_width
3904 ("LINE OF TEXT GIVING WIDTH OF EVENT LOG IS "
3905 "QUITE LONG 'COS SSH LOG ENTRIES ARE WIDE"),
3908 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3910 gtk_widget_show(w1);
3913 es->dp.shortcuts = &es->scs;
3914 es->dp.lastfocus = NULL;
3916 es->dp.window = window;
3918 dlg_refresh(NULL, &es->dp);
3921 set_transient_window_pos(parent, window);
3922 gtk_window_set_transient_for(GTK_WINDOW(window),
3923 GTK_WINDOW(parent));
3925 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3926 gtk_widget_show(window);
3928 g_signal_connect(G_OBJECT(window), "destroy",
3929 G_CALLBACK(eventlog_destroy), es);
3930 g_signal_connect(G_OBJECT(window), "key_press_event",
3931 G_CALLBACK(win_key_press), &es->dp);
3932 g_signal_connect(G_OBJECT(window), "selection_get",
3933 G_CALLBACK(eventlog_selection_get), es);
3934 g_signal_connect(G_OBJECT(window), "selection_clear_event",
3935 G_CALLBACK(eventlog_selection_clear), es);
3938 void *eventlogstuff_new(void)
3940 struct eventlog_stuff *es;
3941 es = snew(struct eventlog_stuff);
3942 memset(es, 0, sizeof(*es));
3946 void logevent_dlg(void *estuff, const char *string)
3948 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3953 if (es->nevents >= es->negsize) {
3955 es->events = sresize(es->events, es->negsize, char *);
3959 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3961 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3962 strcpy(es->events[es->nevents], timebuf);
3963 strcat(es->events[es->nevents], string);
3965 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3970 int askappend(void *frontend, Filename *filename,
3971 void (*callback)(void *ctx, int result), void *ctx)
3973 static const char msgtemplate[] =
3974 "The session log file \"%.*s\" already exists. "
3975 "You can overwrite it with a new session log, "
3976 "append your session log to the end of it, "
3977 "or disable session logging for this session.";
3982 message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
3983 mbtitle = dupprintf("%s Log to File", appname);
3985 mbret = messagebox(get_window(frontend), mbtitle, message,
3986 string_width("LINE OF TEXT SUITABLE FOR THE"
3987 " ASKAPPEND WIDTH"),
3988 "Overwrite", 'o', 1, 2,
3989 "Append", 'a', 0, 1,
3990 "Disable", 'd', -1, 0,