gschem

x_fileselect.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * gschem - gEDA Schematic Capture
00003  * Copyright (C) 1998-2010 Ales Hvezda
00004  * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 #include <config.h>
00021 
00022 #include "gschem.h"
00023 
00024 #ifdef HAVE_LIBDMALLOC
00025 #include <dmalloc.h>
00026 #endif
00027 
00028 
00035 static void
00036 x_fileselect_setup_filechooser_filters (GtkFileChooser *filechooser)
00037 {
00038   GtkFileFilter *filter;
00039   
00040   /* file filter for schematic files (*.sch) */
00041   filter = gtk_file_filter_new ();
00042   gtk_file_filter_set_name (filter, _("Schematics"));
00043   gtk_file_filter_add_pattern (filter, "*.sch");
00044   gtk_file_chooser_add_filter (filechooser, filter);
00045   /* file filter for symbol files (*.sym) */
00046   filter = gtk_file_filter_new ();
00047   gtk_file_filter_set_name (filter, _("Symbols"));
00048   gtk_file_filter_add_pattern (filter, "*.sym");
00049   gtk_file_chooser_add_filter (filechooser, filter);
00050   /* file filter for both symbol and schematic files (*.sym+*.sch) */
00051   filter = gtk_file_filter_new ();
00052   gtk_file_filter_set_name (filter, _("Schematics and symbols"));
00053   gtk_file_filter_add_pattern (filter, "*.sym");
00054   gtk_file_filter_add_pattern (filter, "*.sch");
00055   gtk_file_chooser_add_filter (filechooser, filter);
00056   /* file filter that match any file */
00057   filter = gtk_file_filter_new ();
00058   gtk_file_filter_set_name (filter, _("All files"));
00059   gtk_file_filter_add_pattern (filter, "*");
00060   gtk_file_chooser_add_filter (filechooser, filter);
00061 
00062 }
00063 
00075 static void
00076 x_fileselect_callback_update_preview (GtkFileChooser *chooser,
00077                                       gpointer user_data)
00078 {
00079   Preview *preview = PREVIEW (user_data);
00080   gchar *filename, *preview_filename = NULL;
00081 
00082   filename = gtk_file_chooser_get_preview_filename (chooser);
00083   if (filename != NULL &&
00084       !g_file_test (filename, G_FILE_TEST_IS_DIR)) {
00085     preview_filename = filename;
00086   }
00087 
00088   /* update preview */
00089   g_object_set (preview,
00090                 "filename", preview_filename,
00091                 "active", (preview_filename != NULL),
00092                 NULL);
00093 
00094   g_free (filename);
00095 }
00096 
00116 static void
00117 x_fileselect_add_preview (GtkFileChooser *filechooser)
00118 {
00119   GtkWidget *alignment, *frame, *preview;
00120 
00121   frame = GTK_WIDGET (g_object_new (GTK_TYPE_FRAME,
00122                                     "label", _("Preview"),
00123                                     NULL));
00124   alignment = GTK_WIDGET (g_object_new (GTK_TYPE_ALIGNMENT,
00125                                         "right-padding", 5,
00126                                         "left-padding", 5,
00127                                         "xscale", 0.0,
00128                                         "yscale", 0.0,
00129                                         "xalign", 0.5,
00130                                         "yalign", 0.5,
00131                                         NULL));
00132   preview = GTK_WIDGET (g_object_new (TYPE_PREVIEW,
00133                                       "active", TRUE,
00134                                       NULL));
00135   gtk_container_add (GTK_CONTAINER (alignment), preview);
00136   gtk_container_add (GTK_CONTAINER (frame), alignment);
00137   gtk_widget_show_all (frame);
00138 
00139   g_object_set (filechooser,
00140                 /* GtkFileChooser */
00141                 "use-preview-label", FALSE,
00142                 "preview-widget", frame,
00143                 NULL);
00144   
00145   /* connect callback to update preview */
00146   g_signal_connect (filechooser,
00147                     "update-preview",
00148                     G_CALLBACK (x_fileselect_callback_update_preview),
00149                     preview);
00150   
00151 }
00152 
00165 void
00166 x_fileselect_open(GSCHEM_TOPLEVEL *w_current)
00167 {
00168   PAGE *page = NULL;
00169   GtkWidget *dialog;
00170 
00171   dialog = gtk_file_chooser_dialog_new (_("Open..."),
00172                                         GTK_WINDOW(w_current->main_window),
00173                                         GTK_FILE_CHOOSER_ACTION_OPEN,
00174                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00175                                         GTK_STOCK_OPEN,   GTK_RESPONSE_ACCEPT,
00176                                         NULL);
00177 
00178   /* Set the alternative button order (ok, cancel, help) for other systems */
00179   gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog),
00180                       GTK_RESPONSE_ACCEPT,
00181                       GTK_RESPONSE_CANCEL,
00182                       -1);
00183 
00184   x_fileselect_add_preview (GTK_FILE_CHOOSER (dialog));  
00185   g_object_set (dialog,
00186                 /* GtkFileChooser */
00187                 "select-multiple", TRUE,
00188                 NULL);
00189   /* add file filters to dialog */
00190   x_fileselect_setup_filechooser_filters (GTK_FILE_CHOOSER (dialog));
00191   gtk_widget_show (dialog);
00192   if (gtk_dialog_run ((GtkDialog*)dialog) == GTK_RESPONSE_ACCEPT) {
00193     GSList *tmp, *filenames =
00194       gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog));
00195 
00196     /* open each file */ 
00197     for (tmp = filenames; tmp != NULL;tmp = g_slist_next (tmp)) {
00198       page = x_window_open_page (w_current, (gchar*)tmp->data);
00199     }
00200     /* Switch to the last page opened */
00201     if ( page != NULL )
00202       x_window_set_current_page (w_current, page);
00203 
00204     /* free the list of filenames */
00205     g_slist_foreach (filenames, (GFunc)g_free, NULL);
00206     g_slist_free (filenames);
00207   }
00208   gtk_widget_destroy (dialog);
00209 
00210 }
00211 
00225 void
00226 x_fileselect_save (GSCHEM_TOPLEVEL *w_current)
00227 {
00228   TOPLEVEL *toplevel = w_current->toplevel;
00229   GtkWidget *dialog;
00230 
00231   dialog = gtk_file_chooser_dialog_new (_("Save as..."),
00232                                         GTK_WINDOW(w_current->main_window),
00233                                         GTK_FILE_CHOOSER_ACTION_SAVE,
00234                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00235                                         GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
00236                                         NULL);
00237 
00238   /* Set the alternative button order (ok, cancel, help) for other systems */
00239   gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog),
00240                       GTK_RESPONSE_ACCEPT,
00241                       GTK_RESPONSE_CANCEL,
00242                       -1);
00243 
00244   /* set default response signal. This is usually triggered by the 
00245      "Return" key */
00246   gtk_dialog_set_default_response(GTK_DIALOG(dialog),
00247                   GTK_RESPONSE_ACCEPT);
00248 
00249   g_object_set (dialog,
00250                 /* GtkFileChooser */
00251                 "select-multiple", FALSE,
00252                 /* only in GTK 2.8 */
00253                 /* "do-overwrite-confirmation", TRUE, */
00254                 NULL);
00255   /* add file filters to dialog */
00256   x_fileselect_setup_filechooser_filters (GTK_FILE_CHOOSER (dialog));
00257   /* set the current filename or directory name if new document */
00258   if ((toplevel->page_current->page_filename != NULL) &&
00259       g_file_test (toplevel->page_current->page_filename,
00260                    G_FILE_TEST_EXISTS)) {
00261     gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog),
00262                                    toplevel->page_current->page_filename);
00263   } else {
00264     gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog),
00265                                        "untitled.sch");
00266   }
00267 
00268   gtk_dialog_set_default_response(GTK_DIALOG(dialog),
00269                   GTK_RESPONSE_ACCEPT);
00270   gtk_widget_show (dialog);
00271   if (gtk_dialog_run ((GtkDialog*)dialog) == GTK_RESPONSE_ACCEPT) {
00272     gchar *filename =
00273       gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
00274 
00275     /* If the file already exists, display a dialog box to check if
00276        the user really wants to overwrite it. */
00277     if ((filename != NULL) && g_file_test (filename, G_FILE_TEST_EXISTS)) {
00278       GtkWidget *checkdialog = 
00279         gtk_message_dialog_new (GTK_WINDOW(dialog),
00280                                 (GTK_DIALOG_MODAL | 
00281                                  GTK_DIALOG_DESTROY_WITH_PARENT),
00282                                 GTK_MESSAGE_QUESTION,
00283                                 GTK_BUTTONS_YES_NO,
00284                                 _("The selected file `%s' already exists.\n\n"
00285                                   "Would you like to overwrite it?"),
00286                                 filename);
00287       gtk_window_set_title (GTK_WINDOW (checkdialog), _("Overwrite file?"));
00288       if (gtk_dialog_run (GTK_DIALOG (checkdialog)) != GTK_RESPONSE_YES) {
00289         s_log_message (_("Save cancelled on user request\n"));
00290         g_free (filename);
00291         filename = NULL;
00292       }
00293       gtk_widget_destroy (checkdialog);
00294     }
00295     /* try saving current page of toplevel to file filename */
00296     if (filename != NULL) {
00297       x_window_save_page (w_current,
00298                           w_current->toplevel->page_current,
00299                           filename);
00300     }
00301 
00302     g_free (filename);
00303   }
00304   gtk_widget_destroy (dialog);
00305 
00306 }
00307 
00320 int x_fileselect_load_backup(void *user_data, GString *message)
00321 {
00322   GtkWidget *dialog;
00323   GSCHEM_TOPLEVEL *w_current = (GSCHEM_TOPLEVEL *) user_data;
00324 
00325   g_string_append(message, "\nIf you load the original file, the backup file will be overwritten in the next autosave timeout and it will be lost.\n\nDo you want to load the backup file?\n");
00326 
00327   dialog = gtk_message_dialog_new (GTK_WINDOW(w_current->main_window),
00328                                    GTK_DIALOG_MODAL,
00329                                    GTK_MESSAGE_QUESTION,
00330                                    GTK_BUTTONS_YES_NO,
00331                                    "%s", message->str);
00332 
00333   /* Set the alternative button order (ok, cancel, help) for other systems */
00334   gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog),
00335                       GTK_RESPONSE_YES,
00336                       GTK_RESPONSE_NO,
00337                       -1);
00338 
00339   gtk_widget_show (dialog);
00340   if (gtk_dialog_run ((GtkDialog*)dialog) == GTK_RESPONSE_YES) {
00341     gtk_widget_destroy(dialog);  
00342     return TRUE;
00343   }
00344   else {
00345     gtk_widget_destroy(dialog);  
00346     return FALSE;
00347   }
00348 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines