GnuCash  5.6-150-g038405b370+
business-options-gnome.cpp
1 /*
2  * business-options.c -- Initialize Business Options
3  *
4  * Written By: Derek Atkins <warlord@MIT.EDU>
5  * Copyright (C) 2002,2006 Derek Atkins
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA gnu@gnu.org
23  */
24 
25 #include <gtk/gtk.h>
26 #include <glib/gi18n.h>
27 #include "business-options-gnome.h"
28 
29 #include <config.h> // for scanf format string
30 #include <qof.h>
31 #include <business-gnome-utils.h>
32 #include <gnc-ui-util.h> // for gnc_get_current_book
33 #include <gnc-general-search.h> // for GNC_GENERAL_SEARCH
34 #include "dialog-utils.h" // for gnc_builder_add_from_file
35 
36 #include "gnc-report-combo.h"
37 
38 #include <iostream>
39 #include <sstream>
40 #include <exception>
41 
42 #include "gnc-option-gtk-ui.hpp"
43 #include "gncOwner.h"
44 #include <gnc-option.hpp>
45 #define FUNC_NAME G_STRFUNC
46 
47 
48 static inline GncOwnerType
49 ui_type_to_owner_type(GncOptionUIType ui_type)
50 {
51  if (ui_type == GncOptionUIType::CUSTOMER)
52  return GNC_OWNER_CUSTOMER;
53  if (ui_type == GncOptionUIType::VENDOR)
54  return GNC_OWNER_VENDOR;
55  if (ui_type == GncOptionUIType::EMPLOYEE)
56  return GNC_OWNER_EMPLOYEE;
57  if (ui_type == GncOptionUIType::JOB)
58  return GNC_OWNER_JOB;
59 
60  std::ostringstream oss;
61  oss << "UI type " << static_cast<unsigned int>(ui_type) << " could not be converted to owner type\n";
62  throw std::invalid_argument(oss.str());
63 }
64 
65 
67 {
68 public:
69  GncGtkOwnerUIItem(GtkWidget* widget, GncOptionUIType type) :
70  GncOptionGtkUIItem(widget, type) {}
71  void set_ui_item_from_option(GncOption& option) noexcept override
72  {
73  auto owner = option.get_value<const GncOwner*>();
74  gnc_owner_set_owner(get_widget(), owner);
75  }
76  void set_option_from_ui_item(GncOption& option) noexcept override
77  {
78  GncOwner owner{};
79  gnc_owner_get_owner(get_widget(), &owner);
80  if (owner.type == ui_type_to_owner_type(option.get_ui_type()))
81  option.set_value<const GncOwner*>(&owner);
82  }
83 };
84 
85 template<> void
86 create_option_widget<GncOptionUIType::OWNER>(GncOption& option,
87  GtkGrid *page_box, int row)
88 {
89  GncOwner owner{};
90  auto ui_type{option.get_ui_type()};
91  owner.type = ui_type_to_owner_type(ui_type);
92  auto enclosing{gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5)};
93  gtk_box_set_homogeneous (GTK_BOX (enclosing), FALSE);
94  auto widget = gnc_owner_select_create(nullptr, enclosing,
95  gnc_get_current_book(),
96  &owner);
97 
98  option.set_ui_item(std::make_unique<GncGtkOwnerUIItem>(widget, ui_type));
99  option.set_ui_item_from_option();
100  g_signal_connect (G_OBJECT (widget), "changed",
101  G_CALLBACK (gnc_option_changed_widget_cb), &option);
102  set_name_label(option, page_box, row, false);
103  set_tool_tip(option, enclosing);
104  grid_attach_widget(page_box, enclosing, row);
105 }
106 
108 {
109 public:
110  explicit GncGtkInvoiceUIItem(GtkWidget* widget) :
111  GncOptionGtkUIItem(widget, GncOptionUIType::INVOICE) {}
112  void set_ui_item_from_option(GncOption& option) noexcept override
113  {
114  auto instance{option.get_value<const QofInstance*>()};
115  if (instance)
116  gnc_general_search_set_selected(GNC_GENERAL_SEARCH(get_widget()),
117  GNC_INVOICE(instance));
118  }
119  void set_option_from_ui_item(GncOption& option) noexcept override
120  {
121  option.set_value(qof_instance_cast(gnc_general_search_get_selected(GNC_GENERAL_SEARCH(get_widget()))));
122  }
123 };
124 
125 template<> void
126 create_option_widget<GncOptionUIType::INVOICE>(GncOption& option,
127  GtkGrid *page_box, int row)
128 {
129  auto enclosing{gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5)};
130  gtk_box_set_homogeneous (GTK_BOX (enclosing), FALSE);
131  auto widget{gnc_invoice_select_create(enclosing, gnc_get_current_book(),
132  nullptr, nullptr, nullptr)};
133 
134  option.set_ui_item(std::make_unique<GncGtkInvoiceUIItem>(widget));
135  option.set_ui_item_from_option();
136  g_signal_connect(G_OBJECT (widget), "changed",
137  G_CALLBACK (gnc_option_changed_widget_cb), &option);
138 
139  set_name_label(option, page_box, row, false);
140  set_tool_tip(option, enclosing);
141  grid_attach_widget(page_box, enclosing, row);
142 }
143 
145 {
146 public:
147  explicit GncGtkTaxTableUIItem(GtkWidget* widget) :
148  GncOptionGtkUIItem(widget, GncOptionUIType::TAX_TABLE) {}
149  void set_ui_item_from_option(GncOption& option) noexcept override
150  {
151  auto taxtable{option.get_value<const QofInstance*>()};
152  if (taxtable)
153  gnc_simple_combo_set_value(GTK_COMBO_BOX(get_widget()),
154  GNC_TAXTABLE(taxtable));
155  else
156  gnc_simple_combo_set_value(GTK_COMBO_BOX(get_widget()),
157  nullptr);
158  }
159  void set_option_from_ui_item(GncOption& option) noexcept override
160  {
161  auto taxtable{gnc_simple_combo_get_value(GTK_COMBO_BOX(get_widget()))};
162  option.set_value<const QofInstance*>(qof_instance_cast(taxtable));
163  }
164 };
165 
166 template<> void
167 create_option_widget<GncOptionUIType::TAX_TABLE>(GncOption& option,
168  GtkGrid *page_box, int row)
169 {
170  constexpr const char* glade_file{"business-options-gnome.glade"};
171  constexpr const char* glade_store{"taxtable_store"};
172  constexpr const char* glade_menu{"taxtable_menu"};
173  auto builder{gtk_builder_new()};
174  gnc_builder_add_from_file(builder, glade_file, glade_store);
175  gnc_builder_add_from_file(builder, glade_file, glade_menu);
176  auto widget{GTK_WIDGET(gtk_builder_get_object(builder, glade_menu))};
177  gnc_taxtables_combo(GTK_COMBO_BOX(widget), gnc_get_current_book(), TRUE,
178  nullptr);
179  option.set_ui_item(std::make_unique<GncGtkTaxTableUIItem>(widget));
180  option.set_ui_item_from_option();
181  g_object_unref(builder); // Needs to wait until after widget has been reffed.
182  g_signal_connect (G_OBJECT (widget), "changed",
183  G_CALLBACK (gnc_option_changed_widget_cb), &option);
184 
185  wrap_widget(option, widget, page_box, row);
186 }
187 
189 {
190 public:
191  GncGtkInvReportUIItem(GtkWidget* widget) :
192  GncOptionGtkUIItem(widget, GncOptionUIType::INV_REPORT) {}
193  void set_ui_item_from_option(GncOption& option) noexcept override
194  {
195  std::string guid_string;
196  auto str{option.get_value<std::string>()};
197 
198  if (str.empty())
199  {
200  static const std::string default_guid_string(gnc_get_builtin_default_invoice_print_report ());
201  guid_string = default_guid_string + "/ ";
202  }
203  else
204  guid_string = str;
205 
206  gnc_report_combo_set_active_guid_name (GNC_REPORT_COMBO(get_widget()),
207  guid_string.c_str());
208  }
209  void set_option_from_ui_item(GncOption& option) noexcept override
210  {
211  auto report_guid_name = gnc_report_combo_get_active_guid_name (GNC_REPORT_COMBO(get_widget()));
212  option.set_value(std::string{report_guid_name});
213  g_free (report_guid_name);
214  }
215 };
216 
217 template<> void
218 create_option_widget<GncOptionUIType::INV_REPORT>(GncOption& option,
219  GtkGrid *page_box,
220  int row)
221 {
222  constexpr const char* inv_report{"gnc:custom-report-invoice-template-guids"};
223  auto widget = gnc_default_invoice_report_combo (inv_report);
224  option.set_ui_item(std::make_unique<GncGtkInvReportUIItem>(widget));
225  option.set_ui_item_from_option();
226 
227  g_signal_connect (G_OBJECT (widget), "changed",
228  G_CALLBACK (gnc_option_changed_widget_cb), &option);
229 
230  wrap_widget (option, widget, page_box, row);
231 }
232 
233 void
235 {
236  GncOptionUIFactory::set_func(GncOptionUIType::OWNER,
237  create_option_widget<GncOptionUIType::OWNER>);
238  GncOptionUIFactory::set_func(GncOptionUIType::CUSTOMER,
239  create_option_widget<GncOptionUIType::OWNER>);
240  GncOptionUIFactory::set_func(GncOptionUIType::VENDOR,
241  create_option_widget<GncOptionUIType::OWNER>);
242  GncOptionUIFactory::set_func(GncOptionUIType::EMPLOYEE,
243  create_option_widget<GncOptionUIType::OWNER>);
244  GncOptionUIFactory::set_func(GncOptionUIType::JOB,
245  create_option_widget<GncOptionUIType::OWNER>);
246  GncOptionUIFactory::set_func(GncOptionUIType::INVOICE,
247  create_option_widget<GncOptionUIType::INVOICE>);
248  GncOptionUIFactory::set_func(GncOptionUIType::TAX_TABLE,
249  create_option_widget<GncOptionUIType::TAX_TABLE>);
250  GncOptionUIFactory::set_func(GncOptionUIType::INV_REPORT,
251  create_option_widget<GncOptionUIType::INV_REPORT>);
252 }
static void set_func(GncOptionUIType type, WidgetCreateFunc func)
Register a WidgetCreateFunc.
Business Interface: Object OWNERs.
utility functions for the GnuCash UI
Represents the public interface for an option.
Definition: gnc-option.hpp:136
C++ Public interface for individual options.
class GncOptionGtkUIItem Gtk-specific Interface class for Option Widget
GncOptionUIType
Used by GncOptionClassifier to indicate to dialog-options what control should be displayed for the op...
void gnc_business_options_gnome_initialize(void)
Set up the business and counters pages in the File Preferences dialog.