GnuCash  5.6-150-g038405b370+
dialog-tax-info.c
1 /********************************************************************\
2  * dialog-tax-info.c -- tax information dialog *
3  * Copyright (C) 2001 Gnumatic, Inc. *
4  * Author: Dave Peticolas <dave@krondo.com> *
5  * *
6  * *
7  * updated by J. Alex Aycinena, July 2009 *
8  * *
9  * This program is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU General Public License as *
11  * published by the Free Software Foundation; either version 2 of *
12  * the License, or (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License*
20  * along with this program; if not, contact: *
21  * *
22  * Free Software Foundation Voice: +1-617-542-5942 *
23  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
24  * Boston, MA 02110-1301, USA gnu@gnu.org *
25 \********************************************************************/
26 
27 #include <config.h>
28 
29 #include <gtk/gtk.h>
30 #include <glib/gi18n.h>
31 #include <libguile.h>
32 #include "guile-mappings.h"
33 #include "gnc-guile-utils.h"
34 
35 #include "Account.h"
36 #include "gnc-ui-util.h"
37 #include "dialog-utils.h"
38 #include "gnc-locale-tax.h"
39 #include "gnc-prefs.h"
40 #include "gnc-tree-view-account.h"
41 #include "gnc-component-manager.h"
42 #include "gnc-session.h"
43 #include "qof.h"
44 #include "gnc-ui.h"
45 
46 #define DIALOG_TAX_INFO_CM_CLASS "dialog-tax-info"
47 #define GNC_PREFS_GROUP "dialogs.tax-info"
48 #define GNC_PREF_PANED_POS "paned-position"
49 
50 enum
51 {
52  INCOME,
53  EXPENSE,
54  ASSET,
55  LIAB_EQ,
56  N_CATEGORIES
57 };
58 
59 static struct
60 {
61  SCM payer_name_source;
62  SCM form;
63  SCM description;
64  SCM help;
65  SCM line_data;
66  SCM last_year;
67  SCM copy;
68 
69  SCM codes;
70 
71  SCM tax_entity_type;
72  SCM tax_entity_desc;
73 
74  SCM tax_entity_types;
75 } getters;
76 
77 typedef struct
78 {
79  char *type_code;
80  char *type;
81  char *description;
82  char *combo_box_entry;
83 } TaxTypeInfo;
84 
85 typedef struct
86 {
87  char *code;
88  char *payer_name_source;
89  char *form;
90  char *description;
91  char *help;
92  gboolean copy;
93 } TXFInfo;
94 
95 typedef struct
96 {
97  GtkWidget * dialog;
98 
99  GtkWidget * entity_name_display;
100  GtkWidget * entity_name_entry;
101  GtkWidget * entity_type_display;
102  GtkWidget * entity_type_combo;
103  GtkWidget * tax_identity_edit_button;
104 
105  GtkWidget * acct_info;
106  GtkWidget * income_radio;
107  GtkWidget * expense_radio;
108  GtkWidget * asset_radio;
109  GtkWidget * liab_eq_radio;
110  GtkWidget * account_treeview;
111  GtkWidget * select_button;
112  GtkWidget * num_acct_label;
113  GtkWidget * apply_button;
114 
115  GtkWidget * txf_info;
116  GtkWidget * tax_related_button;
117  GtkWidget * txf_vbox;
118  GtkWidget * txf_category_view;
119  GtkWidget * txf_help_text;
120  GtkWidget * help_scroll;
121  GtkWidget * payer_vbox;
122  GtkWidget * pns_vbox;
123  GtkWidget * current_account_button;
124  GtkWidget * parent_account_button;
125  GtkWidget * copy_vbox;
126  GtkWidget * copy_spin_button;
127 
128  GList * entity_type_infos;
129  GList * income_txf_infos;
130  GList * expense_txf_infos;
131  GList * asset_txf_infos;
132  GList * liab_eq_txf_infos;
133 
134  const gchar * tax_name;
135  const gchar * tax_type;
136  const gchar * tax_type_combo_text;
137  const gchar * default_tax_type;
138 
139  QofBook *this_book;
140 
141  gboolean changed;
142  gboolean tax_type_changed;
143 
144  GNCAccountType account_type;
145 } TaxInfoDialog;
146 
147 static void
148 initialize_getters (void)
149 {
150  gnc_locale_tax_init();
151 
152  getters.payer_name_source = scm_c_eval_string ("gnc:txf-get-payer-name-source");
153  getters.form = scm_c_eval_string ("gnc:txf-get-form");
154  getters.description = scm_c_eval_string ("gnc:txf-get-description");
155  getters.help = scm_c_eval_string ("gnc:txf-get-help");
156  getters.line_data = scm_c_eval_string ("gnc:txf-get-line-data");
157  getters.last_year = scm_c_eval_string ("gnc:txf-get-last-year");
158  getters.copy = scm_c_eval_string ("gnc:txf-get-multiple");
159 
160  getters.codes = scm_c_eval_string ("gnc:txf-get-codes");
161 
162  getters.tax_entity_type = scm_c_eval_string ("gnc:txf-get-tax-entity-type");
163  getters.tax_entity_desc = scm_c_eval_string
164  ("gnc:txf-get-tax-entity-type-description");
165 
166  getters.tax_entity_types = scm_c_eval_string
167  ("gnc:txf-get-tax-entity-type-codes");
168 }
169 
170 static void
171 destroy_tax_type_info (gpointer data)
172 {
173  TaxTypeInfo *tax_type = data;
174 
175  g_free (tax_type->type_code);
176  tax_type->type_code = NULL;
177 
178  g_free (tax_type->type);
179  tax_type->type = NULL;
180 
181  g_free (tax_type->description);
182  tax_type->description = NULL;
183 
184  g_free (tax_type->combo_box_entry);
185  tax_type->combo_box_entry = NULL;
186 
187  g_free (tax_type);
188 }
189 
190 static inline void
191 destroy_tax_type_infos (GList *types)
192 {
193  g_list_free_full (types, destroy_tax_type_info);
194 }
195 
196 static void
197 destroy_txf_info (gpointer data)
198 {
199  TXFInfo *txf_info = data;
200 
201  g_free (txf_info->code);
202  txf_info->code = NULL;
203 
204  g_free (txf_info->payer_name_source);
205  txf_info->payer_name_source = NULL;
206 
207  g_free (txf_info->form);
208  txf_info->form = NULL;
209 
210  g_free (txf_info->description);
211  txf_info->description = NULL;
212 
213  g_free (txf_info->help);
214  txf_info->help = NULL;
215 
216  g_free (txf_info);
217 }
218 
219 static inline void
220 destroy_txf_infos (GList *infos)
221 {
222  g_list_free_full (infos, destroy_txf_info);
223 }
224 
225 static void
226 gnc_tax_info_set_changed (TaxInfoDialog *ti_dialog, gboolean changed)
227 {
228  ti_dialog->changed = changed;
229  gtk_widget_set_sensitive (ti_dialog->apply_button, changed);
230 }
231 
232 static GList *
233 load_txf_info (gint acct_category, TaxInfoDialog *ti_dialog)
234 {
235  GList *infos = NULL;
236  SCM tax_entity_type;
237  SCM category;
238  SCM codes;
239 
240  if (ti_dialog->tax_type == NULL ||
241  (g_strcmp0 (ti_dialog->tax_type, "") == 0))
242  {
243  destroy_txf_infos (infos);
244  return NULL;
245  }
246  else
247  {
248  tax_entity_type = scm_from_utf8_string (ti_dialog->tax_type);
249  }
250 
251  switch (acct_category)
252  {
253  case INCOME:
254  category = scm_c_eval_string ("txf-income-categories");
255  break;
256  case EXPENSE:
257  category = scm_c_eval_string ("txf-expense-categories");
258  break;
259  case ASSET:
260  category = scm_c_eval_string ("txf-asset-categories");
261  break;
262  case LIAB_EQ:
263  category = scm_c_eval_string ("txf-liab-eq-categories");
264  break;
265  default:
266  destroy_txf_infos (infos);
267  return NULL;
268  }
269 
270  if (category == SCM_UNDEFINED)
271  {
272  destroy_txf_infos (infos);
273  return NULL;
274  }
275 
276  codes = scm_call_2 (getters.codes, category, tax_entity_type);
277  if (!scm_is_list (codes))
278  {
279  destroy_txf_infos (infos);
280  return NULL;
281  }
282 
283  while (!scm_is_null (codes))
284  {
285  TXFInfo *txf_info;
286  SCM code_scm;
287  const gchar *last_yr = _("Last Valid Year: ");
288  const gchar *form_line = _("Form Line Data: ");
289  /* Translators: Tax Code */
290  const gchar *code_line_word = _("Code");
291  const gchar *code_line_colon = ": ";
292  const gchar *prefix = "N";
293  gchar *str = NULL;
294  gchar *num_code = NULL;
295  gchar *form_line_data = NULL;
296  gchar *help_text = NULL;
297  SCM scm;
298  gint year;
299  gboolean cpy;
300 
301  code_scm = SCM_CAR (codes);
302  codes = SCM_CDR (codes);
303 
304  scm = scm_call_3 (getters.payer_name_source, category, code_scm,
305  tax_entity_type);
306  if (scm_is_symbol(scm))
307  str = gnc_scm_symbol_to_locale_string (scm);
308  else
309  str = g_strdup ("");
310  if (g_strcmp0 (str, "not-impl") == 0)
311  {
312  g_free (str);
313  continue;
314  }
315 
316  txf_info = g_new0 (TXFInfo, 1);
317 
318  if (g_strcmp0 (str, "none") == 0)
319  txf_info->payer_name_source = NULL;
320  else
321  txf_info->payer_name_source = g_strdup (str);
322  g_free (str);
323 
324  if (scm_is_symbol(code_scm))
325  str = gnc_scm_symbol_to_locale_string (code_scm);
326  else
327  str = g_strdup ("");
328  txf_info->code = g_strdup (str);
329  if (g_str_has_prefix (str, prefix))
330  {
331  const gchar *num_code_tmp;
332  num_code_tmp = g_strdup (str);
333  num_code_tmp++; /* to lose the leading N */
334  num_code = g_strdup (num_code_tmp);
335  num_code_tmp--;
336  g_free ((gpointer *) num_code_tmp);
337  }
338  else
339  num_code = g_strdup (str);
340  g_free (str);
341 
342  scm = scm_call_3 (getters.form, category, code_scm, tax_entity_type);
343  if (scm_is_string(scm))
344  txf_info->form = gnc_scm_to_utf8_string(scm);
345  else
346  txf_info->form = g_strdup ("");
347 
348  scm = scm_call_3 (getters.description, category, code_scm, tax_entity_type);
349  if (scm_is_string(scm))
350  txf_info->description = gnc_scm_to_utf8_string(scm);
351  else
352  txf_info->description = g_strdup ("");
353 
354  scm = scm_call_2 (getters.help, category, code_scm);
355  if (scm_is_string(scm))
356  help_text = gnc_scm_to_utf8_string(scm);
357  else
358  help_text = g_strdup ("");
359 
360  scm = scm_call_3 (getters.last_year, category, code_scm, tax_entity_type);
361  year = scm_is_bool (scm) ? 0 : scm_to_int(scm);
362  scm = scm_call_3 (getters.line_data, category, code_scm, tax_entity_type);
363  if (scm_is_list (scm))
364  {
365  const gchar *now = _("now");
366  gchar *until;
367 
368  until = (gchar *) now;
369  form_line_data = g_strconcat ("\n", "\n", form_line, NULL);
370  while (!scm_is_null (scm))
371  {
372  SCM year_scm;
373  gint line_year;
374  gchar *line = NULL;
375  gchar *temp = NULL;
376  gchar *temp2 = NULL;
377 
378  year_scm = SCM_CAR (scm);
379  scm = SCM_CDR (scm);
380 
381  line_year = scm_is_bool (SCM_CAR (year_scm)) ? 0 :
382  scm_to_int (SCM_CAR (year_scm));
383  if (scm_is_string((SCM_CAR (SCM_CDR (year_scm)))))
384  line = gnc_scm_to_utf8_string((SCM_CAR (SCM_CDR
385  (year_scm))));
386  else
387  line = g_strdup ("");
388  temp2 = g_strdup_printf ("%d", line_year);
389  temp = g_strconcat (form_line_data, "\n", temp2, " - ",
390  ((year != 0) && (until == now))
391  ? g_strdup_printf("%d", year)
392  : until,
393  " ", line, NULL);
394  if (until != now)
395  g_free (until);
396  until = g_strdup_printf ("%d", (line_year - 1));
397  g_free (form_line_data);
398  form_line_data = g_strdup (temp);
399  g_free (line);
400  g_free (temp);
401  g_free (temp2);
402  }
403  if (g_strcmp0 (until, now) != 0)
404  g_free (until);
405  }
406  if (year != 0)
407  {
408  gchar *temp = g_strdup_printf("%d", year);
409  if (form_line_data != NULL)
410  txf_info->help = g_strconcat (last_yr, temp, "\n", "\n",
411  help_text, "\n", "\n",
412  code_line_word,
413  code_line_colon, num_code,
414  form_line_data, NULL);
415  else
416  txf_info->help = g_strconcat (last_yr, temp, "\n", "\n",
417  help_text, "\n", "\n",
418  code_line_word,
419  code_line_colon, num_code, NULL);
420  g_free (temp);
421  }
422  else
423  {
424  if (form_line_data != NULL)
425  txf_info->help = g_strconcat (help_text, "\n", "\n",
426  code_line_word,
427  code_line_colon, num_code,
428  form_line_data, NULL);
429  else
430  txf_info->help = g_strconcat (help_text, "\n", "\n",
431  code_line_word,
432  code_line_colon, num_code, NULL);
433  }
434 
435  g_free (num_code);
436  g_free (help_text);
437  g_free (form_line_data);
438 
439  scm = scm_call_3 (getters.copy, category, code_scm, tax_entity_type);
440  cpy = scm_is_bool (scm) ? (scm_is_false (scm) ? FALSE : TRUE) : FALSE;
441  txf_info->copy = cpy;
442 
443  infos = g_list_prepend (infos, txf_info);
444  }
445  return g_list_reverse (infos);
446 }
447 
448 static GList *
449 tax_infos (TaxInfoDialog *ti_dialog)
450 {
451  return
452  (ti_dialog->account_type == ACCT_TYPE_INCOME)
453  ? ti_dialog->income_txf_infos :
454  ((ti_dialog->account_type == ACCT_TYPE_EXPENSE)
455  ? ti_dialog->expense_txf_infos :
456  (((ti_dialog->account_type == ACCT_TYPE_ASSET)
457  ? ti_dialog->asset_txf_infos :
458  ti_dialog->liab_eq_txf_infos)));
459 }
460 
461 static void
462 load_tax_entity_type_list (TaxInfoDialog *ti_dialog)
463 {
464  GList *types = NULL;
465  SCM tax_types;
466 
467  ti_dialog->tax_type_combo_text = NULL;
468  tax_types = scm_call_0 (getters.tax_entity_types);
469  if (!scm_is_list (tax_types))
470  {
471  destroy_tax_type_infos (types);
472  return;
473  }
474 
475  while (!scm_is_null (tax_types))
476  {
477  TaxTypeInfo *tax_type_info;
478  SCM type_scm;
479  SCM scm;
480 
481  type_scm = SCM_CAR (tax_types);
482  tax_types = SCM_CDR (tax_types);
483 
484  ti_dialog->default_tax_type = NULL;
485 
486  tax_type_info = g_new0 (TaxTypeInfo, 1);
487 
488  if (scm_is_symbol(type_scm))
489  tax_type_info->type_code = gnc_scm_symbol_to_locale_string (type_scm);
490  else
491  tax_type_info->type_code = g_strdup ("");
492 
493  scm = scm_call_1 (getters.tax_entity_type, type_scm);
494  if (scm_is_string(scm))
495  tax_type_info->type = gnc_scm_to_utf8_string(scm);
496  else
497  tax_type_info->type = g_strdup ("");
498 
499  scm = scm_call_1 (getters.tax_entity_desc, type_scm);
500  if (scm_is_string(scm))
501  tax_type_info->description = gnc_scm_to_utf8_string(scm);
502  else
503  tax_type_info->description = g_strdup ("");
504 
505  tax_type_info->combo_box_entry = g_strconcat(tax_type_info->type,
506  " - ",
507  tax_type_info->description, NULL);
508  /* save combo text for current tax type code */
509  if (g_strcmp0 (ti_dialog->tax_type, tax_type_info->type_code) == 0)
510  ti_dialog->tax_type_combo_text = tax_type_info->combo_box_entry;
511  /* the last will be default */
512  ti_dialog->default_tax_type = tax_type_info->combo_box_entry;
513 
514  types = g_list_prepend (types, tax_type_info);
515  }
516  ti_dialog->entity_type_infos = g_list_reverse (types);
517 }
518 
519 static void
520 load_category_list (TaxInfoDialog *ti_dialog)
521 {
522  GtkTreeView *view;
523  GtkListStore *store;
524  GtkTreeIter iter;
525  GList *codes;
526 
527  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
528  store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
529  g_object_ref(store);
530  gtk_tree_view_set_model(view, NULL);
531 
532  gtk_list_store_clear(store);
533 
534  codes = tax_infos (ti_dialog);
535  for ( ; codes; codes = codes->next)
536  {
537  TXFInfo *txf_info = codes->data;
538 
539  gtk_list_store_append(store, &iter);
540  gtk_list_store_set(store, &iter,
541  0, txf_info->form,
542  1, txf_info->description,
543  -1);
544  }
545 
546  gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
547  g_object_unref(store);
548 }
549 
550 static void
551 clear_gui (TaxInfoDialog *ti_dialog)
552 {
553  GtkTreeView *view;
554  GtkTreeSelection *selection;
555 
556  gtk_toggle_button_set_active
557  (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button), FALSE);
558 
559  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
560  selection = gtk_tree_view_get_selection(view);
561  gtk_tree_selection_unselect_all(selection);
562 
563  gtk_toggle_button_set_active
564  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
565 
566  gtk_spin_button_set_value
567  (GTK_SPIN_BUTTON (ti_dialog->copy_spin_button), 1);
568 }
569 
570 static gboolean
571 gnc_tax_info_dialog_account_filter_func (Account *account,
572  gpointer data)
573 {
574  TaxInfoDialog *dialog = data;
576  gboolean included = FALSE;
577 
578  if ((dialog->account_type == ACCT_TYPE_INCOME) ||
579  (dialog->account_type == ACCT_TYPE_EXPENSE))
580  included = (xaccAccountGetType (account) == dialog->account_type);
581  else if (dialog->account_type == ACCT_TYPE_ASSET)
582  included = (ACCT_TYPE_ASSET == fund_acct_type);
583  else if (dialog->account_type == ACCT_TYPE_LIABILITY)
584  included = ((ACCT_TYPE_LIABILITY == fund_acct_type) ||
585  (ACCT_TYPE_EQUITY == fund_acct_type));
586  else
587  included = FALSE;
588  return included;
589 }
590 
591 static TXFInfo *
592 txf_infos_find_code (GList *infos, const char *code)
593 {
594  for (; infos; infos = infos->next)
595  {
596  TXFInfo *info = infos->data;
597 
598  if (g_strcmp0 (code, info->code) == 0)
599  return info;
600  }
601 
602  return NULL;
603 }
604 
605 static void
606 account_to_gui (TaxInfoDialog *ti_dialog, Account *account)
607 {
608  GtkTreeView *view;
609  GtkTreeSelection *selection;
610  GtkTreePath *path;
611  gboolean tax_related;
612  const char *str;
613  TXFInfo *info;
614  GList *infos;
615  gint index = 0;
616 
617  if (!account)
618  {
619  clear_gui (ti_dialog);
620  return;
621  }
622 
623  tax_related = xaccAccountGetTaxRelated (account);
624  gtk_toggle_button_set_active
625  (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button), tax_related);
626 
627  infos = tax_infos (ti_dialog);
628 
629  str = xaccAccountGetTaxUSCode (account);
630  info = txf_infos_find_code (infos, str);
631  if (info)
632  index = g_list_index (infos, info);
633 
634  if (index < 0)
635  index = 0;
636 
637  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
638  selection = gtk_tree_view_get_selection(view);
639  path = gtk_tree_path_new_from_indices(index, -1);
640  gtk_tree_selection_select_path(selection, path);
641  gtk_tree_view_scroll_to_cell(view, path, NULL, TRUE, 0.5, 0);
642  gtk_tree_path_free(path);
643 
644  str = xaccAccountGetTaxUSPayerNameSource (account);
645  if (g_strcmp0 (str, "parent") == 0)
646  gtk_toggle_button_set_active
647  (GTK_TOGGLE_BUTTON (ti_dialog->parent_account_button), TRUE);
648  else
649  gtk_toggle_button_set_active
650  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
651 
652  gtk_spin_button_set_value
653  (GTK_SPIN_BUTTON (ti_dialog->copy_spin_button),
654  (gdouble) xaccAccountGetTaxUSCopyNumber (account));
655 }
656 
657 static void
658 gui_to_accounts (TaxInfoDialog *ti_dialog)
659 {
660  GtkTreeView *view;
661  GtkTreeModel *model;
662  GtkTreeSelection *selection;
663  GtkTreePath *path;
664  GtkTreeIter iter;
665  gint *indices;
666  gboolean tax_related;
667  const char *code;
668  const char *pns;
669  GList *accounts;
670  TXFInfo *info;
671  GList *infos;
672  GList *node;
673  gint64 copy_number;
674 
675  tax_related = gtk_toggle_button_get_active
676  (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button));
677 
678  infos = tax_infos (ti_dialog);
679 
680  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
681  selection = gtk_tree_view_get_selection(view);
682  if (!gtk_tree_selection_get_selected(selection, &model, &iter))
683  return;
684  path = gtk_tree_model_get_path(model, &iter);
685  indices = gtk_tree_path_get_indices(path);
686  info = g_list_nth_data (infos, indices[0]);
687  gtk_tree_path_free(path);
688  g_return_if_fail (info != NULL);
689 
690  code = tax_related ? info->code : NULL;
691 
692  if (tax_related && info->payer_name_source)
693  {
694  gboolean current;
695 
696  current = gtk_toggle_button_get_active
697  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button));
698 
699  pns = current ? "current" : "parent";
700  }
701  else
702  pns = NULL;
703 
704  if (tax_related && info->copy)
705  {
706  copy_number = gtk_spin_button_get_value_as_int
707  (GTK_SPIN_BUTTON (ti_dialog->copy_spin_button));
708  }
709  else
710  copy_number = 0;/* setting to zero removes slot */
711 
713  (GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview));
714 
715  for (node = accounts; node; node = node->next)
716  {
717  Account *account = node->data;
718 
719  xaccAccountBeginEdit (account);
720 
721  xaccAccountSetTaxRelated (account, tax_related);
722  xaccAccountSetTaxUSPayerNameSource (account, pns);
723  xaccAccountSetTaxUSCopyNumber (account, copy_number);
724  /* USCode is last because it removes TaxUS KVP if not tax_related */
725  xaccAccountSetTaxUSCode (account, code);
726 
727  xaccAccountCommitEdit (account);
728  }
729  g_list_free (accounts);
730 }
731 
732 static void
733 identity_edit_destroy_cb (GtkDialog *dialog, gpointer data)
734 {
735  TaxInfoDialog *ti_dialog = data;
736 
737  ti_dialog->entity_name_entry = NULL;
738  ti_dialog->entity_type_combo = NULL;
739 
740  gtk_widget_destroy(GTK_WIDGET(dialog));
741 }
742 
743 static void
744 window_destroy_cb (GtkWidget *object, gpointer data)
745 {
746  TaxInfoDialog *ti_dialog = data;
747 
748  gnc_unregister_gui_component_by_data (DIALOG_TAX_INFO_CM_CLASS, ti_dialog);
749 
750  destroy_tax_type_infos (ti_dialog->entity_type_infos);
751  ti_dialog->entity_type_infos = NULL;
752 
753  destroy_txf_infos (ti_dialog->income_txf_infos);
754  ti_dialog->income_txf_infos = NULL;
755 
756  destroy_txf_infos (ti_dialog->expense_txf_infos);
757  ti_dialog->expense_txf_infos = NULL;
758 
759  destroy_txf_infos (ti_dialog->asset_txf_infos);
760  ti_dialog->asset_txf_infos = NULL;
761 
762  destroy_txf_infos (ti_dialog->liab_eq_txf_infos);
763  ti_dialog->liab_eq_txf_infos = NULL;
764 
765  g_free (ti_dialog);
766 }
767 
768 static void
769 cursor_changed_cb (GtkWidget *widget, gpointer data)
770 {
771  TaxInfoDialog *ti_dialog = data;
772  GncTreeViewAccount *account_tree;
773  Account *account;
774  gint num_children;
775 
776  account_tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
777  account = gnc_tree_view_account_get_cursor_account (account_tree);
778  if (!account)
779  {
780  gtk_widget_set_sensitive(ti_dialog->select_button, FALSE);
781  return;
782  }
783 
784  num_children = gnc_tree_view_account_count_children(account_tree, account);
785  gtk_widget_set_sensitive(ti_dialog->select_button, num_children > 0);
786 }
787 
788 static void
789 select_subaccounts_clicked (GtkWidget *widget, gpointer data)
790 {
791  TaxInfoDialog *ti_dialog = data;
792  GncTreeViewAccount *account_tree;
793  Account *account;
794 
795  account_tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
796  account = gnc_tree_view_account_get_cursor_account (account_tree);
797  if (!account)
798  return;
799 
800  gnc_tree_view_account_select_subaccounts (account_tree, account);
801 
802  gtk_widget_grab_focus (ti_dialog->account_treeview);
803 }
804 
805 static void
806 gnc_tax_info_dialog_response (GtkDialog *dialog, gint response, gpointer data)
807 {
808  TaxInfoDialog *ti_dialog = data;
809 
810  if (ti_dialog->changed && (response == GTK_RESPONSE_APPLY || response == GTK_RESPONSE_OK))
811  gui_to_accounts (ti_dialog);
812 
813  if (response != GTK_RESPONSE_APPLY)
814  gnc_close_gui_component_by_data (DIALOG_TAX_INFO_CM_CLASS, ti_dialog);
815 }
816 
817 static void
818 tax_info_show_acct_type_accounts (TaxInfoDialog *ti_dialog)
819 {
820  GncTreeViewAccount *tree;
821  AccountViewInfo info;
822  GNCAccountType type;
823 
824  tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
825 
827 
828  for (type = 0; type < NUM_ACCOUNT_TYPES; type++) /* from Account.h */
829  {
830  if (ti_dialog->account_type == ACCT_TYPE_EXPENSE)
831  info.include_type[type] = (type == ACCT_TYPE_EXPENSE);
832  else if (ti_dialog->account_type == ACCT_TYPE_INCOME)
833  info.include_type[type] = (type == ACCT_TYPE_INCOME);
834  else if (ti_dialog->account_type == ACCT_TYPE_ASSET)
835  info.include_type[type] = ((type == ACCT_TYPE_BANK) ||
836  (type == ACCT_TYPE_CASH) ||
837  (type == ACCT_TYPE_ASSET) ||
838  (type == ACCT_TYPE_STOCK) ||
839  (type == ACCT_TYPE_MUTUAL) ||
840  (type == ACCT_TYPE_RECEIVABLE));
841  else if (ti_dialog->account_type == ACCT_TYPE_LIABILITY)
842  info.include_type[type] = ((type == ACCT_TYPE_CREDIT) ||
843  (type == ACCT_TYPE_LIABILITY) ||
844  (type == ACCT_TYPE_EQUITY) ||
845  (type == ACCT_TYPE_PAYABLE));
846  else
847  info.include_type[type] = FALSE;
848  }
849 
850  info.show_hidden = TRUE;
852 
853  load_category_list (ti_dialog);
854  cursor_changed_cb(GTK_WIDGET(tree), ti_dialog);
855 }
856 
857 static int
858 gnc_tax_info_update_accounts (TaxInfoDialog *ti_dialog)
859 {
860  GncTreeViewAccount *tree;
861  GtkTreeSelection* selection;
862  GtkWidget *label;
863  int num_accounts;
864  char *string;
865 
866  tree = GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview);
867  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(tree));
868  num_accounts = gtk_tree_selection_count_selected_rows (selection);
869 
870  label = ti_dialog->num_acct_label;
871  string = g_strdup_printf (_("Accounts Selected: %d"), num_accounts);
872  gtk_label_set_text (GTK_LABEL (label), string);
873  g_free (string);
874 
875  gtk_widget_set_sensitive (ti_dialog->txf_info, num_accounts > 0);
876 
877  return num_accounts;
878 }
879 
880 static void
881 gnc_tax_info_set_acct (TaxInfoDialog *ti_dialog, Account *account)
882 {
883  if (account == NULL)
884  return;
885 
886  ti_dialog->account_type = xaccAccountTypeGetFundamental (xaccAccountGetType (account));
887 
888  if (ti_dialog->account_type == ACCT_TYPE_INCOME)
889  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(ti_dialog->income_radio), TRUE);
890  else if (ti_dialog->account_type == ACCT_TYPE_EXPENSE)
891  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(ti_dialog->expense_radio), TRUE);
892  else if (ti_dialog->account_type == ACCT_TYPE_ASSET)
893  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(ti_dialog->asset_radio), TRUE);
894  else if ((ti_dialog->account_type == ACCT_TYPE_LIABILITY) ||
895  (ti_dialog->account_type == ACCT_TYPE_EQUITY))
896  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(ti_dialog->liab_eq_radio), TRUE);
897  else
898  return;
899 
900  gnc_tree_view_account_set_selected_account (GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview),
901  account);
902 }
903 
904 static void
905 gnc_tax_info_acct_type_cb (GtkWidget *w, gpointer data)
906 {
907  TaxInfoDialog *ti_dialog = data;
908  const gchar *button_name;
909 
910  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)))
911  {
912  button_name = gtk_buildable_get_name(GTK_BUILDABLE(w));
913  if (g_strcmp0 (button_name, "income_radio") == 0)
914  ti_dialog->account_type = ACCT_TYPE_INCOME;
915  else if (g_strcmp0 (button_name, "expense_radio") == 0)
916  ti_dialog->account_type = ACCT_TYPE_EXPENSE;
917  else if (g_strcmp0 (button_name, "asset_radio") == 0)
918  ti_dialog->account_type = ACCT_TYPE_ASSET;
919  else if (g_strcmp0 (button_name, "liab_eq_radio") == 0)
920  ti_dialog->account_type = ACCT_TYPE_LIABILITY;
921  else
922  return;
923  tax_info_show_acct_type_accounts (ti_dialog);
925  (GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview));
926  gnc_tax_info_update_accounts (ti_dialog);
927  clear_gui (ti_dialog);
928  }
929  else
930  return;
931 }
932 
933 static void
934 gnc_tax_info_account_changed_cb (GtkTreeSelection *selection,
935  gpointer data)
936 {
937  TaxInfoDialog *ti_dialog = data;
938  GncTreeViewAccount *view;
939  GList *accounts;
940  int num_accounts;
941 
942  g_return_if_fail(GTK_IS_TREE_SELECTION(selection));
943 
944  num_accounts = gnc_tax_info_update_accounts (ti_dialog);
945  switch (num_accounts)
946  {
947  case 0:
948  clear_gui (ti_dialog);
949  gnc_tax_info_set_changed (ti_dialog, FALSE);
950  return;
951 
952  case 1:
953  /* Get the account. This view is set for multiple selection, so we
954  can only get a list of accounts. */
955  view = GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview);
957  if (accounts == NULL)
958  {
959  clear_gui (ti_dialog);
960  gnc_tax_info_set_changed (ti_dialog, FALSE);
961  return;
962  }
963  account_to_gui (ti_dialog, accounts->data);
964  g_list_free (accounts);
965 
966  gnc_tax_info_set_changed (ti_dialog, FALSE);
967  break;
968 
969  default:
970  gnc_tax_info_set_changed (ti_dialog, TRUE);
971  return;
972  }
973 }
974 
975 static void
976 txf_code_select_row_cb (GtkTreeSelection *selection,
977  gpointer user_data)
978 {
979  TaxInfoDialog *ti_dialog = user_data;
980  GtkTreeModel *model;
981  GtkTreePath *path;
982  GtkTreeIter iter;
983  gint *indices;
984  TXFInfo *txf_info;
985  GtkAdjustment *adj;
986  GtkWidget *vbox;
987  GtkTextBuffer *tb;
988  const char *text;
989 
990  if (!gtk_tree_selection_get_selected(selection, &model, &iter))
991  return;
992  path = gtk_tree_model_get_path(model, &iter);
993  indices = gtk_tree_path_get_indices(path);
994  txf_info = g_list_nth_data (tax_infos (ti_dialog), indices[0]);
995  gtk_tree_path_free(path);
996 
997  tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ti_dialog->txf_help_text));
998 
999  text = (txf_info && txf_info->help) ? txf_info->help : "";
1000  gtk_text_buffer_set_text (tb, text, -1);
1001 
1002  adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (ti_dialog->help_scroll));
1003  gtk_adjustment_set_value (adj, 0.0);
1004 
1005  vbox = ti_dialog->payer_vbox;
1006 
1007  if (txf_info && txf_info->payer_name_source)
1008  {
1009  gboolean current;
1010 
1011  gtk_widget_set_sensitive (vbox, TRUE);
1012 
1013  current = (strcmp ("current", txf_info->payer_name_source) == 0);
1014 
1015  if (current)
1016  gtk_toggle_button_set_active
1017  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
1018  else
1019  gtk_toggle_button_set_active
1020  (GTK_TOGGLE_BUTTON (ti_dialog->parent_account_button), TRUE);
1021  }
1022  else
1023  {
1024  gtk_widget_set_sensitive (vbox, FALSE);
1025  gtk_toggle_button_set_active
1026  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
1027  }
1028 
1029  vbox = ti_dialog->copy_vbox;
1030 
1031  if (txf_info && txf_info->copy)
1032  {
1033  gtk_widget_set_sensitive (vbox, TRUE);
1034  }
1035  else
1036  {
1037  gtk_widget_set_sensitive (vbox, FALSE);
1038  }
1039 
1040  gnc_tax_info_set_changed (ti_dialog, TRUE);
1041 }
1042 
1043 static void
1044 set_focus_sensitivity (TaxInfoDialog *ti_dialog)
1045 {
1046  if ((ti_dialog->tax_type == NULL) ||
1047  (g_strcmp0 (ti_dialog->tax_type, "Other") == 0) ||
1048  (g_strcmp0 (ti_dialog->tax_type, "") == 0))
1049  {
1050  gtk_widget_grab_focus (ti_dialog->tax_identity_edit_button);
1051  gtk_widget_set_sensitive (ti_dialog->acct_info, FALSE);
1052  gtk_widget_set_sensitive (ti_dialog->txf_info, FALSE);
1053  gtk_widget_hide (ti_dialog->txf_help_text); /* textview doesn't go insensitive!? */
1054  }
1055  else if (ti_dialog->tax_type_changed)
1056  {
1057  gtk_widget_set_sensitive (ti_dialog->acct_info, TRUE);
1058  gtk_widget_set_sensitive (ti_dialog->txf_info, TRUE);
1059  gtk_widget_grab_focus (ti_dialog->account_treeview);
1060  }
1061  else
1062  {
1063  gtk_widget_set_sensitive (ti_dialog->acct_info, TRUE);
1064  gtk_widget_grab_focus (ti_dialog->account_treeview);
1065  }
1066  if (ti_dialog->asset_txf_infos == NULL)
1067  gtk_widget_hide (ti_dialog->asset_radio);
1068  else
1069  gtk_widget_show (ti_dialog->asset_radio);
1070  if (ti_dialog->liab_eq_txf_infos == NULL)
1071  gtk_widget_hide (ti_dialog->liab_eq_radio);
1072  else
1073  gtk_widget_show (ti_dialog->liab_eq_radio);
1074 }
1075 
1076 static void
1077 identity_edit_response_cb (GtkDialog *dialog, gint response, gpointer data)
1078 {
1079  TaxInfoDialog *ti_dialog = data;
1080  const gchar *entry_name = NULL;
1081  const gchar *entry_type = NULL;
1082  gboolean tax_name_changed = FALSE;
1083  gint active_item = 0;
1084  TaxTypeInfo *selected_type = NULL;
1085 
1086  if (response == GTK_RESPONSE_APPLY)
1087  {
1088  entry_name = gtk_entry_get_text (GTK_ENTRY (ti_dialog->entity_name_entry));
1089  active_item = gtk_combo_box_get_active
1090  (GTK_COMBO_BOX (ti_dialog->entity_type_combo));
1091  if (active_item != -1) /* -1 if there's no active item */
1092  {
1093  selected_type = g_list_nth_data (ti_dialog->entity_type_infos,
1094  (guint) active_item);
1095  if (selected_type)
1096  {
1097  entry_type = selected_type->type_code;
1098  if (!(g_strcmp0 (ti_dialog->tax_type, entry_type) == 0))
1099  {
1100  if (!((g_strcmp0 (ti_dialog->tax_type, "") == 0) &&
1101  (g_strcmp0 (entry_type, "Other") == 0)))
1102  { /* tax type changed */
1103  ti_dialog->tax_type_changed = TRUE;
1104  ti_dialog->tax_type = entry_type;
1105  if (entry_type != NULL)
1106  {
1107  gtk_label_set_text (GTK_LABEL (ti_dialog->entity_type_display),
1108  selected_type->combo_box_entry);
1109  }
1110  else
1111  {
1112  gtk_label_set_text (GTK_LABEL (ti_dialog->entity_type_display),
1113  ti_dialog->default_tax_type);
1114  }
1115  if (ti_dialog->income_txf_infos != NULL)
1116  destroy_txf_infos (ti_dialog->income_txf_infos);
1117  ti_dialog->income_txf_infos = load_txf_info (INCOME, ti_dialog);
1118  if (ti_dialog->expense_txf_infos != NULL)
1119  destroy_txf_infos (ti_dialog->expense_txf_infos);
1120  ti_dialog->expense_txf_infos = load_txf_info (EXPENSE, ti_dialog);
1121  if (ti_dialog->asset_txf_infos != NULL)
1122  destroy_txf_infos (ti_dialog->asset_txf_infos);
1123  ti_dialog->asset_txf_infos = load_txf_info (ASSET, ti_dialog);
1124  if (ti_dialog->liab_eq_txf_infos != NULL)
1125  destroy_txf_infos (ti_dialog->liab_eq_txf_infos);
1126  ti_dialog->liab_eq_txf_infos = load_txf_info (LIAB_EQ, ti_dialog);
1127  gtk_toggle_button_set_active
1128  (GTK_TOGGLE_BUTTON(ti_dialog->expense_radio), TRUE);
1129  tax_info_show_acct_type_accounts (ti_dialog);
1131  (GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview));
1132  gnc_tax_info_update_accounts (ti_dialog);
1133  clear_gui (ti_dialog);
1134  }
1135  else /* tax type changed but from "" to "Other" - doesn't count as change */
1136  ti_dialog->tax_type_changed = FALSE;
1137  }
1138  else /* tax type not changed */
1139  ti_dialog->tax_type_changed = FALSE;
1140  }
1141  }
1142  if (!(g_strcmp0 (ti_dialog->tax_name, entry_name) == 0))
1143  {
1144  if (!(((ti_dialog->tax_name == NULL) &&
1145  (g_strcmp0 (entry_name, "") == 0))))
1146  {
1147  tax_name_changed = TRUE;
1148  ti_dialog->tax_name = g_strdup (entry_name);
1149  gtk_label_set_text (GTK_LABEL (ti_dialog->entity_name_display),
1150  entry_name);
1151  }
1152  else /* tax name changed but from NULL to "" - doesn't count as change */
1153  tax_name_changed = FALSE;
1154  }
1155  else /* tax name not changed */
1156  tax_name_changed = FALSE;
1157  if (tax_name_changed || ti_dialog->tax_type_changed)
1158  gnc_set_current_book_tax_name_type (tax_name_changed, entry_name,
1159  ti_dialog->tax_type_changed, entry_type);
1160  set_focus_sensitivity (ti_dialog);
1161  ti_dialog->tax_type_changed = FALSE;
1162  }
1163  identity_edit_destroy_cb (GTK_DIALOG (dialog), ti_dialog);
1164 }
1165 
1166 static void
1167 identity_edit_clicked_cb (GtkButton *button,
1168  gpointer user_data)
1169 {
1170  TaxInfoDialog *ti_dialog = user_data;
1171  GtkWidget *dialog;
1172  GtkWidget *content_area;
1173  GtkWidget *name_entry;
1174  GtkWidget *label;
1175  GtkWidget *table;
1176  GtkListStore *store;
1177  GList *types = NULL;
1178  GtkTreeIter iter;
1179  gint current_item = -1;
1180  gint item = 0;
1181  GtkCellRenderer *renderer;
1182  GtkWidget *type_combo;
1183 
1184  dialog = gtk_dialog_new_with_buttons (_("Income Tax Identity"),
1185  (GtkWindow *)ti_dialog->dialog,
1186  GTK_DIALOG_MODAL |
1187  GTK_DIALOG_DESTROY_WITH_PARENT,
1188  _("_Cancel"),
1189  GTK_RESPONSE_CANCEL,
1190  _("_Apply"),
1191  GTK_RESPONSE_APPLY,
1192  NULL);
1193 
1194  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1195  name_entry = gtk_entry_new();
1196  ti_dialog->entity_name_entry = name_entry;
1197  if (!(g_strcmp0 (ti_dialog->tax_name, NULL) == 0))
1198  gtk_entry_set_text (GTK_ENTRY (name_entry), ti_dialog->tax_name);
1199  label = gtk_label_new (_("Name"));
1200  gnc_label_set_alignment (label, 1.00, 0.50);
1201  table = gtk_grid_new ();
1202  gtk_grid_set_column_spacing (GTK_GRID(table), 12);
1203  gtk_grid_attach (GTK_GRID(table), label, 0, 0, 1, 1);
1204  gtk_grid_attach (GTK_GRID(table), name_entry, 1, 0, 1, 1);
1205 
1206  store = gtk_list_store_new (1, G_TYPE_STRING);
1207  gtk_list_store_clear(store);
1208  types = ti_dialog->entity_type_infos;
1209  for ( ; types; types = types->next)
1210  {
1211  TaxTypeInfo *tax_type_info = types->data;
1212 
1213  gtk_list_store_append(store, &iter);
1214  gtk_list_store_set(store, &iter, 0, tax_type_info->combo_box_entry, -1);
1215  if (g_strcmp0 (ti_dialog->tax_type, tax_type_info->type_code) == 0)
1216  current_item = item;
1217  item++;
1218  }
1219  type_combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL(store));
1220  g_object_unref(G_OBJECT (store));
1221  renderer = gtk_cell_renderer_text_new();
1222  gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(type_combo), renderer, TRUE);
1223  gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(type_combo), renderer,
1224  "text", 0, NULL);
1225  ti_dialog->entity_type_combo = type_combo;
1226  if (ti_dialog->tax_type)
1227  {
1228  gtk_combo_box_set_active (GTK_COMBO_BOX (type_combo), current_item);
1229  }
1230  else /* set to no active item */
1231  {
1232  gtk_combo_box_set_active (GTK_COMBO_BOX (type_combo), -1);
1233  }
1234  label = gtk_label_new (_("Type"));
1235  gnc_label_set_alignment (label, 1.00, 0.50);
1236  gtk_grid_attach (GTK_GRID(table), label, 0, 1, 1, 1);
1237  gtk_grid_attach (GTK_GRID(table), type_combo, 1, 1, 1, 1);
1238 
1239  label = gtk_label_new (_("CAUTION: If you set TXF categories, and later change 'Type', you will need to manually reset those categories one at a time"));
1240  gtk_label_set_max_width_chars (GTK_LABEL (label), 50);
1241  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1242  gnc_label_set_alignment (label, 0.50, 0.50);
1243  gtk_widget_set_margin_top (GTK_WIDGET(label), 5);
1244  gtk_grid_attach (GTK_GRID(table), label, 0, 2, 2, 1);
1245 
1246  gtk_container_add (GTK_CONTAINER (content_area), table);
1247  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_APPLY);
1248  g_signal_connect (G_OBJECT (dialog), "response",
1249  G_CALLBACK (identity_edit_response_cb), ti_dialog);
1250  g_signal_connect (G_OBJECT (dialog), "destroy",
1251  G_CALLBACK (identity_edit_destroy_cb), ti_dialog);
1252  gtk_widget_show_all (dialog);
1253 }
1254 
1255 static void
1256 tax_related_toggled_cb (GtkToggleButton *togglebutton,
1257  gpointer user_data)
1258 {
1259  TaxInfoDialog *ti_dialog = user_data;
1260  GtkWidget *vbox;
1261  GtkWidget *hbox;
1262  gboolean on;
1263 
1264  on = gtk_toggle_button_get_active (togglebutton);
1265 
1266  vbox = ti_dialog->txf_vbox;
1267  hbox = ti_dialog->pns_vbox;
1268  gtk_widget_set_sensitive (vbox, on);
1269 
1270  gtk_widget_set_sensitive (hbox, on);
1271 
1272  if (on == FALSE)
1273  gtk_widget_hide (ti_dialog->txf_help_text); /* textview doesn't go insensitive!? */
1274  else
1275  gtk_widget_show (ti_dialog->txf_help_text);
1276 
1277  gnc_tax_info_set_changed (ti_dialog, TRUE);
1278 }
1279 
1280 static void
1281 current_account_toggled_cb (GtkToggleButton *togglebutton,
1282  gpointer user_data)
1283 {
1284  TaxInfoDialog *ti_dialog = user_data;
1285 
1286  gnc_tax_info_set_changed (ti_dialog, TRUE);
1287 }
1288 
1289 static void
1290 copy_number_value_changed_cb (GtkSpinButton *spinbutton,
1291  gpointer user_data)
1292 {
1293  TaxInfoDialog *ti_dialog = user_data;
1294 
1295  gnc_tax_info_set_changed (ti_dialog, TRUE);
1296 }
1297 
1298 static void
1299 gnc_tax_info_dialog_create (GtkWidget * parent, TaxInfoDialog *ti_dialog)
1300 {
1301  GtkWidget *dialog;
1302  GtkBuilder *builder;
1303  GtkTreeView *tree_view;
1304  GtkTreeSelection *selection;
1305  GtkWidget *label;
1306 
1307  builder = gtk_builder_new();
1308  gnc_builder_add_from_file (builder, "dialog-tax-info.glade", "copy_spin_adj");
1309  gnc_builder_add_from_file (builder, "dialog-tax-info.glade", "tax_information_dialog");
1310 
1311  dialog = GTK_WIDGET(gtk_builder_get_object (builder, "tax_information_dialog"));
1312  ti_dialog->dialog = dialog;
1313 
1314  // Set the name for this dialog so it can be easily manipulated with css
1315  gtk_widget_set_name (GTK_WIDGET(dialog), "gnc-id-tax-information");
1316  gnc_widget_style_context_add_class (GTK_WIDGET(dialog), "gnc-class-taxes");
1317 
1318  initialize_getters ();
1319 
1320  g_signal_connect (G_OBJECT (dialog), "response",
1321  G_CALLBACK (gnc_tax_info_dialog_response), ti_dialog);
1322 
1323  g_signal_connect (G_OBJECT (dialog), "destroy",
1324  G_CALLBACK (window_destroy_cb), ti_dialog);
1325 
1326  /* parent */
1327  if (parent != NULL)
1328  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
1329 
1330  /* default to ok */
1331  gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_OK);
1332 
1333  /* tax identity */
1334  {
1335  GtkWidget *label;
1336  GtkWidget *edit_button;
1337 
1338  ti_dialog->this_book = gnc_get_current_book();
1339  ti_dialog->tax_name = gnc_get_current_book_tax_name();
1340  ti_dialog->tax_type = gnc_get_current_book_tax_type();
1341 
1342  label = GTK_WIDGET(gtk_builder_get_object (builder, "entity_name"));
1343  ti_dialog->entity_name_display = label;
1344  gtk_label_set_text (GTK_LABEL (label), ti_dialog->tax_name);
1345  ti_dialog->entity_name_entry = NULL;
1346 
1347  load_tax_entity_type_list (ti_dialog); /* initialize tax_type_combo_text */
1348 
1349  label = GTK_WIDGET(gtk_builder_get_object (builder, "entity_type"));
1350  ti_dialog->entity_type_display = label;
1351  if (ti_dialog->tax_type != NULL)
1352  gtk_label_set_text (GTK_LABEL (label), ti_dialog->tax_type_combo_text);
1353  ti_dialog->entity_type_combo = NULL;
1354 
1355  edit_button = GTK_WIDGET(gtk_builder_get_object (builder, "identity_edit_button"));
1356  ti_dialog->tax_identity_edit_button = edit_button;
1357  g_signal_connect (G_OBJECT (edit_button), "clicked",
1358  G_CALLBACK (identity_edit_clicked_cb), ti_dialog);
1359  ti_dialog->tax_type_changed = FALSE;
1360  }
1361 
1362  ti_dialog->income_txf_infos = load_txf_info (INCOME, ti_dialog);
1363  ti_dialog->expense_txf_infos = load_txf_info (EXPENSE, ti_dialog);
1364  ti_dialog->asset_txf_infos = load_txf_info (ASSET, ti_dialog);
1365  ti_dialog->liab_eq_txf_infos = load_txf_info (LIAB_EQ, ti_dialog);
1366 
1367  /* tax information */
1368  {
1369  GtkListStore *store;
1370  GtkTreeViewColumn *column;
1371  GtkCellRenderer *renderer;
1372  GtkWidget *button;
1373  GtkWidget *text;
1374 
1375  ti_dialog->txf_info = GTK_WIDGET(gtk_builder_get_object (builder, "tax_info_vbox"));
1376  button = GTK_WIDGET(gtk_builder_get_object (builder, "tax_related_button"));
1377  ti_dialog->tax_related_button = button;
1378 
1379  g_signal_connect (G_OBJECT (button), "toggled",
1380  G_CALLBACK (tax_related_toggled_cb), ti_dialog);
1381 
1382  text = GTK_WIDGET(gtk_builder_get_object (builder, "txf_help_text"));
1383  gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1384  ti_dialog->txf_help_text = text;
1385 
1386  tree_view = GTK_TREE_VIEW(gtk_builder_get_object (builder, "txf_category_view"));
1387  store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
1388  gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(store));
1389  g_object_unref(store);
1390  renderer = gtk_cell_renderer_text_new();
1391  column = gtk_tree_view_column_new_with_attributes
1392  (_("Form"), renderer, "text", 0, NULL);
1393  gtk_tree_view_append_column(tree_view, GTK_TREE_VIEW_COLUMN(column));
1394  renderer = gtk_cell_renderer_text_new();
1395  column = gtk_tree_view_column_new_with_attributes
1396  (_("Description"), renderer, "text", 1, NULL);
1397  gtk_tree_view_append_column(tree_view, GTK_TREE_VIEW_COLUMN(column));
1398  ti_dialog->txf_category_view = GTK_WIDGET(tree_view);
1399 
1400  selection = gtk_tree_view_get_selection(tree_view);
1401  g_signal_connect (G_OBJECT (selection), "changed",
1402  G_CALLBACK (txf_code_select_row_cb), ti_dialog);
1403 
1404  label = GTK_WIDGET(gtk_builder_get_object (builder, "txf_category_label"));
1405  gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(tree_view));
1406 
1407  ti_dialog->apply_button = GTK_WIDGET(gtk_builder_get_object (builder, "apply_button"));
1408 
1409  button = GTK_WIDGET(gtk_builder_get_object (builder, "current_account_button"));
1410  ti_dialog->current_account_button = button;
1411 
1412  button = GTK_WIDGET(gtk_builder_get_object (builder, "parent_account_button"));
1413  ti_dialog->parent_account_button = button;
1414 
1415  ti_dialog->help_scroll = GTK_WIDGET(gtk_builder_get_object (builder, "help_scroll"));
1416  ti_dialog->payer_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "payer_name_source_vbox"));
1417  ti_dialog->copy_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "copy_number_vbox"));
1418  ti_dialog->txf_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "txf_categories_vbox"));
1419  ti_dialog->pns_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "pns_copy_hbox"));
1420 
1421  g_signal_connect (G_OBJECT (button), "toggled",
1422  G_CALLBACK (current_account_toggled_cb),
1423  ti_dialog);
1424 
1425  button = GTK_WIDGET(gtk_builder_get_object (builder, "copy_spin_button"));
1426  ti_dialog->copy_spin_button = button;
1427 
1428  g_signal_connect (G_OBJECT (button), "value-changed",
1429  G_CALLBACK (copy_number_value_changed_cb),
1430  ti_dialog);
1431  }
1432 
1433  /* account tree */
1434  {
1435  GtkWidget *income_radio, *expense_radio, *asset_radio,
1436  *liab_eq_radio, *box;
1437 
1438  ti_dialog->acct_info = GTK_WIDGET(gtk_builder_get_object (builder, "acct_info_vbox"));
1439  ti_dialog->num_acct_label = GTK_WIDGET(gtk_builder_get_object (builder, "num_accounts_label"));
1440 
1441  tree_view = gnc_tree_view_account_new (FALSE);
1442  gnc_tree_view_account_set_filter (GNC_TREE_VIEW_ACCOUNT(tree_view),
1443  gnc_tax_info_dialog_account_filter_func,
1444  ti_dialog, NULL);
1445  ti_dialog->account_treeview = GTK_WIDGET(tree_view);
1446 
1447  selection = gtk_tree_view_get_selection (tree_view);
1448  gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
1449  g_signal_connect (G_OBJECT (selection), "changed",
1450  G_CALLBACK (gnc_tax_info_account_changed_cb),
1451  ti_dialog);
1452 
1453  gtk_widget_show (ti_dialog->account_treeview);
1454  box = GTK_WIDGET(gtk_builder_get_object (builder, "account_scroll"));
1455  gtk_container_add (GTK_CONTAINER (box), ti_dialog->account_treeview);
1456 
1457  label = GTK_WIDGET(gtk_builder_get_object (builder, "accounts_label"));
1458  gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(tree_view));
1459 
1460  income_radio = GTK_WIDGET(gtk_builder_get_object (builder, "income_radio"));
1461  ti_dialog->income_radio = income_radio;
1462  expense_radio = GTK_WIDGET(gtk_builder_get_object (builder, "expense_radio"));
1463  ti_dialog->expense_radio = expense_radio;
1464  asset_radio = GTK_WIDGET(gtk_builder_get_object (builder, "asset_radio"));
1465  ti_dialog->asset_radio = asset_radio;
1466  liab_eq_radio = GTK_WIDGET(gtk_builder_get_object (builder, "liab_eq_radio"));
1467  ti_dialog->liab_eq_radio = liab_eq_radio;
1468  ti_dialog->account_type = ACCT_TYPE_EXPENSE;
1469  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(expense_radio), TRUE);
1470 
1471  g_signal_connect (G_OBJECT (income_radio), "toggled",
1472  G_CALLBACK (gnc_tax_info_acct_type_cb),
1473  ti_dialog);
1474  g_signal_connect (G_OBJECT (expense_radio), "toggled",
1475  G_CALLBACK (gnc_tax_info_acct_type_cb),
1476  ti_dialog);
1477  g_signal_connect (G_OBJECT (asset_radio), "toggled",
1478  G_CALLBACK (gnc_tax_info_acct_type_cb),
1479  ti_dialog);
1480  g_signal_connect (G_OBJECT (liab_eq_radio), "toggled",
1481  G_CALLBACK (gnc_tax_info_acct_type_cb),
1482  ti_dialog);
1483  }
1484 
1485  /* select subaccounts button */
1486  {
1487  GtkWidget *button;
1488 
1489  button = GTK_WIDGET(gtk_builder_get_object (builder, "select_subaccounts_button"));
1490  ti_dialog->select_button = button;
1491 
1492  g_signal_connect (G_OBJECT (button), "clicked",
1493  G_CALLBACK (select_subaccounts_clicked),
1494  ti_dialog);
1495  g_signal_connect (G_OBJECT (ti_dialog->account_treeview), "cursor_changed",
1496  G_CALLBACK (cursor_changed_cb),
1497  ti_dialog);
1498  }
1499 
1500  tax_info_show_acct_type_accounts (ti_dialog);
1501  gnc_tax_info_update_accounts (ti_dialog);
1502  clear_gui (ti_dialog);
1503  gnc_tax_info_set_changed (ti_dialog, FALSE);
1504 
1505  gnc_restore_window_size(GNC_PREFS_GROUP,
1506  GTK_WINDOW(ti_dialog->dialog), GTK_WINDOW (parent));
1507 
1508 
1509  if (gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL, GNC_PREF_SAVE_GEOMETRY))
1510  {
1511  GObject *object = gtk_builder_get_object (builder, "paned");
1512  gnc_prefs_bind (GNC_PREFS_GROUP, GNC_PREF_PANED_POS, object, "position");
1513  }
1514  g_object_unref (builder);
1515 }
1516 
1517 static void
1518 close_handler (gpointer user_data)
1519 {
1520  TaxInfoDialog *ti_dialog = user_data;
1521 
1522  gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(ti_dialog->dialog));
1523  gtk_widget_destroy (ti_dialog->dialog);
1524 }
1525 
1526 static void
1527 refresh_handler (GHashTable *changes, gpointer user_data)
1528 {
1529  TaxInfoDialog *ti_dialog = user_data;
1530 
1531  gnc_tax_info_update_accounts (ti_dialog);
1532 }
1533 
1534 /********************************************************************\
1535  * gnc_tax_info_dialog *
1536  * opens up a window to set account tax information *
1537  * *
1538  * Args: parent - the parent of the window to be created *
1539  * Return: nothing *
1540 \********************************************************************/
1541 void
1542 gnc_tax_info_dialog (GtkWidget * parent, Account * account)
1543 {
1544  TaxInfoDialog *ti_dialog;
1545  gint component_id;
1546 
1547  ti_dialog = g_new0 (TaxInfoDialog, 1);
1548 
1549  gnc_tax_info_dialog_create (parent, ti_dialog);
1550 
1551  if (account)
1552  gnc_tax_info_set_acct (ti_dialog, account);
1553 
1554  component_id = gnc_register_gui_component (DIALOG_TAX_INFO_CM_CLASS,
1555  refresh_handler, close_handler,
1556  ti_dialog);
1557  gnc_gui_component_set_session (component_id, gnc_get_current_session ());
1558 
1559  gnc_gui_component_watch_entity_type (component_id,
1560  GNC_ID_ACCOUNT,
1561  QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
1562 
1563  set_focus_sensitivity (ti_dialog);
1564 
1565  gtk_widget_show (ti_dialog->dialog);
1566 }
void gnc_tree_view_account_get_view_info(GncTreeViewAccount *view, AccountViewInfo *avi)
Given pointers to an account tree and old style filter block, this function will copy the current con...
GNCAccountType xaccAccountTypeGetFundamental(GNCAccountType t)
Convenience function to return the fundamental type asset/liability/income/expense/equity given an ac...
Definition: Account.cpp:4430
gint64 xaccAccountGetTaxUSCopyNumber(const Account *acc)
Returns copy_number stored in KVP; if KVP doesn&#39;t exist or copy_number is zero, returns 1...
Definition: Account.cpp:4006
GList * gnc_tree_view_account_get_selected_accounts(GncTreeViewAccount *view)
This function returns a list of the accounts associated with the selected items in the account tree v...
Account * gnc_tree_view_account_get_cursor_account(GncTreeViewAccount *view)
This function returns the account in the account tree view at the current location of the cursor...
utility functions for the GnuCash UI
Expense accounts are used to denote expenses.
Definition: Account.h:143
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account&#39;s account type.
Definition: Account.cpp:3217
STRUCTS.
void xaccAccountSetTaxRelated(Account *acc, gboolean tax_related)
DOCUMENT ME!
Definition: Account.cpp:3976
Mutual Fund accounts will typically be shown in registers which show three columns: price...
Definition: Account.h:125
stop here; the following types just aren&#39;t ready for prime time
Definition: Account.h:161
void gnc_tree_view_account_set_view_info(GncTreeViewAccount *view, AccountViewInfo *avi)
Given pointers to an account tree and old style filter block, this function will applies the settings...
The cash account type is used to denote a shoe-box or pillowcase stuffed with * cash.
Definition: Account.h:110
Stock accounts will typically be shown in registers which show three columns: price, number of shares, and value.
Definition: Account.h:122
void gnc_tree_view_account_set_filter(GncTreeViewAccount *view, gnc_tree_view_account_filter_func func, gpointer data, GSourceFunc destroy)
This function attaches a filter function to the given account tree.
Account handling public routines.
void gnc_tree_view_account_select_subaccounts(GncTreeViewAccount *view, Account *account)
This function selects all sub-accounts of an account in the account tree view.
void gnc_tree_view_account_refilter(GncTreeViewAccount *view)
This function forces the account tree filter to be evaluated.
void gnc_prefs_bind(const gchar *group, const gchar *pref_name, gpointer object, const gchar *property)
Bind a setting to a g_object property.
Definition: gnc-prefs.c:181
GtkTreeView implementation for gnucash account tree.
Income accounts are used to denote income.
Definition: Account.h:140
GtkTreeView * gnc_tree_view_account_new(gboolean show_root)
Create a new account tree view.
const char * xaccAccountGetTaxUSPayerNameSource(const Account *acc)
DOCUMENT ME!
Definition: Account.cpp:3994
The bank account type denotes a savings or checking account held at a bank.
Definition: Account.h:107
A/P account type.
Definition: Account.h:151
const char * xaccAccountGetTaxUSCode(const Account *acc)
DOCUMENT ME!
Definition: Account.cpp:3982
gboolean xaccAccountGetTaxRelated(const Account *acc)
DOCUMENT ME!
Definition: Account.cpp:3970
asset (and liability) accounts indicate generic, generalized accounts that are none of the above...
Definition: Account.h:116
GNCAccountType
The account types are used to determine how the transaction data in the account is displayed...
Definition: Account.h:101
Generic api to store and retrieve preferences.
void xaccAccountSetTaxUSCode(Account *acc, const char *code)
DOCUMENT ME!
Definition: Account.cpp:3988
void gnc_tree_view_account_set_selected_account(GncTreeViewAccount *view, Account *account)
This function selects an account in the account tree view.
liability (and asset) accounts indicate generic, generalized accounts that are none of the above...
Definition: Account.h:119
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition: Account.cpp:1475
gint gnc_tree_view_account_count_children(GncTreeViewAccount *view, Account *account)
This function determines if an account in the account tree view has any visible children.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
A/R account type.
Definition: Account.h:149
void xaccAccountSetTaxUSPayerNameSource(Account *acc, const char *source)
DOCUMENT ME!
Definition: Account.cpp:4000
Equity account is used to balance the balance sheet.
Definition: Account.h:146
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition: Account.cpp:1516
The Credit card account is used to denote credit (e.g.
Definition: Account.h:113
void xaccAccountSetTaxUSCopyNumber(Account *acc, gint64 copy_number)
Saves copy_number in KVP if it is greater than 1; if copy_number is zero, deletes KVP...
Definition: Account.cpp:4013