2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
8 * - colour selection woe: what to do about GTK's colour selector
9 * not allowing us full resolution in our own colour selections?
10 * Perhaps making the colour resolution per-platform, at least
11 * at the config level, is actually the least unpleasant
15 * + can't we _somehow_ have less leading between radio buttons?
16 * + wrapping text widgets, the horror, the horror
17 * + labels and their associated edit boxes don't line up
19 * + don't suppose we can fix the vertical offset labels get
20 * from their underlines?
21 * + apparently Left/Right on the treeview should be expanding
22 * and collapsing branches.
23 * + why the hell are the Up/Down focus movement keys sorting
24 * things by _width_? (See the Logging and Features panels
30 * TODO when porting to GTK 2.0:
32 * - GtkTree is apparently deprecated and we should switch to
33 * GtkTreeView instead.
34 * - GtkLabel has a built-in mnemonic scheme, so we should at
35 * least consider switching to that from the current adhockery.
41 #include <gdk/gdkkeysyms.h>
44 #include <X11/Xutil.h>
50 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
64 struct Shortcut sc
[128];
71 int privdata_needs_free
;
72 GtkWidget
**buttons
; int nbuttons
; /* for radio buttons */
73 GtkWidget
*entry
; /* for editbox, combobox, filesel, fontsel */
74 GtkWidget
*button
; /* for filesel, fontsel */
75 GtkWidget
*list
; /* for combobox, listbox */
76 GtkWidget
*menu
; /* for optionmenu (==droplist) */
77 GtkWidget
*optmenu
; /* also for optionmenu */
78 GtkWidget
*text
; /* for text */
79 GtkAdjustment
*adj
; /* for the scrollbar in a list box */
83 tree234
*byctrl
, *bywidget
;
85 struct { unsigned char r
, g
, b
, ok
; } coloursel_result
; /* 0-255 */
86 /* `flags' are set to indicate when a GTK signal handler is being called
87 * due to automatic processing and should not flag a user event. */
89 struct Shortcuts
*shortcuts
;
90 GtkWidget
*window
, *cancelbutton
, *currtreeitem
, **treeitems
;
91 union control
*currfocus
, *lastfocus
;
95 #define FLAG_UPDATING_COMBO_LIST 1
97 enum { /* values for Shortcut.action */
98 SHORTCUT_EMPTY
, /* no shortcut on this key */
99 SHORTCUT_FOCUS
, /* focus the supplied widget */
100 SHORTCUT_UCTRL
, /* do something sane with uctrl */
101 SHORTCUT_UCTRL_UP
, /* uctrl is a draglist, move Up */
102 SHORTCUT_UCTRL_DOWN
, /* uctrl is a draglist, move Down */
106 * Forward references.
108 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
110 static void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
111 int chr
, int action
, void *ptr
);
112 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
114 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
116 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
118 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
119 static void coloursel_ok(GtkButton
*button
, gpointer data
);
120 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
121 static void window_destroy(GtkWidget
*widget
, gpointer data
);
123 static int uctrl_cmp_byctrl(void *av
, void *bv
)
125 struct uctrl
*a
= (struct uctrl
*)av
;
126 struct uctrl
*b
= (struct uctrl
*)bv
;
127 if (a
->ctrl
< b
->ctrl
)
129 else if (a
->ctrl
> b
->ctrl
)
134 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
136 union control
*a
= (union control
*)av
;
137 struct uctrl
*b
= (struct uctrl
*)bv
;
140 else if (a
> b
->ctrl
)
145 static int uctrl_cmp_bywidget(void *av
, void *bv
)
147 struct uctrl
*a
= (struct uctrl
*)av
;
148 struct uctrl
*b
= (struct uctrl
*)bv
;
149 if (a
->toplevel
< b
->toplevel
)
151 else if (a
->toplevel
> b
->toplevel
)
156 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
158 GtkWidget
*a
= (GtkWidget
*)av
;
159 struct uctrl
*b
= (struct uctrl
*)bv
;
162 else if (a
> b
->toplevel
)
167 static void dlg_init(struct dlgparam
*dp
)
169 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
170 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
171 dp
->coloursel_result
.ok
= FALSE
;
174 static void dlg_cleanup(struct dlgparam
*dp
)
178 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
179 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
180 del234(dp
->bywidget
, uc
);
181 if (uc
->privdata_needs_free
)
186 freetree234(dp
->bywidget
);
187 sfree(dp
->treeitems
);
190 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
192 add234(dp
->byctrl
, uc
);
193 add234(dp
->bywidget
, uc
);
196 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
198 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
201 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
203 struct uctrl
*ret
= NULL
;
205 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
213 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
215 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
216 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
220 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
222 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
223 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
225 uc
->privdata_needs_free
= FALSE
;
228 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
230 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
231 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
232 uc
->privdata
= smalloc(size
);
233 uc
->privdata_needs_free
= FALSE
;
237 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
239 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
240 if (dp
->currfocus
!= ctrl
)
241 return dp
->currfocus
;
243 return dp
->lastfocus
;
246 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
248 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
249 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
250 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
251 assert(uc
->buttons
!= NULL
);
252 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
255 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
257 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
258 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
261 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
262 assert(uc
->buttons
!= NULL
);
263 for (i
= 0; i
< uc
->nbuttons
; i
++)
264 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
266 return 0; /* got to return something */
269 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
271 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
272 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
273 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
274 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
277 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
279 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
280 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
281 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
282 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
285 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
287 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
288 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
289 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
290 assert(uc
->entry
!= NULL
);
291 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
294 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
296 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
297 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
298 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
299 assert(uc
->entry
!= NULL
);
300 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
302 buffer
[length
-1] = '\0';
305 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
307 GtkContainer
*cont
= GTK_CONTAINER(data
);
308 /* gtk_container_remove will unref the widget for us; we need not. */
309 gtk_container_remove(cont
, w
);
312 /* The `listbox' functions can also apply to combo boxes. */
313 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
315 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
316 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
);
323 cont
= (uc
->menu ?
GTK_CONTAINER(uc
->menu
) : GTK_CONTAINER(uc
->list
));
325 gtk_container_foreach(cont
, container_remove_and_destroy
, cont
);
328 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
330 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
331 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
333 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
334 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
335 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
339 (GTK_CONTAINER(uc
->menu
),
340 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
342 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
346 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
348 dlg_listbox_addwithindex(ctrl
, dlg
, text
, 0);
352 * Each listbox entry may have a numeric id associated with it.
353 * Note that some front ends only permit a string to be stored at
354 * each position, which means that _if_ you put two identical
355 * strings in any listbox then you MUST not assign them different
356 * IDs and expect to get meaningful results back.
358 void dlg_listbox_addwithindex(union control
*ctrl
, void *dlg
,
359 char const *text
, int id
)
361 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
362 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
364 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
365 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
366 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
368 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
372 * List item in a drop-down (but non-combo) list. Tabs are
373 * ignored; we just provide a standard menu item with the
376 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
378 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
379 gtk_widget_show(menuitem
);
381 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", (gpointer
)id
);
382 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
383 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
384 } else if (!uc
->entry
) {
386 * List item in a non-combo-box list box. We make all of
387 * these Columns containing GtkLabels. This allows us to do
388 * the nasty force_left hack irrespective of whether there
389 * are tabs in the thing.
391 GtkWidget
*listitem
= gtk_list_item_new();
392 GtkWidget
*cols
= columns_new(10);
396 /* Count the tabs in the text, and hence determine # of columns. */
398 for (i
= 0; text
[i
]; i
++)
403 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
404 percents
= smalloc(ncols
* sizeof(gint
));
405 percents
[ncols
-1] = 100;
406 for (i
= 0; i
< ncols
-1; i
++) {
407 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
408 percents
[ncols
-1] -= percents
[i
];
410 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
413 for (i
= 0; i
< ncols
; i
++) {
414 int len
= strcspn(text
, "\t");
415 char *dup
= dupprintf("%.*s", len
, text
);
420 label
= gtk_label_new(dup
);
423 columns_add(COLUMNS(cols
), label
, i
, 1);
424 columns_force_left_align(COLUMNS(cols
), label
);
425 gtk_widget_show(label
);
427 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
428 gtk_widget_show(cols
);
429 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
430 gtk_widget_show(listitem
);
432 if (ctrl
->listbox
.multisel
) {
433 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
434 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
436 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
437 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
439 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
440 GTK_SIGNAL_FUNC(widget_focus
), dp
);
441 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
442 GTK_SIGNAL_FUNC(listitem_button
), dp
);
443 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
446 * List item in a combo-box list, which means the sensible
447 * thing to do is make it a perfectly normal label. Hence
448 * tabs are disregarded.
450 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
452 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
453 gtk_widget_show(listitem
);
455 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
458 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
461 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
463 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
464 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
468 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
469 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
470 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
472 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
474 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
475 g_list_free(children
);
477 return (int)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
480 /* dlg_listbox_index returns <0 if no single element is selected. */
481 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
483 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
484 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
486 GtkWidget
*item
, *activeitem
;
490 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
491 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
492 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
495 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
497 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
499 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
500 i
++, children
= children
->next
) {
501 if (uc
->menu ? activeitem
== item
:
502 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
509 g_list_free(children
);
510 return selected
< 0 ?
-1 : selected
;
513 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
515 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
516 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
518 GtkWidget
*item
, *activeitem
;
520 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
521 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
522 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
524 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
526 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
527 g_list_free(children
);
530 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
531 return item
== activeitem
;
533 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
537 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
539 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
540 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
542 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
543 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
544 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
547 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
549 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
553 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
555 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
556 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
558 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
559 assert(uc
->text
!= NULL
);
561 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
564 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
566 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
567 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
568 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
569 assert(uc
->entry
!= NULL
);
570 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
573 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
575 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
576 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
577 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
578 assert(uc
->entry
!= NULL
);
579 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
581 fn
->path
[lenof(fn
->path
)-1] = '\0';
584 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
586 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
587 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
588 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
589 assert(uc
->entry
!= NULL
);
590 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
593 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
595 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
596 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
597 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
598 assert(uc
->entry
!= NULL
);
599 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
601 fs
->name
[lenof(fs
->name
)-1] = '\0';
605 * Bracketing a large set of updates in these two functions will
606 * cause the front end (if possible) to delay updating the screen
607 * until it's all complete, thus avoiding flicker.
609 void dlg_update_start(union control
*ctrl
, void *dlg
)
612 * Apparently we can't do this at all in GTK. GtkCList supports
613 * freeze and thaw, but not GtkList. Bah.
617 void dlg_update_done(union control
*ctrl
, void *dlg
)
620 * Apparently we can't do this at all in GTK. GtkCList supports
621 * freeze and thaw, but not GtkList. Bah.
625 void dlg_set_focus(union control
*ctrl
, void *dlg
)
627 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
628 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
630 switch (ctrl
->generic
.type
) {
633 /* Check boxes and buttons get the focus _and_ get toggled. */
634 gtk_widget_grab_focus(uc
->toplevel
);
636 case CTRL_FILESELECT
:
637 case CTRL_FONTSELECT
:
639 /* Anything containing an edit box gets that focused. */
640 gtk_widget_grab_focus(uc
->entry
);
644 * Radio buttons: we find the currently selected button and
649 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
650 if (gtk_toggle_button_get_active
651 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
652 gtk_widget_grab_focus(uc
->buttons
[i
]);
658 * If the list is really an option menu, we focus it.
659 * Otherwise we tell it to focus one of its children, which
660 * appears to do the Right Thing.
663 gtk_widget_grab_focus(uc
->optmenu
);
665 assert(uc
->list
!= NULL
);
666 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
673 * During event processing, you might well want to give an error
674 * indication to the user. dlg_beep() is a quick and easy generic
675 * error; dlg_error() puts up a message-box or equivalent.
677 void dlg_beep(void *dlg
)
682 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
684 gtk_widget_destroy(GTK_WIDGET(data
));
687 void dlg_error_msg(void *dlg
, char *msg
)
689 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
690 GtkWidget
*window
, *hbox
, *text
, *ok
;
692 window
= gtk_dialog_new();
693 text
= gtk_label_new(msg
);
694 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
695 hbox
= gtk_hbox_new(FALSE
, 0);
696 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
697 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
698 hbox
, FALSE
, FALSE
, 20);
699 gtk_widget_show(text
);
700 gtk_widget_show(hbox
);
701 gtk_window_set_title(GTK_WINDOW(window
), "Error");
702 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
703 ok
= gtk_button_new_with_label("OK");
704 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
705 ok
, FALSE
, FALSE
, 0);
707 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
708 gtk_window_set_default(GTK_WINDOW(window
), ok
);
709 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
710 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
711 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
712 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
713 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
714 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
716 gint x
, y
, w
, h
, dx
, dy
;
717 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_NONE
);
718 gdk_window_get_origin(GTK_WIDGET(dp
->window
)->window
, &x
, &y
);
719 gdk_window_get_size(GTK_WIDGET(dp
->window
)->window
, &w
, &h
);
722 gtk_widget_set_uposition(GTK_WIDGET(window
), dx
, dy
);
724 gtk_widget_show(window
);
729 * This function signals to the front end that the dialog's
730 * processing is completed, and passes an integer value (typically
733 void dlg_end(void *dlg
, int value
)
735 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
740 void dlg_refresh(union control
*ctrl
, void *dlg
)
742 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
746 if (ctrl
->generic
.handler
!= NULL
)
747 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
751 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
752 assert(uc
->ctrl
!= NULL
);
753 if (uc
->ctrl
->generic
.handler
!= NULL
)
754 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
755 dp
->data
, EVENT_REFRESH
);
760 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
762 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
763 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
766 GtkWidget
*coloursel
=
767 gtk_color_selection_dialog_new("Select a colour");
768 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
770 dp
->coloursel_result
.ok
= FALSE
;
772 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
773 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
774 cvals
[0] = r
/ 255.0;
775 cvals
[1] = g
/ 255.0;
776 cvals
[2] = b
/ 255.0;
777 cvals
[3] = 1.0; /* fully opaque! */
778 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
780 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
781 (gpointer
)coloursel
);
782 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
783 (gpointer
)coloursel
);
784 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
785 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
786 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
787 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
788 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
789 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
790 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
791 (gpointer
)coloursel
);
792 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
793 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
794 (gpointer
)coloursel
);
795 gtk_widget_show(coloursel
);
798 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
799 int *r
, int *g
, int *b
)
801 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
802 if (dp
->coloursel_result
.ok
) {
803 *r
= dp
->coloursel_result
.r
;
804 *g
= dp
->coloursel_result
.g
;
805 *b
= dp
->coloursel_result
.b
;
811 /* ----------------------------------------------------------------------
812 * Signal handlers while the dialog box is active.
815 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
818 struct dlgparam
*dp
= (struct dlgparam
*)data
;
819 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
820 union control
*focus
;
827 if (focus
!= dp
->currfocus
) {
828 dp
->lastfocus
= dp
->currfocus
;
829 dp
->currfocus
= focus
;
835 static void button_clicked(GtkButton
*button
, gpointer data
)
837 struct dlgparam
*dp
= (struct dlgparam
*)data
;
838 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
839 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
842 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
844 struct dlgparam
*dp
= (struct dlgparam
*)data
;
845 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
846 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
849 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
852 * GtkEntry has a nasty habit of eating the Return key, which
853 * is unhelpful since it doesn't actually _do_ anything with it
854 * (it calls gtk_widget_activate, but our edit boxes never need
855 * activating). So I catch Return before GtkEntry sees it, and
856 * pass it straight on to the parent widget. Effect: hitting
857 * Return in an edit box will now activate the default button
858 * in the dialog just like it will everywhere else.
860 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
862 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
863 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
870 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
872 struct dlgparam
*dp
= (struct dlgparam
*)data
;
873 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
874 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
875 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
879 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
882 struct dlgparam
*dp
= (struct dlgparam
*)data
;
883 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
884 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
887 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
890 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
892 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
893 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
894 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
895 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
897 * Up, Down, PgUp or PgDn have been pressed on a ListItem
898 * in a list box. So, if the list box is single-selection:
900 * - if the list item in question isn't already selected,
901 * we simply select it.
902 * - otherwise, we find the next one (or next
903 * however-far-away) in whichever direction we're going,
905 * + in this case, we must also fiddle with the
906 * scrollbar to ensure the newly selected item is
909 * If it's multiple-selection, we do all of the above
910 * except actually selecting anything, so we move the focus
911 * and fiddle the scrollbar to follow it.
913 GtkWidget
*list
= item
->parent
;
915 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
918 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
919 gtk_list_select_child(GTK_LIST(list
), item
);
922 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
923 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
926 (event
->keyval
==GDK_Page_Down
||
927 event
->keyval
==GDK_KP_Page_Down
||
928 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
932 GList
*children
, *chead
;
934 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
936 n
= g_list_length(children
);
940 * Figure out how many list items to a screenful,
941 * and adjust the step appropriately.
943 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
944 step
--; /* go by one less than that */
948 while (children
!= NULL
) {
949 if (item
== children
->data
)
951 children
= children
->next
;
956 if (direction
< 0 && i
> 0)
957 children
= children
->prev
, i
--;
958 else if (direction
> 0 && i
< n
-1)
959 children
= children
->next
, i
++;
963 if (children
&& children
->data
) {
965 gtk_list_select_child(GTK_LIST(list
),
966 GTK_WIDGET(children
->data
));
967 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
968 gtk_adjustment_clamp_page
970 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
971 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
982 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
985 listitem_key(item
, event
, data
, FALSE
);
988 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
991 listitem_key(item
, event
, data
, TRUE
);
994 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
997 struct dlgparam
*dp
= (struct dlgparam
*)data
;
998 if (event
->type
== GDK_2BUTTON_PRESS
||
999 event
->type
== GDK_3BUTTON_PRESS
) {
1000 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1001 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1007 static void list_selchange(GtkList
*list
, gpointer data
)
1009 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1010 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1011 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1014 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1016 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1017 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1018 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1019 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1020 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1023 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1025 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1026 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1030 (index
== 0 && direction
< 0) ||
1031 (index
== g_list_length(children
)-1 && direction
> 0)) {
1036 child
= g_list_nth_data(children
, index
);
1037 gtk_widget_ref(child
);
1038 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1039 g_list_free(children
);
1042 children
= g_list_append(children
, child
);
1043 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1044 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1045 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1048 static void draglist_up(GtkButton
*button
, gpointer data
)
1050 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1051 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1052 draglist_move(dp
, uc
, -1);
1055 static void draglist_down(GtkButton
*button
, gpointer data
)
1057 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1058 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1059 draglist_move(dp
, uc
, +1);
1062 static void filesel_ok(GtkButton
*button
, gpointer data
)
1064 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1065 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1066 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1067 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1068 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1071 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1073 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1074 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1075 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1076 char *name
= gtk_font_selection_dialog_get_font_name
1077 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1078 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1081 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1083 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1084 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1085 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1087 gtk_color_selection_get_color
1088 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1090 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1091 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1092 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1093 dp
->coloursel_result
.ok
= TRUE
;
1094 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1097 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1099 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1100 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1101 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1102 dp
->coloursel_result
.ok
= FALSE
;
1103 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1106 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1108 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1109 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1111 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1112 GtkWidget
*filesel
=
1113 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1114 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1116 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1118 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1120 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1121 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1122 gtk_signal_connect_object
1123 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1124 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1125 gtk_signal_connect_object
1126 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1127 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1128 gtk_widget_show(filesel
);
1131 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1132 gchar
*spacings
[] = { "c", "m", NULL
};
1133 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1134 GtkWidget
*fontsel
=
1135 gtk_font_selection_dialog_new("Select a font");
1136 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1137 gtk_font_selection_dialog_set_filter
1138 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1139 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1140 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1141 if (!gtk_font_selection_dialog_set_font_name
1142 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1144 * If the font name wasn't found as it was, try opening
1145 * it and extracting its FONT property. This should
1146 * have the effect of mapping short aliases into true
1149 GdkFont
*font
= gdk_font_load(fontname
);
1151 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1152 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1153 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1155 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1156 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1158 gtk_font_selection_dialog_set_font_name
1159 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1161 gdk_font_unref(font
);
1165 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1166 "user-data", (gpointer
)fontsel
);
1167 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1169 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1170 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1171 gtk_signal_connect_object
1172 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1173 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1175 gtk_signal_connect_object
1176 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1177 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1179 gtk_widget_show(fontsel
);
1183 /* ----------------------------------------------------------------------
1184 * This function does the main layout work: it reads a controlset,
1185 * it creates the relevant GTK controls, and returns a GtkWidget
1186 * containing the result. (This widget might be a title of some
1187 * sort, it might be a Columns containing many controls, or it
1188 * might be a GtkFrame containing a Columns; whatever it is, it's
1189 * definitely a GtkWidget and should probably be added to a
1192 * `listitemheight' is used to calculate a usize for list boxes: it
1193 * should be the height from the size request of a GtkListItem.
1195 * `win' is required for setting the default button. If it is
1196 * non-NULL, all buttons created will be default-capable (so they
1197 * have extra space round them for the default highlight).
1199 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1200 struct controlset
*s
, int listitemheight
,
1207 if (!s
->boxname
&& s
->boxtitle
) {
1208 /* This controlset is a panel title. */
1209 return gtk_label_new(s
->boxtitle
);
1213 * Otherwise, we expect to be laying out actual controls, so
1214 * we'll start by creating a Columns for the purpose.
1216 cols
= COLUMNS(columns_new(4));
1217 ret
= GTK_WIDGET(cols
);
1218 gtk_widget_show(ret
);
1221 * Create a containing frame if we have a box name.
1224 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1225 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1226 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1227 gtk_widget_show(ret
);
1231 * Now iterate through the controls themselves, create them,
1232 * and add them to the Columns.
1234 for (i
= 0; i
< s
->ncontrols
; i
++) {
1235 union control
*ctrl
= s
->ctrls
[i
];
1238 GtkWidget
*w
= NULL
;
1240 switch (ctrl
->generic
.type
) {
1243 static const int simplecols
[1] = { 100 };
1244 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1245 (ctrl
->columns
.percentages ?
1246 ctrl
->columns
.percentages
: simplecols
));
1248 continue; /* no actual control created */
1251 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1253 columns_taborder_last(cols
, uc
->toplevel
);
1255 continue; /* no actual control created */
1258 uc
= smalloc(sizeof(struct uctrl
));
1260 uc
->privdata
= NULL
;
1261 uc
->privdata_needs_free
= FALSE
;
1263 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1264 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1266 switch (ctrl
->generic
.type
) {
1268 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1270 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1271 if (ctrl
->button
.isdefault
)
1272 gtk_window_set_default(win
, w
);
1273 if (ctrl
->button
.iscancel
)
1274 dp
->cancelbutton
= w
;
1276 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1277 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1278 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1279 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1280 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1281 SHORTCUT_UCTRL
, uc
);
1284 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1285 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1286 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1287 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1288 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1289 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1290 SHORTCUT_UCTRL
, uc
);
1295 * Radio buttons get to go inside their own Columns, no
1299 gint i
, *percentages
;
1303 if (ctrl
->generic
.label
) {
1304 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1305 columns_add(COLUMNS(w
), label
, 0, 1);
1306 columns_force_left_align(COLUMNS(w
), label
);
1307 gtk_widget_show(label
);
1308 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1309 SHORTCUT_UCTRL
, uc
);
1311 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1312 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1314 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1315 100 * i
/ ctrl
->radio
.ncolumns
);
1317 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1319 g_free(percentages
);
1322 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1323 uc
->buttons
= smalloc(uc
->nbuttons
* sizeof(GtkWidget
*));
1325 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1329 b
= (gtk_radio_button_new_with_label
1330 (group
, ctrl
->radio
.buttons
[i
]));
1332 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1333 colstart
= i
% ctrl
->radio
.ncolumns
;
1334 columns_add(COLUMNS(w
), b
, colstart
,
1335 (i
== ctrl
->radio
.nbuttons
-1 ?
1336 ctrl
->radio
.ncolumns
- colstart
: 1));
1337 columns_force_left_align(COLUMNS(w
), b
);
1339 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1340 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1341 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1342 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1343 if (ctrl
->radio
.shortcuts
) {
1344 shortcut_add(scs
, GTK_BIN(b
)->child
,
1345 ctrl
->radio
.shortcuts
[i
],
1346 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.
1377 gtk_widget_size_request(w
, &req
);
1378 gtk_widget_set_usize(w
, 10, req
.height
);
1380 if (ctrl
->generic
.label
) {
1381 GtkWidget
*label
, *container
;
1383 label
= gtk_label_new(ctrl
->generic
.label
);
1384 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1385 SHORTCUT_FOCUS
, uc
->entry
);
1387 container
= columns_new(4);
1388 if (ctrl
->editbox
.percentwidth
== 100) {
1389 columns_add(COLUMNS(container
), label
, 0, 1);
1390 columns_force_left_align(COLUMNS(container
), label
);
1391 columns_add(COLUMNS(container
), w
, 0, 1);
1393 gint percentages
[2];
1394 percentages
[1] = ctrl
->editbox
.percentwidth
;
1395 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1396 columns_set_cols(COLUMNS(container
), 2, percentages
);
1397 columns_add(COLUMNS(container
), label
, 0, 1);
1398 columns_force_left_align(COLUMNS(container
), label
);
1399 columns_add(COLUMNS(container
), w
, 1, 1);
1401 gtk_widget_show(label
);
1406 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1407 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1409 case CTRL_FILESELECT
:
1410 case CTRL_FONTSELECT
:
1415 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1416 "Browse..." : "Change...");
1418 gint percentages
[] = { 75, 25 };
1420 columns_set_cols(COLUMNS(w
), 2, percentages
);
1422 if (ctrl
->generic
.label
) {
1423 ww
= gtk_label_new(ctrl
->generic
.label
);
1424 columns_add(COLUMNS(w
), ww
, 0, 2);
1425 columns_force_left_align(COLUMNS(w
), ww
);
1426 gtk_widget_show(ww
);
1427 shortcut_add(scs
, ww
,
1428 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1429 ctrl
->fileselect
.shortcut
:
1430 ctrl
->fontselect
.shortcut
),
1431 SHORTCUT_UCTRL
, uc
);
1434 uc
->entry
= ww
= gtk_entry_new();
1435 gtk_widget_size_request(ww
, &req
);
1436 gtk_widget_set_usize(ww
, 10, req
.height
);
1437 columns_add(COLUMNS(w
), ww
, 0, 1);
1438 gtk_widget_show(ww
);
1440 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1441 columns_add(COLUMNS(w
), ww
, 1, 1);
1442 gtk_widget_show(ww
);
1444 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1445 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1446 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1447 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1448 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1449 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1450 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1451 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1452 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1453 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1457 if (ctrl
->listbox
.height
== 0) {
1458 uc
->optmenu
= w
= gtk_option_menu_new();
1459 uc
->menu
= gtk_menu_new();
1460 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1461 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1462 (gpointer
)uc
->optmenu
);
1463 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1464 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1466 uc
->list
= gtk_list_new();
1467 if (ctrl
->listbox
.multisel
) {
1468 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1469 GTK_SELECTION_MULTIPLE
);
1471 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1472 GTK_SELECTION_SINGLE
);
1474 w
= gtk_scrolled_window_new(NULL
, NULL
);
1475 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1477 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1479 GTK_POLICY_AUTOMATIC
);
1480 uc
->adj
= gtk_scrolled_window_get_vadjustment
1481 (GTK_SCROLLED_WINDOW(w
));
1483 gtk_widget_show(uc
->list
);
1484 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1485 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1486 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1487 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1490 * Adjust the height of the scrolled window to the
1491 * minimum given by the height parameter.
1493 * This piece of guesswork is a horrid hack based
1494 * on looking inside the GTK 1.2 sources
1495 * (specifically gtkviewport.c, which appears to be
1496 * the widget which provides the border around the
1497 * scrolling area). Anyone lets me know how I can
1498 * do this in a way which isn't at risk from GTK
1499 * upgrades, I'd be grateful.
1502 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1503 gtk_widget_set_usize(w
, 10,
1504 2*edge
+ (ctrl
->listbox
.height
*
1508 if (ctrl
->listbox
.draglist
) {
1510 * GTK doesn't appear to make it easy to
1511 * implement a proper draggable list; so
1512 * instead I'm just going to have to put an Up
1513 * and a Down button to the right of the actual
1514 * list box. Ah well.
1516 GtkWidget
*cols
, *button
;
1517 static const gint percentages
[2] = { 80, 20 };
1519 cols
= columns_new(4);
1520 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1521 columns_add(COLUMNS(cols
), w
, 0, 1);
1523 button
= gtk_button_new_with_label("Up");
1524 columns_add(COLUMNS(cols
), button
, 1, 1);
1525 gtk_widget_show(button
);
1526 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1527 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1528 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1529 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1530 button
= gtk_button_new_with_label("Down");
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_down
), dp
);
1535 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1536 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1542 if (ctrl
->generic
.label
) {
1543 GtkWidget
*label
, *container
;
1545 label
= gtk_label_new(ctrl
->generic
.label
);
1547 container
= columns_new(4);
1548 if (ctrl
->listbox
.percentwidth
== 100) {
1549 columns_add(COLUMNS(container
), label
, 0, 1);
1550 columns_force_left_align(COLUMNS(container
), label
);
1551 columns_add(COLUMNS(container
), w
, 0, 1);
1553 gint percentages
[2];
1554 percentages
[1] = ctrl
->listbox
.percentwidth
;
1555 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1556 columns_set_cols(COLUMNS(container
), 2, percentages
);
1557 columns_add(COLUMNS(container
), label
, 0, 1);
1558 columns_force_left_align(COLUMNS(container
), label
);
1559 columns_add(COLUMNS(container
), w
, 1, 1);
1561 gtk_widget_show(label
);
1563 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1564 SHORTCUT_UCTRL
, uc
);
1569 uc
->text
= w
= gtk_label_new(ctrl
->generic
.label
);
1570 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1571 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1572 /* FIXME: deal with wrapping! */
1578 columns_add(cols
, w
,
1579 COLUMN_START(ctrl
->generic
.column
),
1580 COLUMN_SPAN(ctrl
->generic
.column
));
1582 columns_force_left_align(cols
, w
);
1586 dlg_add_uctrl(dp
, uc
);
1593 struct dlgparam
*dp
;
1595 GtkWidget
*panel
, *treeitem
;
1596 struct Shortcuts shortcuts
;
1599 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1601 struct selparam
*sp
= (struct selparam
*)data
;
1603 panels_switch_to(sp
->panels
, sp
->panel
);
1605 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1606 sp
->dp
->currtreeitem
= sp
->treeitem
;
1609 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1614 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1616 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1618 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1619 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1623 if ((event
->state
& GDK_MOD1_MASK
) &&
1624 (unsigned char)event
->string
[0] > 0 &&
1625 (unsigned char)event
->string
[0] <= 127) {
1626 int schr
= (unsigned char)event
->string
[0];
1627 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1629 switch (sc
->action
) {
1630 case SHORTCUT_FOCUS
:
1631 gtk_widget_grab_focus(sc
->widget
);
1633 case SHORTCUT_UCTRL
:
1635 * We must do something sensible with a uctrl.
1636 * Precisely what this is depends on the type of
1639 switch (sc
->uc
->ctrl
->generic
.type
) {
1642 /* Check boxes and buttons get the focus _and_ get toggled. */
1643 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1644 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1647 case CTRL_FILESELECT
:
1648 case CTRL_FONTSELECT
:
1649 /* File/font selectors have their buttons pressed (ooer),
1650 * and focus transferred to the edit box. */
1651 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1653 gtk_widget_grab_focus(sc
->uc
->entry
);
1657 * Radio buttons are fun, because they have
1658 * multiple shortcuts. We must find whether the
1659 * activated shortcut is the shortcut for the whole
1660 * group, or for a particular button. In the former
1661 * case, we find the currently selected button and
1662 * focus it; in the latter, we focus-and-click the
1663 * button whose shortcut was pressed.
1665 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1667 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1668 if (gtk_toggle_button_get_active
1669 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1670 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1672 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1674 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1675 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1676 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1677 gtk_signal_emit_by_name
1678 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1684 * If the list is really an option menu, we focus
1685 * and click it. Otherwise we tell it to focus one
1686 * of its children, which appears to do the Right
1689 if (sc
->uc
->optmenu
) {
1693 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1694 /* Option menus don't work using the "clicked" signal.
1695 * We need to manufacture a button press event :-/ */
1696 bev
.type
= GDK_BUTTON_PRESS
;
1698 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1699 "button_press_event",
1702 assert(sc
->uc
->list
!= NULL
);
1704 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1705 GTK_DIR_TAB_FORWARD
);
1716 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1718 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1720 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1721 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1723 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1724 if (widget
== dp
->treeitems
[i
]) {
1725 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
) {
1729 if (i
< dp
->ntreeitems
-1)
1734 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1738 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1739 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1747 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1750 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1753 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1754 dir
= (direction
== GTK_DIR_UP
|| direction
== GTK_DIR_LEFT
||
1755 direction
== GTK_DIR_TAB_BACKWARD
) ?
-1 : +1;
1758 * See if any of the treeitems has the focus.
1761 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1762 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1768 * If there's a focused treeitem, we return FALSE to cause the
1769 * focus to move on to some totally other control. If not, we
1770 * focus the selected one.
1775 gtk_widget_grab_focus(dp
->currtreeitem
);
1780 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1781 int chr
, int action
, void *ptr
)
1783 GtkLabel
*label
= GTK_LABEL(labelw
);
1784 gchar
*currstr
, *pattern
;
1787 if (chr
== NO_SHORTCUT
)
1790 chr
= tolower((unsigned char)chr
);
1792 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1794 scs
->sc
[chr
].action
= action
;
1796 if (action
== SHORTCUT_FOCUS
) {
1797 scs
->sc
[chr
].uc
= NULL
;
1798 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1800 scs
->sc
[chr
].widget
= NULL
;
1801 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1804 gtk_label_get(label
, &currstr
);
1805 for (i
= 0; currstr
[i
]; i
++)
1806 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1809 pattern
= dupprintf("%*s_", i
, "");
1811 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1812 gtk_label_set_pattern(label
, pattern
);
1813 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1820 int do_config_box(void)
1822 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1823 *tree
, *treescroll
, *panels
, *panelvbox
;
1824 int index
, level
, listitemheight
;
1825 struct controlbox
*ctrlbox
;
1827 GtkTreeItem
*treeitemlevels
[8];
1828 GtkTree
*treelevels
[8];
1832 struct Shortcuts scs
;
1834 struct selparam
*selparams
= NULL
;
1835 int nselparams
= 0, selparamsize
= 0;
1837 do_defaults(NULL
, &cfg
);
1842 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1844 gtk_widget_size_request(listitem
, &req
);
1845 listitemheight
= req
.height
;
1846 gtk_widget_unref(listitem
);
1851 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1852 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
1855 ctrlbox
= ctrl_new_box();
1856 setup_config_box(ctrlbox
, &sl
, FALSE
, 0);
1857 unix_setup_config_box(ctrlbox
, FALSE
);
1859 window
= gtk_dialog_new();
1860 hbox
= gtk_hbox_new(FALSE
, 4);
1861 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
1862 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
1863 gtk_widget_show(hbox
);
1864 vbox
= gtk_vbox_new(FALSE
, 4);
1865 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
1866 gtk_widget_show(vbox
);
1867 cols
= columns_new(4);
1868 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
1869 gtk_widget_show(cols
);
1870 label
= gtk_label_new("Category:");
1871 columns_add(COLUMNS(cols
), label
, 0, 1);
1872 columns_force_left_align(COLUMNS(cols
), label
);
1873 gtk_widget_show(label
);
1874 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
1875 tree
= gtk_tree_new();
1876 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
1877 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1878 shortcut_add(&scs
, label
, 'g', SHORTCUT_FOCUS
, tree
);
1879 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
1880 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
1881 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
1883 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
1885 GTK_POLICY_AUTOMATIC
);
1886 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
1887 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
1888 gtk_widget_show(tree
);
1889 gtk_widget_show(treescroll
);
1890 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
1891 panels
= panels_new();
1892 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
1893 gtk_widget_show(panels
);
1898 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
1899 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
1902 if (!*s
->pathname
) {
1903 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
1904 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1907 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
1908 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
1910 GtkWidget
*treeitem
;
1914 * We expect never to find an implicit path
1915 * component. For example, we expect never to see
1916 * A/B/C followed by A/D/E, because that would
1917 * _implicitly_ create A/D. All our path prefixes
1918 * are expected to contain actual controls and be
1919 * selectable in the treeview; so we would expect
1920 * to see A/D _explicitly_ before encountering
1923 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
1925 c
= strrchr(s
->pathname
, '/');
1931 treeitem
= gtk_tree_item_new_with_label(c
);
1932 assert(j
-1 < level
);
1934 if (!treelevels
[j
-1]) {
1935 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
1936 gtk_tree_item_set_subtree
1937 (treeitemlevels
[j
-1],
1938 GTK_WIDGET(treelevels
[j
-1]));
1939 gtk_tree_item_expand(treeitemlevels
[j
-1]);
1941 gtk_tree_append(treelevels
[j
-1], treeitem
);
1943 gtk_tree_append(GTK_TREE(tree
), treeitem
);
1945 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
1946 treelevels
[j
] = NULL
;
1949 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
1950 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
1951 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
1952 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1954 gtk_widget_show(treeitem
);
1958 first
= (panelvbox
== NULL
);
1960 panelvbox
= gtk_vbox_new(FALSE
, 4);
1961 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
1963 panels_switch_to(PANELS(panels
), panelvbox
);
1964 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
1967 if (nselparams
>= selparamsize
) {
1969 selparams
= srealloc(selparams
,
1970 selparamsize
* sizeof(*selparams
));
1972 selparams
[nselparams
].dp
= &dp
;
1973 selparams
[nselparams
].panels
= PANELS(panels
);
1974 selparams
[nselparams
].panel
= panelvbox
;
1975 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
1976 selparams
[nselparams
].treeitem
= treeitem
;
1981 w
= layout_ctrls(&dp
,
1982 &selparams
[nselparams
-1].shortcuts
,
1983 s
, listitemheight
, NULL
);
1984 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
1989 dp
.ntreeitems
= nselparams
;
1990 dp
.treeitems
= smalloc(dp
.ntreeitems
* sizeof(GtkWidget
*));
1992 for (index
= 0; index
< nselparams
; index
++) {
1993 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
1994 GTK_SIGNAL_FUNC(treeitem_sel
),
1996 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2000 dlg_refresh(NULL
, &dp
);
2002 dp
.shortcuts
= &selparams
[0].shortcuts
;
2003 dp
.currtreeitem
= dp
.treeitems
[0];
2004 dp
.lastfocus
= NULL
;
2008 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2009 gtk_widget_show(window
);
2012 * Set focus into the first available control.
2014 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2015 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2020 for (j
= 0; j
< s
->ncontrols
; j
++)
2021 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2022 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2023 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2024 dlg_set_focus(s
->ctrls
[j
], &dp
);
2025 dp
.lastfocus
= s
->ctrls
[j
];
2034 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2035 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2036 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2037 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2047 /* ======================================================================
2048 * Below here is a stub main program which allows the dialog box
2049 * code to be compiled and tested with a minimal amount of the rest
2055 /* Compile command for testing:
2057 gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \
2058 ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \
2059 -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs`
2062 void modalfatalbox(char *p
, ...)
2065 fprintf(stderr
, "FATAL ERROR: ");
2067 vfprintf(stderr
, p
, ap
);
2069 fputc('\n', stderr
);
2073 char *cp_name(int codepage
)
2075 return (codepage
== 123 ?
"testing123" :
2076 codepage
== 234 ?
"testing234" :
2077 codepage
== 345 ?
"testing345" :
2081 char *cp_enumerate(int index
)
2083 return (index
== 0 ?
"testing123" :
2084 index
== 1 ?
"testing234" :
2088 int decode_codepage(char *cp_name
)
2090 return (!strcmp(cp_name
, "testing123") ?
123 :
2091 !strcmp(cp_name
, "testing234") ?
234 :
2092 !strcmp(cp_name
, "testing345") ?
345 :
2096 struct printer_enum_tag
{ int dummy
; } printer_test
;
2098 printer_enum
*printer_start_enum(int *nprinters_ptr
) {
2100 return &printer_test
;
2102 char *printer_get_name(printer_enum
*pe
, int i
) {
2103 return (i
==0 ?
"lpr" : i
==1 ?
"lpr -Pfoobar" : NULL
);
2105 void printer_finish_enum(printer_enum
*pe
) { }
2107 char *platform_default_s(const char *name
)
2112 int platform_default_i(const char *name
, int def
)
2117 FontSpec
platform_default_fontspec(const char *name
)
2120 if (!strcmp(name
, "Font"))
2121 strcpy(ret
.name
, "fixed");
2127 Filename
platform_default_filename(const char *name
)
2130 if (!strcmp(name
, "LogFileName"))
2131 strcpy(ret
.path
, "putty.log");
2137 char *x_get_default(const char *key
)
2142 int main(int argc
, char **argv
)
2144 gtk_init(&argc
, &argv
);
2145 printf("returned %d\n", do_config_box());