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
));
997 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1000 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1002 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1003 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1004 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1005 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1006 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1009 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1011 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1012 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1016 (index
== 0 && direction
< 0) ||
1017 (index
== g_list_length(children
)-1 && direction
> 0)) {
1022 child
= g_list_nth_data(children
, index
);
1023 gtk_widget_ref(child
);
1024 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1025 g_list_free(children
);
1028 children
= g_list_append(children
, child
);
1029 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1030 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1031 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1034 static void draglist_up(GtkButton
*button
, gpointer data
)
1036 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1037 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1038 draglist_move(dp
, uc
, -1);
1041 static void draglist_down(GtkButton
*button
, gpointer data
)
1043 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1044 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1045 draglist_move(dp
, uc
, +1);
1048 static void filesel_ok(GtkButton
*button
, gpointer data
)
1050 /* struct dlgparam *dp = (struct dlgparam *)data; */
1051 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1052 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1053 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1054 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1057 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1059 /* struct dlgparam *dp = (struct dlgparam *)data; */
1060 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1061 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1062 char *name
= gtk_font_selection_dialog_get_font_name
1063 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1064 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1067 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1069 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1070 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1071 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1073 gtk_color_selection_get_color
1074 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1076 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1077 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1078 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1079 dp
->coloursel_result
.ok
= TRUE
;
1080 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1083 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1085 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1086 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1087 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1088 dp
->coloursel_result
.ok
= FALSE
;
1089 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1092 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1094 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1095 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1097 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1098 GtkWidget
*filesel
=
1099 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1100 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1102 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1104 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1106 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1107 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1108 gtk_signal_connect_object
1109 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1110 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1111 gtk_signal_connect_object
1112 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1113 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1114 gtk_widget_show(filesel
);
1117 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1118 gchar
*spacings
[] = { "c", "m", NULL
};
1119 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1120 GtkWidget
*fontsel
=
1121 gtk_font_selection_dialog_new("Select a font");
1122 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1123 gtk_font_selection_dialog_set_filter
1124 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1125 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1126 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1127 if (!gtk_font_selection_dialog_set_font_name
1128 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1130 * If the font name wasn't found as it was, try opening
1131 * it and extracting its FONT property. This should
1132 * have the effect of mapping short aliases into true
1135 GdkFont
*font
= gdk_font_load(fontname
);
1137 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1138 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1139 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1141 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1142 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1144 gtk_font_selection_dialog_set_font_name
1145 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1147 gdk_font_unref(font
);
1151 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1152 "user-data", (gpointer
)fontsel
);
1153 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1155 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1156 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1157 gtk_signal_connect_object
1158 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1159 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1161 gtk_signal_connect_object
1162 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1163 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1165 gtk_widget_show(fontsel
);
1169 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1172 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1173 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1175 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1176 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1177 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1180 /* ----------------------------------------------------------------------
1181 * This function does the main layout work: it reads a controlset,
1182 * it creates the relevant GTK controls, and returns a GtkWidget
1183 * containing the result. (This widget might be a title of some
1184 * sort, it might be a Columns containing many controls, or it
1185 * might be a GtkFrame containing a Columns; whatever it is, it's
1186 * definitely a GtkWidget and should probably be added to a
1189 * `listitemheight' is used to calculate a usize for list boxes: it
1190 * should be the height from the size request of a GtkListItem.
1192 * `win' is required for setting the default button. If it is
1193 * non-NULL, all buttons created will be default-capable (so they
1194 * have extra space round them for the default highlight).
1196 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1197 struct controlset
*s
, int listitemheight
,
1204 if (!s
->boxname
&& s
->boxtitle
) {
1205 /* This controlset is a panel title. */
1206 return gtk_label_new(s
->boxtitle
);
1210 * Otherwise, we expect to be laying out actual controls, so
1211 * we'll start by creating a Columns for the purpose.
1213 cols
= COLUMNS(columns_new(4));
1214 ret
= GTK_WIDGET(cols
);
1215 gtk_widget_show(ret
);
1218 * Create a containing frame if we have a box name.
1221 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1222 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1223 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1224 gtk_widget_show(ret
);
1228 * Now iterate through the controls themselves, create them,
1229 * and add them to the Columns.
1231 for (i
= 0; i
< s
->ncontrols
; i
++) {
1232 union control
*ctrl
= s
->ctrls
[i
];
1235 GtkWidget
*w
= NULL
;
1237 switch (ctrl
->generic
.type
) {
1240 static const int simplecols
[1] = { 100 };
1241 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1242 (ctrl
->columns
.percentages ?
1243 ctrl
->columns
.percentages
: simplecols
));
1245 continue; /* no actual control created */
1248 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1250 columns_taborder_last(cols
, uc
->toplevel
);
1252 continue; /* no actual control created */
1255 uc
= snew(struct uctrl
);
1257 uc
->privdata
= NULL
;
1258 uc
->privdata_needs_free
= FALSE
;
1260 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1261 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1263 switch (ctrl
->generic
.type
) {
1265 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1267 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1268 if (ctrl
->button
.isdefault
)
1269 gtk_window_set_default(win
, w
);
1270 if (ctrl
->button
.iscancel
)
1271 dp
->cancelbutton
= w
;
1273 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1274 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1275 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1276 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1277 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1278 SHORTCUT_UCTRL
, uc
);
1281 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1282 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1283 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1284 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1285 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1286 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1287 SHORTCUT_UCTRL
, uc
);
1292 * Radio buttons get to go inside their own Columns, no
1296 gint i
, *percentages
;
1300 if (ctrl
->generic
.label
) {
1301 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1302 columns_add(COLUMNS(w
), label
, 0, 1);
1303 columns_force_left_align(COLUMNS(w
), label
);
1304 gtk_widget_show(label
);
1305 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1306 SHORTCUT_UCTRL
, uc
);
1308 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1309 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1311 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1312 100 * i
/ ctrl
->radio
.ncolumns
);
1314 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1316 g_free(percentages
);
1319 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1320 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1322 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1326 b
= (gtk_radio_button_new_with_label
1327 (group
, ctrl
->radio
.buttons
[i
]));
1329 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1330 colstart
= i
% ctrl
->radio
.ncolumns
;
1331 columns_add(COLUMNS(w
), b
, colstart
,
1332 (i
== ctrl
->radio
.nbuttons
-1 ?
1333 ctrl
->radio
.ncolumns
- colstart
: 1));
1334 columns_force_left_align(COLUMNS(w
), b
);
1336 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1337 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1338 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1339 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1340 if (ctrl
->radio
.shortcuts
) {
1341 shortcut_add(scs
, GTK_BIN(b
)->child
,
1342 ctrl
->radio
.shortcuts
[i
],
1343 SHORTCUT_UCTRL
, uc
);
1352 if (ctrl
->editbox
.has_list
) {
1353 w
= gtk_combo_new();
1354 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1355 uc
->entry
= GTK_COMBO(w
)->entry
;
1356 uc
->list
= GTK_COMBO(w
)->list
;
1358 w
= gtk_entry_new();
1359 if (ctrl
->editbox
.password
)
1360 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1363 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1364 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1365 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1366 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1367 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1368 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1370 * Edit boxes, for some strange reason, have a minimum
1371 * width of 150 in GTK 1.2. We don't want this - we'd
1372 * rather the edit boxes acquired their natural width
1373 * from the column layout of the rest of the box.
1375 * Also, while we're here, we'll squirrel away the
1376 * edit box height so we can use that to centre its
1377 * label vertically beside it.
1379 gtk_widget_size_request(w
, &req
);
1380 gtk_widget_set_usize(w
, 10, req
.height
);
1382 if (ctrl
->generic
.label
) {
1383 GtkWidget
*label
, *container
;
1385 label
= gtk_label_new(ctrl
->generic
.label
);
1387 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1388 SHORTCUT_FOCUS
, uc
->entry
);
1390 container
= columns_new(4);
1391 if (ctrl
->editbox
.percentwidth
== 100) {
1392 columns_add(COLUMNS(container
), label
, 0, 1);
1393 columns_force_left_align(COLUMNS(container
), label
);
1394 columns_add(COLUMNS(container
), w
, 0, 1);
1396 gint percentages
[2];
1397 percentages
[1] = ctrl
->editbox
.percentwidth
;
1398 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1399 columns_set_cols(COLUMNS(container
), 2, percentages
);
1400 columns_add(COLUMNS(container
), label
, 0, 1);
1401 columns_force_left_align(COLUMNS(container
), label
);
1402 columns_add(COLUMNS(container
), w
, 1, 1);
1403 /* Centre the label vertically. */
1404 gtk_widget_set_usize(label
, -1, req
.height
);
1405 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1407 gtk_widget_show(label
);
1412 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1413 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1416 case CTRL_FILESELECT
:
1417 case CTRL_FONTSELECT
:
1422 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1423 "Browse..." : "Change...");
1425 gint percentages
[] = { 75, 25 };
1427 columns_set_cols(COLUMNS(w
), 2, percentages
);
1429 if (ctrl
->generic
.label
) {
1430 ww
= gtk_label_new(ctrl
->generic
.label
);
1431 columns_add(COLUMNS(w
), ww
, 0, 2);
1432 columns_force_left_align(COLUMNS(w
), ww
);
1433 gtk_widget_show(ww
);
1434 shortcut_add(scs
, ww
,
1435 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1436 ctrl
->fileselect
.shortcut
:
1437 ctrl
->fontselect
.shortcut
),
1438 SHORTCUT_UCTRL
, uc
);
1441 uc
->entry
= ww
= gtk_entry_new();
1442 gtk_widget_size_request(ww
, &req
);
1443 gtk_widget_set_usize(ww
, 10, req
.height
);
1444 columns_add(COLUMNS(w
), ww
, 0, 1);
1445 gtk_widget_show(ww
);
1447 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1448 columns_add(COLUMNS(w
), ww
, 1, 1);
1449 gtk_widget_show(ww
);
1451 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1452 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1453 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1454 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1455 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1456 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1457 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1458 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1459 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1460 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1464 if (ctrl
->listbox
.height
== 0) {
1465 uc
->optmenu
= w
= gtk_option_menu_new();
1466 uc
->menu
= gtk_menu_new();
1467 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1468 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1469 (gpointer
)uc
->optmenu
);
1470 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1471 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1473 uc
->list
= gtk_list_new();
1474 if (ctrl
->listbox
.multisel
) {
1475 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1476 GTK_SELECTION_MULTIPLE
);
1478 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1479 GTK_SELECTION_SINGLE
);
1481 w
= gtk_scrolled_window_new(NULL
, NULL
);
1482 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1484 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1486 GTK_POLICY_AUTOMATIC
);
1487 uc
->adj
= gtk_scrolled_window_get_vadjustment
1488 (GTK_SCROLLED_WINDOW(w
));
1490 gtk_widget_show(uc
->list
);
1491 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1492 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1493 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1494 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1497 * Adjust the height of the scrolled window to the
1498 * minimum given by the height parameter.
1500 * This piece of guesswork is a horrid hack based
1501 * on looking inside the GTK 1.2 sources
1502 * (specifically gtkviewport.c, which appears to be
1503 * the widget which provides the border around the
1504 * scrolling area). Anyone lets me know how I can
1505 * do this in a way which isn't at risk from GTK
1506 * upgrades, I'd be grateful.
1509 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1510 gtk_widget_set_usize(w
, 10,
1511 2*edge
+ (ctrl
->listbox
.height
*
1515 if (ctrl
->listbox
.draglist
) {
1517 * GTK doesn't appear to make it easy to
1518 * implement a proper draggable list; so
1519 * instead I'm just going to have to put an Up
1520 * and a Down button to the right of the actual
1521 * list box. Ah well.
1523 GtkWidget
*cols
, *button
;
1524 static const gint percentages
[2] = { 80, 20 };
1526 cols
= columns_new(4);
1527 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1528 columns_add(COLUMNS(cols
), w
, 0, 1);
1530 button
= gtk_button_new_with_label("Up");
1531 columns_add(COLUMNS(cols
), button
, 1, 1);
1532 gtk_widget_show(button
);
1533 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1534 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1535 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1536 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1537 button
= gtk_button_new_with_label("Down");
1538 columns_add(COLUMNS(cols
), button
, 1, 1);
1539 gtk_widget_show(button
);
1540 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1541 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1542 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1543 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1549 if (ctrl
->generic
.label
) {
1550 GtkWidget
*label
, *container
;
1552 label
= gtk_label_new(ctrl
->generic
.label
);
1554 container
= columns_new(4);
1555 if (ctrl
->listbox
.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
->listbox
.percentwidth
;
1562 percentages
[0] = 100 - ctrl
->listbox
.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);
1568 gtk_widget_show(label
);
1570 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1571 SHORTCUT_UCTRL
, uc
);
1577 * Wrapping text widgets don't sit well with the GTK
1578 * layout model, in which widgets state a minimum size
1579 * and the whole window then adjusts to the smallest
1580 * size it can sensibly take given its contents. A
1581 * wrapping text widget _has_ no clear minimum size;
1582 * instead it has a range of possibilities. It can be
1583 * one line deep but 2000 wide, or two lines deep and
1584 * 1000 pixels, or three by 867, or four by 500 and so
1585 * on. It can be as short as you like provided you
1586 * don't mind it being wide, or as narrow as you like
1587 * provided you don't mind it being tall.
1589 * Therefore, it fits very badly into the layout model.
1590 * Hence the only thing to do is pick a width and let
1591 * it choose its own number of lines. To do this I'm
1592 * going to cheat a little. All new wrapping text
1593 * widgets will be created with a minimal text content
1594 * "X"; then, after the rest of the dialog box is set
1595 * up and its size calculated, the text widgets will be
1596 * told their width and given their real text, which
1597 * will cause the size to be recomputed in the y
1598 * direction (because many of them will expand to more
1601 uc
->text
= w
= gtk_label_new("X");
1602 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1603 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1605 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1606 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1612 columns_add(cols
, w
,
1613 COLUMN_START(ctrl
->generic
.column
),
1614 COLUMN_SPAN(ctrl
->generic
.column
));
1616 columns_force_left_align(cols
, w
);
1620 dlg_add_uctrl(dp
, uc
);
1627 struct dlgparam
*dp
;
1629 GtkWidget
*panel
, *treeitem
;
1630 struct Shortcuts shortcuts
;
1633 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1635 struct selparam
*sp
= (struct selparam
*)data
;
1637 panels_switch_to(sp
->panels
, sp
->panel
);
1639 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1640 sp
->dp
->currtreeitem
= sp
->treeitem
;
1643 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1648 static int tree_grab_focus(struct dlgparam
*dp
)
1653 * See if any of the treeitems has the focus.
1656 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1657 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1665 gtk_widget_grab_focus(dp
->currtreeitem
);
1670 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1673 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1675 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1677 * If there's a focused treeitem, we return FALSE to cause the
1678 * focus to move on to some totally other control. If not, we
1679 * focus the selected one.
1681 return tree_grab_focus(dp
);
1684 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1686 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1688 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1689 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1693 if ((event
->state
& GDK_MOD1_MASK
) &&
1694 (unsigned char)event
->string
[0] > 0 &&
1695 (unsigned char)event
->string
[0] <= 127) {
1696 int schr
= (unsigned char)event
->string
[0];
1697 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1699 switch (sc
->action
) {
1701 tree_grab_focus(dp
);
1703 case SHORTCUT_FOCUS
:
1704 gtk_widget_grab_focus(sc
->widget
);
1706 case SHORTCUT_UCTRL
:
1708 * We must do something sensible with a uctrl.
1709 * Precisely what this is depends on the type of
1712 switch (sc
->uc
->ctrl
->generic
.type
) {
1715 /* Check boxes and buttons get the focus _and_ get toggled. */
1716 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1717 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1720 case CTRL_FILESELECT
:
1721 case CTRL_FONTSELECT
:
1722 /* File/font selectors have their buttons pressed (ooer),
1723 * and focus transferred to the edit box. */
1724 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1726 gtk_widget_grab_focus(sc
->uc
->entry
);
1730 * Radio buttons are fun, because they have
1731 * multiple shortcuts. We must find whether the
1732 * activated shortcut is the shortcut for the whole
1733 * group, or for a particular button. In the former
1734 * case, we find the currently selected button and
1735 * focus it; in the latter, we focus-and-click the
1736 * button whose shortcut was pressed.
1738 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1740 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1741 if (gtk_toggle_button_get_active
1742 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1743 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1745 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1747 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1748 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1749 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1750 gtk_signal_emit_by_name
1751 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1757 * If the list is really an option menu, we focus
1758 * and click it. Otherwise we tell it to focus one
1759 * of its children, which appears to do the Right
1762 if (sc
->uc
->optmenu
) {
1766 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1767 /* Option menus don't work using the "clicked" signal.
1768 * We need to manufacture a button press event :-/ */
1769 bev
.type
= GDK_BUTTON_PRESS
;
1771 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1772 "button_press_event",
1775 assert(sc
->uc
->list
!= NULL
);
1777 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1778 GTK_DIR_TAB_FORWARD
);
1789 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1791 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1793 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1794 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1796 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1797 if (widget
== dp
->treeitems
[i
])
1799 if (i
< dp
->ntreeitems
) {
1800 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1807 if (i
< 0 || i
>= dp
->ntreeitems
)
1808 break; /* nothing in that dir to select */
1810 * Determine if this tree item is visible.
1813 GtkWidget
*w
= dp
->treeitems
[i
];
1815 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1816 if (!GTK_WIDGET_VISIBLE(w
)) {
1823 j
= i
; /* got one */
1829 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1832 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1833 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1839 * It's nice for Left and Right to expand and collapse tree
1842 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1843 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1845 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1848 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1849 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1851 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1858 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1859 int chr
, int action
, void *ptr
)
1861 GtkLabel
*label
= GTK_LABEL(labelw
);
1862 gchar
*currstr
, *pattern
;
1865 if (chr
== NO_SHORTCUT
)
1868 chr
= tolower((unsigned char)chr
);
1870 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1872 scs
->sc
[chr
].action
= action
;
1874 if (action
== SHORTCUT_FOCUS
) {
1875 scs
->sc
[chr
].uc
= NULL
;
1876 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1878 scs
->sc
[chr
].widget
= NULL
;
1879 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1882 gtk_label_get(label
, &currstr
);
1883 for (i
= 0; currstr
[i
]; i
++)
1884 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1887 pattern
= dupprintf("%*s_", i
, "");
1889 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1890 gtk_label_set_pattern(label
, pattern
);
1891 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1898 int do_config_box(const char *title
, Config
*cfg
)
1900 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1901 *tree
, *treescroll
, *panels
, *panelvbox
;
1902 int index
, level
, listitemheight
;
1903 struct controlbox
*ctrlbox
;
1905 GtkTreeItem
*treeitemlevels
[8];
1906 GtkTree
*treelevels
[8];
1909 struct Shortcuts scs
;
1911 struct selparam
*selparams
= NULL
;
1912 int nselparams
= 0, selparamsize
= 0;
1917 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1919 gtk_widget_size_request(listitem
, &req
);
1920 listitemheight
= req
.height
;
1921 gtk_widget_unref(listitem
);
1924 get_sesslist(&sl
, TRUE
);
1926 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1927 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
1930 ctrlbox
= ctrl_new_box();
1931 setup_config_box(ctrlbox
, &sl
, FALSE
, 0);
1932 unix_setup_config_box(ctrlbox
, FALSE
);
1934 window
= gtk_dialog_new();
1935 gtk_window_set_title(GTK_WINDOW(window
), title
);
1936 hbox
= gtk_hbox_new(FALSE
, 4);
1937 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
1938 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
1939 gtk_widget_show(hbox
);
1940 vbox
= gtk_vbox_new(FALSE
, 4);
1941 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
1942 gtk_widget_show(vbox
);
1943 cols
= columns_new(4);
1944 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
1945 gtk_widget_show(cols
);
1946 label
= gtk_label_new("Category:");
1947 columns_add(COLUMNS(cols
), label
, 0, 1);
1948 columns_force_left_align(COLUMNS(cols
), label
);
1949 gtk_widget_show(label
);
1950 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
1951 tree
= gtk_tree_new();
1952 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
1953 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1954 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
1955 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
1956 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
1957 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
1959 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
1961 GTK_POLICY_AUTOMATIC
);
1962 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
1963 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
1964 gtk_widget_show(tree
);
1965 gtk_widget_show(treescroll
);
1966 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
1967 panels
= panels_new();
1968 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
1969 gtk_widget_show(panels
);
1974 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
1975 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
1978 if (!*s
->pathname
) {
1979 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
1980 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1983 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
1984 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
1986 GtkWidget
*treeitem
;
1990 * We expect never to find an implicit path
1991 * component. For example, we expect never to see
1992 * A/B/C followed by A/D/E, because that would
1993 * _implicitly_ create A/D. All our path prefixes
1994 * are expected to contain actual controls and be
1995 * selectable in the treeview; so we would expect
1996 * to see A/D _explicitly_ before encountering
1999 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2001 c
= strrchr(s
->pathname
, '/');
2007 treeitem
= gtk_tree_item_new_with_label(c
);
2008 assert(j
-1 < level
);
2010 if (!treelevels
[j
-1]) {
2011 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2012 gtk_tree_item_set_subtree
2013 (treeitemlevels
[j
-1],
2014 GTK_WIDGET(treelevels
[j
-1]));
2015 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2017 gtk_tree_append(treelevels
[j
-1], treeitem
);
2019 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2021 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2022 treelevels
[j
] = NULL
;
2025 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2026 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2027 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2028 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2030 gtk_widget_show(treeitem
);
2034 first
= (panelvbox
== NULL
);
2036 panelvbox
= gtk_vbox_new(FALSE
, 4);
2037 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2039 panels_switch_to(PANELS(panels
), panelvbox
);
2040 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2043 if (nselparams
>= selparamsize
) {
2045 selparams
= sresize(selparams
, selparamsize
,
2048 selparams
[nselparams
].dp
= &dp
;
2049 selparams
[nselparams
].panels
= PANELS(panels
);
2050 selparams
[nselparams
].panel
= panelvbox
;
2051 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2052 selparams
[nselparams
].treeitem
= treeitem
;
2057 w
= layout_ctrls(&dp
,
2058 &selparams
[nselparams
-1].shortcuts
,
2059 s
, listitemheight
, NULL
);
2060 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2065 dp
.ntreeitems
= nselparams
;
2066 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2068 for (index
= 0; index
< nselparams
; index
++) {
2069 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2070 GTK_SIGNAL_FUNC(treeitem_sel
),
2072 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2076 dlg_refresh(NULL
, &dp
);
2078 dp
.shortcuts
= &selparams
[0].shortcuts
;
2079 dp
.currtreeitem
= dp
.treeitems
[0];
2080 dp
.lastfocus
= NULL
;
2084 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2085 gtk_widget_show(window
);
2088 * Set focus into the first available control.
2090 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2091 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2096 for (j
= 0; j
< s
->ncontrols
; j
++)
2097 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2098 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2099 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2100 dlg_set_focus(s
->ctrls
[j
], &dp
);
2101 dp
.lastfocus
= s
->ctrls
[j
];
2110 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2111 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2112 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2113 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2117 get_sesslist(&sl
, FALSE
);
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
);