]> git.tdb.fi Git - libs/gltk.git/commitdiff
Change State into a bitmask to allow more fine-grained control of styles
authorMikko Rasa <tdb@tdb.fi>
Mon, 3 Mar 2008 17:48:55 +0000 (17:48 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 3 Mar 2008 17:48:55 +0000 (17:48 +0000)
17 files changed:
source/button.cpp
source/button.h
source/dropdown.cpp
source/dropdown.h
source/entry.cpp
source/entry.h
source/hslider.cpp
source/indicator.cpp
source/part.cpp
source/slider.cpp
source/state.cpp
source/state.h
source/toggle.cpp
source/toggle.h
source/vslider.cpp
source/widget.cpp
source/widget.h

index 3661c0148a0f56f61d35e85ad08de4074e3a4f4b..20e0ed9241d616a36c2a1f154358931f8d92acf3 100644 (file)
@@ -29,7 +29,7 @@ void Button::button_press(int, int, unsigned btn)
        if(btn==1)
        {
                pressed=true;
-               state=ACTIVE;
+               state|=ACTIVE;
        }
 }
 
@@ -38,25 +38,17 @@ void Button::button_release(int x, int y, unsigned btn)
        if(pressed && btn==1)
        {
                if(geom.is_inside_relative(x, y))
-               {
-                       state=HOVER;
                        signal_clicked.emit();
-               }
-               else
-                       state=NORMAL;
                
+               state&=~ACTIVE;
                pressed=false;
        }
 }
 
-void Button::pointer_enter()
-{
-       state=HOVER;
-}
-
 void Button::pointer_leave()
 {
-       state=NORMAL;
+       Widget::pointer_leave();
+       state&=~ACTIVE;
 }
 
 void Button::render_special(const Part &part) const
index ea96a7d01b69518d47d5bcb406406f1f653e186e..63159ee2318dcc6cd1ecf81e47d8b22797b2fee9 100644 (file)
@@ -39,7 +39,6 @@ public:
        void set_text(const std::string &);
        virtual void button_press(int, int, unsigned);
        virtual void button_release(int, int, unsigned);
-       virtual void pointer_enter();
        virtual void pointer_leave();
 private:
        virtual const char *get_class() const { return "button"; }
index 5f49b9ef33396dea8cd8141d92dcfe52386f9cfd..4d18187a7fb28642934dd5a5aeb0adb0038e85b6 100644 (file)
@@ -18,6 +18,7 @@ namespace GLtk {
 Dropdown::Dropdown(const Resources &r):
        Widget(r),
        list(new List(res)),
+       dropped(false),
        list_active(false)
 {
        list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
@@ -43,14 +44,16 @@ void Dropdown::button_press(int x, int y, unsigned btn)
                list->button_press(x-lgeom.x, y-lgeom.y, btn);
                list_active=true;
        }
-       else if(state==ACTIVE)
+       else if(dropped)
        {
-               state=HOVER;
+               dropped=false;
+               state&=~ACTIVE;
                parent->ungrab_pointer(*this);
        }
        else if(btn==1)
        {
-               state=ACTIVE;
+               dropped=true;
+               state|=ACTIVE;
 
                if(parent)
                {
@@ -79,23 +82,11 @@ void Dropdown::pointer_motion(int x, int y)
        }
 }
 
-void Dropdown::pointer_enter()
-{
-       if(state==NORMAL)
-               state=HOVER;
-}
-
-void Dropdown::pointer_leave()
-{
-       if(state==HOVER)
-               state=NORMAL;
-}
-
 void Dropdown::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
                render_text(part, text);
-       else if(part.get_name()=="list" && state==ACTIVE)
+       else if(part.get_name()=="list" && dropped)
                list->render();
 }
 
@@ -109,7 +100,8 @@ void Dropdown::list_item_selected(unsigned index, const std::string &item)
        text=item;
 
        list_active=false;
-       state=NORMAL;
+       dropped=false;
+       state&=~ACTIVE;
        if(parent)
                parent->ungrab_pointer(*this);
 
index d1a2609772fbe85047664f33a599c9c9d008d009..49088e20f2dc8e01e6567f7b6eb69618f1febc0a 100644 (file)
@@ -30,6 +30,7 @@ public:
 private:
        List *list;
        std::string text;
+       bool dropped;
        bool list_active;
 
 public:
@@ -43,8 +44,6 @@ public:
        virtual void button_press(int, int, unsigned);
        virtual void button_release(int, int, unsigned);
        virtual void pointer_motion(int, int);
-       virtual void pointer_enter();
-       virtual void pointer_leave();
 private:
        virtual const char *get_class() const { return "dropdown"; }
        virtual void render_special(const Part &) const;
index 7e503b3dbc0e960b8c04397dc27b0ff14e640436..9f7c761802f5de84af3e77a764788f929865cf7c 100644 (file)
@@ -57,18 +57,6 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch)
        }
 }
 
-void Entry::focus_in()
-{
-       if(state!=DISABLED)
-               state=ACTIVE;
-}
-
-void Entry::focus_out()
-{
-       if(state==ACTIVE)
-               state=NORMAL;
-}
-
 void Entry::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
index c880bb4f8f639324e880cdce656a9c517173ad1f..e5f3fd88be639bb22d083b93822cdb5109de23ec 100644 (file)
@@ -41,8 +41,6 @@ public:
        const std::string &get_text() const { return text; }
 
        virtual void key_press(unsigned, unsigned, wchar_t);
-       virtual void focus_in();
-       virtual void focus_out();
 private:
        virtual const char *get_class() const { return "entry"; }
        virtual void render_special(const Part &) const;
index 32d0f6521b98f3ddae610557d71625d30aaed43e..a0caca54b17d2292a68f9fc219d27ace4b74a74f 100644 (file)
@@ -27,20 +27,14 @@ void HSlider::button_press(int x, int y, unsigned btn)
        {
                int sx=geom.x+static_cast<int>((geom.w-slider_size)*(value-min)/(max-min));
                if(btn==1 && x>=sx && x<static_cast<int>(sx+slider_size))
-               {
-                       state=ACTIVE;
                        start_drag(x);
-               }
        }
 }
 
 void HSlider::button_release(int, int, unsigned btn)
 {
        if(btn==1)
-       {
                end_drag();
-               state=NORMAL;
-       }
 }
 
 void HSlider::pointer_motion(int x, int)
index 86c25247e28eee4d08e6e41938f58526317ee74c..2debf35d01ee02d973f1170d6c98ad4fbe914e57 100644 (file)
@@ -18,7 +18,10 @@ Indicator::Indicator(const Resources &r):
 
 void Indicator::set_active(bool a)
 {
-       state=(a ? ACTIVE : NORMAL);
+       if(a)
+               state|=ACTIVE;
+       else
+               state&=~ACTIVE;
 }
 
 } // namespace GLtk
index a74c91141ca717c86262c8a634f8db75ff359226..1f58b6187d679c983ad044310b20b5b739e1a6c5 100644 (file)
@@ -56,21 +56,20 @@ Part::Loader::Loader(Part &p, Resources &r):
 Part::Loader::~Loader()
 {
        for(unsigned i=0; i<N_STATES_; ++i)
-       {
                if(part.graphic[i])
                {
                        const Sides &shadow=part.graphic[i]->get_shadow();
                        part.geom.w=max(part.geom.w, part.graphic[i]->get_width()-shadow.left-shadow.right);
                        part.geom.h=max(part.geom.h, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
                }
-               else
-                       part.graphic[i]=part.graphic[NORMAL];
-       }
 }
 
 void Part::Loader::graphic(State s, const string &n)
 {
-       part.graphic[s]=res.get<Graphic>(n);
+       Graphic *grph=res.get<Graphic>(n);
+       for(int i=0; i<N_STATES_; ++i)
+               if((i&s)==s)
+                       part.graphic[i]=grph;
 }
 
 void Part::Loader::align(float x, float y)
index dacd826630bc670cb364f8fa0d27c27f005cdc64..ead5664a303fae8e88ac26c9e0967629a5b02ceb 100644 (file)
@@ -56,6 +56,7 @@ void Slider::start_drag(int p)
        dragging=true;
        drag_start_pos=p;
        drag_start_value=value;
+       state|=ACTIVE;
 }
 
 void Slider::drag(int p)
@@ -67,6 +68,7 @@ void Slider::drag(int p)
 void Slider::end_drag()
 {
        dragging=false;
+       state&=~ACTIVE;
 }
 
 
index 368e69350156337f7e48d2c6a2aaaad8d4f2239a..4e0a1debafadbbd711f03de028252cf20f2ff3c7 100644 (file)
@@ -16,16 +16,33 @@ istream &operator>>(istream &is, State &state)
 {
        string str;
        is>>str;
-       if(str=="NORMAL")
-               state=NORMAL;
-       else if(str=="HOVER")
-               state=HOVER;
-       else if(str=="ACTIVE")
-               state=ACTIVE;
-       else if(str=="DISABLED")
-               state=DISABLED;
-       else
-               is.setstate(ios_base::failbit);
+
+       unsigned start=0;
+       state=NORMAL;
+
+       while(1)
+       {
+               unsigned underscore=str.find('_', start);
+               if(!str.compare(start, underscore-start, "NORMAL"))
+                       state|=NORMAL;
+               else if(!str.compare(start, underscore-start, "HOVER"))
+                       state|=HOVER;
+               else if(!str.compare(start, underscore-start, "ACTIVE"))
+                       state|=ACTIVE;
+               else if(!str.compare(start, underscore-start, "FOCUS"))
+                       state|=FOCUS;
+               else if(!str.compare(start, underscore-start, "DISABLED"))
+                       state|=DISABLED;
+               else
+               {
+                       is.setstate(ios_base::failbit);
+                       break;
+               }
+
+               if(underscore==std::string::npos)
+                       break;
+               start=underscore+1;
+       }
 
        return is;
 }
index 8e29ab063dde7cad948514005ff561f3e986a02a..688d8dcb3baa60cbf7afb0f97302f0275545026f 100644 (file)
@@ -16,13 +16,29 @@ namespace GLtk {
 
 enum State
 {
-       NORMAL,   //< Default state
-       HOVER,    //< Pointer over the widget
-       ACTIVE,   //< Widget is active (e.g. pressed button)
-       DISABLED, //< Widget is unresponsive
-       N_STATES_ //< Sentry value
+       NORMAL=0,   //< Default state
+       HOVER=1,    //< Pointer over the widget
+       ACTIVE=2,   //< Widget is active (e.g. pressed button)
+       FOCUS=4,    //< Widget has input focus
+       DISABLED=8, //< Widget is unresponsive
+       N_STATES_=16 //< Sentry value
 };
 
+inline State operator|(State a, State b)
+{ return static_cast<State>(static_cast<int>(a)|static_cast<int>(b)); }
+
+inline State operator|=(State &a, State b)
+{ a=a|b; return a; }
+
+inline State operator&(State a, State b)
+{ return static_cast<State>(static_cast<int>(a)&static_cast<int>(b)); }
+
+inline State operator&=(State &a, State b)
+{ a=a&b; return a; }
+
+inline State operator~(State a)
+{ return static_cast<State>(~static_cast<int>(a)); }
+
 extern std::istream &operator>>(std::istream &, State &);
 
 } // namespace GLtk
index 1495344219f89013e790636e4d621f21e8399013..dd6ce359cad95ef691884b1e67128fcebafb81be 100644 (file)
@@ -22,7 +22,10 @@ Toggle::Toggle(const Resources &r):
 void Toggle::set_value(bool v)
 {
        value=v;
-       state=(value ? ACTIVE : HOVER);
+       if(value)
+               state|=ACTIVE;
+       else
+               state&=~ACTIVE;
 }
 
 void Toggle::button_press(int, int, unsigned btn)
@@ -40,25 +43,11 @@ void Toggle::button_release(int x, int y, unsigned btn)
                        set_value(!value);
                        signal_toggled.emit(value);
                }
-               else
-                       state=NORMAL;
 
                pressed=false;
        }
 }
 
-void Toggle::pointer_enter()
-{
-       if(!value)
-               state=HOVER;
-}
-
-void Toggle::pointer_leave()
-{
-       if(!value)
-               state=NORMAL;
-}
-
 void Toggle::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
@@ -81,7 +70,10 @@ Toggle &Toggle::Loader::get_object() const
 void Toggle::Loader::finish()
 {
        Toggle &tgl=static_cast<Toggle &>(wdg);
-       tgl.state=(tgl.value ? ACTIVE : NORMAL);
+       if(tgl.value)
+               tgl.state|=ACTIVE;
+       else
+               tgl.state&=~ACTIVE;
 }
 
 } // namespace GLtk
index 6e3d6326fb0388784e7f0b50c6e46329b8be5c98..f01f84bd0ba632c8596f80d1fdbce5f66b0c2907 100644 (file)
@@ -44,8 +44,6 @@ public:
 
        virtual void button_press(int, int, unsigned);
        virtual void button_release(int, int, unsigned);
-       virtual void pointer_enter();
-       virtual void pointer_leave();
 private:
        virtual const char *get_class() const { return "toggle"; }
        virtual void render_special(const Part &) const;
index d9e9e4dee5f5cdeb9201e2f518cc888ed1eee75e..57f98d53ac4de5bb6c512ac4f442fb177a838575 100644 (file)
@@ -27,20 +27,14 @@ void VSlider::button_press(int x, int y, unsigned btn)
        {
                int sy=static_cast<int>((geom.h-slider_size)*(value-min)/(max-min));
                if(btn==1 && y>=sy && y<static_cast<int>(sy+slider_size))
-               {
-                       state=ACTIVE;
                        start_drag(y);
-               }
        }
 }
 
 void VSlider::button_release(int, int, unsigned btn)
 {
        if(btn==1 && dragging)
-       {
                end_drag();
-               state=NORMAL;
-       }
 }
 
 void VSlider::pointer_motion(int, int y)
index 0128367c2b32a6a6c54ae458b3d3a994b38cdf2e..be1f0a0cc90fb981bc0ea1a121e3f3f92d95030e 100644 (file)
@@ -115,6 +115,26 @@ void Widget::render_text(const Part &part, const string &text) const
        GL::pop_matrix();
 }
 
+void Widget::pointer_enter()
+{
+       state|=HOVER;
+}
+
+void Widget::pointer_leave()
+{
+       state&=~HOVER;
+}
+
+void Widget::focus_in()
+{
+       state|=FOCUS;
+}
+
+void Widget::focus_out()
+{
+       state&=~FOCUS;
+}
+
 void Widget::update_style()
 {
        string sname=get_class();
index a83cecc9bef8c14643cc4855faaeda1712ac603d..7fdc9066ec7d395c75fe437f97e049dad267730c 100644 (file)
@@ -79,12 +79,12 @@ public:
        virtual void button_press(int, int, unsigned) { }
        virtual void button_release(int, int, unsigned) { }
        virtual void pointer_motion(int, int) { }
-       virtual void pointer_enter() { }
-       virtual void pointer_leave() { }
+       virtual void pointer_enter();
+       virtual void pointer_leave();
        virtual void key_press(unsigned, unsigned, wchar_t) { }
        virtual void key_release(unsigned, unsigned) { }
-       virtual void focus_in() { }
-       virtual void focus_out() { }
+       virtual void focus_in();
+       virtual void focus_out();
 
 protected:
        /**