gattrib

x_window.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 
00020 /*------------------------------------------------------------------*/
00029 #ifdef HAVE_CONFIG_H
00030 #include "config.h"
00031 #endif
00032 
00033 
00034 /*------------------------------------------------------------------
00035  * Includes required to run graphical widgets.
00036  *------------------------------------------------------------------*/
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <gtk/gtk.h>
00040 
00041 #ifdef HAVE_STRING_H
00042 #include <string.h>
00043 #endif
00044 
00045 /*------------------------------------------------------------------
00046  * Gattrib specific includes
00047  *------------------------------------------------------------------*/
00048 #include <libgeda/libgeda.h>       /* geda library fcns  */
00049 #include "../include/struct.h"     /* typdef and struct declarations */
00050 #include "../include/prototype.h"  /* function prototypes */
00051 #include "../include/globals.h"
00052 
00053 #ifdef HAVE_LIBDMALLOC
00054 #include <dmalloc.h>
00055 #endif
00056 
00057 /*------------------------------------------------------------------
00058  * Gattrib specific defines
00059  *------------------------------------------------------------------*/
00060 #define GATTRIB_THEME_ICON_NAME "geda-gattrib"
00061 
00062 static void
00063 x_window_create_menu(GtkWindow *window, GtkWidget **menubar);
00064 
00065 static void
00066 x_window_set_default_icon( void );
00067 
00084 void
00085 x_window_init()
00086 {
00087   GtkWidget *menu_bar;
00088   GtkWidget *main_vbox;
00089 
00090   /* Set default icon */
00091   x_window_set_default_icon();
00092 
00093   /*  window is a global declared in globals.h.  */
00094   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  
00095 
00096   gtk_window_set_title( GTK_WINDOW(window), "gattrib -- gEDA attribute editor"); 
00097   gtk_window_set_default_size(GTK_WINDOW(window), 750, 600);  
00098   
00099   gtk_signal_connect (GTK_OBJECT (window), "delete_event",
00100               GTK_SIGNAL_FUNC (gattrib_really_quit), 0);
00101 
00102   /* -----  Now create main_vbox.  This is a container which organizes child  ----- */  
00103   /* -----  widgets into a vertical column.  ----- */  
00104   main_vbox = gtk_vbox_new(FALSE,1);
00105   gtk_container_set_border_width(GTK_CONTAINER(main_vbox), 1);
00106   gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(main_vbox) );
00107 
00108   /* -----  Now create menu bar  ----- */  
00109   x_window_create_menu(GTK_WINDOW(window), &menu_bar);
00110   gtk_box_pack_start(GTK_BOX (main_vbox), menu_bar, FALSE, TRUE, 0);
00111 
00112   /* -----  Now init notebook widget  ----- */  
00113   notebook = gtk_notebook_new();
00114   gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_BOTTOM);
00115   gtk_box_pack_start(GTK_BOX(main_vbox), notebook, TRUE, TRUE, 0);
00116   
00117   /* -----  Now malloc -- but don't fill out -- space for sheets  ----- */  
00118   /* This basically sets up the overhead for the sheets, as I understand
00119    * it.  The memory for the actual sheet cells is allocated later,
00120    * when gtk_sheet_new is invoked, I think.  */
00121   sheets = g_malloc0(NUM_SHEETS * sizeof(GtkWidget *));
00122 }
00123 
00124 
00125 /*------------------------------------------------------------------
00126  * \brief File Open menu
00127  *
00128  * File open menu. Currently unimplemented.
00129  * \todo this should really be done in two stages:
00130  * -# close the current project and reinitialize structures
00131  * -# load the new project
00132  */
00133 #ifdef UNIMPLEMENTED_FEATURES
00134 static void
00135 menu_file_open()
00136 {
00137   x_dialog_unimplemented_feature();
00138 #if 0
00139   GSList *file_list;
00140 
00141   file_list = x_fileselect_open();
00142   
00143   /* Load the files, don't check if it went OK */
00144   x_fileselect_load_files(file_list);
00145   
00146   g_slist_foreach(file_list, (GFunc)g_free, NULL);
00147   g_slist_free(file_list);
00148 #endif
00149 }
00150 #endif
00151 
00157 static void
00158 menu_file_save()
00159 {
00160   s_toplevel_gtksheet_to_toplevel(pr_current);  /* Dumps sheet data into TOPLEVEL */
00161   s_page_save_all(pr_current);  /* saves all pages in design */
00162 
00163   sheet_head->CHANGED = FALSE;
00164 }
00165 
00171 static void 
00172 menu_file_export_csv()
00173 {
00174   gint cur_page;
00175 
00176   /* first verify that we are on the correct page (components) */
00177   cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
00178 
00179   /* Check that we are on components page. */
00180   if (cur_page == 0) {
00181     x_dialog_export_file();
00182   } else {
00183     x_dialog_unimplemented_feature();  /* We only support export 
00184                                           of components now */
00185   }
00186 }
00187 
00193 static void 
00194 menu_edit_newattrib()
00195 {
00196   gint cur_page;
00197 
00198   /* first verify that we are on the correct page (components) */
00199   cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
00200 
00201   /* Check that we are on components page. */
00202   if (cur_page == 0) {
00203     x_dialog_newattrib();  /* This creates dialog box  */
00204   }
00205 }
00206 
00212 static void
00213 menu_edit_delattrib()
00214 {
00215   x_dialog_delattrib();
00216 }
00217 
00221 static const GtkActionEntry actions[] = {
00222   /* name, stock-id, label, accelerator, tooltip, callback function */
00223   /* File menu */
00224   { "file", NULL, "_File"},
00225   /* { "file-open", GTK_STOCK_OPEN, "Open", "<Control>O", "", menu_file_open}, */
00226   { "file-save", GTK_STOCK_SAVE, "Save", "<Control>S", "", menu_file_save},
00227   { "file-export-csv", NULL, "Export CSV", "", "", menu_file_export_csv},
00228   /* { "file-print", GTK_STOCK_PRINT, "Print", "<Control>P", "", x_dialog_unimplemented_feature}, */
00229   { "file-quit", GTK_STOCK_QUIT, "Quit", "<Control>Q", "", G_CALLBACK(gattrib_really_quit)},
00230 
00231   /* Edit menu */
00232   { "edit", NULL, "_Edit"},
00233   { "edit-add-attrib", NULL, "Add new attrib column", "", "", menu_edit_newattrib},
00234   { "edit-delete-attrib", NULL, "Delete attrib column", "", "", menu_edit_delattrib},
00235   /* { "edit-find-attrib", GTK_STOCK_FIND, "Find attrib value", "<Control>F", "", x_dialog_unimplemented_feature}, */
00236   /* { "edit-search-replace-attrib-value", NULL, "Search and replace attrib value", "", "", x_dialog_unimplemented_feature}, */
00237   /* { "edit-search-for-refdes", NULL, "Search for refdes", "", "", x_dialog_unimplemented_feature}, */
00238 
00239   /* Visibility menu */
00240   { "visibility", NULL, "_Visibility"},
00241   { "visibility-invisible", NULL, "Set selected invisible", "", "", s_visibility_set_invisible},
00242   { "visibility-name-only", NULL, "Set selected name visible only", "", "", s_visibility_set_name_only},
00243   { "visibility-value-only", NULL, "Set selected value visible only", "", "", s_visibility_set_value_only},
00244   { "visibility-name-value", NULL, "Set selected name and value visible", "", "", s_visibility_set_name_and_value},
00245 
00246   /* Help menu */
00247   { "help", NULL, "_Help"},
00248   { "help-about", GTK_STOCK_ABOUT, "About", "", "", x_dialog_about_dialog},
00249 };
00250 
00251 
00266 static void
00267 x_window_create_menu(GtkWindow *window, GtkWidget **menubar)
00268 {
00269   gchar *menu_file;
00270   GtkUIManager *ui;
00271   GtkActionGroup *action_group;
00272   GError *error = NULL;
00273 
00274   /* Create and fill the action group object */
00275   action_group = gtk_action_group_new("");
00276   gtk_action_group_add_actions(action_group, actions, G_N_ELEMENTS(actions), NULL);
00277 
00278   /* Create the UI manager object */
00279   ui = gtk_ui_manager_new();
00280 
00281   gtk_ui_manager_insert_action_group(ui, action_group, 0);
00282 
00283   menu_file = g_build_filename(s_path_sys_data (), "gattrib-menus.xml", NULL);
00284 
00285   gtk_ui_manager_add_ui_from_file(ui, menu_file, &error);
00286   if(error != NULL) {
00287     /* An error occured, terminate */
00288     fprintf(stderr, "Error loading %s:\n%s\n", menu_file, error->message);
00289     exit(1);
00290   }
00291 
00292   g_free(menu_file);
00293 
00294   gtk_window_add_accel_group (window, gtk_ui_manager_get_accel_group(ui));
00295 
00296   *menubar = gtk_ui_manager_get_widget(ui, "/ui/menubar/");
00297 }
00298 
00299 
00315 void
00316 x_window_add_items()
00317 {
00318   gint i, j;
00319   gint num_rows, num_cols;
00320   gchar *text, *error_string;
00321   gint visibility, show_name_value;
00322   
00323   /* Do these sanity check to prevent later segfaults */
00324   if (sheet_head->comp_count == 0) {
00325     error_string = "No components found in entire design!\n"
00326             "Do you have refdeses on your components?";
00327     x_dialog_fatal_error(error_string, 1);
00328   }
00329 
00330   if (sheet_head->comp_attrib_count == 0) {
00331     error_string = "No configurable component attributes found in entire design!\n"
00332             "Please attach at least some attributes before running gattrib.";
00333     x_dialog_fatal_error(error_string, 2);
00334   }
00335 
00336   if (sheet_head->pin_count == 0) {
00337     error_string = "No pins found on any components!\n"
00338             "Please check your design.";
00339     x_dialog_fatal_error(error_string, 3);
00340   }
00341 
00342 
00343   /*  initialize the gtksheet. */
00344   x_gtksheet_init();  /* this creates a new gtksheet having dimensions specified
00345                * in sheet_head->comp_count, etc. . .  */
00346 
00347   if (sheet_head->comp_count > 0 ) {
00348     x_gtksheet_add_row_labels(GTK_SHEET(sheets[0]), 
00349                   sheet_head->comp_count, sheet_head->master_comp_list_head);
00350     x_gtksheet_add_col_labels(GTK_SHEET(sheets[0]), 
00351                   sheet_head->comp_attrib_count, sheet_head->master_comp_attrib_list_head);
00352   }
00353 
00354 #ifdef UNIMPLEMENTED_FEATURES
00355   /* This is not ready.  I need to implement net attributes */
00356   if (sheet_head->net_count > 0 ) {
00357     x_gtksheet_add_row_labels(GTK_SHEET(sheets[1]), 
00358                   sheet_head->net_count, sheet_head->master_net_list_head);
00359     x_gtksheet_add_col_labels(GTK_SHEET(sheets[1]), 
00360                   sheet_head->net_attrib_count, sheet_head->master_net_attrib_list_head);
00361   } else {
00362     x_gtksheet_add_row_labels(GTK_SHEET(sheets[1]), 1, NULL);
00363     x_gtksheet_add_col_labels(GTK_SHEET(sheets[1]), 1, NULL);
00364   }  
00365 #endif
00366 
00367 #ifdef UNIMPLEMENTED_FEATURES
00368   if (sheet_head->pin_count > 0 ) {
00369     x_gtksheet_add_row_labels(GTK_SHEET(sheets[2]), 
00370                   sheet_head->pin_count, sheet_head->master_pin_list_head);
00371     x_gtksheet_add_col_labels(GTK_SHEET(sheets[2]), 
00372                   sheet_head->pin_attrib_count, sheet_head->master_pin_attrib_list_head);
00373   }
00374 #endif
00375 
00376   /* ------ Comp sheet: put values in the individual cells ------- */
00377   num_rows = sheet_head->comp_count;
00378   num_cols = sheet_head->comp_attrib_count;
00379   for (i = 0; i < num_rows; i++) {
00380     for (j = 0; j < num_cols; j++) {
00381       if ( (sheet_head->component_table)[i][j].attrib_value ) { /* NULL = no entry */
00382     text = (gchar *) g_strdup( (sheet_head->component_table)[i][j].attrib_value );
00383     visibility = (sheet_head->component_table)[i][j].visibility;
00384     show_name_value = (sheet_head->component_table)[i][j].show_name_value;
00385     x_gtksheet_add_cell_item( GTK_SHEET(sheets[0]), i, j, (gchar *) text, 
00386                   visibility, show_name_value );
00387     g_free(text);
00388       }
00389     }
00390   }
00391 
00392 #ifdef UNIMPLEMENTED_FEATURES
00393   /* ------ Net sheet: put values in the individual cells ------- */
00394   num_rows = sheet_head->net_count;
00395   num_cols = sheet_head->net_attrib_count;
00396   for (i = 0; i < num_rows; i++) {
00397     for (j = 0; j < num_cols; j++) {
00398       if ( (sheet_head->net_table)[i][j].attrib_value ) { /* NULL = no entry */
00399     text = (gchar *) g_strdup( (sheet_head->net_table)[i][j].attrib_value );
00400     visibility = (sheet_head->net_table)[i][j].visibility;
00401     show_name_value = (sheet_head->component_table)[i][j].show_name_value;
00402     x_gtksheet_add_cell_item( GTK_SHEET(sheets[1]), i, j, (gchar *) text,
00403                   visibility, show_name_value );
00404     g_free(text);
00405       }
00406     }
00407   }
00408 #endif
00409 
00410 #ifdef UNIMPLEMENTED_FEATURES
00411   /* ------ Pin sheet: put pin attribs in the individual cells ------- */
00412   num_rows = sheet_head->pin_count;
00413   num_cols = sheet_head->pin_attrib_count;
00414   for (i = 0; i < num_rows; i++) {
00415     for (j = 0; j < num_cols; j++) {
00416       if ( (sheet_head->pin_table)[i][j].attrib_value ) { /* NULL = no entry */
00417     text = (gchar *) g_strdup( (sheet_head->pin_table)[i][j].attrib_value );
00418     /* pins have no visibility attributes, must therefore provide default. */
00419     x_gtksheet_add_cell_item( GTK_SHEET(sheets[2]), i, j, (gchar *) text, 
00420                   VISIBLE, SHOW_VALUE );
00421     g_free(text);
00422       }
00423     }
00424   }
00425 #endif
00426 
00427   gtk_widget_show_all( GTK_WIDGET(window) );
00428 }
00429 
00430 
00438 static void
00439 x_window_set_default_icon( void )
00440 {
00441   gtk_window_set_default_icon_name( GATTRIB_THEME_ICON_NAME );
00442 }
00443 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines