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 g_object_set_data(G_OBJECT(menuitem), "user-data",
458 GINT_TO_POINTER(id));
459 g_signal_connect(G_OBJECT(menuitem), "activate",
460 G_CALLBACK(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 g_object_set_data(G_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 g_signal_connect(G_OBJECT(listitem), "key_press_event",
530 G_CALLBACK(listitem_multi_key), uc->adj);
532 g_signal_connect(G_OBJECT(listitem), "key_press_event",
533 G_CALLBACK(listitem_single_key), uc->adj);
535 g_signal_connect(G_OBJECT(listitem), "focus_in_event",
536 G_CALLBACK(widget_focus), dp);
537 g_signal_connect(G_OBJECT(listitem), "button_press_event",
538 G_CALLBACK(listitem_button_press), dp);
539 g_signal_connect(G_OBJECT(listitem), "button_release_event",
540 G_CALLBACK(listitem_button_release), dp);
541 g_object_set_data(G_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 = G_OBJECT(g_list_nth_data(children, index));
595 g_list_free(children);
597 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item), "user-data"));
600 #if GTK_CHECK_VERSION(2,0,0)
606 path = gtk_tree_path_new_from_indices(index, -1);
607 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
608 gtk_tree_model_get(GTK_TREE_MODEL(uc->listmodel), &iter, 0, &ret, -1);
609 gtk_tree_path_free(path);
614 assert(!"We shouldn't get here");
615 return -1; /* placate dataflow analysis */
618 /* dlg_listbox_index returns <0 if no single element is selected. */
619 int dlg_listbox_index(union control *ctrl, void *dlg)
621 struct dlgparam *dp = (struct dlgparam *)dlg;
622 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
624 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
625 uc->ctrl->generic.type == CTRL_LISTBOX);
627 #if !GTK_CHECK_VERSION(2,4,0)
628 if (uc->menu || uc->list) {
630 GtkWidget *item, *activeitem;
635 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
637 activeitem = NULL; /* unnecessarily placate gcc */
639 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
641 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
642 i++, children = children->next) {
643 if (uc->menu ? activeitem == item :
644 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
651 g_list_free(children);
652 return selected < 0 ? -1 : selected;
657 * This API function already does the right thing in the
658 * case of no current selection.
660 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo));
663 #if GTK_CHECK_VERSION(2,0,0)
665 GtkTreeSelection *treesel;
672 assert(uc->treeview != NULL);
673 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
675 if (gtk_tree_selection_count_selected_rows(treesel) != 1)
678 sellist = gtk_tree_selection_get_selected_rows(treesel, &model);
680 assert(sellist && sellist->data);
681 path = sellist->data;
683 if (gtk_tree_path_get_depth(path) != 1) {
686 indices = gtk_tree_path_get_indices(path);
694 g_list_foreach(sellist, (GFunc)gtk_tree_path_free, NULL);
695 g_list_free(sellist);
700 assert(!"We shouldn't get here");
701 return -1; /* placate dataflow analysis */
704 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
706 struct dlgparam *dp = (struct dlgparam *)dlg;
707 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
709 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
710 uc->ctrl->generic.type == CTRL_LISTBOX);
712 #if !GTK_CHECK_VERSION(2,4,0)
713 if (uc->menu || uc->list) {
715 GtkWidget *item, *activeitem;
717 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
718 uc->ctrl->generic.type == CTRL_LISTBOX);
719 assert(uc->menu != NULL || uc->list != NULL);
721 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
723 item = GTK_WIDGET(g_list_nth_data(children, index));
724 g_list_free(children);
727 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
728 return item == activeitem;
730 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
736 * This API function already does the right thing in the
737 * case of no current selection.
739 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo)) == index;
742 #if GTK_CHECK_VERSION(2,0,0)
744 GtkTreeSelection *treesel;
748 assert(uc->treeview != NULL);
749 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
751 path = gtk_tree_path_new_from_indices(index, -1);
752 ret = gtk_tree_selection_path_is_selected(treesel, path);
753 gtk_tree_path_free(path);
758 assert(!"We shouldn't get here");
759 return -1; /* placate dataflow analysis */
762 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
764 struct dlgparam *dp = (struct dlgparam *)dlg;
765 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
767 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
768 uc->ctrl->generic.type == CTRL_LISTBOX);
770 #if !GTK_CHECK_VERSION(2,4,0)
772 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
778 gdouble newtop, newbot;
780 gtk_list_select_item(GTK_LIST(uc->list), index);
783 * Scroll the list box if necessary to ensure the newly
784 * selected item is visible.
786 items = gtk_container_children(GTK_CONTAINER(uc->list));
787 nitems = g_list_length(items);
789 int modified = FALSE;
791 newtop = uc->adj->lower +
792 (uc->adj->upper - uc->adj->lower) * index / nitems;
793 newbot = uc->adj->lower +
794 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
795 if (uc->adj->value > newtop) {
797 uc->adj->value = newtop;
798 } else if (uc->adj->value < newbot - uc->adj->page_size) {
800 uc->adj->value = newbot - uc->adj->page_size;
803 gtk_adjustment_value_changed(uc->adj);
809 gtk_combo_box_set_active(GTK_COMBO_BOX(uc->combo), index);
813 #if GTK_CHECK_VERSION(2,0,0)
815 GtkTreeSelection *treesel;
818 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
820 path = gtk_tree_path_new_from_indices(index, -1);
821 gtk_tree_selection_select_path(treesel, path);
822 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc->treeview),
823 path, NULL, FALSE, 0.0, 0.0);
824 gtk_tree_path_free(path);
828 assert(!"We shouldn't get here");
831 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
833 struct dlgparam *dp = (struct dlgparam *)dlg;
834 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
836 assert(uc->ctrl->generic.type == CTRL_TEXT);
837 assert(uc->text != NULL);
839 gtk_label_set_text(GTK_LABEL(uc->text), text);
842 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
844 struct dlgparam *dp = (struct dlgparam *)dlg;
845 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
847 switch (uc->ctrl->generic.type) {
849 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
850 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
853 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
854 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
857 gtk_label_set_text(GTK_LABEL(uc->label), text);
858 shortcut_highlight(uc->label, ctrl->radio.shortcut);
861 gtk_label_set_text(GTK_LABEL(uc->label), text);
862 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
864 case CTRL_FILESELECT:
865 gtk_label_set_text(GTK_LABEL(uc->label), text);
866 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
868 case CTRL_FONTSELECT:
869 gtk_label_set_text(GTK_LABEL(uc->label), text);
870 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
873 gtk_label_set_text(GTK_LABEL(uc->label), text);
874 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
877 assert(!"This shouldn't happen");
882 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
884 struct dlgparam *dp = (struct dlgparam *)dlg;
885 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
886 /* We must copy fn->path before passing it to gtk_entry_set_text.
887 * See comment in dlg_editbox_set() for the reasons. */
888 char *duppath = dupstr(fn->path);
889 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
890 assert(uc->entry != NULL);
891 gtk_entry_set_text(GTK_ENTRY(uc->entry), duppath);
895 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
897 struct dlgparam *dp = (struct dlgparam *)dlg;
898 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
899 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
900 assert(uc->entry != NULL);
901 return filename_from_str(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
904 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
906 struct dlgparam *dp = (struct dlgparam *)dlg;
907 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
908 /* We must copy fs->name before passing it to gtk_entry_set_text.
909 * See comment in dlg_editbox_set() for the reasons. */
910 char *dupname = dupstr(fs->name);
911 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
912 assert(uc->entry != NULL);
913 gtk_entry_set_text(GTK_ENTRY(uc->entry), dupname);
917 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
919 struct dlgparam *dp = (struct dlgparam *)dlg;
920 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
921 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
922 assert(uc->entry != NULL);
923 return fontspec_new(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
927 * Bracketing a large set of updates in these two functions will
928 * cause the front end (if possible) to delay updating the screen
929 * until it's all complete, thus avoiding flicker.
931 void dlg_update_start(union control *ctrl, void *dlg)
934 * Apparently we can't do this at all in GTK. GtkCList supports
935 * freeze and thaw, but not GtkList. Bah.
939 void dlg_update_done(union control *ctrl, void *dlg)
942 * Apparently we can't do this at all in GTK. GtkCList supports
943 * freeze and thaw, but not GtkList. Bah.
947 void dlg_set_focus(union control *ctrl, void *dlg)
949 struct dlgparam *dp = (struct dlgparam *)dlg;
950 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
952 switch (ctrl->generic.type) {
955 /* Check boxes and buttons get the focus _and_ get toggled. */
956 gtk_widget_grab_focus(uc->toplevel);
958 case CTRL_FILESELECT:
959 case CTRL_FONTSELECT:
962 /* Anything containing an edit box gets that focused. */
963 gtk_widget_grab_focus(uc->entry);
965 #if GTK_CHECK_VERSION(2,4,0)
966 else if (uc->combo) {
967 /* Failing that, there'll be a combo box. */
968 gtk_widget_grab_focus(uc->combo);
974 * Radio buttons: we find the currently selected button and
979 for (i = 0; i < ctrl->radio.nbuttons; i++)
980 if (gtk_toggle_button_get_active
981 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
982 gtk_widget_grab_focus(uc->buttons[i]);
987 #if !GTK_CHECK_VERSION(2,4,0)
989 gtk_widget_grab_focus(uc->optmenu);
994 gtk_widget_grab_focus(uc->combo);
998 #if !GTK_CHECK_VERSION(2,0,0)
1001 * For GTK-1 style list boxes, we tell it to focus one
1002 * of its children, which appears to do the Right
1005 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
1010 gtk_widget_grab_focus(uc->treeview);
1014 assert(!"We shouldn't get here");
1020 * During event processing, you might well want to give an error
1021 * indication to the user. dlg_beep() is a quick and easy generic
1022 * error; dlg_error() puts up a message-box or equivalent.
1024 void dlg_beep(void *dlg)
1029 static void errmsg_button_clicked(GtkButton *button, gpointer data)
1031 gtk_widget_destroy(GTK_WIDGET(data));
1034 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
1036 gint x, y, w, h, dx, dy;
1038 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
1039 gtk_widget_size_request(GTK_WIDGET(child), &req);
1041 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(parent)), &x, &y);
1042 gdk_window_get_size(gtk_widget_get_window(GTK_WIDGET(parent)), &w, &h);
1045 * One corner of the transient will be offset inwards, by 1/4
1046 * of the parent window's size, from the corresponding corner
1047 * of the parent window. The corner will be chosen so as to
1048 * place the transient closer to the centre of the screen; this
1049 * should avoid transients going off the edge of the screen on
1052 if (x + w/2 < gdk_screen_width() / 2)
1053 dx = x + w/4; /* work from left edges */
1055 dx = x + 3*w/4 - req.width; /* work from right edges */
1056 if (y + h/2 < gdk_screen_height() / 2)
1057 dy = y + h/4; /* work from top edges */
1059 dy = y + 3*h/4 - req.height; /* work from bottom edges */
1060 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
1063 void dlg_error_msg(void *dlg, const char *msg)
1065 struct dlgparam *dp = (struct dlgparam *)dlg;
1066 GtkWidget *window, *hbox, *text, *ok;
1068 window = gtk_dialog_new();
1069 text = gtk_label_new(msg);
1070 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1071 hbox = gtk_hbox_new(FALSE, 0);
1072 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1073 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
1074 hbox, FALSE, FALSE, 20);
1075 gtk_widget_show(text);
1076 gtk_widget_show(hbox);
1077 gtk_window_set_title(GTK_WINDOW(window), "Error");
1078 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1079 ok = gtk_button_new_with_label("OK");
1080 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(window))),
1081 ok, FALSE, FALSE, 0);
1082 gtk_widget_show(ok);
1083 gtk_widget_set_can_default(ok, TRUE);
1084 gtk_window_set_default(GTK_WINDOW(window), ok);
1085 g_signal_connect(G_OBJECT(ok), "clicked",
1086 G_CALLBACK(errmsg_button_clicked), window);
1087 g_signal_connect(G_OBJECT(window), "destroy",
1088 G_CALLBACK(window_destroy), NULL);
1089 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1090 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
1091 set_transient_window_pos(dp->window, window);
1092 gtk_widget_show(window);
1097 * This function signals to the front end that the dialog's
1098 * processing is completed, and passes an integer value (typically
1099 * a success status).
1101 void dlg_end(void *dlg, int value)
1103 struct dlgparam *dp = (struct dlgparam *)dlg;
1105 gtk_widget_destroy(dp->window);
1108 void dlg_refresh(union control *ctrl, void *dlg)
1110 struct dlgparam *dp = (struct dlgparam *)dlg;
1114 if (ctrl->generic.handler != NULL)
1115 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1119 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
1120 assert(uc->ctrl != NULL);
1121 if (uc->ctrl->generic.handler != NULL)
1122 uc->ctrl->generic.handler(uc->ctrl, dp,
1123 dp->data, EVENT_REFRESH);
1128 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
1130 struct dlgparam *dp = (struct dlgparam *)dlg;
1131 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
1132 GtkWidget *okbutton, *cancelbutton;
1134 GtkWidget *coloursel =
1135 gtk_color_selection_dialog_new("Select a colour");
1136 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
1137 GtkColorSelection *cs = GTK_COLOR_SELECTION
1138 (gtk_color_selection_dialog_get_color_selection(ccs));
1140 dp->coloursel_result.ok = FALSE;
1142 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
1143 #if GTK_CHECK_VERSION(2,0,0)
1144 gtk_color_selection_set_has_opacity_control(cs, FALSE);
1146 gtk_color_selection_set_opacity(cs, FALSE);
1149 #if GTK_CHECK_VERSION(2,0,0)
1152 col.red = r * 0x0101;
1153 col.green = g * 0x0101;
1154 col.blue = b * 0x0101;
1155 gtk_color_selection_set_current_color(cs, &col);
1160 cvals[0] = r / 255.0;
1161 cvals[1] = g / 255.0;
1162 cvals[2] = b / 255.0;
1163 cvals[3] = 1.0; /* fully opaque! */
1164 gtk_color_selection_set_color(cs, cvals);
1168 #if GTK_CHECK_VERSION(2,0,0)
1169 g_object_get(G_OBJECT(ccs),
1170 "ok-button", &okbutton,
1171 "cancel-button", &cancelbutton,
1172 (const char *)NULL);
1174 okbutton = ccs->ok_button;
1175 cancelbutton = ccs->cancel_button;
1177 g_object_set_data(G_OBJECT(okbutton), "user-data",
1178 (gpointer)coloursel);
1179 g_object_set_data(G_OBJECT(cancelbutton), "user-data",
1180 (gpointer)coloursel);
1181 g_object_set_data(G_OBJECT(coloursel), "user-data", (gpointer)uc);
1182 g_signal_connect(G_OBJECT(okbutton), "clicked",
1183 G_CALLBACK(coloursel_ok), (gpointer)dp);
1184 g_signal_connect(G_OBJECT(cancelbutton), "clicked",
1185 G_CALLBACK(coloursel_cancel), (gpointer)dp);
1186 g_signal_connect_swapped(G_OBJECT(okbutton), "clicked",
1187 G_CALLBACK(gtk_widget_destroy),
1188 (gpointer)coloursel);
1189 g_signal_connect_swapped(G_OBJECT(cancelbutton), "clicked",
1190 G_CALLBACK(gtk_widget_destroy),
1191 (gpointer)coloursel);
1192 gtk_widget_show(coloursel);
1195 int dlg_coloursel_results(union control *ctrl, void *dlg,
1196 int *r, int *g, int *b)
1198 struct dlgparam *dp = (struct dlgparam *)dlg;
1199 if (dp->coloursel_result.ok) {
1200 *r = dp->coloursel_result.r;
1201 *g = dp->coloursel_result.g;
1202 *b = dp->coloursel_result.b;
1208 /* ----------------------------------------------------------------------
1209 * Signal handlers while the dialog box is active.
1212 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
1215 struct dlgparam *dp = (struct dlgparam *)data;
1216 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1217 union control *focus;
1224 if (focus != dp->currfocus) {
1225 dp->lastfocus = dp->currfocus;
1226 dp->currfocus = focus;
1232 static void button_clicked(GtkButton *button, gpointer data)
1234 struct dlgparam *dp = (struct dlgparam *)data;
1235 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1236 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1239 static void button_toggled(GtkToggleButton *tb, gpointer data)
1241 struct dlgparam *dp = (struct dlgparam *)data;
1242 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
1243 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1246 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
1250 * GtkEntry has a nasty habit of eating the Return key, which
1251 * is unhelpful since it doesn't actually _do_ anything with it
1252 * (it calls gtk_widget_activate, but our edit boxes never need
1253 * activating). So I catch Return before GtkEntry sees it, and
1254 * pass it straight on to the parent widget. Effect: hitting
1255 * Return in an edit box will now activate the default button
1256 * in the dialog just like it will everywhere else.
1258 GtkWidget *parent = gtk_widget_get_parent(widget);
1259 if (event->keyval == GDK_KEY_Return && parent != NULL) {
1260 gboolean return_val;
1261 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
1262 g_signal_emit_by_name(G_OBJECT(parent), "key_press_event",
1263 event, &return_val);
1269 static void editbox_changed(GtkEditable *ed, gpointer data)
1271 struct dlgparam *dp = (struct dlgparam *)data;
1272 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1273 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1274 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1278 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1281 struct dlgparam *dp = (struct dlgparam *)data;
1282 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1283 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1287 #if !GTK_CHECK_VERSION(2,0,0)
1290 * GTK 1 list box event handlers.
1293 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1294 gpointer data, int multiple)
1296 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1298 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1299 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1300 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1301 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1303 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1304 * in a list box. So, if the list box is single-selection:
1306 * - if the list item in question isn't already selected,
1307 * we simply select it.
1308 * - otherwise, we find the next one (or next
1309 * however-far-away) in whichever direction we're going,
1311 * + in this case, we must also fiddle with the
1312 * scrollbar to ensure the newly selected item is
1315 * If it's multiple-selection, we do all of the above
1316 * except actually selecting anything, so we move the focus
1317 * and fiddle the scrollbar to follow it.
1319 GtkWidget *list = item->parent;
1321 g_signal_stop_emission_by_name(G_OBJECT(item), "key_press_event");
1324 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1325 gtk_list_select_child(GTK_LIST(list), item);
1328 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1329 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1332 (event->keyval==GDK_Page_Down ||
1333 event->keyval==GDK_KP_Page_Down ||
1334 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1337 GList *children, *chead;
1339 chead = children = gtk_container_children(GTK_CONTAINER(list));
1341 n = g_list_length(children);
1345 * Figure out how many list items to a screenful,
1346 * and adjust the step appropriately.
1348 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1349 step--; /* go by one less than that */
1353 while (children != NULL) {
1354 if (item == children->data)
1356 children = children->next;
1361 if (direction < 0 && i > 0)
1362 children = children->prev, i--;
1363 else if (direction > 0 && i < n-1)
1364 children = children->next, i++;
1368 if (children && children->data) {
1370 gtk_list_select_child(GTK_LIST(list),
1371 GTK_WIDGET(children->data));
1372 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1373 gtk_adjustment_clamp_page
1375 adj->lower + (adj->upper-adj->lower) * i / n,
1376 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1387 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1390 return listitem_key(item, event, data, FALSE);
1393 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1396 return listitem_key(item, event, data, TRUE);
1399 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1402 struct dlgparam *dp = (struct dlgparam *)data;
1403 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1404 switch (event->type) {
1406 case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1407 case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1408 case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1413 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1416 struct dlgparam *dp = (struct dlgparam *)data;
1417 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1418 if (uc->nclicks>1) {
1419 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1425 static void list_selchange(GtkList *list, gpointer data)
1427 struct dlgparam *dp = (struct dlgparam *)data;
1428 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1430 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1433 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1435 int index = dlg_listbox_index(uc->ctrl, dp);
1436 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1440 (index == 0 && direction < 0) ||
1441 (index == g_list_length(children)-1 && direction > 0)) {
1446 child = g_list_nth_data(children, index);
1447 gtk_widget_ref(child);
1448 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1449 g_list_free(children);
1452 children = g_list_append(children, child);
1453 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1454 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1455 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1458 static void draglist_up(GtkButton *button, gpointer data)
1460 struct dlgparam *dp = (struct dlgparam *)data;
1461 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1462 draglist_move(dp, uc, -1);
1465 static void draglist_down(GtkButton *button, gpointer data)
1467 struct dlgparam *dp = (struct dlgparam *)data;
1468 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1469 draglist_move(dp, uc, +1);
1472 #else /* !GTK_CHECK_VERSION(2,0,0) */
1475 * GTK 2 list box event handlers.
1478 static void listbox_doubleclick(GtkTreeView *treeview, GtkTreePath *path,
1479 GtkTreeViewColumn *column, gpointer data)
1481 struct dlgparam *dp = (struct dlgparam *)data;
1482 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(treeview));
1484 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1487 static void listbox_selchange(GtkTreeSelection *treeselection,
1490 struct dlgparam *dp = (struct dlgparam *)data;
1491 GtkTreeView *tree = gtk_tree_selection_get_tree_view(treeselection);
1492 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1494 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1497 struct draglist_valchange_ctx {
1499 struct dlgparam *dp;
1502 static gboolean draglist_valchange(gpointer data)
1504 struct draglist_valchange_ctx *ctx =
1505 (struct draglist_valchange_ctx *)data;
1507 ctx->uc->ctrl->generic.handler(ctx->uc->ctrl, ctx->dp,
1508 ctx->dp->data, EVENT_VALCHANGE);
1515 static void listbox_reorder(GtkTreeModel *treemodel, GtkTreePath *path,
1516 GtkTreeIter *iter, gpointer data)
1518 struct dlgparam *dp = (struct dlgparam *)data;
1522 if (dp->flags & FLAG_UPDATING_LISTBOX)
1523 return; /* not a user drag operation */
1525 tree = g_object_get_data(G_OBJECT(treemodel), "user-data");
1526 uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1529 * We should cause EVENT_VALCHANGE on the list box, now
1530 * that its rows have been reordered. However, the GTK 2
1531 * docs say that at the point this signal is received the
1532 * new row might not have actually been filled in yet.
1534 * (So what smegging use is it then, eh? Don't suppose it
1535 * occurred to you at any point that letting the
1536 * application know _after_ the reordering was compelete
1537 * might be helpful to someone?)
1539 * To get round this, I schedule an idle function, which I
1540 * hope won't be called until the main event loop is
1541 * re-entered after the drag-and-drop handler has finished
1542 * furtling with the list store.
1544 struct draglist_valchange_ctx *ctx =
1545 snew(struct draglist_valchange_ctx);
1548 g_idle_add(draglist_valchange, ctx);
1552 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1554 #if !GTK_CHECK_VERSION(2,4,0)
1556 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1558 struct dlgparam *dp = (struct dlgparam *)data;
1559 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1560 gpointer optmenu = g_object_get_data(G_OBJECT(menushell), "user-data");
1561 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1562 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1567 static void droplist_selchange(GtkComboBox *combo, gpointer data)
1569 struct dlgparam *dp = (struct dlgparam *)data;
1570 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(combo));
1572 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1575 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1577 static void filesel_ok(GtkButton *button, gpointer data)
1579 /* struct dlgparam *dp = (struct dlgparam *)data; */
1580 gpointer filesel = g_object_get_data(G_OBJECT(button), "user-data");
1581 struct uctrl *uc = g_object_get_data(G_OBJECT(filesel), "user-data");
1582 const char *name = gtk_file_selection_get_filename
1583 (GTK_FILE_SELECTION(filesel));
1584 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1587 static void fontsel_ok(GtkButton *button, gpointer data)
1589 /* struct dlgparam *dp = (struct dlgparam *)data; */
1591 #if !GTK_CHECK_VERSION(2,0,0)
1593 gpointer fontsel = g_object_get_data(G_OBJECT(button), "user-data");
1594 struct uctrl *uc = g_object_get_data(G_OBJECT(fontsel), "user-data");
1595 const char *name = gtk_font_selection_dialog_get_font_name
1596 (GTK_FONT_SELECTION_DIALOG(fontsel));
1597 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1601 unifontsel *fontsel = (unifontsel *)g_object_get_data
1602 (G_OBJECT(button), "user-data");
1603 struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1604 char *name = unifontsel_get_name(fontsel);
1605 assert(name); /* should always be ok after OK pressed */
1606 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1612 static void coloursel_ok(GtkButton *button, gpointer data)
1614 struct dlgparam *dp = (struct dlgparam *)data;
1615 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1616 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1617 GtkColorSelection *cs = GTK_COLOR_SELECTION
1618 (gtk_color_selection_dialog_get_color_selection
1619 (GTK_COLOR_SELECTION_DIALOG(coloursel)));
1620 #if GTK_CHECK_VERSION(2,0,0)
1623 gtk_color_selection_get_current_color(cs, &col);
1624 dp->coloursel_result.r = col.red / 0x0100;
1625 dp->coloursel_result.g = col.green / 0x0100;
1626 dp->coloursel_result.b = col.blue / 0x0100;
1631 gtk_color_selection_get_color(cs, cvals);
1632 dp->coloursel_result.r = (int) (255 * cvals[0]);
1633 dp->coloursel_result.g = (int) (255 * cvals[1]);
1634 dp->coloursel_result.b = (int) (255 * cvals[2]);
1637 dp->coloursel_result.ok = TRUE;
1638 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1641 static void coloursel_cancel(GtkButton *button, gpointer data)
1643 struct dlgparam *dp = (struct dlgparam *)data;
1644 gpointer coloursel = g_object_get_data(G_OBJECT(button), "user-data");
1645 struct uctrl *uc = g_object_get_data(G_OBJECT(coloursel), "user-data");
1646 dp->coloursel_result.ok = FALSE;
1647 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1650 static void filefont_clicked(GtkButton *button, gpointer data)
1652 struct dlgparam *dp = (struct dlgparam *)data;
1653 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1655 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1656 GtkWidget *filesel =
1657 gtk_file_selection_new(uc->ctrl->fileselect.title);
1658 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1660 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1662 g_object_set_data(G_OBJECT(filesel), "user-data", (gpointer)uc);
1664 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1665 G_CALLBACK(filesel_ok), (gpointer)dp);
1666 g_signal_connect_swapped
1667 (G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1668 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1669 g_signal_connect_swapped
1670 (G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1671 G_CALLBACK(gtk_widget_destroy), (gpointer)filesel);
1672 gtk_widget_show(filesel);
1675 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1676 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1678 #if !GTK_CHECK_VERSION(2,0,0)
1681 * Use the GTK 1 standard font selector.
1684 gchar *spacings[] = { "c", "m", NULL };
1685 GtkWidget *fontsel =
1686 gtk_font_selection_dialog_new("Select a font");
1687 gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1688 gtk_font_selection_dialog_set_filter
1689 (GTK_FONT_SELECTION_DIALOG(fontsel),
1690 GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1691 NULL, NULL, NULL, NULL, spacings, NULL);
1692 if (!gtk_font_selection_dialog_set_font_name
1693 (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1695 * If the font name wasn't found as it was, try opening
1696 * it and extracting its FONT property. This should
1697 * have the effect of mapping short aliases into true
1700 GdkFont *font = gdk_font_load(fontname);
1702 XFontStruct *xfs = GDK_FONT_XFONT(font);
1703 Display *disp = GDK_FONT_XDISPLAY(font);
1704 Atom fontprop = XInternAtom(disp, "FONT", False);
1707 if (XGetFontProperty(xfs, fontprop, &ret)) {
1708 char *name = XGetAtomName(disp, (Atom)ret);
1710 gtk_font_selection_dialog_set_font_name
1711 (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1713 gdk_font_unref(font);
1717 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1718 "user-data", (gpointer)fontsel);
1719 g_object_set_data(G_OBJECT(fontsel), "user-data", (gpointer)uc);
1721 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1722 "clicked", G_CALLBACK(fontsel_ok), (gpointer)dp);
1723 g_signal_connect_swapped
1724 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1725 "clicked", G_CALLBACK(gtk_widget_destroy),
1727 g_signal_connect_swapped
1728 (G_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1729 "clicked", G_CALLBACK(gtk_widget_destroy),
1731 gtk_widget_show(fontsel);
1733 #else /* !GTK_CHECK_VERSION(2,0,0) */
1736 * Use the unifontsel code provided in gtkfont.c.
1739 unifontsel *fontsel = unifontsel_new("Select a font");
1741 gtk_window_set_modal(fontsel->window, TRUE);
1742 unifontsel_set_name(fontsel, fontname);
1744 g_object_set_data(G_OBJECT(fontsel->ok_button),
1745 "user-data", (gpointer)fontsel);
1746 fontsel->user_data = uc;
1747 g_signal_connect(G_OBJECT(fontsel->ok_button), "clicked",
1748 G_CALLBACK(fontsel_ok), (gpointer)dp);
1749 g_signal_connect_swapped(G_OBJECT(fontsel->ok_button), "clicked",
1750 G_CALLBACK(unifontsel_destroy),
1752 g_signal_connect_swapped(G_OBJECT(fontsel->cancel_button),"clicked",
1753 G_CALLBACK(unifontsel_destroy),
1756 gtk_widget_show(GTK_WIDGET(fontsel->window));
1758 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1763 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1766 struct dlgparam *dp = (struct dlgparam *)data;
1767 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1769 gtk_widget_set_usize(uc->text, alloc->width, -1);
1770 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1771 g_signal_handler_disconnect(G_OBJECT(uc->text), uc->textsig);
1774 /* ----------------------------------------------------------------------
1775 * This function does the main layout work: it reads a controlset,
1776 * it creates the relevant GTK controls, and returns a GtkWidget
1777 * containing the result. (This widget might be a title of some
1778 * sort, it might be a Columns containing many controls, or it
1779 * might be a GtkFrame containing a Columns; whatever it is, it's
1780 * definitely a GtkWidget and should probably be added to a
1783 * `win' is required for setting the default button. If it is
1784 * non-NULL, all buttons created will be default-capable (so they
1785 * have extra space round them for the default highlight).
1787 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1788 struct controlset *s, GtkWindow *win)
1794 if (!s->boxname && s->boxtitle) {
1795 /* This controlset is a panel title. */
1796 return gtk_label_new(s->boxtitle);
1800 * Otherwise, we expect to be laying out actual controls, so
1801 * we'll start by creating a Columns for the purpose.
1803 cols = COLUMNS(columns_new(4));
1804 ret = GTK_WIDGET(cols);
1805 gtk_widget_show(ret);
1808 * Create a containing frame if we have a box name.
1811 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1812 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1813 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1814 gtk_widget_show(ret);
1818 * Now iterate through the controls themselves, create them,
1819 * and add them to the Columns.
1821 for (i = 0; i < s->ncontrols; i++) {
1822 union control *ctrl = s->ctrls[i];
1825 GtkWidget *w = NULL;
1827 switch (ctrl->generic.type) {
1830 static const int simplecols[1] = { 100 };
1831 columns_set_cols(cols, ctrl->columns.ncols,
1832 (ctrl->columns.percentages ?
1833 ctrl->columns.percentages : simplecols));
1835 continue; /* no actual control created */
1838 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1840 columns_taborder_last(cols, uc->toplevel);
1842 continue; /* no actual control created */
1845 uc = snew(struct uctrl);
1849 #if !GTK_CHECK_VERSION(2,4,0)
1850 uc->list = uc->menu = uc->optmenu = NULL;
1854 #if GTK_CHECK_VERSION(2,0,0)
1855 uc->treeview = NULL;
1856 uc->listmodel = NULL;
1858 uc->button = uc->text = NULL;
1862 switch (ctrl->generic.type) {
1864 w = gtk_button_new_with_label(ctrl->generic.label);
1866 gtk_widget_set_can_default(w, TRUE);
1867 if (ctrl->button.isdefault)
1868 gtk_window_set_default(win, w);
1869 if (ctrl->button.iscancel)
1870 dp->cancelbutton = w;
1872 g_signal_connect(G_OBJECT(w), "clicked",
1873 G_CALLBACK(button_clicked), dp);
1874 g_signal_connect(G_OBJECT(w), "focus_in_event",
1875 G_CALLBACK(widget_focus), dp);
1876 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1877 ctrl->button.shortcut, SHORTCUT_UCTRL, uc);
1880 w = gtk_check_button_new_with_label(ctrl->generic.label);
1881 g_signal_connect(G_OBJECT(w), "toggled",
1882 G_CALLBACK(button_toggled), dp);
1883 g_signal_connect(G_OBJECT(w), "focus_in_event",
1884 G_CALLBACK(widget_focus), dp);
1885 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(w)),
1886 ctrl->checkbox.shortcut, SHORTCUT_UCTRL, uc);
1891 * Radio buttons get to go inside their own Columns, no
1895 gint i, *percentages;
1899 if (ctrl->generic.label) {
1900 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1901 columns_add(COLUMNS(w), label, 0, 1);
1902 columns_force_left_align(COLUMNS(w), label);
1903 gtk_widget_show(label);
1904 shortcut_add(scs, label, ctrl->radio.shortcut,
1905 SHORTCUT_UCTRL, uc);
1908 percentages = g_new(gint, ctrl->radio.ncolumns);
1909 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1911 ((100 * (i+1) / ctrl->radio.ncolumns) -
1912 100 * i / ctrl->radio.ncolumns);
1914 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1916 g_free(percentages);
1919 uc->nbuttons = ctrl->radio.nbuttons;
1920 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1922 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1926 b = (gtk_radio_button_new_with_label
1927 (group, ctrl->radio.buttons[i]));
1929 group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
1930 colstart = i % ctrl->radio.ncolumns;
1931 columns_add(COLUMNS(w), b, colstart,
1932 (i == ctrl->radio.nbuttons-1 ?
1933 ctrl->radio.ncolumns - colstart : 1));
1934 columns_force_left_align(COLUMNS(w), b);
1936 g_signal_connect(G_OBJECT(b), "toggled",
1937 G_CALLBACK(button_toggled), dp);
1938 g_signal_connect(G_OBJECT(b), "focus_in_event",
1939 G_CALLBACK(widget_focus), dp);
1940 if (ctrl->radio.shortcuts) {
1941 shortcut_add(scs, gtk_bin_get_child(GTK_BIN(b)),
1942 ctrl->radio.shortcuts[i],
1943 SHORTCUT_UCTRL, uc);
1951 GtkWidget *signalobject;
1953 if (ctrl->editbox.has_list) {
1954 #if !GTK_CHECK_VERSION(2,4,0)
1958 w = gtk_combo_new();
1959 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1960 uc->entry = GTK_COMBO(w)->entry;
1961 uc->list = GTK_COMBO(w)->list;
1962 signalobject = uc->entry;
1967 uc->listmodel = gtk_list_store_new(2, G_TYPE_INT,
1969 w = gtk_combo_box_entry_new_with_model
1970 (GTK_TREE_MODEL(uc->listmodel), 1);
1971 /* We cannot support password combo boxes. */
1972 assert(!ctrl->editbox.password);
1974 signalobject = uc->combo;
1977 w = gtk_entry_new();
1978 if (ctrl->editbox.password)
1979 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
1984 g_signal_connect(G_OBJECT(signalobject), "changed",
1985 G_CALLBACK(editbox_changed), dp);
1986 g_signal_connect(G_OBJECT(signalobject), "key_press_event",
1987 G_CALLBACK(editbox_key), dp);
1988 g_signal_connect(G_OBJECT(signalobject), "focus_in_event",
1989 G_CALLBACK(widget_focus), dp);
1990 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
1991 G_CALLBACK(editbox_lostfocus), dp);
1992 g_signal_connect(G_OBJECT(signalobject), "focus_out_event",
1993 G_CALLBACK(editbox_lostfocus), dp);
1995 * Edit boxes, for some strange reason, have a minimum
1996 * width of 150 in GTK 1.2. We don't want this - we'd
1997 * rather the edit boxes acquired their natural width
1998 * from the column layout of the rest of the box.
2000 * Also, while we're here, we'll squirrel away the
2001 * edit box height so we can use that to centre its
2002 * label vertically beside it.
2004 gtk_widget_size_request(w, &req);
2005 gtk_widget_set_usize(w, 10, req.height);
2007 if (ctrl->generic.label) {
2008 GtkWidget *label, *container;
2010 label = gtk_label_new(ctrl->generic.label);
2012 shortcut_add(scs, label, ctrl->editbox.shortcut,
2013 SHORTCUT_FOCUS, uc->entry);
2015 container = columns_new(4);
2016 if (ctrl->editbox.percentwidth == 100) {
2017 columns_add(COLUMNS(container), label, 0, 1);
2018 columns_force_left_align(COLUMNS(container), label);
2019 columns_add(COLUMNS(container), w, 0, 1);
2021 gint percentages[2];
2022 percentages[1] = ctrl->editbox.percentwidth;
2023 percentages[0] = 100 - ctrl->editbox.percentwidth;
2024 columns_set_cols(COLUMNS(container), 2, percentages);
2025 columns_add(COLUMNS(container), label, 0, 1);
2026 columns_force_left_align(COLUMNS(container), label);
2027 columns_add(COLUMNS(container), w, 1, 1);
2028 /* Centre the label vertically. */
2029 gtk_widget_set_usize(label, -1, req.height);
2030 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2032 gtk_widget_show(label);
2040 case CTRL_FILESELECT:
2041 case CTRL_FONTSELECT:
2045 const char *browsebtn =
2046 (ctrl->generic.type == CTRL_FILESELECT ?
2047 "Browse..." : "Change...");
2049 gint percentages[] = { 75, 25 };
2051 columns_set_cols(COLUMNS(w), 2, percentages);
2053 if (ctrl->generic.label) {
2054 ww = gtk_label_new(ctrl->generic.label);
2055 columns_add(COLUMNS(w), ww, 0, 2);
2056 columns_force_left_align(COLUMNS(w), ww);
2057 gtk_widget_show(ww);
2058 shortcut_add(scs, ww,
2059 (ctrl->generic.type == CTRL_FILESELECT ?
2060 ctrl->fileselect.shortcut :
2061 ctrl->fontselect.shortcut),
2062 SHORTCUT_UCTRL, uc);
2066 uc->entry = ww = gtk_entry_new();
2067 gtk_widget_size_request(ww, &req);
2068 gtk_widget_set_usize(ww, 10, req.height);
2069 columns_add(COLUMNS(w), ww, 0, 1);
2070 gtk_widget_show(ww);
2072 uc->button = ww = gtk_button_new_with_label(browsebtn);
2073 columns_add(COLUMNS(w), ww, 1, 1);
2074 gtk_widget_show(ww);
2076 g_signal_connect(G_OBJECT(uc->entry), "key_press_event",
2077 G_CALLBACK(editbox_key), dp);
2079 g_signal_connect(G_OBJECT(uc->entry), "changed",
2080 G_CALLBACK(editbox_changed), dp);
2081 g_signal_connect(G_OBJECT(uc->entry), "focus_in_event",
2082 G_CALLBACK(widget_focus), dp);
2083 g_signal_connect(G_OBJECT(uc->button), "focus_in_event",
2084 G_CALLBACK(widget_focus), dp);
2085 g_signal_connect(G_OBJECT(ww), "clicked",
2086 G_CALLBACK(filefont_clicked), dp);
2091 #if GTK_CHECK_VERSION(2,0,0)
2093 * First construct the list data store, with the right
2094 * number of columns.
2096 # if !GTK_CHECK_VERSION(2,4,0)
2097 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2098 * because combo boxes are still done the old GTK1 way.) */
2099 if (ctrl->listbox.height > 0)
2106 cols = ctrl->listbox.ncols;
2107 cols = cols ? cols : 1;
2108 types = snewn(1 + cols, GType);
2110 types[0] = G_TYPE_INT;
2111 for (i = 0; i < cols; i++)
2112 types[i+1] = G_TYPE_STRING;
2114 uc->listmodel = gtk_list_store_newv(1 + cols, types);
2121 * See if it's a drop-down list (non-editable combo
2124 if (ctrl->listbox.height == 0) {
2125 #if !GTK_CHECK_VERSION(2,4,0)
2127 * GTK1 and early-GTK2 option-menu style of
2130 uc->optmenu = w = gtk_option_menu_new();
2131 uc->menu = gtk_menu_new();
2132 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
2133 g_object_set_data(G_OBJECT(uc->menu), "user-data",
2134 (gpointer)uc->optmenu);
2135 g_signal_connect(G_OBJECT(uc->optmenu), "focus_in_event",
2136 G_CALLBACK(widget_focus), dp);
2139 * Late-GTK2 style using a GtkComboBox.
2141 GtkCellRenderer *cr;
2144 * Create a non-editable GtkComboBox (that is, not
2145 * its subclass GtkComboBoxEntry).
2147 w = gtk_combo_box_new_with_model
2148 (GTK_TREE_MODEL(uc->listmodel));
2152 * Tell it how to render a list item (i.e. which
2153 * column to look at in the list model).
2155 cr = gtk_cell_renderer_text_new();
2156 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), cr, TRUE);
2157 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), cr,
2161 * And tell it to notify us when the selection
2164 g_signal_connect(G_OBJECT(w), "changed",
2165 G_CALLBACK(droplist_selchange), dp);
2168 #if !GTK_CHECK_VERSION(2,0,0)
2170 * GTK1-style full list box.
2172 uc->list = gtk_list_new();
2173 if (ctrl->listbox.multisel == 2) {
2174 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2175 GTK_SELECTION_EXTENDED);
2176 } else if (ctrl->listbox.multisel == 1) {
2177 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2178 GTK_SELECTION_MULTIPLE);
2180 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2181 GTK_SELECTION_SINGLE);
2183 w = gtk_scrolled_window_new(NULL, NULL);
2184 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
2186 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
2188 GTK_POLICY_AUTOMATIC);
2189 uc->adj = gtk_scrolled_window_get_vadjustment
2190 (GTK_SCROLLED_WINDOW(w));
2192 gtk_widget_show(uc->list);
2193 g_signal_connect(G_OBJECT(uc->list), "selection-changed",
2194 G_CALLBACK(list_selchange), dp);
2195 g_signal_connect(G_OBJECT(uc->list), "focus_in_event",
2196 G_CALLBACK(widget_focus), dp);
2199 * Adjust the height of the scrolled window to the
2200 * minimum given by the height parameter.
2202 * This piece of guesswork is a horrid hack based
2203 * on looking inside the GTK 1.2 sources
2204 * (specifically gtkviewport.c, which appears to be
2205 * the widget which provides the border around the
2206 * scrolling area). Anyone lets me know how I can
2207 * do this in a way which isn't at risk from GTK
2208 * upgrades, I'd be grateful.
2212 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
2213 gtk_widget_set_usize(w, 10,
2214 2*edge + (ctrl->listbox.height *
2215 get_listitemheight(w)));
2218 if (ctrl->listbox.draglist) {
2220 * GTK doesn't appear to make it easy to
2221 * implement a proper draggable list; so
2222 * instead I'm just going to have to put an Up
2223 * and a Down button to the right of the actual
2224 * list box. Ah well.
2226 GtkWidget *cols, *button;
2227 static const gint percentages[2] = { 80, 20 };
2229 cols = columns_new(4);
2230 columns_set_cols(COLUMNS(cols), 2, percentages);
2231 columns_add(COLUMNS(cols), w, 0, 1);
2233 button = gtk_button_new_with_label("Up");
2234 columns_add(COLUMNS(cols), button, 1, 1);
2235 gtk_widget_show(button);
2236 g_signal_connect(G_OBJECT(button), "clicked",
2237 G_CALLBACK(draglist_up), dp);
2238 g_signal_connect(G_OBJECT(button), "focus_in_event",
2239 G_CALLBACK(widget_focus), dp);
2240 button = gtk_button_new_with_label("Down");
2241 columns_add(COLUMNS(cols), button, 1, 1);
2242 gtk_widget_show(button);
2243 g_signal_connect(G_OBJECT(button), "clicked",
2244 G_CALLBACK(draglist_down), dp);
2245 g_signal_connect(G_OBJECT(button), "focus_in_event",
2246 G_CALLBACK(widget_focus), dp);
2252 * GTK2 treeview-based full list box.
2254 GtkTreeSelection *sel;
2257 * Create the list box itself, its columns, and
2258 * its containing scrolled window.
2260 w = gtk_tree_view_new_with_model
2261 (GTK_TREE_MODEL(uc->listmodel));
2262 g_object_set_data(G_OBJECT(uc->listmodel), "user-data",
2264 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2265 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
2266 gtk_tree_selection_set_mode
2267 (sel, ctrl->listbox.multisel ? GTK_SELECTION_MULTIPLE :
2268 GTK_SELECTION_SINGLE);
2270 g_signal_connect(G_OBJECT(w), "row-activated",
2271 G_CALLBACK(listbox_doubleclick), dp);
2272 g_signal_connect(G_OBJECT(sel), "changed",
2273 G_CALLBACK(listbox_selchange), dp);
2275 if (ctrl->listbox.draglist) {
2276 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w), TRUE);
2277 g_signal_connect(G_OBJECT(uc->listmodel), "row-inserted",
2278 G_CALLBACK(listbox_reorder), dp);
2285 cols = ctrl->listbox.ncols;
2286 cols = cols ? cols : 1;
2287 for (i = 0; i < cols; i++) {
2288 GtkTreeViewColumn *column;
2289 GtkCellRenderer *cellrend;
2291 * It appears that GTK 2 doesn't leave us any
2292 * particularly sensible way to honour the
2293 * "percentages" specification in the ctrl
2296 cellrend = gtk_cell_renderer_text_new();
2297 if (!ctrl->listbox.hscroll) {
2298 g_object_set(G_OBJECT(cellrend),
2299 "ellipsize", PANGO_ELLIPSIZE_END,
2300 "ellipsize-set", TRUE,
2303 column = gtk_tree_view_column_new_with_attributes
2304 ("heading", cellrend, "text", i+1, (char *)NULL);
2305 gtk_tree_view_column_set_sizing
2306 (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2307 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2314 scroll = gtk_scrolled_window_new(NULL, NULL);
2315 gtk_scrolled_window_set_shadow_type
2316 (GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
2318 gtk_container_add(GTK_CONTAINER(scroll), w);
2319 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2320 GTK_POLICY_AUTOMATIC,
2322 gtk_widget_set_size_request
2324 ctrl->listbox.height * get_listitemheight(w));
2331 if (ctrl->generic.label) {
2332 GtkWidget *label, *container;
2335 label = gtk_label_new(ctrl->generic.label);
2337 shortcut_add(scs, label, ctrl->listbox.shortcut,
2340 container = columns_new(4);
2341 if (ctrl->listbox.percentwidth == 100) {
2342 columns_add(COLUMNS(container), label, 0, 1);
2343 columns_force_left_align(COLUMNS(container), label);
2344 columns_add(COLUMNS(container), w, 0, 1);
2346 gint percentages[2];
2347 percentages[1] = ctrl->listbox.percentwidth;
2348 percentages[0] = 100 - ctrl->listbox.percentwidth;
2349 columns_set_cols(COLUMNS(container), 2, percentages);
2350 columns_add(COLUMNS(container), label, 0, 1);
2351 columns_force_left_align(COLUMNS(container), label);
2352 columns_add(COLUMNS(container), w, 1, 1);
2353 /* Centre the label vertically. */
2354 gtk_widget_size_request(w, &req);
2355 gtk_widget_set_usize(label, -1, req.height);
2356 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2358 gtk_widget_show(label);
2368 * Wrapping text widgets don't sit well with the GTK
2369 * layout model, in which widgets state a minimum size
2370 * and the whole window then adjusts to the smallest
2371 * size it can sensibly take given its contents. A
2372 * wrapping text widget _has_ no clear minimum size;
2373 * instead it has a range of possibilities. It can be
2374 * one line deep but 2000 wide, or two lines deep and
2375 * 1000 pixels, or three by 867, or four by 500 and so
2376 * on. It can be as short as you like provided you
2377 * don't mind it being wide, or as narrow as you like
2378 * provided you don't mind it being tall.
2380 * Therefore, it fits very badly into the layout model.
2381 * Hence the only thing to do is pick a width and let
2382 * it choose its own number of lines. To do this I'm
2383 * going to cheat a little. All new wrapping text
2384 * widgets will be created with a minimal text content
2385 * "X"; then, after the rest of the dialog box is set
2386 * up and its size calculated, the text widgets will be
2387 * told their width and given their real text, which
2388 * will cause the size to be recomputed in the y
2389 * direction (because many of them will expand to more
2392 uc->text = w = gtk_label_new("X");
2393 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
2394 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
2396 g_signal_connect(G_OBJECT(w), "size-allocate",
2397 G_CALLBACK(label_sizealloc), dp);
2403 columns_add(cols, w,
2404 COLUMN_START(ctrl->generic.column),
2405 COLUMN_SPAN(ctrl->generic.column));
2407 columns_force_left_align(cols, w);
2411 dlg_add_uctrl(dp, uc);
2418 struct dlgparam *dp;
2419 GtkNotebook *panels;
2421 #if !GTK_CHECK_VERSION(2,0,0)
2422 GtkWidget *treeitem;
2425 GtkTreePath *treepath;
2427 struct Shortcuts shortcuts;
2430 #if GTK_CHECK_VERSION(2,0,0)
2431 static void treeselection_changed(GtkTreeSelection *treeselection,
2434 struct selparam *sps = (struct selparam *)data, *sp;
2435 GtkTreeModel *treemodel;
2436 GtkTreeIter treeiter;
2440 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2443 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
2446 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2447 gtk_notebook_set_page(sp->panels, page_num);
2449 dlg_refresh(NULL, sp->dp);
2451 sp->dp->shortcuts = &sp->shortcuts;
2454 static void treeitem_sel(GtkItem *item, gpointer data)
2456 struct selparam *sp = (struct selparam *)data;
2459 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2460 gtk_notebook_set_page(sp->panels, page_num);
2462 dlg_refresh(NULL, sp->dp);
2464 sp->dp->shortcuts = &sp->shortcuts;
2465 sp->dp->currtreeitem = sp->treeitem;
2469 static void window_destroy(GtkWidget *widget, gpointer data)
2474 #if !GTK_CHECK_VERSION(2,0,0)
2475 static int tree_grab_focus(struct dlgparam *dp)
2480 * See if any of the treeitems has the focus.
2483 for (i = 0; i < dp->ntreeitems; i++)
2484 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
2492 gtk_widget_grab_focus(dp->currtreeitem);
2497 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
2500 struct dlgparam *dp = (struct dlgparam *)data;
2502 g_signal_stop_emission_by_name(G_OBJECT(container), "focus");
2504 * If there's a focused treeitem, we return FALSE to cause the
2505 * focus to move on to some totally other control. If not, we
2506 * focus the selected one.
2508 return tree_grab_focus(dp);
2512 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2514 struct dlgparam *dp = (struct dlgparam *)data;
2516 if (event->keyval == GDK_KEY_Escape && dp->cancelbutton) {
2517 g_signal_emit_by_name(G_OBJECT(dp->cancelbutton), "clicked");
2521 if ((event->state & GDK_MOD1_MASK) &&
2522 (unsigned char)event->string[0] > 0 &&
2523 (unsigned char)event->string[0] <= 127) {
2524 int schr = (unsigned char)event->string[0];
2525 struct Shortcut *sc = &dp->shortcuts->sc[schr];
2527 switch (sc->action) {
2529 #if GTK_CHECK_VERSION(2,0,0)
2530 gtk_widget_grab_focus(sc->widget);
2532 tree_grab_focus(dp);
2535 case SHORTCUT_FOCUS:
2536 gtk_widget_grab_focus(sc->widget);
2538 case SHORTCUT_UCTRL:
2540 * We must do something sensible with a uctrl.
2541 * Precisely what this is depends on the type of
2544 switch (sc->uc->ctrl->generic.type) {
2547 /* Check boxes and buttons get the focus _and_ get toggled. */
2548 gtk_widget_grab_focus(sc->uc->toplevel);
2549 g_signal_emit_by_name(G_OBJECT(sc->uc->toplevel), "clicked");
2551 case CTRL_FILESELECT:
2552 case CTRL_FONTSELECT:
2553 /* File/font selectors have their buttons pressed (ooer),
2554 * and focus transferred to the edit box. */
2555 g_signal_emit_by_name(G_OBJECT(sc->uc->button), "clicked");
2556 gtk_widget_grab_focus(sc->uc->entry);
2560 * Radio buttons are fun, because they have
2561 * multiple shortcuts. We must find whether the
2562 * activated shortcut is the shortcut for the whole
2563 * group, or for a particular button. In the former
2564 * case, we find the currently selected button and
2565 * focus it; in the latter, we focus-and-click the
2566 * button whose shortcut was pressed.
2568 if (schr == sc->uc->ctrl->radio.shortcut) {
2570 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2571 if (gtk_toggle_button_get_active
2572 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
2573 gtk_widget_grab_focus(sc->uc->buttons[i]);
2575 } else if (sc->uc->ctrl->radio.shortcuts) {
2577 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2578 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
2579 gtk_widget_grab_focus(sc->uc->buttons[i]);
2580 g_signal_emit_by_name
2581 (G_OBJECT(sc->uc->buttons[i]), "clicked");
2587 #if !GTK_CHECK_VERSION(2,4,0)
2588 if (sc->uc->optmenu) {
2592 gtk_widget_grab_focus(sc->uc->optmenu);
2593 /* Option menus don't work using the "clicked" signal.
2594 * We need to manufacture a button press event :-/ */
2595 bev.type = GDK_BUTTON_PRESS;
2597 g_signal_emit_by_name(G_OBJECT(sc->uc->optmenu),
2598 "button_press_event",
2603 if (sc->uc->combo) {
2604 gtk_widget_grab_focus(sc->uc->combo);
2605 gtk_combo_box_popup(GTK_COMBO_BOX(sc->uc->combo));
2609 #if !GTK_CHECK_VERSION(2,0,0)
2612 * For GTK-1 style list boxes, we tell it to
2613 * focus one of its children, which appears to
2614 * do the Right Thing.
2616 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
2617 GTK_DIR_TAB_FORWARD);
2621 if (sc->uc->treeview) {
2622 gtk_widget_grab_focus(sc->uc->treeview);
2626 assert(!"We shouldn't get here");
2636 #if !GTK_CHECK_VERSION(2,0,0)
2637 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2639 struct dlgparam *dp = (struct dlgparam *)data;
2641 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2642 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2644 for (i = 0; i < dp->ntreeitems; i++)
2645 if (widget == dp->treeitems[i])
2647 if (i < dp->ntreeitems) {
2648 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2655 if (i < 0 || i >= dp->ntreeitems)
2656 break; /* nothing in that dir to select */
2658 * Determine if this tree item is visible.
2661 GtkWidget *w = dp->treeitems[i];
2663 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2664 if (!GTK_WIDGET_VISIBLE(w)) {
2671 j = i; /* got one */
2677 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2679 g_signal_emit_by_name(G_OBJECT(dp->treeitems[j]), "toggle");
2680 gtk_widget_grab_focus(dp->treeitems[j]);
2686 * It's nice for Left and Right to expand and collapse tree
2689 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2690 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2691 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2694 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2695 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
2696 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2704 static void shortcut_highlight(GtkWidget *labelw, int chr)
2706 GtkLabel *label = GTK_LABEL(labelw);
2707 gchar *currstr, *pattern;
2710 gtk_label_get(label, &currstr);
2711 for (i = 0; currstr[i]; i++)
2712 if (tolower((unsigned char)currstr[i]) == chr) {
2715 pattern = dupprintf("%*s_", i, "");
2717 gtk_widget_size_request(GTK_WIDGET(label), &req);
2718 gtk_label_set_pattern(label, pattern);
2719 gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
2726 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2727 int chr, int action, void *ptr)
2729 if (chr == NO_SHORTCUT)
2732 chr = tolower((unsigned char)chr);
2734 assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2736 scs->sc[chr].action = action;
2738 if (action == SHORTCUT_FOCUS) {
2739 scs->sc[chr].uc = NULL;
2740 scs->sc[chr].widget = (GtkWidget *)ptr;
2742 scs->sc[chr].widget = NULL;
2743 scs->sc[chr].uc = (struct uctrl *)ptr;
2746 shortcut_highlight(labelw, chr);
2749 int get_listitemheight(GtkWidget *w)
2751 #if !GTK_CHECK_VERSION(2,0,0)
2752 GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2754 gtk_widget_size_request(listitem, &req);
2755 g_object_ref_sink(G_OBJECT(listitem));
2759 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
2760 gtk_cell_renderer_get_size(cr, w, NULL, NULL, NULL, NULL, &height);
2761 g_object_ref(G_OBJECT(cr));
2762 g_object_ref_sink(G_OBJECT(cr));
2763 g_object_unref(G_OBJECT(cr));
2768 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2770 #if !GTK_CHECK_VERSION(2,0,0)
2773 * In GTK 1, laying out the buttons at the bottom of the
2774 * configuration box is nice and easy, because a GtkDialog's
2775 * action_area is a GtkHBox which stretches to cover the full
2776 * width of the dialog. So we just put our Columns widget
2777 * straight into that hbox, and it ends up just where we want
2780 gtk_box_pack_start(GTK_BOX(dlg->action_area), w, TRUE, TRUE, 0);
2784 * In GTK 2, the action area is now a GtkHButtonBox and its
2785 * layout behaviour seems to be different: it doesn't stretch
2786 * to cover the full width of the window, but instead finds its
2787 * own preferred width and right-aligns that within the window.
2788 * This isn't what we want, because we have both left-aligned
2789 * and right-aligned buttons coming out of the above call to
2790 * layout_ctrls(), and right-aligning the whole thing will
2791 * result in the former being centred and looking weird.
2793 * So instead we abandon the dialog's action area completely:
2794 * we gtk_widget_hide() it in the below code, and we also call
2795 * gtk_dialog_set_has_separator() to remove the separator above
2796 * it. We then insert our own action area into the end of the
2797 * dialog's main vbox, and add our own separator above that.
2799 * (Ideally, if we were a native GTK app, we would use the
2800 * GtkHButtonBox's _own_ innate ability to support one set of
2801 * buttons being right-aligned and one left-aligned. But to do
2802 * that here, we would have to either (a) pick apart our cross-
2803 * platform layout structures and treat them specially for this
2804 * particular set of controls, which would be painful, or else
2805 * (b) develop a special and simpler cross-platform
2806 * representation for these particular controls, and introduce
2807 * special-case code into all the _other_ platforms to handle
2808 * it. Neither appeals. Therefore, I regretfully discard the
2809 * GTKHButtonBox and go it alone.)
2813 align = gtk_alignment_new(0, 0, 1, 1);
2814 gtk_container_add(GTK_CONTAINER(align), w);
2816 * The purpose of this GtkAlignment is to provide padding
2817 * around the buttons. The padding we use is twice the padding
2818 * used in our GtkColumns, because we nest two GtkColumns most
2819 * of the time (one separating the tree view from the main
2820 * controls, and another for the main controls themselves).
2822 #if GTK_CHECK_VERSION(2,4,0)
2823 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2825 gtk_widget_show(align);
2826 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2827 align, FALSE, TRUE, 0);
2828 w = gtk_hseparator_new();
2829 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(dlg)),
2832 gtk_widget_hide(gtk_dialog_get_action_area(dlg));
2833 gtk_dialog_set_has_separator(dlg, FALSE);
2837 int do_config_box(const char *title, Conf *conf, int midsession,
2840 GtkWidget *window, *hbox, *vbox, *cols, *label,
2841 *tree, *treescroll, *panels, *panelvbox;
2842 int index, level, protocol;
2843 struct controlbox *ctrlbox;
2845 #if GTK_CHECK_VERSION(2,0,0)
2846 GtkTreeStore *treestore;
2847 GtkCellRenderer *treerenderer;
2848 GtkTreeViewColumn *treecolumn;
2849 GtkTreeSelection *treeselection;
2850 GtkTreeIter treeiterlevels[8];
2852 GtkTreeItem *treeitemlevels[8];
2853 GtkTree *treelevels[8];
2856 struct Shortcuts scs;
2858 struct selparam *selparams = NULL;
2859 int nselparams = 0, selparamsize = 0;
2863 for (index = 0; index < lenof(scs.sc); index++) {
2864 scs.sc[index].action = SHORTCUT_EMPTY;
2867 window = gtk_dialog_new();
2869 ctrlbox = ctrl_new_box();
2870 protocol = conf_get_int(conf, CONF_protocol);
2871 setup_config_box(ctrlbox, midsession, protocol, protcfginfo);
2872 unix_setup_config_box(ctrlbox, midsession, protocol);
2873 gtk_setup_config_box(ctrlbox, midsession, window);
2875 gtk_window_set_title(GTK_WINDOW(window), title);
2876 hbox = gtk_hbox_new(FALSE, 4);
2878 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
2879 hbox, TRUE, TRUE, 0);
2880 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2881 gtk_widget_show(hbox);
2882 vbox = gtk_vbox_new(FALSE, 4);
2883 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2884 gtk_widget_show(vbox);
2885 cols = columns_new(4);
2886 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2887 gtk_widget_show(cols);
2888 label = gtk_label_new("Category:");
2889 columns_add(COLUMNS(cols), label, 0, 1);
2890 columns_force_left_align(COLUMNS(cols), label);
2891 gtk_widget_show(label);
2892 treescroll = gtk_scrolled_window_new(NULL, NULL);
2893 #if GTK_CHECK_VERSION(2,0,0)
2894 treestore = gtk_tree_store_new
2895 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2896 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2897 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2898 treerenderer = gtk_cell_renderer_text_new();
2899 treecolumn = gtk_tree_view_column_new_with_attributes
2900 ("Label", treerenderer, "text", 0, NULL);
2901 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2902 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2903 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2904 gtk_container_add(GTK_CONTAINER(treescroll), tree);
2906 tree = gtk_tree_new();
2907 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2908 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2909 g_signal_connect(G_OBJECT(tree), "focus",
2910 G_CALLBACK(tree_focus), &dp);
2912 g_signal_connect(G_OBJECT(tree), "focus_in_event",
2913 G_CALLBACK(widget_focus), &dp);
2914 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2915 gtk_widget_show(treescroll);
2916 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2917 panels = gtk_notebook_new();
2918 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2919 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2920 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2921 gtk_widget_show(panels);
2926 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2927 struct controlset *s = ctrlbox->ctrlsets[index];
2930 if (!*s->pathname) {
2931 w = layout_ctrls(&dp, &scs, s, GTK_WINDOW(window));
2933 set_dialog_action_area(GTK_DIALOG(window), w);
2935 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2936 if (j != INT_MAX) { /* add to treeview, start new panel */
2938 #if GTK_CHECK_VERSION(2,0,0)
2939 GtkTreeIter treeiter;
2941 GtkWidget *treeitem;
2946 * We expect never to find an implicit path
2947 * component. For example, we expect never to see
2948 * A/B/C followed by A/D/E, because that would
2949 * _implicitly_ create A/D. All our path prefixes
2950 * are expected to contain actual controls and be
2951 * selectable in the treeview; so we would expect
2952 * to see A/D _explicitly_ before encountering
2955 assert(j == ctrl_path_elements(s->pathname) - 1);
2957 c = strrchr(s->pathname, '/');
2965 first = (panelvbox == NULL);
2967 panelvbox = gtk_vbox_new(FALSE, 4);
2968 gtk_widget_show(panelvbox);
2969 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
2974 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
2976 gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num);
2979 if (nselparams >= selparamsize) {
2981 selparams = sresize(selparams, selparamsize,
2984 selparams[nselparams].dp = &dp;
2985 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
2986 selparams[nselparams].panel = panelvbox;
2987 selparams[nselparams].shortcuts = scs; /* structure copy */
2989 assert(j-1 < level);
2991 #if GTK_CHECK_VERSION(2,0,0)
2993 /* treeiterlevels[j-1] will always be valid because we
2994 * don't allow implicit path components; see above.
2996 gtk_tree_store_append(treestore, &treeiter,
2997 &treeiterlevels[j-1]);
2999 gtk_tree_store_append(treestore, &treeiter, NULL);
3000 gtk_tree_store_set(treestore, &treeiter,
3002 TREESTORE_PARAMS, nselparams,
3004 treeiterlevels[j] = treeiter;
3006 selparams[nselparams].depth = j;
3008 selparams[nselparams].treepath =
3009 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
3010 &treeiterlevels[j-1]);
3012 * We are going to collapse all tree branches
3013 * at depth greater than 2, but not _yet_; see
3014 * the comment at the call to
3015 * gtk_tree_view_collapse_row below.
3017 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree),
3018 selparams[nselparams].treepath,
3021 selparams[nselparams].treepath = NULL;
3024 treeitem = gtk_tree_item_new_with_label(c);
3026 if (!treelevels[j-1]) {
3027 treelevels[j-1] = GTK_TREE(gtk_tree_new());
3028 gtk_tree_item_set_subtree
3029 (treeitemlevels[j-1],
3030 GTK_WIDGET(treelevels[j-1]));
3032 gtk_tree_item_expand(treeitemlevels[j-1]);
3034 gtk_tree_item_collapse(treeitemlevels[j-1]);
3036 gtk_tree_append(treelevels[j-1], treeitem);
3038 gtk_tree_append(GTK_TREE(tree), treeitem);
3040 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
3041 treelevels[j] = NULL;
3043 g_signal_connect(G_OBJECT(treeitem), "key_press_event",
3044 G_CALLBACK(tree_key_press), &dp);
3045 g_signal_connect(G_OBJECT(treeitem), "focus_in_event",
3046 G_CALLBACK(widget_focus), &dp);
3048 gtk_widget_show(treeitem);
3051 gtk_tree_select_child(GTK_TREE(tree), treeitem);
3052 selparams[nselparams].treeitem = treeitem;
3059 w = layout_ctrls(&dp, &selparams[nselparams-1].shortcuts, s, NULL);
3060 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
3065 #if GTK_CHECK_VERSION(2,0,0)
3071 * We want our tree view to come up with all branches at
3072 * depth 2 or more collapsed. However, if we start off
3073 * with those branches collapsed, then the tree view's
3074 * size request will be calculated based on the width of
3075 * the collapsed tree. So instead we start with them all
3076 * expanded; then we ask for the current size request,
3077 * collapse the relevant rows, and force the width to the
3078 * value we just computed. This arranges that the tree
3079 * view is wide enough to have all branches expanded
3083 gtk_widget_size_request(tree, &req);
3085 for (i = 0; i < nselparams; i++)
3086 if (selparams[i].depth >= 2)
3087 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree),
3088 selparams[i].treepath);
3090 gtk_widget_set_size_request(tree, req.width, -1);
3094 #if GTK_CHECK_VERSION(2,0,0)
3095 g_signal_connect(G_OBJECT(treeselection), "changed",
3096 G_CALLBACK(treeselection_changed), selparams);
3098 dp.ntreeitems = nselparams;
3099 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
3101 for (index = 0; index < nselparams; index++) {
3102 g_signal_connect(G_OBJECT(selparams[index].treeitem), "select",
3103 G_CALLBACK(treeitem_sel),
3105 dp.treeitems[index] = selparams[index].treeitem;
3110 dlg_refresh(NULL, &dp);
3112 dp.shortcuts = &selparams[0].shortcuts;
3113 #if !GTK_CHECK_VERSION(2,0,0)
3114 dp.currtreeitem = dp.treeitems[0];
3116 dp.lastfocus = NULL;
3122 extern void set_window_icon(GtkWidget *window,
3123 const char *const *const *icon,
3125 extern const char *const *const cfg_icon[];
3126 extern const int n_cfg_icon;
3127 set_window_icon(window, cfg_icon, n_cfg_icon);
3130 #if !GTK_CHECK_VERSION(2,0,0)
3131 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
3134 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
3136 GTK_POLICY_AUTOMATIC);
3137 gtk_widget_show(tree);
3139 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3140 gtk_widget_show(window);
3143 * Set focus into the first available control.
3145 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3146 struct controlset *s = ctrlbox->ctrlsets[index];
3151 for (j = 0; j < s->ncontrols; j++)
3152 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
3153 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
3154 s->ctrls[j]->generic.type != CTRL_TEXT) {
3155 dlg_set_focus(s->ctrls[j], &dp);
3156 dp.lastfocus = s->ctrls[j];
3165 g_signal_connect(G_OBJECT(window), "destroy",
3166 G_CALLBACK(window_destroy), NULL);
3167 g_signal_connect(G_OBJECT(window), "key_press_event",
3168 G_CALLBACK(win_key_press), &dp);
3178 static void messagebox_handler(union control *ctrl, void *dlg,
3179 void *data, int event)
3181 if (event == EVENT_ACTION)
3182 dlg_end(dlg, ctrl->generic.context.i);
3184 int messagebox(GtkWidget *parentwin, const char *title, const char *msg,
3187 GtkWidget *window, *w0, *w1;
3188 struct controlbox *ctrlbox;
3189 struct controlset *s0, *s1;
3192 struct Shortcuts scs;
3198 for (index = 0; index < lenof(scs.sc); index++) {
3199 scs.sc[index].action = SHORTCUT_EMPTY;
3202 ctrlbox = ctrl_new_box();
3205 va_start(ap, minwid);
3206 while (va_arg(ap, char *) != NULL) {
3208 (void) va_arg(ap, int); /* shortcut */
3209 (void) va_arg(ap, int); /* normal/default/cancel */
3210 (void) va_arg(ap, int); /* end value */
3214 s0 = ctrl_getset(ctrlbox, "", "", "");
3215 c = ctrl_columns(s0, 2, 50, 50);
3216 c->columns.ncols = s0->ncolumns = ncols;
3217 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
3218 for (index = 0; index < ncols; index++)
3219 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
3220 va_start(ap, minwid);
3223 char *title = va_arg(ap, char *);
3224 int shortcut, type, value;
3227 shortcut = va_arg(ap, int);
3228 type = va_arg(ap, int);
3229 value = va_arg(ap, int);
3230 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
3231 messagebox_handler, I(value));
3232 c->generic.column = index++;
3234 c->button.isdefault = TRUE;
3236 c->button.iscancel = TRUE;
3240 s1 = ctrl_getset(ctrlbox, "x", "", "");
3241 ctrl_text(s1, msg, HELPCTX(no_help));
3243 window = gtk_dialog_new();
3244 gtk_window_set_title(GTK_WINDOW(window), title);
3245 w0 = layout_ctrls(&dp, &scs, s0, GTK_WINDOW(window));
3246 set_dialog_action_area(GTK_DIALOG(window), w0);
3247 gtk_widget_show(w0);
3248 w1 = layout_ctrls(&dp, &scs, s1, GTK_WINDOW(window));
3249 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3250 gtk_widget_set_usize(w1, minwid+20, -1);
3252 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3254 gtk_widget_show(w1);
3256 dp.shortcuts = &scs;
3257 dp.lastfocus = NULL;
3261 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3263 set_transient_window_pos(parentwin, window);
3264 gtk_window_set_transient_for(GTK_WINDOW(window),
3265 GTK_WINDOW(parentwin));
3267 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3268 gtk_widget_show(window);
3270 g_signal_connect(G_OBJECT(window), "destroy",
3271 G_CALLBACK(window_destroy), NULL);
3272 g_signal_connect(G_OBJECT(window), "key_press_event",
3273 G_CALLBACK(win_key_press), &dp);
3278 ctrl_free_box(ctrlbox);
3283 int string_width(const char *text)
3285 GtkWidget *label = gtk_label_new(text);
3287 gtk_widget_size_request(label, &req);
3288 g_object_ref_sink(G_OBJECT(label));
3292 int reallyclose(void *frontend)
3294 char *title = dupcat(appname, " Exit Confirmation", NULL);
3295 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
3296 title, "Are you sure you want to close this session?",
3297 string_width("Most of the width of the above text"),
3305 int verify_ssh_host_key(void *frontend, char *host, int port,
3306 const char *keytype, char *keystr, char *fingerprint,
3307 void (*callback)(void *ctx, int result), void *ctx)
3309 static const char absenttxt[] =
3310 "The server's host key is not cached. You have no guarantee "
3311 "that the server is the computer you think it is.\n"
3312 "The server's %s key fingerprint is:\n"
3314 "If you trust this host, press \"Accept\" to add the key to "
3315 "PuTTY's cache and carry on connecting.\n"
3316 "If you want to carry on connecting just once, without "
3317 "adding the key to the cache, press \"Connect Once\".\n"
3318 "If you do not trust this host, press \"Cancel\" to abandon the "
3320 static const char wrongtxt[] =
3321 "WARNING - POTENTIAL SECURITY BREACH!\n"
3322 "The server's host key does not match the one PuTTY has "
3323 "cached. This means that either the server administrator "
3324 "has changed the host key, or you have actually connected "
3325 "to another computer pretending to be the server.\n"
3326 "The new %s key fingerprint is:\n"
3328 "If you were expecting this change and trust the new key, "
3329 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3330 "If you want to carry on connecting but without updating "
3331 "the cache, press \"Connect Once\".\n"
3332 "If you want to abandon the connection completely, press "
3333 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3341 ret = verify_host_key(host, port, keytype, keystr);
3343 if (ret == 0) /* success - key matched OK */
3346 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
3348 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3349 "PuTTY Security Alert", text,
3350 string_width(fingerprint),
3351 "Accept", 'a', 0, 2,
3352 "Connect Once", 'o', 0, 1,
3353 "Cancel", 'c', -1, 0,
3359 store_host_key(host, port, keytype, keystr);
3360 return 1; /* continue with connection */
3361 } else if (ret == 1)
3362 return 1; /* continue with connection */
3363 return 0; /* do not continue with connection */
3367 * Ask whether the selected algorithm is acceptable (since it was
3368 * below the configured 'warn' threshold).
3370 int askalg(void *frontend, const char *algtype, const char *algname,
3371 void (*callback)(void *ctx, int result), void *ctx)
3373 static const char msg[] =
3374 "The first %s supported by the server is "
3375 "%s, which is below the configured warning threshold.\n"
3376 "Continue with connection?";
3380 text = dupprintf(msg, algtype, algname);
3381 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3382 "PuTTY Security Alert", text,
3383 string_width("Continue with connection?"),
3396 void old_keyfile_warning(void)
3399 * This should never happen on Unix. We hope.
3403 void fatal_message_box(void *window, const char *msg)
3405 messagebox(window, "PuTTY Fatal Error", msg,
3406 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3407 "OK", 'o', 1, 1, NULL);
3410 void nonfatal_message_box(void *window, const char *msg)
3412 messagebox(window, "PuTTY Error", msg,
3413 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3414 "OK", 'o', 1, 1, NULL);
3417 void fatalbox(const char *p, ...)
3422 msg = dupvprintf(p, ap);
3424 fatal_message_box(NULL, msg);
3429 void nonfatal(const char *p, ...)
3434 msg = dupvprintf(p, ap);
3436 nonfatal_message_box(NULL, msg);
3440 static GtkWidget *aboutbox = NULL;
3442 static void about_close_clicked(GtkButton *button, gpointer data)
3444 gtk_widget_destroy(aboutbox);
3448 static void licence_clicked(GtkButton *button, gpointer data)
3452 const char *licence =
3453 "Copyright 1997-2015 Simon Tatham.\n\n"
3455 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3456 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3457 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3458 "Markus Kuhn, Colin Watson, Christopher Staite, and CORE SDI S.A.\n\n"
3460 "Permission is hereby granted, free of charge, to any person "
3461 "obtaining a copy of this software and associated documentation "
3462 "files (the ""Software""), to deal in the Software without restriction, "
3463 "including without limitation the rights to use, copy, modify, merge, "
3464 "publish, distribute, sublicense, and/or sell copies of the Software, "
3465 "and to permit persons to whom the Software is furnished to do so, "
3466 "subject to the following conditions:\n\n"
3468 "The above copyright notice and this permission notice shall be "
3469 "included in all copies or substantial portions of the Software.\n\n"
3471 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3472 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3473 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3474 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3475 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3476 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3477 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3478 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3479 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3480 "OTHER DEALINGS IN THE SOFTWARE.";
3482 title = dupcat(appname, " Licence", NULL);
3483 assert(aboutbox != NULL);
3484 messagebox(aboutbox, title, licence,
3485 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3486 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3487 "OK", 'o', 1, 1, NULL);
3491 void about_box(void *window)
3497 gtk_widget_grab_focus(aboutbox);
3501 aboutbox = gtk_dialog_new();
3502 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
3503 title = dupcat("About ", appname, NULL);
3504 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
3507 w = gtk_button_new_with_label("Close");
3508 gtk_widget_set_can_default(w, TRUE);
3509 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
3510 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3511 w, FALSE, FALSE, 0);
3512 g_signal_connect(G_OBJECT(w), "clicked",
3513 G_CALLBACK(about_close_clicked), NULL);
3516 w = gtk_button_new_with_label("View Licence");
3517 gtk_widget_set_can_default(w, TRUE);
3518 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(aboutbox))),
3519 w, FALSE, FALSE, 0);
3520 g_signal_connect(G_OBJECT(w), "clicked",
3521 G_CALLBACK(licence_clicked), NULL);
3524 w = gtk_label_new(appname);
3526 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3527 w, FALSE, FALSE, 0);
3530 w = gtk_label_new(ver);
3532 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3533 w, FALSE, FALSE, 5);
3536 w = gtk_label_new("Copyright 1997-2015 Simon Tatham. All rights reserved");
3538 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(aboutbox))),
3539 w, FALSE, FALSE, 5);
3542 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
3543 gtk_window_set_transient_for(GTK_WINDOW(aboutbox),
3544 GTK_WINDOW(window));
3545 gtk_widget_show(aboutbox);
3548 struct eventlog_stuff {
3549 GtkWidget *parentwin, *window;
3550 struct controlbox *eventbox;
3551 struct Shortcuts scs;
3553 union control *listctrl;
3555 int nevents, negsize;
3558 int ignore_selchange;
3561 static void eventlog_destroy(GtkWidget *widget, gpointer data)
3563 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3568 dlg_cleanup(&es->dp);
3569 ctrl_free_box(es->eventbox);
3571 static void eventlog_ok_handler(union control *ctrl, void *dlg,
3572 void *data, int event)
3574 if (event == EVENT_ACTION)
3577 static void eventlog_list_handler(union control *ctrl, void *dlg,
3578 void *data, int event)
3580 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3582 if (event == EVENT_REFRESH) {
3585 dlg_update_start(ctrl, dlg);
3586 dlg_listbox_clear(ctrl, dlg);
3587 for (i = 0; i < es->nevents; i++) {
3588 dlg_listbox_add(ctrl, dlg, es->events[i]);
3590 dlg_update_done(ctrl, dlg);
3591 } else if (event == EVENT_SELCHANGE) {
3596 * If this SELCHANGE event is happening as a result of
3597 * deliberate deselection because someone else has grabbed
3598 * the selection, the last thing we want to do is pre-empt
3601 if (es->ignore_selchange)
3605 * Construct the data to use as the selection.
3610 for (i = 0; i < es->nevents; i++) {
3611 if (dlg_listbox_issel(ctrl, dlg, i)) {
3612 int extralen = strlen(es->events[i]);
3614 if (es->sellen + extralen + 2 > selsize) {
3615 selsize = es->sellen + extralen + 512;
3616 es->seldata = sresize(es->seldata, selsize, char);
3619 strcpy(es->seldata + es->sellen, es->events[i]);
3620 es->sellen += extralen;
3621 es->seldata[es->sellen++] = '\n';
3625 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
3626 GDK_CURRENT_TIME)) {
3627 extern GdkAtom compound_text_atom;
3629 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3630 GDK_SELECTION_TYPE_STRING, 1);
3631 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3632 compound_text_atom, 1);
3638 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
3639 guint info, guint time_stamp, gpointer data)
3641 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3643 gtk_selection_data_set(seldata, gtk_selection_data_get_target(seldata), 8,
3644 (unsigned char *)es->seldata, es->sellen);
3647 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
3650 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3654 * Deselect everything in the list box.
3656 uc = dlg_find_byctrl(&es->dp, es->listctrl);
3657 es->ignore_selchange = 1;
3658 #if !GTK_CHECK_VERSION(2,0,0)
3660 gtk_list_unselect_all(GTK_LIST(uc->list));
3662 assert(uc->treeview);
3663 gtk_tree_selection_unselect_all
3664 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview)));
3666 es->ignore_selchange = 0;
3674 void showeventlog(void *estuff, void *parentwin)
3676 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3677 GtkWidget *window, *w0, *w1;
3678 GtkWidget *parent = GTK_WIDGET(parentwin);
3679 struct controlset *s0, *s1;
3685 gtk_widget_grab_focus(es->window);
3691 for (index = 0; index < lenof(es->scs.sc); index++) {
3692 es->scs.sc[index].action = SHORTCUT_EMPTY;
3695 es->eventbox = ctrl_new_box();
3697 s0 = ctrl_getset(es->eventbox, "", "", "");
3698 ctrl_columns(s0, 3, 33, 34, 33);
3699 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
3700 eventlog_ok_handler, P(NULL));
3701 c->button.column = 1;
3702 c->button.isdefault = TRUE;
3704 s1 = ctrl_getset(es->eventbox, "x", "", "");
3705 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3706 eventlog_list_handler, P(es));
3707 c->listbox.height = 10;
3708 c->listbox.multisel = 2;
3709 c->listbox.ncols = 3;
3710 c->listbox.percentages = snewn(3, int);
3711 c->listbox.percentages[0] = 25;
3712 c->listbox.percentages[1] = 10;
3713 c->listbox.percentages[2] = 65;
3715 es->window = window = gtk_dialog_new();
3716 title = dupcat(appname, " Event Log", NULL);
3717 gtk_window_set_title(GTK_WINDOW(window), title);
3719 w0 = layout_ctrls(&es->dp, &es->scs, s0, GTK_WINDOW(window));
3720 set_dialog_action_area(GTK_DIALOG(window), w0);
3721 gtk_widget_show(w0);
3722 w1 = layout_ctrls(&es->dp, &es->scs, s1, GTK_WINDOW(window));
3723 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3724 gtk_widget_set_usize(w1, 20 +
3725 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3726 " IS QUITE LONG 'COS SSH LOG ENTRIES"
3729 (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
3731 gtk_widget_show(w1);
3734 es->dp.shortcuts = &es->scs;
3735 es->dp.lastfocus = NULL;
3737 es->dp.window = window;
3739 dlg_refresh(NULL, &es->dp);
3742 set_transient_window_pos(parent, window);
3743 gtk_window_set_transient_for(GTK_WINDOW(window),
3744 GTK_WINDOW(parent));
3746 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3747 gtk_widget_show(window);
3749 g_signal_connect(G_OBJECT(window), "destroy",
3750 G_CALLBACK(eventlog_destroy), es);
3751 g_signal_connect(G_OBJECT(window), "key_press_event",
3752 G_CALLBACK(win_key_press), &es->dp);
3753 g_signal_connect(G_OBJECT(window), "selection_get",
3754 G_CALLBACK(eventlog_selection_get), es);
3755 g_signal_connect(G_OBJECT(window), "selection_clear_event",
3756 G_CALLBACK(eventlog_selection_clear), es);
3759 void *eventlogstuff_new(void)
3761 struct eventlog_stuff *es;
3762 es = snew(struct eventlog_stuff);
3763 memset(es, 0, sizeof(*es));
3767 void logevent_dlg(void *estuff, const char *string)
3769 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3774 if (es->nevents >= es->negsize) {
3776 es->events = sresize(es->events, es->negsize, char *);
3780 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3782 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3783 strcpy(es->events[es->nevents], timebuf);
3784 strcat(es->events[es->nevents], string);
3786 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3791 int askappend(void *frontend, Filename *filename,
3792 void (*callback)(void *ctx, int result), void *ctx)
3794 static const char msgtemplate[] =
3795 "The session log file \"%.*s\" already exists. "
3796 "You can overwrite it with a new session log, "
3797 "append your session log to the end of it, "
3798 "or disable session logging for this session.";
3803 message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
3804 mbtitle = dupprintf("%s Log to File", appname);
3806 mbret = messagebox(get_window(frontend), mbtitle, message,
3807 string_width("LINE OF TEXT SUITABLE FOR THE"
3808 " ASKAPPEND WIDTH"),
3809 "Overwrite", 'o', 1, 2,
3810 "Append", 'a', 0, 1,
3811 "Disable", 'd', -1, 0,