00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Serge Lussier,,, * 00003 * serge.lussier@videotron.ca * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the * 00017 * Free Software Foundation, Inc., * 00018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00019 ***************************************************************************/ 00020 00021 #include <stdui.h> 00022 #ifndef STDUISTDOBJECT_H 00023 #define STDUISTDOBJECT_H 00024 00025 #include <stdui.h> 00026 00027 #include <list> 00028 #include <stack> 00029 #include <stdmessage.h> 00030 #include <cevent.h> 00031 00032 namespace stdui { 00033 00041 class stdobject : public sigc::trackable 00042 { 00043 public: 00044 typedef std::list<stdobject*> list; 00045 typedef stdobject::list::iterator iterator; 00046 typedef stdobject::list::const_iterator const_iterator; 00047 00048 stdobject(); 00049 stdobject(stdobject* __parentobj, const std::string& nameid="undef", stdflags __flags=0); 00050 00056 template <class T> int parent_chain(std::list<T*>& l); 00057 template <class T> T* parentof(); 00058 template <class T> int childrenof(std::list<T>& l); 00059 template <class T> T* firstchildof(); 00060 template <class T> T* nextchildof(); 00061 template <class T> T* lastchildof(); 00062 00063 virtual ~stdobject(); 00064 int addchild(stdobject* _obj); 00065 00070 stdobject* parent() { return _parent; } 00071 int destroy_children(); 00072 int setparent(stdobject* _obj); 00073 00079 sigc::signal<bool, stdobject*>& destroy_signal() { return destroy_; } 00080 00085 const std::string& classname() const { return _clsname; } 00086 00091 const std::string& name() const { return _name; } 00092 00098 sigc::signal<bool, stdobject*>::iterator destroy_delegate(const sigc::slot<bool, stdobject*>& _slot){ 00099 return destroy_.connect(_slot); 00100 } 00101 00106 pthread_t threadid() { return _thid; } 00107 int postevent( stdmessage msg ); 00108 private: 00110 stdflag _flags; 00112 list::iterator _tpl_it; 00113 pthread_t _thid; 00114 std::stack<stdmessage> _holdmsgqueu; 00115 bool _destroy(stdobject* o); 00116 protected: 00118 stdobject* _parent; 00120 sigc::signal<bool, stdobject*> destroy_; 00122 stdobject::list _children; 00124 list _parents; 00126 std::string _clsname; 00128 std::string _name; 00129 protected: 00130 void purgemessages(); 00131 virtual evt::result messageevent( cmessageevent* mes ); 00132 00133 00137 virtual evt::result keyevent(ckeypressevent* kev) { return evt::reject; } 00138 00142 virtual evt::result mouseevent( cmouseevent* mev ) { return evt::reject; } 00143 }; // end of stdobject 00144 00145 00146 template<class T> int stdobject::parent_chain(std::list<T*>& l) 00147 { 00148 T* pr = parentof<T>(); 00149 while(pr){ 00150 l.push_back(pr); 00151 pr = pr->parentof<T>(); 00152 } 00153 return l.size(); 00154 } 00155 00156 00162 template<class T> T* stdobject::parentof() 00163 { 00164 T* P=0l; 00165 stdobject* o = this; 00166 while(P == 0l){ 00167 o = o->parent(); 00168 if(!o) return 0l; 00169 if( ( P = dynamic_cast<T*>(o) ) != 0l) return P; 00170 } 00171 return (T*)0l; 00172 } 00173 00174 00181 template <class T> int stdobject::childrenof(std::list<T>& l) 00182 { 00183 T* obj=0l; 00184 iterator temp_it = _tpl_it; 00185 if( !l.empty() ) l.clear(); 00186 if( ! ( obj = firstchildof<T>() ) ){ 00187 _tpl_it = temp_it; 00188 return 0; 00189 } 00190 do l.push_back(obj); while( ( obj = nextchildof<T>() ) != 0l); 00191 _tpl_it = temp_it; 00192 return l.size(); 00193 } 00194 00195 00202 template <class T> T* stdobject::firstchildof() 00203 { 00204 T* pobj; 00205 if( _children.empty() ) return (T*)0l; 00206 _tpl_it = _children.begin(); 00207 do{ 00208 if( (pobj = dynamic_cast<T*>(_tpl_it)) != 0l ) return pobj; 00209 _tpl_it++; 00210 } while(_tpl_it != _children.end()); 00211 return (T*)0l; 00212 } 00213 00214 00222 template <class T> T* stdobject::nextchildof() 00223 { 00224 T* pobj; 00225 if(_tpl_it == _children.end() ) if( !(pobj = firstchildof<T>())) return (T*)0l; 00226 _tpl_it++; 00227 do{ 00228 if( (pobj = dynamic_cast<T*>(_tpl_it)) != 0l ) return pobj; 00229 _tpl_it++; 00230 } while(_tpl_it != _children.end()); 00231 return (T*)0l; 00232 } 00233 00234 00241 template <class T> T* stdobject::lastchildof() 00242 { 00243 T* pobj; 00244 iterator it = _children.end(); 00245 it--; 00246 if(it == _children.end()) return (T*)0l; 00247 do{ 00248 if( (pobj = dynamic_cast<T*>(*it)) ) return pobj; 00249 it--; 00250 }while(_tpl_it != _children.end()); 00251 return it == _children.end() ? (T*)0l : *it; 00252 } 00253 00254 00255 00256 } 00257 00258 #endif