2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
6 * TODO when porting to GTK 2.0:
8 * - GtkList is deprecated and we should switch to GtkTreeView instead
10 * - GtkLabel has a built-in mnemonic scheme, so we should at
11 * least consider switching to that from the current adhockery.
19 #include <gdk/gdkkeysyms.h>
22 #include <X11/Xutil.h>
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
42 struct Shortcut sc[128];
49 int privdata_needs_free;
50 GtkWidget **buttons; int nbuttons; /* for radio buttons */
51 GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */
52 GtkWidget *button; /* for filesel, fontsel */
53 GtkWidget *list; /* for combobox, listbox */
54 GtkWidget *menu; /* for optionmenu (==droplist) */
55 GtkWidget *optmenu; /* also for optionmenu */
56 GtkWidget *text; /* for text */
57 GtkWidget *label; /* for dlg_label_change */
58 GtkAdjustment *adj; /* for the scrollbar in a list box */
64 tree234 *byctrl, *bywidget;
66 struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
67 /* `flags' are set to indicate when a GTK signal handler is being called
68 * due to automatic processing and should not flag a user event. */
70 struct Shortcuts *shortcuts;
71 GtkWidget *window, *cancelbutton;
72 union control *currfocus, *lastfocus;
73 #if !GTK_CHECK_VERSION(2,0,0)
74 GtkWidget *currtreeitem, **treeitems;
79 #define FLAG_UPDATING_COMBO_LIST 1
81 enum { /* values for Shortcut.action */
82 SHORTCUT_EMPTY, /* no shortcut on this key */
83 SHORTCUT_TREE, /* focus a tree item */
84 SHORTCUT_FOCUS, /* focus the supplied widget */
85 SHORTCUT_UCTRL, /* do something sane with uctrl */
86 SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */
87 SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */
90 #if GTK_CHECK_VERSION(2,0,0)
101 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
103 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
104 int chr, int action, void *ptr);
105 static void shortcut_highlight(GtkWidget *label, int chr);
106 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
108 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
110 static gboolean listitem_button(GtkWidget *item, GdkEventButton *event,
112 static void menuitem_activate(GtkMenuItem *item, gpointer data);
113 static void coloursel_ok(GtkButton *button, gpointer data);
114 static void coloursel_cancel(GtkButton *button, gpointer data);
115 static void window_destroy(GtkWidget *widget, gpointer data);
117 static int uctrl_cmp_byctrl(void *av, void *bv)
119 struct uctrl *a = (struct uctrl *)av;
120 struct uctrl *b = (struct uctrl *)bv;
121 if (a->ctrl < b->ctrl)
123 else if (a->ctrl > b->ctrl)
128 static int uctrl_cmp_byctrl_find(void *av, void *bv)
130 union control *a = (union control *)av;
131 struct uctrl *b = (struct uctrl *)bv;
134 else if (a > b->ctrl)
139 static int uctrl_cmp_bywidget(void *av, void *bv)
141 struct uctrl *a = (struct uctrl *)av;
142 struct uctrl *b = (struct uctrl *)bv;
143 if (a->toplevel < b->toplevel)
145 else if (a->toplevel > b->toplevel)
150 static int uctrl_cmp_bywidget_find(void *av, void *bv)
152 GtkWidget *a = (GtkWidget *)av;
153 struct uctrl *b = (struct uctrl *)bv;
156 else if (a > b->toplevel)
161 static void dlg_init(struct dlgparam *dp)
163 dp->byctrl = newtree234(uctrl_cmp_byctrl);
164 dp->bywidget = newtree234(uctrl_cmp_bywidget);
165 dp->coloursel_result.ok = FALSE;
166 dp->window = dp->cancelbutton = NULL;
167 #if !GTK_CHECK_VERSION(2,0,0)
168 dp->treeitems = NULL;
169 dp->currtreeitem = NULL;
172 dp->currfocus = NULL;
175 static void dlg_cleanup(struct dlgparam *dp)
179 freetree234(dp->byctrl); /* doesn't free the uctrls inside */
181 while ( (uc = index234(dp->bywidget, 0)) != NULL) {
182 del234(dp->bywidget, uc);
183 if (uc->privdata_needs_free)
188 freetree234(dp->bywidget);
190 #if !GTK_CHECK_VERSION(2,0,0)
191 sfree(dp->treeitems);
195 static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
197 add234(dp->byctrl, uc);
198 add234(dp->bywidget, uc);
201 static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
205 return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
208 static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
210 struct uctrl *ret = NULL;
214 ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
222 void *dlg_get_privdata(union control *ctrl, void *dlg)
224 struct dlgparam *dp = (struct dlgparam *)dlg;
225 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
229 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
231 struct dlgparam *dp = (struct dlgparam *)dlg;
232 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
234 uc->privdata_needs_free = FALSE;
237 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
239 struct dlgparam *dp = (struct dlgparam *)dlg;
240 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
242 * This is an internal allocation routine, so it's allowed to
243 * use smalloc directly.
245 uc->privdata = smalloc(size);
246 uc->privdata_needs_free = FALSE;
250 union control *dlg_last_focused(union control *ctrl, void *dlg)
252 struct dlgparam *dp = (struct dlgparam *)dlg;
253 if (dp->currfocus != ctrl)
254 return dp->currfocus;
256 return dp->lastfocus;
259 void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
261 struct dlgparam *dp = (struct dlgparam *)dlg;
262 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
263 assert(uc->ctrl->generic.type == CTRL_RADIO);
264 assert(uc->buttons != NULL);
265 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
268 int dlg_radiobutton_get(union control *ctrl, void *dlg)
270 struct dlgparam *dp = (struct dlgparam *)dlg;
271 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
274 assert(uc->ctrl->generic.type == CTRL_RADIO);
275 assert(uc->buttons != NULL);
276 for (i = 0; i < uc->nbuttons; i++)
277 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
279 return 0; /* got to return something */
282 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
284 struct dlgparam *dp = (struct dlgparam *)dlg;
285 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
286 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
287 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
290 int dlg_checkbox_get(union control *ctrl, void *dlg)
292 struct dlgparam *dp = (struct dlgparam *)dlg;
293 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
294 assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
295 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
298 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
300 struct dlgparam *dp = (struct dlgparam *)dlg;
301 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
302 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
303 assert(uc->entry != NULL);
305 * GTK 2 implements gtk_entry_set_text by means of two separate
306 * operations: first delete the previous text leaving the empty
307 * string, then insert the new text. This causes two calls to
308 * the "changed" signal.
310 * The first call to "changed", if allowed to proceed normally,
311 * will cause an EVENT_VALCHANGE event on the edit box, causing
312 * a call to dlg_editbox_get() which will read the empty string
313 * out of the GtkEntry - and promptly write it straight into
314 * the Config structure, which is precisely where our `text'
315 * pointer is probably pointing, so the second editing
316 * operation will insert that instead of the string we
317 * originally asked for.
319 * Hence, we must block our "changed" signal handler for the
320 * duration of this call to gtk_entry_set_text.
322 g_signal_handler_block(uc->entry, uc->entrysig);
323 gtk_entry_set_text(GTK_ENTRY(uc->entry), text);
324 g_signal_handler_unblock(uc->entry, uc->entrysig);
327 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
329 struct dlgparam *dp = (struct dlgparam *)dlg;
330 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
331 assert(uc->ctrl->generic.type == CTRL_EDITBOX);
332 assert(uc->entry != NULL);
333 strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
335 buffer[length-1] = '\0';
338 static void container_remove_and_destroy(GtkWidget *w, gpointer data)
340 GtkContainer *cont = GTK_CONTAINER(data);
341 /* gtk_container_remove will unref the widget for us; we need not. */
342 gtk_container_remove(cont, w);
345 /* The `listbox' functions can also apply to combo boxes. */
346 void dlg_listbox_clear(union control *ctrl, void *dlg)
348 struct dlgparam *dp = (struct dlgparam *)dlg;
349 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
351 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
352 uc->ctrl->generic.type == CTRL_LISTBOX);
353 assert(uc->menu != NULL || uc->list != NULL);
356 gtk_container_foreach(GTK_CONTAINER(uc->menu),
357 container_remove_and_destroy,
358 GTK_CONTAINER(uc->menu));
360 gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
364 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
366 struct dlgparam *dp = (struct dlgparam *)dlg;
367 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
369 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
370 uc->ctrl->generic.type == CTRL_LISTBOX);
371 assert(uc->menu != NULL || uc->list != NULL);
375 (GTK_CONTAINER(uc->menu),
376 g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
378 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
382 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
384 dlg_listbox_addwithid(ctrl, dlg, text, 0);
388 * Each listbox entry may have a numeric id associated with it.
389 * Note that some front ends only permit a string to be stored at
390 * each position, which means that _if_ you put two identical
391 * strings in any listbox then you MUST not assign them different
392 * IDs and expect to get meaningful results back.
394 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
395 char const *text, int id)
397 struct dlgparam *dp = (struct dlgparam *)dlg;
398 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
400 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
401 uc->ctrl->generic.type == CTRL_LISTBOX);
402 assert(uc->menu != NULL || uc->list != NULL);
404 dp->flags |= FLAG_UPDATING_COMBO_LIST;
408 * List item in a drop-down (but non-combo) list. Tabs are
409 * ignored; we just provide a standard menu item with the
412 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
414 gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
415 gtk_widget_show(menuitem);
417 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
418 GINT_TO_POINTER(id));
419 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
420 GTK_SIGNAL_FUNC(menuitem_activate), dp);
421 } else if (!uc->entry) {
423 * List item in a non-combo-box list box. We make all of
424 * these Columns containing GtkLabels. This allows us to do
425 * the nasty force_left hack irrespective of whether there
426 * are tabs in the thing.
428 GtkWidget *listitem = gtk_list_item_new();
429 GtkWidget *cols = columns_new(10);
433 /* Count the tabs in the text, and hence determine # of columns. */
435 for (i = 0; text[i]; i++)
440 (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
441 percents = snewn(ncols, gint);
442 percents[ncols-1] = 100;
443 for (i = 0; i < ncols-1; i++) {
444 percents[i] = uc->ctrl->listbox.percentages[i];
445 percents[ncols-1] -= percents[i];
447 columns_set_cols(COLUMNS(cols), ncols, percents);
450 for (i = 0; i < ncols; i++) {
451 int len = strcspn(text, "\t");
452 char *dup = dupprintf("%.*s", len, text);
457 label = gtk_label_new(dup);
460 columns_add(COLUMNS(cols), label, i, 1);
461 columns_force_left_align(COLUMNS(cols), label);
462 gtk_widget_show(label);
464 gtk_container_add(GTK_CONTAINER(listitem), cols);
465 gtk_widget_show(cols);
466 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
467 gtk_widget_show(listitem);
469 if (ctrl->listbox.multisel) {
470 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
471 GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj);
473 gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
474 GTK_SIGNAL_FUNC(listitem_single_key), uc->adj);
476 gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event",
477 GTK_SIGNAL_FUNC(widget_focus), dp);
478 gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event",
479 GTK_SIGNAL_FUNC(listitem_button), dp);
480 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
481 GINT_TO_POINTER(id));
484 * List item in a combo-box list, which means the sensible
485 * thing to do is make it a perfectly normal label. Hence
486 * tabs are disregarded.
488 GtkWidget *listitem = gtk_list_item_new_with_label(text);
490 gtk_container_add(GTK_CONTAINER(uc->list), listitem);
491 gtk_widget_show(listitem);
493 gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
494 GINT_TO_POINTER(id));
497 dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
500 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
502 struct dlgparam *dp = (struct dlgparam *)dlg;
503 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
507 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
508 uc->ctrl->generic.type == CTRL_LISTBOX);
509 assert(uc->menu != NULL || uc->list != NULL);
511 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
513 item = GTK_OBJECT(g_list_nth_data(children, index));
514 g_list_free(children);
516 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), "user-data"));
519 /* dlg_listbox_index returns <0 if no single element is selected. */
520 int dlg_listbox_index(union control *ctrl, void *dlg)
522 struct dlgparam *dp = (struct dlgparam *)dlg;
523 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
525 GtkWidget *item, *activeitem;
529 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
530 uc->ctrl->generic.type == CTRL_LISTBOX);
531 assert(uc->menu != NULL || uc->list != NULL);
534 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
536 activeitem = NULL; /* unnecessarily placate gcc */
538 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
540 for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
541 i++, children = children->next) {
542 if (uc->menu ? activeitem == item :
543 GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
550 g_list_free(children);
551 return selected < 0 ? -1 : selected;
554 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
556 struct dlgparam *dp = (struct dlgparam *)dlg;
557 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
559 GtkWidget *item, *activeitem;
561 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
562 uc->ctrl->generic.type == CTRL_LISTBOX);
563 assert(uc->menu != NULL || uc->list != NULL);
565 children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
567 item = GTK_WIDGET(g_list_nth_data(children, index));
568 g_list_free(children);
571 activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
572 return item == activeitem;
574 return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
578 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
580 struct dlgparam *dp = (struct dlgparam *)dlg;
581 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
583 assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
584 uc->ctrl->generic.type == CTRL_LISTBOX);
585 assert(uc->optmenu != NULL || uc->list != NULL);
588 gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
592 gdouble newtop, newbot;
594 gtk_list_select_item(GTK_LIST(uc->list), index);
597 * Scroll the list box if necessary to ensure the newly
598 * selected item is visible.
600 items = gtk_container_children(GTK_CONTAINER(uc->list));
601 nitems = g_list_length(items);
603 int modified = FALSE;
605 newtop = uc->adj->lower +
606 (uc->adj->upper - uc->adj->lower) * index / nitems;
607 newbot = uc->adj->lower +
608 (uc->adj->upper - uc->adj->lower) * (index+1) / nitems;
609 if (uc->adj->value > newtop) {
611 uc->adj->value = newtop;
612 } else if (uc->adj->value < newbot - uc->adj->page_size) {
614 uc->adj->value = newbot - uc->adj->page_size;
617 gtk_adjustment_value_changed(uc->adj);
622 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
624 struct dlgparam *dp = (struct dlgparam *)dlg;
625 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
627 assert(uc->ctrl->generic.type == CTRL_TEXT);
628 assert(uc->text != NULL);
630 gtk_label_set_text(GTK_LABEL(uc->text), text);
633 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
635 struct dlgparam *dp = (struct dlgparam *)dlg;
636 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
638 switch (uc->ctrl->generic.type) {
640 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
641 shortcut_highlight(uc->toplevel, ctrl->button.shortcut);
644 gtk_label_set_text(GTK_LABEL(uc->toplevel), text);
645 shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut);
648 gtk_label_set_text(GTK_LABEL(uc->label), text);
649 shortcut_highlight(uc->label, ctrl->radio.shortcut);
652 gtk_label_set_text(GTK_LABEL(uc->label), text);
653 shortcut_highlight(uc->label, ctrl->editbox.shortcut);
655 case CTRL_FILESELECT:
656 gtk_label_set_text(GTK_LABEL(uc->label), text);
657 shortcut_highlight(uc->label, ctrl->fileselect.shortcut);
659 case CTRL_FONTSELECT:
660 gtk_label_set_text(GTK_LABEL(uc->label), text);
661 shortcut_highlight(uc->label, ctrl->fontselect.shortcut);
664 gtk_label_set_text(GTK_LABEL(uc->label), text);
665 shortcut_highlight(uc->label, ctrl->listbox.shortcut);
668 assert(!"This shouldn't happen");
673 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
675 struct dlgparam *dp = (struct dlgparam *)dlg;
676 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
677 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
678 assert(uc->entry != NULL);
679 gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path);
682 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
684 struct dlgparam *dp = (struct dlgparam *)dlg;
685 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
686 assert(uc->ctrl->generic.type == CTRL_FILESELECT);
687 assert(uc->entry != NULL);
688 strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
690 fn->path[lenof(fn->path)-1] = '\0';
693 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
695 struct dlgparam *dp = (struct dlgparam *)dlg;
696 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
697 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
698 assert(uc->entry != NULL);
699 gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name);
702 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
704 struct dlgparam *dp = (struct dlgparam *)dlg;
705 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
706 assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
707 assert(uc->entry != NULL);
708 strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
710 fs->name[lenof(fs->name)-1] = '\0';
714 * Bracketing a large set of updates in these two functions will
715 * cause the front end (if possible) to delay updating the screen
716 * until it's all complete, thus avoiding flicker.
718 void dlg_update_start(union control *ctrl, void *dlg)
721 * Apparently we can't do this at all in GTK. GtkCList supports
722 * freeze and thaw, but not GtkList. Bah.
726 void dlg_update_done(union control *ctrl, void *dlg)
729 * Apparently we can't do this at all in GTK. GtkCList supports
730 * freeze and thaw, but not GtkList. Bah.
734 void dlg_set_focus(union control *ctrl, void *dlg)
736 struct dlgparam *dp = (struct dlgparam *)dlg;
737 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
739 switch (ctrl->generic.type) {
742 /* Check boxes and buttons get the focus _and_ get toggled. */
743 gtk_widget_grab_focus(uc->toplevel);
745 case CTRL_FILESELECT:
746 case CTRL_FONTSELECT:
748 /* Anything containing an edit box gets that focused. */
749 gtk_widget_grab_focus(uc->entry);
753 * Radio buttons: we find the currently selected button and
758 for (i = 0; i < ctrl->radio.nbuttons; i++)
759 if (gtk_toggle_button_get_active
760 (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
761 gtk_widget_grab_focus(uc->buttons[i]);
767 * If the list is really an option menu, we focus it.
768 * Otherwise we tell it to focus one of its children, which
769 * appears to do the Right Thing.
772 gtk_widget_grab_focus(uc->optmenu);
774 assert(uc->list != NULL);
775 #if GTK_CHECK_VERSION(2,0,0)
776 gtk_widget_grab_focus(uc->list);
778 gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
786 * During event processing, you might well want to give an error
787 * indication to the user. dlg_beep() is a quick and easy generic
788 * error; dlg_error() puts up a message-box or equivalent.
790 void dlg_beep(void *dlg)
795 static void errmsg_button_clicked(GtkButton *button, gpointer data)
797 gtk_widget_destroy(GTK_WIDGET(data));
800 static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
802 gint x, y, w, h, dx, dy;
804 gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
805 gtk_widget_size_request(GTK_WIDGET(child), &req);
807 gdk_window_get_origin(GTK_WIDGET(parent)->window, &x, &y);
808 gdk_window_get_size(GTK_WIDGET(parent)->window, &w, &h);
811 * One corner of the transient will be offset inwards, by 1/4
812 * of the parent window's size, from the corresponding corner
813 * of the parent window. The corner will be chosen so as to
814 * place the transient closer to the centre of the screen; this
815 * should avoid transients going off the edge of the screen on
818 if (x + w/2 < gdk_screen_width() / 2)
819 dx = x + w/4; /* work from left edges */
821 dx = x + 3*w/4 - req.width; /* work from right edges */
822 if (y + h/2 < gdk_screen_height() / 2)
823 dy = y + h/4; /* work from top edges */
825 dy = y + 3*h/4 - req.height; /* work from bottom edges */
826 gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
829 void dlg_error_msg(void *dlg, char *msg)
831 struct dlgparam *dp = (struct dlgparam *)dlg;
832 GtkWidget *window, *hbox, *text, *ok;
834 window = gtk_dialog_new();
835 text = gtk_label_new(msg);
836 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
837 hbox = gtk_hbox_new(FALSE, 0);
838 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
839 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
840 hbox, FALSE, FALSE, 20);
841 gtk_widget_show(text);
842 gtk_widget_show(hbox);
843 gtk_window_set_title(GTK_WINDOW(window), "Error");
844 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
845 ok = gtk_button_new_with_label("OK");
846 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
847 ok, FALSE, FALSE, 0);
849 GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
850 gtk_window_set_default(GTK_WINDOW(window), ok);
851 gtk_signal_connect(GTK_OBJECT(ok), "clicked",
852 GTK_SIGNAL_FUNC(errmsg_button_clicked), window);
853 gtk_signal_connect(GTK_OBJECT(window), "destroy",
854 GTK_SIGNAL_FUNC(window_destroy), NULL);
855 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
856 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
857 set_transient_window_pos(dp->window, window);
858 gtk_widget_show(window);
863 * This function signals to the front end that the dialog's
864 * processing is completed, and passes an integer value (typically
867 void dlg_end(void *dlg, int value)
869 struct dlgparam *dp = (struct dlgparam *)dlg;
871 gtk_widget_destroy(dp->window);
874 void dlg_refresh(union control *ctrl, void *dlg)
876 struct dlgparam *dp = (struct dlgparam *)dlg;
880 if (ctrl->generic.handler != NULL)
881 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
885 for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
886 assert(uc->ctrl != NULL);
887 if (uc->ctrl->generic.handler != NULL)
888 uc->ctrl->generic.handler(uc->ctrl, dp,
889 dp->data, EVENT_REFRESH);
894 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
896 struct dlgparam *dp = (struct dlgparam *)dlg;
897 struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
900 GtkWidget *coloursel =
901 gtk_color_selection_dialog_new("Select a colour");
902 GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
904 dp->coloursel_result.ok = FALSE;
906 gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
907 #if GTK_CHECK_VERSION(2,0,0)
908 gtk_color_selection_set_has_opacity_control(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
910 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
912 cvals[0] = r / 255.0;
913 cvals[1] = g / 255.0;
914 cvals[2] = b / 255.0;
915 cvals[3] = 1.0; /* fully opaque! */
916 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals);
918 gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data",
919 (gpointer)coloursel);
920 gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data",
921 (gpointer)coloursel);
922 gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc);
923 gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked",
924 GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp);
925 gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked",
926 GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp);
927 gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked",
928 GTK_SIGNAL_FUNC(gtk_widget_destroy),
929 (gpointer)coloursel);
930 gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked",
931 GTK_SIGNAL_FUNC(gtk_widget_destroy),
932 (gpointer)coloursel);
933 gtk_widget_show(coloursel);
936 int dlg_coloursel_results(union control *ctrl, void *dlg,
937 int *r, int *g, int *b)
939 struct dlgparam *dp = (struct dlgparam *)dlg;
940 if (dp->coloursel_result.ok) {
941 *r = dp->coloursel_result.r;
942 *g = dp->coloursel_result.g;
943 *b = dp->coloursel_result.b;
949 /* ----------------------------------------------------------------------
950 * Signal handlers while the dialog box is active.
953 static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
956 struct dlgparam *dp = (struct dlgparam *)data;
957 struct uctrl *uc = dlg_find_bywidget(dp, widget);
958 union control *focus;
965 if (focus != dp->currfocus) {
966 dp->lastfocus = dp->currfocus;
967 dp->currfocus = focus;
973 static void button_clicked(GtkButton *button, gpointer data)
975 struct dlgparam *dp = (struct dlgparam *)data;
976 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
977 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
980 static void button_toggled(GtkToggleButton *tb, gpointer data)
982 struct dlgparam *dp = (struct dlgparam *)data;
983 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
984 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
987 static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
991 * GtkEntry has a nasty habit of eating the Return key, which
992 * is unhelpful since it doesn't actually _do_ anything with it
993 * (it calls gtk_widget_activate, but our edit boxes never need
994 * activating). So I catch Return before GtkEntry sees it, and
995 * pass it straight on to the parent widget. Effect: hitting
996 * Return in an edit box will now activate the default button
997 * in the dialog just like it will everywhere else.
999 if (event->keyval == GDK_Return && widget->parent != NULL) {
1000 gboolean return_val;
1001 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1002 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
1003 event, &return_val);
1009 static void editbox_changed(GtkEditable *ed, gpointer data)
1011 struct dlgparam *dp = (struct dlgparam *)data;
1012 if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
1013 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1014 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1018 static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
1021 struct dlgparam *dp = (struct dlgparam *)data;
1022 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
1023 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
1027 static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
1028 gpointer data, int multiple)
1030 GtkAdjustment *adj = GTK_ADJUSTMENT(data);
1032 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
1033 event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
1034 event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
1035 event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
1037 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1038 * in a list box. So, if the list box is single-selection:
1040 * - if the list item in question isn't already selected,
1041 * we simply select it.
1042 * - otherwise, we find the next one (or next
1043 * however-far-away) in whichever direction we're going,
1045 * + in this case, we must also fiddle with the
1046 * scrollbar to ensure the newly selected item is
1049 * If it's multiple-selection, we do all of the above
1050 * except actually selecting anything, so we move the focus
1051 * and fiddle the scrollbar to follow it.
1053 GtkWidget *list = item->parent;
1055 gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event");
1058 GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
1059 gtk_list_select_child(GTK_LIST(list), item);
1062 (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
1063 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1066 (event->keyval==GDK_Page_Down ||
1067 event->keyval==GDK_KP_Page_Down ||
1068 event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
1071 GList *children, *chead;
1073 chead = children = gtk_container_children(GTK_CONTAINER(list));
1075 n = g_list_length(children);
1079 * Figure out how many list items to a screenful,
1080 * and adjust the step appropriately.
1082 step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
1083 step--; /* go by one less than that */
1087 while (children != NULL) {
1088 if (item == children->data)
1090 children = children->next;
1095 if (direction < 0 && i > 0)
1096 children = children->prev, i--;
1097 else if (direction > 0 && i < n-1)
1098 children = children->next, i++;
1102 if (children && children->data) {
1104 gtk_list_select_child(GTK_LIST(list),
1105 GTK_WIDGET(children->data));
1106 gtk_widget_grab_focus(GTK_WIDGET(children->data));
1107 gtk_adjustment_clamp_page
1109 adj->lower + (adj->upper-adj->lower) * i / n,
1110 adj->lower + (adj->upper-adj->lower) * (i+1) / n);
1121 static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
1124 return listitem_key(item, event, data, FALSE);
1127 static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
1130 return listitem_key(item, event, data, TRUE);
1133 static gboolean listitem_button(GtkWidget *item, GdkEventButton *event,
1136 struct dlgparam *dp = (struct dlgparam *)data;
1137 if (event->type == GDK_2BUTTON_PRESS ||
1138 event->type == GDK_3BUTTON_PRESS) {
1139 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
1140 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
1146 static void list_selchange(GtkList *list, gpointer data)
1148 struct dlgparam *dp = (struct dlgparam *)data;
1149 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
1151 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1154 static void menuitem_activate(GtkMenuItem *item, gpointer data)
1156 struct dlgparam *dp = (struct dlgparam *)data;
1157 GtkWidget *menushell = GTK_WIDGET(item)->parent;
1158 gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data");
1159 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
1160 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
1163 static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
1165 int index = dlg_listbox_index(uc->ctrl, dp);
1166 GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
1170 (index == 0 && direction < 0) ||
1171 (index == g_list_length(children)-1 && direction > 0)) {
1176 child = g_list_nth_data(children, index);
1177 gtk_widget_ref(child);
1178 gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
1179 g_list_free(children);
1182 children = g_list_append(children, child);
1183 gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
1184 gtk_list_select_item(GTK_LIST(uc->list), index + direction);
1185 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
1188 static void draglist_up(GtkButton *button, gpointer data)
1190 struct dlgparam *dp = (struct dlgparam *)data;
1191 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1192 draglist_move(dp, uc, -1);
1195 static void draglist_down(GtkButton *button, gpointer data)
1197 struct dlgparam *dp = (struct dlgparam *)data;
1198 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1199 draglist_move(dp, uc, +1);
1202 static void filesel_ok(GtkButton *button, gpointer data)
1204 /* struct dlgparam *dp = (struct dlgparam *)data; */
1205 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1206 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data");
1207 const char *name = gtk_file_selection_get_filename
1208 (GTK_FILE_SELECTION(filesel));
1209 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1212 static void fontsel_ok(GtkButton *button, gpointer data)
1214 /* struct dlgparam *dp = (struct dlgparam *)data; */
1215 gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1216 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data");
1217 const char *name = gtk_font_selection_dialog_get_font_name
1218 (GTK_FONT_SELECTION_DIALOG(fontsel));
1219 gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
1222 static void coloursel_ok(GtkButton *button, gpointer data)
1224 struct dlgparam *dp = (struct dlgparam *)data;
1225 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1226 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1228 gtk_color_selection_get_color
1229 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel),
1231 dp->coloursel_result.r = (int) (255 * cvals[0]);
1232 dp->coloursel_result.g = (int) (255 * cvals[1]);
1233 dp->coloursel_result.b = (int) (255 * cvals[2]);
1234 dp->coloursel_result.ok = TRUE;
1235 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1238 static void coloursel_cancel(GtkButton *button, gpointer data)
1240 struct dlgparam *dp = (struct dlgparam *)data;
1241 gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1242 struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
1243 dp->coloursel_result.ok = FALSE;
1244 uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
1247 static void filefont_clicked(GtkButton *button, gpointer data)
1249 struct dlgparam *dp = (struct dlgparam *)data;
1250 struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
1252 if (uc->ctrl->generic.type == CTRL_FILESELECT) {
1253 GtkWidget *filesel =
1254 gtk_file_selection_new(uc->ctrl->fileselect.title);
1255 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1257 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1259 gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc);
1261 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1262 GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp);
1263 gtk_signal_connect_object
1264 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1265 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1266 gtk_signal_connect_object
1267 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1268 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1269 gtk_widget_show(filesel);
1272 if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
1273 #if !GTK_CHECK_VERSION(2,0,0)
1274 gchar *spacings[] = { "c", "m", NULL };
1276 const gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
1277 /* TODO: In GTK 2, this only seems to offer client-side fonts. */
1278 GtkWidget *fontsel =
1279 gtk_font_selection_dialog_new("Select a font");
1280 gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
1281 #if !GTK_CHECK_VERSION(2,0,0)
1282 gtk_font_selection_dialog_set_filter
1283 (GTK_FONT_SELECTION_DIALOG(fontsel),
1284 GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
1285 NULL, NULL, NULL, NULL, spacings, NULL);
1287 if (!gtk_font_selection_dialog_set_font_name
1288 (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
1290 * If the font name wasn't found as it was, try opening
1291 * it and extracting its FONT property. This should
1292 * have the effect of mapping short aliases into true
1295 GdkFont *font = gdk_font_load(fontname);
1297 XFontStruct *xfs = GDK_FONT_XFONT(font);
1298 Display *disp = GDK_FONT_XDISPLAY(font);
1299 Atom fontprop = XInternAtom(disp, "FONT", False);
1302 if (XGetFontProperty(xfs, fontprop, &ret)) {
1303 char *name = XGetAtomName(disp, (Atom)ret);
1305 gtk_font_selection_dialog_set_font_name
1306 (GTK_FONT_SELECTION_DIALOG(fontsel), name);
1308 gdk_font_unref(font);
1312 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1313 "user-data", (gpointer)fontsel);
1314 gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc);
1316 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1317 "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
1318 gtk_signal_connect_object
1319 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
1320 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1322 gtk_signal_connect_object
1323 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
1324 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
1326 gtk_widget_show(fontsel);
1330 static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
1333 struct dlgparam *dp = (struct dlgparam *)data;
1334 struct uctrl *uc = dlg_find_bywidget(dp, widget);
1336 gtk_widget_set_usize(uc->text, alloc->width, -1);
1337 gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
1338 gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig);
1341 /* ----------------------------------------------------------------------
1342 * This function does the main layout work: it reads a controlset,
1343 * it creates the relevant GTK controls, and returns a GtkWidget
1344 * containing the result. (This widget might be a title of some
1345 * sort, it might be a Columns containing many controls, or it
1346 * might be a GtkFrame containing a Columns; whatever it is, it's
1347 * definitely a GtkWidget and should probably be added to a
1350 * `listitemheight' is used to calculate a usize for list boxes: it
1351 * should be the height from the size request of a GtkListItem.
1353 * `win' is required for setting the default button. If it is
1354 * non-NULL, all buttons created will be default-capable (so they
1355 * have extra space round them for the default highlight).
1357 GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
1358 struct controlset *s, int listitemheight,
1365 if (!s->boxname && s->boxtitle) {
1366 /* This controlset is a panel title. */
1367 return gtk_label_new(s->boxtitle);
1371 * Otherwise, we expect to be laying out actual controls, so
1372 * we'll start by creating a Columns for the purpose.
1374 cols = COLUMNS(columns_new(4));
1375 ret = GTK_WIDGET(cols);
1376 gtk_widget_show(ret);
1379 * Create a containing frame if we have a box name.
1382 ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
1383 gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
1384 gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
1385 gtk_widget_show(ret);
1389 * Now iterate through the controls themselves, create them,
1390 * and add them to the Columns.
1392 for (i = 0; i < s->ncontrols; i++) {
1393 union control *ctrl = s->ctrls[i];
1396 GtkWidget *w = NULL;
1398 switch (ctrl->generic.type) {
1401 static const int simplecols[1] = { 100 };
1402 columns_set_cols(cols, ctrl->columns.ncols,
1403 (ctrl->columns.percentages ?
1404 ctrl->columns.percentages : simplecols));
1406 continue; /* no actual control created */
1409 struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
1411 columns_taborder_last(cols, uc->toplevel);
1413 continue; /* no actual control created */
1416 uc = snew(struct uctrl);
1418 uc->privdata = NULL;
1419 uc->privdata_needs_free = FALSE;
1421 uc->entry = uc->list = uc->menu = NULL;
1422 uc->button = uc->optmenu = uc->text = NULL;
1425 switch (ctrl->generic.type) {
1427 w = gtk_button_new_with_label(ctrl->generic.label);
1429 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
1430 if (ctrl->button.isdefault)
1431 gtk_window_set_default(win, w);
1432 if (ctrl->button.iscancel)
1433 dp->cancelbutton = w;
1435 gtk_signal_connect(GTK_OBJECT(w), "clicked",
1436 GTK_SIGNAL_FUNC(button_clicked), dp);
1437 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1438 GTK_SIGNAL_FUNC(widget_focus), dp);
1439 shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut,
1440 SHORTCUT_UCTRL, uc);
1443 w = gtk_check_button_new_with_label(ctrl->generic.label);
1444 gtk_signal_connect(GTK_OBJECT(w), "toggled",
1445 GTK_SIGNAL_FUNC(button_toggled), dp);
1446 gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
1447 GTK_SIGNAL_FUNC(widget_focus), dp);
1448 shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut,
1449 SHORTCUT_UCTRL, uc);
1454 * Radio buttons get to go inside their own Columns, no
1458 gint i, *percentages;
1462 if (ctrl->generic.label) {
1463 GtkWidget *label = gtk_label_new(ctrl->generic.label);
1464 columns_add(COLUMNS(w), label, 0, 1);
1465 columns_force_left_align(COLUMNS(w), label);
1466 gtk_widget_show(label);
1467 shortcut_add(scs, label, ctrl->radio.shortcut,
1468 SHORTCUT_UCTRL, uc);
1471 percentages = g_new(gint, ctrl->radio.ncolumns);
1472 for (i = 0; i < ctrl->radio.ncolumns; i++) {
1474 ((100 * (i+1) / ctrl->radio.ncolumns) -
1475 100 * i / ctrl->radio.ncolumns);
1477 columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
1479 g_free(percentages);
1482 uc->nbuttons = ctrl->radio.nbuttons;
1483 uc->buttons = snewn(uc->nbuttons, GtkWidget *);
1485 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1489 b = (gtk_radio_button_new_with_label
1490 (group, ctrl->radio.buttons[i]));
1492 group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
1493 colstart = i % ctrl->radio.ncolumns;
1494 columns_add(COLUMNS(w), b, colstart,
1495 (i == ctrl->radio.nbuttons-1 ?
1496 ctrl->radio.ncolumns - colstart : 1));
1497 columns_force_left_align(COLUMNS(w), b);
1499 gtk_signal_connect(GTK_OBJECT(b), "toggled",
1500 GTK_SIGNAL_FUNC(button_toggled), dp);
1501 gtk_signal_connect(GTK_OBJECT(b), "focus_in_event",
1502 GTK_SIGNAL_FUNC(widget_focus), dp);
1503 if (ctrl->radio.shortcuts) {
1504 shortcut_add(scs, GTK_BIN(b)->child,
1505 ctrl->radio.shortcuts[i],
1506 SHORTCUT_UCTRL, uc);
1515 if (ctrl->editbox.has_list) {
1516 w = gtk_combo_new();
1517 gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
1518 uc->entry = GTK_COMBO(w)->entry;
1519 uc->list = GTK_COMBO(w)->list;
1521 w = gtk_entry_new();
1522 if (ctrl->editbox.password)
1523 gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
1527 gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
1528 GTK_SIGNAL_FUNC(editbox_changed), dp);
1529 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
1530 GTK_SIGNAL_FUNC(editbox_key), dp);
1531 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
1532 GTK_SIGNAL_FUNC(widget_focus), dp);
1534 * Edit boxes, for some strange reason, have a minimum
1535 * width of 150 in GTK 1.2. We don't want this - we'd
1536 * rather the edit boxes acquired their natural width
1537 * from the column layout of the rest of the box.
1539 * Also, while we're here, we'll squirrel away the
1540 * edit box height so we can use that to centre its
1541 * label vertically beside it.
1543 gtk_widget_size_request(w, &req);
1544 gtk_widget_set_usize(w, 10, req.height);
1546 if (ctrl->generic.label) {
1547 GtkWidget *label, *container;
1549 label = gtk_label_new(ctrl->generic.label);
1551 shortcut_add(scs, label, ctrl->editbox.shortcut,
1552 SHORTCUT_FOCUS, uc->entry);
1554 container = columns_new(4);
1555 if (ctrl->editbox.percentwidth == 100) {
1556 columns_add(COLUMNS(container), label, 0, 1);
1557 columns_force_left_align(COLUMNS(container), label);
1558 columns_add(COLUMNS(container), w, 0, 1);
1560 gint percentages[2];
1561 percentages[1] = ctrl->editbox.percentwidth;
1562 percentages[0] = 100 - ctrl->editbox.percentwidth;
1563 columns_set_cols(COLUMNS(container), 2, percentages);
1564 columns_add(COLUMNS(container), label, 0, 1);
1565 columns_force_left_align(COLUMNS(container), label);
1566 columns_add(COLUMNS(container), w, 1, 1);
1567 /* Centre the label vertically. */
1568 gtk_widget_set_usize(label, -1, req.height);
1569 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1571 gtk_widget_show(label);
1577 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event",
1578 GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
1581 case CTRL_FILESELECT:
1582 case CTRL_FONTSELECT:
1587 (ctrl->generic.type == CTRL_FILESELECT ?
1588 "Browse..." : "Change...");
1590 gint percentages[] = { 75, 25 };
1592 columns_set_cols(COLUMNS(w), 2, percentages);
1594 if (ctrl->generic.label) {
1595 ww = gtk_label_new(ctrl->generic.label);
1596 columns_add(COLUMNS(w), ww, 0, 2);
1597 columns_force_left_align(COLUMNS(w), ww);
1598 gtk_widget_show(ww);
1599 shortcut_add(scs, ww,
1600 (ctrl->generic.type == CTRL_FILESELECT ?
1601 ctrl->fileselect.shortcut :
1602 ctrl->fontselect.shortcut),
1603 SHORTCUT_UCTRL, uc);
1607 uc->entry = ww = gtk_entry_new();
1608 gtk_widget_size_request(ww, &req);
1609 gtk_widget_set_usize(ww, 10, req.height);
1610 columns_add(COLUMNS(w), ww, 0, 1);
1611 gtk_widget_show(ww);
1613 uc->button = ww = gtk_button_new_with_label(browsebtn);
1614 columns_add(COLUMNS(w), ww, 1, 1);
1615 gtk_widget_show(ww);
1617 gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
1618 GTK_SIGNAL_FUNC(editbox_key), dp);
1620 gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
1621 GTK_SIGNAL_FUNC(editbox_changed), dp);
1622 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
1623 GTK_SIGNAL_FUNC(widget_focus), dp);
1624 gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
1625 GTK_SIGNAL_FUNC(widget_focus), dp);
1626 gtk_signal_connect(GTK_OBJECT(ww), "clicked",
1627 GTK_SIGNAL_FUNC(filefont_clicked), dp);
1631 if (ctrl->listbox.height == 0) {
1632 uc->optmenu = w = gtk_option_menu_new();
1633 uc->menu = gtk_menu_new();
1634 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
1635 gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data",
1636 (gpointer)uc->optmenu);
1637 gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event",
1638 GTK_SIGNAL_FUNC(widget_focus), dp);
1640 uc->list = gtk_list_new();
1641 if (ctrl->listbox.multisel == 2) {
1642 gtk_list_set_selection_mode(GTK_LIST(uc->list),
1643 GTK_SELECTION_EXTENDED);
1644 } else if (ctrl->listbox.multisel == 1) {
1645 gtk_list_set_selection_mode(GTK_LIST(uc->list),
1646 GTK_SELECTION_MULTIPLE);
1648 gtk_list_set_selection_mode(GTK_LIST(uc->list),
1649 GTK_SELECTION_SINGLE);
1651 w = gtk_scrolled_window_new(NULL, NULL);
1652 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
1654 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
1656 GTK_POLICY_AUTOMATIC);
1657 uc->adj = gtk_scrolled_window_get_vadjustment
1658 (GTK_SCROLLED_WINDOW(w));
1660 gtk_widget_show(uc->list);
1661 gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed",
1662 GTK_SIGNAL_FUNC(list_selchange), dp);
1663 gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event",
1664 GTK_SIGNAL_FUNC(widget_focus), dp);
1667 * Adjust the height of the scrolled window to the
1668 * minimum given by the height parameter.
1670 * This piece of guesswork is a horrid hack based
1671 * on looking inside the GTK 1.2 sources
1672 * (specifically gtkviewport.c, which appears to be
1673 * the widget which provides the border around the
1674 * scrolling area). Anyone lets me know how I can
1675 * do this in a way which isn't at risk from GTK
1676 * upgrades, I'd be grateful.
1680 #if GTK_CHECK_VERSION(2,0,0)
1681 edge = GTK_WIDGET(uc->list)->style->ythickness;
1683 edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
1685 gtk_widget_set_usize(w, 10,
1686 2*edge + (ctrl->listbox.height *
1690 if (ctrl->listbox.draglist) {
1692 * GTK doesn't appear to make it easy to
1693 * implement a proper draggable list; so
1694 * instead I'm just going to have to put an Up
1695 * and a Down button to the right of the actual
1696 * list box. Ah well.
1698 GtkWidget *cols, *button;
1699 static const gint percentages[2] = { 80, 20 };
1701 cols = columns_new(4);
1702 columns_set_cols(COLUMNS(cols), 2, percentages);
1703 columns_add(COLUMNS(cols), w, 0, 1);
1705 button = gtk_button_new_with_label("Up");
1706 columns_add(COLUMNS(cols), button, 1, 1);
1707 gtk_widget_show(button);
1708 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1709 GTK_SIGNAL_FUNC(draglist_up), dp);
1710 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
1711 GTK_SIGNAL_FUNC(widget_focus), dp);
1712 button = gtk_button_new_with_label("Down");
1713 columns_add(COLUMNS(cols), button, 1, 1);
1714 gtk_widget_show(button);
1715 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1716 GTK_SIGNAL_FUNC(draglist_down), dp);
1717 gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
1718 GTK_SIGNAL_FUNC(widget_focus), dp);
1724 if (ctrl->generic.label) {
1725 GtkWidget *label, *container;
1727 label = gtk_label_new(ctrl->generic.label);
1729 container = columns_new(4);
1730 if (ctrl->listbox.percentwidth == 100) {
1731 columns_add(COLUMNS(container), label, 0, 1);
1732 columns_force_left_align(COLUMNS(container), label);
1733 columns_add(COLUMNS(container), w, 0, 1);
1735 gint percentages[2];
1736 percentages[1] = ctrl->listbox.percentwidth;
1737 percentages[0] = 100 - ctrl->listbox.percentwidth;
1738 columns_set_cols(COLUMNS(container), 2, percentages);
1739 columns_add(COLUMNS(container), label, 0, 1);
1740 columns_force_left_align(COLUMNS(container), label);
1741 columns_add(COLUMNS(container), w, 1, 1);
1743 gtk_widget_show(label);
1745 shortcut_add(scs, label, ctrl->listbox.shortcut,
1746 SHORTCUT_UCTRL, uc);
1753 * Wrapping text widgets don't sit well with the GTK
1754 * layout model, in which widgets state a minimum size
1755 * and the whole window then adjusts to the smallest
1756 * size it can sensibly take given its contents. A
1757 * wrapping text widget _has_ no clear minimum size;
1758 * instead it has a range of possibilities. It can be
1759 * one line deep but 2000 wide, or two lines deep and
1760 * 1000 pixels, or three by 867, or four by 500 and so
1761 * on. It can be as short as you like provided you
1762 * don't mind it being wide, or as narrow as you like
1763 * provided you don't mind it being tall.
1765 * Therefore, it fits very badly into the layout model.
1766 * Hence the only thing to do is pick a width and let
1767 * it choose its own number of lines. To do this I'm
1768 * going to cheat a little. All new wrapping text
1769 * widgets will be created with a minimal text content
1770 * "X"; then, after the rest of the dialog box is set
1771 * up and its size calculated, the text widgets will be
1772 * told their width and given their real text, which
1773 * will cause the size to be recomputed in the y
1774 * direction (because many of them will expand to more
1777 uc->text = w = gtk_label_new("X");
1778 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
1779 gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
1781 gtk_signal_connect(GTK_OBJECT(w), "size-allocate",
1782 GTK_SIGNAL_FUNC(label_sizealloc), dp);
1788 columns_add(cols, w,
1789 COLUMN_START(ctrl->generic.column),
1790 COLUMN_SPAN(ctrl->generic.column));
1792 columns_force_left_align(cols, w);
1796 dlg_add_uctrl(dp, uc);
1803 struct dlgparam *dp;
1804 GtkNotebook *panels;
1806 #if !GTK_CHECK_VERSION(2,0,0)
1807 GtkWidget *treeitem;
1809 struct Shortcuts shortcuts;
1812 #if GTK_CHECK_VERSION(2,0,0)
1813 static void treeselection_changed(GtkTreeSelection *treeselection,
1816 struct selparam *sps = (struct selparam *)data, *sp;
1817 GtkTreeModel *treemodel;
1818 GtkTreeIter treeiter;
1822 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
1825 gtk_tree_model_get(treemodel, &treeiter, TREESTORE_PARAMS, &spindex, -1);
1828 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
1829 gtk_notebook_set_page(sp->panels, page_num);
1831 dlg_refresh(NULL, sp->dp);
1833 sp->dp->shortcuts = &sp->shortcuts;
1836 static void treeitem_sel(GtkItem *item, gpointer data)
1838 struct selparam *sp = (struct selparam *)data;
1841 page_num = gtk_notebook_page_num(sp->panels, sp->panel);
1842 gtk_notebook_set_page(sp->panels, page_num);
1844 dlg_refresh(NULL, sp->dp);
1846 sp->dp->shortcuts = &sp->shortcuts;
1847 sp->dp->currtreeitem = sp->treeitem;
1851 static void window_destroy(GtkWidget *widget, gpointer data)
1856 #if !GTK_CHECK_VERSION(2,0,0)
1857 static int tree_grab_focus(struct dlgparam *dp)
1862 * See if any of the treeitems has the focus.
1865 for (i = 0; i < dp->ntreeitems; i++)
1866 if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
1874 gtk_widget_grab_focus(dp->currtreeitem);
1879 gint tree_focus(GtkContainer *container, GtkDirectionType direction,
1882 struct dlgparam *dp = (struct dlgparam *)data;
1884 gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus");
1886 * If there's a focused treeitem, we return FALSE to cause the
1887 * focus to move on to some totally other control. If not, we
1888 * focus the selected one.
1890 return tree_grab_focus(dp);
1894 int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
1896 struct dlgparam *dp = (struct dlgparam *)data;
1898 if (event->keyval == GDK_Escape && dp->cancelbutton) {
1899 gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked");
1903 if ((event->state & GDK_MOD1_MASK) &&
1904 (unsigned char)event->string[0] > 0 &&
1905 (unsigned char)event->string[0] <= 127) {
1906 int schr = (unsigned char)event->string[0];
1907 struct Shortcut *sc = &dp->shortcuts->sc[schr];
1909 switch (sc->action) {
1911 #if GTK_CHECK_VERSION(2,0,0)
1912 gtk_widget_grab_focus(sc->widget);
1914 tree_grab_focus(dp);
1917 case SHORTCUT_FOCUS:
1918 gtk_widget_grab_focus(sc->widget);
1920 case SHORTCUT_UCTRL:
1922 * We must do something sensible with a uctrl.
1923 * Precisely what this is depends on the type of
1926 switch (sc->uc->ctrl->generic.type) {
1929 /* Check boxes and buttons get the focus _and_ get toggled. */
1930 gtk_widget_grab_focus(sc->uc->toplevel);
1931 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel),
1934 case CTRL_FILESELECT:
1935 case CTRL_FONTSELECT:
1936 /* File/font selectors have their buttons pressed (ooer),
1937 * and focus transferred to the edit box. */
1938 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button),
1940 gtk_widget_grab_focus(sc->uc->entry);
1944 * Radio buttons are fun, because they have
1945 * multiple shortcuts. We must find whether the
1946 * activated shortcut is the shortcut for the whole
1947 * group, or for a particular button. In the former
1948 * case, we find the currently selected button and
1949 * focus it; in the latter, we focus-and-click the
1950 * button whose shortcut was pressed.
1952 if (schr == sc->uc->ctrl->radio.shortcut) {
1954 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
1955 if (gtk_toggle_button_get_active
1956 (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
1957 gtk_widget_grab_focus(sc->uc->buttons[i]);
1959 } else if (sc->uc->ctrl->radio.shortcuts) {
1961 for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
1962 if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
1963 gtk_widget_grab_focus(sc->uc->buttons[i]);
1964 gtk_signal_emit_by_name
1965 (GTK_OBJECT(sc->uc->buttons[i]), "clicked");
1971 * If the list is really an option menu, we focus
1972 * and click it. Otherwise we tell it to focus one
1973 * of its children, which appears to do the Right
1976 if (sc->uc->optmenu) {
1980 gtk_widget_grab_focus(sc->uc->optmenu);
1981 /* Option menus don't work using the "clicked" signal.
1982 * We need to manufacture a button press event :-/ */
1983 bev.type = GDK_BUTTON_PRESS;
1985 gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu),
1986 "button_press_event",
1989 assert(sc->uc->list != NULL);
1991 #if GTK_CHECK_VERSION(2,0,0)
1992 gtk_widget_grab_focus(sc->uc->list);
1994 gtk_container_focus(GTK_CONTAINER(sc->uc->list),
1995 GTK_DIR_TAB_FORWARD);
2007 #if !GTK_CHECK_VERSION(2,0,0)
2008 int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
2010 struct dlgparam *dp = (struct dlgparam *)data;
2012 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
2013 event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
2015 for (i = 0; i < dp->ntreeitems; i++)
2016 if (widget == dp->treeitems[i])
2018 if (i < dp->ntreeitems) {
2019 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
2026 if (i < 0 || i >= dp->ntreeitems)
2027 break; /* nothing in that dir to select */
2029 * Determine if this tree item is visible.
2032 GtkWidget *w = dp->treeitems[i];
2034 while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
2035 if (!GTK_WIDGET_VISIBLE(w)) {
2042 j = i; /* got one */
2048 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2051 gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle");
2052 gtk_widget_grab_focus(dp->treeitems[j]);
2058 * It's nice for Left and Right to expand and collapse tree
2061 if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
2062 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2064 gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
2067 if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
2068 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
2070 gtk_tree_item_expand(GTK_TREE_ITEM(widget));
2078 static void shortcut_highlight(GtkWidget *labelw, int chr)
2080 GtkLabel *label = GTK_LABEL(labelw);
2081 gchar *currstr, *pattern;
2084 gtk_label_get(label, &currstr);
2085 for (i = 0; currstr[i]; i++)
2086 if (tolower((unsigned char)currstr[i]) == chr) {
2089 pattern = dupprintf("%*s_", i, "");
2091 gtk_widget_size_request(GTK_WIDGET(label), &req);
2092 gtk_label_set_pattern(label, pattern);
2093 gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
2100 void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
2101 int chr, int action, void *ptr)
2103 if (chr == NO_SHORTCUT)
2106 chr = tolower((unsigned char)chr);
2108 assert(scs->sc[chr].action == SHORTCUT_EMPTY);
2110 scs->sc[chr].action = action;
2112 if (action == SHORTCUT_FOCUS) {
2113 scs->sc[chr].uc = NULL;
2114 scs->sc[chr].widget = (GtkWidget *)ptr;
2116 scs->sc[chr].widget = NULL;
2117 scs->sc[chr].uc = (struct uctrl *)ptr;
2120 shortcut_highlight(labelw, chr);
2123 int get_listitemheight(void)
2125 GtkWidget *listitem = gtk_list_item_new_with_label("foo");
2127 gtk_widget_size_request(listitem, &req);
2128 gtk_object_sink(GTK_OBJECT(listitem));
2132 void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
2134 #if !GTK_CHECK_VERSION(2,0,0)
2137 * In GTK 1, laying out the buttons at the bottom of the
2138 * configuration box is nice and easy, because a GtkDialog's
2139 * action_area is a GtkHBox which stretches to cover the full
2140 * width of the dialog. So we just put our Columns widget
2141 * straight into that hbox, and it ends up just where we want
2144 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
2149 * In GTK 2, the action area is now a GtkHButtonBox and its
2150 * layout behaviour seems to be different: it doesn't stretch
2151 * to cover the full width of the window, but instead finds its
2152 * own preferred width and right-aligns that within the window.
2153 * This isn't what we want, because we have both left-aligned
2154 * and right-aligned buttons coming out of the above call to
2155 * layout_ctrls(), and right-aligning the whole thing will
2156 * result in the former being centred and looking weird.
2158 * So instead we abandon the dialog's action area completely:
2159 * we gtk_widget_hide() it in the below code, and we also call
2160 * gtk_dialog_set_has_separator() to remove the separator above
2161 * it. We then insert our own action area into the end of the
2162 * dialog's main vbox, and add our own separator above that.
2164 * (Ideally, if we were a native GTK app, we would use the
2165 * GtkHButtonBox's _own_ innate ability to support one set of
2166 * buttons being right-aligned and one left-aligned. But to do
2167 * that here, we would have to either (a) pick apart our cross-
2168 * platform layout structures and treat them specially for this
2169 * particular set of controls, which would be painful, or else
2170 * (b) develop a special and simpler cross-platform
2171 * representation for these particular controls, and introduce
2172 * special-case code into all the _other_ platforms to handle
2173 * it. Neither appeals. Therefore, I regretfully discard the
2174 * GTKHButtonBox and go it alone.)
2178 align = gtk_alignment_new(0, 0, 1, 1);
2179 gtk_container_add(GTK_CONTAINER(align), w);
2181 * The purpose of this GtkAlignment is to provide padding
2182 * around the buttons. The padding we use is twice the padding
2183 * used in our GtkColumns, because we nest two GtkColumns most
2184 * of the time (one separating the tree view from the main
2185 * controls, and another for the main controls themselves).
2187 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
2188 gtk_widget_show(align);
2189 gtk_box_pack_end(GTK_BOX(dlg->vbox), align, FALSE, TRUE, 0);
2190 w = gtk_hseparator_new();
2191 gtk_box_pack_end(GTK_BOX(dlg->vbox), w, FALSE, TRUE, 0);
2193 gtk_widget_hide(dlg->action_area);
2194 gtk_dialog_set_has_separator(dlg, FALSE);
2198 int do_config_box(const char *title, Config *cfg, int midsession,
2201 GtkWidget *window, *hbox, *vbox, *cols, *label,
2202 *tree, *treescroll, *panels, *panelvbox;
2203 int index, level, listitemheight;
2204 struct controlbox *ctrlbox;
2206 #if GTK_CHECK_VERSION(2,0,0)
2207 GtkTreeStore *treestore;
2208 GtkCellRenderer *treerenderer;
2209 GtkTreeViewColumn *treecolumn;
2210 GtkTreeSelection *treeselection;
2211 GtkTreeIter treeiterlevels[8];
2213 GtkTreeItem *treeitemlevels[8];
2214 GtkTree *treelevels[8];
2217 struct Shortcuts scs;
2219 struct selparam *selparams = NULL;
2220 int nselparams = 0, selparamsize = 0;
2224 listitemheight = get_listitemheight();
2226 for (index = 0; index < lenof(scs.sc); index++) {
2227 scs.sc[index].action = SHORTCUT_EMPTY;
2230 window = gtk_dialog_new();
2232 ctrlbox = ctrl_new_box();
2233 setup_config_box(ctrlbox, midsession, cfg->protocol, protcfginfo);
2234 unix_setup_config_box(ctrlbox, midsession, cfg->protocol);
2235 gtk_setup_config_box(ctrlbox, midsession, window);
2237 gtk_window_set_title(GTK_WINDOW(window), title);
2238 hbox = gtk_hbox_new(FALSE, 4);
2239 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0);
2240 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2241 gtk_widget_show(hbox);
2242 vbox = gtk_vbox_new(FALSE, 4);
2243 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
2244 gtk_widget_show(vbox);
2245 cols = columns_new(4);
2246 gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
2247 gtk_widget_show(cols);
2248 label = gtk_label_new("Category:");
2249 columns_add(COLUMNS(cols), label, 0, 1);
2250 columns_force_left_align(COLUMNS(cols), label);
2251 gtk_widget_show(label);
2252 treescroll = gtk_scrolled_window_new(NULL, NULL);
2253 #if GTK_CHECK_VERSION(2,0,0)
2254 treestore = gtk_tree_store_new
2255 (TREESTORE_NUM, G_TYPE_STRING, G_TYPE_INT);
2256 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore));
2257 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
2258 treerenderer = gtk_cell_renderer_text_new();
2259 treecolumn = gtk_tree_view_column_new_with_attributes
2260 ("Label", treerenderer, "text", 0, NULL);
2261 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), treecolumn);
2262 treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
2263 gtk_tree_selection_set_mode(treeselection, GTK_SELECTION_BROWSE);
2264 gtk_container_add(GTK_CONTAINER(treescroll), tree);
2266 tree = gtk_tree_new();
2267 gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
2268 gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
2269 gtk_signal_connect(GTK_OBJECT(tree), "focus",
2270 GTK_SIGNAL_FUNC(tree_focus), &dp);
2272 gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event",
2273 GTK_SIGNAL_FUNC(widget_focus), &dp);
2274 shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
2275 gtk_widget_show(treescroll);
2276 gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
2277 panels = gtk_notebook_new();
2278 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE);
2279 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE);
2280 gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
2281 gtk_widget_show(panels);
2286 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2287 struct controlset *s = ctrlbox->ctrlsets[index];
2290 if (!*s->pathname) {
2291 w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window));
2293 set_dialog_action_area(GTK_DIALOG(window), w);
2295 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
2296 if (j != INT_MAX) { /* add to treeview, start new panel */
2298 #if GTK_CHECK_VERSION(2,0,0)
2299 GtkTreeIter treeiter;
2301 GtkWidget *treeitem;
2306 * We expect never to find an implicit path
2307 * component. For example, we expect never to see
2308 * A/B/C followed by A/D/E, because that would
2309 * _implicitly_ create A/D. All our path prefixes
2310 * are expected to contain actual controls and be
2311 * selectable in the treeview; so we would expect
2312 * to see A/D _explicitly_ before encountering
2315 assert(j == ctrl_path_elements(s->pathname) - 1);
2317 c = strrchr(s->pathname, '/');
2325 first = (panelvbox == NULL);
2327 panelvbox = gtk_vbox_new(FALSE, 4);
2328 gtk_widget_show(panelvbox);
2329 gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox,
2334 page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels),
2336 gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num);
2339 if (nselparams >= selparamsize) {
2341 selparams = sresize(selparams, selparamsize,
2344 selparams[nselparams].dp = &dp;
2345 selparams[nselparams].panels = GTK_NOTEBOOK(panels);
2346 selparams[nselparams].panel = panelvbox;
2347 selparams[nselparams].shortcuts = scs; /* structure copy */
2349 assert(j-1 < level);
2351 #if GTK_CHECK_VERSION(2,0,0)
2353 /* treeiterlevels[j-1] will always be valid because we
2354 * don't allow implicit path components; see above.
2356 gtk_tree_store_append(treestore, &treeiter,
2357 &treeiterlevels[j-1]);
2359 gtk_tree_store_append(treestore, &treeiter, NULL);
2360 gtk_tree_store_set(treestore, &treeiter,
2362 TREESTORE_PARAMS, nselparams,
2364 treeiterlevels[j] = treeiter;
2369 path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
2370 &treeiterlevels[j-1]);
2372 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree), path,
2375 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree), path);
2376 gtk_tree_path_free(path);
2379 treeitem = gtk_tree_item_new_with_label(c);
2381 if (!treelevels[j-1]) {
2382 treelevels[j-1] = GTK_TREE(gtk_tree_new());
2383 gtk_tree_item_set_subtree
2384 (treeitemlevels[j-1],
2385 GTK_WIDGET(treelevels[j-1]));
2387 gtk_tree_item_expand(treeitemlevels[j-1]);
2389 gtk_tree_item_collapse(treeitemlevels[j-1]);
2391 gtk_tree_append(treelevels[j-1], treeitem);
2393 gtk_tree_append(GTK_TREE(tree), treeitem);
2395 treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
2396 treelevels[j] = NULL;
2398 gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event",
2399 GTK_SIGNAL_FUNC(tree_key_press), &dp);
2400 gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event",
2401 GTK_SIGNAL_FUNC(widget_focus), &dp);
2403 gtk_widget_show(treeitem);
2406 gtk_tree_select_child(GTK_TREE(tree), treeitem);
2407 selparams[nselparams].treeitem = treeitem;
2414 w = layout_ctrls(&dp,
2415 &selparams[nselparams-1].shortcuts,
2416 s, listitemheight, NULL);
2417 gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
2422 #if GTK_CHECK_VERSION(2,0,0)
2423 g_signal_connect(G_OBJECT(treeselection), "changed",
2424 G_CALLBACK(treeselection_changed), selparams);
2426 dp.ntreeitems = nselparams;
2427 dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
2429 for (index = 0; index < nselparams; index++) {
2430 gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
2431 GTK_SIGNAL_FUNC(treeitem_sel),
2433 dp.treeitems[index] = selparams[index].treeitem;
2438 dlg_refresh(NULL, &dp);
2440 dp.shortcuts = &selparams[0].shortcuts;
2441 #if !GTK_CHECK_VERSION(2,0,0)
2442 dp.currtreeitem = dp.treeitems[0];
2444 dp.lastfocus = NULL;
2450 extern void set_window_icon(GtkWidget *window,
2451 const char *const *const *icon,
2453 extern const char *const *const cfg_icon[];
2454 extern const int n_cfg_icon;
2455 set_window_icon(window, cfg_icon, n_cfg_icon);
2458 #if !GTK_CHECK_VERSION(2,0,0)
2459 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
2462 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
2464 GTK_POLICY_AUTOMATIC);
2465 gtk_widget_show(tree);
2467 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2468 gtk_widget_show(window);
2471 * Set focus into the first available control.
2473 for (index = 0; index < ctrlbox->nctrlsets; index++) {
2474 struct controlset *s = ctrlbox->ctrlsets[index];
2479 for (j = 0; j < s->ncontrols; j++)
2480 if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
2481 s->ctrls[j]->generic.type != CTRL_COLUMNS &&
2482 s->ctrls[j]->generic.type != CTRL_TEXT) {
2483 dlg_set_focus(s->ctrls[j], &dp);
2484 dp.lastfocus = s->ctrls[j];
2493 gtk_signal_connect(GTK_OBJECT(window), "destroy",
2494 GTK_SIGNAL_FUNC(window_destroy), NULL);
2495 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
2496 GTK_SIGNAL_FUNC(win_key_press), &dp);
2506 static void messagebox_handler(union control *ctrl, void *dlg,
2507 void *data, int event)
2509 if (event == EVENT_ACTION)
2510 dlg_end(dlg, ctrl->generic.context.i);
2512 int messagebox(GtkWidget *parentwin, char *title, char *msg, int minwid, ...)
2514 GtkWidget *window, *w0, *w1;
2515 struct controlbox *ctrlbox;
2516 struct controlset *s0, *s1;
2519 struct Shortcuts scs;
2525 for (index = 0; index < lenof(scs.sc); index++) {
2526 scs.sc[index].action = SHORTCUT_EMPTY;
2529 ctrlbox = ctrl_new_box();
2532 va_start(ap, minwid);
2533 while (va_arg(ap, char *) != NULL) {
2535 (void) va_arg(ap, int); /* shortcut */
2536 (void) va_arg(ap, int); /* normal/default/cancel */
2537 (void) va_arg(ap, int); /* end value */
2541 s0 = ctrl_getset(ctrlbox, "", "", "");
2542 c = ctrl_columns(s0, 2, 50, 50);
2543 c->columns.ncols = s0->ncolumns = ncols;
2544 c->columns.percentages = sresize(c->columns.percentages, ncols, int);
2545 for (index = 0; index < ncols; index++)
2546 c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
2547 va_start(ap, minwid);
2550 char *title = va_arg(ap, char *);
2551 int shortcut, type, value;
2554 shortcut = va_arg(ap, int);
2555 type = va_arg(ap, int);
2556 value = va_arg(ap, int);
2557 c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
2558 messagebox_handler, I(value));
2559 c->generic.column = index++;
2561 c->button.isdefault = TRUE;
2563 c->button.iscancel = TRUE;
2567 s1 = ctrl_getset(ctrlbox, "x", "", "");
2568 ctrl_text(s1, msg, HELPCTX(no_help));
2570 window = gtk_dialog_new();
2571 gtk_window_set_title(GTK_WINDOW(window), title);
2572 w0 = layout_ctrls(&dp, &scs, s0, 0, GTK_WINDOW(window));
2573 set_dialog_action_area(GTK_DIALOG(window), w0);
2574 gtk_widget_show(w0);
2575 w1 = layout_ctrls(&dp, &scs, s1, 0, GTK_WINDOW(window));
2576 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
2577 gtk_widget_set_usize(w1, minwid+20, -1);
2578 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
2580 gtk_widget_show(w1);
2582 dp.shortcuts = &scs;
2583 dp.lastfocus = NULL;
2587 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
2589 set_transient_window_pos(parentwin, window);
2590 gtk_window_set_transient_for(GTK_WINDOW(window),
2591 GTK_WINDOW(parentwin));
2593 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2594 gtk_widget_show(window);
2596 gtk_signal_connect(GTK_OBJECT(window), "destroy",
2597 GTK_SIGNAL_FUNC(window_destroy), NULL);
2598 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
2599 GTK_SIGNAL_FUNC(win_key_press), &dp);
2604 ctrl_free_box(ctrlbox);
2609 static int string_width(char *text)
2611 GtkWidget *label = gtk_label_new(text);
2613 gtk_widget_size_request(label, &req);
2614 gtk_object_sink(GTK_OBJECT(label));
2618 int reallyclose(void *frontend)
2620 char *title = dupcat(appname, " Exit Confirmation", NULL);
2621 int ret = messagebox(GTK_WIDGET(get_window(frontend)),
2622 title, "Are you sure you want to close this session?",
2623 string_width("Most of the width of the above text"),
2631 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
2632 char *keystr, char *fingerprint,
2633 void (*callback)(void *ctx, int result), void *ctx)
2635 static const char absenttxt[] =
2636 "The server's host key is not cached. You have no guarantee "
2637 "that the server is the computer you think it is.\n"
2638 "The server's %s key fingerprint is:\n"
2640 "If you trust this host, press \"Accept\" to add the key to "
2641 "PuTTY's cache and carry on connecting.\n"
2642 "If you want to carry on connecting just once, without "
2643 "adding the key to the cache, press \"Connect Once\".\n"
2644 "If you do not trust this host, press \"Cancel\" to abandon the "
2646 static const char wrongtxt[] =
2647 "WARNING - POTENTIAL SECURITY BREACH!\n"
2648 "The server's host key does not match the one PuTTY has "
2649 "cached. This means that either the server administrator "
2650 "has changed the host key, or you have actually connected "
2651 "to another computer pretending to be the server.\n"
2652 "The new %s key fingerprint is:\n"
2654 "If you were expecting this change and trust the new key, "
2655 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2656 "If you want to carry on connecting but without updating "
2657 "the cache, press \"Connect Once\".\n"
2658 "If you want to abandon the connection completely, press "
2659 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2667 ret = verify_host_key(host, port, keytype, keystr);
2669 if (ret == 0) /* success - key matched OK */
2672 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
2674 ret = messagebox(GTK_WIDGET(get_window(frontend)),
2675 "PuTTY Security Alert", text,
2676 string_width(fingerprint),
2677 "Accept", 'a', 0, 2,
2678 "Connect Once", 'o', 0, 1,
2679 "Cancel", 'c', -1, 0,
2685 store_host_key(host, port, keytype, keystr);
2686 return 1; /* continue with connection */
2687 } else if (ret == 1)
2688 return 1; /* continue with connection */
2689 return 0; /* do not continue with connection */
2693 * Ask whether the selected algorithm is acceptable (since it was
2694 * below the configured 'warn' threshold).
2696 int askalg(void *frontend, const char *algtype, const char *algname,
2697 void (*callback)(void *ctx, int result), void *ctx)
2699 static const char msg[] =
2700 "The first %s supported by the server is "
2701 "%s, which is below the configured warning threshold.\n"
2702 "Continue with connection?";
2706 text = dupprintf(msg, algtype, algname);
2707 ret = messagebox(GTK_WIDGET(get_window(frontend)),
2708 "PuTTY Security Alert", text,
2709 string_width("Continue with connection?"),
2722 void old_keyfile_warning(void)
2725 * This should never happen on Unix. We hope.
2729 void fatal_message_box(void *window, char *msg)
2731 messagebox(window, "PuTTY Fatal Error", msg,
2732 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2733 "OK", 'o', 1, 1, NULL);
2736 void fatalbox(char *p, ...)
2741 msg = dupvprintf(p, ap);
2743 fatal_message_box(NULL, msg);
2748 static GtkWidget *aboutbox = NULL;
2750 static void about_close_clicked(GtkButton *button, gpointer data)
2752 gtk_widget_destroy(aboutbox);
2756 static void licence_clicked(GtkButton *button, gpointer data)
2761 "Copyright 1997-2007 Simon Tatham.\n\n"
2763 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2764 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2765 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
2766 "Markus Kuhn, Colin Watson, and CORE SDI S.A.\n\n"
2768 "Permission is hereby granted, free of charge, to any person "
2769 "obtaining a copy of this software and associated documentation "
2770 "files (the ""Software""), to deal in the Software without restriction, "
2771 "including without limitation the rights to use, copy, modify, merge, "
2772 "publish, distribute, sublicense, and/or sell copies of the Software, "
2773 "and to permit persons to whom the Software is furnished to do so, "
2774 "subject to the following conditions:\n\n"
2776 "The above copyright notice and this permission notice shall be "
2777 "included in all copies or substantial portions of the Software.\n\n"
2779 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2780 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2781 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2782 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2783 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
2784 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2785 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2786 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2787 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2788 "OTHER DEALINGS IN THE SOFTWARE.";
2790 title = dupcat(appname, " Licence", NULL);
2791 assert(aboutbox != NULL);
2792 messagebox(aboutbox, title, licence,
2793 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2794 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2795 "OK", 'o', 1, 1, NULL);
2799 void about_box(void *window)
2805 gtk_widget_grab_focus(aboutbox);
2809 aboutbox = gtk_dialog_new();
2810 gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
2811 title = dupcat("About ", appname, NULL);
2812 gtk_window_set_title(GTK_WINDOW(aboutbox), title);
2815 w = gtk_button_new_with_label("Close");
2816 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
2817 gtk_window_set_default(GTK_WINDOW(aboutbox), w);
2818 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
2819 w, FALSE, FALSE, 0);
2820 gtk_signal_connect(GTK_OBJECT(w), "clicked",
2821 GTK_SIGNAL_FUNC(about_close_clicked), NULL);
2824 w = gtk_button_new_with_label("View Licence");
2825 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
2826 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
2827 w, FALSE, FALSE, 0);
2828 gtk_signal_connect(GTK_OBJECT(w), "clicked",
2829 GTK_SIGNAL_FUNC(licence_clicked), NULL);
2832 w = gtk_label_new(appname);
2833 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2834 w, FALSE, FALSE, 0);
2837 w = gtk_label_new(ver);
2838 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2839 w, FALSE, FALSE, 5);
2842 w = gtk_label_new("Copyright 1997-2007 Simon Tatham. All rights reserved");
2843 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
2844 w, FALSE, FALSE, 5);
2847 set_transient_window_pos(GTK_WIDGET(window), aboutbox);
2848 gtk_widget_show(aboutbox);
2851 struct eventlog_stuff {
2852 GtkWidget *parentwin, *window;
2853 struct controlbox *eventbox;
2854 struct Shortcuts scs;
2856 union control *listctrl;
2858 int nevents, negsize;
2861 int ignore_selchange;
2864 static void eventlog_destroy(GtkWidget *widget, gpointer data)
2866 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2870 dlg_cleanup(&es->dp);
2871 ctrl_free_box(es->eventbox);
2873 static void eventlog_ok_handler(union control *ctrl, void *dlg,
2874 void *data, int event)
2876 if (event == EVENT_ACTION)
2879 static void eventlog_list_handler(union control *ctrl, void *dlg,
2880 void *data, int event)
2882 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2884 if (event == EVENT_REFRESH) {
2887 dlg_update_start(ctrl, dlg);
2888 dlg_listbox_clear(ctrl, dlg);
2889 for (i = 0; i < es->nevents; i++) {
2890 dlg_listbox_add(ctrl, dlg, es->events[i]);
2892 dlg_update_done(ctrl, dlg);
2893 } else if (event == EVENT_SELCHANGE) {
2898 * If this SELCHANGE event is happening as a result of
2899 * deliberate deselection because someone else has grabbed
2900 * the selection, the last thing we want to do is pre-empt
2903 if (es->ignore_selchange)
2907 * Construct the data to use as the selection.
2912 for (i = 0; i < es->nevents; i++) {
2913 if (dlg_listbox_issel(ctrl, dlg, i)) {
2914 int extralen = strlen(es->events[i]);
2916 if (es->sellen + extralen + 2 > selsize) {
2917 selsize = es->sellen + extralen + 512;
2918 es->seldata = sresize(es->seldata, selsize, char);
2921 strcpy(es->seldata + es->sellen, es->events[i]);
2922 es->sellen += extralen;
2923 es->seldata[es->sellen++] = '\n';
2927 if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
2928 GDK_CURRENT_TIME)) {
2929 extern GdkAtom compound_text_atom;
2931 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
2932 GDK_SELECTION_TYPE_STRING, 1);
2933 gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
2934 compound_text_atom, 1);
2940 void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2941 guint info, guint time_stamp, gpointer data)
2943 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2945 gtk_selection_data_set(seldata, seldata->target, 8,
2946 (unsigned char *)es->seldata, es->sellen);
2949 gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2952 struct eventlog_stuff *es = (struct eventlog_stuff *)data;
2956 * Deselect everything in the list box.
2958 uc = dlg_find_byctrl(&es->dp, es->listctrl);
2959 es->ignore_selchange = 1;
2960 gtk_list_unselect_all(GTK_LIST(uc->list));
2961 es->ignore_selchange = 0;
2969 void showeventlog(void *estuff, void *parentwin)
2971 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
2972 GtkWidget *window, *w0, *w1;
2973 GtkWidget *parent = GTK_WIDGET(parentwin);
2974 struct controlset *s0, *s1;
2976 int listitemheight, index;
2980 gtk_widget_grab_focus(es->window);
2986 for (index = 0; index < lenof(es->scs.sc); index++) {
2987 es->scs.sc[index].action = SHORTCUT_EMPTY;
2990 es->eventbox = ctrl_new_box();
2992 s0 = ctrl_getset(es->eventbox, "", "", "");
2993 ctrl_columns(s0, 3, 33, 34, 33);
2994 c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
2995 eventlog_ok_handler, P(NULL));
2996 c->button.column = 1;
2997 c->button.isdefault = TRUE;
2999 s1 = ctrl_getset(es->eventbox, "x", "", "");
3000 es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
3001 eventlog_list_handler, P(es));
3002 c->listbox.height = 10;
3003 c->listbox.multisel = 2;
3004 c->listbox.ncols = 3;
3005 c->listbox.percentages = snewn(3, int);
3006 c->listbox.percentages[0] = 25;
3007 c->listbox.percentages[1] = 10;
3008 c->listbox.percentages[2] = 65;
3010 listitemheight = get_listitemheight();
3012 es->window = window = gtk_dialog_new();
3013 title = dupcat(appname, " Event Log", NULL);
3014 gtk_window_set_title(GTK_WINDOW(window), title);
3016 w0 = layout_ctrls(&es->dp, &es->scs, s0,
3017 listitemheight, GTK_WINDOW(window));
3018 set_dialog_action_area(GTK_DIALOG(window), w0);
3019 gtk_widget_show(w0);
3020 w1 = layout_ctrls(&es->dp, &es->scs, s1,
3021 listitemheight, GTK_WINDOW(window));
3022 gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
3023 gtk_widget_set_usize(w1, 20 +
3024 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3025 " IS QUITE LONG 'COS SSH LOG ENTRIES"
3027 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
3029 gtk_widget_show(w1);
3032 es->dp.shortcuts = &es->scs;
3033 es->dp.lastfocus = NULL;
3035 es->dp.window = window;
3037 dlg_refresh(NULL, &es->dp);
3040 set_transient_window_pos(parent, window);
3041 gtk_window_set_transient_for(GTK_WINDOW(window),
3042 GTK_WINDOW(parent));
3044 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3045 gtk_widget_show(window);
3047 gtk_signal_connect(GTK_OBJECT(window), "destroy",
3048 GTK_SIGNAL_FUNC(eventlog_destroy), es);
3049 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3050 GTK_SIGNAL_FUNC(win_key_press), &es->dp);
3051 gtk_signal_connect(GTK_OBJECT(window), "selection_get",
3052 GTK_SIGNAL_FUNC(eventlog_selection_get), es);
3053 gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event",
3054 GTK_SIGNAL_FUNC(eventlog_selection_clear), es);
3057 void *eventlogstuff_new(void)
3059 struct eventlog_stuff *es;
3060 es = snew(struct eventlog_stuff);
3061 memset(es, 0, sizeof(*es));
3065 void logevent_dlg(void *estuff, const char *string)
3067 struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
3072 if (es->nevents >= es->negsize) {
3074 es->events = sresize(es->events, es->negsize, char *);
3078 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
3080 es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
3081 strcpy(es->events[es->nevents], timebuf);
3082 strcat(es->events[es->nevents], string);
3084 dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
3089 int askappend(void *frontend, Filename filename,
3090 void (*callback)(void *ctx, int result), void *ctx)
3092 static const char msgtemplate[] =
3093 "The session log file \"%.*s\" already exists. "
3094 "You can overwrite it with a new session log, "
3095 "append your session log to the end of it, "
3096 "or disable session logging for this session.";
3101 message = dupprintf(msgtemplate, FILENAME_MAX, filename.path);
3102 mbtitle = dupprintf("%s Log to File", appname);
3104 mbret = messagebox(get_window(frontend), mbtitle, message,
3105 string_width("LINE OF TEXT SUITABLE FOR THE"
3106 " ASKAPPEND WIDTH"),
3107 "Overwrite", 'o', 1, 2,
3108 "Append", 'a', 0, 1,
3109 "Disable", 'd', -1, 0,