2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
10 #if !GTK_CHECK_VERSION(3,0,0)
11 #include <gdk/gdkkeysyms.h>
16 #include <X11/Xutil.h>
19 #include "gtkcompat.h"
25 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
40 struct Shortcut sc[128];
46 GtkWidget **buttons; int nbuttons; /* for radio buttons */
47 GtkWidget *entry; /* for editbox, filesel, fontsel */
48 GtkWidget *button; /* for filesel, fontsel */
49 #if !GTK_CHECK_VERSION(2,4,0)
50 GtkWidget *list; /* for listbox (in GTK1), combobox (<=GTK2.3) */
51 GtkWidget *menu; /* for optionmenu (==droplist) */
52 GtkWidget *optmenu; /* also for optionmenu */
54 GtkWidget *combo; /* for combo box (either editable or not) */
56 #if GTK_CHECK_VERSION(2,0,0)
57 GtkWidget *treeview; /* for listbox (GTK2), droplist+combo (>=2.4) */
58 GtkListStore *listmodel; /* for all types of list box */
60 GtkWidget *text; /* for text */
61 GtkWidget *label; /* for dlg_label_change */
62 GtkAdjustment *adj; /* for the scrollbar in a list box */
69 tree234 *byctrl, *bywidget;
71 struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
72 /* `flags' are set to indicate when a GTK signal handler is being called
73 * due to automatic processing and should not flag a user event. */
75 struct Shortcuts *shortcuts;
76 GtkWidget *window, *cancelbutton;
77 union control *currfocus, *lastfocus;
78 #if !GTK_CHECK_VERSION(2,0,0)
79 GtkWidget *currtreeitem, **treeitems;
84 #define FLAG_UPDATING_COMBO_LIST 1
85 #define FLAG_UPDATING_LISTBOX 2
87 enum { /* values for Shortcut.action */
88 SHORTCUT_EMPTY, /* no shortcut on this key */
89 SHORTCUT_TREE, /* focus a tree item */
90 SHORTCUT_FOCUS, /* focus the supplied widget */
91 SHORTCUT_UCTRL, /* do something sane with uctrl */
92 SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */
93 SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */
96 #if GTK_CHECK_VERSION(2,0,0)
105 * Forward references.
107 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
109 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
110 int chr, int action, void *ptr);
111 static void shortcut_highlight(GtkWidget *label, int chr);
112 #if !GTK_CHECK_VERSION(2,0,0)
113 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
115 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
117 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
119 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
122 #if !GTK_CHECK_VERSION(2,4,0)
123 static void menuitem_activate(GtkMenuItem *item, gpointer data);
125 static void coloursel_ok(GtkButton *button, gpointer data);
126 static void coloursel_cancel(GtkButton *button, gpointer data);
127 static void window_destroy(GtkWidget *widget, gpointer data);
128 int get_listitemheight(GtkWidget *widget);
130 static int uctrl_cmp_byctrl(void *av, void *bv)
132 struct uctrl *a = (struct uctrl *)av;
133 struct uctrl *b = (struct uctrl *)bv;
134 if (a->ctrl < b->ctrl)
136 else if (a->ctrl > b->ctrl)
141 static int uctrl_cmp_byctrl_find(void *av, void *bv)
143 union control *a = (union control *)av;
144 struct uctrl *b = (struct uctrl *)bv;
147 else if (a > b->ctrl)
152 static int uctrl_cmp_bywidget(void *av, void *bv)
154 struct uctrl *a = (struct uctrl *)av;
155 struct uctrl *b = (struct uctrl *)bv;
156 if (a->toplevel < b->toplevel)
158 else if (a->toplevel > b->toplevel)
163 static int uctrl_cmp_bywidget_find(void *av, void *bv)
165 GtkWidget *a = (GtkWidget *)av;
166 struct uctrl *b = (struct uctrl *)bv;
169 else if (a > b->toplevel)
174 static void dlg_init(struct dlgparam *dp)
176 dp->byctrl = newtree234(uctrl_cmp_byctrl);
177 dp->bywidget = newtree234(uctrl_cmp_bywidget);
178 dp->coloursel_result.ok = FALSE;
179 dp->window = dp->cancelbutton = NULL;
180 #if !GTK_CHECK_VERSION(2,0,0)
181 dp->treeitems = NULL;
182 dp->currtreeitem = NULL;
185 dp->currfocus = NULL;
188 static void dlg_cleanup(struct dlgparam *dp)
192 freetree234(dp->byctrl); /* doesn't free the uctrls inside */
194 while ( (uc = index234(dp->bywidget, 0)) != NULL) {
195 del234(dp->bywidget, uc);
199 freetree234(dp->bywidget);
201 #if !GTK_CHECK_VERSION(2,0,0)
202 sfree(dp->treeitems);
206 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
208 add234(dp->byctrl, uc);
209 add234(dp->bywidget, uc);
212 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
216 return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
219 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
221 struct uctrl *ret = NULL;
225 ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
228 w = gtk_widget_get_parent(w);
233 union control *dlg_last_focused(union control *ctrl, void *dlg)
235 struct dlgparam *dp = (struct dlgparam *)dlg;
236 if (dp->currfocus != ctrl)
237 return dp->currfocus;
239 return dp->lastfocus;
242 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
244 struct dlgparam *dp = (struct dlgparam *)dlg;
245 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
246 assert(uc->ctrl->generic.type == CTRL_RADIO);
247 assert(uc->buttons != NULL);
248 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
251 int dlg_radiobutton_get(union control *ctrl, void *dlg)
253 struct dlgparam *dp = (struct dlgparam *)dlg;
254 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
257 assert(uc->ctrl->generic.type == CTRL_RADIO);
258 assert(uc->buttons != NULL);
259 for (i = 0; i < uc->nbuttons; i++)
260 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
262 return 0; /* got to return something */
265 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
267 struct dlgparam *dp = (struct dlgparam *)dlg;
268 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
269 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
270 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
273 int dlg_checkbox_get(union control *ctrl, void *dlg)
275 struct dlgparam *dp = (struct dlgparam *)dlg;
276 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
277 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
278 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
281 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
283 struct dlgparam *dp = (struct dlgparam *)dlg;
284 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
287 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
289 #if GTK_CHECK_VERSION(2,4,0)
291 entry = gtk_bin_get_child(GTK_BIN(uc->combo));
296 assert(entry != NULL);
299 * GTK 2 implements gtk_entry_set_text by means of two separate
300 * operations: first delete the previous text leaving the empty
301 * string, then insert the new text. This causes two calls to
302 * the "changed" signal.
304 * The first call to "changed", if allowed to proceed normally,
305 * will cause an EVENT_VALCHANGE event on the edit box, causing
306 * a call to dlg_editbox_get() which will read the empty string
307 * out of the GtkEntry - and promptly write it straight into the
308 * Conf structure, which is precisely where our `text' pointer
309 * is probably pointing, so the second editing operation will
310 * insert that instead of the string we originally asked for.
312 * Hence, we must take our own copy of the text before we do
315 tmpstring = dupstr(text);
316 gtk_entry_set_text(GTK_ENTRY(entry), tmpstring);
320 char *dlg_editbox_get(union control *ctrl, void *dlg)
322 struct dlgparam *dp = (struct dlgparam *)dlg;
323 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
324 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
326 #if GTK_CHECK_VERSION(2,4,0)
328 #if GTK_CHECK_VERSION(2,6,0)
329 return dupstr(gtk_combo_box_get_active_text(GTK_COMBO_BOX(uc->combo)));
331 return dupstr(gtk_entry_get_text
332 (GTK_ENTRY(gtk_bin_get_child(GTK_BIN(uc->combo)))));
338 return dupstr(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
341 assert(!"We shouldn't get here");
344 #if !GTK_CHECK_VERSION(2,4,0)
345 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
347 GtkContainer *cont = GTK_CONTAINER(data);
348 /* gtk_container_remove will unref the widget for us; we need not. */
349 gtk_container_remove(cont, w);
353 /* The `listbox' functions can also apply to combo boxes. */
354 void dlg_listbox_clear(union control *ctrl, void *dlg)
356 struct dlgparam *dp = (struct dlgparam *)dlg;
357 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
359 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
360 uc->ctrl->generic.type == CTRL_LISTBOX);
362 #if !GTK_CHECK_VERSION(2,4,0)
364 gtk_container_foreach(GTK_CONTAINER(uc->menu),
365 container_remove_and_destroy,
366 GTK_CONTAINER(uc->menu));
370 gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
374 #if GTK_CHECK_VERSION(2,0,0)
376 gtk_list_store_clear(uc->listmodel);
380 assert(!"We shouldn't get here");
383 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
385 struct dlgparam *dp = (struct dlgparam *)dlg;
386 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
388 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
389 uc->ctrl->generic.type == CTRL_LISTBOX);
391 #if !GTK_CHECK_VERSION(2,4,0)
394 (GTK_CONTAINER(uc->menu),
395 g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
399 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
403 #if GTK_CHECK_VERSION(2,0,0)
407 assert(uc->listmodel != NULL);
408 path = gtk_tree_path_new_from_indices(index, -1);
409 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
410 gtk_list_store_remove(uc->listmodel, &iter);
411 gtk_tree_path_free(path);
415 assert(!"We shouldn't get here");
418 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
420 dlg_listbox_addwithid(ctrl, dlg, text, 0);
424 * Each listbox entry may have a numeric id associated with it.
425 * Note that some front ends only permit a string to be stored at
426 * each position, which means that _if_ you put two identical
427 * strings in any listbox then you MUST not assign them different
428 * IDs and expect to get meaningful results back.
430 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
431 char const *text, int id)
433 struct dlgparam *dp = (struct dlgparam *)dlg;
434 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
436 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
437 uc->ctrl->generic.type == CTRL_LISTBOX);
440 * This routine is long and complicated in both GTK 1 and 2,
441 * and completely different. Sigh.
443 dp->flags |= FLAG_UPDATING_COMBO_LIST;
445 #if !GTK_CHECK_VERSION(2,4,0)
448 * List item in a drop-down (but non-combo) list. Tabs are
449 * ignored; we just provide a standard menu item with the
452 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
454 gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
455 gtk_widget_show(menuitem);
457 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
458 GINT_TO_POINTER(id));
459 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
460 GTK_SIGNAL_FUNC(menuitem_activate), dp);
463 if (uc->list && uc->entry) {
465 * List item in a combo-box list, which means the sensible
466 * thing to do is make it a perfectly normal label. Hence
467 * tabs are disregarded.
469 GtkWidget *listitem = gtk_list_item_new_with_label(text);
471 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
472 gtk_widget_show(listitem);
474 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
475 GINT_TO_POINTER(id));
479 #if !GTK_CHECK_VERSION(2,0,0)
482 * List item in a non-combo-box list box. We make all of
483 * these Columns containing GtkLabels. This allows us to do
484 * the nasty force_left hack irrespective of whether there
485 * are tabs in the thing.
487 GtkWidget *listitem = gtk_list_item_new();
488 GtkWidget *cols = columns_new(10);
492 /* Count the tabs in the text, and hence determine # of columns. */
494 for (i = 0; text[i]; i++)
499 (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
500 percents = snewn(ncols, gint);
501 percents[ncols-1] = 100;
502 for (i = 0; i < ncols-1; i++) {
503 percents[i] = uc->ctrl->listbox.percentages[i];
504 percents[ncols-1] -= percents[i];
506 columns_set_cols(COLUMNS(cols), ncols, percents);
509 for (i = 0; i < ncols; i++) {
510 int len = strcspn(text, "\t");
511 char *dup = dupprintf("%.*s", len, text);
516 label = gtk_label_new(dup);
519 columns_add(COLUMNS(cols), label, i, 1);
520 columns_force_left_align(COLUMNS(cols), label);
521 gtk_widget_show(label);
523 gtk_container_add(GTK_CONTAINER(listitem), cols);
524 gtk_widget_show(cols);
525 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
526 gtk_widget_show(listitem);
528 if (ctrl->listbox.multisel) {
529 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
530 GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj);
532 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
533 GTK_SIGNAL_FUNC(listitem_single_key), uc->adj);
535 gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event",
536 GTK_SIGNAL_FUNC(widget_focus), dp);
537 gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event",
538 GTK_SIGNAL_FUNC(listitem_button_press), dp);
539 gtk_signal_connect(GTK_OBJECT(listitem), "button_release_event",
540 GTK_SIGNAL_FUNC(listitem_button_release), dp);
541 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
542 GINT_TO_POINTER(id));
550 dp->flags |= FLAG_UPDATING_LISTBOX;/* inhibit drag-list update */
551 gtk_list_store_append(uc->listmodel, &iter);
552 dp->flags &= ~FLAG_UPDATING_LISTBOX;
553 gtk_list_store_set(uc->listmodel, &iter, 0, id, -1);
556 * Now go through text and divide it into columns at the tabs,
559 cols = (uc->ctrl->generic.type == CTRL_LISTBOX ? ctrl->listbox.ncols : 1);
560 cols = cols ? cols : 1;
561 for (i = 0; i < cols; i++) {
562 int collen = strcspn(text, "\t");
563 char *tmpstr = snewn(collen+1, char);
564 memcpy(tmpstr, text, collen);
565 tmpstr[collen] = '\0';
566 gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1);
574 assert(!"We shouldn't get here");
576 dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
579 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
581 struct dlgparam *dp = (struct dlgparam *)dlg;
582 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
584 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
585 uc->ctrl->generic.type == CTRL_LISTBOX);
587 #if !GTK_CHECK_VERSION(2,4,0)
588 if (uc->menu || uc->list) {
592 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
594 item = GTK_OBJECT(g_list_nth_data(children, index));
595 g_list_free(children);
597 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
601 #if GTK_CHECK_VERSION(2,0,0)
607 path = gtk_tree_path_new_from_indices(index, -1);
608 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
609 gtk_tree_model_get(GTK_TREE_MODEL(uc->listmodel), &iter, 0, &ret, -1);
610 gtk_tree_path_free(path);
615 assert(!"We shouldn't get here");
616 return -1; /* placate dataflow analysis */
619 /* dlg_listbox_index returns <0 if no single element is selected. */
620 int dlg_listbox_index(union control *ctrl, void *dlg)
622 struct dlgparam *dp = (struct dlgparam *)dlg;
623 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
625 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
626 uc->ctrl->generic.type == CTRL_LISTBOX);
628 #if !GTK_CHECK_VERSION(2,4,0)
629 if (uc->menu || uc->list) {
631 GtkWidget *item, *activeitem;
636 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
638 activeitem = NULL; /* unnecessarily placate gcc */
640 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
642 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
643 i++, children = children->next) {
644 if (uc->menu ? activeitem == item :
645 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
652 g_list_free(children);
653 return selected < 0 ? -1 : selected;
658 * This API function already does the right thing in the
659 * case of no current selection.
661 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo));
664 #if GTK_CHECK_VERSION(2,0,0)
666 GtkTreeSelection *treesel;
673 assert(uc->treeview != NULL);
674 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
676 if (gtk_tree_selection_count_selected_rows(treesel) != 1)
679 sellist = gtk_tree_selection_get_selected_rows(treesel, &model);
681 assert(sellist && sellist->data);
682 path = sellist->data;
684 if (gtk_tree_path_get_depth(path) != 1) {
687 indices = gtk_tree_path_get_indices(path);
695 g_list_foreach(sellist, (GFunc)gtk_tree_path_free, NULL);
696 g_list_free(sellist);
701 assert(!"We shouldn't get here");
702 return -1; /* placate dataflow analysis */
705 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
707 struct dlgparam *dp = (struct dlgparam *)dlg;
708 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
710 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
711 uc->ctrl->generic.type == CTRL_LISTBOX);
713 #if !GTK_CHECK_VERSION(2,4,0)
714 if (uc->menu || uc->list) {
716 GtkWidget *item, *activeitem;
718 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
719 uc->ctrl->generic.type == CTRL_LISTBOX);
720 assert(uc->menu != NULL || uc->list != NULL);
722 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
724 item = GTK_WIDGET(g_list_nth_data(children, index));
725 g_list_free(children);
728 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
729 return item == activeitem;
731 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
737 * This API function already does the right thing in the
738 * case of no current selection.
740 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo)) == index;
743 #if GTK_CHECK_VERSION(2,0,0)
745 GtkTreeSelection *treesel;
749 assert(uc->treeview != NULL);
750 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
752 path = gtk_tree_path_new_from_indices(index, -1);
753 ret = gtk_tree_selection_path_is_selected(treesel, path);
754 gtk_tree_path_free(path);
759 assert(!"We shouldn't get here");
760 return -1; /* placate dataflow analysis */
763 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
765 struct dlgparam *dp = (struct dlgparam *)dlg;
766 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
768 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
769 uc->ctrl->generic.type == CTRL_LISTBOX);
771 #if !GTK_CHECK_VERSION(2,4,0)
773 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
779 gdouble newtop, newbot;
781 gtk_list_select_item(GTK_LIST(uc->list), index);
784 * Scroll the list box if necessary to ensure the newly
785 * selected item is visible.
787 items = gtk_container_children(GTK_CONTAINER(uc->list));
788 nitems = g_list_length(items);
790 int modified = FALSE;
792 newtop = uc->adj->lower +
793 (uc->adj->upper - uc->adj->lower) * index / nitems;
794 newbot = uc->adj->lower +
795 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
796 if (uc->adj->value > newtop) {
798 uc->adj->value = newtop;
799 } else if (uc->adj->value < newbot - uc->adj->page_size) {
801 uc->adj->value = newbot - uc->adj->page_size;
804 gtk_adjustment_value_changed(uc->adj);
810 gtk_combo_box_set_active(GTK_COMBO_BOX(uc->combo), index);
814 #if GTK_CHECK_VERSION(2,0,0)
816 GtkTreeSelection *treesel;
819 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
821 path = gtk_tree_path_new_from_indices(index, -1);
822 gtk_tree_selection_select_path(treesel, path);
823 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc->treeview),
824 path, NULL, FALSE, 0.0, 0.0);
825 gtk_tree_path_free(path);
829 assert(!"We shouldn't get here");
832 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
834 struct dlgparam *dp = (struct dlgparam *)dlg;
835 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
837 assert(uc->ctrl->generic.type == CTRL_TEXT);
838 assert(uc->text != NULL);
840 gtk_label_set_text(GTK_LABEL(uc->text), text);
843 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
845 struct dlgparam *dp = (struct dlgparam *)dlg;
846 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
848 switch (uc->ctrl->generic.type) {
850 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
851 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
854 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
855 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
858 gtk_label_set_text(GTK_LABEL(uc->label), text);
859 shortcut_highlight(uc->label, ctrl->radio.shortcut);
862 gtk_label_set_text(GTK_LABEL(uc->label), text);
863 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
865 case CTRL_FILESELECT:
866 gtk_label_set_text(GTK_LABEL(uc->label), text);
867 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
869 case CTRL_FONTSELECT:
870 gtk_label_set_text(GTK_LABEL(uc->label), text);
871 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
874 gtk_label_set_text(GTK_LABEL(uc->label), text);
875 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
878 assert(!"This shouldn't happen");
883 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
885 struct dlgparam *dp = (struct dlgparam *)dlg;
886 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
887 /* We must copy fn->path before passing it to gtk_entry_set_text.
888 * See comment in dlg_editbox_set() for the reasons. */
889 char *duppath = dupstr(fn->path);
890 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
891 assert(uc->entry != NULL);
892 gtk_entry_set_text(GTK_ENTRY(uc->entry), duppath);
896 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
898 struct dlgparam *dp = (struct dlgparam *)dlg;
899 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
900 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
901 assert(uc->entry != NULL);
902 return filename_from_str(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
905 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
907 struct dlgparam *dp = (struct dlgparam *)dlg;
908 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
909 /* We must copy fs->name before passing it to gtk_entry_set_text.
910 * See comment in dlg_editbox_set() for the reasons. */
911 char *dupname = dupstr(fs->name);
912 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
913 assert(uc->entry != NULL);
914 gtk_entry_set_text(GTK_ENTRY(uc->entry), dupname);
918 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
920 struct dlgparam *dp = (struct dlgparam *)dlg;
921 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
922 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
923 assert(uc->entry != NULL);
924 return fontspec_new(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
928 * Bracketing a large set of updates in these two functions will
929 * cause the front end (if possible) to delay updating the screen
930 * until it's all complete, thus avoiding flicker.
932 void dlg_update_start(union control *ctrl, void *dlg)
935 * Apparently we can't do this at all in GTK. GtkCList supports
936 * freeze and thaw, but not GtkList. Bah.
940 void dlg_update_done(union control *ctrl, void *dlg)
943 * Apparently we can't do this at all in GTK. GtkCList supports
944 * freeze and thaw, but not GtkList. Bah.
948 void dlg_set_focus(union control *ctrl, void *dlg)
950 struct dlgparam *dp = (struct dlgparam *)dlg;
951 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
953 switch (ctrl->generic.type) {
956 /* Check boxes and buttons get the focus _and_ get toggled. */
957 gtk_widget_grab_focus(uc->toplevel);
959 case CTRL_FILESELECT:
960 case CTRL_FONTSELECT:
963 /* Anything containing an edit box gets that focused. */
964 gtk_widget_grab_focus(uc->entry);
966 #if GTK_CHECK_VERSION(2,4,0)
967 else if (uc->combo) {
968 /* Failing that, there'll be a combo box. */
969 gtk_widget_grab_focus(uc->combo);
975 * Radio buttons: we find the currently selected button and
980 for (i = 0; i < ctrl->radio.nbuttons; i++)
981 if (gtk_toggle_button_get_active
982 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
983 gtk_widget_grab_focus(uc->buttons[i]);
988 #if !GTK_CHECK_VERSION(2,4,0)
990 gtk_widget_grab_focus(uc->optmenu);
995 gtk_widget_grab_focus(uc->combo);
999 #if !GTK_CHECK_VERSION(2,0,0)
1002 * For GTK-1 style list boxes, we tell it to focus one
1003 * of its children, which appears to do the Right
1006 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
1011 gtk_widget_grab_focus(uc->treeview);
1015 assert(!"We shouldn't get here");
1021 * During event processing, you might well want to give an error
1022 * indication to the user. dlg_beep() is a quick and easy generic
1023 * error; dlg_error() puts up a message-box or equivalent.
1025 void dlg_beep(void *dlg)
1030 static void errmsg_button_clicked(GtkButton *button, gpointer data)
1032 gtk_widget_destroy(GTK_WIDGET(data));
1035 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
1037 gint x, y, w, h, dx, dy;
1039 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
1040 gtk_widget_size_request(GTK_WIDGET(child), &req);
1042 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(parent)), &x, &y);
1043 gdk_window_get_size(gtk_widget_get_window(GTK_WIDGET(parent)), &w, &h);
1046 * One corner of the transient will be offset inwards, by 1/4
1047 * of the parent window's size, from the corresponding corner
1048 * of the parent window. The corner will be chosen so as to
1049 * place the transient closer to the centre of the screen; this
1050 * should avoid transients going off the edge of the screen on
1053 if (x + w/2 < gdk_screen_width() / 2)
1054 dx = x + w/4; /* work from left edges */
1056 dx = x + 3*w/4 - req.width; /* work from right edges */
1057 if (y + h/2 < gdk_screen_height() / 2)
1058 dy = y + h/4; /* work from top edges */
1060 dy = y + 3*h/4 - req.height; /* work from bottom edges */
1061 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
1064 void dlg_error_msg(void *dlg, const char *msg)
1066 struct dlgparam *dp = (struct dlgparam *)dlg;
1067 GtkWidget *window, *hbox, *text, *ok;
1069 window = gtk_dialog_new();
1070 text = gtk_label_new(msg);
1071 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1072 hbox = gtk_hbox_new(FALSE, 0);
1073 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1074 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
1075 hbox, FALSE, FALSE, 20);
1076 gtk_widget_show(text);
1077 gtk_widget_show(hbox);
1078 gtk_window_set_title(GTK_WINDOW(window), "Error");
1079 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1080 ok = gtk_button_new_with_label("OK");
1081 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(window))),
1082 ok, FALSE, FALSE, 0);
1083 gtk_widget_show(ok);
1084 gtk_widget_set_can_default(ok, TRUE);
1085 gtk_window_set_default(GTK_WINDOW(window), ok);
1086 gtk_signal_connect(GTK_OBJECT(ok), "clicked",
1087 GTK_SIGNAL_FUNC(errmsg_button_clicked), window);
1088 gtk_signal_connect(GTK_OBJECT(window), "destroy",
1089 GTK_SIGNAL_FUNC(window_destroy), NULL);
1090 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1091 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
1092 set_transient_window_pos(dp->window, window);
1093 gtk_widget_show(window);
1098 * This function signals to the front end that the dialog's
1099 * processing is completed, and passes an integer value (typically
1100 * a success status).
1102 void dlg_end(void *dlg, int value)
1104 struct dlgparam *dp = (struct dlgparam *)dlg;
1106 gtk_widget_destroy(dp->window);
1109 void dlg_refresh(union control *ctrl, void *dlg)
1111 struct dlgparam *dp = (struct dlgparam *)dlg;
1115 if (ctrl->generic.handler != NULL)
1116 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1120 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
1121 assert(uc->ctrl != NULL);
1122 if (uc->ctrl->generic.handler != NULL)
1123 uc->ctrl->generic.handler(uc->ctrl, dp,
1124 dp->data, EVENT_REFRESH);
1129 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
1131 struct dlgparam *dp = (struct dlgparam *)dlg;
1132 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
1134 GtkWidget *okbutton, *cancelbutton;
1136 GtkWidget *coloursel =
1137 gtk_color_selection_dialog_new("Select a colour");
1138 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
1139 GtkColorSelection *cs = GTK_COLOR_SELECTION
1140 (gtk_color_selection_dialog_get_color_selection(ccs));
1142 dp->coloursel_result.ok = FALSE;
1144 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
1145 #if GTK_CHECK_VERSION(2,0,0)
1146 gtk_color_selection_set_has_opacity_control(cs, FALSE);
1148 gtk_color_selection_set_opacity(cs, FALSE);
1150 cvals[0] = r / 255.0;
1151 cvals[1] = g / 255.0;
1152 cvals[2] = b / 255.0;
1153 cvals[3] = 1.0; /* fully opaque! */
1154 gtk_color_selection_set_color(cs, cvals);
1156 #if GTK_CHECK_VERSION(2,0,0)
1157 g_object_get(G_OBJECT(ccs),
1158 "ok-button", &okbutton,
1159 "cancel-button", &cancelbutton,
1160 (const char *)NULL);
1162 okbutton = ccs->ok_button;
1163 cancelbutton = ccs->cancel_button;
1165 g_object_set_data(G_OBJECT(okbutton), "user-data",
1166 (gpointer)coloursel);
1167 g_object_set_data(G_OBJECT(cancelbutton), "user-data",
1168 (gpointer)coloursel);
1169 gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc);
1170 gtk_signal_connect(GTK_OBJECT(okbutton), "clicked",
1171 GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp);
1172 gtk_signal_connect(GTK_OBJECT(cancelbutton), "clicked",
1173 GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp);
1174 gtk_signal_connect_object(GTK_OBJECT(okbutton), "clicked",
1175 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1176 (gpointer)coloursel);
1177 gtk_signal_connect_object(GTK_OBJECT(cancelbutton), "clicked",
1178 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1179 (gpointer)coloursel);
1180 gtk_widget_show(coloursel);
1183 int dlg_coloursel_results(union control *ctrl, void *dlg,
1184 int *r, int *g, int *b)
1186 struct dlgparam *dp = (struct dlgparam *)dlg;
1187 if (dp->coloursel_result.ok) {
1188 *r = dp->coloursel_result.r;
1189 *g = dp->coloursel_result.g;
1190 *b = dp->coloursel_result.b;
1196 /* ----------------------------------------------------------------------
1197 * Signal handlers while the dialog box is active.
1200 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
1203 struct dlgparam *dp = (struct dlgparam *)data;
1204 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1205 union control *focus;
1212 if (focus != dp->currfocus) {
1213 dp->lastfocus = dp->currfocus;
1214 dp->currfocus = focus;
1220 static void button_clicked(GtkButton *button, gpointer data)
1222 struct dlgparam *dp = (struct dlgparam *)data;
1223 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1224 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1227 static void button_toggled(GtkToggleButton *tb, gpointer data)
1229 struct dlgparam *dp = (struct dlgparam *)data;
1230 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
1231 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1234 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
1238 * GtkEntry has a nasty habit of eating the Return key, which
1239 * is unhelpful since it doesn't actually _do_ anything with it
1240 * (it calls gtk_widget_activate, but our edit boxes never need
1241 * activating). So I catch Return before GtkEntry sees it, and
1242 * pass it straight on to the parent widget. Effect: hitting
1243 * Return in an edit box will now activate the default button
1244 * in the dialog just like it will everywhere else.
1246 GtkWidget *parent = gtk_widget_get_parent(widget);
1247 if (event->keyval == GDK_KEY_Return && parent != NULL) {
1248 gboolean return_val;
1249 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1250 gtk_signal_emit_by_name(GTK_OBJECT(parent), "key_press_event",
1251 event, &return_val);
1257 static void editbox_changed(GtkEditable *ed, gpointer data)
1259 struct dlgparam *dp = (struct dlgparam *)data;
1260 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1261 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1262 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1266 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1269 struct dlgparam *dp = (struct dlgparam *)data;
1270 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1271 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1275 #if !GTK_CHECK_VERSION(2,0,0)
1278 * GTK 1 list box event handlers.
1281 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1282 gpointer data, int multiple)
1284 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1286 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1287 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1288 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1289 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1291 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1292 * in a list box. So, if the list box is single-selection:
1294 * - if the list item in question isn't already selected,
1295 * we simply select it.
1296 * - otherwise, we find the next one (or next
1297 * however-far-away) in whichever direction we're going,
1299 * + in this case, we must also fiddle with the
1300 * scrollbar to ensure the newly selected item is
1303 * If it's multiple-selection, we do all of the above
1304 * except actually selecting anything, so we move the focus
1305 * and fiddle the scrollbar to follow it.
1307 GtkWidget *list = item->parent;
1309 gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event");
1312 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1313 gtk_list_select_child(GTK_LIST(list), item);
1316 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1317 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1320 (event->keyval==GDK_Page_Down ||
1321 event->keyval==GDK_KP_Page_Down ||
1322 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1325 GList *children, *chead;
1327 chead = children = gtk_container_children(GTK_CONTAINER(list));
1329 n = g_list_length(children);
1333 * Figure out how many list items to a screenful,
1334 * and adjust the step appropriately.
1336 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1337 step--; /* go by one less than that */
1341 while (children != NULL) {
1342 if (item == children->data)
1344 children = children->next;
1349 if (direction < 0 && i > 0)
1350 children = children->prev, i--;
1351 else if (direction > 0 && i < n-1)
1352 children = children->next, i++;
1356 if (children && children->data) {
1358 gtk_list_select_child(GTK_LIST(list),
1359 GTK_WIDGET(children->data));
1360 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1361 gtk_adjustment_clamp_page
1363 adj->lower + (adj->upper-adj->lower) * i / n,
1364 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1375 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1378 return listitem_key(item, event, data, FALSE);
1381 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1384 return listitem_key(item, event, data, TRUE);
1387 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1390 struct dlgparam *dp = (struct dlgparam *)data;
1391 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1392 switch (event->type) {
1394 case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1395 case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1396 case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1401 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1404 struct dlgparam *dp = (struct dlgparam *)data;
1405 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1406 if (uc->nclicks>1) {
1407 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1413 static void list_selchange(GtkList *list, gpointer data)
1415 struct dlgparam *dp = (struct dlgparam *)data;
1416 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1418 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1421 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1423 int index = dlg_listbox_index(uc->ctrl, dp);
1424 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1428 (index == 0 && direction < 0) ||
1429 (index == g_list_length(children)-1 && direction > 0)) {
1434 child = g_list_nth_data(children, index);
1435 gtk_widget_ref(child);
1436 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1437 g_list_free(children);
1440 children = g_list_append(children, child);
1441 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1442 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1443 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1446 static void draglist_up(GtkButton *button, gpointer data)
1448 struct dlgparam *dp = (struct dlgparam *)data;
1449 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1450 draglist_move(dp, uc, -1);
1453 static void draglist_down(GtkButton *button, gpointer data)
1455 struct dlgparam *dp = (struct dlgparam *)data;
1456 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1457 draglist_move(dp, uc, +1);
1460 #else /* !GTK_CHECK_VERSION(2,0,0) */
1463 * GTK 2 list box event handlers.
1466 static void listbox_doubleclick(GtkTreeView *treeview, GtkTreePath *path,
1467 GtkTreeViewColumn *column, gpointer data)
1469 struct dlgparam *dp = (struct dlgparam *)data;
1470 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(treeview));
1472 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1475 static void listbox_selchange(GtkTreeSelection *treeselection,
1478 struct dlgparam *dp = (struct dlgparam *)data;
1479 GtkTreeView *tree = gtk_tree_selection_get_tree_view(treeselection);
1480 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1482 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1485 struct draglist_valchange_ctx {
1487 struct dlgparam *dp;
1490 static gboolean draglist_valchange(gpointer data)
1492 struct draglist_valchange_ctx *ctx =
1493 (struct draglist_valchange_ctx *)data;
1495 ctx->uc->ctrl->generic.handler(ctx->uc->ctrl, ctx->dp,
1496 ctx->dp->data, EVENT_VALCHANGE);
1503 static void listbox_reorder(GtkTreeModel *treemodel, GtkTreePath *path,
1504 GtkTreeIter *iter, gpointer data)
1506 struct dlgparam *dp = (struct dlgparam *)data;
1510 if (dp->flags & FLAG_UPDATING_LISTBOX)
1511 return; /* not a user drag operation */
1513 tree = g_object_get_data(G_OBJECT(treemodel), "user-data");
1514 uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1517 * We should cause EVENT_VALCHANGE on the list box, now
1518 * that its rows have been reordered. However, the GTK 2
1519 * docs say that at the point this signal is received the
1520 * new row might not have actually been filled in yet.
1522 * (So what smegging use is it then, eh? Don't suppose it
1523 * occurred to you at any point that letting the
1524 * application know _after_ the reordering was compelete
1525 * might be helpful to someone?)
1527 * To get round this, I schedule an idle function, which I
1528 * hope won't be called until the main event loop is
1529 * re-entered after the drag-and-drop handler has finished
1530 * furtling with the list store.
1532 struct draglist_valchange_ctx *ctx =
1533 snew(struct draglist_valchange_ctx);
1536 g_idle_add(draglist_valchange, ctx);
1540 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1542 #if !GTK_CHECK_VERSION(2,4,0)
1544 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1546 struct dlgparam *dp = (struct dlgparam *)data;
1547 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1548 gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data");
1549 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1550 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1555 static void droplist_selchange(GtkComboBox *combo, gpointer data)
1557 struct dlgparam *dp = (struct dlgparam *)data;
1558 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(combo));
1560 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1563 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1565 static void filesel_ok(GtkButton *button, gpointer data)
1567 /* struct dlgparam *dp = (struct dlgparam *)data; */
1568 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1569 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data");
1570 const char *name = gtk_file_selection_get_filename
1571 (GTK_FILE_SELECTION(filesel));
1572 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1575 static void fontsel_ok(GtkButton *button, gpointer data)
1577 /* struct dlgparam *dp = (struct dlgparam *)data; */
1579 #if !GTK_CHECK_VERSION(2,0,0)
1581 gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1582 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data");
1583 const char *name = gtk_font_selection_dialog_get_font_name
1584 (GTK_FONT_SELECTION_DIALOG(fontsel));
1585 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1589 unifontsel *fontsel = (unifontsel *)gtk_object_get_data
1590 (GTK_OBJECT(button), "user-data");
1591 struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1592 char *name = unifontsel_get_name(fontsel);
1593 assert(name); /* should always be ok after OK pressed */
1594 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1600 static void coloursel_ok(GtkButton *button, gpointer data)
1602 struct dlgparam *dp = (struct dlgparam *)data;
1603 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1604 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1605 GtkColorSelection *cs = GTK_COLOR_SELECTION
1606 (gtk_color_selection_dialog_get_color_selection
1607 (GTK_COLOR_SELECTION_DIALOG(coloursel)));
1609 gtk_color_selection_get_color(cs, cvals);
1610 dp->coloursel_result.r = (int) (255 * cvals[0]);
1611 dp->coloursel_result.g = (int) (255 * cvals[1]);
1612 dp->coloursel_result.b = (int) (255 * cvals[2]);
1613 dp->coloursel_result.ok = TRUE;
1614 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1617 static void coloursel_cancel(GtkButton *button, gpointer data)
1619 struct dlgparam *dp = (struct dlgparam *)data;
1620 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1621 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1622 dp->coloursel_result.ok = FALSE;
1623 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1626 static void filefont_clicked(GtkButton *button, gpointer data)
1628 struct dlgparam *dp = (struct dlgparam *)data;
1629 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1631 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1632 GtkWidget *filesel =
1633 gtk_file_selection_new(uc->ctrl->fileselect.title);
1634 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1636 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1638 gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc);
1640 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1641 GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp);
1642 gtk_signal_connect_object
1643 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1644 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1645 gtk_signal_connect_object
1646 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1647 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1648 gtk_widget_show(filesel);
1651 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1652 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1654 #if !GTK_CHECK_VERSION(2,0,0)
1657 * Use the GTK 1 standard font selector.
1660 gchar *spacings[] = { "c", "m", NULL };
1661 GtkWidget *fontsel =
1662 gtk_font_selection_dialog_new("Select a font");
1663 gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1664 gtk_font_selection_dialog_set_filter
1665 (GTK_FONT_SELECTION_DIALOG(fontsel),
1666 GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1667 NULL, NULL, NULL, NULL, spacings, NULL);
1668 if (!gtk_font_selection_dialog_set_font_name
1669 (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1671 * If the font name wasn't found as it was, try opening
1672 * it and extracting its FONT property. This should
1673 * have the effect of mapping short aliases into true
1676 GdkFont *font = gdk_font_load(fontname);
1678 XFontStruct *xfs = GDK_FONT_XFONT(font);
1679 Display *disp = GDK_FONT_XDISPLAY(font);
1680 Atom fontprop = XInternAtom(disp, "FONT", False);
1683 if (XGetFontProperty(xfs, fontprop, &ret)) {
1684 char *name = XGetAtomName(disp, (Atom)ret);
1686 gtk_font_selection_dialog_set_font_name
1687 (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1689 gdk_font_unref(font);
1693 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1694 "user-data", (gpointer)fontsel);
1695 gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc);
1697 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1698 "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1699 gtk_signal_connect_object
1700 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1701 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1703 gtk_signal_connect_object
1704 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1705 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1707 gtk_widget_show(fontsel);
1709 #else /* !GTK_CHECK_VERSION(2,0,0) */
1712 * Use the unifontsel code provided in gtkfont.c.
1715 unifontsel *fontsel = unifontsel_new("Select a font");
1717 gtk_window_set_modal(fontsel->window, TRUE);
1718 unifontsel_set_name(fontsel, fontname);
1720 gtk_object_set_data(GTK_OBJECT(fontsel->ok_button),
1721 "user-data", (gpointer)fontsel);
1722 fontsel->user_data = uc;
1723 gtk_signal_connect(GTK_OBJECT(fontsel->ok_button), "clicked",
1724 GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1725 gtk_signal_connect_object(GTK_OBJECT(fontsel->ok_button), "clicked",
1726 GTK_SIGNAL_FUNC(unifontsel_destroy),
1728 gtk_signal_connect_object(GTK_OBJECT(fontsel->cancel_button),"clicked",
1729 GTK_SIGNAL_FUNC(unifontsel_destroy),
1732 gtk_widget_show(GTK_WIDGET(fontsel->window));
1734 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1739 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1742 struct dlgparam *dp = (struct dlgparam *)data;
1743 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1745 gtk_widget_set_usize(uc->text, alloc->width, -1);
1746 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1747 gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig);
1750 /* ----------------------------------------------------------------------
1751 * This function does the main layout work: it reads a controlset,
1752 * it creates the relevant GTK controls, and returns a GtkWidget
1753 * containing the result. (This widget might be a title of some
1754 * sort, it might be a Columns containing many controls, or it
1755 * might be a GtkFrame containing a Columns; whatever it is, it's
1756 * definitely a GtkWidget and should probably be added to a
1759 * `win' is required for setting the default button. If it is
1760 * non-NULL, all buttons created will be default-capable (so they
1761 * have extra space round them for the default highlight).
1763 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1764 struct controlset *s, GtkWindow *win)
1770 if (!s->boxname && s->boxtitle) {
1771 /* This controlset is a panel title. */
1772 return gtk_label_new(s->boxtitle);
1776 * Otherwise, we expect to be laying out actual controls, so
1777 * we'll start by creating a Columns for the purpose.
1779 cols = COLUMNS(columns_new(4));
1780 ret = GTK_WIDGET(cols);
1781 gtk_widget_show(ret);
1784 * Create a containing frame if we have a box name.
1787 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1788 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1789 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1790 gtk_widget_show(ret);
1794 * Now iterate through the controls themselves, create them,
1795 * and add them to the Columns.
1797 for (i = 0; i < s->ncontrols; i++) {
1798 union control *ctrl = s->ctrls[i];
1801 GtkWidget *w = NULL;
1803 switch (ctrl->generic.type) {
1806 static const int simplecols[1] = { 100 };
1807 columns_set_cols(cols, ctrl->columns.ncols,
1808 (ctrl->columns.percentages ?
1809 ctrl->columns.percentages : simplecols));
1811 continue; /* no actual control created */
1814 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1816 columns_taborder_last(cols, uc->toplevel);
1818 continue; /* no actual control created */
1821 uc = snew(struct uctrl);
1825 #if !GTK_CHECK_VERSION(2,4,0)
1826 uc->list = uc->menu = uc->optmenu = NULL;
1830 #if GTK_CHECK_VERSION(2,0,0)
1831 uc->treeview = NULL;
1832 uc->listmodel = NULL;
1834 uc->button = uc->text = NULL;
1838 switch (ctrl->generic.type) {
1840 w = gtk_button_new_with_label(ctrl->generic.label);
1842 gtk_widget_set_can_default(w, TRUE);
1843 if (ctrl->button.isdefault)
1844 gtk_window_set_default(win, w);
1845 if (ctrl->button.iscancel)
1846 dp->cancelbutton = w;
1848 gtk_signal_connect(GTK_OBJECT(w), "clicked",
1849 GTK_SIGNAL_FUNC(button_clicked), dp);
1850 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1851 GTK_SIGNAL_FUNC(widget_focus), dp);
1852 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1853 ctrl->button.shortcut, SHORTCUT_UCTRL, uc);
1856 w = gtk_check_button_new_with_label(ctrl->generic.label);
1857 gtk_signal_connect(GTK_OBJECT(w), "toggled",
1858 GTK_SIGNAL_FUNC(button_toggled), dp);
1859 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1860 GTK_SIGNAL_FUNC(widget_focus), dp);
1861 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1862 ctrl->checkbox.shortcut, SHORTCUT_UCTRL, uc);
1867 * Radio buttons get to go inside their own Columns, no
1871 gint i, *percentages;
1875 if (ctrl->generic.label) {
1876 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1877 columns_add(COLUMNS(w), label, 0, 1);
1878 columns_force_left_align(COLUMNS(w), label);
1879 gtk_widget_show(label);
1880 shortcut_add(scs, label, ctrl->radio.shortcut,
1881 SHORTCUT_UCTRL, uc);
1884 percentages = g_new(gint, ctrl->radio.ncolumns);
1885 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1887 ((100 * (i+1) / ctrl->radio.ncolumns) -
1888 100 * i / ctrl->radio.ncolumns);
1890 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1892 g_free(percentages);
1895 uc->nbuttons = ctrl->radio.nbuttons;
1896 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1898 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1902 b = (gtk_radio_button_new_with_label
1903 (group, ctrl->radio.buttons[i]));
1905 group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
1906 colstart = i % ctrl->radio.ncolumns;
1907 columns_add(COLUMNS(w), b, colstart,
1908 (i == ctrl->radio.nbuttons-1 ?
1909 ctrl->radio.ncolumns - colstart : 1));
1910 columns_force_left_align(COLUMNS(w), b);
1912 gtk_signal_connect(GTK_OBJECT(b), "toggled",
1913 GTK_SIGNAL_FUNC(button_toggled), dp);
1914 gtk_signal_connect(GTK_OBJECT(b), "focus_in_event",
1915 GTK_SIGNAL_FUNC(widget_focus), dp);
1916 if (ctrl->radio.shortcuts) {
1917 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(b)),
1918 ctrl->radio.shortcuts[i],
1919 SHORTCUT_UCTRL, uc);
1927 GtkWidget *signalobject;
1929 if (ctrl->editbox.has_list) {
1930 #if !GTK_CHECK_VERSION(2,4,0)
1934 w = gtk_combo_new();
1935 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1936 uc->entry = GTK_COMBO(w)->entry;
1937 uc->list = GTK_COMBO(w)->list;
1938 signalobject = uc->entry;
1943 uc->listmodel = gtk_list_store_new(2, G_TYPE_INT,
1945 w = gtk_combo_box_entry_new_with_model
1946 (GTK_TREE_MODEL(uc->listmodel), 1);
1947 /* We cannot support password combo boxes. */
1948 assert(!ctrl->editbox.password);
1950 signalobject = uc->combo;
1953 w = gtk_entry_new();
1954 if (ctrl->editbox.password)
1955 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
1960 gtk_signal_connect(GTK_OBJECT(signalobject), "changed",
1961 GTK_SIGNAL_FUNC(editbox_changed), dp);
1962 gtk_signal_connect(GTK_OBJECT(signalobject), "key_press_event",
1963 GTK_SIGNAL_FUNC(editbox_key), dp);
1964 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_in_event",
1965 GTK_SIGNAL_FUNC(widget_focus), dp);
1966 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_out_event",
1967 GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1968 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_out_event",
1969 GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1971 * Edit boxes, for some strange reason, have a minimum
1972 * width of 150 in GTK 1.2. We don't want this - we'd
1973 * rather the edit boxes acquired their natural width
1974 * from the column layout of the rest of the box.
1976 * Also, while we're here, we'll squirrel away the
1977 * edit box height so we can use that to centre its
1978 * label vertically beside it.
1980 gtk_widget_size_request(w, &req);
1981 gtk_widget_set_usize(w, 10, req.height);
1983 if (ctrl->generic.label) {
1984 GtkWidget *label, *container;
1986 label = gtk_label_new(ctrl->generic.label);
1988 shortcut_add(scs, label, ctrl->editbox.shortcut,
1989 SHORTCUT_FOCUS, uc->entry);
1991 container = columns_new(4);
1992 if (ctrl->editbox.percentwidth == 100) {
1993 columns_add(COLUMNS(container), label, 0, 1);
1994 columns_force_left_align(COLUMNS(container), label);
1995 columns_add(COLUMNS(container), w, 0, 1);
1997 gint percentages[2];
1998 percentages[1] = ctrl->editbox.percentwidth;
1999 percentages[0] = 100 - ctrl->editbox.percentwidth;
2000 columns_set_cols(COLUMNS(container), 2, percentages);
2001 columns_add(COLUMNS(container), label, 0, 1);
2002 columns_force_left_align(COLUMNS(container), label);
2003 columns_add(COLUMNS(container), w, 1, 1);
2004 /* Centre the label vertically. */
2005 gtk_widget_set_usize(label, -1, req.height);
2006 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2008 gtk_widget_show(label);
2016 case CTRL_FILESELECT:
2017 case CTRL_FONTSELECT:
2021 const char *browsebtn =
2022 (ctrl->generic.type == CTRL_FILESELECT ?
2023 "Browse..." : "Change...");
2025 gint percentages[] = { 75, 25 };
2027 columns_set_cols(COLUMNS(w), 2, percentages);
2029 if (ctrl->generic.label) {
2030 ww = gtk_label_new(ctrl->generic.label);
2031 columns_add(COLUMNS(w), ww, 0, 2);
2032 columns_force_left_align(COLUMNS(w), ww);
2033 gtk_widget_show(ww);
2034 shortcut_add(scs, ww,
2035 (ctrl->generic.type == CTRL_FILESELECT ?
2036 ctrl->fileselect.shortcut :
2037 ctrl->fontselect.shortcut),
2038 SHORTCUT_UCTRL, uc);
2042 uc->entry = ww = gtk_entry_new();
2043 gtk_widget_size_request(ww, &req);
2044 gtk_widget_set_usize(ww, 10, req.height);
2045 columns_add(COLUMNS(w), ww, 0, 1);
2046 gtk_widget_show(ww);
2048 uc->button = ww = gtk_button_new_with_label(browsebtn);
2049 columns_add(COLUMNS(w), ww, 1, 1);
2050 gtk_widget_show(ww);
2052 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
2053 GTK_SIGNAL_FUNC(editbox_key), dp);
2055 gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
2056 GTK_SIGNAL_FUNC(editbox_changed), dp);
2057 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
2058 GTK_SIGNAL_FUNC(widget_focus), dp);
2059 gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
2060 GTK_SIGNAL_FUNC(widget_focus), dp);
2061 gtk_signal_connect(GTK_OBJECT(ww), "clicked",
2062 GTK_SIGNAL_FUNC(filefont_clicked), dp);
2067 #if GTK_CHECK_VERSION(2,0,0)
2069 * First construct the list data store, with the right
2070 * number of columns.
2072 # if !GTK_CHECK_VERSION(2,4,0)
2073 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2074 * because combo boxes are still done the old GTK1 way.) */
2075 if (ctrl->listbox.height > 0)
2082 cols = ctrl->listbox.ncols;
2083 cols = cols ? cols : 1;
2084 types = snewn(1 + cols, GType);
2086 types[0] = G_TYPE_INT;
2087 for (i = 0; i < cols; i++)
2088 types[i+1] = G_TYPE_STRING;
2090 uc->listmodel = gtk_list_store_newv(1 + cols, types);
2097 * See if it's a drop-down list (non-editable combo
2100 if (ctrl->listbox.height == 0) {
2101 #if !GTK_CHECK_VERSION(2,4,0)
2103 * GTK1 and early-GTK2 option-menu style of
2106 uc->optmenu = w = gtk_option_menu_new();
2107 uc->menu = gtk_menu_new();
2108 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
2109 gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data",
2110 (gpointer)uc->optmenu);
2111 gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event",
2112 GTK_SIGNAL_FUNC(widget_focus), dp);
2115 * Late-GTK2 style using a GtkComboBox.
2117 GtkCellRenderer *cr;
2120 * Create a non-editable GtkComboBox (that is, not
2121 * its subclass GtkComboBoxEntry).
2123 w = gtk_combo_box_new_with_model
2124 (GTK_TREE_MODEL(uc->listmodel));
2128 * Tell it how to render a list item (i.e. which
2129 * column to look at in the list model).
2131 cr = gtk_cell_renderer_text_new();
2132 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), cr, TRUE);
2133 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), cr,
2137 * And tell it to notify us when the selection
2140 g_signal_connect(G_OBJECT(w), "changed",
2141 G_CALLBACK(droplist_selchange), dp);
2144 #if !GTK_CHECK_VERSION(2,0,0)
2146 * GTK1-style full list box.
2148 uc->list = gtk_list_new();
2149 if (ctrl->listbox.multisel == 2) {
2150 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2151 GTK_SELECTION_EXTENDED);
2152 } else if (ctrl->listbox.multisel == 1) {
2153 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2154 GTK_SELECTION_MULTIPLE);
2156 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2157 GTK_SELECTION_SINGLE);
2159 w = gtk_scrolled_window_new(NULL, NULL);
2160 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
2162 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
2164 GTK_POLICY_AUTOMATIC);
2165 uc->adj = gtk_scrolled_window_get_vadjustment
2166 (GTK_SCROLLED_WINDOW(w));
2168 gtk_widget_show(uc->list);
2169 gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed",
2170 GTK_SIGNAL_FUNC(list_selchange), dp);
2171 gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event",
2172 GTK_SIGNAL_FUNC(widget_focus), dp);
2175 * Adjust the height of the scrolled window to the
2176 * minimum given by the height parameter.
2178 * This piece of guesswork is a horrid hack based
2179 * on looking inside the GTK 1.2 sources
2180 * (specifically gtkviewport.c, which appears to be
2181 * the widget which provides the border around the
2182 * scrolling area). Anyone lets me know how I can
2183 * do this in a way which isn't at risk from GTK
2184 * upgrades, I'd be grateful.
2188 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
2189 gtk_widget_set_usize(w, 10,
2190 2*edge + (ctrl->listbox.height *
2191 get_listitemheight(w)));
2194 if (ctrl->listbox.draglist) {
2196 * GTK doesn't appear to make it easy to
2197 * implement a proper draggable list; so
2198 * instead I'm just going to have to put an Up
2199 * and a Down button to the right of the actual
2200 * list box. Ah well.
2202 GtkWidget *cols, *button;
2203 static const gint percentages[2] = { 80, 20 };
2205 cols = columns_new(4);
2206 columns_set_cols(COLUMNS(cols), 2, percentages);
2207 columns_add(COLUMNS(cols), w, 0, 1);
2209 button = gtk_button_new_with_label("Up");
2210 columns_add(COLUMNS(cols), button, 1, 1);
2211 gtk_widget_show(button);
2212 gtk_signal_connect(GTK_OBJECT(button), "clicked",
2213 GTK_SIGNAL_FUNC(draglist_up), dp);
2214 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
2215 GTK_SIGNAL_FUNC(widget_focus), dp);
2216 button = gtk_button_new_with_label("Down");
2217 columns_add(COLUMNS(cols), button, 1, 1);
2218 gtk_widget_show(button);
2219 gtk_signal_connect(GTK_OBJECT(button), "clicked",
2220 GTK_SIGNAL_FUNC(draglist_down), dp);
2221 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
2222 GTK_SIGNAL_FUNC(widget_focus), dp);
2228 * GTK2 treeview-based full list box.
2230 GtkTreeSelection *sel;
2233 * Create the list box itself, its columns, and
2234 * its containing scrolled window.
2236 w = gtk_tree_view_new_with_model
2237 (GTK_TREE_MODEL(uc->listmodel));
2238 g_object_set_data(G_OBJECT(uc->listmodel), "user-data",
2240 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2241 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
2242 gtk_tree_selection_set_mode
2243 (sel, ctrl->listbox.multisel ? GTK_SELECTION_MULTIPLE :
2244 GTK_SELECTION_SINGLE);
2246 gtk_signal_connect(GTK_OBJECT(w), "row-activated",
2247 GTK_SIGNAL_FUNC(listbox_doubleclick), dp);
2248 g_signal_connect(G_OBJECT(sel), "changed",
2249 G_CALLBACK(listbox_selchange), dp);
2251 if (ctrl->listbox.draglist) {
2252 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w), TRUE);
2253 g_signal_connect(G_OBJECT(uc->listmodel), "row-inserted",
2254 G_CALLBACK(listbox_reorder), dp);
2261 cols = ctrl->listbox.ncols;
2262 cols = cols ? cols : 1;
2263 for (i = 0; i < cols; i++) {
2264 GtkTreeViewColumn *column;
2265 GtkCellRenderer *cellrend;
2267 * It appears that GTK 2 doesn't leave us any
2268 * particularly sensible way to honour the
2269 * "percentages" specification in the ctrl
2272 cellrend = gtk_cell_renderer_text_new();
2273 if (!ctrl->listbox.hscroll) {
2274 gtk_object_set(GTK_OBJECT(cellrend),
2275 "ellipsize", PANGO_ELLIPSIZE_END,
2276 "ellipsize-set", TRUE,
2279 column = gtk_tree_view_column_new_with_attributes
2280 ("heading", cellrend, "text", i+1, (char *)NULL);
2281 gtk_tree_view_column_set_sizing
2282 (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2283 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2290 scroll = gtk_scrolled_window_new(NULL, NULL);
2291 gtk_scrolled_window_set_shadow_type
2292 (GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
2294 gtk_container_add(GTK_CONTAINER(scroll), w);
2295 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2296 GTK_POLICY_AUTOMATIC,
2298 gtk_widget_set_size_request
2300 ctrl->listbox.height * get_listitemheight(w));
2307 if (ctrl->generic.label) {
2308 GtkWidget *label, *container;
2311 label = gtk_label_new(ctrl->generic.label);
2313 shortcut_add(scs, label, ctrl->listbox.shortcut,
2316 container = columns_new(4);
2317 if (ctrl->listbox.percentwidth == 100) {
2318 columns_add(COLUMNS(container), label, 0, 1);
2319 columns_force_left_align(COLUMNS(container), label);
2320 columns_add(COLUMNS(container), w, 0, 1);
2322 gint percentages[2];
2323 percentages[1] = ctrl->listbox.percentwidth;
2324 percentages[0] = 100 - ctrl->listbox.percentwidth;
2325 columns_set_cols(COLUMNS(container), 2, percentages);
2326 columns_add(COLUMNS(container), label, 0, 1);
2327 columns_force_left_align(COLUMNS(container), label);
2328 columns_add(COLUMNS(container), w, 1, 1);
2329 /* Centre the label vertically. */
2330 gtk_widget_size_request(w, &req);
2331 gtk_widget_set_usize(label, -1, req.height);
2332 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2334 gtk_widget_show(label);
2344 * Wrapping text widgets don't sit well with the GTK
2345 * layout model, in which widgets state a minimum size
2346 * and the whole window then adjusts to the smallest
2347 * size it can sensibly take given its contents. A
2348 * wrapping text widget _has_ no clear minimum size;
2349 * instead it has a range of possibilities. It can be
2350 * one line deep but 2000 wide, or two lines deep and
2351 * 1000 pixels, or three by 867, or four by 500 and so
2352 * on. It can be as short as you like provided you
2353 * don't mind it being wide, or as narrow as you like
2354 * provided you don't mind it being tall.
2356 * Therefore, it fits very badly into the layout model.
2357 * Hence the only thing to do is pick a width and let
2358 * it choose its own number of lines. To do this I'm
2359 * going to cheat a little. All new wrapping text
2360 * widgets will be created with a minimal text content
2361 * "X"; then, after the rest of the dialog box is set
2362 * up and its size calculated, the text widgets will be
2363 * told their width and given their real text, which
2364 * will cause the size to be recomputed in the y
2365 * direction (because many of them will expand to more
2368 uc->text = w = gtk_label_new("X");
2369 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
2370 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
2372 gtk_signal_connect(GTK_OBJECT(w), "size-allocate",
2373 GTK_SIGNAL_FUNC(label_sizealloc), dp);
2379 columns_add(cols, w,
2380 COLUMN_START(ctrl->generic.column),
2381 COLUMN_SPAN(ctrl->generic.column));
2383 columns_force_left_align(cols, w);
2387 dlg_add_uctrl(dp, uc);
2394 struct dlgparam *dp;
2395 GtkNotebook *panels;
2397 #if !GTK_CHECK_VERSION(2,0,0)
2398 GtkWidget *treeitem;
2401 GtkTreePath *treepath;
2403 struct Shortcuts shortcuts;
2406 #if GTK_CHECK_VERSION(2,0,0)
2407 static void treeselection_changed(GtkTreeSelection *treeselection,
2410 struct selparam *sps = (struct selparam *)data, *sp;
2411 GtkTreeModel *treemodel;
2412 GtkTreeIter treeiter;
2416 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2419 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
2422 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2423 gtk_notebook_set_page(sp->panels, page_num);
2425 dlg_refresh(NULL, sp->dp);
2427 sp->dp->shortcuts = &sp->shortcuts;
2430 static void treeitem_sel(GtkItem *item, gpointer data)
2432 struct selparam *sp = (struct selparam *)data;
2435 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2436 gtk_notebook_set_page(sp->panels, page_num);
2438 dlg_refresh(NULL, sp->dp);
2440 sp->dp->shortcuts = &sp->shortcuts;
2441 sp->dp->currtreeitem = sp->treeitem;
2445 static void window_destroy(GtkWidget *widget, gpointer data)
2450 #if !GTK_CHECK_VERSION(2,0,0)
2451 static int tree_grab_focus(struct dlgparam *dp)
2456 * See if any of the treeitems has the focus.
2459 for (i = 0; i < dp->ntreeitems; i++)
2460 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
2468 gtk_widget_grab_focus(dp->currtreeitem);
2473 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
2476 struct dlgparam *dp = (struct dlgparam *)data;
2478 gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus");
2480 * If there's a focused treeitem, we return FALSE to cause the
2481 * focus to move on to some totally other control. If not, we
2482 * focus the selected one.
2484 return tree_grab_focus(dp);
2488 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2490 struct dlgparam *dp = (struct dlgparam *)data;
2492 if (event->keyval == GDK_KEY_Escape && dp->cancelbutton) {
2493 gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked");
2497 if ((event->state & GDK_MOD1_MASK) &&
2498 (unsigned char)event->string[0] > 0 &&
2499 (unsigned char)event->string[0] <= 127) {
2500 int schr = (unsigned char)event->string[0];
2501 struct Shortcut *sc = &dp->shortcuts->sc[schr];
2503 switch (sc->action) {
2505 #if GTK_CHECK_VERSION(2,0,0)
2506 gtk_widget_grab_focus(sc->widget);
2508 tree_grab_focus(dp);
2511 case SHORTCUT_FOCUS:
2512 gtk_widget_grab_focus(sc->widget);
2514 case SHORTCUT_UCTRL:
2516 * We must do something sensible with a uctrl.
2517 * Precisely what this is depends on the type of
2520 switch (sc->uc->ctrl->generic.type) {
2523 /* Check boxes and buttons get the focus _and_ get toggled. */
2524 gtk_widget_grab_focus(sc->uc->toplevel);
2525 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel),
2528 case CTRL_FILESELECT:
2529 case CTRL_FONTSELECT:
2530 /* File/font selectors have their buttons pressed (ooer),
2531 * and focus transferred to the edit box. */
2532 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button),
2534 gtk_widget_grab_focus(sc->uc->entry);
2538 * Radio buttons are fun, because they have
2539 * multiple shortcuts. We must find whether the
2540 * activated shortcut is the shortcut for the whole
2541 * group, or for a particular button. In the former
2542 * case, we find the currently selected button and
2543 * focus it; in the latter, we focus-and-click the
2544 * button whose shortcut was pressed.
2546 if (schr == sc->uc->ctrl->radio.shortcut) {
2548 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2549 if (gtk_toggle_button_get_active
2550 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
2551 gtk_widget_grab_focus(sc->uc->buttons[i]);
2553 } else if (sc->uc->ctrl->radio.shortcuts) {
2555 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2556 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
2557 gtk_widget_grab_focus(sc->uc->buttons[i]);
2558 gtk_signal_emit_by_name
2559 (GTK_OBJECT(sc->uc->buttons[i]), "clicked");
2565 #if !GTK_CHECK_VERSION(2,4,0)
2566 if (sc->uc->optmenu) {
2570 gtk_widget_grab_focus(sc->uc->optmenu);
2571 /* Option menus don't work using the "clicked" signal.
2572 * We need to manufacture a button press event :-/ */
2573 bev.type = GDK_BUTTON_PRESS;
2575 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu),
2576 "button_press_event",
2581 if (sc->uc->combo) {
2582 gtk_widget_grab_focus(sc->uc->combo);
2583 gtk_combo_box_popup(GTK_COMBO_BOX(sc->uc->combo));
2587 #if !GTK_CHECK_VERSION(2,0,0)
2590 * For GTK-1 style list boxes, we tell it to
2591 * focus one of its children, which appears to
2592 * do the Right Thing.
2594 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
2595 GTK_DIR_TAB_FORWARD);
2599 if (sc->uc->treeview) {
2600 gtk_widget_grab_focus(sc->uc->treeview);
2604 assert(!"We shouldn't get here");
2614 #if !GTK_CHECK_VERSION(2,0,0)
2615 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2617 struct dlgparam *dp = (struct dlgparam *)data;
2619 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2620 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2622 for (i = 0; i < dp->ntreeitems; i++)
2623 if (widget == dp->treeitems[i])
2625 if (i < dp->ntreeitems) {
2626 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2633 if (i < 0 || i >= dp->ntreeitems)
2634 break; /* nothing in that dir to select */
2636 * Determine if this tree item is visible.
2639 GtkWidget *w = dp->treeitems[i];
2641 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2642 if (!GTK_WIDGET_VISIBLE(w)) {
2649 j = i; /* got one */
2655 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2658 gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle");
2659 gtk_widget_grab_focus(dp->treeitems[j]);
2665 * It's nice for Left and Right to expand and collapse tree
2668 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2669 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2671 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2674 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2675 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2677 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2685 static void shortcut_highlight(GtkWidget *labelw, int chr)
2687 GtkLabel *label = GTK_LABEL(labelw);
2688 gchar *currstr, *pattern;
2691 gtk_label_get(label, &currstr);
2692 for (i = 0; currstr[i]; i++)
2693 if (tolower((unsigned char)currstr[i]) == chr) {
2696 pattern = dupprintf("%*s_", i, "");
2698 gtk_widget_size_request(GTK_WIDGET(label), &req);
2699 gtk_label_set_pattern(label, pattern);
2700 gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
2707 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2708 int chr, int action, void *ptr)
2710 if (chr == NO_SHORTCUT)
2713 chr = tolower((unsigned char)chr);
2715 assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2717 scs->sc[chr].action = action;
2719 if (action == SHORTCUT_FOCUS) {
2720 scs->sc[chr].uc = NULL;
2721 scs->sc[chr].widget = (GtkWidget *)ptr;
2723 scs->sc[chr].widget = NULL;
2724 scs->sc[chr].uc = (struct uctrl *)ptr;
2727 shortcut_highlight(labelw, chr);
2730 int get_listitemheight(GtkWidget *w)
2732 #if !GTK_CHECK_VERSION(2,0,0)
2733 GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2735 gtk_widget_size_request(listitem, &req);
2736 gtk_object_sink(GTK_OBJECT(listitem));
2740 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
2741 gtk_cell_renderer_get_size(cr, w, NULL, NULL, NULL, NULL, &height);
2742 g_object_ref(G_OBJECT(cr));
2743 gtk_object_sink(GTK_OBJECT(cr));
2744 g_object_unref(G_OBJECT(cr));
2749 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2751 #if !GTK_CHECK_VERSION(2,0,0)
2754 * In GTK 1, laying out the buttons at the bottom of the
2755 * configuration box is nice and easy, because a GtkDialog's
2756 * action_area is a GtkHBox which stretches to cover the full
2757 * width of the dialog. So we just put our Columns widget
2758 * straight into that hbox, and it ends up just where we want
2761 gtk_box_pack_start(GTK_BOX(dlg->action_area), w, TRUE, TRUE, 0);
2765 * In GTK 2, the action area is now a GtkHButtonBox and its
2766 * layout behaviour seems to be different: it doesn't stretch
2767 * to cover the full width of the window, but instead finds its
2768 * own preferred width and right-aligns that within the window.
2769 * This isn't what we want, because we have both left-aligned
2770 * and right-aligned buttons coming out of the above call to
2771 * layout_ctrls(), and right-aligning the whole thing will
2772 * result in the former being centred and looking weird.
2774 * So instead we abandon the dialog's action area completely:
2775 * we gtk_widget_hide() it in the below code, and we also call
2776 * gtk_dialog_set_has_separator() to remove the separator above
2777 * it. We then insert our own action area into the end of the
2778 * dialog's main vbox, and add our own separator above that.
2780 * (Ideally, if we were a native GTK app, we would use the
2781 * GtkHButtonBox's _own_ innate ability to support one set of
2782 * buttons being right-aligned and one left-aligned. But to do
2783 * that here, we would have to either (a) pick apart our cross-
2784 * platform layout structures and treat them specially for this
2785 * particular set of controls, which would be painful, or else
2786 * (b) develop a special and simpler cross-platform
2787 * representation for these particular controls, and introduce
2788 * special-case code into all the _other_ platforms to handle
2789 * it. Neither appeals. Therefore, I regretfully discard the
2790 * GTKHButtonBox and go it alone.)
2794 align = gtk_alignment_new(0, 0, 1, 1);
2795 gtk_container_add(GTK_CONTAINER(align), w);
2797 * The purpose of this GtkAlignment is to provide padding
2798 * around the buttons. The padding we use is twice the padding
2799 * used in our GtkColumns, because we nest two GtkColumns most
2800 * of the time (one separating the tree view from the main
2801 * controls, and another for the main controls themselves).
2803 #if GTK_CHECK_VERSION(2,4,0)
2804 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2806 gtk_widget_show(align);
2807 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2808 align, FALSE, TRUE, 0);
2809 w = gtk_hseparator_new();
2810 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2813 gtk_widget_hide(gtk_dialog_get_action_area(dlg));
2814 gtk_dialog_set_has_separator(dlg, FALSE);
2818 int do_config_box(const char *title, Conf *conf, int midsession,
2821 GtkWidget *window, *hbox, *vbox, *cols, *label,
2822 *tree, *treescroll, *panels, *panelvbox;
2823 int index, level, protocol;
2824 struct controlbox *ctrlbox;
2826 #if GTK_CHECK_VERSION(2,0,0)
2827 GtkTreeStore *treestore;
2828 GtkCellRenderer *treerenderer;
2829 GtkTreeViewColumn *treecolumn;
2830 GtkTreeSelection *treeselection;
2831 GtkTreeIter treeiterlevels[8];
2833 GtkTreeItem *treeitemlevels[8];
2834 GtkTree *treelevels[8];
2837 struct Shortcuts scs;
2839 struct selparam *selparams = NULL;
2840 int nselparams = 0, selparamsize = 0;
2844 for (index = 0; index < lenof(scs.sc); index++) {
2845 scs.sc[index].action = SHORTCUT_EMPTY;
2848 window = gtk_dialog_new();
2850 ctrlbox = ctrl_new_box();
2851 protocol = conf_get_int(conf, CONF_protocol);
2852 setup_config_box(ctrlbox, midsession, protocol, protcfginfo);
2853 unix_setup_config_box(ctrlbox, midsession, protocol);
2854 gtk_setup_config_box(ctrlbox, midsession, window);
2856 gtk_window_set_title(GTK_WINDOW(window), title);
2857 hbox = gtk_hbox_new(FALSE, 4);
2859 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
2860 hbox, TRUE, TRUE, 0);
2861 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2862 gtk_widget_show(hbox);
2863 vbox = gtk_vbox_new(FALSE, 4);
2864 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2865 gtk_widget_show(vbox);
2866 cols = columns_new(4);
2867 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2868 gtk_widget_show(cols);
2869 label = gtk_label_new("Category:");
2870 columns_add(COLUMNS(cols), label, 0, 1);
2871 columns_force_left_align(COLUMNS(cols), label);
2872 gtk_widget_show(label);
2873 treescroll = gtk_scrolled_window_new(NULL, NULL);
2874 #if GTK_CHECK_VERSION(2,0,0)
2875 treestore = gtk_tree_store_new
2876 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2877 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2878 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2879 treerenderer = gtk_cell_renderer_text_new();
2880 treecolumn = gtk_tree_view_column_new_with_attributes
2881 ("Label", treerenderer, "text", 0, NULL);
2882 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2883 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2884 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2885 gtk_container_add(GTK_CONTAINER(treescroll), tree);
2887 tree = gtk_tree_new();
2888 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2889 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2890 gtk_signal_connect(GTK_OBJECT(tree), "focus",
2891 GTK_SIGNAL_FUNC(tree_focus), &dp);
2893 gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event",
2894 GTK_SIGNAL_FUNC(widget_focus), &dp);
2895 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2896 gtk_widget_show(treescroll);
2897 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2898 panels = gtk_notebook_new();
2899 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2900 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2901 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2902 gtk_widget_show(panels);
2907 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2908 struct controlset *s = ctrlbox->ctrlsets[index];
2911 if (!*s->pathname) {
2912 w = layout_ctrls(&dp, &scs, s, GTK_WINDOW(window));
2914 set_dialog_action_area(GTK_DIALOG(window), w);
2916 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2917 if (j != INT_MAX) { /* add to treeview, start new panel */
2919 #if GTK_CHECK_VERSION(2,0,0)
2920 GtkTreeIter treeiter;
2922 GtkWidget *treeitem;
2927 * We expect never to find an implicit path
2928 * component. For example, we expect never to see
2929 * A/B/C followed by A/D/E, because that would
2930 * _implicitly_ create A/D. All our path prefixes
2931 * are expected to contain actual controls and be
2932 * selectable in the treeview; so we would expect
2933 * to see A/D _explicitly_ before encountering
2936 assert(j == ctrl_path_elements(s->pathname) - 1);
2938 c = strrchr(s->pathname, '/');
2946 first = (panelvbox == NULL);
2948 panelvbox = gtk_vbox_new(FALSE, 4);
2949 gtk_widget_show(panelvbox);
2950 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
2955 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
2957 gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num);
2960 if (nselparams >= selparamsize) {
2962 selparams = sresize(selparams, selparamsize,
2965 selparams[nselparams].dp = &dp;
2966 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
2967 selparams[nselparams].panel = panelvbox;
2968 selparams[nselparams].shortcuts = scs; /* structure copy */
2970 assert(j-1 < level);
2972 #if GTK_CHECK_VERSION(2,0,0)
2974 /* treeiterlevels[j-1] will always be valid because we
2975 * don't allow implicit path components; see above.
2977 gtk_tree_store_append(treestore, &treeiter,
2978 &treeiterlevels[j-1]);
2980 gtk_tree_store_append(treestore, &treeiter, NULL);
2981 gtk_tree_store_set(treestore, &treeiter,
2983 TREESTORE_PARAMS, nselparams,
2985 treeiterlevels[j] = treeiter;
2987 selparams[nselparams].depth = j;
2989 selparams[nselparams].treepath =
2990 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
2991 &treeiterlevels[j-1]);
2993 * We are going to collapse all tree branches
2994 * at depth greater than 2, but not _yet_; see
2995 * the comment at the call to
2996 * gtk_tree_view_collapse_row below.
2998 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree),
2999 selparams[nselparams].treepath,
3002 selparams[nselparams].treepath = NULL;
3005 treeitem = gtk_tree_item_new_with_label(c);
3007 if (!treelevels[j-1]) {
3008 treelevels[j-1] = GTK_TREE(gtk_tree_new());
3009 gtk_tree_item_set_subtree
3010 (treeitemlevels[j-1],
3011 GTK_WIDGET(treelevels[j-1]));
3013 gtk_tree_item_expand(treeitemlevels[j-1]);
3015 gtk_tree_item_collapse(treeitemlevels[j-1]);
3017 gtk_tree_append(treelevels[j-1], treeitem);
3019 gtk_tree_append(GTK_TREE(tree), treeitem);
3021 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
3022 treelevels[j] = NULL;
3024 gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event",
3025 GTK_SIGNAL_FUNC(tree_key_press), &dp);
3026 gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event",
3027 GTK_SIGNAL_FUNC(widget_focus), &dp);
3029 gtk_widget_show(treeitem);
3032 gtk_tree_select_child(GTK_TREE(tree), treeitem);
3033 selparams[nselparams].treeitem = treeitem;
3040 w = layout_ctrls(&dp, &selparams[nselparams-1].shortcuts, s, NULL);
3041 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
3046 #if GTK_CHECK_VERSION(2,0,0)
3052 * We want our tree view to come up with all branches at
3053 * depth 2 or more collapsed. However, if we start off
3054 * with those branches collapsed, then the tree view's
3055 * size request will be calculated based on the width of
3056 * the collapsed tree. So instead we start with them all
3057 * expanded; then we ask for the current size request,
3058 * collapse the relevant rows, and force the width to the
3059 * value we just computed. This arranges that the tree
3060 * view is wide enough to have all branches expanded
3064 gtk_widget_size_request(tree, &req);
3066 for (i = 0; i < nselparams; i++)
3067 if (selparams[i].depth >= 2)
3068 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree),
3069 selparams[i].treepath);
3071 gtk_widget_set_size_request(tree, req.width, -1);
3075 #if GTK_CHECK_VERSION(2,0,0)
3076 g_signal_connect(G_OBJECT(treeselection), "changed",
3077 G_CALLBACK(treeselection_changed), selparams);
3079 dp.ntreeitems = nselparams;
3080 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
3082 for (index = 0; index < nselparams; index++) {
3083 gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
3084 GTK_SIGNAL_FUNC(treeitem_sel),
3086 dp.treeitems[index] = selparams[index].treeitem;
3091 dlg_refresh(NULL, &dp);
3093 dp.shortcuts = &selparams[0].shortcuts;
3094 #if !GTK_CHECK_VERSION(2,0,0)
3095 dp.currtreeitem = dp.treeitems[0];
3097 dp.lastfocus = NULL;
3103 extern void set_window_icon(GtkWidget *window,
3104 const char *const *const *icon,
3106 extern const char *const *const cfg_icon[];
3107 extern const int n_cfg_icon;
3108 set_window_icon(window, cfg_icon, n_cfg_icon);
3111 #if !GTK_CHECK_VERSION(2,0,0)
3112 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
3115 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
3117 GTK_POLICY_AUTOMATIC);
3118 gtk_widget_show(tree);
3120 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3121 gtk_widget_show(window);
3124 * Set focus into the first available control.
3126 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3127 struct controlset *s = ctrlbox->ctrlsets[index];
3132 for (j = 0; j < s->ncontrols; j++)
3133 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
3134 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
3135 s->ctrls[j]->generic.type != CTRL_TEXT) {
3136 dlg_set_focus(s->ctrls[j], &dp);
3137 dp.lastfocus = s->ctrls[j];
3146 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3147 GTK_SIGNAL_FUNC(window_destroy), NULL);
3148 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3149 GTK_SIGNAL_FUNC(win_key_press), &dp);
3159 static void messagebox_handler(union control *ctrl, void *dlg,
3160 void *data, int event)
3162 if (event == EVENT_ACTION)
3163 dlg_end(dlg, ctrl->generic.context.i);
3165 int messagebox(GtkWidget *parentwin, const char *title, const char *msg,
3168 GtkWidget *window, *w0, *w1;
3169 struct controlbox *ctrlbox;
3170 struct controlset *s0, *s1;
3173 struct Shortcuts scs;
3179 for (index = 0; index < lenof(scs.sc); index++) {
3180 scs.sc[index].action = SHORTCUT_EMPTY;
3183 ctrlbox = ctrl_new_box();
3186 va_start(ap, minwid);
3187 while (va_arg(ap, char *) != NULL) {
3189 (void) va_arg(ap, int); /* shortcut */
3190 (void) va_arg(ap, int); /* normal/default/cancel */
3191 (void) va_arg(ap, int); /* end value */
3195 s0 = ctrl_getset(ctrlbox, "", "", "");
3196 c = ctrl_columns(s0, 2, 50, 50);
3197 c->columns.ncols = s0->ncolumns = ncols;
3198 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
3199 for (index = 0; index < ncols; index++)
3200 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
3201 va_start(ap, minwid);
3204 char *title = va_arg(ap, char *);
3205 int shortcut, type, value;
3208 shortcut = va_arg(ap, int);
3209 type = va_arg(ap, int);
3210 value = va_arg(ap, int);
3211 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
3212 messagebox_handler, I(value));
3213 c->generic.column = index++;
3215 c->button.isdefault = TRUE;
3217 c->button.iscancel = TRUE;
3221 s1 = ctrl_getset(ctrlbox, "x", "", "");
3222 ctrl_text(s1, msg, HELPCTX(no_help));
3224 window = gtk_dialog_new();
3225 gtk_window_set_title(GTK_WINDOW(window), title);
3226 w0 = layout_ctrls(&dp, &scs, s0, GTK_WINDOW(window));
3227 set_dialog_action_area(GTK_DIALOG(window), w0);
3228 gtk_widget_show(w0);
3229 w1 = layout_ctrls(&dp, &scs, s1, GTK_WINDOW(window));
3230 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3231 gtk_widget_set_usize(w1, minwid+20, -1);
3233 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3235 gtk_widget_show(w1);
3237 dp.shortcuts = &scs;
3238 dp.lastfocus = NULL;
3242 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3244 set_transient_window_pos(parentwin, window);
3245 gtk_window_set_transient_for(GTK_WINDOW(window),
3246 GTK_WINDOW(parentwin));
3248 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3249 gtk_widget_show(window);
3251 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3252 GTK_SIGNAL_FUNC(window_destroy), NULL);
3253 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3254 GTK_SIGNAL_FUNC(win_key_press), &dp);
3259 ctrl_free_box(ctrlbox);
3264 int string_width(const char *text)
3266 GtkWidget *label = gtk_label_new(text);
3268 gtk_widget_size_request(label, &req);
3269 gtk_object_sink(GTK_OBJECT(label));
3273 int reallyclose(void *frontend)
3275 char *title = dupcat(appname, " Exit Confirmation", NULL);
3276 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
3277 title, "Are you sure you want to close this session?",
3278 string_width("Most of the width of the above text"),
3286 int verify_ssh_host_key(void *frontend, char *host, int port,
3287 const char *keytype, char *keystr, char *fingerprint,
3288 void (*callback)(void *ctx, int result), void *ctx)
3290 static const char absenttxt[] =
3291 "The server's host key is not cached. You have no guarantee "
3292 "that the server is the computer you think it is.\n"
3293 "The server's %s key fingerprint is:\n"
3295 "If you trust this host, press \"Accept\" to add the key to "
3296 "PuTTY's cache and carry on connecting.\n"
3297 "If you want to carry on connecting just once, without "
3298 "adding the key to the cache, press \"Connect Once\".\n"
3299 "If you do not trust this host, press \"Cancel\" to abandon the "
3301 static const char wrongtxt[] =
3302 "WARNING - POTENTIAL SECURITY BREACH!\n"
3303 "The server's host key does not match the one PuTTY has "
3304 "cached. This means that either the server administrator "
3305 "has changed the host key, or you have actually connected "
3306 "to another computer pretending to be the server.\n"
3307 "The new %s key fingerprint is:\n"
3309 "If you were expecting this change and trust the new key, "
3310 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3311 "If you want to carry on connecting but without updating "
3312 "the cache, press \"Connect Once\".\n"
3313 "If you want to abandon the connection completely, press "
3314 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3322 ret = verify_host_key(host, port, keytype, keystr);
3324 if (ret == 0) /* success - key matched OK */
3327 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
3329 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3330 "PuTTY Security Alert", text,
3331 string_width(fingerprint),
3332 "Accept", 'a', 0, 2,
3333 "Connect Once", 'o', 0, 1,
3334 "Cancel", 'c', -1, 0,
3340 store_host_key(host, port, keytype, keystr);
3341 return 1; /* continue with connection */
3342 } else if (ret == 1)
3343 return 1; /* continue with connection */
3344 return 0; /* do not continue with connection */
3348 * Ask whether the selected algorithm is acceptable (since it was
3349 * below the configured 'warn' threshold).
3351 int askalg(void *frontend, const char *algtype, const char *algname,
3352 void (*callback)(void *ctx, int result), void *ctx)
3354 static const char msg[] =
3355 "The first %s supported by the server is "
3356 "%s, which is below the configured warning threshold.\n"
3357 "Continue with connection?";
3361 text = dupprintf(msg, algtype, algname);
3362 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3363 "PuTTY Security Alert", text,
3364 string_width("Continue with connection?"),
3377 void old_keyfile_warning(void)
3380 * This should never happen on Unix. We hope.
3384 void fatal_message_box(void *window, const char *msg)
3386 messagebox(window, "PuTTY Fatal Error", msg,
3387 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3388 "OK", 'o', 1, 1, NULL);
3391 void nonfatal_message_box(void *window, const char *msg)
3393 messagebox(window, "PuTTY Error", msg,
3394 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3395 "OK", 'o', 1, 1, NULL);
3398 void fatalbox(const char *p, ...)
3403 msg = dupvprintf(p, ap);
3405 fatal_message_box(NULL, msg);
3410 void nonfatal(const char *p, ...)
3415 msg = dupvprintf(p, ap);
3417 nonfatal_message_box(NULL, msg);
3421 static GtkWidget *aboutbox = NULL;
3423 static void about_close_clicked(GtkButton *button, gpointer data)
3425 gtk_widget_destroy(aboutbox);
3429 static void licence_clicked(GtkButton *button, gpointer data)
3433 const char *licence =
3434 "Copyright 1997-2015 Simon Tatham.\n\n"
3436 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3437 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3438 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3439 "Markus Kuhn, Colin Watson, Christopher Staite, and CORE SDI S.A.\n\n"
3441 "Permission is hereby granted, free of charge, to any person "
3442 "obtaining a copy of this software and associated documentation "
3443 "files (the ""Software""), to deal in the Software without restriction, "
3444 "including without limitation the rights to use, copy, modify, merge, "
3445 "publish, distribute, sublicense, and/or sell copies of the Software, "
3446 "and to permit persons to whom the Software is furnished to do so, "
3447 "subject to the following conditions:\n\n"
3449 "The above copyright notice and this permission notice shall be "
3450 "included in all copies or substantial portions of the Software.\n\n"
3452 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3453 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3454 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3455 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3456 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3457 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3458 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3459 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3460 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3461 "OTHER DEALINGS IN THE SOFTWARE.";
3463 title = dupcat(appname, " Licence", NULL);
3464 assert(aboutbox != NULL);
3465 messagebox(aboutbox, title, licence,
3466 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3467 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3468 "OK", 'o', 1, 1, NULL);
3472 void about_box(void *window)
3478 gtk_widget_grab_focus(aboutbox);
3482 aboutbox = gtk_dialog_new();
3483 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
3484 title = dupcat("About ", appname, NULL);
3485 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
3488 w = gtk_button_new_with_label("Close");
3489 gtk_widget_set_can_default(w, TRUE);
3490 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
3491 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3492 w, FALSE, FALSE, 0);
3493 gtk_signal_connect(GTK_OBJECT(w), "clicked",
3494 GTK_SIGNAL_FUNC(about_close_clicked), NULL);
3497 w = gtk_button_new_with_label("View Licence");
3498 gtk_widget_set_can_default(w, TRUE);
3499 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3500 w, FALSE, FALSE, 0);
3501 gtk_signal_connect(GTK_OBJECT(w), "clicked",
3502 GTK_SIGNAL_FUNC(licence_clicked), NULL);
3505 w = gtk_label_new(appname);
3507 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3508 w, FALSE, FALSE, 0);
3511 w = gtk_label_new(ver);
3513 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3514 w, FALSE, FALSE, 5);
3517 w = gtk_label_new("Copyright 1997-2015 Simon Tatham. All rights reserved");
3519 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3520 w, FALSE, FALSE, 5);
3523 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
3524 gtk_window_set_transient_for(GTK_WINDOW(aboutbox),
3525 GTK_WINDOW(window));
3526 gtk_widget_show(aboutbox);
3529 struct eventlog_stuff {
3530 GtkWidget *parentwin, *window;
3531 struct controlbox *eventbox;
3532 struct Shortcuts scs;
3534 union control *listctrl;
3536 int nevents, negsize;
3539 int ignore_selchange;
3542 static void eventlog_destroy(GtkWidget *widget, gpointer data)
3544 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3549 dlg_cleanup(&es->dp);
3550 ctrl_free_box(es->eventbox);
3552 static void eventlog_ok_handler(union control *ctrl, void *dlg,
3553 void *data, int event)
3555 if (event == EVENT_ACTION)
3558 static void eventlog_list_handler(union control *ctrl, void *dlg,
3559 void *data, int event)
3561 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3563 if (event == EVENT_REFRESH) {
3566 dlg_update_start(ctrl, dlg);
3567 dlg_listbox_clear(ctrl, dlg);
3568 for (i = 0; i < es->nevents; i++) {
3569 dlg_listbox_add(ctrl, dlg, es->events[i]);
3571 dlg_update_done(ctrl, dlg);
3572 } else if (event == EVENT_SELCHANGE) {
3577 * If this SELCHANGE event is happening as a result of
3578 * deliberate deselection because someone else has grabbed
3579 * the selection, the last thing we want to do is pre-empt
3582 if (es->ignore_selchange)
3586 * Construct the data to use as the selection.
3591 for (i = 0; i < es->nevents; i++) {
3592 if (dlg_listbox_issel(ctrl, dlg, i)) {
3593 int extralen = strlen(es->events[i]);
3595 if (es->sellen + extralen + 2 > selsize) {
3596 selsize = es->sellen + extralen + 512;
3597 es->seldata = sresize(es->seldata, selsize, char);
3600 strcpy(es->seldata + es->sellen, es->events[i]);
3601 es->sellen += extralen;
3602 es->seldata[es->sellen++] = '\n';
3606 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
3607 GDK_CURRENT_TIME)) {
3608 extern GdkAtom compound_text_atom;
3610 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3611 GDK_SELECTION_TYPE_STRING, 1);
3612 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3613 compound_text_atom, 1);
3619 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
3620 guint info, guint time_stamp, gpointer data)
3622 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3624 gtk_selection_data_set(seldata, gtk_selection_data_get_target(seldata), 8,
3625 (unsigned char *)es->seldata, es->sellen);
3628 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
3631 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3635 * Deselect everything in the list box.
3637 uc = dlg_find_byctrl(&es->dp, es->listctrl);
3638 es->ignore_selchange = 1;
3639 #if !GTK_CHECK_VERSION(2,0,0)
3641 gtk_list_unselect_all(GTK_LIST(uc->list));
3643 assert(uc->treeview);
3644 gtk_tree_selection_unselect_all
3645 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview)));
3647 es->ignore_selchange = 0;
3655 void showeventlog(void *estuff, void *parentwin)
3657 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3658 GtkWidget *window, *w0, *w1;
3659 GtkWidget *parent = GTK_WIDGET(parentwin);
3660 struct controlset *s0, *s1;
3666 gtk_widget_grab_focus(es->window);
3672 for (index = 0; index < lenof(es->scs.sc); index++) {
3673 es->scs.sc[index].action = SHORTCUT_EMPTY;
3676 es->eventbox = ctrl_new_box();
3678 s0 = ctrl_getset(es->eventbox, "", "", "");
3679 ctrl_columns(s0, 3, 33, 34, 33);
3680 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
3681 eventlog_ok_handler, P(NULL));
3682 c->button.column = 1;
3683 c->button.isdefault = TRUE;
3685 s1 = ctrl_getset(es->eventbox, "x", "", "");
3686 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3687 eventlog_list_handler, P(es));
3688 c->listbox.height = 10;
3689 c->listbox.multisel = 2;
3690 c->listbox.ncols = 3;
3691 c->listbox.percentages = snewn(3, int);
3692 c->listbox.percentages[0] = 25;
3693 c->listbox.percentages[1] = 10;
3694 c->listbox.percentages[2] = 65;
3696 es->window = window = gtk_dialog_new();
3697 title = dupcat(appname, " Event Log", NULL);
3698 gtk_window_set_title(GTK_WINDOW(window), title);
3700 w0 = layout_ctrls(&es->dp, &es->scs, s0, GTK_WINDOW(window));
3701 set_dialog_action_area(GTK_DIALOG(window), w0);
3702 gtk_widget_show(w0);
3703 w1 = layout_ctrls(&es->dp, &es->scs, s1, GTK_WINDOW(window));
3704 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3705 gtk_widget_set_usize(w1, 20 +
3706 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3707 " IS QUITE LONG 'COS SSH LOG ENTRIES"
3710 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3712 gtk_widget_show(w1);
3715 es->dp.shortcuts = &es->scs;
3716 es->dp.lastfocus = NULL;
3718 es->dp.window = window;
3720 dlg_refresh(NULL, &es->dp);
3723 set_transient_window_pos(parent, window);
3724 gtk_window_set_transient_for(GTK_WINDOW(window),
3725 GTK_WINDOW(parent));
3727 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3728 gtk_widget_show(window);
3730 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3731 GTK_SIGNAL_FUNC(eventlog_destroy), es);
3732 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3733 GTK_SIGNAL_FUNC(win_key_press), &es->dp);
3734 gtk_signal_connect(GTK_OBJECT(window), "selection_get",
3735 GTK_SIGNAL_FUNC(eventlog_selection_get), es);
3736 gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event",
3737 GTK_SIGNAL_FUNC(eventlog_selection_clear), es);
3740 void *eventlogstuff_new(void)
3742 struct eventlog_stuff *es;
3743 es = snew(struct eventlog_stuff);
3744 memset(es, 0, sizeof(*es));
3748 void logevent_dlg(void *estuff, const char *string)
3750 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3755 if (es->nevents >= es->negsize) {
3757 es->events = sresize(es->events, es->negsize, char *);
3761 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3763 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3764 strcpy(es->events[es->nevents], timebuf);
3765 strcat(es->events[es->nevents], string);
3767 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3772 int askappend(void *frontend, Filename *filename,
3773 void (*callback)(void *ctx, int result), void *ctx)
3775 static const char msgtemplate[] =
3776 "The session log file \"%.*s\" already exists. "
3777 "You can overwrite it with a new session log, "
3778 "append your session log to the end of it, "
3779 "or disable session logging for this session.";
3784 message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
3785 mbtitle = dupprintf("%s Log to File", appname);
3787 mbret = messagebox(get_window(frontend), mbtitle, message,
3788 string_width("LINE OF TEXT SUITABLE FOR THE"
3789 " ASKAPPEND WIDTH"),
3790 "Overwrite", 'o', 1, 2,
3791 "Append", 'a', 0, 1,
3792 "Disable", 'd', -1, 0,