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.
17 #include <gdk/gdkkeysyms.h>
20 #include <X11/Xutil.h>
26 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
40 struct Shortcut sc
[128];
47 int privdata_needs_free
;
48 GtkWidget
**buttons
; int nbuttons
; /* for radio buttons */
49 GtkWidget
*entry
; /* for editbox, combobox, filesel, fontsel */
50 GtkWidget
*button
; /* for filesel, fontsel */
51 GtkWidget
*list
; /* for combobox, listbox */
52 GtkWidget
*menu
; /* for optionmenu (==droplist) */
53 GtkWidget
*optmenu
; /* also for optionmenu */
54 GtkWidget
*text
; /* for text */
55 GtkAdjustment
*adj
; /* for the scrollbar in a list box */
60 tree234
*byctrl
, *bywidget
;
62 struct { unsigned char r
, g
, b
, ok
; } coloursel_result
; /* 0-255 */
63 /* `flags' are set to indicate when a GTK signal handler is being called
64 * due to automatic processing and should not flag a user event. */
66 struct Shortcuts
*shortcuts
;
67 GtkWidget
*window
, *cancelbutton
, *currtreeitem
, **treeitems
;
68 union control
*currfocus
, *lastfocus
;
72 #define FLAG_UPDATING_COMBO_LIST 1
74 enum { /* values for Shortcut.action */
75 SHORTCUT_EMPTY
, /* no shortcut on this key */
76 SHORTCUT_TREE
, /* focus a tree item */
77 SHORTCUT_FOCUS
, /* focus the supplied widget */
78 SHORTCUT_UCTRL
, /* do something sane with uctrl */
79 SHORTCUT_UCTRL_UP
, /* uctrl is a draglist, move Up */
80 SHORTCUT_UCTRL_DOWN
, /* uctrl is a draglist, move Down */
86 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
88 static void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
89 int chr
, int action
, void *ptr
);
90 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
92 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
94 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
96 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
97 static void coloursel_ok(GtkButton
*button
, gpointer data
);
98 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
99 static void window_destroy(GtkWidget
*widget
, gpointer data
);
101 static int uctrl_cmp_byctrl(void *av
, void *bv
)
103 struct uctrl
*a
= (struct uctrl
*)av
;
104 struct uctrl
*b
= (struct uctrl
*)bv
;
105 if (a
->ctrl
< b
->ctrl
)
107 else if (a
->ctrl
> b
->ctrl
)
112 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
114 union control
*a
= (union control
*)av
;
115 struct uctrl
*b
= (struct uctrl
*)bv
;
118 else if (a
> b
->ctrl
)
123 static int uctrl_cmp_bywidget(void *av
, void *bv
)
125 struct uctrl
*a
= (struct uctrl
*)av
;
126 struct uctrl
*b
= (struct uctrl
*)bv
;
127 if (a
->toplevel
< b
->toplevel
)
129 else if (a
->toplevel
> b
->toplevel
)
134 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
136 GtkWidget
*a
= (GtkWidget
*)av
;
137 struct uctrl
*b
= (struct uctrl
*)bv
;
140 else if (a
> b
->toplevel
)
145 static void dlg_init(struct dlgparam
*dp
)
147 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
148 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
149 dp
->coloursel_result
.ok
= FALSE
;
152 static void dlg_cleanup(struct dlgparam
*dp
)
156 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
157 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
158 del234(dp
->bywidget
, uc
);
159 if (uc
->privdata_needs_free
)
164 freetree234(dp
->bywidget
);
165 sfree(dp
->treeitems
);
168 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
170 add234(dp
->byctrl
, uc
);
171 add234(dp
->bywidget
, uc
);
174 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
176 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
179 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
181 struct uctrl
*ret
= NULL
;
183 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
191 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
193 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
194 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
198 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
200 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
201 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
203 uc
->privdata_needs_free
= FALSE
;
206 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
208 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
209 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
210 uc
->privdata
= smalloc(size
);
211 uc
->privdata_needs_free
= FALSE
;
215 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
217 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
218 if (dp
->currfocus
!= ctrl
)
219 return dp
->currfocus
;
221 return dp
->lastfocus
;
224 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
226 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
227 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
228 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
229 assert(uc
->buttons
!= NULL
);
230 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
233 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
235 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
236 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
239 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
240 assert(uc
->buttons
!= NULL
);
241 for (i
= 0; i
< uc
->nbuttons
; i
++)
242 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
244 return 0; /* got to return something */
247 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
249 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
250 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
251 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
252 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
255 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
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 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
263 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
265 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
266 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
267 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
268 assert(uc
->entry
!= NULL
);
269 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
272 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
274 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
275 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
276 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
277 assert(uc
->entry
!= NULL
);
278 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
280 buffer
[length
-1] = '\0';
283 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
285 GtkContainer
*cont
= GTK_CONTAINER(data
);
286 /* gtk_container_remove will unref the widget for us; we need not. */
287 gtk_container_remove(cont
, w
);
290 /* The `listbox' functions can also apply to combo boxes. */
291 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
293 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
294 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
297 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
298 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
299 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
301 cont
= (uc
->menu ?
GTK_CONTAINER(uc
->menu
) : GTK_CONTAINER(uc
->list
));
303 gtk_container_foreach(cont
, container_remove_and_destroy
, cont
);
306 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
308 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
309 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
311 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
312 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
313 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
317 (GTK_CONTAINER(uc
->menu
),
318 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
320 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
324 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
326 dlg_listbox_addwithindex(ctrl
, dlg
, text
, 0);
330 * Each listbox entry may have a numeric id associated with it.
331 * Note that some front ends only permit a string to be stored at
332 * each position, which means that _if_ you put two identical
333 * strings in any listbox then you MUST not assign them different
334 * IDs and expect to get meaningful results back.
336 void dlg_listbox_addwithindex(union control
*ctrl
, void *dlg
,
337 char const *text
, int id
)
339 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
340 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
342 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
343 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
344 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
346 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
350 * List item in a drop-down (but non-combo) list. Tabs are
351 * ignored; we just provide a standard menu item with the
354 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
356 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
357 gtk_widget_show(menuitem
);
359 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", (gpointer
)id
);
360 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
361 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
362 } else if (!uc
->entry
) {
364 * List item in a non-combo-box list box. We make all of
365 * these Columns containing GtkLabels. This allows us to do
366 * the nasty force_left hack irrespective of whether there
367 * are tabs in the thing.
369 GtkWidget
*listitem
= gtk_list_item_new();
370 GtkWidget
*cols
= columns_new(10);
374 /* Count the tabs in the text, and hence determine # of columns. */
376 for (i
= 0; text
[i
]; i
++)
381 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
382 percents
= smalloc(ncols
* sizeof(gint
));
383 percents
[ncols
-1] = 100;
384 for (i
= 0; i
< ncols
-1; i
++) {
385 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
386 percents
[ncols
-1] -= percents
[i
];
388 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
391 for (i
= 0; i
< ncols
; i
++) {
392 int len
= strcspn(text
, "\t");
393 char *dup
= dupprintf("%.*s", len
, text
);
398 label
= gtk_label_new(dup
);
401 columns_add(COLUMNS(cols
), label
, i
, 1);
402 columns_force_left_align(COLUMNS(cols
), label
);
403 gtk_widget_show(label
);
405 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
406 gtk_widget_show(cols
);
407 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
408 gtk_widget_show(listitem
);
410 if (ctrl
->listbox
.multisel
) {
411 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
412 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
414 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
415 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
417 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
418 GTK_SIGNAL_FUNC(widget_focus
), dp
);
419 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
420 GTK_SIGNAL_FUNC(listitem_button
), dp
);
421 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
424 * List item in a combo-box list, which means the sensible
425 * thing to do is make it a perfectly normal label. Hence
426 * tabs are disregarded.
428 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
430 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
431 gtk_widget_show(listitem
);
433 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
436 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
439 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
441 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
442 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
446 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
447 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
448 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
450 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
452 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
453 g_list_free(children
);
455 return (int)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
458 /* dlg_listbox_index returns <0 if no single element is selected. */
459 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
461 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
462 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
464 GtkWidget
*item
, *activeitem
;
468 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
469 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
470 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
473 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
475 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
477 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
478 i
++, children
= children
->next
) {
479 if (uc
->menu ? activeitem
== item
:
480 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
487 g_list_free(children
);
488 return selected
< 0 ?
-1 : selected
;
491 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
493 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
494 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
496 GtkWidget
*item
, *activeitem
;
498 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
499 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
500 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
502 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
504 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
505 g_list_free(children
);
508 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
509 return item
== activeitem
;
511 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
515 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
517 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
518 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
520 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
521 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
522 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
525 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
527 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
531 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
533 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
534 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
536 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
537 assert(uc
->text
!= NULL
);
539 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
542 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
544 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
545 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
546 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
547 assert(uc
->entry
!= NULL
);
548 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
551 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
553 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
554 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
555 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
556 assert(uc
->entry
!= NULL
);
557 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
559 fn
->path
[lenof(fn
->path
)-1] = '\0';
562 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
564 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
565 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
566 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
567 assert(uc
->entry
!= NULL
);
568 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
571 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
573 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
574 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
575 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
576 assert(uc
->entry
!= NULL
);
577 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
579 fs
->name
[lenof(fs
->name
)-1] = '\0';
583 * Bracketing a large set of updates in these two functions will
584 * cause the front end (if possible) to delay updating the screen
585 * until it's all complete, thus avoiding flicker.
587 void dlg_update_start(union control
*ctrl
, void *dlg
)
590 * Apparently we can't do this at all in GTK. GtkCList supports
591 * freeze and thaw, but not GtkList. Bah.
595 void dlg_update_done(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_set_focus(union control
*ctrl
, void *dlg
)
605 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
606 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
608 switch (ctrl
->generic
.type
) {
611 /* Check boxes and buttons get the focus _and_ get toggled. */
612 gtk_widget_grab_focus(uc
->toplevel
);
614 case CTRL_FILESELECT
:
615 case CTRL_FONTSELECT
:
617 /* Anything containing an edit box gets that focused. */
618 gtk_widget_grab_focus(uc
->entry
);
622 * Radio buttons: we find the currently selected button and
627 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
628 if (gtk_toggle_button_get_active
629 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
630 gtk_widget_grab_focus(uc
->buttons
[i
]);
636 * If the list is really an option menu, we focus it.
637 * Otherwise we tell it to focus one of its children, which
638 * appears to do the Right Thing.
641 gtk_widget_grab_focus(uc
->optmenu
);
643 assert(uc
->list
!= NULL
);
644 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
651 * During event processing, you might well want to give an error
652 * indication to the user. dlg_beep() is a quick and easy generic
653 * error; dlg_error() puts up a message-box or equivalent.
655 void dlg_beep(void *dlg
)
660 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
662 gtk_widget_destroy(GTK_WIDGET(data
));
665 void dlg_error_msg(void *dlg
, char *msg
)
667 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
668 GtkWidget
*window
, *hbox
, *text
, *ok
;
670 window
= gtk_dialog_new();
671 text
= gtk_label_new(msg
);
672 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
673 hbox
= gtk_hbox_new(FALSE
, 0);
674 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
675 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
676 hbox
, FALSE
, FALSE
, 20);
677 gtk_widget_show(text
);
678 gtk_widget_show(hbox
);
679 gtk_window_set_title(GTK_WINDOW(window
), "Error");
680 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
681 ok
= gtk_button_new_with_label("OK");
682 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
683 ok
, FALSE
, FALSE
, 0);
685 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
686 gtk_window_set_default(GTK_WINDOW(window
), ok
);
687 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
688 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
689 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
690 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
691 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
692 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
694 gint x
, y
, w
, h
, dx
, dy
;
695 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_NONE
);
696 gdk_window_get_origin(GTK_WIDGET(dp
->window
)->window
, &x
, &y
);
697 gdk_window_get_size(GTK_WIDGET(dp
->window
)->window
, &w
, &h
);
700 gtk_widget_set_uposition(GTK_WIDGET(window
), dx
, dy
);
702 gtk_widget_show(window
);
707 * This function signals to the front end that the dialog's
708 * processing is completed, and passes an integer value (typically
711 void dlg_end(void *dlg
, int value
)
713 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
718 void dlg_refresh(union control
*ctrl
, void *dlg
)
720 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
724 if (ctrl
->generic
.handler
!= NULL
)
725 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
729 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
730 assert(uc
->ctrl
!= NULL
);
731 if (uc
->ctrl
->generic
.handler
!= NULL
)
732 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
733 dp
->data
, EVENT_REFRESH
);
738 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
740 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
741 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
744 GtkWidget
*coloursel
=
745 gtk_color_selection_dialog_new("Select a colour");
746 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
748 dp
->coloursel_result
.ok
= FALSE
;
750 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
751 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
752 cvals
[0] = r
/ 255.0;
753 cvals
[1] = g
/ 255.0;
754 cvals
[2] = b
/ 255.0;
755 cvals
[3] = 1.0; /* fully opaque! */
756 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
758 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
759 (gpointer
)coloursel
);
760 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
761 (gpointer
)coloursel
);
762 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
763 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
764 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
765 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
766 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
767 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
768 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
769 (gpointer
)coloursel
);
770 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
771 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
772 (gpointer
)coloursel
);
773 gtk_widget_show(coloursel
);
776 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
777 int *r
, int *g
, int *b
)
779 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
780 if (dp
->coloursel_result
.ok
) {
781 *r
= dp
->coloursel_result
.r
;
782 *g
= dp
->coloursel_result
.g
;
783 *b
= dp
->coloursel_result
.b
;
789 /* ----------------------------------------------------------------------
790 * Signal handlers while the dialog box is active.
793 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
796 struct dlgparam
*dp
= (struct dlgparam
*)data
;
797 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
798 union control
*focus
;
805 if (focus
!= dp
->currfocus
) {
806 dp
->lastfocus
= dp
->currfocus
;
807 dp
->currfocus
= focus
;
813 static void button_clicked(GtkButton
*button
, gpointer data
)
815 struct dlgparam
*dp
= (struct dlgparam
*)data
;
816 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
817 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
820 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
822 struct dlgparam
*dp
= (struct dlgparam
*)data
;
823 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
824 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
827 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
830 * GtkEntry has a nasty habit of eating the Return key, which
831 * is unhelpful since it doesn't actually _do_ anything with it
832 * (it calls gtk_widget_activate, but our edit boxes never need
833 * activating). So I catch Return before GtkEntry sees it, and
834 * pass it straight on to the parent widget. Effect: hitting
835 * Return in an edit box will now activate the default button
836 * in the dialog just like it will everywhere else.
838 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
840 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
841 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
848 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
850 struct dlgparam
*dp
= (struct dlgparam
*)data
;
851 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
852 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
853 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
857 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
860 struct dlgparam
*dp
= (struct dlgparam
*)data
;
861 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
862 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
865 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
868 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
870 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
871 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
872 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
873 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
875 * Up, Down, PgUp or PgDn have been pressed on a ListItem
876 * in a list box. So, if the list box is single-selection:
878 * - if the list item in question isn't already selected,
879 * we simply select it.
880 * - otherwise, we find the next one (or next
881 * however-far-away) in whichever direction we're going,
883 * + in this case, we must also fiddle with the
884 * scrollbar to ensure the newly selected item is
887 * If it's multiple-selection, we do all of the above
888 * except actually selecting anything, so we move the focus
889 * and fiddle the scrollbar to follow it.
891 GtkWidget
*list
= item
->parent
;
893 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
896 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
897 gtk_list_select_child(GTK_LIST(list
), item
);
900 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
901 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
904 (event
->keyval
==GDK_Page_Down
||
905 event
->keyval
==GDK_KP_Page_Down
||
906 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
910 GList
*children
, *chead
;
912 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
914 n
= g_list_length(children
);
918 * Figure out how many list items to a screenful,
919 * and adjust the step appropriately.
921 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
922 step
--; /* go by one less than that */
926 while (children
!= NULL
) {
927 if (item
== children
->data
)
929 children
= children
->next
;
934 if (direction
< 0 && i
> 0)
935 children
= children
->prev
, i
--;
936 else if (direction
> 0 && i
< n
-1)
937 children
= children
->next
, i
++;
941 if (children
&& children
->data
) {
943 gtk_list_select_child(GTK_LIST(list
),
944 GTK_WIDGET(children
->data
));
945 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
946 gtk_adjustment_clamp_page
948 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
949 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
960 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
963 listitem_key(item
, event
, data
, FALSE
);
966 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
969 listitem_key(item
, event
, data
, TRUE
);
972 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
975 struct dlgparam
*dp
= (struct dlgparam
*)data
;
976 if (event
->type
== GDK_2BUTTON_PRESS
||
977 event
->type
== GDK_3BUTTON_PRESS
) {
978 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
979 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
985 static void list_selchange(GtkList
*list
, gpointer data
)
987 struct dlgparam
*dp
= (struct dlgparam
*)data
;
988 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
989 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
992 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
994 struct dlgparam
*dp
= (struct dlgparam
*)data
;
995 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
996 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
997 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
998 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1001 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1003 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1004 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1008 (index
== 0 && direction
< 0) ||
1009 (index
== g_list_length(children
)-1 && direction
> 0)) {
1014 child
= g_list_nth_data(children
, index
);
1015 gtk_widget_ref(child
);
1016 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1017 g_list_free(children
);
1020 children
= g_list_append(children
, child
);
1021 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1022 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1023 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1026 static void draglist_up(GtkButton
*button
, gpointer data
)
1028 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1029 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1030 draglist_move(dp
, uc
, -1);
1033 static void draglist_down(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 filesel_ok(GtkButton
*button
, gpointer data
)
1042 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1043 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1044 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1045 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1046 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1049 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1051 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1052 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1053 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1054 char *name
= gtk_font_selection_dialog_get_font_name
1055 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1056 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1059 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1061 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1062 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1063 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1065 gtk_color_selection_get_color
1066 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1068 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1069 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1070 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1071 dp
->coloursel_result
.ok
= TRUE
;
1072 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1075 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1077 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1078 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1079 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1080 dp
->coloursel_result
.ok
= FALSE
;
1081 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1084 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1086 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1087 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1089 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1090 GtkWidget
*filesel
=
1091 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1092 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1094 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1096 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1098 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1099 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1100 gtk_signal_connect_object
1101 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1102 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1103 gtk_signal_connect_object
1104 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1105 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1106 gtk_widget_show(filesel
);
1109 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1110 gchar
*spacings
[] = { "c", "m", NULL
};
1111 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1112 GtkWidget
*fontsel
=
1113 gtk_font_selection_dialog_new("Select a font");
1114 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1115 gtk_font_selection_dialog_set_filter
1116 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1117 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1118 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1119 if (!gtk_font_selection_dialog_set_font_name
1120 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1122 * If the font name wasn't found as it was, try opening
1123 * it and extracting its FONT property. This should
1124 * have the effect of mapping short aliases into true
1127 GdkFont
*font
= gdk_font_load(fontname
);
1129 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1130 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1131 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1133 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1134 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1136 gtk_font_selection_dialog_set_font_name
1137 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1139 gdk_font_unref(font
);
1143 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1144 "user-data", (gpointer
)fontsel
);
1145 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1147 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1148 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1149 gtk_signal_connect_object
1150 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1151 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1153 gtk_signal_connect_object
1154 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1155 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1157 gtk_widget_show(fontsel
);
1161 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1164 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1165 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1167 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1168 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1169 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1172 /* ----------------------------------------------------------------------
1173 * This function does the main layout work: it reads a controlset,
1174 * it creates the relevant GTK controls, and returns a GtkWidget
1175 * containing the result. (This widget might be a title of some
1176 * sort, it might be a Columns containing many controls, or it
1177 * might be a GtkFrame containing a Columns; whatever it is, it's
1178 * definitely a GtkWidget and should probably be added to a
1181 * `listitemheight' is used to calculate a usize for list boxes: it
1182 * should be the height from the size request of a GtkListItem.
1184 * `win' is required for setting the default button. If it is
1185 * non-NULL, all buttons created will be default-capable (so they
1186 * have extra space round them for the default highlight).
1188 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1189 struct controlset
*s
, int listitemheight
,
1196 if (!s
->boxname
&& s
->boxtitle
) {
1197 /* This controlset is a panel title. */
1198 return gtk_label_new(s
->boxtitle
);
1202 * Otherwise, we expect to be laying out actual controls, so
1203 * we'll start by creating a Columns for the purpose.
1205 cols
= COLUMNS(columns_new(4));
1206 ret
= GTK_WIDGET(cols
);
1207 gtk_widget_show(ret
);
1210 * Create a containing frame if we have a box name.
1213 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1214 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1215 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1216 gtk_widget_show(ret
);
1220 * Now iterate through the controls themselves, create them,
1221 * and add them to the Columns.
1223 for (i
= 0; i
< s
->ncontrols
; i
++) {
1224 union control
*ctrl
= s
->ctrls
[i
];
1227 GtkWidget
*w
= NULL
;
1229 switch (ctrl
->generic
.type
) {
1232 static const int simplecols
[1] = { 100 };
1233 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1234 (ctrl
->columns
.percentages ?
1235 ctrl
->columns
.percentages
: simplecols
));
1237 continue; /* no actual control created */
1240 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1242 columns_taborder_last(cols
, uc
->toplevel
);
1244 continue; /* no actual control created */
1247 uc
= smalloc(sizeof(struct uctrl
));
1249 uc
->privdata
= NULL
;
1250 uc
->privdata_needs_free
= FALSE
;
1252 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1253 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1255 switch (ctrl
->generic
.type
) {
1257 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1259 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1260 if (ctrl
->button
.isdefault
)
1261 gtk_window_set_default(win
, w
);
1262 if (ctrl
->button
.iscancel
)
1263 dp
->cancelbutton
= w
;
1265 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1266 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1267 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1268 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1269 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1270 SHORTCUT_UCTRL
, uc
);
1273 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1274 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1275 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1276 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1277 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1278 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1279 SHORTCUT_UCTRL
, uc
);
1284 * Radio buttons get to go inside their own Columns, no
1288 gint i
, *percentages
;
1292 if (ctrl
->generic
.label
) {
1293 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1294 columns_add(COLUMNS(w
), label
, 0, 1);
1295 columns_force_left_align(COLUMNS(w
), label
);
1296 gtk_widget_show(label
);
1297 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1298 SHORTCUT_UCTRL
, uc
);
1300 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1301 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1303 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1304 100 * i
/ ctrl
->radio
.ncolumns
);
1306 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1308 g_free(percentages
);
1311 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1312 uc
->buttons
= smalloc(uc
->nbuttons
* sizeof(GtkWidget
*));
1314 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1318 b
= (gtk_radio_button_new_with_label
1319 (group
, ctrl
->radio
.buttons
[i
]));
1321 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1322 colstart
= i
% ctrl
->radio
.ncolumns
;
1323 columns_add(COLUMNS(w
), b
, colstart
,
1324 (i
== ctrl
->radio
.nbuttons
-1 ?
1325 ctrl
->radio
.ncolumns
- colstart
: 1));
1326 columns_force_left_align(COLUMNS(w
), b
);
1328 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1329 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1330 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1331 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1332 if (ctrl
->radio
.shortcuts
) {
1333 shortcut_add(scs
, GTK_BIN(b
)->child
,
1334 ctrl
->radio
.shortcuts
[i
],
1335 SHORTCUT_UCTRL
, uc
);
1344 if (ctrl
->editbox
.has_list
) {
1345 w
= gtk_combo_new();
1346 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1347 uc
->entry
= GTK_COMBO(w
)->entry
;
1348 uc
->list
= GTK_COMBO(w
)->list
;
1350 w
= gtk_entry_new();
1351 if (ctrl
->editbox
.password
)
1352 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1355 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1356 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1357 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1358 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1359 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1360 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1362 * Edit boxes, for some strange reason, have a minimum
1363 * width of 150 in GTK 1.2. We don't want this - we'd
1364 * rather the edit boxes acquired their natural width
1365 * from the column layout of the rest of the box.
1367 * Also, while we're here, we'll squirrel away the
1368 * edit box height so we can use that to centre its
1369 * label vertically beside it.
1371 gtk_widget_size_request(w
, &req
);
1372 gtk_widget_set_usize(w
, 10, req
.height
);
1374 if (ctrl
->generic
.label
) {
1375 GtkWidget
*label
, *container
;
1377 label
= gtk_label_new(ctrl
->generic
.label
);
1379 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1380 SHORTCUT_FOCUS
, uc
->entry
);
1382 container
= columns_new(4);
1383 if (ctrl
->editbox
.percentwidth
== 100) {
1384 columns_add(COLUMNS(container
), label
, 0, 1);
1385 columns_force_left_align(COLUMNS(container
), label
);
1386 columns_add(COLUMNS(container
), w
, 0, 1);
1388 gint percentages
[2];
1389 percentages
[1] = ctrl
->editbox
.percentwidth
;
1390 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1391 columns_set_cols(COLUMNS(container
), 2, percentages
);
1392 columns_add(COLUMNS(container
), label
, 0, 1);
1393 columns_force_left_align(COLUMNS(container
), label
);
1394 columns_add(COLUMNS(container
), w
, 1, 1);
1395 /* Centre the label vertically. */
1396 gtk_widget_set_usize(label
, -1, req
.height
);
1397 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1399 gtk_widget_show(label
);
1404 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1405 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1408 case CTRL_FILESELECT
:
1409 case CTRL_FONTSELECT
:
1414 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1415 "Browse..." : "Change...");
1417 gint percentages
[] = { 75, 25 };
1419 columns_set_cols(COLUMNS(w
), 2, percentages
);
1421 if (ctrl
->generic
.label
) {
1422 ww
= gtk_label_new(ctrl
->generic
.label
);
1423 columns_add(COLUMNS(w
), ww
, 0, 2);
1424 columns_force_left_align(COLUMNS(w
), ww
);
1425 gtk_widget_show(ww
);
1426 shortcut_add(scs
, ww
,
1427 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1428 ctrl
->fileselect
.shortcut
:
1429 ctrl
->fontselect
.shortcut
),
1430 SHORTCUT_UCTRL
, uc
);
1433 uc
->entry
= ww
= gtk_entry_new();
1434 gtk_widget_size_request(ww
, &req
);
1435 gtk_widget_set_usize(ww
, 10, req
.height
);
1436 columns_add(COLUMNS(w
), ww
, 0, 1);
1437 gtk_widget_show(ww
);
1439 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1440 columns_add(COLUMNS(w
), ww
, 1, 1);
1441 gtk_widget_show(ww
);
1443 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1444 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1445 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1446 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1447 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1448 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1449 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1450 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1451 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1452 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1456 if (ctrl
->listbox
.height
== 0) {
1457 uc
->optmenu
= w
= gtk_option_menu_new();
1458 uc
->menu
= gtk_menu_new();
1459 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1460 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1461 (gpointer
)uc
->optmenu
);
1462 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1463 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1465 uc
->list
= gtk_list_new();
1466 if (ctrl
->listbox
.multisel
) {
1467 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1468 GTK_SELECTION_MULTIPLE
);
1470 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1471 GTK_SELECTION_SINGLE
);
1473 w
= gtk_scrolled_window_new(NULL
, NULL
);
1474 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1476 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1478 GTK_POLICY_AUTOMATIC
);
1479 uc
->adj
= gtk_scrolled_window_get_vadjustment
1480 (GTK_SCROLLED_WINDOW(w
));
1482 gtk_widget_show(uc
->list
);
1483 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1484 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1485 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1486 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1489 * Adjust the height of the scrolled window to the
1490 * minimum given by the height parameter.
1492 * This piece of guesswork is a horrid hack based
1493 * on looking inside the GTK 1.2 sources
1494 * (specifically gtkviewport.c, which appears to be
1495 * the widget which provides the border around the
1496 * scrolling area). Anyone lets me know how I can
1497 * do this in a way which isn't at risk from GTK
1498 * upgrades, I'd be grateful.
1501 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1502 gtk_widget_set_usize(w
, 10,
1503 2*edge
+ (ctrl
->listbox
.height
*
1507 if (ctrl
->listbox
.draglist
) {
1509 * GTK doesn't appear to make it easy to
1510 * implement a proper draggable list; so
1511 * instead I'm just going to have to put an Up
1512 * and a Down button to the right of the actual
1513 * list box. Ah well.
1515 GtkWidget
*cols
, *button
;
1516 static const gint percentages
[2] = { 80, 20 };
1518 cols
= columns_new(4);
1519 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1520 columns_add(COLUMNS(cols
), w
, 0, 1);
1522 button
= gtk_button_new_with_label("Up");
1523 columns_add(COLUMNS(cols
), button
, 1, 1);
1524 gtk_widget_show(button
);
1525 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1526 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1527 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1528 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1529 button
= gtk_button_new_with_label("Down");
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_down
), dp
);
1534 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1535 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1541 if (ctrl
->generic
.label
) {
1542 GtkWidget
*label
, *container
;
1544 label
= gtk_label_new(ctrl
->generic
.label
);
1546 container
= columns_new(4);
1547 if (ctrl
->listbox
.percentwidth
== 100) {
1548 columns_add(COLUMNS(container
), label
, 0, 1);
1549 columns_force_left_align(COLUMNS(container
), label
);
1550 columns_add(COLUMNS(container
), w
, 0, 1);
1552 gint percentages
[2];
1553 percentages
[1] = ctrl
->listbox
.percentwidth
;
1554 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1555 columns_set_cols(COLUMNS(container
), 2, percentages
);
1556 columns_add(COLUMNS(container
), label
, 0, 1);
1557 columns_force_left_align(COLUMNS(container
), label
);
1558 columns_add(COLUMNS(container
), w
, 1, 1);
1560 gtk_widget_show(label
);
1562 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1563 SHORTCUT_UCTRL
, uc
);
1569 * Wrapping text widgets don't sit well with the GTK
1570 * layout model, in which widgets state a minimum size
1571 * and the whole window then adjusts to the smallest
1572 * size it can sensibly take given its contents. A
1573 * wrapping text widget _has_ no clear minimum size;
1574 * instead it has a range of possibilities. It can be
1575 * one line deep but 2000 wide, or two lines deep and
1576 * 1000 pixels, or three by 867, or four by 500 and so
1577 * on. It can be as short as you like provided you
1578 * don't mind it being wide, or as narrow as you like
1579 * provided you don't mind it being tall.
1581 * Therefore, it fits very badly into the layout model.
1582 * Hence the only thing to do is pick a width and let
1583 * it choose its own number of lines. To do this I'm
1584 * going to cheat a little. All new wrapping text
1585 * widgets will be created with a minimal text content
1586 * "X"; then, after the rest of the dialog box is set
1587 * up and its size calculated, the text widgets will be
1588 * told their width and given their real text, which
1589 * will cause the size to be recomputed in the y
1590 * direction (because many of them will expand to more
1593 uc
->text
= w
= gtk_label_new("X");
1594 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1595 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1597 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1598 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1604 columns_add(cols
, w
,
1605 COLUMN_START(ctrl
->generic
.column
),
1606 COLUMN_SPAN(ctrl
->generic
.column
));
1608 columns_force_left_align(cols
, w
);
1612 dlg_add_uctrl(dp
, uc
);
1619 struct dlgparam
*dp
;
1621 GtkWidget
*panel
, *treeitem
;
1622 struct Shortcuts shortcuts
;
1625 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1627 struct selparam
*sp
= (struct selparam
*)data
;
1629 panels_switch_to(sp
->panels
, sp
->panel
);
1631 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1632 sp
->dp
->currtreeitem
= sp
->treeitem
;
1635 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1640 static int tree_grab_focus(struct dlgparam
*dp
)
1645 * See if any of the treeitems has the focus.
1648 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1649 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1657 gtk_widget_grab_focus(dp
->currtreeitem
);
1662 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1665 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1667 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1669 * If there's a focused treeitem, we return FALSE to cause the
1670 * focus to move on to some totally other control. If not, we
1671 * focus the selected one.
1673 return tree_grab_focus(dp
);
1676 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1678 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1680 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1681 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1685 if ((event
->state
& GDK_MOD1_MASK
) &&
1686 (unsigned char)event
->string
[0] > 0 &&
1687 (unsigned char)event
->string
[0] <= 127) {
1688 int schr
= (unsigned char)event
->string
[0];
1689 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1691 switch (sc
->action
) {
1693 tree_grab_focus(dp
);
1695 case SHORTCUT_FOCUS
:
1696 gtk_widget_grab_focus(sc
->widget
);
1698 case SHORTCUT_UCTRL
:
1700 * We must do something sensible with a uctrl.
1701 * Precisely what this is depends on the type of
1704 switch (sc
->uc
->ctrl
->generic
.type
) {
1707 /* Check boxes and buttons get the focus _and_ get toggled. */
1708 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1709 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1712 case CTRL_FILESELECT
:
1713 case CTRL_FONTSELECT
:
1714 /* File/font selectors have their buttons pressed (ooer),
1715 * and focus transferred to the edit box. */
1716 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1718 gtk_widget_grab_focus(sc
->uc
->entry
);
1722 * Radio buttons are fun, because they have
1723 * multiple shortcuts. We must find whether the
1724 * activated shortcut is the shortcut for the whole
1725 * group, or for a particular button. In the former
1726 * case, we find the currently selected button and
1727 * focus it; in the latter, we focus-and-click the
1728 * button whose shortcut was pressed.
1730 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1732 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1733 if (gtk_toggle_button_get_active
1734 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1735 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1737 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1739 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1740 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1741 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1742 gtk_signal_emit_by_name
1743 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1749 * If the list is really an option menu, we focus
1750 * and click it. Otherwise we tell it to focus one
1751 * of its children, which appears to do the Right
1754 if (sc
->uc
->optmenu
) {
1758 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1759 /* Option menus don't work using the "clicked" signal.
1760 * We need to manufacture a button press event :-/ */
1761 bev
.type
= GDK_BUTTON_PRESS
;
1763 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1764 "button_press_event",
1767 assert(sc
->uc
->list
!= NULL
);
1769 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1770 GTK_DIR_TAB_FORWARD
);
1781 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1783 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1785 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1786 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1788 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1789 if (widget
== dp
->treeitems
[i
])
1791 if (i
< dp
->ntreeitems
) {
1792 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1799 if (i
< 0 || i
>= dp
->ntreeitems
)
1800 break; /* nothing in that dir to select */
1802 * Determine if this tree item is visible.
1805 GtkWidget
*w
= dp
->treeitems
[i
];
1807 while (w
&& GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
)) {
1808 if (!GTK_WIDGET_VISIBLE(w
)) {
1815 j
= i
; /* got one */
1821 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1825 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1826 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1832 * It's nice for Left and Right to expand and collapse tree
1835 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1836 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1838 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1841 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1842 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1844 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1851 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1852 int chr
, int action
, void *ptr
)
1854 GtkLabel
*label
= GTK_LABEL(labelw
);
1855 gchar
*currstr
, *pattern
;
1858 if (chr
== NO_SHORTCUT
)
1861 chr
= tolower((unsigned char)chr
);
1863 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1865 scs
->sc
[chr
].action
= action
;
1867 if (action
== SHORTCUT_FOCUS
) {
1868 scs
->sc
[chr
].uc
= NULL
;
1869 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1871 scs
->sc
[chr
].widget
= NULL
;
1872 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1875 gtk_label_get(label
, &currstr
);
1876 for (i
= 0; currstr
[i
]; i
++)
1877 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1880 pattern
= dupprintf("%*s_", i
, "");
1882 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1883 gtk_label_set_pattern(label
, pattern
);
1884 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1891 int do_config_box(const char *title
)
1893 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1894 *tree
, *treescroll
, *panels
, *panelvbox
;
1895 int index
, level
, listitemheight
;
1896 struct controlbox
*ctrlbox
;
1898 GtkTreeItem
*treeitemlevels
[8];
1899 GtkTree
*treelevels
[8];
1903 struct Shortcuts scs
;
1905 struct selparam
*selparams
= NULL
;
1906 int nselparams
= 0, selparamsize
= 0;
1908 do_defaults(NULL
, &cfg
);
1913 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1915 gtk_widget_size_request(listitem
, &req
);
1916 listitemheight
= req
.height
;
1917 gtk_widget_unref(listitem
);
1922 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1923 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
1926 ctrlbox
= ctrl_new_box();
1927 setup_config_box(ctrlbox
, &sl
, FALSE
, 0);
1928 unix_setup_config_box(ctrlbox
, FALSE
);
1930 window
= gtk_dialog_new();
1931 gtk_window_set_title(GTK_WINDOW(window
), title
);
1932 hbox
= gtk_hbox_new(FALSE
, 4);
1933 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
1934 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
1935 gtk_widget_show(hbox
);
1936 vbox
= gtk_vbox_new(FALSE
, 4);
1937 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
1938 gtk_widget_show(vbox
);
1939 cols
= columns_new(4);
1940 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
1941 gtk_widget_show(cols
);
1942 label
= gtk_label_new("Category:");
1943 columns_add(COLUMNS(cols
), label
, 0, 1);
1944 columns_force_left_align(COLUMNS(cols
), label
);
1945 gtk_widget_show(label
);
1946 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
1947 tree
= gtk_tree_new();
1948 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
1949 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1950 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
1951 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
1952 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
1953 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
1955 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
1957 GTK_POLICY_AUTOMATIC
);
1958 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
1959 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
1960 gtk_widget_show(tree
);
1961 gtk_widget_show(treescroll
);
1962 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
1963 panels
= panels_new();
1964 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
1965 gtk_widget_show(panels
);
1970 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
1971 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
1974 if (!*s
->pathname
) {
1975 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
1976 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1979 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
1980 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
1982 GtkWidget
*treeitem
;
1986 * We expect never to find an implicit path
1987 * component. For example, we expect never to see
1988 * A/B/C followed by A/D/E, because that would
1989 * _implicitly_ create A/D. All our path prefixes
1990 * are expected to contain actual controls and be
1991 * selectable in the treeview; so we would expect
1992 * to see A/D _explicitly_ before encountering
1995 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
1997 c
= strrchr(s
->pathname
, '/');
2003 treeitem
= gtk_tree_item_new_with_label(c
);
2004 assert(j
-1 < level
);
2006 if (!treelevels
[j
-1]) {
2007 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2008 gtk_tree_item_set_subtree
2009 (treeitemlevels
[j
-1],
2010 GTK_WIDGET(treelevels
[j
-1]));
2011 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2013 gtk_tree_append(treelevels
[j
-1], treeitem
);
2015 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2017 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2018 treelevels
[j
] = NULL
;
2021 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2022 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2023 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2024 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2026 gtk_widget_show(treeitem
);
2030 first
= (panelvbox
== NULL
);
2032 panelvbox
= gtk_vbox_new(FALSE
, 4);
2033 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2035 panels_switch_to(PANELS(panels
), panelvbox
);
2036 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2039 if (nselparams
>= selparamsize
) {
2041 selparams
= srealloc(selparams
,
2042 selparamsize
* sizeof(*selparams
));
2044 selparams
[nselparams
].dp
= &dp
;
2045 selparams
[nselparams
].panels
= PANELS(panels
);
2046 selparams
[nselparams
].panel
= panelvbox
;
2047 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2048 selparams
[nselparams
].treeitem
= treeitem
;
2053 w
= layout_ctrls(&dp
,
2054 &selparams
[nselparams
-1].shortcuts
,
2055 s
, listitemheight
, NULL
);
2056 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2061 dp
.ntreeitems
= nselparams
;
2062 dp
.treeitems
= smalloc(dp
.ntreeitems
* sizeof(GtkWidget
*));
2064 for (index
= 0; index
< nselparams
; index
++) {
2065 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2066 GTK_SIGNAL_FUNC(treeitem_sel
),
2068 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2072 dlg_refresh(NULL
, &dp
);
2074 dp
.shortcuts
= &selparams
[0].shortcuts
;
2075 dp
.currtreeitem
= dp
.treeitems
[0];
2076 dp
.lastfocus
= NULL
;
2080 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2081 gtk_widget_show(window
);
2084 * Set focus into the first available control.
2086 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2087 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2092 for (j
= 0; j
< s
->ncontrols
; j
++)
2093 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2094 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2095 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2096 dlg_set_focus(s
->ctrls
[j
], &dp
);
2097 dp
.lastfocus
= s
->ctrls
[j
];
2106 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2107 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2108 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2109 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2119 /* ======================================================================
2120 * Below here is a stub main program which allows the dialog box
2121 * code to be compiled and tested with a minimal amount of the rest
2127 /* Compile command for testing:
2129 gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \
2130 ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \
2131 -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs`
2135 void modalfatalbox(char *p
, ...)
2138 fprintf(stderr
, "FATAL ERROR: ");
2140 vfprintf(stderr
, p
, ap
);
2142 fputc('\n', stderr
);
2146 char *cp_name(int codepage
)
2148 return (codepage
== 123 ?
"testing123" :
2149 codepage
== 234 ?
"testing234" :
2150 codepage
== 345 ?
"testing345" :
2154 char *cp_enumerate(int index
)
2156 return (index
== 0 ?
"testing123" :
2157 index
== 1 ?
"testing234" :
2161 int decode_codepage(char *cp_name
)
2163 return (!strcmp(cp_name
, "testing123") ?
123 :
2164 !strcmp(cp_name
, "testing234") ?
234 :
2165 !strcmp(cp_name
, "testing345") ?
345 :
2169 struct printer_enum_tag
{ int dummy
; } printer_test
;
2171 printer_enum
*printer_start_enum(int *nprinters_ptr
) {
2173 return &printer_test
;
2175 char *printer_get_name(printer_enum
*pe
, int i
) {
2176 return (i
==0 ?
"lpr" : i
==1 ?
"lpr -Pfoobar" : NULL
);
2178 void printer_finish_enum(printer_enum
*pe
) { }
2180 char *platform_default_s(const char *name
)
2185 int platform_default_i(const char *name
, int def
)
2190 FontSpec
platform_default_fontspec(const char *name
)
2193 if (!strcmp(name
, "Font"))
2194 strcpy(ret
.name
, "fixed");
2200 Filename
platform_default_filename(const char *name
)
2203 if (!strcmp(name
, "LogFileName"))
2204 strcpy(ret
.path
, "putty.log");
2210 char *x_get_default(const char *key
)
2215 int main(int argc
, char **argv
)
2217 gtk_init(&argc
, &argv
);
2218 printf("returned %d\n", do_config_box("PuTTY Configuration"));