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 */
64 tree234
*byctrl
, *bywidget
;
66 struct { unsigned char r
, g
, b
, ok
; } coloursel_result
; /* 0-255 */
67 /* `flags' are set to indicate when a GTK signal handler is being called
68 * due to automatic processing and should not flag a user event. */
70 struct Shortcuts
*shortcuts
;
71 GtkWidget
*window
, *cancelbutton
, *currtreeitem
, **treeitems
;
72 union control
*currfocus
, *lastfocus
;
76 #define FLAG_UPDATING_COMBO_LIST 1
78 enum { /* values for Shortcut.action */
79 SHORTCUT_EMPTY
, /* no shortcut on this key */
80 SHORTCUT_TREE
, /* focus a tree item */
81 SHORTCUT_FOCUS
, /* focus the supplied widget */
82 SHORTCUT_UCTRL
, /* do something sane with uctrl */
83 SHORTCUT_UCTRL_UP
, /* uctrl is a draglist, move Up */
84 SHORTCUT_UCTRL_DOWN
, /* uctrl is a draglist, move Down */
90 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
92 static void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
93 int chr
, int action
, void *ptr
);
94 static void shortcut_highlight(GtkWidget
*label
, int chr
);
95 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
97 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
99 static int listitem_button_press(GtkWidget
*item
, GdkEventButton
*event
,
101 static int listitem_button_release(GtkWidget
*item
, GdkEventButton
*event
,
103 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
104 static void coloursel_ok(GtkButton
*button
, gpointer data
);
105 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
106 static void window_destroy(GtkWidget
*widget
, gpointer data
);
108 static int uctrl_cmp_byctrl(void *av
, void *bv
)
110 struct uctrl
*a
= (struct uctrl
*)av
;
111 struct uctrl
*b
= (struct uctrl
*)bv
;
112 if (a
->ctrl
< b
->ctrl
)
114 else if (a
->ctrl
> b
->ctrl
)
119 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
121 union control
*a
= (union control
*)av
;
122 struct uctrl
*b
= (struct uctrl
*)bv
;
125 else if (a
> b
->ctrl
)
130 static int uctrl_cmp_bywidget(void *av
, void *bv
)
132 struct uctrl
*a
= (struct uctrl
*)av
;
133 struct uctrl
*b
= (struct uctrl
*)bv
;
134 if (a
->toplevel
< b
->toplevel
)
136 else if (a
->toplevel
> b
->toplevel
)
141 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
143 GtkWidget
*a
= (GtkWidget
*)av
;
144 struct uctrl
*b
= (struct uctrl
*)bv
;
147 else if (a
> b
->toplevel
)
152 static void dlg_init(struct dlgparam
*dp
)
154 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
155 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
156 dp
->coloursel_result
.ok
= FALSE
;
157 dp
->treeitems
= NULL
;
158 dp
->window
= dp
->cancelbutton
= dp
->currtreeitem
= NULL
;
160 dp
->currfocus
= NULL
;
163 static void dlg_cleanup(struct dlgparam
*dp
)
167 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
169 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
170 del234(dp
->bywidget
, uc
);
171 if (uc
->privdata_needs_free
)
176 freetree234(dp
->bywidget
);
178 sfree(dp
->treeitems
);
181 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
183 add234(dp
->byctrl
, uc
);
184 add234(dp
->bywidget
, uc
);
187 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
191 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
194 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
196 struct uctrl
*ret
= NULL
;
200 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
208 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
210 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
211 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
215 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
217 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
218 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
220 uc
->privdata_needs_free
= FALSE
;
223 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
225 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
226 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
228 * This is an internal allocation routine, so it's allowed to
229 * use smalloc directly.
231 uc
->privdata
= smalloc(size
);
232 uc
->privdata_needs_free
= FALSE
;
236 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
238 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
239 if (dp
->currfocus
!= ctrl
)
240 return dp
->currfocus
;
242 return dp
->lastfocus
;
245 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
247 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
248 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
249 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
250 assert(uc
->buttons
!= NULL
);
251 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
254 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
256 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
257 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
260 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
261 assert(uc
->buttons
!= NULL
);
262 for (i
= 0; i
< uc
->nbuttons
; i
++)
263 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
265 return 0; /* got to return something */
268 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
270 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
271 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
272 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
273 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
276 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
278 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
279 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
280 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
281 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
284 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
286 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
287 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
288 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
289 assert(uc
->entry
!= NULL
);
290 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
293 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
295 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
296 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
297 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
298 assert(uc
->entry
!= NULL
);
299 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
301 buffer
[length
-1] = '\0';
304 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
306 GtkContainer
*cont
= GTK_CONTAINER(data
);
307 /* gtk_container_remove will unref the widget for us; we need not. */
308 gtk_container_remove(cont
, w
);
311 /* The `listbox' functions can also apply to combo boxes. */
312 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
314 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
315 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
317 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
318 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
319 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
322 gtk_container_foreach(GTK_CONTAINER(uc
->menu
),
323 container_remove_and_destroy
,
324 GTK_CONTAINER(uc
->menu
));
326 gtk_list_clear_items(GTK_LIST(uc
->list
), 0, -1);
330 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
332 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
333 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
335 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
336 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
337 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
341 (GTK_CONTAINER(uc
->menu
),
342 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
344 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
348 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
350 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
354 * Each listbox entry may have a numeric id associated with it.
355 * Note that some front ends only permit a string to be stored at
356 * each position, which means that _if_ you put two identical
357 * strings in any listbox then you MUST not assign them different
358 * IDs and expect to get meaningful results back.
360 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
361 char const *text
, int id
)
363 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
364 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
366 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
367 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
368 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
370 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
374 * List item in a drop-down (but non-combo) list. Tabs are
375 * ignored; we just provide a standard menu item with the
378 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
380 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
381 gtk_widget_show(menuitem
);
383 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
384 GINT_TO_POINTER(id
));
385 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
386 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
387 } else if (!uc
->entry
) {
389 * List item in a non-combo-box list box. We make all of
390 * these Columns containing GtkLabels. This allows us to do
391 * the nasty force_left hack irrespective of whether there
392 * are tabs in the thing.
394 GtkWidget
*listitem
= gtk_list_item_new();
395 GtkWidget
*cols
= columns_new(10);
399 /* Count the tabs in the text, and hence determine # of columns. */
401 for (i
= 0; text
[i
]; i
++)
406 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
407 percents
= snewn(ncols
, gint
);
408 percents
[ncols
-1] = 100;
409 for (i
= 0; i
< ncols
-1; i
++) {
410 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
411 percents
[ncols
-1] -= percents
[i
];
413 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
416 for (i
= 0; i
< ncols
; i
++) {
417 int len
= strcspn(text
, "\t");
418 char *dup
= dupprintf("%.*s", len
, text
);
423 label
= gtk_label_new(dup
);
426 columns_add(COLUMNS(cols
), label
, i
, 1);
427 columns_force_left_align(COLUMNS(cols
), label
);
428 gtk_widget_show(label
);
430 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
431 gtk_widget_show(cols
);
432 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
433 gtk_widget_show(listitem
);
435 if (ctrl
->listbox
.multisel
) {
436 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
437 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
439 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
440 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
442 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
443 GTK_SIGNAL_FUNC(widget_focus
), dp
);
444 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
445 GTK_SIGNAL_FUNC(listitem_button_press
), dp
);
446 gtk_signal_connect(GTK_OBJECT(listitem
), "button_release_event",
447 GTK_SIGNAL_FUNC(listitem_button_release
), dp
);
448 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
449 GINT_TO_POINTER(id
));
452 * List item in a combo-box list, which means the sensible
453 * thing to do is make it a perfectly normal label. Hence
454 * tabs are disregarded.
456 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
458 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
459 gtk_widget_show(listitem
);
461 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
462 GINT_TO_POINTER(id
));
465 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
468 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
470 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
471 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
475 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
476 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
477 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
479 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
481 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
482 g_list_free(children
);
484 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
), "user-data"));
487 /* dlg_listbox_index returns <0 if no single element is selected. */
488 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
490 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
491 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
493 GtkWidget
*item
, *activeitem
;
497 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
498 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
499 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
502 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
504 activeitem
= NULL
; /* unnecessarily placate gcc */
506 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
508 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
509 i
++, children
= children
->next
) {
510 if (uc
->menu ? activeitem
== item
:
511 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
518 g_list_free(children
);
519 return selected
< 0 ?
-1 : selected
;
522 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
524 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
525 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
527 GtkWidget
*item
, *activeitem
;
529 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
530 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
531 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
533 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
535 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
536 g_list_free(children
);
539 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
540 return item
== activeitem
;
542 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
546 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
548 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
549 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
551 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
552 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
553 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
556 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
560 gdouble newtop
, newbot
;
562 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
565 * Scroll the list box if necessary to ensure the newly
566 * selected item is visible.
568 items
= gtk_container_children(GTK_CONTAINER(uc
->list
));
569 nitems
= g_list_length(items
);
571 int modified
= FALSE
;
573 newtop
= uc
->adj
->lower
+
574 (uc
->adj
->upper
- uc
->adj
->lower
) * index
/ nitems
;
575 newbot
= uc
->adj
->lower
+
576 (uc
->adj
->upper
- uc
->adj
->lower
) * (index
+1) / nitems
;
577 if (uc
->adj
->value
> newtop
) {
579 uc
->adj
->value
= newtop
;
580 } else if (uc
->adj
->value
< newbot
- uc
->adj
->page_size
) {
582 uc
->adj
->value
= newbot
- uc
->adj
->page_size
;
585 gtk_adjustment_value_changed(uc
->adj
);
590 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
592 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
593 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
595 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
596 assert(uc
->text
!= NULL
);
598 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
601 void dlg_label_change(union control
*ctrl
, void *dlg
, char const *text
)
603 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
604 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
606 switch (uc
->ctrl
->generic
.type
) {
608 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
609 shortcut_highlight(uc
->toplevel
, ctrl
->button
.shortcut
);
612 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
613 shortcut_highlight(uc
->toplevel
, ctrl
->checkbox
.shortcut
);
616 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
617 shortcut_highlight(uc
->label
, ctrl
->radio
.shortcut
);
620 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
621 shortcut_highlight(uc
->label
, ctrl
->editbox
.shortcut
);
623 case CTRL_FILESELECT
:
624 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
625 shortcut_highlight(uc
->label
, ctrl
->fileselect
.shortcut
);
627 case CTRL_FONTSELECT
:
628 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
629 shortcut_highlight(uc
->label
, ctrl
->fontselect
.shortcut
);
632 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
633 shortcut_highlight(uc
->label
, ctrl
->listbox
.shortcut
);
636 assert(!"This shouldn't happen");
641 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
643 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
644 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
645 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
646 assert(uc
->entry
!= NULL
);
647 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
650 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
652 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
653 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
654 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
655 assert(uc
->entry
!= NULL
);
656 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
658 fn
->path
[lenof(fn
->path
)-1] = '\0';
661 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
663 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
664 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
665 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
666 assert(uc
->entry
!= NULL
);
667 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
670 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
672 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
673 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
674 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
675 assert(uc
->entry
!= NULL
);
676 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
678 fs
->name
[lenof(fs
->name
)-1] = '\0';
682 * Bracketing a large set of updates in these two functions will
683 * cause the front end (if possible) to delay updating the screen
684 * until it's all complete, thus avoiding flicker.
686 void dlg_update_start(union control
*ctrl
, void *dlg
)
689 * Apparently we can't do this at all in GTK. GtkCList supports
690 * freeze and thaw, but not GtkList. Bah.
694 void dlg_update_done(union control
*ctrl
, void *dlg
)
697 * Apparently we can't do this at all in GTK. GtkCList supports
698 * freeze and thaw, but not GtkList. Bah.
702 void dlg_set_focus(union control
*ctrl
, void *dlg
)
704 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
705 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
707 switch (ctrl
->generic
.type
) {
710 /* Check boxes and buttons get the focus _and_ get toggled. */
711 gtk_widget_grab_focus(uc
->toplevel
);
713 case CTRL_FILESELECT
:
714 case CTRL_FONTSELECT
:
716 /* Anything containing an edit box gets that focused. */
717 gtk_widget_grab_focus(uc
->entry
);
721 * Radio buttons: we find the currently selected button and
726 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
727 if (gtk_toggle_button_get_active
728 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
729 gtk_widget_grab_focus(uc
->buttons
[i
]);
735 * If the list is really an option menu, we focus it.
736 * Otherwise we tell it to focus one of its children, which
737 * appears to do the Right Thing.
740 gtk_widget_grab_focus(uc
->optmenu
);
742 assert(uc
->list
!= NULL
);
743 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
750 * During event processing, you might well want to give an error
751 * indication to the user. dlg_beep() is a quick and easy generic
752 * error; dlg_error() puts up a message-box or equivalent.
754 void dlg_beep(void *dlg
)
759 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
761 gtk_widget_destroy(GTK_WIDGET(data
));
764 static void set_transient_window_pos(GtkWidget
*parent
, GtkWidget
*child
)
766 gint x
, y
, w
, h
, dx
, dy
;
768 gtk_window_set_position(GTK_WINDOW(child
), GTK_WIN_POS_NONE
);
769 gtk_widget_size_request(GTK_WIDGET(child
), &req
);
771 gdk_window_get_origin(GTK_WIDGET(parent
)->window
, &x
, &y
);
772 gdk_window_get_size(GTK_WIDGET(parent
)->window
, &w
, &h
);
775 * One corner of the transient will be offset inwards, by 1/4
776 * of the parent window's size, from the corresponding corner
777 * of the parent window. The corner will be chosen so as to
778 * place the transient closer to the centre of the screen; this
779 * should avoid transients going off the edge of the screen on
782 if (x
+ w
/2 < gdk_screen_width() / 2)
783 dx
= x
+ w
/4; /* work from left edges */
785 dx
= x
+ 3*w
/4 - req
.width
; /* work from right edges */
786 if (y
+ h
/2 < gdk_screen_height() / 2)
787 dy
= y
+ h
/4; /* work from top edges */
789 dy
= y
+ 3*h
/4 - req
.height
; /* work from bottom edges */
790 gtk_widget_set_uposition(GTK_WIDGET(child
), dx
, dy
);
793 void dlg_error_msg(void *dlg
, char *msg
)
795 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
796 GtkWidget
*window
, *hbox
, *text
, *ok
;
798 window
= gtk_dialog_new();
799 text
= gtk_label_new(msg
);
800 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
801 hbox
= gtk_hbox_new(FALSE
, 0);
802 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
803 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
804 hbox
, FALSE
, FALSE
, 20);
805 gtk_widget_show(text
);
806 gtk_widget_show(hbox
);
807 gtk_window_set_title(GTK_WINDOW(window
), "Error");
808 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
809 ok
= gtk_button_new_with_label("OK");
810 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
811 ok
, FALSE
, FALSE
, 0);
813 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
814 gtk_window_set_default(GTK_WINDOW(window
), ok
);
815 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
816 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
817 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
818 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
819 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
820 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
821 set_transient_window_pos(dp
->window
, window
);
822 gtk_widget_show(window
);
827 * This function signals to the front end that the dialog's
828 * processing is completed, and passes an integer value (typically
831 void dlg_end(void *dlg
, int value
)
833 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
835 gtk_widget_destroy(dp
->window
);
838 void dlg_refresh(union control
*ctrl
, void *dlg
)
840 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
844 if (ctrl
->generic
.handler
!= NULL
)
845 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
849 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
850 assert(uc
->ctrl
!= NULL
);
851 if (uc
->ctrl
->generic
.handler
!= NULL
)
852 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
853 dp
->data
, EVENT_REFRESH
);
858 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
860 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
861 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
864 GtkWidget
*coloursel
=
865 gtk_color_selection_dialog_new("Select a colour");
866 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
868 dp
->coloursel_result
.ok
= FALSE
;
870 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
871 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
872 cvals
[0] = r
/ 255.0;
873 cvals
[1] = g
/ 255.0;
874 cvals
[2] = b
/ 255.0;
875 cvals
[3] = 1.0; /* fully opaque! */
876 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
878 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
879 (gpointer
)coloursel
);
880 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
881 (gpointer
)coloursel
);
882 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
883 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
884 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
885 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
886 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
887 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
888 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
889 (gpointer
)coloursel
);
890 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
891 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
892 (gpointer
)coloursel
);
893 gtk_widget_show(coloursel
);
896 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
897 int *r
, int *g
, int *b
)
899 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
900 if (dp
->coloursel_result
.ok
) {
901 *r
= dp
->coloursel_result
.r
;
902 *g
= dp
->coloursel_result
.g
;
903 *b
= dp
->coloursel_result
.b
;
909 /* ----------------------------------------------------------------------
910 * Signal handlers while the dialog box is active.
913 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
916 struct dlgparam
*dp
= (struct dlgparam
*)data
;
917 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
918 union control
*focus
;
925 if (focus
!= dp
->currfocus
) {
926 dp
->lastfocus
= dp
->currfocus
;
927 dp
->currfocus
= focus
;
933 static void button_clicked(GtkButton
*button
, gpointer data
)
935 struct dlgparam
*dp
= (struct dlgparam
*)data
;
936 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
937 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
940 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
942 struct dlgparam
*dp
= (struct dlgparam
*)data
;
943 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
944 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
947 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
950 * GtkEntry has a nasty habit of eating the Return key, which
951 * is unhelpful since it doesn't actually _do_ anything with it
952 * (it calls gtk_widget_activate, but our edit boxes never need
953 * activating). So I catch Return before GtkEntry sees it, and
954 * pass it straight on to the parent widget. Effect: hitting
955 * Return in an edit box will now activate the default button
956 * in the dialog just like it will everywhere else.
958 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
960 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
961 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
968 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
970 struct dlgparam
*dp
= (struct dlgparam
*)data
;
971 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
972 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
973 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
977 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
980 struct dlgparam
*dp
= (struct dlgparam
*)data
;
981 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
982 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
985 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
988 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
990 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
991 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
992 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
993 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
995 * Up, Down, PgUp or PgDn have been pressed on a ListItem
996 * in a list box. So, if the list box is single-selection:
998 * - if the list item in question isn't already selected,
999 * we simply select it.
1000 * - otherwise, we find the next one (or next
1001 * however-far-away) in whichever direction we're going,
1003 * + in this case, we must also fiddle with the
1004 * scrollbar to ensure the newly selected item is
1007 * If it's multiple-selection, we do all of the above
1008 * except actually selecting anything, so we move the focus
1009 * and fiddle the scrollbar to follow it.
1011 GtkWidget
*list
= item
->parent
;
1013 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
1016 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
1017 gtk_list_select_child(GTK_LIST(list
), item
);
1020 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
1021 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1024 (event
->keyval
==GDK_Page_Down
||
1025 event
->keyval
==GDK_KP_Page_Down
||
1026 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1029 GList
*children
, *chead
;
1031 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
1033 n
= g_list_length(children
);
1037 * Figure out how many list items to a screenful,
1038 * and adjust the step appropriately.
1040 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
1041 step
--; /* go by one less than that */
1045 while (children
!= NULL
) {
1046 if (item
== children
->data
)
1048 children
= children
->next
;
1053 if (direction
< 0 && i
> 0)
1054 children
= children
->prev
, i
--;
1055 else if (direction
> 0 && i
< n
-1)
1056 children
= children
->next
, i
++;
1060 if (children
&& children
->data
) {
1062 gtk_list_select_child(GTK_LIST(list
),
1063 GTK_WIDGET(children
->data
));
1064 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
1065 gtk_adjustment_clamp_page
1067 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
1068 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
1079 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
1082 return listitem_key(item
, event
, data
, FALSE
);
1085 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
1088 return listitem_key(item
, event
, data
, TRUE
);
1091 static int listitem_button_press(GtkWidget
*item
, GdkEventButton
*event
,
1094 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1095 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1096 switch (event
->type
) {
1098 case GDK_BUTTON_PRESS
: uc
->nclicks
= 1; break;
1099 case GDK_2BUTTON_PRESS
: uc
->nclicks
= 2; break;
1100 case GDK_3BUTTON_PRESS
: uc
->nclicks
= 3; break;
1105 static int listitem_button_release(GtkWidget
*item
, GdkEventButton
*event
,
1108 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1109 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1110 if (uc
->nclicks
>1) {
1111 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1117 static void list_selchange(GtkList
*list
, gpointer data
)
1119 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1120 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1122 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1125 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1127 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1128 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1129 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1130 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1131 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1134 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1136 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1137 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1141 (index
== 0 && direction
< 0) ||
1142 (index
== g_list_length(children
)-1 && direction
> 0)) {
1147 child
= g_list_nth_data(children
, index
);
1148 gtk_widget_ref(child
);
1149 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1150 g_list_free(children
);
1153 children
= g_list_append(children
, child
);
1154 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1155 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1156 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1159 static void draglist_up(GtkButton
*button
, gpointer data
)
1161 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1162 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1163 draglist_move(dp
, uc
, -1);
1166 static void draglist_down(GtkButton
*button
, gpointer data
)
1168 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1169 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1170 draglist_move(dp
, uc
, +1);
1173 static void filesel_ok(GtkButton
*button
, gpointer data
)
1175 /* struct dlgparam *dp = (struct dlgparam *)data; */
1176 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1177 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1178 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1179 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1182 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1184 /* struct dlgparam *dp = (struct dlgparam *)data; */
1185 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1186 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1187 char *name
= gtk_font_selection_dialog_get_font_name
1188 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1189 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1192 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1194 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1195 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1196 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1198 gtk_color_selection_get_color
1199 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1201 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1202 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1203 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1204 dp
->coloursel_result
.ok
= TRUE
;
1205 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1208 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1210 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1211 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1212 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1213 dp
->coloursel_result
.ok
= FALSE
;
1214 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1217 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1219 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1220 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1222 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1223 GtkWidget
*filesel
=
1224 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1225 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1227 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1229 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1231 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1232 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1233 gtk_signal_connect_object
1234 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1235 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1236 gtk_signal_connect_object
1237 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1238 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1239 gtk_widget_show(filesel
);
1242 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1243 gchar
*spacings
[] = { "c", "m", NULL
};
1244 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1245 GtkWidget
*fontsel
=
1246 gtk_font_selection_dialog_new("Select a font");
1247 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1248 gtk_font_selection_dialog_set_filter
1249 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1250 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1251 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1252 if (!gtk_font_selection_dialog_set_font_name
1253 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1255 * If the font name wasn't found as it was, try opening
1256 * it and extracting its FONT property. This should
1257 * have the effect of mapping short aliases into true
1260 GdkFont
*font
= gdk_font_load(fontname
);
1262 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1263 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1264 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1266 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1267 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1269 gtk_font_selection_dialog_set_font_name
1270 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1272 gdk_font_unref(font
);
1276 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1277 "user-data", (gpointer
)fontsel
);
1278 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1280 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1281 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1282 gtk_signal_connect_object
1283 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1284 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1286 gtk_signal_connect_object
1287 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1288 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1290 gtk_widget_show(fontsel
);
1294 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1297 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1298 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1300 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1301 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1302 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1305 /* ----------------------------------------------------------------------
1306 * This function does the main layout work: it reads a controlset,
1307 * it creates the relevant GTK controls, and returns a GtkWidget
1308 * containing the result. (This widget might be a title of some
1309 * sort, it might be a Columns containing many controls, or it
1310 * might be a GtkFrame containing a Columns; whatever it is, it's
1311 * definitely a GtkWidget and should probably be added to a
1314 * `listitemheight' is used to calculate a usize for list boxes: it
1315 * should be the height from the size request of a GtkListItem.
1317 * `win' is required for setting the default button. If it is
1318 * non-NULL, all buttons created will be default-capable (so they
1319 * have extra space round them for the default highlight).
1321 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1322 struct controlset
*s
, int listitemheight
,
1329 if (!s
->boxname
&& s
->boxtitle
) {
1330 /* This controlset is a panel title. */
1331 return gtk_label_new(s
->boxtitle
);
1335 * Otherwise, we expect to be laying out actual controls, so
1336 * we'll start by creating a Columns for the purpose.
1338 cols
= COLUMNS(columns_new(4));
1339 ret
= GTK_WIDGET(cols
);
1340 gtk_widget_show(ret
);
1343 * Create a containing frame if we have a box name.
1346 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1347 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1348 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1349 gtk_widget_show(ret
);
1353 * Now iterate through the controls themselves, create them,
1354 * and add them to the Columns.
1356 for (i
= 0; i
< s
->ncontrols
; i
++) {
1357 union control
*ctrl
= s
->ctrls
[i
];
1360 GtkWidget
*w
= NULL
;
1362 switch (ctrl
->generic
.type
) {
1365 static const int simplecols
[1] = { 100 };
1366 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1367 (ctrl
->columns
.percentages ?
1368 ctrl
->columns
.percentages
: simplecols
));
1370 continue; /* no actual control created */
1373 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1375 columns_taborder_last(cols
, uc
->toplevel
);
1377 continue; /* no actual control created */
1380 uc
= snew(struct uctrl
);
1382 uc
->privdata
= NULL
;
1383 uc
->privdata_needs_free
= FALSE
;
1385 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1386 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1390 switch (ctrl
->generic
.type
) {
1392 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1394 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1395 if (ctrl
->button
.isdefault
)
1396 gtk_window_set_default(win
, w
);
1397 if (ctrl
->button
.iscancel
)
1398 dp
->cancelbutton
= w
;
1400 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1401 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1402 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1403 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1404 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1405 SHORTCUT_UCTRL
, uc
);
1408 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1409 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1410 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1411 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1412 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1413 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1414 SHORTCUT_UCTRL
, uc
);
1419 * Radio buttons get to go inside their own Columns, no
1423 gint i
, *percentages
;
1427 if (ctrl
->generic
.label
) {
1428 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1429 columns_add(COLUMNS(w
), label
, 0, 1);
1430 columns_force_left_align(COLUMNS(w
), label
);
1431 gtk_widget_show(label
);
1432 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1433 SHORTCUT_UCTRL
, uc
);
1436 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1437 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1439 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1440 100 * i
/ ctrl
->radio
.ncolumns
);
1442 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1444 g_free(percentages
);
1447 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1448 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1450 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1454 b
= (gtk_radio_button_new_with_label
1455 (group
, ctrl
->radio
.buttons
[i
]));
1457 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1458 colstart
= i
% ctrl
->radio
.ncolumns
;
1459 columns_add(COLUMNS(w
), b
, colstart
,
1460 (i
== ctrl
->radio
.nbuttons
-1 ?
1461 ctrl
->radio
.ncolumns
- colstart
: 1));
1462 columns_force_left_align(COLUMNS(w
), b
);
1464 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1465 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1466 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1467 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1468 if (ctrl
->radio
.shortcuts
) {
1469 shortcut_add(scs
, GTK_BIN(b
)->child
,
1470 ctrl
->radio
.shortcuts
[i
],
1471 SHORTCUT_UCTRL
, uc
);
1480 if (ctrl
->editbox
.has_list
) {
1481 w
= gtk_combo_new();
1482 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1483 uc
->entry
= GTK_COMBO(w
)->entry
;
1484 uc
->list
= GTK_COMBO(w
)->list
;
1486 w
= gtk_entry_new();
1487 if (ctrl
->editbox
.password
)
1488 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1491 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1492 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1493 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1494 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1495 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1496 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1498 * Edit boxes, for some strange reason, have a minimum
1499 * width of 150 in GTK 1.2. We don't want this - we'd
1500 * rather the edit boxes acquired their natural width
1501 * from the column layout of the rest of the box.
1503 * Also, while we're here, we'll squirrel away the
1504 * edit box height so we can use that to centre its
1505 * label vertically beside it.
1507 gtk_widget_size_request(w
, &req
);
1508 gtk_widget_set_usize(w
, 10, req
.height
);
1510 if (ctrl
->generic
.label
) {
1511 GtkWidget
*label
, *container
;
1513 label
= gtk_label_new(ctrl
->generic
.label
);
1515 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1516 SHORTCUT_FOCUS
, uc
->entry
);
1518 container
= columns_new(4);
1519 if (ctrl
->editbox
.percentwidth
== 100) {
1520 columns_add(COLUMNS(container
), label
, 0, 1);
1521 columns_force_left_align(COLUMNS(container
), label
);
1522 columns_add(COLUMNS(container
), w
, 0, 1);
1524 gint percentages
[2];
1525 percentages
[1] = ctrl
->editbox
.percentwidth
;
1526 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1527 columns_set_cols(COLUMNS(container
), 2, percentages
);
1528 columns_add(COLUMNS(container
), label
, 0, 1);
1529 columns_force_left_align(COLUMNS(container
), label
);
1530 columns_add(COLUMNS(container
), w
, 1, 1);
1531 /* Centre the label vertically. */
1532 gtk_widget_set_usize(label
, -1, req
.height
);
1533 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1535 gtk_widget_show(label
);
1541 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1542 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1545 case CTRL_FILESELECT
:
1546 case CTRL_FONTSELECT
:
1551 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1552 "Browse..." : "Change...");
1554 gint percentages
[] = { 75, 25 };
1556 columns_set_cols(COLUMNS(w
), 2, percentages
);
1558 if (ctrl
->generic
.label
) {
1559 ww
= gtk_label_new(ctrl
->generic
.label
);
1560 columns_add(COLUMNS(w
), ww
, 0, 2);
1561 columns_force_left_align(COLUMNS(w
), ww
);
1562 gtk_widget_show(ww
);
1563 shortcut_add(scs
, ww
,
1564 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1565 ctrl
->fileselect
.shortcut
:
1566 ctrl
->fontselect
.shortcut
),
1567 SHORTCUT_UCTRL
, uc
);
1571 uc
->entry
= ww
= gtk_entry_new();
1572 gtk_widget_size_request(ww
, &req
);
1573 gtk_widget_set_usize(ww
, 10, req
.height
);
1574 columns_add(COLUMNS(w
), ww
, 0, 1);
1575 gtk_widget_show(ww
);
1577 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1578 columns_add(COLUMNS(w
), ww
, 1, 1);
1579 gtk_widget_show(ww
);
1581 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1582 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1583 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1584 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1585 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1586 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1587 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1588 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1589 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1590 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1594 if (ctrl
->listbox
.height
== 0) {
1595 uc
->optmenu
= w
= gtk_option_menu_new();
1596 uc
->menu
= gtk_menu_new();
1597 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1598 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1599 (gpointer
)uc
->optmenu
);
1600 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1601 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1603 uc
->list
= gtk_list_new();
1604 if (ctrl
->listbox
.multisel
== 2) {
1605 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1606 GTK_SELECTION_EXTENDED
);
1607 } else if (ctrl
->listbox
.multisel
== 1) {
1608 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1609 GTK_SELECTION_MULTIPLE
);
1611 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1612 GTK_SELECTION_SINGLE
);
1614 w
= gtk_scrolled_window_new(NULL
, NULL
);
1615 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1617 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1619 GTK_POLICY_AUTOMATIC
);
1620 uc
->adj
= gtk_scrolled_window_get_vadjustment
1621 (GTK_SCROLLED_WINDOW(w
));
1623 gtk_widget_show(uc
->list
);
1624 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1625 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1626 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1627 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1630 * Adjust the height of the scrolled window to the
1631 * minimum given by the height parameter.
1633 * This piece of guesswork is a horrid hack based
1634 * on looking inside the GTK 1.2 sources
1635 * (specifically gtkviewport.c, which appears to be
1636 * the widget which provides the border around the
1637 * scrolling area). Anyone lets me know how I can
1638 * do this in a way which isn't at risk from GTK
1639 * upgrades, I'd be grateful.
1642 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1643 gtk_widget_set_usize(w
, 10,
1644 2*edge
+ (ctrl
->listbox
.height
*
1648 if (ctrl
->listbox
.draglist
) {
1650 * GTK doesn't appear to make it easy to
1651 * implement a proper draggable list; so
1652 * instead I'm just going to have to put an Up
1653 * and a Down button to the right of the actual
1654 * list box. Ah well.
1656 GtkWidget
*cols
, *button
;
1657 static const gint percentages
[2] = { 80, 20 };
1659 cols
= columns_new(4);
1660 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1661 columns_add(COLUMNS(cols
), w
, 0, 1);
1663 button
= gtk_button_new_with_label("Up");
1664 columns_add(COLUMNS(cols
), button
, 1, 1);
1665 gtk_widget_show(button
);
1666 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1667 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1668 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1669 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1670 button
= gtk_button_new_with_label("Down");
1671 columns_add(COLUMNS(cols
), button
, 1, 1);
1672 gtk_widget_show(button
);
1673 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1674 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1675 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1676 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1682 if (ctrl
->generic
.label
) {
1683 GtkWidget
*label
, *container
;
1685 label
= gtk_label_new(ctrl
->generic
.label
);
1687 container
= columns_new(4);
1688 if (ctrl
->listbox
.percentwidth
== 100) {
1689 columns_add(COLUMNS(container
), label
, 0, 1);
1690 columns_force_left_align(COLUMNS(container
), label
);
1691 columns_add(COLUMNS(container
), w
, 0, 1);
1693 gint percentages
[2];
1694 percentages
[1] = ctrl
->listbox
.percentwidth
;
1695 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1696 columns_set_cols(COLUMNS(container
), 2, percentages
);
1697 columns_add(COLUMNS(container
), label
, 0, 1);
1698 columns_force_left_align(COLUMNS(container
), label
);
1699 columns_add(COLUMNS(container
), w
, 1, 1);
1701 gtk_widget_show(label
);
1703 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1704 SHORTCUT_UCTRL
, uc
);
1711 * Wrapping text widgets don't sit well with the GTK
1712 * layout model, in which widgets state a minimum size
1713 * and the whole window then adjusts to the smallest
1714 * size it can sensibly take given its contents. A
1715 * wrapping text widget _has_ no clear minimum size;
1716 * instead it has a range of possibilities. It can be
1717 * one line deep but 2000 wide, or two lines deep and
1718 * 1000 pixels, or three by 867, or four by 500 and so
1719 * on. It can be as short as you like provided you
1720 * don't mind it being wide, or as narrow as you like
1721 * provided you don't mind it being tall.
1723 * Therefore, it fits very badly into the layout model.
1724 * Hence the only thing to do is pick a width and let
1725 * it choose its own number of lines. To do this I'm
1726 * going to cheat a little. All new wrapping text
1727 * widgets will be created with a minimal text content
1728 * "X"; then, after the rest of the dialog box is set
1729 * up and its size calculated, the text widgets will be
1730 * told their width and given their real text, which
1731 * will cause the size to be recomputed in the y
1732 * direction (because many of them will expand to more
1735 uc
->text
= w
= gtk_label_new("X");
1736 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1737 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1739 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1740 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1746 columns_add(cols
, w
,
1747 COLUMN_START(ctrl
->generic
.column
),
1748 COLUMN_SPAN(ctrl
->generic
.column
));
1750 columns_force_left_align(cols
, w
);
1754 dlg_add_uctrl(dp
, uc
);
1761 struct dlgparam
*dp
;
1762 GtkNotebook
*panels
;
1763 GtkWidget
*panel
, *treeitem
;
1764 struct Shortcuts shortcuts
;
1767 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1769 struct selparam
*sp
= (struct selparam
*)data
;
1772 page_num
= gtk_notebook_page_num(sp
->panels
, sp
->panel
);
1773 gtk_notebook_set_page(sp
->panels
, page_num
);
1775 dlg_refresh(NULL
, sp
->dp
);
1777 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1778 sp
->dp
->currtreeitem
= sp
->treeitem
;
1781 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1786 static int tree_grab_focus(struct dlgparam
*dp
)
1791 * See if any of the treeitems has the focus.
1794 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1795 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1803 gtk_widget_grab_focus(dp
->currtreeitem
);
1808 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1811 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1813 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1815 * If there's a focused treeitem, we return FALSE to cause the
1816 * focus to move on to some totally other control. If not, we
1817 * focus the selected one.
1819 return tree_grab_focus(dp
);
1822 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1824 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1826 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1827 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1831 if ((event
->state
& GDK_MOD1_MASK
) &&
1832 (unsigned char)event
->string
[0] > 0 &&
1833 (unsigned char)event
->string
[0] <= 127) {
1834 int schr
= (unsigned char)event
->string
[0];
1835 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1837 switch (sc
->action
) {
1839 tree_grab_focus(dp
);
1841 case SHORTCUT_FOCUS
:
1842 gtk_widget_grab_focus(sc
->widget
);
1844 case SHORTCUT_UCTRL
:
1846 * We must do something sensible with a uctrl.
1847 * Precisely what this is depends on the type of
1850 switch (sc
->uc
->ctrl
->generic
.type
) {
1853 /* Check boxes and buttons get the focus _and_ get toggled. */
1854 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1855 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1858 case CTRL_FILESELECT
:
1859 case CTRL_FONTSELECT
:
1860 /* File/font selectors have their buttons pressed (ooer),
1861 * and focus transferred to the edit box. */
1862 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1864 gtk_widget_grab_focus(sc
->uc
->entry
);
1868 * Radio buttons are fun, because they have
1869 * multiple shortcuts. We must find whether the
1870 * activated shortcut is the shortcut for the whole
1871 * group, or for a particular button. In the former
1872 * case, we find the currently selected button and
1873 * focus it; in the latter, we focus-and-click the
1874 * button whose shortcut was pressed.
1876 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1878 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1879 if (gtk_toggle_button_get_active
1880 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1881 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1883 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1885 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1886 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1887 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1888 gtk_signal_emit_by_name
1889 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1895 * If the list is really an option menu, we focus
1896 * and click it. Otherwise we tell it to focus one
1897 * of its children, which appears to do the Right
1900 if (sc
->uc
->optmenu
) {
1904 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1905 /* Option menus don't work using the "clicked" signal.
1906 * We need to manufacture a button press event :-/ */
1907 bev
.type
= GDK_BUTTON_PRESS
;
1909 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1910 "button_press_event",
1913 assert(sc
->uc
->list
!= NULL
);
1915 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1916 GTK_DIR_TAB_FORWARD
);
1927 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1929 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1931 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1932 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1934 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1935 if (widget
== dp
->treeitems
[i
])
1937 if (i
< dp
->ntreeitems
) {
1938 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1945 if (i
< 0 || i
>= dp
->ntreeitems
)
1946 break; /* nothing in that dir to select */
1948 * Determine if this tree item is visible.
1951 GtkWidget
*w
= dp
->treeitems
[i
];
1953 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1954 if (!GTK_WIDGET_VISIBLE(w
)) {
1961 j
= i
; /* got one */
1967 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1970 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1971 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1977 * It's nice for Left and Right to expand and collapse tree
1980 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1981 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1983 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1986 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1987 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1989 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1996 static void shortcut_highlight(GtkWidget
*labelw
, int chr
)
1998 GtkLabel
*label
= GTK_LABEL(labelw
);
1999 gchar
*currstr
, *pattern
;
2002 gtk_label_get(label
, &currstr
);
2003 for (i
= 0; currstr
[i
]; i
++)
2004 if (tolower((unsigned char)currstr
[i
]) == chr
) {
2007 pattern
= dupprintf("%*s_", i
, "");
2009 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
2010 gtk_label_set_pattern(label
, pattern
);
2011 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
2018 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
2019 int chr
, int action
, void *ptr
)
2021 if (chr
== NO_SHORTCUT
)
2024 chr
= tolower((unsigned char)chr
);
2026 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
2028 scs
->sc
[chr
].action
= action
;
2030 if (action
== SHORTCUT_FOCUS
) {
2031 scs
->sc
[chr
].uc
= NULL
;
2032 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
2034 scs
->sc
[chr
].widget
= NULL
;
2035 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
2038 shortcut_highlight(labelw
, chr
);
2041 int get_listitemheight(void)
2043 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
2045 gtk_widget_size_request(listitem
, &req
);
2046 gtk_widget_unref(listitem
);
2050 int do_config_box(const char *title
, Config
*cfg
, int midsession
,
2053 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
2054 *tree
, *treescroll
, *panels
, *panelvbox
;
2055 int index
, level
, listitemheight
;
2056 struct controlbox
*ctrlbox
;
2058 GtkTreeItem
*treeitemlevels
[8];
2059 GtkTree
*treelevels
[8];
2061 struct Shortcuts scs
;
2063 struct selparam
*selparams
= NULL
;
2064 int nselparams
= 0, selparamsize
= 0;
2068 listitemheight
= get_listitemheight();
2070 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2071 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2074 window
= gtk_dialog_new();
2076 ctrlbox
= ctrl_new_box();
2077 setup_config_box(ctrlbox
, midsession
, cfg
->protocol
, protcfginfo
);
2078 unix_setup_config_box(ctrlbox
, midsession
, cfg
->protocol
);
2079 gtk_setup_config_box(ctrlbox
, midsession
, window
);
2081 gtk_window_set_title(GTK_WINDOW(window
), title
);
2082 hbox
= gtk_hbox_new(FALSE
, 4);
2083 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
2084 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
2085 gtk_widget_show(hbox
);
2086 vbox
= gtk_vbox_new(FALSE
, 4);
2087 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
2088 gtk_widget_show(vbox
);
2089 cols
= columns_new(4);
2090 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
2091 gtk_widget_show(cols
);
2092 label
= gtk_label_new("Category:");
2093 columns_add(COLUMNS(cols
), label
, 0, 1);
2094 columns_force_left_align(COLUMNS(cols
), label
);
2095 gtk_widget_show(label
);
2096 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
2097 tree
= gtk_tree_new();
2098 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
2099 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2100 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
2101 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
2102 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
2103 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
2104 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
2105 gtk_widget_show(treescroll
);
2106 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
2107 panels
= gtk_notebook_new();
2108 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels
), FALSE
);
2109 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels
), FALSE
);
2110 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
2111 gtk_widget_show(panels
);
2116 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2117 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2120 if (!*s
->pathname
) {
2121 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
2122 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2125 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
2126 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
2128 GtkWidget
*treeitem
;
2132 * We expect never to find an implicit path
2133 * component. For example, we expect never to see
2134 * A/B/C followed by A/D/E, because that would
2135 * _implicitly_ create A/D. All our path prefixes
2136 * are expected to contain actual controls and be
2137 * selectable in the treeview; so we would expect
2138 * to see A/D _explicitly_ before encountering
2141 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2143 c
= strrchr(s
->pathname
, '/');
2149 treeitem
= gtk_tree_item_new_with_label(c
);
2150 assert(j
-1 < level
);
2152 if (!treelevels
[j
-1]) {
2153 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2154 gtk_tree_item_set_subtree
2155 (treeitemlevels
[j
-1],
2156 GTK_WIDGET(treelevels
[j
-1]));
2158 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2160 gtk_tree_item_collapse(treeitemlevels
[j
-1]);
2162 gtk_tree_append(treelevels
[j
-1], treeitem
);
2164 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2166 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2167 treelevels
[j
] = NULL
;
2170 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2171 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2172 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2173 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2175 gtk_widget_show(treeitem
);
2179 first
= (panelvbox
== NULL
);
2181 panelvbox
= gtk_vbox_new(FALSE
, 4);
2182 gtk_widget_show(panelvbox
);
2183 gtk_notebook_append_page(GTK_NOTEBOOK(panels
), panelvbox
,
2188 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(panels
),
2190 gtk_notebook_set_page(GTK_NOTEBOOK(panels
), page_num
);
2191 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2194 if (nselparams
>= selparamsize
) {
2196 selparams
= sresize(selparams
, selparamsize
,
2199 selparams
[nselparams
].dp
= &dp
;
2200 selparams
[nselparams
].panels
= GTK_NOTEBOOK(panels
);
2201 selparams
[nselparams
].panel
= panelvbox
;
2202 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2203 selparams
[nselparams
].treeitem
= treeitem
;
2208 w
= layout_ctrls(&dp
,
2209 &selparams
[nselparams
-1].shortcuts
,
2210 s
, listitemheight
, NULL
);
2211 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2216 dp
.ntreeitems
= nselparams
;
2217 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2219 for (index
= 0; index
< nselparams
; index
++) {
2220 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2221 GTK_SIGNAL_FUNC(treeitem_sel
),
2223 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2227 dlg_refresh(NULL
, &dp
);
2229 dp
.shortcuts
= &selparams
[0].shortcuts
;
2230 dp
.currtreeitem
= dp
.treeitems
[0];
2231 dp
.lastfocus
= NULL
;
2237 extern void set_window_icon(GtkWidget
*window
,
2238 const char *const *const *icon
,
2240 extern const char *const *const cfg_icon
[];
2241 extern const int n_cfg_icon
;
2242 set_window_icon(window
, cfg_icon
, n_cfg_icon
);
2245 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
2247 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
2249 GTK_POLICY_AUTOMATIC
);
2250 gtk_widget_show(tree
);
2252 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2253 gtk_widget_show(window
);
2256 * Set focus into the first available control.
2258 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2259 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2264 for (j
= 0; j
< s
->ncontrols
; j
++)
2265 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2266 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2267 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2268 dlg_set_focus(s
->ctrls
[j
], &dp
);
2269 dp
.lastfocus
= s
->ctrls
[j
];
2278 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2279 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2280 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2281 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2291 static void messagebox_handler(union control
*ctrl
, void *dlg
,
2292 void *data
, int event
)
2294 if (event
== EVENT_ACTION
)
2295 dlg_end(dlg
, ctrl
->generic
.context
.i
);
2297 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
2299 GtkWidget
*window
, *w0
, *w1
;
2300 struct controlbox
*ctrlbox
;
2301 struct controlset
*s0
, *s1
;
2304 struct Shortcuts scs
;
2310 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2311 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2314 ctrlbox
= ctrl_new_box();
2317 va_start(ap
, minwid
);
2318 while (va_arg(ap
, char *) != NULL
) {
2320 (void) va_arg(ap
, int); /* shortcut */
2321 (void) va_arg(ap
, int); /* normal/default/cancel */
2322 (void) va_arg(ap
, int); /* end value */
2326 s0
= ctrl_getset(ctrlbox
, "", "", "");
2327 c
= ctrl_columns(s0
, 2, 50, 50);
2328 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
2329 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
2330 for (index
= 0; index
< ncols
; index
++)
2331 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
2332 va_start(ap
, minwid
);
2335 char *title
= va_arg(ap
, char *);
2336 int shortcut
, type
, value
;
2339 shortcut
= va_arg(ap
, int);
2340 type
= va_arg(ap
, int);
2341 value
= va_arg(ap
, int);
2342 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
2343 messagebox_handler
, I(value
));
2344 c
->generic
.column
= index
++;
2346 c
->button
.isdefault
= TRUE
;
2348 c
->button
.iscancel
= TRUE
;
2352 s1
= ctrl_getset(ctrlbox
, "x", "", "");
2353 ctrl_text(s1
, msg
, HELPCTX(no_help
));
2355 window
= gtk_dialog_new();
2356 gtk_window_set_title(GTK_WINDOW(window
), title
);
2357 w0
= layout_ctrls(&dp
, &scs
, s0
, 0, GTK_WINDOW(window
));
2358 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2360 gtk_widget_show(w0
);
2361 w1
= layout_ctrls(&dp
, &scs
, s1
, 0, GTK_WINDOW(window
));
2362 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2363 gtk_widget_set_usize(w1
, minwid
+20, -1);
2364 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2366 gtk_widget_show(w1
);
2368 dp
.shortcuts
= &scs
;
2369 dp
.lastfocus
= NULL
;
2373 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
2375 set_transient_window_pos(parentwin
, window
);
2376 gtk_window_set_transient_for(GTK_WINDOW(window
),
2377 GTK_WINDOW(parentwin
));
2379 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2380 gtk_widget_show(window
);
2382 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2383 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2384 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2385 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2390 ctrl_free_box(ctrlbox
);
2395 static int string_width(char *text
)
2397 GtkWidget
*label
= gtk_label_new(text
);
2399 gtk_widget_size_request(label
, &req
);
2400 gtk_widget_unref(label
);
2404 int reallyclose(void *frontend
)
2406 char *title
= dupcat(appname
, " Exit Confirmation", NULL
);
2407 int ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2408 title
, "Are you sure you want to close this session?",
2409 string_width("Most of the width of the above text"),
2417 int verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
2418 char *keystr
, char *fingerprint
,
2419 void (*callback
)(void *ctx
, int result
), void *ctx
)
2421 static const char absenttxt
[] =
2422 "The server's host key is not cached. You have no guarantee "
2423 "that the server is the computer you think it is.\n"
2424 "The server's %s key fingerprint is:\n"
2426 "If you trust this host, press \"Accept\" to add the key to "
2427 "PuTTY's cache and carry on connecting.\n"
2428 "If you want to carry on connecting just once, without "
2429 "adding the key to the cache, press \"Connect Once\".\n"
2430 "If you do not trust this host, press \"Cancel\" to abandon the "
2432 static const char wrongtxt
[] =
2433 "WARNING - POTENTIAL SECURITY BREACH!\n"
2434 "The server's host key does not match the one PuTTY has "
2435 "cached. This means that either the server administrator "
2436 "has changed the host key, or you have actually connected "
2437 "to another computer pretending to be the server.\n"
2438 "The new %s key fingerprint is:\n"
2440 "If you were expecting this change and trust the new key, "
2441 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2442 "If you want to carry on connecting but without updating "
2443 "the cache, press \"Connect Once\".\n"
2444 "If you want to abandon the connection completely, press "
2445 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2453 ret
= verify_host_key(host
, port
, keytype
, keystr
);
2455 if (ret
== 0) /* success - key matched OK */
2458 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), keytype
, fingerprint
);
2460 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2461 "PuTTY Security Alert", text
,
2462 string_width(fingerprint
),
2463 "Accept", 'a', 0, 2,
2464 "Connect Once", 'o', 0, 1,
2465 "Cancel", 'c', -1, 0,
2471 store_host_key(host
, port
, keytype
, keystr
);
2472 return 1; /* continue with connection */
2473 } else if (ret
== 1)
2474 return 1; /* continue with connection */
2475 return 0; /* do not continue with connection */
2479 * Ask whether the selected algorithm is acceptable (since it was
2480 * below the configured 'warn' threshold).
2482 int askalg(void *frontend
, const char *algtype
, const char *algname
,
2483 void (*callback
)(void *ctx
, int result
), void *ctx
)
2485 static const char msg
[] =
2486 "The first %s supported by the server is "
2487 "%s, which is below the configured warning threshold.\n"
2488 "Continue with connection?";
2492 text
= dupprintf(msg
, algtype
, algname
);
2493 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2494 "PuTTY Security Alert", text
,
2495 string_width("Continue with connection?"),
2508 void old_keyfile_warning(void)
2511 * This should never happen on Unix. We hope.
2515 void fatal_message_box(void *window
, char *msg
)
2517 messagebox(window
, "PuTTY Fatal Error", msg
,
2518 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2519 "OK", 'o', 1, 1, NULL
);
2522 void fatalbox(char *p
, ...)
2527 msg
= dupvprintf(p
, ap
);
2529 fatal_message_box(NULL
, msg
);
2534 static GtkWidget
*aboutbox
= NULL
;
2536 static void about_close_clicked(GtkButton
*button
, gpointer data
)
2538 gtk_widget_destroy(aboutbox
);
2542 static void licence_clicked(GtkButton
*button
, gpointer data
)
2547 "Copyright 1997-2007 Simon Tatham.\n\n"
2549 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2550 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2551 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
2552 "Markus Kuhn, and CORE SDI S.A.\n\n"
2554 "Permission is hereby granted, free of charge, to any person "
2555 "obtaining a copy of this software and associated documentation "
2556 "files (the ""Software""), to deal in the Software without restriction, "
2557 "including without limitation the rights to use, copy, modify, merge, "
2558 "publish, distribute, sublicense, and/or sell copies of the Software, "
2559 "and to permit persons to whom the Software is furnished to do so, "
2560 "subject to the following conditions:\n\n"
2562 "The above copyright notice and this permission notice shall be "
2563 "included in all copies or substantial portions of the Software.\n\n"
2565 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2566 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2567 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2568 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2569 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
2570 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2571 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2572 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2573 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2574 "OTHER DEALINGS IN THE SOFTWARE.";
2576 title
= dupcat(appname
, " Licence", NULL
);
2577 assert(aboutbox
!= NULL
);
2578 messagebox(aboutbox
, title
, licence
,
2579 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2580 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2581 "OK", 'o', 1, 1, NULL
);
2585 void about_box(void *window
)
2591 gtk_widget_grab_focus(aboutbox
);
2595 aboutbox
= gtk_dialog_new();
2596 gtk_container_set_border_width(GTK_CONTAINER(aboutbox
), 10);
2597 title
= dupcat("About ", appname
, NULL
);
2598 gtk_window_set_title(GTK_WINDOW(aboutbox
), title
);
2601 w
= gtk_button_new_with_label("Close");
2602 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2603 gtk_window_set_default(GTK_WINDOW(aboutbox
), w
);
2604 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2605 w
, FALSE
, FALSE
, 0);
2606 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2607 GTK_SIGNAL_FUNC(about_close_clicked
), NULL
);
2610 w
= gtk_button_new_with_label("View Licence");
2611 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2612 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2613 w
, FALSE
, FALSE
, 0);
2614 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2615 GTK_SIGNAL_FUNC(licence_clicked
), NULL
);
2618 w
= gtk_label_new(appname
);
2619 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2620 w
, FALSE
, FALSE
, 0);
2623 w
= gtk_label_new(ver
);
2624 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2625 w
, FALSE
, FALSE
, 5);
2628 w
= gtk_label_new("Copyright 1997-2007 Simon Tatham. All rights reserved");
2629 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2630 w
, FALSE
, FALSE
, 5);
2633 set_transient_window_pos(GTK_WIDGET(window
), aboutbox
);
2634 gtk_widget_show(aboutbox
);
2637 struct eventlog_stuff
{
2638 GtkWidget
*parentwin
, *window
;
2639 struct controlbox
*eventbox
;
2640 struct Shortcuts scs
;
2642 union control
*listctrl
;
2644 int nevents
, negsize
;
2647 int ignore_selchange
;
2650 static void eventlog_destroy(GtkWidget
*widget
, gpointer data
)
2652 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2656 dlg_cleanup(&es
->dp
);
2657 ctrl_free_box(es
->eventbox
);
2659 static void eventlog_ok_handler(union control
*ctrl
, void *dlg
,
2660 void *data
, int event
)
2662 if (event
== EVENT_ACTION
)
2665 static void eventlog_list_handler(union control
*ctrl
, void *dlg
,
2666 void *data
, int event
)
2668 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2670 if (event
== EVENT_REFRESH
) {
2673 dlg_update_start(ctrl
, dlg
);
2674 dlg_listbox_clear(ctrl
, dlg
);
2675 for (i
= 0; i
< es
->nevents
; i
++) {
2676 dlg_listbox_add(ctrl
, dlg
, es
->events
[i
]);
2678 dlg_update_done(ctrl
, dlg
);
2679 } else if (event
== EVENT_SELCHANGE
) {
2684 * If this SELCHANGE event is happening as a result of
2685 * deliberate deselection because someone else has grabbed
2686 * the selection, the last thing we want to do is pre-empt
2689 if (es
->ignore_selchange
)
2693 * Construct the data to use as the selection.
2698 for (i
= 0; i
< es
->nevents
; i
++) {
2699 if (dlg_listbox_issel(ctrl
, dlg
, i
)) {
2700 int extralen
= strlen(es
->events
[i
]);
2702 if (es
->sellen
+ extralen
+ 2 > selsize
) {
2703 selsize
= es
->sellen
+ extralen
+ 512;
2704 es
->seldata
= sresize(es
->seldata
, selsize
, char);
2707 strcpy(es
->seldata
+ es
->sellen
, es
->events
[i
]);
2708 es
->sellen
+= extralen
;
2709 es
->seldata
[es
->sellen
++] = '\n';
2713 if (gtk_selection_owner_set(es
->window
, GDK_SELECTION_PRIMARY
,
2714 GDK_CURRENT_TIME
)) {
2715 extern GdkAtom compound_text_atom
;
2717 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2718 GDK_SELECTION_TYPE_STRING
, 1);
2719 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2720 compound_text_atom
, 1);
2726 void eventlog_selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
2727 guint info
, guint time_stamp
, gpointer data
)
2729 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2731 gtk_selection_data_set(seldata
, seldata
->target
, 8,
2732 (unsigned char *)es
->seldata
, es
->sellen
);
2735 gint
eventlog_selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
2738 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2742 * Deselect everything in the list box.
2744 uc
= dlg_find_byctrl(&es
->dp
, es
->listctrl
);
2745 es
->ignore_selchange
= 1;
2746 gtk_list_unselect_all(GTK_LIST(uc
->list
));
2747 es
->ignore_selchange
= 0;
2755 void showeventlog(void *estuff
, void *parentwin
)
2757 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2758 GtkWidget
*window
, *w0
, *w1
;
2759 GtkWidget
*parent
= GTK_WIDGET(parentwin
);
2760 struct controlset
*s0
, *s1
;
2762 int listitemheight
, index
;
2766 gtk_widget_grab_focus(es
->window
);
2772 for (index
= 0; index
< lenof(es
->scs
.sc
); index
++) {
2773 es
->scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2776 es
->eventbox
= ctrl_new_box();
2778 s0
= ctrl_getset(es
->eventbox
, "", "", "");
2779 ctrl_columns(s0
, 3, 33, 34, 33);
2780 c
= ctrl_pushbutton(s0
, "Close", 'c', HELPCTX(no_help
),
2781 eventlog_ok_handler
, P(NULL
));
2782 c
->button
.column
= 1;
2783 c
->button
.isdefault
= TRUE
;
2785 s1
= ctrl_getset(es
->eventbox
, "x", "", "");
2786 es
->listctrl
= c
= ctrl_listbox(s1
, NULL
, NO_SHORTCUT
, HELPCTX(no_help
),
2787 eventlog_list_handler
, P(es
));
2788 c
->listbox
.height
= 10;
2789 c
->listbox
.multisel
= 2;
2790 c
->listbox
.ncols
= 3;
2791 c
->listbox
.percentages
= snewn(3, int);
2792 c
->listbox
.percentages
[0] = 25;
2793 c
->listbox
.percentages
[1] = 10;
2794 c
->listbox
.percentages
[2] = 65;
2796 listitemheight
= get_listitemheight();
2798 es
->window
= window
= gtk_dialog_new();
2799 title
= dupcat(appname
, " Event Log", NULL
);
2800 gtk_window_set_title(GTK_WINDOW(window
), title
);
2802 w0
= layout_ctrls(&es
->dp
, &es
->scs
, s0
,
2803 listitemheight
, GTK_WINDOW(window
));
2804 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2806 gtk_widget_show(w0
);
2807 w1
= layout_ctrls(&es
->dp
, &es
->scs
, s1
,
2808 listitemheight
, GTK_WINDOW(window
));
2809 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2810 gtk_widget_set_usize(w1
, 20 +
2811 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
2812 " IS QUITE LONG 'COS SSH LOG ENTRIES"
2814 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2816 gtk_widget_show(w1
);
2819 es
->dp
.shortcuts
= &es
->scs
;
2820 es
->dp
.lastfocus
= NULL
;
2822 es
->dp
.window
= window
;
2824 dlg_refresh(NULL
, &es
->dp
);
2827 set_transient_window_pos(parent
, window
);
2828 gtk_window_set_transient_for(GTK_WINDOW(window
),
2829 GTK_WINDOW(parent
));
2831 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2832 gtk_widget_show(window
);
2834 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2835 GTK_SIGNAL_FUNC(eventlog_destroy
), es
);
2836 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2837 GTK_SIGNAL_FUNC(win_key_press
), &es
->dp
);
2838 gtk_signal_connect(GTK_OBJECT(window
), "selection_get",
2839 GTK_SIGNAL_FUNC(eventlog_selection_get
), es
);
2840 gtk_signal_connect(GTK_OBJECT(window
), "selection_clear_event",
2841 GTK_SIGNAL_FUNC(eventlog_selection_clear
), es
);
2844 void *eventlogstuff_new(void)
2846 struct eventlog_stuff
*es
;
2847 es
= snew(struct eventlog_stuff
);
2848 memset(es
, 0, sizeof(*es
));
2852 void logevent_dlg(void *estuff
, const char *string
)
2854 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2859 if (es
->nevents
>= es
->negsize
) {
2861 es
->events
= sresize(es
->events
, es
->negsize
, char *);
2865 strftime(timebuf
, sizeof(timebuf
), "%Y-%m-%d %H:%M:%S\t", &tm
);
2867 es
->events
[es
->nevents
] = snewn(strlen(timebuf
) + strlen(string
) + 1, char);
2868 strcpy(es
->events
[es
->nevents
], timebuf
);
2869 strcat(es
->events
[es
->nevents
], string
);
2871 dlg_listbox_add(es
->listctrl
, &es
->dp
, es
->events
[es
->nevents
]);
2876 int askappend(void *frontend
, Filename filename
,
2877 void (*callback
)(void *ctx
, int result
), void *ctx
)
2879 static const char msgtemplate
[] =
2880 "The session log file \"%.*s\" already exists. "
2881 "You can overwrite it with a new session log, "
2882 "append your session log to the end of it, "
2883 "or disable session logging for this session.";
2888 message
= dupprintf(msgtemplate
, FILENAME_MAX
, filename
.path
);
2889 mbtitle
= dupprintf("%s Log to File", appname
);
2891 mbret
= messagebox(get_window(frontend
), mbtitle
, message
,
2892 string_width("LINE OF TEXT SUITABLE FOR THE"
2893 " ASKAPPEND WIDTH"),
2894 "Overwrite", 'o', 1, 2,
2895 "Append", 'a', 0, 1,
2896 "Disable", 'd', -1, 0,