gattrib

x_fileselect.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * gattrib -- gEDA component and net attribute manipulation using spreadsheet.
00003  * Copyright (C) 2003-2010 Stuart D. Brorson.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00018  */
00019 /*------------------------------------------------------------------*/
00028 #ifdef HAVE_CONFIG_H
00029 #include "config.h"
00030 #endif
00031 
00032 /*------------------------------------------------------------------
00033  * Includes required to run graphical widgets.
00034  *------------------------------------------------------------------*/
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <gtk/gtk.h>
00038 #include <gdk/gdk.h>
00039 #include <gdk/gdkkeysyms.h>
00040 
00041 #include <glib.h>
00042 #include <glib-object.h>
00043 
00044 #include <sys/types.h>
00045 
00046 #ifdef HAVE_SYS_PARAM_H
00047 #include <sys/param.h>
00048 #endif
00049 
00050 #include <sys/stat.h>
00051 
00052 #ifdef HAVE_UNISTD_H
00053 #include <unistd.h>
00054 #endif
00055 
00056 #ifdef HAVE_STRING_H
00057 #include <string.h>
00058 #endif
00059 
00060 
00061 /*------------------------------------------------------------------
00062  * Gattrib specific includes
00063  *------------------------------------------------------------------*/
00064 #include <config.h>
00065 #include <libgeda/libgeda.h>       /* geda library fcns  */
00066 #include "../include/struct.h"     /* typdef and struct declarations */
00067 #include "../include/prototype.h"  /* function prototypes */
00068 #include "../include/globals.h"
00069 
00070 #ifdef HAVE_LIBDMALLOC
00071 #include <dmalloc.h>
00072 #endif
00073 
00074 
00075 /* ----- x_fileselect stuff begins here ----- */
00076 
00077 /*------------------------------------------------------------------*/
00083 static void
00084 x_fileselect_setup_filechooser_filters (GtkFileChooser *filechooser)
00085 {
00086   GtkFileFilter *filter;
00087   
00088   /* file filter for schematic files (*.sch) */
00089   filter = gtk_file_filter_new ();
00090   gtk_file_filter_set_name (filter, "Schematics");
00091   gtk_file_filter_add_pattern (filter, "*.sch");
00092   gtk_file_chooser_add_filter (filechooser, filter);
00093   /* file filter for symbol files (*.sym) */
00094   filter = gtk_file_filter_new ();
00095   gtk_file_filter_set_name (filter, "Symbols");
00096   gtk_file_filter_add_pattern (filter, "*.sym");
00097   gtk_file_chooser_add_filter (filechooser, filter);
00098   /* file filter for both symbol and schematic files (*.sym+*.sch) */
00099   filter = gtk_file_filter_new ();
00100   gtk_file_filter_set_name (filter, "Schematics and symbols");
00101   gtk_file_filter_add_pattern (filter, "*.sym");
00102   gtk_file_filter_add_pattern (filter, "*.sch");
00103   gtk_file_chooser_add_filter (filechooser, filter);
00104   /* file filter that match any file */
00105   filter = gtk_file_filter_new ();
00106   gtk_file_filter_set_name (filter, "All files");
00107   gtk_file_filter_add_pattern (filter, "*");
00108   gtk_file_chooser_add_filter (filechooser, filter);
00109 
00110 }
00111 
00123 gboolean
00124 x_fileselect_load_files (GSList *filenames)
00125 {
00126   GList *iter;
00127   PAGE *p_local;
00128   GSList *filename;
00129 
00130   /* iterate over selected files */
00131   for (filename = filenames;
00132        filename != NULL;
00133        filename = g_slist_next (filename)) {
00134     gchar *string = (gchar*)filename->data;
00135     
00136     if (!quiet_mode) {
00137       s_log_message("Loading file [%s]\n", string);
00138     }
00139 
00140     s_page_goto (pr_current, s_page_new (pr_current, string));
00141 
00142     if(s_toplevel_read_page(pr_current, string) == 0) {
00143        fprintf(stderr, "Couldn't load schematic [%s]\n", string);
00144        return FALSE;
00145     }
00146 
00147     /* Now add all items found to the master lists */
00148     s_sheet_data_add_master_comp_list_items (s_page_objects (pr_current->page_current));
00149     s_sheet_data_add_master_comp_attrib_list_items (s_page_objects (pr_current->page_current));
00150 #if 0
00151     /* Note that this must be changed.  We need to input the entire project
00152      * before doing anything with the nets because we need to first
00153      * determine where they are all connected!   */
00154     s_sheet_data_add_master_net_list_items (pr_current->page_current->object_list);
00155     s_sheet_data_add_master_net_attrib_list_items (pr_current->page_current->object_list);
00156 #endif
00157     
00158     s_sheet_data_add_master_pin_list_items (s_page_objects (pr_current->page_current));
00159     s_sheet_data_add_master_pin_attrib_list_items (s_page_objects (pr_current->page_current));
00160   }     /* end of loop over files     */
00161   
00162 
00163   /* ---------- Sort the master lists  ---------- */
00164   s_string_list_sort_master_comp_list();
00165   s_string_list_sort_master_comp_attrib_list();
00166 
00167 #if 0
00168   /* Note that this must be changed.  We need to input the entire project
00169    * before doing anything with the nets because we need to first
00170    * determine where they are all connected!   */
00171   s_string_list_sort_master_net_list();
00172   s_string_list_sort_master_net_attrib_list();
00173 #endif
00174 
00175   s_string_list_sort_master_pin_list();
00176   s_string_list_sort_master_pin_attrib_list();
00177 
00178   /* ---------- Create and load the tables  ---------- */
00179   sheet_head->component_table = s_table_new(sheet_head->comp_count, sheet_head->comp_attrib_count);
00180   sheet_head->net_table = s_table_new(sheet_head->net_count, sheet_head->net_attrib_count);
00181   sheet_head->pin_table = s_table_new(sheet_head->pin_count, sheet_head->pin_attrib_count);
00182 
00183   /* must iterate over all pages in design */
00184   for ( iter = geda_list_get_glist( pr_current->pages );
00185         iter != NULL;
00186         iter = g_list_next( iter ) ) {
00187     p_local = (PAGE *)iter->data;
00188 
00189     /* only traverse pages which are toplevel */
00190     if (p_local->page_control == 0) {
00191       /* adds all components from page to comp_table */
00192       s_table_add_toplevel_comp_items_to_comp_table (s_page_objects (p_local));
00193 #if 0
00194       /* Note that this must be changed.  We need to input the entire project
00195        * before doing anything with the nets because we need to first
00196        * determine where they are all connected!   */
00197 
00198       /* adds all nets from page to net_table */
00199       s_table_add_toplevel_net_items_to_net_table(p_local->object_head);
00200 #endif
00201 
00202       /* adds all pins from page to pin_table */
00203       s_table_add_toplevel_pin_items_to_pin_table (s_page_objects (p_local));
00204     }
00205   } /* for loop over pages */
00206 
00207   /* -------------- update windows --------------- */
00208   x_window_add_items();    /* This updates the top level stuff,
00209                             * and then calls another fcn to update
00210                             * the GtkSheet itself.  */
00211 
00212 
00213   /* ---------- Now verify correctness of entire design.  ---------- */
00214   s_toplevel_verify_design(pr_current);  /* pr_current is a global */
00215 
00216   return TRUE;
00217 }
00218 
00227 GSList *
00228 x_fileselect_open (void)
00229 {
00230   GtkWidget *dialog;
00231   GSList *filenames = NULL;
00232 
00233   dialog = gtk_file_chooser_dialog_new ("Open...",
00234                                         GTK_WINDOW(window),
00235                                         GTK_FILE_CHOOSER_ACTION_OPEN,
00236                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00237                                         GTK_STOCK_OPEN,   GTK_RESPONSE_ACCEPT,
00238                                         NULL);
00239 
00240   /* Set the alternative button order (ok, cancel, help) for other systems */
00241   gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog),
00242                       GTK_RESPONSE_ACCEPT,
00243                       GTK_RESPONSE_CANCEL,
00244                       -1);
00245   
00246   g_object_set (dialog,
00247                 /* GtkFileChooser */
00248                 "select-multiple", TRUE,
00249                 NULL);
00250   /* add file filters to dialog */
00251   x_fileselect_setup_filechooser_filters (GTK_FILE_CHOOSER (dialog));
00252   gtk_widget_show (dialog);
00253 
00254   if(gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
00255      filenames = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog));
00256   
00257   gtk_widget_destroy (dialog);
00258   return filenames;
00259 }
00260 
00261 /*------------------------------------------------------------------*/
00273 void
00274 x_fileselect_save (void)
00275 {
00276   GtkWidget *dialog;
00277 
00278   dialog = gtk_file_chooser_dialog_new ("Save as...",
00279                                         GTK_WINDOW(window),
00280                                         GTK_FILE_CHOOSER_ACTION_SAVE,
00281                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00282                                         GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
00283                                         NULL);
00284 
00285   /* Set the alternative button order (ok, cancel, help) for other systems */
00286   gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog),
00287                       GTK_RESPONSE_ACCEPT,
00288                       GTK_RESPONSE_CANCEL,
00289                       -1);
00290   
00291   g_object_set (dialog,
00292                 /* GtkFileChooser */
00293                 "select-multiple", FALSE,
00294                 /* only in GTK 2.8 */
00295                 /* "do-overwrite-confirmation", TRUE, */
00296                 NULL);
00297   /* add file filters to dialog */
00298   x_fileselect_setup_filechooser_filters (GTK_FILE_CHOOSER (dialog));
00299   gtk_widget_show (dialog);
00300   if (gtk_dialog_run ((GtkDialog*)dialog) == GTK_RESPONSE_ACCEPT) {
00301     gchar *filename =
00302       gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
00303 
00304     /* try saving current page of toplevel to file filename */
00305     if (filename != NULL &&
00306         f_save (pr_current, pr_current->page_current, filename, NULL)) {
00307       s_log_message ("Saved As [%s]\n", filename);
00308 
00309       /* replace page filename with new one, do not free filename */
00310       g_free (pr_current->page_current->page_filename);
00311       pr_current->page_current->page_filename = filename;
00312 
00313       /* reset the changed flag of current page*/
00314       pr_current->page_current->CHANGED = 0;
00315 
00316     } else {
00317       /* report error in log and status bar */
00318       s_log_message ("Could NOT save [%s]\n",
00319                      pr_current->page_current->page_filename);
00320 
00321       g_free (filename);
00322 
00323     }
00324   }
00325   gtk_widget_destroy (dialog);
00326 
00327 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines