dialog-billterms.c

00001 /*
00002  * dialog-billterms.c -- Dialog to create and edit billing terms
00003  * Copyright (C) 2002 Derek Atkins
00004  * Author: Derek Atkins <warlord@MIT.EDU>
00005  * Copyright (c) 2006 David Hampton <hampton@employees.org>
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, contact:
00019  *
00020  * Free Software Foundation           Voice:  +1-617-542-5942
00021  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00022  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00023  */
00024 
00025 #include "config.h"
00026 
00027 #include <gtk/gtk.h>
00028 #include <glib/gi18n.h>
00029 
00030 #include "dialog-utils.h"
00031 #include "gnc-component-manager.h"
00032 #include "gnc-ui.h"
00033 #include "gnc-gui-query.h"
00034 #include "gnc-gconf-utils.h"
00035 #include "gnc-ui-util.h"
00036 #include "qof.h"
00037 
00038 #include "gncBillTerm.h"
00039 #include "dialog-billterms.h"
00040 
00041 #define DIALOG_BILLTERMS_CM_CLASS "billterms-dialog"
00042 
00043 enum term_cols {
00044   BILL_TERM_COL_NAME = 0,
00045   BILL_TERM_COL_TERM,
00046   NUM_BILL_TERM_COLS
00047 };
00048 
00049 void billterms_new_term_cb (GtkButton *button, BillTermsWindow *btw);
00050 void billterms_delete_term_cb (GtkButton *button, BillTermsWindow *btw);
00051 void billterms_edit_term_cb (GtkButton *button, BillTermsWindow *btw);
00052 void billterms_window_close (GtkWidget *widget, gpointer data);
00053 void billterms_window_destroy_cb (GtkWidget *widget, gpointer data);
00054 void billterms_type_combobox_changed (GtkComboBox *cb, gpointer data);
00055 
00056 typedef struct _billterm_notebook {
00057   GtkTooltips *         tooltips;
00058   GtkWidget *           notebook;
00059 
00060   /* "Days" widgets */
00061   GtkWidget *           days_due_days;
00062   GtkWidget *           days_disc_days;
00063   GtkWidget *           days_disc;
00064 
00065   /* "Proximo" widgets */
00066   GtkWidget *           prox_due_day;
00067   GtkWidget *           prox_disc_day;
00068   GtkWidget *           prox_disc;
00069   GtkWidget *           prox_cutoff;
00070 
00071   /* What kind of term is this? */
00072   GncBillTermType       type;
00073 } BillTermNB;
00074 
00075 struct _billterms_window {
00076   GtkWidget *   dialog;
00077   GtkWidget *   terms_view;
00078   GtkWidget *   desc_entry;
00079   GtkWidget *   type_label;
00080   GtkWidget *   term_vbox;
00081   BillTermNB    notebook;
00082 
00083   GncBillTerm * current_term;
00084   GNCBook *     book;
00085   gint          component_id;
00086 };
00087 
00088 typedef struct _new_billterms {
00089   GtkWidget *   dialog;
00090   GtkWidget *   name_entry;
00091   GtkWidget *   desc_entry;
00092   BillTermNB    notebook;
00093 
00094   BillTermsWindow *     btw;
00095   GncBillTerm * this_term;
00096 } NewBillTerm;
00097 
00098 
00099 static GtkWidget *
00100 read_widget (GladeXML *xml, char *name, gboolean read_only)
00101 {
00102   GtkWidget *widget = glade_xml_get_widget (xml, name);
00103   if (read_only) {
00104     GtkAdjustment *adj;
00105     gtk_editable_set_editable (GTK_EDITABLE (widget), FALSE);
00106     adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
00107     adj->step_increment = 0.0;
00108     adj->page_increment = 0.0;
00109     gtk_adjustment_changed (adj);
00110   }
00111 
00112   return widget;
00113 }
00114 
00115 /* NOTE: The caller needs to unref once they attach */
00116 static void
00117 init_notebook_widgets (BillTermNB *notebook, gboolean read_only,
00118                        GtkDialog *dialog, gpointer user_data)
00119 {
00120   GladeXML *xml;
00121   GtkWidget *parent;
00122 
00123   /* Initialize the tooltips */
00124   notebook->tooltips = gtk_tooltips_new ();
00125 
00126   /* Load the notebook from XML */
00127   xml = gnc_glade_xml_new ("billterms.glade", "Term Notebook");
00128   notebook->notebook = glade_xml_get_widget (xml, "term_notebook");
00129   parent = glade_xml_get_widget (xml, "Term Notebook");
00130 
00131   /* load the "days" widgets */
00132   notebook->days_due_days = read_widget (xml, "days:due_days", read_only);
00133   notebook->days_disc_days = read_widget (xml, "days:discount_days", read_only);
00134   notebook->days_disc = read_widget (xml, "days:discount", read_only);
00135 
00136   /* load the "proximo" widgets */
00137   notebook->prox_due_day = read_widget (xml, "prox:due_day", read_only);
00138   notebook->prox_disc_day = read_widget (xml, "prox:discount_day", read_only);
00139   notebook->prox_disc = read_widget (xml, "prox:discount", read_only);
00140   notebook->prox_cutoff = read_widget (xml, "prox:cutoff_day", read_only);
00141 
00142   /* Disconnect the notebook from the window */
00143   g_object_ref (notebook->notebook);
00144   gtk_container_remove (GTK_CONTAINER (parent), notebook->notebook);
00145   gtk_widget_destroy (parent);
00146 
00147   /* NOTE: The caller needs to unref once they attach */
00148 }
00149 
00150 static void
00151 set_numeric (GtkWidget *widget, GncBillTerm *term,
00152              void (*func)(GncBillTerm *, gnc_numeric))
00153 {
00154   gnc_numeric val;
00155   gdouble fl = 0.0;
00156 
00157   fl = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
00158   val = double_to_gnc_numeric (fl, 100000, GNC_RND_ROUND);
00159   func (term, val);
00160 }
00161 
00162 static void
00163 get_numeric (GtkWidget *widget, GncBillTerm *term,
00164              gnc_numeric (*func)(GncBillTerm *))
00165 {
00166   gnc_numeric val;
00167   gdouble fl;
00168 
00169   val = func (term);
00170   fl = gnc_numeric_to_double (val);
00171   gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), fl);
00172 }
00173 
00174 static void
00175 set_int (GtkWidget *widget, GncBillTerm *term,
00176              void (*func)(GncBillTerm *, gint))
00177 {
00178   gint val;
00179 
00180   val = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (widget));
00181   func (term, val);
00182 }
00183 
00184 static void
00185 get_int (GtkWidget *widget, GncBillTerm *term,
00186              gint (*func)(GncBillTerm *))
00187 {
00188   gint val;
00189 
00190   val = func (term);
00191   gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), (gfloat)val);
00192 }
00193 
00194 /* return TRUE if anything truly changed */
00195 static gboolean
00196 ui_to_billterm (NewBillTerm *nbt)
00197 {
00198   BillTermNB *notebook;
00199   GncBillTerm *term;
00200   const char *text;
00201 
00202   term = nbt->this_term;
00203   notebook = &nbt->notebook;
00204 
00205   text = gtk_entry_get_text (GTK_ENTRY (nbt->desc_entry));
00206   if (text)
00207     gncBillTermSetDescription (term, text);
00208 
00209   gncBillTermSetType (nbt->this_term, nbt->notebook.type);
00210 
00211   switch (nbt->notebook.type) {
00212   case GNC_TERM_TYPE_DAYS:
00213     set_int (notebook->days_due_days, term, gncBillTermSetDueDays);
00214     set_int (notebook->days_disc_days, term, gncBillTermSetDiscountDays);
00215     set_numeric (notebook->days_disc, term, gncBillTermSetDiscount);
00216     break;
00217 
00218   case GNC_TERM_TYPE_PROXIMO:
00219     set_int (notebook->prox_due_day, term, gncBillTermSetDueDays);
00220     set_int (notebook->prox_disc_day, term, gncBillTermSetDiscountDays);
00221     set_numeric (notebook->prox_disc, term, gncBillTermSetDiscount);
00222     set_int (notebook->prox_cutoff, term, gncBillTermSetCutoff);
00223     break;
00224   }
00225 
00226   return gncBillTermIsDirty (term);
00227 }
00228 
00229 static void
00230 billterm_to_ui (GncBillTerm *term, GtkWidget *desc, BillTermNB *notebook)
00231 {
00232   gtk_entry_set_text (GTK_ENTRY (desc), gncBillTermGetDescription (term));
00233   notebook->type = gncBillTermGetType (term);
00234 
00235   switch (notebook->type) {
00236   case GNC_TERM_TYPE_DAYS:
00237     get_int (notebook->days_due_days, term, gncBillTermGetDueDays);
00238     get_int (notebook->days_disc_days, term, gncBillTermGetDiscountDays);
00239     get_numeric (notebook->days_disc, term, gncBillTermGetDiscount);
00240     break;
00241 
00242   case GNC_TERM_TYPE_PROXIMO:
00243     get_int (notebook->prox_due_day, term, gncBillTermGetDueDays);
00244     get_int (notebook->prox_disc_day, term, gncBillTermGetDiscountDays);
00245     get_numeric (notebook->prox_disc, term, gncBillTermGetDiscount);
00246     get_int (notebook->prox_cutoff, term, gncBillTermGetCutoff);
00247     break;
00248   }
00249 }
00250 
00251 static gboolean
00252 verify_term_ok (NewBillTerm *nbt)
00253 {
00254   char *message;
00255   gnc_numeric num;
00256 
00257   return TRUE;
00258 
00259   /* verify the discount day(s) is less than the due day(s) */
00260   num = gnc_numeric_zero ();
00261   if (gnc_numeric_negative_p (num)) {
00262     message = _("Negative amounts are not allowed.");
00263     gnc_error_dialog (nbt->dialog, "%s", message);
00264     return FALSE;
00265   }
00266   if (gnc_numeric_compare (num, gnc_numeric_create (100, 1)) > 0) {
00267     message = _("Percentage amount must be between 0 and 100.");
00268     gnc_error_dialog (nbt->dialog, "%s", message);
00269     return FALSE;
00270   }
00271   return TRUE;
00272 }
00273 
00274 static gboolean
00275 new_billterm_ok_cb (NewBillTerm *nbt)
00276 {
00277   BillTermsWindow *btw;
00278   const char *name = NULL;
00279   char *message;
00280 
00281   g_return_val_if_fail (nbt, FALSE);
00282   btw = nbt->btw;
00283 
00284   /* Verify that we've got real, valid data */
00285 
00286   /* verify the name, maybe */
00287   if (nbt->this_term == NULL) {
00288     name = gtk_entry_get_text (GTK_ENTRY (nbt->name_entry));
00289     if (name == NULL || *name == '\0') {
00290       message = _("You must provide a name for this Billing Term.");
00291       gnc_error_dialog (nbt->dialog, "%s", message);
00292       return FALSE;
00293     }
00294     if (gncBillTermLookupByName (btw->book, name)) {
00295       message = g_strdup_printf(_(
00296                          "You must provide a unique name for this Billing Term. "
00297                          "Your choice \"%s\" is already in use."), name);
00298       gnc_error_dialog (nbt->dialog, "%s", message);
00299       g_free (message);
00300       return FALSE;
00301     }
00302   }
00303 
00304   /* Verify the actual data */
00305   if (!verify_term_ok (nbt))
00306     return FALSE;
00307 
00308   gnc_suspend_gui_refresh ();
00309 
00310   /* Ok, it's all valid, now either change or add this thing */
00311   if (nbt->this_term == NULL) {
00312     nbt->this_term = gncBillTermCreate (btw->book);
00313     gncBillTermBeginEdit (nbt->this_term);
00314     gncBillTermSetName (nbt->this_term, name);
00315     /* Reset the current term */
00316     btw->current_term = nbt->this_term;
00317   } else
00318     gncBillTermBeginEdit (btw->current_term);
00319 
00320   /* Fill in the rest of the term */
00321   if (ui_to_billterm (nbt))
00322     gncBillTermChanged (btw->current_term);
00323 
00324   /* Mark the table as changed and commit it */
00325   gncBillTermCommitEdit (btw->current_term);
00326 
00327   gnc_resume_gui_refresh();
00328   return TRUE;
00329 }
00330 
00331 static void
00332 show_notebook (BillTermNB *notebook)
00333 {
00334   g_return_if_fail (notebook->type > 0);
00335   gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook->notebook),
00336                                  notebook->type-1);
00337 }
00338 
00339 static void
00340 maybe_set_type (NewBillTerm *nbt, GncBillTermType type)
00341 {
00342   /* See if anything to do? */
00343   if (type == nbt->notebook.type)
00344     return;
00345 
00346   /* Yep.  Let's refresh */
00347   nbt->notebook.type = type;
00348   show_notebook (&nbt->notebook);
00349 }
00350 
00351 void
00352 billterms_type_combobox_changed (GtkComboBox *cb, gpointer data)
00353 {
00354   NewBillTerm *nbt = data;
00355   gint value;
00356 
00357   value = gtk_combo_box_get_active(cb);
00358   maybe_set_type (nbt, value + 1);
00359 }
00360 
00361 static GncBillTerm *
00362 new_billterm_dialog (BillTermsWindow *btw, GncBillTerm *term,
00363                      const char *name)
00364 {
00365   GncBillTerm *created_term = NULL;
00366   NewBillTerm *nbt;
00367   GladeXML *xml;
00368   GtkWidget *box, *combo_box;
00369   gint response;
00370   gboolean done;
00371   const gchar *dialog_name;
00372 
00373   if (!btw) return NULL;
00374 
00375   nbt = g_new0 (NewBillTerm, 1);
00376   nbt->btw = btw;
00377   nbt->this_term = term;
00378 
00379   /* Open and read the XML */
00380   dialog_name = term ? "Edit Term Dialog" : "New Term Dialog";
00381   xml = gnc_glade_xml_new ("billterms.glade", dialog_name);
00382   nbt->dialog = glade_xml_get_widget (xml, dialog_name);
00383   nbt->name_entry = glade_xml_get_widget (xml, "name_entry");
00384   nbt->desc_entry = glade_xml_get_widget (xml, "desc_entry");
00385   if (name)
00386     gtk_entry_set_text (GTK_ENTRY (nbt->name_entry), name);
00387 
00388   /* Initialize the notebook widgets */
00389   init_notebook_widgets (&nbt->notebook, FALSE,
00390                          GTK_DIALOG (nbt->dialog), nbt);
00391 
00392   /* Attach the notebook */
00393   box = glade_xml_get_widget (xml, "notebook_box");
00394   gtk_box_pack_start (GTK_BOX (box), nbt->notebook.notebook, TRUE, TRUE, 0);
00395   g_object_unref (nbt->notebook.notebook);
00396 
00397   /* Fill in the widgets appropriately */
00398   if (term)
00399     billterm_to_ui (term, nbt->desc_entry, &nbt->notebook);
00400   else
00401     nbt->notebook.type = GNC_TERM_TYPE_DAYS;
00402 
00403   /* Create the menu */
00404   combo_box = glade_xml_get_widget (xml, "type_combobox");
00405   gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), nbt->notebook.type - 1);
00406 
00407   /* Show the right notebook page */
00408   show_notebook (&nbt->notebook);
00409 
00410   /* Setup signals */
00411   glade_xml_signal_autoconnect_full( xml,
00412                                      gnc_glade_autoconnect_full_func,
00413                                      nbt);
00414 
00415   gtk_window_set_transient_for (GTK_WINDOW(nbt->dialog),
00416                                 GTK_WINDOW(btw->dialog));
00417 
00418   /* Show what we should */
00419   gtk_widget_show_all (nbt->dialog);
00420   if (term) {
00421     gtk_widget_grab_focus (nbt->desc_entry);
00422   } else
00423     gtk_widget_grab_focus (nbt->name_entry);
00424 
00425   done = FALSE;
00426   while (!done) {
00427     response = gtk_dialog_run (GTK_DIALOG (nbt->dialog));
00428     switch (response) {
00429      case GTK_RESPONSE_OK:
00430       if (new_billterm_ok_cb (nbt)) {
00431         created_term = nbt->this_term;
00432         done = TRUE;
00433       }
00434       break;
00435      default:
00436       done = TRUE;
00437       break;
00438     }
00439   }
00440 
00441   gtk_widget_destroy(nbt->dialog);
00442   g_free(nbt);
00443 
00444   return created_term;
00445 }
00446 
00447 /***********************************************************************/
00448 
00449 static void
00450 billterms_term_refresh (BillTermsWindow *btw)
00451 {
00452   char *type_label;
00453 
00454   g_return_if_fail (btw);
00455 
00456   if (!btw->current_term) {
00457     gtk_widget_hide_all (btw->term_vbox);
00458     return;
00459   }
00460 
00461   gtk_widget_show_all (btw->term_vbox);
00462   billterm_to_ui (btw->current_term, btw->desc_entry, &btw->notebook);
00463   switch (gncBillTermGetType (btw->current_term)) {
00464   case GNC_TERM_TYPE_DAYS:
00465     type_label = _("Days");
00466     break;
00467   case GNC_TERM_TYPE_PROXIMO:
00468     type_label = _("Proximo");
00469     break;
00470   default:
00471     type_label = _("Unknown");
00472     break;
00473   }
00474   show_notebook (&btw->notebook);
00475   gtk_label_set_text (GTK_LABEL (btw->type_label), type_label);
00476 }
00477 
00478 static void
00479 billterms_window_refresh (BillTermsWindow *btw)
00480 {
00481   GList *list, *node;
00482   GncBillTerm *term;
00483   GtkTreeView *view;
00484   GtkListStore *store;
00485   GtkTreeIter iter;
00486   GtkTreePath *path;
00487   GtkTreeSelection *selection;
00488   GtkTreeRowReference *reference = NULL;
00489 
00490   g_return_if_fail (btw);
00491   view = GTK_TREE_VIEW (btw->terms_view);
00492   store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
00493 
00494   /* Clear the list */
00495   gtk_list_store_clear (store);
00496   gnc_gui_component_clear_watches (btw->component_id);
00497 
00498   /* Add the items to the list */
00499   list = gncBillTermGetTerms (btw->book);
00500 
00501   /* If there are no terms, clear the term display */
00502   if (list == NULL) {
00503     btw->current_term = NULL;
00504     billterms_term_refresh (btw);
00505   } else {
00506     list = g_list_reverse (g_list_copy (list));
00507   }
00508 
00509   for ( node = list; node; node = node->next) {
00510     term = node->data;
00511     gnc_gui_component_watch_entity (btw->component_id,
00512                                     gncBillTermGetGUID (term),
00513                                     QOF_EVENT_MODIFY);
00514 
00515     gtk_list_store_prepend(store, &iter);
00516     gtk_list_store_set(store, &iter,
00517                        BILL_TERM_COL_NAME, gncBillTermGetName(term),
00518                        BILL_TERM_COL_TERM, term,
00519                        -1);
00520     if (term == btw->current_term) {
00521       path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
00522       reference = gtk_tree_row_reference_new(GTK_TREE_MODEL(store), path);
00523       gtk_tree_path_free(path);
00524     }
00525   }
00526 
00527   g_list_free (list);
00528 
00529   gnc_gui_component_watch_entity_type (btw->component_id,
00530                                        GNC_BILLTERM_MODULE_NAME,
00531                                        QOF_EVENT_CREATE | QOF_EVENT_DESTROY);
00532 
00533   if (reference) {
00534     path = gtk_tree_row_reference_get_path(reference);
00535     gtk_tree_row_reference_free(reference);
00536     if (path) {
00537       selection = gtk_tree_view_get_selection(view);
00538       gtk_tree_selection_select_path(selection, path);
00539       gtk_tree_view_scroll_to_cell(view, path, NULL, TRUE, 0.5, 0.0);
00540       gtk_tree_path_free(path);
00541     }
00542   }
00543 }
00544 
00545 static void
00546 billterm_selection_changed (GtkTreeSelection *selection,
00547                             BillTermsWindow  *btw) 
00548 {
00549   GncBillTerm *term = NULL;
00550   GtkTreeModel *model;
00551   GtkTreeIter iter;
00552 
00553   g_return_if_fail (btw);
00554 
00555   if (gtk_tree_selection_get_selected(selection, &model, &iter))
00556     gtk_tree_model_get(model, &iter, BILL_TERM_COL_TERM, &term, -1);
00557 
00558   /* If we've changed, then reset the term list */
00559   if (term != btw->current_term)
00560     btw->current_term = term;
00561 
00562   /* And force a refresh of the entries */
00563   billterms_term_refresh (btw);
00564 }
00565 
00566 static void
00567 billterm_selection_activated (GtkTreeView       *tree_view,
00568                               GtkTreePath       *path,
00569                               GtkTreeViewColumn *column,
00570                               BillTermsWindow   *btw)
00571 {
00572   new_billterm_dialog (btw, btw->current_term, NULL);
00573 }
00574 
00575 void
00576 billterms_new_term_cb (GtkButton *button, BillTermsWindow *btw)
00577 {
00578   g_return_if_fail (btw);
00579   new_billterm_dialog (btw, NULL, NULL);
00580 }
00581 
00582 void
00583 billterms_delete_term_cb (GtkButton *button, BillTermsWindow *btw)
00584 {
00585   g_return_if_fail (btw);
00586 
00587   if (!btw->current_term)
00588     return;
00589 
00590   if (gncBillTermGetRefcount (btw->current_term) > 0) {
00591     gnc_error_dialog (btw->dialog,
00592                       _("Term \"%s\" is in use.  You cannot delete it."),
00593                       gncBillTermGetName (btw->current_term));
00594     return;
00595   }
00596 
00597   if (gnc_verify_dialog (btw->dialog, FALSE,
00598                          _("Are you sure you want to delete \"%s\"?"),
00599                          gncBillTermGetName (btw->current_term))) {
00600     /* Ok, let's remove it */
00601     gnc_suspend_gui_refresh ();
00602     gncBillTermBeginEdit (btw->current_term);
00603     gncBillTermDestroy (btw->current_term);
00604     btw->current_term = NULL;
00605     gnc_resume_gui_refresh ();
00606   }
00607 }
00608 
00609 void
00610 billterms_edit_term_cb (GtkButton *button, BillTermsWindow *btw)
00611 {
00612   g_return_if_fail (btw);
00613   if (!btw->current_term)
00614     return;
00615   new_billterm_dialog (btw, btw->current_term, NULL);
00616 }
00617 
00618 static void
00619 billterms_window_refresh_handler (GHashTable *changes, gpointer data)
00620 {
00621   BillTermsWindow *btw = data;
00622 
00623   g_return_if_fail (data);
00624   billterms_window_refresh (btw);
00625 }
00626 
00627 static void
00628 billterms_window_close_handler (gpointer data)
00629 {
00630   BillTermsWindow *btw = data;
00631   g_return_if_fail (btw);
00632 
00633   gtk_widget_destroy (btw->dialog);
00634 }
00635 
00636 void
00637 billterms_window_close (GtkWidget *widget, gpointer data)
00638 {
00639   BillTermsWindow *btw = data;
00640 
00641   gnc_ui_billterms_window_destroy (btw);
00642 }
00643 
00644 void
00645 billterms_window_destroy_cb (GtkWidget *widget, gpointer data)
00646 {
00647   BillTermsWindow *btw = data;
00648 
00649   if (!btw) return;
00650 
00651   gnc_unregister_gui_component (btw->component_id);
00652 
00653   g_free (btw);
00654 }
00655 
00656 static gboolean
00657 find_handler (gpointer find_data, gpointer user_data)
00658 {
00659   BillTermsWindow *btw = user_data;
00660   GNCBook *book = find_data;
00661 
00662   return (btw != NULL && btw->book == book);
00663 }
00664 
00665 /* Create a billterms window */
00666 BillTermsWindow *
00667 gnc_ui_billterms_window_new (GNCBook *book)
00668 {
00669   BillTermsWindow *btw;
00670   GladeXML *xml;
00671   GtkWidget *widget;
00672   GtkTreeView *view;
00673   GtkTreeViewColumn *column;
00674   GtkCellRenderer *renderer;
00675   GtkListStore *store;
00676   GtkTreeSelection *selection;
00677 
00678   if (!book) return NULL;
00679 
00680   /*
00681    * Find an existing billterm window.  If found, bring it to
00682    * the front.  If we have an actual owner, then set it in
00683    * the window.
00684    */
00685   btw = gnc_find_first_gui_component (DIALOG_BILLTERMS_CM_CLASS,
00686                                       find_handler, book);
00687   if (btw) {
00688     gtk_window_present (GTK_WINDOW(btw->dialog));
00689     return btw;
00690   }
00691 
00692   /* Didn't find one -- create a new window */
00693   btw = g_new0 (BillTermsWindow, 1);
00694   btw->book = book;
00695 
00696   /* Open and read the XML */
00697   xml = gnc_glade_xml_new ("billterms.glade", "Terms Window");
00698   btw->dialog = glade_xml_get_widget (xml, "Terms Window");
00699   btw->terms_view = glade_xml_get_widget (xml, "terms_view");
00700   btw->desc_entry = glade_xml_get_widget (xml, "desc_entry");
00701   btw->type_label = glade_xml_get_widget (xml, "type_label");
00702   btw->term_vbox = glade_xml_get_widget (xml, "term_vbox");
00703 
00704   /* Initialize the view */
00705   view = GTK_TREE_VIEW(btw->terms_view);
00706   store = gtk_list_store_new (NUM_BILL_TERM_COLS, G_TYPE_STRING, G_TYPE_POINTER);
00707   gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
00708   g_object_unref(store);
00709 
00710   renderer = gtk_cell_renderer_text_new();
00711   column = gtk_tree_view_column_new_with_attributes("", renderer,
00712                                                     "text", BILL_TERM_COL_NAME,
00713                                                     NULL);
00714   gtk_tree_view_append_column(view, column);
00715 
00716   g_signal_connect(view, "row-activated",
00717                    G_CALLBACK(billterm_selection_activated), btw);
00718   selection = gtk_tree_view_get_selection(view);
00719   g_signal_connect(selection, "changed",
00720                    G_CALLBACK(billterm_selection_changed), btw);
00721 
00722   /* Initialize the notebook widgets */
00723   init_notebook_widgets (&btw->notebook, TRUE,
00724                          GTK_DIALOG (btw->dialog), btw);
00725 
00726   /* Attach the notebook */
00727   widget = glade_xml_get_widget (xml, "notebook_box");
00728   gtk_box_pack_start (GTK_BOX (widget), btw->notebook.notebook,
00729                       TRUE, TRUE, 0);
00730   g_object_unref (btw->notebook.notebook);
00731 
00732   /* Setup signals */
00733   glade_xml_signal_autoconnect_full( xml,
00734                                      gnc_glade_autoconnect_full_func,
00735                                      btw);
00736 
00737   /* register with component manager */
00738   btw->component_id =
00739     gnc_register_gui_component (DIALOG_BILLTERMS_CM_CLASS,
00740                                 billterms_window_refresh_handler,
00741                                 billterms_window_close_handler,
00742                                 btw);
00743 
00744   gtk_widget_show_all (btw->dialog);
00745   billterms_window_refresh (btw);
00746 
00747   return btw;
00748 }
00749 
00750 /* Destroy a billterms window */
00751 void
00752 gnc_ui_billterms_window_destroy (BillTermsWindow *btw)
00753 {
00754   if (!btw)
00755     return;
00756 
00757   gnc_close_gui_component (btw->component_id);
00758 }
00759 
00760 #if 0
00761 /* Create a new billterms by name */
00762 GncBillTerm *
00763 gnc_ui_billterms_new_from_name (GNCBook *book, const char *name)
00764 {
00765   BillTermsWindow *btw;
00766 
00767   if (!book) return NULL;
00768 
00769   btw = gnc_ui_billterms_window_new (book);
00770   if (!btw) return NULL;
00771 
00772   return new_billterm_dialog (btw, NULL, name);
00773 }
00774 #endif

Generated on Fri Jul 25 05:05:12 2008 for GnuCash by  doxygen 1.5.2