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
);
211 * This is an internal allocation routine, so it's allowed to
212 * use smalloc directly.
214 uc
->privdata
= smalloc(size
);
215 uc
->privdata_needs_free
= FALSE
;
219 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
221 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
222 if (dp
->currfocus
!= ctrl
)
223 return dp
->currfocus
;
225 return dp
->lastfocus
;
228 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
230 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
231 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
232 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
233 assert(uc
->buttons
!= NULL
);
234 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
237 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
239 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
240 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
243 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
244 assert(uc
->buttons
!= NULL
);
245 for (i
= 0; i
< uc
->nbuttons
; i
++)
246 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
248 return 0; /* got to return something */
251 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
253 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
254 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
255 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
256 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
259 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
261 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
262 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
263 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
264 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
267 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
269 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
270 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
271 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
272 assert(uc
->entry
!= NULL
);
273 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
276 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
278 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
279 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
280 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
281 assert(uc
->entry
!= NULL
);
282 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
284 buffer
[length
-1] = '\0';
287 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
289 GtkContainer
*cont
= GTK_CONTAINER(data
);
290 /* gtk_container_remove will unref the widget for us; we need not. */
291 gtk_container_remove(cont
, w
);
294 /* The `listbox' functions can also apply to combo boxes. */
295 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
297 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
298 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
301 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
302 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
303 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
305 cont
= (uc
->menu ?
GTK_CONTAINER(uc
->menu
) : GTK_CONTAINER(uc
->list
));
307 gtk_container_foreach(cont
, container_remove_and_destroy
, cont
);
310 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
312 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
313 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
315 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
316 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
317 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
321 (GTK_CONTAINER(uc
->menu
),
322 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
324 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
328 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
330 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
334 * Each listbox entry may have a numeric id associated with it.
335 * Note that some front ends only permit a string to be stored at
336 * each position, which means that _if_ you put two identical
337 * strings in any listbox then you MUST not assign them different
338 * IDs and expect to get meaningful results back.
340 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
341 char const *text
, int id
)
343 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
344 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
346 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
347 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
348 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
350 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
354 * List item in a drop-down (but non-combo) list. Tabs are
355 * ignored; we just provide a standard menu item with the
358 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
360 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
361 gtk_widget_show(menuitem
);
363 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", (gpointer
)id
);
364 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
365 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
366 } else if (!uc
->entry
) {
368 * List item in a non-combo-box list box. We make all of
369 * these Columns containing GtkLabels. This allows us to do
370 * the nasty force_left hack irrespective of whether there
371 * are tabs in the thing.
373 GtkWidget
*listitem
= gtk_list_item_new();
374 GtkWidget
*cols
= columns_new(10);
378 /* Count the tabs in the text, and hence determine # of columns. */
380 for (i
= 0; text
[i
]; i
++)
385 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
386 percents
= snewn(ncols
, gint
);
387 percents
[ncols
-1] = 100;
388 for (i
= 0; i
< ncols
-1; i
++) {
389 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
390 percents
[ncols
-1] -= percents
[i
];
392 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
395 for (i
= 0; i
< ncols
; i
++) {
396 int len
= strcspn(text
, "\t");
397 char *dup
= dupprintf("%.*s", len
, text
);
402 label
= gtk_label_new(dup
);
405 columns_add(COLUMNS(cols
), label
, i
, 1);
406 columns_force_left_align(COLUMNS(cols
), label
);
407 gtk_widget_show(label
);
409 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
410 gtk_widget_show(cols
);
411 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
412 gtk_widget_show(listitem
);
414 if (ctrl
->listbox
.multisel
) {
415 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
416 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
418 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
419 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
421 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
422 GTK_SIGNAL_FUNC(widget_focus
), dp
);
423 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
424 GTK_SIGNAL_FUNC(listitem_button
), dp
);
425 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
428 * List item in a combo-box list, which means the sensible
429 * thing to do is make it a perfectly normal label. Hence
430 * tabs are disregarded.
432 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
434 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
435 gtk_widget_show(listitem
);
437 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
440 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
443 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
445 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
446 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
450 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
451 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
452 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
454 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
456 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
457 g_list_free(children
);
459 return (int)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
462 /* dlg_listbox_index returns <0 if no single element is selected. */
463 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
465 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
466 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
468 GtkWidget
*item
, *activeitem
;
472 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
473 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
474 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
477 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
479 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
481 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
482 i
++, children
= children
->next
) {
483 if (uc
->menu ? activeitem
== item
:
484 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
491 g_list_free(children
);
492 return selected
< 0 ?
-1 : selected
;
495 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
497 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
498 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
500 GtkWidget
*item
, *activeitem
;
502 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
503 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
504 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
506 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
508 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
509 g_list_free(children
);
512 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
513 return item
== activeitem
;
515 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
519 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
521 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
522 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
524 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
525 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
526 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
529 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
531 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
535 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
537 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
538 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
540 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
541 assert(uc
->text
!= NULL
);
543 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
546 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
548 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
549 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
550 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
551 assert(uc
->entry
!= NULL
);
552 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
555 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
557 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
558 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
559 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
560 assert(uc
->entry
!= NULL
);
561 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
563 fn
->path
[lenof(fn
->path
)-1] = '\0';
566 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
568 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
569 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
570 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
571 assert(uc
->entry
!= NULL
);
572 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
575 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
577 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
578 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
579 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
580 assert(uc
->entry
!= NULL
);
581 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
583 fs
->name
[lenof(fs
->name
)-1] = '\0';
587 * Bracketing a large set of updates in these two functions will
588 * cause the front end (if possible) to delay updating the screen
589 * until it's all complete, thus avoiding flicker.
591 void dlg_update_start(union control
*ctrl
, void *dlg
)
594 * Apparently we can't do this at all in GTK. GtkCList supports
595 * freeze and thaw, but not GtkList. Bah.
599 void dlg_update_done(union control
*ctrl
, void *dlg
)
602 * Apparently we can't do this at all in GTK. GtkCList supports
603 * freeze and thaw, but not GtkList. Bah.
607 void dlg_set_focus(union control
*ctrl
, void *dlg
)
609 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
610 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
612 switch (ctrl
->generic
.type
) {
615 /* Check boxes and buttons get the focus _and_ get toggled. */
616 gtk_widget_grab_focus(uc
->toplevel
);
618 case CTRL_FILESELECT
:
619 case CTRL_FONTSELECT
:
621 /* Anything containing an edit box gets that focused. */
622 gtk_widget_grab_focus(uc
->entry
);
626 * Radio buttons: we find the currently selected button and
631 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
632 if (gtk_toggle_button_get_active
633 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
634 gtk_widget_grab_focus(uc
->buttons
[i
]);
640 * If the list is really an option menu, we focus it.
641 * Otherwise we tell it to focus one of its children, which
642 * appears to do the Right Thing.
645 gtk_widget_grab_focus(uc
->optmenu
);
647 assert(uc
->list
!= NULL
);
648 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
655 * During event processing, you might well want to give an error
656 * indication to the user. dlg_beep() is a quick and easy generic
657 * error; dlg_error() puts up a message-box or equivalent.
659 void dlg_beep(void *dlg
)
664 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
666 gtk_widget_destroy(GTK_WIDGET(data
));
669 void dlg_error_msg(void *dlg
, char *msg
)
671 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
672 GtkWidget
*window
, *hbox
, *text
, *ok
;
674 window
= gtk_dialog_new();
675 text
= gtk_label_new(msg
);
676 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
677 hbox
= gtk_hbox_new(FALSE
, 0);
678 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
679 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
680 hbox
, FALSE
, FALSE
, 20);
681 gtk_widget_show(text
);
682 gtk_widget_show(hbox
);
683 gtk_window_set_title(GTK_WINDOW(window
), "Error");
684 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
685 ok
= gtk_button_new_with_label("OK");
686 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
687 ok
, FALSE
, FALSE
, 0);
689 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
690 gtk_window_set_default(GTK_WINDOW(window
), ok
);
691 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
692 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
693 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
694 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
695 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
696 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
698 gint x
, y
, w
, h
, dx
, dy
;
699 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_NONE
);
700 gdk_window_get_origin(GTK_WIDGET(dp
->window
)->window
, &x
, &y
);
701 gdk_window_get_size(GTK_WIDGET(dp
->window
)->window
, &w
, &h
);
704 gtk_widget_set_uposition(GTK_WIDGET(window
), dx
, dy
);
706 gtk_widget_show(window
);
711 * This function signals to the front end that the dialog's
712 * processing is completed, and passes an integer value (typically
715 void dlg_end(void *dlg
, int value
)
717 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
722 void dlg_refresh(union control
*ctrl
, void *dlg
)
724 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
728 if (ctrl
->generic
.handler
!= NULL
)
729 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
733 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
734 assert(uc
->ctrl
!= NULL
);
735 if (uc
->ctrl
->generic
.handler
!= NULL
)
736 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
737 dp
->data
, EVENT_REFRESH
);
742 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
744 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
745 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
748 GtkWidget
*coloursel
=
749 gtk_color_selection_dialog_new("Select a colour");
750 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
752 dp
->coloursel_result
.ok
= FALSE
;
754 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
755 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
756 cvals
[0] = r
/ 255.0;
757 cvals
[1] = g
/ 255.0;
758 cvals
[2] = b
/ 255.0;
759 cvals
[3] = 1.0; /* fully opaque! */
760 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
762 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
763 (gpointer
)coloursel
);
764 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
765 (gpointer
)coloursel
);
766 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
767 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
768 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
769 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
770 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
771 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
772 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
773 (gpointer
)coloursel
);
774 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
775 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
776 (gpointer
)coloursel
);
777 gtk_widget_show(coloursel
);
780 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
781 int *r
, int *g
, int *b
)
783 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
784 if (dp
->coloursel_result
.ok
) {
785 *r
= dp
->coloursel_result
.r
;
786 *g
= dp
->coloursel_result
.g
;
787 *b
= dp
->coloursel_result
.b
;
793 /* ----------------------------------------------------------------------
794 * Signal handlers while the dialog box is active.
797 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
800 struct dlgparam
*dp
= (struct dlgparam
*)data
;
801 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
802 union control
*focus
;
809 if (focus
!= dp
->currfocus
) {
810 dp
->lastfocus
= dp
->currfocus
;
811 dp
->currfocus
= focus
;
817 static void button_clicked(GtkButton
*button
, gpointer data
)
819 struct dlgparam
*dp
= (struct dlgparam
*)data
;
820 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
821 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
824 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
826 struct dlgparam
*dp
= (struct dlgparam
*)data
;
827 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
828 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
831 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
834 * GtkEntry has a nasty habit of eating the Return key, which
835 * is unhelpful since it doesn't actually _do_ anything with it
836 * (it calls gtk_widget_activate, but our edit boxes never need
837 * activating). So I catch Return before GtkEntry sees it, and
838 * pass it straight on to the parent widget. Effect: hitting
839 * Return in an edit box will now activate the default button
840 * in the dialog just like it will everywhere else.
842 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
844 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
845 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
852 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
854 struct dlgparam
*dp
= (struct dlgparam
*)data
;
855 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
856 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
857 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
861 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
864 struct dlgparam
*dp
= (struct dlgparam
*)data
;
865 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
866 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
869 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
872 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
874 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
875 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
876 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
877 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
879 * Up, Down, PgUp or PgDn have been pressed on a ListItem
880 * in a list box. So, if the list box is single-selection:
882 * - if the list item in question isn't already selected,
883 * we simply select it.
884 * - otherwise, we find the next one (or next
885 * however-far-away) in whichever direction we're going,
887 * + in this case, we must also fiddle with the
888 * scrollbar to ensure the newly selected item is
891 * If it's multiple-selection, we do all of the above
892 * except actually selecting anything, so we move the focus
893 * and fiddle the scrollbar to follow it.
895 GtkWidget
*list
= item
->parent
;
897 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
900 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
901 gtk_list_select_child(GTK_LIST(list
), item
);
904 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
905 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
908 (event
->keyval
==GDK_Page_Down
||
909 event
->keyval
==GDK_KP_Page_Down
||
910 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
914 GList
*children
, *chead
;
916 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
918 n
= g_list_length(children
);
922 * Figure out how many list items to a screenful,
923 * and adjust the step appropriately.
925 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
926 step
--; /* go by one less than that */
930 while (children
!= NULL
) {
931 if (item
== children
->data
)
933 children
= children
->next
;
938 if (direction
< 0 && i
> 0)
939 children
= children
->prev
, i
--;
940 else if (direction
> 0 && i
< n
-1)
941 children
= children
->next
, i
++;
945 if (children
&& children
->data
) {
947 gtk_list_select_child(GTK_LIST(list
),
948 GTK_WIDGET(children
->data
));
949 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
950 gtk_adjustment_clamp_page
952 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
953 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
964 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
967 listitem_key(item
, event
, data
, FALSE
);
970 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
973 listitem_key(item
, event
, data
, TRUE
);
976 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
979 struct dlgparam
*dp
= (struct dlgparam
*)data
;
980 if (event
->type
== GDK_2BUTTON_PRESS
||
981 event
->type
== GDK_3BUTTON_PRESS
) {
982 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
983 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
989 static void list_selchange(GtkList
*list
, gpointer data
)
991 struct dlgparam
*dp
= (struct dlgparam
*)data
;
992 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
993 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
996 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
998 struct dlgparam
*dp
= (struct dlgparam
*)data
;
999 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1000 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1001 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1002 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1005 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1007 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1008 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1012 (index
== 0 && direction
< 0) ||
1013 (index
== g_list_length(children
)-1 && direction
> 0)) {
1018 child
= g_list_nth_data(children
, index
);
1019 gtk_widget_ref(child
);
1020 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1021 g_list_free(children
);
1024 children
= g_list_append(children
, child
);
1025 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1026 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1027 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1030 static void draglist_up(GtkButton
*button
, gpointer data
)
1032 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1033 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1034 draglist_move(dp
, uc
, -1);
1037 static void draglist_down(GtkButton
*button
, gpointer data
)
1039 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1040 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1041 draglist_move(dp
, uc
, +1);
1044 static void filesel_ok(GtkButton
*button
, gpointer data
)
1046 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1047 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1048 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1049 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1050 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1053 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1055 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1056 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1057 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1058 char *name
= gtk_font_selection_dialog_get_font_name
1059 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1060 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1063 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1065 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1066 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1067 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1069 gtk_color_selection_get_color
1070 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1072 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1073 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1074 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1075 dp
->coloursel_result
.ok
= TRUE
;
1076 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1079 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1081 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1082 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1083 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1084 dp
->coloursel_result
.ok
= FALSE
;
1085 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1088 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1090 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1091 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1093 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1094 GtkWidget
*filesel
=
1095 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1096 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1098 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1100 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1102 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1103 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1104 gtk_signal_connect_object
1105 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1106 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1107 gtk_signal_connect_object
1108 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1109 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1110 gtk_widget_show(filesel
);
1113 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1114 gchar
*spacings
[] = { "c", "m", NULL
};
1115 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1116 GtkWidget
*fontsel
=
1117 gtk_font_selection_dialog_new("Select a font");
1118 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1119 gtk_font_selection_dialog_set_filter
1120 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1121 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1122 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1123 if (!gtk_font_selection_dialog_set_font_name
1124 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1126 * If the font name wasn't found as it was, try opening
1127 * it and extracting its FONT property. This should
1128 * have the effect of mapping short aliases into true
1131 GdkFont
*font
= gdk_font_load(fontname
);
1133 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1134 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1135 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1137 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1138 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1140 gtk_font_selection_dialog_set_font_name
1141 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1143 gdk_font_unref(font
);
1147 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1148 "user-data", (gpointer
)fontsel
);
1149 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1151 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1152 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1153 gtk_signal_connect_object
1154 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1155 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1157 gtk_signal_connect_object
1158 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1159 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1161 gtk_widget_show(fontsel
);
1165 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1168 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1169 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1171 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1172 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1173 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1176 /* ----------------------------------------------------------------------
1177 * This function does the main layout work: it reads a controlset,
1178 * it creates the relevant GTK controls, and returns a GtkWidget
1179 * containing the result. (This widget might be a title of some
1180 * sort, it might be a Columns containing many controls, or it
1181 * might be a GtkFrame containing a Columns; whatever it is, it's
1182 * definitely a GtkWidget and should probably be added to a
1185 * `listitemheight' is used to calculate a usize for list boxes: it
1186 * should be the height from the size request of a GtkListItem.
1188 * `win' is required for setting the default button. If it is
1189 * non-NULL, all buttons created will be default-capable (so they
1190 * have extra space round them for the default highlight).
1192 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1193 struct controlset
*s
, int listitemheight
,
1200 if (!s
->boxname
&& s
->boxtitle
) {
1201 /* This controlset is a panel title. */
1202 return gtk_label_new(s
->boxtitle
);
1206 * Otherwise, we expect to be laying out actual controls, so
1207 * we'll start by creating a Columns for the purpose.
1209 cols
= COLUMNS(columns_new(4));
1210 ret
= GTK_WIDGET(cols
);
1211 gtk_widget_show(ret
);
1214 * Create a containing frame if we have a box name.
1217 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1218 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1219 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1220 gtk_widget_show(ret
);
1224 * Now iterate through the controls themselves, create them,
1225 * and add them to the Columns.
1227 for (i
= 0; i
< s
->ncontrols
; i
++) {
1228 union control
*ctrl
= s
->ctrls
[i
];
1231 GtkWidget
*w
= NULL
;
1233 switch (ctrl
->generic
.type
) {
1236 static const int simplecols
[1] = { 100 };
1237 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1238 (ctrl
->columns
.percentages ?
1239 ctrl
->columns
.percentages
: simplecols
));
1241 continue; /* no actual control created */
1244 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1246 columns_taborder_last(cols
, uc
->toplevel
);
1248 continue; /* no actual control created */
1251 uc
= snew(struct uctrl
);
1253 uc
->privdata
= NULL
;
1254 uc
->privdata_needs_free
= FALSE
;
1256 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1257 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1259 switch (ctrl
->generic
.type
) {
1261 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1263 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1264 if (ctrl
->button
.isdefault
)
1265 gtk_window_set_default(win
, w
);
1266 if (ctrl
->button
.iscancel
)
1267 dp
->cancelbutton
= w
;
1269 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1270 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1271 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1272 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1273 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1274 SHORTCUT_UCTRL
, uc
);
1277 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1278 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1279 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1280 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1281 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1282 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1283 SHORTCUT_UCTRL
, uc
);
1288 * Radio buttons get to go inside their own Columns, no
1292 gint i
, *percentages
;
1296 if (ctrl
->generic
.label
) {
1297 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1298 columns_add(COLUMNS(w
), label
, 0, 1);
1299 columns_force_left_align(COLUMNS(w
), label
);
1300 gtk_widget_show(label
);
1301 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1302 SHORTCUT_UCTRL
, uc
);
1304 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1305 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1307 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1308 100 * i
/ ctrl
->radio
.ncolumns
);
1310 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1312 g_free(percentages
);
1315 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1316 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1318 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1322 b
= (gtk_radio_button_new_with_label
1323 (group
, ctrl
->radio
.buttons
[i
]));
1325 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1326 colstart
= i
% ctrl
->radio
.ncolumns
;
1327 columns_add(COLUMNS(w
), b
, colstart
,
1328 (i
== ctrl
->radio
.nbuttons
-1 ?
1329 ctrl
->radio
.ncolumns
- colstart
: 1));
1330 columns_force_left_align(COLUMNS(w
), b
);
1332 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1333 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1334 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1335 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1336 if (ctrl
->radio
.shortcuts
) {
1337 shortcut_add(scs
, GTK_BIN(b
)->child
,
1338 ctrl
->radio
.shortcuts
[i
],
1339 SHORTCUT_UCTRL
, uc
);
1348 if (ctrl
->editbox
.has_list
) {
1349 w
= gtk_combo_new();
1350 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1351 uc
->entry
= GTK_COMBO(w
)->entry
;
1352 uc
->list
= GTK_COMBO(w
)->list
;
1354 w
= gtk_entry_new();
1355 if (ctrl
->editbox
.password
)
1356 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1359 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1360 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1361 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1362 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1363 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1364 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1366 * Edit boxes, for some strange reason, have a minimum
1367 * width of 150 in GTK 1.2. We don't want this - we'd
1368 * rather the edit boxes acquired their natural width
1369 * from the column layout of the rest of the box.
1371 * Also, while we're here, we'll squirrel away the
1372 * edit box height so we can use that to centre its
1373 * label vertically beside it.
1375 gtk_widget_size_request(w
, &req
);
1376 gtk_widget_set_usize(w
, 10, req
.height
);
1378 if (ctrl
->generic
.label
) {
1379 GtkWidget
*label
, *container
;
1381 label
= gtk_label_new(ctrl
->generic
.label
);
1383 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1384 SHORTCUT_FOCUS
, uc
->entry
);
1386 container
= columns_new(4);
1387 if (ctrl
->editbox
.percentwidth
== 100) {
1388 columns_add(COLUMNS(container
), label
, 0, 1);
1389 columns_force_left_align(COLUMNS(container
), label
);
1390 columns_add(COLUMNS(container
), w
, 0, 1);
1392 gint percentages
[2];
1393 percentages
[1] = ctrl
->editbox
.percentwidth
;
1394 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1395 columns_set_cols(COLUMNS(container
), 2, percentages
);
1396 columns_add(COLUMNS(container
), label
, 0, 1);
1397 columns_force_left_align(COLUMNS(container
), label
);
1398 columns_add(COLUMNS(container
), w
, 1, 1);
1399 /* Centre the label vertically. */
1400 gtk_widget_set_usize(label
, -1, req
.height
);
1401 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1403 gtk_widget_show(label
);
1408 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1409 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1412 case CTRL_FILESELECT
:
1413 case CTRL_FONTSELECT
:
1418 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1419 "Browse..." : "Change...");
1421 gint percentages
[] = { 75, 25 };
1423 columns_set_cols(COLUMNS(w
), 2, percentages
);
1425 if (ctrl
->generic
.label
) {
1426 ww
= gtk_label_new(ctrl
->generic
.label
);
1427 columns_add(COLUMNS(w
), ww
, 0, 2);
1428 columns_force_left_align(COLUMNS(w
), ww
);
1429 gtk_widget_show(ww
);
1430 shortcut_add(scs
, ww
,
1431 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1432 ctrl
->fileselect
.shortcut
:
1433 ctrl
->fontselect
.shortcut
),
1434 SHORTCUT_UCTRL
, uc
);
1437 uc
->entry
= ww
= gtk_entry_new();
1438 gtk_widget_size_request(ww
, &req
);
1439 gtk_widget_set_usize(ww
, 10, req
.height
);
1440 columns_add(COLUMNS(w
), ww
, 0, 1);
1441 gtk_widget_show(ww
);
1443 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1444 columns_add(COLUMNS(w
), ww
, 1, 1);
1445 gtk_widget_show(ww
);
1447 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1448 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1449 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1450 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1451 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1452 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1453 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1454 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1455 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1456 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1460 if (ctrl
->listbox
.height
== 0) {
1461 uc
->optmenu
= w
= gtk_option_menu_new();
1462 uc
->menu
= gtk_menu_new();
1463 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1464 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1465 (gpointer
)uc
->optmenu
);
1466 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1467 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1469 uc
->list
= gtk_list_new();
1470 if (ctrl
->listbox
.multisel
) {
1471 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1472 GTK_SELECTION_MULTIPLE
);
1474 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1475 GTK_SELECTION_SINGLE
);
1477 w
= gtk_scrolled_window_new(NULL
, NULL
);
1478 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1480 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1482 GTK_POLICY_AUTOMATIC
);
1483 uc
->adj
= gtk_scrolled_window_get_vadjustment
1484 (GTK_SCROLLED_WINDOW(w
));
1486 gtk_widget_show(uc
->list
);
1487 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1488 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1489 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1490 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1493 * Adjust the height of the scrolled window to the
1494 * minimum given by the height parameter.
1496 * This piece of guesswork is a horrid hack based
1497 * on looking inside the GTK 1.2 sources
1498 * (specifically gtkviewport.c, which appears to be
1499 * the widget which provides the border around the
1500 * scrolling area). Anyone lets me know how I can
1501 * do this in a way which isn't at risk from GTK
1502 * upgrades, I'd be grateful.
1505 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1506 gtk_widget_set_usize(w
, 10,
1507 2*edge
+ (ctrl
->listbox
.height
*
1511 if (ctrl
->listbox
.draglist
) {
1513 * GTK doesn't appear to make it easy to
1514 * implement a proper draggable list; so
1515 * instead I'm just going to have to put an Up
1516 * and a Down button to the right of the actual
1517 * list box. Ah well.
1519 GtkWidget
*cols
, *button
;
1520 static const gint percentages
[2] = { 80, 20 };
1522 cols
= columns_new(4);
1523 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1524 columns_add(COLUMNS(cols
), w
, 0, 1);
1526 button
= gtk_button_new_with_label("Up");
1527 columns_add(COLUMNS(cols
), button
, 1, 1);
1528 gtk_widget_show(button
);
1529 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1530 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1531 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1532 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1533 button
= gtk_button_new_with_label("Down");
1534 columns_add(COLUMNS(cols
), button
, 1, 1);
1535 gtk_widget_show(button
);
1536 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1537 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1538 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1539 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1545 if (ctrl
->generic
.label
) {
1546 GtkWidget
*label
, *container
;
1548 label
= gtk_label_new(ctrl
->generic
.label
);
1550 container
= columns_new(4);
1551 if (ctrl
->listbox
.percentwidth
== 100) {
1552 columns_add(COLUMNS(container
), label
, 0, 1);
1553 columns_force_left_align(COLUMNS(container
), label
);
1554 columns_add(COLUMNS(container
), w
, 0, 1);
1556 gint percentages
[2];
1557 percentages
[1] = ctrl
->listbox
.percentwidth
;
1558 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1559 columns_set_cols(COLUMNS(container
), 2, percentages
);
1560 columns_add(COLUMNS(container
), label
, 0, 1);
1561 columns_force_left_align(COLUMNS(container
), label
);
1562 columns_add(COLUMNS(container
), w
, 1, 1);
1564 gtk_widget_show(label
);
1566 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1567 SHORTCUT_UCTRL
, uc
);
1573 * Wrapping text widgets don't sit well with the GTK
1574 * layout model, in which widgets state a minimum size
1575 * and the whole window then adjusts to the smallest
1576 * size it can sensibly take given its contents. A
1577 * wrapping text widget _has_ no clear minimum size;
1578 * instead it has a range of possibilities. It can be
1579 * one line deep but 2000 wide, or two lines deep and
1580 * 1000 pixels, or three by 867, or four by 500 and so
1581 * on. It can be as short as you like provided you
1582 * don't mind it being wide, or as narrow as you like
1583 * provided you don't mind it being tall.
1585 * Therefore, it fits very badly into the layout model.
1586 * Hence the only thing to do is pick a width and let
1587 * it choose its own number of lines. To do this I'm
1588 * going to cheat a little. All new wrapping text
1589 * widgets will be created with a minimal text content
1590 * "X"; then, after the rest of the dialog box is set
1591 * up and its size calculated, the text widgets will be
1592 * told their width and given their real text, which
1593 * will cause the size to be recomputed in the y
1594 * direction (because many of them will expand to more
1597 uc
->text
= w
= gtk_label_new("X");
1598 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1599 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1601 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1602 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1608 columns_add(cols
, w
,
1609 COLUMN_START(ctrl
->generic
.column
),
1610 COLUMN_SPAN(ctrl
->generic
.column
));
1612 columns_force_left_align(cols
, w
);
1616 dlg_add_uctrl(dp
, uc
);
1623 struct dlgparam
*dp
;
1625 GtkWidget
*panel
, *treeitem
;
1626 struct Shortcuts shortcuts
;
1629 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1631 struct selparam
*sp
= (struct selparam
*)data
;
1633 panels_switch_to(sp
->panels
, sp
->panel
);
1635 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1636 sp
->dp
->currtreeitem
= sp
->treeitem
;
1639 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1644 static int tree_grab_focus(struct dlgparam
*dp
)
1649 * See if any of the treeitems has the focus.
1652 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1653 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1661 gtk_widget_grab_focus(dp
->currtreeitem
);
1666 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1669 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1671 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1673 * If there's a focused treeitem, we return FALSE to cause the
1674 * focus to move on to some totally other control. If not, we
1675 * focus the selected one.
1677 return tree_grab_focus(dp
);
1680 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1682 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1684 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1685 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1689 if ((event
->state
& GDK_MOD1_MASK
) &&
1690 (unsigned char)event
->string
[0] > 0 &&
1691 (unsigned char)event
->string
[0] <= 127) {
1692 int schr
= (unsigned char)event
->string
[0];
1693 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1695 switch (sc
->action
) {
1697 tree_grab_focus(dp
);
1699 case SHORTCUT_FOCUS
:
1700 gtk_widget_grab_focus(sc
->widget
);
1702 case SHORTCUT_UCTRL
:
1704 * We must do something sensible with a uctrl.
1705 * Precisely what this is depends on the type of
1708 switch (sc
->uc
->ctrl
->generic
.type
) {
1711 /* Check boxes and buttons get the focus _and_ get toggled. */
1712 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1713 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1716 case CTRL_FILESELECT
:
1717 case CTRL_FONTSELECT
:
1718 /* File/font selectors have their buttons pressed (ooer),
1719 * and focus transferred to the edit box. */
1720 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1722 gtk_widget_grab_focus(sc
->uc
->entry
);
1726 * Radio buttons are fun, because they have
1727 * multiple shortcuts. We must find whether the
1728 * activated shortcut is the shortcut for the whole
1729 * group, or for a particular button. In the former
1730 * case, we find the currently selected button and
1731 * focus it; in the latter, we focus-and-click the
1732 * button whose shortcut was pressed.
1734 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1736 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1737 if (gtk_toggle_button_get_active
1738 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1739 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1741 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1743 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1744 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1745 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1746 gtk_signal_emit_by_name
1747 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1753 * If the list is really an option menu, we focus
1754 * and click it. Otherwise we tell it to focus one
1755 * of its children, which appears to do the Right
1758 if (sc
->uc
->optmenu
) {
1762 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1763 /* Option menus don't work using the "clicked" signal.
1764 * We need to manufacture a button press event :-/ */
1765 bev
.type
= GDK_BUTTON_PRESS
;
1767 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1768 "button_press_event",
1771 assert(sc
->uc
->list
!= NULL
);
1773 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1774 GTK_DIR_TAB_FORWARD
);
1785 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1787 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1789 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1790 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1792 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1793 if (widget
== dp
->treeitems
[i
])
1795 if (i
< dp
->ntreeitems
) {
1796 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1803 if (i
< 0 || i
>= dp
->ntreeitems
)
1804 break; /* nothing in that dir to select */
1806 * Determine if this tree item is visible.
1809 GtkWidget
*w
= dp
->treeitems
[i
];
1811 while (w
&& GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
)) {
1812 if (!GTK_WIDGET_VISIBLE(w
)) {
1819 j
= i
; /* got one */
1825 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1829 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1830 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1836 * It's nice for Left and Right to expand and collapse tree
1839 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1840 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1842 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1845 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1846 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1848 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1855 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1856 int chr
, int action
, void *ptr
)
1858 GtkLabel
*label
= GTK_LABEL(labelw
);
1859 gchar
*currstr
, *pattern
;
1862 if (chr
== NO_SHORTCUT
)
1865 chr
= tolower((unsigned char)chr
);
1867 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1869 scs
->sc
[chr
].action
= action
;
1871 if (action
== SHORTCUT_FOCUS
) {
1872 scs
->sc
[chr
].uc
= NULL
;
1873 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1875 scs
->sc
[chr
].widget
= NULL
;
1876 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1879 gtk_label_get(label
, &currstr
);
1880 for (i
= 0; currstr
[i
]; i
++)
1881 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1884 pattern
= dupprintf("%*s_", i
, "");
1886 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1887 gtk_label_set_pattern(label
, pattern
);
1888 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1895 int do_config_box(const char *title
)
1897 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1898 *tree
, *treescroll
, *panels
, *panelvbox
;
1899 int index
, level
, listitemheight
;
1900 struct controlbox
*ctrlbox
;
1902 GtkTreeItem
*treeitemlevels
[8];
1903 GtkTree
*treelevels
[8];
1907 struct Shortcuts scs
;
1909 struct selparam
*selparams
= NULL
;
1910 int nselparams
= 0, selparamsize
= 0;
1912 do_defaults(NULL
, &cfg
);
1917 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1919 gtk_widget_size_request(listitem
, &req
);
1920 listitemheight
= req
.height
;
1921 gtk_widget_unref(listitem
);
1926 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1927 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
1930 ctrlbox
= ctrl_new_box();
1931 setup_config_box(ctrlbox
, &sl
, FALSE
, 0);
1932 unix_setup_config_box(ctrlbox
, FALSE
);
1934 window
= gtk_dialog_new();
1935 gtk_window_set_title(GTK_WINDOW(window
), title
);
1936 hbox
= gtk_hbox_new(FALSE
, 4);
1937 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
1938 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
1939 gtk_widget_show(hbox
);
1940 vbox
= gtk_vbox_new(FALSE
, 4);
1941 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
1942 gtk_widget_show(vbox
);
1943 cols
= columns_new(4);
1944 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
1945 gtk_widget_show(cols
);
1946 label
= gtk_label_new("Category:");
1947 columns_add(COLUMNS(cols
), label
, 0, 1);
1948 columns_force_left_align(COLUMNS(cols
), label
);
1949 gtk_widget_show(label
);
1950 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
1951 tree
= gtk_tree_new();
1952 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
1953 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1954 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
1955 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
1956 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
1957 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
1959 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
1961 GTK_POLICY_AUTOMATIC
);
1962 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
1963 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
1964 gtk_widget_show(tree
);
1965 gtk_widget_show(treescroll
);
1966 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
1967 panels
= panels_new();
1968 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
1969 gtk_widget_show(panels
);
1974 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
1975 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
1978 if (!*s
->pathname
) {
1979 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
1980 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1983 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
1984 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
1986 GtkWidget
*treeitem
;
1990 * We expect never to find an implicit path
1991 * component. For example, we expect never to see
1992 * A/B/C followed by A/D/E, because that would
1993 * _implicitly_ create A/D. All our path prefixes
1994 * are expected to contain actual controls and be
1995 * selectable in the treeview; so we would expect
1996 * to see A/D _explicitly_ before encountering
1999 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2001 c
= strrchr(s
->pathname
, '/');
2007 treeitem
= gtk_tree_item_new_with_label(c
);
2008 assert(j
-1 < level
);
2010 if (!treelevels
[j
-1]) {
2011 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2012 gtk_tree_item_set_subtree
2013 (treeitemlevels
[j
-1],
2014 GTK_WIDGET(treelevels
[j
-1]));
2015 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2017 gtk_tree_append(treelevels
[j
-1], treeitem
);
2019 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2021 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2022 treelevels
[j
] = NULL
;
2025 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2026 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2027 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2028 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2030 gtk_widget_show(treeitem
);
2034 first
= (panelvbox
== NULL
);
2036 panelvbox
= gtk_vbox_new(FALSE
, 4);
2037 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2039 panels_switch_to(PANELS(panels
), panelvbox
);
2040 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2043 if (nselparams
>= selparamsize
) {
2045 selparams
= sresize(selparams
, selparamsize
,
2048 selparams
[nselparams
].dp
= &dp
;
2049 selparams
[nselparams
].panels
= PANELS(panels
);
2050 selparams
[nselparams
].panel
= panelvbox
;
2051 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2052 selparams
[nselparams
].treeitem
= treeitem
;
2057 w
= layout_ctrls(&dp
,
2058 &selparams
[nselparams
-1].shortcuts
,
2059 s
, listitemheight
, NULL
);
2060 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2065 dp
.ntreeitems
= nselparams
;
2066 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2068 for (index
= 0; index
< nselparams
; index
++) {
2069 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2070 GTK_SIGNAL_FUNC(treeitem_sel
),
2072 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2076 dlg_refresh(NULL
, &dp
);
2078 dp
.shortcuts
= &selparams
[0].shortcuts
;
2079 dp
.currtreeitem
= dp
.treeitems
[0];
2080 dp
.lastfocus
= NULL
;
2084 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2085 gtk_widget_show(window
);
2088 * Set focus into the first available control.
2090 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2091 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2096 for (j
= 0; j
< s
->ncontrols
; j
++)
2097 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2098 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2099 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2100 dlg_set_focus(s
->ctrls
[j
], &dp
);
2101 dp
.lastfocus
= s
->ctrls
[j
];
2110 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2111 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2112 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2113 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2123 /* ======================================================================
2124 * Below here is a stub main program which allows the dialog box
2125 * code to be compiled and tested with a minimal amount of the rest
2131 /* Compile command for testing:
2133 gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \
2134 ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \
2135 -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs`
2139 void modalfatalbox(char *p
, ...)
2142 fprintf(stderr
, "FATAL ERROR: ");
2144 vfprintf(stderr
, p
, ap
);
2146 fputc('\n', stderr
);
2150 char *cp_name(int codepage
)
2152 return (codepage
== 123 ?
"testing123" :
2153 codepage
== 234 ?
"testing234" :
2154 codepage
== 345 ?
"testing345" :
2158 char *cp_enumerate(int index
)
2160 return (index
== 0 ?
"testing123" :
2161 index
== 1 ?
"testing234" :
2165 int decode_codepage(char *cp_name
)
2167 return (!strcmp(cp_name
, "testing123") ?
123 :
2168 !strcmp(cp_name
, "testing234") ?
234 :
2169 !strcmp(cp_name
, "testing345") ?
345 :
2173 struct printer_enum_tag
{ int dummy
; } printer_test
;
2175 printer_enum
*printer_start_enum(int *nprinters_ptr
) {
2177 return &printer_test
;
2179 char *printer_get_name(printer_enum
*pe
, int i
) {
2180 return (i
==0 ?
"lpr" : i
==1 ?
"lpr -Pfoobar" : NULL
);
2182 void printer_finish_enum(printer_enum
*pe
) { }
2184 char *platform_default_s(const char *name
)
2189 int platform_default_i(const char *name
, int def
)
2194 FontSpec
platform_default_fontspec(const char *name
)
2197 if (!strcmp(name
, "Font"))
2198 strcpy(ret
.name
, "fixed");
2204 Filename
platform_default_filename(const char *name
)
2207 if (!strcmp(name
, "LogFileName"))
2208 strcpy(ret
.path
, "putty.log");
2214 char *x_get_default(const char *key
)
2219 int main(int argc
, char **argv
)
2221 gtk_init(&argc
, &argv
);
2222 printf("returned %d\n", do_config_box("PuTTY Configuration"));