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>
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
42 struct Shortcut sc
[128];
49 int privdata_needs_free
;
50 GtkWidget
**buttons
; int nbuttons
; /* for radio buttons */
51 GtkWidget
*entry
; /* for editbox, combobox, filesel, fontsel */
52 GtkWidget
*button
; /* for filesel, fontsel */
53 GtkWidget
*list
; /* for combobox, listbox */
54 GtkWidget
*menu
; /* for optionmenu (==droplist) */
55 GtkWidget
*optmenu
; /* also for optionmenu */
56 GtkWidget
*text
; /* for text */
57 GtkWidget
*label
; /* for dlg_label_change */
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 void shortcut_highlight(GtkWidget
*label
, int chr
);
94 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
96 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
98 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
100 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
101 static void coloursel_ok(GtkButton
*button
, gpointer data
);
102 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
103 static void window_destroy(GtkWidget
*widget
, gpointer data
);
105 static int uctrl_cmp_byctrl(void *av
, void *bv
)
107 struct uctrl
*a
= (struct uctrl
*)av
;
108 struct uctrl
*b
= (struct uctrl
*)bv
;
109 if (a
->ctrl
< b
->ctrl
)
111 else if (a
->ctrl
> b
->ctrl
)
116 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
118 union control
*a
= (union control
*)av
;
119 struct uctrl
*b
= (struct uctrl
*)bv
;
122 else if (a
> b
->ctrl
)
127 static int uctrl_cmp_bywidget(void *av
, void *bv
)
129 struct uctrl
*a
= (struct uctrl
*)av
;
130 struct uctrl
*b
= (struct uctrl
*)bv
;
131 if (a
->toplevel
< b
->toplevel
)
133 else if (a
->toplevel
> b
->toplevel
)
138 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
140 GtkWidget
*a
= (GtkWidget
*)av
;
141 struct uctrl
*b
= (struct uctrl
*)bv
;
144 else if (a
> b
->toplevel
)
149 static void dlg_init(struct dlgparam
*dp
)
151 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
152 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
153 dp
->coloursel_result
.ok
= FALSE
;
154 dp
->treeitems
= NULL
;
155 dp
->window
= dp
->cancelbutton
= dp
->currtreeitem
= NULL
;
157 dp
->currfocus
= NULL
;
160 static void dlg_cleanup(struct dlgparam
*dp
)
164 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
166 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
167 del234(dp
->bywidget
, uc
);
168 if (uc
->privdata_needs_free
)
173 freetree234(dp
->bywidget
);
175 sfree(dp
->treeitems
);
178 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
180 add234(dp
->byctrl
, uc
);
181 add234(dp
->bywidget
, uc
);
184 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
188 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
191 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
193 struct uctrl
*ret
= NULL
;
197 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
205 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
207 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
208 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
212 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
214 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
215 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
217 uc
->privdata_needs_free
= FALSE
;
220 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
222 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
223 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
225 * This is an internal allocation routine, so it's allowed to
226 * use smalloc directly.
228 uc
->privdata
= smalloc(size
);
229 uc
->privdata_needs_free
= FALSE
;
233 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
235 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
236 if (dp
->currfocus
!= ctrl
)
237 return dp
->currfocus
;
239 return dp
->lastfocus
;
242 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
244 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
245 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
246 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
247 assert(uc
->buttons
!= NULL
);
248 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
251 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
253 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
254 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
257 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
258 assert(uc
->buttons
!= NULL
);
259 for (i
= 0; i
< uc
->nbuttons
; i
++)
260 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
262 return 0; /* got to return something */
265 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
267 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
268 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
269 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
270 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
273 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
275 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
276 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
277 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
278 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
281 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
283 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
284 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
285 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
286 assert(uc
->entry
!= NULL
);
287 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
290 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
292 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
293 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
294 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
295 assert(uc
->entry
!= NULL
);
296 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
298 buffer
[length
-1] = '\0';
301 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
303 GtkContainer
*cont
= GTK_CONTAINER(data
);
304 /* gtk_container_remove will unref the widget for us; we need not. */
305 gtk_container_remove(cont
, w
);
308 /* The `listbox' functions can also apply to combo boxes. */
309 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
311 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
312 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
314 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
315 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
316 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
319 gtk_container_foreach(GTK_CONTAINER(uc
->menu
),
320 container_remove_and_destroy
,
321 GTK_CONTAINER(uc
->menu
));
323 gtk_list_clear_items(GTK_LIST(uc
->list
), 0, -1);
327 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
329 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
330 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
332 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
333 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
334 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
338 (GTK_CONTAINER(uc
->menu
),
339 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
341 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
345 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
347 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
351 * Each listbox entry may have a numeric id associated with it.
352 * Note that some front ends only permit a string to be stored at
353 * each position, which means that _if_ you put two identical
354 * strings in any listbox then you MUST not assign them different
355 * IDs and expect to get meaningful results back.
357 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
358 char const *text
, int id
)
360 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
361 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
363 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
364 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
365 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
367 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
371 * List item in a drop-down (but non-combo) list. Tabs are
372 * ignored; we just provide a standard menu item with the
375 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
377 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
378 gtk_widget_show(menuitem
);
380 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
381 GINT_TO_POINTER(id
));
382 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
383 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
384 } else if (!uc
->entry
) {
386 * List item in a non-combo-box list box. We make all of
387 * these Columns containing GtkLabels. This allows us to do
388 * the nasty force_left hack irrespective of whether there
389 * are tabs in the thing.
391 GtkWidget
*listitem
= gtk_list_item_new();
392 GtkWidget
*cols
= columns_new(10);
396 /* Count the tabs in the text, and hence determine # of columns. */
398 for (i
= 0; text
[i
]; i
++)
403 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
404 percents
= snewn(ncols
, gint
);
405 percents
[ncols
-1] = 100;
406 for (i
= 0; i
< ncols
-1; i
++) {
407 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
408 percents
[ncols
-1] -= percents
[i
];
410 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
413 for (i
= 0; i
< ncols
; i
++) {
414 int len
= strcspn(text
, "\t");
415 char *dup
= dupprintf("%.*s", len
, text
);
420 label
= gtk_label_new(dup
);
423 columns_add(COLUMNS(cols
), label
, i
, 1);
424 columns_force_left_align(COLUMNS(cols
), label
);
425 gtk_widget_show(label
);
427 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
428 gtk_widget_show(cols
);
429 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
430 gtk_widget_show(listitem
);
432 if (ctrl
->listbox
.multisel
) {
433 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
434 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
436 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
437 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
439 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
440 GTK_SIGNAL_FUNC(widget_focus
), dp
);
441 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
442 GTK_SIGNAL_FUNC(listitem_button
), dp
);
443 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
444 GINT_TO_POINTER(id
));
447 * List item in a combo-box list, which means the sensible
448 * thing to do is make it a perfectly normal label. Hence
449 * tabs are disregarded.
451 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
453 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
454 gtk_widget_show(listitem
);
456 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
457 GINT_TO_POINTER(id
));
460 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
463 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
465 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
466 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
470 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
471 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
472 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
474 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
476 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
477 g_list_free(children
);
479 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
), "user-data"));
482 /* dlg_listbox_index returns <0 if no single element is selected. */
483 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
485 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
486 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
488 GtkWidget
*item
, *activeitem
;
492 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
493 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
494 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
497 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
499 activeitem
= NULL
; /* unnecessarily placate gcc */
501 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
503 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
504 i
++, children
= children
->next
) {
505 if (uc
->menu ? activeitem
== item
:
506 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
513 g_list_free(children
);
514 return selected
< 0 ?
-1 : selected
;
517 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
519 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
520 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
522 GtkWidget
*item
, *activeitem
;
524 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
525 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
526 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
528 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
530 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
531 g_list_free(children
);
534 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
535 return item
== activeitem
;
537 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
541 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
543 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
544 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
546 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
547 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
548 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
551 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
555 gdouble newtop
, newbot
;
557 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
560 * Scroll the list box if necessary to ensure the newly
561 * selected item is visible.
563 items
= gtk_container_children(GTK_CONTAINER(uc
->list
));
564 nitems
= g_list_length(items
);
566 int modified
= FALSE
;
568 newtop
= uc
->adj
->lower
+
569 (uc
->adj
->upper
- uc
->adj
->lower
) * index
/ nitems
;
570 newbot
= uc
->adj
->lower
+
571 (uc
->adj
->upper
- uc
->adj
->lower
) * (index
+1) / nitems
;
572 if (uc
->adj
->value
> newtop
) {
574 uc
->adj
->value
= newtop
;
575 } else if (uc
->adj
->value
< newbot
- uc
->adj
->page_size
) {
577 uc
->adj
->value
= newbot
- uc
->adj
->page_size
;
580 gtk_adjustment_value_changed(uc
->adj
);
585 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
587 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
588 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
590 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
591 assert(uc
->text
!= NULL
);
593 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
596 void dlg_label_change(union control
*ctrl
, void *dlg
, char const *text
)
598 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
599 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
601 switch (uc
->ctrl
->generic
.type
) {
603 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
604 shortcut_highlight(uc
->toplevel
, ctrl
->button
.shortcut
);
607 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
608 shortcut_highlight(uc
->toplevel
, ctrl
->checkbox
.shortcut
);
611 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
612 shortcut_highlight(uc
->label
, ctrl
->radio
.shortcut
);
615 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
616 shortcut_highlight(uc
->label
, ctrl
->editbox
.shortcut
);
618 case CTRL_FILESELECT
:
619 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
620 shortcut_highlight(uc
->label
, ctrl
->fileselect
.shortcut
);
622 case CTRL_FONTSELECT
:
623 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
624 shortcut_highlight(uc
->label
, ctrl
->fontselect
.shortcut
);
627 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
628 shortcut_highlight(uc
->label
, ctrl
->listbox
.shortcut
);
631 assert(!"This shouldn't happen");
636 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
638 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
639 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
640 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
641 assert(uc
->entry
!= NULL
);
642 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
645 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
647 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
648 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
649 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
650 assert(uc
->entry
!= NULL
);
651 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
653 fn
->path
[lenof(fn
->path
)-1] = '\0';
656 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
658 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
659 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
660 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
661 assert(uc
->entry
!= NULL
);
662 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
665 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
667 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
668 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
669 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
670 assert(uc
->entry
!= NULL
);
671 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
673 fs
->name
[lenof(fs
->name
)-1] = '\0';
677 * Bracketing a large set of updates in these two functions will
678 * cause the front end (if possible) to delay updating the screen
679 * until it's all complete, thus avoiding flicker.
681 void dlg_update_start(union control
*ctrl
, void *dlg
)
684 * Apparently we can't do this at all in GTK. GtkCList supports
685 * freeze and thaw, but not GtkList. Bah.
689 void dlg_update_done(union control
*ctrl
, void *dlg
)
692 * Apparently we can't do this at all in GTK. GtkCList supports
693 * freeze and thaw, but not GtkList. Bah.
697 void dlg_set_focus(union control
*ctrl
, void *dlg
)
699 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
700 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
702 switch (ctrl
->generic
.type
) {
705 /* Check boxes and buttons get the focus _and_ get toggled. */
706 gtk_widget_grab_focus(uc
->toplevel
);
708 case CTRL_FILESELECT
:
709 case CTRL_FONTSELECT
:
711 /* Anything containing an edit box gets that focused. */
712 gtk_widget_grab_focus(uc
->entry
);
716 * Radio buttons: we find the currently selected button and
721 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
722 if (gtk_toggle_button_get_active
723 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
724 gtk_widget_grab_focus(uc
->buttons
[i
]);
730 * If the list is really an option menu, we focus it.
731 * Otherwise we tell it to focus one of its children, which
732 * appears to do the Right Thing.
735 gtk_widget_grab_focus(uc
->optmenu
);
737 assert(uc
->list
!= NULL
);
738 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
745 * During event processing, you might well want to give an error
746 * indication to the user. dlg_beep() is a quick and easy generic
747 * error; dlg_error() puts up a message-box or equivalent.
749 void dlg_beep(void *dlg
)
754 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
756 gtk_widget_destroy(GTK_WIDGET(data
));
759 static void set_transient_window_pos(GtkWidget
*parent
, GtkWidget
*child
)
761 gint x
, y
, w
, h
, dx
, dy
;
763 gtk_window_set_position(GTK_WINDOW(child
), GTK_WIN_POS_NONE
);
764 gtk_widget_size_request(GTK_WIDGET(child
), &req
);
766 gdk_window_get_origin(GTK_WIDGET(parent
)->window
, &x
, &y
);
767 gdk_window_get_size(GTK_WIDGET(parent
)->window
, &w
, &h
);
770 * One corner of the transient will be offset inwards, by 1/4
771 * of the parent window's size, from the corresponding corner
772 * of the parent window. The corner will be chosen so as to
773 * place the transient closer to the centre of the screen; this
774 * should avoid transients going off the edge of the screen on
777 if (x
+ w
/2 < gdk_screen_width() / 2)
778 dx
= x
+ w
/4; /* work from left edges */
780 dx
= x
+ 3*w
/4 - req
.width
; /* work from right edges */
781 if (y
+ h
/2 < gdk_screen_height() / 2)
782 dy
= y
+ h
/4; /* work from top edges */
784 dy
= y
+ 3*h
/4 - req
.height
; /* work from bottom edges */
785 gtk_widget_set_uposition(GTK_WIDGET(child
), dx
, dy
);
788 void dlg_error_msg(void *dlg
, char *msg
)
790 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
791 GtkWidget
*window
, *hbox
, *text
, *ok
;
793 window
= gtk_dialog_new();
794 text
= gtk_label_new(msg
);
795 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
796 hbox
= gtk_hbox_new(FALSE
, 0);
797 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
798 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
799 hbox
, FALSE
, FALSE
, 20);
800 gtk_widget_show(text
);
801 gtk_widget_show(hbox
);
802 gtk_window_set_title(GTK_WINDOW(window
), "Error");
803 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
804 ok
= gtk_button_new_with_label("OK");
805 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
806 ok
, FALSE
, FALSE
, 0);
808 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
809 gtk_window_set_default(GTK_WINDOW(window
), ok
);
810 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
811 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
812 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
813 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
814 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
815 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
816 set_transient_window_pos(dp
->window
, window
);
817 gtk_widget_show(window
);
822 * This function signals to the front end that the dialog's
823 * processing is completed, and passes an integer value (typically
826 void dlg_end(void *dlg
, int value
)
828 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
830 gtk_widget_destroy(dp
->window
);
833 void dlg_refresh(union control
*ctrl
, void *dlg
)
835 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
839 if (ctrl
->generic
.handler
!= NULL
)
840 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
844 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
845 assert(uc
->ctrl
!= NULL
);
846 if (uc
->ctrl
->generic
.handler
!= NULL
)
847 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
848 dp
->data
, EVENT_REFRESH
);
853 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
855 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
856 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
859 GtkWidget
*coloursel
=
860 gtk_color_selection_dialog_new("Select a colour");
861 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
863 dp
->coloursel_result
.ok
= FALSE
;
865 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
866 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
867 cvals
[0] = r
/ 255.0;
868 cvals
[1] = g
/ 255.0;
869 cvals
[2] = b
/ 255.0;
870 cvals
[3] = 1.0; /* fully opaque! */
871 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
873 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
874 (gpointer
)coloursel
);
875 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
876 (gpointer
)coloursel
);
877 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
878 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
879 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
880 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
881 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
882 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
883 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
884 (gpointer
)coloursel
);
885 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
886 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
887 (gpointer
)coloursel
);
888 gtk_widget_show(coloursel
);
891 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
892 int *r
, int *g
, int *b
)
894 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
895 if (dp
->coloursel_result
.ok
) {
896 *r
= dp
->coloursel_result
.r
;
897 *g
= dp
->coloursel_result
.g
;
898 *b
= dp
->coloursel_result
.b
;
904 /* ----------------------------------------------------------------------
905 * Signal handlers while the dialog box is active.
908 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
911 struct dlgparam
*dp
= (struct dlgparam
*)data
;
912 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
913 union control
*focus
;
920 if (focus
!= dp
->currfocus
) {
921 dp
->lastfocus
= dp
->currfocus
;
922 dp
->currfocus
= focus
;
928 static void button_clicked(GtkButton
*button
, gpointer data
)
930 struct dlgparam
*dp
= (struct dlgparam
*)data
;
931 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
932 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
935 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
937 struct dlgparam
*dp
= (struct dlgparam
*)data
;
938 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
939 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
942 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
945 * GtkEntry has a nasty habit of eating the Return key, which
946 * is unhelpful since it doesn't actually _do_ anything with it
947 * (it calls gtk_widget_activate, but our edit boxes never need
948 * activating). So I catch Return before GtkEntry sees it, and
949 * pass it straight on to the parent widget. Effect: hitting
950 * Return in an edit box will now activate the default button
951 * in the dialog just like it will everywhere else.
953 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
955 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
956 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
963 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
965 struct dlgparam
*dp
= (struct dlgparam
*)data
;
966 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
967 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
968 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
972 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
975 struct dlgparam
*dp
= (struct dlgparam
*)data
;
976 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
977 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
980 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
983 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
985 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
986 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
987 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
988 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
990 * Up, Down, PgUp or PgDn have been pressed on a ListItem
991 * in a list box. So, if the list box is single-selection:
993 * - if the list item in question isn't already selected,
994 * we simply select it.
995 * - otherwise, we find the next one (or next
996 * however-far-away) in whichever direction we're going,
998 * + in this case, we must also fiddle with the
999 * scrollbar to ensure the newly selected item is
1002 * If it's multiple-selection, we do all of the above
1003 * except actually selecting anything, so we move the focus
1004 * and fiddle the scrollbar to follow it.
1006 GtkWidget
*list
= item
->parent
;
1008 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
1011 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
1012 gtk_list_select_child(GTK_LIST(list
), item
);
1015 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
1016 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1019 (event
->keyval
==GDK_Page_Down
||
1020 event
->keyval
==GDK_KP_Page_Down
||
1021 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1024 GList
*children
, *chead
;
1026 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
1028 n
= g_list_length(children
);
1032 * Figure out how many list items to a screenful,
1033 * and adjust the step appropriately.
1035 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
1036 step
--; /* go by one less than that */
1040 while (children
!= NULL
) {
1041 if (item
== children
->data
)
1043 children
= children
->next
;
1048 if (direction
< 0 && i
> 0)
1049 children
= children
->prev
, i
--;
1050 else if (direction
> 0 && i
< n
-1)
1051 children
= children
->next
, i
++;
1055 if (children
&& children
->data
) {
1057 gtk_list_select_child(GTK_LIST(list
),
1058 GTK_WIDGET(children
->data
));
1059 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
1060 gtk_adjustment_clamp_page
1062 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
1063 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
1074 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
1077 return listitem_key(item
, event
, data
, FALSE
);
1080 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
1083 return listitem_key(item
, event
, data
, TRUE
);
1086 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
1089 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1090 if (event
->type
== GDK_2BUTTON_PRESS
||
1091 event
->type
== GDK_3BUTTON_PRESS
) {
1092 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1093 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1099 static void list_selchange(GtkList
*list
, gpointer data
)
1101 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1102 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1104 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1107 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1109 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1110 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1111 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1112 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1113 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1116 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1118 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1119 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1123 (index
== 0 && direction
< 0) ||
1124 (index
== g_list_length(children
)-1 && direction
> 0)) {
1129 child
= g_list_nth_data(children
, index
);
1130 gtk_widget_ref(child
);
1131 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1132 g_list_free(children
);
1135 children
= g_list_append(children
, child
);
1136 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1137 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1138 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1141 static void draglist_up(GtkButton
*button
, gpointer data
)
1143 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1144 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1145 draglist_move(dp
, uc
, -1);
1148 static void draglist_down(GtkButton
*button
, gpointer data
)
1150 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1151 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1152 draglist_move(dp
, uc
, +1);
1155 static void filesel_ok(GtkButton
*button
, gpointer data
)
1157 /* struct dlgparam *dp = (struct dlgparam *)data; */
1158 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1159 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1160 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1161 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1164 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1166 /* struct dlgparam *dp = (struct dlgparam *)data; */
1167 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1168 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1169 char *name
= gtk_font_selection_dialog_get_font_name
1170 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1171 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1174 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1176 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1177 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1178 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1180 gtk_color_selection_get_color
1181 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1183 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1184 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1185 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1186 dp
->coloursel_result
.ok
= TRUE
;
1187 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1190 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1192 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1193 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1194 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1195 dp
->coloursel_result
.ok
= FALSE
;
1196 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1199 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1201 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1202 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1204 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1205 GtkWidget
*filesel
=
1206 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1207 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1209 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1211 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1213 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1214 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1215 gtk_signal_connect_object
1216 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1217 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1218 gtk_signal_connect_object
1219 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1220 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1221 gtk_widget_show(filesel
);
1224 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1225 gchar
*spacings
[] = { "c", "m", NULL
};
1226 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1227 GtkWidget
*fontsel
=
1228 gtk_font_selection_dialog_new("Select a font");
1229 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1230 gtk_font_selection_dialog_set_filter
1231 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1232 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1233 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1234 if (!gtk_font_selection_dialog_set_font_name
1235 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1237 * If the font name wasn't found as it was, try opening
1238 * it and extracting its FONT property. This should
1239 * have the effect of mapping short aliases into true
1242 GdkFont
*font
= gdk_font_load(fontname
);
1244 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1245 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1246 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1248 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1249 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1251 gtk_font_selection_dialog_set_font_name
1252 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1254 gdk_font_unref(font
);
1258 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1259 "user-data", (gpointer
)fontsel
);
1260 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1262 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1263 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1264 gtk_signal_connect_object
1265 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1266 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1268 gtk_signal_connect_object
1269 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1270 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1272 gtk_widget_show(fontsel
);
1276 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1279 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1280 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1282 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1283 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1284 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1287 /* ----------------------------------------------------------------------
1288 * This function does the main layout work: it reads a controlset,
1289 * it creates the relevant GTK controls, and returns a GtkWidget
1290 * containing the result. (This widget might be a title of some
1291 * sort, it might be a Columns containing many controls, or it
1292 * might be a GtkFrame containing a Columns; whatever it is, it's
1293 * definitely a GtkWidget and should probably be added to a
1296 * `listitemheight' is used to calculate a usize for list boxes: it
1297 * should be the height from the size request of a GtkListItem.
1299 * `win' is required for setting the default button. If it is
1300 * non-NULL, all buttons created will be default-capable (so they
1301 * have extra space round them for the default highlight).
1303 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1304 struct controlset
*s
, int listitemheight
,
1311 if (!s
->boxname
&& s
->boxtitle
) {
1312 /* This controlset is a panel title. */
1313 return gtk_label_new(s
->boxtitle
);
1317 * Otherwise, we expect to be laying out actual controls, so
1318 * we'll start by creating a Columns for the purpose.
1320 cols
= COLUMNS(columns_new(4));
1321 ret
= GTK_WIDGET(cols
);
1322 gtk_widget_show(ret
);
1325 * Create a containing frame if we have a box name.
1328 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1329 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1330 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1331 gtk_widget_show(ret
);
1335 * Now iterate through the controls themselves, create them,
1336 * and add them to the Columns.
1338 for (i
= 0; i
< s
->ncontrols
; i
++) {
1339 union control
*ctrl
= s
->ctrls
[i
];
1342 GtkWidget
*w
= NULL
;
1344 switch (ctrl
->generic
.type
) {
1347 static const int simplecols
[1] = { 100 };
1348 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1349 (ctrl
->columns
.percentages ?
1350 ctrl
->columns
.percentages
: simplecols
));
1352 continue; /* no actual control created */
1355 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1357 columns_taborder_last(cols
, uc
->toplevel
);
1359 continue; /* no actual control created */
1362 uc
= snew(struct uctrl
);
1364 uc
->privdata
= NULL
;
1365 uc
->privdata_needs_free
= FALSE
;
1367 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1368 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1371 switch (ctrl
->generic
.type
) {
1373 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1375 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1376 if (ctrl
->button
.isdefault
)
1377 gtk_window_set_default(win
, w
);
1378 if (ctrl
->button
.iscancel
)
1379 dp
->cancelbutton
= w
;
1381 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1382 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1383 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1384 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1385 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1386 SHORTCUT_UCTRL
, uc
);
1389 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1390 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1391 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1392 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1393 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1394 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1395 SHORTCUT_UCTRL
, uc
);
1400 * Radio buttons get to go inside their own Columns, no
1404 gint i
, *percentages
;
1408 if (ctrl
->generic
.label
) {
1409 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1410 columns_add(COLUMNS(w
), label
, 0, 1);
1411 columns_force_left_align(COLUMNS(w
), label
);
1412 gtk_widget_show(label
);
1413 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1414 SHORTCUT_UCTRL
, uc
);
1417 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1418 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1420 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1421 100 * i
/ ctrl
->radio
.ncolumns
);
1423 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1425 g_free(percentages
);
1428 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1429 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1431 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1435 b
= (gtk_radio_button_new_with_label
1436 (group
, ctrl
->radio
.buttons
[i
]));
1438 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1439 colstart
= i
% ctrl
->radio
.ncolumns
;
1440 columns_add(COLUMNS(w
), b
, colstart
,
1441 (i
== ctrl
->radio
.nbuttons
-1 ?
1442 ctrl
->radio
.ncolumns
- colstart
: 1));
1443 columns_force_left_align(COLUMNS(w
), b
);
1445 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1446 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1447 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1448 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1449 if (ctrl
->radio
.shortcuts
) {
1450 shortcut_add(scs
, GTK_BIN(b
)->child
,
1451 ctrl
->radio
.shortcuts
[i
],
1452 SHORTCUT_UCTRL
, uc
);
1461 if (ctrl
->editbox
.has_list
) {
1462 w
= gtk_combo_new();
1463 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1464 uc
->entry
= GTK_COMBO(w
)->entry
;
1465 uc
->list
= GTK_COMBO(w
)->list
;
1467 w
= gtk_entry_new();
1468 if (ctrl
->editbox
.password
)
1469 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1472 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1473 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1474 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1475 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1476 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1477 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1479 * Edit boxes, for some strange reason, have a minimum
1480 * width of 150 in GTK 1.2. We don't want this - we'd
1481 * rather the edit boxes acquired their natural width
1482 * from the column layout of the rest of the box.
1484 * Also, while we're here, we'll squirrel away the
1485 * edit box height so we can use that to centre its
1486 * label vertically beside it.
1488 gtk_widget_size_request(w
, &req
);
1489 gtk_widget_set_usize(w
, 10, req
.height
);
1491 if (ctrl
->generic
.label
) {
1492 GtkWidget
*label
, *container
;
1494 label
= gtk_label_new(ctrl
->generic
.label
);
1496 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1497 SHORTCUT_FOCUS
, uc
->entry
);
1499 container
= columns_new(4);
1500 if (ctrl
->editbox
.percentwidth
== 100) {
1501 columns_add(COLUMNS(container
), label
, 0, 1);
1502 columns_force_left_align(COLUMNS(container
), label
);
1503 columns_add(COLUMNS(container
), w
, 0, 1);
1505 gint percentages
[2];
1506 percentages
[1] = ctrl
->editbox
.percentwidth
;
1507 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1508 columns_set_cols(COLUMNS(container
), 2, percentages
);
1509 columns_add(COLUMNS(container
), label
, 0, 1);
1510 columns_force_left_align(COLUMNS(container
), label
);
1511 columns_add(COLUMNS(container
), w
, 1, 1);
1512 /* Centre the label vertically. */
1513 gtk_widget_set_usize(label
, -1, req
.height
);
1514 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1516 gtk_widget_show(label
);
1522 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1523 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1526 case CTRL_FILESELECT
:
1527 case CTRL_FONTSELECT
:
1532 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1533 "Browse..." : "Change...");
1535 gint percentages
[] = { 75, 25 };
1537 columns_set_cols(COLUMNS(w
), 2, percentages
);
1539 if (ctrl
->generic
.label
) {
1540 ww
= gtk_label_new(ctrl
->generic
.label
);
1541 columns_add(COLUMNS(w
), ww
, 0, 2);
1542 columns_force_left_align(COLUMNS(w
), ww
);
1543 gtk_widget_show(ww
);
1544 shortcut_add(scs
, ww
,
1545 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1546 ctrl
->fileselect
.shortcut
:
1547 ctrl
->fontselect
.shortcut
),
1548 SHORTCUT_UCTRL
, uc
);
1552 uc
->entry
= ww
= gtk_entry_new();
1553 gtk_widget_size_request(ww
, &req
);
1554 gtk_widget_set_usize(ww
, 10, req
.height
);
1555 columns_add(COLUMNS(w
), ww
, 0, 1);
1556 gtk_widget_show(ww
);
1558 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1559 columns_add(COLUMNS(w
), ww
, 1, 1);
1560 gtk_widget_show(ww
);
1562 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1563 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1564 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1565 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1566 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1567 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1568 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1569 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1570 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1571 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1575 if (ctrl
->listbox
.height
== 0) {
1576 uc
->optmenu
= w
= gtk_option_menu_new();
1577 uc
->menu
= gtk_menu_new();
1578 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1579 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1580 (gpointer
)uc
->optmenu
);
1581 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1582 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1584 uc
->list
= gtk_list_new();
1585 if (ctrl
->listbox
.multisel
== 2) {
1586 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1587 GTK_SELECTION_EXTENDED
);
1588 } else if (ctrl
->listbox
.multisel
== 1) {
1589 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1590 GTK_SELECTION_MULTIPLE
);
1592 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1593 GTK_SELECTION_SINGLE
);
1595 w
= gtk_scrolled_window_new(NULL
, NULL
);
1596 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1598 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1600 GTK_POLICY_AUTOMATIC
);
1601 uc
->adj
= gtk_scrolled_window_get_vadjustment
1602 (GTK_SCROLLED_WINDOW(w
));
1604 gtk_widget_show(uc
->list
);
1605 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1606 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1607 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1608 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1611 * Adjust the height of the scrolled window to the
1612 * minimum given by the height parameter.
1614 * This piece of guesswork is a horrid hack based
1615 * on looking inside the GTK 1.2 sources
1616 * (specifically gtkviewport.c, which appears to be
1617 * the widget which provides the border around the
1618 * scrolling area). Anyone lets me know how I can
1619 * do this in a way which isn't at risk from GTK
1620 * upgrades, I'd be grateful.
1623 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1624 gtk_widget_set_usize(w
, 10,
1625 2*edge
+ (ctrl
->listbox
.height
*
1629 if (ctrl
->listbox
.draglist
) {
1631 * GTK doesn't appear to make it easy to
1632 * implement a proper draggable list; so
1633 * instead I'm just going to have to put an Up
1634 * and a Down button to the right of the actual
1635 * list box. Ah well.
1637 GtkWidget
*cols
, *button
;
1638 static const gint percentages
[2] = { 80, 20 };
1640 cols
= columns_new(4);
1641 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1642 columns_add(COLUMNS(cols
), w
, 0, 1);
1644 button
= gtk_button_new_with_label("Up");
1645 columns_add(COLUMNS(cols
), button
, 1, 1);
1646 gtk_widget_show(button
);
1647 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1648 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1649 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1650 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1651 button
= gtk_button_new_with_label("Down");
1652 columns_add(COLUMNS(cols
), button
, 1, 1);
1653 gtk_widget_show(button
);
1654 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1655 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1656 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1657 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1663 if (ctrl
->generic
.label
) {
1664 GtkWidget
*label
, *container
;
1666 label
= gtk_label_new(ctrl
->generic
.label
);
1668 container
= columns_new(4);
1669 if (ctrl
->listbox
.percentwidth
== 100) {
1670 columns_add(COLUMNS(container
), label
, 0, 1);
1671 columns_force_left_align(COLUMNS(container
), label
);
1672 columns_add(COLUMNS(container
), w
, 0, 1);
1674 gint percentages
[2];
1675 percentages
[1] = ctrl
->listbox
.percentwidth
;
1676 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1677 columns_set_cols(COLUMNS(container
), 2, percentages
);
1678 columns_add(COLUMNS(container
), label
, 0, 1);
1679 columns_force_left_align(COLUMNS(container
), label
);
1680 columns_add(COLUMNS(container
), w
, 1, 1);
1682 gtk_widget_show(label
);
1684 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1685 SHORTCUT_UCTRL
, uc
);
1692 * Wrapping text widgets don't sit well with the GTK
1693 * layout model, in which widgets state a minimum size
1694 * and the whole window then adjusts to the smallest
1695 * size it can sensibly take given its contents. A
1696 * wrapping text widget _has_ no clear minimum size;
1697 * instead it has a range of possibilities. It can be
1698 * one line deep but 2000 wide, or two lines deep and
1699 * 1000 pixels, or three by 867, or four by 500 and so
1700 * on. It can be as short as you like provided you
1701 * don't mind it being wide, or as narrow as you like
1702 * provided you don't mind it being tall.
1704 * Therefore, it fits very badly into the layout model.
1705 * Hence the only thing to do is pick a width and let
1706 * it choose its own number of lines. To do this I'm
1707 * going to cheat a little. All new wrapping text
1708 * widgets will be created with a minimal text content
1709 * "X"; then, after the rest of the dialog box is set
1710 * up and its size calculated, the text widgets will be
1711 * told their width and given their real text, which
1712 * will cause the size to be recomputed in the y
1713 * direction (because many of them will expand to more
1716 uc
->text
= w
= gtk_label_new("X");
1717 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1718 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1720 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1721 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1727 columns_add(cols
, w
,
1728 COLUMN_START(ctrl
->generic
.column
),
1729 COLUMN_SPAN(ctrl
->generic
.column
));
1731 columns_force_left_align(cols
, w
);
1735 dlg_add_uctrl(dp
, uc
);
1742 struct dlgparam
*dp
;
1743 GtkNotebook
*panels
;
1744 GtkWidget
*panel
, *treeitem
;
1745 struct Shortcuts shortcuts
;
1748 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1750 struct selparam
*sp
= (struct selparam
*)data
;
1753 page_num
= gtk_notebook_page_num(sp
->panels
, sp
->panel
);
1754 gtk_notebook_set_page(sp
->panels
, page_num
);
1756 dlg_refresh(NULL
, sp
->dp
);
1758 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1759 sp
->dp
->currtreeitem
= sp
->treeitem
;
1762 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1767 static int tree_grab_focus(struct dlgparam
*dp
)
1772 * See if any of the treeitems has the focus.
1775 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1776 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1784 gtk_widget_grab_focus(dp
->currtreeitem
);
1789 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1792 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1794 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1796 * If there's a focused treeitem, we return FALSE to cause the
1797 * focus to move on to some totally other control. If not, we
1798 * focus the selected one.
1800 return tree_grab_focus(dp
);
1803 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1805 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1807 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1808 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1812 if ((event
->state
& GDK_MOD1_MASK
) &&
1813 (unsigned char)event
->string
[0] > 0 &&
1814 (unsigned char)event
->string
[0] <= 127) {
1815 int schr
= (unsigned char)event
->string
[0];
1816 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1818 switch (sc
->action
) {
1820 tree_grab_focus(dp
);
1822 case SHORTCUT_FOCUS
:
1823 gtk_widget_grab_focus(sc
->widget
);
1825 case SHORTCUT_UCTRL
:
1827 * We must do something sensible with a uctrl.
1828 * Precisely what this is depends on the type of
1831 switch (sc
->uc
->ctrl
->generic
.type
) {
1834 /* Check boxes and buttons get the focus _and_ get toggled. */
1835 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1836 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1839 case CTRL_FILESELECT
:
1840 case CTRL_FONTSELECT
:
1841 /* File/font selectors have their buttons pressed (ooer),
1842 * and focus transferred to the edit box. */
1843 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1845 gtk_widget_grab_focus(sc
->uc
->entry
);
1849 * Radio buttons are fun, because they have
1850 * multiple shortcuts. We must find whether the
1851 * activated shortcut is the shortcut for the whole
1852 * group, or for a particular button. In the former
1853 * case, we find the currently selected button and
1854 * focus it; in the latter, we focus-and-click the
1855 * button whose shortcut was pressed.
1857 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1859 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1860 if (gtk_toggle_button_get_active
1861 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1862 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1864 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1866 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1867 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1868 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1869 gtk_signal_emit_by_name
1870 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1876 * If the list is really an option menu, we focus
1877 * and click it. Otherwise we tell it to focus one
1878 * of its children, which appears to do the Right
1881 if (sc
->uc
->optmenu
) {
1885 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1886 /* Option menus don't work using the "clicked" signal.
1887 * We need to manufacture a button press event :-/ */
1888 bev
.type
= GDK_BUTTON_PRESS
;
1890 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1891 "button_press_event",
1894 assert(sc
->uc
->list
!= NULL
);
1896 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1897 GTK_DIR_TAB_FORWARD
);
1908 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1910 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1912 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1913 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1915 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1916 if (widget
== dp
->treeitems
[i
])
1918 if (i
< dp
->ntreeitems
) {
1919 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1926 if (i
< 0 || i
>= dp
->ntreeitems
)
1927 break; /* nothing in that dir to select */
1929 * Determine if this tree item is visible.
1932 GtkWidget
*w
= dp
->treeitems
[i
];
1934 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1935 if (!GTK_WIDGET_VISIBLE(w
)) {
1942 j
= i
; /* got one */
1948 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1951 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1952 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1958 * It's nice for Left and Right to expand and collapse tree
1961 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1962 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1964 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1967 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1968 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1970 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1977 static void shortcut_highlight(GtkWidget
*labelw
, int chr
)
1979 GtkLabel
*label
= GTK_LABEL(labelw
);
1980 gchar
*currstr
, *pattern
;
1983 gtk_label_get(label
, &currstr
);
1984 for (i
= 0; currstr
[i
]; i
++)
1985 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1988 pattern
= dupprintf("%*s_", i
, "");
1990 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1991 gtk_label_set_pattern(label
, pattern
);
1992 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1999 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
2000 int chr
, int action
, void *ptr
)
2002 if (chr
== NO_SHORTCUT
)
2005 chr
= tolower((unsigned char)chr
);
2007 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
2009 scs
->sc
[chr
].action
= action
;
2011 if (action
== SHORTCUT_FOCUS
) {
2012 scs
->sc
[chr
].uc
= NULL
;
2013 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
2015 scs
->sc
[chr
].widget
= NULL
;
2016 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
2019 shortcut_highlight(labelw
, chr
);
2022 int get_listitemheight(void)
2024 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
2026 gtk_widget_size_request(listitem
, &req
);
2027 gtk_widget_unref(listitem
);
2031 int do_config_box(const char *title
, Config
*cfg
, int midsession
,
2034 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
2035 *tree
, *treescroll
, *panels
, *panelvbox
;
2036 int index
, level
, listitemheight
;
2037 struct controlbox
*ctrlbox
;
2039 GtkTreeItem
*treeitemlevels
[8];
2040 GtkTree
*treelevels
[8];
2042 struct Shortcuts scs
;
2044 struct selparam
*selparams
= NULL
;
2045 int nselparams
= 0, selparamsize
= 0;
2049 listitemheight
= get_listitemheight();
2051 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2052 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2055 window
= gtk_dialog_new();
2057 ctrlbox
= ctrl_new_box();
2058 setup_config_box(ctrlbox
, midsession
, cfg
->protocol
, protcfginfo
);
2059 unix_setup_config_box(ctrlbox
, midsession
, cfg
->protocol
);
2060 gtk_setup_config_box(ctrlbox
, midsession
, window
);
2062 gtk_window_set_title(GTK_WINDOW(window
), title
);
2063 hbox
= gtk_hbox_new(FALSE
, 4);
2064 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
2065 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
2066 gtk_widget_show(hbox
);
2067 vbox
= gtk_vbox_new(FALSE
, 4);
2068 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
2069 gtk_widget_show(vbox
);
2070 cols
= columns_new(4);
2071 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
2072 gtk_widget_show(cols
);
2073 label
= gtk_label_new("Category:");
2074 columns_add(COLUMNS(cols
), label
, 0, 1);
2075 columns_force_left_align(COLUMNS(cols
), label
);
2076 gtk_widget_show(label
);
2077 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
2078 tree
= gtk_tree_new();
2079 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
2080 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2081 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
2082 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
2083 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
2084 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
2086 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
2088 GTK_POLICY_AUTOMATIC
);
2089 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
2090 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
2091 gtk_widget_show(tree
);
2092 gtk_widget_show(treescroll
);
2093 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
2094 panels
= gtk_notebook_new();
2095 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels
), FALSE
);
2096 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels
), FALSE
);
2097 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
2098 gtk_widget_show(panels
);
2103 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2104 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2107 if (!*s
->pathname
) {
2108 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
2109 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2112 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
2113 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
2115 GtkWidget
*treeitem
;
2119 * We expect never to find an implicit path
2120 * component. For example, we expect never to see
2121 * A/B/C followed by A/D/E, because that would
2122 * _implicitly_ create A/D. All our path prefixes
2123 * are expected to contain actual controls and be
2124 * selectable in the treeview; so we would expect
2125 * to see A/D _explicitly_ before encountering
2128 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2130 c
= strrchr(s
->pathname
, '/');
2136 treeitem
= gtk_tree_item_new_with_label(c
);
2137 assert(j
-1 < level
);
2139 if (!treelevels
[j
-1]) {
2140 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2141 gtk_tree_item_set_subtree
2142 (treeitemlevels
[j
-1],
2143 GTK_WIDGET(treelevels
[j
-1]));
2144 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2146 gtk_tree_append(treelevels
[j
-1], treeitem
);
2148 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2150 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2151 treelevels
[j
] = NULL
;
2154 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2155 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2156 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2157 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2159 gtk_widget_show(treeitem
);
2163 first
= (panelvbox
== NULL
);
2165 panelvbox
= gtk_vbox_new(FALSE
, 4);
2166 gtk_widget_show(panelvbox
);
2167 gtk_notebook_append_page(GTK_NOTEBOOK(panels
), panelvbox
,
2172 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(panels
),
2174 gtk_notebook_set_page(GTK_NOTEBOOK(panels
), page_num
);
2175 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2178 if (nselparams
>= selparamsize
) {
2180 selparams
= sresize(selparams
, selparamsize
,
2183 selparams
[nselparams
].dp
= &dp
;
2184 selparams
[nselparams
].panels
= GTK_NOTEBOOK(panels
);
2185 selparams
[nselparams
].panel
= panelvbox
;
2186 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2187 selparams
[nselparams
].treeitem
= treeitem
;
2192 w
= layout_ctrls(&dp
,
2193 &selparams
[nselparams
-1].shortcuts
,
2194 s
, listitemheight
, NULL
);
2195 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2200 dp
.ntreeitems
= nselparams
;
2201 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2203 for (index
= 0; index
< nselparams
; index
++) {
2204 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2205 GTK_SIGNAL_FUNC(treeitem_sel
),
2207 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2211 dlg_refresh(NULL
, &dp
);
2213 dp
.shortcuts
= &selparams
[0].shortcuts
;
2214 dp
.currtreeitem
= dp
.treeitems
[0];
2215 dp
.lastfocus
= NULL
;
2221 extern void set_window_icon(GtkWidget
*window
,
2222 const char *const *const *icon
,
2224 extern const char *const *const cfg_icon
[];
2225 extern const int n_cfg_icon
;
2226 set_window_icon(window
, cfg_icon
, n_cfg_icon
);
2229 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2230 gtk_widget_show(window
);
2233 * Set focus into the first available control.
2235 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2236 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2241 for (j
= 0; j
< s
->ncontrols
; j
++)
2242 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2243 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2244 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2245 dlg_set_focus(s
->ctrls
[j
], &dp
);
2246 dp
.lastfocus
= s
->ctrls
[j
];
2255 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2256 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2257 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2258 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2268 static void messagebox_handler(union control
*ctrl
, void *dlg
,
2269 void *data
, int event
)
2271 if (event
== EVENT_ACTION
)
2272 dlg_end(dlg
, ctrl
->generic
.context
.i
);
2274 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
2276 GtkWidget
*window
, *w0
, *w1
;
2277 struct controlbox
*ctrlbox
;
2278 struct controlset
*s0
, *s1
;
2281 struct Shortcuts scs
;
2287 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2288 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2291 ctrlbox
= ctrl_new_box();
2294 va_start(ap
, minwid
);
2295 while (va_arg(ap
, char *) != NULL
) {
2297 (void) va_arg(ap
, int); /* shortcut */
2298 (void) va_arg(ap
, int); /* normal/default/cancel */
2299 (void) va_arg(ap
, int); /* end value */
2303 s0
= ctrl_getset(ctrlbox
, "", "", "");
2304 c
= ctrl_columns(s0
, 2, 50, 50);
2305 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
2306 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
2307 for (index
= 0; index
< ncols
; index
++)
2308 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
2309 va_start(ap
, minwid
);
2312 char *title
= va_arg(ap
, char *);
2313 int shortcut
, type
, value
;
2316 shortcut
= va_arg(ap
, int);
2317 type
= va_arg(ap
, int);
2318 value
= va_arg(ap
, int);
2319 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
2320 messagebox_handler
, I(value
));
2321 c
->generic
.column
= index
++;
2323 c
->button
.isdefault
= TRUE
;
2325 c
->button
.iscancel
= TRUE
;
2329 s1
= ctrl_getset(ctrlbox
, "x", "", "");
2330 ctrl_text(s1
, msg
, HELPCTX(no_help
));
2332 window
= gtk_dialog_new();
2333 gtk_window_set_title(GTK_WINDOW(window
), title
);
2334 w0
= layout_ctrls(&dp
, &scs
, s0
, 0, GTK_WINDOW(window
));
2335 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2337 gtk_widget_show(w0
);
2338 w1
= layout_ctrls(&dp
, &scs
, s1
, 0, GTK_WINDOW(window
));
2339 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2340 gtk_widget_set_usize(w1
, minwid
+20, -1);
2341 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2343 gtk_widget_show(w1
);
2345 dp
.shortcuts
= &scs
;
2346 dp
.lastfocus
= NULL
;
2350 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
2352 set_transient_window_pos(parentwin
, window
);
2353 gtk_window_set_transient_for(GTK_WINDOW(window
),
2354 GTK_WINDOW(parentwin
));
2356 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2357 gtk_widget_show(window
);
2359 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2360 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2361 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2362 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2367 ctrl_free_box(ctrlbox
);
2372 static int string_width(char *text
)
2374 GtkWidget
*label
= gtk_label_new(text
);
2376 gtk_widget_size_request(label
, &req
);
2377 gtk_widget_unref(label
);
2381 int reallyclose(void *frontend
)
2383 char *title
= dupcat(appname
, " Exit Confirmation", NULL
);
2384 int ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2385 title
, "Are you sure you want to close this session?",
2386 string_width("Most of the width of the above text"),
2394 int verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
2395 char *keystr
, char *fingerprint
,
2396 void (*callback
)(void *ctx
, int result
), void *ctx
)
2398 static const char absenttxt
[] =
2399 "The server's host key is not cached. You have no guarantee "
2400 "that the server is the computer you think it is.\n"
2401 "The server's %s key fingerprint is:\n"
2403 "If you trust this host, press \"Accept\" to add the key to "
2404 "PuTTY's cache and carry on connecting.\n"
2405 "If you want to carry on connecting just once, without "
2406 "adding the key to the cache, press \"Connect Once\".\n"
2407 "If you do not trust this host, press \"Cancel\" to abandon the "
2409 static const char wrongtxt
[] =
2410 "WARNING - POTENTIAL SECURITY BREACH!\n"
2411 "The server's host key does not match the one PuTTY has "
2412 "cached. This means that either the server administrator "
2413 "has changed the host key, or you have actually connected "
2414 "to another computer pretending to be the server.\n"
2415 "The new %s key fingerprint is:\n"
2417 "If you were expecting this change and trust the new key, "
2418 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2419 "If you want to carry on connecting but without updating "
2420 "the cache, press \"Connect Once\".\n"
2421 "If you want to abandon the connection completely, press "
2422 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2430 ret
= verify_host_key(host
, port
, keytype
, keystr
);
2432 if (ret
== 0) /* success - key matched OK */
2435 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), keytype
, fingerprint
);
2437 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2438 "PuTTY Security Alert", text
,
2439 string_width(fingerprint
),
2440 "Accept", 'a', 0, 2,
2441 "Connect Once", 'o', 0, 1,
2442 "Cancel", 'c', -1, 0,
2448 store_host_key(host
, port
, keytype
, keystr
);
2449 return 1; /* continue with connection */
2450 } else if (ret
== 1)
2451 return 1; /* continue with connection */
2452 return 0; /* do not continue with connection */
2456 * Ask whether the selected algorithm is acceptable (since it was
2457 * below the configured 'warn' threshold).
2459 int askalg(void *frontend
, const char *algtype
, const char *algname
,
2460 void (*callback
)(void *ctx
, int result
), void *ctx
)
2462 static const char msg
[] =
2463 "The first %s supported by the server is "
2464 "%s, which is below the configured warning threshold.\n"
2465 "Continue with connection?";
2469 text
= dupprintf(msg
, algtype
, algname
);
2470 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2471 "PuTTY Security Alert", text
,
2472 string_width("Continue with connection?"),
2485 void old_keyfile_warning(void)
2488 * This should never happen on Unix. We hope.
2492 void fatal_message_box(void *window
, char *msg
)
2494 messagebox(window
, "PuTTY Fatal Error", msg
,
2495 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2496 "OK", 'o', 1, 1, NULL
);
2499 void fatalbox(char *p
, ...)
2504 msg
= dupvprintf(p
, ap
);
2506 fatal_message_box(NULL
, msg
);
2511 static GtkWidget
*aboutbox
= NULL
;
2513 static void about_close_clicked(GtkButton
*button
, gpointer data
)
2515 gtk_widget_destroy(aboutbox
);
2519 static void licence_clicked(GtkButton
*button
, gpointer data
)
2524 "Copyright 1997-2007 Simon Tatham.\n\n"
2526 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2527 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2528 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
2529 "Markus Kuhn, and CORE SDI S.A.\n\n"
2531 "Permission is hereby granted, free of charge, to any person "
2532 "obtaining a copy of this software and associated documentation "
2533 "files (the ""Software""), to deal in the Software without restriction, "
2534 "including without limitation the rights to use, copy, modify, merge, "
2535 "publish, distribute, sublicense, and/or sell copies of the Software, "
2536 "and to permit persons to whom the Software is furnished to do so, "
2537 "subject to the following conditions:\n\n"
2539 "The above copyright notice and this permission notice shall be "
2540 "included in all copies or substantial portions of the Software.\n\n"
2542 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2543 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2544 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2545 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2546 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
2547 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2548 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2549 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2550 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2551 "OTHER DEALINGS IN THE SOFTWARE.";
2553 title
= dupcat(appname
, " Licence", NULL
);
2554 assert(aboutbox
!= NULL
);
2555 messagebox(aboutbox
, title
, licence
,
2556 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2557 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2558 "OK", 'o', 1, 1, NULL
);
2562 void about_box(void *window
)
2568 gtk_widget_grab_focus(aboutbox
);
2572 aboutbox
= gtk_dialog_new();
2573 gtk_container_set_border_width(GTK_CONTAINER(aboutbox
), 10);
2574 title
= dupcat("About ", appname
, NULL
);
2575 gtk_window_set_title(GTK_WINDOW(aboutbox
), title
);
2578 w
= gtk_button_new_with_label("Close");
2579 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2580 gtk_window_set_default(GTK_WINDOW(aboutbox
), w
);
2581 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2582 w
, FALSE
, FALSE
, 0);
2583 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2584 GTK_SIGNAL_FUNC(about_close_clicked
), NULL
);
2587 w
= gtk_button_new_with_label("View Licence");
2588 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2589 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2590 w
, FALSE
, FALSE
, 0);
2591 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2592 GTK_SIGNAL_FUNC(licence_clicked
), NULL
);
2595 w
= gtk_label_new(appname
);
2596 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2597 w
, FALSE
, FALSE
, 0);
2600 w
= gtk_label_new(ver
);
2601 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2602 w
, FALSE
, FALSE
, 5);
2605 w
= gtk_label_new("Copyright 1997-2007 Simon Tatham. All rights reserved");
2606 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2607 w
, FALSE
, FALSE
, 5);
2610 set_transient_window_pos(GTK_WIDGET(window
), aboutbox
);
2611 gtk_widget_show(aboutbox
);
2614 struct eventlog_stuff
{
2615 GtkWidget
*parentwin
, *window
;
2616 struct controlbox
*eventbox
;
2617 struct Shortcuts scs
;
2619 union control
*listctrl
;
2621 int nevents
, negsize
;
2624 int ignore_selchange
;
2627 static void eventlog_destroy(GtkWidget
*widget
, gpointer data
)
2629 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2633 dlg_cleanup(&es
->dp
);
2634 ctrl_free_box(es
->eventbox
);
2636 static void eventlog_ok_handler(union control
*ctrl
, void *dlg
,
2637 void *data
, int event
)
2639 if (event
== EVENT_ACTION
)
2642 static void eventlog_list_handler(union control
*ctrl
, void *dlg
,
2643 void *data
, int event
)
2645 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2647 if (event
== EVENT_REFRESH
) {
2650 dlg_update_start(ctrl
, dlg
);
2651 dlg_listbox_clear(ctrl
, dlg
);
2652 for (i
= 0; i
< es
->nevents
; i
++) {
2653 dlg_listbox_add(ctrl
, dlg
, es
->events
[i
]);
2655 dlg_update_done(ctrl
, dlg
);
2656 } else if (event
== EVENT_SELCHANGE
) {
2661 * If this SELCHANGE event is happening as a result of
2662 * deliberate deselection because someone else has grabbed
2663 * the selection, the last thing we want to do is pre-empt
2666 if (es
->ignore_selchange
)
2670 * Construct the data to use as the selection.
2675 for (i
= 0; i
< es
->nevents
; i
++) {
2676 if (dlg_listbox_issel(ctrl
, dlg
, i
)) {
2677 int extralen
= strlen(es
->events
[i
]);
2679 if (es
->sellen
+ extralen
+ 2 > selsize
) {
2680 selsize
= es
->sellen
+ extralen
+ 512;
2681 es
->seldata
= sresize(es
->seldata
, selsize
, char);
2684 strcpy(es
->seldata
+ es
->sellen
, es
->events
[i
]);
2685 es
->sellen
+= extralen
;
2686 es
->seldata
[es
->sellen
++] = '\n';
2690 if (gtk_selection_owner_set(es
->window
, GDK_SELECTION_PRIMARY
,
2691 GDK_CURRENT_TIME
)) {
2692 extern GdkAtom compound_text_atom
;
2694 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2695 GDK_SELECTION_TYPE_STRING
, 1);
2696 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2697 compound_text_atom
, 1);
2703 void eventlog_selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
2704 guint info
, guint time_stamp
, gpointer data
)
2706 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2708 gtk_selection_data_set(seldata
, seldata
->target
, 8,
2709 (unsigned char *)es
->seldata
, es
->sellen
);
2712 gint
eventlog_selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
2715 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2719 * Deselect everything in the list box.
2721 uc
= dlg_find_byctrl(&es
->dp
, es
->listctrl
);
2722 es
->ignore_selchange
= 1;
2723 gtk_list_unselect_all(GTK_LIST(uc
->list
));
2724 es
->ignore_selchange
= 0;
2732 void showeventlog(void *estuff
, void *parentwin
)
2734 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2735 GtkWidget
*window
, *w0
, *w1
;
2736 GtkWidget
*parent
= GTK_WIDGET(parentwin
);
2737 struct controlset
*s0
, *s1
;
2739 int listitemheight
, index
;
2743 gtk_widget_grab_focus(es
->window
);
2749 for (index
= 0; index
< lenof(es
->scs
.sc
); index
++) {
2750 es
->scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2753 es
->eventbox
= ctrl_new_box();
2755 s0
= ctrl_getset(es
->eventbox
, "", "", "");
2756 ctrl_columns(s0
, 3, 33, 34, 33);
2757 c
= ctrl_pushbutton(s0
, "Close", 'c', HELPCTX(no_help
),
2758 eventlog_ok_handler
, P(NULL
));
2759 c
->button
.column
= 1;
2760 c
->button
.isdefault
= TRUE
;
2762 s1
= ctrl_getset(es
->eventbox
, "x", "", "");
2763 es
->listctrl
= c
= ctrl_listbox(s1
, NULL
, NO_SHORTCUT
, HELPCTX(no_help
),
2764 eventlog_list_handler
, P(es
));
2765 c
->listbox
.height
= 10;
2766 c
->listbox
.multisel
= 2;
2767 c
->listbox
.ncols
= 3;
2768 c
->listbox
.percentages
= snewn(3, int);
2769 c
->listbox
.percentages
[0] = 25;
2770 c
->listbox
.percentages
[1] = 10;
2771 c
->listbox
.percentages
[2] = 65;
2773 listitemheight
= get_listitemheight();
2775 es
->window
= window
= gtk_dialog_new();
2776 title
= dupcat(appname
, " Event Log", NULL
);
2777 gtk_window_set_title(GTK_WINDOW(window
), title
);
2779 w0
= layout_ctrls(&es
->dp
, &es
->scs
, s0
,
2780 listitemheight
, GTK_WINDOW(window
));
2781 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2783 gtk_widget_show(w0
);
2784 w1
= layout_ctrls(&es
->dp
, &es
->scs
, s1
,
2785 listitemheight
, GTK_WINDOW(window
));
2786 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2787 gtk_widget_set_usize(w1
, 20 +
2788 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
2789 " IS QUITE LONG 'COS SSH LOG ENTRIES"
2791 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2793 gtk_widget_show(w1
);
2796 es
->dp
.shortcuts
= &es
->scs
;
2797 es
->dp
.lastfocus
= NULL
;
2799 es
->dp
.window
= window
;
2801 dlg_refresh(NULL
, &es
->dp
);
2804 set_transient_window_pos(parent
, window
);
2805 gtk_window_set_transient_for(GTK_WINDOW(window
),
2806 GTK_WINDOW(parent
));
2808 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2809 gtk_widget_show(window
);
2811 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2812 GTK_SIGNAL_FUNC(eventlog_destroy
), es
);
2813 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2814 GTK_SIGNAL_FUNC(win_key_press
), &es
->dp
);
2815 gtk_signal_connect(GTK_OBJECT(window
), "selection_get",
2816 GTK_SIGNAL_FUNC(eventlog_selection_get
), es
);
2817 gtk_signal_connect(GTK_OBJECT(window
), "selection_clear_event",
2818 GTK_SIGNAL_FUNC(eventlog_selection_clear
), es
);
2821 void *eventlogstuff_new(void)
2823 struct eventlog_stuff
*es
;
2824 es
= snew(struct eventlog_stuff
);
2825 memset(es
, 0, sizeof(*es
));
2829 void logevent_dlg(void *estuff
, const char *string
)
2831 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2836 if (es
->nevents
>= es
->negsize
) {
2838 es
->events
= sresize(es
->events
, es
->negsize
, char *);
2842 strftime(timebuf
, sizeof(timebuf
), "%Y-%m-%d %H:%M:%S\t", &tm
);
2844 es
->events
[es
->nevents
] = snewn(strlen(timebuf
) + strlen(string
) + 1, char);
2845 strcpy(es
->events
[es
->nevents
], timebuf
);
2846 strcat(es
->events
[es
->nevents
], string
);
2848 dlg_listbox_add(es
->listctrl
, &es
->dp
, es
->events
[es
->nevents
]);
2853 int askappend(void *frontend
, Filename filename
,
2854 void (*callback
)(void *ctx
, int result
), void *ctx
)
2856 static const char msgtemplate
[] =
2857 "The session log file \"%.*s\" already exists. "
2858 "You can overwrite it with a new session log, "
2859 "append your session log to the end of it, "
2860 "or disable session logging for this session.";
2865 message
= dupprintf(msgtemplate
, FILENAME_MAX
, filename
.path
);
2866 mbtitle
= dupprintf("%s Log to File", appname
);
2868 mbret
= messagebox(get_window(frontend
), mbtitle
, message
,
2869 string_width("LINE OF TEXT SUITABLE FOR THE"
2870 " ASKAPPEND WIDTH"),
2871 "Overwrite", 'o', 1, 2,
2872 "Append", 'a', 0, 1,
2873 "Disable", 'd', -1, 0,