dialog-budget-list.c

Go to the documentation of this file.
00001 /********************************************************************\
00002  * dialog-budget-list.c --  Implementation of the budget list       *
00003  *                          dialog.                                 *
00004  * Copyright (C) 08 sep 2003    Darin Willits <darin@willits.ca>    *
00005  *                                                                  *
00006  * This program is free software; you can redistribute it and/or    *
00007  * modify it under the terms of the GNU General Public License as   *
00008  * published by the Free Software Foundation; either version 2 of   *
00009  * the License, or (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, contact:                        *
00018  *                                                                  *
00019  * Free Software Foundation           Voice:  +1-617-542-5942       *
00020  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00021  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00022  *                                                                  *
00023 \********************************************************************/
00024 
00039 // Includes
00040 #include <glib.h>
00041 #include <gnome.h>
00042 
00043 #include "dialog-budget-list.h"
00044 #include "dialog-budget-workbench.h"
00045 #include "druid-budget-create.h"
00046 #include "dialog-utils.h"
00047 #include "gnc-ui-util.h"
00048 
00049 #include "gnc-budget-gui.h"
00050 #include "gnc-budget-book.h"
00051 #include "gnc-budget.h"
00052 #include "gnc-budget-list-tree-model.h"
00053 
00054 
00055 /* The budget list dialog structure. */
00056 typedef struct{
00057     GtkWidget* wnd;
00058     GladeXML* xml;
00059     
00060     GtkWidget* treeView;
00061     GtkTreeModel* treeModel;
00062 
00063 } BudgetListDlg;
00064 
00065 /* The budget list columns. */
00066 enum{
00067     COLUMN_NAME,
00068     COLUMN_DESCRIPTION,
00069     BUDGET_LIST_NUM_COLS
00070 };
00071 
00072 
00073 /* Add the new budget object to the tree model.  
00074  * FIXME: This will be unnecessary once we create a tree model
00075  * which directly relates to the book's list of budgets but for 
00076  * now we have to hack around it.  This is ugly though and prone
00077  * to error so it should/must be fixed.
00078  */
00079 void add_budget_to_model( gpointer data, gpointer user_data )
00080 {
00081     GncBudget* budget;
00082     GtkTreeIter iter;
00083     GtkTreeModel* treeModel;
00084     /*BudgetListDlg* dlg;*/
00085     
00086     budget = data;
00087     treeModel = user_data;
00088 
00089     if((budget == NULL) || (treeModel == NULL)){
00090         return;
00091     }
00092     
00093     gtk_list_store_append (GTK_LIST_STORE(treeModel), &iter);
00094     gtk_list_store_set (GTK_LIST_STORE(treeModel), &iter,
00095                           COLUMN_NAME, gnc_budget_get_name(budget),
00096                           COLUMN_DESCRIPTION, gnc_budget_get_description(budget),
00097                           -1);
00098 }
00099 
00100 /* Initialize the tree model with the list of budgets from the
00101  * book.
00102  */
00103 /*
00104 static void fill_model(BudgetListDlg* dlg)
00105 {
00106     GList *budgetList;
00107     GtkListStore* store;
00108 
00109     store = gtk_list_store_new (BUDGET_LIST_NUM_COLS,
00110                               G_TYPE_STRING,
00111                               G_TYPE_STRING);
00112 
00113     dlg->treeModel = GTK_TREE_MODEL(store);
00114     
00115     budgetList = gnc_book_get_budgets(gnc_get_current_book());
00116     g_list_foreach( budgetList, add_budget_to_model,  dlg->treeModel);
00117 }
00118 */
00119 /* Add the columns to the tree view. */
00120 static void add_columns(GtkTreeView* treeView)
00121 {
00122     GtkCellRenderer *renderer;
00123     GtkTreeViewColumn *column;
00124 
00125     /* column for name */
00126     renderer = gtk_cell_renderer_text_new ();
00127     column = gtk_tree_view_column_new_with_attributes ("Name",
00128                                                      renderer,
00129                                                      "text",
00130                                                      COLUMN_NAME,
00131                                                      NULL);
00132     gtk_tree_view_append_column (treeView, column);
00133 
00134     /* column for description */
00135     renderer = gtk_cell_renderer_text_new ();
00136     column = gtk_tree_view_column_new_with_attributes ("Description",
00137                                                      renderer,
00138                                                      "text",
00139                                                      COLUMN_DESCRIPTION,
00140                                                      NULL);
00141     gtk_tree_view_append_column (treeView, column);
00142 }
00143 
00144 /* Destroy the budget list dialog. */
00145 static void budget_list_dialog_destroy(BudgetListDlg* dlg)
00146 {
00147     if(dlg == NULL){
00148         return;
00149     }
00150     gtk_widget_destroy(dlg->wnd);
00151     g_free(dlg);
00152 }
00153 
00154 /* Event handler for the close button. */
00155 static void close_button_clicked(GtkWidget* object, gpointer data)
00156 {
00157     budget_list_dialog_destroy(data);
00158 }
00159 
00160 /* Event handler for the new button. */
00161 static void new_button_clicked(GtkWidget* object, gpointer data)
00162 {
00163     BudgetListDlg* dlg = data;
00164 
00165     gnc_budget_druid_create(dlg->treeModel);
00166 }
00167 
00168 /* Event handler for the edit button.  Create a budget workbench 
00169  * dialog with the given budget object. */
00170 static void edit_button_clicked(GtkWidget* object, gpointer data)
00171 {
00172     GtkTreeIter iter;
00173     BudgetListDlg* dlg = data;
00174     GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(dlg->treeView));
00175     GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(dlg->treeView));
00176 
00177     /* If a budget is selected use it to create a budget workbench. */
00178     if (gtk_tree_selection_get_selected (selection, NULL, &iter)){
00179         gint i;
00180         GtkTreePath *path;
00181         GncBudget* budget;
00182         GList* budgetList;
00183 
00184         path = gtk_tree_model_get_path (model, &iter);
00185         i = gtk_tree_path_get_indices (path)[0];
00186     
00187         budgetList = gnc_book_get_budgets(gnc_get_current_book());
00188         budget = g_list_nth_data(budgetList, i);
00189 
00190         if(budget != NULL){
00191             printf("Creating Workbench: Budget: %p\n", budget);
00192             /* Create the workbench. */
00193             gnc_budget_workbench_dialog_create(budget);
00194         }
00195         
00196         gtk_tree_path_free (path);
00197     }
00198 }
00199 
00200 /* Create the budget list dialog. */
00201 static void budget_create_list_dialog(BudgetListDlg* dlg)
00202 {
00203     GladeXML *xml;
00204     GtkWidget *new_button;
00205     GtkWidget *edit_button;
00206     GtkWidget *close_button;
00207     
00208     /* Load the main dialog widget. */
00209     xml = gnc_glade_xml_new (GNC_BUDGET_GUI_FILE, "Budget List");
00210     dlg->wnd = glade_xml_get_widget(xml, "Budget List");
00211     dlg->xml = xml;
00212     
00213     /* Create and fill the model */
00214     /*fill_model(dlg);*/
00215     dlg->treeModel = gnc_budget_list_tree_model_new(gnc_get_current_book());
00216     
00217     dlg->treeView = glade_xml_get_widget(xml, "Budget List View");
00218     gtk_tree_view_set_model (GTK_TREE_VIEW(dlg->treeView), dlg->treeModel);
00219 
00220     add_columns(GTK_TREE_VIEW(dlg->treeView));
00221 
00222     
00223     /* Connect signals. */
00224     new_button = glade_xml_get_widget( dlg->xml, "new_button" );
00225     g_signal_connect( new_button, "clicked",
00226                         G_CALLBACK(new_button_clicked), dlg);
00227     edit_button = glade_xml_get_widget( dlg->xml, "edit_button" );
00228     g_signal_connect( edit_button, "clicked",
00229                         G_CALLBACK(edit_button_clicked), dlg);
00230     close_button = glade_xml_get_widget( dlg->xml, "close_button" );
00231     g_signal_connect( close_button, "clicked",
00232                         G_CALLBACK(close_button_clicked), dlg);
00233 }
00234 
00235 /* Public interface for creating a budget list dialog. */
00236 void gnc_budget_list_dialog_create(void)
00237 {
00238     BudgetListDlg* dlg;
00239 
00240     dlg = g_new0(BudgetListDlg, 1);
00241 
00242     budget_create_list_dialog(dlg);
00243     
00244     gtk_widget_show_all(dlg->wnd);
00245 }
00246 

Generated on Sun Sep 4 18:07:35 2005 for GnuCash by  doxygen 1.4.3-20050530