bakery 2.6

Association.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2002 The Bakery team
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the Free
00016  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #ifndef BAKERY_CONFIGURATION_ASSOCIATION_H
00020 #define BAKERY_CONFIGURATION_ASSOCIATION_H
00021 
00022 #include "bakery/Configuration/AssociationBase.h"
00023 #include <gtkmm/togglebutton.h>
00024 #include <gtkmm/entry.h>
00025 #include <gtkmm/range.h>
00026 #include <gtkmm/spinbutton.h>
00027 #include <gtkmm/combo.h>
00028 #include <gtkmm/optionmenu.h>
00029 
00030 namespace Bakery
00031 {
00032 namespace Conf
00033 {
00034   
00035 template< class T_Widget >
00036 class Association;
00037 
00038 template< class T_Widget >
00039 class AssociationCreation : public AssociationBase
00040 {
00041 public:
00042   static const AssociationPtr create(const Glib::ustring& full_key, T_Widget& widget, bool instant)
00043   {
00044     return AssociationPtr( new Association<T_Widget>(full_key, widget, instant) );
00045   }
00046 
00047   virtual ~AssociationCreation()
00048   {
00049   }
00050 
00051   
00052 protected:
00053   AssociationCreation(const Glib::ustring& full_key, T_Widget& widget, bool instant)
00054   : AssociationBase(full_key,instant),
00055     m_widget(widget)
00056   {
00057   }
00058 
00059   AssociationCreation(const AssociationCreation& other); // Not implemented
00060 
00061   T_Widget& m_widget;
00062 };
00063 
00064 template< class T_Widget >
00065 class Association : public AssociationCreation<T_Widget>
00066 {
00067 public:
00068 
00069   //For some reason, the compiler can not use this if it is in the base class:
00070   //typedef AssociationCreation<T_Widget>::Callback Callback;
00071   typedef sigc::slot<void> Callback;
00072   
00077   virtual void connect_widget(Callback on_widget_changed);
00078 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00079   virtual void load_widget();
00080   virtual void save_widget();
00081 #else
00082   virtual void load_widget(std::auto_ptr<Glib::Error>& error);
00083   virtual void save_widget(std::auto_ptr<Glib::Error>& error);
00084 #endif
00085   
00086 protected:
00087   Association(const Glib::ustring& full_key, T_Widget& widget, bool instant)
00088   : AssociationCreation<T_Widget>(full_key, widget, instant)
00089   {};
00090 };
00091 
00092 //----------------------------------------------------------------------
00093 // Association<T> specializations. These provide widget/key
00094 // association behaviors that are specific to individual widget types.
00095 //----------------------------------------------------------------------
00096 
00097 //SpinButton specializatipn:
00098 
00099 template<>
00100 class Association<Gtk::SpinButton>  : public AssociationCreation<Gtk::SpinButton>
00101 {
00102 public:
00103   typedef Gtk::SpinButton type_widget;
00104   
00105   void connect_widget(Callback widget_changed)
00106   {
00107     m_widget.signal_value_changed().connect(widget_changed);
00108   }
00109 
00110 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00111   void load_widget()
00112 #else
00113   void load_widget(std::auto_ptr<Glib::Error>& error)
00114 #endif // GLIBMM_EXCEPTIONS_ENABLED
00115   {
00116 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00117     double val = get_conf_client()->get_float(get_key());
00118 #else
00119     double val = get_conf_client()->get_float(get_key(), error);
00120     if (error.get() != NULL)
00121 #endif
00122       if (m_widget.get_value() != val)
00123         m_widget.set_value(val);
00124   }
00125 
00126 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00127   void save_widget()
00128 #else
00129   void save_widget(std::auto_ptr<Glib::Error>& error)
00130 #endif
00131   {
00132     double val = m_widget.get_value();
00133 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00134     double existing_val = get_conf_client()->get_float(get_key());
00135 #else
00136     double existing_val = get_conf_client()->get_float(get_key(), error);
00137     if (error.get() != NULL)
00138 #endif
00139       if (existing_val != val)
00140 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00141         get_conf_client()->set(get_key(), val);
00142 #else
00143         get_conf_client()->set(get_key(), val, error);
00144 #endif
00145   }
00146   
00147  
00148   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00149   : AssociationCreation<type_widget>(full_key, widget, instant)
00150   {};
00151 };
00152 
00153 
00154 //Gtk::Entry specializatipn:
00155 
00156 template<>
00157 class Association<Gtk::Entry>  : public AssociationCreation<Gtk::Entry>
00158 {
00159 public:
00160   typedef Gtk::Entry type_widget;
00161   
00162   void connect_widget(Callback widget_changed)
00163   {
00164     m_widget.signal_changed().connect(widget_changed);
00165   }
00166 
00167 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00168   void load_widget()
00169 #else
00170   void load_widget(std::auto_ptr<Glib::Error>& error)
00171 #endif
00172   {
00173     // Only set it if it has changed (avoids excess notifications).
00174 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00175     Glib::ustring val = get_conf_client()->get_string(get_key());
00176 #else
00177     Glib::ustring val = get_conf_client()->get_string(get_key(), error);
00178     if (error.get() != NULL)
00179 #endif
00180       if (m_widget.get_text() != val)
00181         m_widget.set_text(val);
00182   }
00183 
00184 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00185   void save_widget()
00186 #else
00187   void save_widget(std::auto_ptr<Glib::Error>& error)
00188 #endif
00189   {
00190     // Only set it if it has changed (avoids excess notifications).
00191     Glib::ustring val = m_widget.get_text();
00192 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00193     Glib::ustring existing_val = get_conf_client()->get_string(get_key());
00194 #else
00195     Glib::ustring existing_val = get_conf_client()->get_string(get_key(), error);
00196     if (error.get() != NULL)
00197 #endif
00198     {
00199       if (existing_val != val)
00200 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00201         get_conf_client()->set(get_key(), val);
00202 #else
00203         get_conf_client()->set(get_key(), val, error);
00204 #endif
00205     }
00206   }
00207   
00208   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00209   : AssociationCreation<type_widget>(full_key, widget, instant)
00210   {};
00211 };
00212 
00213 
00214 //Gtk::ToggleButton specializatipn:
00215 
00216 template<>
00217 class Association<Gtk::ToggleButton>  : public AssociationCreation<Gtk::ToggleButton>
00218 {
00219 public:
00220   typedef Gtk::ToggleButton type_widget;
00221   
00222   void connect_widget(Callback widget_changed)
00223   {
00224     m_widget.signal_toggled().connect(widget_changed);
00225   }
00226 
00227 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00228   void load_widget()
00229 #else
00230   void load_widget(std::auto_ptr<Glib::Error>& error)
00231 #endif
00232   {
00233     // Only set it if it has changed (avoids excess notifications).
00234 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00235     bool val = get_conf_client()->get_bool(get_key());
00236 #else
00237     bool val = get_conf_client()->get_bool(get_key(), error);
00238     if (error.get() != NULL)
00239 #endif
00240       if (m_widget.get_active() != val)
00241         m_widget.set_active(val);
00242   }
00243 
00244 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00245   void save_widget()
00246 #else
00247   void save_widget(std::auto_ptr<Glib::Error>& error)
00248 #endif
00249   {
00250     // Only set it if it has changed (avoids excess notifications).
00251     bool val = m_widget.get_active();
00252 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00253     bool existing_val = get_conf_client()->get_bool(get_key());
00254 #else
00255     bool existing_val = get_conf_client()->get_bool(get_key(), error);
00256     if (error.get() != NULL)
00257 #endif
00258       if (existing_val != val)
00259 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00260       get_conf_client()->set(get_key(), val);
00261 #else
00262       get_conf_client()->set(get_key(), val, error);
00263 #endif
00264   }
00265   
00266   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00267   : AssociationCreation<type_widget>(full_key, widget, instant)
00268   {};
00269 };
00270 
00271 
00272 //Gtk::Range specializatipn:
00273 
00274 template<>
00275 class Association<Gtk::Range>  : public AssociationCreation<Gtk::Range>
00276 {
00277 public:
00278   typedef Gtk::Range type_widget;
00279   
00280   void connect_widget(Callback widget_changed)
00281   {
00282     m_widget.signal_value_changed().connect(widget_changed);
00283   }
00284 
00285 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00286   void load_widget()
00287 #else
00288   void load_widget(std::auto_ptr<Glib::Error>& error)
00289 #endif
00290   {
00291 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00292     double val = get_conf_client()->get_float(get_key());
00293 #else
00294     double val = get_conf_client()->get_float(get_key(), error);
00295     if (error.get() != NULL)
00296 #endif
00297       if (m_widget.get_value() != val)
00298         m_widget.set_value(val);
00299   }
00300 
00301 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00302   void save_widget()
00303 #else
00304   void save_widget(std::auto_ptr<Glib::Error>& error)
00305 #endif
00306   {
00307     double val = m_widget.get_value();
00308 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00309     double existing_val = get_conf_client()->get_float(get_key());
00310 #else
00311     double existing_val = get_conf_client()->get_float(get_key(), error);
00312     if (error.get() != NULL)
00313 #endif
00314       if (existing_val != val)
00315 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00316         get_conf_client()->set(get_key(), val);
00317 #else
00318         get_conf_client()->set(get_key(), val, error);
00319 #endif
00320   }
00321   
00322 
00323   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00324   : AssociationCreation<type_widget>(full_key, widget, instant)
00325   {};
00326 };
00327 
00328 
00329 #ifndef GTKMM_DISABLE_DEPRECATED
00330 //Gtk::OptionMenu specializatipn:
00331 //Note that OptionMenu is deprecated in favour of ComboBox anyway.
00332 
00333 template<>
00334 class Association<Gtk::OptionMenu>  : public AssociationCreation<Gtk::OptionMenu>
00335 {
00336 public:
00337   typedef Gtk::OptionMenu type_widget;
00338   
00339   void connect_widget(Callback widget_changed)
00340   {
00341     m_widget.signal_changed().connect(widget_changed);
00342   }
00343 
00344 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00345   void load_widget()
00346 #else
00347   void load_widget(std::auto_ptr<Glib::Error>& error)
00348 #endif
00349   {
00350     // Only set it if it has changed (avoids excess notifications).
00351 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00352     int val = get_conf_client()->get_int(get_key());
00353 #else
00354     int val = get_conf_client()->get_int(get_key(), error);
00355     if (error.get() != NULL)
00356 #endif
00357     if (m_widget.get_history() != val)
00358       m_widget.set_history(guint(val));
00359   }
00360 
00361 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00362   void save_widget()
00363 #else
00364   void save_widget(std::auto_ptr<Glib::Error>& error)
00365 #endif
00366   {
00367     // Only set it if it has changed (avoids excess notifications).
00368     int val = m_widget.get_history();
00369 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00370     int existing_val = get_conf_client()->get_int(get_key());
00371 #else
00372     int existing_val = get_conf_client()->get_int(get_key(), error);
00373     if (error.get() != NULL)
00374 #endif
00375           if (existing_val != val)
00376 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00377             get_conf_client()->set(get_key(), val);
00378 #else
00379             get_conf_client()->set(get_key(), val, error);
00380 #endif
00381       }
00382       
00383       Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00384       : AssociationCreation<type_widget>(full_key, widget, instant)
00385       {}
00386     };
00387 
00388     //Gtk::Combo specializatipn:
00389     //Note that Combo is deprecated in favour of ComboBox anyway.
00390 
00391     template<>
00392     class Association<Gtk::Combo>  : public AssociationCreation<Gtk::Combo>
00393     {
00394     public:
00395       typedef Gtk::Combo type_widget;
00396       
00397       void connect_widget(Callback widget_changed)
00398       {
00399         m_widget.get_entry()->signal_changed().connect(widget_changed);
00400       }
00401 
00402 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00403       void load_widget()
00404 #else
00405       void load_widget(std::auto_ptr<Glib::Error>& error)
00406 #endif
00407       {
00408         // Only set it if it has changed (avoids excess notifications).
00409 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00410         Glib::ustring val = get_conf_client()->get_string(get_key());
00411 #else
00412         Glib::ustring val = get_conf_client()->get_string(get_key(), error);
00413         if (error.get() != NULL)
00414 #endif
00415           if (m_widget.get_entry()->get_text() != val)
00416             m_widget.get_entry()->set_text(val);
00417       }
00418 
00419 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00420       void save_widget()
00421 #else
00422       void save_widget(std::auto_ptr<Glib::Error>& error)
00423 #endif
00424       {
00425         // Only set it if it has changed (avoids excess notifications).
00426         Glib::ustring val = m_widget.get_entry()->get_text();
00427 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00428         Glib::ustring existing_val = get_conf_client()->get_string(get_key());
00429 #else
00430         Glib::ustring existing_val = get_conf_client()->get_string(get_key(), error);
00431         if (error.get() != NULL)
00432 #endif
00433           if (existing_val != val)
00434 #ifdef GLIBMM_EXCEPTIONS_ENABLED
00435         get_conf_client()->set(get_key(), val);
00436 #else
00437         get_conf_client()->set(get_key(), val, error);
00438 #endif
00439   }
00440 
00441   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00442   : AssociationCreation<type_widget>(full_key, widget, instant)
00443   {}
00444 };
00445 #endif // !GTKMM_DISABLE_DEPRECATED
00446 
00447 } //namespace Conf
00448 
00449 } //namespace Bakery
00450 
00451 #endif //BAKERY_CONFIGURATION_ASSOCIATION_H