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
;
719 gtk_widget_destroy(dp
->window
);
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
)
913 GList
*children
, *chead
;
915 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
917 n
= g_list_length(children
);
921 * Figure out how many list items to a screenful,
922 * and adjust the step appropriately.
924 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
925 step
--; /* go by one less than that */
929 while (children
!= NULL
) {
930 if (item
== children
->data
)
932 children
= children
->next
;
937 if (direction
< 0 && i
> 0)
938 children
= children
->prev
, i
--;
939 else if (direction
> 0 && i
< n
-1)
940 children
= children
->next
, i
++;
944 if (children
&& children
->data
) {
946 gtk_list_select_child(GTK_LIST(list
),
947 GTK_WIDGET(children
->data
));
948 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
949 gtk_adjustment_clamp_page
951 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
952 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
963 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
966 return listitem_key(item
, event
, data
, FALSE
);
969 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
972 return listitem_key(item
, event
, data
, TRUE
);
975 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
978 struct dlgparam
*dp
= (struct dlgparam
*)data
;
979 if (event
->type
== GDK_2BUTTON_PRESS
||
980 event
->type
== GDK_3BUTTON_PRESS
) {
981 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
982 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
988 static void list_selchange(GtkList
*list
, gpointer data
)
990 struct dlgparam
*dp
= (struct dlgparam
*)data
;
991 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
992 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
995 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
997 struct dlgparam
*dp
= (struct dlgparam
*)data
;
998 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
999 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1000 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1001 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1004 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1006 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1007 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1011 (index
== 0 && direction
< 0) ||
1012 (index
== g_list_length(children
)-1 && direction
> 0)) {
1017 child
= g_list_nth_data(children
, index
);
1018 gtk_widget_ref(child
);
1019 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1020 g_list_free(children
);
1023 children
= g_list_append(children
, child
);
1024 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1025 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1026 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1029 static void draglist_up(GtkButton
*button
, gpointer data
)
1031 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1032 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1033 draglist_move(dp
, uc
, -1);
1036 static void draglist_down(GtkButton
*button
, gpointer data
)
1038 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1039 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1040 draglist_move(dp
, uc
, +1);
1043 static void filesel_ok(GtkButton
*button
, gpointer data
)
1045 /* struct dlgparam *dp = (struct dlgparam *)data; */
1046 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1047 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1048 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1049 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1052 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1054 /* struct dlgparam *dp = (struct dlgparam *)data; */
1055 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1056 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1057 char *name
= gtk_font_selection_dialog_get_font_name
1058 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1059 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1062 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1064 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1065 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1066 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1068 gtk_color_selection_get_color
1069 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1071 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1072 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1073 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1074 dp
->coloursel_result
.ok
= TRUE
;
1075 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1078 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1080 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1081 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1082 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1083 dp
->coloursel_result
.ok
= FALSE
;
1084 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1087 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1089 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1090 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1092 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1093 GtkWidget
*filesel
=
1094 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1095 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1097 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1099 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1101 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1102 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1103 gtk_signal_connect_object
1104 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1105 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1106 gtk_signal_connect_object
1107 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1108 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1109 gtk_widget_show(filesel
);
1112 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1113 gchar
*spacings
[] = { "c", "m", NULL
};
1114 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1115 GtkWidget
*fontsel
=
1116 gtk_font_selection_dialog_new("Select a font");
1117 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1118 gtk_font_selection_dialog_set_filter
1119 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1120 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1121 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1122 if (!gtk_font_selection_dialog_set_font_name
1123 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1125 * If the font name wasn't found as it was, try opening
1126 * it and extracting its FONT property. This should
1127 * have the effect of mapping short aliases into true
1130 GdkFont
*font
= gdk_font_load(fontname
);
1132 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1133 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1134 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1136 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1137 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1139 gtk_font_selection_dialog_set_font_name
1140 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1142 gdk_font_unref(font
);
1146 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1147 "user-data", (gpointer
)fontsel
);
1148 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1150 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1151 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1152 gtk_signal_connect_object
1153 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1154 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1156 gtk_signal_connect_object
1157 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1158 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1160 gtk_widget_show(fontsel
);
1164 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1167 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1168 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1170 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1171 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1172 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1175 /* ----------------------------------------------------------------------
1176 * This function does the main layout work: it reads a controlset,
1177 * it creates the relevant GTK controls, and returns a GtkWidget
1178 * containing the result. (This widget might be a title of some
1179 * sort, it might be a Columns containing many controls, or it
1180 * might be a GtkFrame containing a Columns; whatever it is, it's
1181 * definitely a GtkWidget and should probably be added to a
1184 * `listitemheight' is used to calculate a usize for list boxes: it
1185 * should be the height from the size request of a GtkListItem.
1187 * `win' is required for setting the default button. If it is
1188 * non-NULL, all buttons created will be default-capable (so they
1189 * have extra space round them for the default highlight).
1191 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1192 struct controlset
*s
, int listitemheight
,
1199 if (!s
->boxname
&& s
->boxtitle
) {
1200 /* This controlset is a panel title. */
1201 return gtk_label_new(s
->boxtitle
);
1205 * Otherwise, we expect to be laying out actual controls, so
1206 * we'll start by creating a Columns for the purpose.
1208 cols
= COLUMNS(columns_new(4));
1209 ret
= GTK_WIDGET(cols
);
1210 gtk_widget_show(ret
);
1213 * Create a containing frame if we have a box name.
1216 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1217 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1218 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1219 gtk_widget_show(ret
);
1223 * Now iterate through the controls themselves, create them,
1224 * and add them to the Columns.
1226 for (i
= 0; i
< s
->ncontrols
; i
++) {
1227 union control
*ctrl
= s
->ctrls
[i
];
1230 GtkWidget
*w
= NULL
;
1232 switch (ctrl
->generic
.type
) {
1235 static const int simplecols
[1] = { 100 };
1236 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1237 (ctrl
->columns
.percentages ?
1238 ctrl
->columns
.percentages
: simplecols
));
1240 continue; /* no actual control created */
1243 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1245 columns_taborder_last(cols
, uc
->toplevel
);
1247 continue; /* no actual control created */
1250 uc
= snew(struct uctrl
);
1252 uc
->privdata
= NULL
;
1253 uc
->privdata_needs_free
= FALSE
;
1255 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1256 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1258 switch (ctrl
->generic
.type
) {
1260 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1262 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1263 if (ctrl
->button
.isdefault
)
1264 gtk_window_set_default(win
, w
);
1265 if (ctrl
->button
.iscancel
)
1266 dp
->cancelbutton
= w
;
1268 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1269 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1270 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1271 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1272 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1273 SHORTCUT_UCTRL
, uc
);
1276 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1277 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1278 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1279 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1280 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1281 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1282 SHORTCUT_UCTRL
, uc
);
1287 * Radio buttons get to go inside their own Columns, no
1291 gint i
, *percentages
;
1295 if (ctrl
->generic
.label
) {
1296 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1297 columns_add(COLUMNS(w
), label
, 0, 1);
1298 columns_force_left_align(COLUMNS(w
), label
);
1299 gtk_widget_show(label
);
1300 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1301 SHORTCUT_UCTRL
, uc
);
1303 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1304 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1306 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1307 100 * i
/ ctrl
->radio
.ncolumns
);
1309 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1311 g_free(percentages
);
1314 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1315 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1317 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1321 b
= (gtk_radio_button_new_with_label
1322 (group
, ctrl
->radio
.buttons
[i
]));
1324 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1325 colstart
= i
% ctrl
->radio
.ncolumns
;
1326 columns_add(COLUMNS(w
), b
, colstart
,
1327 (i
== ctrl
->radio
.nbuttons
-1 ?
1328 ctrl
->radio
.ncolumns
- colstart
: 1));
1329 columns_force_left_align(COLUMNS(w
), b
);
1331 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1332 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1333 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1334 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1335 if (ctrl
->radio
.shortcuts
) {
1336 shortcut_add(scs
, GTK_BIN(b
)->child
,
1337 ctrl
->radio
.shortcuts
[i
],
1338 SHORTCUT_UCTRL
, uc
);
1347 if (ctrl
->editbox
.has_list
) {
1348 w
= gtk_combo_new();
1349 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1350 uc
->entry
= GTK_COMBO(w
)->entry
;
1351 uc
->list
= GTK_COMBO(w
)->list
;
1353 w
= gtk_entry_new();
1354 if (ctrl
->editbox
.password
)
1355 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1358 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1359 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1360 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1361 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1362 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1363 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1365 * Edit boxes, for some strange reason, have a minimum
1366 * width of 150 in GTK 1.2. We don't want this - we'd
1367 * rather the edit boxes acquired their natural width
1368 * from the column layout of the rest of the box.
1370 * Also, while we're here, we'll squirrel away the
1371 * edit box height so we can use that to centre its
1372 * label vertically beside it.
1374 gtk_widget_size_request(w
, &req
);
1375 gtk_widget_set_usize(w
, 10, req
.height
);
1377 if (ctrl
->generic
.label
) {
1378 GtkWidget
*label
, *container
;
1380 label
= gtk_label_new(ctrl
->generic
.label
);
1382 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1383 SHORTCUT_FOCUS
, uc
->entry
);
1385 container
= columns_new(4);
1386 if (ctrl
->editbox
.percentwidth
== 100) {
1387 columns_add(COLUMNS(container
), label
, 0, 1);
1388 columns_force_left_align(COLUMNS(container
), label
);
1389 columns_add(COLUMNS(container
), w
, 0, 1);
1391 gint percentages
[2];
1392 percentages
[1] = ctrl
->editbox
.percentwidth
;
1393 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1394 columns_set_cols(COLUMNS(container
), 2, percentages
);
1395 columns_add(COLUMNS(container
), label
, 0, 1);
1396 columns_force_left_align(COLUMNS(container
), label
);
1397 columns_add(COLUMNS(container
), w
, 1, 1);
1398 /* Centre the label vertically. */
1399 gtk_widget_set_usize(label
, -1, req
.height
);
1400 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1402 gtk_widget_show(label
);
1407 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1408 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1411 case CTRL_FILESELECT
:
1412 case CTRL_FONTSELECT
:
1417 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1418 "Browse..." : "Change...");
1420 gint percentages
[] = { 75, 25 };
1422 columns_set_cols(COLUMNS(w
), 2, percentages
);
1424 if (ctrl
->generic
.label
) {
1425 ww
= gtk_label_new(ctrl
->generic
.label
);
1426 columns_add(COLUMNS(w
), ww
, 0, 2);
1427 columns_force_left_align(COLUMNS(w
), ww
);
1428 gtk_widget_show(ww
);
1429 shortcut_add(scs
, ww
,
1430 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1431 ctrl
->fileselect
.shortcut
:
1432 ctrl
->fontselect
.shortcut
),
1433 SHORTCUT_UCTRL
, uc
);
1436 uc
->entry
= ww
= gtk_entry_new();
1437 gtk_widget_size_request(ww
, &req
);
1438 gtk_widget_set_usize(ww
, 10, req
.height
);
1439 columns_add(COLUMNS(w
), ww
, 0, 1);
1440 gtk_widget_show(ww
);
1442 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1443 columns_add(COLUMNS(w
), ww
, 1, 1);
1444 gtk_widget_show(ww
);
1446 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1447 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1448 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1449 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1450 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1451 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1452 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1453 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1454 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1455 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1459 if (ctrl
->listbox
.height
== 0) {
1460 uc
->optmenu
= w
= gtk_option_menu_new();
1461 uc
->menu
= gtk_menu_new();
1462 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1463 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1464 (gpointer
)uc
->optmenu
);
1465 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1466 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1468 uc
->list
= gtk_list_new();
1469 if (ctrl
->listbox
.multisel
) {
1470 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1471 GTK_SELECTION_MULTIPLE
);
1473 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1474 GTK_SELECTION_SINGLE
);
1476 w
= gtk_scrolled_window_new(NULL
, NULL
);
1477 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1479 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1481 GTK_POLICY_AUTOMATIC
);
1482 uc
->adj
= gtk_scrolled_window_get_vadjustment
1483 (GTK_SCROLLED_WINDOW(w
));
1485 gtk_widget_show(uc
->list
);
1486 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1487 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1488 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1489 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1492 * Adjust the height of the scrolled window to the
1493 * minimum given by the height parameter.
1495 * This piece of guesswork is a horrid hack based
1496 * on looking inside the GTK 1.2 sources
1497 * (specifically gtkviewport.c, which appears to be
1498 * the widget which provides the border around the
1499 * scrolling area). Anyone lets me know how I can
1500 * do this in a way which isn't at risk from GTK
1501 * upgrades, I'd be grateful.
1504 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1505 gtk_widget_set_usize(w
, 10,
1506 2*edge
+ (ctrl
->listbox
.height
*
1510 if (ctrl
->listbox
.draglist
) {
1512 * GTK doesn't appear to make it easy to
1513 * implement a proper draggable list; so
1514 * instead I'm just going to have to put an Up
1515 * and a Down button to the right of the actual
1516 * list box. Ah well.
1518 GtkWidget
*cols
, *button
;
1519 static const gint percentages
[2] = { 80, 20 };
1521 cols
= columns_new(4);
1522 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1523 columns_add(COLUMNS(cols
), w
, 0, 1);
1525 button
= gtk_button_new_with_label("Up");
1526 columns_add(COLUMNS(cols
), button
, 1, 1);
1527 gtk_widget_show(button
);
1528 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1529 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1530 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1531 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1532 button
= gtk_button_new_with_label("Down");
1533 columns_add(COLUMNS(cols
), button
, 1, 1);
1534 gtk_widget_show(button
);
1535 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1536 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1537 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1538 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1544 if (ctrl
->generic
.label
) {
1545 GtkWidget
*label
, *container
;
1547 label
= gtk_label_new(ctrl
->generic
.label
);
1549 container
= columns_new(4);
1550 if (ctrl
->listbox
.percentwidth
== 100) {
1551 columns_add(COLUMNS(container
), label
, 0, 1);
1552 columns_force_left_align(COLUMNS(container
), label
);
1553 columns_add(COLUMNS(container
), w
, 0, 1);
1555 gint percentages
[2];
1556 percentages
[1] = ctrl
->listbox
.percentwidth
;
1557 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1558 columns_set_cols(COLUMNS(container
), 2, percentages
);
1559 columns_add(COLUMNS(container
), label
, 0, 1);
1560 columns_force_left_align(COLUMNS(container
), label
);
1561 columns_add(COLUMNS(container
), w
, 1, 1);
1563 gtk_widget_show(label
);
1565 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1566 SHORTCUT_UCTRL
, uc
);
1572 * Wrapping text widgets don't sit well with the GTK
1573 * layout model, in which widgets state a minimum size
1574 * and the whole window then adjusts to the smallest
1575 * size it can sensibly take given its contents. A
1576 * wrapping text widget _has_ no clear minimum size;
1577 * instead it has a range of possibilities. It can be
1578 * one line deep but 2000 wide, or two lines deep and
1579 * 1000 pixels, or three by 867, or four by 500 and so
1580 * on. It can be as short as you like provided you
1581 * don't mind it being wide, or as narrow as you like
1582 * provided you don't mind it being tall.
1584 * Therefore, it fits very badly into the layout model.
1585 * Hence the only thing to do is pick a width and let
1586 * it choose its own number of lines. To do this I'm
1587 * going to cheat a little. All new wrapping text
1588 * widgets will be created with a minimal text content
1589 * "X"; then, after the rest of the dialog box is set
1590 * up and its size calculated, the text widgets will be
1591 * told their width and given their real text, which
1592 * will cause the size to be recomputed in the y
1593 * direction (because many of them will expand to more
1596 uc
->text
= w
= gtk_label_new("X");
1597 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1598 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1600 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1601 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1607 columns_add(cols
, w
,
1608 COLUMN_START(ctrl
->generic
.column
),
1609 COLUMN_SPAN(ctrl
->generic
.column
));
1611 columns_force_left_align(cols
, w
);
1615 dlg_add_uctrl(dp
, uc
);
1622 struct dlgparam
*dp
;
1624 GtkWidget
*panel
, *treeitem
;
1625 struct Shortcuts shortcuts
;
1628 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1630 struct selparam
*sp
= (struct selparam
*)data
;
1632 panels_switch_to(sp
->panels
, sp
->panel
);
1634 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1635 sp
->dp
->currtreeitem
= sp
->treeitem
;
1638 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1643 static int tree_grab_focus(struct dlgparam
*dp
)
1648 * See if any of the treeitems has the focus.
1651 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1652 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1660 gtk_widget_grab_focus(dp
->currtreeitem
);
1665 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1668 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1670 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1672 * If there's a focused treeitem, we return FALSE to cause the
1673 * focus to move on to some totally other control. If not, we
1674 * focus the selected one.
1676 return tree_grab_focus(dp
);
1679 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1681 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1683 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1684 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1688 if ((event
->state
& GDK_MOD1_MASK
) &&
1689 (unsigned char)event
->string
[0] > 0 &&
1690 (unsigned char)event
->string
[0] <= 127) {
1691 int schr
= (unsigned char)event
->string
[0];
1692 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1694 switch (sc
->action
) {
1696 tree_grab_focus(dp
);
1698 case SHORTCUT_FOCUS
:
1699 gtk_widget_grab_focus(sc
->widget
);
1701 case SHORTCUT_UCTRL
:
1703 * We must do something sensible with a uctrl.
1704 * Precisely what this is depends on the type of
1707 switch (sc
->uc
->ctrl
->generic
.type
) {
1710 /* Check boxes and buttons get the focus _and_ get toggled. */
1711 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1712 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1715 case CTRL_FILESELECT
:
1716 case CTRL_FONTSELECT
:
1717 /* File/font selectors have their buttons pressed (ooer),
1718 * and focus transferred to the edit box. */
1719 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1721 gtk_widget_grab_focus(sc
->uc
->entry
);
1725 * Radio buttons are fun, because they have
1726 * multiple shortcuts. We must find whether the
1727 * activated shortcut is the shortcut for the whole
1728 * group, or for a particular button. In the former
1729 * case, we find the currently selected button and
1730 * focus it; in the latter, we focus-and-click the
1731 * button whose shortcut was pressed.
1733 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1735 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1736 if (gtk_toggle_button_get_active
1737 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1738 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1740 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1742 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1743 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1744 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1745 gtk_signal_emit_by_name
1746 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1752 * If the list is really an option menu, we focus
1753 * and click it. Otherwise we tell it to focus one
1754 * of its children, which appears to do the Right
1757 if (sc
->uc
->optmenu
) {
1761 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1762 /* Option menus don't work using the "clicked" signal.
1763 * We need to manufacture a button press event :-/ */
1764 bev
.type
= GDK_BUTTON_PRESS
;
1766 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1767 "button_press_event",
1770 assert(sc
->uc
->list
!= NULL
);
1772 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1773 GTK_DIR_TAB_FORWARD
);
1784 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1786 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1788 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1789 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1791 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1792 if (widget
== dp
->treeitems
[i
])
1794 if (i
< dp
->ntreeitems
) {
1795 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1802 if (i
< 0 || i
>= dp
->ntreeitems
)
1803 break; /* nothing in that dir to select */
1805 * Determine if this tree item is visible.
1808 GtkWidget
*w
= dp
->treeitems
[i
];
1810 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1811 if (!GTK_WIDGET_VISIBLE(w
)) {
1818 j
= i
; /* got one */
1824 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1827 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1828 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1834 * It's nice for Left and Right to expand and collapse tree
1837 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1838 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1840 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1843 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1844 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1846 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1853 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1854 int chr
, int action
, void *ptr
)
1856 GtkLabel
*label
= GTK_LABEL(labelw
);
1857 gchar
*currstr
, *pattern
;
1860 if (chr
== NO_SHORTCUT
)
1863 chr
= tolower((unsigned char)chr
);
1865 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1867 scs
->sc
[chr
].action
= action
;
1869 if (action
== SHORTCUT_FOCUS
) {
1870 scs
->sc
[chr
].uc
= NULL
;
1871 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1873 scs
->sc
[chr
].widget
= NULL
;
1874 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1877 gtk_label_get(label
, &currstr
);
1878 for (i
= 0; currstr
[i
]; i
++)
1879 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1882 pattern
= dupprintf("%*s_", i
, "");
1884 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1885 gtk_label_set_pattern(label
, pattern
);
1886 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1893 int do_config_box(const char *title
, Config
*cfg
)
1895 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1896 *tree
, *treescroll
, *panels
, *panelvbox
;
1897 int index
, level
, listitemheight
;
1898 struct controlbox
*ctrlbox
;
1900 GtkTreeItem
*treeitemlevels
[8];
1901 GtkTree
*treelevels
[8];
1904 struct Shortcuts scs
;
1906 struct selparam
*selparams
= NULL
;
1907 int nselparams
= 0, selparamsize
= 0;
1909 do_defaults(NULL
, cfg
);
1914 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1916 gtk_widget_size_request(listitem
, &req
);
1917 listitemheight
= req
.height
;
1918 gtk_widget_unref(listitem
);
1923 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1924 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
1927 ctrlbox
= ctrl_new_box();
1928 setup_config_box(ctrlbox
, &sl
, FALSE
, 0);
1929 unix_setup_config_box(ctrlbox
, FALSE
);
1931 window
= gtk_dialog_new();
1932 gtk_window_set_title(GTK_WINDOW(window
), title
);
1933 hbox
= gtk_hbox_new(FALSE
, 4);
1934 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
1935 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
1936 gtk_widget_show(hbox
);
1937 vbox
= gtk_vbox_new(FALSE
, 4);
1938 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
1939 gtk_widget_show(vbox
);
1940 cols
= columns_new(4);
1941 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
1942 gtk_widget_show(cols
);
1943 label
= gtk_label_new("Category:");
1944 columns_add(COLUMNS(cols
), label
, 0, 1);
1945 columns_force_left_align(COLUMNS(cols
), label
);
1946 gtk_widget_show(label
);
1947 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
1948 tree
= gtk_tree_new();
1949 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
1950 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1951 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
1952 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
1953 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
1954 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
1956 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
1958 GTK_POLICY_AUTOMATIC
);
1959 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
1960 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
1961 gtk_widget_show(tree
);
1962 gtk_widget_show(treescroll
);
1963 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
1964 panels
= panels_new();
1965 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
1966 gtk_widget_show(panels
);
1971 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
1972 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
1975 if (!*s
->pathname
) {
1976 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
1977 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1980 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
1981 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
1983 GtkWidget
*treeitem
;
1987 * We expect never to find an implicit path
1988 * component. For example, we expect never to see
1989 * A/B/C followed by A/D/E, because that would
1990 * _implicitly_ create A/D. All our path prefixes
1991 * are expected to contain actual controls and be
1992 * selectable in the treeview; so we would expect
1993 * to see A/D _explicitly_ before encountering
1996 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
1998 c
= strrchr(s
->pathname
, '/');
2004 treeitem
= gtk_tree_item_new_with_label(c
);
2005 assert(j
-1 < level
);
2007 if (!treelevels
[j
-1]) {
2008 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2009 gtk_tree_item_set_subtree
2010 (treeitemlevels
[j
-1],
2011 GTK_WIDGET(treelevels
[j
-1]));
2012 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2014 gtk_tree_append(treelevels
[j
-1], treeitem
);
2016 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2018 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2019 treelevels
[j
] = NULL
;
2022 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2023 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2024 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2025 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2027 gtk_widget_show(treeitem
);
2031 first
= (panelvbox
== NULL
);
2033 panelvbox
= gtk_vbox_new(FALSE
, 4);
2034 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2036 panels_switch_to(PANELS(panels
), panelvbox
);
2037 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2040 if (nselparams
>= selparamsize
) {
2042 selparams
= sresize(selparams
, selparamsize
,
2045 selparams
[nselparams
].dp
= &dp
;
2046 selparams
[nselparams
].panels
= PANELS(panels
);
2047 selparams
[nselparams
].panel
= panelvbox
;
2048 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2049 selparams
[nselparams
].treeitem
= treeitem
;
2054 w
= layout_ctrls(&dp
,
2055 &selparams
[nselparams
-1].shortcuts
,
2056 s
, listitemheight
, NULL
);
2057 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2062 dp
.ntreeitems
= nselparams
;
2063 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2065 for (index
= 0; index
< nselparams
; index
++) {
2066 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2067 GTK_SIGNAL_FUNC(treeitem_sel
),
2069 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2073 dlg_refresh(NULL
, &dp
);
2075 dp
.shortcuts
= &selparams
[0].shortcuts
;
2076 dp
.currtreeitem
= dp
.treeitems
[0];
2077 dp
.lastfocus
= NULL
;
2081 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2082 gtk_widget_show(window
);
2085 * Set focus into the first available control.
2087 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2088 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2093 for (j
= 0; j
< s
->ncontrols
; j
++)
2094 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2095 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2096 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2097 dlg_set_focus(s
->ctrls
[j
], &dp
);
2098 dp
.lastfocus
= s
->ctrls
[j
];
2107 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2108 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2109 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2110 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2120 /* ======================================================================
2121 * Below here is a stub main program which allows the dialog box
2122 * code to be compiled and tested with a minimal amount of the rest
2128 /* Compile command for testing:
2130 gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \
2131 ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \
2132 -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs`
2136 void modalfatalbox(char *p
, ...)
2139 fprintf(stderr
, "FATAL ERROR: ");
2141 vfprintf(stderr
, p
, ap
);
2143 fputc('\n', stderr
);
2147 char *cp_name(int codepage
)
2149 return (codepage
== 123 ?
"testing123" :
2150 codepage
== 234 ?
"testing234" :
2151 codepage
== 345 ?
"testing345" :
2155 char *cp_enumerate(int index
)
2157 return (index
== 0 ?
"testing123" :
2158 index
== 1 ?
"testing234" :
2162 int decode_codepage(char *cp_name
)
2164 return (!strcmp(cp_name
, "testing123") ?
123 :
2165 !strcmp(cp_name
, "testing234") ?
234 :
2166 !strcmp(cp_name
, "testing345") ?
345 :
2170 struct printer_enum_tag
{ int dummy
; } printer_test
;
2172 printer_enum
*printer_start_enum(int *nprinters_ptr
) {
2174 return &printer_test
;
2176 char *printer_get_name(printer_enum
*pe
, int i
) {
2177 return (i
==0 ?
"lpr" : i
==1 ?
"lpr -Pfoobar" : NULL
);
2179 void printer_finish_enum(printer_enum
*pe
) { }
2181 char *platform_default_s(const char *name
)
2186 int platform_default_i(const char *name
, int def
)
2191 FontSpec
platform_default_fontspec(const char *name
)
2194 if (!strcmp(name
, "Font"))
2195 strcpy(ret
.name
, "fixed");
2201 Filename
platform_default_filename(const char *name
)
2204 if (!strcmp(name
, "LogFileName"))
2205 strcpy(ret
.path
, "putty.log");
2211 char *x_get_default(const char *key
)
2216 int main(int argc
, char **argv
)
2219 gtk_init(&argc
, &argv
);
2220 printf("returned %d\n", do_config_box("PuTTY Configuration", &cfg
));