added "Yes" and "No" buttons to the simple_dialog,

added a callback method, to be called if a button was pressed

svn path=/trunk/; revision=9907
This commit is contained in:
Ulf Lamping 2004-01-29 23:07:17 +00:00
parent e66b849e23
commit 419837e531
2 changed files with 85 additions and 37 deletions

View File

@ -1,7 +1,7 @@
/* simple_dialog.c /* simple_dialog.c
* Simple message dialog box routines. * Simple message dialog box routines.
* *
* $Id: simple_dialog.c,v 1.16 2004/01/21 21:19:34 ulfl Exp $ * $Id: simple_dialog.c,v 1.17 2004/01/29 23:07:17 ulfl Exp $
* *
* Ethereal - Network traffic analyzer * Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org> * By Gerald Combs <gerald@zing.org>
@ -45,7 +45,9 @@
static void simple_dialog_cancel_cb(GtkWidget *, gpointer); static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
static const gchar bm_key[] = "button mask"; #define CALLBACK_FCT_KEY "ESD_Callback_Fct"
#define CALLBACK_BTN_KEY "ESD_Callback_Btn"
#define CALLBACK_DATA_KEY "ESD_Callback_Data"
/* Simple dialog function - Displays a dialog box with the supplied message /* Simple dialog function - Displays a dialog box with the supplied message
* text. * text.
@ -61,10 +63,10 @@ static const gchar bm_key[] = "button mask";
*/ */
#define ESD_MAX_MSG_LEN 2048 #define ESD_MAX_MSG_LEN 2048
void gpointer
simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) { simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...) {
GtkWidget *win, *main_vb, *top_hb, *type_pm, *msg_label, GtkWidget *win, *main_vb, *top_hb, *type_pm, *msg_label,
*bbox, *ok_bt, *cancel_bt; *bbox, *bt;
GdkPixmap *pixmap; GdkPixmap *pixmap;
GdkBitmap *mask; GdkBitmap *mask;
GtkStyle *style; GtkStyle *style;
@ -83,6 +85,11 @@ simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
icon = eexcl3d64_xpm; icon = eexcl3d64_xpm;
win = dlg_window_new("Ethereal: Error"); win = dlg_window_new("Ethereal: Error");
break; break;
case ESD_TYPE_QUEST:
/* XXX: we need a question mark here */
icon = eexcl3d64_xpm;
win = dlg_window_new("Ethereal: Question");
break;
case ESD_TYPE_INFO : case ESD_TYPE_INFO :
default : default :
icon = eicon3d64_xpm; icon = eicon3d64_xpm;
@ -95,8 +102,6 @@ simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
gtk_container_border_width(GTK_CONTAINER(win), 7); gtk_container_border_width(GTK_CONTAINER(win), 7);
OBJECT_SET_DATA(win, bm_key, btn_mask);
/* Container for our rows */ /* Container for our rows */
main_vb = gtk_vbox_new(FALSE, 5); main_vb = gtk_vbox_new(FALSE, 5);
gtk_container_border_width(GTK_CONTAINER(main_vb), 5); gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
@ -128,44 +133,77 @@ simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
gtk_widget_show(msg_label); gtk_widget_show(msg_label);
/* Button row */ /* Button row */
if (btn_mask && *btn_mask == ESD_BTN_CANCEL) { switch(btn_mask) {
bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL); case(0):
} else { case(ESD_BTN_OK):
bbox = dlg_button_row_new(GTK_STOCK_OK, NULL); bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
break;
case(ESD_BTN_OK | ESD_BTN_CANCEL):
bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
break;
case(ESD_BTN_YES | ESD_BTN_NO | ESD_BTN_CANCEL):
bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
break;
default:
g_assert_not_reached();
} }
gtk_container_add(GTK_CONTAINER(main_vb), bbox); gtk_container_add(GTK_CONTAINER(main_vb), bbox);
gtk_widget_show(bbox); gtk_widget_show(bbox);
ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK); bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
SIGNAL_CONNECT_OBJECT(ok_bt, "clicked", gtk_widget_destroy, win); if(bt) {
gtk_widget_grab_default(ok_bt); OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
if (btn_mask && *btn_mask == ESD_BTN_CANCEL) { gtk_widget_grab_default(bt);
cancel_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
SIGNAL_CONNECT(cancel_bt, "clicked", simple_dialog_cancel_cb, win);
/* Catch the "key_press_event" signal in the window, so that we can catch
the ESC key being pressed and act as if the "Cancel" button had
been selected. */
dlg_set_cancel(win, cancel_bt);
} else {
/* Catch the "key_press_event" signal in the window, so that we can catch /* Catch the "key_press_event" signal in the window, so that we can catch
the ESC key being pressed and act as if the "OK" button had the ESC key being pressed and act as if the "OK" button had
been selected. */ been selected. */
dlg_set_cancel(win, ok_bt); dlg_set_cancel(win, bt);
} }
if (btn_mask) bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
*btn_mask = ESD_BTN_OK; if(bt) {
OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
}
bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
if(bt) {
OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
}
bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
if(bt) {
OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
/* Catch the "key_press_event" signal in the window, so that we can catch
the ESC key being pressed and act as if the "OK" button had
been selected. */
dlg_set_cancel(win, bt);
gtk_widget_grab_default(bt);
}
gtk_widget_show(win); gtk_widget_show(win);
return win;
} }
static void static void
simple_dialog_cancel_cb(GtkWidget *w _U_, gpointer win) { simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
gint *btn_mask = (gint *) OBJECT_GET_DATA(win, bm_key); gint button = GPOINTER_TO_INT( OBJECT_GET_DATA(w, CALLBACK_BTN_KEY));
simple_dialog_cb_t callback_fct = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
gpointer data = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
if (btn_mask)
*btn_mask = ESD_BTN_CANCEL;
gtk_widget_destroy(GTK_WIDGET(win)); gtk_widget_destroy(GTK_WIDGET(win));
if (callback_fct)
(callback_fct) (win, button, data);
}
void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
{
OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
} }

View File

@ -2,7 +2,7 @@
* Definitions for dialog box routines with toolkit-independent APIs but * Definitions for dialog box routines with toolkit-independent APIs but
* toolkit-dependent implementations. * toolkit-dependent implementations.
* *
* $Id: simple_dialog.h,v 1.4 2002/08/28 21:00:41 jmayer Exp $ * $Id: simple_dialog.h,v 1.5 2004/01/29 23:07:17 ulfl Exp $
* *
* Ethereal - Network traffic analyzer * Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org> * By Gerald Combs <gerald@zing.org>
@ -35,22 +35,32 @@ extern "C" {
#define ESD_TYPE_INFO 0x00 #define ESD_TYPE_INFO 0x00
#define ESD_TYPE_WARN 0x01 #define ESD_TYPE_WARN 0x01
#define ESD_TYPE_CRIT 0x02 #define ESD_TYPE_CRIT 0x02
#define ESD_TYPE_QUEST 0x03
/* Flag to be ORed with the dialog type, to specify that the dialog is /* Flag to be ORed with the dialog type, to specify that the dialog is
to be modal. */ to be modal. */
#define ESD_TYPE_MODAL 0x04 #define ESD_TYPE_MODAL 0x10
/* Which buttons to display. */ /* Which buttons to display. */
#define ESD_BTN_OK 0 #define ESD_BTN_OK 0x01
#define ESD_BTN_CANCEL 1 #define ESD_BTN_CANCEL 0x02
#define ESD_BTN_YES 0x04
#define ESD_BTN_NO 0x08
/* show a simple dialog */
#if __GNUC__ >= 2 #if __GNUC__ >= 2
void simple_dialog(gint, gint *, gchar *, ...) extern gpointer simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...)
__attribute__((format (printf, 3, 4))); __attribute__((format (printf, 3, 4)));
#else #else
void simple_dialog(gint, gint *, gchar *, ...); extern gpointer simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...);
#endif #endif
/* callback function type */
typedef void (* simple_dialog_cb_t) (gpointer dialog, gint btn, gpointer data);
/* set the callback function, which has to be called when a button was pressed */
extern void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */