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
;
157 static void dlg_cleanup(struct dlgparam
*dp
)
161 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
163 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
164 del234(dp
->bywidget
, uc
);
165 if (uc
->privdata_needs_free
)
170 freetree234(dp
->bywidget
);
172 sfree(dp
->treeitems
);
175 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
177 add234(dp
->byctrl
, uc
);
178 add234(dp
->bywidget
, uc
);
181 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
185 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
188 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
190 struct uctrl
*ret
= NULL
;
194 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
202 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
204 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
205 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
209 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
211 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
212 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
214 uc
->privdata_needs_free
= FALSE
;
217 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
219 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
220 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
222 * This is an internal allocation routine, so it's allowed to
223 * use smalloc directly.
225 uc
->privdata
= smalloc(size
);
226 uc
->privdata_needs_free
= FALSE
;
230 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
232 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
233 if (dp
->currfocus
!= ctrl
)
234 return dp
->currfocus
;
236 return dp
->lastfocus
;
239 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
241 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
242 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
243 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
244 assert(uc
->buttons
!= NULL
);
245 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
248 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
250 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
251 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
254 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
255 assert(uc
->buttons
!= NULL
);
256 for (i
= 0; i
< uc
->nbuttons
; i
++)
257 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
259 return 0; /* got to return something */
262 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
264 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
265 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
266 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
267 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
270 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
272 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
273 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
274 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
275 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
278 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
280 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
281 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
282 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
283 assert(uc
->entry
!= NULL
);
284 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
287 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
289 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
290 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
291 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
292 assert(uc
->entry
!= NULL
);
293 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
295 buffer
[length
-1] = '\0';
298 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
300 GtkContainer
*cont
= GTK_CONTAINER(data
);
301 /* gtk_container_remove will unref the widget for us; we need not. */
302 gtk_container_remove(cont
, w
);
305 /* The `listbox' functions can also apply to combo boxes. */
306 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
308 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
309 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
311 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
312 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
313 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
316 gtk_container_foreach(GTK_CONTAINER(uc
->menu
),
317 container_remove_and_destroy
,
318 GTK_CONTAINER(uc
->menu
));
320 gtk_list_clear_items(GTK_LIST(uc
->list
), 0, -1);
324 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
326 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
327 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
329 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
330 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
331 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
335 (GTK_CONTAINER(uc
->menu
),
336 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
338 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
342 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
344 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
348 * Each listbox entry may have a numeric id associated with it.
349 * Note that some front ends only permit a string to be stored at
350 * each position, which means that _if_ you put two identical
351 * strings in any listbox then you MUST not assign them different
352 * IDs and expect to get meaningful results back.
354 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
355 char const *text
, int id
)
357 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
358 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
360 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
361 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
362 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
364 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
368 * List item in a drop-down (but non-combo) list. Tabs are
369 * ignored; we just provide a standard menu item with the
372 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
374 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
375 gtk_widget_show(menuitem
);
377 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", (gpointer
)id
);
378 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
379 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
380 } else if (!uc
->entry
) {
382 * List item in a non-combo-box list box. We make all of
383 * these Columns containing GtkLabels. This allows us to do
384 * the nasty force_left hack irrespective of whether there
385 * are tabs in the thing.
387 GtkWidget
*listitem
= gtk_list_item_new();
388 GtkWidget
*cols
= columns_new(10);
392 /* Count the tabs in the text, and hence determine # of columns. */
394 for (i
= 0; text
[i
]; i
++)
399 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
400 percents
= snewn(ncols
, gint
);
401 percents
[ncols
-1] = 100;
402 for (i
= 0; i
< ncols
-1; i
++) {
403 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
404 percents
[ncols
-1] -= percents
[i
];
406 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
409 for (i
= 0; i
< ncols
; i
++) {
410 int len
= strcspn(text
, "\t");
411 char *dup
= dupprintf("%.*s", len
, text
);
416 label
= gtk_label_new(dup
);
419 columns_add(COLUMNS(cols
), label
, i
, 1);
420 columns_force_left_align(COLUMNS(cols
), label
);
421 gtk_widget_show(label
);
423 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
424 gtk_widget_show(cols
);
425 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
426 gtk_widget_show(listitem
);
428 if (ctrl
->listbox
.multisel
) {
429 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
430 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
432 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
433 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
435 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
436 GTK_SIGNAL_FUNC(widget_focus
), dp
);
437 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
438 GTK_SIGNAL_FUNC(listitem_button
), dp
);
439 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
442 * List item in a combo-box list, which means the sensible
443 * thing to do is make it a perfectly normal label. Hence
444 * tabs are disregarded.
446 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
448 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
449 gtk_widget_show(listitem
);
451 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data", (gpointer
)id
);
454 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
457 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
459 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
460 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
464 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
465 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
466 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
468 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
470 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
471 g_list_free(children
);
473 return (int)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
476 /* dlg_listbox_index returns <0 if no single element is selected. */
477 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
479 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
480 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
482 GtkWidget
*item
, *activeitem
;
486 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
487 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
488 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
491 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
493 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
495 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
496 i
++, children
= children
->next
) {
497 if (uc
->menu ? activeitem
== item
:
498 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
505 g_list_free(children
);
506 return selected
< 0 ?
-1 : selected
;
509 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
511 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
512 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
514 GtkWidget
*item
, *activeitem
;
516 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
517 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
518 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
520 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
522 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
523 g_list_free(children
);
526 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
527 return item
== activeitem
;
529 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
533 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
535 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
536 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
538 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
539 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
540 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
543 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
545 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
549 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
551 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
552 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
554 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
555 assert(uc
->text
!= NULL
);
557 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
560 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
562 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
563 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
564 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
565 assert(uc
->entry
!= NULL
);
566 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
569 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
571 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
572 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
573 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
574 assert(uc
->entry
!= NULL
);
575 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
577 fn
->path
[lenof(fn
->path
)-1] = '\0';
580 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
582 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
583 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
584 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
585 assert(uc
->entry
!= NULL
);
586 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
589 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
591 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
592 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
593 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
594 assert(uc
->entry
!= NULL
);
595 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
597 fs
->name
[lenof(fs
->name
)-1] = '\0';
601 * Bracketing a large set of updates in these two functions will
602 * cause the front end (if possible) to delay updating the screen
603 * until it's all complete, thus avoiding flicker.
605 void dlg_update_start(union control
*ctrl
, void *dlg
)
608 * Apparently we can't do this at all in GTK. GtkCList supports
609 * freeze and thaw, but not GtkList. Bah.
613 void dlg_update_done(union control
*ctrl
, void *dlg
)
616 * Apparently we can't do this at all in GTK. GtkCList supports
617 * freeze and thaw, but not GtkList. Bah.
621 void dlg_set_focus(union control
*ctrl
, void *dlg
)
623 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
624 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
626 switch (ctrl
->generic
.type
) {
629 /* Check boxes and buttons get the focus _and_ get toggled. */
630 gtk_widget_grab_focus(uc
->toplevel
);
632 case CTRL_FILESELECT
:
633 case CTRL_FONTSELECT
:
635 /* Anything containing an edit box gets that focused. */
636 gtk_widget_grab_focus(uc
->entry
);
640 * Radio buttons: we find the currently selected button and
645 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
646 if (gtk_toggle_button_get_active
647 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
648 gtk_widget_grab_focus(uc
->buttons
[i
]);
654 * If the list is really an option menu, we focus it.
655 * Otherwise we tell it to focus one of its children, which
656 * appears to do the Right Thing.
659 gtk_widget_grab_focus(uc
->optmenu
);
661 assert(uc
->list
!= NULL
);
662 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
669 * During event processing, you might well want to give an error
670 * indication to the user. dlg_beep() is a quick and easy generic
671 * error; dlg_error() puts up a message-box or equivalent.
673 void dlg_beep(void *dlg
)
678 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
680 gtk_widget_destroy(GTK_WIDGET(data
));
683 static void set_transient_window_pos(GtkWidget
*parent
, GtkWidget
*child
)
685 gint x
, y
, w
, h
, dx
, dy
;
687 gtk_window_set_position(GTK_WINDOW(child
), GTK_WIN_POS_NONE
);
688 gtk_widget_size_request(GTK_WIDGET(child
), &req
);
690 gdk_window_get_origin(GTK_WIDGET(parent
)->window
, &x
, &y
);
691 gdk_window_get_size(GTK_WIDGET(parent
)->window
, &w
, &h
);
694 * One corner of the transient will be offset inwards, by 1/4
695 * of the parent window's size, from the corresponding corner
696 * of the parent window. The corner will be chosen so as to
697 * place the transient closer to the centre of the screen; this
698 * should avoid transients going off the edge of the screen on
701 if (x
+ w
/2 < gdk_screen_width() / 2)
702 dx
= x
+ w
/4; /* work from left edges */
704 dx
= x
+ 3*w
/4 - req
.width
; /* work from right edges */
705 if (y
+ h
/2 < gdk_screen_height() / 2)
706 dy
= y
+ h
/4; /* work from top edges */
708 dy
= y
+ 3*h
/4 - req
.height
; /* work from bottom edges */
709 gtk_widget_set_uposition(GTK_WIDGET(child
), dx
, dy
);
712 void dlg_error_msg(void *dlg
, char *msg
)
714 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
715 GtkWidget
*window
, *hbox
, *text
, *ok
;
717 window
= gtk_dialog_new();
718 text
= gtk_label_new(msg
);
719 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
720 hbox
= gtk_hbox_new(FALSE
, 0);
721 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
722 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
723 hbox
, FALSE
, FALSE
, 20);
724 gtk_widget_show(text
);
725 gtk_widget_show(hbox
);
726 gtk_window_set_title(GTK_WINDOW(window
), "Error");
727 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
728 ok
= gtk_button_new_with_label("OK");
729 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
730 ok
, FALSE
, FALSE
, 0);
732 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
733 gtk_window_set_default(GTK_WINDOW(window
), ok
);
734 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
735 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
736 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
737 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
738 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
739 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
740 set_transient_window_pos(dp
->window
, window
);
741 gtk_widget_show(window
);
746 * This function signals to the front end that the dialog's
747 * processing is completed, and passes an integer value (typically
750 void dlg_end(void *dlg
, int value
)
752 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
754 gtk_widget_destroy(dp
->window
);
757 void dlg_refresh(union control
*ctrl
, void *dlg
)
759 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
763 if (ctrl
->generic
.handler
!= NULL
)
764 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
768 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
769 assert(uc
->ctrl
!= NULL
);
770 if (uc
->ctrl
->generic
.handler
!= NULL
)
771 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
772 dp
->data
, EVENT_REFRESH
);
777 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
779 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
780 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
783 GtkWidget
*coloursel
=
784 gtk_color_selection_dialog_new("Select a colour");
785 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
787 dp
->coloursel_result
.ok
= FALSE
;
789 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
790 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
791 cvals
[0] = r
/ 255.0;
792 cvals
[1] = g
/ 255.0;
793 cvals
[2] = b
/ 255.0;
794 cvals
[3] = 1.0; /* fully opaque! */
795 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
797 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
798 (gpointer
)coloursel
);
799 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
800 (gpointer
)coloursel
);
801 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
802 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
803 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
804 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
805 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
806 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
807 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
808 (gpointer
)coloursel
);
809 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
810 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
811 (gpointer
)coloursel
);
812 gtk_widget_show(coloursel
);
815 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
816 int *r
, int *g
, int *b
)
818 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
819 if (dp
->coloursel_result
.ok
) {
820 *r
= dp
->coloursel_result
.r
;
821 *g
= dp
->coloursel_result
.g
;
822 *b
= dp
->coloursel_result
.b
;
828 /* ----------------------------------------------------------------------
829 * Signal handlers while the dialog box is active.
832 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
835 struct dlgparam
*dp
= (struct dlgparam
*)data
;
836 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
837 union control
*focus
;
844 if (focus
!= dp
->currfocus
) {
845 dp
->lastfocus
= dp
->currfocus
;
846 dp
->currfocus
= focus
;
852 static void button_clicked(GtkButton
*button
, gpointer data
)
854 struct dlgparam
*dp
= (struct dlgparam
*)data
;
855 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
856 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
859 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
861 struct dlgparam
*dp
= (struct dlgparam
*)data
;
862 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
863 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
866 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
869 * GtkEntry has a nasty habit of eating the Return key, which
870 * is unhelpful since it doesn't actually _do_ anything with it
871 * (it calls gtk_widget_activate, but our edit boxes never need
872 * activating). So I catch Return before GtkEntry sees it, and
873 * pass it straight on to the parent widget. Effect: hitting
874 * Return in an edit box will now activate the default button
875 * in the dialog just like it will everywhere else.
877 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
879 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
880 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
887 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
889 struct dlgparam
*dp
= (struct dlgparam
*)data
;
890 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
891 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
892 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
896 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
899 struct dlgparam
*dp
= (struct dlgparam
*)data
;
900 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
901 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
904 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
907 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
909 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
910 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
911 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
912 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
914 * Up, Down, PgUp or PgDn have been pressed on a ListItem
915 * in a list box. So, if the list box is single-selection:
917 * - if the list item in question isn't already selected,
918 * we simply select it.
919 * - otherwise, we find the next one (or next
920 * however-far-away) in whichever direction we're going,
922 * + in this case, we must also fiddle with the
923 * scrollbar to ensure the newly selected item is
926 * If it's multiple-selection, we do all of the above
927 * except actually selecting anything, so we move the focus
928 * and fiddle the scrollbar to follow it.
930 GtkWidget
*list
= item
->parent
;
932 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
935 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
936 gtk_list_select_child(GTK_LIST(list
), item
);
939 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
940 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
943 (event
->keyval
==GDK_Page_Down
||
944 event
->keyval
==GDK_KP_Page_Down
||
945 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
948 GList
*children
, *chead
;
950 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
952 n
= g_list_length(children
);
956 * Figure out how many list items to a screenful,
957 * and adjust the step appropriately.
959 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
960 step
--; /* go by one less than that */
964 while (children
!= NULL
) {
965 if (item
== children
->data
)
967 children
= children
->next
;
972 if (direction
< 0 && i
> 0)
973 children
= children
->prev
, i
--;
974 else if (direction
> 0 && i
< n
-1)
975 children
= children
->next
, i
++;
979 if (children
&& children
->data
) {
981 gtk_list_select_child(GTK_LIST(list
),
982 GTK_WIDGET(children
->data
));
983 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
984 gtk_adjustment_clamp_page
986 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
987 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
998 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
1001 return listitem_key(item
, event
, data
, FALSE
);
1004 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
1007 return listitem_key(item
, event
, data
, TRUE
);
1010 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
1013 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1014 if (event
->type
== GDK_2BUTTON_PRESS
||
1015 event
->type
== GDK_3BUTTON_PRESS
) {
1016 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1017 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1023 static void list_selchange(GtkList
*list
, gpointer data
)
1025 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1026 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1028 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1031 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1033 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1034 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1035 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1036 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1037 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1040 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1042 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1043 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1047 (index
== 0 && direction
< 0) ||
1048 (index
== g_list_length(children
)-1 && direction
> 0)) {
1053 child
= g_list_nth_data(children
, index
);
1054 gtk_widget_ref(child
);
1055 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1056 g_list_free(children
);
1059 children
= g_list_append(children
, child
);
1060 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1061 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1062 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1065 static void draglist_up(GtkButton
*button
, gpointer data
)
1067 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1068 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1069 draglist_move(dp
, uc
, -1);
1072 static void draglist_down(GtkButton
*button
, gpointer data
)
1074 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1075 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1076 draglist_move(dp
, uc
, +1);
1079 static void filesel_ok(GtkButton
*button
, gpointer data
)
1081 /* struct dlgparam *dp = (struct dlgparam *)data; */
1082 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1083 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1084 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1085 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1088 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1090 /* struct dlgparam *dp = (struct dlgparam *)data; */
1091 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1092 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1093 char *name
= gtk_font_selection_dialog_get_font_name
1094 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1095 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1098 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1100 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1101 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1102 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1104 gtk_color_selection_get_color
1105 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1107 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1108 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1109 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1110 dp
->coloursel_result
.ok
= TRUE
;
1111 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1114 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1116 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1117 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1118 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1119 dp
->coloursel_result
.ok
= FALSE
;
1120 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1123 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1125 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1126 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1128 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1129 GtkWidget
*filesel
=
1130 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1131 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1133 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1135 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1137 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1138 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1139 gtk_signal_connect_object
1140 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1141 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1142 gtk_signal_connect_object
1143 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1144 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1145 gtk_widget_show(filesel
);
1148 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1149 gchar
*spacings
[] = { "c", "m", NULL
};
1150 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1151 GtkWidget
*fontsel
=
1152 gtk_font_selection_dialog_new("Select a font");
1153 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1154 gtk_font_selection_dialog_set_filter
1155 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1156 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1157 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1158 if (!gtk_font_selection_dialog_set_font_name
1159 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1161 * If the font name wasn't found as it was, try opening
1162 * it and extracting its FONT property. This should
1163 * have the effect of mapping short aliases into true
1166 GdkFont
*font
= gdk_font_load(fontname
);
1168 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1169 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1170 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1172 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1173 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1175 gtk_font_selection_dialog_set_font_name
1176 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1178 gdk_font_unref(font
);
1182 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1183 "user-data", (gpointer
)fontsel
);
1184 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1186 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1187 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1188 gtk_signal_connect_object
1189 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1190 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1192 gtk_signal_connect_object
1193 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1194 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1196 gtk_widget_show(fontsel
);
1200 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1203 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1204 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1206 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1207 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1208 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1211 /* ----------------------------------------------------------------------
1212 * This function does the main layout work: it reads a controlset,
1213 * it creates the relevant GTK controls, and returns a GtkWidget
1214 * containing the result. (This widget might be a title of some
1215 * sort, it might be a Columns containing many controls, or it
1216 * might be a GtkFrame containing a Columns; whatever it is, it's
1217 * definitely a GtkWidget and should probably be added to a
1220 * `listitemheight' is used to calculate a usize for list boxes: it
1221 * should be the height from the size request of a GtkListItem.
1223 * `win' is required for setting the default button. If it is
1224 * non-NULL, all buttons created will be default-capable (so they
1225 * have extra space round them for the default highlight).
1227 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1228 struct controlset
*s
, int listitemheight
,
1235 if (!s
->boxname
&& s
->boxtitle
) {
1236 /* This controlset is a panel title. */
1237 return gtk_label_new(s
->boxtitle
);
1241 * Otherwise, we expect to be laying out actual controls, so
1242 * we'll start by creating a Columns for the purpose.
1244 cols
= COLUMNS(columns_new(4));
1245 ret
= GTK_WIDGET(cols
);
1246 gtk_widget_show(ret
);
1249 * Create a containing frame if we have a box name.
1252 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1253 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1254 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1255 gtk_widget_show(ret
);
1259 * Now iterate through the controls themselves, create them,
1260 * and add them to the Columns.
1262 for (i
= 0; i
< s
->ncontrols
; i
++) {
1263 union control
*ctrl
= s
->ctrls
[i
];
1266 GtkWidget
*w
= NULL
;
1268 switch (ctrl
->generic
.type
) {
1271 static const int simplecols
[1] = { 100 };
1272 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1273 (ctrl
->columns
.percentages ?
1274 ctrl
->columns
.percentages
: simplecols
));
1276 continue; /* no actual control created */
1279 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1281 columns_taborder_last(cols
, uc
->toplevel
);
1283 continue; /* no actual control created */
1286 uc
= snew(struct uctrl
);
1288 uc
->privdata
= NULL
;
1289 uc
->privdata_needs_free
= FALSE
;
1291 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1292 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1294 switch (ctrl
->generic
.type
) {
1296 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1298 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1299 if (ctrl
->button
.isdefault
)
1300 gtk_window_set_default(win
, w
);
1301 if (ctrl
->button
.iscancel
)
1302 dp
->cancelbutton
= w
;
1304 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1305 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1306 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1307 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1308 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1309 SHORTCUT_UCTRL
, uc
);
1312 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1313 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1314 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1315 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1316 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1317 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1318 SHORTCUT_UCTRL
, uc
);
1323 * Radio buttons get to go inside their own Columns, no
1327 gint i
, *percentages
;
1331 if (ctrl
->generic
.label
) {
1332 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1333 columns_add(COLUMNS(w
), label
, 0, 1);
1334 columns_force_left_align(COLUMNS(w
), label
);
1335 gtk_widget_show(label
);
1336 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1337 SHORTCUT_UCTRL
, uc
);
1339 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1340 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1342 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1343 100 * i
/ ctrl
->radio
.ncolumns
);
1345 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1347 g_free(percentages
);
1350 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1351 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1353 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1357 b
= (gtk_radio_button_new_with_label
1358 (group
, ctrl
->radio
.buttons
[i
]));
1360 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1361 colstart
= i
% ctrl
->radio
.ncolumns
;
1362 columns_add(COLUMNS(w
), b
, colstart
,
1363 (i
== ctrl
->radio
.nbuttons
-1 ?
1364 ctrl
->radio
.ncolumns
- colstart
: 1));
1365 columns_force_left_align(COLUMNS(w
), b
);
1367 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1368 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1369 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1370 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1371 if (ctrl
->radio
.shortcuts
) {
1372 shortcut_add(scs
, GTK_BIN(b
)->child
,
1373 ctrl
->radio
.shortcuts
[i
],
1374 SHORTCUT_UCTRL
, uc
);
1383 if (ctrl
->editbox
.has_list
) {
1384 w
= gtk_combo_new();
1385 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1386 uc
->entry
= GTK_COMBO(w
)->entry
;
1387 uc
->list
= GTK_COMBO(w
)->list
;
1389 w
= gtk_entry_new();
1390 if (ctrl
->editbox
.password
)
1391 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1394 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1395 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1396 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1397 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1398 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1399 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1401 * Edit boxes, for some strange reason, have a minimum
1402 * width of 150 in GTK 1.2. We don't want this - we'd
1403 * rather the edit boxes acquired their natural width
1404 * from the column layout of the rest of the box.
1406 * Also, while we're here, we'll squirrel away the
1407 * edit box height so we can use that to centre its
1408 * label vertically beside it.
1410 gtk_widget_size_request(w
, &req
);
1411 gtk_widget_set_usize(w
, 10, req
.height
);
1413 if (ctrl
->generic
.label
) {
1414 GtkWidget
*label
, *container
;
1416 label
= gtk_label_new(ctrl
->generic
.label
);
1418 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1419 SHORTCUT_FOCUS
, uc
->entry
);
1421 container
= columns_new(4);
1422 if (ctrl
->editbox
.percentwidth
== 100) {
1423 columns_add(COLUMNS(container
), label
, 0, 1);
1424 columns_force_left_align(COLUMNS(container
), label
);
1425 columns_add(COLUMNS(container
), w
, 0, 1);
1427 gint percentages
[2];
1428 percentages
[1] = ctrl
->editbox
.percentwidth
;
1429 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1430 columns_set_cols(COLUMNS(container
), 2, percentages
);
1431 columns_add(COLUMNS(container
), label
, 0, 1);
1432 columns_force_left_align(COLUMNS(container
), label
);
1433 columns_add(COLUMNS(container
), w
, 1, 1);
1434 /* Centre the label vertically. */
1435 gtk_widget_set_usize(label
, -1, req
.height
);
1436 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1438 gtk_widget_show(label
);
1443 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1444 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1447 case CTRL_FILESELECT
:
1448 case CTRL_FONTSELECT
:
1453 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1454 "Browse..." : "Change...");
1456 gint percentages
[] = { 75, 25 };
1458 columns_set_cols(COLUMNS(w
), 2, percentages
);
1460 if (ctrl
->generic
.label
) {
1461 ww
= gtk_label_new(ctrl
->generic
.label
);
1462 columns_add(COLUMNS(w
), ww
, 0, 2);
1463 columns_force_left_align(COLUMNS(w
), ww
);
1464 gtk_widget_show(ww
);
1465 shortcut_add(scs
, ww
,
1466 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1467 ctrl
->fileselect
.shortcut
:
1468 ctrl
->fontselect
.shortcut
),
1469 SHORTCUT_UCTRL
, uc
);
1472 uc
->entry
= ww
= gtk_entry_new();
1473 gtk_widget_size_request(ww
, &req
);
1474 gtk_widget_set_usize(ww
, 10, req
.height
);
1475 columns_add(COLUMNS(w
), ww
, 0, 1);
1476 gtk_widget_show(ww
);
1478 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1479 columns_add(COLUMNS(w
), ww
, 1, 1);
1480 gtk_widget_show(ww
);
1482 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1483 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1484 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1485 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1486 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1487 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1488 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1489 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1490 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1491 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1495 if (ctrl
->listbox
.height
== 0) {
1496 uc
->optmenu
= w
= gtk_option_menu_new();
1497 uc
->menu
= gtk_menu_new();
1498 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1499 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1500 (gpointer
)uc
->optmenu
);
1501 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1502 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1504 uc
->list
= gtk_list_new();
1505 if (ctrl
->listbox
.multisel
== 2) {
1506 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1507 GTK_SELECTION_EXTENDED
);
1508 } else if (ctrl
->listbox
.multisel
== 1) {
1509 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1510 GTK_SELECTION_MULTIPLE
);
1512 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1513 GTK_SELECTION_SINGLE
);
1515 w
= gtk_scrolled_window_new(NULL
, NULL
);
1516 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1518 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1520 GTK_POLICY_AUTOMATIC
);
1521 uc
->adj
= gtk_scrolled_window_get_vadjustment
1522 (GTK_SCROLLED_WINDOW(w
));
1524 gtk_widget_show(uc
->list
);
1525 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1526 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1527 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1528 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1531 * Adjust the height of the scrolled window to the
1532 * minimum given by the height parameter.
1534 * This piece of guesswork is a horrid hack based
1535 * on looking inside the GTK 1.2 sources
1536 * (specifically gtkviewport.c, which appears to be
1537 * the widget which provides the border around the
1538 * scrolling area). Anyone lets me know how I can
1539 * do this in a way which isn't at risk from GTK
1540 * upgrades, I'd be grateful.
1543 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1544 gtk_widget_set_usize(w
, 10,
1545 2*edge
+ (ctrl
->listbox
.height
*
1549 if (ctrl
->listbox
.draglist
) {
1551 * GTK doesn't appear to make it easy to
1552 * implement a proper draggable list; so
1553 * instead I'm just going to have to put an Up
1554 * and a Down button to the right of the actual
1555 * list box. Ah well.
1557 GtkWidget
*cols
, *button
;
1558 static const gint percentages
[2] = { 80, 20 };
1560 cols
= columns_new(4);
1561 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1562 columns_add(COLUMNS(cols
), w
, 0, 1);
1564 button
= gtk_button_new_with_label("Up");
1565 columns_add(COLUMNS(cols
), button
, 1, 1);
1566 gtk_widget_show(button
);
1567 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1568 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1569 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1570 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1571 button
= gtk_button_new_with_label("Down");
1572 columns_add(COLUMNS(cols
), button
, 1, 1);
1573 gtk_widget_show(button
);
1574 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1575 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1576 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1577 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1583 if (ctrl
->generic
.label
) {
1584 GtkWidget
*label
, *container
;
1586 label
= gtk_label_new(ctrl
->generic
.label
);
1588 container
= columns_new(4);
1589 if (ctrl
->listbox
.percentwidth
== 100) {
1590 columns_add(COLUMNS(container
), label
, 0, 1);
1591 columns_force_left_align(COLUMNS(container
), label
);
1592 columns_add(COLUMNS(container
), w
, 0, 1);
1594 gint percentages
[2];
1595 percentages
[1] = ctrl
->listbox
.percentwidth
;
1596 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1597 columns_set_cols(COLUMNS(container
), 2, percentages
);
1598 columns_add(COLUMNS(container
), label
, 0, 1);
1599 columns_force_left_align(COLUMNS(container
), label
);
1600 columns_add(COLUMNS(container
), w
, 1, 1);
1602 gtk_widget_show(label
);
1604 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1605 SHORTCUT_UCTRL
, uc
);
1611 * Wrapping text widgets don't sit well with the GTK
1612 * layout model, in which widgets state a minimum size
1613 * and the whole window then adjusts to the smallest
1614 * size it can sensibly take given its contents. A
1615 * wrapping text widget _has_ no clear minimum size;
1616 * instead it has a range of possibilities. It can be
1617 * one line deep but 2000 wide, or two lines deep and
1618 * 1000 pixels, or three by 867, or four by 500 and so
1619 * on. It can be as short as you like provided you
1620 * don't mind it being wide, or as narrow as you like
1621 * provided you don't mind it being tall.
1623 * Therefore, it fits very badly into the layout model.
1624 * Hence the only thing to do is pick a width and let
1625 * it choose its own number of lines. To do this I'm
1626 * going to cheat a little. All new wrapping text
1627 * widgets will be created with a minimal text content
1628 * "X"; then, after the rest of the dialog box is set
1629 * up and its size calculated, the text widgets will be
1630 * told their width and given their real text, which
1631 * will cause the size to be recomputed in the y
1632 * direction (because many of them will expand to more
1635 uc
->text
= w
= gtk_label_new("X");
1636 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1637 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1639 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1640 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1646 columns_add(cols
, w
,
1647 COLUMN_START(ctrl
->generic
.column
),
1648 COLUMN_SPAN(ctrl
->generic
.column
));
1650 columns_force_left_align(cols
, w
);
1654 dlg_add_uctrl(dp
, uc
);
1661 struct dlgparam
*dp
;
1663 GtkWidget
*panel
, *treeitem
;
1664 struct Shortcuts shortcuts
;
1667 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1669 struct selparam
*sp
= (struct selparam
*)data
;
1671 panels_switch_to(sp
->panels
, sp
->panel
);
1673 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1674 sp
->dp
->currtreeitem
= sp
->treeitem
;
1677 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1682 static int tree_grab_focus(struct dlgparam
*dp
)
1687 * See if any of the treeitems has the focus.
1690 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1691 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1699 gtk_widget_grab_focus(dp
->currtreeitem
);
1704 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1707 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1709 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1711 * If there's a focused treeitem, we return FALSE to cause the
1712 * focus to move on to some totally other control. If not, we
1713 * focus the selected one.
1715 return tree_grab_focus(dp
);
1718 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1720 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1722 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1723 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1727 if ((event
->state
& GDK_MOD1_MASK
) &&
1728 (unsigned char)event
->string
[0] > 0 &&
1729 (unsigned char)event
->string
[0] <= 127) {
1730 int schr
= (unsigned char)event
->string
[0];
1731 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1733 switch (sc
->action
) {
1735 tree_grab_focus(dp
);
1737 case SHORTCUT_FOCUS
:
1738 gtk_widget_grab_focus(sc
->widget
);
1740 case SHORTCUT_UCTRL
:
1742 * We must do something sensible with a uctrl.
1743 * Precisely what this is depends on the type of
1746 switch (sc
->uc
->ctrl
->generic
.type
) {
1749 /* Check boxes and buttons get the focus _and_ get toggled. */
1750 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1751 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1754 case CTRL_FILESELECT
:
1755 case CTRL_FONTSELECT
:
1756 /* File/font selectors have their buttons pressed (ooer),
1757 * and focus transferred to the edit box. */
1758 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1760 gtk_widget_grab_focus(sc
->uc
->entry
);
1764 * Radio buttons are fun, because they have
1765 * multiple shortcuts. We must find whether the
1766 * activated shortcut is the shortcut for the whole
1767 * group, or for a particular button. In the former
1768 * case, we find the currently selected button and
1769 * focus it; in the latter, we focus-and-click the
1770 * button whose shortcut was pressed.
1772 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1774 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1775 if (gtk_toggle_button_get_active
1776 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1777 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1779 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1781 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1782 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1783 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1784 gtk_signal_emit_by_name
1785 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1791 * If the list is really an option menu, we focus
1792 * and click it. Otherwise we tell it to focus one
1793 * of its children, which appears to do the Right
1796 if (sc
->uc
->optmenu
) {
1800 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1801 /* Option menus don't work using the "clicked" signal.
1802 * We need to manufacture a button press event :-/ */
1803 bev
.type
= GDK_BUTTON_PRESS
;
1805 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1806 "button_press_event",
1809 assert(sc
->uc
->list
!= NULL
);
1811 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1812 GTK_DIR_TAB_FORWARD
);
1823 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1825 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1827 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1828 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1830 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1831 if (widget
== dp
->treeitems
[i
])
1833 if (i
< dp
->ntreeitems
) {
1834 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1841 if (i
< 0 || i
>= dp
->ntreeitems
)
1842 break; /* nothing in that dir to select */
1844 * Determine if this tree item is visible.
1847 GtkWidget
*w
= dp
->treeitems
[i
];
1849 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1850 if (!GTK_WIDGET_VISIBLE(w
)) {
1857 j
= i
; /* got one */
1863 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1866 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1867 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1873 * It's nice for Left and Right to expand and collapse tree
1876 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1877 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1879 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1882 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1883 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1885 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1892 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1893 int chr
, int action
, void *ptr
)
1895 GtkLabel
*label
= GTK_LABEL(labelw
);
1896 gchar
*currstr
, *pattern
;
1899 if (chr
== NO_SHORTCUT
)
1902 chr
= tolower((unsigned char)chr
);
1904 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
1906 scs
->sc
[chr
].action
= action
;
1908 if (action
== SHORTCUT_FOCUS
) {
1909 scs
->sc
[chr
].uc
= NULL
;
1910 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
1912 scs
->sc
[chr
].widget
= NULL
;
1913 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
1916 gtk_label_get(label
, &currstr
);
1917 for (i
= 0; currstr
[i
]; i
++)
1918 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1921 pattern
= dupprintf("%*s_", i
, "");
1923 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1924 gtk_label_set_pattern(label
, pattern
);
1925 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1932 int get_listitemheight(void)
1934 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
1936 gtk_widget_size_request(listitem
, &req
);
1937 gtk_widget_unref(listitem
);
1941 int do_config_box(const char *title
, Config
*cfg
, int midsession
)
1943 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
1944 *tree
, *treescroll
, *panels
, *panelvbox
;
1945 int index
, level
, listitemheight
;
1946 struct controlbox
*ctrlbox
;
1948 GtkTreeItem
*treeitemlevels
[8];
1949 GtkTree
*treelevels
[8];
1952 struct Shortcuts scs
;
1954 struct selparam
*selparams
= NULL
;
1955 int nselparams
= 0, selparamsize
= 0;
1959 get_sesslist(&sl
, TRUE
);
1961 listitemheight
= get_listitemheight();
1963 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
1964 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
1967 window
= gtk_dialog_new();
1969 ctrlbox
= ctrl_new_box();
1970 setup_config_box(ctrlbox
, &sl
, midsession
, 0);
1971 unix_setup_config_box(ctrlbox
, midsession
, window
);
1973 gtk_window_set_title(GTK_WINDOW(window
), title
);
1974 hbox
= gtk_hbox_new(FALSE
, 4);
1975 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
1976 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
1977 gtk_widget_show(hbox
);
1978 vbox
= gtk_vbox_new(FALSE
, 4);
1979 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
1980 gtk_widget_show(vbox
);
1981 cols
= columns_new(4);
1982 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
1983 gtk_widget_show(cols
);
1984 label
= gtk_label_new("Category:");
1985 columns_add(COLUMNS(cols
), label
, 0, 1);
1986 columns_force_left_align(COLUMNS(cols
), label
);
1987 gtk_widget_show(label
);
1988 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
1989 tree
= gtk_tree_new();
1990 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
1991 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
1992 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
1993 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
1994 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
1995 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
1997 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
1999 GTK_POLICY_AUTOMATIC
);
2000 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
2001 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
2002 gtk_widget_show(tree
);
2003 gtk_widget_show(treescroll
);
2004 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
2005 panels
= panels_new();
2006 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
2007 gtk_widget_show(panels
);
2012 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2013 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2016 if (!*s
->pathname
) {
2017 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
2018 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2021 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
2022 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
2024 GtkWidget
*treeitem
;
2028 * We expect never to find an implicit path
2029 * component. For example, we expect never to see
2030 * A/B/C followed by A/D/E, because that would
2031 * _implicitly_ create A/D. All our path prefixes
2032 * are expected to contain actual controls and be
2033 * selectable in the treeview; so we would expect
2034 * to see A/D _explicitly_ before encountering
2037 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2039 c
= strrchr(s
->pathname
, '/');
2045 treeitem
= gtk_tree_item_new_with_label(c
);
2046 assert(j
-1 < level
);
2048 if (!treelevels
[j
-1]) {
2049 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2050 gtk_tree_item_set_subtree
2051 (treeitemlevels
[j
-1],
2052 GTK_WIDGET(treelevels
[j
-1]));
2053 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2055 gtk_tree_append(treelevels
[j
-1], treeitem
);
2057 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2059 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2060 treelevels
[j
] = NULL
;
2063 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2064 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2065 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2066 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2068 gtk_widget_show(treeitem
);
2072 first
= (panelvbox
== NULL
);
2074 panelvbox
= gtk_vbox_new(FALSE
, 4);
2075 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2077 panels_switch_to(PANELS(panels
), panelvbox
);
2078 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2081 if (nselparams
>= selparamsize
) {
2083 selparams
= sresize(selparams
, selparamsize
,
2086 selparams
[nselparams
].dp
= &dp
;
2087 selparams
[nselparams
].panels
= PANELS(panels
);
2088 selparams
[nselparams
].panel
= panelvbox
;
2089 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2090 selparams
[nselparams
].treeitem
= treeitem
;
2095 w
= layout_ctrls(&dp
,
2096 &selparams
[nselparams
-1].shortcuts
,
2097 s
, listitemheight
, NULL
);
2098 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2103 dp
.ntreeitems
= nselparams
;
2104 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2106 for (index
= 0; index
< nselparams
; index
++) {
2107 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2108 GTK_SIGNAL_FUNC(treeitem_sel
),
2110 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2114 dlg_refresh(NULL
, &dp
);
2116 dp
.shortcuts
= &selparams
[0].shortcuts
;
2117 dp
.currtreeitem
= dp
.treeitems
[0];
2118 dp
.lastfocus
= NULL
;
2122 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2123 gtk_widget_show(window
);
2126 * Set focus into the first available control.
2128 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2129 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2134 for (j
= 0; j
< s
->ncontrols
; j
++)
2135 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2136 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2137 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2138 dlg_set_focus(s
->ctrls
[j
], &dp
);
2139 dp
.lastfocus
= s
->ctrls
[j
];
2148 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2149 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2150 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2151 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2155 get_sesslist(&sl
, FALSE
);
2162 static void messagebox_handler(union control
*ctrl
, void *dlg
,
2163 void *data
, int event
)
2165 if (event
== EVENT_ACTION
)
2166 dlg_end(dlg
, ctrl
->generic
.context
.i
);
2168 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
2170 GtkWidget
*window
, *w0
, *w1
;
2171 struct controlbox
*ctrlbox
;
2172 struct controlset
*s0
, *s1
;
2175 struct Shortcuts scs
;
2181 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2182 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2185 ctrlbox
= ctrl_new_box();
2188 va_start(ap
, minwid
);
2189 while (va_arg(ap
, char *) != NULL
) {
2191 (void) va_arg(ap
, int); /* shortcut */
2192 (void) va_arg(ap
, int); /* normal/default/cancel */
2193 (void) va_arg(ap
, int); /* end value */
2197 s0
= ctrl_getset(ctrlbox
, "", "", "");
2198 c
= ctrl_columns(s0
, 2, 50, 50);
2199 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
2200 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
2201 for (index
= 0; index
< ncols
; index
++)
2202 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
2203 va_start(ap
, minwid
);
2206 char *title
= va_arg(ap
, char *);
2207 int shortcut
, type
, value
;
2210 shortcut
= va_arg(ap
, int);
2211 type
= va_arg(ap
, int);
2212 value
= va_arg(ap
, int);
2213 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
2214 messagebox_handler
, I(value
));
2215 c
->generic
.column
= index
++;
2217 c
->button
.isdefault
= TRUE
;
2219 c
->button
.iscancel
= TRUE
;
2223 s1
= ctrl_getset(ctrlbox
, "x", "", "");
2224 ctrl_text(s1
, msg
, HELPCTX(no_help
));
2226 window
= gtk_dialog_new();
2227 gtk_window_set_title(GTK_WINDOW(window
), title
);
2228 w0
= layout_ctrls(&dp
, &scs
, s0
, 0, GTK_WINDOW(window
));
2229 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2231 gtk_widget_show(w0
);
2232 w1
= layout_ctrls(&dp
, &scs
, s1
, 0, GTK_WINDOW(window
));
2233 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2234 gtk_widget_set_usize(w1
, minwid
+20, -1);
2235 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2237 gtk_widget_show(w1
);
2239 dp
.shortcuts
= &scs
;
2240 dp
.lastfocus
= NULL
;
2244 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
2246 set_transient_window_pos(parentwin
, window
);
2247 gtk_window_set_transient_for(GTK_WINDOW(window
),
2248 GTK_WINDOW(parentwin
));
2250 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2251 gtk_widget_show(window
);
2253 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2254 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2255 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2256 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2261 ctrl_free_box(ctrlbox
);
2266 static int string_width(char *text
)
2268 GtkWidget
*label
= gtk_label_new(text
);
2270 gtk_widget_size_request(label
, &req
);
2271 gtk_widget_unref(label
);
2275 int reallyclose(void *frontend
)
2277 char *title
= dupcat(appname
, " Exit Confirmation", NULL
);
2278 int ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2279 title
, "Are you sure you want to close this session?",
2280 string_width("Most of the width of the above text"),
2288 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
2289 char *keystr
, char *fingerprint
)
2291 static const char absenttxt
[] =
2292 "The server's host key is not cached. You have no guarantee "
2293 "that the server is the computer you think it is.\n"
2294 "The server's key fingerprint is:\n"
2296 "If you trust this host, press \"Accept\" to add the key to "
2297 "PuTTY's cache and carry on connecting.\n"
2298 "If you want to carry on connecting just once, without "
2299 "adding the key to the cache, press \"Connect Once\".\n"
2300 "If you do not trust this host, press \"Cancel\" to abandon the "
2302 static const char wrongtxt
[] =
2303 "WARNING - POTENTIAL SECURITY BREACH!\n"
2304 "The server's host key does not match the one PuTTY has "
2305 "cached. This means that either the server administrator "
2306 "has changed the host key, or you have actually connected "
2307 "to another computer pretending to be the server.\n"
2308 "The new key fingerprint is:\n"
2310 "If you were expecting this change and trust the new key, "
2311 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2312 "If you want to carry on connecting but without updating "
2313 "the cache, press \"Connect Once\".\n"
2314 "If you want to abandon the connection completely, press "
2315 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2323 ret
= verify_host_key(host
, port
, keytype
, keystr
);
2325 if (ret
== 0) /* success - key matched OK */
2328 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), fingerprint
);
2330 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2331 "PuTTY Security Alert", text
,
2332 string_width(fingerprint
),
2333 "Accept", 'a', 0, 2,
2334 "Connect Once", 'o', 0, 1,
2335 "Cancel", 'c', -1, 0,
2343 store_host_key(host
, port
, keytype
, keystr
);
2347 * Ask whether the selected cipher is acceptable (since it was
2348 * below the configured 'warn' threshold).
2349 * cs: 0 = both ways, 1 = client->server, 2 = server->client
2351 void askcipher(void *frontend
, char *ciphername
, int cs
)
2353 static const char msg
[] =
2354 "The first %scipher supported by the server is "
2355 "%s, which is below the configured warning threshold.\n"
2356 "Continue with connection?";
2360 text
= dupprintf(msg
, (cs
== 0) ?
"" :
2361 (cs
== 1) ?
"client-to-server " : "server-to-client ",
2363 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2364 "PuTTY Security Alert", text
,
2365 string_width("Continue with connection?"),
2378 void old_keyfile_warning(void)
2381 * This should never happen on Unix. We hope.
2385 void fatal_message_box(void *window
, char *msg
)
2387 messagebox(window
, "PuTTY Fatal Error", msg
,
2388 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2389 "OK", 'o', 1, 1, NULL
);
2392 void fatalbox(char *p
, ...)
2397 msg
= dupvprintf(p
, ap
);
2399 fatal_message_box(NULL
, msg
);
2404 static GtkWidget
*aboutbox
= NULL
;
2406 static void about_close_clicked(GtkButton
*button
, gpointer data
)
2408 gtk_widget_destroy(aboutbox
);
2412 static void licence_clicked(GtkButton
*button
, gpointer data
)
2417 "Copyright 1997-2003 Simon Tatham.\n\n"
2419 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2420 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2421 "Barry, Justin Bradford, Ben Harris, and CORE SDI S.A.\n\n"
2423 "Permission is hereby granted, free of charge, to any person "
2424 "obtaining a copy of this software and associated documentation "
2425 "files (the ""Software""), to deal in the Software without restriction, "
2426 "including without limitation the rights to use, copy, modify, merge, "
2427 "publish, distribute, sublicense, and/or sell copies of the Software, "
2428 "and to permit persons to whom the Software is furnished to do so, "
2429 "subject to the following conditions:\n\n"
2431 "The above copyright notice and this permission notice shall be "
2432 "included in all copies or substantial portions of the Software.\n\n"
2434 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2435 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2436 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2437 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2438 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
2439 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2440 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2441 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2442 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2443 "OTHER DEALINGS IN THE SOFTWARE.";
2445 title
= dupcat(appname
, " Licence", NULL
);
2446 assert(aboutbox
!= NULL
);
2447 messagebox(aboutbox
, title
, licence
,
2448 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2449 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2450 "OK", 'o', 1, 1, NULL
);
2454 void about_box(void *window
)
2460 gtk_widget_grab_focus(aboutbox
);
2464 aboutbox
= gtk_dialog_new();
2465 gtk_container_set_border_width(GTK_CONTAINER(aboutbox
), 10);
2466 title
= dupcat("About ", appname
, NULL
);
2467 gtk_window_set_title(GTK_WINDOW(aboutbox
), title
);
2470 w
= gtk_button_new_with_label("Close");
2471 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2472 gtk_window_set_default(GTK_WINDOW(aboutbox
), w
);
2473 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2474 w
, FALSE
, FALSE
, 0);
2475 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2476 GTK_SIGNAL_FUNC(about_close_clicked
), NULL
);
2479 w
= gtk_button_new_with_label("View Licence");
2480 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2481 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2482 w
, FALSE
, FALSE
, 0);
2483 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2484 GTK_SIGNAL_FUNC(licence_clicked
), NULL
);
2487 w
= gtk_label_new(appname
);
2488 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2489 w
, FALSE
, FALSE
, 0);
2492 w
= gtk_label_new(ver
);
2493 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2494 w
, FALSE
, FALSE
, 5);
2497 w
= gtk_label_new("Copyright 1997-2003 Simon Tatham. All rights reserved");
2498 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2499 w
, FALSE
, FALSE
, 5);
2502 set_transient_window_pos(GTK_WIDGET(window
), aboutbox
);
2503 gtk_widget_show(aboutbox
);
2506 struct eventlog_stuff
{
2507 GtkWidget
*parentwin
, *window
;
2508 struct controlbox
*eventbox
;
2509 struct Shortcuts scs
;
2511 union control
*listctrl
;
2513 int nevents
, negsize
;
2516 int ignore_selchange
;
2519 static void eventlog_destroy(GtkWidget
*widget
, gpointer data
)
2521 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2525 dlg_cleanup(&es
->dp
);
2526 ctrl_free_box(es
->eventbox
);
2528 static void eventlog_ok_handler(union control
*ctrl
, void *dlg
,
2529 void *data
, int event
)
2531 if (event
== EVENT_ACTION
)
2534 static void eventlog_list_handler(union control
*ctrl
, void *dlg
,
2535 void *data
, int event
)
2537 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2539 if (event
== EVENT_REFRESH
) {
2542 dlg_update_start(ctrl
, dlg
);
2543 dlg_listbox_clear(ctrl
, dlg
);
2544 for (i
= 0; i
< es
->nevents
; i
++) {
2545 dlg_listbox_add(ctrl
, dlg
, es
->events
[i
]);
2547 dlg_update_done(ctrl
, dlg
);
2548 } else if (event
== EVENT_SELCHANGE
) {
2553 * If this SELCHANGE event is happening as a result of
2554 * deliberate deselection because someone else has grabbed
2555 * the selection, the last thing we want to do is pre-empt
2558 if (es
->ignore_selchange
)
2562 * Construct the data to use as the selection.
2567 for (i
= 0; i
< es
->nevents
; i
++) {
2568 if (dlg_listbox_issel(ctrl
, dlg
, i
)) {
2569 int extralen
= strlen(es
->events
[i
]);
2571 if (es
->sellen
+ extralen
+ 2 > selsize
) {
2572 selsize
= es
->sellen
+ extralen
+ 512;
2573 es
->seldata
= sresize(es
->seldata
, selsize
, char);
2576 strcpy(es
->seldata
+ es
->sellen
, es
->events
[i
]);
2577 es
->sellen
+= extralen
;
2578 es
->seldata
[es
->sellen
++] = '\n';
2582 if (gtk_selection_owner_set(es
->window
, GDK_SELECTION_PRIMARY
,
2583 GDK_CURRENT_TIME
)) {
2584 extern GdkAtom compound_text_atom
;
2586 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2587 GDK_SELECTION_TYPE_STRING
, 1);
2588 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2589 compound_text_atom
, 1);
2595 void eventlog_selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
2596 guint info
, guint time_stamp
, gpointer data
)
2598 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2600 gtk_selection_data_set(seldata
, seldata
->target
, 8,
2601 es
->seldata
, es
->sellen
);
2604 gint
eventlog_selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
2607 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2611 * Deselect everything in the list box.
2613 uc
= dlg_find_byctrl(&es
->dp
, es
->listctrl
);
2614 es
->ignore_selchange
= 1;
2615 gtk_list_unselect_all(GTK_LIST(uc
->list
));
2616 es
->ignore_selchange
= 0;
2624 void showeventlog(void *estuff
, void *parentwin
)
2626 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2627 GtkWidget
*window
, *w0
, *w1
;
2628 GtkWidget
*parent
= GTK_WIDGET(parentwin
);
2629 struct controlset
*s0
, *s1
;
2631 int listitemheight
, index
;
2635 gtk_widget_grab_focus(es
->window
);
2641 for (index
= 0; index
< lenof(es
->scs
.sc
); index
++) {
2642 es
->scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2645 es
->eventbox
= ctrl_new_box();
2647 s0
= ctrl_getset(es
->eventbox
, "", "", "");
2648 ctrl_columns(s0
, 3, 33, 34, 33);
2649 c
= ctrl_pushbutton(s0
, "Close", 'c', HELPCTX(no_help
),
2650 eventlog_ok_handler
, P(NULL
));
2651 c
->button
.column
= 1;
2652 c
->button
.isdefault
= TRUE
;
2654 s1
= ctrl_getset(es
->eventbox
, "x", "", "");
2655 es
->listctrl
= c
= ctrl_listbox(s1
, NULL
, NO_SHORTCUT
, HELPCTX(no_help
),
2656 eventlog_list_handler
, P(es
));
2657 c
->listbox
.height
= 10;
2658 c
->listbox
.multisel
= 2;
2659 c
->listbox
.ncols
= 3;
2660 c
->listbox
.percentages
= snewn(3, int);
2661 c
->listbox
.percentages
[0] = 25;
2662 c
->listbox
.percentages
[1] = 10;
2663 c
->listbox
.percentages
[2] = 65;
2665 listitemheight
= get_listitemheight();
2667 es
->window
= window
= gtk_dialog_new();
2668 title
= dupcat(appname
, " Event Log", NULL
);
2669 gtk_window_set_title(GTK_WINDOW(window
), title
);
2671 w0
= layout_ctrls(&es
->dp
, &es
->scs
, s0
,
2672 listitemheight
, GTK_WINDOW(window
));
2673 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2675 gtk_widget_show(w0
);
2676 w1
= layout_ctrls(&es
->dp
, &es
->scs
, s1
,
2677 listitemheight
, GTK_WINDOW(window
));
2678 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2679 gtk_widget_set_usize(w1
, 20 +
2680 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
2681 " IS QUITE LONG 'COS SSH LOG ENTRIES"
2683 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2685 gtk_widget_show(w1
);
2688 es
->dp
.shortcuts
= &es
->scs
;
2689 es
->dp
.lastfocus
= NULL
;
2691 es
->dp
.window
= window
;
2693 dlg_refresh(NULL
, &es
->dp
);
2696 set_transient_window_pos(parent
, window
);
2697 gtk_window_set_transient_for(GTK_WINDOW(window
),
2698 GTK_WINDOW(parent
));
2700 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2701 gtk_widget_show(window
);
2703 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2704 GTK_SIGNAL_FUNC(eventlog_destroy
), es
);
2705 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2706 GTK_SIGNAL_FUNC(win_key_press
), &es
->dp
);
2707 gtk_signal_connect(GTK_OBJECT(window
), "selection_get",
2708 GTK_SIGNAL_FUNC(eventlog_selection_get
), es
);
2709 gtk_signal_connect(GTK_OBJECT(window
), "selection_clear_event",
2710 GTK_SIGNAL_FUNC(eventlog_selection_clear
), es
);
2713 void *eventlogstuff_new(void)
2715 struct eventlog_stuff
*es
;
2716 es
= snew(struct eventlog_stuff
);
2717 memset(es
, 0, sizeof(*es
));
2721 void logevent_dlg(void *estuff
, char *string
)
2723 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2728 if (es
->nevents
>= es
->negsize
) {
2730 es
->events
= sresize(es
->events
, es
->negsize
, char *);
2734 strftime(timebuf
, sizeof(timebuf
), "%Y-%m-%d %H:%M:%S\t",
2737 es
->events
[es
->nevents
] = snewn(strlen(timebuf
) + strlen(string
) + 1, char);
2738 strcpy(es
->events
[es
->nevents
], timebuf
);
2739 strcat(es
->events
[es
->nevents
], string
);
2741 dlg_listbox_add(es
->listctrl
, &es
->dp
, es
->events
[es
->nevents
]);
2746 int askappend(void *frontend
, Filename filename
)
2748 static const char msgtemplate
[] =
2749 "The session log file \"%.*s\" already exists. "
2750 "You can overwrite it with a new session log, "
2751 "append your session log to the end of it, "
2752 "or disable session logging for this session.";
2757 message
= dupprintf(msgtemplate
, FILENAME_MAX
, filename
.path
);
2758 mbtitle
= dupprintf("%s Log to File", appname
);
2760 mbret
= messagebox(get_window(frontend
), mbtitle
, message
,
2761 string_width("LINE OF TEXT SUITABLE FOR THE"
2762 " ASKAPPEND WIDTH"),
2763 "Overwrite", 'o', 1, 2,
2764 "Append", 'a', 0, 1,
2765 "Disable", 'd', -1, 0,