|
发表于 2005-5-24 13:18:47
|
显示全部楼层
笨办法,用全局变量
[code:1]
//main.c
#include <gtk/gtk.h>
#include <glib.h>
#include "msgdialog.h"
#include <libintl.h>
void on_button_clicked(GtkWidget *widget,gpointer data)
{
int ret;
ret=my_message_dialog_ok_cancel(dgettext("","\n\n\n\nÄãºÃ\n\n\n\n"),dgettext("","ÄãºÃ"));
printf("ret=%d\n",ret);
}
//test
main()
{
GtkWidget *win;
GtkWidget *button;
gtk_set_locale();
gtk_init(NULL,NULL);
win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
button=gtk_button_new_with_label("test");
gtk_container_add(GTK_CONTAINER(win),button);
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (on_button_clicked),
NULL);
gtk_signal_connect (GTK_OBJECT (win), "destroy",
GTK_SIGNAL_FUNC (gtk_main_quit),
NULL);
gtk_widget_show_all(win);
gtk_main();
}
[/code:1]
[code:1]
// msgdialog.c
//
#include <gtk/gtk.h>
#include <glib.h>
#include "msgdialog.h"
#define MY_MSGDLG_BUTTON_OK 0
#define MY_MSGDLG_BUTTON_OK_AND_CANCEL 1
gint dlg_ret;
GtkWidget *dialog;
static void on_ok_button_clicked(GtkWidget *widget,gpointer data);
static void on_cancel_button_clicked(GtkWidget *widget,gpointer data);
static void on_dialog_destroy(GtkWidget *widget,gpointer data);
static gint my_message_dialog(gchar *content,gchar *title,gint button)//button = MY_MSGDLG_BUTTON_OK or MY_MSGDLG_BUTTON_OK_AND_CANCEL
{
GtkWidget *dialog_vbox;
GtkWidget *label;
GtkWidget *dialog_action_area;
GtkWidget *hbuttonbox;
GtkWidget *ok_button;
GtkWidget *cancel_button;
gtk_set_locale();
gtk_init(NULL,NULL);
dialog = gtk_dialog_new ();
gtk_object_set_data (GTK_OBJECT (dialog), "dialog", dialog);
gtk_window_set_title (GTK_WINDOW (dialog), (title));
gtk_window_set_policy (GTK_WINDOW (dialog), TRUE, TRUE, FALSE);
dialog_vbox = GTK_DIALOG (dialog)->vbox;
gtk_object_set_data (GTK_OBJECT (dialog), "dialog_vbox", dialog_vbox);
gtk_widget_show (dialog_vbox);
label = gtk_label_new ((content));
gtk_widget_ref (label);
gtk_object_set_data_full (GTK_OBJECT (dialog), "label", label,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (label);
gtk_box_pack_start (GTK_BOX (dialog_vbox), label, FALSE, FALSE, 0);
dialog_action_area = GTK_DIALOG (dialog)->action_area;
gtk_object_set_data (GTK_OBJECT (dialog), "dialog_action_area", dialog_action_area);
gtk_widget_show (dialog_action_area);
gtk_container_set_border_width (GTK_CONTAINER (dialog_action_area), 10);
hbuttonbox = gtk_hbutton_box_new ();
gtk_widget_ref (hbuttonbox);
gtk_object_set_data_full (GTK_OBJECT (dialog), "hbuttonbox", hbuttonbox,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (hbuttonbox);
gtk_box_pack_start (GTK_BOX (dialog_action_area), hbuttonbox, TRUE, TRUE, 0);
ok_button = gtk_button_new_with_label (("OK"));
gtk_widget_ref (ok_button);
gtk_object_set_data_full (GTK_OBJECT (dialog), "ok_button", ok_button,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (ok_button);
gtk_container_add (GTK_CONTAINER (hbuttonbox), ok_button);
GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
gtk_signal_connect (GTK_OBJECT (ok_button), "clicked",
GTK_SIGNAL_FUNC (on_ok_button_clicked),
NULL);
gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
GTK_SIGNAL_FUNC (on_dialog_destroy),
NULL);
if(button==MY_MSGDLG_BUTTON_OK_AND_CANCEL)
{
cancel_button = gtk_button_new_with_label (("Cancel"));
gtk_widget_ref (cancel_button);
gtk_object_set_data_full (GTK_OBJECT (dialog), "cancel_button", cancel_button,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (cancel_button);
gtk_container_add (GTK_CONTAINER (hbuttonbox), cancel_button);
GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
gtk_signal_connect (GTK_OBJECT (cancel_button), "clicked",
GTK_SIGNAL_FUNC (on_cancel_button_clicked),
NULL);
}
gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER);
gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
gtk_widget_show_all(dialog);
gtk_main();
if(dlg_ret!=0 && dlg_ret!=1)
dlg_ret=1;
return dlg_ret;
}
static void on_ok_button_clicked(GtkWidget *widget,gpointer data)
{
dlg_ret=MY_MSGDLG_OK;
gtk_widget_destroy(dialog);
}
static void on_cancel_button_clicked(GtkWidget *widget,gpointer data)
{
dlg_ret=MY_MSGDLG_CANCEL;
gtk_widget_destroy(dialog);
}
static void on_dialog_destroy(GtkWidget *widget,gpointer data)
{
gtk_main_quit();
}
gint my_message_dialog_ok(gchar *content,gchar *title)
{
return my_message_dialog(content,title,MY_MSGDLG_BUTTON_OK);
}
gint my_message_dialog_ok_cancel(gchar *content,gchar *title)
{
return my_message_dialog(content,title,MY_MSGDLG_BUTTON_OK_AND_CANCEL);
}
[/code:1]
[code:1]
// msgdialog.h
#include <glib.h>
#define MY_MSGDLG_OK 0
#define MY_MSGDLG_CANCEL 1
gint my_message_dialog_ok(gchar *content,gchar *title);
gint my_message_dialog_ok_cancel(gchar *content,gchar *title);
[/code:1] |
|