29 #include <glib/gi18n.h>
31 #include "dialog-utils.h"
32 #include "gnc-component-manager.h"
34 #include "gnc-gui-query.h"
38 #include "gnc-amount-edit.h"
42 #include "dialog-tax-table.h"
44 #define DIALOG_TAX_TABLE_CM_CLASS "tax-table-dialog"
45 #define GNC_PREFS_GROUP "dialogs.business.tax-tables"
49 TAX_TABLE_COL_NAME = 0,
50 TAX_TABLE_COL_POINTER,
56 TAX_ENTRY_COL_NAME = 0,
57 TAX_ENTRY_COL_POINTER,
62 void tax_table_new_table_cb (GtkButton *button,
TaxTableWindow *ttw);
63 void tax_table_delete_table_cb (GtkButton *button,
TaxTableWindow *ttw);
64 void tax_table_new_entry_cb (GtkButton *button,
TaxTableWindow *ttw);
65 void tax_table_edit_entry_cb (GtkButton *button,
TaxTableWindow *ttw);
66 void tax_table_delete_entry_cb (GtkButton *button,
TaxTableWindow *ttw);
67 void tax_table_window_close (GtkWidget *widget, gpointer data);
68 void tax_table_window_destroy_cb (GtkWidget *widget, gpointer data);
73 GtkWidget * names_view;
74 GtkWidget * entries_view;
85 GtkWidget * name_entry;
86 GtkWidget * amount_entry;
87 GtkWidget * acct_tree;
100 const char *name = NULL;
105 g_return_val_if_fail (ntt, FALSE);
113 name = gtk_entry_get_text (GTK_ENTRY (ntt->name_entry));
114 if (name == NULL || *name ==
'\0')
116 message = _(
"You must provide a name for this Tax Table.");
117 gnc_error_dialog (ntt->dialog,
"%s", message);
120 if (gncTaxTableLookupByName (ttw->book, name))
122 message = g_strdup_printf(_(
123 "You must provide a unique name for this Tax Table. "
124 "Your choice \"%s\" is already in use."), name);
125 gnc_error_dialog (ntt->dialog,
"%s", message);
132 amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (ntt->amount_entry));
135 gnc_numeric_create (100, 1)) > 0)
137 message = _(
"Percentage amount must be between -100 and 100.");
138 gnc_error_dialog (ntt->dialog,
"%s", message);
146 message = _(
"You must choose a Tax Account.");
147 gnc_error_dialog (ntt->dialog,
"%s", message);
151 gnc_suspend_gui_refresh ();
157 gncTaxTableBeginEdit (table);
158 gncTaxTableSetName (table, name);
160 ttw->current_table = table;
161 ntt->created_table = table;
164 gncTaxTableBeginEdit (ttw->current_table);
176 entry = gncTaxTableEntryCreate ();
177 gncTaxTableAddEntry (ttw->current_table, entry);
178 ttw->current_entry = entry;
181 gncTaxTableEntrySetAccount (entry, acc);
182 gncTaxTableEntrySetType (entry, ntt->type);
183 gncTaxTableEntrySetAmount (entry, amount);
187 gncTaxTableChanged (ttw->current_table);
188 gncTaxTableCommitEdit (ttw->current_table);
190 gnc_resume_gui_refresh();
195 combo_changed (GtkWidget *widget,
NewTaxTable *ntt)
199 g_return_if_fail(GTK_IS_COMBO_BOX(widget));
200 g_return_if_fail(ntt);
202 index = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
203 ntt->type = index + 1;
213 GtkWidget *box, *widget, *combo;
215 gint response, index;
217 if (!ttw)
return NULL;
218 if (new_table && entry)
return NULL;
223 ntt->new_table = new_table;
226 ntt->type = gncTaxTableEntryGetType (entry);
231 builder = gtk_builder_new();
232 gnc_builder_add_from_file (builder,
"dialog-tax-table.glade",
"type_liststore");
233 gnc_builder_add_from_file (builder,
"dialog-tax-table.glade",
"New Tax Table Dialog");
235 ntt->dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"New Tax Table Dialog"));
236 ntt->name_entry = GTK_WIDGET(gtk_builder_get_object (builder,
"name_entry"));
238 gtk_entry_set_text (GTK_ENTRY (ntt->name_entry), name);
241 combo = GTK_WIDGET(gtk_builder_get_object (builder,
"type_combobox"));
243 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), index - 1);
244 g_signal_connect (combo,
"changed", G_CALLBACK (combo_changed), ntt);
247 box = GTK_WIDGET(gtk_builder_get_object (builder,
"amount_box"));
248 ntt->amount_entry = widget = gnc_amount_edit_new ();
249 gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (widget), TRUE);
250 gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (widget), 100000);
251 gtk_box_pack_start (GTK_BOX (box), widget, TRUE, TRUE, 0);
253 box = GTK_WIDGET(gtk_builder_get_object (builder,
"acct_window"));
255 gtk_container_add (GTK_CONTAINER (box), ntt->acct_tree);
256 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(ntt->acct_tree), FALSE);
259 gtk_entry_set_activates_default(GTK_ENTRY (gnc_amount_edit_gtk_entry
260 (GNC_AMOUNT_EDIT (ntt->amount_entry))),
264 widget = GTK_WIDGET(gtk_builder_get_object (builder,
"value_label"));
265 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), ntt->amount_entry);
266 widget = GTK_WIDGET(gtk_builder_get_object (builder,
"account_label"));
267 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), ntt->acct_tree);
272 gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (ntt->amount_entry),
273 gncTaxTableEntryGetAmount (entry));
275 gncTaxTableEntryGetAccount (entry));
279 gtk_window_set_transient_for (GTK_WINDOW(ntt->dialog), GTK_WINDOW(ttw->dialog));
282 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, ntt);
285 gtk_widget_show_all (ntt->dialog);
286 if (new_table == FALSE)
288 gtk_widget_hide (GTK_WIDGET(gtk_builder_get_object (builder,
"table_title")));
289 gtk_widget_hide (GTK_WIDGET(gtk_builder_get_object (builder,
"table_name")));
290 gtk_widget_hide (GTK_WIDGET(gtk_builder_get_object (builder,
"spacer")));
291 gtk_widget_hide (ntt->name_entry);
293 widget = GTK_WIDGET(gtk_builder_get_object (builder,
"ttd_table"));
294 gtk_table_set_row_spacing (GTK_TABLE(widget), 0, 0);
295 gtk_table_set_row_spacing (GTK_TABLE(widget), 1, 0);
296 gtk_table_set_row_spacing (GTK_TABLE(widget), 2, 0);
297 gtk_widget_grab_focus (gnc_amount_edit_gtk_entry
298 (GNC_AMOUNT_EDIT (ntt->amount_entry)));
301 gtk_widget_grab_focus (ntt->name_entry);
304 gtk_widget_show (ntt->dialog);
309 response = gtk_dialog_run (GTK_DIALOG (ntt->dialog));
312 case GTK_RESPONSE_OK:
313 if (new_tax_table_ok_cb (ntt))
315 created_table = ntt->created_table;
325 g_object_unref(G_OBJECT(builder));
327 gtk_widget_destroy(ntt->dialog);
330 return created_table;
343 GtkTreeSelection *selection;
344 GtkTreeRowReference *reference = NULL;
347 g_return_if_fail (ttw);
349 view = GTK_TREE_VIEW (ttw->entries_view);
350 store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
353 selected_entry = ttw->current_entry;
354 gtk_list_store_clear (store);
355 if (ttw->current_table == NULL)
359 list = gncTaxTableGetEntries (ttw->current_table);
361 list = g_list_reverse (g_list_copy (list));
363 for (node = list ; node; node = node->next)
367 Account *acc = gncTaxTableEntryGetAccount (entry);
368 gnc_numeric amount = gncTaxTableEntryGetAmount (entry);
371 switch (gncTaxTableEntryGetType (entry))
375 g_strdup_printf (
"%s%%",
376 xaccPrintAmount (amount,
377 gnc_default_print_info (FALSE)));
381 g_strdup_printf (
"%s",
382 xaccPrintAmount (amount,
383 gnc_default_print_info (TRUE)));
387 gtk_list_store_prepend(store, &iter);
388 gtk_list_store_set(store, &iter,
389 TAX_ENTRY_COL_NAME, row_text[0],
390 TAX_ENTRY_COL_POINTER, entry,
391 TAX_ENTRY_COL_AMOUNT, row_text[1],
393 if (entry == selected_entry)
395 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
396 reference = gtk_tree_row_reference_new(GTK_TREE_MODEL(store), path);
397 gtk_tree_path_free(path);
400 g_free (row_text[0]);
401 g_free (row_text[1]);
406 path = gtk_tree_row_reference_get_path(reference);
407 gtk_tree_row_reference_free(reference);
410 selection = gtk_tree_view_get_selection(view);
411 gtk_tree_selection_select_path(selection, path);
412 gtk_tree_view_scroll_to_cell(view, path, NULL, TRUE, 0.5, 0.0);
413 gtk_tree_path_free(path);
426 GtkTreeSelection *selection;
427 GtkTreeRowReference *reference = NULL;
429 g_return_if_fail (ttw);
430 view = GTK_TREE_VIEW (ttw->names_view);
431 store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
434 gtk_list_store_clear(store);
436 gnc_gui_component_clear_watches (ttw->component_id);
439 list = gncTaxTableGetTables (ttw->book);
441 list = g_list_reverse (g_list_copy (list));
443 for (node = list; node; node = node->next)
447 gnc_gui_component_watch_entity (ttw->component_id,
451 gtk_list_store_prepend(store, &iter);
452 gtk_list_store_set(store, &iter,
453 TAX_TABLE_COL_NAME, gncTaxTableGetName (table),
454 TAX_TABLE_COL_POINTER, table,
456 if (table == ttw->current_table)
458 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
459 reference = gtk_tree_row_reference_new(GTK_TREE_MODEL(store), path);
460 gtk_tree_path_free(path);
467 gnc_gui_component_watch_entity_type (ttw->component_id,
468 GNC_TAXTABLE_MODULE_NAME,
469 QOF_EVENT_CREATE | QOF_EVENT_DESTROY);
473 path = gtk_tree_row_reference_get_path(reference);
474 gtk_tree_row_reference_free(reference);
477 selection = gtk_tree_view_get_selection(view);
478 gtk_tree_selection_select_path(selection, path);
479 gtk_tree_view_scroll_to_cell(view, path, NULL, TRUE, 0.5, 0.0);
480 gtk_tree_path_free(path);
484 tax_table_entries_refresh (ttw);
489 tax_table_selection_changed (GtkTreeSelection *selection,
497 g_return_if_fail (ttw);
499 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
502 gtk_tree_model_get(model, &iter, TAX_TABLE_COL_POINTER, &table, -1);
503 g_return_if_fail (table);
506 if (table != ttw->current_table)
508 ttw->current_table = table;
509 ttw->current_entry = NULL;
512 tax_table_entries_refresh (ttw);
516 tax_table_entry_selection_changed (GtkTreeSelection *selection,
523 g_return_if_fail (ttw);
525 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
527 ttw->current_entry = NULL;
531 gtk_tree_model_get(model, &iter, TAX_ENTRY_COL_POINTER, &ttw->current_entry, -1);
535 tax_table_entry_row_activated (GtkTreeView *tree_view,
537 GtkTreeViewColumn *column,
542 new_tax_table_dialog (ttw, FALSE, ttw->current_entry, NULL);
548 g_return_if_fail (ttw);
549 new_tax_table_dialog (ttw, TRUE, NULL, NULL);
553 tax_table_delete_table_cb (GtkButton *button,
TaxTableWindow *ttw)
555 g_return_if_fail (ttw);
557 if (!ttw->current_table)
560 if (gncTaxTableGetRefcount (ttw->current_table) > 0)
563 g_strdup_printf (_(
"Tax table \"%s\" is in use. You cannot delete it."),
564 gncTaxTableGetName (ttw->current_table));
565 gnc_error_dialog (ttw->dialog,
"%s", message);
570 if (gnc_verify_dialog (ttw->dialog, FALSE,
571 _(
"Are you sure you want to delete \"%s\"?"),
572 gncTaxTableGetName (ttw->current_table)))
575 gnc_suspend_gui_refresh ();
576 gncTaxTableBeginEdit (ttw->current_table);
577 gncTaxTableDestroy (ttw->current_table);
578 ttw->current_table = NULL;
579 ttw->current_entry = NULL;
580 gnc_resume_gui_refresh ();
587 g_return_if_fail (ttw);
588 if (!ttw->current_table)
590 new_tax_table_dialog (ttw, FALSE, NULL, NULL);
596 g_return_if_fail (ttw);
597 if (!ttw->current_entry)
599 new_tax_table_dialog (ttw, FALSE, ttw->current_entry, NULL);
603 tax_table_delete_entry_cb (GtkButton *button,
TaxTableWindow *ttw)
605 g_return_if_fail (ttw);
606 if (!ttw->current_table || !ttw->current_entry)
609 if (g_list_length (gncTaxTableGetEntries (ttw->current_table)) <= 1)
611 char *message = _(
"You cannot remove the last entry from the tax table. "
612 "Try deleting the tax table if you want to do that.");
613 gnc_error_dialog (ttw->dialog,
"%s", message);
617 if (gnc_verify_dialog (ttw->dialog, FALSE,
"%s",
618 _(
"Are you sure you want to delete this entry?")))
621 gnc_suspend_gui_refresh ();
622 gncTaxTableBeginEdit (ttw->current_table);
623 gncTaxTableRemoveEntry (ttw->current_table, ttw->current_entry);
624 gncTaxTableEntryDestroy (ttw->current_entry);
625 gncTaxTableChanged (ttw->current_table);
626 gncTaxTableCommitEdit (ttw->current_table);
627 ttw->current_entry = NULL;
628 gnc_resume_gui_refresh ();
633 tax_table_window_refresh_handler (GHashTable *changes, gpointer data)
637 g_return_if_fail (data);
638 tax_table_window_refresh (ttw);
642 tax_table_window_close_handler (gpointer data)
645 g_return_if_fail (ttw);
647 gtk_widget_destroy (ttw->dialog);
651 tax_table_window_close (GtkWidget *widget, gpointer data)
655 gnc_save_window_size (GNC_PREFS_GROUP, GTK_WINDOW (ttw->dialog));
656 gnc_ui_tax_table_window_destroy (ttw);
660 tax_table_window_destroy_cb (GtkWidget *widget, gpointer data)
666 gnc_unregister_gui_component (ttw->component_id);
672 find_handler (gpointer
find_data, gpointer user_data)
677 return (ttw != NULL && ttw->book == book);
682 gnc_ui_tax_table_window_new (
QofBook *book)
687 GtkTreeViewColumn *column;
688 GtkCellRenderer *renderer;
690 GtkTreeSelection *selection;
692 if (!book)
return NULL;
699 ttw = gnc_find_first_gui_component (DIALOG_TAX_TABLE_CM_CLASS, find_handler,
703 gtk_window_present (GTK_WINDOW(ttw->dialog));
712 builder = gtk_builder_new();
713 gnc_builder_add_from_file (builder,
"dialog-tax-table.glade",
"Tax Table Window");
714 ttw->dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"Tax Table Window"));
715 ttw->names_view = GTK_WIDGET(gtk_builder_get_object (builder,
"tax_tables_view"));
716 ttw->entries_view = GTK_WIDGET(gtk_builder_get_object (builder,
"tax_table_entries"));
719 view = GTK_TREE_VIEW(ttw->names_view);
720 store = gtk_list_store_new (NUM_TAX_TABLE_COLS, G_TYPE_STRING,
722 gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
723 g_object_unref(store);
725 renderer = gtk_cell_renderer_text_new();
726 column = gtk_tree_view_column_new_with_attributes(
"", renderer,
727 "text", TAX_TABLE_COL_NAME,
729 gtk_tree_view_append_column(view, column);
731 selection = gtk_tree_view_get_selection(view);
732 g_signal_connect(selection,
"changed",
733 G_CALLBACK(tax_table_selection_changed), ttw);
736 view = GTK_TREE_VIEW(ttw->entries_view);
737 store = gtk_list_store_new (NUM_TAX_ENTRY_COLS, G_TYPE_STRING,
738 G_TYPE_POINTER, G_TYPE_STRING);
739 gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
740 g_object_unref(store);
742 renderer = gtk_cell_renderer_text_new();
743 column = gtk_tree_view_column_new_with_attributes(
"", renderer,
744 "text", TAX_ENTRY_COL_NAME,
746 gtk_tree_view_append_column(view, column);
748 selection = gtk_tree_view_get_selection(view);
749 g_signal_connect(selection,
"changed",
750 G_CALLBACK(tax_table_entry_selection_changed), ttw);
751 g_signal_connect(view,
"row-activated",
752 G_CALLBACK(tax_table_entry_row_activated), ttw);
755 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, ttw);
759 gnc_register_gui_component (DIALOG_TAX_TABLE_CM_CLASS,
760 tax_table_window_refresh_handler,
761 tax_table_window_close_handler,
764 tax_table_window_refresh (ttw);
765 gnc_restore_window_size (GNC_PREFS_GROUP, GTK_WINDOW (ttw->dialog));
766 gtk_widget_show_all (ttw->dialog);
768 g_object_unref(G_OBJECT(builder));
780 gnc_close_gui_component (ttw->component_id);
785 gnc_ui_tax_table_new_from_name (
QofBook *book,
const char *name)
789 if (!book)
return NULL;
791 ttw = gnc_ui_tax_table_window_new (book);
792 if (!ttw)
return NULL;
794 return new_tax_table_dialog (ttw, TRUE, NULL, name);
utility functions for the GnuCash UI
gint gnc_numeric_compare(gnc_numeric a, gnc_numeric b)
gchar * gnc_account_get_full_name(const Account *account)
#define gncTaxTableGetGUID(x)
GtkTreeView implementation for gnucash account tree.
GtkTreeView * gnc_tree_view_account_new(gboolean show_root)
Tax Table programming interface.
gnc_numeric gnc_numeric_abs(gnc_numeric a)
void gnc_tree_view_account_set_selected_account(GncTreeViewAccount *view, Account *account)
Account * gnc_tree_view_account_get_selected_account(GncTreeViewAccount *view)