2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
6 * TODO when porting to GTK 2.0:
8 * - GtkTree is apparently deprecated and we should switch to
10 * - GtkLabel has a built-in mnemonic scheme, so we should at
11 * least consider switching to that from the current adhockery.
19 #include <gdk/gdkkeysyms.h>
22 #include <X11/Xutil.h>
28 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
43 struct Shortcut sc
[128];
50 int privdata_needs_free
;
51 GtkWidget
**buttons
; int nbuttons
; /* for radio buttons */
52 GtkWidget
*entry
; /* for editbox, combobox, filesel, fontsel */
53 GtkWidget
*button
; /* for filesel, fontsel */
54 GtkWidget
*list
; /* for combobox, listbox */
55 GtkWidget
*menu
; /* for optionmenu (==droplist) */
56 GtkWidget
*optmenu
; /* also for optionmenu */
57 GtkWidget
*text
; /* for text */
58 GtkWidget
*label
; /* for dlg_label_change */
59 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(GtkWidget
*item
, GdkEventButton
*event
,
101 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
102 static void coloursel_ok(GtkButton
*button
, gpointer data
);
103 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
104 static void window_destroy(GtkWidget
*widget
, gpointer data
);
106 static int uctrl_cmp_byctrl(void *av
, void *bv
)
108 struct uctrl
*a
= (struct uctrl
*)av
;
109 struct uctrl
*b
= (struct uctrl
*)bv
;
110 if (a
->ctrl
< b
->ctrl
)
112 else if (a
->ctrl
> b
->ctrl
)
117 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
119 union control
*a
= (union control
*)av
;
120 struct uctrl
*b
= (struct uctrl
*)bv
;
123 else if (a
> b
->ctrl
)
128 static int uctrl_cmp_bywidget(void *av
, void *bv
)
130 struct uctrl
*a
= (struct uctrl
*)av
;
131 struct uctrl
*b
= (struct uctrl
*)bv
;
132 if (a
->toplevel
< b
->toplevel
)
134 else if (a
->toplevel
> b
->toplevel
)
139 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
141 GtkWidget
*a
= (GtkWidget
*)av
;
142 struct uctrl
*b
= (struct uctrl
*)bv
;
145 else if (a
> b
->toplevel
)
150 static void dlg_init(struct dlgparam
*dp
)
152 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
153 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
154 dp
->coloursel_result
.ok
= FALSE
;
155 dp
->treeitems
= NULL
;
156 dp
->window
= dp
->cancelbutton
= dp
->currtreeitem
= NULL
;
158 dp
->currfocus
= NULL
;
161 static void dlg_cleanup(struct dlgparam
*dp
)
165 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
167 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
168 del234(dp
->bywidget
, uc
);
169 if (uc
->privdata_needs_free
)
174 freetree234(dp
->bywidget
);
176 sfree(dp
->treeitems
);
179 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
181 add234(dp
->byctrl
, uc
);
182 add234(dp
->bywidget
, uc
);
185 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
189 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
192 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
194 struct uctrl
*ret
= NULL
;
198 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
206 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
208 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
209 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
213 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
215 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
216 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
218 uc
->privdata_needs_free
= FALSE
;
221 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
223 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
224 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
226 * This is an internal allocation routine, so it's allowed to
227 * use smalloc directly.
229 uc
->privdata
= smalloc(size
);
230 uc
->privdata_needs_free
= FALSE
;
234 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
236 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
237 if (dp
->currfocus
!= ctrl
)
238 return dp
->currfocus
;
240 return dp
->lastfocus
;
243 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
245 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
246 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
247 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
248 assert(uc
->buttons
!= NULL
);
249 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
252 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
254 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
255 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
258 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
259 assert(uc
->buttons
!= NULL
);
260 for (i
= 0; i
< uc
->nbuttons
; i
++)
261 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
263 return 0; /* got to return something */
266 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
268 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
269 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
270 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
271 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
274 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
276 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
277 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
278 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
279 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
282 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
284 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
285 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
286 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
287 assert(uc
->entry
!= NULL
);
288 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), text
);
291 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
293 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
294 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
295 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
296 assert(uc
->entry
!= NULL
);
297 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
299 buffer
[length
-1] = '\0';
302 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
304 GtkContainer
*cont
= GTK_CONTAINER(data
);
305 /* gtk_container_remove will unref the widget for us; we need not. */
306 gtk_container_remove(cont
, w
);
309 /* The `listbox' functions can also apply to combo boxes. */
310 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
312 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
313 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
315 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
316 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
317 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
320 gtk_container_foreach(GTK_CONTAINER(uc
->menu
),
321 container_remove_and_destroy
,
322 GTK_CONTAINER(uc
->menu
));
324 gtk_list_clear_items(GTK_LIST(uc
->list
), 0, -1);
328 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
330 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
331 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
333 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
334 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
335 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
339 (GTK_CONTAINER(uc
->menu
),
340 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
342 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
346 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
348 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
352 * Each listbox entry may have a numeric id associated with it.
353 * Note that some front ends only permit a string to be stored at
354 * each position, which means that _if_ you put two identical
355 * strings in any listbox then you MUST not assign them different
356 * IDs and expect to get meaningful results back.
358 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
359 char const *text
, int id
)
361 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
362 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
364 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
365 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
366 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
368 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
372 * List item in a drop-down (but non-combo) list. Tabs are
373 * ignored; we just provide a standard menu item with the
376 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
378 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
379 gtk_widget_show(menuitem
);
381 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
382 GINT_TO_POINTER(id
));
383 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
384 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
385 } else if (!uc
->entry
) {
387 * List item in a non-combo-box list box. We make all of
388 * these Columns containing GtkLabels. This allows us to do
389 * the nasty force_left hack irrespective of whether there
390 * are tabs in the thing.
392 GtkWidget
*listitem
= gtk_list_item_new();
393 GtkWidget
*cols
= columns_new(10);
397 /* Count the tabs in the text, and hence determine # of columns. */
399 for (i
= 0; text
[i
]; i
++)
404 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
405 percents
= snewn(ncols
, gint
);
406 percents
[ncols
-1] = 100;
407 for (i
= 0; i
< ncols
-1; i
++) {
408 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
409 percents
[ncols
-1] -= percents
[i
];
411 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
414 for (i
= 0; i
< ncols
; i
++) {
415 int len
= strcspn(text
, "\t");
416 char *dup
= dupprintf("%.*s", len
, text
);
421 label
= gtk_label_new(dup
);
424 columns_add(COLUMNS(cols
), label
, i
, 1);
425 columns_force_left_align(COLUMNS(cols
), label
);
426 gtk_widget_show(label
);
428 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
429 gtk_widget_show(cols
);
430 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
431 gtk_widget_show(listitem
);
433 if (ctrl
->listbox
.multisel
) {
434 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
435 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
437 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
438 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
440 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
441 GTK_SIGNAL_FUNC(widget_focus
), dp
);
442 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
443 GTK_SIGNAL_FUNC(listitem_button
), dp
);
444 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
445 GINT_TO_POINTER(id
));
448 * List item in a combo-box list, which means the sensible
449 * thing to do is make it a perfectly normal label. Hence
450 * tabs are disregarded.
452 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
454 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
455 gtk_widget_show(listitem
);
457 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
458 GINT_TO_POINTER(id
));
461 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
464 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
466 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
467 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
471 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
472 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
473 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
475 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
477 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
478 g_list_free(children
);
480 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
), "user-data"));
483 /* dlg_listbox_index returns <0 if no single element is selected. */
484 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
486 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
487 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
489 GtkWidget
*item
, *activeitem
;
493 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
494 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
495 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
498 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
500 activeitem
= NULL
; /* unnecessarily placate gcc */
502 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
504 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
505 i
++, children
= children
->next
) {
506 if (uc
->menu ? activeitem
== item
:
507 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
514 g_list_free(children
);
515 return selected
< 0 ?
-1 : selected
;
518 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
520 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
521 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
523 GtkWidget
*item
, *activeitem
;
525 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
526 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
527 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
529 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
531 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
532 g_list_free(children
);
535 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
536 return item
== activeitem
;
538 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
542 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
544 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
545 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
547 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
548 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
549 assert(uc
->optmenu
!= NULL
|| uc
->list
!= NULL
);
552 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
556 gdouble newtop
, newbot
;
558 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
561 * Scroll the list box if necessary to ensure the newly
562 * selected item is visible.
564 items
= gtk_container_children(GTK_CONTAINER(uc
->list
));
565 nitems
= g_list_length(items
);
567 int modified
= FALSE
;
569 newtop
= uc
->adj
->lower
+
570 (uc
->adj
->upper
- uc
->adj
->lower
) * index
/ nitems
;
571 newbot
= uc
->adj
->lower
+
572 (uc
->adj
->upper
- uc
->adj
->lower
) * (index
+1) / nitems
;
573 if (uc
->adj
->value
> newtop
) {
575 uc
->adj
->value
= newtop
;
576 } else if (uc
->adj
->value
< newbot
- uc
->adj
->page_size
) {
578 uc
->adj
->value
= newbot
- uc
->adj
->page_size
;
581 gtk_adjustment_value_changed(uc
->adj
);
586 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
588 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
589 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
591 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
592 assert(uc
->text
!= NULL
);
594 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
597 void dlg_label_change(union control
*ctrl
, void *dlg
, char const *text
)
599 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
600 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
602 switch (uc
->ctrl
->generic
.type
) {
604 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
605 shortcut_highlight(uc
->toplevel
, ctrl
->button
.shortcut
);
608 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
609 shortcut_highlight(uc
->toplevel
, ctrl
->checkbox
.shortcut
);
612 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
613 shortcut_highlight(uc
->label
, ctrl
->radio
.shortcut
);
616 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
617 shortcut_highlight(uc
->label
, ctrl
->editbox
.shortcut
);
619 case CTRL_FILESELECT
:
620 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
621 shortcut_highlight(uc
->label
, ctrl
->fileselect
.shortcut
);
623 case CTRL_FONTSELECT
:
624 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
625 shortcut_highlight(uc
->label
, ctrl
->fontselect
.shortcut
);
628 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
629 shortcut_highlight(uc
->label
, ctrl
->listbox
.shortcut
);
632 assert(!"This shouldn't happen");
637 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
639 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
640 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
641 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
642 assert(uc
->entry
!= NULL
);
643 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
646 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
648 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
649 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
650 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
651 assert(uc
->entry
!= NULL
);
652 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
654 fn
->path
[lenof(fn
->path
)-1] = '\0';
657 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
659 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
660 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
661 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
662 assert(uc
->entry
!= NULL
);
663 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
666 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
668 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
669 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
670 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
671 assert(uc
->entry
!= NULL
);
672 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
674 fs
->name
[lenof(fs
->name
)-1] = '\0';
678 * Bracketing a large set of updates in these two functions will
679 * cause the front end (if possible) to delay updating the screen
680 * until it's all complete, thus avoiding flicker.
682 void dlg_update_start(union control
*ctrl
, void *dlg
)
685 * Apparently we can't do this at all in GTK. GtkCList supports
686 * freeze and thaw, but not GtkList. Bah.
690 void dlg_update_done(union control
*ctrl
, void *dlg
)
693 * Apparently we can't do this at all in GTK. GtkCList supports
694 * freeze and thaw, but not GtkList. Bah.
698 void dlg_set_focus(union control
*ctrl
, void *dlg
)
700 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
701 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
703 switch (ctrl
->generic
.type
) {
706 /* Check boxes and buttons get the focus _and_ get toggled. */
707 gtk_widget_grab_focus(uc
->toplevel
);
709 case CTRL_FILESELECT
:
710 case CTRL_FONTSELECT
:
712 /* Anything containing an edit box gets that focused. */
713 gtk_widget_grab_focus(uc
->entry
);
717 * Radio buttons: we find the currently selected button and
722 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
723 if (gtk_toggle_button_get_active
724 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
725 gtk_widget_grab_focus(uc
->buttons
[i
]);
731 * If the list is really an option menu, we focus it.
732 * Otherwise we tell it to focus one of its children, which
733 * appears to do the Right Thing.
736 gtk_widget_grab_focus(uc
->optmenu
);
738 assert(uc
->list
!= NULL
);
739 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
746 * During event processing, you might well want to give an error
747 * indication to the user. dlg_beep() is a quick and easy generic
748 * error; dlg_error() puts up a message-box or equivalent.
750 void dlg_beep(void *dlg
)
755 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
757 gtk_widget_destroy(GTK_WIDGET(data
));
760 static void set_transient_window_pos(GtkWidget
*parent
, GtkWidget
*child
)
762 gint x
, y
, w
, h
, dx
, dy
;
764 gtk_window_set_position(GTK_WINDOW(child
), GTK_WIN_POS_NONE
);
765 gtk_widget_size_request(GTK_WIDGET(child
), &req
);
767 gdk_window_get_origin(GTK_WIDGET(parent
)->window
, &x
, &y
);
768 gdk_window_get_size(GTK_WIDGET(parent
)->window
, &w
, &h
);
771 * One corner of the transient will be offset inwards, by 1/4
772 * of the parent window's size, from the corresponding corner
773 * of the parent window. The corner will be chosen so as to
774 * place the transient closer to the centre of the screen; this
775 * should avoid transients going off the edge of the screen on
778 if (x
+ w
/2 < gdk_screen_width() / 2)
779 dx
= x
+ w
/4; /* work from left edges */
781 dx
= x
+ 3*w
/4 - req
.width
; /* work from right edges */
782 if (y
+ h
/2 < gdk_screen_height() / 2)
783 dy
= y
+ h
/4; /* work from top edges */
785 dy
= y
+ 3*h
/4 - req
.height
; /* work from bottom edges */
786 gtk_widget_set_uposition(GTK_WIDGET(child
), dx
, dy
);
789 void dlg_error_msg(void *dlg
, char *msg
)
791 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
792 GtkWidget
*window
, *hbox
, *text
, *ok
;
794 window
= gtk_dialog_new();
795 text
= gtk_label_new(msg
);
796 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
797 hbox
= gtk_hbox_new(FALSE
, 0);
798 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
799 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
800 hbox
, FALSE
, FALSE
, 20);
801 gtk_widget_show(text
);
802 gtk_widget_show(hbox
);
803 gtk_window_set_title(GTK_WINDOW(window
), "Error");
804 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
805 ok
= gtk_button_new_with_label("OK");
806 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
807 ok
, FALSE
, FALSE
, 0);
809 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
810 gtk_window_set_default(GTK_WINDOW(window
), ok
);
811 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
812 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
813 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
814 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
815 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
816 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
817 set_transient_window_pos(dp
->window
, window
);
818 gtk_widget_show(window
);
823 * This function signals to the front end that the dialog's
824 * processing is completed, and passes an integer value (typically
827 void dlg_end(void *dlg
, int value
)
829 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
831 gtk_widget_destroy(dp
->window
);
834 void dlg_refresh(union control
*ctrl
, void *dlg
)
836 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
840 if (ctrl
->generic
.handler
!= NULL
)
841 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
845 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
846 assert(uc
->ctrl
!= NULL
);
847 if (uc
->ctrl
->generic
.handler
!= NULL
)
848 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
849 dp
->data
, EVENT_REFRESH
);
854 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
856 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
857 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
860 GtkWidget
*coloursel
=
861 gtk_color_selection_dialog_new("Select a colour");
862 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
864 dp
->coloursel_result
.ok
= FALSE
;
866 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
867 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
868 cvals
[0] = r
/ 255.0;
869 cvals
[1] = g
/ 255.0;
870 cvals
[2] = b
/ 255.0;
871 cvals
[3] = 1.0; /* fully opaque! */
872 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
874 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
875 (gpointer
)coloursel
);
876 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
877 (gpointer
)coloursel
);
878 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
879 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
880 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
881 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
882 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
883 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
884 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
885 (gpointer
)coloursel
);
886 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
887 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
888 (gpointer
)coloursel
);
889 gtk_widget_show(coloursel
);
892 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
893 int *r
, int *g
, int *b
)
895 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
896 if (dp
->coloursel_result
.ok
) {
897 *r
= dp
->coloursel_result
.r
;
898 *g
= dp
->coloursel_result
.g
;
899 *b
= dp
->coloursel_result
.b
;
905 /* ----------------------------------------------------------------------
906 * Signal handlers while the dialog box is active.
909 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
912 struct dlgparam
*dp
= (struct dlgparam
*)data
;
913 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
914 union control
*focus
;
921 if (focus
!= dp
->currfocus
) {
922 dp
->lastfocus
= dp
->currfocus
;
923 dp
->currfocus
= focus
;
929 static void button_clicked(GtkButton
*button
, gpointer data
)
931 struct dlgparam
*dp
= (struct dlgparam
*)data
;
932 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
933 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
936 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
938 struct dlgparam
*dp
= (struct dlgparam
*)data
;
939 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
940 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
943 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
946 * GtkEntry has a nasty habit of eating the Return key, which
947 * is unhelpful since it doesn't actually _do_ anything with it
948 * (it calls gtk_widget_activate, but our edit boxes never need
949 * activating). So I catch Return before GtkEntry sees it, and
950 * pass it straight on to the parent widget. Effect: hitting
951 * Return in an edit box will now activate the default button
952 * in the dialog just like it will everywhere else.
954 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
956 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
957 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
964 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
966 struct dlgparam
*dp
= (struct dlgparam
*)data
;
967 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
968 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
969 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
973 static void editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
976 struct dlgparam
*dp
= (struct dlgparam
*)data
;
977 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
978 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
981 static int listitem_key(GtkWidget
*item
, GdkEventKey
*event
, gpointer data
,
984 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
986 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
987 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
988 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
989 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
991 * Up, Down, PgUp or PgDn have been pressed on a ListItem
992 * in a list box. So, if the list box is single-selection:
994 * - if the list item in question isn't already selected,
995 * we simply select it.
996 * - otherwise, we find the next one (or next
997 * however-far-away) in whichever direction we're going,
999 * + in this case, we must also fiddle with the
1000 * scrollbar to ensure the newly selected item is
1003 * If it's multiple-selection, we do all of the above
1004 * except actually selecting anything, so we move the focus
1005 * and fiddle the scrollbar to follow it.
1007 GtkWidget
*list
= item
->parent
;
1009 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
1012 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
1013 gtk_list_select_child(GTK_LIST(list
), item
);
1016 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
1017 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1020 (event
->keyval
==GDK_Page_Down
||
1021 event
->keyval
==GDK_KP_Page_Down
||
1022 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1025 GList
*children
, *chead
;
1027 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
1029 n
= g_list_length(children
);
1033 * Figure out how many list items to a screenful,
1034 * and adjust the step appropriately.
1036 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
1037 step
--; /* go by one less than that */
1041 while (children
!= NULL
) {
1042 if (item
== children
->data
)
1044 children
= children
->next
;
1049 if (direction
< 0 && i
> 0)
1050 children
= children
->prev
, i
--;
1051 else if (direction
> 0 && i
< n
-1)
1052 children
= children
->next
, i
++;
1056 if (children
&& children
->data
) {
1058 gtk_list_select_child(GTK_LIST(list
),
1059 GTK_WIDGET(children
->data
));
1060 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
1061 gtk_adjustment_clamp_page
1063 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
1064 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
1075 static int listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
1078 return listitem_key(item
, event
, data
, FALSE
);
1081 static int listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
1084 return listitem_key(item
, event
, data
, TRUE
);
1087 static int listitem_button(GtkWidget
*item
, GdkEventButton
*event
,
1090 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1091 if (event
->type
== GDK_2BUTTON_PRESS
||
1092 event
->type
== GDK_3BUTTON_PRESS
) {
1093 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1094 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1100 static void list_selchange(GtkList
*list
, gpointer data
)
1102 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1103 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1105 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1108 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1110 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1111 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1112 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1113 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1114 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1117 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1119 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1120 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1124 (index
== 0 && direction
< 0) ||
1125 (index
== g_list_length(children
)-1 && direction
> 0)) {
1130 child
= g_list_nth_data(children
, index
);
1131 gtk_widget_ref(child
);
1132 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1133 g_list_free(children
);
1136 children
= g_list_append(children
, child
);
1137 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1138 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1139 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1142 static void draglist_up(GtkButton
*button
, gpointer data
)
1144 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1145 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1146 draglist_move(dp
, uc
, -1);
1149 static void draglist_down(GtkButton
*button
, gpointer data
)
1151 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1152 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1153 draglist_move(dp
, uc
, +1);
1156 static void filesel_ok(GtkButton
*button
, gpointer data
)
1158 /* struct dlgparam *dp = (struct dlgparam *)data; */
1159 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1160 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1161 char *name
= gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1162 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1165 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1167 /* struct dlgparam *dp = (struct dlgparam *)data; */
1168 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1169 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1170 char *name
= gtk_font_selection_dialog_get_font_name
1171 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1172 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1175 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1177 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1178 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1179 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1181 gtk_color_selection_get_color
1182 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1184 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1185 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1186 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1187 dp
->coloursel_result
.ok
= TRUE
;
1188 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1191 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1193 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1194 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1195 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1196 dp
->coloursel_result
.ok
= FALSE
;
1197 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1200 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1202 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1203 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1205 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1206 GtkWidget
*filesel
=
1207 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1208 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1210 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1212 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1214 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1215 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1216 gtk_signal_connect_object
1217 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1218 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1219 gtk_signal_connect_object
1220 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1221 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1222 gtk_widget_show(filesel
);
1225 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1226 gchar
*spacings
[] = { "c", "m", NULL
};
1227 gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1228 GtkWidget
*fontsel
=
1229 gtk_font_selection_dialog_new("Select a font");
1230 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1231 gtk_font_selection_dialog_set_filter
1232 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1233 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1234 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1235 if (!gtk_font_selection_dialog_set_font_name
1236 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1238 * If the font name wasn't found as it was, try opening
1239 * it and extracting its FONT property. This should
1240 * have the effect of mapping short aliases into true
1243 GdkFont
*font
= gdk_font_load(fontname
);
1245 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1246 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1247 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1249 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1250 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1252 gtk_font_selection_dialog_set_font_name
1253 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1255 gdk_font_unref(font
);
1259 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1260 "user-data", (gpointer
)fontsel
);
1261 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1263 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1264 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1265 gtk_signal_connect_object
1266 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1267 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1269 gtk_signal_connect_object
1270 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1271 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1273 gtk_widget_show(fontsel
);
1277 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1280 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1281 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1283 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1284 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1285 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1288 /* ----------------------------------------------------------------------
1289 * This function does the main layout work: it reads a controlset,
1290 * it creates the relevant GTK controls, and returns a GtkWidget
1291 * containing the result. (This widget might be a title of some
1292 * sort, it might be a Columns containing many controls, or it
1293 * might be a GtkFrame containing a Columns; whatever it is, it's
1294 * definitely a GtkWidget and should probably be added to a
1297 * `listitemheight' is used to calculate a usize for list boxes: it
1298 * should be the height from the size request of a GtkListItem.
1300 * `win' is required for setting the default button. If it is
1301 * non-NULL, all buttons created will be default-capable (so they
1302 * have extra space round them for the default highlight).
1304 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1305 struct controlset
*s
, int listitemheight
,
1312 if (!s
->boxname
&& s
->boxtitle
) {
1313 /* This controlset is a panel title. */
1314 return gtk_label_new(s
->boxtitle
);
1318 * Otherwise, we expect to be laying out actual controls, so
1319 * we'll start by creating a Columns for the purpose.
1321 cols
= COLUMNS(columns_new(4));
1322 ret
= GTK_WIDGET(cols
);
1323 gtk_widget_show(ret
);
1326 * Create a containing frame if we have a box name.
1329 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1330 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1331 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1332 gtk_widget_show(ret
);
1336 * Now iterate through the controls themselves, create them,
1337 * and add them to the Columns.
1339 for (i
= 0; i
< s
->ncontrols
; i
++) {
1340 union control
*ctrl
= s
->ctrls
[i
];
1343 GtkWidget
*w
= NULL
;
1345 switch (ctrl
->generic
.type
) {
1348 static const int simplecols
[1] = { 100 };
1349 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1350 (ctrl
->columns
.percentages ?
1351 ctrl
->columns
.percentages
: simplecols
));
1353 continue; /* no actual control created */
1356 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1358 columns_taborder_last(cols
, uc
->toplevel
);
1360 continue; /* no actual control created */
1363 uc
= snew(struct uctrl
);
1365 uc
->privdata
= NULL
;
1366 uc
->privdata_needs_free
= FALSE
;
1368 uc
->entry
= uc
->list
= uc
->menu
= NULL
;
1369 uc
->button
= uc
->optmenu
= uc
->text
= NULL
;
1372 switch (ctrl
->generic
.type
) {
1374 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1376 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1377 if (ctrl
->button
.isdefault
)
1378 gtk_window_set_default(win
, w
);
1379 if (ctrl
->button
.iscancel
)
1380 dp
->cancelbutton
= w
;
1382 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1383 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1384 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1385 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1386 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1387 SHORTCUT_UCTRL
, uc
);
1390 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1391 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1392 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1393 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1394 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1395 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1396 SHORTCUT_UCTRL
, uc
);
1401 * Radio buttons get to go inside their own Columns, no
1405 gint i
, *percentages
;
1409 if (ctrl
->generic
.label
) {
1410 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1411 columns_add(COLUMNS(w
), label
, 0, 1);
1412 columns_force_left_align(COLUMNS(w
), label
);
1413 gtk_widget_show(label
);
1414 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1415 SHORTCUT_UCTRL
, uc
);
1418 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1419 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1421 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1422 100 * i
/ ctrl
->radio
.ncolumns
);
1424 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1426 g_free(percentages
);
1429 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1430 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1432 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1436 b
= (gtk_radio_button_new_with_label
1437 (group
, ctrl
->radio
.buttons
[i
]));
1439 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1440 colstart
= i
% ctrl
->radio
.ncolumns
;
1441 columns_add(COLUMNS(w
), b
, colstart
,
1442 (i
== ctrl
->radio
.nbuttons
-1 ?
1443 ctrl
->radio
.ncolumns
- colstart
: 1));
1444 columns_force_left_align(COLUMNS(w
), b
);
1446 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1447 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1448 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1449 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1450 if (ctrl
->radio
.shortcuts
) {
1451 shortcut_add(scs
, GTK_BIN(b
)->child
,
1452 ctrl
->radio
.shortcuts
[i
],
1453 SHORTCUT_UCTRL
, uc
);
1462 if (ctrl
->editbox
.has_list
) {
1463 w
= gtk_combo_new();
1464 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1465 uc
->entry
= GTK_COMBO(w
)->entry
;
1466 uc
->list
= GTK_COMBO(w
)->list
;
1468 w
= gtk_entry_new();
1469 if (ctrl
->editbox
.password
)
1470 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1473 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1474 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1475 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1476 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1477 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1478 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1480 * Edit boxes, for some strange reason, have a minimum
1481 * width of 150 in GTK 1.2. We don't want this - we'd
1482 * rather the edit boxes acquired their natural width
1483 * from the column layout of the rest of the box.
1485 * Also, while we're here, we'll squirrel away the
1486 * edit box height so we can use that to centre its
1487 * label vertically beside it.
1489 gtk_widget_size_request(w
, &req
);
1490 gtk_widget_set_usize(w
, 10, req
.height
);
1492 if (ctrl
->generic
.label
) {
1493 GtkWidget
*label
, *container
;
1495 label
= gtk_label_new(ctrl
->generic
.label
);
1497 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
1498 SHORTCUT_FOCUS
, uc
->entry
);
1500 container
= columns_new(4);
1501 if (ctrl
->editbox
.percentwidth
== 100) {
1502 columns_add(COLUMNS(container
), label
, 0, 1);
1503 columns_force_left_align(COLUMNS(container
), label
);
1504 columns_add(COLUMNS(container
), w
, 0, 1);
1506 gint percentages
[2];
1507 percentages
[1] = ctrl
->editbox
.percentwidth
;
1508 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
1509 columns_set_cols(COLUMNS(container
), 2, percentages
);
1510 columns_add(COLUMNS(container
), label
, 0, 1);
1511 columns_force_left_align(COLUMNS(container
), label
);
1512 columns_add(COLUMNS(container
), w
, 1, 1);
1513 /* Centre the label vertically. */
1514 gtk_widget_set_usize(label
, -1, req
.height
);
1515 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1517 gtk_widget_show(label
);
1523 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_out_event",
1524 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1527 case CTRL_FILESELECT
:
1528 case CTRL_FONTSELECT
:
1533 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1534 "Browse..." : "Change...");
1536 gint percentages
[] = { 75, 25 };
1538 columns_set_cols(COLUMNS(w
), 2, percentages
);
1540 if (ctrl
->generic
.label
) {
1541 ww
= gtk_label_new(ctrl
->generic
.label
);
1542 columns_add(COLUMNS(w
), ww
, 0, 2);
1543 columns_force_left_align(COLUMNS(w
), ww
);
1544 gtk_widget_show(ww
);
1545 shortcut_add(scs
, ww
,
1546 (ctrl
->generic
.type
== CTRL_FILESELECT ?
1547 ctrl
->fileselect
.shortcut
:
1548 ctrl
->fontselect
.shortcut
),
1549 SHORTCUT_UCTRL
, uc
);
1553 uc
->entry
= ww
= gtk_entry_new();
1554 gtk_widget_size_request(ww
, &req
);
1555 gtk_widget_set_usize(ww
, 10, req
.height
);
1556 columns_add(COLUMNS(w
), ww
, 0, 1);
1557 gtk_widget_show(ww
);
1559 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
1560 columns_add(COLUMNS(w
), ww
, 1, 1);
1561 gtk_widget_show(ww
);
1563 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
1564 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1565 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
1566 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1567 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
1568 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1569 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
1570 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1571 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
1572 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
1576 if (ctrl
->listbox
.height
== 0) {
1577 uc
->optmenu
= w
= gtk_option_menu_new();
1578 uc
->menu
= gtk_menu_new();
1579 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
1580 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
1581 (gpointer
)uc
->optmenu
);
1582 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
1583 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1585 uc
->list
= gtk_list_new();
1586 if (ctrl
->listbox
.multisel
== 2) {
1587 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1588 GTK_SELECTION_EXTENDED
);
1589 } else if (ctrl
->listbox
.multisel
== 1) {
1590 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1591 GTK_SELECTION_MULTIPLE
);
1593 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
1594 GTK_SELECTION_SINGLE
);
1596 w
= gtk_scrolled_window_new(NULL
, NULL
);
1597 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
1599 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
1601 GTK_POLICY_AUTOMATIC
);
1602 uc
->adj
= gtk_scrolled_window_get_vadjustment
1603 (GTK_SCROLLED_WINDOW(w
));
1605 gtk_widget_show(uc
->list
);
1606 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
1607 GTK_SIGNAL_FUNC(list_selchange
), dp
);
1608 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
1609 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1612 * Adjust the height of the scrolled window to the
1613 * minimum given by the height parameter.
1615 * This piece of guesswork is a horrid hack based
1616 * on looking inside the GTK 1.2 sources
1617 * (specifically gtkviewport.c, which appears to be
1618 * the widget which provides the border around the
1619 * scrolling area). Anyone lets me know how I can
1620 * do this in a way which isn't at risk from GTK
1621 * upgrades, I'd be grateful.
1624 int edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
1625 gtk_widget_set_usize(w
, 10,
1626 2*edge
+ (ctrl
->listbox
.height
*
1630 if (ctrl
->listbox
.draglist
) {
1632 * GTK doesn't appear to make it easy to
1633 * implement a proper draggable list; so
1634 * instead I'm just going to have to put an Up
1635 * and a Down button to the right of the actual
1636 * list box. Ah well.
1638 GtkWidget
*cols
, *button
;
1639 static const gint percentages
[2] = { 80, 20 };
1641 cols
= columns_new(4);
1642 columns_set_cols(COLUMNS(cols
), 2, percentages
);
1643 columns_add(COLUMNS(cols
), w
, 0, 1);
1645 button
= gtk_button_new_with_label("Up");
1646 columns_add(COLUMNS(cols
), button
, 1, 1);
1647 gtk_widget_show(button
);
1648 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1649 GTK_SIGNAL_FUNC(draglist_up
), dp
);
1650 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1651 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1652 button
= gtk_button_new_with_label("Down");
1653 columns_add(COLUMNS(cols
), button
, 1, 1);
1654 gtk_widget_show(button
);
1655 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1656 GTK_SIGNAL_FUNC(draglist_down
), dp
);
1657 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
1658 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1664 if (ctrl
->generic
.label
) {
1665 GtkWidget
*label
, *container
;
1667 label
= gtk_label_new(ctrl
->generic
.label
);
1669 container
= columns_new(4);
1670 if (ctrl
->listbox
.percentwidth
== 100) {
1671 columns_add(COLUMNS(container
), label
, 0, 1);
1672 columns_force_left_align(COLUMNS(container
), label
);
1673 columns_add(COLUMNS(container
), w
, 0, 1);
1675 gint percentages
[2];
1676 percentages
[1] = ctrl
->listbox
.percentwidth
;
1677 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
1678 columns_set_cols(COLUMNS(container
), 2, percentages
);
1679 columns_add(COLUMNS(container
), label
, 0, 1);
1680 columns_force_left_align(COLUMNS(container
), label
);
1681 columns_add(COLUMNS(container
), w
, 1, 1);
1683 gtk_widget_show(label
);
1685 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
1686 SHORTCUT_UCTRL
, uc
);
1693 * Wrapping text widgets don't sit well with the GTK
1694 * layout model, in which widgets state a minimum size
1695 * and the whole window then adjusts to the smallest
1696 * size it can sensibly take given its contents. A
1697 * wrapping text widget _has_ no clear minimum size;
1698 * instead it has a range of possibilities. It can be
1699 * one line deep but 2000 wide, or two lines deep and
1700 * 1000 pixels, or three by 867, or four by 500 and so
1701 * on. It can be as short as you like provided you
1702 * don't mind it being wide, or as narrow as you like
1703 * provided you don't mind it being tall.
1705 * Therefore, it fits very badly into the layout model.
1706 * Hence the only thing to do is pick a width and let
1707 * it choose its own number of lines. To do this I'm
1708 * going to cheat a little. All new wrapping text
1709 * widgets will be created with a minimal text content
1710 * "X"; then, after the rest of the dialog box is set
1711 * up and its size calculated, the text widgets will be
1712 * told their width and given their real text, which
1713 * will cause the size to be recomputed in the y
1714 * direction (because many of them will expand to more
1717 uc
->text
= w
= gtk_label_new("X");
1718 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
1719 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
1721 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
1722 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
1728 columns_add(cols
, w
,
1729 COLUMN_START(ctrl
->generic
.column
),
1730 COLUMN_SPAN(ctrl
->generic
.column
));
1732 columns_force_left_align(cols
, w
);
1736 dlg_add_uctrl(dp
, uc
);
1743 struct dlgparam
*dp
;
1745 GtkWidget
*panel
, *treeitem
;
1746 struct Shortcuts shortcuts
;
1749 static void treeitem_sel(GtkItem
*item
, gpointer data
)
1751 struct selparam
*sp
= (struct selparam
*)data
;
1753 panels_switch_to(sp
->panels
, sp
->panel
);
1755 dlg_refresh(NULL
, sp
->dp
);
1757 sp
->dp
->shortcuts
= &sp
->shortcuts
;
1758 sp
->dp
->currtreeitem
= sp
->treeitem
;
1761 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1766 static int tree_grab_focus(struct dlgparam
*dp
)
1771 * See if any of the treeitems has the focus.
1774 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1775 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
1783 gtk_widget_grab_focus(dp
->currtreeitem
);
1788 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
1791 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1793 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
1795 * If there's a focused treeitem, we return FALSE to cause the
1796 * focus to move on to some totally other control. If not, we
1797 * focus the selected one.
1799 return tree_grab_focus(dp
);
1802 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1804 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1806 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
1807 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
1811 if ((event
->state
& GDK_MOD1_MASK
) &&
1812 (unsigned char)event
->string
[0] > 0 &&
1813 (unsigned char)event
->string
[0] <= 127) {
1814 int schr
= (unsigned char)event
->string
[0];
1815 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
1817 switch (sc
->action
) {
1819 tree_grab_focus(dp
);
1821 case SHORTCUT_FOCUS
:
1822 gtk_widget_grab_focus(sc
->widget
);
1824 case SHORTCUT_UCTRL
:
1826 * We must do something sensible with a uctrl.
1827 * Precisely what this is depends on the type of
1830 switch (sc
->uc
->ctrl
->generic
.type
) {
1833 /* Check boxes and buttons get the focus _and_ get toggled. */
1834 gtk_widget_grab_focus(sc
->uc
->toplevel
);
1835 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
1838 case CTRL_FILESELECT
:
1839 case CTRL_FONTSELECT
:
1840 /* File/font selectors have their buttons pressed (ooer),
1841 * and focus transferred to the edit box. */
1842 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
1844 gtk_widget_grab_focus(sc
->uc
->entry
);
1848 * Radio buttons are fun, because they have
1849 * multiple shortcuts. We must find whether the
1850 * activated shortcut is the shortcut for the whole
1851 * group, or for a particular button. In the former
1852 * case, we find the currently selected button and
1853 * focus it; in the latter, we focus-and-click the
1854 * button whose shortcut was pressed.
1856 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
1858 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1859 if (gtk_toggle_button_get_active
1860 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
1861 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1863 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
1865 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
1866 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
1867 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
1868 gtk_signal_emit_by_name
1869 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
1875 * If the list is really an option menu, we focus
1876 * and click it. Otherwise we tell it to focus one
1877 * of its children, which appears to do the Right
1880 if (sc
->uc
->optmenu
) {
1884 gtk_widget_grab_focus(sc
->uc
->optmenu
);
1885 /* Option menus don't work using the "clicked" signal.
1886 * We need to manufacture a button press event :-/ */
1887 bev
.type
= GDK_BUTTON_PRESS
;
1889 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
1890 "button_press_event",
1893 assert(sc
->uc
->list
!= NULL
);
1895 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
1896 GTK_DIR_TAB_FORWARD
);
1907 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1909 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1911 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1912 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
1914 for (i
= 0; i
< dp
->ntreeitems
; i
++)
1915 if (widget
== dp
->treeitems
[i
])
1917 if (i
< dp
->ntreeitems
) {
1918 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
1925 if (i
< 0 || i
>= dp
->ntreeitems
)
1926 break; /* nothing in that dir to select */
1928 * Determine if this tree item is visible.
1931 GtkWidget
*w
= dp
->treeitems
[i
];
1933 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
1934 if (!GTK_WIDGET_VISIBLE(w
)) {
1941 j
= i
; /* got one */
1947 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1950 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
1951 gtk_widget_grab_focus(dp
->treeitems
[j
]);
1957 * It's nice for Left and Right to expand and collapse tree
1960 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
1961 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1963 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
1966 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
1967 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
1969 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
1976 static void shortcut_highlight(GtkWidget
*labelw
, int chr
)
1978 GtkLabel
*label
= GTK_LABEL(labelw
);
1979 gchar
*currstr
, *pattern
;
1982 gtk_label_get(label
, &currstr
);
1983 for (i
= 0; currstr
[i
]; i
++)
1984 if (tolower((unsigned char)currstr
[i
]) == chr
) {
1987 pattern
= dupprintf("%*s_", i
, "");
1989 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
1990 gtk_label_set_pattern(label
, pattern
);
1991 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
1998 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
1999 int chr
, int action
, void *ptr
)
2001 if (chr
== NO_SHORTCUT
)
2004 chr
= tolower((unsigned char)chr
);
2006 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
2008 scs
->sc
[chr
].action
= action
;
2010 if (action
== SHORTCUT_FOCUS
) {
2011 scs
->sc
[chr
].uc
= NULL
;
2012 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
2014 scs
->sc
[chr
].widget
= NULL
;
2015 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
2018 shortcut_highlight(labelw
, chr
);
2021 int get_listitemheight(void)
2023 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
2025 gtk_widget_size_request(listitem
, &req
);
2026 gtk_widget_unref(listitem
);
2030 int do_config_box(const char *title
, Config
*cfg
, int midsession
,
2033 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
2034 *tree
, *treescroll
, *panels
, *panelvbox
;
2035 int index
, level
, listitemheight
;
2036 struct controlbox
*ctrlbox
;
2038 GtkTreeItem
*treeitemlevels
[8];
2039 GtkTree
*treelevels
[8];
2041 struct Shortcuts scs
;
2043 struct selparam
*selparams
= NULL
;
2044 int nselparams
= 0, selparamsize
= 0;
2048 listitemheight
= get_listitemheight();
2050 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2051 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2054 window
= gtk_dialog_new();
2056 ctrlbox
= ctrl_new_box();
2057 setup_config_box(ctrlbox
, midsession
, cfg
->protocol
, protcfginfo
);
2058 unix_setup_config_box(ctrlbox
, midsession
, cfg
->protocol
);
2059 gtk_setup_config_box(ctrlbox
, midsession
, window
);
2061 gtk_window_set_title(GTK_WINDOW(window
), title
);
2062 hbox
= gtk_hbox_new(FALSE
, 4);
2063 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
2064 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
2065 gtk_widget_show(hbox
);
2066 vbox
= gtk_vbox_new(FALSE
, 4);
2067 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
2068 gtk_widget_show(vbox
);
2069 cols
= columns_new(4);
2070 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
2071 gtk_widget_show(cols
);
2072 label
= gtk_label_new("Category:");
2073 columns_add(COLUMNS(cols
), label
, 0, 1);
2074 columns_force_left_align(COLUMNS(cols
), label
);
2075 gtk_widget_show(label
);
2076 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
2077 tree
= gtk_tree_new();
2078 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
2079 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2080 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
2081 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
2082 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
2083 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
2085 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
2087 GTK_POLICY_AUTOMATIC
);
2088 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
2089 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
2090 gtk_widget_show(tree
);
2091 gtk_widget_show(treescroll
);
2092 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
2093 panels
= panels_new();
2094 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
2095 gtk_widget_show(panels
);
2100 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2101 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2104 if (!*s
->pathname
) {
2105 w
= layout_ctrls(&dp
, &scs
, s
, listitemheight
, GTK_WINDOW(window
));
2106 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2109 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
2110 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
2112 GtkWidget
*treeitem
;
2116 * We expect never to find an implicit path
2117 * component. For example, we expect never to see
2118 * A/B/C followed by A/D/E, because that would
2119 * _implicitly_ create A/D. All our path prefixes
2120 * are expected to contain actual controls and be
2121 * selectable in the treeview; so we would expect
2122 * to see A/D _explicitly_ before encountering
2125 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2127 c
= strrchr(s
->pathname
, '/');
2133 treeitem
= gtk_tree_item_new_with_label(c
);
2134 assert(j
-1 < level
);
2136 if (!treelevels
[j
-1]) {
2137 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
2138 gtk_tree_item_set_subtree
2139 (treeitemlevels
[j
-1],
2140 GTK_WIDGET(treelevels
[j
-1]));
2141 gtk_tree_item_expand(treeitemlevels
[j
-1]);
2143 gtk_tree_append(treelevels
[j
-1], treeitem
);
2145 gtk_tree_append(GTK_TREE(tree
), treeitem
);
2147 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
2148 treelevels
[j
] = NULL
;
2151 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
2152 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
2153 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
2154 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2156 gtk_widget_show(treeitem
);
2160 first
= (panelvbox
== NULL
);
2162 panelvbox
= gtk_vbox_new(FALSE
, 4);
2163 gtk_container_add(GTK_CONTAINER(panels
), panelvbox
);
2165 panels_switch_to(PANELS(panels
), panelvbox
);
2166 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
2169 if (nselparams
>= selparamsize
) {
2171 selparams
= sresize(selparams
, selparamsize
,
2174 selparams
[nselparams
].dp
= &dp
;
2175 selparams
[nselparams
].panels
= PANELS(panels
);
2176 selparams
[nselparams
].panel
= panelvbox
;
2177 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2178 selparams
[nselparams
].treeitem
= treeitem
;
2183 w
= layout_ctrls(&dp
,
2184 &selparams
[nselparams
-1].shortcuts
,
2185 s
, listitemheight
, NULL
);
2186 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
2191 dp
.ntreeitems
= nselparams
;
2192 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
2194 for (index
= 0; index
< nselparams
; index
++) {
2195 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
2196 GTK_SIGNAL_FUNC(treeitem_sel
),
2198 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
2202 dlg_refresh(NULL
, &dp
);
2204 dp
.shortcuts
= &selparams
[0].shortcuts
;
2205 dp
.currtreeitem
= dp
.treeitems
[0];
2206 dp
.lastfocus
= NULL
;
2210 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2211 gtk_widget_show(window
);
2214 * Set focus into the first available control.
2216 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2217 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2222 for (j
= 0; j
< s
->ncontrols
; j
++)
2223 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
2224 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
2225 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
2226 dlg_set_focus(s
->ctrls
[j
], &dp
);
2227 dp
.lastfocus
= s
->ctrls
[j
];
2236 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2237 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2238 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2239 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2249 static void messagebox_handler(union control
*ctrl
, void *dlg
,
2250 void *data
, int event
)
2252 if (event
== EVENT_ACTION
)
2253 dlg_end(dlg
, ctrl
->generic
.context
.i
);
2255 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
2257 GtkWidget
*window
, *w0
, *w1
;
2258 struct controlbox
*ctrlbox
;
2259 struct controlset
*s0
, *s1
;
2262 struct Shortcuts scs
;
2268 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2269 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2272 ctrlbox
= ctrl_new_box();
2275 va_start(ap
, minwid
);
2276 while (va_arg(ap
, char *) != NULL
) {
2278 (void) va_arg(ap
, int); /* shortcut */
2279 (void) va_arg(ap
, int); /* normal/default/cancel */
2280 (void) va_arg(ap
, int); /* end value */
2284 s0
= ctrl_getset(ctrlbox
, "", "", "");
2285 c
= ctrl_columns(s0
, 2, 50, 50);
2286 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
2287 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
2288 for (index
= 0; index
< ncols
; index
++)
2289 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
2290 va_start(ap
, minwid
);
2293 char *title
= va_arg(ap
, char *);
2294 int shortcut
, type
, value
;
2297 shortcut
= va_arg(ap
, int);
2298 type
= va_arg(ap
, int);
2299 value
= va_arg(ap
, int);
2300 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
2301 messagebox_handler
, I(value
));
2302 c
->generic
.column
= index
++;
2304 c
->button
.isdefault
= TRUE
;
2306 c
->button
.iscancel
= TRUE
;
2310 s1
= ctrl_getset(ctrlbox
, "x", "", "");
2311 ctrl_text(s1
, msg
, HELPCTX(no_help
));
2313 window
= gtk_dialog_new();
2314 gtk_window_set_title(GTK_WINDOW(window
), title
);
2315 w0
= layout_ctrls(&dp
, &scs
, s0
, 0, GTK_WINDOW(window
));
2316 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2318 gtk_widget_show(w0
);
2319 w1
= layout_ctrls(&dp
, &scs
, s1
, 0, GTK_WINDOW(window
));
2320 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2321 gtk_widget_set_usize(w1
, minwid
+20, -1);
2322 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2324 gtk_widget_show(w1
);
2326 dp
.shortcuts
= &scs
;
2327 dp
.lastfocus
= NULL
;
2331 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
2333 set_transient_window_pos(parentwin
, window
);
2334 gtk_window_set_transient_for(GTK_WINDOW(window
),
2335 GTK_WINDOW(parentwin
));
2337 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2338 gtk_widget_show(window
);
2340 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2341 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
2342 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2343 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
2348 ctrl_free_box(ctrlbox
);
2353 static int string_width(char *text
)
2355 GtkWidget
*label
= gtk_label_new(text
);
2357 gtk_widget_size_request(label
, &req
);
2358 gtk_widget_unref(label
);
2362 int reallyclose(void *frontend
)
2364 char *title
= dupcat(appname
, " Exit Confirmation", NULL
);
2365 int ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2366 title
, "Are you sure you want to close this session?",
2367 string_width("Most of the width of the above text"),
2375 int verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
2376 char *keystr
, char *fingerprint
,
2377 void (*callback
)(void *ctx
, int result
), void *ctx
)
2379 static const char absenttxt
[] =
2380 "The server's host key is not cached. You have no guarantee "
2381 "that the server is the computer you think it is.\n"
2382 "The server's %s key fingerprint is:\n"
2384 "If you trust this host, press \"Accept\" to add the key to "
2385 "PuTTY's cache and carry on connecting.\n"
2386 "If you want to carry on connecting just once, without "
2387 "adding the key to the cache, press \"Connect Once\".\n"
2388 "If you do not trust this host, press \"Cancel\" to abandon the "
2390 static const char wrongtxt
[] =
2391 "WARNING - POTENTIAL SECURITY BREACH!\n"
2392 "The server's host key does not match the one PuTTY has "
2393 "cached. This means that either the server administrator "
2394 "has changed the host key, or you have actually connected "
2395 "to another computer pretending to be the server.\n"
2396 "The new %s key fingerprint is:\n"
2398 "If you were expecting this change and trust the new key, "
2399 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
2400 "If you want to carry on connecting but without updating "
2401 "the cache, press \"Connect Once\".\n"
2402 "If you want to abandon the connection completely, press "
2403 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
2411 ret
= verify_host_key(host
, port
, keytype
, keystr
);
2413 if (ret
== 0) /* success - key matched OK */
2416 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), keytype
, fingerprint
);
2418 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2419 "PuTTY Security Alert", text
,
2420 string_width(fingerprint
),
2421 "Accept", 'a', 0, 2,
2422 "Connect Once", 'o', 0, 1,
2423 "Cancel", 'c', -1, 0,
2429 store_host_key(host
, port
, keytype
, keystr
);
2430 return 1; /* continue with connection */
2431 } else if (ret
== 1)
2432 return 1; /* continue with connection */
2433 return 0; /* do not continue with connection */
2437 * Ask whether the selected algorithm is acceptable (since it was
2438 * below the configured 'warn' threshold).
2440 int askalg(void *frontend
, const char *algtype
, const char *algname
,
2441 void (*callback
)(void *ctx
, int result
), void *ctx
)
2443 static const char msg
[] =
2444 "The first %s supported by the server is "
2445 "%s, which is below the configured warning threshold.\n"
2446 "Continue with connection?";
2450 text
= dupprintf(msg
, algtype
, algname
);
2451 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
2452 "PuTTY Security Alert", text
,
2453 string_width("Continue with connection?"),
2466 void old_keyfile_warning(void)
2469 * This should never happen on Unix. We hope.
2473 void fatal_message_box(void *window
, char *msg
)
2475 messagebox(window
, "PuTTY Fatal Error", msg
,
2476 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
2477 "OK", 'o', 1, 1, NULL
);
2480 void fatalbox(char *p
, ...)
2485 msg
= dupvprintf(p
, ap
);
2487 fatal_message_box(NULL
, msg
);
2492 static GtkWidget
*aboutbox
= NULL
;
2494 static void about_close_clicked(GtkButton
*button
, gpointer data
)
2496 gtk_widget_destroy(aboutbox
);
2500 static void licence_clicked(GtkButton
*button
, gpointer data
)
2505 "Copyright 1997-2006 Simon Tatham.\n\n"
2507 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
2508 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
2509 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
2510 "Markus Kuhn, and CORE SDI S.A.\n\n"
2512 "Permission is hereby granted, free of charge, to any person "
2513 "obtaining a copy of this software and associated documentation "
2514 "files (the ""Software""), to deal in the Software without restriction, "
2515 "including without limitation the rights to use, copy, modify, merge, "
2516 "publish, distribute, sublicense, and/or sell copies of the Software, "
2517 "and to permit persons to whom the Software is furnished to do so, "
2518 "subject to the following conditions:\n\n"
2520 "The above copyright notice and this permission notice shall be "
2521 "included in all copies or substantial portions of the Software.\n\n"
2523 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
2524 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
2525 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2526 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
2527 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
2528 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
2529 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
2530 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
2531 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
2532 "OTHER DEALINGS IN THE SOFTWARE.";
2534 title
= dupcat(appname
, " Licence", NULL
);
2535 assert(aboutbox
!= NULL
);
2536 messagebox(aboutbox
, title
, licence
,
2537 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
2538 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
2539 "OK", 'o', 1, 1, NULL
);
2543 void about_box(void *window
)
2549 gtk_widget_grab_focus(aboutbox
);
2553 aboutbox
= gtk_dialog_new();
2554 gtk_container_set_border_width(GTK_CONTAINER(aboutbox
), 10);
2555 title
= dupcat("About ", appname
, NULL
);
2556 gtk_window_set_title(GTK_WINDOW(aboutbox
), title
);
2559 w
= gtk_button_new_with_label("Close");
2560 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2561 gtk_window_set_default(GTK_WINDOW(aboutbox
), w
);
2562 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2563 w
, FALSE
, FALSE
, 0);
2564 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2565 GTK_SIGNAL_FUNC(about_close_clicked
), NULL
);
2568 w
= gtk_button_new_with_label("View Licence");
2569 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
2570 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
2571 w
, FALSE
, FALSE
, 0);
2572 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
2573 GTK_SIGNAL_FUNC(licence_clicked
), NULL
);
2576 w
= gtk_label_new(appname
);
2577 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2578 w
, FALSE
, FALSE
, 0);
2581 w
= gtk_label_new(ver
);
2582 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2583 w
, FALSE
, FALSE
, 5);
2586 w
= gtk_label_new("Copyright 1997-2006 Simon Tatham. All rights reserved");
2587 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
2588 w
, FALSE
, FALSE
, 5);
2591 set_transient_window_pos(GTK_WIDGET(window
), aboutbox
);
2592 gtk_widget_show(aboutbox
);
2595 struct eventlog_stuff
{
2596 GtkWidget
*parentwin
, *window
;
2597 struct controlbox
*eventbox
;
2598 struct Shortcuts scs
;
2600 union control
*listctrl
;
2602 int nevents
, negsize
;
2605 int ignore_selchange
;
2608 static void eventlog_destroy(GtkWidget
*widget
, gpointer data
)
2610 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2614 dlg_cleanup(&es
->dp
);
2615 ctrl_free_box(es
->eventbox
);
2617 static void eventlog_ok_handler(union control
*ctrl
, void *dlg
,
2618 void *data
, int event
)
2620 if (event
== EVENT_ACTION
)
2623 static void eventlog_list_handler(union control
*ctrl
, void *dlg
,
2624 void *data
, int event
)
2626 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2628 if (event
== EVENT_REFRESH
) {
2631 dlg_update_start(ctrl
, dlg
);
2632 dlg_listbox_clear(ctrl
, dlg
);
2633 for (i
= 0; i
< es
->nevents
; i
++) {
2634 dlg_listbox_add(ctrl
, dlg
, es
->events
[i
]);
2636 dlg_update_done(ctrl
, dlg
);
2637 } else if (event
== EVENT_SELCHANGE
) {
2642 * If this SELCHANGE event is happening as a result of
2643 * deliberate deselection because someone else has grabbed
2644 * the selection, the last thing we want to do is pre-empt
2647 if (es
->ignore_selchange
)
2651 * Construct the data to use as the selection.
2656 for (i
= 0; i
< es
->nevents
; i
++) {
2657 if (dlg_listbox_issel(ctrl
, dlg
, i
)) {
2658 int extralen
= strlen(es
->events
[i
]);
2660 if (es
->sellen
+ extralen
+ 2 > selsize
) {
2661 selsize
= es
->sellen
+ extralen
+ 512;
2662 es
->seldata
= sresize(es
->seldata
, selsize
, char);
2665 strcpy(es
->seldata
+ es
->sellen
, es
->events
[i
]);
2666 es
->sellen
+= extralen
;
2667 es
->seldata
[es
->sellen
++] = '\n';
2671 if (gtk_selection_owner_set(es
->window
, GDK_SELECTION_PRIMARY
,
2672 GDK_CURRENT_TIME
)) {
2673 extern GdkAtom compound_text_atom
;
2675 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2676 GDK_SELECTION_TYPE_STRING
, 1);
2677 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
2678 compound_text_atom
, 1);
2684 void eventlog_selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
2685 guint info
, guint time_stamp
, gpointer data
)
2687 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2689 gtk_selection_data_set(seldata
, seldata
->target
, 8,
2690 es
->seldata
, es
->sellen
);
2693 gint
eventlog_selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
2696 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
2700 * Deselect everything in the list box.
2702 uc
= dlg_find_byctrl(&es
->dp
, es
->listctrl
);
2703 es
->ignore_selchange
= 1;
2704 gtk_list_unselect_all(GTK_LIST(uc
->list
));
2705 es
->ignore_selchange
= 0;
2713 void showeventlog(void *estuff
, void *parentwin
)
2715 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2716 GtkWidget
*window
, *w0
, *w1
;
2717 GtkWidget
*parent
= GTK_WIDGET(parentwin
);
2718 struct controlset
*s0
, *s1
;
2720 int listitemheight
, index
;
2724 gtk_widget_grab_focus(es
->window
);
2730 for (index
= 0; index
< lenof(es
->scs
.sc
); index
++) {
2731 es
->scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2734 es
->eventbox
= ctrl_new_box();
2736 s0
= ctrl_getset(es
->eventbox
, "", "", "");
2737 ctrl_columns(s0
, 3, 33, 34, 33);
2738 c
= ctrl_pushbutton(s0
, "Close", 'c', HELPCTX(no_help
),
2739 eventlog_ok_handler
, P(NULL
));
2740 c
->button
.column
= 1;
2741 c
->button
.isdefault
= TRUE
;
2743 s1
= ctrl_getset(es
->eventbox
, "x", "", "");
2744 es
->listctrl
= c
= ctrl_listbox(s1
, NULL
, NO_SHORTCUT
, HELPCTX(no_help
),
2745 eventlog_list_handler
, P(es
));
2746 c
->listbox
.height
= 10;
2747 c
->listbox
.multisel
= 2;
2748 c
->listbox
.ncols
= 3;
2749 c
->listbox
.percentages
= snewn(3, int);
2750 c
->listbox
.percentages
[0] = 25;
2751 c
->listbox
.percentages
[1] = 10;
2752 c
->listbox
.percentages
[2] = 65;
2754 listitemheight
= get_listitemheight();
2756 es
->window
= window
= gtk_dialog_new();
2757 title
= dupcat(appname
, " Event Log", NULL
);
2758 gtk_window_set_title(GTK_WINDOW(window
), title
);
2760 w0
= layout_ctrls(&es
->dp
, &es
->scs
, s0
,
2761 listitemheight
, GTK_WINDOW(window
));
2762 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->action_area
),
2764 gtk_widget_show(w0
);
2765 w1
= layout_ctrls(&es
->dp
, &es
->scs
, s1
,
2766 listitemheight
, GTK_WINDOW(window
));
2767 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
2768 gtk_widget_set_usize(w1
, 20 +
2769 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
2770 " IS QUITE LONG 'COS SSH LOG ENTRIES"
2772 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
2774 gtk_widget_show(w1
);
2777 es
->dp
.shortcuts
= &es
->scs
;
2778 es
->dp
.lastfocus
= NULL
;
2780 es
->dp
.window
= window
;
2782 dlg_refresh(NULL
, &es
->dp
);
2785 set_transient_window_pos(parent
, window
);
2786 gtk_window_set_transient_for(GTK_WINDOW(window
),
2787 GTK_WINDOW(parent
));
2789 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
2790 gtk_widget_show(window
);
2792 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
2793 GTK_SIGNAL_FUNC(eventlog_destroy
), es
);
2794 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
2795 GTK_SIGNAL_FUNC(win_key_press
), &es
->dp
);
2796 gtk_signal_connect(GTK_OBJECT(window
), "selection_get",
2797 GTK_SIGNAL_FUNC(eventlog_selection_get
), es
);
2798 gtk_signal_connect(GTK_OBJECT(window
), "selection_clear_event",
2799 GTK_SIGNAL_FUNC(eventlog_selection_clear
), es
);
2802 void *eventlogstuff_new(void)
2804 struct eventlog_stuff
*es
;
2805 es
= snew(struct eventlog_stuff
);
2806 memset(es
, 0, sizeof(*es
));
2810 void logevent_dlg(void *estuff
, const char *string
)
2812 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
2817 if (es
->nevents
>= es
->negsize
) {
2819 es
->events
= sresize(es
->events
, es
->negsize
, char *);
2823 strftime(timebuf
, sizeof(timebuf
), "%Y-%m-%d %H:%M:%S\t", &tm
);
2825 es
->events
[es
->nevents
] = snewn(strlen(timebuf
) + strlen(string
) + 1, char);
2826 strcpy(es
->events
[es
->nevents
], timebuf
);
2827 strcat(es
->events
[es
->nevents
], string
);
2829 dlg_listbox_add(es
->listctrl
, &es
->dp
, es
->events
[es
->nevents
]);
2834 int askappend(void *frontend
, Filename filename
,
2835 void (*callback
)(void *ctx
, int result
), void *ctx
)
2837 static const char msgtemplate
[] =
2838 "The session log file \"%.*s\" already exists. "
2839 "You can overwrite it with a new session log, "
2840 "append your session log to the end of it, "
2841 "or disable session logging for this session.";
2846 message
= dupprintf(msgtemplate
, FILENAME_MAX
, filename
.path
);
2847 mbtitle
= dupprintf("%s Log to File", appname
);
2849 mbret
= messagebox(get_window(frontend
), mbtitle
, message
,
2850 string_width("LINE OF TEXT SUITABLE FOR THE"
2851 " ASKAPPEND WIDTH"),
2852 "Overwrite", 'o', 1, 2,
2853 "Append", 'a', 0, 1,
2854 "Disable", 'd', -1, 0,