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.
19 #include <gdk/gdkkeysyms.h>
22 #include <X11/Xutil.h>
28 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
43 struct Shortcut sc
[128];
50 int privdata_needs_free
;
51 GtkWidget
**buttons
; int nbuttons
; /* for radio buttons */
52 GtkWidget
*entry
; /* for editbox, combobox, filesel, fontsel */
53 GtkWidget
*button
; /* for filesel, fontsel */
54 GtkWidget
*list
; /* for combobox, listbox */
55 GtkWidget
*menu
; /* for optionmenu (==droplist) */
56 GtkWidget
*optmenu
; /* also for optionmenu */
57 GtkWidget
*text
; /* for text */
58 GtkAdjustment
*adj
; /* for the scrollbar in a list box */
63 tree234
*byctrl
, *bywidget
;
65 struct { unsigned char r
, g
, b
, ok
; } coloursel_result
; /* 0-255 */
66 /* `flags' are set to indicate when a GTK signal handler is being called
67 * due to automatic processing and should not flag a user event. */
69 struct Shortcuts
*shortcuts
;
70 GtkWidget
*window
, *cancelbutton
, *currtreeitem
, **treeitems
;
71 union control
*currfocus
, *lastfocus
;
75 #define FLAG_UPDATING_COMBO_LIST 1
77 enum { /* values for Shortcut.action */
78 SHORTCUT_EMPTY
, /* no shortcut on this key */
79 SHORTCUT_TREE
, /* focus a tree item */
80 SHORTCUT_FOCUS
, /* focus the supplied widget */
81 SHORTCUT_UCTRL
, /* do something sane with uctrl */
82 SHORTCUT_UCTRL_UP
, /* uctrl is a draglist, move Up */
83 SHORTCUT_UCTRL_DOWN
, /* uctrl is a draglist, move Down */
89 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
91 static void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
92 int chr
, int action
, void *ptr
);
93 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
95 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
97 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
99 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
100 static void coloursel_ok(GtkButton
*button
, gpointer data
);
101 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
102 static void window_destroy(GtkWidget
*widget
, gpointer data
);
104 static int uctrl_cmp_byctrl(void *av
, void *bv
)
106 struct uctrl
*a
= (struct uctrl
*)av
;
107 struct uctrl
*b
= (struct uctrl
*)bv
;
108 if (a
->ctrl
< b
->ctrl
)
110 else if (a
->ctrl
> b
->ctrl
)
115 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
117 union control
*a
= (union control
*)av
;
118 struct uctrl
*b
= (struct uctrl
*)bv
;
121 else if (a
> b
->ctrl
)
126 static int uctrl_cmp_bywidget(void *av
, void *bv
)
128 struct uctrl
*a
= (struct uctrl
*)av
;
129 struct uctrl
*b
= (struct uctrl
*)bv
;
130 if (a
->toplevel
< b
->toplevel
)
132 else if (a
->toplevel
> b
->toplevel
)
137 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
139 GtkWidget
*a
= (GtkWidget
*)av
;
140 struct uctrl
*b
= (struct uctrl
*)bv
;
143 else if (a
> b
->toplevel
)
148 static void dlg_init(struct dlgparam
*dp
)
150 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
151 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
152 dp
->coloursel_result
.ok
= FALSE
;
153 dp
->treeitems
= NULL
;
154 dp
->window
= dp
->cancelbutton
= dp
->currtreeitem
= NULL
;
156 dp
->currfocus
= NULL
;
159 static void dlg_cleanup(struct dlgparam
*dp
)
163 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
165 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
166 del234(dp
->bywidget
, uc
);
167 if (uc
->privdata_needs_free
)
172 freetree234(dp
->bywidget
);
174 sfree(dp
->treeitems
);
177 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
179 add234(dp
->byctrl
, uc
);
180 add234(dp
->bywidget
, uc
);
183 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
187 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
190 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
192 struct uctrl
*ret
= NULL
;
196 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
204 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
206 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
207 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
211 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
213 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
214 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
216 uc
->privdata_needs_free
= FALSE
;
219 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
221 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
222 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
224 * This is an internal allocation routine, so it's allowed to
225 * use smalloc directly.
227 uc
->privdata
= smalloc(size
);
228 uc
->privdata_needs_free
= FALSE
;
232 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
234 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
235 if (dp
->currfocus
!= ctrl
)
236 return dp
->currfocus
;
238 return dp
->lastfocus
;
241 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
243 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
244 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
245 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
246 assert(uc
->buttons
!= NULL
);
247 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
250 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
252 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
253 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
256 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
257 assert(uc
->buttons
!= NULL
);
258 for (i
= 0; i
< uc
->nbuttons
; i
++)
259 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
261 return 0; /* got to return something */
264 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
266 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
267 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
268 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
269 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
272 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
274 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
275 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
276 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
277 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
280 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
282 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
283 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
284 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
285 assert(uc
->entry
!= NULL
);
286 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
289 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
291 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
292 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
293 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
294 assert(uc
->entry
!= NULL
);
295 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
297 buffer
[length
-1] = '\0';
300 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
302 GtkContainer
*cont
= GTK_CONTAINER(data
);
303 /* gtk_container_remove will unref the widget for us; we need not. */
304 gtk_container_remove(cont
, w
);
307 /* The `listbox' functions can also apply to combo boxes. */
308 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
310 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
311 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
313 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
314 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
315 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
318 gtk_container_foreach(GTK_CONTAINER(uc
->menu
),
319 container_remove_and_destroy
,
320 GTK_CONTAINER(uc
->menu
));
322 gtk_list_clear_items(GTK_LIST(uc
->list
), 0, -1);
326 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
328 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
329 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
331 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
332 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
333 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
337 (GTK_CONTAINER(uc
->menu
),
338 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
340 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
344 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
346 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
350 * Each listbox entry may have a numeric id associated with it.
351 * Note that some front ends only permit a string to be stored at
352 * each position, which means that _if_ you put two identical
353 * strings in any listbox then you MUST not assign them different
354 * IDs and expect to get meaningful results back.
356 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
357 char const *text
, int id
)
359 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
360 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
362 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
363 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
364 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
366 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
370 * List item in a drop-down (but non-combo) list. Tabs are
371 * ignored; we just provide a standard menu item with the
374 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
376 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
377 gtk_widget_show(menuitem
);
379 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
380 GINT_TO_POINTER(id
));
381 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
382 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
383 } else if (!uc
->entry
) {
385 * List item in a non-combo-box list box. We make all of
386 * these Columns containing GtkLabels. This allows us to do
387 * the nasty force_left hack irrespective of whether there
388 * are tabs in the thing.
390 GtkWidget
*listitem
= gtk_list_item_new();
391 GtkWidget
*cols
= columns_new(10);
395 /* Count the tabs in the text, and hence determine # of columns. */
397 for (i
= 0; text
[i
]; i
++)
402 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
403 percents
= snewn(ncols
, gint
);
404 percents
[ncols
-1] = 100;
405 for (i
= 0; i
< ncols
-1; i
++) {
406 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
407 percents
[ncols
-1] -= percents
[i
];
409 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
412 for (i
= 0; i
< ncols
; i
++) {
413 int len
= strcspn(text
, "\t");
414 char *dup
= dupprintf("%.*s", len
, text
);
419 label
= gtk_label_new(dup
);
422 columns_add(COLUMNS(cols
), label
, i
, 1);
423 columns_force_left_align(COLUMNS(cols
), label
);
424 gtk_widget_show(label
);
426 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
427 gtk_widget_show(cols
);
428 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
429 gtk_widget_show(listitem
);
431 if (ctrl
->listbox
.multisel
) {
432 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
433 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
435 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
436 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
438 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
439 GTK_SIGNAL_FUNC(widget_focus
), dp
);
440 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
441 GTK_SIGNAL_FUNC(listitem_button
), dp
);
442 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
443 GINT_TO_POINTER(id
));
446 * List item in a combo-box list, which means the sensible
447 * thing to do is make it a perfectly normal label. Hence
448 * tabs are disregarded.
450 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
452 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
453 gtk_widget_show(listitem
);
455 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
456 GINT_TO_POINTER(id
));
459 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
462 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
464 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
465 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
469 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
470 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
471 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
473 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
475 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
476 g_list_free(children
);
478 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
), "user-data"));
481 /* dlg_listbox_index returns <0 if no single element is selected. */
482 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
484 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
485 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
487 GtkWidget
*item
, *activeitem
;
491 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
492 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
493 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
496 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
498 activeitem
= NULL
; /* unnecessarily placate gcc */
500 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
502 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
503 i
++, children
= children
->next
) {
504 if (uc
->menu ? activeitem
== item
:
505 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
512 g_list_free(children
);
513 return selected
< 0 ?
-1 : selected
;
516 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
518 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
519 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
521 GtkWidget
*item
, *activeitem
;
523 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
524 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
525 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
527 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
529 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
530 g_list_free(children
);
533 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
534 return item
== activeitem
;
536 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
540 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
542 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
543 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
545 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
546 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
547 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
550 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
554 gdouble newtop
, newbot
;
556 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
559 * Scroll the list box if necessary to ensure the newly
560 * selected item is visible.
562 items
= gtk_container_children(GTK_CONTAINER(uc
->list
));
563 nitems
= g_list_length(items
);
565 int modified
= FALSE
;
567 newtop
= uc
->adj
->lower
+
568 (uc
->adj
->upper
- uc
->adj
->lower
) * index
/ nitems
;
569 newbot
= uc
->adj
->lower
+
570 (uc
->adj
->upper
- uc
->adj
->lower
) * (index
+1) / nitems
;
571 if (uc
->adj
->value
> newtop
) {
573 uc
->adj
->value
= newtop
;
574 } else if (uc
->adj
->value
< newbot
- uc
->adj
->page_size
) {
576 uc
->adj
->value
= newbot
- uc
->adj
->page_size
;
579 gtk_adjustment_value_changed(uc
->adj
);
584 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
586 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
587 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
589 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
590 assert(uc
->text
!= NULL
);
592 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
595 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
597 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
598 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
599 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
600 assert(uc
->entry
!= NULL
);
601 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
604 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
606 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
607 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
608 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
609 assert(uc
->entry
!= NULL
);
610 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
612 fn
->path
[lenof(fn
->path
)-1] = '\0';
615 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
617 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
618 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
619 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
620 assert(uc
->entry
!= NULL
);
621 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
624 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
626 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
627 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
628 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
629 assert(uc
->entry
!= NULL
);
630 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
632 fs
->name
[lenof(fs
->name
)-1] = '\0';
636 * Bracketing a large set of updates in these two functions will
637 * cause the front end (if possible) to delay updating the screen
638 * until it's all complete, thus avoiding flicker.
640 void dlg_update_start(union control
*ctrl
, void *dlg
)
643 * Apparently we can't do this at all in GTK. GtkCList supports
644 * freeze and thaw, but not GtkList. Bah.
648 void dlg_update_done(union control
*ctrl
, void *dlg
)
651 * Apparently we can't do this at all in GTK. GtkCList supports
652 * freeze and thaw, but not GtkList. Bah.
656 void dlg_set_focus(union control
*ctrl
, void *dlg
)
658 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
659 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
661 switch (ctrl
->generic
.type
) {
664 /* Check boxes and buttons get the focus _and_ get toggled. */
665 gtk_widget_grab_focus(uc
->toplevel
);
667 case CTRL_FILESELECT
:
668 case CTRL_FONTSELECT
:
670 /* Anything containing an edit box gets that focused. */
671 gtk_widget_grab_focus(uc
->entry
);
675 * Radio buttons: we find the currently selected button and
680 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
681 if (gtk_toggle_button_get_active
682 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
683 gtk_widget_grab_focus(uc
->buttons
[i
]);
689 * If the list is really an option menu, we focus it.
690 * Otherwise we tell it to focus one of its children, which
691 * appears to do the Right Thing.
694 gtk_widget_grab_focus(uc
->optmenu
);
696 assert(uc
->list
!= NULL
);
697 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
704 * During event processing, you might well want to give an error
705 * indication to the user. dlg_beep() is a quick and easy generic
706 * error; dlg_error() puts up a message-box or equivalent.
708 void dlg_beep(void *dlg
)
713 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
715 gtk_widget_destroy(GTK_WIDGET(data
));
718 static void set_transient_window_pos(GtkWidget
*parent
, GtkWidget
*child
)
720 gint x
, y
, w
, h
, dx
, dy
;
722 gtk_window_set_position(GTK_WINDOW(child
), GTK_WIN_POS_NONE
);
723 gtk_widget_size_request(GTK_WIDGET(child
), &req
);
725 gdk_window_get_origin(GTK_WIDGET(parent
)->window
, &x
, &y
);
726 gdk_window_get_size(GTK_WIDGET(parent
)->window
, &w
, &h
);
729 * One corner of the transient will be offset inwards, by 1/4
730 * of the parent window's size, from the corresponding corner
731 * of the parent window. The corner will be chosen so as to
732 * place the transient closer to the centre of the screen; this
733 * should avoid transients going off the edge of the screen on
736 if (x
+ w
/2 < gdk_screen_width() / 2)
737 dx
= x
+ w
/4; /* work from left edges */
739 dx
= x
+ 3*w
/4 - req
.width
; /* work from right edges */
740 if (y
+ h
/2 < gdk_screen_height() / 2)
741 dy
= y
+ h
/4; /* work from top edges */
743 dy
= y
+ 3*h
/4 - req
.height
; /* work from bottom edges */
744 gtk_widget_set_uposition(GTK_WIDGET(child
), dx
, dy
);
747 void dlg_error_msg(void *dlg
, char *msg
)
749 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
750 GtkWidget
*window
, *hbox
, *text
, *ok
;
752 window
= gtk_dialog_new();
753 text
= gtk_label_new(msg
);
754 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
755 hbox
= gtk_hbox_new(FALSE
, 0);
756 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
757 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
758 hbox
, FALSE
, FALSE
, 20);
759 gtk_widget_show(text
);
760 gtk_widget_show(hbox
);
761 gtk_window_set_title(GTK_WINDOW(window
), "Error");
762 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
763 ok
= gtk_button_new_with_label("OK");
764 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
765 ok
, FALSE
, FALSE
, 0);
767 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
768 gtk_window_set_default(GTK_WINDOW(window
), ok
);
769 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
770 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
771 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
772 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
773 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
774 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
775 set_transient_window_pos(dp
->window
, window
);
776 gtk_widget_show(window
);
781 * This function signals to the front end that the dialog's
782 * processing is completed, and passes an integer value (typically
785 void dlg_end(void *dlg
, int value
)
787 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
789 gtk_widget_destroy(dp
->window
);
792 void dlg_refresh(union control
*ctrl
, void *dlg
)
794 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
798 if (ctrl
->generic
.handler
!= NULL
)
799 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
803 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
804 assert(uc
->ctrl
!= NULL
);
805 if (uc
->ctrl
->generic
.handler
!= NULL
)
806 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
807 dp
->data
, EVENT_REFRESH
);
812 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
814 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
815 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
818 GtkWidget
*coloursel
=
819 gtk_color_selection_dialog_new("Select a colour");
820 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
822 dp
->coloursel_result
.ok
= FALSE
;
824 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
825 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
826 cvals
[0] = r
/ 255.0;
827 cvals
[1] = g
/ 255.0;
828 cvals
[2] = b
/ 255.0;
829 cvals
[3] = 1.0; /* fully opaque! */
830 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
832 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
833 (gpointer
)coloursel
);
834 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
835 (gpointer
)coloursel
);
836 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
837 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
838 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
839 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
840 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
841 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
842 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
843 (gpointer
)coloursel
);
844 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
845 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
846 (gpointer
)coloursel
);
847 gtk_widget_show(coloursel
);
850 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
851 int *r
, int *g
, int *b
)
853 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
854 if (dp
->coloursel_result
.ok
) {
855 *r
= dp
->coloursel_result
.r
;
856 *g
= dp
->coloursel_result
.g
;
857 *b
= dp
->coloursel_result
.b
;
863 /* ----------------------------------------------------------------------
864 * Signal handlers while the dialog box is active.
867 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
870 struct dlgparam
*dp
= (struct dlgparam
*)data
;
871 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
872 union control
*focus
;
879 if (focus
!= dp
->currfocus
) {
880 dp
->lastfocus
= dp
->currfocus
;
881 dp
->currfocus
= focus
;
887 static void button_clicked(GtkButton
*button
, gpointer data
)
889 struct dlgparam
*dp
= (struct dlgparam
*)data
;
890 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
891 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
894 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
896 struct dlgparam
*dp
= (struct dlgparam
*)data
;
897 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
898 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
901 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
904 * GtkEntry has a nasty habit of eating the Return key, which
905 * is unhelpful since it doesn't actually _do_ anything with it
906 * (it calls gtk_widget_activate, but our edit boxes never need
907 * activating). So I catch Return before GtkEntry sees it, and
908 * pass it straight on to the parent widget. Effect: hitting
909 * Return in an edit box will now activate the default button
910 * in the dialog just like it will everywhere else.
912 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
914 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
915 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
922 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
924 struct dlgparam
*dp
= (struct dlgparam
*)data
;
925 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
926 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
927 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
931 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
934 struct dlgparam
*dp
= (struct dlgparam
*)data
;
935 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
936 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
939 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
942 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
944 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
945 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
946 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
947 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
949 * Up, Down, PgUp or PgDn have been pressed on a ListItem
950 * in a list box. So, if the list box is single-selection:
952 * - if the list item in question isn't already selected,
953 * we simply select it.
954 * - otherwise, we find the next one (or next
955 * however-far-away) in whichever direction we're going,
957 * + in this case, we must also fiddle with the
958 * scrollbar to ensure the newly selected item is
961 * If it's multiple-selection, we do all of the above
962 * except actually selecting anything, so we move the focus
963 * and fiddle the scrollbar to follow it.
965 GtkWidget
*list
= item
->parent
;
967 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
970 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
971 gtk_list_select_child(GTK_LIST(list
), item
);
974 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
975 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
978 (event
->keyval
==GDK_Page_Down
||
979 event
->keyval
==GDK_KP_Page_Down
||
980 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
983 GList
*children
, *chead
;
985 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
987 n
= g_list_length(children
);
991 * Figure out how many list items to a screenful,
992 * and adjust the step appropriately.
994 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
995 step
--; /* go by one less than that */
999 while (children
!= NULL
) {
1000 if (item
== children
->data
)
1002 children
= children
->next
;
1007 if (direction
< 0 && i
> 0)
1008 children
= children
->prev
, i
--;
1009 else if (direction
> 0 && i
< n
-1)
1010 children
= children
->next
, i
++;
1014 if (children
&& children
->data
) {
1016 gtk_list_select_child(GTK_LIST(list
),
1017 GTK_WIDGET(children
->data
));
1018 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
1019 gtk_adjustment_clamp_page
1021 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
1022 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
1033 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
1036 return listitem_key(item
, event
, data
, FALSE
);
1039 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
1042 return listitem_key(item
, event
, data
, TRUE
);
1045 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
1048 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1049 if (event
->type
== GDK_2BUTTON_PRESS
||
1050 event
->type
== GDK_3BUTTON_PRESS
) {
1051 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1052 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1058 static void list_selchange(GtkList
*list
, gpointer data
)
1060 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1061 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1063 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1066 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1068 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1069 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1070 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1071 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1072 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1075 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1077 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1078 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1082 (index
== 0 && direction
< 0) ||
1083 (index
== g_list_length(children
)-1 && direction
> 0)) {
1088 child
= g_list_nth_data(children
, index
);
1089 gtk_widget_ref(child
);
1090 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1091 g_list_free(children
);
1094 children
= g_list_append(children
, child
);
1095 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1096 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1097 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1100 static void draglist_up(GtkButton
*button
, gpointer data
)
1102 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1103 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1104 draglist_move(dp
, uc
, -1);
1107 static void draglist_down(GtkButton
*button
, gpointer data
)
1109 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1110 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1111 draglist_move(dp
, uc
, +1);
1114 static void filesel_ok(GtkButton
*button
, gpointer data
)
1116 /* struct dlgparam *dp = (struct dlgparam *)data; */
1117 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1118 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1119 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1120 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1123 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1125 /* struct dlgparam *dp = (struct dlgparam *)data; */
1126 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1127 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1128 char *name
= gtk_font_selection_dialog_get_font_name
1129 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1130 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1133 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1135 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1136 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1137 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1139 gtk_color_selection_get_color
1140 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1142 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1143 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1144 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1145 dp
->coloursel_result
.ok
= TRUE
;
1146 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1149 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1151 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1152 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1153 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1154 dp
->coloursel_result
.ok
= FALSE
;
1155 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1158 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1160 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1161 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1163 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1164 GtkWidget
*filesel
=
1165 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1166 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1168 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1170 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1172 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1173 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1174 gtk_signal_connect_object
1175 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1176 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1177 gtk_signal_connect_object
1178 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1179 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1180 gtk_widget_show(filesel
);
1183 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1184 gchar
*spacings
[] = { "c", "m", NULL
};
1185 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1186 GtkWidget
*fontsel
=
1187 gtk_font_selection_dialog_new("Select a font");
1188 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1189 gtk_font_selection_dialog_set_filter
1190 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1191 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1192 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1193 if (!gtk_font_selection_dialog_set_font_name
1194 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1196 * If the font name wasn't found as it was, try opening
1197 * it and extracting its FONT property. This should
1198 * have the effect of mapping short aliases into true
1201 GdkFont
*font
= gdk_font_load(fontname
);
1203 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1204 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1205 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1207 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1208 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1210 gtk_font_selection_dialog_set_font_name
1211 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1213 gdk_font_unref(font
);
1217 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1218 "user-data", (gpointer
)fontsel
);
1219 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1221 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1222 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1223 gtk_signal_connect_object
1224 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1225 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1227 gtk_signal_connect_object
1228 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1229 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1231 gtk_widget_show(fontsel
);
1235 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1238 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1239 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1241 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1242 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1243 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1246 /* ----------------------------------------------------------------------
1247 * This function does the main layout work: it reads a controlset,
1248 * it creates the relevant GTK controls, and returns a GtkWidget
1249 * containing the result. (This widget might be a title of some
1250 * sort, it might be a Columns containing many controls, or it
1251 * might be a GtkFrame containing a Columns; whatever it is, it's
1252 * definitely a GtkWidget and should probably be added to a
1255 * `listitemheight' is used to calculate a usize for list boxes: it
1256 * should be the height from the size request of a GtkListItem.
1258 * `win' is required for setting the default button. If it is
1259 * non-NULL, all buttons created will be default-capable (so they
1260 * have extra space round them for the default highlight).
1262 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1263 struct controlset
*s
, int listitemheight
,
1270 if (!s
->boxname
&& s
->boxtitle
) {
1271 /* This controlset is a panel title. */
1272 return gtk_label_new(s
->boxtitle
);
1276 * Otherwise, we expect to be laying out actual controls, so
1277 * we'll start by creating a Columns for the purpose.
1279 cols
= COLUMNS(columns_new(4));
1280 ret
= GTK_WIDGET(cols
);
1281 gtk_widget_show(ret
);
1284 * Create a containing frame if we have a box name.
1287 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1288 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1289 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1290 gtk_widget_show(ret
);
1294 * Now iterate through the controls themselves, create them,
1295 * and add them to the Columns.
1297 for (i
= 0; i
< s
->ncontrols
; i
++) {
1298 union control
*ctrl
= s
->ctrls
[i
];
1301 GtkWidget
*w
= NULL
;
1303 switch (ctrl
->generic
.type
) {
1306 static const int simplecols
[1] = { 100 };
1307 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1308 (ctrl
->columns
.percentages ?
1309 ctrl
->columns
.percentages
: simplecols
));
1311 continue; /* no actual control created */
1314 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1316 columns_taborder_last(cols
, uc
->toplevel
);
1318 continue; /* no actual control created */
1321 uc
= snew(struct uctrl
);
1323 uc
->privdata
= NULL
;
1324 uc
->privdata_needs_free
= FALSE
;
1326 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1327 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1329 switch (ctrl
->generic
.type
) {
1331 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1333 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1334 if (ctrl
->button
.isdefault
)
1335 gtk_window_set_default(win
, w
);
1336 if (ctrl
->button
.iscancel
)
1337 dp
->cancelbutton
= w
;
1339 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1340 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1341 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1342 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1343 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1344 SHORTCUT_UCTRL
, uc
);
1347 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1348 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1349 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1350 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1351 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1352 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1353 SHORTCUT_UCTRL
, uc
);
1358 * Radio buttons get to go inside their own Columns, no
1362 gint i
, *percentages
;
1366 if (ctrl
->generic
.label
) {
1367 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1368 columns_add(COLUMNS(w
), label
, 0, 1);
1369 columns_force_left_align(COLUMNS(w
), label
);
1370 gtk_widget_show(label
);
1371 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1372 SHORTCUT_UCTRL
, uc
);
1374 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1375 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1377 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1378 100 * i
/ ctrl
->radio
.ncolumns
);
1380 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1382 g_free(percentages
);
1385 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1386 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1388 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1392 b
= (gtk_radio_button_new_with_label
1393 (group
, ctrl
->radio
.buttons
[i
]));
1395 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1396 colstart
= i
% ctrl
->radio
.ncolumns
;
1397 columns_add(COLUMNS(w
), b
, colstart
,
1398 (i
== ctrl
->radio
.nbuttons
-1 ?
1399 ctrl
->radio
.ncolumns
- colstart
: 1));
1400 columns_force_left_align(COLUMNS(w
), b
);
1402 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1403 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1404 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1405 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1406 if (ctrl
->radio
.shortcuts
) {
1407 shortcut_add(scs
, GTK_BIN(b
)->child
,
1408 ctrl
->radio
.shortcuts
[i
],
1409 SHORTCUT_UCTRL
, uc
);
1418 if (ctrl
->editbox
.has_list
) {
1419 w
= gtk_combo_new();
1420 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1421 uc
->entry
= GTK_COMBO(w
)->entry
;
1422 uc
->list
= GTK_COMBO(w
)->list
;
1424 w
= gtk_entry_new();
1425 if (ctrl
->editbox
.password
)
1426 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1429 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1430 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1431 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1432 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1433 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1434 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1436 * Edit boxes, for some strange reason, have a minimum
1437 * width of 150 in GTK 1.2. We don't want this - we'd
1438 * rather the edit boxes acquired their natural width
1439 * from the column layout of the rest of the box.
1441 * Also, while we're here, we'll squirrel away the
1442 * edit box height so we can use that to centre its
1443 * label vertically beside it.
1445 gtk_widget_size_request(w
, &req
);
1446 gtk_widget_set_usize(w
, 10, req
.height
);
1448 if (ctrl
->generic
.label
) {
1449 GtkWidget
*label
, *container
;
1451 label
= gtk_label_new(ctrl
->generic
.label
);
1453 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1454 SHORTCUT_FOCUS
, uc
->entry
);
1456 container
= columns_new(4);
1457 if (ctrl
->editbox
.percentwidth
== 100) {
1458 columns_add(COLUMNS(container
), label
, 0, 1);
1459 columns_force_left_align(COLUMNS(container
), label
);
1460 columns_add(COLUMNS(container
), w
, 0, 1);
1462 gint percentages
[2];
1463 percentages
[1] = ctrl
->editbox
.percentwidth
;
1464 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1465 columns_set_cols(COLUMNS(container
), 2, percentages
);
1466 columns_add(COLUMNS(container
), label
, 0, 1);
1467 columns_force_left_align(COLUMNS(container
), label
);
1468 columns_add(COLUMNS(container
), w
, 1, 1);
1469 /* Centre the label vertically. */
1470 gtk_widget_set_usize(label
, -1, req
.height
);
1471 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1473 gtk_widget_show(label
);
1478 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1479 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1482 case CTRL_FILESELECT
:
1483 case CTRL_FONTSELECT
:
1488 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1489 "Browse..." : "Change...");
1491 gint percentages
[] = { 75, 25 };
1493 columns_set_cols(COLUMNS(w
), 2, percentages
);
1495 if (ctrl
->generic
.label
) {
1496 ww
= gtk_label_new(ctrl
->generic
.label
);
1497 columns_add(COLUMNS(w
), ww
, 0, 2);
1498 columns_force_left_align(COLUMNS(w
), ww
);
1499 gtk_widget_show(ww
);
1500 shortcut_add(scs
, ww
,
1501 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1502 ctrl
->fileselect
.shortcut
:
1503 ctrl
->fontselect
.shortcut
),
1504 SHORTCUT_UCTRL
, uc
);
1507 uc
->entry
= ww
= gtk_entry_new();
1508 gtk_widget_size_request(ww
, &req
);
1509 gtk_widget_set_usize(ww
, 10, req
.height
);
1510 columns_add(COLUMNS(w
), ww
, 0, 1);
1511 gtk_widget_show(ww
);
1513 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1514 columns_add(COLUMNS(w
), ww
, 1, 1);
1515 gtk_widget_show(ww
);
1517 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1518 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1519 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1520 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1521 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1522 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1523 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1524 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1525 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1526 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1530 if (ctrl
->listbox
.height
== 0) {
1531 uc
->optmenu
= w
= gtk_option_menu_new();
1532 uc
->menu
= gtk_menu_new();
1533 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1534 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1535 (gpointer
)uc
->optmenu
);
1536 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1537 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1539 uc
->list
= gtk_list_new();
1540 if (ctrl
->listbox
.multisel
== 2) {
1541 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1542 GTK_SELECTION_EXTENDED
);
1543 } else if (ctrl
->listbox
.multisel
== 1) {
1544 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1545 GTK_SELECTION_MULTIPLE
);
1547 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1548 GTK_SELECTION_SINGLE
);
1550 w
= gtk_scrolled_window_new(NULL
, NULL
);
1551 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1553 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1555 GTK_POLICY_AUTOMATIC
);
1556 uc
->adj
= gtk_scrolled_window_get_vadjustment
1557 (GTK_SCROLLED_WINDOW(w
));
1559 gtk_widget_show(uc
->list
);
1560 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1561 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1562 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1563 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1566 * Adjust the height of the scrolled window to the
1567 * minimum given by the height parameter.
1569 * This piece of guesswork is a horrid hack based
1570 * on looking inside the GTK 1.2 sources
1571 * (specifically gtkviewport.c, which appears to be
1572 * the widget which provides the border around the
1573 * scrolling area). Anyone lets me know how I can
1574 * do this in a way which isn't at risk from GTK
1575 * upgrades, I'd be grateful.
1578 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1579 gtk_widget_set_usize(w
, 10,
1580 2*edge
+ (ctrl
->listbox
.height
*
1584 if (ctrl
->listbox
.draglist
) {
1586 * GTK doesn't appear to make it easy to
1587 * implement a proper draggable list; so
1588 * instead I'm just going to have to put an Up
1589 * and a Down button to the right of the actual
1590 * list box. Ah well.
1592 GtkWidget
*cols
, *button
;
1593 static const gint percentages
[2] = { 80, 20 };
1595 cols
= columns_new(4);
1596 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1597 columns_add(COLUMNS(cols
), w
, 0, 1);
1599 button
= gtk_button_new_with_label("Up");
1600 columns_add(COLUMNS(cols
), button
, 1, 1);
1601 gtk_widget_show(button
);
1602 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1603 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1604 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1605 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1606 button
= gtk_button_new_with_label("Down");
1607 columns_add(COLUMNS(cols
), button
, 1, 1);
1608 gtk_widget_show(button
);
1609 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1610 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1611 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1612 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1618 if (ctrl
->generic
.label
) {
1619 GtkWidget
*label
, *container
;
1621 label
= gtk_label_new(ctrl
->generic
.label
);
1623 container
= columns_new(4);
1624 if (ctrl
->listbox
.percentwidth
== 100) {
1625 columns_add(COLUMNS(container
), label
, 0, 1);
1626 columns_force_left_align(COLUMNS(container
), label
);
1627 columns_add(COLUMNS(container
), w
, 0, 1);
1629 gint percentages
[2];
1630 percentages
[1] = ctrl
->listbox
.percentwidth
;
1631 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1632 columns_set_cols(COLUMNS(container
), 2, percentages
);
1633 columns_add(COLUMNS(container
), label
, 0, 1);
1634 columns_force_left_align(COLUMNS(container
), label
);
1635 columns_add(COLUMNS(container
), w
, 1, 1);
1637 gtk_widget_show(label
);
1639 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1640 SHORTCUT_UCTRL
, uc
);
1646 * Wrapping text widgets don't sit well with the GTK
1647 * layout model, in which widgets state a minimum size
1648 * and the whole window then adjusts to the smallest
1649 * size it can sensibly take given its contents. A
1650 * wrapping text widget _has_ no clear minimum size;
1651 * instead it has a range of possibilities. It can be
1652 * one line deep but 2000 wide, or two lines deep and
1653 * 1000 pixels, or three by 867, or four by 500 and so
1654 * on. It can be as short as you like provided you
1655 * don't mind it being wide, or as narrow as you like
1656 * provided you don't mind it being tall.
1658 * Therefore, it fits very badly into the layout model.
1659 * Hence the only thing to do is pick a width and let
1660 * it choose its own number of lines. To do this I'm
1661 * going to cheat a little. All new wrapping text
1662 * widgets will be created with a minimal text content
1663 * "X"; then, after the rest of the dialog box is set
1664 * up and its size calculated, the text widgets will be
1665 * told their width and given their real text, which
1666 * will cause the size to be recomputed in the y
1667 * direction (because many of them will expand to more
1670 uc
->text
= w
= gtk_label_new("X");
1671 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1672 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1674 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1675 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1681 columns_add(cols
, w
,
1682 COLUMN_START(ctrl
->generic
.column
),
1683 COLUMN_SPAN(ctrl
->generic
.column
));
1685 columns_force_left_align(cols
, w
);
1689 dlg_add_uctrl(dp
, uc
);
1696 struct dlgparam
*dp
;
1698 GtkWidget
*panel
, *treeitem
;
1699 struct Shortcuts shortcuts
;
1702 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1704 struct selparam
*sp
= (struct selparam
*)data
;
1706 panels_switch_to(sp
->panels
, sp
->panel
);
1708 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1709 sp
->dp
->currtreeitem
= sp
->treeitem
;
1712 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1717 static int tree_grab_focus(struct dlgparam
*dp
)
1722 * See if any of the treeitems has the focus.
1725 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1726 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1734 gtk_widget_grab_focus(dp
->currtreeitem
);
1739 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1742 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1744 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1746 * If there's a focused treeitem, we return FALSE to cause the
1747 * focus to move on to some totally other control. If not, we
1748 * focus the selected one.
1750 return tree_grab_focus(dp
);
1753 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1755 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1757 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1758 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1762 if ((event
->state
& GDK_MOD1_MASK
) &&
1763 (unsigned char)event
->string
[0] > 0 &&
1764 (unsigned char)event
->string
[0] <= 127) {
1765 int schr
= (unsigned char)event
->string
[0];
1766 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1768 switch (sc
->action
) {
1770 tree_grab_focus(dp
);
1772 case SHORTCUT_FOCUS
:
1773 gtk_widget_grab_focus(sc
->widget
);
1775 case SHORTCUT_UCTRL
:
1777 * We must do something sensible with a uctrl.
1778 * Precisely what this is depends on the type of
1781 switch (sc
->uc
->ctrl
->generic
.type
) {
1784 /* Check boxes and buttons get the focus _and_ get toggled. */
1785 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1786 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1789 case CTRL_FILESELECT
:
1790 case CTRL_FONTSELECT
:
1791 /* File/font selectors have their buttons pressed (ooer),
1792 * and focus transferred to the edit box. */
1793 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1795 gtk_widget_grab_focus(sc
->uc
->entry
);
1799 * Radio buttons are fun, because they have
1800 * multiple shortcuts. We must find whether the
1801 * activated shortcut is the shortcut for the whole
1802 * group, or for a particular button. In the former
1803 * case, we find the currently selected button and
1804 * focus it; in the latter, we focus-and-click the
1805 * button whose shortcut was pressed.
1807 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1809 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1810 if (gtk_toggle_button_get_active
1811 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1812 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1814 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1816 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1817 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1818 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1819 gtk_signal_emit_by_name
1820 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1826 * If the list is really an option menu, we focus
1827 * and click it. Otherwise we tell it to focus one
1828 * of its children, which appears to do the Right
1831 if (sc
->uc
->optmenu
) {
1835 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1836 /* Option menus don't work using the "clicked" signal.
1837 * We need to manufacture a button press event :-/ */
1838 bev
.type
= GDK_BUTTON_PRESS
;
1840 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1841 "button_press_event",
1844 assert(sc
->uc
->list
!= NULL
);
1846 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1847 GTK_DIR_TAB_FORWARD
);
1858 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1860 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1862 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1863 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1865 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1866 if (widget
== dp
->treeitems
[i
])
1868 if (i
< dp
->ntreeitems
) {
1869 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1876 if (i
< 0 || i
>= dp
->ntreeitems
)
1877 break; /* nothing in that dir to select */
1879 * Determine if this tree item is visible.
1882 GtkWidget
*w
= dp
->treeitems
[i
];
1884 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1885 if (!GTK_WIDGET_VISIBLE(w
)) {
1892 j
= i
; /* got one */
1898 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1901 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1902 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1908 * It's nice for Left and Right to expand and collapse tree
1911 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1912 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1914 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1917 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1918 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1920 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1927 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1928 int chr
, int action
, void *ptr
)
1930 GtkLabel
*label
= GTK_LABEL(labelw
);
1931 gchar
*currstr
, *pattern
;
1934 if (chr
== NO_SHORTCUT
)
1937 chr
= tolower((unsigned char)chr
);
1939 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1941 scs
->sc
[chr
].action
= action
;
1943 if (action
== SHORTCUT_FOCUS
) {
1944 scs
->sc
[chr
].uc
= NULL
;
1945 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1947 scs
->sc
[chr
].widget
= NULL
;
1948 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1951 gtk_label_get(label
, &currstr
);
1952 for (i
= 0; currstr
[i
]; i
++)
1953 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1956 pattern
= dupprintf("%*s_", i
, "");
1958 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1959 gtk_label_set_pattern(label
, pattern
);
1960 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1967 int get_listitemheight(void)
1969 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1971 gtk_widget_size_request(listitem
, &req
);
1972 gtk_widget_unref(listitem
);
1976 int do_config_box(const char *title
, Config
*cfg
, int midsession
,
1979 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1980 *tree
, *treescroll
, *panels
, *panelvbox
;
1981 int index
, level
, listitemheight
;
1982 struct controlbox
*ctrlbox
;
1984 GtkTreeItem
*treeitemlevels
[8];
1985 GtkTree
*treelevels
[8];
1987 struct Shortcuts scs
;
1989 struct selparam
*selparams
= NULL
;
1990 int nselparams
= 0, selparamsize
= 0;
1994 listitemheight
= get_listitemheight();
1996 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1997 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2000 window
= gtk_dialog_new();
2002 ctrlbox
= ctrl_new_box();
2003 setup_config_box(ctrlbox
, midsession
, cfg
->protocol
, protcfginfo
);
2004 unix_setup_config_box(ctrlbox
, midsession
);
2005 gtk_setup_config_box(ctrlbox
, midsession
, window
);
2007 gtk_window_set_title(GTK_WINDOW(window
), title
);
2008 hbox
= gtk_hbox_new(FALSE
, 4);
2009 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
2010 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
2011 gtk_widget_show(hbox
);
2012 vbox
= gtk_vbox_new(FALSE
, 4);
2013 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
2014 gtk_widget_show(vbox
);
2015 cols
= columns_new(4);
2016 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
2017 gtk_widget_show(cols
);
2018 label
= gtk_label_new("Category:");
2019 columns_add(COLUMNS(cols
), label
, 0, 1);
2020 columns_force_left_align(COLUMNS(cols
), label
);
2021 gtk_widget_show(label
);
2022 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
2023 tree
= gtk_tree_new();
2024 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
2025 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2026 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
2027 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
2028 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
2029 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
2031 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
2033 GTK_POLICY_AUTOMATIC
);
2034 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
2035 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
2036 gtk_widget_show(tree
);
2037 gtk_widget_show(treescroll
);
2038 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
2039 panels
= panels_new();
2040 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
2041 gtk_widget_show(panels
);
2046 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2047 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2050 if (!*s
->pathname
) {
2051 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
2052 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2055 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
2056 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
2058 GtkWidget
*treeitem
;
2062 * We expect never to find an implicit path
2063 * component. For example, we expect never to see
2064 * A/B/C followed by A/D/E, because that would
2065 * _implicitly_ create A/D. All our path prefixes
2066 * are expected to contain actual controls and be
2067 * selectable in the treeview; so we would expect
2068 * to see A/D _explicitly_ before encountering
2071 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2073 c
= strrchr(s
->pathname
, '/');
2079 treeitem
= gtk_tree_item_new_with_label(c
);
2080 assert(j
-1 < level
);
2082 if (!treelevels
[j
-1]) {
2083 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2084 gtk_tree_item_set_subtree
2085 (treeitemlevels
[j
-1],
2086 GTK_WIDGET(treelevels
[j
-1]));
2087 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2089 gtk_tree_append(treelevels
[j
-1], treeitem
);
2091 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2093 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2094 treelevels
[j
] = NULL
;
2097 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2098 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2099 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2100 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2102 gtk_widget_show(treeitem
);
2106 first
= (panelvbox
== NULL
);
2108 panelvbox
= gtk_vbox_new(FALSE
, 4);
2109 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2111 panels_switch_to(PANELS(panels
), panelvbox
);
2112 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2115 if (nselparams
>= selparamsize
) {
2117 selparams
= sresize(selparams
, selparamsize
,
2120 selparams
[nselparams
].dp
= &dp
;
2121 selparams
[nselparams
].panels
= PANELS(panels
);
2122 selparams
[nselparams
].panel
= panelvbox
;
2123 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2124 selparams
[nselparams
].treeitem
= treeitem
;
2129 w
= layout_ctrls(&dp
,
2130 &selparams
[nselparams
-1].shortcuts
,
2131 s
, listitemheight
, NULL
);
2132 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2137 dp
.ntreeitems
= nselparams
;
2138 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2140 for (index
= 0; index
< nselparams
; index
++) {
2141 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2142 GTK_SIGNAL_FUNC(treeitem_sel
),
2144 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2148 dlg_refresh(NULL
, &dp
);
2150 dp
.shortcuts
= &selparams
[0].shortcuts
;
2151 dp
.currtreeitem
= dp
.treeitems
[0];
2152 dp
.lastfocus
= NULL
;
2156 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2157 gtk_widget_show(window
);
2160 * Set focus into the first available control.
2162 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2163 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2168 for (j
= 0; j
< s
->ncontrols
; j
++)
2169 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2170 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2171 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2172 dlg_set_focus(s
->ctrls
[j
], &dp
);
2173 dp
.lastfocus
= s
->ctrls
[j
];
2182 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2183 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2184 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2185 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2195 static void messagebox_handler(union control
*ctrl
, void *dlg
,
2196 void *data
, int event
)
2198 if (event
== EVENT_ACTION
)
2199 dlg_end(dlg
, ctrl
->generic
.context
.i
);
2201 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
2203 GtkWidget
*window
, *w0
, *w1
;
2204 struct controlbox
*ctrlbox
;
2205 struct controlset
*s0
, *s1
;
2208 struct Shortcuts scs
;
2214 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2215 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2218 ctrlbox
= ctrl_new_box();
2221 va_start(ap
, minwid
);
2222 while (va_arg(ap
, char *) != NULL
) {
2224 (void) va_arg(ap
, int); /* shortcut */
2225 (void) va_arg(ap
, int); /* normal/default/cancel */
2226 (void) va_arg(ap
, int); /* end value */
2230 s0
= ctrl_getset(ctrlbox
, "", "", "");
2231 c
= ctrl_columns(s0
, 2, 50, 50);
2232 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
2233 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
2234 for (index
= 0; index
< ncols
; index
++)
2235 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
2236 va_start(ap
, minwid
);
2239 char *title
= va_arg(ap
, char *);
2240 int shortcut
, type
, value
;
2243 shortcut
= va_arg(ap
, int);
2244 type
= va_arg(ap
, int);
2245 value
= va_arg(ap
, int);
2246 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
2247 messagebox_handler
, I(value
));
2248 c
->generic
.column
= index
++;
2250 c
->button
.isdefault
= TRUE
;
2252 c
->button
.iscancel
= TRUE
;
2256 s1
= ctrl_getset(ctrlbox
, "x", "", "");
2257 ctrl_text(s1
, msg
, HELPCTX(no_help
));
2259 window
= gtk_dialog_new();
2260 gtk_window_set_title(GTK_WINDOW(window
), title
);
2261 w0
= layout_ctrls(&dp
, &scs
, s0
, 0, GTK_WINDOW(window
));
2262 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2264 gtk_widget_show(w0
);
2265 w1
= layout_ctrls(&dp
, &scs
, s1
, 0, GTK_WINDOW(window
));
2266 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2267 gtk_widget_set_usize(w1
, minwid
+20, -1);
2268 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2270 gtk_widget_show(w1
);
2272 dp
.shortcuts
= &scs
;
2273 dp
.lastfocus
= NULL
;
2277 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
2279 set_transient_window_pos(parentwin
, window
);
2280 gtk_window_set_transient_for(GTK_WINDOW(window
),
2281 GTK_WINDOW(parentwin
));
2283 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2284 gtk_widget_show(window
);
2286 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2287 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2288 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2289 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2294 ctrl_free_box(ctrlbox
);
2299 static int string_width(char *text
)
2301 GtkWidget
*label
= gtk_label_new(text
);
2303 gtk_widget_size_request(label
, &req
);
2304 gtk_widget_unref(label
);
2308 int reallyclose(void *frontend
)
2310 char *title
= dupcat(appname
, " Exit Confirmation", NULL
);
2311 int ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2312 title
, "Are you sure you want to close this session?",
2313 string_width("Most of the width of the above text"),
2321 int verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
2322 char *keystr
, char *fingerprint
,
2323 void (*callback
)(void *ctx
, int result
), void *ctx
)
2325 static const char absenttxt
[] =
2326 "The server's host key is not cached. You have no guarantee "
2327 "that the server is the computer you think it is.\n"
2328 "The server's %s key fingerprint is:\n"
2330 "If you trust this host, press \"Accept\" to add the key to "
2331 "PuTTY's cache and carry on connecting.\n"
2332 "If you want to carry on connecting just once, without "
2333 "adding the key to the cache, press \"Connect Once\".\n"
2334 "If you do not trust this host, press \"Cancel\" to abandon the "
2336 static const char wrongtxt
[] =
2337 "WARNING - POTENTIAL SECURITY BREACH!\n"
2338 "The server's host key does not match the one PuTTY has "
2339 "cached. This means that either the server administrator "
2340 "has changed the host key, or you have actually connected "
2341 "to another computer pretending to be the server.\n"
2342 "The new %s key fingerprint is:\n"
2344 "If you were expecting this change and trust the new key, "
2345 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2346 "If you want to carry on connecting but without updating "
2347 "the cache, press \"Connect Once\".\n"
2348 "If you want to abandon the connection completely, press "
2349 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2357 ret
= verify_host_key(host
, port
, keytype
, keystr
);
2359 if (ret
== 0) /* success - key matched OK */
2362 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), keytype
, fingerprint
);
2364 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2365 "PuTTY Security Alert", text
,
2366 string_width(fingerprint
),
2367 "Accept", 'a', 0, 2,
2368 "Connect Once", 'o', 0, 1,
2369 "Cancel", 'c', -1, 0,
2375 store_host_key(host
, port
, keytype
, keystr
);
2376 return 1; /* continue with connection */
2377 } else if (ret
== 1)
2378 return 1; /* continue with connection */
2379 return 0; /* do not continue with connection */
2383 * Ask whether the selected algorithm is acceptable (since it was
2384 * below the configured 'warn' threshold).
2386 int askalg(void *frontend
, const char *algtype
, const char *algname
,
2387 void (*callback
)(void *ctx
, int result
), void *ctx
)
2389 static const char msg
[] =
2390 "The first %s supported by the server is "
2391 "%s, which is below the configured warning threshold.\n"
2392 "Continue with connection?";
2396 text
= dupprintf(msg
, algtype
, algname
);
2397 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2398 "PuTTY Security Alert", text
,
2399 string_width("Continue with connection?"),
2412 void old_keyfile_warning(void)
2415 * This should never happen on Unix. We hope.
2419 void fatal_message_box(void *window
, char *msg
)
2421 messagebox(window
, "PuTTY Fatal Error", msg
,
2422 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2423 "OK", 'o', 1, 1, NULL
);
2426 void fatalbox(char *p
, ...)
2431 msg
= dupvprintf(p
, ap
);
2433 fatal_message_box(NULL
, msg
);
2438 static GtkWidget
*aboutbox
= NULL
;
2440 static void about_close_clicked(GtkButton
*button
, gpointer data
)
2442 gtk_widget_destroy(aboutbox
);
2446 static void licence_clicked(GtkButton
*button
, gpointer data
)
2451 "Copyright 1997-2005 Simon Tatham.\n\n"
2453 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2454 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2455 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
2456 "Markus Kuhn, and CORE SDI S.A.\n\n"
2458 "Permission is hereby granted, free of charge, to any person "
2459 "obtaining a copy of this software and associated documentation "
2460 "files (the ""Software""), to deal in the Software without restriction, "
2461 "including without limitation the rights to use, copy, modify, merge, "
2462 "publish, distribute, sublicense, and/or sell copies of the Software, "
2463 "and to permit persons to whom the Software is furnished to do so, "
2464 "subject to the following conditions:\n\n"
2466 "The above copyright notice and this permission notice shall be "
2467 "included in all copies or substantial portions of the Software.\n\n"
2469 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2470 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2471 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2472 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2473 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
2474 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2475 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2476 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2477 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2478 "OTHER DEALINGS IN THE SOFTWARE.";
2480 title
= dupcat(appname
, " Licence", NULL
);
2481 assert(aboutbox
!= NULL
);
2482 messagebox(aboutbox
, title
, licence
,
2483 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2484 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2485 "OK", 'o', 1, 1, NULL
);
2489 void about_box(void *window
)
2495 gtk_widget_grab_focus(aboutbox
);
2499 aboutbox
= gtk_dialog_new();
2500 gtk_container_set_border_width(GTK_CONTAINER(aboutbox
), 10);
2501 title
= dupcat("About ", appname
, NULL
);
2502 gtk_window_set_title(GTK_WINDOW(aboutbox
), title
);
2505 w
= gtk_button_new_with_label("Close");
2506 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2507 gtk_window_set_default(GTK_WINDOW(aboutbox
), w
);
2508 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2509 w
, FALSE
, FALSE
, 0);
2510 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2511 GTK_SIGNAL_FUNC(about_close_clicked
), NULL
);
2514 w
= gtk_button_new_with_label("View Licence");
2515 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2516 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2517 w
, FALSE
, FALSE
, 0);
2518 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2519 GTK_SIGNAL_FUNC(licence_clicked
), NULL
);
2522 w
= gtk_label_new(appname
);
2523 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2524 w
, FALSE
, FALSE
, 0);
2527 w
= gtk_label_new(ver
);
2528 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2529 w
, FALSE
, FALSE
, 5);
2532 w
= gtk_label_new("Copyright 1997-2005 Simon Tatham. All rights reserved");
2533 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2534 w
, FALSE
, FALSE
, 5);
2537 set_transient_window_pos(GTK_WIDGET(window
), aboutbox
);
2538 gtk_widget_show(aboutbox
);
2541 struct eventlog_stuff
{
2542 GtkWidget
*parentwin
, *window
;
2543 struct controlbox
*eventbox
;
2544 struct Shortcuts scs
;
2546 union control
*listctrl
;
2548 int nevents
, negsize
;
2551 int ignore_selchange
;
2554 static void eventlog_destroy(GtkWidget
*widget
, gpointer data
)
2556 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2560 dlg_cleanup(&es
->dp
);
2561 ctrl_free_box(es
->eventbox
);
2563 static void eventlog_ok_handler(union control
*ctrl
, void *dlg
,
2564 void *data
, int event
)
2566 if (event
== EVENT_ACTION
)
2569 static void eventlog_list_handler(union control
*ctrl
, void *dlg
,
2570 void *data
, int event
)
2572 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2574 if (event
== EVENT_REFRESH
) {
2577 dlg_update_start(ctrl
, dlg
);
2578 dlg_listbox_clear(ctrl
, dlg
);
2579 for (i
= 0; i
< es
->nevents
; i
++) {
2580 dlg_listbox_add(ctrl
, dlg
, es
->events
[i
]);
2582 dlg_update_done(ctrl
, dlg
);
2583 } else if (event
== EVENT_SELCHANGE
) {
2588 * If this SELCHANGE event is happening as a result of
2589 * deliberate deselection because someone else has grabbed
2590 * the selection, the last thing we want to do is pre-empt
2593 if (es
->ignore_selchange
)
2597 * Construct the data to use as the selection.
2602 for (i
= 0; i
< es
->nevents
; i
++) {
2603 if (dlg_listbox_issel(ctrl
, dlg
, i
)) {
2604 int extralen
= strlen(es
->events
[i
]);
2606 if (es
->sellen
+ extralen
+ 2 > selsize
) {
2607 selsize
= es
->sellen
+ extralen
+ 512;
2608 es
->seldata
= sresize(es
->seldata
, selsize
, char);
2611 strcpy(es
->seldata
+ es
->sellen
, es
->events
[i
]);
2612 es
->sellen
+= extralen
;
2613 es
->seldata
[es
->sellen
++] = '\n';
2617 if (gtk_selection_owner_set(es
->window
, GDK_SELECTION_PRIMARY
,
2618 GDK_CURRENT_TIME
)) {
2619 extern GdkAtom compound_text_atom
;
2621 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2622 GDK_SELECTION_TYPE_STRING
, 1);
2623 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2624 compound_text_atom
, 1);
2630 void eventlog_selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
2631 guint info
, guint time_stamp
, gpointer data
)
2633 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2635 gtk_selection_data_set(seldata
, seldata
->target
, 8,
2636 es
->seldata
, es
->sellen
);
2639 gint
eventlog_selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
2642 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2646 * Deselect everything in the list box.
2648 uc
= dlg_find_byctrl(&es
->dp
, es
->listctrl
);
2649 es
->ignore_selchange
= 1;
2650 gtk_list_unselect_all(GTK_LIST(uc
->list
));
2651 es
->ignore_selchange
= 0;
2659 void showeventlog(void *estuff
, void *parentwin
)
2661 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2662 GtkWidget
*window
, *w0
, *w1
;
2663 GtkWidget
*parent
= GTK_WIDGET(parentwin
);
2664 struct controlset
*s0
, *s1
;
2666 int listitemheight
, index
;
2670 gtk_widget_grab_focus(es
->window
);
2676 for (index
= 0; index
< lenof(es
->scs
.sc
); index
++) {
2677 es
->scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2680 es
->eventbox
= ctrl_new_box();
2682 s0
= ctrl_getset(es
->eventbox
, "", "", "");
2683 ctrl_columns(s0
, 3, 33, 34, 33);
2684 c
= ctrl_pushbutton(s0
, "Close", 'c', HELPCTX(no_help
),
2685 eventlog_ok_handler
, P(NULL
));
2686 c
->button
.column
= 1;
2687 c
->button
.isdefault
= TRUE
;
2689 s1
= ctrl_getset(es
->eventbox
, "x", "", "");
2690 es
->listctrl
= c
= ctrl_listbox(s1
, NULL
, NO_SHORTCUT
, HELPCTX(no_help
),
2691 eventlog_list_handler
, P(es
));
2692 c
->listbox
.height
= 10;
2693 c
->listbox
.multisel
= 2;
2694 c
->listbox
.ncols
= 3;
2695 c
->listbox
.percentages
= snewn(3, int);
2696 c
->listbox
.percentages
[0] = 25;
2697 c
->listbox
.percentages
[1] = 10;
2698 c
->listbox
.percentages
[2] = 65;
2700 listitemheight
= get_listitemheight();
2702 es
->window
= window
= gtk_dialog_new();
2703 title
= dupcat(appname
, " Event Log", NULL
);
2704 gtk_window_set_title(GTK_WINDOW(window
), title
);
2706 w0
= layout_ctrls(&es
->dp
, &es
->scs
, s0
,
2707 listitemheight
, GTK_WINDOW(window
));
2708 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2710 gtk_widget_show(w0
);
2711 w1
= layout_ctrls(&es
->dp
, &es
->scs
, s1
,
2712 listitemheight
, GTK_WINDOW(window
));
2713 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2714 gtk_widget_set_usize(w1
, 20 +
2715 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
2716 " IS QUITE LONG 'COS SSH LOG ENTRIES"
2718 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2720 gtk_widget_show(w1
);
2723 es
->dp
.shortcuts
= &es
->scs
;
2724 es
->dp
.lastfocus
= NULL
;
2726 es
->dp
.window
= window
;
2728 dlg_refresh(NULL
, &es
->dp
);
2731 set_transient_window_pos(parent
, window
);
2732 gtk_window_set_transient_for(GTK_WINDOW(window
),
2733 GTK_WINDOW(parent
));
2735 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2736 gtk_widget_show(window
);
2738 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2739 GTK_SIGNAL_FUNC(eventlog_destroy
), es
);
2740 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2741 GTK_SIGNAL_FUNC(win_key_press
), &es
->dp
);
2742 gtk_signal_connect(GTK_OBJECT(window
), "selection_get",
2743 GTK_SIGNAL_FUNC(eventlog_selection_get
), es
);
2744 gtk_signal_connect(GTK_OBJECT(window
), "selection_clear_event",
2745 GTK_SIGNAL_FUNC(eventlog_selection_clear
), es
);
2748 void *eventlogstuff_new(void)
2750 struct eventlog_stuff
*es
;
2751 es
= snew(struct eventlog_stuff
);
2752 memset(es
, 0, sizeof(*es
));
2756 void logevent_dlg(void *estuff
, const char *string
)
2758 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2763 if (es
->nevents
>= es
->negsize
) {
2765 es
->events
= sresize(es
->events
, es
->negsize
, char *);
2769 strftime(timebuf
, sizeof(timebuf
), "%Y-%m-%d %H:%M:%S\t", &tm
);
2771 es
->events
[es
->nevents
] = snewn(strlen(timebuf
) + strlen(string
) + 1, char);
2772 strcpy(es
->events
[es
->nevents
], timebuf
);
2773 strcat(es
->events
[es
->nevents
], string
);
2775 dlg_listbox_add(es
->listctrl
, &es
->dp
, es
->events
[es
->nevents
]);
2780 int askappend(void *frontend
, Filename filename
,
2781 void (*callback
)(void *ctx
, int result
), void *ctx
)
2783 static const char msgtemplate
[] =
2784 "The session log file \"%.*s\" already exists. "
2785 "You can overwrite it with a new session log, "
2786 "append your session log to the end of it, "
2787 "or disable session logging for this session.";
2792 message
= dupprintf(msgtemplate
, FILENAME_MAX
, filename
.path
);
2793 mbtitle
= dupprintf("%s Log to File", appname
);
2795 mbret
= messagebox(get_window(frontend
), mbtitle
, message
,
2796 string_width("LINE OF TEXT SUITABLE FOR THE"
2797 " ASKAPPEND WIDTH"),
2798 "Overwrite", 'o', 1, 2,
2799 "Append", 'a', 0, 1,
2800 "Disable", 'd', -1, 0,