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   virtual void load_widget();
00079   virtual void save_widget();
00080   
00081 protected:
00082   Association(const Glib::ustring& full_key, T_Widget& widget, bool instant)
00083   : AssociationCreation<T_Widget>(full_key, widget, instant)
00084   {};
00085 };
00086 
00087 //----------------------------------------------------------------------
00088 // Association<T> specializations. These provide widget/key
00089 // association behaviors that are specific to individual widget types.
00090 //----------------------------------------------------------------------
00091 
00092 //SpinButton specializatipn:
00093 
00094 template<>
00095 class Association<Gtk::SpinButton>  : public AssociationCreation<Gtk::SpinButton>
00096 {
00097 public:
00098   typedef Gtk::SpinButton type_widget;
00099   
00100   void connect_widget(Callback widget_changed)
00101   {
00102     m_widget.signal_value_changed().connect(widget_changed);
00103   }
00104 
00105   void load_widget()
00106   {
00107     double val = get_conf_client()->get_float(get_key());
00108     if(m_widget.get_value() != val)
00109       m_widget.set_value(val);
00110   }
00111 
00112   void save_widget()
00113   {
00114     double val = m_widget.get_value();
00115     if(get_conf_client()->get_float(get_key()) != val)
00116       get_conf_client()->set(get_key(), val);
00117   }
00118   
00119  
00120   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00121   : AssociationCreation<type_widget>(full_key, widget, instant)
00122   {};
00123 };
00124 
00125 
00126 //Gtk::Entry specializatipn:
00127 
00128 template<>
00129 class Association<Gtk::Entry>  : public AssociationCreation<Gtk::Entry>
00130 {
00131 public:
00132   typedef Gtk::Entry type_widget;
00133   
00134   void connect_widget(Callback widget_changed)
00135   {
00136     m_widget.signal_changed().connect(widget_changed);
00137   }
00138 
00139   void load_widget()
00140   {
00141     // Only set it if it has changed (avoids excess notifications).
00142     Glib::ustring val = get_conf_client()->get_string(get_key());
00143     if(m_widget.get_text() != val)
00144       m_widget.set_text(val);
00145   }
00146 
00147   void save_widget()
00148   {
00149     // Only set it if it has changed (avoids excess notifications).
00150     Glib::ustring val = m_widget.get_text();
00151     if(get_conf_client()->get_string(get_key()) != val)
00152       get_conf_client()->set(get_key(), val);
00153   }
00154   
00155   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00156   : AssociationCreation<type_widget>(full_key, widget, instant)
00157   {};
00158 };
00159 
00160 
00161 //Gtk::ToggleButton specializatipn:
00162 
00163 template<>
00164 class Association<Gtk::ToggleButton>  : public AssociationCreation<Gtk::ToggleButton>
00165 {
00166 public:
00167   typedef Gtk::ToggleButton type_widget;
00168   
00169   void connect_widget(Callback widget_changed)
00170   {
00171     m_widget.signal_toggled().connect(widget_changed);
00172   }
00173 
00174   void load_widget()
00175   {
00176     // Only set it if it has changed (avoids excess notifications).
00177     bool val = get_conf_client()->get_bool(get_key());
00178     if (m_widget.get_active() != val)
00179       m_widget.set_active(val);
00180   }
00181 
00182   void save_widget()
00183   {
00184     // Only set it if it has changed (avoids excess notifications).
00185     bool val = m_widget.get_active();
00186     if(get_conf_client()->get_bool(get_key()) != val)
00187       get_conf_client()->set(get_key(), val);
00188   }
00189   
00190   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00191   : AssociationCreation<type_widget>(full_key, widget, instant)
00192   {};
00193 };
00194 
00195 
00196 //Gtk::Range specializatipn:
00197 
00198 template<>
00199 class Association<Gtk::Range>  : public AssociationCreation<Gtk::Range>
00200 {
00201 public:
00202   typedef Gtk::Range type_widget;
00203   
00204   void connect_widget(Callback widget_changed)
00205   {
00206     m_widget.signal_value_changed().connect(widget_changed);
00207   }
00208 
00209   void load_widget()
00210   {
00211     double val = get_conf_client()->get_float(get_key());
00212     if (m_widget.get_value() != val)
00213       m_widget.set_value(val);
00214   }
00215 
00216   void save_widget()
00217   {
00218     double val = m_widget.get_value();
00219     if (get_conf_client()->get_float(get_key()) != val)
00220       get_conf_client()->set(get_key(), val);
00221   }
00222   
00223 
00224   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00225   : AssociationCreation<type_widget>(full_key, widget, instant)
00226   {};
00227 };
00228 
00229 
00230 //Gtk::OptionMenu specializatipn:
00231 //Note that OptionMenu is deprecated in favour of ComboBox anyway.
00232 
00233 template<>
00234 class Association<Gtk::OptionMenu>  : public AssociationCreation<Gtk::OptionMenu>
00235 {
00236 public:
00237   typedef Gtk::OptionMenu type_widget;
00238   
00239   void connect_widget(Callback widget_changed)
00240   {
00241     m_widget.signal_changed().connect(widget_changed);
00242   }
00243 
00244   void load_widget()
00245   {
00246     // Only set it if it has changed (avoids excess notifications).
00247     int val = get_conf_client()->get_int(get_key());
00248     if (m_widget.get_history() != val)
00249       m_widget.set_history(guint(val));
00250   }
00251 
00252   void save_widget()
00253   {
00254     // Only set it if it has changed (avoids excess notifications).
00255     int val = m_widget.get_history();
00256     if (get_conf_client()->get_int(get_key()) != val)
00257       get_conf_client()->set(get_key(), val);
00258   }
00259   
00260   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00261   : AssociationCreation<type_widget>(full_key, widget, instant)
00262   {}
00263 };
00264 
00265 //Gtk::Combo specializatipn:
00266 //Note that Combo is deprecated in favour of ComboBox anyway.
00267 
00268 template<>
00269 class Association<Gtk::Combo>  : public AssociationCreation<Gtk::Combo>
00270 {
00271 public:
00272   typedef Gtk::Combo type_widget;
00273   
00274   void connect_widget(Callback widget_changed)
00275   {
00276     m_widget.get_entry()->signal_changed().connect(widget_changed);
00277   }
00278 
00279   void load_widget()
00280   {
00281     // Only set it if it has changed (avoids excess notifications).
00282     Glib::ustring val = get_conf_client()->get_string(get_key());
00283     if(m_widget.get_entry()->get_text() != val)
00284       m_widget.get_entry()->set_text(val);
00285   }
00286 
00287   void save_widget()
00288   {
00289     // Only set it if it has changed (avoids excess notifications).
00290     Glib::ustring val = m_widget.get_entry()->get_text();
00291     if(get_conf_client()->get_string(get_key()) != val)
00292       get_conf_client()->set(get_key(), val);
00293   }
00294 
00295   Association(const Glib::ustring& full_key, type_widget& widget, bool instant)
00296   : AssociationCreation<type_widget>(full_key, widget, instant)
00297   {}
00298 };
00299 
00300 } //namespace Conf
00301 
00302 } //namespace Bakery
00303 
00304 #endif //BAKERY_CONFIGURATION_ASSOCIATION_H

Generated on Thu Aug 24 08:45:28 2006 for bakery by  doxygen 1.4.7