]> git.tdb.fi Git - libs/gltk.git/commitdiff
Support loading Panels from datafiles
authorMikko Rasa <tdb@tdb.fi>
Thu, 22 Nov 2007 15:27:08 +0000 (15:27 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 22 Nov 2007 15:27:08 +0000 (15:27 +0000)
Refactor the Panel focus logic
Add visibility property to widgets

source/panel.cpp
source/panel.h
source/widget.cpp
source/widget.h

index c93d430be07f16a988fa1a120af013104cd8889e..efe857ec6860be568acc50b4e37134cd2f85ec44 100644 (file)
@@ -29,13 +29,12 @@ void Panel::button_press(int x, int y, unsigned btn)
                pointer_focus->button_press(x-geom.x, y-geom.y, btn);
        else if(geom.is_inside(x, y))
        {
-               for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
-                       if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
-                       {
-                               (*i)->button_press(x-geom.x, y-geom.y, btn);
-                               pointer_grab=btn;
-                               set_input_focus(*i);
-                       }
+               if(Widget *wdg=get_child_at(x, y))
+               {
+                       wdg->button_press(x-geom.x, y-geom.y, btn);
+                       pointer_grab=btn;
+                       set_input_focus(wdg);
+               }
        }
 }
 
@@ -49,19 +48,13 @@ void Panel::button_release(int x, int y, unsigned btn)
                {
                        pointer_grab=0;
 
-                       for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
-                               if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
-                               {
-                                       set_pointer_focus(*i);
-                                       break;
-                               }
+                       set_pointer_focus(get_child_at(x, y));
                }
        }
        else if(geom.is_inside(x, y))
        {
-               for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
-                       if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
-                               (*i)->button_release(x-geom.x, y-geom.y, btn);
+               if(Widget *wdg=get_child_at(x, y))
+                       wdg->button_release(x-geom.x, y-geom.y, btn);
        }
 }
 
@@ -71,17 +64,10 @@ void Panel::pointer_motion(int x, int y)
                pointer_focus->pointer_motion(x-geom.x, y-geom.y);
        else if(geom.is_inside(x, y))
        {
-               bool found=false;
-               for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
-                       if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
-                       {
-                               set_pointer_focus(*i);
-                               (*i)->pointer_motion(x-geom.x, y-geom.y);
-                               found=true;
-                       }
-
-               if(!found)
-                       set_pointer_focus(0);
+               Widget *wdg=get_child_at(x, y);
+               set_pointer_focus(wdg);
+               if(wdg)
+                       wdg->pointer_motion(x-geom.x, y-geom.y);
        }
 }
 
@@ -112,7 +98,8 @@ void Panel::render_part(const Part &part) const
        if(part.get_name()=="children")
        {
                for(ChildSeq::const_iterator i=children.begin(); i!=children.end(); ++i)
-                       (*i)->render();
+                       if((*i)->is_visible())
+                               (*i)->render();
        }
        else
                Widget::render_part(part);
@@ -146,23 +133,41 @@ void Panel::set_input_focus(Widget *wdg)
        }
 }
 
+Widget *Panel::get_child_at(int x, int y)
+{
+       for(ChildSeq::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
+               if((*i)->is_visible() && (*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
+                       return *i;
+
+       return 0;
+}
+
 
 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        Widget::Loader(p),
-       panel(p),
+       pnl(p),
        wdg_map(m)
 {
        add("label", &Loader::child<Label>);
+       add("panel", &Loader::panel);
 }
 
 template<typename T>
 void Panel::Loader::child(const string &n)
 {
-       RefPtr<T> chl=new T(panel.res);
+       RefPtr<T> chl=new T(pnl.res);
        load_sub(*chl);
-       panel.add(*chl.get());
+       pnl.add(*chl.get());
        wdg_map[n]=chl.release();
 }
 
+void Panel::Loader::panel(const string &n)
+{
+       RefPtr<Panel> p=new Panel(pnl.res);
+       load_sub(*p, wdg_map);
+       pnl.add(*p.get());
+       wdg_map[n]=p.release();
+}
+
 } // namespace GLtk
 } // namespace Msp
index bf17829c101449747c07a23d61170811dcb08d4c..d922a991eb281b16017304786388d8915bc78170 100644 (file)
@@ -12,7 +12,7 @@ public:
        class Loader: public Widget::Loader
        {
        private:
-               Panel &panel;
+               Panel &pnl;
                std::map<std::string, Widget *> &wdg_map;
        
        public:
@@ -20,6 +20,7 @@ public:
        private:
                template<typename T>
                void child(const std::string &);
+               void panel(const std::string &);
        };
 
        Panel(const Resources &);
@@ -46,6 +47,7 @@ private:
        void render_part(const Part &) const;
        void set_pointer_focus(Widget *);
        void set_input_focus(Widget *);
+       Widget *get_child_at(int, int);
 };
 
 } // namespace GLtk
index 8674a138ab7b6cf240ff7510524c346e3265a77a..e1538dec221bedc9a190c5904421ce6e1d16d87c 100644 (file)
@@ -47,7 +47,8 @@ void Widget::render() const
 Widget::Widget(const Resources &r):
        res(r),
        style(0),
-       state(NORMAL)
+       state(NORMAL),
+       visible(true)
 { }
 
 void Widget::update_style()
@@ -100,6 +101,7 @@ Widget::Loader::Loader(Widget &w):
        add("position", &Loader::position);
        add("size",     &Loader::size);
        add("style",    &Loader::style);
+       add("visible",  &Widget::visible);
 }
 
 void Widget::Loader::position(int x, int y)
index 79187f3b5e864bf40b77fdab827b93e676c3d38a..557df434adcd34f98d858324ab753e8271b9a1a5 100644 (file)
@@ -22,6 +22,7 @@ public:
 
        public:
                Loader(Widget &);
+               Widget &get_object() const { return wdg; }
        private:
                void position(int, int);
                void size(unsigned, unsigned);
@@ -34,6 +35,7 @@ public:
        void set_geometry(const Geometry &);
        void set_style(const std::string &);
        const Geometry &get_geometry() const { return geom; }
+       bool is_visible() const { return visible; }
        void render() const;
        virtual void button_press(int, int, unsigned) { }
        virtual void button_release(int, int, unsigned) { }
@@ -50,6 +52,7 @@ protected:
        std::string style_name;
        const Style *style;
        State state;
+       bool visible;
 
        Widget(const Resources &);
        virtual const char *get_class() const { return "widget"; }