28 #include <glib/gi18n.h>
30 #include "dialog-utils.h"
31 #include "gnc-component-manager.h"
33 #include "gnc-gui-query.h"
38 #include "dialog-billterms.h"
40 #define DIALOG_BILLTERMS_CM_CLASS "billterms-dialog"
44 BILL_TERM_COL_NAME = 0,
50 void billterms_delete_term_cb (GtkButton *button,
BillTermsWindow *btw);
52 void billterms_window_close (GtkWidget *widget, gpointer data);
53 void billterms_window_destroy_cb (GtkWidget *widget, gpointer data);
54 void billterms_type_combobox_changed (GtkComboBox *cb, gpointer data);
61 GtkWidget * days_due_days;
62 GtkWidget * days_disc_days;
63 GtkWidget * days_disc;
66 GtkWidget * prox_due_day;
67 GtkWidget * prox_disc_day;
68 GtkWidget * prox_disc;
69 GtkWidget * prox_cutoff;
78 GtkWidget * terms_view;
79 GtkWidget * desc_entry;
80 GtkWidget * type_label;
81 GtkWidget * term_vbox;
92 GtkWidget * name_entry;
93 GtkWidget * desc_entry;
102 read_widget (GtkBuilder *builder,
char *name, gboolean read_only)
104 GtkWidget *widget = GTK_WIDGET(gtk_builder_get_object (builder, name));
108 gtk_editable_set_editable (GTK_EDITABLE (widget), FALSE);
109 adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
110 adj->step_increment = 0.0;
111 adj->page_increment = 0.0;
112 gtk_adjustment_changed (adj);
120 init_notebook_widgets (
BillTermNB *notebook, gboolean read_only,
121 GtkDialog *dialog, gpointer user_data)
127 builder = gtk_builder_new();
128 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"discount_adj");
129 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"discount_days_adj");
130 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"due_days_adj");
131 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"pdiscount_adj");
132 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"pdiscount_day_adj");
133 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"pdue_day_adj");
134 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"pcutoff_day_adj");
135 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"Term Notebook");
136 notebook->notebook = GTK_WIDGET(gtk_builder_get_object (builder,
"term_notebook"));
137 parent = GTK_WIDGET(gtk_builder_get_object (builder,
"Term Notebook"));
140 notebook->days_due_days = read_widget (builder,
"days:due_days", read_only);
141 notebook->days_disc_days = read_widget (builder,
"days:discount_days", read_only);
142 notebook->days_disc = read_widget (builder,
"days:discount", read_only);
145 notebook->prox_due_day = read_widget (builder,
"prox:due_day", read_only);
146 notebook->prox_disc_day = read_widget (builder,
"prox:discount_day", read_only);
147 notebook->prox_disc = read_widget (builder,
"prox:discount", read_only);
148 notebook->prox_cutoff = read_widget (builder,
"prox:cutoff_day", read_only);
151 g_object_ref (notebook->notebook);
152 gtk_container_remove (GTK_CONTAINER (parent), notebook->notebook);
153 g_object_unref(G_OBJECT(builder));
154 gtk_widget_destroy (parent);
166 fl = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
180 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), fl);
189 val = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (widget));
200 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), (gfloat)val);
211 term = nbt->this_term;
212 notebook = &nbt->notebook;
214 text = gtk_entry_get_text (GTK_ENTRY (nbt->desc_entry));
216 gncBillTermSetDescription (term, text);
218 gncBillTermSetType (nbt->this_term, nbt->notebook.type);
220 switch (nbt->notebook.type)
222 case GNC_TERM_TYPE_DAYS:
223 set_int (notebook->days_due_days, term, gncBillTermSetDueDays);
224 set_int (notebook->days_disc_days, term, gncBillTermSetDiscountDays);
225 set_numeric (notebook->days_disc, term, gncBillTermSetDiscount);
228 case GNC_TERM_TYPE_PROXIMO:
229 set_int (notebook->prox_due_day, term, gncBillTermSetDueDays);
230 set_int (notebook->prox_disc_day, term, gncBillTermSetDiscountDays);
231 set_numeric (notebook->prox_disc, term, gncBillTermSetDiscount);
232 set_int (notebook->prox_cutoff, term, gncBillTermSetCutoff);
236 return gncBillTermIsDirty (term);
242 gtk_entry_set_text (GTK_ENTRY (desc), gncBillTermGetDescription (term));
243 notebook->type = gncBillTermGetType (term);
245 switch (notebook->type)
247 case GNC_TERM_TYPE_DAYS:
248 get_int (notebook->days_due_days, term, gncBillTermGetDueDays);
249 get_int (notebook->days_disc_days, term, gncBillTermGetDiscountDays);
250 get_numeric (notebook->days_disc, term, gncBillTermGetDiscount);
253 case GNC_TERM_TYPE_PROXIMO:
254 get_int (notebook->prox_due_day, term, gncBillTermGetDueDays);
255 get_int (notebook->prox_disc_day, term, gncBillTermGetDiscountDays);
256 get_numeric (notebook->prox_disc, term, gncBillTermGetDiscount);
257 get_int (notebook->prox_cutoff, term, gncBillTermGetCutoff);
265 char *message = _(
"Discount days cannot be more than due days.");
268 gint days_due_days, days_disc_days;
269 gint prox_due_days, prox_disc_days;
276 notebook = &nbt->notebook;
280 days_due_days=gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (notebook->days_due_days));
281 days_disc_days=gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (notebook->days_disc_days));
282 prox_due_days=gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (notebook->prox_due_day));
283 prox_disc_days=gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (notebook->prox_disc_day));
285 switch (nbt->notebook.type)
287 case GNC_TERM_TYPE_DAYS:
288 if (days_due_days<days_disc_days)
290 gnc_error_dialog (nbt->dialog,
"%s", message);
294 case GNC_TERM_TYPE_PROXIMO:
295 if (prox_due_days<prox_disc_days)
297 gnc_error_dialog (nbt->dialog,
"%s", message);
310 const char *name = NULL;
313 g_return_val_if_fail (nbt, FALSE);
319 if (nbt->this_term == NULL)
321 name = gtk_entry_get_text (GTK_ENTRY (nbt->name_entry));
322 if (name == NULL || *name ==
'\0')
324 message = _(
"You must provide a name for this Billing Term.");
325 gnc_error_dialog (nbt->dialog,
"%s", message);
328 if (gncBillTermLookupByName (btw->book, name))
330 message = g_strdup_printf(_(
331 "You must provide a unique name for this Billing Term. "
332 "Your choice \"%s\" is already in use."), name);
333 gnc_error_dialog (nbt->dialog,
"%s", message);
340 if (!verify_term_ok (nbt))
343 gnc_suspend_gui_refresh ();
346 if (nbt->this_term == NULL)
348 nbt->this_term = gncBillTermCreate (btw->book);
349 gncBillTermBeginEdit (nbt->this_term);
350 gncBillTermSetName (nbt->this_term, name);
352 btw->current_term = nbt->this_term;
355 gncBillTermBeginEdit (btw->current_term);
358 if (ui_to_billterm (nbt))
359 gncBillTermChanged (btw->current_term);
362 gncBillTermCommitEdit (btw->current_term);
364 gnc_resume_gui_refresh();
371 g_return_if_fail (notebook->type > 0);
372 gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook->notebook),
377 maybe_set_type (
NewBillTerm *nbt, GncBillTermType type)
380 if (type == nbt->notebook.type)
384 nbt->notebook.type = type;
385 show_notebook (&nbt->notebook);
389 billterms_type_combobox_changed (GtkComboBox *cb, gpointer data)
394 value = gtk_combo_box_get_active(cb);
395 maybe_set_type (nbt, value + 1);
405 GtkWidget *box, *combo_box;
408 const gchar *dialog_name;
409 const gchar *dialog_desc;
410 const gchar *dialog_combo;
411 const gchar *dialog_nb;
413 if (!btw)
return NULL;
417 nbt->this_term = term;
422 dialog_name =
"New Term Dialog";
423 dialog_desc =
"description_entry";
424 dialog_combo =
"type_combobox";
425 dialog_nb =
"note_book_hbox";
429 dialog_name =
"Edit Term Dialog";
430 dialog_desc =
"entry_desc";
431 dialog_combo =
"type_combo";
432 dialog_nb =
"notebook_hbox";
434 builder = gtk_builder_new();
435 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"type_liststore");
436 gnc_builder_add_from_file (builder,
"dialog-billterms.glade", dialog_name);
437 nbt->dialog = GTK_WIDGET(gtk_builder_get_object (builder, dialog_name));
438 nbt->name_entry = GTK_WIDGET(gtk_builder_get_object (builder,
"name_entry"));
439 nbt->desc_entry = GTK_WIDGET(gtk_builder_get_object (builder, dialog_desc));
441 gtk_entry_set_text (GTK_ENTRY (nbt->name_entry), name);
444 init_notebook_widgets (&nbt->notebook, FALSE,
445 GTK_DIALOG (nbt->dialog), nbt);
448 box = GTK_WIDGET(gtk_builder_get_object (builder, dialog_nb));
449 gtk_box_pack_start (GTK_BOX (box), nbt->notebook.notebook, TRUE, TRUE, 0);
450 g_object_unref (nbt->notebook.notebook);
454 billterm_to_ui (term, nbt->desc_entry, &nbt->notebook);
456 nbt->notebook.type = GNC_TERM_TYPE_DAYS;
459 combo_box = GTK_WIDGET(gtk_builder_get_object (builder, dialog_combo));
460 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), nbt->notebook.type - 1);
463 show_notebook (&nbt->notebook);
466 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, nbt);
468 gtk_window_set_transient_for (GTK_WINDOW(nbt->dialog),
469 GTK_WINDOW(btw->dialog));
472 gtk_widget_show_all (nbt->dialog);
475 gtk_widget_grab_focus (nbt->desc_entry);
478 gtk_widget_grab_focus (nbt->name_entry);
483 response = gtk_dialog_run (GTK_DIALOG (nbt->dialog));
486 case GTK_RESPONSE_OK:
487 if (new_billterm_ok_cb (nbt))
489 created_term = nbt->this_term;
499 g_object_unref(G_OBJECT(builder));
501 gtk_widget_destroy(nbt->dialog);
514 g_return_if_fail (btw);
516 if (!btw->current_term)
518 gtk_widget_hide (btw->term_vbox);
522 gtk_widget_show_all (btw->term_vbox);
523 billterm_to_ui (btw->current_term, btw->desc_entry, &btw->notebook);
524 switch (gncBillTermGetType (btw->current_term))
526 case GNC_TERM_TYPE_DAYS:
527 type_label = _(
"Days");
529 case GNC_TERM_TYPE_PROXIMO:
530 type_label = _(
"Proximo");
533 type_label = _(
"Unknown");
536 show_notebook (&btw->notebook);
537 gtk_label_set_text (GTK_LABEL (btw->type_label), type_label);
549 GtkTreeSelection *selection;
550 GtkTreeRowReference *reference = NULL;
552 g_return_if_fail (btw);
553 view = GTK_TREE_VIEW (btw->terms_view);
554 store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
557 gtk_list_store_clear (store);
558 gnc_gui_component_clear_watches (btw->component_id);
561 list = gncBillTermGetTerms (btw->book);
566 btw->current_term = NULL;
567 billterms_term_refresh (btw);
571 list = g_list_reverse (g_list_copy (list));
574 for ( node = list; node; node = node->next)
577 gnc_gui_component_watch_entity (btw->component_id,
578 gncBillTermGetGUID (term),
581 gtk_list_store_prepend(store, &iter);
582 gtk_list_store_set(store, &iter,
583 BILL_TERM_COL_NAME, gncBillTermGetName(term),
584 BILL_TERM_COL_TERM, term,
586 if (term == btw->current_term)
588 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
589 reference = gtk_tree_row_reference_new(GTK_TREE_MODEL(store), path);
590 gtk_tree_path_free(path);
596 gnc_gui_component_watch_entity_type (btw->component_id,
597 GNC_BILLTERM_MODULE_NAME,
598 QOF_EVENT_CREATE | QOF_EVENT_DESTROY);
602 path = gtk_tree_row_reference_get_path(reference);
603 gtk_tree_row_reference_free(reference);
606 selection = gtk_tree_view_get_selection(view);
607 gtk_tree_selection_select_path(selection, path);
608 gtk_tree_view_scroll_to_cell(view, path, NULL, TRUE, 0.5, 0.0);
609 gtk_tree_path_free(path);
615 billterm_selection_changed (GtkTreeSelection *selection,
622 g_return_if_fail (btw);
624 if (gtk_tree_selection_get_selected(selection, &model, &iter))
625 gtk_tree_model_get(model, &iter, BILL_TERM_COL_TERM, &term, -1);
628 if (term != btw->current_term)
629 btw->current_term = term;
632 billterms_term_refresh (btw);
636 billterm_selection_activated (GtkTreeView *tree_view,
638 GtkTreeViewColumn *column,
641 new_billterm_dialog (btw, btw->current_term, NULL);
647 g_return_if_fail (btw);
648 new_billterm_dialog (btw, NULL, NULL);
654 g_return_if_fail (btw);
656 if (!btw->current_term)
659 if (gncBillTermGetRefcount (btw->current_term) > 0)
661 gnc_error_dialog (btw->dialog,
662 _(
"Term \"%s\" is in use. You cannot delete it."),
663 gncBillTermGetName (btw->current_term));
667 if (gnc_verify_dialog (btw->dialog, FALSE,
668 _(
"Are you sure you want to delete \"%s\"?"),
669 gncBillTermGetName (btw->current_term)))
672 gnc_suspend_gui_refresh ();
673 gncBillTermBeginEdit (btw->current_term);
674 gncBillTermDestroy (btw->current_term);
675 btw->current_term = NULL;
676 gnc_resume_gui_refresh ();
683 g_return_if_fail (btw);
684 if (!btw->current_term)
686 new_billterm_dialog (btw, btw->current_term, NULL);
690 billterms_window_refresh_handler (GHashTable *changes, gpointer data)
694 g_return_if_fail (data);
695 billterms_window_refresh (btw);
699 billterms_window_close_handler (gpointer data)
702 g_return_if_fail (btw);
704 gtk_widget_destroy (btw->dialog);
708 billterms_window_close (GtkWidget *widget, gpointer data)
712 gnc_ui_billterms_window_destroy (btw);
716 billterms_window_destroy_cb (GtkWidget *widget, gpointer data)
722 gnc_unregister_gui_component (btw->component_id);
728 find_handler (gpointer
find_data, gpointer user_data)
733 return (btw != NULL && btw->book == book);
738 gnc_ui_billterms_window_new (
QofBook *book)
744 GtkTreeViewColumn *column;
745 GtkCellRenderer *renderer;
747 GtkTreeSelection *selection;
749 if (!book)
return NULL;
756 btw = gnc_find_first_gui_component (DIALOG_BILLTERMS_CM_CLASS,
760 gtk_window_present (GTK_WINDOW(btw->dialog));
769 builder = gtk_builder_new();
770 gnc_builder_add_from_file (builder,
"dialog-billterms.glade",
"Terms Window");
771 btw->dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"Terms Window"));
772 btw->terms_view = GTK_WIDGET(gtk_builder_get_object (builder,
"terms_view"));
773 btw->desc_entry = GTK_WIDGET(gtk_builder_get_object (builder,
"desc_entry"));
774 btw->type_label = GTK_WIDGET(gtk_builder_get_object (builder,
"type_label"));
775 btw->term_vbox = GTK_WIDGET(gtk_builder_get_object (builder,
"term_vbox"));
778 view = GTK_TREE_VIEW(btw->terms_view);
779 store = gtk_list_store_new (NUM_BILL_TERM_COLS, G_TYPE_STRING, G_TYPE_POINTER);
780 gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
781 g_object_unref(store);
783 renderer = gtk_cell_renderer_text_new();
784 column = gtk_tree_view_column_new_with_attributes(
"", renderer,
785 "text", BILL_TERM_COL_NAME,
787 gtk_tree_view_append_column(view, column);
789 g_signal_connect(view,
"row-activated",
790 G_CALLBACK(billterm_selection_activated), btw);
791 selection = gtk_tree_view_get_selection(view);
792 g_signal_connect(selection,
"changed",
793 G_CALLBACK(billterm_selection_changed), btw);
796 init_notebook_widgets (&btw->notebook, TRUE,
797 GTK_DIALOG (btw->dialog), btw);
800 widget = GTK_WIDGET(gtk_builder_get_object (builder,
"notebook_box"));
801 gtk_box_pack_start (GTK_BOX (widget), btw->notebook.notebook,
803 g_object_unref (btw->notebook.notebook);
806 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, btw);
810 gnc_register_gui_component (DIALOG_BILLTERMS_CM_CLASS,
811 billterms_window_refresh_handler,
812 billterms_window_close_handler,
815 gtk_widget_show_all (btw->dialog);
816 billterms_window_refresh (btw);
818 g_object_unref(G_OBJECT(builder));
830 gnc_close_gui_component (btw->component_id);
836 gnc_ui_billterms_new_from_name (
QofBook *book,
const char *name)
840 if (!book)
return NULL;
842 btw = gnc_ui_billterms_window_new (book);
843 if (!btw)
return NULL;
845 return new_billterm_dialog (btw, NULL, name);
gnc_numeric double_to_gnc_numeric(double n, gint64 denom, gint how)
utility functions for the GnuCash UI
gdouble gnc_numeric_to_double(gnc_numeric n)