Guitarix
gx_ui_builder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3  * Copyright (C) 2011 Pete Shorthose
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  * --------------------------------------------------------------------------
19  */
20 
21 /* ------- This is the GUI namespace ------- */
22 
23 #pragma once
24 
25 #ifndef SRC_HEADERS_GX_UI_BUILDER_H_
26 #define SRC_HEADERS_GX_UI_BUILDER_H_
27 
28 class PluginUI;
29 class MainWindow;
30 
31 namespace gx_gui {
32 
33 class uiElement {
34 public:
35  virtual ~uiElement() {}
36 };
37 
38 template<class T>
39 class uiToggle: public uiElement {
40 protected:
42  const std::string id;
43  Gtk::ToggleButton* button;
44  void on_button_toggled();
45  void on_parameter_changed(T v);
46 public:
47  uiToggle(gx_engine::GxMachineBase& machine, Gtk::ToggleButton *b, const std::string& id);
48 };
49 
50 template<class T>
51 uiToggle<T>::uiToggle(gx_engine::GxMachineBase& machine_, Gtk::ToggleButton *b, const std::string& id_)
52  : uiElement(), machine(machine_), id(id_), button(b) {
53  if (!machine.parameter_hasId(id)) {
54  return;
55  }
56  button->set_active(machine.get_parameter_value<T>(id));
57  button->signal_toggled().connect(sigc::mem_fun(this, &uiToggle<T>::on_button_toggled));
58  machine.signal_parameter_value<T>(id).connect(sigc::mem_fun(this, &uiToggle<T>::on_parameter_changed));
59 }
60 
61 template<>
63  machine.set_parameter_value(id, static_cast<float>(button->get_active()));
64 }
65 
66 template<>
68  machine.set_parameter_value(id, button->get_active());
69 }
70 
71 template<>
73  button->set_active(v != 0.0);
74 }
75 
76 template<>
78  button->set_active(v);
79 }
80 
81 /****************************************************************
82  ** class GxBuilder
83  **
84  ** Attempts to correct the mismatch between GtkBuilder and
85  ** Gtk::Builder wrt. reference counting.
86  **
87  ** Use get_toplevel or get_toplevel_derived to become the owner of a
88  ** toplevel widget of the loaded builder widget tree (specifying
89  ** object id's for the loader means that only part of the defined
90  ** widget tree is loaded). These pointers must be delete'd to destroy
91  ** the widget (and its child widgets). If you loaded a window
92  ** (GtkWindow or derived) you must use one of the get_toplevel
93  ** functions and delete the instance to avoid a memory leak.
94  **
95  ** Use find_widget for getting a pointer to a widget (but you don't
96  ** become owner of that widget, don't use delete). If you want to add
97  ** the loaded widget tree to a container, use this function (the
98  ** container will ref the widget and manage its lifetime).
99  **
100  ** template function code mostly copied from Gtk::Builder, look
101  ** there for comments.
102  */
103 
104 class GxBuilder: public Gtk::Builder {
105 private:
106  // only implemented for base class, make inaccessable
107  static Glib::RefPtr<GxBuilder> create_from_file(const std::string& filename, const char* object_id);
108  static Glib::RefPtr<GxBuilder> create_from_file(const std::string& filename, const Glib::ustring& object_id);
109  static Glib::RefPtr<GxBuilder> create_from_file(const std::string& filename, const Glib::StringArrayHandle& object_ids);
110  static Glib::RefPtr<GxBuilder> create_from_string(const Glib::ustring& buffer, const char* object_id);
111  static Glib::RefPtr<GxBuilder> create_from_string(const Glib::ustring& buffer, const Glib::ustring& object_id);
112  static Glib::RefPtr<GxBuilder> create_from_string(const Glib::ustring& buffer, const Glib::StringArrayHandle& object_ids);
113  GObject* get_cobject(const Glib::ustring& name);
114 protected:
115  Gtk::Object* get_widget_checked(const Glib::ustring& name, GType type, bool take_ref);
116 public:
117  static inline Glib::RefPtr<GxBuilder> create() { return Glib::RefPtr<GxBuilder>(new GxBuilder()); }
118 
119  static Glib::RefPtr<GxBuilder> create_from_file(
120  const std::string& filename, gx_engine::GxMachineBase* pmach = 0, const char* object_id = 0);
121 
122  static Glib::RefPtr<GxBuilder> create_from_file(
123  const std::string& filename, gx_engine::GxMachineBase* pmach, const Glib::StringArrayHandle& object_ids);
124 
125  static Glib::RefPtr<GxBuilder> create_from_string(
126  const Glib::ustring& buffer, gx_engine::GxMachineBase* pmach = 0, const char* object_id = 0);
127 
128  static Glib::RefPtr<GxBuilder> create_from_string(
129  const Glib::ustring& buffer, gx_engine::GxMachineBase* pmach, const Glib::StringArrayHandle& object_ids);
130 
131  void fixup_controlparameters(gx_engine::GxMachineBase& machine);
132 
133  template <class T_Widget> inline
134  void find_widget(const Glib::ustring& name, T_Widget*& widget) {
135  widget = 0;
136  widget = dynamic_cast<T_Widget*>(this->get_widget_checked(name, T_Widget::get_base_type(), false));
137  assert(widget);
138  }
139 
140  template <class T_Widget, class F> inline
141  void find_widget_derived(const Glib::ustring& name, T_Widget*& widget, F f) {
142  widget = 0;
143  typedef typename T_Widget::BaseObjectType cwidget_type;
144  cwidget_type* pCWidget = (cwidget_type*)get_cobject(name);
145  if(!pCWidget) {
146  return;
147  }
148  Glib::ObjectBase* pObjectBase = ObjectBase::_get_current_wrapper((GObject*)pCWidget);
149  if (pObjectBase) {
150  widget = dynamic_cast<T_Widget*>( Glib::wrap((GtkWidget*)pCWidget) );
151  if (!widget) {
152  g_critical("GxBuilder::get_widget_derived(): dynamic_cast<> failed. An existing C++ instance, of a different type, seems to exist.");
153  }
154  } else {
155  widget = f(pCWidget);
156  }
157  }
158 
159  bool has_object(const Glib::ustring& name) {
160  return gtk_builder_get_object(gobj(), name.c_str()) != 0;
161  }
162 
163  template <class T_Widget> inline
164  void get_toplevel(const Glib::ustring& name, T_Widget*& widget) {
165  widget = 0;
166  GType type = T_Widget::get_base_type();
167  widget = dynamic_cast<T_Widget*>(this->get_widget_checked(name, type, !g_type_is_a(type, GTK_TYPE_WINDOW)));
168  assert(widget);
169  assert(!widget->get_parent());
170  }
171 
172  Gtk::Window *get_first_window();
173 
174  template <class T_Widget, class F> inline
175  void get_toplevel_derived(const Glib::ustring& name, T_Widget*& widget, F f) {
176  widget = 0;
177  typedef typename T_Widget::BaseObjectType cwidget_type;
178  cwidget_type* pCWidget = (cwidget_type*)get_cobject(name);
179  if(!pCWidget) {
180  return;
181  }
182  if (!g_type_is_a(G_OBJECT_TYPE(pCWidget), GTK_TYPE_WINDOW)) {
183  g_object_ref(pCWidget);
184  }
185  Glib::ObjectBase* pObjectBase = ObjectBase::_get_current_wrapper((GObject*)pCWidget);
186  if (pObjectBase) {
187  widget = dynamic_cast<T_Widget*>( Glib::wrap((GtkWidget*)pCWidget) );
188  if (!widget) {
189  g_critical("GxBuilder::get_widget_derived(): dynamic_cast<> failed. An existing C++ instance, of a different type, seems to exist.");
190  }
191  } else {
192  widget = f(pCWidget);
193  }
194  assert(!widget->get_parent());
195  }
196 };
197 
198 class StackBoxBuilder;
199 
201 protected:
203  std::vector<PluginUI*> *pluginlist;
204  static StackBoxBuilder *intf;
205  static void openTabBox_(const char* label);
206  static void openVerticalBox_(const char* label);
207  static void openVerticalBox1_(const char* label);
208  static void openVerticalBox2_(const char* label);
209  static void openHorizontalBox_(const char* label);
210  //static void openHorizontalBox_(const char* label, int spacing);
211  static void openHorizontalhideBox_(const char* label);
212  static void openHorizontalTableBox_(const char* label);
213  static void openFrameBox_(const char *label);
214  static void openFlipLabelBox_(const char* label);
215  static void openpaintampBox_(const char* label);
216  static void insertSpacer_();
217  static void set_next_flags_(int flags);
218  static void create_mid_rackknob_(const char *id, const char *label);
219  static void create_small_rackknob_(const char *id, const char *label);
220  static void create_small_rackknobr_(const char *id, const char *label);
221  static void create_big_rackknob_(const char *id, const char *label);
222  static void create_master_slider_(const char *id, const char *label);
223  static void create_feedback_slider_(const char *id, const char *label);
224  static void create_selector_no_caption_(const char *id);
225  static void create_selector_(const char *id, const char *label);
226  static void create_simple_meter_(const char *id);
227  static void create_simple_c_meter_(const char *id, const char *idl, const char *label);
228  static void create_spin_value_(const char *id, const char *label);
229  static void create_switch_no_caption_(const char *sw_type,const char * id);
230  static void create_feedback_switch_(const char *sw_type,const char * id);
231  static void create_fload_switch_(const char *sw_type,const char * id,const char * idf);
232  static void create_switch_(const char *sw_type,const char * id, const char *label);
233  static void create_wheel_(const char * id, const char *label);
234  static void create_port_display_(const char *id, const char *label);
235  static void create_p_display_(const char *id, const char *idl, const char *idh);
236  static void create_simple_spin_value_(const char *id);
237  static void create_eq_rackslider_no_caption_(const char *id);
238  static void closeBox_();
239  static void load_glade_(const char *data);
240  static void load_glade_file_(const char *fname);
241  virtual bool load(gx_engine::Plugin *p);
242 public:
243  UiBuilderImpl(MainWindow *i, StackBoxBuilder *b, std::vector<PluginUI*> *pl=0);
244  bool load_unit(PluginDef *pl);
245  friend class gx_engine::GxMachineRemote;
246 };
247 
248 GtkWidget *load_toplevel(GtkBuilder *builder, const char* filename, const char* windowname);
249 
250 } /* end of gx_gui namespace */
251 #endif // SRC_HEADERS_GX_UI_BUILDER_H_
gx_gui::uiToggle::button
Gtk::ToggleButton * button
Definition: gx_ui_builder.h:43
gx_gui::load_toplevel
GtkWidget * load_toplevel(GtkBuilder *builder, const char *filename, const char *windowname)
Definition: gx_ui_builder.cpp:36
gx_gui::UiBuilderImpl
Definition: gx_ui_builder.h:199
gx_gui::uiToggle::id
const std::string id
Definition: gx_ui_builder.h:42
gx_gui::uiToggle::on_button_toggled
void on_button_toggled()
gx_gui::uiToggle::uiToggle
uiToggle(gx_engine::GxMachineBase &machine, Gtk::ToggleButton *b, const std::string &id)
Definition: gx_ui_builder.h:51
gx_gui::uiElement
Definition: gx_ui_builder.h:33
gx_engine::GxMachineBase
Definition: machine.h:53
gx_gui::GxBuilder
Definition: gx_ui_builder.h:103
gx_engine::UiBuilderBase
Definition: gx_pluginloader.h:86
gx_gui::uiToggle
Definition: gx_ui_builder.h:39
gx_engine::GxMachineRemote
Definition: machine.h:391
gx_gui::uiToggle::on_parameter_changed
void on_parameter_changed(T v)
PluginUI
Definition: gx_main_window.h:149
PluginDef
Definition: gx_plugin.h:183
Glib::wrap
Gxw::BigKnob * wrap(GxBigKnob *object, bool take_copy)
Definition: bigknob.cc:44
gx_gui::uiToggle::machine
gx_engine::GxMachineBase & machine
Definition: gx_ui_builder.h:41
gx_gui
Definition: gx_gui_helpers.h:28
gx_gui::StackBoxBuilder
Definition: gx_stackbox_builder.h:44
gx_gui::uiElement::~uiElement
virtual ~uiElement()
Definition: gx_ui_builder.h:35
gx_engine::Plugin
Definition: gx_pluginloader.h:44
MainWindow
Definition: gx_main_window.h:574
main
int main(int argc, char *argv[])
Definition: gxw_demo.cc:67