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
;
708 assert(uc
->treeview
!= NULL
);
709 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
));
711 if (gtk_tree_selection_count_selected_rows(treesel
) != 1)
714 sellist
= gtk_tree_selection_get_selected_rows(treesel
, NULL
);
716 assert(sellist
&& sellist
->data
);
717 path
= sellist
->data
;
719 if (gtk_tree_path_get_depth(path
) != 1) {
722 indices
= gtk_tree_path_get_indices(path
);
730 g_list_foreach(sellist
, (GFunc
)gtk_tree_path_free
, NULL
);
731 g_list_free(sellist
);
736 assert(!"We shouldn't get here");
737 return -1; /* placate dataflow analysis */
740 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
742 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
743 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
745 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
746 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
748 #if !GTK_CHECK_VERSION(2,4,0)
749 if (uc
->menu
|| uc
->list
) {
751 GtkWidget
*item
, *activeitem
;
753 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
754 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
755 assert(uc
->menu
!= NULL
|| uc
->list
!= NULL
);
757 children
= gtk_container_children(GTK_CONTAINER(uc
->menu ? uc
->menu
:
759 item
= GTK_WIDGET(g_list_nth_data(children
, index
));
760 g_list_free(children
);
763 activeitem
= gtk_menu_get_active(GTK_MENU(uc
->menu
));
764 return item
== activeitem
;
766 return GTK_WIDGET_STATE(item
) == GTK_STATE_SELECTED
;
772 * This API function already does the right thing in the
773 * case of no current selection.
775 return gtk_combo_box_get_active(GTK_COMBO_BOX(uc
->combo
)) == index
;
778 #if GTK_CHECK_VERSION(2,0,0)
780 GtkTreeSelection
*treesel
;
784 assert(uc
->treeview
!= NULL
);
785 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
));
787 path
= gtk_tree_path_new_from_indices(index
, -1);
788 ret
= gtk_tree_selection_path_is_selected(treesel
, path
);
789 gtk_tree_path_free(path
);
794 assert(!"We shouldn't get here");
795 return -1; /* placate dataflow analysis */
798 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
800 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
801 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
803 assert(uc
->ctrl
->generic
.type
== CTRL_EDITBOX
||
804 uc
->ctrl
->generic
.type
== CTRL_LISTBOX
);
806 #if !GTK_CHECK_VERSION(2,4,0)
808 gtk_option_menu_set_history(GTK_OPTION_MENU(uc
->optmenu
), index
);
814 gdouble newtop
, newbot
;
816 gtk_list_select_item(GTK_LIST(uc
->list
), index
);
819 * Scroll the list box if necessary to ensure the newly
820 * selected item is visible.
822 items
= gtk_container_children(GTK_CONTAINER(uc
->list
));
823 nitems
= g_list_length(items
);
825 int modified
= FALSE
;
827 newtop
= uc
->adj
->lower
+
828 (uc
->adj
->upper
- uc
->adj
->lower
) * index
/ nitems
;
829 newbot
= uc
->adj
->lower
+
830 (uc
->adj
->upper
- uc
->adj
->lower
) * (index
+1) / nitems
;
831 if (uc
->adj
->value
> newtop
) {
833 uc
->adj
->value
= newtop
;
834 } else if (uc
->adj
->value
< newbot
- uc
->adj
->page_size
) {
836 uc
->adj
->value
= newbot
- uc
->adj
->page_size
;
839 gtk_adjustment_value_changed(uc
->adj
);
845 gtk_combo_box_set_active(GTK_COMBO_BOX(uc
->combo
), index
);
849 #if GTK_CHECK_VERSION(2,0,0)
851 GtkTreeSelection
*treesel
;
854 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
));
856 path
= gtk_tree_path_new_from_indices(index
, -1);
857 gtk_tree_selection_select_path(treesel
, path
);
858 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(uc
->treeview
),
859 path
, NULL
, FALSE
, 0.0, 0.0);
860 gtk_tree_path_free(path
);
864 assert(!"We shouldn't get here");
867 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
869 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
870 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
872 assert(uc
->ctrl
->generic
.type
== CTRL_TEXT
);
873 assert(uc
->text
!= NULL
);
875 gtk_label_set_text(GTK_LABEL(uc
->text
), text
);
878 void dlg_label_change(union control
*ctrl
, void *dlg
, char const *text
)
880 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
881 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
883 switch (uc
->ctrl
->generic
.type
) {
885 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
886 shortcut_highlight(uc
->toplevel
, ctrl
->button
.shortcut
);
889 gtk_label_set_text(GTK_LABEL(uc
->toplevel
), text
);
890 shortcut_highlight(uc
->toplevel
, ctrl
->checkbox
.shortcut
);
893 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
894 shortcut_highlight(uc
->label
, ctrl
->radio
.shortcut
);
897 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
898 shortcut_highlight(uc
->label
, ctrl
->editbox
.shortcut
);
900 case CTRL_FILESELECT
:
901 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
902 shortcut_highlight(uc
->label
, ctrl
->fileselect
.shortcut
);
904 case CTRL_FONTSELECT
:
905 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
906 shortcut_highlight(uc
->label
, ctrl
->fontselect
.shortcut
);
909 gtk_label_set_text(GTK_LABEL(uc
->label
), text
);
910 shortcut_highlight(uc
->label
, ctrl
->listbox
.shortcut
);
913 assert(!"This shouldn't happen");
918 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
920 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
921 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
922 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
923 assert(uc
->entry
!= NULL
);
924 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fn
.path
);
927 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
929 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
930 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
931 assert(uc
->ctrl
->generic
.type
== CTRL_FILESELECT
);
932 assert(uc
->entry
!= NULL
);
933 strncpy(fn
->path
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
935 fn
->path
[lenof(fn
->path
)-1] = '\0';
938 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
940 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
941 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
942 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
943 assert(uc
->entry
!= NULL
);
944 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), fs
.name
);
947 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
949 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
950 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
951 assert(uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
952 assert(uc
->entry
!= NULL
);
953 strncpy(fs
->name
, gtk_entry_get_text(GTK_ENTRY(uc
->entry
)),
955 fs
->name
[lenof(fs
->name
)-1] = '\0';
959 * Bracketing a large set of updates in these two functions will
960 * cause the front end (if possible) to delay updating the screen
961 * until it's all complete, thus avoiding flicker.
963 void dlg_update_start(union control
*ctrl
, void *dlg
)
966 * Apparently we can't do this at all in GTK. GtkCList supports
967 * freeze and thaw, but not GtkList. Bah.
971 void dlg_update_done(union control
*ctrl
, void *dlg
)
974 * Apparently we can't do this at all in GTK. GtkCList supports
975 * freeze and thaw, but not GtkList. Bah.
979 void dlg_set_focus(union control
*ctrl
, void *dlg
)
981 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
982 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
984 switch (ctrl
->generic
.type
) {
987 /* Check boxes and buttons get the focus _and_ get toggled. */
988 gtk_widget_grab_focus(uc
->toplevel
);
990 case CTRL_FILESELECT
:
991 case CTRL_FONTSELECT
:
994 /* Anything containing an edit box gets that focused. */
995 gtk_widget_grab_focus(uc
->entry
);
997 #if GTK_CHECK_VERSION(2,4,0)
998 else if (uc
->combo
) {
999 /* Failing that, there'll be a combo box. */
1000 gtk_widget_grab_focus(uc
->combo
);
1006 * Radio buttons: we find the currently selected button and
1011 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
1012 if (gtk_toggle_button_get_active
1013 (GTK_TOGGLE_BUTTON(uc
->buttons
[i
]))) {
1014 gtk_widget_grab_focus(uc
->buttons
[i
]);
1019 #if !GTK_CHECK_VERSION(2,4,0)
1021 gtk_widget_grab_focus(uc
->optmenu
);
1026 gtk_widget_grab_focus(uc
->combo
);
1030 #if !GTK_CHECK_VERSION(2,0,0)
1033 * For GTK-1 style list boxes, we tell it to focus one
1034 * of its children, which appears to do the Right
1037 gtk_container_focus(GTK_CONTAINER(uc
->list
), GTK_DIR_TAB_FORWARD
);
1042 gtk_widget_grab_focus(uc
->treeview
);
1046 assert(!"We shouldn't get here");
1052 * During event processing, you might well want to give an error
1053 * indication to the user. dlg_beep() is a quick and easy generic
1054 * error; dlg_error() puts up a message-box or equivalent.
1056 void dlg_beep(void *dlg
)
1061 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
1063 gtk_widget_destroy(GTK_WIDGET(data
));
1066 static void set_transient_window_pos(GtkWidget
*parent
, GtkWidget
*child
)
1068 gint x
, y
, w
, h
, dx
, dy
;
1070 gtk_window_set_position(GTK_WINDOW(child
), GTK_WIN_POS_NONE
);
1071 gtk_widget_size_request(GTK_WIDGET(child
), &req
);
1073 gdk_window_get_origin(GTK_WIDGET(parent
)->window
, &x
, &y
);
1074 gdk_window_get_size(GTK_WIDGET(parent
)->window
, &w
, &h
);
1077 * One corner of the transient will be offset inwards, by 1/4
1078 * of the parent window's size, from the corresponding corner
1079 * of the parent window. The corner will be chosen so as to
1080 * place the transient closer to the centre of the screen; this
1081 * should avoid transients going off the edge of the screen on
1084 if (x
+ w
/2 < gdk_screen_width() / 2)
1085 dx
= x
+ w
/4; /* work from left edges */
1087 dx
= x
+ 3*w
/4 - req
.width
; /* work from right edges */
1088 if (y
+ h
/2 < gdk_screen_height() / 2)
1089 dy
= y
+ h
/4; /* work from top edges */
1091 dy
= y
+ 3*h
/4 - req
.height
; /* work from bottom edges */
1092 gtk_widget_set_uposition(GTK_WIDGET(child
), dx
, dy
);
1095 void dlg_error_msg(void *dlg
, char *msg
)
1097 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1098 GtkWidget
*window
, *hbox
, *text
, *ok
;
1100 window
= gtk_dialog_new();
1101 text
= gtk_label_new(msg
);
1102 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
1103 hbox
= gtk_hbox_new(FALSE
, 0);
1104 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
1105 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
1106 hbox
, FALSE
, FALSE
, 20);
1107 gtk_widget_show(text
);
1108 gtk_widget_show(hbox
);
1109 gtk_window_set_title(GTK_WINDOW(window
), "Error");
1110 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
1111 ok
= gtk_button_new_with_label("OK");
1112 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1113 ok
, FALSE
, FALSE
, 0);
1114 gtk_widget_show(ok
);
1115 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
1116 gtk_window_set_default(GTK_WINDOW(window
), ok
);
1117 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
1118 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
1119 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
1120 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1121 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
1122 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(dp
->window
));
1123 set_transient_window_pos(dp
->window
, window
);
1124 gtk_widget_show(window
);
1129 * This function signals to the front end that the dialog's
1130 * processing is completed, and passes an integer value (typically
1131 * a success status).
1133 void dlg_end(void *dlg
, int value
)
1135 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1137 gtk_widget_destroy(dp
->window
);
1140 void dlg_refresh(union control
*ctrl
, void *dlg
)
1142 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1146 if (ctrl
->generic
.handler
!= NULL
)
1147 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
1151 for (i
= 0; (uc
= index234(dp
->byctrl
, i
)) != NULL
; i
++) {
1152 assert(uc
->ctrl
!= NULL
);
1153 if (uc
->ctrl
->generic
.handler
!= NULL
)
1154 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
,
1155 dp
->data
, EVENT_REFRESH
);
1160 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
1162 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1163 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
);
1166 GtkWidget
*coloursel
=
1167 gtk_color_selection_dialog_new("Select a colour");
1168 GtkColorSelectionDialog
*ccs
= GTK_COLOR_SELECTION_DIALOG(coloursel
);
1170 dp
->coloursel_result
.ok
= FALSE
;
1172 gtk_window_set_modal(GTK_WINDOW(coloursel
), TRUE
);
1173 #if GTK_CHECK_VERSION(2,0,0)
1174 gtk_color_selection_set_has_opacity_control(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
1176 gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs
->colorsel
), FALSE
);
1178 cvals
[0] = r
/ 255.0;
1179 cvals
[1] = g
/ 255.0;
1180 cvals
[2] = b
/ 255.0;
1181 cvals
[3] = 1.0; /* fully opaque! */
1182 gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs
->colorsel
), cvals
);
1184 gtk_object_set_data(GTK_OBJECT(ccs
->ok_button
), "user-data",
1185 (gpointer
)coloursel
);
1186 gtk_object_set_data(GTK_OBJECT(ccs
->cancel_button
), "user-data",
1187 (gpointer
)coloursel
);
1188 gtk_object_set_data(GTK_OBJECT(coloursel
), "user-data", (gpointer
)uc
);
1189 gtk_signal_connect(GTK_OBJECT(ccs
->ok_button
), "clicked",
1190 GTK_SIGNAL_FUNC(coloursel_ok
), (gpointer
)dp
);
1191 gtk_signal_connect(GTK_OBJECT(ccs
->cancel_button
), "clicked",
1192 GTK_SIGNAL_FUNC(coloursel_cancel
), (gpointer
)dp
);
1193 gtk_signal_connect_object(GTK_OBJECT(ccs
->ok_button
), "clicked",
1194 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1195 (gpointer
)coloursel
);
1196 gtk_signal_connect_object(GTK_OBJECT(ccs
->cancel_button
), "clicked",
1197 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1198 (gpointer
)coloursel
);
1199 gtk_widget_show(coloursel
);
1202 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1203 int *r
, int *g
, int *b
)
1205 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1206 if (dp
->coloursel_result
.ok
) {
1207 *r
= dp
->coloursel_result
.r
;
1208 *g
= dp
->coloursel_result
.g
;
1209 *b
= dp
->coloursel_result
.b
;
1215 /* ----------------------------------------------------------------------
1216 * Signal handlers while the dialog box is active.
1219 static gboolean
widget_focus(GtkWidget
*widget
, GdkEventFocus
*event
,
1222 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1223 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1224 union control
*focus
;
1231 if (focus
!= dp
->currfocus
) {
1232 dp
->lastfocus
= dp
->currfocus
;
1233 dp
->currfocus
= focus
;
1239 static void button_clicked(GtkButton
*button
, gpointer data
)
1241 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1242 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1243 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1246 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
1248 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1249 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tb
));
1250 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1253 static gboolean
editbox_key(GtkWidget
*widget
, GdkEventKey
*event
,
1257 * GtkEntry has a nasty habit of eating the Return key, which
1258 * is unhelpful since it doesn't actually _do_ anything with it
1259 * (it calls gtk_widget_activate, but our edit boxes never need
1260 * activating). So I catch Return before GtkEntry sees it, and
1261 * pass it straight on to the parent widget. Effect: hitting
1262 * Return in an edit box will now activate the default button
1263 * in the dialog just like it will everywhere else.
1265 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
1266 gboolean return_val
;
1267 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
1268 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
1269 event
, &return_val
);
1275 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
1277 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1278 if (!(dp
->flags
& FLAG_UPDATING_COMBO_LIST
)) {
1279 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
1280 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1284 static gboolean
editbox_lostfocus(GtkWidget
*ed
, GdkEventFocus
*event
,
1287 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1288 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(ed
));
1289 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
1293 #if !GTK_CHECK_VERSION(2,0,0)
1296 * GTK 1 list box event handlers.
1299 static gboolean
listitem_key(GtkWidget
*item
, GdkEventKey
*event
,
1300 gpointer data
, int multiple
)
1302 GtkAdjustment
*adj
= GTK_ADJUSTMENT(data
);
1304 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
1305 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
||
1306 event
->keyval
== GDK_Page_Up
|| event
->keyval
== GDK_KP_Page_Up
||
1307 event
->keyval
== GDK_Page_Down
|| event
->keyval
== GDK_KP_Page_Down
) {
1309 * Up, Down, PgUp or PgDn have been pressed on a ListItem
1310 * in a list box. So, if the list box is single-selection:
1312 * - if the list item in question isn't already selected,
1313 * we simply select it.
1314 * - otherwise, we find the next one (or next
1315 * however-far-away) in whichever direction we're going,
1317 * + in this case, we must also fiddle with the
1318 * scrollbar to ensure the newly selected item is
1321 * If it's multiple-selection, we do all of the above
1322 * except actually selecting anything, so we move the focus
1323 * and fiddle the scrollbar to follow it.
1325 GtkWidget
*list
= item
->parent
;
1327 gtk_signal_emit_stop_by_name(GTK_OBJECT(item
), "key_press_event");
1330 GTK_WIDGET_STATE(item
) != GTK_STATE_SELECTED
) {
1331 gtk_list_select_child(GTK_LIST(list
), item
);
1334 (event
->keyval
==GDK_Up
|| event
->keyval
==GDK_KP_Up
||
1335 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1338 (event
->keyval
==GDK_Page_Down
||
1339 event
->keyval
==GDK_KP_Page_Down
||
1340 event
->keyval
==GDK_Page_Up
|| event
->keyval
==GDK_KP_Page_Up
)
1343 GList
*children
, *chead
;
1345 chead
= children
= gtk_container_children(GTK_CONTAINER(list
));
1347 n
= g_list_length(children
);
1351 * Figure out how many list items to a screenful,
1352 * and adjust the step appropriately.
1354 step
= 0.5 + adj
->page_size
* n
/ (adj
->upper
- adj
->lower
);
1355 step
--; /* go by one less than that */
1359 while (children
!= NULL
) {
1360 if (item
== children
->data
)
1362 children
= children
->next
;
1367 if (direction
< 0 && i
> 0)
1368 children
= children
->prev
, i
--;
1369 else if (direction
> 0 && i
< n
-1)
1370 children
= children
->next
, i
++;
1374 if (children
&& children
->data
) {
1376 gtk_list_select_child(GTK_LIST(list
),
1377 GTK_WIDGET(children
->data
));
1378 gtk_widget_grab_focus(GTK_WIDGET(children
->data
));
1379 gtk_adjustment_clamp_page
1381 adj
->lower
+ (adj
->upper
-adj
->lower
) * i
/ n
,
1382 adj
->lower
+ (adj
->upper
-adj
->lower
) * (i
+1) / n
);
1393 static gboolean
listitem_single_key(GtkWidget
*item
, GdkEventKey
*event
,
1396 return listitem_key(item
, event
, data
, FALSE
);
1399 static gboolean
listitem_multi_key(GtkWidget
*item
, GdkEventKey
*event
,
1402 return listitem_key(item
, event
, data
, TRUE
);
1405 static gboolean
listitem_button_press(GtkWidget
*item
, GdkEventButton
*event
,
1408 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1409 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1410 switch (event
->type
) {
1412 case GDK_BUTTON_PRESS
: uc
->nclicks
= 1; break;
1413 case GDK_2BUTTON_PRESS
: uc
->nclicks
= 2; break;
1414 case GDK_3BUTTON_PRESS
: uc
->nclicks
= 3; break;
1419 static gboolean
listitem_button_release(GtkWidget
*item
, GdkEventButton
*event
,
1422 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1423 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(item
));
1424 if (uc
->nclicks
>1) {
1425 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1431 static void list_selchange(GtkList
*list
, gpointer data
)
1433 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1434 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(list
));
1436 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1439 static void draglist_move(struct dlgparam
*dp
, struct uctrl
*uc
, int direction
)
1441 int index
= dlg_listbox_index(uc
->ctrl
, dp
);
1442 GList
*children
= gtk_container_children(GTK_CONTAINER(uc
->list
));
1446 (index
== 0 && direction
< 0) ||
1447 (index
== g_list_length(children
)-1 && direction
> 0)) {
1452 child
= g_list_nth_data(children
, index
);
1453 gtk_widget_ref(child
);
1454 gtk_list_clear_items(GTK_LIST(uc
->list
), index
, index
+1);
1455 g_list_free(children
);
1458 children
= g_list_append(children
, child
);
1459 gtk_list_insert_items(GTK_LIST(uc
->list
), children
, index
+ direction
);
1460 gtk_list_select_item(GTK_LIST(uc
->list
), index
+ direction
);
1461 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1464 static void draglist_up(GtkButton
*button
, gpointer data
)
1466 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1467 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1468 draglist_move(dp
, uc
, -1);
1471 static void draglist_down(GtkButton
*button
, gpointer data
)
1473 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1474 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1475 draglist_move(dp
, uc
, +1);
1478 #else /* !GTK_CHECK_VERSION(2,0,0) */
1481 * GTK 2 list box event handlers.
1484 static void listbox_doubleclick(GtkTreeView
*treeview
, GtkTreePath
*path
,
1485 GtkTreeViewColumn
*column
, gpointer data
)
1487 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1488 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(treeview
));
1490 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1493 static void listbox_selchange(GtkTreeSelection
*treeselection
,
1496 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1497 GtkTreeView
*tree
= gtk_tree_selection_get_tree_view(treeselection
);
1498 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tree
));
1500 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1503 struct draglist_valchange_ctx
{
1505 struct dlgparam
*dp
;
1508 static gboolean
draglist_valchange(gpointer data
)
1510 struct draglist_valchange_ctx
*ctx
=
1511 (struct draglist_valchange_ctx
*)data
;
1513 ctx
->uc
->ctrl
->generic
.handler(ctx
->uc
->ctrl
, ctx
->dp
,
1514 ctx
->dp
->data
, EVENT_VALCHANGE
);
1521 static void listbox_reorder(GtkTreeModel
*treemodel
, GtkTreePath
*path
,
1522 GtkTreeIter
*iter
, gpointer data
)
1524 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1528 if (dp
->flags
& FLAG_UPDATING_LISTBOX
)
1529 return; /* not a user drag operation */
1531 tree
= g_object_get_data(G_OBJECT(treemodel
), "user-data");
1532 uc
= dlg_find_bywidget(dp
, GTK_WIDGET(tree
));
1535 * We should cause EVENT_VALCHANGE on the list box, now
1536 * that its rows have been reordered. However, the GTK 2
1537 * docs say that at the point this signal is received the
1538 * new row might not have actually been filled in yet.
1540 * (So what smegging use is it then, eh? Don't suppose it
1541 * occurred to you at any point that letting the
1542 * application know _after_ the reordering was compelete
1543 * might be helpful to someone?)
1545 * To get round this, I schedule an idle function, which I
1546 * hope won't be called until the main event loop is
1547 * re-entered after the drag-and-drop handler has finished
1548 * furtling with the list store.
1550 struct draglist_valchange_ctx
*ctx
=
1551 snew(struct draglist_valchange_ctx
);
1554 g_idle_add(draglist_valchange
, ctx
);
1558 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1560 #if !GTK_CHECK_VERSION(2,4,0)
1562 static void menuitem_activate(GtkMenuItem
*item
, gpointer data
)
1564 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1565 GtkWidget
*menushell
= GTK_WIDGET(item
)->parent
;
1566 gpointer optmenu
= gtk_object_get_data(GTK_OBJECT(menushell
), "user-data");
1567 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(optmenu
));
1568 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1573 static void droplist_selchange(GtkComboBox
*combo
, gpointer data
)
1575 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1576 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(combo
));
1578 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1581 #endif /* !GTK_CHECK_VERSION(2,4,0) */
1583 static void filesel_ok(GtkButton
*button
, gpointer data
)
1585 /* struct dlgparam *dp = (struct dlgparam *)data; */
1586 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1587 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(filesel
), "user-data");
1588 const char *name
= gtk_file_selection_get_filename
1589 (GTK_FILE_SELECTION(filesel
));
1590 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1593 static void fontsel_ok(GtkButton
*button
, gpointer data
)
1595 /* struct dlgparam *dp = (struct dlgparam *)data; */
1597 #if !GTK_CHECK_VERSION(2,0,0)
1599 gpointer fontsel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1600 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(fontsel
), "user-data");
1601 const char *name
= gtk_font_selection_dialog_get_font_name
1602 (GTK_FONT_SELECTION_DIALOG(fontsel
));
1603 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1607 unifontsel
*fontsel
= (unifontsel
*)gtk_object_get_data
1608 (GTK_OBJECT(button
), "user-data");
1609 struct uctrl
*uc
= (struct uctrl
*)fontsel
->user_data
;
1610 char *name
= unifontsel_get_name(fontsel
);
1611 assert(name
); /* should always be ok after OK pressed */
1612 gtk_entry_set_text(GTK_ENTRY(uc
->entry
), name
);
1618 static void coloursel_ok(GtkButton
*button
, gpointer data
)
1620 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1621 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1622 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1624 gtk_color_selection_get_color
1625 (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel
)->colorsel
),
1627 dp
->coloursel_result
.r
= (int) (255 * cvals
[0]);
1628 dp
->coloursel_result
.g
= (int) (255 * cvals
[1]);
1629 dp
->coloursel_result
.b
= (int) (255 * cvals
[2]);
1630 dp
->coloursel_result
.ok
= TRUE
;
1631 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1634 static void coloursel_cancel(GtkButton
*button
, gpointer data
)
1636 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1637 gpointer coloursel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1638 struct uctrl
*uc
= gtk_object_get_data(GTK_OBJECT(coloursel
), "user-data");
1639 dp
->coloursel_result
.ok
= FALSE
;
1640 uc
->ctrl
->generic
.handler(uc
->ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
1643 static void filefont_clicked(GtkButton
*button
, gpointer data
)
1645 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1646 struct uctrl
*uc
= dlg_find_bywidget(dp
, GTK_WIDGET(button
));
1648 if (uc
->ctrl
->generic
.type
== CTRL_FILESELECT
) {
1649 GtkWidget
*filesel
=
1650 gtk_file_selection_new(uc
->ctrl
->fileselect
.title
);
1651 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1653 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1655 gtk_object_set_data(GTK_OBJECT(filesel
), "user-data", (gpointer
)uc
);
1657 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1658 GTK_SIGNAL_FUNC(filesel_ok
), (gpointer
)dp
);
1659 gtk_signal_connect_object
1660 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1661 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1662 gtk_signal_connect_object
1663 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1664 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1665 gtk_widget_show(filesel
);
1668 if (uc
->ctrl
->generic
.type
== CTRL_FONTSELECT
) {
1669 const gchar
*fontname
= gtk_entry_get_text(GTK_ENTRY(uc
->entry
));
1671 #if !GTK_CHECK_VERSION(2,0,0)
1674 * Use the GTK 1 standard font selector.
1677 gchar
*spacings
[] = { "c", "m", NULL
};
1678 GtkWidget
*fontsel
=
1679 gtk_font_selection_dialog_new("Select a font");
1680 gtk_window_set_modal(GTK_WINDOW(fontsel
), TRUE
);
1681 gtk_font_selection_dialog_set_filter
1682 (GTK_FONT_SELECTION_DIALOG(fontsel
),
1683 GTK_FONT_FILTER_BASE
, GTK_FONT_ALL
,
1684 NULL
, NULL
, NULL
, NULL
, spacings
, NULL
);
1685 if (!gtk_font_selection_dialog_set_font_name
1686 (GTK_FONT_SELECTION_DIALOG(fontsel
), fontname
)) {
1688 * If the font name wasn't found as it was, try opening
1689 * it and extracting its FONT property. This should
1690 * have the effect of mapping short aliases into true
1693 GdkFont
*font
= gdk_font_load(fontname
);
1695 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
1696 Display
*disp
= GDK_FONT_XDISPLAY(font
);
1697 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
1700 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
1701 char *name
= XGetAtomName(disp
, (Atom
)ret
);
1703 gtk_font_selection_dialog_set_font_name
1704 (GTK_FONT_SELECTION_DIALOG(fontsel
), name
);
1706 gdk_font_unref(font
);
1710 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1711 "user-data", (gpointer
)fontsel
);
1712 gtk_object_set_data(GTK_OBJECT(fontsel
), "user-data", (gpointer
)uc
);
1714 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1715 "clicked", GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1716 gtk_signal_connect_object
1717 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->ok_button
),
1718 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1720 gtk_signal_connect_object
1721 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel
)->cancel_button
),
1722 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy
),
1724 gtk_widget_show(fontsel
);
1726 #else /* !GTK_CHECK_VERSION(2,0,0) */
1729 * Use the unifontsel code provided in gtkfont.c.
1732 unifontsel
*fontsel
= unifontsel_new("Select a font");
1734 gtk_window_set_modal(fontsel
->window
, TRUE
);
1735 unifontsel_set_name(fontsel
, fontname
);
1737 gtk_object_set_data(GTK_OBJECT(fontsel
->ok_button
),
1738 "user-data", (gpointer
)fontsel
);
1739 fontsel
->user_data
= uc
;
1740 gtk_signal_connect(GTK_OBJECT(fontsel
->ok_button
), "clicked",
1741 GTK_SIGNAL_FUNC(fontsel_ok
), (gpointer
)dp
);
1742 gtk_signal_connect_object(GTK_OBJECT(fontsel
->ok_button
), "clicked",
1743 GTK_SIGNAL_FUNC(unifontsel_destroy
),
1745 gtk_signal_connect_object(GTK_OBJECT(fontsel
->cancel_button
),"clicked",
1746 GTK_SIGNAL_FUNC(unifontsel_destroy
),
1749 gtk_widget_show(GTK_WIDGET(fontsel
->window
));
1751 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1756 static void label_sizealloc(GtkWidget
*widget
, GtkAllocation
*alloc
,
1759 struct dlgparam
*dp
= (struct dlgparam
*)data
;
1760 struct uctrl
*uc
= dlg_find_bywidget(dp
, widget
);
1762 gtk_widget_set_usize(uc
->text
, alloc
->width
, -1);
1763 gtk_label_set_text(GTK_LABEL(uc
->text
), uc
->ctrl
->generic
.label
);
1764 gtk_signal_disconnect(GTK_OBJECT(uc
->text
), uc
->textsig
);
1767 /* ----------------------------------------------------------------------
1768 * This function does the main layout work: it reads a controlset,
1769 * it creates the relevant GTK controls, and returns a GtkWidget
1770 * containing the result. (This widget might be a title of some
1771 * sort, it might be a Columns containing many controls, or it
1772 * might be a GtkFrame containing a Columns; whatever it is, it's
1773 * definitely a GtkWidget and should probably be added to a
1776 * `win' is required for setting the default button. If it is
1777 * non-NULL, all buttons created will be default-capable (so they
1778 * have extra space round them for the default highlight).
1780 GtkWidget
*layout_ctrls(struct dlgparam
*dp
, struct Shortcuts
*scs
,
1781 struct controlset
*s
, GtkWindow
*win
)
1787 if (!s
->boxname
&& s
->boxtitle
) {
1788 /* This controlset is a panel title. */
1789 return gtk_label_new(s
->boxtitle
);
1793 * Otherwise, we expect to be laying out actual controls, so
1794 * we'll start by creating a Columns for the purpose.
1796 cols
= COLUMNS(columns_new(4));
1797 ret
= GTK_WIDGET(cols
);
1798 gtk_widget_show(ret
);
1801 * Create a containing frame if we have a box name.
1804 ret
= gtk_frame_new(s
->boxtitle
); /* NULL is valid here */
1805 gtk_container_set_border_width(GTK_CONTAINER(cols
), 4);
1806 gtk_container_add(GTK_CONTAINER(ret
), GTK_WIDGET(cols
));
1807 gtk_widget_show(ret
);
1811 * Now iterate through the controls themselves, create them,
1812 * and add them to the Columns.
1814 for (i
= 0; i
< s
->ncontrols
; i
++) {
1815 union control
*ctrl
= s
->ctrls
[i
];
1818 GtkWidget
*w
= NULL
;
1820 switch (ctrl
->generic
.type
) {
1823 static const int simplecols
[1] = { 100 };
1824 columns_set_cols(cols
, ctrl
->columns
.ncols
,
1825 (ctrl
->columns
.percentages ?
1826 ctrl
->columns
.percentages
: simplecols
));
1828 continue; /* no actual control created */
1831 struct uctrl
*uc
= dlg_find_byctrl(dp
, ctrl
->tabdelay
.ctrl
);
1833 columns_taborder_last(cols
, uc
->toplevel
);
1835 continue; /* no actual control created */
1838 uc
= snew(struct uctrl
);
1840 uc
->privdata
= NULL
;
1841 uc
->privdata_needs_free
= FALSE
;
1844 #if !GTK_CHECK_VERSION(2,4,0)
1845 uc
->list
= uc
->menu
= uc
->optmenu
= NULL
;
1849 #if GTK_CHECK_VERSION(2,0,0)
1850 uc
->treeview
= NULL
;
1851 uc
->listmodel
= NULL
;
1853 uc
->button
= uc
->text
= NULL
;
1857 switch (ctrl
->generic
.type
) {
1859 w
= gtk_button_new_with_label(ctrl
->generic
.label
);
1861 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1862 if (ctrl
->button
.isdefault
)
1863 gtk_window_set_default(win
, w
);
1864 if (ctrl
->button
.iscancel
)
1865 dp
->cancelbutton
= w
;
1867 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1868 GTK_SIGNAL_FUNC(button_clicked
), dp
);
1869 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1870 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1871 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->button
.shortcut
,
1872 SHORTCUT_UCTRL
, uc
);
1875 w
= gtk_check_button_new_with_label(ctrl
->generic
.label
);
1876 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1877 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1878 gtk_signal_connect(GTK_OBJECT(w
), "focus_in_event",
1879 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1880 shortcut_add(scs
, GTK_BIN(w
)->child
, ctrl
->checkbox
.shortcut
,
1881 SHORTCUT_UCTRL
, uc
);
1886 * Radio buttons get to go inside their own Columns, no
1890 gint i
, *percentages
;
1894 if (ctrl
->generic
.label
) {
1895 GtkWidget
*label
= gtk_label_new(ctrl
->generic
.label
);
1896 columns_add(COLUMNS(w
), label
, 0, 1);
1897 columns_force_left_align(COLUMNS(w
), label
);
1898 gtk_widget_show(label
);
1899 shortcut_add(scs
, label
, ctrl
->radio
.shortcut
,
1900 SHORTCUT_UCTRL
, uc
);
1903 percentages
= g_new(gint
, ctrl
->radio
.ncolumns
);
1904 for (i
= 0; i
< ctrl
->radio
.ncolumns
; i
++) {
1906 ((100 * (i
+1) / ctrl
->radio
.ncolumns
) -
1907 100 * i
/ ctrl
->radio
.ncolumns
);
1909 columns_set_cols(COLUMNS(w
), ctrl
->radio
.ncolumns
,
1911 g_free(percentages
);
1914 uc
->nbuttons
= ctrl
->radio
.nbuttons
;
1915 uc
->buttons
= snewn(uc
->nbuttons
, GtkWidget
*);
1917 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1921 b
= (gtk_radio_button_new_with_label
1922 (group
, ctrl
->radio
.buttons
[i
]));
1924 group
= gtk_radio_button_group(GTK_RADIO_BUTTON(b
));
1925 colstart
= i
% ctrl
->radio
.ncolumns
;
1926 columns_add(COLUMNS(w
), b
, colstart
,
1927 (i
== ctrl
->radio
.nbuttons
-1 ?
1928 ctrl
->radio
.ncolumns
- colstart
: 1));
1929 columns_force_left_align(COLUMNS(w
), b
);
1931 gtk_signal_connect(GTK_OBJECT(b
), "toggled",
1932 GTK_SIGNAL_FUNC(button_toggled
), dp
);
1933 gtk_signal_connect(GTK_OBJECT(b
), "focus_in_event",
1934 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1935 if (ctrl
->radio
.shortcuts
) {
1936 shortcut_add(scs
, GTK_BIN(b
)->child
,
1937 ctrl
->radio
.shortcuts
[i
],
1938 SHORTCUT_UCTRL
, uc
);
1946 GtkWidget
*signalobject
;
1948 if (ctrl
->editbox
.has_list
) {
1949 #if !GTK_CHECK_VERSION(2,4,0)
1953 w
= gtk_combo_new();
1954 gtk_combo_set_value_in_list(GTK_COMBO(w
), FALSE
, TRUE
);
1955 uc
->entry
= GTK_COMBO(w
)->entry
;
1956 uc
->list
= GTK_COMBO(w
)->list
;
1957 signalobject
= uc
->entry
;
1962 uc
->listmodel
= gtk_list_store_new(2, G_TYPE_INT
,
1964 w
= gtk_combo_box_entry_new_with_model
1965 (GTK_TREE_MODEL(uc
->listmodel
), 1);
1966 /* We cannot support password combo boxes. */
1967 assert(!ctrl
->editbox
.password
);
1969 signalobject
= uc
->combo
;
1972 w
= gtk_entry_new();
1973 if (ctrl
->editbox
.password
)
1974 gtk_entry_set_visibility(GTK_ENTRY(w
), FALSE
);
1979 gtk_signal_connect(GTK_OBJECT(signalobject
), "changed",
1980 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
1981 gtk_signal_connect(GTK_OBJECT(signalobject
), "key_press_event",
1982 GTK_SIGNAL_FUNC(editbox_key
), dp
);
1983 gtk_signal_connect(GTK_OBJECT(signalobject
), "focus_in_event",
1984 GTK_SIGNAL_FUNC(widget_focus
), dp
);
1985 gtk_signal_connect(GTK_OBJECT(signalobject
), "focus_out_event",
1986 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1987 gtk_signal_connect(GTK_OBJECT(signalobject
), "focus_out_event",
1988 GTK_SIGNAL_FUNC(editbox_lostfocus
), dp
);
1990 * Edit boxes, for some strange reason, have a minimum
1991 * width of 150 in GTK 1.2. We don't want this - we'd
1992 * rather the edit boxes acquired their natural width
1993 * from the column layout of the rest of the box.
1995 * Also, while we're here, we'll squirrel away the
1996 * edit box height so we can use that to centre its
1997 * label vertically beside it.
1999 gtk_widget_size_request(w
, &req
);
2000 gtk_widget_set_usize(w
, 10, req
.height
);
2002 if (ctrl
->generic
.label
) {
2003 GtkWidget
*label
, *container
;
2005 label
= gtk_label_new(ctrl
->generic
.label
);
2007 shortcut_add(scs
, label
, ctrl
->editbox
.shortcut
,
2008 SHORTCUT_FOCUS
, uc
->entry
);
2010 container
= columns_new(4);
2011 if (ctrl
->editbox
.percentwidth
== 100) {
2012 columns_add(COLUMNS(container
), label
, 0, 1);
2013 columns_force_left_align(COLUMNS(container
), label
);
2014 columns_add(COLUMNS(container
), w
, 0, 1);
2016 gint percentages
[2];
2017 percentages
[1] = ctrl
->editbox
.percentwidth
;
2018 percentages
[0] = 100 - ctrl
->editbox
.percentwidth
;
2019 columns_set_cols(COLUMNS(container
), 2, percentages
);
2020 columns_add(COLUMNS(container
), label
, 0, 1);
2021 columns_force_left_align(COLUMNS(container
), label
);
2022 columns_add(COLUMNS(container
), w
, 1, 1);
2023 /* Centre the label vertically. */
2024 gtk_widget_set_usize(label
, -1, req
.height
);
2025 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
2027 gtk_widget_show(label
);
2035 case CTRL_FILESELECT
:
2036 case CTRL_FONTSELECT
:
2041 (ctrl
->generic
.type
== CTRL_FILESELECT ?
2042 "Browse..." : "Change...");
2044 gint percentages
[] = { 75, 25 };
2046 columns_set_cols(COLUMNS(w
), 2, percentages
);
2048 if (ctrl
->generic
.label
) {
2049 ww
= gtk_label_new(ctrl
->generic
.label
);
2050 columns_add(COLUMNS(w
), ww
, 0, 2);
2051 columns_force_left_align(COLUMNS(w
), ww
);
2052 gtk_widget_show(ww
);
2053 shortcut_add(scs
, ww
,
2054 (ctrl
->generic
.type
== CTRL_FILESELECT ?
2055 ctrl
->fileselect
.shortcut
:
2056 ctrl
->fontselect
.shortcut
),
2057 SHORTCUT_UCTRL
, uc
);
2061 uc
->entry
= ww
= gtk_entry_new();
2062 gtk_widget_size_request(ww
, &req
);
2063 gtk_widget_set_usize(ww
, 10, req
.height
);
2064 columns_add(COLUMNS(w
), ww
, 0, 1);
2065 gtk_widget_show(ww
);
2067 uc
->button
= ww
= gtk_button_new_with_label(browsebtn
);
2068 columns_add(COLUMNS(w
), ww
, 1, 1);
2069 gtk_widget_show(ww
);
2071 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "key_press_event",
2072 GTK_SIGNAL_FUNC(editbox_key
), dp
);
2074 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "changed",
2075 GTK_SIGNAL_FUNC(editbox_changed
), dp
);
2076 gtk_signal_connect(GTK_OBJECT(uc
->entry
), "focus_in_event",
2077 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2078 gtk_signal_connect(GTK_OBJECT(uc
->button
), "focus_in_event",
2079 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2080 gtk_signal_connect(GTK_OBJECT(ww
), "clicked",
2081 GTK_SIGNAL_FUNC(filefont_clicked
), dp
);
2086 #if GTK_CHECK_VERSION(2,0,0)
2088 * First construct the list data store, with the right
2089 * number of columns.
2091 # if !GTK_CHECK_VERSION(2,4,0)
2092 /* (For GTK 2.0 to 2.3, we do this for full listboxes only,
2093 * because combo boxes are still done the old GTK1 way.) */
2094 if (ctrl
->listbox
.height
> 0)
2101 cols
= ctrl
->listbox
.ncols
;
2102 cols
= cols ? cols
: 1;
2103 types
= snewn(1 + cols
, GType
);
2105 types
[0] = G_TYPE_INT
;
2106 for (i
= 0; i
< cols
; i
++)
2107 types
[i
+1] = G_TYPE_STRING
;
2109 uc
->listmodel
= gtk_list_store_newv(1 + cols
, types
);
2116 * See if it's a drop-down list (non-editable combo
2119 if (ctrl
->listbox
.height
== 0) {
2120 #if !GTK_CHECK_VERSION(2,4,0)
2122 * GTK1 and early-GTK2 option-menu style of
2125 uc
->optmenu
= w
= gtk_option_menu_new();
2126 uc
->menu
= gtk_menu_new();
2127 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), uc
->menu
);
2128 gtk_object_set_data(GTK_OBJECT(uc
->menu
), "user-data",
2129 (gpointer
)uc
->optmenu
);
2130 gtk_signal_connect(GTK_OBJECT(uc
->optmenu
), "focus_in_event",
2131 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2134 * Late-GTK2 style using a GtkComboBox.
2136 GtkCellRenderer
*cr
;
2139 * Create a non-editable GtkComboBox (that is, not
2140 * its subclass GtkComboBoxEntry).
2142 w
= gtk_combo_box_new_with_model
2143 (GTK_TREE_MODEL(uc
->listmodel
));
2147 * Tell it how to render a list item (i.e. which
2148 * column to look at in the list model).
2150 cr
= gtk_cell_renderer_text_new();
2151 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w
), cr
, TRUE
);
2152 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w
), cr
,
2156 * And tell it to notify us when the selection
2159 g_signal_connect(G_OBJECT(w
), "changed",
2160 G_CALLBACK(droplist_selchange
), dp
);
2163 #if !GTK_CHECK_VERSION(2,0,0)
2165 * GTK1-style full list box.
2167 uc
->list
= gtk_list_new();
2168 if (ctrl
->listbox
.multisel
== 2) {
2169 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
2170 GTK_SELECTION_EXTENDED
);
2171 } else if (ctrl
->listbox
.multisel
== 1) {
2172 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
2173 GTK_SELECTION_MULTIPLE
);
2175 gtk_list_set_selection_mode(GTK_LIST(uc
->list
),
2176 GTK_SELECTION_SINGLE
);
2178 w
= gtk_scrolled_window_new(NULL
, NULL
);
2179 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w
),
2181 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
2183 GTK_POLICY_AUTOMATIC
);
2184 uc
->adj
= gtk_scrolled_window_get_vadjustment
2185 (GTK_SCROLLED_WINDOW(w
));
2187 gtk_widget_show(uc
->list
);
2188 gtk_signal_connect(GTK_OBJECT(uc
->list
), "selection-changed",
2189 GTK_SIGNAL_FUNC(list_selchange
), dp
);
2190 gtk_signal_connect(GTK_OBJECT(uc
->list
), "focus_in_event",
2191 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2194 * Adjust the height of the scrolled window to the
2195 * minimum given by the height parameter.
2197 * This piece of guesswork is a horrid hack based
2198 * on looking inside the GTK 1.2 sources
2199 * (specifically gtkviewport.c, which appears to be
2200 * the widget which provides the border around the
2201 * scrolling area). Anyone lets me know how I can
2202 * do this in a way which isn't at risk from GTK
2203 * upgrades, I'd be grateful.
2207 edge
= GTK_WIDGET(uc
->list
)->style
->klass
->ythickness
;
2208 gtk_widget_set_usize(w
, 10,
2209 2*edge
+ (ctrl
->listbox
.height
*
2210 get_listitemheight(w
)));
2213 if (ctrl
->listbox
.draglist
) {
2215 * GTK doesn't appear to make it easy to
2216 * implement a proper draggable list; so
2217 * instead I'm just going to have to put an Up
2218 * and a Down button to the right of the actual
2219 * list box. Ah well.
2221 GtkWidget
*cols
, *button
;
2222 static const gint percentages
[2] = { 80, 20 };
2224 cols
= columns_new(4);
2225 columns_set_cols(COLUMNS(cols
), 2, percentages
);
2226 columns_add(COLUMNS(cols
), w
, 0, 1);
2228 button
= gtk_button_new_with_label("Up");
2229 columns_add(COLUMNS(cols
), button
, 1, 1);
2230 gtk_widget_show(button
);
2231 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
2232 GTK_SIGNAL_FUNC(draglist_up
), dp
);
2233 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
2234 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2235 button
= gtk_button_new_with_label("Down");
2236 columns_add(COLUMNS(cols
), button
, 1, 1);
2237 gtk_widget_show(button
);
2238 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
2239 GTK_SIGNAL_FUNC(draglist_down
), dp
);
2240 gtk_signal_connect(GTK_OBJECT(button
), "focus_in_event",
2241 GTK_SIGNAL_FUNC(widget_focus
), dp
);
2247 * GTK2 treeview-based full list box.
2249 GtkTreeSelection
*sel
;
2252 * Create the list box itself, its columns, and
2253 * its containing scrolled window.
2255 w
= gtk_tree_view_new_with_model
2256 (GTK_TREE_MODEL(uc
->listmodel
));
2257 g_object_set_data(G_OBJECT(uc
->listmodel
), "user-data",
2259 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2260 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(w
));
2261 gtk_tree_selection_set_mode
2262 (sel
, ctrl
->listbox
.multisel ? GTK_SELECTION_MULTIPLE
:
2263 GTK_SELECTION_SINGLE
);
2265 gtk_signal_connect(GTK_OBJECT(w
), "row-activated",
2266 GTK_SIGNAL_FUNC(listbox_doubleclick
), dp
);
2267 g_signal_connect(G_OBJECT(sel
), "changed",
2268 G_CALLBACK(listbox_selchange
), dp
);
2270 if (ctrl
->listbox
.draglist
) {
2271 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(w
), TRUE
);
2272 g_signal_connect(G_OBJECT(uc
->listmodel
), "row-inserted",
2273 G_CALLBACK(listbox_reorder
), dp
);
2280 cols
= ctrl
->listbox
.ncols
;
2281 cols
= cols ? cols
: 1;
2282 for (i
= 0; i
< cols
; i
++) {
2283 GtkTreeViewColumn
*column
;
2285 * It appears that GTK 2 doesn't leave us any
2286 * particularly sensible way to honour the
2287 * "percentages" specification in the ctrl
2290 column
= gtk_tree_view_column_new_with_attributes
2291 ("heading", gtk_cell_renderer_text_new(),
2292 "text", i
+1, (char *)NULL
);
2293 gtk_tree_view_column_set_sizing
2294 (column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
2295 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2302 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2303 gtk_scrolled_window_set_shadow_type
2304 (GTK_SCROLLED_WINDOW(scroll
), GTK_SHADOW_IN
);
2306 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2307 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2308 GTK_POLICY_AUTOMATIC
,
2310 gtk_widget_set_size_request
2312 ctrl
->listbox
.height
* get_listitemheight(w
));
2319 if (ctrl
->generic
.label
) {
2320 GtkWidget
*label
, *container
;
2323 label
= gtk_label_new(ctrl
->generic
.label
);
2325 shortcut_add(scs
, label
, ctrl
->listbox
.shortcut
,
2328 container
= columns_new(4);
2329 if (ctrl
->listbox
.percentwidth
== 100) {
2330 columns_add(COLUMNS(container
), label
, 0, 1);
2331 columns_force_left_align(COLUMNS(container
), label
);
2332 columns_add(COLUMNS(container
), w
, 0, 1);
2334 gint percentages
[2];
2335 percentages
[1] = ctrl
->listbox
.percentwidth
;
2336 percentages
[0] = 100 - ctrl
->listbox
.percentwidth
;
2337 columns_set_cols(COLUMNS(container
), 2, percentages
);
2338 columns_add(COLUMNS(container
), label
, 0, 1);
2339 columns_force_left_align(COLUMNS(container
), label
);
2340 columns_add(COLUMNS(container
), w
, 1, 1);
2341 /* Centre the label vertically. */
2342 gtk_widget_size_request(w
, &req
);
2343 gtk_widget_set_usize(label
, -1, req
.height
);
2344 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
2346 gtk_widget_show(label
);
2356 * Wrapping text widgets don't sit well with the GTK
2357 * layout model, in which widgets state a minimum size
2358 * and the whole window then adjusts to the smallest
2359 * size it can sensibly take given its contents. A
2360 * wrapping text widget _has_ no clear minimum size;
2361 * instead it has a range of possibilities. It can be
2362 * one line deep but 2000 wide, or two lines deep and
2363 * 1000 pixels, or three by 867, or four by 500 and so
2364 * on. It can be as short as you like provided you
2365 * don't mind it being wide, or as narrow as you like
2366 * provided you don't mind it being tall.
2368 * Therefore, it fits very badly into the layout model.
2369 * Hence the only thing to do is pick a width and let
2370 * it choose its own number of lines. To do this I'm
2371 * going to cheat a little. All new wrapping text
2372 * widgets will be created with a minimal text content
2373 * "X"; then, after the rest of the dialog box is set
2374 * up and its size calculated, the text widgets will be
2375 * told their width and given their real text, which
2376 * will cause the size to be recomputed in the y
2377 * direction (because many of them will expand to more
2380 uc
->text
= w
= gtk_label_new("X");
2381 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.0);
2382 gtk_label_set_line_wrap(GTK_LABEL(w
), TRUE
);
2384 gtk_signal_connect(GTK_OBJECT(w
), "size-allocate",
2385 GTK_SIGNAL_FUNC(label_sizealloc
), dp
);
2391 columns_add(cols
, w
,
2392 COLUMN_START(ctrl
->generic
.column
),
2393 COLUMN_SPAN(ctrl
->generic
.column
));
2395 columns_force_left_align(cols
, w
);
2399 dlg_add_uctrl(dp
, uc
);
2406 struct dlgparam
*dp
;
2407 GtkNotebook
*panels
;
2409 #if !GTK_CHECK_VERSION(2,0,0)
2410 GtkWidget
*treeitem
;
2413 GtkTreePath
*treepath
;
2415 struct Shortcuts shortcuts
;
2418 #if GTK_CHECK_VERSION(2,0,0)
2419 static void treeselection_changed(GtkTreeSelection
*treeselection
,
2422 struct selparam
*sps
= (struct selparam
*)data
, *sp
;
2423 GtkTreeModel
*treemodel
;
2424 GtkTreeIter treeiter
;
2428 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2431 gtk_tree_model_get(treemodel
, &treeiter
, TREESTORE_PARAMS
, &spindex
, -1);
2434 page_num
= gtk_notebook_page_num(sp
->panels
, sp
->panel
);
2435 gtk_notebook_set_page(sp
->panels
, page_num
);
2437 dlg_refresh(NULL
, sp
->dp
);
2439 sp
->dp
->shortcuts
= &sp
->shortcuts
;
2442 static void treeitem_sel(GtkItem
*item
, gpointer data
)
2444 struct selparam
*sp
= (struct selparam
*)data
;
2447 page_num
= gtk_notebook_page_num(sp
->panels
, sp
->panel
);
2448 gtk_notebook_set_page(sp
->panels
, page_num
);
2450 dlg_refresh(NULL
, sp
->dp
);
2452 sp
->dp
->shortcuts
= &sp
->shortcuts
;
2453 sp
->dp
->currtreeitem
= sp
->treeitem
;
2457 static void window_destroy(GtkWidget
*widget
, gpointer data
)
2462 #if !GTK_CHECK_VERSION(2,0,0)
2463 static int tree_grab_focus(struct dlgparam
*dp
)
2468 * See if any of the treeitems has the focus.
2471 for (i
= 0; i
< dp
->ntreeitems
; i
++)
2472 if (GTK_WIDGET_HAS_FOCUS(dp
->treeitems
[i
])) {
2480 gtk_widget_grab_focus(dp
->currtreeitem
);
2485 gint
tree_focus(GtkContainer
*container
, GtkDirectionType direction
,
2488 struct dlgparam
*dp
= (struct dlgparam
*)data
;
2490 gtk_signal_emit_stop_by_name(GTK_OBJECT(container
), "focus");
2492 * If there's a focused treeitem, we return FALSE to cause the
2493 * focus to move on to some totally other control. If not, we
2494 * focus the selected one.
2496 return tree_grab_focus(dp
);
2500 int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
2502 struct dlgparam
*dp
= (struct dlgparam
*)data
;
2504 if (event
->keyval
== GDK_Escape
&& dp
->cancelbutton
) {
2505 gtk_signal_emit_by_name(GTK_OBJECT(dp
->cancelbutton
), "clicked");
2509 if ((event
->state
& GDK_MOD1_MASK
) &&
2510 (unsigned char)event
->string
[0] > 0 &&
2511 (unsigned char)event
->string
[0] <= 127) {
2512 int schr
= (unsigned char)event
->string
[0];
2513 struct Shortcut
*sc
= &dp
->shortcuts
->sc
[schr
];
2515 switch (sc
->action
) {
2517 #if GTK_CHECK_VERSION(2,0,0)
2518 gtk_widget_grab_focus(sc
->widget
);
2520 tree_grab_focus(dp
);
2523 case SHORTCUT_FOCUS
:
2524 gtk_widget_grab_focus(sc
->widget
);
2526 case SHORTCUT_UCTRL
:
2528 * We must do something sensible with a uctrl.
2529 * Precisely what this is depends on the type of
2532 switch (sc
->uc
->ctrl
->generic
.type
) {
2535 /* Check boxes and buttons get the focus _and_ get toggled. */
2536 gtk_widget_grab_focus(sc
->uc
->toplevel
);
2537 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->toplevel
),
2540 case CTRL_FILESELECT
:
2541 case CTRL_FONTSELECT
:
2542 /* File/font selectors have their buttons pressed (ooer),
2543 * and focus transferred to the edit box. */
2544 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->button
),
2546 gtk_widget_grab_focus(sc
->uc
->entry
);
2550 * Radio buttons are fun, because they have
2551 * multiple shortcuts. We must find whether the
2552 * activated shortcut is the shortcut for the whole
2553 * group, or for a particular button. In the former
2554 * case, we find the currently selected button and
2555 * focus it; in the latter, we focus-and-click the
2556 * button whose shortcut was pressed.
2558 if (schr
== sc
->uc
->ctrl
->radio
.shortcut
) {
2560 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
2561 if (gtk_toggle_button_get_active
2562 (GTK_TOGGLE_BUTTON(sc
->uc
->buttons
[i
]))) {
2563 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
2565 } else if (sc
->uc
->ctrl
->radio
.shortcuts
) {
2567 for (i
= 0; i
< sc
->uc
->ctrl
->radio
.nbuttons
; i
++)
2568 if (schr
== sc
->uc
->ctrl
->radio
.shortcuts
[i
]) {
2569 gtk_widget_grab_focus(sc
->uc
->buttons
[i
]);
2570 gtk_signal_emit_by_name
2571 (GTK_OBJECT(sc
->uc
->buttons
[i
]), "clicked");
2577 #if !GTK_CHECK_VERSION(2,4,0)
2578 if (sc
->uc
->optmenu
) {
2582 gtk_widget_grab_focus(sc
->uc
->optmenu
);
2583 /* Option menus don't work using the "clicked" signal.
2584 * We need to manufacture a button press event :-/ */
2585 bev
.type
= GDK_BUTTON_PRESS
;
2587 gtk_signal_emit_by_name(GTK_OBJECT(sc
->uc
->optmenu
),
2588 "button_press_event",
2593 if (sc
->uc
->combo
) {
2594 gtk_widget_grab_focus(sc
->uc
->combo
);
2595 gtk_combo_box_popup(GTK_COMBO_BOX(sc
->uc
->combo
));
2599 #if !GTK_CHECK_VERSION(2,0,0)
2602 * For GTK-1 style list boxes, we tell it to
2603 * focus one of its children, which appears to
2604 * do the Right Thing.
2606 gtk_container_focus(GTK_CONTAINER(sc
->uc
->list
),
2607 GTK_DIR_TAB_FORWARD
);
2611 if (sc
->uc
->treeview
) {
2612 gtk_widget_grab_focus(sc
->uc
->treeview
);
2616 assert(!"We shouldn't get here");
2626 #if !GTK_CHECK_VERSION(2,0,0)
2627 int tree_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
2629 struct dlgparam
*dp
= (struct dlgparam
*)data
;
2631 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
||
2632 event
->keyval
== GDK_Down
|| event
->keyval
== GDK_KP_Down
) {
2634 for (i
= 0; i
< dp
->ntreeitems
; i
++)
2635 if (widget
== dp
->treeitems
[i
])
2637 if (i
< dp
->ntreeitems
) {
2638 if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_KP_Up
)
2645 if (i
< 0 || i
>= dp
->ntreeitems
)
2646 break; /* nothing in that dir to select */
2648 * Determine if this tree item is visible.
2651 GtkWidget
*w
= dp
->treeitems
[i
];
2653 while (w
&& (GTK_IS_TREE_ITEM(w
) || GTK_IS_TREE(w
))) {
2654 if (!GTK_WIDGET_VISIBLE(w
)) {
2661 j
= i
; /* got one */
2667 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
2670 gtk_signal_emit_by_name(GTK_OBJECT(dp
->treeitems
[j
]), "toggle");
2671 gtk_widget_grab_focus(dp
->treeitems
[j
]);
2677 * It's nice for Left and Right to expand and collapse tree
2680 if (event
->keyval
== GDK_Left
|| event
->keyval
== GDK_KP_Left
) {
2681 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
2683 gtk_tree_item_collapse(GTK_TREE_ITEM(widget
));
2686 if (event
->keyval
== GDK_Right
|| event
->keyval
== GDK_KP_Right
) {
2687 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
),
2689 gtk_tree_item_expand(GTK_TREE_ITEM(widget
));
2697 static void shortcut_highlight(GtkWidget
*labelw
, int chr
)
2699 GtkLabel
*label
= GTK_LABEL(labelw
);
2700 gchar
*currstr
, *pattern
;
2703 gtk_label_get(label
, &currstr
);
2704 for (i
= 0; currstr
[i
]; i
++)
2705 if (tolower((unsigned char)currstr
[i
]) == chr
) {
2708 pattern
= dupprintf("%*s_", i
, "");
2710 gtk_widget_size_request(GTK_WIDGET(label
), &req
);
2711 gtk_label_set_pattern(label
, pattern
);
2712 gtk_widget_set_usize(GTK_WIDGET(label
), -1, req
.height
);
2719 void shortcut_add(struct Shortcuts
*scs
, GtkWidget
*labelw
,
2720 int chr
, int action
, void *ptr
)
2722 if (chr
== NO_SHORTCUT
)
2725 chr
= tolower((unsigned char)chr
);
2727 assert(scs
->sc
[chr
].action
== SHORTCUT_EMPTY
);
2729 scs
->sc
[chr
].action
= action
;
2731 if (action
== SHORTCUT_FOCUS
) {
2732 scs
->sc
[chr
].uc
= NULL
;
2733 scs
->sc
[chr
].widget
= (GtkWidget
*)ptr
;
2735 scs
->sc
[chr
].widget
= NULL
;
2736 scs
->sc
[chr
].uc
= (struct uctrl
*)ptr
;
2739 shortcut_highlight(labelw
, chr
);
2742 int get_listitemheight(GtkWidget
*w
)
2744 #if !GTK_CHECK_VERSION(2,0,0)
2745 GtkWidget
*listitem
= gtk_list_item_new_with_label("foo");
2747 gtk_widget_size_request(listitem
, &req
);
2748 gtk_object_sink(GTK_OBJECT(listitem
));
2752 GtkCellRenderer
*cr
= gtk_cell_renderer_text_new();
2753 gtk_cell_renderer_get_size(cr
, w
, NULL
, NULL
, NULL
, NULL
, &height
);
2754 g_object_ref(G_OBJECT(cr
));
2755 gtk_object_sink(GTK_OBJECT(cr
));
2756 g_object_unref(G_OBJECT(cr
));
2761 void set_dialog_action_area(GtkDialog
*dlg
, GtkWidget
*w
)
2763 #if !GTK_CHECK_VERSION(2,0,0)
2766 * In GTK 1, laying out the buttons at the bottom of the
2767 * configuration box is nice and easy, because a GtkDialog's
2768 * action_area is a GtkHBox which stretches to cover the full
2769 * width of the dialog. So we just put our Columns widget
2770 * straight into that hbox, and it ends up just where we want
2773 gtk_box_pack_start(GTK_BOX(dlg
->action_area
), w
, TRUE
, TRUE
, 0);
2777 * In GTK 2, the action area is now a GtkHButtonBox and its
2778 * layout behaviour seems to be different: it doesn't stretch
2779 * to cover the full width of the window, but instead finds its
2780 * own preferred width and right-aligns that within the window.
2781 * This isn't what we want, because we have both left-aligned
2782 * and right-aligned buttons coming out of the above call to
2783 * layout_ctrls(), and right-aligning the whole thing will
2784 * result in the former being centred and looking weird.
2786 * So instead we abandon the dialog's action area completely:
2787 * we gtk_widget_hide() it in the below code, and we also call
2788 * gtk_dialog_set_has_separator() to remove the separator above
2789 * it. We then insert our own action area into the end of the
2790 * dialog's main vbox, and add our own separator above that.
2792 * (Ideally, if we were a native GTK app, we would use the
2793 * GtkHButtonBox's _own_ innate ability to support one set of
2794 * buttons being right-aligned and one left-aligned. But to do
2795 * that here, we would have to either (a) pick apart our cross-
2796 * platform layout structures and treat them specially for this
2797 * particular set of controls, which would be painful, or else
2798 * (b) develop a special and simpler cross-platform
2799 * representation for these particular controls, and introduce
2800 * special-case code into all the _other_ platforms to handle
2801 * it. Neither appeals. Therefore, I regretfully discard the
2802 * GTKHButtonBox and go it alone.)
2806 align
= gtk_alignment_new(0, 0, 1, 1);
2807 gtk_container_add(GTK_CONTAINER(align
), w
);
2809 * The purpose of this GtkAlignment is to provide padding
2810 * around the buttons. The padding we use is twice the padding
2811 * used in our GtkColumns, because we nest two GtkColumns most
2812 * of the time (one separating the tree view from the main
2813 * controls, and another for the main controls themselves).
2815 #if GTK_CHECK_VERSION(2,4,0)
2816 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 8, 8, 8, 8);
2818 gtk_widget_show(align
);
2819 gtk_box_pack_end(GTK_BOX(dlg
->vbox
), align
, FALSE
, TRUE
, 0);
2820 w
= gtk_hseparator_new();
2821 gtk_box_pack_end(GTK_BOX(dlg
->vbox
), w
, FALSE
, TRUE
, 0);
2823 gtk_widget_hide(dlg
->action_area
);
2824 gtk_dialog_set_has_separator(dlg
, FALSE
);
2828 int do_config_box(const char *title
, Config
*cfg
, int midsession
,
2831 GtkWidget
*window
, *hbox
, *vbox
, *cols
, *label
,
2832 *tree
, *treescroll
, *panels
, *panelvbox
;
2834 struct controlbox
*ctrlbox
;
2836 #if GTK_CHECK_VERSION(2,0,0)
2837 GtkTreeStore
*treestore
;
2838 GtkCellRenderer
*treerenderer
;
2839 GtkTreeViewColumn
*treecolumn
;
2840 GtkTreeSelection
*treeselection
;
2841 GtkTreeIter treeiterlevels
[8];
2843 GtkTreeItem
*treeitemlevels
[8];
2844 GtkTree
*treelevels
[8];
2847 struct Shortcuts scs
;
2849 struct selparam
*selparams
= NULL
;
2850 int nselparams
= 0, selparamsize
= 0;
2854 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
2855 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
2858 window
= gtk_dialog_new();
2860 ctrlbox
= ctrl_new_box();
2861 setup_config_box(ctrlbox
, midsession
, cfg
->protocol
, protcfginfo
);
2862 unix_setup_config_box(ctrlbox
, midsession
, cfg
->protocol
);
2863 gtk_setup_config_box(ctrlbox
, midsession
, window
);
2865 gtk_window_set_title(GTK_WINDOW(window
), title
);
2866 hbox
= gtk_hbox_new(FALSE
, 4);
2867 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
), hbox
, TRUE
, TRUE
, 0);
2868 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 10);
2869 gtk_widget_show(hbox
);
2870 vbox
= gtk_vbox_new(FALSE
, 4);
2871 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
2872 gtk_widget_show(vbox
);
2873 cols
= columns_new(4);
2874 gtk_box_pack_start(GTK_BOX(vbox
), cols
, FALSE
, FALSE
, 0);
2875 gtk_widget_show(cols
);
2876 label
= gtk_label_new("Category:");
2877 columns_add(COLUMNS(cols
), label
, 0, 1);
2878 columns_force_left_align(COLUMNS(cols
), label
);
2879 gtk_widget_show(label
);
2880 treescroll
= gtk_scrolled_window_new(NULL
, NULL
);
2881 #if GTK_CHECK_VERSION(2,0,0)
2882 treestore
= gtk_tree_store_new
2883 (TREESTORE_NUM
, G_TYPE_STRING
, G_TYPE_INT
);
2884 tree
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(treestore
));
2885 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
2886 treerenderer
= gtk_cell_renderer_text_new();
2887 treecolumn
= gtk_tree_view_column_new_with_attributes
2888 ("Label", treerenderer
, "text", 0, NULL
);
2889 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), treecolumn
);
2890 treeselection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
2891 gtk_tree_selection_set_mode(treeselection
, GTK_SELECTION_BROWSE
);
2892 gtk_container_add(GTK_CONTAINER(treescroll
), tree
);
2894 tree
= gtk_tree_new();
2895 gtk_tree_set_view_mode(GTK_TREE(tree
), GTK_TREE_VIEW_ITEM
);
2896 gtk_tree_set_selection_mode(GTK_TREE(tree
), GTK_SELECTION_BROWSE
);
2897 gtk_signal_connect(GTK_OBJECT(tree
), "focus",
2898 GTK_SIGNAL_FUNC(tree_focus
), &dp
);
2900 gtk_signal_connect(GTK_OBJECT(tree
), "focus_in_event",
2901 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
2902 shortcut_add(&scs
, label
, 'g', SHORTCUT_TREE
, tree
);
2903 gtk_widget_show(treescroll
);
2904 gtk_box_pack_start(GTK_BOX(vbox
), treescroll
, TRUE
, TRUE
, 0);
2905 panels
= gtk_notebook_new();
2906 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels
), FALSE
);
2907 gtk_notebook_set_show_border(GTK_NOTEBOOK(panels
), FALSE
);
2908 gtk_box_pack_start(GTK_BOX(hbox
), panels
, TRUE
, TRUE
, 0);
2909 gtk_widget_show(panels
);
2914 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
2915 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
2918 if (!*s
->pathname
) {
2919 w
= layout_ctrls(&dp
, &scs
, s
, GTK_WINDOW(window
));
2921 set_dialog_action_area(GTK_DIALOG(window
), w
);
2923 int j
= path ?
ctrl_path_compare(s
->pathname
, path
) : 0;
2924 if (j
!= INT_MAX
) { /* add to treeview, start new panel */
2926 #if GTK_CHECK_VERSION(2,0,0)
2927 GtkTreeIter treeiter
;
2929 GtkWidget
*treeitem
;
2934 * We expect never to find an implicit path
2935 * component. For example, we expect never to see
2936 * A/B/C followed by A/D/E, because that would
2937 * _implicitly_ create A/D. All our path prefixes
2938 * are expected to contain actual controls and be
2939 * selectable in the treeview; so we would expect
2940 * to see A/D _explicitly_ before encountering
2943 assert(j
== ctrl_path_elements(s
->pathname
) - 1);
2945 c
= strrchr(s
->pathname
, '/');
2953 first
= (panelvbox
== NULL
);
2955 panelvbox
= gtk_vbox_new(FALSE
, 4);
2956 gtk_widget_show(panelvbox
);
2957 gtk_notebook_append_page(GTK_NOTEBOOK(panels
), panelvbox
,
2962 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(panels
),
2964 gtk_notebook_set_page(GTK_NOTEBOOK(panels
), page_num
);
2967 if (nselparams
>= selparamsize
) {
2969 selparams
= sresize(selparams
, selparamsize
,
2972 selparams
[nselparams
].dp
= &dp
;
2973 selparams
[nselparams
].panels
= GTK_NOTEBOOK(panels
);
2974 selparams
[nselparams
].panel
= panelvbox
;
2975 selparams
[nselparams
].shortcuts
= scs
; /* structure copy */
2977 assert(j
-1 < level
);
2979 #if GTK_CHECK_VERSION(2,0,0)
2981 /* treeiterlevels[j-1] will always be valid because we
2982 * don't allow implicit path components; see above.
2984 gtk_tree_store_append(treestore
, &treeiter
,
2985 &treeiterlevels
[j
-1]);
2987 gtk_tree_store_append(treestore
, &treeiter
, NULL
);
2988 gtk_tree_store_set(treestore
, &treeiter
,
2990 TREESTORE_PARAMS
, nselparams
,
2992 treeiterlevels
[j
] = treeiter
;
2994 selparams
[nselparams
].depth
= j
;
2996 selparams
[nselparams
].treepath
=
2997 gtk_tree_model_get_path(GTK_TREE_MODEL(treestore
),
2998 &treeiterlevels
[j
-1]);
3000 * We are going to collapse all tree branches
3001 * at depth greater than 2, but not _yet_; see
3002 * the comment at the call to
3003 * gtk_tree_view_collapse_row below.
3005 gtk_tree_view_expand_row(GTK_TREE_VIEW(tree
),
3006 selparams
[nselparams
].treepath
,
3009 selparams
[nselparams
].treepath
= NULL
;
3012 treeitem
= gtk_tree_item_new_with_label(c
);
3014 if (!treelevels
[j
-1]) {
3015 treelevels
[j
-1] = GTK_TREE(gtk_tree_new());
3016 gtk_tree_item_set_subtree
3017 (treeitemlevels
[j
-1],
3018 GTK_WIDGET(treelevels
[j
-1]));
3020 gtk_tree_item_expand(treeitemlevels
[j
-1]);
3022 gtk_tree_item_collapse(treeitemlevels
[j
-1]);
3024 gtk_tree_append(treelevels
[j
-1], treeitem
);
3026 gtk_tree_append(GTK_TREE(tree
), treeitem
);
3028 treeitemlevels
[j
] = GTK_TREE_ITEM(treeitem
);
3029 treelevels
[j
] = NULL
;
3031 gtk_signal_connect(GTK_OBJECT(treeitem
), "key_press_event",
3032 GTK_SIGNAL_FUNC(tree_key_press
), &dp
);
3033 gtk_signal_connect(GTK_OBJECT(treeitem
), "focus_in_event",
3034 GTK_SIGNAL_FUNC(widget_focus
), &dp
);
3036 gtk_widget_show(treeitem
);
3039 gtk_tree_select_child(GTK_TREE(tree
), treeitem
);
3040 selparams
[nselparams
].treeitem
= treeitem
;
3047 w
= layout_ctrls(&dp
, &selparams
[nselparams
-1].shortcuts
, s
, NULL
);
3048 gtk_box_pack_start(GTK_BOX(panelvbox
), w
, FALSE
, FALSE
, 0);
3053 #if GTK_CHECK_VERSION(2,0,0)
3059 * We want our tree view to come up with all branches at
3060 * depth 2 or more collapsed. However, if we start off
3061 * with those branches collapsed, then the tree view's
3062 * size request will be calculated based on the width of
3063 * the collapsed tree. So instead we start with them all
3064 * expanded; then we ask for the current size request,
3065 * collapse the relevant rows, and force the width to the
3066 * value we just computed. This arranges that the tree
3067 * view is wide enough to have all branches expanded
3071 gtk_widget_size_request(tree
, &req
);
3073 for (i
= 0; i
< nselparams
; i
++)
3074 if (selparams
[i
].depth
>= 2)
3075 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree
),
3076 selparams
[i
].treepath
);
3078 gtk_widget_set_size_request(tree
, req
.width
, -1);
3082 #if GTK_CHECK_VERSION(2,0,0)
3083 g_signal_connect(G_OBJECT(treeselection
), "changed",
3084 G_CALLBACK(treeselection_changed
), selparams
);
3086 dp
.ntreeitems
= nselparams
;
3087 dp
.treeitems
= snewn(dp
.ntreeitems
, GtkWidget
*);
3089 for (index
= 0; index
< nselparams
; index
++) {
3090 gtk_signal_connect(GTK_OBJECT(selparams
[index
].treeitem
), "select",
3091 GTK_SIGNAL_FUNC(treeitem_sel
),
3093 dp
.treeitems
[index
] = selparams
[index
].treeitem
;
3098 dlg_refresh(NULL
, &dp
);
3100 dp
.shortcuts
= &selparams
[0].shortcuts
;
3101 #if !GTK_CHECK_VERSION(2,0,0)
3102 dp
.currtreeitem
= dp
.treeitems
[0];
3104 dp
.lastfocus
= NULL
;
3110 extern void set_window_icon(GtkWidget
*window
,
3111 const char *const *const *icon
,
3113 extern const char *const *const cfg_icon
[];
3114 extern const int n_cfg_icon
;
3115 set_window_icon(window
, cfg_icon
, n_cfg_icon
);
3118 #if !GTK_CHECK_VERSION(2,0,0)
3119 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll
),
3122 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll
),
3124 GTK_POLICY_AUTOMATIC
);
3125 gtk_widget_show(tree
);
3127 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
3128 gtk_widget_show(window
);
3131 * Set focus into the first available control.
3133 for (index
= 0; index
< ctrlbox
->nctrlsets
; index
++) {
3134 struct controlset
*s
= ctrlbox
->ctrlsets
[index
];
3139 for (j
= 0; j
< s
->ncontrols
; j
++)
3140 if (s
->ctrls
[j
]->generic
.type
!= CTRL_TABDELAY
&&
3141 s
->ctrls
[j
]->generic
.type
!= CTRL_COLUMNS
&&
3142 s
->ctrls
[j
]->generic
.type
!= CTRL_TEXT
) {
3143 dlg_set_focus(s
->ctrls
[j
], &dp
);
3144 dp
.lastfocus
= s
->ctrls
[j
];
3153 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
3154 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
3155 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
3156 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
3166 static void messagebox_handler(union control
*ctrl
, void *dlg
,
3167 void *data
, int event
)
3169 if (event
== EVENT_ACTION
)
3170 dlg_end(dlg
, ctrl
->generic
.context
.i
);
3172 int messagebox(GtkWidget
*parentwin
, char *title
, char *msg
, int minwid
, ...)
3174 GtkWidget
*window
, *w0
, *w1
;
3175 struct controlbox
*ctrlbox
;
3176 struct controlset
*s0
, *s1
;
3179 struct Shortcuts scs
;
3185 for (index
= 0; index
< lenof(scs
.sc
); index
++) {
3186 scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
3189 ctrlbox
= ctrl_new_box();
3192 va_start(ap
, minwid
);
3193 while (va_arg(ap
, char *) != NULL
) {
3195 (void) va_arg(ap
, int); /* shortcut */
3196 (void) va_arg(ap
, int); /* normal/default/cancel */
3197 (void) va_arg(ap
, int); /* end value */
3201 s0
= ctrl_getset(ctrlbox
, "", "", "");
3202 c
= ctrl_columns(s0
, 2, 50, 50);
3203 c
->columns
.ncols
= s0
->ncolumns
= ncols
;
3204 c
->columns
.percentages
= sresize(c
->columns
.percentages
, ncols
, int);
3205 for (index
= 0; index
< ncols
; index
++)
3206 c
->columns
.percentages
[index
] = (index
+1)*100/ncols
- index
*100/ncols
;
3207 va_start(ap
, minwid
);
3210 char *title
= va_arg(ap
, char *);
3211 int shortcut
, type
, value
;
3214 shortcut
= va_arg(ap
, int);
3215 type
= va_arg(ap
, int);
3216 value
= va_arg(ap
, int);
3217 c
= ctrl_pushbutton(s0
, title
, shortcut
, HELPCTX(no_help
),
3218 messagebox_handler
, I(value
));
3219 c
->generic
.column
= index
++;
3221 c
->button
.isdefault
= TRUE
;
3223 c
->button
.iscancel
= TRUE
;
3227 s1
= ctrl_getset(ctrlbox
, "x", "", "");
3228 ctrl_text(s1
, msg
, HELPCTX(no_help
));
3230 window
= gtk_dialog_new();
3231 gtk_window_set_title(GTK_WINDOW(window
), title
);
3232 w0
= layout_ctrls(&dp
, &scs
, s0
, GTK_WINDOW(window
));
3233 set_dialog_action_area(GTK_DIALOG(window
), w0
);
3234 gtk_widget_show(w0
);
3235 w1
= layout_ctrls(&dp
, &scs
, s1
, GTK_WINDOW(window
));
3236 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
3237 gtk_widget_set_usize(w1
, minwid
+20, -1);
3238 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
3240 gtk_widget_show(w1
);
3242 dp
.shortcuts
= &scs
;
3243 dp
.lastfocus
= NULL
;
3247 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
3249 set_transient_window_pos(parentwin
, window
);
3250 gtk_window_set_transient_for(GTK_WINDOW(window
),
3251 GTK_WINDOW(parentwin
));
3253 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
3254 gtk_widget_show(window
);
3256 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
3257 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
3258 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
3259 GTK_SIGNAL_FUNC(win_key_press
), &dp
);
3264 ctrl_free_box(ctrlbox
);
3269 static int string_width(char *text
)
3271 GtkWidget
*label
= gtk_label_new(text
);
3273 gtk_widget_size_request(label
, &req
);
3274 gtk_object_sink(GTK_OBJECT(label
));
3278 int reallyclose(void *frontend
)
3280 char *title
= dupcat(appname
, " Exit Confirmation", NULL
);
3281 int ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
3282 title
, "Are you sure you want to close this session?",
3283 string_width("Most of the width of the above text"),
3291 int verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
3292 char *keystr
, char *fingerprint
,
3293 void (*callback
)(void *ctx
, int result
), void *ctx
)
3295 static const char absenttxt
[] =
3296 "The server's host key is not cached. You have no guarantee "
3297 "that the server is the computer you think it is.\n"
3298 "The server's %s key fingerprint is:\n"
3300 "If you trust this host, press \"Accept\" to add the key to "
3301 "PuTTY's cache and carry on connecting.\n"
3302 "If you want to carry on connecting just once, without "
3303 "adding the key to the cache, press \"Connect Once\".\n"
3304 "If you do not trust this host, press \"Cancel\" to abandon the "
3306 static const char wrongtxt
[] =
3307 "WARNING - POTENTIAL SECURITY BREACH!\n"
3308 "The server's host key does not match the one PuTTY has "
3309 "cached. This means that either the server administrator "
3310 "has changed the host key, or you have actually connected "
3311 "to another computer pretending to be the server.\n"
3312 "The new %s key fingerprint is:\n"
3314 "If you were expecting this change and trust the new key, "
3315 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
3316 "If you want to carry on connecting but without updating "
3317 "the cache, press \"Connect Once\".\n"
3318 "If you want to abandon the connection completely, press "
3319 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
3327 ret
= verify_host_key(host
, port
, keytype
, keystr
);
3329 if (ret
== 0) /* success - key matched OK */
3332 text
= dupprintf((ret
== 2 ? wrongtxt
: absenttxt
), keytype
, fingerprint
);
3334 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
3335 "PuTTY Security Alert", text
,
3336 string_width(fingerprint
),
3337 "Accept", 'a', 0, 2,
3338 "Connect Once", 'o', 0, 1,
3339 "Cancel", 'c', -1, 0,
3345 store_host_key(host
, port
, keytype
, keystr
);
3346 return 1; /* continue with connection */
3347 } else if (ret
== 1)
3348 return 1; /* continue with connection */
3349 return 0; /* do not continue with connection */
3353 * Ask whether the selected algorithm is acceptable (since it was
3354 * below the configured 'warn' threshold).
3356 int askalg(void *frontend
, const char *algtype
, const char *algname
,
3357 void (*callback
)(void *ctx
, int result
), void *ctx
)
3359 static const char msg
[] =
3360 "The first %s supported by the server is "
3361 "%s, which is below the configured warning threshold.\n"
3362 "Continue with connection?";
3366 text
= dupprintf(msg
, algtype
, algname
);
3367 ret
= messagebox(GTK_WIDGET(get_window(frontend
)),
3368 "PuTTY Security Alert", text
,
3369 string_width("Continue with connection?"),
3382 void old_keyfile_warning(void)
3385 * This should never happen on Unix. We hope.
3389 void fatal_message_box(void *window
, char *msg
)
3391 messagebox(window
, "PuTTY Fatal Error", msg
,
3392 string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
3393 "OK", 'o', 1, 1, NULL
);
3396 void fatalbox(char *p
, ...)
3401 msg
= dupvprintf(p
, ap
);
3403 fatal_message_box(NULL
, msg
);
3408 static GtkWidget
*aboutbox
= NULL
;
3410 static void about_close_clicked(GtkButton
*button
, gpointer data
)
3412 gtk_widget_destroy(aboutbox
);
3416 static void licence_clicked(GtkButton
*button
, gpointer data
)
3421 "Copyright 1997-2008 Simon Tatham.\n\n"
3423 "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
3424 "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
3425 "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
3426 "Markus Kuhn, Colin Watson, and CORE SDI S.A.\n\n"
3428 "Permission is hereby granted, free of charge, to any person "
3429 "obtaining a copy of this software and associated documentation "
3430 "files (the ""Software""), to deal in the Software without restriction, "
3431 "including without limitation the rights to use, copy, modify, merge, "
3432 "publish, distribute, sublicense, and/or sell copies of the Software, "
3433 "and to permit persons to whom the Software is furnished to do so, "
3434 "subject to the following conditions:\n\n"
3436 "The above copyright notice and this permission notice shall be "
3437 "included in all copies or substantial portions of the Software.\n\n"
3439 "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
3440 "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
3441 "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
3442 "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
3443 "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
3444 "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
3445 "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
3446 "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
3447 "CONNECTION WITH THE SOFTWARE OR THE USE OR "
3448 "OTHER DEALINGS IN THE SOFTWARE.";
3450 title
= dupcat(appname
, " Licence", NULL
);
3451 assert(aboutbox
!= NULL
);
3452 messagebox(aboutbox
, title
, licence
,
3453 string_width("LONGISH LINE OF TEXT SO THE LICENCE"
3454 " BOX ISN'T EXCESSIVELY TALL AND THIN"),
3455 "OK", 'o', 1, 1, NULL
);
3459 void about_box(void *window
)
3465 gtk_widget_grab_focus(aboutbox
);
3469 aboutbox
= gtk_dialog_new();
3470 gtk_container_set_border_width(GTK_CONTAINER(aboutbox
), 10);
3471 title
= dupcat("About ", appname
, NULL
);
3472 gtk_window_set_title(GTK_WINDOW(aboutbox
), title
);
3475 w
= gtk_button_new_with_label("Close");
3476 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
3477 gtk_window_set_default(GTK_WINDOW(aboutbox
), w
);
3478 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
3479 w
, FALSE
, FALSE
, 0);
3480 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
3481 GTK_SIGNAL_FUNC(about_close_clicked
), NULL
);
3484 w
= gtk_button_new_with_label("View Licence");
3485 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
3486 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox
)->action_area
),
3487 w
, FALSE
, FALSE
, 0);
3488 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
3489 GTK_SIGNAL_FUNC(licence_clicked
), NULL
);
3492 w
= gtk_label_new(appname
);
3493 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
3494 w
, FALSE
, FALSE
, 0);
3497 w
= gtk_label_new(ver
);
3498 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
3499 w
, FALSE
, FALSE
, 5);
3502 w
= gtk_label_new("Copyright 1997-2008 Simon Tatham. All rights reserved");
3503 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox
)->vbox
),
3504 w
, FALSE
, FALSE
, 5);
3507 set_transient_window_pos(GTK_WIDGET(window
), aboutbox
);
3508 gtk_widget_show(aboutbox
);
3511 struct eventlog_stuff
{
3512 GtkWidget
*parentwin
, *window
;
3513 struct controlbox
*eventbox
;
3514 struct Shortcuts scs
;
3516 union control
*listctrl
;
3518 int nevents
, negsize
;
3521 int ignore_selchange
;
3524 static void eventlog_destroy(GtkWidget
*widget
, gpointer data
)
3526 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3530 dlg_cleanup(&es
->dp
);
3531 ctrl_free_box(es
->eventbox
);
3533 static void eventlog_ok_handler(union control
*ctrl
, void *dlg
,
3534 void *data
, int event
)
3536 if (event
== EVENT_ACTION
)
3539 static void eventlog_list_handler(union control
*ctrl
, void *dlg
,
3540 void *data
, int event
)
3542 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3544 if (event
== EVENT_REFRESH
) {
3547 dlg_update_start(ctrl
, dlg
);
3548 dlg_listbox_clear(ctrl
, dlg
);
3549 for (i
= 0; i
< es
->nevents
; i
++) {
3550 dlg_listbox_add(ctrl
, dlg
, es
->events
[i
]);
3552 dlg_update_done(ctrl
, dlg
);
3553 } else if (event
== EVENT_SELCHANGE
) {
3558 * If this SELCHANGE event is happening as a result of
3559 * deliberate deselection because someone else has grabbed
3560 * the selection, the last thing we want to do is pre-empt
3563 if (es
->ignore_selchange
)
3567 * Construct the data to use as the selection.
3572 for (i
= 0; i
< es
->nevents
; i
++) {
3573 if (dlg_listbox_issel(ctrl
, dlg
, i
)) {
3574 int extralen
= strlen(es
->events
[i
]);
3576 if (es
->sellen
+ extralen
+ 2 > selsize
) {
3577 selsize
= es
->sellen
+ extralen
+ 512;
3578 es
->seldata
= sresize(es
->seldata
, selsize
, char);
3581 strcpy(es
->seldata
+ es
->sellen
, es
->events
[i
]);
3582 es
->sellen
+= extralen
;
3583 es
->seldata
[es
->sellen
++] = '\n';
3587 if (gtk_selection_owner_set(es
->window
, GDK_SELECTION_PRIMARY
,
3588 GDK_CURRENT_TIME
)) {
3589 extern GdkAtom compound_text_atom
;
3591 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
3592 GDK_SELECTION_TYPE_STRING
, 1);
3593 gtk_selection_add_target(es
->window
, GDK_SELECTION_PRIMARY
,
3594 compound_text_atom
, 1);
3600 void eventlog_selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
3601 guint info
, guint time_stamp
, gpointer data
)
3603 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3605 gtk_selection_data_set(seldata
, seldata
->target
, 8,
3606 (unsigned char *)es
->seldata
, es
->sellen
);
3609 gint
eventlog_selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
3612 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)data
;
3616 * Deselect everything in the list box.
3618 uc
= dlg_find_byctrl(&es
->dp
, es
->listctrl
);
3619 es
->ignore_selchange
= 1;
3620 #if !GTK_CHECK_VERSION(2,0,0)
3622 gtk_list_unselect_all(GTK_LIST(uc
->list
));
3624 assert(uc
->treeview
);
3625 gtk_tree_selection_unselect_all
3626 (gtk_tree_view_get_selection(GTK_TREE_VIEW(uc
->treeview
)));
3628 es
->ignore_selchange
= 0;
3636 void showeventlog(void *estuff
, void *parentwin
)
3638 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
3639 GtkWidget
*window
, *w0
, *w1
;
3640 GtkWidget
*parent
= GTK_WIDGET(parentwin
);
3641 struct controlset
*s0
, *s1
;
3647 gtk_widget_grab_focus(es
->window
);
3653 for (index
= 0; index
< lenof(es
->scs
.sc
); index
++) {
3654 es
->scs
.sc
[index
].action
= SHORTCUT_EMPTY
;
3657 es
->eventbox
= ctrl_new_box();
3659 s0
= ctrl_getset(es
->eventbox
, "", "", "");
3660 ctrl_columns(s0
, 3, 33, 34, 33);
3661 c
= ctrl_pushbutton(s0
, "Close", 'c', HELPCTX(no_help
),
3662 eventlog_ok_handler
, P(NULL
));
3663 c
->button
.column
= 1;
3664 c
->button
.isdefault
= TRUE
;
3666 s1
= ctrl_getset(es
->eventbox
, "x", "", "");
3667 es
->listctrl
= c
= ctrl_listbox(s1
, NULL
, NO_SHORTCUT
, HELPCTX(no_help
),
3668 eventlog_list_handler
, P(es
));
3669 c
->listbox
.height
= 10;
3670 c
->listbox
.multisel
= 2;
3671 c
->listbox
.ncols
= 3;
3672 c
->listbox
.percentages
= snewn(3, int);
3673 c
->listbox
.percentages
[0] = 25;
3674 c
->listbox
.percentages
[1] = 10;
3675 c
->listbox
.percentages
[2] = 65;
3677 es
->window
= window
= gtk_dialog_new();
3678 title
= dupcat(appname
, " Event Log", NULL
);
3679 gtk_window_set_title(GTK_WINDOW(window
), title
);
3681 w0
= layout_ctrls(&es
->dp
, &es
->scs
, s0
, GTK_WINDOW(window
));
3682 set_dialog_action_area(GTK_DIALOG(window
), w0
);
3683 gtk_widget_show(w0
);
3684 w1
= layout_ctrls(&es
->dp
, &es
->scs
, s1
, GTK_WINDOW(window
));
3685 gtk_container_set_border_width(GTK_CONTAINER(w1
), 10);
3686 gtk_widget_set_usize(w1
, 20 +
3687 string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
3688 " IS QUITE LONG 'COS SSH LOG ENTRIES"
3690 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
3692 gtk_widget_show(w1
);
3695 es
->dp
.shortcuts
= &es
->scs
;
3696 es
->dp
.lastfocus
= NULL
;
3698 es
->dp
.window
= window
;
3700 dlg_refresh(NULL
, &es
->dp
);
3703 set_transient_window_pos(parent
, window
);
3704 gtk_window_set_transient_for(GTK_WINDOW(window
),
3705 GTK_WINDOW(parent
));
3707 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
3708 gtk_widget_show(window
);
3710 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
3711 GTK_SIGNAL_FUNC(eventlog_destroy
), es
);
3712 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
3713 GTK_SIGNAL_FUNC(win_key_press
), &es
->dp
);
3714 gtk_signal_connect(GTK_OBJECT(window
), "selection_get",
3715 GTK_SIGNAL_FUNC(eventlog_selection_get
), es
);
3716 gtk_signal_connect(GTK_OBJECT(window
), "selection_clear_event",
3717 GTK_SIGNAL_FUNC(eventlog_selection_clear
), es
);
3720 void *eventlogstuff_new(void)
3722 struct eventlog_stuff
*es
;
3723 es
= snew(struct eventlog_stuff
);
3724 memset(es
, 0, sizeof(*es
));
3728 void logevent_dlg(void *estuff
, const char *string
)
3730 struct eventlog_stuff
*es
= (struct eventlog_stuff
*)estuff
;
3735 if (es
->nevents
>= es
->negsize
) {
3737 es
->events
= sresize(es
->events
, es
->negsize
, char *);
3741 strftime(timebuf
, sizeof(timebuf
), "%Y-%m-%d %H:%M:%S\t", &tm
);
3743 es
->events
[es
->nevents
] = snewn(strlen(timebuf
) + strlen(string
) + 1, char);
3744 strcpy(es
->events
[es
->nevents
], timebuf
);
3745 strcat(es
->events
[es
->nevents
], string
);
3747 dlg_listbox_add(es
->listctrl
, &es
->dp
, es
->events
[es
->nevents
]);
3752 int askappend(void *frontend
, Filename filename
,
3753 void (*callback
)(void *ctx
, int result
), void *ctx
)
3755 static const char msgtemplate
[] =
3756 "The session log file \"%.*s\" already exists. "
3757 "You can overwrite it with a new session log, "
3758 "append your session log to the end of it, "
3759 "or disable session logging for this session.";
3764 message
= dupprintf(msgtemplate
, FILENAME_MAX
, filename
.path
);
3765 mbtitle
= dupprintf("%s Log to File", appname
);
3767 mbret
= messagebox(get_window(frontend
), mbtitle
, message
,
3768 string_width("LINE OF TEXT SUITABLE FOR THE"
3769 " ASKAPPEND WIDTH"),
3770 "Overwrite", 'o', 1, 2,
3771 "Append", 'a', 0, 1,
3772 "Disable", 'd', -1, 0,