list_data_types.c

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /*
00003   list_data_types.c
00004   Window allowing the user to manage his data types (add, remove, edit,
00005   save and load them)
00006   
00007   (C) Copyright 2007 - 2007 Olivier Delhomme
00008   e-mail : heraia@delhomme.org
00009   URL    : http://heraia.tuxfamily.org
00010  
00011   This program is free software; you can redistribute it and/or modify
00012   it under the terms of the GNU General Public License as published by
00013   the Free Software Foundation; either version 2, or  (at your option) 
00014   any later version.
00015  
00016   This program is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY;  without even the implied warranty of
00018   MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the
00019   GNU General Public License for more details.
00020  
00021   You should have received a copy of the GNU General Public License
00022   along with this program; if not, write to the Free Software
00023   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
00024 */
00025 
00026 #include <libheraia.h>
00027 
00028 static gboolean delete_ldt_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data);
00029 static void destroy_ldt_window(GtkWidget *widget, GdkEvent  *event, gpointer data);
00030 static void ldt_add_button_clicked(GtkWidget *widget, gpointer data);
00031 static void ldt_remove_button_clicked(GtkWidget *widget, gpointer data);
00032 static void ldt_edit_button_clicked(GtkWidget *widget, gpointer data);
00033 static void ldt_save_button_clicked(GtkWidget *widget, gpointer data);
00034 static void connect_list_data_types_signals(heraia_window_t *main_window);
00035 
00036 /**
00037  *  Shows or hide the list data type window
00038  */
00039 void on_ldt_menu_activate(GtkWidget *widget, gpointer data)
00040 {
00041         heraia_window_t *main_window = (heraia_window_t *) data;
00042         GtkWidget *check_menu_item = NULL;
00043 
00044         if (main_window != NULL)
00045                 {
00046                         check_menu_item = heraia_get_widget(main_window->xmls->main, "ldt_menu");
00047 
00048                         if (is_cmi_checked(check_menu_item) == TRUE)
00049                                 {  /* if the menu is checked, shows the window */
00050                                         move_and_show_dialog_box(heraia_get_widget(main_window->xmls->main, "list_data_types_window"), main_window->win_prop->ldt);
00051                                 }
00052                         else
00053                                 {
00054                                         record_and_hide_dialog_box(heraia_get_widget(main_window->xmls->main, "list_data_types_window"), main_window->win_prop->ldt);
00055                                 }
00056                 }
00057 }
00058 
00059 
00060 /**
00061  *  Adds the data type name to the treeview
00062  */
00063 void add_data_type_name_to_treeview(heraia_window_t *main_window, gchar *name)
00064 {
00065         GtkListStore *list_store = NULL;  /* Treeview Stuff for rendering */
00066         GtkTreeIter iter;                 /* the text in it.              */
00067         GtkCellRenderer *renderer = NULL; 
00068         GtkTreeViewColumn *column = NULL;  
00069         GtkTreeView *treeview = NULL;     /* Treeview where data type name will be displayed */
00070 
00071         treeview = GTK_TREE_VIEW(heraia_get_widget(main_window->xmls->main, "ldt_treeview"));
00072 
00073         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview));
00074 
00075         if (list_store == NULL)
00076                 {
00077                         list_store = gtk_list_store_new(LDT_TV_N_COLUMNS, G_TYPE_STRING);
00078                         renderer = gtk_cell_renderer_text_new();
00079                         column = gtk_tree_view_column_new_with_attributes("Name", renderer, "text", LDT_TV_COLUMN_NAME, NULL);
00080                         gtk_tree_view_append_column(treeview, column);
00081                 }
00082 
00083         gtk_list_store_append(list_store, &iter);
00084         gtk_list_store_set(list_store, &iter, LDT_TV_COLUMN_NAME, name, -1);
00085         gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(list_store));
00086 }
00087 
00088 
00089 /**
00090  *  Called when the list data types is killed or closed
00091  *  !! Not to be confused with delete_dt_window_event !!
00092  */
00093 static gboolean delete_ldt_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data)
00094 {       
00095         heraia_window_t *main_window = (heraia_window_t *) data;
00096 
00097         g_signal_emit_by_name(heraia_get_widget(main_window->xmls->main, "ldt_menu"), "activate");
00098 
00099         return TRUE;
00100 }
00101 
00102 
00103 /**
00104  *  Called when the list data types is killed or closed
00105  *  !! Not to be confused with destroy_dt_window !!
00106  */
00107 static void destroy_ldt_window(GtkWidget *widget, GdkEvent  *event, gpointer data)
00108 {
00109         heraia_window_t *main_window = (heraia_window_t *) data;
00110 
00111         g_signal_emit_by_name(heraia_get_widget(main_window->xmls->main, "ldt_menu"), "activate");
00112 }
00113 
00114 
00115 /**
00116  *  When the add button (+) is clicked
00117  */
00118 static void ldt_add_button_clicked(GtkWidget *widget, gpointer data)
00119 {
00120         heraia_window_t *main_window = (heraia_window_t *) data;
00121 
00122         main_window->current_data_type = new_data_type("", DT_SPIN_MIN);
00123 
00124         /* data interpretor widget creation */
00125         create_ud_data_interpretor_widgets(main_window, main_window->current_data_type);
00126 
00127         show_data_type_window(main_window, main_window->current_data_type);
00128 }
00129 
00130 
00131 /**
00132  *  When the remove (-) button is clicked
00133  */
00134 static void ldt_remove_button_clicked(GtkWidget *widget, gpointer data)
00135 {
00136         heraia_window_t *main_window = (heraia_window_t *) data;
00137         GList *data_type_list = main_window->data_type_list;
00138         GtkTreeView *treeview = NULL; 
00139         GtkTreeSelection *selection = NULL;
00140         GtkTreeIter iter;
00141         GtkTreeModel *model = NULL;
00142         GtkListStore *list_store = NULL;
00143         gchar *name = NULL;
00144         data_type_t *a_data_type = NULL;
00145 
00146         treeview = GTK_TREE_VIEW(heraia_get_widget(main_window->xmls->main, "ldt_treeview"));
00147 
00148         /* gets the selection of the treeview (if any) */
00149         selection = gtk_tree_view_get_selection(treeview);
00150 
00151         if (gtk_tree_selection_get_selected(selection, &model, &iter))
00152                 {    
00153                         gtk_tree_model_get(model, &iter, LDT_TV_COLUMN_NAME, &name, -1);
00154                         data_type_list = is_data_type_name_already_used(data_type_list, name);
00155                         
00156                         if (data_type_list != NULL)
00157                                 {
00158                                         /* frees internal structure */
00159                                         a_data_type = (data_type_t *) data_type_list->data;
00160                                         
00161                                         /* the data interpretor part */
00162                                         destroy_a_single_widget(a_data_type->di_label);
00163                                         destroy_a_single_widget(a_data_type->di_entry);
00164                                         free_data_type(a_data_type);
00165 
00166                                         main_window->data_type_list = g_list_delete_link(main_window->data_type_list, data_type_list);
00167 
00168                                         /* frees the treeview accordingly */
00169                                         list_store = GTK_LIST_STORE(model);
00170                                         if (list_store != NULL)
00171                                                 {
00172                                                         gtk_list_store_remove(list_store, &iter);
00173                                                         gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(list_store));
00174                                                 }
00175                                 }
00176                 }
00177 }
00178 
00179 
00180 /**
00181  *  When the edit button is clicked
00182  */
00183 static void ldt_edit_button_clicked(GtkWidget *widget, gpointer data)
00184 {
00185         heraia_window_t *main_window = (heraia_window_t *) data;
00186 
00187         GList *data_type_list = main_window->data_type_list;
00188         GtkTreeSelection *selection = NULL;
00189         GtkTreeIter iter;
00190         GtkTreeModel *model = NULL;
00191         gchar *name = NULL;
00192 
00193         /* gets the selection of the treeview (if any) */
00194         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(heraia_get_widget(main_window->xmls->main, "ldt_treeview")));
00195 
00196         if (gtk_tree_selection_get_selected(selection, &model, &iter))
00197                 {    
00198                         gtk_tree_model_get(model, &iter, LDT_TV_COLUMN_NAME, &name, -1);
00199                         data_type_list = is_data_type_name_already_used(data_type_list, name);
00200                         
00201                         if (data_type_list != NULL)
00202                                 {               
00203                                         /** 
00204                                          *  We use a copy because we want to be able to be in edit mode even if
00205                                          *  the user clicked add(+) button.
00206                                          */
00207                                         main_window->current_data_type = copy_data_type_struct(main_window, data_type_list->data);
00208                                         
00209                                         show_data_type_window(main_window, main_window->current_data_type);
00210                                 }
00211                 }
00212 }
00213 
00214 
00215 /**
00216  *  When the save button is clicked
00217  */
00218 static void ldt_save_button_clicked(GtkWidget *widget, gpointer data)
00219 {
00220         heraia_window_t *main_window = (heraia_window_t *) data;
00221         /* GList *data_type_list = main_window->data_type_list; */
00222 
00223         /**
00224          *  I'll do this later ...
00225          *  I first have to understand XML !
00226          */
00227         log_message(main_window, G_LOG_LEVEL_WARNING, "This function is not yet implemented. Please Contribute :)");
00228 
00229 }
00230 
00231 
00232 /**
00233  *  Connects list_data_types's window signals.
00234  */
00235 static void connect_list_data_types_signals(heraia_window_t *main_window)
00236 {
00237 
00238         if (main_window != NULL && main_window->xmls != NULL && main_window->xmls->main != NULL)
00239                 {
00240                         /* list data types menu */
00241                         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "ldt_menu")), "activate", 
00242                                                          G_CALLBACK(on_ldt_menu_activate), main_window);
00243       
00244                         /* list data types window killed or destroyed */
00245                         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "list_data_types_window")), "delete_event", 
00246                                                          G_CALLBACK(delete_ldt_window_event), main_window);
00247 
00248                         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "list_data_types_window")), "destroy", 
00249                                                          G_CALLBACK(destroy_ldt_window), main_window);
00250 
00251                         /* Add button    */
00252                         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "ldt_add_button")), "clicked", 
00253                                                          G_CALLBACK(ldt_add_button_clicked), main_window);
00254 
00255                         /* Remove button */
00256                         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "ldt_remove_button")), "clicked", 
00257                                                          G_CALLBACK(ldt_remove_button_clicked), main_window);
00258 
00259                         /* Edit button   */
00260                         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "ldt_edit_button")), "clicked", 
00261                                                          G_CALLBACK(ldt_edit_button_clicked), main_window);
00262 
00263                         /* Save button   */
00264                         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "ldt_save_button")), "clicked", 
00265                                                          G_CALLBACK(ldt_save_button_clicked), main_window);
00266                 }
00267 }
00268 
00269 
00270 /**
00271  *  Inits the list data type window
00272  *  with default values
00273  *  Should be called only once
00274  */
00275 void list_data_types_init_interface(heraia_window_t *main_window)
00276 {
00277 
00278         if (main_window != NULL)
00279                 {
00280                         connect_list_data_types_signals(main_window);
00281                 }
00282 }

Generated on Tue Jun 30 23:18:17 2009 for Heraia by  doxygen 1.5.8