Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

mlview-file-selection.c

Go to the documentation of this file.
00001 #include "mlview-file-selection.h"
00002 
00003 
00004 typedef struct _MlViewFileSelectionRunInfo MlViewFileSelectionRunInfo ;
00005 
00006 /*I copied this struct from the GnomeDialogRunInfo in libgnomeui*/
00007 struct _MlViewFileSelectionRunInfo{
00008         enum MLVIEW_SELECTED_BUTTON clicked_button ; /*The number of the button clicked*/
00009         gint close_id ; /*the signal connection id of the widget close callback*/
00010         gint ok_clicked_id ; /*the signal connection id of the "ok button clicked" callback*/
00011         gint cancel_clicked_id ;
00012         gint destroy_id ; /*the signal connection id of the widget "destroy" callback*/
00013         gboolean is_destroyed ;
00014         gboolean is_closed ;
00015         GMainLoop * mainloop ; /*The id of the event mainloop*/
00016 } ;
00017 
00018 static void mlview_file_selection_class_init (MlViewFileSelectionClass *a_klass) ;
00019 static void mlview_file_selection_init() ;
00020 static void mlview_file_selection_destroy(GtkObject * a_object) ;
00021 static void mlview_file_selection_ok_clicked_callback(GtkButton *a_button, MlViewFileSelectionRunInfo * a_info) ;
00022 static void mlview_file_selection_cancel_clicked_callback(GtkButton *a_button, MlViewFileSelectionRunInfo * a_info) ;
00023 static void mlview_file_selection_closed_callback(GtkWidget *a_filesel, 
00024                                                           GdkEvent *a_event ,
00025                                                           MlViewFileSelectionRunInfo * a_run_info) ;
00026 static void mlview_file_selection_disconnect(MlViewFileSelection *a_filesel, MlViewFileSelectionRunInfo * a_run_info) ;
00027 static void mlview_file_selection_event_loop_quit(MlViewFileSelectionRunInfo * a_user_data) ;
00028 static void mlview_file_selection_mark_destroy(GtkWidget *a_filesel, 
00029                                                GdkEvent *a_event,
00030                                                MlViewFileSelectionRunInfo * a_run_info) ;
00031 static enum MLVIEW_SELECTED_BUTTON mlview_file_selection_run_real(MlViewFileSelection * a_file_selection,
00032                                                                        gboolean a_close_after) ;
00033 
00034 GtkFileSelectionClass * parent_class=NULL ;
00035 
00036 /*===========================================
00037  *private methods.
00038  *==========================================*/
00039 
00040 static void
00041 mlview_file_selection_class_init(MlViewFileSelectionClass *a_klass)
00042 {
00043         GtkObjectClass *object_class=GTK_OBJECT_CLASS(a_klass) ;
00044         parent_class = gtk_type_class(GTK_TYPE_FILE_SELECTION) ;
00045         object_class->destroy = mlview_file_selection_destroy ; /*overload the destroy method of GtkObject*/
00046 }
00047 
00048 
00049 static void
00050 mlview_file_selection_init(MlViewFileSelection *a_file_sel)
00051 {
00052         g_assert(a_file_sel != NULL) ;
00053         g_assert(MLVIEW_IS_FILE_SELECTION(a_file_sel)) ;
00054         /*we do not have anything to do for the moment ...*/
00055 }
00056 
00057 static void 
00058 mlview_file_selection_ok_clicked_callback(GtkButton *a_button, MlViewFileSelectionRunInfo * a_info)
00059 {
00060         g_return_if_fail(a_button != NULL) ;
00061         g_return_if_fail(a_info != NULL) ;
00062         g_return_if_fail(GTK_IS_BUTTON(a_button)) ;
00063         
00064         a_info->clicked_button = OK_BUTTON ;
00065         mlview_file_selection_event_loop_quit(a_info) ;
00066 }
00067 
00068 
00069 static void 
00070 mlview_file_selection_cancel_clicked_callback(GtkButton *a_button, MlViewFileSelectionRunInfo * a_info)
00071 {
00072         g_return_if_fail(a_button != NULL) ;
00073         g_return_if_fail(a_info != NULL) ;
00074         g_return_if_fail(GTK_IS_BUTTON(a_button)) ;
00075 
00076         a_info->clicked_button = CANCEL_BUTTON ;
00077         mlview_file_selection_event_loop_quit(a_info) ;
00078 }
00079 
00080 static void 
00081 mlview_file_selection_closed_callback(GtkWidget *a_filesel, 
00082                                                  GdkEvent * a_event,
00083                                                  MlViewFileSelectionRunInfo * a_run_info)
00084 {
00085         g_return_if_fail(a_run_info != NULL) ;
00086         a_run_info->clicked_button = WINDOW_CLOSED ;
00087         mlview_file_selection_event_loop_quit(a_run_info) ;
00088 }
00089 
00090 static void 
00091 mlview_file_selection_mark_destroy(GtkWidget *a_filesel, 
00092                                    GdkEvent *a_event,
00093                                    MlViewFileSelectionRunInfo * a_run_info)
00094 {
00095         g_return_if_fail(a_run_info != NULL) ;
00096         a_run_info->clicked_button = 0 ;
00097         mlview_file_selection_event_loop_quit(a_run_info) ;
00098 }
00099 
00100 
00101 static void 
00102 mlview_file_selection_event_loop_quit(MlViewFileSelectionRunInfo * a_run_info)
00103 {
00104         g_return_if_fail(a_run_info) ;
00105         if (a_run_info->mainloop && g_main_is_running(a_run_info->mainloop)) {
00106                 g_main_quit(a_run_info->mainloop) ;
00107         }
00108 }
00109 
00110 
00111 static void 
00112 mlview_file_selection_disconnect(MlViewFileSelection *a_filesel, MlViewFileSelectionRunInfo * a_run_info)
00113 {
00114         g_return_if_fail(a_filesel != NULL) ;
00115         g_return_if_fail(a_run_info != NULL) ;
00116 
00117         
00118         if (a_run_info->is_destroyed  != TRUE){
00119                 gtk_signal_disconnect(GTK_OBJECT(GTK_FILE_SELECTION(a_filesel)->ok_button),
00120                                       a_run_info->ok_clicked_id) ;
00121                 gtk_signal_disconnect(GTK_OBJECT(GTK_FILE_SELECTION(a_filesel)->cancel_button),
00122                                       a_run_info->cancel_clicked_id) ;
00123                 gtk_signal_disconnect(GTK_OBJECT(a_filesel),
00124                                       a_run_info->close_id) ;
00125                 gtk_signal_disconnect(GTK_OBJECT(a_filesel),
00126                                       a_run_info->destroy_id) ;
00127         }
00128         a_run_info->is_closed = TRUE ;
00129 }
00130 
00131 
00132 /*
00133  *mlview_file_selection_run_real:
00134  *@a_file_selection:
00135  *
00136  */
00137 static enum MLVIEW_SELECTED_BUTTON
00138 mlview_file_selection_run_real (MlViewFileSelection * a_file_selection, gboolean a_close_after)
00139 {
00140         gboolean was_modal ; /*a boolean to store the initial modal state of the file_selection
00141                               *so that we can restore it when we get out.
00142                               */
00143         MlViewFileSelectionRunInfo  run_info ;
00144 
00145         g_return_val_if_fail(a_file_selection, -1) ;
00146         g_return_val_if_fail(MLVIEW_IS_FILE_SELECTION(a_file_selection), -1) ;
00147 
00148         
00149         memset(&run_info, 0, sizeof(MlViewFileSelectionRunInfo)) ;
00150 
00151         /*let's connect to all the helpful signals: clicks on the buttons, delete_event, close, destroy*/
00152         run_info.ok_clicked_id = 
00153                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(a_file_selection)->ok_button), "clicked",
00154                                    GTK_SIGNAL_FUNC(mlview_file_selection_ok_clicked_callback),
00155                                    &run_info) ;
00156 
00157         run_info.cancel_clicked_id = 
00158                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(a_file_selection)->cancel_button), "clicked",
00159                                    GTK_SIGNAL_FUNC(mlview_file_selection_cancel_clicked_callback),
00160                                    &run_info) ;
00161 
00162         run_info.close_id  = 
00163                 gtk_signal_connect(GTK_OBJECT(a_file_selection), "delete_event",
00164                                    GTK_SIGNAL_FUNC(mlview_file_selection_closed_callback),
00165                                    &run_info) ;
00166 
00167         run_info.destroy_id = 
00168                 gtk_signal_connect(GTK_OBJECT(a_file_selection), "destroy",
00169                                    GTK_SIGNAL_FUNC(mlview_file_selection_mark_destroy),
00170                                    &run_info) ;
00171         
00172         was_modal = GTK_WINDOW(a_file_selection)->modal ;
00173         /*we have to set the file selection as modal*/
00174         if (!was_modal)
00175                 gtk_window_set_modal(GTK_WINDOW(a_file_selection), TRUE) ;
00176 
00177         if ( ! GTK_WIDGET_VISIBLE(GTK_WIDGET(a_file_selection)) )
00178         gtk_widget_show_all(GTK_WIDGET(a_file_selection)) ;
00179 
00180         run_info.mainloop = g_main_new(FALSE) ;
00181         g_main_run(run_info.mainloop) ;
00182 
00183         /*let's disconnect to all the signals we connected to*/
00184         mlview_file_selection_disconnect(a_file_selection,&run_info) ;
00185         if (!run_info.is_destroyed){
00186                 if (!was_modal) /*file selection was not initially modal let's restore its state*/
00187                         gtk_window_set_modal(GTK_WINDOW(a_file_selection), FALSE) ;
00188         }
00189         if (run_info.mainloop)
00190                 g_main_destroy(run_info.mainloop) ;
00191         
00192         if ( a_close_after == TRUE )
00193                 gtk_widget_hide(GTK_WIDGET(a_file_selection)) ;
00194         return run_info.clicked_button ;
00195 }
00196 
00197 /*
00198  *Destroys the mlview_file_selection.
00199  *@a_object: the file selection to destroy.
00200  */
00201 static void 
00202 mlview_file_selection_destroy(GtkObject * a_object)
00203 {
00204         g_return_if_fail(a_object != NULL) ;
00205         g_return_if_fail(MLVIEW_IS_FILE_SELECTION(a_object)) ;
00206         
00207         if (GTK_OBJECT_CLASS(parent_class)->destroy ){
00208                 (GTK_OBJECT_CLASS(parent_class)->destroy)(a_object) ;
00209         }
00210 }
00211 
00212 /*===========================================
00213  *Public methods.
00214  *==========================================*/
00215 guint
00216 mlview_file_selection_get_type (void)
00217 {
00218         static guint mlview_file_selection_type = 0 ;
00219         if(!mlview_file_selection_type){
00220                 static const GtkTypeInfo mlview_file_selection_type_info =
00221                 {
00222                         "MlViewFileSelection",
00223                         sizeof(MlViewFileSelection),
00224                         sizeof(MlViewFileSelectionClass),
00225                         (GtkClassInitFunc) mlview_file_selection_class_init,
00226                         (GtkObjectInitFunc) mlview_file_selection_init,
00227                         NULL,
00228                         NULL,
00229                         (GtkClassInitFunc) NULL
00230                 };
00231                 mlview_file_selection_type = 
00232                         gtk_type_unique(gtk_file_selection_get_type(), &mlview_file_selection_type_info) ;
00233         }
00234         return mlview_file_selection_type ;
00235 }
00236 
00237 
00242 GtkWidget *
00243 mlview_file_selection_new(void)
00244 {
00245         MlViewFileSelection * filesel;
00246         filesel = gtk_type_new(MLVIEW_TYPE_FILE_SELECTION) ;
00247         g_assert(MLVIEW_IS_FILE_SELECTION(filesel)) ;
00248         return GTK_WIDGET(filesel) ;
00249 }
00250 
00251 
00257 gint
00258 mlview_file_selection_run(MlViewFileSelection * a_file_selection, gboolean a_close_after)
00259 {
00260         g_return_val_if_fail(a_file_selection != NULL, -2) ;
00261         g_return_val_if_fail(MLVIEW_IS_FILE_SELECTION(a_file_selection), -2) ;
00262         return mlview_file_selection_run_real(a_file_selection, a_close_after) ;
00263 }

Generated on Sat Jul 6 09:57:32 2002 for Gnome-MlView by doxygen1.2.16