object.h

Go to the documentation of this file.
00001 /*
00002   libwt - Vassilis Virvilis Toolkit - a widget library
00003   Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm>
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library 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 GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #ifndef WT_OBJECT_H
00022 #define WT_OBJECT_H
00023 
00024 #include <string>
00025 #include <list>
00026 #include <map>
00027 
00028 #include <sigc++/object.h>
00029 
00030 #include <wt/wsignal.h>
00031 
00032 namespace Wt {
00033 
00034 class SDLTimer;
00035 
00036 class Event;
00037 class ChildEvent;
00038 class CustomEvent;
00039 class TimerEvent;
00040 class MouseEvent;
00041 class WheelEvent;
00042 class FocusEvent;
00043 class KeyEvent;
00044 
00045 class Object : virtual public sigc::trackable {
00046 public:
00047     typedef std::list<Object*> List;
00048 
00049     Object(Object *parent = 0, const std::string &name = "Object::anon");
00050 
00051     virtual ~Object();
00052 
00053     Object *parent() const {
00054         return parent_;
00055     };
00056 
00057     const List& children() const {
00058         return children_;
00059     }
00060 
00061     List& children() {
00062         return children_;
00063     }
00064 
00065     bool isDescendant(const Object *p) const;
00066 
00067     virtual void insertChild(Object *obj);
00068     virtual void removeChild(Object *obj);
00069     void reparent(Object *new_parent);
00070 
00071     template <typename T>
00072     void deleteFirstChild() {
00073         List::iterator it(children().begin());
00074         List::iterator end(children().end());
00075 
00076         while (it != end) {
00077             T *t = dynamic_cast<T *>(*it);
00078             // before iteration invalidation
00079             ++it;
00080             // this will invalidate the iterator
00081             if (t) {
00082                 delete t;
00083                 break;
00084             }
00085         }
00086     }
00087 
00088     template <typename T>
00089     void deleteLastChild() {
00090         List::reverse_iterator it(children().rbegin());
00091         List::reverse_iterator end(children().rend());
00092 
00093         while (it != end) {
00094             T *t = dynamic_cast<T *>(*it);
00095             // before iteration invalidation
00096             ++it;
00097             // this will invalidate the iterator
00098             if (t) {
00099                 delete t;
00100                 break;
00101             }
00102         }
00103     }
00104 
00105     template <typename T>
00106     void deleteAllChildren() {
00107         List::iterator it(children().begin());
00108         List::iterator end(children().end());
00109 
00110         while (it != end) {
00111             T *t = dynamic_cast<T *>(*it);
00112             // before iteration invalidation
00113             ++it;
00114             // this will invalidate the iterator
00115             if (t) {
00116                 delete t;
00117             }
00118         }
00119     }
00120 
00121     /// returns top level parent: it may return itself
00122     Object *topParent() const;
00123 
00124     const std::string& name() const {
00125         return name_;
00126     }
00127 
00128     void installEventFilter(const Object * filterObj);
00129     void removeEventFilter(const Object * obj);
00130     virtual bool eventFilter(Object *, Event *) {
00131         return false;
00132     }
00133 
00134     int startTimer(int interval);
00135     void killTimer(int id);
00136     void killTimers();
00137 
00138     /// signals
00139     Signal01<void, const Object *> destroyed;
00140 
00141     /// event handling
00142     virtual bool event(Event *e);
00143 
00144 protected:
00145     /// event handling specialization
00146     virtual void childEvent(ChildEvent *) {}
00147     virtual void customEvent(CustomEvent *) {}
00148     virtual void timerEvent(TimerEvent *) {}
00149 
00150     bool filterEvent(Event *);
00151 
00152 private:
00153     Object *parent_;
00154     std::string name_;
00155     List children_;
00156     List eventFilters_;
00157 
00158     /// returns the SDLTimer with id \p id or NULL if not exist
00159     SDLTimer *timer(int id) const;
00160 
00161     typedef std::map<int, SDLTimer *> SDLTimerPtrMap;
00162     SDLTimerPtrMap timers_;
00163 };
00164 
00165 std::ostream& operator<<(std::ostream& s, const Object* obj);
00166 std::ostream& operator<<(std::ostream& s, const Object& ref);
00167 std::ostream& operator<<(std::ostream& s, const Event* e);
00168 std::ostream& operator<<(std::ostream& s, const Event& e);
00169 
00170 }
00171 #endif // WT_OBJECT_H

Generated Fri Jul 28 19:23:00 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.