2 * gtkdlg.c - GTK implementation of the PuTTY configuration box.
10 #include <gdk/gdkkeysyms.h>
13 #include <X11/Xutil.h>
19 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
34 struct Shortcut sc
[128];
41 int privdata_needs_free
;
42 GtkWidget
**buttons
; int nbuttons
; /* for radio buttons */
43 GtkWidget
*entry
; /* for editbox, filesel, fontsel */
44 GtkWidget
*button
; /* for filesel, fontsel */
45 #if !GTK_CHECK_VERSION(2,4,0)
46 GtkWidget
*list
; /* for listbox (in GTK1), combobox (<=GTK2.3) */
47 GtkWidget
*menu
; /* for optionmenu (==droplist) */
48 GtkWidget
*optmenu
; /* also for optionmenu */
50 GtkWidget
*combo
; /* for combo box (either editable or not) */
52 #if GTK_CHECK_VERSION(2,0,0)
53 GtkWidget
*treeview
; /* for listbox (GTK2), droplist+combo (>=2.4) */
54 GtkListStore
*listmodel
; /* for all types of list box */
56 GtkWidget
*text
; /* for text */
57 GtkWidget
*label
; /* for dlg_label_change */
58 GtkAdjustment
*adj
; /* for the scrollbar in a list box */
65 tree234
*byctrl
, *bywidget
;
67 struct { unsigned char r
, g
, b
, ok
; } coloursel_result
; /* 0-255 */
68 /* `flags' are set to indicate when a GTK signal handler is being called
69 * due to automatic processing and should not flag a user event. */
71 struct Shortcuts
*shortcuts
;
72 GtkWidget
*window
, *cancelbutton
;
73 union control
*currfocus
, *lastfocus
;
74 #if !GTK_CHECK_VERSION(2,0,0)
75 GtkWidget
*currtreeitem
, **treeitems
;
80 #define FLAG_UPDATING_COMBO_LIST 1
81 #define FLAG_UPDATING_LISTBOX 2
83 enum { /* values for Shortcut.action */
84 SHORTCUT_EMPTY
, /* no shortcut on this key */
85 SHORTCUT_TREE
, /* focus a tree item */
86 SHORTCUT_FOCUS
, /* focus the supplied widget */
87 SHORTCUT_UCTRL
, /* do something sane with uctrl */
88 SHORTCUT_UCTRL_UP
, /* uctrl is a draglist, move Up */
89 SHORTCUT_UCTRL_DOWN
, /* uctrl is a draglist, move Down */
92 #if GTK_CHECK_VERSION(2,0,0)
101 * Forward references.
103 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
105 static void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
106 int chr
, int action
, void *ptr
);
107 static void shortcut_highlight(GtkWidget
*label
, int chr
);
108 #if !GTK_CHECK_VERSION(2,0,0)
109 static gboolean
listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
111 static gboolean
listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
113 static gboolean
listitem_button_press(GtkWidget
*item
, GdkEventButton
*event
,
115 static gboolean
listitem_button_release(GtkWidget
*item
, GdkEventButton
*event
,
118 #if !GTK_CHECK_VERSION(2,4,0)
119 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
);
121 static void coloursel_ok(GtkButton
*button
, gpointer data
);
122 static void coloursel_cancel(GtkButton
*button
, gpointer data
);
123 static void window_destroy(GtkWidget
*widget
, gpointer data
);
124 int get_listitemheight(GtkWidget
*widget
);
126 static int uctrl_cmp_byctrl(void *av
, void *bv
)
128 struct uctrl
*a
= (struct uctrl
*)av
;
129 struct uctrl
*b
= (struct uctrl
*)bv
;
130 if (a
->ctrl
< b
->ctrl
)
132 else if (a
->ctrl
> b
->ctrl
)
137 static int uctrl_cmp_byctrl_find(void *av
, void *bv
)
139 union control
*a
= (union control
*)av
;
140 struct uctrl
*b
= (struct uctrl
*)bv
;
143 else if (a
> b
->ctrl
)
148 static int uctrl_cmp_bywidget(void *av
, void *bv
)
150 struct uctrl
*a
= (struct uctrl
*)av
;
151 struct uctrl
*b
= (struct uctrl
*)bv
;
152 if (a
->toplevel
< b
->toplevel
)
154 else if (a
->toplevel
> b
->toplevel
)
159 static int uctrl_cmp_bywidget_find(void *av
, void *bv
)
161 GtkWidget
*a
= (GtkWidget
*)av
;
162 struct uctrl
*b
= (struct uctrl
*)bv
;
165 else if (a
> b
->toplevel
)
170 static void dlg_init(struct dlgparam
*dp
)
172 dp
->byctrl
= newtree234(uctrl_cmp_byctrl
);
173 dp
->bywidget
= newtree234(uctrl_cmp_bywidget
);
174 dp
->coloursel_result
.ok
= FALSE
;
175 dp
->window
= dp
->cancelbutton
= NULL
;
176 #if !GTK_CHECK_VERSION(2,0,0)
177 dp
->treeitems
= NULL
;
178 dp
->currtreeitem
= NULL
;
181 dp
->currfocus
= NULL
;
184 static void dlg_cleanup(struct dlgparam
*dp
)
188 freetree234(dp
->byctrl
); /* doesn't free the uctrls inside */
190 while ( (uc
= index234(dp
->bywidget
, 0)) != NULL
) {
191 del234(dp
->bywidget
, uc
);
192 if (uc
->privdata_needs_free
)
197 freetree234(dp
->bywidget
);
199 #if !GTK_CHECK_VERSION(2,0,0)
200 sfree(dp
->treeitems
);
204 static void dlg_add_uctrl(struct dlgparam
*dp
, struct uctrl
*uc
)
206 add234(dp
->byctrl
, uc
);
207 add234(dp
->bywidget
, uc
);
210 static struct uctrl
*dlg_find_byctrl(struct dlgparam
*dp
, union control
*ctrl
)
214 return find234(dp
->byctrl
, ctrl
, uctrl_cmp_byctrl_find
);
217 static struct uctrl
*dlg_find_bywidget(struct dlgparam
*dp
, GtkWidget
*w
)
219 struct uctrl
*ret
= NULL
;
223 ret
= find234(dp
->bywidget
, w
, uctrl_cmp_bywidget_find
);
231 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
233 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
234 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
238 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
240 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
241 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
243 uc
->privdata_needs_free
= FALSE
;
246 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
248 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
249 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
251 * This is an internal allocation routine, so it's allowed to
252 * use smalloc directly.
254 uc
->privdata
= smalloc(size
);
255 uc
->privdata_needs_free
= FALSE
;
259 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
261 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
262 if (dp
->currfocus
!= ctrl
)
263 return dp
->currfocus
;
265 return dp
->lastfocus
;
268 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int which
)
270 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
271 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
272 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
273 assert(uc
->buttons
!= NULL
);
274 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->buttons
[which
]), TRUE
);
277 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
279 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
280 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
283 assert(uc
->ctrl
->generic
.type
== CTRL_RADIO
);
284 assert(uc
->buttons
!= NULL
);
285 for (i
= 0; i
< uc
->nbuttons
; i
++)
286 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->buttons
[i
])))
288 return 0; /* got to return something */
291 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
293 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
294 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
295 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
296 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc
->toplevel
), checked
);
299 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
301 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
302 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
303 assert(uc
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
304 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc
->toplevel
));
307 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
309 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
310 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
313 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
315 #if GTK_CHECK_VERSION(2,4,0)
317 entry
= gtk_bin_get_child(GTK_BIN(uc
->combo
));
322 assert(entry
!= NULL
);
325 * GTK 2 implements gtk_entry_set_text by means of two separate
326 * operations: first delete the previous text leaving the empty
327 * string, then insert the new text. This causes two calls to
328 * the "changed" signal.
330 * The first call to "changed", if allowed to proceed normally,
331 * will cause an EVENT_VALCHANGE event on the edit box, causing
332 * a call to dlg_editbox_get() which will read the empty string
333 * out of the GtkEntry - and promptly write it straight into
334 * the Config structure, which is precisely where our `text'
335 * pointer is probably pointing, so the second editing
336 * operation will insert that instead of the string we
337 * originally asked for.
339 * Hence, we must take our own copy of the text before we do
342 tmpstring
= dupstr(text
);
343 gtk_entry_set_text(GTK_ENTRY(entry
), tmpstring
);
347 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
349 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
350 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
351 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
);
353 #if GTK_CHECK_VERSION(2,4,0)
355 #if GTK_CHECK_VERSION(2,6,0)
357 gtk_combo_box_get_active_text(GTK_COMBO_BOX(uc
->combo
)),
362 (GTK_ENTRY(gtk_bin_get_child(GTK_BIN(uc
->combo
)))),
365 buffer
[length
-1] = '\0';
371 strncpy(buffer
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
373 buffer
[length
-1] = '\0';
377 assert(!"We shouldn't get here");
380 #if !GTK_CHECK_VERSION(2,4,0)
381 static void container_remove_and_destroy(GtkWidget
*w
, gpointer data
)
383 GtkContainer
*cont
= GTK_CONTAINER(data
);
384 /* gtk_container_remove will unref the widget for us; we need not. */
385 gtk_container_remove(cont
, w
);
389 /* The `listbox' functions can also apply to combo boxes. */
390 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
392 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
393 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
395 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
396 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
398 #if !GTK_CHECK_VERSION(2,4,0)
400 gtk_container_foreach(GTK_CONTAINER(uc
->menu
),
401 container_remove_and_destroy
,
402 GTK_CONTAINER(uc
->menu
));
406 gtk_list_clear_items(GTK_LIST(uc
->list
), 0, -1);
410 #if GTK_CHECK_VERSION(2,0,0)
412 gtk_list_store_clear(uc
->listmodel
);
416 assert(!"We shouldn't get here");
419 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
421 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
422 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
424 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
425 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
427 #if !GTK_CHECK_VERSION(2,4,0)
430 (GTK_CONTAINER(uc
->menu
),
431 g_list_nth_data(GTK_MENU_SHELL(uc
->menu
)->children
, index
));
435 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
439 #if GTK_CHECK_VERSION(2,0,0)
443 assert(uc
->listmodel
!= NULL
);
444 path
= gtk_tree_path_new_from_indices(index
, -1);
445 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc
->listmodel
), &iter
, path
);
446 gtk_list_store_remove(uc
->listmodel
, &iter
);
447 gtk_tree_path_free(path
);
451 assert(!"We shouldn't get here");
454 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
456 dlg_listbox_addwithid(ctrl
, dlg
, text
, 0);
460 * Each listbox entry may have a numeric id associated with it.
461 * Note that some front ends only permit a string to be stored at
462 * each position, which means that _if_ you put two identical
463 * strings in any listbox then you MUST not assign them different
464 * IDs and expect to get meaningful results back.
466 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
467 char const *text
, int id
)
469 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
470 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
472 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
473 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
476 * This routine is long and complicated in both GTK 1 and 2,
477 * and completely different. Sigh.
479 dp
->flags
|= FLAG_UPDATING_COMBO_LIST
;
481 #if !GTK_CHECK_VERSION(2,4,0)
484 * List item in a drop-down (but non-combo) list. Tabs are
485 * ignored; we just provide a standard menu item with the
488 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
490 gtk_container_add(GTK_CONTAINER(uc
->menu
), menuitem
);
491 gtk_widget_show(menuitem
);
493 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
494 GINT_TO_POINTER(id
));
495 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
496 GTK_SIGNAL_FUNC(menuitem_activate
), dp
);
499 if (uc
->list
&& uc
->entry
) {
501 * List item in a combo-box list, which means the sensible
502 * thing to do is make it a perfectly normal label. Hence
503 * tabs are disregarded.
505 GtkWidget
*listitem
= gtk_list_item_new_with_label(text
);
507 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
508 gtk_widget_show(listitem
);
510 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
511 GINT_TO_POINTER(id
));
515 #if !GTK_CHECK_VERSION(2,0,0)
518 * List item in a non-combo-box list box. We make all of
519 * these Columns containing GtkLabels. This allows us to do
520 * the nasty force_left hack irrespective of whether there
521 * are tabs in the thing.
523 GtkWidget
*listitem
= gtk_list_item_new();
524 GtkWidget
*cols
= columns_new(10);
528 /* Count the tabs in the text, and hence determine # of columns. */
530 for (i
= 0; text
[i
]; i
++)
535 (uc
->ctrl
->listbox
.ncols ? uc
->ctrl
->listbox
.ncols
: 1));
536 percents
= snewn(ncols
, gint
);
537 percents
[ncols
-1] = 100;
538 for (i
= 0; i
< ncols
-1; i
++) {
539 percents
[i
] = uc
->ctrl
->listbox
.percentages
[i
];
540 percents
[ncols
-1] -= percents
[i
];
542 columns_set_cols(COLUMNS(cols
), ncols
, percents
);
545 for (i
= 0; i
< ncols
; i
++) {
546 int len
= strcspn(text
, "\t");
547 char *dup
= dupprintf("%.*s", len
, text
);
552 label
= gtk_label_new(dup
);
555 columns_add(COLUMNS(cols
), label
, i
, 1);
556 columns_force_left_align(COLUMNS(cols
), label
);
557 gtk_widget_show(label
);
559 gtk_container_add(GTK_CONTAINER(listitem
), cols
);
560 gtk_widget_show(cols
);
561 gtk_container_add(GTK_CONTAINER(uc
->list
), listitem
);
562 gtk_widget_show(listitem
);
564 if (ctrl
->listbox
.multisel
) {
565 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
566 GTK_SIGNAL_FUNC(listitem_multi_key
), uc
->adj
);
568 gtk_signal_connect(GTK_OBJECT(listitem
), "key_press_event",
569 GTK_SIGNAL_FUNC(listitem_single_key
), uc
->adj
);
571 gtk_signal_connect(GTK_OBJECT(listitem
), "focus_in_event",
572 GTK_SIGNAL_FUNC(widget_focus
), dp
);
573 gtk_signal_connect(GTK_OBJECT(listitem
), "button_press_event",
574 GTK_SIGNAL_FUNC(listitem_button_press
), dp
);
575 gtk_signal_connect(GTK_OBJECT(listitem
), "button_release_event",
576 GTK_SIGNAL_FUNC(listitem_button_release
), dp
);
577 gtk_object_set_data(GTK_OBJECT(listitem
), "user-data",
578 GINT_TO_POINTER(id
));
586 dp
->flags
|= FLAG_UPDATING_LISTBOX
;/* inhibit drag-list update */
587 gtk_list_store_append(uc
->listmodel
, &iter
);
588 dp
->flags
&= ~FLAG_UPDATING_LISTBOX
;
589 gtk_list_store_set(uc
->listmodel
, &iter
, 0, id
, -1);
592 * Now go through text and divide it into columns at the tabs,
595 cols
= (uc
->ctrl
->generic
.type
== CTRL_LISTBOX ? ctrl
->listbox
.ncols
: 1);
596 cols
= cols ? cols
: 1;
597 for (i
= 0; i
< cols
; i
++) {
598 int collen
= strcspn(text
, "\t");
599 char *tmpstr
= snewn(collen
+1, char);
600 memcpy(tmpstr
, text
, collen
);
601 tmpstr
[collen
] = '\0';
602 gtk_list_store_set(uc
->listmodel
, &iter
, i
+1, tmpstr
, -1);
610 assert(!"We shouldn't get here");
612 dp
->flags
&= ~FLAG_UPDATING_COMBO_LIST
;
615 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
617 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
618 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
620 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
621 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
623 #if !GTK_CHECK_VERSION(2,4,0)
624 if (uc
->menu
|| uc
->list
) {
628 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
630 item
= GTK_OBJECT(g_list_nth_data(children
, index
));
631 g_list_free(children
);
633 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
637 #if GTK_CHECK_VERSION(2,0,0)
643 path
= gtk_tree_path_new_from_indices(index
, -1);
644 gtk_tree_model_get_iter(GTK_TREE_MODEL(uc
->listmodel
), &iter
, path
);
645 gtk_tree_model_get(GTK_TREE_MODEL(uc
->listmodel
), &iter
, 0, &ret
, -1);
646 gtk_tree_path_free(path
);
651 assert(!"We shouldn't get here");
652 return -1; /* placate dataflow analysis */
655 /* dlg_listbox_index returns <0 if no single element is selected. */
656 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
658 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
659 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
661 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
662 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
664 #if !GTK_CHECK_VERSION(2,4,0)
665 if (uc
->menu
|| uc
->list
) {
667 GtkWidget
*item
, *activeitem
;
672 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
674 activeitem
= NULL
; /* unnecessarily placate gcc */
676 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
678 for (i
= 0; children
!=NULL
&& (item
= GTK_WIDGET(children
->data
))!=NULL
;
679 i
++, children
= children
->next
) {
680 if (uc
->menu ? activeitem
== item
:
681 GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
) {
688 g_list_free(children
);
689 return selected
< 0 ?
-1 : selected
;
694 * This API function already does the right thing in the
695 * case of no current selection.
697 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc
->combo
));
700 #if GTK_CHECK_VERSION(2,0,0)
702 GtkTreeSelection
*treesel
;
709 assert(uc
->treeview
!= NULL
);
710 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
));
712 if (gtk_tree_selection_count_selected_rows(treesel
) != 1)
715 sellist
= gtk_tree_selection_get_selected_rows(treesel
, &model
);
717 assert(sellist
&& sellist
->data
);
718 path
= sellist
->data
;
720 if (gtk_tree_path_get_depth(path
) != 1) {
723 indices
= gtk_tree_path_get_indices(path
);
731 g_list_foreach(sellist
, (GFunc
)gtk_tree_path_free
, NULL
);
732 g_list_free(sellist
);
737 assert(!"We shouldn't get here");
738 return -1; /* placate dataflow analysis */
741 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
743 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
744 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
746 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
747 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
749 #if !GTK_CHECK_VERSION(2,4,0)
750 if (uc
->menu
|| uc
->list
) {
752 GtkWidget
*item
, *activeitem
;
754 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
755 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
756 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
758 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
760 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
761 g_list_free(children
);
764 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
765 return item
== activeitem
;
767 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
773 * This API function already does the right thing in the
774 * case of no current selection.
776 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc
->combo
)) == index
;
779 #if GTK_CHECK_VERSION(2,0,0)
781 GtkTreeSelection
*treesel
;
785 assert(uc
->treeview
!= NULL
);
786 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
));
788 path
= gtk_tree_path_new_from_indices(index
, -1);
789 ret
= gtk_tree_selection_path_is_selected(treesel
, path
);
790 gtk_tree_path_free(path
);
795 assert(!"We shouldn't get here");
796 return -1; /* placate dataflow analysis */
799 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
801 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
802 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
804 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
805 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
807 #if !GTK_CHECK_VERSION(2,4,0)
809 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
815 gdouble newtop
, newbot
;
817 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
820 * Scroll the list box if necessary to ensure the newly
821 * selected item is visible.
823 items
= gtk_container_children(GTK_CONTAINER(uc
->list
));
824 nitems
= g_list_length(items
);
826 int modified
= FALSE
;
828 newtop
= uc
->adj
->lower
+
829 (uc
->adj
->upper
- uc
->adj
->lower
) * index
/ nitems
;
830 newbot
= uc
->adj
->lower
+
831 (uc
->adj
->upper
- uc
->adj
->lower
) * (index
+1) / nitems
;
832 if (uc
->adj
->value
> newtop
) {
834 uc
->adj
->value
= newtop
;
835 } else if (uc
->adj
->value
< newbot
- uc
->adj
->page_size
) {
837 uc
->adj
->value
= newbot
- uc
->adj
->page_size
;
840 gtk_adjustment_value_changed(uc
->adj
);
846 gtk_combo_box_set_active(GTK_COMBO_BOX(uc
->combo
), index
);
850 #if GTK_CHECK_VERSION(2,0,0)
852 GtkTreeSelection
*treesel
;
855 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
));
857 path
= gtk_tree_path_new_from_indices(index
, -1);
858 gtk_tree_selection_select_path(treesel
, path
);
859 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc
->treeview
),
860 path
, NULL
, FALSE
, 0.0, 0.0);
861 gtk_tree_path_free(path
);
865 assert(!"We shouldn't get here");
868 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
870 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
871 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
873 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
874 assert(uc
->text
!= NULL
);
876 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
879 void dlg_label_change(union control
*ctrl
, void *dlg
, char const *text
)
881 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
882 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
884 switch (uc
->ctrl
->generic
.type
) {
886 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
887 shortcut_highlight(uc
->toplevel
, ctrl
->button
.shortcut
);
890 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
891 shortcut_highlight(uc
->toplevel
, ctrl
->checkbox
.shortcut
);
894 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
895 shortcut_highlight(uc
->label
, ctrl
->radio
.shortcut
);
898 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
899 shortcut_highlight(uc
->label
, ctrl
->editbox
.shortcut
);
901 case CTRL_FILESELECT
:
902 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
903 shortcut_highlight(uc
->label
, ctrl
->fileselect
.shortcut
);
905 case CTRL_FONTSELECT
:
906 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
907 shortcut_highlight(uc
->label
, ctrl
->fontselect
.shortcut
);
910 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
911 shortcut_highlight(uc
->label
, ctrl
->listbox
.shortcut
);
914 assert(!"This shouldn't happen");
919 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
921 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
922 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
923 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
924 assert(uc
->entry
!= NULL
);
925 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
928 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
930 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
931 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
932 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
933 assert(uc
->entry
!= NULL
);
934 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
936 fn
->path
[lenof(fn
->path
)-1] = '\0';
939 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
941 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
942 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
943 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
944 assert(uc
->entry
!= NULL
);
945 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
948 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
950 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
951 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
952 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
953 assert(uc
->entry
!= NULL
);
954 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
956 fs
->name
[lenof(fs
->name
)-1] = '\0';
960 * Bracketing a large set of updates in these two functions will
961 * cause the front end (if possible) to delay updating the screen
962 * until it's all complete, thus avoiding flicker.
964 void dlg_update_start(union control
*ctrl
, void *dlg
)
967 * Apparently we can't do this at all in GTK. GtkCList supports
968 * freeze and thaw, but not GtkList. Bah.
972 void dlg_update_done(union control
*ctrl
, void *dlg
)
975 * Apparently we can't do this at all in GTK. GtkCList supports
976 * freeze and thaw, but not GtkList. Bah.
980 void dlg_set_focus(union control
*ctrl
, void *dlg
)
982 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
983 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
985 switch (ctrl
->generic
.type
) {
988 /* Check boxes and buttons get the focus _and_ get toggled. */
989 gtk_widget_grab_focus(uc
->toplevel
);
991 case CTRL_FILESELECT
:
992 case CTRL_FONTSELECT
:
995 /* Anything containing an edit box gets that focused. */
996 gtk_widget_grab_focus(uc
->entry
);
998 #if GTK_CHECK_VERSION(2,4,0)
999 else if (uc
->combo
) {
1000 /* Failing that, there'll be a combo box. */
1001 gtk_widget_grab_focus(uc
->combo
);
1007 * Radio buttons: we find the currently selected button and
1012 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
1013 if (gtk_toggle_button_get_active
1014 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
1015 gtk_widget_grab_focus(uc
->buttons
[i
]);
1020 #if !GTK_CHECK_VERSION(2,4,0)
1022 gtk_widget_grab_focus(uc
->optmenu
);
1027 gtk_widget_grab_focus(uc
->combo
);
1031 #if !GTK_CHECK_VERSION(2,0,0)
1034 * For GTK-1 style list boxes, we tell it to focus one
1035 * of its children, which appears to do the Right
1038 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
1043 gtk_widget_grab_focus(uc
->treeview
);
1047 assert(!"We shouldn't get here");
1053 * During event processing, you might well want to give an error
1054 * indication to the user. dlg_beep() is a quick and easy generic
1055 * error; dlg_error() puts up a message-box or equivalent.
1057 void dlg_beep(void *dlg
)
1062 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
1064 gtk_widget_destroy(GTK_WIDGET(data
));
1067 static void set_transient_window_pos(GtkWidget
*parent
, GtkWidget
*child
)
1069 gint x
, y
, w
, h
, dx
, dy
;
1071 gtk_window_set_position(GTK_WINDOW(child
), GTK_WIN_POS_NONE
);
1072 gtk_widget_size_request(GTK_WIDGET(child
), &req
);
1074 gdk_window_get_origin(GTK_WIDGET(parent
)->window
, &x
, &y
);
1075 gdk_window_get_size(GTK_WIDGET(parent
)->window
, &w
, &h
);
1078 * One corner of the transient will be offset inwards, by 1/4
1079 * of the parent window's size, from the corresponding corner
1080 * of the parent window. The corner will be chosen so as to
1081 * place the transient closer to the centre of the screen; this
1082 * should avoid transients going off the edge of the screen on
1085 if (x
+ w
/2 < gdk_screen_width() / 2)
1086 dx
= x
+ w
/4; /* work from left edges */
1088 dx
= x
+ 3*w
/4 - req
.width
; /* work from right edges */
1089 if (y
+ h
/2 < gdk_screen_height() / 2)
1090 dy
= y
+ h
/4; /* work from top edges */
1092 dy
= y
+ 3*h
/4 - req
.height
; /* work from bottom edges */
1093 gtk_widget_set_uposition(GTK_WIDGET(child
), dx
, dy
);
1096 void dlg_error_msg(void *dlg
, char *msg
)
1098 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1099 GtkWidget
*window
, *hbox
, *text
, *ok
;
1101 window
= gtk_dialog_new();
1102 text
= gtk_label_new(msg
);
1103 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
1104 hbox
= gtk_hbox_new(FALSE
, 0);
1105 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
1106 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
1107 hbox
, FALSE
, FALSE
, 20);
1108 gtk_widget_show(text
);
1109 gtk_widget_show(hbox
);
1110 gtk_window_set_title(GTK_WINDOW(window
), "Error");
1111 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
1112 ok
= gtk_button_new_with_label("OK");
1113 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1114 ok
, FALSE
, FALSE
, 0);
1115 gtk_widget_show(ok
);
1116 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
1117 gtk_window_set_default(GTK_WINDOW(window
), ok
);
1118 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
1119 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
1120 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
1121 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1122 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
1123 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
1124 set_transient_window_pos(dp
->window
, window
);
1125 gtk_widget_show(window
);
1130 * This function signals to the front end that the dialog's
1131 * processing is completed, and passes an integer value (typically
1132 * a success status).
1134 void dlg_end(void *dlg
, int value
)
1136 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1138 gtk_widget_destroy(dp
->window
);
1141 void dlg_refresh(union control
*ctrl
, void *dlg
)
1143 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1147 if (ctrl
->generic
.handler
!= NULL
)
1148 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
1152 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
1153 assert(uc
->ctrl
!= NULL
);
1154 if (uc
->ctrl
->generic
.handler
!= NULL
)
1155 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
1156 dp
->data
, EVENT_REFRESH
);
1161 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
1163 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1164 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
1167 GtkWidget
*coloursel
=
1168 gtk_color_selection_dialog_new("Select a colour");
1169 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
1171 dp
->coloursel_result
.ok
= FALSE
;
1173 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
1174 #if GTK_CHECK_VERSION(2,0,0)
1175 gtk_color_selection_set_has_opacity_control(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
1177 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
1179 cvals
[0] = r
/ 255.0;
1180 cvals
[1] = g
/ 255.0;
1181 cvals
[2] = b
/ 255.0;
1182 cvals
[3] = 1.0; /* fully opaque! */
1183 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
1185 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
1186 (gpointer
)coloursel
);
1187 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
1188 (gpointer
)coloursel
);
1189 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
1190 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
1191 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
1192 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
1193 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
1194 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
1195 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1196 (gpointer
)coloursel
);
1197 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
1198 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1199 (gpointer
)coloursel
);
1200 gtk_widget_show(coloursel
);
1203 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1204 int *r
, int *g
, int *b
)
1206 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1207 if (dp
->coloursel_result
.ok
) {
1208 *r
= dp
->coloursel_result
.r
;
1209 *g
= dp
->coloursel_result
.g
;
1210 *b
= dp
->coloursel_result
.b
;
1216 /* ----------------------------------------------------------------------
1217 * Signal handlers while the dialog box is active.
1220 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
1223 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1224 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1225 union control
*focus
;
1232 if (focus
!= dp
->currfocus
) {
1233 dp
->lastfocus
= dp
->currfocus
;
1234 dp
->currfocus
= focus
;
1240 static void button_clicked(GtkButton
*button
, gpointer data
)
1242 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1243 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1244 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1247 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
1249 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1250 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
1251 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1254 static gboolean
editbox_key(GtkWidget
*widget
, GdkEventKey
*event
,
1258 * GtkEntry has a nasty habit of eating the Return key, which
1259 * is unhelpful since it doesn't actually _do_ anything with it
1260 * (it calls gtk_widget_activate, but our edit boxes never need
1261 * activating). So I catch Return before GtkEntry sees it, and
1262 * pass it straight on to the parent widget. Effect: hitting
1263 * Return in an edit box will now activate the default button
1264 * in the dialog just like it will everywhere else.
1266 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
1267 gboolean return_val
;
1268 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
1269 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
1270 event
, &return_val
);
1276 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
1278 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1279 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
1280 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
1281 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1285 static gboolean
editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
1288 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1289 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
1290 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
1294 #if !GTK_CHECK_VERSION(2,0,0)
1297 * GTK 1 list box event handlers.
1300 static gboolean
listitem_key(GtkWidget
*item
, GdkEventKey
*event
,
1301 gpointer data
, int multiple
)
1303 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
1305 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1306 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
1307 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
1308 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
1310 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1311 * in a list box. So, if the list box is single-selection:
1313 * - if the list item in question isn't already selected,
1314 * we simply select it.
1315 * - otherwise, we find the next one (or next
1316 * however-far-away) in whichever direction we're going,
1318 * + in this case, we must also fiddle with the
1319 * scrollbar to ensure the newly selected item is
1322 * If it's multiple-selection, we do all of the above
1323 * except actually selecting anything, so we move the focus
1324 * and fiddle the scrollbar to follow it.
1326 GtkWidget
*list
= item
->parent
;
1328 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
1331 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
1332 gtk_list_select_child(GTK_LIST(list
), item
);
1335 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
1336 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1339 (event
->keyval
==GDK_Page_Down
||
1340 event
->keyval
==GDK_KP_Page_Down
||
1341 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1344 GList
*children
, *chead
;
1346 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
1348 n
= g_list_length(children
);
1352 * Figure out how many list items to a screenful,
1353 * and adjust the step appropriately.
1355 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
1356 step
--; /* go by one less than that */
1360 while (children
!= NULL
) {
1361 if (item
== children
->data
)
1363 children
= children
->next
;
1368 if (direction
< 0 && i
> 0)
1369 children
= children
->prev
, i
--;
1370 else if (direction
> 0 && i
< n
-1)
1371 children
= children
->next
, i
++;
1375 if (children
&& children
->data
) {
1377 gtk_list_select_child(GTK_LIST(list
),
1378 GTK_WIDGET(children
->data
));
1379 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
1380 gtk_adjustment_clamp_page
1382 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
1383 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
1394 static gboolean
listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
1397 return listitem_key(item
, event
, data
, FALSE
);
1400 static gboolean
listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
1403 return listitem_key(item
, event
, data
, TRUE
);
1406 static gboolean
listitem_button_press(GtkWidget
*item
, GdkEventButton
*event
,
1409 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1410 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1411 switch (event
->type
) {
1413 case GDK_BUTTON_PRESS
: uc
->nclicks
= 1; break;
1414 case GDK_2BUTTON_PRESS
: uc
->nclicks
= 2; break;
1415 case GDK_3BUTTON_PRESS
: uc
->nclicks
= 3; break;
1420 static gboolean
listitem_button_release(GtkWidget
*item
, GdkEventButton
*event
,
1423 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1424 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1425 if (uc
->nclicks
>1) {
1426 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1432 static void list_selchange(GtkList
*list
, gpointer data
)
1434 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1435 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1437 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1440 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1442 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1443 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1447 (index
== 0 && direction
< 0) ||
1448 (index
== g_list_length(children
)-1 && direction
> 0)) {
1453 child
= g_list_nth_data(children
, index
);
1454 gtk_widget_ref(child
);
1455 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1456 g_list_free(children
);
1459 children
= g_list_append(children
, child
);
1460 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1461 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1462 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1465 static void draglist_up(GtkButton
*button
, gpointer data
)
1467 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1468 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1469 draglist_move(dp
, uc
, -1);
1472 static void draglist_down(GtkButton
*button
, gpointer data
)
1474 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1475 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1476 draglist_move(dp
, uc
, +1);
1479 #else /* !GTK_CHECK_VERSION(2,0,0) */
1482 * GTK 2 list box event handlers.
1485 static void listbox_doubleclick(GtkTreeView
*treeview
, GtkTreePath
*path
,
1486 GtkTreeViewColumn
*column
, gpointer data
)
1488 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1489 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(treeview
));
1491 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1494 static void listbox_selchange(GtkTreeSelection
*treeselection
,
1497 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1498 GtkTreeView
*tree
= gtk_tree_selection_get_tree_view(treeselection
);
1499 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tree
));
1501 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1504 struct draglist_valchange_ctx
{
1506 struct dlgparam
*dp
;
1509 static gboolean
draglist_valchange(gpointer data
)
1511 struct draglist_valchange_ctx
*ctx
=
1512 (struct draglist_valchange_ctx
*)data
;
1514 ctx
->uc
->ctrl
->generic
.handler(ctx
->uc
->ctrl
, ctx
->dp
,
1515 ctx
->dp
->data
, EVENT_VALCHANGE
);
1522 static void listbox_reorder(GtkTreeModel
*treemodel
, GtkTreePath
*path
,
1523 GtkTreeIter
*iter
, gpointer data
)
1525 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1529 if (dp
->flags
& FLAG_UPDATING_LISTBOX
)
1530 return; /* not a user drag operation */
1532 tree
= g_object_get_data(G_OBJECT(treemodel
), "user-data");
1533 uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tree
));
1536 * We should cause EVENT_VALCHANGE on the list box, now
1537 * that its rows have been reordered. However, the GTK 2
1538 * docs say that at the point this signal is received the
1539 * new row might not have actually been filled in yet.
1541 * (So what smegging use is it then, eh? Don't suppose it
1542 * occurred to you at any point that letting the
1543 * application know _after_ the reordering was compelete
1544 * might be helpful to someone?)
1546 * To get round this, I schedule an idle function, which I
1547 * hope won't be called until the main event loop is
1548 * re-entered after the drag-and-drop handler has finished
1549 * furtling with the list store.
1551 struct draglist_valchange_ctx
*ctx
=
1552 snew(struct draglist_valchange_ctx
);
1555 g_idle_add(draglist_valchange
, ctx
);
1559 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1561 #if !GTK_CHECK_VERSION(2,4,0)
1563 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1565 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1566 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1567 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1568 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1569 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1574 static void droplist_selchange(GtkComboBox
*combo
, gpointer data
)
1576 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1577 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(combo
));
1579 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1582 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1584 static void filesel_ok(GtkButton
*button
, gpointer data
)
1586 /* struct dlgparam *dp = (struct dlgparam *)data; */
1587 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1588 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1589 const char *name
= gtk_file_selection_get_filename
1590 (GTK_FILE_SELECTION(filesel
));
1591 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1594 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1596 /* struct dlgparam *dp = (struct dlgparam *)data; */
1598 #if !GTK_CHECK_VERSION(2,0,0)
1600 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1601 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1602 const char *name
= gtk_font_selection_dialog_get_font_name
1603 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1604 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1608 unifontsel
*fontsel
= (unifontsel
*)gtk_object_get_data
1609 (GTK_OBJECT(button
), "user-data");
1610 struct uctrl
*uc
= (struct uctrl
*)fontsel
->user_data
;
1611 char *name
= unifontsel_get_name(fontsel
);
1612 assert(name
); /* should always be ok after OK pressed */
1613 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1619 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1621 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1622 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1623 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1625 gtk_color_selection_get_color
1626 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1628 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1629 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1630 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1631 dp
->coloursel_result
.ok
= TRUE
;
1632 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1635 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1637 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1638 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1639 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1640 dp
->coloursel_result
.ok
= FALSE
;
1641 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1644 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1646 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1647 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1649 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1650 GtkWidget
*filesel
=
1651 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1652 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1654 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1656 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1658 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1659 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1660 gtk_signal_connect_object
1661 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1662 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1663 gtk_signal_connect_object
1664 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1665 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1666 gtk_widget_show(filesel
);
1669 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1670 const gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1672 #if !GTK_CHECK_VERSION(2,0,0)
1675 * Use the GTK 1 standard font selector.
1678 gchar
*spacings
[] = { "c", "m", NULL
};
1679 GtkWidget
*fontsel
=
1680 gtk_font_selection_dialog_new("Select a font");
1681 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1682 gtk_font_selection_dialog_set_filter
1683 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1684 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1685 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1686 if (!gtk_font_selection_dialog_set_font_name
1687 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1689 * If the font name wasn't found as it was, try opening
1690 * it and extracting its FONT property. This should
1691 * have the effect of mapping short aliases into true
1694 GdkFont
*font
= gdk_font_load(fontname
);
1696 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1697 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1698 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1701 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1702 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1704 gtk_font_selection_dialog_set_font_name
1705 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1707 gdk_font_unref(font
);
1711 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1712 "user-data", (gpointer
)fontsel
);
1713 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1715 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1716 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1717 gtk_signal_connect_object
1718 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1719 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1721 gtk_signal_connect_object
1722 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1723 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1725 gtk_widget_show(fontsel
);
1727 #else /* !GTK_CHECK_VERSION(2,0,0) */
1730 * Use the unifontsel code provided in gtkfont.c.
1733 unifontsel
*fontsel
= unifontsel_new("Select a font");
1735 gtk_window_set_modal(fontsel
->window
, TRUE
);
1736 unifontsel_set_name(fontsel
, fontname
);
1738 gtk_object_set_data(GTK_OBJECT(fontsel
->ok_button
),
1739 "user-data", (gpointer
)fontsel
);
1740 fontsel
->user_data
= uc
;
1741 gtk_signal_connect(GTK_OBJECT(fontsel
->ok_button
), "clicked",
1742 GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1743 gtk_signal_connect_object(GTK_OBJECT(fontsel
->ok_button
), "clicked",
1744 GTK_SIGNAL_FUNC(unifontsel_destroy
),
1746 gtk_signal_connect_object(GTK_OBJECT(fontsel
->cancel_button
),"clicked",
1747 GTK_SIGNAL_FUNC(unifontsel_destroy
),
1750 gtk_widget_show(GTK_WIDGET(fontsel
->window
));
1752 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1757 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1760 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1761 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1763 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1764 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1765 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1768 /* ----------------------------------------------------------------------
1769 * This function does the main layout work: it reads a controlset,
1770 * it creates the relevant GTK controls, and returns a GtkWidget
1771 * containing the result. (This widget might be a title of some
1772 * sort, it might be a Columns containing many controls, or it
1773 * might be a GtkFrame containing a Columns; whatever it is, it's
1774 * definitely a GtkWidget and should probably be added to a
1777 * `win' is required for setting the default button. If it is
1778 * non-NULL, all buttons created will be default-capable (so they
1779 * have extra space round them for the default highlight).
1781 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1782 struct controlset
*s
, GtkWindow
*win
)
1788 if (!s
->boxname
&& s
->boxtitle
) {
1789 /* This controlset is a panel title. */
1790 return gtk_label_new(s
->boxtitle
);
1794 * Otherwise, we expect to be laying out actual controls, so
1795 * we'll start by creating a Columns for the purpose.
1797 cols
= COLUMNS(columns_new(4));
1798 ret
= GTK_WIDGET(cols
);
1799 gtk_widget_show(ret
);
1802 * Create a containing frame if we have a box name.
1805 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1806 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1807 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1808 gtk_widget_show(ret
);
1812 * Now iterate through the controls themselves, create them,
1813 * and add them to the Columns.
1815 for (i
= 0; i
< s
->ncontrols
; i
++) {
1816 union control
*ctrl
= s
->ctrls
[i
];
1819 GtkWidget
*w
= NULL
;
1821 switch (ctrl
->generic
.type
) {
1824 static const int simplecols
[1] = { 100 };
1825 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1826 (ctrl
->columns
.percentages ?
1827 ctrl
->columns
.percentages
: simplecols
));
1829 continue; /* no actual control created */
1832 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1834 columns_taborder_last(cols
, uc
->toplevel
);
1836 continue; /* no actual control created */
1839 uc
= snew(struct uctrl
);
1841 uc
->privdata
= NULL
;
1842 uc
->privdata_needs_free
= FALSE
;
1845 #if !GTK_CHECK_VERSION(2,4,0)
1846 uc
->list
= uc
->menu
= uc
->optmenu
= NULL
;
1850 #if GTK_CHECK_VERSION(2,0,0)
1851 uc
->treeview
= NULL
;
1852 uc
->listmodel
= NULL
;
1854 uc
->button
= uc
->text
= NULL
;
1858 switch (ctrl
->generic
.type
) {
1860 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1862 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1863 if (ctrl
->button
.isdefault
)
1864 gtk_window_set_default(win
, w
);
1865 if (ctrl
->button
.iscancel
)
1866 dp
->cancelbutton
= w
;
1868 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1869 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1870 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1871 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1872 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1873 SHORTCUT_UCTRL
, uc
);
1876 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1877 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1878 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1879 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1880 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1881 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1882 SHORTCUT_UCTRL
, uc
);
1887 * Radio buttons get to go inside their own Columns, no
1891 gint i
, *percentages
;
1895 if (ctrl
->generic
.label
) {
1896 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1897 columns_add(COLUMNS(w
), label
, 0, 1);
1898 columns_force_left_align(COLUMNS(w
), label
);
1899 gtk_widget_show(label
);
1900 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1901 SHORTCUT_UCTRL
, uc
);
1904 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1905 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1907 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1908 100 * i
/ ctrl
->radio
.ncolumns
);
1910 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1912 g_free(percentages
);
1915 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1916 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1918 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1922 b
= (gtk_radio_button_new_with_label
1923 (group
, ctrl
->radio
.buttons
[i
]));
1925 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1926 colstart
= i
% ctrl
->radio
.ncolumns
;
1927 columns_add(COLUMNS(w
), b
, colstart
,
1928 (i
== ctrl
->radio
.nbuttons
-1 ?
1929 ctrl
->radio
.ncolumns
- colstart
: 1));
1930 columns_force_left_align(COLUMNS(w
), b
);
1932 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1933 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1934 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1935 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1936 if (ctrl
->radio
.shortcuts
) {
1937 shortcut_add(scs
, GTK_BIN(b
)->child
,
1938 ctrl
->radio
.shortcuts
[i
],
1939 SHORTCUT_UCTRL
, uc
);
1947 GtkWidget
*signalobject
;
1949 if (ctrl
->editbox
.has_list
) {
1950 #if !GTK_CHECK_VERSION(2,4,0)
1954 w
= gtk_combo_new();
1955 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1956 uc
->entry
= GTK_COMBO(w
)->entry
;
1957 uc
->list
= GTK_COMBO(w
)->list
;
1958 signalobject
= uc
->entry
;
1963 uc
->listmodel
= gtk_list_store_new(2, G_TYPE_INT
,
1965 w
= gtk_combo_box_entry_new_with_model
1966 (GTK_TREE_MODEL(uc
->listmodel
), 1);
1967 /* We cannot support password combo boxes. */
1968 assert(!ctrl
->editbox
.password
);
1970 signalobject
= uc
->combo
;
1973 w
= gtk_entry_new();
1974 if (ctrl
->editbox
.password
)
1975 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1980 gtk_signal_connect(GTK_OBJECT(signalobject
), "changed",
1981 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1982 gtk_signal_connect(GTK_OBJECT(signalobject
), "key_press_event",
1983 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1984 gtk_signal_connect(GTK_OBJECT(signalobject
), "focus_in_event",
1985 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1986 gtk_signal_connect(GTK_OBJECT(signalobject
), "focus_out_event",
1987 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1988 gtk_signal_connect(GTK_OBJECT(signalobject
), "focus_out_event",
1989 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1991 * Edit boxes, for some strange reason, have a minimum
1992 * width of 150 in GTK 1.2. We don't want this - we'd
1993 * rather the edit boxes acquired their natural width
1994 * from the column layout of the rest of the box.
1996 * Also, while we're here, we'll squirrel away the
1997 * edit box height so we can use that to centre its
1998 * label vertically beside it.
2000 gtk_widget_size_request(w
, &req
);
2001 gtk_widget_set_usize(w
, 10, req
.height
);
2003 if (ctrl
->generic
.label
) {
2004 GtkWidget
*label
, *container
;
2006 label
= gtk_label_new(ctrl
->generic
.label
);
2008 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
2009 SHORTCUT_FOCUS
, uc
->entry
);
2011 container
= columns_new(4);
2012 if (ctrl
->editbox
.percentwidth
== 100) {
2013 columns_add(COLUMNS(container
), label
, 0, 1);
2014 columns_force_left_align(COLUMNS(container
), label
);
2015 columns_add(COLUMNS(container
), w
, 0, 1);
2017 gint percentages
[2];
2018 percentages
[1] = ctrl
->editbox
.percentwidth
;
2019 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
2020 columns_set_cols(COLUMNS(container
), 2, percentages
);
2021 columns_add(COLUMNS(container
), label
, 0, 1);
2022 columns_force_left_align(COLUMNS(container
), label
);
2023 columns_add(COLUMNS(container
), w
, 1, 1);
2024 /* Centre the label vertically. */
2025 gtk_widget_set_usize(label
, -1, req
.height
);
2026 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
2028 gtk_widget_show(label
);
2036 case CTRL_FILESELECT
:
2037 case CTRL_FONTSELECT
:
2042 (ctrl
->generic
.type
== CTRL_FILESELECT ?
2043 "Browse..." : "Change...");
2045 gint percentages
[] = { 75, 25 };
2047 columns_set_cols(COLUMNS(w
), 2, percentages
);
2049 if (ctrl
->generic
.label
) {
2050 ww
= gtk_label_new(ctrl
->generic
.label
);
2051 columns_add(COLUMNS(w
), ww
, 0, 2);
2052 columns_force_left_align(COLUMNS(w
), ww
);
2053 gtk_widget_show(ww
);
2054 shortcut_add(scs
, ww
,
2055 (ctrl
->generic
.type
== CTRL_FILESELECT ?
2056 ctrl
->fileselect
.shortcut
:
2057 ctrl
->fontselect
.shortcut
),
2058 SHORTCUT_UCTRL
, uc
);
2062 uc
->entry
= ww
= gtk_entry_new();
2063 gtk_widget_size_request(ww
, &req
);
2064 gtk_widget_set_usize(ww
, 10, req
.height
);
2065 columns_add(COLUMNS(w
), ww
, 0, 1);
2066 gtk_widget_show(ww
);
2068 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
2069 columns_add(COLUMNS(w
), ww
, 1, 1);
2070 gtk_widget_show(ww
);
2072 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
2073 GTK_SIGNAL_FUNC(editbox_key
), dp
);
2075 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
2076 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
2077 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
2078 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2079 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
2080 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2081 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
2082 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
2087 #if GTK_CHECK_VERSION(2,0,0)
2089 * First construct the list data store, with the right
2090 * number of columns.
2092 # if !GTK_CHECK_VERSION(2,4,0)
2093 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2094 * because combo boxes are still done the old GTK1 way.) */
2095 if (ctrl
->listbox
.height
> 0)
2102 cols
= ctrl
->listbox
.ncols
;
2103 cols
= cols ? cols
: 1;
2104 types
= snewn(1 + cols
, GType
);
2106 types
[0] = G_TYPE_INT
;
2107 for (i
= 0; i
< cols
; i
++)
2108 types
[i
+1] = G_TYPE_STRING
;
2110 uc
->listmodel
= gtk_list_store_newv(1 + cols
, types
);
2117 * See if it's a drop-down list (non-editable combo
2120 if (ctrl
->listbox
.height
== 0) {
2121 #if !GTK_CHECK_VERSION(2,4,0)
2123 * GTK1 and early-GTK2 option-menu style of
2126 uc
->optmenu
= w
= gtk_option_menu_new();
2127 uc
->menu
= gtk_menu_new();
2128 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
2129 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
2130 (gpointer
)uc
->optmenu
);
2131 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
2132 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2135 * Late-GTK2 style using a GtkComboBox.
2137 GtkCellRenderer
*cr
;
2140 * Create a non-editable GtkComboBox (that is, not
2141 * its subclass GtkComboBoxEntry).
2143 w
= gtk_combo_box_new_with_model
2144 (GTK_TREE_MODEL(uc
->listmodel
));
2148 * Tell it how to render a list item (i.e. which
2149 * column to look at in the list model).
2151 cr
= gtk_cell_renderer_text_new();
2152 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w
), cr
, TRUE
);
2153 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w
), cr
,
2157 * And tell it to notify us when the selection
2160 g_signal_connect(G_OBJECT(w
), "changed",
2161 G_CALLBACK(droplist_selchange
), dp
);
2164 #if !GTK_CHECK_VERSION(2,0,0)
2166 * GTK1-style full list box.
2168 uc
->list
= gtk_list_new();
2169 if (ctrl
->listbox
.multisel
== 2) {
2170 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
2171 GTK_SELECTION_EXTENDED
);
2172 } else if (ctrl
->listbox
.multisel
== 1) {
2173 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
2174 GTK_SELECTION_MULTIPLE
);
2176 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
2177 GTK_SELECTION_SINGLE
);
2179 w
= gtk_scrolled_window_new(NULL
, NULL
);
2180 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
2182 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
2184 GTK_POLICY_AUTOMATIC
);
2185 uc
->adj
= gtk_scrolled_window_get_vadjustment
2186 (GTK_SCROLLED_WINDOW(w
));
2188 gtk_widget_show(uc
->list
);
2189 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
2190 GTK_SIGNAL_FUNC(list_selchange
), dp
);
2191 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
2192 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2195 * Adjust the height of the scrolled window to the
2196 * minimum given by the height parameter.
2198 * This piece of guesswork is a horrid hack based
2199 * on looking inside the GTK 1.2 sources
2200 * (specifically gtkviewport.c, which appears to be
2201 * the widget which provides the border around the
2202 * scrolling area). Anyone lets me know how I can
2203 * do this in a way which isn't at risk from GTK
2204 * upgrades, I'd be grateful.
2208 edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
2209 gtk_widget_set_usize(w
, 10,
2210 2*edge
+ (ctrl
->listbox
.height
*
2211 get_listitemheight(w
)));
2214 if (ctrl
->listbox
.draglist
) {
2216 * GTK doesn't appear to make it easy to
2217 * implement a proper draggable list; so
2218 * instead I'm just going to have to put an Up
2219 * and a Down button to the right of the actual
2220 * list box. Ah well.
2222 GtkWidget
*cols
, *button
;
2223 static const gint percentages
[2] = { 80, 20 };
2225 cols
= columns_new(4);
2226 columns_set_cols(COLUMNS(cols
), 2, percentages
);
2227 columns_add(COLUMNS(cols
), w
, 0, 1);
2229 button
= gtk_button_new_with_label("Up");
2230 columns_add(COLUMNS(cols
), button
, 1, 1);
2231 gtk_widget_show(button
);
2232 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
2233 GTK_SIGNAL_FUNC(draglist_up
), dp
);
2234 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
2235 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2236 button
= gtk_button_new_with_label("Down");
2237 columns_add(COLUMNS(cols
), button
, 1, 1);
2238 gtk_widget_show(button
);
2239 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
2240 GTK_SIGNAL_FUNC(draglist_down
), dp
);
2241 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
2242 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2248 * GTK2 treeview-based full list box.
2250 GtkTreeSelection
*sel
;
2253 * Create the list box itself, its columns, and
2254 * its containing scrolled window.
2256 w
= gtk_tree_view_new_with_model
2257 (GTK_TREE_MODEL(uc
->listmodel
));
2258 g_object_set_data(G_OBJECT(uc
->listmodel
), "user-data",
2260 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2261 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(w
));
2262 gtk_tree_selection_set_mode
2263 (sel
, ctrl
->listbox
.multisel ? GTK_SELECTION_MULTIPLE
:
2264 GTK_SELECTION_SINGLE
);
2266 gtk_signal_connect(GTK_OBJECT(w
), "row-activated",
2267 GTK_SIGNAL_FUNC(listbox_doubleclick
), dp
);
2268 g_signal_connect(G_OBJECT(sel
), "changed",
2269 G_CALLBACK(listbox_selchange
), dp
);
2271 if (ctrl
->listbox
.draglist
) {
2272 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w
), TRUE
);
2273 g_signal_connect(G_OBJECT(uc
->listmodel
), "row-inserted",
2274 G_CALLBACK(listbox_reorder
), dp
);
2281 cols
= ctrl
->listbox
.ncols
;
2282 cols
= cols ? cols
: 1;
2283 for (i
= 0; i
< cols
; i
++) {
2284 GtkTreeViewColumn
*column
;
2286 * It appears that GTK 2 doesn't leave us any
2287 * particularly sensible way to honour the
2288 * "percentages" specification in the ctrl
2291 column
= gtk_tree_view_column_new_with_attributes
2292 ("heading", gtk_cell_renderer_text_new(),
2293 "text", i
+1, (char *)NULL
);
2294 gtk_tree_view_column_set_sizing
2295 (column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
2296 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2303 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2304 gtk_scrolled_window_set_shadow_type
2305 (GTK_SCROLLED_WINDOW(scroll
), GTK_SHADOW_IN
);
2307 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2308 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2309 GTK_POLICY_AUTOMATIC
,
2311 gtk_widget_set_size_request
2313 ctrl
->listbox
.height
* get_listitemheight(w
));
2320 if (ctrl
->generic
.label
) {
2321 GtkWidget
*label
, *container
;
2324 label
= gtk_label_new(ctrl
->generic
.label
);
2326 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
2329 container
= columns_new(4);
2330 if (ctrl
->listbox
.percentwidth
== 100) {
2331 columns_add(COLUMNS(container
), label
, 0, 1);
2332 columns_force_left_align(COLUMNS(container
), label
);
2333 columns_add(COLUMNS(container
), w
, 0, 1);
2335 gint percentages
[2];
2336 percentages
[1] = ctrl
->listbox
.percentwidth
;
2337 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
2338 columns_set_cols(COLUMNS(container
), 2, percentages
);
2339 columns_add(COLUMNS(container
), label
, 0, 1);
2340 columns_force_left_align(COLUMNS(container
), label
);
2341 columns_add(COLUMNS(container
), w
, 1, 1);
2342 /* Centre the label vertically. */
2343 gtk_widget_size_request(w
, &req
);
2344 gtk_widget_set_usize(label
, -1, req
.height
);
2345 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
2347 gtk_widget_show(label
);
2357 * Wrapping text widgets don't sit well with the GTK
2358 * layout model, in which widgets state a minimum size
2359 * and the whole window then adjusts to the smallest
2360 * size it can sensibly take given its contents. A
2361 * wrapping text widget _has_ no clear minimum size;
2362 * instead it has a range of possibilities. It can be
2363 * one line deep but 2000 wide, or two lines deep and
2364 * 1000 pixels, or three by 867, or four by 500 and so
2365 * on. It can be as short as you like provided you
2366 * don't mind it being wide, or as narrow as you like
2367 * provided you don't mind it being tall.
2369 * Therefore, it fits very badly into the layout model.
2370 * Hence the only thing to do is pick a width and let
2371 * it choose its own number of lines. To do this I'm
2372 * going to cheat a little. All new wrapping text
2373 * widgets will be created with a minimal text content
2374 * "X"; then, after the rest of the dialog box is set
2375 * up and its size calculated, the text widgets will be
2376 * told their width and given their real text, which
2377 * will cause the size to be recomputed in the y
2378 * direction (because many of them will expand to more
2381 uc
->text
= w
= gtk_label_new("X");
2382 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
2383 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
2385 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
2386 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
2392 columns_add(cols
, w
,
2393 COLUMN_START(ctrl
->generic
.column
),
2394 COLUMN_SPAN(ctrl
->generic
.column
));
2396 columns_force_left_align(cols
, w
);
2400 dlg_add_uctrl(dp
, uc
);
2407 struct dlgparam
*dp
;
2408 GtkNotebook
*panels
;
2410 #if !GTK_CHECK_VERSION(2,0,0)
2411 GtkWidget
*treeitem
;
2414 GtkTreePath
*treepath
;
2416 struct Shortcuts shortcuts
;
2419 #if GTK_CHECK_VERSION(2,0,0)
2420 static void treeselection_changed(GtkTreeSelection
*treeselection
,
2423 struct selparam
*sps
= (struct selparam
*)data
, *sp
;
2424 GtkTreeModel
*treemodel
;
2425 GtkTreeIter treeiter
;
2429 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2432 gtk_tree_model_get(treemodel
, &treeiter
, TREESTORE_PARAMS
, &spindex
, -1);
2435 page_num
= gtk_notebook_page_num(sp
->panels
, sp
->panel
);
2436 gtk_notebook_set_page(sp
->panels
, page_num
);
2438 dlg_refresh(NULL
, sp
->dp
);
2440 sp
->dp
->shortcuts
= &sp
->shortcuts
;
2443 static void treeitem_sel(GtkItem
*item
, gpointer data
)
2445 struct selparam
*sp
= (struct selparam
*)data
;
2448 page_num
= gtk_notebook_page_num(sp
->panels
, sp
->panel
);
2449 gtk_notebook_set_page(sp
->panels
, page_num
);
2451 dlg_refresh(NULL
, sp
->dp
);
2453 sp
->dp
->shortcuts
= &sp
->shortcuts
;
2454 sp
->dp
->currtreeitem
= sp
->treeitem
;
2458 static void window_destroy(GtkWidget
*widget
, gpointer data
)
2463 #if !GTK_CHECK_VERSION(2,0,0)
2464 static int tree_grab_focus(struct dlgparam
*dp
)
2469 * See if any of the treeitems has the focus.
2472 for (i
= 0; i
< dp
->ntreeitems
; i
++)
2473 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
2481 gtk_widget_grab_focus(dp
->currtreeitem
);
2486 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
2489 struct dlgparam
*dp
= (struct dlgparam
*)data
;
2491 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
2493 * If there's a focused treeitem, we return FALSE to cause the
2494 * focus to move on to some totally other control. If not, we
2495 * focus the selected one.
2497 return tree_grab_focus(dp
);
2501 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
2503 struct dlgparam
*dp
= (struct dlgparam
*)data
;
2505 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
2506 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
2510 if ((event
->state
& GDK_MOD1_MASK
) &&
2511 (unsigned char)event
->string
[0] > 0 &&
2512 (unsigned char)event
->string
[0] <= 127) {
2513 int schr
= (unsigned char)event
->string
[0];
2514 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
2516 switch (sc
->action
) {
2518 #if GTK_CHECK_VERSION(2,0,0)
2519 gtk_widget_grab_focus(sc
->widget
);
2521 tree_grab_focus(dp
);
2524 case SHORTCUT_FOCUS
:
2525 gtk_widget_grab_focus(sc
->widget
);
2527 case SHORTCUT_UCTRL
:
2529 * We must do something sensible with a uctrl.
2530 * Precisely what this is depends on the type of
2533 switch (sc
->uc
->ctrl
->generic
.type
) {
2536 /* Check boxes and buttons get the focus _and_ get toggled. */
2537 gtk_widget_grab_focus(sc
->uc
->toplevel
);
2538 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
2541 case CTRL_FILESELECT
:
2542 case CTRL_FONTSELECT
:
2543 /* File/font selectors have their buttons pressed (ooer),
2544 * and focus transferred to the edit box. */
2545 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
2547 gtk_widget_grab_focus(sc
->uc
->entry
);
2551 * Radio buttons are fun, because they have
2552 * multiple shortcuts. We must find whether the
2553 * activated shortcut is the shortcut for the whole
2554 * group, or for a particular button. In the former
2555 * case, we find the currently selected button and
2556 * focus it; in the latter, we focus-and-click the
2557 * button whose shortcut was pressed.
2559 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
2561 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
2562 if (gtk_toggle_button_get_active
2563 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
2564 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
2566 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
2568 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
2569 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
2570 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
2571 gtk_signal_emit_by_name
2572 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
2578 #if !GTK_CHECK_VERSION(2,4,0)
2579 if (sc
->uc
->optmenu
) {
2583 gtk_widget_grab_focus(sc
->uc
->optmenu
);
2584 /* Option menus don't work using the "clicked" signal.
2585 * We need to manufacture a button press event :-/ */
2586 bev
.type
= GDK_BUTTON_PRESS
;
2588 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
2589 "button_press_event",
2594 if (sc
->uc
->combo
) {
2595 gtk_widget_grab_focus(sc
->uc
->combo
);
2596 gtk_combo_box_popup(GTK_COMBO_BOX(sc
->uc
->combo
));
2600 #if !GTK_CHECK_VERSION(2,0,0)
2603 * For GTK-1 style list boxes, we tell it to
2604 * focus one of its children, which appears to
2605 * do the Right Thing.
2607 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
2608 GTK_DIR_TAB_FORWARD
);
2612 if (sc
->uc
->treeview
) {
2613 gtk_widget_grab_focus(sc
->uc
->treeview
);
2617 assert(!"We shouldn't get here");
2627 #if !GTK_CHECK_VERSION(2,0,0)
2628 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
2630 struct dlgparam
*dp
= (struct dlgparam
*)data
;
2632 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
2633 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
2635 for (i
= 0; i
< dp
->ntreeitems
; i
++)
2636 if (widget
== dp
->treeitems
[i
])
2638 if (i
< dp
->ntreeitems
) {
2639 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
2646 if (i
< 0 || i
>= dp
->ntreeitems
)
2647 break; /* nothing in that dir to select */
2649 * Determine if this tree item is visible.
2652 GtkWidget
*w
= dp
->treeitems
[i
];
2654 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
2655 if (!GTK_WIDGET_VISIBLE(w
)) {
2662 j
= i
; /* got one */
2668 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
2671 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
2672 gtk_widget_grab_focus(dp
->treeitems
[j
]);
2678 * It's nice for Left and Right to expand and collapse tree
2681 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
2682 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
2684 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
2687 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
2688 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
2690 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
2698 static void shortcut_highlight(GtkWidget
*labelw
, int chr
)
2700 GtkLabel
*label
= GTK_LABEL(labelw
);
2701 gchar
*currstr
, *pattern
;
2704 gtk_label_get(label
, &currstr
);
2705 for (i
= 0; currstr
[i
]; i
++)
2706 if (tolower((unsigned char)currstr
[i
]) == chr
) {
2709 pattern
= dupprintf("%*s_", i
, "");
2711 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
2712 gtk_label_set_pattern(label
, pattern
);
2713 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
2720 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
2721 int chr
, int action
, void *ptr
)
2723 if (chr
== NO_SHORTCUT
)
2726 chr
= tolower((unsigned char)chr
);
2728 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
2730 scs
->sc
[chr
].action
= action
;
2732 if (action
== SHORTCUT_FOCUS
) {
2733 scs
->sc
[chr
].uc
= NULL
;
2734 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
2736 scs
->sc
[chr
].widget
= NULL
;
2737 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
2740 shortcut_highlight(labelw
, chr
);
2743 int get_listitemheight(GtkWidget
*w
)
2745 #if !GTK_CHECK_VERSION(2,0,0)
2746 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
2748 gtk_widget_size_request(listitem
, &req
);
2749 gtk_object_sink(GTK_OBJECT(listitem
));
2753 GtkCellRenderer
*cr
= gtk_cell_renderer_text_new();
2754 gtk_cell_renderer_get_size(cr
, w
, NULL
, NULL
, NULL
, NULL
, &height
);
2755 g_object_ref(G_OBJECT(cr
));
2756 gtk_object_sink(GTK_OBJECT(cr
));
2757 g_object_unref(G_OBJECT(cr
));
2762 void set_dialog_action_area(GtkDialog
*dlg
, GtkWidget
*w
)
2764 #if !GTK_CHECK_VERSION(2,0,0)
2767 * In GTK 1, laying out the buttons at the bottom of the
2768 * configuration box is nice and easy, because a GtkDialog's
2769 * action_area is a GtkHBox which stretches to cover the full
2770 * width of the dialog. So we just put our Columns widget
2771 * straight into that hbox, and it ends up just where we want
2774 gtk_box_pack_start(GTK_BOX(dlg
->action_area
), w
, TRUE
, TRUE
, 0);
2778 * In GTK 2, the action area is now a GtkHButtonBox and its
2779 * layout behaviour seems to be different: it doesn't stretch
2780 * to cover the full width of the window, but instead finds its
2781 * own preferred width and right-aligns that within the window.
2782 * This isn't what we want, because we have both left-aligned
2783 * and right-aligned buttons coming out of the above call to
2784 * layout_ctrls(), and right-aligning the whole thing will
2785 * result in the former being centred and looking weird.
2787 * So instead we abandon the dialog's action area completely:
2788 * we gtk_widget_hide() it in the below code, and we also call
2789 * gtk_dialog_set_has_separator() to remove the separator above
2790 * it. We then insert our own action area into the end of the
2791 * dialog's main vbox, and add our own separator above that.
2793 * (Ideally, if we were a native GTK app, we would use the
2794 * GtkHButtonBox's _own_ innate ability to support one set of
2795 * buttons being right-aligned and one left-aligned. But to do
2796 * that here, we would have to either (a) pick apart our cross-
2797 * platform layout structures and treat them specially for this
2798 * particular set of controls, which would be painful, or else
2799 * (b) develop a special and simpler cross-platform
2800 * representation for these particular controls, and introduce
2801 * special-case code into all the _other_ platforms to handle
2802 * it. Neither appeals. Therefore, I regretfully discard the
2803 * GTKHButtonBox and go it alone.)
2807 align
= gtk_alignment_new(0, 0, 1, 1);
2808 gtk_container_add(GTK_CONTAINER(align
), w
);
2810 * The purpose of this GtkAlignment is to provide padding
2811 * around the buttons. The padding we use is twice the padding
2812 * used in our GtkColumns, because we nest two GtkColumns most
2813 * of the time (one separating the tree view from the main
2814 * controls, and another for the main controls themselves).
2816 #if GTK_CHECK_VERSION(2,4,0)
2817 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 8, 8, 8, 8);
2819 gtk_widget_show(align
);
2820 gtk_box_pack_end(GTK_BOX(dlg
->vbox
), align
, FALSE
, TRUE
, 0);
2821 w
= gtk_hseparator_new();
2822 gtk_box_pack_end(GTK_BOX(dlg
->vbox
), w
, FALSE
, TRUE
, 0);
2824 gtk_widget_hide(dlg
->action_area
);
2825 gtk_dialog_set_has_separator(dlg
, FALSE
);
2829 int do_config_box(const char *title
, Config
*cfg
, int midsession
,
2832 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
2833 *tree
, *treescroll
, *panels
, *panelvbox
;
2835 struct controlbox
*ctrlbox
;
2837 #if GTK_CHECK_VERSION(2,0,0)
2838 GtkTreeStore
*treestore
;
2839 GtkCellRenderer
*treerenderer
;
2840 GtkTreeViewColumn
*treecolumn
;
2841 GtkTreeSelection
*treeselection
;
2842 GtkTreeIter treeiterlevels
[8];
2844 GtkTreeItem
*treeitemlevels
[8];
2845 GtkTree
*treelevels
[8];
2848 struct Shortcuts scs
;
2850 struct selparam
*selparams
= NULL
;
2851 int nselparams
= 0, selparamsize
= 0;
2855 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2856 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2859 window
= gtk_dialog_new();
2861 ctrlbox
= ctrl_new_box();
2862 setup_config_box(ctrlbox
, midsession
, cfg
->protocol
, protcfginfo
);
2863 unix_setup_config_box(ctrlbox
, midsession
, cfg
->protocol
);
2864 gtk_setup_config_box(ctrlbox
, midsession
, window
);
2866 gtk_window_set_title(GTK_WINDOW(window
), title
);
2867 hbox
= gtk_hbox_new(FALSE
, 4);
2868 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
2869 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
2870 gtk_widget_show(hbox
);
2871 vbox
= gtk_vbox_new(FALSE
, 4);
2872 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
2873 gtk_widget_show(vbox
);
2874 cols
= columns_new(4);
2875 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
2876 gtk_widget_show(cols
);
2877 label
= gtk_label_new("Category:");
2878 columns_add(COLUMNS(cols
), label
, 0, 1);
2879 columns_force_left_align(COLUMNS(cols
), label
);
2880 gtk_widget_show(label
);
2881 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
2882 #if GTK_CHECK_VERSION(2,0,0)
2883 treestore
= gtk_tree_store_new
2884 (TREESTORE_NUM
, G_TYPE_STRING
, G_TYPE_INT
);
2885 tree
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore
));
2886 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
2887 treerenderer
= gtk_cell_renderer_text_new();
2888 treecolumn
= gtk_tree_view_column_new_with_attributes
2889 ("Label", treerenderer
, "text", 0, NULL
);
2890 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), treecolumn
);
2891 treeselection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
2892 gtk_tree_selection_set_mode(treeselection
, GTK_SELECTION_BROWSE
);
2893 gtk_container_add(GTK_CONTAINER(treescroll
), tree
);
2895 tree
= gtk_tree_new();
2896 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
2897 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
2898 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
2899 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
2901 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
2902 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2903 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
2904 gtk_widget_show(treescroll
);
2905 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
2906 panels
= gtk_notebook_new();
2907 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels
), FALSE
);
2908 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels
), FALSE
);
2909 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
2910 gtk_widget_show(panels
);
2915 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2916 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2919 if (!*s
->pathname
) {
2920 w
= layout_ctrls(&dp
, &scs
, s
, GTK_WINDOW(window
));
2922 set_dialog_action_area(GTK_DIALOG(window
), w
);
2924 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
2925 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
2927 #if GTK_CHECK_VERSION(2,0,0)
2928 GtkTreeIter treeiter
;
2930 GtkWidget
*treeitem
;
2935 * We expect never to find an implicit path
2936 * component. For example, we expect never to see
2937 * A/B/C followed by A/D/E, because that would
2938 * _implicitly_ create A/D. All our path prefixes
2939 * are expected to contain actual controls and be
2940 * selectable in the treeview; so we would expect
2941 * to see A/D _explicitly_ before encountering
2944 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2946 c
= strrchr(s
->pathname
, '/');
2954 first
= (panelvbox
== NULL
);
2956 panelvbox
= gtk_vbox_new(FALSE
, 4);
2957 gtk_widget_show(panelvbox
);
2958 gtk_notebook_append_page(GTK_NOTEBOOK(panels
), panelvbox
,
2963 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(panels
),
2965 gtk_notebook_set_page(GTK_NOTEBOOK(panels
), page_num
);
2968 if (nselparams
>= selparamsize
) {
2970 selparams
= sresize(selparams
, selparamsize
,
2973 selparams
[nselparams
].dp
= &dp
;
2974 selparams
[nselparams
].panels
= GTK_NOTEBOOK(panels
);
2975 selparams
[nselparams
].panel
= panelvbox
;
2976 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2978 assert(j
-1 < level
);
2980 #if GTK_CHECK_VERSION(2,0,0)
2982 /* treeiterlevels[j-1] will always be valid because we
2983 * don't allow implicit path components; see above.
2985 gtk_tree_store_append(treestore
, &treeiter
,
2986 &treeiterlevels
[j
-1]);
2988 gtk_tree_store_append(treestore
, &treeiter
, NULL
);
2989 gtk_tree_store_set(treestore
, &treeiter
,
2991 TREESTORE_PARAMS
, nselparams
,
2993 treeiterlevels
[j
] = treeiter
;
2995 selparams
[nselparams
].depth
= j
;
2997 selparams
[nselparams
].treepath
=
2998 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore
),
2999 &treeiterlevels
[j
-1]);
3001 * We are going to collapse all tree branches
3002 * at depth greater than 2, but not _yet_; see
3003 * the comment at the call to
3004 * gtk_tree_view_collapse_row below.
3006 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree
),
3007 selparams
[nselparams
].treepath
,
3010 selparams
[nselparams
].treepath
= NULL
;
3013 treeitem
= gtk_tree_item_new_with_label(c
);
3015 if (!treelevels
[j
-1]) {
3016 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
3017 gtk_tree_item_set_subtree
3018 (treeitemlevels
[j
-1],
3019 GTK_WIDGET(treelevels
[j
-1]));
3021 gtk_tree_item_expand(treeitemlevels
[j
-1]);
3023 gtk_tree_item_collapse(treeitemlevels
[j
-1]);
3025 gtk_tree_append(treelevels
[j
-1], treeitem
);
3027 gtk_tree_append(GTK_TREE(tree
), treeitem
);
3029 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
3030 treelevels
[j
] = NULL
;
3032 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
3033 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
3034 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
3035 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
3037 gtk_widget_show(treeitem
);
3040 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
3041 selparams
[nselparams
].treeitem
= treeitem
;
3048 w
= layout_ctrls(&dp
, &selparams
[nselparams
-1].shortcuts
, s
, NULL
);
3049 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
3054 #if GTK_CHECK_VERSION(2,0,0)
3060 * We want our tree view to come up with all branches at
3061 * depth 2 or more collapsed. However, if we start off
3062 * with those branches collapsed, then the tree view's
3063 * size request will be calculated based on the width of
3064 * the collapsed tree. So instead we start with them all
3065 * expanded; then we ask for the current size request,
3066 * collapse the relevant rows, and force the width to the
3067 * value we just computed. This arranges that the tree
3068 * view is wide enough to have all branches expanded
3072 gtk_widget_size_request(tree
, &req
);
3074 for (i
= 0; i
< nselparams
; i
++)
3075 if (selparams
[i
].depth
>= 2)
3076 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree
),
3077 selparams
[i
].treepath
);
3079 gtk_widget_set_size_request(tree
, req
.width
, -1);
3083 #if GTK_CHECK_VERSION(2,0,0)
3084 g_signal_connect(G_OBJECT(treeselection
), "changed",
3085 G_CALLBACK(treeselection_changed
), selparams
);
3087 dp
.ntreeitems
= nselparams
;
3088 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
3090 for (index
= 0; index
< nselparams
; index
++) {
3091 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
3092 GTK_SIGNAL_FUNC(treeitem_sel
),
3094 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
3099 dlg_refresh(NULL
, &dp
);
3101 dp
.shortcuts
= &selparams
[0].shortcuts
;
3102 #if !GTK_CHECK_VERSION(2,0,0)
3103 dp
.currtreeitem
= dp
.treeitems
[0];
3105 dp
.lastfocus
= NULL
;
3111 extern void set_window_icon(GtkWidget
*window
,
3112 const char *const *const *icon
,
3114 extern const char *const *const cfg_icon
[];
3115 extern const int n_cfg_icon
;
3116 set_window_icon(window
, cfg_icon
, n_cfg_icon
);
3119 #if !GTK_CHECK_VERSION(2,0,0)
3120 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
3123 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
3125 GTK_POLICY_AUTOMATIC
);
3126 gtk_widget_show(tree
);
3128 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
3129 gtk_widget_show(window
);
3132 * Set focus into the first available control.
3134 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
3135 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
3140 for (j
= 0; j
< s
->ncontrols
; j
++)
3141 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
3142 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
3143 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
3144 dlg_set_focus(s
->ctrls
[j
], &dp
);
3145 dp
.lastfocus
= s
->ctrls
[j
];
3154 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
3155 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
3156 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
3157 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
3167 static void messagebox_handler(union control
*ctrl
, void *dlg
,
3168 void *data
, int event
)
3170 if (event
== EVENT_ACTION
)
3171 dlg_end(dlg
, ctrl
->generic
.context
.i
);
3173 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
3175 GtkWidget
*window
, *w0
, *w1
;
3176 struct controlbox
*ctrlbox
;
3177 struct controlset
*s0
, *s1
;
3180 struct Shortcuts scs
;
3186 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
3187 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
3190 ctrlbox
= ctrl_new_box();
3193 va_start(ap
, minwid
);
3194 while (va_arg(ap
, char *) != NULL
) {
3196 (void) va_arg(ap
, int); /* shortcut */
3197 (void) va_arg(ap
, int); /* normal/default/cancel */
3198 (void) va_arg(ap
, int); /* end value */
3202 s0
= ctrl_getset(ctrlbox
, "", "", "");
3203 c
= ctrl_columns(s0
, 2, 50, 50);
3204 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
3205 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
3206 for (index
= 0; index
< ncols
; index
++)
3207 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
3208 va_start(ap
, minwid
);
3211 char *title
= va_arg(ap
, char *);
3212 int shortcut
, type
, value
;
3215 shortcut
= va_arg(ap
, int);
3216 type
= va_arg(ap
, int);
3217 value
= va_arg(ap
, int);
3218 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
3219 messagebox_handler
, I(value
));
3220 c
->generic
.column
= index
++;
3222 c
->button
.isdefault
= TRUE
;
3224 c
->button
.iscancel
= TRUE
;
3228 s1
= ctrl_getset(ctrlbox
, "x", "", "");
3229 ctrl_text(s1
, msg
, HELPCTX(no_help
));
3231 window
= gtk_dialog_new();
3232 gtk_window_set_title(GTK_WINDOW(window
), title
);
3233 w0
= layout_ctrls(&dp
, &scs
, s0
, GTK_WINDOW(window
));
3234 set_dialog_action_area(GTK_DIALOG(window
), w0
);
3235 gtk_widget_show(w0
);
3236 w1
= layout_ctrls(&dp
, &scs
, s1
, GTK_WINDOW(window
));
3237 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
3238 gtk_widget_set_usize(w1
, minwid
+20, -1);
3239 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
3241 gtk_widget_show(w1
);
3243 dp
.shortcuts
= &scs
;
3244 dp
.lastfocus
= NULL
;
3248 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
3250 set_transient_window_pos(parentwin
, window
);
3251 gtk_window_set_transient_for(GTK_WINDOW(window
),
3252 GTK_WINDOW(parentwin
));
3254 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
3255 gtk_widget_show(window
);
3257 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
3258 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
3259 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
3260 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
3265 ctrl_free_box(ctrlbox
);
3270 static int string_width(char *text
)
3272 GtkWidget
*label
= gtk_label_new(text
);
3274 gtk_widget_size_request(label
, &req
);
3275 gtk_object_sink(GTK_OBJECT(label
));
3279 int reallyclose(void *frontend
)
3281 char *title
= dupcat(appname
, " Exit Confirmation", NULL
);
3282 int ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
3283 title
, "Are you sure you want to close this session?",
3284 string_width("Most of the width of the above text"),
3292 int verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
3293 char *keystr
, char *fingerprint
,
3294 void (*callback
)(void *ctx
, int result
), void *ctx
)
3296 static const char absenttxt
[] =
3297 "The server's host key is not cached. You have no guarantee "
3298 "that the server is the computer you think it is.\n"
3299 "The server's %s key fingerprint is:\n"
3301 "If you trust this host, press \"Accept\" to add the key to "
3302 "PuTTY's cache and carry on connecting.\n"
3303 "If you want to carry on connecting just once, without "
3304 "adding the key to the cache, press \"Connect Once\".\n"
3305 "If you do not trust this host, press \"Cancel\" to abandon the "
3307 static const char wrongtxt
[] =
3308 "WARNING - POTENTIAL SECURITY BREACH!\n"
3309 "The server's host key does not match the one PuTTY has "
3310 "cached. This means that either the server administrator "
3311 "has changed the host key, or you have actually connected "
3312 "to another computer pretending to be the server.\n"
3313 "The new %s key fingerprint is:\n"
3315 "If you were expecting this change and trust the new key, "
3316 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3317 "If you want to carry on connecting but without updating "
3318 "the cache, press \"Connect Once\".\n"
3319 "If you want to abandon the connection completely, press "
3320 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3328 ret
= verify_host_key(host
, port
, keytype
, keystr
);
3330 if (ret
== 0) /* success - key matched OK */
3333 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), keytype
, fingerprint
);
3335 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
3336 "PuTTY Security Alert", text
,
3337 string_width(fingerprint
),
3338 "Accept", 'a', 0, 2,
3339 "Connect Once", 'o', 0, 1,
3340 "Cancel", 'c', -1, 0,
3346 store_host_key(host
, port
, keytype
, keystr
);
3347 return 1; /* continue with connection */
3348 } else if (ret
== 1)
3349 return 1; /* continue with connection */
3350 return 0; /* do not continue with connection */
3354 * Ask whether the selected algorithm is acceptable (since it was
3355 * below the configured 'warn' threshold).
3357 int askalg(void *frontend
, const char *algtype
, const char *algname
,
3358 void (*callback
)(void *ctx
, int result
), void *ctx
)
3360 static const char msg
[] =
3361 "The first %s supported by the server is "
3362 "%s, which is below the configured warning threshold.\n"
3363 "Continue with connection?";
3367 text
= dupprintf(msg
, algtype
, algname
);
3368 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
3369 "PuTTY Security Alert", text
,
3370 string_width("Continue with connection?"),
3383 void old_keyfile_warning(void)
3386 * This should never happen on Unix. We hope.
3390 void fatal_message_box(void *window
, char *msg
)
3392 messagebox(window
, "PuTTY Fatal Error", msg
,
3393 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3394 "OK", 'o', 1, 1, NULL
);
3397 void fatalbox(char *p
, ...)
3402 msg
= dupvprintf(p
, ap
);
3404 fatal_message_box(NULL
, msg
);
3409 static GtkWidget
*aboutbox
= NULL
;
3411 static void about_close_clicked(GtkButton
*button
, gpointer data
)
3413 gtk_widget_destroy(aboutbox
);
3417 static void licence_clicked(GtkButton
*button
, gpointer data
)
3422 "Copyright 1997-2008 Simon Tatham.\n\n"
3424 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3425 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3426 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3427 "Markus Kuhn, Colin Watson, and CORE SDI S.A.\n\n"
3429 "Permission is hereby granted, free of charge, to any person "
3430 "obtaining a copy of this software and associated documentation "
3431 "files (the ""Software""), to deal in the Software without restriction, "
3432 "including without limitation the rights to use, copy, modify, merge, "
3433 "publish, distribute, sublicense, and/or sell copies of the Software, "
3434 "and to permit persons to whom the Software is furnished to do so, "
3435 "subject to the following conditions:\n\n"
3437 "The above copyright notice and this permission notice shall be "
3438 "included in all copies or substantial portions of the Software.\n\n"
3440 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3441 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3442 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3443 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3444 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3445 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3446 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3447 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3448 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3449 "OTHER DEALINGS IN THE SOFTWARE.";
3451 title
= dupcat(appname
, " Licence", NULL
);
3452 assert(aboutbox
!= NULL
);
3453 messagebox(aboutbox
, title
, licence
,
3454 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3455 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3456 "OK", 'o', 1, 1, NULL
);
3460 void about_box(void *window
)
3466 gtk_widget_grab_focus(aboutbox
);
3470 aboutbox
= gtk_dialog_new();
3471 gtk_container_set_border_width(GTK_CONTAINER(aboutbox
), 10);
3472 title
= dupcat("About ", appname
, NULL
);
3473 gtk_window_set_title(GTK_WINDOW(aboutbox
), title
);
3476 w
= gtk_button_new_with_label("Close");
3477 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
3478 gtk_window_set_default(GTK_WINDOW(aboutbox
), w
);
3479 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
3480 w
, FALSE
, FALSE
, 0);
3481 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
3482 GTK_SIGNAL_FUNC(about_close_clicked
), NULL
);
3485 w
= gtk_button_new_with_label("View Licence");
3486 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
3487 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
3488 w
, FALSE
, FALSE
, 0);
3489 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
3490 GTK_SIGNAL_FUNC(licence_clicked
), NULL
);
3493 w
= gtk_label_new(appname
);
3494 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
3495 w
, FALSE
, FALSE
, 0);
3498 w
= gtk_label_new(ver
);
3499 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
3500 w
, FALSE
, FALSE
, 5);
3503 w
= gtk_label_new("Copyright 1997-2008 Simon Tatham. All rights reserved");
3504 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
3505 w
, FALSE
, FALSE
, 5);
3508 set_transient_window_pos(GTK_WIDGET(window
), aboutbox
);
3509 gtk_widget_show(aboutbox
);
3512 struct eventlog_stuff
{
3513 GtkWidget
*parentwin
, *window
;
3514 struct controlbox
*eventbox
;
3515 struct Shortcuts scs
;
3517 union control
*listctrl
;
3519 int nevents
, negsize
;
3522 int ignore_selchange
;
3525 static void eventlog_destroy(GtkWidget
*widget
, gpointer data
)
3527 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3531 dlg_cleanup(&es
->dp
);
3532 ctrl_free_box(es
->eventbox
);
3534 static void eventlog_ok_handler(union control
*ctrl
, void *dlg
,
3535 void *data
, int event
)
3537 if (event
== EVENT_ACTION
)
3540 static void eventlog_list_handler(union control
*ctrl
, void *dlg
,
3541 void *data
, int event
)
3543 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3545 if (event
== EVENT_REFRESH
) {
3548 dlg_update_start(ctrl
, dlg
);
3549 dlg_listbox_clear(ctrl
, dlg
);
3550 for (i
= 0; i
< es
->nevents
; i
++) {
3551 dlg_listbox_add(ctrl
, dlg
, es
->events
[i
]);
3553 dlg_update_done(ctrl
, dlg
);
3554 } else if (event
== EVENT_SELCHANGE
) {
3559 * If this SELCHANGE event is happening as a result of
3560 * deliberate deselection because someone else has grabbed
3561 * the selection, the last thing we want to do is pre-empt
3564 if (es
->ignore_selchange
)
3568 * Construct the data to use as the selection.
3573 for (i
= 0; i
< es
->nevents
; i
++) {
3574 if (dlg_listbox_issel(ctrl
, dlg
, i
)) {
3575 int extralen
= strlen(es
->events
[i
]);
3577 if (es
->sellen
+ extralen
+ 2 > selsize
) {
3578 selsize
= es
->sellen
+ extralen
+ 512;
3579 es
->seldata
= sresize(es
->seldata
, selsize
, char);
3582 strcpy(es
->seldata
+ es
->sellen
, es
->events
[i
]);
3583 es
->sellen
+= extralen
;
3584 es
->seldata
[es
->sellen
++] = '\n';
3588 if (gtk_selection_owner_set(es
->window
, GDK_SELECTION_PRIMARY
,
3589 GDK_CURRENT_TIME
)) {
3590 extern GdkAtom compound_text_atom
;
3592 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
3593 GDK_SELECTION_TYPE_STRING
, 1);
3594 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
3595 compound_text_atom
, 1);
3601 void eventlog_selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
3602 guint info
, guint time_stamp
, gpointer data
)
3604 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3606 gtk_selection_data_set(seldata
, seldata
->target
, 8,
3607 (unsigned char *)es
->seldata
, es
->sellen
);
3610 gint
eventlog_selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
3613 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3617 * Deselect everything in the list box.
3619 uc
= dlg_find_byctrl(&es
->dp
, es
->listctrl
);
3620 es
->ignore_selchange
= 1;
3621 #if !GTK_CHECK_VERSION(2,0,0)
3623 gtk_list_unselect_all(GTK_LIST(uc
->list
));
3625 assert(uc
->treeview
);
3626 gtk_tree_selection_unselect_all
3627 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
)));
3629 es
->ignore_selchange
= 0;
3637 void showeventlog(void *estuff
, void *parentwin
)
3639 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
3640 GtkWidget
*window
, *w0
, *w1
;
3641 GtkWidget
*parent
= GTK_WIDGET(parentwin
);
3642 struct controlset
*s0
, *s1
;
3648 gtk_widget_grab_focus(es
->window
);
3654 for (index
= 0; index
< lenof(es
->scs
.sc
); index
++) {
3655 es
->scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
3658 es
->eventbox
= ctrl_new_box();
3660 s0
= ctrl_getset(es
->eventbox
, "", "", "");
3661 ctrl_columns(s0
, 3, 33, 34, 33);
3662 c
= ctrl_pushbutton(s0
, "Close", 'c', HELPCTX(no_help
),
3663 eventlog_ok_handler
, P(NULL
));
3664 c
->button
.column
= 1;
3665 c
->button
.isdefault
= TRUE
;
3667 s1
= ctrl_getset(es
->eventbox
, "x", "", "");
3668 es
->listctrl
= c
= ctrl_listbox(s1
, NULL
, NO_SHORTCUT
, HELPCTX(no_help
),
3669 eventlog_list_handler
, P(es
));
3670 c
->listbox
.height
= 10;
3671 c
->listbox
.multisel
= 2;
3672 c
->listbox
.ncols
= 3;
3673 c
->listbox
.percentages
= snewn(3, int);
3674 c
->listbox
.percentages
[0] = 25;
3675 c
->listbox
.percentages
[1] = 10;
3676 c
->listbox
.percentages
[2] = 65;
3678 es
->window
= window
= gtk_dialog_new();
3679 title
= dupcat(appname
, " Event Log", NULL
);
3680 gtk_window_set_title(GTK_WINDOW(window
), title
);
3682 w0
= layout_ctrls(&es
->dp
, &es
->scs
, s0
, GTK_WINDOW(window
));
3683 set_dialog_action_area(GTK_DIALOG(window
), w0
);
3684 gtk_widget_show(w0
);
3685 w1
= layout_ctrls(&es
->dp
, &es
->scs
, s1
, GTK_WINDOW(window
));
3686 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
3687 gtk_widget_set_usize(w1
, 20 +
3688 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3689 " IS QUITE LONG 'COS SSH LOG ENTRIES"
3691 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
3693 gtk_widget_show(w1
);
3696 es
->dp
.shortcuts
= &es
->scs
;
3697 es
->dp
.lastfocus
= NULL
;
3699 es
->dp
.window
= window
;
3701 dlg_refresh(NULL
, &es
->dp
);
3704 set_transient_window_pos(parent
, window
);
3705 gtk_window_set_transient_for(GTK_WINDOW(window
),
3706 GTK_WINDOW(parent
));
3708 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
3709 gtk_widget_show(window
);
3711 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
3712 GTK_SIGNAL_FUNC(eventlog_destroy
), es
);
3713 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
3714 GTK_SIGNAL_FUNC(win_key_press
), &es
->dp
);
3715 gtk_signal_connect(GTK_OBJECT(window
), "selection_get",
3716 GTK_SIGNAL_FUNC(eventlog_selection_get
), es
);
3717 gtk_signal_connect(GTK_OBJECT(window
), "selection_clear_event",
3718 GTK_SIGNAL_FUNC(eventlog_selection_clear
), es
);
3721 void *eventlogstuff_new(void)
3723 struct eventlog_stuff
*es
;
3724 es
= snew(struct eventlog_stuff
);
3725 memset(es
, 0, sizeof(*es
));
3729 void logevent_dlg(void *estuff
, const char *string
)
3731 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
3736 if (es
->nevents
>= es
->negsize
) {
3738 es
->events
= sresize(es
->events
, es
->negsize
, char *);
3742 strftime(timebuf
, sizeof(timebuf
), "%Y-%m-%d %H:%M:%S\t", &tm
);
3744 es
->events
[es
->nevents
] = snewn(strlen(timebuf
) + strlen(string
) + 1, char);
3745 strcpy(es
->events
[es
->nevents
], timebuf
);
3746 strcat(es
->events
[es
->nevents
], string
);
3748 dlg_listbox_add(es
->listctrl
, &es
->dp
, es
->events
[es
->nevents
]);
3753 int askappend(void *frontend
, Filename filename
,
3754 void (*callback
)(void *ctx
, int result
), void *ctx
)
3756 static const char msgtemplate
[] =
3757 "The session log file \"%.*s\" already exists. "
3758 "You can overwrite it with a new session log, "
3759 "append your session log to the end of it, "
3760 "or disable session logging for this session.";
3765 message
= dupprintf(msgtemplate
, FILENAME_MAX
, filename
.path
);
3766 mbtitle
= dupprintf("%s Log to File", appname
);
3768 mbret
= messagebox(get_window(frontend
), mbtitle
, message
,
3769 string_width("LINE OF TEXT SUITABLE FOR THE"
3770 " ASKAPPEND WIDTH"),
3771 "Overwrite", 'o', 1, 2,
3772 "Append", 'a', 0, 1,
3773 "Disable", 'd', -1, 0,