bakery  2.6
Association.h
Go to the documentation of this file.
1 /*
2  * Copyright 2002 The Bakery team
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #ifndef BAKERY_CONFIGURATION_ASSOCIATION_H
20 #define BAKERY_CONFIGURATION_ASSOCIATION_H
21 
23 #include <gtkmm/togglebutton.h>
24 #include <gtkmm/entry.h>
25 #include <gtkmm/range.h>
26 #include <gtkmm/spinbutton.h>
27 #include <gtkmm/combo.h>
28 #include <gtkmm/optionmenu.h>
29 
30 namespace Bakery
31 {
32 namespace Conf
33 {
34 
35 template< class T_Widget >
37 
38 template< class T_Widget >
40 {
41 public:
42  static const AssociationPtr create(const Glib::ustring& full_key, T_Widget& widget, bool instant)
43  {
44  return AssociationPtr( new Association<T_Widget>(full_key, widget, instant) );
45  }
46 
48  {
49  }
50 
51 
52 protected:
53  AssociationCreation(const Glib::ustring& full_key, T_Widget& widget, bool instant)
54  : AssociationBase(full_key,instant),
55  m_widget(widget)
56  {
57  }
58 
59  AssociationCreation(const AssociationCreation& other); // Not implemented
60 
61  T_Widget& m_widget;
62 };
63 
64 template< class T_Widget >
65 class Association : public AssociationCreation<T_Widget>
66 {
67 public:
68 
69  //For some reason, the compiler can not use this if it is in the base class:
70  //typedef AssociationCreation<T_Widget>::Callback Callback;
71  typedef sigc::slot<void> Callback;
72 
78 #ifdef GLIBMM_EXCEPTIONS_ENABLED
79  virtual void load_widget();
80  virtual void save_widget();
81 #else
82  virtual void load_widget(std::auto_ptr<Glib::Error>& error);
83  virtual void save_widget(std::auto_ptr<Glib::Error>& error);
84 #endif
85 
86 protected:
87  Association(const Glib::ustring& full_key, T_Widget& widget, bool instant)
88  : AssociationCreation<T_Widget>(full_key, widget, instant)
89  {};
90 };
91 
92 //----------------------------------------------------------------------
93 // Association<T> specializations. These provide widget/key
94 // association behaviors that are specific to individual widget types.
95 //----------------------------------------------------------------------
96 
97 //SpinButton specializatipn:
98 
99 template<>
100 class Association<Gtk::SpinButton> : public AssociationCreation<Gtk::SpinButton>
101 {
102 public:
103  typedef Gtk::SpinButton type_widget;
104 
105  void connect_widget(Callback widget_changed)
106  {
107  m_widget.signal_value_changed().connect(widget_changed);
108  }
109 
110 #ifdef GLIBMM_EXCEPTIONS_ENABLED
111  void load_widget()
112 #else
113  void load_widget(std::auto_ptr<Glib::Error>& error)
114 #endif // GLIBMM_EXCEPTIONS_ENABLED
115  {
116 #ifdef GLIBMM_EXCEPTIONS_ENABLED
117  double val = get_conf_client()->get_float(get_key());
118 #else
119  double val = get_conf_client()->get_float(get_key(), error);
120  if (error.get() != NULL)
121 #endif
122  if (m_widget.get_value() != val)
123  m_widget.set_value(val);
124  }
125 
126 #ifdef GLIBMM_EXCEPTIONS_ENABLED
127  void save_widget()
128 #else
129  void save_widget(std::auto_ptr<Glib::Error>& error)
130 #endif
131  {
132  double val = m_widget.get_value();
133 #ifdef GLIBMM_EXCEPTIONS_ENABLED
134  double existing_val = get_conf_client()->get_float(get_key());
135 #else
136  double existing_val = get_conf_client()->get_float(get_key(), error);
137  if (error.get() != NULL)
138 #endif
139  if (existing_val != val)
140 #ifdef GLIBMM_EXCEPTIONS_ENABLED
141  get_conf_client()->set(get_key(), val);
142 #else
143  get_conf_client()->set(get_key(), val, error);
144 #endif
145  }
146 
147 
148  Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
149  : AssociationCreation<type_widget>(full_key, widget, instant)
150  {};
151 };
152 
153 
154 //Gtk::Entry specializatipn:
155 
156 template<>
157 class Association<Gtk::Entry> : public AssociationCreation<Gtk::Entry>
158 {
159 public:
160  typedef Gtk::Entry type_widget;
161 
162  void connect_widget(Callback widget_changed)
163  {
164  m_widget.signal_changed().connect(widget_changed);
165  }
166 
167 #ifdef GLIBMM_EXCEPTIONS_ENABLED
168  void load_widget()
169 #else
170  void load_widget(std::auto_ptr<Glib::Error>& error)
171 #endif
172  {
173  // Only set it if it has changed (avoids excess notifications).
174 #ifdef GLIBMM_EXCEPTIONS_ENABLED
175  Glib::ustring val = get_conf_client()->get_string(get_key());
176 #else
177  Glib::ustring val = get_conf_client()->get_string(get_key(), error);
178  if (error.get() != NULL)
179 #endif
180  if (m_widget.get_text() != val)
181  m_widget.set_text(val);
182  }
183 
184 #ifdef GLIBMM_EXCEPTIONS_ENABLED
185  void save_widget()
186 #else
187  void save_widget(std::auto_ptr<Glib::Error>& error)
188 #endif
189  {
190  // Only set it if it has changed (avoids excess notifications).
191  Glib::ustring val = m_widget.get_text();
192 #ifdef GLIBMM_EXCEPTIONS_ENABLED
193  Glib::ustring existing_val = get_conf_client()->get_string(get_key());
194 #else
195  Glib::ustring existing_val = get_conf_client()->get_string(get_key(), error);
196  if (error.get() != NULL)
197 #endif
198  {
199  if (existing_val != val)
200 #ifdef GLIBMM_EXCEPTIONS_ENABLED
201  get_conf_client()->set(get_key(), val);
202 #else
203  get_conf_client()->set(get_key(), val, error);
204 #endif
205  }
206  }
207 
208  Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
209  : AssociationCreation<type_widget>(full_key, widget, instant)
210  {};
211 };
212 
213 
214 //Gtk::ToggleButton specializatipn:
215 
216 template<>
217 class Association<Gtk::ToggleButton> : public AssociationCreation<Gtk::ToggleButton>
218 {
219 public:
220  typedef Gtk::ToggleButton type_widget;
221 
222  void connect_widget(Callback widget_changed)
223  {
224  m_widget.signal_toggled().connect(widget_changed);
225  }
226 
227 #ifdef GLIBMM_EXCEPTIONS_ENABLED
228  void load_widget()
229 #else
230  void load_widget(std::auto_ptr<Glib::Error>& error)
231 #endif
232  {
233  // Only set it if it has changed (avoids excess notifications).
234 #ifdef GLIBMM_EXCEPTIONS_ENABLED
235  bool val = get_conf_client()->get_bool(get_key());
236 #else
237  bool val = get_conf_client()->get_bool(get_key(), error);
238  if (error.get() != NULL)
239 #endif
240  if (m_widget.get_active() != val)
241  m_widget.set_active(val);
242  }
243 
244 #ifdef GLIBMM_EXCEPTIONS_ENABLED
245  void save_widget()
246 #else
247  void save_widget(std::auto_ptr<Glib::Error>& error)
248 #endif
249  {
250  // Only set it if it has changed (avoids excess notifications).
251  bool val = m_widget.get_active();
252 #ifdef GLIBMM_EXCEPTIONS_ENABLED
253  bool existing_val = get_conf_client()->get_bool(get_key());
254 #else
255  bool existing_val = get_conf_client()->get_bool(get_key(), error);
256  if (error.get() != NULL)
257 #endif
258  if (existing_val != val)
259 #ifdef GLIBMM_EXCEPTIONS_ENABLED
260  get_conf_client()->set(get_key(), val);
261 #else
262  get_conf_client()->set(get_key(), val, error);
263 #endif
264  }
265 
266  Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
267  : AssociationCreation<type_widget>(full_key, widget, instant)
268  {};
269 };
270 
271 
272 //Gtk::Range specializatipn:
273 
274 template<>
275 class Association<Gtk::Range> : public AssociationCreation<Gtk::Range>
276 {
277 public:
278  typedef Gtk::Range type_widget;
279 
280  void connect_widget(Callback widget_changed)
281  {
282  m_widget.signal_value_changed().connect(widget_changed);
283  }
284 
285 #ifdef GLIBMM_EXCEPTIONS_ENABLED
286  void load_widget()
287 #else
288  void load_widget(std::auto_ptr<Glib::Error>& error)
289 #endif
290  {
291 #ifdef GLIBMM_EXCEPTIONS_ENABLED
292  double val = get_conf_client()->get_float(get_key());
293 #else
294  double val = get_conf_client()->get_float(get_key(), error);
295  if (error.get() != NULL)
296 #endif
297  if (m_widget.get_value() != val)
298  m_widget.set_value(val);
299  }
300 
301 #ifdef GLIBMM_EXCEPTIONS_ENABLED
302  void save_widget()
303 #else
304  void save_widget(std::auto_ptr<Glib::Error>& error)
305 #endif
306  {
307  double val = m_widget.get_value();
308 #ifdef GLIBMM_EXCEPTIONS_ENABLED
309  double existing_val = get_conf_client()->get_float(get_key());
310 #else
311  double existing_val = get_conf_client()->get_float(get_key(), error);
312  if (error.get() != NULL)
313 #endif
314  if (existing_val != val)
315 #ifdef GLIBMM_EXCEPTIONS_ENABLED
316  get_conf_client()->set(get_key(), val);
317 #else
318  get_conf_client()->set(get_key(), val, error);
319 #endif
320  }
321 
322 
323  Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
324  : AssociationCreation<type_widget>(full_key, widget, instant)
325  {};
326 };
327 
328 
329 #ifndef GTKMM_DISABLE_DEPRECATED
330 //Gtk::OptionMenu specializatipn:
331 //Note that OptionMenu is deprecated in favour of ComboBox anyway.
332 
333 template<>
334 class Association<Gtk::OptionMenu> : public AssociationCreation<Gtk::OptionMenu>
335 {
336 public:
337  typedef Gtk::OptionMenu type_widget;
338 
339  void connect_widget(Callback widget_changed)
340  {
341  m_widget.signal_changed().connect(widget_changed);
342  }
343 
344 #ifdef GLIBMM_EXCEPTIONS_ENABLED
345  void load_widget()
346 #else
347  void load_widget(std::auto_ptr<Glib::Error>& error)
348 #endif
349  {
350  // Only set it if it has changed (avoids excess notifications).
351 #ifdef GLIBMM_EXCEPTIONS_ENABLED
352  int val = get_conf_client()->get_int(get_key());
353 #else
354  int val = get_conf_client()->get_int(get_key(), error);
355  if (error.get() != NULL)
356 #endif
357  if (m_widget.get_history() != val)
358  m_widget.set_history(guint(val));
359  }
360 
361 #ifdef GLIBMM_EXCEPTIONS_ENABLED
362  void save_widget()
363 #else
364  void save_widget(std::auto_ptr<Glib::Error>& error)
365 #endif
366  {
367  // Only set it if it has changed (avoids excess notifications).
368  int val = m_widget.get_history();
369 #ifdef GLIBMM_EXCEPTIONS_ENABLED
370  int existing_val = get_conf_client()->get_int(get_key());
371 #else
372  int existing_val = get_conf_client()->get_int(get_key(), error);
373  if (error.get() != NULL)
374 #endif
375  if (existing_val != val)
376 #ifdef GLIBMM_EXCEPTIONS_ENABLED
377  get_conf_client()->set(get_key(), val);
378 #else
379  get_conf_client()->set(get_key(), val, error);
380 #endif
381  }
382 
383  Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
384  : AssociationCreation<type_widget>(full_key, widget, instant)
385  {}
386  };
387 
388  //Gtk::Combo specializatipn:
389  //Note that Combo is deprecated in favour of ComboBox anyway.
390 
391  template<>
392  class Association<Gtk::Combo> : public AssociationCreation<Gtk::Combo>
393  {
394  public:
395  typedef Gtk::Combo type_widget;
396 
397  void connect_widget(Callback widget_changed)
398  {
399  m_widget.get_entry()->signal_changed().connect(widget_changed);
400  }
401 
402 #ifdef GLIBMM_EXCEPTIONS_ENABLED
403  void load_widget()
404 #else
405  void load_widget(std::auto_ptr<Glib::Error>& error)
406 #endif
407  {
408  // Only set it if it has changed (avoids excess notifications).
409 #ifdef GLIBMM_EXCEPTIONS_ENABLED
410  Glib::ustring val = get_conf_client()->get_string(get_key());
411 #else
412  Glib::ustring val = get_conf_client()->get_string(get_key(), error);
413  if (error.get() != NULL)
414 #endif
415  if (m_widget.get_entry()->get_text() != val)
416  m_widget.get_entry()->set_text(val);
417  }
418 
419 #ifdef GLIBMM_EXCEPTIONS_ENABLED
420  void save_widget()
421 #else
422  void save_widget(std::auto_ptr<Glib::Error>& error)
423 #endif
424  {
425  // Only set it if it has changed (avoids excess notifications).
426  Glib::ustring val = m_widget.get_entry()->get_text();
427 #ifdef GLIBMM_EXCEPTIONS_ENABLED
428  Glib::ustring existing_val = get_conf_client()->get_string(get_key());
429 #else
430  Glib::ustring existing_val = get_conf_client()->get_string(get_key(), error);
431  if (error.get() != NULL)
432 #endif
433  if (existing_val != val)
434 #ifdef GLIBMM_EXCEPTIONS_ENABLED
435  get_conf_client()->set(get_key(), val);
436 #else
437  get_conf_client()->set(get_key(), val, error);
438 #endif
439  }
440 
441  Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
442  : AssociationCreation<type_widget>(full_key, widget, instant)
443  {}
444 };
445 #endif // !GTKMM_DISABLE_DEPRECATED
446 
447 } //namespace Conf
448 
449 } //namespace Bakery
450 
451 #endif //BAKERY_CONFIGURATION_ASSOCIATION_H
void save_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:187
Gtk::Entry type_widget
Definition: Association.h:160
void save_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:364
Glib::ustring get_key() const
Association(const Glib::ustring &full_key, T_Widget &widget, bool instant)
Definition: Association.h:87
T_Widget & m_widget
Definition: Association.h:61
void connect_widget(Callback widget_changed)
Definition: Association.h:339
void load_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:113
Association(const Glib::ustring &full_key, type_widget &widget, bool instant)
Definition: Association.h:323
void save_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:304
sigc::slot< void > Callback
Definition: Association.h:71
Association(const Glib::ustring &full_key, type_widget &widget, bool instant)
Definition: Association.h:383
sharedptr< AssociationBase > AssociationPtr
Definition: AssociationBase.h:37
Definition: Association.h:36
virtual ~AssociationCreation()
Definition: Association.h:47
Gtk::OptionMenu type_widget
Definition: Association.h:337
Association(const Glib::ustring &full_key, type_widget &widget, bool instant)
Definition: Association.h:266
void load_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:347
void save_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:129
Gtk::Combo type_widget
Definition: Association.h:395
void save_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:422
Gtk::SpinButton type_widget
Definition: Association.h:103
Gtk::ToggleButton type_widget
Definition: Association.h:220
void load_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:170
void connect_widget(Callback widget_changed)
Definition: Association.h:222
virtual void load_widget(std::auto_ptr< Glib::Error > &error)
void connect_widget(Callback widget_changed)
Definition: Association.h:397
AssociationCreation(const Glib::ustring &full_key, T_Widget &widget, bool instant)
Definition: Association.h:53
Association(const Glib::ustring &full_key, type_widget &widget, bool instant)
Definition: Association.h:148
Gtk::Range type_widget
Definition: Association.h:278
void connect_widget(Callback widget_changed)
Definition: Association.h:280
void load_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:230
void save_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:247
void load_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:405
Definition: Association.h:39
Association(const Glib::ustring &full_key, type_widget &widget, bool instant)
Definition: Association.h:441
sigc::slot< void > Callback
Definition: AssociationBase.h:63
void connect_widget(Callback widget_changed)
Definition: Association.h:105
Glib::RefPtr< const Gnome::Conf::Client > get_conf_client() const
static const AssociationPtr create(const Glib::ustring &full_key, T_Widget &widget, bool instant)
Definition: Association.h:42
void connect_widget(Callback widget_changed)
Definition: Association.h:162
void load_widget(std::auto_ptr< Glib::Error > &error)
Definition: Association.h:288
Association(const Glib::ustring &full_key, type_widget &widget, bool instant)
Definition: Association.h:208
virtual void connect_widget(Callback on_widget_changed)
These methods must be implemented explicitly for each specialization of Association to provide app...
Provides behaviors that are common to all widget/key associations.
Definition: AssociationBase.h:34
virtual void save_widget(std::auto_ptr< Glib::Error > &error)
A shared reference-counting smart-pointer.
Definition: sharedptr.h:31