GnuCash  5.6-150-g038405b370+
gnc-option-gtk-ui.hpp
1 /********************************************************************\
2  * gnc-option-gtk-ui.hpp -- Gtk Widgets for manipulating options *
3  * Copyright 2022 John Ralls <jralls@ceridwen.us> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21 \********************************************************************/
22 
23 #ifndef GNC_OPTION_GTK_UI_HPP
24 #define GNC_OPTION_GTK_UI_HPP
25 
26 #include <gtk/gtk.h>
27 #include <glib/gi18n.h>
28 #include <vector>
29 #include <gnc-option.hpp>
30 #include <gnc-option-uitype.hpp>
31 #include <gnc-option-ui.hpp>
32 #include <libguile.h>
33 
45 typedef void (*WidgetCreateFunc)(GncOption&, GtkGrid*, int);
46 
52 {
53 public:
58  static void set_func(GncOptionUIType type, WidgetCreateFunc func);
65  static void create(GncOption& option, GtkGrid* page, int row);
66 private:
67  static std::vector<WidgetCreateFunc> s_registry;
68  static bool s_initialized;
69 };
70 
75 {
76 public:
77  GncOptionGtkUIItem(GtkWidget* widget, GncOptionUIType type);
80  virtual ~GncOptionGtkUIItem() override;
82  virtual void set_selectable(bool) const noexcept override;
84  void clear_ui_item() override;
85  void set_widget(GtkWidget* widget);
86  virtual GtkWidget* get_widget() const { return m_widget; }
87  virtual SCM get_widget_scm_value(const GncOption&) const;
88 private:
89  GtkWidget* m_widget;
90 };
91 
92 void gnc_option_changed_widget_cb (GtkWidget *widget, GncOption *option);
93 void gnc_option_changed_option_cb (GtkWidget *dummy, GncOption *option);
94 
95 template<GncOptionUIType type> void
96 create_option_widget(GncOption& option, GtkGrid*, int row);
97 
102 template <typename Instance> inline const QofInstance*
103 qof_instance_cast(Instance inst)
104 {
105  static_assert(std::is_pointer_v<Instance>, "Pointers Only!");
106  return reinterpret_cast<const QofInstance*>(inst);
107 }
108 
109 inline void
110 align_label (GtkLabel *name_label)
111 {
112  /* some option widgets have a large vertical footprint so align
113  the label name to the top and add a small top margin */
114  gtk_widget_set_valign (GTK_WIDGET(name_label), GTK_ALIGN_START);
115  gtk_widget_set_margin_top (GTK_WIDGET(name_label), 6);
116 }
117 
118 inline void
119 set_name_label(const GncOption& option, GtkGrid* page_box, int row, bool align)
120 {
121  auto name{option.get_name().c_str()};
122  if (name && *name)
123  {
124  auto label{gtk_label_new(_(name))};
125  if (align)
126  align_label(GTK_LABEL(label));
127  gtk_widget_set_halign (GTK_WIDGET(label), GTK_ALIGN_END);
128  gtk_grid_attach(GTK_GRID(page_box), label, 0, row, 1, 1);
129  }
130 }
131 
132 inline void
133 set_tool_tip(const GncOption& option, GtkWidget* box)
134 {
135  auto documentation{option.get_docstring().c_str()};
136  if (documentation && *documentation)
137  gtk_widget_set_tooltip_text(box, _(documentation));
138 }
139 
140 inline void
141 grid_attach_widget(GtkGrid* grid, GtkWidget* widget, int row)
142 {
143  /* attach the option widget to the second column of the grid */
144  gtk_grid_attach (grid, widget, 1, row, 1, 1);
145 
146 }
147 
148 inline void
149 wrap_widget (const GncOption& option, GtkWidget* widget, GtkGrid* page_box, int row)
150 {
151  auto enclosing{gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5)};
152  gtk_box_set_homogeneous (GTK_BOX (enclosing), FALSE);
153  gtk_box_pack_start(GTK_BOX(enclosing), widget, FALSE, FALSE, 0);
154  set_name_label(option, page_box, row, false);
155  set_tool_tip(option, enclosing);
156  gtk_widget_show_all(enclosing);
157  grid_attach_widget(page_box, enclosing, row);
158 }
159 #endif //GNC_OPTION_GTK_UI_HPP
static void set_func(GncOptionUIType type, WidgetCreateFunc func)
Register a WidgetCreateFunc.
OptionUITypes.
Factory class that keeps track of which GncOptionValueType needs which WidgetCreateFunc and calls the...
Represents the public interface for an option.
Definition: gnc-option.hpp:135
C++ Public interface for individual options.
virtual void set_selectable(bool) const noexcept override
Control wether the widget is sensitive.
void clear_ui_item() override
Clear the data from the widget.
Holds a pointer to the UI item which will control the option and an enum representing the type of the...
static void create(GncOption &option, GtkGrid *page, int row)
Create a widget.
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...