Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

dialogs.c

Go to the documentation of this file.
00001 
00005 /* GtkSQL -- an interactive graphical query tool for PostgreSQL
00006  * Copyright (C) 1998-2003  Lionel ULMER, Darryl Luff
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 #include "../config.h"
00024 
00025 #include <gtk/gtk.h>
00026 
00027 #include "appconfig.h"
00028 #include "dialogs.h"
00029 #include "Pics/warning.xpm"
00030 #include "Pics/question.xpm"
00031 
00033 typedef struct _call_data {
00034     GtkWidget *dialog;                      
00035     void (*user_ok_callback) (void *);      
00036     void (*user_cancel_callback) (void *);  
00037     void *user_data;                        
00038 } CallData;
00039 
00041 static void dialog_ok_callback(GtkWidget * widget, gpointer data)
00042 {
00043     CallData *data_rec = (CallData *) data;
00044 
00045     if (data_rec->user_ok_callback != NULL)
00046         data_rec->user_ok_callback(data_rec->user_data);
00047 
00048     gtk_widget_destroy(GTK_WIDGET(data_rec->dialog));
00049 }
00050 
00056 GtkWidget *create_warning_dialog(const char *title, const char *text) {
00057     return create_standard_dialog(title, text, DLGTYPE_WARNING, 
00058         NULL, "Ok", NULL, NULL, NULL, NULL);
00059 }
00060     
00062 static void dialog_cancel_callback(GtkWidget * widget, gpointer data)
00063 {
00064     CallData *data_rec = (CallData *) data;
00065 
00066     if (data_rec->user_cancel_callback != NULL)
00067         data_rec->user_cancel_callback(data_rec->user_data);
00068 
00069     gtk_widget_destroy(GTK_WIDGET(data_rec->dialog));
00070 }
00071 
00084 GtkWidget *create_standard_dialog(const char *title, const char *text,
00085                                   const DlgType predefined_type, char **pixmap,
00086                                   const char *ok_text, void (*ok_callback) (void *),
00087                                   const char *cancel_text,
00088                                   void (*cancel_callback) (void *),
00089                                   void *callback_data)
00090 {
00091     GtkWidget *dialog, *table, *label, *ok_button, *cancel_button;
00092     CallData *data_send;
00093     GtkWidget *lbl;
00094 
00095     switch (predefined_type) {
00096     case DLGTYPE_WARNING:
00097         pixmap = warning_xpm;
00098         if (title == NULL)
00099             title = "Warning";
00100         break;
00101 
00102     case DLGTYPE_QUESTION:
00103         pixmap = question_xpm;
00104         if (title == NULL)
00105             title = "Question";
00106         break;
00107 
00108     default:
00109         break;
00110     }
00111 
00112     if (title == NULL)
00113         title = "Message";
00114 
00115     if (text == NULL)
00116         text = "";
00117 
00118     /* The dialog */
00119     dialog = gtk_dialog_new();
00120     gtk_window_set_title(GTK_WINDOW(dialog), title);
00121     gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
00122     gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
00123     gtk_grab_add(dialog);
00124     gtk_widget_realize(dialog);
00125 
00126     /* Now, the top half (pixmap + text) */
00127     table = gtk_table_new(2, 1, FALSE);
00128     gtk_table_set_row_spacings(GTK_TABLE(table), 5);
00129     gtk_table_set_col_spacings(GTK_TABLE(table), 5);
00130 
00131     if (pixmap != NULL) {
00132         GdkBitmap *mask;
00133         GdkPixmap *gdkpix;
00134         GtkStyle *style;
00135         GtkWidget *gtkpix;
00136 
00137         style = gtk_widget_get_style(dialog);
00138         gdkpix = gdk_pixmap_create_from_xpm_d(dialog->window,
00139                                               &mask,
00140                                               &style->bg[GTK_STATE_NORMAL],
00141                                               pixmap);
00142         gtkpix = gtk_pixmap_new(gdkpix, mask);
00143 
00144         gtk_table_attach_defaults(GTK_TABLE(table), gtkpix, 0, 1, 0, 1);
00145         gtk_widget_show(gtkpix);
00146     }
00147 
00148     label = gtk_label_new(text);
00149     gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
00150     gtk_widget_show(label);
00151 
00152     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE,
00153                        TRUE, 0);
00154     gtk_widget_show(table);
00155 
00156     /* The structure for the callbacks */
00157     data_send = (CallData *) g_malloc(sizeof(CallData));
00158     data_send->dialog = dialog;
00159     data_send->user_data = callback_data;
00160     data_send->user_ok_callback = ok_callback;
00161     data_send->user_cancel_callback = cancel_callback;
00162 
00163     /* The bottom half */
00164     if (ok_text != NULL) {
00165         lbl = gtk_label_new(ok_text);
00166         gtk_misc_set_padding(GTK_MISC(lbl), 4, 0);
00167         gtk_widget_show(lbl);
00168         ok_button = gtk_button_new();
00169         gtk_container_add(GTK_CONTAINER(ok_button), lbl);
00170         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
00171                            GTK_SIGNAL_FUNC(dialog_ok_callback), data_send);
00172         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
00173                            ok_button, FALSE, FALSE, 0);
00174         GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
00175         gtk_widget_grab_default(ok_button);
00176         gtk_widget_show(ok_button);
00177     }
00178 
00179     if (cancel_text != NULL) {
00180         lbl = gtk_label_new(cancel_text);
00181         gtk_misc_set_padding(GTK_MISC(lbl), 4, 0);
00182         gtk_widget_show(lbl);
00183         cancel_button = gtk_button_new();
00184         gtk_container_add(GTK_CONTAINER(cancel_button), lbl);
00185         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
00186                            GTK_SIGNAL_FUNC(dialog_cancel_callback),
00187                            data_send);
00188         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
00189                            cancel_button, FALSE, FALSE, 0);
00190         gtk_widget_show(cancel_button);
00191     }
00192 
00193     return dialog;
00194 }
00195 
00196 
00198 void display_error(char *title, char *message)
00199 {
00200     GtkWidget *dialog =
00201         create_standard_dialog(title, message, 1, NULL, "Ok", NULL, NULL,
00202                                NULL, NULL);
00203     gtk_widget_show(dialog);
00204 }
00205 
00207 int openFileDialogDisplayed = 0;
00208 
00210 GtkWidget *openFileDialog = NULL;
00211 
00214 static void file_open_dialog_ok_callback(GtkWidget * widget, gpointer data)
00215 {
00216     gchar *filename =
00217         gtk_file_selection_get_filename(GTK_FILE_SELECTION(openFileDialog));
00218 
00219     cfg_set_current_dir(filename);
00220     if (data) {
00221         gtk_entry_set_text(GTK_ENTRY(data), filename);
00222     }
00223     openFileDialogDisplayed = 0;
00224 }
00225 
00227 void file_open_dialog(GtkWidget * txt)
00228 {
00229     if (openFileDialogDisplayed) {
00230         /* Should never happen */
00231     }
00232     else {
00233         openFileDialog = gtk_file_selection_new("Select file to open.");
00234         gtk_file_selection_set_filename(GTK_FILE_SELECTION(openFileDialog),
00235                                         cfg_get_current_dir());
00236         gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION
00237                                                (openFileDialog));
00238 
00239         /* Ensure that the dialog box is destroyed when the user clicks a
00240          * button. */
00241         gtk_signal_connect(GTK_OBJECT
00242                            (GTK_FILE_SELECTION(openFileDialog)->ok_button),
00243                            "clicked",
00244                            GTK_SIGNAL_FUNC(file_open_dialog_ok_callback),
00245                            txt);
00246         gtk_signal_connect_object(GTK_OBJECT
00247                                   (GTK_FILE_SELECTION(openFileDialog)->
00248                                    ok_button), "clicked",
00249                                   GTK_SIGNAL_FUNC(gtk_widget_destroy),
00250                                   (gpointer) openFileDialog);
00251         gtk_signal_connect_object(GTK_OBJECT
00252                                   (GTK_FILE_SELECTION(openFileDialog)->
00253                                    cancel_button), "clicked",
00254                                   GTK_SIGNAL_FUNC(gtk_widget_destroy),
00255                                   (gpointer) openFileDialog);
00256 
00257         /* Display that dialog */
00258         gtk_grab_add(openFileDialog);
00259         gtk_widget_show(openFileDialog);
00260         openFileDialogDisplayed = 1;
00261     }
00262 }
00263 
00269 GtkWidget *show_warning_dialog(char *title, char *text) {
00270     GtkWidget *dlg = create_standard_dialog(title, text, DLGTYPE_WARNING, NULL, "Ok", NULL, NULL, NULL, NULL);
00271     if (dlg != NULL)
00272         gtk_widget_show(dlg);
00273 
00274     return dlg;
00275 }

Generated on Sun May 9 19:19:16 2004 for GtkSQL by doxygen1.2.18