UniWidgets  1.0.0
UDefaultFunctions.h
00001 #ifndef _UDEFAULTFUNCTIONS_H
00002 #define _UDEFAULTFUNCTIONS_H
00003 // -------------------------------------------------------------------------
00004 #include <uniwidgets/UVoid.h>
00005 #include <uniwidgets/UPixbufCache.h>
00006 // -------------------------------------------------------------------------
00007 class GuiPM;
00008 // -------------------------------------------------------------------------
00009 #define WRAPPED_TYPENAME( typename ) (std::string( g_type_name(typename) )+ "_uwrapped").c_str()
00010 // -------------------------------------------------------------------------
00019 template<class GtkParentClass>
00020 class UDefaultFunctions : public GtkParentClass, public UVoid, public UPixbufCache
00021 {
00022 public:
00023   typedef GtkParentClass GtkmmBaseType;
00024   typedef UDefaultFunctions<GtkParentClass> BaseType;
00025   UDefaultFunctions() :
00026     Glib::ObjectBase(WRAPPED_TYPENAME(GtkParentClass::get_type()))
00027     ,UVoid(this)
00028     ,property_auto_connect_(*this, "auto-connect", true)
00029     ,property_disconnect_effect_(*this, "disconnect-effect", 1)
00030     ,property_lock_view_(*this, "lock-view", false)
00031   {}
00032 
00033   explicit UDefaultFunctions(typename GtkParentClass::BaseObjectType* o) :
00034     GtkParentClass(o)
00035     ,UVoid(this)
00036     ,property_auto_connect_(*this, "auto-connect", true)
00037     ,property_disconnect_effect_(*this, "disconnect-effect", 1)
00038     ,property_lock_view_(*this, "lock-view", false)
00039   {}
00040 
00041   virtual ~UDefaultFunctions() {}
00045   virtual void on_hierarchy_changed(Gtk::Widget* w);
00046 
00047   GuiPM* search_gpm();                  
00048   ConnectorRef get_connector_from_hierarchy();      
00049   virtual void add_lock(const Gtk::Widget& w);      
00050   virtual void unlock_current();                
00052 private:
00053   Glib::Property<bool> property_auto_connect_;      
00054   Glib::Property<int> property_disconnect_effect_;  
00055   Glib::Property<bool> property_lock_view_;     
00056 };
00057 // -------------------------------------------------------------------------
00058 inline Glib::PropertyProxy<bool>
00059 UVoid::property_auto_connect()
00060 {
00061   return Glib::PropertyProxy<bool>(obj_,"auto-connect");
00062 }
00063 // -------------------------------------------------------------------------
00064 inline Glib::PropertyProxy_ReadOnly<bool>
00065 UVoid::property_auto_connect() const
00066 {
00067   return Glib::PropertyProxy_ReadOnly<bool>(obj_,"auto-connect");
00068 }
00069 // -------------------------------------------------------------------------
00070 inline Glib::PropertyProxy<int>
00071 UVoid::property_disconnect_effect()
00072 {
00073   return Glib::PropertyProxy<int>(obj_,"disconnect-effect");
00074 }
00075 // -------------------------------------------------------------------------
00076 inline Glib::PropertyProxy_ReadOnly<int>
00077 UVoid::property_disconnect_effect() const
00078 {
00079   return Glib::PropertyProxy_ReadOnly<int>(obj_,"disconnect-effect");
00080 }
00081 // -------------------------------------------------------------------------
00082 inline Glib::PropertyProxy<bool>
00083 UVoid::property_lock_view()
00084 {
00085   return Glib::PropertyProxy<bool>(obj_,"lock-view");
00086 }
00087 // -------------------------------------------------------------------------
00088 inline Glib::PropertyProxy_ReadOnly<bool>
00089 UVoid::property_lock_view() const
00090 {
00091   return Glib::PropertyProxy_ReadOnly<bool>(obj_,"lock-view");
00092 }
00093 // -------------------------------------------------------------------------
00094 /* The template functions must be in header.
00095  * The compiler must able to see function implementation on instantiation.
00096  * For more details see http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12 */
00097 template<class T>
00098 GuiPM* UDefaultFunctions<T>::search_gpm()
00099 {
00100   Gtk::Widget* parent = reinterpret_cast<Gtk::Widget*>(T::get_parent());
00101   for (Gtk::Widget* p = parent; p != NULL; p = reinterpret_cast<Gtk::Widget*>(p->get_parent()))
00102     if ( UVoid* uv = dynamic_cast<UVoid*>(p) )// && uv->gpm != NULL)
00103       if ( uv->gpm != NULL) return uv->gpm;
00104   return NULL;
00105 }
00106 // -------------------------------------------------------------------------
00107 template<class T> ConnectorRef
00108 UDefaultFunctions<T>::get_connector_from_hierarchy()
00109 {
00110   Gtk::Widget* parent = reinterpret_cast<Gtk::Widget*>(T::get_parent());
00111   while (parent != NULL) {
00112     if(UVoid* uv = dynamic_cast<UVoid*>(parent)) {
00113       ConnectorRef new_connector = uv->get_connector();
00114       return new_connector;
00115     }
00116     parent = reinterpret_cast<Gtk::Widget*>(parent->get_parent());
00117   }
00118   return ConnectorRef();
00119 }
00120 // -------------------------------------------------------------------------
00121 template<class T> void
00122 UDefaultFunctions<T>::on_hierarchy_changed(Gtk::Widget* w)
00123 {
00124   GtkmmBaseType::on_hierarchy_changed(w);
00125   if ( !property_auto_connect_ )
00126     return;
00127   ConnectorRef connector = get_connector_from_hierarchy();
00128   set_connector(connector);
00129 }
00130 // -------------------------------------------------------------------------
00131 template<class T>
00132 void UDefaultFunctions<T>::add_lock(const Gtk::Widget& w)
00133 {
00134   Gtk::Widget* prev = this;
00135   Gtk::Widget* next = reinterpret_cast<Gtk::Widget*>(T::get_parent());
00136   while (next != NULL) {
00137     if (UVoid* uv = dynamic_cast<UVoid*>(next)) {
00138       uv->add_lock(*prev);
00139       break;
00140     }
00141 
00142     prev = next;
00143     next = reinterpret_cast<Gtk::Widget*>(next->get_parent());
00144   }
00145 }
00146 // -------------------------------------------------------------------------
00147 template<class T>
00148 void UDefaultFunctions<T>::unlock_current()
00149 {
00150   Gtk::Widget* parent = reinterpret_cast<Gtk::Widget*>(T::get_parent());
00151   for (Gtk::Widget* p = parent; p != NULL; p = reinterpret_cast<Gtk::Widget*>(p->get_parent()))
00152     if (UVoid* uv = dynamic_cast<UVoid*>(p)) {
00153       uv->unlock_current();
00154       break;
00155     }
00156 }
00157 // -------------------------------------------------------------------------
00158 #endif