2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
10 #if !GTK_CHECK_VERSION(3,0,0)
11 #include <gdk/gdkkeysyms.h>
15 #include <X11/Xutil.h>
21 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
36 struct Shortcut sc[128];
42 GtkWidget **buttons; int nbuttons; /* for radio buttons */
43 GtkWidget *entry; /* for editbox, filesel, fontsel */
44 GtkWidget *button; /* for filesel, fontsel */
45 #if !GTK_CHECK_VERSION(2,4,0)
46 GtkWidget *list; /* for listbox (in GTK1), combobox (<=GTK2.3) */
47 GtkWidget *menu; /* for optionmenu (==droplist) */
48 GtkWidget *optmenu; /* also for optionmenu */
50 GtkWidget *combo; /* for combo box (either editable or not) */
52 #if GTK_CHECK_VERSION(2,0,0)
53 GtkWidget *treeview; /* for listbox (GTK2), droplist+combo (>=2.4) */
54 GtkListStore *listmodel; /* for all types of list box */
56 GtkWidget *text; /* for text */
57 GtkWidget *label; /* for dlg_label_change */
58 GtkAdjustment *adj; /* for the scrollbar in a list box */
65 tree234 *byctrl, *bywidget;
67 struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
68 /* `flags' are set to indicate when a GTK signal handler is being called
69 * due to automatic processing and should not flag a user event. */
71 struct Shortcuts *shortcuts;
72 GtkWidget *window, *cancelbutton;
73 union control *currfocus, *lastfocus;
74 #if !GTK_CHECK_VERSION(2,0,0)
75 GtkWidget *currtreeitem, **treeitems;
80 #define FLAG_UPDATING_COMBO_LIST 1
81 #define FLAG_UPDATING_LISTBOX 2
83 enum { /* values for Shortcut.action */
84 SHORTCUT_EMPTY, /* no shortcut on this key */
85 SHORTCUT_TREE, /* focus a tree item */
86 SHORTCUT_FOCUS, /* focus the supplied widget */
87 SHORTCUT_UCTRL, /* do something sane with uctrl */
88 SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */
89 SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */
92 #if GTK_CHECK_VERSION(2,0,0)
101 * Forward references.
103 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
105 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
106 int chr, int action, void *ptr);
107 static void shortcut_highlight(GtkWidget *label, int chr);
108 #if !GTK_CHECK_VERSION(2,0,0)
109 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
111 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
113 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
115 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
118 #if !GTK_CHECK_VERSION(2,4,0)
119 static void menuitem_activate(GtkMenuItem *item, gpointer data);
121 static void coloursel_ok(GtkButton *button, gpointer data);
122 static void coloursel_cancel(GtkButton *button, gpointer data);
123 static void window_destroy(GtkWidget *widget, gpointer data);
124 int get_listitemheight(GtkWidget *widget);
126 static int uctrl_cmp_byctrl(void *av, void *bv)
128 struct uctrl *a = (struct uctrl *)av;
129 struct uctrl *b = (struct uctrl *)bv;
130 if (a->ctrl < b->ctrl)
132 else if (a->ctrl > b->ctrl)
137 static int uctrl_cmp_byctrl_find(void *av, void *bv)
139 union control *a = (union control *)av;
140 struct uctrl *b = (struct uctrl *)bv;
143 else if (a > b->ctrl)
148 static int uctrl_cmp_bywidget(void *av, void *bv)
150 struct uctrl *a = (struct uctrl *)av;
151 struct uctrl *b = (struct uctrl *)bv;
152 if (a->toplevel < b->toplevel)
154 else if (a->toplevel > b->toplevel)
159 static int uctrl_cmp_bywidget_find(void *av, void *bv)
161 GtkWidget *a = (GtkWidget *)av;
162 struct uctrl *b = (struct uctrl *)bv;
165 else if (a > b->toplevel)
170 static void dlg_init(struct dlgparam *dp)
172 dp->byctrl = newtree234(uctrl_cmp_byctrl);
173 dp->bywidget = newtree234(uctrl_cmp_bywidget);
174 dp->coloursel_result.ok = FALSE;
175 dp->window = dp->cancelbutton = NULL;
176 #if !GTK_CHECK_VERSION(2,0,0)
177 dp->treeitems = NULL;
178 dp->currtreeitem = NULL;
181 dp->currfocus = NULL;
184 static void dlg_cleanup(struct dlgparam *dp)
188 freetree234(dp->byctrl); /* doesn't free the uctrls inside */
190 while ( (uc = index234(dp->bywidget, 0)) != NULL) {
191 del234(dp->bywidget, uc);
195 freetree234(dp->bywidget);
197 #if !GTK_CHECK_VERSION(2,0,0)
198 sfree(dp->treeitems);
202 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
204 add234(dp->byctrl, uc);
205 add234(dp->bywidget, uc);
208 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
212 return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
215 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
217 struct uctrl *ret = NULL;
221 ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
229 union control *dlg_last_focused(union control *ctrl, void *dlg)
231 struct dlgparam *dp = (struct dlgparam *)dlg;
232 if (dp->currfocus != ctrl)
233 return dp->currfocus;
235 return dp->lastfocus;
238 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
240 struct dlgparam *dp = (struct dlgparam *)dlg;
241 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
242 assert(uc->ctrl->generic.type == CTRL_RADIO);
243 assert(uc->buttons != NULL);
244 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
247 int dlg_radiobutton_get(union control *ctrl, void *dlg)
249 struct dlgparam *dp = (struct dlgparam *)dlg;
250 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
253 assert(uc->ctrl->generic.type == CTRL_RADIO);
254 assert(uc->buttons != NULL);
255 for (i = 0; i < uc->nbuttons; i++)
256 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
258 return 0; /* got to return something */
261 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
263 struct dlgparam *dp = (struct dlgparam *)dlg;
264 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
265 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
266 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
269 int dlg_checkbox_get(union control *ctrl, void *dlg)
271 struct dlgparam *dp = (struct dlgparam *)dlg;
272 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
273 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
274 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
277 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
279 struct dlgparam *dp = (struct dlgparam *)dlg;
280 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
283 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
285 #if GTK_CHECK_VERSION(2,4,0)
287 entry = gtk_bin_get_child(GTK_BIN(uc->combo));
292 assert(entry != NULL);
295 * GTK 2 implements gtk_entry_set_text by means of two separate
296 * operations: first delete the previous text leaving the empty
297 * string, then insert the new text. This causes two calls to
298 * the "changed" signal.
300 * The first call to "changed", if allowed to proceed normally,
301 * will cause an EVENT_VALCHANGE event on the edit box, causing
302 * a call to dlg_editbox_get() which will read the empty string
303 * out of the GtkEntry - and promptly write it straight into the
304 * Conf structure, which is precisely where our `text' pointer
305 * is probably pointing, so the second editing operation will
306 * insert that instead of the string we originally asked for.
308 * Hence, we must take our own copy of the text before we do
311 tmpstring = dupstr(text);
312 gtk_entry_set_text(GTK_ENTRY(entry), tmpstring);
316 char *dlg_editbox_get(union control *ctrl, void *dlg)
318 struct dlgparam *dp = (struct dlgparam *)dlg;
319 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
320 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
322 #if GTK_CHECK_VERSION(2,4,0)
324 #if GTK_CHECK_VERSION(2,6,0)
325 return dupstr(gtk_combo_box_get_active_text(GTK_COMBO_BOX(uc->combo)));
327 return dupstr(gtk_entry_get_text
328 (GTK_ENTRY(gtk_bin_get_child(GTK_BIN(uc->combo)))));
334 return dupstr(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
337 assert(!"We shouldn't get here");
340 #if !GTK_CHECK_VERSION(2,4,0)
341 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
343 GtkContainer *cont = GTK_CONTAINER(data);
344 /* gtk_container_remove will unref the widget for us; we need not. */
345 gtk_container_remove(cont, w);
349 /* The `listbox' functions can also apply to combo boxes. */
350 void dlg_listbox_clear(union control *ctrl, void *dlg)
352 struct dlgparam *dp = (struct dlgparam *)dlg;
353 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
355 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
356 uc->ctrl->generic.type == CTRL_LISTBOX);
358 #if !GTK_CHECK_VERSION(2,4,0)
360 gtk_container_foreach(GTK_CONTAINER(uc->menu),
361 container_remove_and_destroy,
362 GTK_CONTAINER(uc->menu));
366 gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
370 #if GTK_CHECK_VERSION(2,0,0)
372 gtk_list_store_clear(uc->listmodel);
376 assert(!"We shouldn't get here");
379 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
381 struct dlgparam *dp = (struct dlgparam *)dlg;
382 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
384 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
385 uc->ctrl->generic.type == CTRL_LISTBOX);
387 #if !GTK_CHECK_VERSION(2,4,0)
390 (GTK_CONTAINER(uc->menu),
391 g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
395 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
399 #if GTK_CHECK_VERSION(2,0,0)
403 assert(uc->listmodel != NULL);
404 path = gtk_tree_path_new_from_indices(index, -1);
405 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
406 gtk_list_store_remove(uc->listmodel, &iter);
407 gtk_tree_path_free(path);
411 assert(!"We shouldn't get here");
414 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
416 dlg_listbox_addwithid(ctrl, dlg, text, 0);
420 * Each listbox entry may have a numeric id associated with it.
421 * Note that some front ends only permit a string to be stored at
422 * each position, which means that _if_ you put two identical
423 * strings in any listbox then you MUST not assign them different
424 * IDs and expect to get meaningful results back.
426 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
427 char const *text, int id)
429 struct dlgparam *dp = (struct dlgparam *)dlg;
430 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
432 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
433 uc->ctrl->generic.type == CTRL_LISTBOX);
436 * This routine is long and complicated in both GTK 1 and 2,
437 * and completely different. Sigh.
439 dp->flags |= FLAG_UPDATING_COMBO_LIST;
441 #if !GTK_CHECK_VERSION(2,4,0)
444 * List item in a drop-down (but non-combo) list. Tabs are
445 * ignored; we just provide a standard menu item with the
448 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
450 gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
451 gtk_widget_show(menuitem);
453 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
454 GINT_TO_POINTER(id));
455 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
456 GTK_SIGNAL_FUNC(menuitem_activate), dp);
459 if (uc->list && uc->entry) {
461 * List item in a combo-box list, which means the sensible
462 * thing to do is make it a perfectly normal label. Hence
463 * tabs are disregarded.
465 GtkWidget *listitem = gtk_list_item_new_with_label(text);
467 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
468 gtk_widget_show(listitem);
470 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
471 GINT_TO_POINTER(id));
475 #if !GTK_CHECK_VERSION(2,0,0)
478 * List item in a non-combo-box list box. We make all of
479 * these Columns containing GtkLabels. This allows us to do
480 * the nasty force_left hack irrespective of whether there
481 * are tabs in the thing.
483 GtkWidget *listitem = gtk_list_item_new();
484 GtkWidget *cols = columns_new(10);
488 /* Count the tabs in the text, and hence determine # of columns. */
490 for (i = 0; text[i]; i++)
495 (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
496 percents = snewn(ncols, gint);
497 percents[ncols-1] = 100;
498 for (i = 0; i < ncols-1; i++) {
499 percents[i] = uc->ctrl->listbox.percentages[i];
500 percents[ncols-1] -= percents[i];
502 columns_set_cols(COLUMNS(cols), ncols, percents);
505 for (i = 0; i < ncols; i++) {
506 int len = strcspn(text, "\t");
507 char *dup = dupprintf("%.*s", len, text);
512 label = gtk_label_new(dup);
515 columns_add(COLUMNS(cols), label, i, 1);
516 columns_force_left_align(COLUMNS(cols), label);
517 gtk_widget_show(label);
519 gtk_container_add(GTK_CONTAINER(listitem), cols);
520 gtk_widget_show(cols);
521 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
522 gtk_widget_show(listitem);
524 if (ctrl->listbox.multisel) {
525 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
526 GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj);
528 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
529 GTK_SIGNAL_FUNC(listitem_single_key), uc->adj);
531 gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event",
532 GTK_SIGNAL_FUNC(widget_focus), dp);
533 gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event",
534 GTK_SIGNAL_FUNC(listitem_button_press), dp);
535 gtk_signal_connect(GTK_OBJECT(listitem), "button_release_event",
536 GTK_SIGNAL_FUNC(listitem_button_release), dp);
537 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
538 GINT_TO_POINTER(id));
546 dp->flags |= FLAG_UPDATING_LISTBOX;/* inhibit drag-list update */
547 gtk_list_store_append(uc->listmodel, &iter);
548 dp->flags &= ~FLAG_UPDATING_LISTBOX;
549 gtk_list_store_set(uc->listmodel, &iter, 0, id, -1);
552 * Now go through text and divide it into columns at the tabs,
555 cols = (uc->ctrl->generic.type == CTRL_LISTBOX ? ctrl->listbox.ncols : 1);
556 cols = cols ? cols : 1;
557 for (i = 0; i < cols; i++) {
558 int collen = strcspn(text, "\t");
559 char *tmpstr = snewn(collen+1, char);
560 memcpy(tmpstr, text, collen);
561 tmpstr[collen] = '\0';
562 gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1);
570 assert(!"We shouldn't get here");
572 dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
575 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
577 struct dlgparam *dp = (struct dlgparam *)dlg;
578 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
580 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
581 uc->ctrl->generic.type == CTRL_LISTBOX);
583 #if !GTK_CHECK_VERSION(2,4,0)
584 if (uc->menu || uc->list) {
588 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
590 item = GTK_OBJECT(g_list_nth_data(children, index));
591 g_list_free(children);
593 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
597 #if GTK_CHECK_VERSION(2,0,0)
603 path = gtk_tree_path_new_from_indices(index, -1);
604 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc->listmodel), &iter, path);
605 gtk_tree_model_get(GTK_TREE_MODEL(uc->listmodel), &iter, 0, &ret, -1);
606 gtk_tree_path_free(path);
611 assert(!"We shouldn't get here");
612 return -1; /* placate dataflow analysis */
615 /* dlg_listbox_index returns <0 if no single element is selected. */
616 int dlg_listbox_index(union control *ctrl, void *dlg)
618 struct dlgparam *dp = (struct dlgparam *)dlg;
619 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
621 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
622 uc->ctrl->generic.type == CTRL_LISTBOX);
624 #if !GTK_CHECK_VERSION(2,4,0)
625 if (uc->menu || uc->list) {
627 GtkWidget *item, *activeitem;
632 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
634 activeitem = NULL; /* unnecessarily placate gcc */
636 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
638 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
639 i++, children = children->next) {
640 if (uc->menu ? activeitem == item :
641 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
648 g_list_free(children);
649 return selected < 0 ? -1 : selected;
654 * This API function already does the right thing in the
655 * case of no current selection.
657 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo));
660 #if GTK_CHECK_VERSION(2,0,0)
662 GtkTreeSelection *treesel;
669 assert(uc->treeview != NULL);
670 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
672 if (gtk_tree_selection_count_selected_rows(treesel) != 1)
675 sellist = gtk_tree_selection_get_selected_rows(treesel, &model);
677 assert(sellist && sellist->data);
678 path = sellist->data;
680 if (gtk_tree_path_get_depth(path) != 1) {
683 indices = gtk_tree_path_get_indices(path);
691 g_list_foreach(sellist, (GFunc)gtk_tree_path_free, NULL);
692 g_list_free(sellist);
697 assert(!"We shouldn't get here");
698 return -1; /* placate dataflow analysis */
701 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
703 struct dlgparam *dp = (struct dlgparam *)dlg;
704 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
706 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
707 uc->ctrl->generic.type == CTRL_LISTBOX);
709 #if !GTK_CHECK_VERSION(2,4,0)
710 if (uc->menu || uc->list) {
712 GtkWidget *item, *activeitem;
714 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
715 uc->ctrl->generic.type == CTRL_LISTBOX);
716 assert(uc->menu != NULL || uc->list != NULL);
718 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
720 item = GTK_WIDGET(g_list_nth_data(children, index));
721 g_list_free(children);
724 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
725 return item == activeitem;
727 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
733 * This API function already does the right thing in the
734 * case of no current selection.
736 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc->combo)) == index;
739 #if GTK_CHECK_VERSION(2,0,0)
741 GtkTreeSelection *treesel;
745 assert(uc->treeview != NULL);
746 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
748 path = gtk_tree_path_new_from_indices(index, -1);
749 ret = gtk_tree_selection_path_is_selected(treesel, path);
750 gtk_tree_path_free(path);
755 assert(!"We shouldn't get here");
756 return -1; /* placate dataflow analysis */
759 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
761 struct dlgparam *dp = (struct dlgparam *)dlg;
762 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
764 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
765 uc->ctrl->generic.type == CTRL_LISTBOX);
767 #if !GTK_CHECK_VERSION(2,4,0)
769 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
775 gdouble newtop, newbot;
777 gtk_list_select_item(GTK_LIST(uc->list), index);
780 * Scroll the list box if necessary to ensure the newly
781 * selected item is visible.
783 items = gtk_container_children(GTK_CONTAINER(uc->list));
784 nitems = g_list_length(items);
786 int modified = FALSE;
788 newtop = uc->adj->lower +
789 (uc->adj->upper - uc->adj->lower) * index / nitems;
790 newbot = uc->adj->lower +
791 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
792 if (uc->adj->value > newtop) {
794 uc->adj->value = newtop;
795 } else if (uc->adj->value < newbot - uc->adj->page_size) {
797 uc->adj->value = newbot - uc->adj->page_size;
800 gtk_adjustment_value_changed(uc->adj);
806 gtk_combo_box_set_active(GTK_COMBO_BOX(uc->combo), index);
810 #if GTK_CHECK_VERSION(2,0,0)
812 GtkTreeSelection *treesel;
815 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview));
817 path = gtk_tree_path_new_from_indices(index, -1);
818 gtk_tree_selection_select_path(treesel, path);
819 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc->treeview),
820 path, NULL, FALSE, 0.0, 0.0);
821 gtk_tree_path_free(path);
825 assert(!"We shouldn't get here");
828 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
830 struct dlgparam *dp = (struct dlgparam *)dlg;
831 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
833 assert(uc->ctrl->generic.type == CTRL_TEXT);
834 assert(uc->text != NULL);
836 gtk_label_set_text(GTK_LABEL(uc->text), text);
839 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
841 struct dlgparam *dp = (struct dlgparam *)dlg;
842 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
844 switch (uc->ctrl->generic.type) {
846 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
847 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
850 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
851 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
854 gtk_label_set_text(GTK_LABEL(uc->label), text);
855 shortcut_highlight(uc->label, ctrl->radio.shortcut);
858 gtk_label_set_text(GTK_LABEL(uc->label), text);
859 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
861 case CTRL_FILESELECT:
862 gtk_label_set_text(GTK_LABEL(uc->label), text);
863 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
865 case CTRL_FONTSELECT:
866 gtk_label_set_text(GTK_LABEL(uc->label), text);
867 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
870 gtk_label_set_text(GTK_LABEL(uc->label), text);
871 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
874 assert(!"This shouldn't happen");
879 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
881 struct dlgparam *dp = (struct dlgparam *)dlg;
882 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
883 /* We must copy fn->path before passing it to gtk_entry_set_text.
884 * See comment in dlg_editbox_set() for the reasons. */
885 char *duppath = dupstr(fn->path);
886 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
887 assert(uc->entry != NULL);
888 gtk_entry_set_text(GTK_ENTRY(uc->entry), duppath);
892 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
894 struct dlgparam *dp = (struct dlgparam *)dlg;
895 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
896 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
897 assert(uc->entry != NULL);
898 return filename_from_str(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
901 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
903 struct dlgparam *dp = (struct dlgparam *)dlg;
904 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
905 /* We must copy fs->name before passing it to gtk_entry_set_text.
906 * See comment in dlg_editbox_set() for the reasons. */
907 char *dupname = dupstr(fs->name);
908 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
909 assert(uc->entry != NULL);
910 gtk_entry_set_text(GTK_ENTRY(uc->entry), dupname);
914 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
916 struct dlgparam *dp = (struct dlgparam *)dlg;
917 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
918 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
919 assert(uc->entry != NULL);
920 return fontspec_new(gtk_entry_get_text(GTK_ENTRY(uc->entry)));
924 * Bracketing a large set of updates in these two functions will
925 * cause the front end (if possible) to delay updating the screen
926 * until it's all complete, thus avoiding flicker.
928 void dlg_update_start(union control *ctrl, void *dlg)
931 * Apparently we can't do this at all in GTK. GtkCList supports
932 * freeze and thaw, but not GtkList. Bah.
936 void dlg_update_done(union control *ctrl, void *dlg)
939 * Apparently we can't do this at all in GTK. GtkCList supports
940 * freeze and thaw, but not GtkList. Bah.
944 void dlg_set_focus(union control *ctrl, void *dlg)
946 struct dlgparam *dp = (struct dlgparam *)dlg;
947 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
949 switch (ctrl->generic.type) {
952 /* Check boxes and buttons get the focus _and_ get toggled. */
953 gtk_widget_grab_focus(uc->toplevel);
955 case CTRL_FILESELECT:
956 case CTRL_FONTSELECT:
959 /* Anything containing an edit box gets that focused. */
960 gtk_widget_grab_focus(uc->entry);
962 #if GTK_CHECK_VERSION(2,4,0)
963 else if (uc->combo) {
964 /* Failing that, there'll be a combo box. */
965 gtk_widget_grab_focus(uc->combo);
971 * Radio buttons: we find the currently selected button and
976 for (i = 0; i < ctrl->radio.nbuttons; i++)
977 if (gtk_toggle_button_get_active
978 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
979 gtk_widget_grab_focus(uc->buttons[i]);
984 #if !GTK_CHECK_VERSION(2,4,0)
986 gtk_widget_grab_focus(uc->optmenu);
991 gtk_widget_grab_focus(uc->combo);
995 #if !GTK_CHECK_VERSION(2,0,0)
998 * For GTK-1 style list boxes, we tell it to focus one
999 * of its children, which appears to do the Right
1002 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
1007 gtk_widget_grab_focus(uc->treeview);
1011 assert(!"We shouldn't get here");
1017 * During event processing, you might well want to give an error
1018 * indication to the user. dlg_beep() is a quick and easy generic
1019 * error; dlg_error() puts up a message-box or equivalent.
1021 void dlg_beep(void *dlg)
1026 static void errmsg_button_clicked(GtkButton *button, gpointer data)
1028 gtk_widget_destroy(GTK_WIDGET(data));
1031 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
1033 gint x, y, w, h, dx, dy;
1035 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
1036 gtk_widget_size_request(GTK_WIDGET(child), &req);
1038 gdk_window_get_origin(GTK_WIDGET(parent)->window, &x, &y);
1039 gdk_window_get_size(GTK_WIDGET(parent)->window, &w, &h);
1042 * One corner of the transient will be offset inwards, by 1/4
1043 * of the parent window's size, from the corresponding corner
1044 * of the parent window. The corner will be chosen so as to
1045 * place the transient closer to the centre of the screen; this
1046 * should avoid transients going off the edge of the screen on
1049 if (x + w/2 < gdk_screen_width() / 2)
1050 dx = x + w/4; /* work from left edges */
1052 dx = x + 3*w/4 - req.width; /* work from right edges */
1053 if (y + h/2 < gdk_screen_height() / 2)
1054 dy = y + h/4; /* work from top edges */
1056 dy = y + 3*h/4 - req.height; /* work from bottom edges */
1057 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
1060 void dlg_error_msg(void *dlg, const char *msg)
1062 struct dlgparam *dp = (struct dlgparam *)dlg;
1063 GtkWidget *window, *hbox, *text, *ok;
1065 window = gtk_dialog_new();
1066 text = gtk_label_new(msg);
1067 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1068 hbox = gtk_hbox_new(FALSE, 0);
1069 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1070 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
1071 hbox, FALSE, FALSE, 20);
1072 gtk_widget_show(text);
1073 gtk_widget_show(hbox);
1074 gtk_window_set_title(GTK_WINDOW(window), "Error");
1075 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1076 ok = gtk_button_new_with_label("OK");
1077 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
1078 ok, FALSE, FALSE, 0);
1079 gtk_widget_show(ok);
1080 GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
1081 gtk_window_set_default(GTK_WINDOW(window), ok);
1082 gtk_signal_connect(GTK_OBJECT(ok), "clicked",
1083 GTK_SIGNAL_FUNC(errmsg_button_clicked), window);
1084 gtk_signal_connect(GTK_OBJECT(window), "destroy",
1085 GTK_SIGNAL_FUNC(window_destroy), NULL);
1086 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1087 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
1088 set_transient_window_pos(dp->window, window);
1089 gtk_widget_show(window);
1094 * This function signals to the front end that the dialog's
1095 * processing is completed, and passes an integer value (typically
1096 * a success status).
1098 void dlg_end(void *dlg, int value)
1100 struct dlgparam *dp = (struct dlgparam *)dlg;
1102 gtk_widget_destroy(dp->window);
1105 void dlg_refresh(union control *ctrl, void *dlg)
1107 struct dlgparam *dp = (struct dlgparam *)dlg;
1111 if (ctrl->generic.handler != NULL)
1112 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1116 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
1117 assert(uc->ctrl != NULL);
1118 if (uc->ctrl->generic.handler != NULL)
1119 uc->ctrl->generic.handler(uc->ctrl, dp,
1120 dp->data, EVENT_REFRESH);
1125 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
1127 struct dlgparam *dp = (struct dlgparam *)dlg;
1128 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
1131 GtkWidget *coloursel =
1132 gtk_color_selection_dialog_new("Select a colour");
1133 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
1135 dp->coloursel_result.ok = FALSE;
1137 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
1138 #if GTK_CHECK_VERSION(2,0,0)
1139 gtk_color_selection_set_has_opacity_control(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
1141 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
1143 cvals[0] = r / 255.0;
1144 cvals[1] = g / 255.0;
1145 cvals[2] = b / 255.0;
1146 cvals[3] = 1.0; /* fully opaque! */
1147 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals);
1149 gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data",
1150 (gpointer)coloursel);
1151 gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data",
1152 (gpointer)coloursel);
1153 gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc);
1154 gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked",
1155 GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp);
1156 gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked",
1157 GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp);
1158 gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked",
1159 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1160 (gpointer)coloursel);
1161 gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked",
1162 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1163 (gpointer)coloursel);
1164 gtk_widget_show(coloursel);
1167 int dlg_coloursel_results(union control *ctrl, void *dlg,
1168 int *r, int *g, int *b)
1170 struct dlgparam *dp = (struct dlgparam *)dlg;
1171 if (dp->coloursel_result.ok) {
1172 *r = dp->coloursel_result.r;
1173 *g = dp->coloursel_result.g;
1174 *b = dp->coloursel_result.b;
1180 /* ----------------------------------------------------------------------
1181 * Signal handlers while the dialog box is active.
1184 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
1187 struct dlgparam *dp = (struct dlgparam *)data;
1188 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1189 union control *focus;
1196 if (focus != dp->currfocus) {
1197 dp->lastfocus = dp->currfocus;
1198 dp->currfocus = focus;
1204 static void button_clicked(GtkButton *button, gpointer data)
1206 struct dlgparam *dp = (struct dlgparam *)data;
1207 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1208 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1211 static void button_toggled(GtkToggleButton *tb, gpointer data)
1213 struct dlgparam *dp = (struct dlgparam *)data;
1214 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
1215 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1218 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
1222 * GtkEntry has a nasty habit of eating the Return key, which
1223 * is unhelpful since it doesn't actually _do_ anything with it
1224 * (it calls gtk_widget_activate, but our edit boxes never need
1225 * activating). So I catch Return before GtkEntry sees it, and
1226 * pass it straight on to the parent widget. Effect: hitting
1227 * Return in an edit box will now activate the default button
1228 * in the dialog just like it will everywhere else.
1230 if (event->keyval == GDK_Return && widget->parent != NULL) {
1231 gboolean return_val;
1232 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1233 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
1234 event, &return_val);
1240 static void editbox_changed(GtkEditable *ed, gpointer data)
1242 struct dlgparam *dp = (struct dlgparam *)data;
1243 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1244 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1245 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1249 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1252 struct dlgparam *dp = (struct dlgparam *)data;
1253 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1254 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1258 #if !GTK_CHECK_VERSION(2,0,0)
1261 * GTK 1 list box event handlers.
1264 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1265 gpointer data, int multiple)
1267 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1269 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1270 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1271 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1272 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1274 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1275 * in a list box. So, if the list box is single-selection:
1277 * - if the list item in question isn't already selected,
1278 * we simply select it.
1279 * - otherwise, we find the next one (or next
1280 * however-far-away) in whichever direction we're going,
1282 * + in this case, we must also fiddle with the
1283 * scrollbar to ensure the newly selected item is
1286 * If it's multiple-selection, we do all of the above
1287 * except actually selecting anything, so we move the focus
1288 * and fiddle the scrollbar to follow it.
1290 GtkWidget *list = item->parent;
1292 gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event");
1295 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1296 gtk_list_select_child(GTK_LIST(list), item);
1299 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1300 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1303 (event->keyval==GDK_Page_Down ||
1304 event->keyval==GDK_KP_Page_Down ||
1305 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1308 GList *children, *chead;
1310 chead = children = gtk_container_children(GTK_CONTAINER(list));
1312 n = g_list_length(children);
1316 * Figure out how many list items to a screenful,
1317 * and adjust the step appropriately.
1319 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1320 step--; /* go by one less than that */
1324 while (children != NULL) {
1325 if (item == children->data)
1327 children = children->next;
1332 if (direction < 0 && i > 0)
1333 children = children->prev, i--;
1334 else if (direction > 0 && i < n-1)
1335 children = children->next, i++;
1339 if (children && children->data) {
1341 gtk_list_select_child(GTK_LIST(list),
1342 GTK_WIDGET(children->data));
1343 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1344 gtk_adjustment_clamp_page
1346 adj->lower + (adj->upper-adj->lower) * i / n,
1347 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1358 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1361 return listitem_key(item, event, data, FALSE);
1364 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1367 return listitem_key(item, event, data, TRUE);
1370 static gboolean listitem_button_press(GtkWidget *item, GdkEventButton *event,
1373 struct dlgparam *dp = (struct dlgparam *)data;
1374 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1375 switch (event->type) {
1377 case GDK_BUTTON_PRESS: uc->nclicks = 1; break;
1378 case GDK_2BUTTON_PRESS: uc->nclicks = 2; break;
1379 case GDK_3BUTTON_PRESS: uc->nclicks = 3; break;
1384 static gboolean listitem_button_release(GtkWidget *item, GdkEventButton *event,
1387 struct dlgparam *dp = (struct dlgparam *)data;
1388 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1389 if (uc->nclicks>1) {
1390 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1396 static void list_selchange(GtkList *list, gpointer data)
1398 struct dlgparam *dp = (struct dlgparam *)data;
1399 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1401 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1404 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1406 int index = dlg_listbox_index(uc->ctrl, dp);
1407 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1411 (index == 0 && direction < 0) ||
1412 (index == g_list_length(children)-1 && direction > 0)) {
1417 child = g_list_nth_data(children, index);
1418 gtk_widget_ref(child);
1419 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1420 g_list_free(children);
1423 children = g_list_append(children, child);
1424 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1425 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1426 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1429 static void draglist_up(GtkButton *button, gpointer data)
1431 struct dlgparam *dp = (struct dlgparam *)data;
1432 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1433 draglist_move(dp, uc, -1);
1436 static void draglist_down(GtkButton *button, gpointer data)
1438 struct dlgparam *dp = (struct dlgparam *)data;
1439 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1440 draglist_move(dp, uc, +1);
1443 #else /* !GTK_CHECK_VERSION(2,0,0) */
1446 * GTK 2 list box event handlers.
1449 static void listbox_doubleclick(GtkTreeView *treeview, GtkTreePath *path,
1450 GtkTreeViewColumn *column, gpointer data)
1452 struct dlgparam *dp = (struct dlgparam *)data;
1453 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(treeview));
1455 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1458 static void listbox_selchange(GtkTreeSelection *treeselection,
1461 struct dlgparam *dp = (struct dlgparam *)data;
1462 GtkTreeView *tree = gtk_tree_selection_get_tree_view(treeselection);
1463 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1465 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1468 struct draglist_valchange_ctx {
1470 struct dlgparam *dp;
1473 static gboolean draglist_valchange(gpointer data)
1475 struct draglist_valchange_ctx *ctx =
1476 (struct draglist_valchange_ctx *)data;
1478 ctx->uc->ctrl->generic.handler(ctx->uc->ctrl, ctx->dp,
1479 ctx->dp->data, EVENT_VALCHANGE);
1486 static void listbox_reorder(GtkTreeModel *treemodel, GtkTreePath *path,
1487 GtkTreeIter *iter, gpointer data)
1489 struct dlgparam *dp = (struct dlgparam *)data;
1493 if (dp->flags & FLAG_UPDATING_LISTBOX)
1494 return; /* not a user drag operation */
1496 tree = g_object_get_data(G_OBJECT(treemodel), "user-data");
1497 uc = dlg_find_bywidget(dp, GTK_WIDGET(tree));
1500 * We should cause EVENT_VALCHANGE on the list box, now
1501 * that its rows have been reordered. However, the GTK 2
1502 * docs say that at the point this signal is received the
1503 * new row might not have actually been filled in yet.
1505 * (So what smegging use is it then, eh? Don't suppose it
1506 * occurred to you at any point that letting the
1507 * application know _after_ the reordering was compelete
1508 * might be helpful to someone?)
1510 * To get round this, I schedule an idle function, which I
1511 * hope won't be called until the main event loop is
1512 * re-entered after the drag-and-drop handler has finished
1513 * furtling with the list store.
1515 struct draglist_valchange_ctx *ctx =
1516 snew(struct draglist_valchange_ctx);
1519 g_idle_add(draglist_valchange, ctx);
1523 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1525 #if !GTK_CHECK_VERSION(2,4,0)
1527 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1529 struct dlgparam *dp = (struct dlgparam *)data;
1530 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1531 gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data");
1532 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1533 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1538 static void droplist_selchange(GtkComboBox *combo, gpointer data)
1540 struct dlgparam *dp = (struct dlgparam *)data;
1541 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(combo));
1543 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1546 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1548 static void filesel_ok(GtkButton *button, gpointer data)
1550 /* struct dlgparam *dp = (struct dlgparam *)data; */
1551 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1552 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data");
1553 const char *name = gtk_file_selection_get_filename
1554 (GTK_FILE_SELECTION(filesel));
1555 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1558 static void fontsel_ok(GtkButton *button, gpointer data)
1560 /* struct dlgparam *dp = (struct dlgparam *)data; */
1562 #if !GTK_CHECK_VERSION(2,0,0)
1564 gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1565 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data");
1566 const char *name = gtk_font_selection_dialog_get_font_name
1567 (GTK_FONT_SELECTION_DIALOG(fontsel));
1568 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1572 unifontsel *fontsel = (unifontsel *)gtk_object_get_data
1573 (GTK_OBJECT(button), "user-data");
1574 struct uctrl *uc = (struct uctrl *)fontsel->user_data;
1575 char *name = unifontsel_get_name(fontsel);
1576 assert(name); /* should always be ok after OK pressed */
1577 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1583 static void coloursel_ok(GtkButton *button, gpointer data)
1585 struct dlgparam *dp = (struct dlgparam *)data;
1586 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1587 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1589 gtk_color_selection_get_color
1590 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel),
1592 dp->coloursel_result.r = (int) (255 * cvals[0]);
1593 dp->coloursel_result.g = (int) (255 * cvals[1]);
1594 dp->coloursel_result.b = (int) (255 * cvals[2]);
1595 dp->coloursel_result.ok = TRUE;
1596 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1599 static void coloursel_cancel(GtkButton *button, gpointer data)
1601 struct dlgparam *dp = (struct dlgparam *)data;
1602 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1603 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1604 dp->coloursel_result.ok = FALSE;
1605 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1608 static void filefont_clicked(GtkButton *button, gpointer data)
1610 struct dlgparam *dp = (struct dlgparam *)data;
1611 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1613 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1614 GtkWidget *filesel =
1615 gtk_file_selection_new(uc->ctrl->fileselect.title);
1616 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1618 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1620 gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc);
1622 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1623 GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp);
1624 gtk_signal_connect_object
1625 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1626 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1627 gtk_signal_connect_object
1628 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1629 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1630 gtk_widget_show(filesel);
1633 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1634 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1636 #if !GTK_CHECK_VERSION(2,0,0)
1639 * Use the GTK 1 standard font selector.
1642 gchar *spacings[] = { "c", "m", NULL };
1643 GtkWidget *fontsel =
1644 gtk_font_selection_dialog_new("Select a font");
1645 gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1646 gtk_font_selection_dialog_set_filter
1647 (GTK_FONT_SELECTION_DIALOG(fontsel),
1648 GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1649 NULL, NULL, NULL, NULL, spacings, NULL);
1650 if (!gtk_font_selection_dialog_set_font_name
1651 (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1653 * If the font name wasn't found as it was, try opening
1654 * it and extracting its FONT property. This should
1655 * have the effect of mapping short aliases into true
1658 GdkFont *font = gdk_font_load(fontname);
1660 XFontStruct *xfs = GDK_FONT_XFONT(font);
1661 Display *disp = GDK_FONT_XDISPLAY(font);
1662 Atom fontprop = XInternAtom(disp, "FONT", False);
1665 if (XGetFontProperty(xfs, fontprop, &ret)) {
1666 char *name = XGetAtomName(disp, (Atom)ret);
1668 gtk_font_selection_dialog_set_font_name
1669 (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1671 gdk_font_unref(font);
1675 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1676 "user-data", (gpointer)fontsel);
1677 gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc);
1679 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1680 "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1681 gtk_signal_connect_object
1682 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1683 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1685 gtk_signal_connect_object
1686 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1687 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1689 gtk_widget_show(fontsel);
1691 #else /* !GTK_CHECK_VERSION(2,0,0) */
1694 * Use the unifontsel code provided in gtkfont.c.
1697 unifontsel *fontsel = unifontsel_new("Select a font");
1699 gtk_window_set_modal(fontsel->window, TRUE);
1700 unifontsel_set_name(fontsel, fontname);
1702 gtk_object_set_data(GTK_OBJECT(fontsel->ok_button),
1703 "user-data", (gpointer)fontsel);
1704 fontsel->user_data = uc;
1705 gtk_signal_connect(GTK_OBJECT(fontsel->ok_button), "clicked",
1706 GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1707 gtk_signal_connect_object(GTK_OBJECT(fontsel->ok_button), "clicked",
1708 GTK_SIGNAL_FUNC(unifontsel_destroy),
1710 gtk_signal_connect_object(GTK_OBJECT(fontsel->cancel_button),"clicked",
1711 GTK_SIGNAL_FUNC(unifontsel_destroy),
1714 gtk_widget_show(GTK_WIDGET(fontsel->window));
1716 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1721 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1724 struct dlgparam *dp = (struct dlgparam *)data;
1725 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1727 gtk_widget_set_usize(uc->text, alloc->width, -1);
1728 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1729 gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig);
1732 /* ----------------------------------------------------------------------
1733 * This function does the main layout work: it reads a controlset,
1734 * it creates the relevant GTK controls, and returns a GtkWidget
1735 * containing the result. (This widget might be a title of some
1736 * sort, it might be a Columns containing many controls, or it
1737 * might be a GtkFrame containing a Columns; whatever it is, it's
1738 * definitely a GtkWidget and should probably be added to a
1741 * `win' is required for setting the default button. If it is
1742 * non-NULL, all buttons created will be default-capable (so they
1743 * have extra space round them for the default highlight).
1745 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1746 struct controlset *s, GtkWindow *win)
1752 if (!s->boxname && s->boxtitle) {
1753 /* This controlset is a panel title. */
1754 return gtk_label_new(s->boxtitle);
1758 * Otherwise, we expect to be laying out actual controls, so
1759 * we'll start by creating a Columns for the purpose.
1761 cols = COLUMNS(columns_new(4));
1762 ret = GTK_WIDGET(cols);
1763 gtk_widget_show(ret);
1766 * Create a containing frame if we have a box name.
1769 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1770 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1771 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1772 gtk_widget_show(ret);
1776 * Now iterate through the controls themselves, create them,
1777 * and add them to the Columns.
1779 for (i = 0; i < s->ncontrols; i++) {
1780 union control *ctrl = s->ctrls[i];
1783 GtkWidget *w = NULL;
1785 switch (ctrl->generic.type) {
1788 static const int simplecols[1] = { 100 };
1789 columns_set_cols(cols, ctrl->columns.ncols,
1790 (ctrl->columns.percentages ?
1791 ctrl->columns.percentages : simplecols));
1793 continue; /* no actual control created */
1796 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1798 columns_taborder_last(cols, uc->toplevel);
1800 continue; /* no actual control created */
1803 uc = snew(struct uctrl);
1807 #if !GTK_CHECK_VERSION(2,4,0)
1808 uc->list = uc->menu = uc->optmenu = NULL;
1812 #if GTK_CHECK_VERSION(2,0,0)
1813 uc->treeview = NULL;
1814 uc->listmodel = NULL;
1816 uc->button = uc->text = NULL;
1820 switch (ctrl->generic.type) {
1822 w = gtk_button_new_with_label(ctrl->generic.label);
1824 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
1825 if (ctrl->button.isdefault)
1826 gtk_window_set_default(win, w);
1827 if (ctrl->button.iscancel)
1828 dp->cancelbutton = w;
1830 gtk_signal_connect(GTK_OBJECT(w), "clicked",
1831 GTK_SIGNAL_FUNC(button_clicked), dp);
1832 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1833 GTK_SIGNAL_FUNC(widget_focus), dp);
1834 shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut,
1835 SHORTCUT_UCTRL, uc);
1838 w = gtk_check_button_new_with_label(ctrl->generic.label);
1839 gtk_signal_connect(GTK_OBJECT(w), "toggled",
1840 GTK_SIGNAL_FUNC(button_toggled), dp);
1841 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1842 GTK_SIGNAL_FUNC(widget_focus), dp);
1843 shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut,
1844 SHORTCUT_UCTRL, uc);
1849 * Radio buttons get to go inside their own Columns, no
1853 gint i, *percentages;
1857 if (ctrl->generic.label) {
1858 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1859 columns_add(COLUMNS(w), label, 0, 1);
1860 columns_force_left_align(COLUMNS(w), label);
1861 gtk_widget_show(label);
1862 shortcut_add(scs, label, ctrl->radio.shortcut,
1863 SHORTCUT_UCTRL, uc);
1866 percentages = g_new(gint, ctrl->radio.ncolumns);
1867 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1869 ((100 * (i+1) / ctrl->radio.ncolumns) -
1870 100 * i / ctrl->radio.ncolumns);
1872 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1874 g_free(percentages);
1877 uc->nbuttons = ctrl->radio.nbuttons;
1878 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1880 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1884 b = (gtk_radio_button_new_with_label
1885 (group, ctrl->radio.buttons[i]));
1887 group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
1888 colstart = i % ctrl->radio.ncolumns;
1889 columns_add(COLUMNS(w), b, colstart,
1890 (i == ctrl->radio.nbuttons-1 ?
1891 ctrl->radio.ncolumns - colstart : 1));
1892 columns_force_left_align(COLUMNS(w), b);
1894 gtk_signal_connect(GTK_OBJECT(b), "toggled",
1895 GTK_SIGNAL_FUNC(button_toggled), dp);
1896 gtk_signal_connect(GTK_OBJECT(b), "focus_in_event",
1897 GTK_SIGNAL_FUNC(widget_focus), dp);
1898 if (ctrl->radio.shortcuts) {
1899 shortcut_add(scs, GTK_BIN(b)->child,
1900 ctrl->radio.shortcuts[i],
1901 SHORTCUT_UCTRL, uc);
1909 GtkWidget *signalobject;
1911 if (ctrl->editbox.has_list) {
1912 #if !GTK_CHECK_VERSION(2,4,0)
1916 w = gtk_combo_new();
1917 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1918 uc->entry = GTK_COMBO(w)->entry;
1919 uc->list = GTK_COMBO(w)->list;
1920 signalobject = uc->entry;
1925 uc->listmodel = gtk_list_store_new(2, G_TYPE_INT,
1927 w = gtk_combo_box_entry_new_with_model
1928 (GTK_TREE_MODEL(uc->listmodel), 1);
1929 /* We cannot support password combo boxes. */
1930 assert(!ctrl->editbox.password);
1932 signalobject = uc->combo;
1935 w = gtk_entry_new();
1936 if (ctrl->editbox.password)
1937 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
1942 gtk_signal_connect(GTK_OBJECT(signalobject), "changed",
1943 GTK_SIGNAL_FUNC(editbox_changed), dp);
1944 gtk_signal_connect(GTK_OBJECT(signalobject), "key_press_event",
1945 GTK_SIGNAL_FUNC(editbox_key), dp);
1946 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_in_event",
1947 GTK_SIGNAL_FUNC(widget_focus), dp);
1948 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_out_event",
1949 GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1950 gtk_signal_connect(GTK_OBJECT(signalobject), "focus_out_event",
1951 GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1953 * Edit boxes, for some strange reason, have a minimum
1954 * width of 150 in GTK 1.2. We don't want this - we'd
1955 * rather the edit boxes acquired their natural width
1956 * from the column layout of the rest of the box.
1958 * Also, while we're here, we'll squirrel away the
1959 * edit box height so we can use that to centre its
1960 * label vertically beside it.
1962 gtk_widget_size_request(w, &req);
1963 gtk_widget_set_usize(w, 10, req.height);
1965 if (ctrl->generic.label) {
1966 GtkWidget *label, *container;
1968 label = gtk_label_new(ctrl->generic.label);
1970 shortcut_add(scs, label, ctrl->editbox.shortcut,
1971 SHORTCUT_FOCUS, uc->entry);
1973 container = columns_new(4);
1974 if (ctrl->editbox.percentwidth == 100) {
1975 columns_add(COLUMNS(container), label, 0, 1);
1976 columns_force_left_align(COLUMNS(container), label);
1977 columns_add(COLUMNS(container), w, 0, 1);
1979 gint percentages[2];
1980 percentages[1] = ctrl->editbox.percentwidth;
1981 percentages[0] = 100 - ctrl->editbox.percentwidth;
1982 columns_set_cols(COLUMNS(container), 2, percentages);
1983 columns_add(COLUMNS(container), label, 0, 1);
1984 columns_force_left_align(COLUMNS(container), label);
1985 columns_add(COLUMNS(container), w, 1, 1);
1986 /* Centre the label vertically. */
1987 gtk_widget_set_usize(label, -1, req.height);
1988 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1990 gtk_widget_show(label);
1998 case CTRL_FILESELECT:
1999 case CTRL_FONTSELECT:
2003 const char *browsebtn =
2004 (ctrl->generic.type == CTRL_FILESELECT ?
2005 "Browse..." : "Change...");
2007 gint percentages[] = { 75, 25 };
2009 columns_set_cols(COLUMNS(w), 2, percentages);
2011 if (ctrl->generic.label) {
2012 ww = gtk_label_new(ctrl->generic.label);
2013 columns_add(COLUMNS(w), ww, 0, 2);
2014 columns_force_left_align(COLUMNS(w), ww);
2015 gtk_widget_show(ww);
2016 shortcut_add(scs, ww,
2017 (ctrl->generic.type == CTRL_FILESELECT ?
2018 ctrl->fileselect.shortcut :
2019 ctrl->fontselect.shortcut),
2020 SHORTCUT_UCTRL, uc);
2024 uc->entry = ww = gtk_entry_new();
2025 gtk_widget_size_request(ww, &req);
2026 gtk_widget_set_usize(ww, 10, req.height);
2027 columns_add(COLUMNS(w), ww, 0, 1);
2028 gtk_widget_show(ww);
2030 uc->button = ww = gtk_button_new_with_label(browsebtn);
2031 columns_add(COLUMNS(w), ww, 1, 1);
2032 gtk_widget_show(ww);
2034 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
2035 GTK_SIGNAL_FUNC(editbox_key), dp);
2037 gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
2038 GTK_SIGNAL_FUNC(editbox_changed), dp);
2039 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
2040 GTK_SIGNAL_FUNC(widget_focus), dp);
2041 gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
2042 GTK_SIGNAL_FUNC(widget_focus), dp);
2043 gtk_signal_connect(GTK_OBJECT(ww), "clicked",
2044 GTK_SIGNAL_FUNC(filefont_clicked), dp);
2049 #if GTK_CHECK_VERSION(2,0,0)
2051 * First construct the list data store, with the right
2052 * number of columns.
2054 # if !GTK_CHECK_VERSION(2,4,0)
2055 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2056 * because combo boxes are still done the old GTK1 way.) */
2057 if (ctrl->listbox.height > 0)
2064 cols = ctrl->listbox.ncols;
2065 cols = cols ? cols : 1;
2066 types = snewn(1 + cols, GType);
2068 types[0] = G_TYPE_INT;
2069 for (i = 0; i < cols; i++)
2070 types[i+1] = G_TYPE_STRING;
2072 uc->listmodel = gtk_list_store_newv(1 + cols, types);
2079 * See if it's a drop-down list (non-editable combo
2082 if (ctrl->listbox.height == 0) {
2083 #if !GTK_CHECK_VERSION(2,4,0)
2085 * GTK1 and early-GTK2 option-menu style of
2088 uc->optmenu = w = gtk_option_menu_new();
2089 uc->menu = gtk_menu_new();
2090 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
2091 gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data",
2092 (gpointer)uc->optmenu);
2093 gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event",
2094 GTK_SIGNAL_FUNC(widget_focus), dp);
2097 * Late-GTK2 style using a GtkComboBox.
2099 GtkCellRenderer *cr;
2102 * Create a non-editable GtkComboBox (that is, not
2103 * its subclass GtkComboBoxEntry).
2105 w = gtk_combo_box_new_with_model
2106 (GTK_TREE_MODEL(uc->listmodel));
2110 * Tell it how to render a list item (i.e. which
2111 * column to look at in the list model).
2113 cr = gtk_cell_renderer_text_new();
2114 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), cr, TRUE);
2115 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), cr,
2119 * And tell it to notify us when the selection
2122 g_signal_connect(G_OBJECT(w), "changed",
2123 G_CALLBACK(droplist_selchange), dp);
2126 #if !GTK_CHECK_VERSION(2,0,0)
2128 * GTK1-style full list box.
2130 uc->list = gtk_list_new();
2131 if (ctrl->listbox.multisel == 2) {
2132 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2133 GTK_SELECTION_EXTENDED);
2134 } else if (ctrl->listbox.multisel == 1) {
2135 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2136 GTK_SELECTION_MULTIPLE);
2138 gtk_list_set_selection_mode(GTK_LIST(uc->list),
2139 GTK_SELECTION_SINGLE);
2141 w = gtk_scrolled_window_new(NULL, NULL);
2142 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
2144 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
2146 GTK_POLICY_AUTOMATIC);
2147 uc->adj = gtk_scrolled_window_get_vadjustment
2148 (GTK_SCROLLED_WINDOW(w));
2150 gtk_widget_show(uc->list);
2151 gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed",
2152 GTK_SIGNAL_FUNC(list_selchange), dp);
2153 gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event",
2154 GTK_SIGNAL_FUNC(widget_focus), dp);
2157 * Adjust the height of the scrolled window to the
2158 * minimum given by the height parameter.
2160 * This piece of guesswork is a horrid hack based
2161 * on looking inside the GTK 1.2 sources
2162 * (specifically gtkviewport.c, which appears to be
2163 * the widget which provides the border around the
2164 * scrolling area). Anyone lets me know how I can
2165 * do this in a way which isn't at risk from GTK
2166 * upgrades, I'd be grateful.
2170 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
2171 gtk_widget_set_usize(w, 10,
2172 2*edge + (ctrl->listbox.height *
2173 get_listitemheight(w)));
2176 if (ctrl->listbox.draglist) {
2178 * GTK doesn't appear to make it easy to
2179 * implement a proper draggable list; so
2180 * instead I'm just going to have to put an Up
2181 * and a Down button to the right of the actual
2182 * list box. Ah well.
2184 GtkWidget *cols, *button;
2185 static const gint percentages[2] = { 80, 20 };
2187 cols = columns_new(4);
2188 columns_set_cols(COLUMNS(cols), 2, percentages);
2189 columns_add(COLUMNS(cols), w, 0, 1);
2191 button = gtk_button_new_with_label("Up");
2192 columns_add(COLUMNS(cols), button, 1, 1);
2193 gtk_widget_show(button);
2194 gtk_signal_connect(GTK_OBJECT(button), "clicked",
2195 GTK_SIGNAL_FUNC(draglist_up), dp);
2196 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
2197 GTK_SIGNAL_FUNC(widget_focus), dp);
2198 button = gtk_button_new_with_label("Down");
2199 columns_add(COLUMNS(cols), button, 1, 1);
2200 gtk_widget_show(button);
2201 gtk_signal_connect(GTK_OBJECT(button), "clicked",
2202 GTK_SIGNAL_FUNC(draglist_down), dp);
2203 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
2204 GTK_SIGNAL_FUNC(widget_focus), dp);
2210 * GTK2 treeview-based full list box.
2212 GtkTreeSelection *sel;
2215 * Create the list box itself, its columns, and
2216 * its containing scrolled window.
2218 w = gtk_tree_view_new_with_model
2219 (GTK_TREE_MODEL(uc->listmodel));
2220 g_object_set_data(G_OBJECT(uc->listmodel), "user-data",
2222 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2223 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
2224 gtk_tree_selection_set_mode
2225 (sel, ctrl->listbox.multisel ? GTK_SELECTION_MULTIPLE :
2226 GTK_SELECTION_SINGLE);
2228 gtk_signal_connect(GTK_OBJECT(w), "row-activated",
2229 GTK_SIGNAL_FUNC(listbox_doubleclick), dp);
2230 g_signal_connect(G_OBJECT(sel), "changed",
2231 G_CALLBACK(listbox_selchange), dp);
2233 if (ctrl->listbox.draglist) {
2234 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w), TRUE);
2235 g_signal_connect(G_OBJECT(uc->listmodel), "row-inserted",
2236 G_CALLBACK(listbox_reorder), dp);
2243 cols = ctrl->listbox.ncols;
2244 cols = cols ? cols : 1;
2245 for (i = 0; i < cols; i++) {
2246 GtkTreeViewColumn *column;
2247 GtkCellRenderer *cellrend;
2249 * It appears that GTK 2 doesn't leave us any
2250 * particularly sensible way to honour the
2251 * "percentages" specification in the ctrl
2254 cellrend = gtk_cell_renderer_text_new();
2255 if (!ctrl->listbox.hscroll) {
2256 gtk_object_set(GTK_OBJECT(cellrend),
2257 "ellipsize", PANGO_ELLIPSIZE_END,
2258 "ellipsize-set", TRUE,
2261 column = gtk_tree_view_column_new_with_attributes
2262 ("heading", cellrend, "text", i+1, (char *)NULL);
2263 gtk_tree_view_column_set_sizing
2264 (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2265 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2272 scroll = gtk_scrolled_window_new(NULL, NULL);
2273 gtk_scrolled_window_set_shadow_type
2274 (GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
2276 gtk_container_add(GTK_CONTAINER(scroll), w);
2277 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2278 GTK_POLICY_AUTOMATIC,
2280 gtk_widget_set_size_request
2282 ctrl->listbox.height * get_listitemheight(w));
2289 if (ctrl->generic.label) {
2290 GtkWidget *label, *container;
2293 label = gtk_label_new(ctrl->generic.label);
2295 shortcut_add(scs, label, ctrl->listbox.shortcut,
2298 container = columns_new(4);
2299 if (ctrl->listbox.percentwidth == 100) {
2300 columns_add(COLUMNS(container), label, 0, 1);
2301 columns_force_left_align(COLUMNS(container), label);
2302 columns_add(COLUMNS(container), w, 0, 1);
2304 gint percentages[2];
2305 percentages[1] = ctrl->listbox.percentwidth;
2306 percentages[0] = 100 - ctrl->listbox.percentwidth;
2307 columns_set_cols(COLUMNS(container), 2, percentages);
2308 columns_add(COLUMNS(container), label, 0, 1);
2309 columns_force_left_align(COLUMNS(container), label);
2310 columns_add(COLUMNS(container), w, 1, 1);
2311 /* Centre the label vertically. */
2312 gtk_widget_size_request(w, &req);
2313 gtk_widget_set_usize(label, -1, req.height);
2314 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2316 gtk_widget_show(label);
2326 * Wrapping text widgets don't sit well with the GTK
2327 * layout model, in which widgets state a minimum size
2328 * and the whole window then adjusts to the smallest
2329 * size it can sensibly take given its contents. A
2330 * wrapping text widget _has_ no clear minimum size;
2331 * instead it has a range of possibilities. It can be
2332 * one line deep but 2000 wide, or two lines deep and
2333 * 1000 pixels, or three by 867, or four by 500 and so
2334 * on. It can be as short as you like provided you
2335 * don't mind it being wide, or as narrow as you like
2336 * provided you don't mind it being tall.
2338 * Therefore, it fits very badly into the layout model.
2339 * Hence the only thing to do is pick a width and let
2340 * it choose its own number of lines. To do this I'm
2341 * going to cheat a little. All new wrapping text
2342 * widgets will be created with a minimal text content
2343 * "X"; then, after the rest of the dialog box is set
2344 * up and its size calculated, the text widgets will be
2345 * told their width and given their real text, which
2346 * will cause the size to be recomputed in the y
2347 * direction (because many of them will expand to more
2350 uc->text = w = gtk_label_new("X");
2351 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
2352 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
2354 gtk_signal_connect(GTK_OBJECT(w), "size-allocate",
2355 GTK_SIGNAL_FUNC(label_sizealloc), dp);
2361 columns_add(cols, w,
2362 COLUMN_START(ctrl->generic.column),
2363 COLUMN_SPAN(ctrl->generic.column));
2365 columns_force_left_align(cols, w);
2369 dlg_add_uctrl(dp, uc);
2376 struct dlgparam *dp;
2377 GtkNotebook *panels;
2379 #if !GTK_CHECK_VERSION(2,0,0)
2380 GtkWidget *treeitem;
2383 GtkTreePath *treepath;
2385 struct Shortcuts shortcuts;
2388 #if GTK_CHECK_VERSION(2,0,0)
2389 static void treeselection_changed(GtkTreeSelection *treeselection,
2392 struct selparam *sps = (struct selparam *)data, *sp;
2393 GtkTreeModel *treemodel;
2394 GtkTreeIter treeiter;
2398 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2401 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
2404 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2405 gtk_notebook_set_page(sp->panels, page_num);
2407 dlg_refresh(NULL, sp->dp);
2409 sp->dp->shortcuts = &sp->shortcuts;
2412 static void treeitem_sel(GtkItem *item, gpointer data)
2414 struct selparam *sp = (struct selparam *)data;
2417 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
2418 gtk_notebook_set_page(sp->panels, page_num);
2420 dlg_refresh(NULL, sp->dp);
2422 sp->dp->shortcuts = &sp->shortcuts;
2423 sp->dp->currtreeitem = sp->treeitem;
2427 static void window_destroy(GtkWidget *widget, gpointer data)
2432 #if !GTK_CHECK_VERSION(2,0,0)
2433 static int tree_grab_focus(struct dlgparam *dp)
2438 * See if any of the treeitems has the focus.
2441 for (i = 0; i < dp->ntreeitems; i++)
2442 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
2450 gtk_widget_grab_focus(dp->currtreeitem);
2455 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
2458 struct dlgparam *dp = (struct dlgparam *)data;
2460 gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus");
2462 * If there's a focused treeitem, we return FALSE to cause the
2463 * focus to move on to some totally other control. If not, we
2464 * focus the selected one.
2466 return tree_grab_focus(dp);
2470 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2472 struct dlgparam *dp = (struct dlgparam *)data;
2474 if (event->keyval == GDK_Escape && dp->cancelbutton) {
2475 gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked");
2479 if ((event->state & GDK_MOD1_MASK) &&
2480 (unsigned char)event->string[0] > 0 &&
2481 (unsigned char)event->string[0] <= 127) {
2482 int schr = (unsigned char)event->string[0];
2483 struct Shortcut *sc = &dp->shortcuts->sc[schr];
2485 switch (sc->action) {
2487 #if GTK_CHECK_VERSION(2,0,0)
2488 gtk_widget_grab_focus(sc->widget);
2490 tree_grab_focus(dp);
2493 case SHORTCUT_FOCUS:
2494 gtk_widget_grab_focus(sc->widget);
2496 case SHORTCUT_UCTRL:
2498 * We must do something sensible with a uctrl.
2499 * Precisely what this is depends on the type of
2502 switch (sc->uc->ctrl->generic.type) {
2505 /* Check boxes and buttons get the focus _and_ get toggled. */
2506 gtk_widget_grab_focus(sc->uc->toplevel);
2507 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel),
2510 case CTRL_FILESELECT:
2511 case CTRL_FONTSELECT:
2512 /* File/font selectors have their buttons pressed (ooer),
2513 * and focus transferred to the edit box. */
2514 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button),
2516 gtk_widget_grab_focus(sc->uc->entry);
2520 * Radio buttons are fun, because they have
2521 * multiple shortcuts. We must find whether the
2522 * activated shortcut is the shortcut for the whole
2523 * group, or for a particular button. In the former
2524 * case, we find the currently selected button and
2525 * focus it; in the latter, we focus-and-click the
2526 * button whose shortcut was pressed.
2528 if (schr == sc->uc->ctrl->radio.shortcut) {
2530 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2531 if (gtk_toggle_button_get_active
2532 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
2533 gtk_widget_grab_focus(sc->uc->buttons[i]);
2535 } else if (sc->uc->ctrl->radio.shortcuts) {
2537 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
2538 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
2539 gtk_widget_grab_focus(sc->uc->buttons[i]);
2540 gtk_signal_emit_by_name
2541 (GTK_OBJECT(sc->uc->buttons[i]), "clicked");
2547 #if !GTK_CHECK_VERSION(2,4,0)
2548 if (sc->uc->optmenu) {
2552 gtk_widget_grab_focus(sc->uc->optmenu);
2553 /* Option menus don't work using the "clicked" signal.
2554 * We need to manufacture a button press event :-/ */
2555 bev.type = GDK_BUTTON_PRESS;
2557 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu),
2558 "button_press_event",
2563 if (sc->uc->combo) {
2564 gtk_widget_grab_focus(sc->uc->combo);
2565 gtk_combo_box_popup(GTK_COMBO_BOX(sc->uc->combo));
2569 #if !GTK_CHECK_VERSION(2,0,0)
2572 * For GTK-1 style list boxes, we tell it to
2573 * focus one of its children, which appears to
2574 * do the Right Thing.
2576 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
2577 GTK_DIR_TAB_FORWARD);
2581 if (sc->uc->treeview) {
2582 gtk_widget_grab_focus(sc->uc->treeview);
2586 assert(!"We shouldn't get here");
2596 #if !GTK_CHECK_VERSION(2,0,0)
2597 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2599 struct dlgparam *dp = (struct dlgparam *)data;
2601 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2602 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2604 for (i = 0; i < dp->ntreeitems; i++)
2605 if (widget == dp->treeitems[i])
2607 if (i < dp->ntreeitems) {
2608 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2615 if (i < 0 || i >= dp->ntreeitems)
2616 break; /* nothing in that dir to select */
2618 * Determine if this tree item is visible.
2621 GtkWidget *w = dp->treeitems[i];
2623 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2624 if (!GTK_WIDGET_VISIBLE(w)) {
2631 j = i; /* got one */
2637 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2640 gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle");
2641 gtk_widget_grab_focus(dp->treeitems[j]);
2647 * It's nice for Left and Right to expand and collapse tree
2650 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2651 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2653 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2656 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2657 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2659 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2667 static void shortcut_highlight(GtkWidget *labelw, int chr)
2669 GtkLabel *label = GTK_LABEL(labelw);
2670 gchar *currstr, *pattern;
2673 gtk_label_get(label, &currstr);
2674 for (i = 0; currstr[i]; i++)
2675 if (tolower((unsigned char)currstr[i]) == chr) {
2678 pattern = dupprintf("%*s_", i, "");
2680 gtk_widget_size_request(GTK_WIDGET(label), &req);
2681 gtk_label_set_pattern(label, pattern);
2682 gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
2689 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2690 int chr, int action, void *ptr)
2692 if (chr == NO_SHORTCUT)
2695 chr = tolower((unsigned char)chr);
2697 assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2699 scs->sc[chr].action = action;
2701 if (action == SHORTCUT_FOCUS) {
2702 scs->sc[chr].uc = NULL;
2703 scs->sc[chr].widget = (GtkWidget *)ptr;
2705 scs->sc[chr].widget = NULL;
2706 scs->sc[chr].uc = (struct uctrl *)ptr;
2709 shortcut_highlight(labelw, chr);
2712 int get_listitemheight(GtkWidget *w)
2714 #if !GTK_CHECK_VERSION(2,0,0)
2715 GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2717 gtk_widget_size_request(listitem, &req);
2718 gtk_object_sink(GTK_OBJECT(listitem));
2722 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
2723 gtk_cell_renderer_get_size(cr, w, NULL, NULL, NULL, NULL, &height);
2724 g_object_ref(G_OBJECT(cr));
2725 gtk_object_sink(GTK_OBJECT(cr));
2726 g_object_unref(G_OBJECT(cr));
2731 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2733 #if !GTK_CHECK_VERSION(2,0,0)
2736 * In GTK 1, laying out the buttons at the bottom of the
2737 * configuration box is nice and easy, because a GtkDialog's
2738 * action_area is a GtkHBox which stretches to cover the full
2739 * width of the dialog. So we just put our Columns widget
2740 * straight into that hbox, and it ends up just where we want
2743 gtk_box_pack_start(GTK_BOX(dlg->action_area), w, TRUE, TRUE, 0);
2747 * In GTK 2, the action area is now a GtkHButtonBox and its
2748 * layout behaviour seems to be different: it doesn't stretch
2749 * to cover the full width of the window, but instead finds its
2750 * own preferred width and right-aligns that within the window.
2751 * This isn't what we want, because we have both left-aligned
2752 * and right-aligned buttons coming out of the above call to
2753 * layout_ctrls(), and right-aligning the whole thing will
2754 * result in the former being centred and looking weird.
2756 * So instead we abandon the dialog's action area completely:
2757 * we gtk_widget_hide() it in the below code, and we also call
2758 * gtk_dialog_set_has_separator() to remove the separator above
2759 * it. We then insert our own action area into the end of the
2760 * dialog's main vbox, and add our own separator above that.
2762 * (Ideally, if we were a native GTK app, we would use the
2763 * GtkHButtonBox's _own_ innate ability to support one set of
2764 * buttons being right-aligned and one left-aligned. But to do
2765 * that here, we would have to either (a) pick apart our cross-
2766 * platform layout structures and treat them specially for this
2767 * particular set of controls, which would be painful, or else
2768 * (b) develop a special and simpler cross-platform
2769 * representation for these particular controls, and introduce
2770 * special-case code into all the _other_ platforms to handle
2771 * it. Neither appeals. Therefore, I regretfully discard the
2772 * GTKHButtonBox and go it alone.)
2776 align = gtk_alignment_new(0, 0, 1, 1);
2777 gtk_container_add(GTK_CONTAINER(align), w);
2779 * The purpose of this GtkAlignment is to provide padding
2780 * around the buttons. The padding we use is twice the padding
2781 * used in our GtkColumns, because we nest two GtkColumns most
2782 * of the time (one separating the tree view from the main
2783 * controls, and another for the main controls themselves).
2785 #if GTK_CHECK_VERSION(2,4,0)
2786 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2788 gtk_widget_show(align);
2789 gtk_box_pack_end(GTK_BOX(dlg->vbox), align, FALSE, TRUE, 0);
2790 w = gtk_hseparator_new();
2791 gtk_box_pack_end(GTK_BOX(dlg->vbox), w, FALSE, TRUE, 0);
2793 gtk_widget_hide(dlg->action_area);
2794 gtk_dialog_set_has_separator(dlg, FALSE);
2798 int do_config_box(const char *title, Conf *conf, int midsession,
2801 GtkWidget *window, *hbox, *vbox, *cols, *label,
2802 *tree, *treescroll, *panels, *panelvbox;
2803 int index, level, protocol;
2804 struct controlbox *ctrlbox;
2806 #if GTK_CHECK_VERSION(2,0,0)
2807 GtkTreeStore *treestore;
2808 GtkCellRenderer *treerenderer;
2809 GtkTreeViewColumn *treecolumn;
2810 GtkTreeSelection *treeselection;
2811 GtkTreeIter treeiterlevels[8];
2813 GtkTreeItem *treeitemlevels[8];
2814 GtkTree *treelevels[8];
2817 struct Shortcuts scs;
2819 struct selparam *selparams = NULL;
2820 int nselparams = 0, selparamsize = 0;
2824 for (index = 0; index < lenof(scs.sc); index++) {
2825 scs.sc[index].action = SHORTCUT_EMPTY;
2828 window = gtk_dialog_new();
2830 ctrlbox = ctrl_new_box();
2831 protocol = conf_get_int(conf, CONF_protocol);
2832 setup_config_box(ctrlbox, midsession, protocol, protcfginfo);
2833 unix_setup_config_box(ctrlbox, midsession, protocol);
2834 gtk_setup_config_box(ctrlbox, midsession, window);
2836 gtk_window_set_title(GTK_WINDOW(window), title);
2837 hbox = gtk_hbox_new(FALSE, 4);
2838 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0);
2839 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2840 gtk_widget_show(hbox);
2841 vbox = gtk_vbox_new(FALSE, 4);
2842 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2843 gtk_widget_show(vbox);
2844 cols = columns_new(4);
2845 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2846 gtk_widget_show(cols);
2847 label = gtk_label_new("Category:");
2848 columns_add(COLUMNS(cols), label, 0, 1);
2849 columns_force_left_align(COLUMNS(cols), label);
2850 gtk_widget_show(label);
2851 treescroll = gtk_scrolled_window_new(NULL, NULL);
2852 #if GTK_CHECK_VERSION(2,0,0)
2853 treestore = gtk_tree_store_new
2854 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2855 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2856 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2857 treerenderer = gtk_cell_renderer_text_new();
2858 treecolumn = gtk_tree_view_column_new_with_attributes
2859 ("Label", treerenderer, "text", 0, NULL);
2860 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2861 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2862 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2863 gtk_container_add(GTK_CONTAINER(treescroll), tree);
2865 tree = gtk_tree_new();
2866 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2867 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2868 gtk_signal_connect(GTK_OBJECT(tree), "focus",
2869 GTK_SIGNAL_FUNC(tree_focus), &dp);
2871 gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event",
2872 GTK_SIGNAL_FUNC(widget_focus), &dp);
2873 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2874 gtk_widget_show(treescroll);
2875 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2876 panels = gtk_notebook_new();
2877 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2878 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2879 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2880 gtk_widget_show(panels);
2885 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2886 struct controlset *s = ctrlbox->ctrlsets[index];
2889 if (!*s->pathname) {
2890 w = layout_ctrls(&dp, &scs, s, GTK_WINDOW(window));
2892 set_dialog_action_area(GTK_DIALOG(window), w);
2894 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2895 if (j != INT_MAX) { /* add to treeview, start new panel */
2897 #if GTK_CHECK_VERSION(2,0,0)
2898 GtkTreeIter treeiter;
2900 GtkWidget *treeitem;
2905 * We expect never to find an implicit path
2906 * component. For example, we expect never to see
2907 * A/B/C followed by A/D/E, because that would
2908 * _implicitly_ create A/D. All our path prefixes
2909 * are expected to contain actual controls and be
2910 * selectable in the treeview; so we would expect
2911 * to see A/D _explicitly_ before encountering
2914 assert(j == ctrl_path_elements(s->pathname) - 1);
2916 c = strrchr(s->pathname, '/');
2924 first = (panelvbox == NULL);
2926 panelvbox = gtk_vbox_new(FALSE, 4);
2927 gtk_widget_show(panelvbox);
2928 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
2933 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
2935 gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num);
2938 if (nselparams >= selparamsize) {
2940 selparams = sresize(selparams, selparamsize,
2943 selparams[nselparams].dp = &dp;
2944 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
2945 selparams[nselparams].panel = panelvbox;
2946 selparams[nselparams].shortcuts = scs; /* structure copy */
2948 assert(j-1 < level);
2950 #if GTK_CHECK_VERSION(2,0,0)
2952 /* treeiterlevels[j-1] will always be valid because we
2953 * don't allow implicit path components; see above.
2955 gtk_tree_store_append(treestore, &treeiter,
2956 &treeiterlevels[j-1]);
2958 gtk_tree_store_append(treestore, &treeiter, NULL);
2959 gtk_tree_store_set(treestore, &treeiter,
2961 TREESTORE_PARAMS, nselparams,
2963 treeiterlevels[j] = treeiter;
2965 selparams[nselparams].depth = j;
2967 selparams[nselparams].treepath =
2968 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
2969 &treeiterlevels[j-1]);
2971 * We are going to collapse all tree branches
2972 * at depth greater than 2, but not _yet_; see
2973 * the comment at the call to
2974 * gtk_tree_view_collapse_row below.
2976 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree),
2977 selparams[nselparams].treepath,
2980 selparams[nselparams].treepath = NULL;
2983 treeitem = gtk_tree_item_new_with_label(c);
2985 if (!treelevels[j-1]) {
2986 treelevels[j-1] = GTK_TREE(gtk_tree_new());
2987 gtk_tree_item_set_subtree
2988 (treeitemlevels[j-1],
2989 GTK_WIDGET(treelevels[j-1]));
2991 gtk_tree_item_expand(treeitemlevels[j-1]);
2993 gtk_tree_item_collapse(treeitemlevels[j-1]);
2995 gtk_tree_append(treelevels[j-1], treeitem);
2997 gtk_tree_append(GTK_TREE(tree), treeitem);
2999 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
3000 treelevels[j] = NULL;
3002 gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event",
3003 GTK_SIGNAL_FUNC(tree_key_press), &dp);
3004 gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event",
3005 GTK_SIGNAL_FUNC(widget_focus), &dp);
3007 gtk_widget_show(treeitem);
3010 gtk_tree_select_child(GTK_TREE(tree), treeitem);
3011 selparams[nselparams].treeitem = treeitem;
3018 w = layout_ctrls(&dp, &selparams[nselparams-1].shortcuts, s, NULL);
3019 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
3024 #if GTK_CHECK_VERSION(2,0,0)
3030 * We want our tree view to come up with all branches at
3031 * depth 2 or more collapsed. However, if we start off
3032 * with those branches collapsed, then the tree view's
3033 * size request will be calculated based on the width of
3034 * the collapsed tree. So instead we start with them all
3035 * expanded; then we ask for the current size request,
3036 * collapse the relevant rows, and force the width to the
3037 * value we just computed. This arranges that the tree
3038 * view is wide enough to have all branches expanded
3042 gtk_widget_size_request(tree, &req);
3044 for (i = 0; i < nselparams; i++)
3045 if (selparams[i].depth >= 2)
3046 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree),
3047 selparams[i].treepath);
3049 gtk_widget_set_size_request(tree, req.width, -1);
3053 #if GTK_CHECK_VERSION(2,0,0)
3054 g_signal_connect(G_OBJECT(treeselection), "changed",
3055 G_CALLBACK(treeselection_changed), selparams);
3057 dp.ntreeitems = nselparams;
3058 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
3060 for (index = 0; index < nselparams; index++) {
3061 gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
3062 GTK_SIGNAL_FUNC(treeitem_sel),
3064 dp.treeitems[index] = selparams[index].treeitem;
3069 dlg_refresh(NULL, &dp);
3071 dp.shortcuts = &selparams[0].shortcuts;
3072 #if !GTK_CHECK_VERSION(2,0,0)
3073 dp.currtreeitem = dp.treeitems[0];
3075 dp.lastfocus = NULL;
3081 extern void set_window_icon(GtkWidget *window,
3082 const char *const *const *icon,
3084 extern const char *const *const cfg_icon[];
3085 extern const int n_cfg_icon;
3086 set_window_icon(window, cfg_icon, n_cfg_icon);
3089 #if !GTK_CHECK_VERSION(2,0,0)
3090 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
3093 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
3095 GTK_POLICY_AUTOMATIC);
3096 gtk_widget_show(tree);
3098 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3099 gtk_widget_show(window);
3102 * Set focus into the first available control.
3104 for (index = 0; index < ctrlbox->nctrlsets; index++) {
3105 struct controlset *s = ctrlbox->ctrlsets[index];
3110 for (j = 0; j < s->ncontrols; j++)
3111 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
3112 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
3113 s->ctrls[j]->generic.type != CTRL_TEXT) {
3114 dlg_set_focus(s->ctrls[j], &dp);
3115 dp.lastfocus = s->ctrls[j];
3124 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3125 GTK_SIGNAL_FUNC(window_destroy), NULL);
3126 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3127 GTK_SIGNAL_FUNC(win_key_press), &dp);
3137 static void messagebox_handler(union control *ctrl, void *dlg,
3138 void *data, int event)
3140 if (event == EVENT_ACTION)
3141 dlg_end(dlg, ctrl->generic.context.i);
3143 int messagebox(GtkWidget *parentwin, const char *title, const char *msg,
3146 GtkWidget *window, *w0, *w1;
3147 struct controlbox *ctrlbox;
3148 struct controlset *s0, *s1;
3151 struct Shortcuts scs;
3157 for (index = 0; index < lenof(scs.sc); index++) {
3158 scs.sc[index].action = SHORTCUT_EMPTY;
3161 ctrlbox = ctrl_new_box();
3164 va_start(ap, minwid);
3165 while (va_arg(ap, char *) != NULL) {
3167 (void) va_arg(ap, int); /* shortcut */
3168 (void) va_arg(ap, int); /* normal/default/cancel */
3169 (void) va_arg(ap, int); /* end value */
3173 s0 = ctrl_getset(ctrlbox, "", "", "");
3174 c = ctrl_columns(s0, 2, 50, 50);
3175 c->columns.ncols = s0->ncolumns = ncols;
3176 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
3177 for (index = 0; index < ncols; index++)
3178 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
3179 va_start(ap, minwid);
3182 char *title = va_arg(ap, char *);
3183 int shortcut, type, value;
3186 shortcut = va_arg(ap, int);
3187 type = va_arg(ap, int);
3188 value = va_arg(ap, int);
3189 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
3190 messagebox_handler, I(value));
3191 c->generic.column = index++;
3193 c->button.isdefault = TRUE;
3195 c->button.iscancel = TRUE;
3199 s1 = ctrl_getset(ctrlbox, "x", "", "");
3200 ctrl_text(s1, msg, HELPCTX(no_help));
3202 window = gtk_dialog_new();
3203 gtk_window_set_title(GTK_WINDOW(window), title);
3204 w0 = layout_ctrls(&dp, &scs, s0, GTK_WINDOW(window));
3205 set_dialog_action_area(GTK_DIALOG(window), w0);
3206 gtk_widget_show(w0);
3207 w1 = layout_ctrls(&dp, &scs, s1, GTK_WINDOW(window));
3208 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3209 gtk_widget_set_usize(w1, minwid+20, -1);
3210 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
3212 gtk_widget_show(w1);
3214 dp.shortcuts = &scs;
3215 dp.lastfocus = NULL;
3219 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3221 set_transient_window_pos(parentwin, window);
3222 gtk_window_set_transient_for(GTK_WINDOW(window),
3223 GTK_WINDOW(parentwin));
3225 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3226 gtk_widget_show(window);
3228 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3229 GTK_SIGNAL_FUNC(window_destroy), NULL);
3230 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3231 GTK_SIGNAL_FUNC(win_key_press), &dp);
3236 ctrl_free_box(ctrlbox);
3241 int string_width(const char *text)
3243 GtkWidget *label = gtk_label_new(text);
3245 gtk_widget_size_request(label, &req);
3246 gtk_object_sink(GTK_OBJECT(label));
3250 int reallyclose(void *frontend)
3252 char *title = dupcat(appname, " Exit Confirmation", NULL);
3253 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
3254 title, "Are you sure you want to close this session?",
3255 string_width("Most of the width of the above text"),
3263 int verify_ssh_host_key(void *frontend, char *host, int port,
3264 const char *keytype, char *keystr, char *fingerprint,
3265 void (*callback)(void *ctx, int result), void *ctx)
3267 static const char absenttxt[] =
3268 "The server's host key is not cached. You have no guarantee "
3269 "that the server is the computer you think it is.\n"
3270 "The server's %s key fingerprint is:\n"
3272 "If you trust this host, press \"Accept\" to add the key to "
3273 "PuTTY's cache and carry on connecting.\n"
3274 "If you want to carry on connecting just once, without "
3275 "adding the key to the cache, press \"Connect Once\".\n"
3276 "If you do not trust this host, press \"Cancel\" to abandon the "
3278 static const char wrongtxt[] =
3279 "WARNING - POTENTIAL SECURITY BREACH!\n"
3280 "The server's host key does not match the one PuTTY has "
3281 "cached. This means that either the server administrator "
3282 "has changed the host key, or you have actually connected "
3283 "to another computer pretending to be the server.\n"
3284 "The new %s key fingerprint is:\n"
3286 "If you were expecting this change and trust the new key, "
3287 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3288 "If you want to carry on connecting but without updating "
3289 "the cache, press \"Connect Once\".\n"
3290 "If you want to abandon the connection completely, press "
3291 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3299 ret = verify_host_key(host, port, keytype, keystr);
3301 if (ret == 0) /* success - key matched OK */
3304 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
3306 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3307 "PuTTY Security Alert", text,
3308 string_width(fingerprint),
3309 "Accept", 'a', 0, 2,
3310 "Connect Once", 'o', 0, 1,
3311 "Cancel", 'c', -1, 0,
3317 store_host_key(host, port, keytype, keystr);
3318 return 1; /* continue with connection */
3319 } else if (ret == 1)
3320 return 1; /* continue with connection */
3321 return 0; /* do not continue with connection */
3325 * Ask whether the selected algorithm is acceptable (since it was
3326 * below the configured 'warn' threshold).
3328 int askalg(void *frontend, const char *algtype, const char *algname,
3329 void (*callback)(void *ctx, int result), void *ctx)
3331 static const char msg[] =
3332 "The first %s supported by the server is "
3333 "%s, which is below the configured warning threshold.\n"
3334 "Continue with connection?";
3338 text = dupprintf(msg, algtype, algname);
3339 ret = messagebox(GTK_WIDGET(get_window(frontend)),
3340 "PuTTY Security Alert", text,
3341 string_width("Continue with connection?"),
3354 void old_keyfile_warning(void)
3357 * This should never happen on Unix. We hope.
3361 void fatal_message_box(void *window, const char *msg)
3363 messagebox(window, "PuTTY Fatal Error", msg,
3364 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3365 "OK", 'o', 1, 1, NULL);
3368 void nonfatal_message_box(void *window, const char *msg)
3370 messagebox(window, "PuTTY Error", msg,
3371 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3372 "OK", 'o', 1, 1, NULL);
3375 void fatalbox(const char *p, ...)
3380 msg = dupvprintf(p, ap);
3382 fatal_message_box(NULL, msg);
3387 void nonfatal(const char *p, ...)
3392 msg = dupvprintf(p, ap);
3394 nonfatal_message_box(NULL, msg);
3398 static GtkWidget *aboutbox = NULL;
3400 static void about_close_clicked(GtkButton *button, gpointer data)
3402 gtk_widget_destroy(aboutbox);
3406 static void licence_clicked(GtkButton *button, gpointer data)
3410 const char *licence =
3411 "Copyright 1997-2015 Simon Tatham.\n\n"
3413 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3414 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3415 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3416 "Markus Kuhn, Colin Watson, Christopher Staite, and CORE SDI S.A.\n\n"
3418 "Permission is hereby granted, free of charge, to any person "
3419 "obtaining a copy of this software and associated documentation "
3420 "files (the ""Software""), to deal in the Software without restriction, "
3421 "including without limitation the rights to use, copy, modify, merge, "
3422 "publish, distribute, sublicense, and/or sell copies of the Software, "
3423 "and to permit persons to whom the Software is furnished to do so, "
3424 "subject to the following conditions:\n\n"
3426 "The above copyright notice and this permission notice shall be "
3427 "included in all copies or substantial portions of the Software.\n\n"
3429 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3430 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3431 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3432 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3433 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3434 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3435 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3436 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3437 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3438 "OTHER DEALINGS IN THE SOFTWARE.";
3440 title = dupcat(appname, " Licence", NULL);
3441 assert(aboutbox != NULL);
3442 messagebox(aboutbox, title, licence,
3443 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3444 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3445 "OK", 'o', 1, 1, NULL);
3449 void about_box(void *window)
3455 gtk_widget_grab_focus(aboutbox);
3459 aboutbox = gtk_dialog_new();
3460 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
3461 title = dupcat("About ", appname, NULL);
3462 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
3465 w = gtk_button_new_with_label("Close");
3466 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
3467 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
3468 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
3469 w, FALSE, FALSE, 0);
3470 gtk_signal_connect(GTK_OBJECT(w), "clicked",
3471 GTK_SIGNAL_FUNC(about_close_clicked), NULL);
3474 w = gtk_button_new_with_label("View Licence");
3475 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
3476 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
3477 w, FALSE, FALSE, 0);
3478 gtk_signal_connect(GTK_OBJECT(w), "clicked",
3479 GTK_SIGNAL_FUNC(licence_clicked), NULL);
3482 w = gtk_label_new(appname);
3483 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
3484 w, FALSE, FALSE, 0);
3487 w = gtk_label_new(ver);
3488 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
3489 w, FALSE, FALSE, 5);
3492 w = gtk_label_new("Copyright 1997-2015 Simon Tatham. All rights reserved");
3493 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
3494 w, FALSE, FALSE, 5);
3497 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
3498 gtk_window_set_transient_for(GTK_WINDOW(aboutbox),
3499 GTK_WINDOW(window));
3500 gtk_widget_show(aboutbox);
3503 struct eventlog_stuff {
3504 GtkWidget *parentwin, *window;
3505 struct controlbox *eventbox;
3506 struct Shortcuts scs;
3508 union control *listctrl;
3510 int nevents, negsize;
3513 int ignore_selchange;
3516 static void eventlog_destroy(GtkWidget *widget, gpointer data)
3518 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3523 dlg_cleanup(&es->dp);
3524 ctrl_free_box(es->eventbox);
3526 static void eventlog_ok_handler(union control *ctrl, void *dlg,
3527 void *data, int event)
3529 if (event == EVENT_ACTION)
3532 static void eventlog_list_handler(union control *ctrl, void *dlg,
3533 void *data, int event)
3535 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3537 if (event == EVENT_REFRESH) {
3540 dlg_update_start(ctrl, dlg);
3541 dlg_listbox_clear(ctrl, dlg);
3542 for (i = 0; i < es->nevents; i++) {
3543 dlg_listbox_add(ctrl, dlg, es->events[i]);
3545 dlg_update_done(ctrl, dlg);
3546 } else if (event == EVENT_SELCHANGE) {
3551 * If this SELCHANGE event is happening as a result of
3552 * deliberate deselection because someone else has grabbed
3553 * the selection, the last thing we want to do is pre-empt
3556 if (es->ignore_selchange)
3560 * Construct the data to use as the selection.
3565 for (i = 0; i < es->nevents; i++) {
3566 if (dlg_listbox_issel(ctrl, dlg, i)) {
3567 int extralen = strlen(es->events[i]);
3569 if (es->sellen + extralen + 2 > selsize) {
3570 selsize = es->sellen + extralen + 512;
3571 es->seldata = sresize(es->seldata, selsize, char);
3574 strcpy(es->seldata + es->sellen, es->events[i]);
3575 es->sellen += extralen;
3576 es->seldata[es->sellen++] = '\n';
3580 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
3581 GDK_CURRENT_TIME)) {
3582 extern GdkAtom compound_text_atom;
3584 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3585 GDK_SELECTION_TYPE_STRING, 1);
3586 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
3587 compound_text_atom, 1);
3593 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
3594 guint info, guint time_stamp, gpointer data)
3596 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3598 gtk_selection_data_set(seldata, seldata->target, 8,
3599 (unsigned char *)es->seldata, es->sellen);
3602 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
3605 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
3609 * Deselect everything in the list box.
3611 uc = dlg_find_byctrl(&es->dp, es->listctrl);
3612 es->ignore_selchange = 1;
3613 #if !GTK_CHECK_VERSION(2,0,0)
3615 gtk_list_unselect_all(GTK_LIST(uc->list));
3617 assert(uc->treeview);
3618 gtk_tree_selection_unselect_all
3619 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc->treeview)));
3621 es->ignore_selchange = 0;
3629 void showeventlog(void *estuff, void *parentwin)
3631 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3632 GtkWidget *window, *w0, *w1;
3633 GtkWidget *parent = GTK_WIDGET(parentwin);
3634 struct controlset *s0, *s1;
3640 gtk_widget_grab_focus(es->window);
3646 for (index = 0; index < lenof(es->scs.sc); index++) {
3647 es->scs.sc[index].action = SHORTCUT_EMPTY;
3650 es->eventbox = ctrl_new_box();
3652 s0 = ctrl_getset(es->eventbox, "", "", "");
3653 ctrl_columns(s0, 3, 33, 34, 33);
3654 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
3655 eventlog_ok_handler, P(NULL));
3656 c->button.column = 1;
3657 c->button.isdefault = TRUE;
3659 s1 = ctrl_getset(es->eventbox, "x", "", "");
3660 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3661 eventlog_list_handler, P(es));
3662 c->listbox.height = 10;
3663 c->listbox.multisel = 2;
3664 c->listbox.ncols = 3;
3665 c->listbox.percentages = snewn(3, int);
3666 c->listbox.percentages[0] = 25;
3667 c->listbox.percentages[1] = 10;
3668 c->listbox.percentages[2] = 65;
3670 es->window = window = gtk_dialog_new();
3671 title = dupcat(appname, " Event Log", NULL);
3672 gtk_window_set_title(GTK_WINDOW(window), title);
3674 w0 = layout_ctrls(&es->dp, &es->scs, s0, GTK_WINDOW(window));
3675 set_dialog_action_area(GTK_DIALOG(window), w0);
3676 gtk_widget_show(w0);
3677 w1 = layout_ctrls(&es->dp, &es->scs, s1, GTK_WINDOW(window));
3678 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3679 gtk_widget_set_usize(w1, 20 +
3680 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3681 " IS QUITE LONG 'COS SSH LOG ENTRIES"
3683 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
3685 gtk_widget_show(w1);
3688 es->dp.shortcuts = &es->scs;
3689 es->dp.lastfocus = NULL;
3691 es->dp.window = window;
3693 dlg_refresh(NULL, &es->dp);
3696 set_transient_window_pos(parent, window);
3697 gtk_window_set_transient_for(GTK_WINDOW(window),
3698 GTK_WINDOW(parent));
3700 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3701 gtk_widget_show(window);
3703 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3704 GTK_SIGNAL_FUNC(eventlog_destroy), es);
3705 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3706 GTK_SIGNAL_FUNC(win_key_press), &es->dp);
3707 gtk_signal_connect(GTK_OBJECT(window), "selection_get",
3708 GTK_SIGNAL_FUNC(eventlog_selection_get), es);
3709 gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event",
3710 GTK_SIGNAL_FUNC(eventlog_selection_clear), es);
3713 void *eventlogstuff_new(void)
3715 struct eventlog_stuff *es;
3716 es = snew(struct eventlog_stuff);
3717 memset(es, 0, sizeof(*es));
3721 void logevent_dlg(void *estuff, const char *string)
3723 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3728 if (es->nevents >= es->negsize) {
3730 es->events = sresize(es->events, es->negsize, char *);
3734 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3736 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3737 strcpy(es->events[es->nevents], timebuf);
3738 strcat(es->events[es->nevents], string);
3740 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3745 int askappend(void *frontend, Filename *filename,
3746 void (*callback)(void *ctx, int result), void *ctx)
3748 static const char msgtemplate[] =
3749 "The session log file \"%.*s\" already exists. "
3750 "You can overwrite it with a new session log, "
3751 "append your session log to the end of it, "
3752 "or disable session logging for this session.";
3757 message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
3758 mbtitle = dupprintf("%s Log to File", appname);
3760 mbret = messagebox(get_window(frontend), mbtitle, message,
3761 string_width("LINE OF TEXT SUITABLE FOR THE"
3762 " ASKAPPEND WIDTH"),
3763 "Overwrite", 'o', 1, 2,
3764 "Append", 'a', 0, 1,
3765 "Disable", 'd', -1, 0,