2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
6 * TODO when porting to GTK 2.0:
8 * - GtkTree is apparently deprecated and we should switch to
10 * - GtkLabel has a built-in mnemonic scheme, so we should at
11 * least consider switching to that from the current adhockery.
18 #include <gdk/gdkkeysyms.h>
21 #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 GtkAdjustment
*adj
; /* for the scrollbar in a list box */
62 tree234
*byctrl
, *bywidget
;
64 struct { unsigned char r
, g
, b
, ok
; } coloursel_result
; /* 0-255 */
65 /* `flags' are set to indicate when a GTK signal handler is being called
66 * due to automatic processing and should not flag a user event. */
68 struct Shortcuts
*shortcuts
;
69 GtkWidget
*window
, *cancelbutton
, *currtreeitem
, **treeitems
;
70 union control
*currfocus
, *lastfocus
;
74 #define FLAG_UPDATING_COMBO_LIST 1
76 enum { /* values for Shortcut.action */
77 SHORTCUT_EMPTY
, /* no shortcut on this key */
78 SHORTCUT_TREE
, /* focus a tree item */
79 SHORTCUT_FOCUS
, /* focus the supplied widget */
80 SHORTCUT_UCTRL
, /* do something sane with uctrl */
81 SHORTCUT_UCTRL_UP
, /* uctrl is a draglist, move Up */
82 SHORTCUT_UCTRL_DOWN
, /* uctrl is a draglist, move Down */
88 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
90 static void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
91 int chr
, int action
, void *ptr
);
92 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
94 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
96 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
98 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
99 static void coloursel_ok(GtkButton
*button
, gpointer data
);
100 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
101 static void window_destroy(GtkWidget
*widget
, gpointer data
);
103 static int uctrl_cmp_byctrl(void *av
, void *bv
)
105 struct uctrl
*a
= (struct uctrl
*)av
;
106 struct uctrl
*b
= (struct uctrl
*)bv
;
107 if (a
->ctrl
< b
->ctrl
)
109 else if (a
->ctrl
> b
->ctrl
)
114 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
116 union control
*a
= (union control
*)av
;
117 struct uctrl
*b
= (struct uctrl
*)bv
;
120 else if (a
> b
->ctrl
)
125 static int uctrl_cmp_bywidget(void *av
, void *bv
)
127 struct uctrl
*a
= (struct uctrl
*)av
;
128 struct uctrl
*b
= (struct uctrl
*)bv
;
129 if (a
->toplevel
< b
->toplevel
)
131 else if (a
->toplevel
> b
->toplevel
)
136 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
138 GtkWidget
*a
= (GtkWidget
*)av
;
139 struct uctrl
*b
= (struct uctrl
*)bv
;
142 else if (a
> b
->toplevel
)
147 static void dlg_init(struct dlgparam
*dp
)
149 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
150 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
151 dp
->coloursel_result
.ok
= FALSE
;
152 dp
->treeitems
= NULL
;
153 dp
->window
= dp
->cancelbutton
= dp
->currtreeitem
= NULL
;
156 static void dlg_cleanup(struct dlgparam
*dp
)
160 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
161 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
162 del234(dp
->bywidget
, uc
);
163 if (uc
->privdata_needs_free
)
168 freetree234(dp
->bywidget
);
169 sfree(dp
->treeitems
);
172 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
174 add234(dp
->byctrl
, uc
);
175 add234(dp
->bywidget
, uc
);
178 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
180 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
183 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
185 struct uctrl
*ret
= NULL
;
187 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
195 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
197 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
198 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
202 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
204 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
205 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
207 uc
->privdata_needs_free
= FALSE
;
210 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
212 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
213 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
215 * This is an internal allocation routine, so it's allowed to
216 * use smalloc directly.
218 uc
->privdata
= smalloc(size
);
219 uc
->privdata_needs_free
= FALSE
;
223 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
225 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
226 if (dp
->currfocus
!= ctrl
)
227 return dp
->currfocus
;
229 return dp
->lastfocus
;
232 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
234 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
235 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
236 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
237 assert(uc
->buttons
!= NULL
);
238 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
241 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
243 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
244 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
247 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
248 assert(uc
->buttons
!= NULL
);
249 for (i
= 0; i
< uc
->nbuttons
; i
++)
250 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
252 return 0; /* got to return something */
255 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
257 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
258 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
259 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
260 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
263 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
265 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
266 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
267 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
268 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
271 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
273 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
274 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
275 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
276 assert(uc
->entry
!= NULL
);
277 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
280 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
282 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
283 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
284 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
285 assert(uc
->entry
!= NULL
);
286 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
288 buffer
[length
-1] = '\0';
291 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
293 GtkContainer
*cont
= GTK_CONTAINER(data
);
294 /* gtk_container_remove will unref the widget for us; we need not. */
295 gtk_container_remove(cont
, w
);
298 /* The `listbox' functions can also apply to combo boxes. */
299 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
301 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
302 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
305 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
306 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
307 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
309 cont
= (uc
->menu ?
GTK_CONTAINER(uc
->menu
) : GTK_CONTAINER(uc
->list
));
311 gtk_container_foreach(cont
, container_remove_and_destroy
, cont
);
314 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
316 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
317 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
319 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
320 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
321 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
325 (GTK_CONTAINER(uc
->menu
),
326 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
328 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
332 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
334 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
338 * Each listbox entry may have a numeric id associated with it.
339 * Note that some front ends only permit a string to be stored at
340 * each position, which means that _if_ you put two identical
341 * strings in any listbox then you MUST not assign them different
342 * IDs and expect to get meaningful results back.
344 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
345 char const *text
, int id
)
347 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
348 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
350 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
351 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
352 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
354 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
358 * List item in a drop-down (but non-combo) list. Tabs are
359 * ignored; we just provide a standard menu item with the
362 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
364 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
365 gtk_widget_show(menuitem
);
367 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", (gpointer
)id
);
368 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
369 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
370 } else if (!uc
->entry
) {
372 * List item in a non-combo-box list box. We make all of
373 * these Columns containing GtkLabels. This allows us to do
374 * the nasty force_left hack irrespective of whether there
375 * are tabs in the thing.
377 GtkWidget
*listitem
= gtk_list_item_new();
378 GtkWidget
*cols
= columns_new(10);
382 /* Count the tabs in the text, and hence determine # of columns. */
384 for (i
= 0; text
[i
]; i
++)
389 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
390 percents
= snewn(ncols
, gint
);
391 percents
[ncols
-1] = 100;
392 for (i
= 0; i
< ncols
-1; i
++) {
393 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
394 percents
[ncols
-1] -= percents
[i
];
396 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
399 for (i
= 0; i
< ncols
; i
++) {
400 int len
= strcspn(text
, "\t");
401 char *dup
= dupprintf("%.*s", len
, text
);
406 label
= gtk_label_new(dup
);
409 columns_add(COLUMNS(cols
), label
, i
, 1);
410 columns_force_left_align(COLUMNS(cols
), label
);
411 gtk_widget_show(label
);
413 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
414 gtk_widget_show(cols
);
415 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
416 gtk_widget_show(listitem
);
418 if (ctrl
->listbox
.multisel
) {
419 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
420 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
422 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
423 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
425 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
426 GTK_SIGNAL_FUNC(widget_focus
), dp
);
427 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
428 GTK_SIGNAL_FUNC(listitem_button
), dp
);
429 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
432 * List item in a combo-box list, which means the sensible
433 * thing to do is make it a perfectly normal label. Hence
434 * tabs are disregarded.
436 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
438 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
439 gtk_widget_show(listitem
);
441 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
444 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
447 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
449 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
450 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
454 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
455 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
456 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
458 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
460 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
461 g_list_free(children
);
463 return (int)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
466 /* dlg_listbox_index returns <0 if no single element is selected. */
467 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
469 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
470 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
472 GtkWidget
*item
, *activeitem
;
476 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
477 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
478 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
481 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
483 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
485 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
486 i
++, children
= children
->next
) {
487 if (uc
->menu ? activeitem
== item
:
488 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
495 g_list_free(children
);
496 return selected
< 0 ?
-1 : selected
;
499 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
501 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
502 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
504 GtkWidget
*item
, *activeitem
;
506 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
507 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
508 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
510 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
512 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
513 g_list_free(children
);
516 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
517 return item
== activeitem
;
519 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
523 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
525 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
526 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
528 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
529 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
530 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
533 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
535 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
539 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
541 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
542 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
544 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
545 assert(uc
->text
!= NULL
);
547 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
550 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
552 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
553 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
554 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
555 assert(uc
->entry
!= NULL
);
556 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
559 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
561 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
562 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
563 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
564 assert(uc
->entry
!= NULL
);
565 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
567 fn
->path
[lenof(fn
->path
)-1] = '\0';
570 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
572 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
573 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
574 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
575 assert(uc
->entry
!= NULL
);
576 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
579 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
581 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
582 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
583 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
584 assert(uc
->entry
!= NULL
);
585 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
587 fs
->name
[lenof(fs
->name
)-1] = '\0';
591 * Bracketing a large set of updates in these two functions will
592 * cause the front end (if possible) to delay updating the screen
593 * until it's all complete, thus avoiding flicker.
595 void dlg_update_start(union control
*ctrl
, void *dlg
)
598 * Apparently we can't do this at all in GTK. GtkCList supports
599 * freeze and thaw, but not GtkList. Bah.
603 void dlg_update_done(union control
*ctrl
, void *dlg
)
606 * Apparently we can't do this at all in GTK. GtkCList supports
607 * freeze and thaw, but not GtkList. Bah.
611 void dlg_set_focus(union control
*ctrl
, void *dlg
)
613 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
614 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
616 switch (ctrl
->generic
.type
) {
619 /* Check boxes and buttons get the focus _and_ get toggled. */
620 gtk_widget_grab_focus(uc
->toplevel
);
622 case CTRL_FILESELECT
:
623 case CTRL_FONTSELECT
:
625 /* Anything containing an edit box gets that focused. */
626 gtk_widget_grab_focus(uc
->entry
);
630 * Radio buttons: we find the currently selected button and
635 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
636 if (gtk_toggle_button_get_active
637 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
638 gtk_widget_grab_focus(uc
->buttons
[i
]);
644 * If the list is really an option menu, we focus it.
645 * Otherwise we tell it to focus one of its children, which
646 * appears to do the Right Thing.
649 gtk_widget_grab_focus(uc
->optmenu
);
651 assert(uc
->list
!= NULL
);
652 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
659 * During event processing, you might well want to give an error
660 * indication to the user. dlg_beep() is a quick and easy generic
661 * error; dlg_error() puts up a message-box or equivalent.
663 void dlg_beep(void *dlg
)
668 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
670 gtk_widget_destroy(GTK_WIDGET(data
));
673 void dlg_error_msg(void *dlg
, char *msg
)
675 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
676 GtkWidget
*window
, *hbox
, *text
, *ok
;
678 window
= gtk_dialog_new();
679 text
= gtk_label_new(msg
);
680 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
681 hbox
= gtk_hbox_new(FALSE
, 0);
682 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
683 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
684 hbox
, FALSE
, FALSE
, 20);
685 gtk_widget_show(text
);
686 gtk_widget_show(hbox
);
687 gtk_window_set_title(GTK_WINDOW(window
), "Error");
688 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
689 ok
= gtk_button_new_with_label("OK");
690 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
691 ok
, FALSE
, FALSE
, 0);
693 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
694 gtk_window_set_default(GTK_WINDOW(window
), ok
);
695 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
696 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
697 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
698 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
699 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
700 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
702 gint x
, y
, w
, h
, dx
, dy
;
703 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_NONE
);
704 gdk_window_get_origin(GTK_WIDGET(dp
->window
)->window
, &x
, &y
);
705 gdk_window_get_size(GTK_WIDGET(dp
->window
)->window
, &w
, &h
);
708 gtk_widget_set_uposition(GTK_WIDGET(window
), dx
, dy
);
710 gtk_widget_show(window
);
715 * This function signals to the front end that the dialog's
716 * processing is completed, and passes an integer value (typically
719 void dlg_end(void *dlg
, int value
)
721 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
723 gtk_widget_destroy(dp
->window
);
726 void dlg_refresh(union control
*ctrl
, void *dlg
)
728 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
732 if (ctrl
->generic
.handler
!= NULL
)
733 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
737 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
738 assert(uc
->ctrl
!= NULL
);
739 if (uc
->ctrl
->generic
.handler
!= NULL
)
740 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
741 dp
->data
, EVENT_REFRESH
);
746 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
748 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
749 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
752 GtkWidget
*coloursel
=
753 gtk_color_selection_dialog_new("Select a colour");
754 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
756 dp
->coloursel_result
.ok
= FALSE
;
758 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
759 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
760 cvals
[0] = r
/ 255.0;
761 cvals
[1] = g
/ 255.0;
762 cvals
[2] = b
/ 255.0;
763 cvals
[3] = 1.0; /* fully opaque! */
764 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
766 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
767 (gpointer
)coloursel
);
768 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
769 (gpointer
)coloursel
);
770 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
771 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
772 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
773 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
774 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
775 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
776 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
777 (gpointer
)coloursel
);
778 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
779 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
780 (gpointer
)coloursel
);
781 gtk_widget_show(coloursel
);
784 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
785 int *r
, int *g
, int *b
)
787 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
788 if (dp
->coloursel_result
.ok
) {
789 *r
= dp
->coloursel_result
.r
;
790 *g
= dp
->coloursel_result
.g
;
791 *b
= dp
->coloursel_result
.b
;
797 /* ----------------------------------------------------------------------
798 * Signal handlers while the dialog box is active.
801 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
804 struct dlgparam
*dp
= (struct dlgparam
*)data
;
805 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
806 union control
*focus
;
813 if (focus
!= dp
->currfocus
) {
814 dp
->lastfocus
= dp
->currfocus
;
815 dp
->currfocus
= focus
;
821 static void button_clicked(GtkButton
*button
, gpointer data
)
823 struct dlgparam
*dp
= (struct dlgparam
*)data
;
824 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
825 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
828 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
830 struct dlgparam
*dp
= (struct dlgparam
*)data
;
831 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
832 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
835 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
838 * GtkEntry has a nasty habit of eating the Return key, which
839 * is unhelpful since it doesn't actually _do_ anything with it
840 * (it calls gtk_widget_activate, but our edit boxes never need
841 * activating). So I catch Return before GtkEntry sees it, and
842 * pass it straight on to the parent widget. Effect: hitting
843 * Return in an edit box will now activate the default button
844 * in the dialog just like it will everywhere else.
846 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
848 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
849 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
856 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
858 struct dlgparam
*dp
= (struct dlgparam
*)data
;
859 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
860 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
861 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
865 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
868 struct dlgparam
*dp
= (struct dlgparam
*)data
;
869 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
870 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
873 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
876 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
878 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
879 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
880 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
881 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
883 * Up, Down, PgUp or PgDn have been pressed on a ListItem
884 * in a list box. So, if the list box is single-selection:
886 * - if the list item in question isn't already selected,
887 * we simply select it.
888 * - otherwise, we find the next one (or next
889 * however-far-away) in whichever direction we're going,
891 * + in this case, we must also fiddle with the
892 * scrollbar to ensure the newly selected item is
895 * If it's multiple-selection, we do all of the above
896 * except actually selecting anything, so we move the focus
897 * and fiddle the scrollbar to follow it.
899 GtkWidget
*list
= item
->parent
;
901 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
904 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
905 gtk_list_select_child(GTK_LIST(list
), item
);
908 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
909 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
912 (event
->keyval
==GDK_Page_Down
||
913 event
->keyval
==GDK_KP_Page_Down
||
914 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
917 GList
*children
, *chead
;
919 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
921 n
= g_list_length(children
);
925 * Figure out how many list items to a screenful,
926 * and adjust the step appropriately.
928 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
929 step
--; /* go by one less than that */
933 while (children
!= NULL
) {
934 if (item
== children
->data
)
936 children
= children
->next
;
941 if (direction
< 0 && i
> 0)
942 children
= children
->prev
, i
--;
943 else if (direction
> 0 && i
< n
-1)
944 children
= children
->next
, i
++;
948 if (children
&& children
->data
) {
950 gtk_list_select_child(GTK_LIST(list
),
951 GTK_WIDGET(children
->data
));
952 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
953 gtk_adjustment_clamp_page
955 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
956 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
967 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
970 return listitem_key(item
, event
, data
, FALSE
);
973 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
976 return listitem_key(item
, event
, data
, TRUE
);
979 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
982 struct dlgparam
*dp
= (struct dlgparam
*)data
;
983 if (event
->type
== GDK_2BUTTON_PRESS
||
984 event
->type
== GDK_3BUTTON_PRESS
) {
985 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
986 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
992 static void list_selchange(GtkList
*list
, gpointer data
)
994 struct dlgparam
*dp
= (struct dlgparam
*)data
;
995 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
996 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
999 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1001 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1002 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1003 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1004 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1005 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1008 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1010 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1011 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1015 (index
== 0 && direction
< 0) ||
1016 (index
== g_list_length(children
)-1 && direction
> 0)) {
1021 child
= g_list_nth_data(children
, index
);
1022 gtk_widget_ref(child
);
1023 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1024 g_list_free(children
);
1027 children
= g_list_append(children
, child
);
1028 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1029 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1030 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1033 static void draglist_up(GtkButton
*button
, gpointer data
)
1035 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1036 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1037 draglist_move(dp
, uc
, -1);
1040 static void draglist_down(GtkButton
*button
, gpointer data
)
1042 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1043 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1044 draglist_move(dp
, uc
, +1);
1047 static void filesel_ok(GtkButton
*button
, gpointer data
)
1049 /* struct dlgparam *dp = (struct dlgparam *)data; */
1050 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1051 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1052 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1053 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1056 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1058 /* struct dlgparam *dp = (struct dlgparam *)data; */
1059 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1060 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1061 char *name
= gtk_font_selection_dialog_get_font_name
1062 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1063 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1066 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1068 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1069 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1070 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1072 gtk_color_selection_get_color
1073 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1075 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1076 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1077 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1078 dp
->coloursel_result
.ok
= TRUE
;
1079 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1082 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1084 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1085 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1086 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1087 dp
->coloursel_result
.ok
= FALSE
;
1088 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1091 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1093 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1094 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1096 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1097 GtkWidget
*filesel
=
1098 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1099 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1101 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1103 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1105 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1106 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1107 gtk_signal_connect_object
1108 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1109 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1110 gtk_signal_connect_object
1111 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1112 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1113 gtk_widget_show(filesel
);
1116 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1117 gchar
*spacings
[] = { "c", "m", NULL
};
1118 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1119 GtkWidget
*fontsel
=
1120 gtk_font_selection_dialog_new("Select a font");
1121 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1122 gtk_font_selection_dialog_set_filter
1123 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1124 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1125 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1126 if (!gtk_font_selection_dialog_set_font_name
1127 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1129 * If the font name wasn't found as it was, try opening
1130 * it and extracting its FONT property. This should
1131 * have the effect of mapping short aliases into true
1134 GdkFont
*font
= gdk_font_load(fontname
);
1136 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1137 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1138 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1140 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1141 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1143 gtk_font_selection_dialog_set_font_name
1144 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1146 gdk_font_unref(font
);
1150 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1151 "user-data", (gpointer
)fontsel
);
1152 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1154 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1155 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1156 gtk_signal_connect_object
1157 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1158 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1160 gtk_signal_connect_object
1161 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1162 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1164 gtk_widget_show(fontsel
);
1168 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1171 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1172 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1174 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1175 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1176 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1179 /* ----------------------------------------------------------------------
1180 * This function does the main layout work: it reads a controlset,
1181 * it creates the relevant GTK controls, and returns a GtkWidget
1182 * containing the result. (This widget might be a title of some
1183 * sort, it might be a Columns containing many controls, or it
1184 * might be a GtkFrame containing a Columns; whatever it is, it's
1185 * definitely a GtkWidget and should probably be added to a
1188 * `listitemheight' is used to calculate a usize for list boxes: it
1189 * should be the height from the size request of a GtkListItem.
1191 * `win' is required for setting the default button. If it is
1192 * non-NULL, all buttons created will be default-capable (so they
1193 * have extra space round them for the default highlight).
1195 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1196 struct controlset
*s
, int listitemheight
,
1203 if (!s
->boxname
&& s
->boxtitle
) {
1204 /* This controlset is a panel title. */
1205 return gtk_label_new(s
->boxtitle
);
1209 * Otherwise, we expect to be laying out actual controls, so
1210 * we'll start by creating a Columns for the purpose.
1212 cols
= COLUMNS(columns_new(4));
1213 ret
= GTK_WIDGET(cols
);
1214 gtk_widget_show(ret
);
1217 * Create a containing frame if we have a box name.
1220 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1221 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1222 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1223 gtk_widget_show(ret
);
1227 * Now iterate through the controls themselves, create them,
1228 * and add them to the Columns.
1230 for (i
= 0; i
< s
->ncontrols
; i
++) {
1231 union control
*ctrl
= s
->ctrls
[i
];
1234 GtkWidget
*w
= NULL
;
1236 switch (ctrl
->generic
.type
) {
1239 static const int simplecols
[1] = { 100 };
1240 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1241 (ctrl
->columns
.percentages ?
1242 ctrl
->columns
.percentages
: simplecols
));
1244 continue; /* no actual control created */
1247 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1249 columns_taborder_last(cols
, uc
->toplevel
);
1251 continue; /* no actual control created */
1254 uc
= snew(struct uctrl
);
1256 uc
->privdata
= NULL
;
1257 uc
->privdata_needs_free
= FALSE
;
1259 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1260 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1262 switch (ctrl
->generic
.type
) {
1264 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1266 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1267 if (ctrl
->button
.isdefault
)
1268 gtk_window_set_default(win
, w
);
1269 if (ctrl
->button
.iscancel
)
1270 dp
->cancelbutton
= w
;
1272 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1273 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1274 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1275 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1276 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1277 SHORTCUT_UCTRL
, uc
);
1280 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1281 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1282 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1283 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1284 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1285 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1286 SHORTCUT_UCTRL
, uc
);
1291 * Radio buttons get to go inside their own Columns, no
1295 gint i
, *percentages
;
1299 if (ctrl
->generic
.label
) {
1300 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1301 columns_add(COLUMNS(w
), label
, 0, 1);
1302 columns_force_left_align(COLUMNS(w
), label
);
1303 gtk_widget_show(label
);
1304 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1305 SHORTCUT_UCTRL
, uc
);
1307 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1308 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1310 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1311 100 * i
/ ctrl
->radio
.ncolumns
);
1313 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1315 g_free(percentages
);
1318 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1319 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1321 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1325 b
= (gtk_radio_button_new_with_label
1326 (group
, ctrl
->radio
.buttons
[i
]));
1328 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1329 colstart
= i
% ctrl
->radio
.ncolumns
;
1330 columns_add(COLUMNS(w
), b
, colstart
,
1331 (i
== ctrl
->radio
.nbuttons
-1 ?
1332 ctrl
->radio
.ncolumns
- colstart
: 1));
1333 columns_force_left_align(COLUMNS(w
), b
);
1335 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1336 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1337 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1338 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1339 if (ctrl
->radio
.shortcuts
) {
1340 shortcut_add(scs
, GTK_BIN(b
)->child
,
1341 ctrl
->radio
.shortcuts
[i
],
1342 SHORTCUT_UCTRL
, uc
);
1351 if (ctrl
->editbox
.has_list
) {
1352 w
= gtk_combo_new();
1353 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1354 uc
->entry
= GTK_COMBO(w
)->entry
;
1355 uc
->list
= GTK_COMBO(w
)->list
;
1357 w
= gtk_entry_new();
1358 if (ctrl
->editbox
.password
)
1359 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1362 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1363 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1364 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1365 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1366 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1367 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1369 * Edit boxes, for some strange reason, have a minimum
1370 * width of 150 in GTK 1.2. We don't want this - we'd
1371 * rather the edit boxes acquired their natural width
1372 * from the column layout of the rest of the box.
1374 * Also, while we're here, we'll squirrel away the
1375 * edit box height so we can use that to centre its
1376 * label vertically beside it.
1378 gtk_widget_size_request(w
, &req
);
1379 gtk_widget_set_usize(w
, 10, req
.height
);
1381 if (ctrl
->generic
.label
) {
1382 GtkWidget
*label
, *container
;
1384 label
= gtk_label_new(ctrl
->generic
.label
);
1386 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1387 SHORTCUT_FOCUS
, uc
->entry
);
1389 container
= columns_new(4);
1390 if (ctrl
->editbox
.percentwidth
== 100) {
1391 columns_add(COLUMNS(container
), label
, 0, 1);
1392 columns_force_left_align(COLUMNS(container
), label
);
1393 columns_add(COLUMNS(container
), w
, 0, 1);
1395 gint percentages
[2];
1396 percentages
[1] = ctrl
->editbox
.percentwidth
;
1397 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1398 columns_set_cols(COLUMNS(container
), 2, percentages
);
1399 columns_add(COLUMNS(container
), label
, 0, 1);
1400 columns_force_left_align(COLUMNS(container
), label
);
1401 columns_add(COLUMNS(container
), w
, 1, 1);
1402 /* Centre the label vertically. */
1403 gtk_widget_set_usize(label
, -1, req
.height
);
1404 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1406 gtk_widget_show(label
);
1411 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1412 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1415 case CTRL_FILESELECT
:
1416 case CTRL_FONTSELECT
:
1421 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1422 "Browse..." : "Change...");
1424 gint percentages
[] = { 75, 25 };
1426 columns_set_cols(COLUMNS(w
), 2, percentages
);
1428 if (ctrl
->generic
.label
) {
1429 ww
= gtk_label_new(ctrl
->generic
.label
);
1430 columns_add(COLUMNS(w
), ww
, 0, 2);
1431 columns_force_left_align(COLUMNS(w
), ww
);
1432 gtk_widget_show(ww
);
1433 shortcut_add(scs
, ww
,
1434 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1435 ctrl
->fileselect
.shortcut
:
1436 ctrl
->fontselect
.shortcut
),
1437 SHORTCUT_UCTRL
, uc
);
1440 uc
->entry
= ww
= gtk_entry_new();
1441 gtk_widget_size_request(ww
, &req
);
1442 gtk_widget_set_usize(ww
, 10, req
.height
);
1443 columns_add(COLUMNS(w
), ww
, 0, 1);
1444 gtk_widget_show(ww
);
1446 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1447 columns_add(COLUMNS(w
), ww
, 1, 1);
1448 gtk_widget_show(ww
);
1450 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1451 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1452 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1453 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1454 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1455 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1456 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1457 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1458 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1459 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1463 if (ctrl
->listbox
.height
== 0) {
1464 uc
->optmenu
= w
= gtk_option_menu_new();
1465 uc
->menu
= gtk_menu_new();
1466 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1467 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1468 (gpointer
)uc
->optmenu
);
1469 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1470 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1472 uc
->list
= gtk_list_new();
1473 if (ctrl
->listbox
.multisel
) {
1474 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1475 GTK_SELECTION_MULTIPLE
);
1477 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1478 GTK_SELECTION_SINGLE
);
1480 w
= gtk_scrolled_window_new(NULL
, NULL
);
1481 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1483 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1485 GTK_POLICY_AUTOMATIC
);
1486 uc
->adj
= gtk_scrolled_window_get_vadjustment
1487 (GTK_SCROLLED_WINDOW(w
));
1489 gtk_widget_show(uc
->list
);
1490 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1491 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1492 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1493 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1496 * Adjust the height of the scrolled window to the
1497 * minimum given by the height parameter.
1499 * This piece of guesswork is a horrid hack based
1500 * on looking inside the GTK 1.2 sources
1501 * (specifically gtkviewport.c, which appears to be
1502 * the widget which provides the border around the
1503 * scrolling area). Anyone lets me know how I can
1504 * do this in a way which isn't at risk from GTK
1505 * upgrades, I'd be grateful.
1508 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1509 gtk_widget_set_usize(w
, 10,
1510 2*edge
+ (ctrl
->listbox
.height
*
1514 if (ctrl
->listbox
.draglist
) {
1516 * GTK doesn't appear to make it easy to
1517 * implement a proper draggable list; so
1518 * instead I'm just going to have to put an Up
1519 * and a Down button to the right of the actual
1520 * list box. Ah well.
1522 GtkWidget
*cols
, *button
;
1523 static const gint percentages
[2] = { 80, 20 };
1525 cols
= columns_new(4);
1526 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1527 columns_add(COLUMNS(cols
), w
, 0, 1);
1529 button
= gtk_button_new_with_label("Up");
1530 columns_add(COLUMNS(cols
), button
, 1, 1);
1531 gtk_widget_show(button
);
1532 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1533 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1534 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1535 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1536 button
= gtk_button_new_with_label("Down");
1537 columns_add(COLUMNS(cols
), button
, 1, 1);
1538 gtk_widget_show(button
);
1539 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1540 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1541 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1542 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1548 if (ctrl
->generic
.label
) {
1549 GtkWidget
*label
, *container
;
1551 label
= gtk_label_new(ctrl
->generic
.label
);
1553 container
= columns_new(4);
1554 if (ctrl
->listbox
.percentwidth
== 100) {
1555 columns_add(COLUMNS(container
), label
, 0, 1);
1556 columns_force_left_align(COLUMNS(container
), label
);
1557 columns_add(COLUMNS(container
), w
, 0, 1);
1559 gint percentages
[2];
1560 percentages
[1] = ctrl
->listbox
.percentwidth
;
1561 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1562 columns_set_cols(COLUMNS(container
), 2, percentages
);
1563 columns_add(COLUMNS(container
), label
, 0, 1);
1564 columns_force_left_align(COLUMNS(container
), label
);
1565 columns_add(COLUMNS(container
), w
, 1, 1);
1567 gtk_widget_show(label
);
1569 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1570 SHORTCUT_UCTRL
, uc
);
1576 * Wrapping text widgets don't sit well with the GTK
1577 * layout model, in which widgets state a minimum size
1578 * and the whole window then adjusts to the smallest
1579 * size it can sensibly take given its contents. A
1580 * wrapping text widget _has_ no clear minimum size;
1581 * instead it has a range of possibilities. It can be
1582 * one line deep but 2000 wide, or two lines deep and
1583 * 1000 pixels, or three by 867, or four by 500 and so
1584 * on. It can be as short as you like provided you
1585 * don't mind it being wide, or as narrow as you like
1586 * provided you don't mind it being tall.
1588 * Therefore, it fits very badly into the layout model.
1589 * Hence the only thing to do is pick a width and let
1590 * it choose its own number of lines. To do this I'm
1591 * going to cheat a little. All new wrapping text
1592 * widgets will be created with a minimal text content
1593 * "X"; then, after the rest of the dialog box is set
1594 * up and its size calculated, the text widgets will be
1595 * told their width and given their real text, which
1596 * will cause the size to be recomputed in the y
1597 * direction (because many of them will expand to more
1600 uc
->text
= w
= gtk_label_new("X");
1601 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1602 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1604 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1605 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1611 columns_add(cols
, w
,
1612 COLUMN_START(ctrl
->generic
.column
),
1613 COLUMN_SPAN(ctrl
->generic
.column
));
1615 columns_force_left_align(cols
, w
);
1619 dlg_add_uctrl(dp
, uc
);
1626 struct dlgparam
*dp
;
1628 GtkWidget
*panel
, *treeitem
;
1629 struct Shortcuts shortcuts
;
1632 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1634 struct selparam
*sp
= (struct selparam
*)data
;
1636 panels_switch_to(sp
->panels
, sp
->panel
);
1638 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1639 sp
->dp
->currtreeitem
= sp
->treeitem
;
1642 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1647 static int tree_grab_focus(struct dlgparam
*dp
)
1652 * See if any of the treeitems has the focus.
1655 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1656 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1664 gtk_widget_grab_focus(dp
->currtreeitem
);
1669 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1672 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1674 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1676 * If there's a focused treeitem, we return FALSE to cause the
1677 * focus to move on to some totally other control. If not, we
1678 * focus the selected one.
1680 return tree_grab_focus(dp
);
1683 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1685 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1687 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1688 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1692 if ((event
->state
& GDK_MOD1_MASK
) &&
1693 (unsigned char)event
->string
[0] > 0 &&
1694 (unsigned char)event
->string
[0] <= 127) {
1695 int schr
= (unsigned char)event
->string
[0];
1696 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1698 switch (sc
->action
) {
1700 tree_grab_focus(dp
);
1702 case SHORTCUT_FOCUS
:
1703 gtk_widget_grab_focus(sc
->widget
);
1705 case SHORTCUT_UCTRL
:
1707 * We must do something sensible with a uctrl.
1708 * Precisely what this is depends on the type of
1711 switch (sc
->uc
->ctrl
->generic
.type
) {
1714 /* Check boxes and buttons get the focus _and_ get toggled. */
1715 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1716 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1719 case CTRL_FILESELECT
:
1720 case CTRL_FONTSELECT
:
1721 /* File/font selectors have their buttons pressed (ooer),
1722 * and focus transferred to the edit box. */
1723 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1725 gtk_widget_grab_focus(sc
->uc
->entry
);
1729 * Radio buttons are fun, because they have
1730 * multiple shortcuts. We must find whether the
1731 * activated shortcut is the shortcut for the whole
1732 * group, or for a particular button. In the former
1733 * case, we find the currently selected button and
1734 * focus it; in the latter, we focus-and-click the
1735 * button whose shortcut was pressed.
1737 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1739 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1740 if (gtk_toggle_button_get_active
1741 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1742 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1744 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1746 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1747 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1748 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1749 gtk_signal_emit_by_name
1750 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1756 * If the list is really an option menu, we focus
1757 * and click it. Otherwise we tell it to focus one
1758 * of its children, which appears to do the Right
1761 if (sc
->uc
->optmenu
) {
1765 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1766 /* Option menus don't work using the "clicked" signal.
1767 * We need to manufacture a button press event :-/ */
1768 bev
.type
= GDK_BUTTON_PRESS
;
1770 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1771 "button_press_event",
1774 assert(sc
->uc
->list
!= NULL
);
1776 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1777 GTK_DIR_TAB_FORWARD
);
1788 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1790 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1792 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1793 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1795 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1796 if (widget
== dp
->treeitems
[i
])
1798 if (i
< dp
->ntreeitems
) {
1799 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1806 if (i
< 0 || i
>= dp
->ntreeitems
)
1807 break; /* nothing in that dir to select */
1809 * Determine if this tree item is visible.
1812 GtkWidget
*w
= dp
->treeitems
[i
];
1814 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1815 if (!GTK_WIDGET_VISIBLE(w
)) {
1822 j
= i
; /* got one */
1828 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1831 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1832 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1838 * It's nice for Left and Right to expand and collapse tree
1841 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1842 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1844 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1847 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1848 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1850 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1857 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1858 int chr
, int action
, void *ptr
)
1860 GtkLabel
*label
= GTK_LABEL(labelw
);
1861 gchar
*currstr
, *pattern
;
1864 if (chr
== NO_SHORTCUT
)
1867 chr
= tolower((unsigned char)chr
);
1869 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1871 scs
->sc
[chr
].action
= action
;
1873 if (action
== SHORTCUT_FOCUS
) {
1874 scs
->sc
[chr
].uc
= NULL
;
1875 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1877 scs
->sc
[chr
].widget
= NULL
;
1878 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1881 gtk_label_get(label
, &currstr
);
1882 for (i
= 0; currstr
[i
]; i
++)
1883 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1886 pattern
= dupprintf("%*s_", i
, "");
1888 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1889 gtk_label_set_pattern(label
, pattern
);
1890 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1897 int do_config_box(const char *title
, Config
*cfg
)
1899 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1900 *tree
, *treescroll
, *panels
, *panelvbox
;
1901 int index
, level
, listitemheight
;
1902 struct controlbox
*ctrlbox
;
1904 GtkTreeItem
*treeitemlevels
[8];
1905 GtkTree
*treelevels
[8];
1908 struct Shortcuts scs
;
1910 struct selparam
*selparams
= NULL
;
1911 int nselparams
= 0, selparamsize
= 0;
1913 do_defaults(NULL
, cfg
);
1918 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1920 gtk_widget_size_request(listitem
, &req
);
1921 listitemheight
= req
.height
;
1922 gtk_widget_unref(listitem
);
1927 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1928 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
1931 ctrlbox
= ctrl_new_box();
1932 setup_config_box(ctrlbox
, &sl
, FALSE
, 0);
1933 unix_setup_config_box(ctrlbox
, FALSE
);
1935 window
= gtk_dialog_new();
1936 gtk_window_set_title(GTK_WINDOW(window
), title
);
1937 hbox
= gtk_hbox_new(FALSE
, 4);
1938 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
1939 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
1940 gtk_widget_show(hbox
);
1941 vbox
= gtk_vbox_new(FALSE
, 4);
1942 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
1943 gtk_widget_show(vbox
);
1944 cols
= columns_new(4);
1945 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
1946 gtk_widget_show(cols
);
1947 label
= gtk_label_new("Category:");
1948 columns_add(COLUMNS(cols
), label
, 0, 1);
1949 columns_force_left_align(COLUMNS(cols
), label
);
1950 gtk_widget_show(label
);
1951 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
1952 tree
= gtk_tree_new();
1953 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
1954 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1955 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
1956 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
1957 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
1958 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
1960 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
1962 GTK_POLICY_AUTOMATIC
);
1963 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
1964 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
1965 gtk_widget_show(tree
);
1966 gtk_widget_show(treescroll
);
1967 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
1968 panels
= panels_new();
1969 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
1970 gtk_widget_show(panels
);
1975 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
1976 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
1979 if (!*s
->pathname
) {
1980 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
1981 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1984 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
1985 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
1987 GtkWidget
*treeitem
;
1991 * We expect never to find an implicit path
1992 * component. For example, we expect never to see
1993 * A/B/C followed by A/D/E, because that would
1994 * _implicitly_ create A/D. All our path prefixes
1995 * are expected to contain actual controls and be
1996 * selectable in the treeview; so we would expect
1997 * to see A/D _explicitly_ before encountering
2000 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2002 c
= strrchr(s
->pathname
, '/');
2008 treeitem
= gtk_tree_item_new_with_label(c
);
2009 assert(j
-1 < level
);
2011 if (!treelevels
[j
-1]) {
2012 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2013 gtk_tree_item_set_subtree
2014 (treeitemlevels
[j
-1],
2015 GTK_WIDGET(treelevels
[j
-1]));
2016 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2018 gtk_tree_append(treelevels
[j
-1], treeitem
);
2020 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2022 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2023 treelevels
[j
] = NULL
;
2026 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2027 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2028 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2029 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2031 gtk_widget_show(treeitem
);
2035 first
= (panelvbox
== NULL
);
2037 panelvbox
= gtk_vbox_new(FALSE
, 4);
2038 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2040 panels_switch_to(PANELS(panels
), panelvbox
);
2041 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2044 if (nselparams
>= selparamsize
) {
2046 selparams
= sresize(selparams
, selparamsize
,
2049 selparams
[nselparams
].dp
= &dp
;
2050 selparams
[nselparams
].panels
= PANELS(panels
);
2051 selparams
[nselparams
].panel
= panelvbox
;
2052 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2053 selparams
[nselparams
].treeitem
= treeitem
;
2058 w
= layout_ctrls(&dp
,
2059 &selparams
[nselparams
-1].shortcuts
,
2060 s
, listitemheight
, NULL
);
2061 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2066 dp
.ntreeitems
= nselparams
;
2067 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2069 for (index
= 0; index
< nselparams
; index
++) {
2070 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2071 GTK_SIGNAL_FUNC(treeitem_sel
),
2073 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2077 dlg_refresh(NULL
, &dp
);
2079 dp
.shortcuts
= &selparams
[0].shortcuts
;
2080 dp
.currtreeitem
= dp
.treeitems
[0];
2081 dp
.lastfocus
= NULL
;
2085 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2086 gtk_widget_show(window
);
2089 * Set focus into the first available control.
2091 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2092 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2097 for (j
= 0; j
< s
->ncontrols
; j
++)
2098 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2099 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2100 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2101 dlg_set_focus(s
->ctrls
[j
], &dp
);
2102 dp
.lastfocus
= s
->ctrls
[j
];
2111 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2112 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2113 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2114 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2124 static void messagebox_handler(union control
*ctrl
, void *dlg
,
2125 void *data
, int event
)
2127 if (event
== EVENT_ACTION
)
2128 dlg_end(dlg
, ctrl
->generic
.context
.i
);
2130 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
2132 GtkWidget
*window
, *w0
, *w1
;
2133 struct controlbox
*ctrlbox
;
2134 struct controlset
*s0
, *s1
;
2137 struct Shortcuts scs
;
2143 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2144 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2147 ctrlbox
= ctrl_new_box();
2150 va_start(ap
, minwid
);
2151 while (va_arg(ap
, char *) != NULL
) {
2153 (void) va_arg(ap
, int); /* shortcut */
2154 (void) va_arg(ap
, int); /* normal/default/cancel */
2155 (void) va_arg(ap
, int); /* end value */
2159 s0
= ctrl_getset(ctrlbox
, "", "", "");
2160 c
= ctrl_columns(s0
, 2, 50, 50);
2161 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
2162 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
2163 for (index
= 0; index
< ncols
; index
++)
2164 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
2165 va_start(ap
, minwid
);
2168 char *title
= va_arg(ap
, char *);
2169 int shortcut
, type
, value
;
2172 shortcut
= va_arg(ap
, int);
2173 type
= va_arg(ap
, int);
2174 value
= va_arg(ap
, int);
2175 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
2176 messagebox_handler
, I(value
));
2177 c
->generic
.column
= index
++;
2179 c
->button
.isdefault
= TRUE
;
2181 c
->button
.iscancel
= TRUE
;
2185 s1
= ctrl_getset(ctrlbox
, "x", "", "");
2186 ctrl_text(s1
, msg
, HELPCTX(no_help
));
2188 window
= gtk_dialog_new();
2189 gtk_window_set_title(GTK_WINDOW(window
), title
);
2190 w0
= layout_ctrls(&dp
, &scs
, s0
, 0, GTK_WINDOW(window
));
2191 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2193 gtk_widget_show(w0
);
2194 w1
= layout_ctrls(&dp
, &scs
, s1
, 0, GTK_WINDOW(window
));
2195 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2196 gtk_widget_set_usize(w1
, minwid
+20, -1);
2197 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2199 gtk_widget_show(w1
);
2201 dp
.shortcuts
= &scs
;
2202 dp
.lastfocus
= NULL
;
2206 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
2208 gint x
, y
, w
, h
, dx
, dy
;
2209 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_NONE
);
2210 gdk_window_get_origin(parentwin
->window
, &x
, &y
);
2211 gdk_window_get_size(parentwin
->window
, &w
, &h
);
2214 gtk_widget_set_uposition(GTK_WIDGET(window
), dx
, dy
);
2215 gtk_window_set_transient_for(GTK_WINDOW(window
),
2216 GTK_WINDOW(parentwin
));
2218 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2219 gtk_widget_show(window
);
2221 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2222 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2223 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2224 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2229 ctrl_free_box(ctrlbox
);
2234 static int string_width(char *text
)
2236 GtkWidget
*label
= gtk_label_new(text
);
2238 gtk_widget_size_request(label
, &req
);
2239 gtk_widget_unref(label
);
2243 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
2244 char *keystr
, char *fingerprint
)
2246 static const char absenttxt
[] =
2247 "The server's host key is not cached. You have no guarantee "
2248 "that the server is the computer you think it is.\n"
2249 "The server's key fingerprint is:\n"
2251 "If you trust this host, press \"Accept\" to add the key to "
2252 "PuTTY's cache and carry on connecting.\n"
2253 "If you want to carry on connecting just once, without "
2254 "adding the key to the cache, press \"Connect Once\".\n"
2255 "If you do not trust this host, press \"Cancel\" to abandon the "
2257 static const char wrongtxt
[] =
2258 "WARNING - POTENTIAL SECURITY BREACH!\n"
2259 "The server's host key does not match the one PuTTY has "
2260 "cached. This means that either the server administrator "
2261 "has changed the host key, or you have actually connected "
2262 "to another computer pretending to be the server.\n"
2263 "The new key fingerprint is:\n"
2265 "If you were expecting this change and trust the new key, "
2266 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2267 "If you want to carry on connecting but without updating "
2268 "the cache, press \"Connect Once\".\n"
2269 "If you want to abandon the connection completely, press "
2270 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2278 ret
= verify_host_key(host
, port
, keytype
, keystr
);
2280 if (ret
== 0) /* success - key matched OK */
2283 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), fingerprint
);
2285 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2286 "PuTTY Security Alert", text
,
2287 string_width(fingerprint
),
2288 "Accept", 'a', 0, 2,
2289 "Connect Once", 'o', 0, 1,
2290 "Cancel", 'c', -1, 0,
2298 store_host_key(host
, port
, keytype
, keystr
);
2302 * Ask whether the selected cipher is acceptable (since it was
2303 * below the configured 'warn' threshold).
2304 * cs: 0 = both ways, 1 = client->server, 2 = server->client
2306 void askcipher(void *frontend
, char *ciphername
, int cs
)
2308 static const char msg
[] =
2309 "The first %scipher supported by the server is "
2310 "%s, which is below the configured warning threshold.\n"
2311 "Continue with connection?";
2315 text
= dupprintf(msg
, (cs
== 0) ?
"" :
2316 (cs
== 1) ?
"client-to-server " : "server-to-client ",
2318 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2319 "PuTTY Security Alert", text
,
2320 string_width("Continue with connection?"),
2333 void old_keyfile_warning(void)
2336 * This should never happen on Unix. We hope.
2340 void fatalbox(char *p
, ...)
2345 msg
= dupvprintf(p
, ap
);
2347 messagebox(NULL
, "PuTTY Fatal Error", msg
,
2348 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2349 "OK", 'o', 1, 1, NULL
);
2353 void connection_fatal(void *frontend
, char *p
, ...)
2358 msg
= dupvprintf(p
, ap
);
2360 messagebox(GTK_WIDGET(get_window(frontend
)),
2361 "PuTTY Fatal Error", msg
,
2362 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2363 "OK", 'o', 1, 1, NULL
);