2 * Utilities to use when constructing dialogs
4 * $Id: dlg_utils.c,v 1.11 2003/09/20 04:59:43 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <gdk/gdkkeysyms.h>
32 #include "gtkglobals.h"
34 #include "compat_macros.h"
37 dlg_activate (GtkWidget *widget, gpointer ok_button);
40 dlg_key_press (GtkWidget *widget, GdkEventKey *event, gpointer cancel_button);
42 /* Create a dialog box window that belongs to Ethereal's main window. */
44 dlg_window_new(const gchar *title)
48 #if GTK_MAJOR_VERSION < 2
49 win = gtk_window_new(GTK_WINDOW_DIALOG);
51 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
52 gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER_ON_PARENT);
54 gtk_window_set_transient_for(GTK_WINDOW(win), GTK_WINDOW(top_level));
55 gtk_window_set_title(GTK_WINDOW(win), title);
56 SIGNAL_CONNECT(win, "realize", window_icon_realize_cb, NULL);
60 /* Set the "activate" signal for a widget to call a routine to
61 activate the "OK" button for a dialog box.
63 XXX - there should be a way to specify that a GtkEntry widget
64 shouldn't itself handle the Return key, but should let it be
65 passed on to the parent, so that you don't have to do this
66 by hand for every GtkEntry widget in a dialog box, but, alas,
67 there isn't. (Does this problem exist for other widgets?
68 I.e., are there any others that seize the Return key? */
70 dlg_set_activate(GtkWidget *widget, GtkWidget *ok_button)
72 SIGNAL_CONNECT(widget, "activate", dlg_activate, ok_button);
76 dlg_activate (GtkWidget *widget _U_, gpointer ok_button)
78 gtk_widget_activate(GTK_WIDGET(ok_button));
81 /* Set the "key_press_event" signal for a top-level dialog window to
82 call a routine to activate the "Cancel" button for a dialog box if
83 the key being pressed is the <Esc> key.
85 XXX - there should be a GTK+ widget that'll do that for you, and
86 let you specify a "Cancel" button. It should also not impose
87 a requirement that there be a separator in the dialog box, as
88 the GtkDialog widget does; the visual convention that there's
89 such a separator between the rest of the dialog boxes and buttons
90 such as "OK" and "Cancel" is, for better or worse, not universal
91 (not even in GTK+ - look at the GtkFileSelection dialog!). */
93 dlg_set_cancel(GtkWidget *widget, GtkWidget *cancel_button)
95 SIGNAL_CONNECT(widget, "key_press_event", dlg_key_press, cancel_button);
99 dlg_key_press (GtkWidget *widget, GdkEventKey *event, gpointer cancel_button)
101 g_return_val_if_fail (widget != NULL, FALSE);
102 g_return_val_if_fail (event != NULL, FALSE);
104 if (event->keyval == GDK_Escape) {
105 gtk_widget_activate(GTK_WIDGET(cancel_button));
112 #if GTK_MAJOR_VERSION < 2
113 /* Sigh. GTK+ appears not to acknowledge that it should be possible
114 to attach mnemonics to anything other than menu items; provide
115 routines to create radio and check buttons with labels that
116 include mnemonics. */
119 GtkAccelGroup *accel_group;
123 dlg_fix_label_callback(GtkWidget *label_widget, gpointer data)
125 fix_label_args_t *args = data;
129 gtk_label_get(GTK_LABEL(label_widget), &label);
130 accel_key = gtk_label_parse_uline(GTK_LABEL(label_widget), label);
131 if (accel_key != GDK_VoidSymbol) {
132 /* Yes, we have a mnemonic. */
133 gtk_widget_add_accelerator(args->button, "clicked", args->accel_group,
134 accel_key, 0, GTK_ACCEL_LOCKED);
135 gtk_widget_add_accelerator(args->button, "clicked", args->accel_group,
136 accel_key, GDK_MOD1_MASK, GTK_ACCEL_LOCKED);
141 dlg_fix_button_label(GtkWidget *button, GtkAccelGroup *accel_group)
143 fix_label_args_t args;
145 args.button = button;
146 args.accel_group = accel_group;
147 gtk_container_foreach(GTK_CONTAINER(button), dlg_fix_label_callback, &args);
151 dlg_radio_button_new_with_label_with_mnemonic(GSList *group,
152 const gchar *label, GtkAccelGroup *accel_group)
154 GtkWidget *radio_button;
156 radio_button = gtk_radio_button_new_with_label (group, label);
157 dlg_fix_button_label(radio_button, accel_group);
162 dlg_check_button_new_with_label_with_mnemonic(const gchar *label,
163 GtkAccelGroup *accel_group)
165 GtkWidget *check_button;
167 check_button = gtk_check_button_new_with_label (label);
168 dlg_fix_button_label(check_button, accel_group);