From 2accd84fb2f8cc96efea8b3f27e381c2d2749160 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 30 Nov 2012 19:57:46 +0200 Subject: [PATCH] Add protected functions for manipulating widget state --- source/button.cpp | 8 ++++---- source/dropdown.cpp | 6 +++--- source/indicator.cpp | 5 +---- source/slider.cpp | 4 ++-- source/toggle.cpp | 9 +++------ source/widget.cpp | 13 +++++++++---- source/widget.h | 6 ++++++ 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/source/button.cpp b/source/button.cpp index fb05262..5f9b18b 100644 --- a/source/button.cpp +++ b/source/button.cpp @@ -85,7 +85,7 @@ void Button::button_press(int, int, unsigned btn) if(btn==1) { pressed = true; - state |= ACTIVE; + set_state(ACTIVE); } } @@ -93,7 +93,7 @@ void Button::button_release(int x, int y, unsigned btn) { if(pressed && btn==1) { - state &= ~ACTIVE; + clear_state(ACTIVE); pressed = false; if(geom.is_inside_relative(x, y)) @@ -106,9 +106,9 @@ void Button::pointer_motion(int x, int y) if(pressed) { if(!geom.is_inside_relative(x, y)) - state &= ~ACTIVE; + clear_state(ACTIVE); else - state |= ACTIVE; + set_state(ACTIVE); } } diff --git a/source/dropdown.cpp b/source/dropdown.cpp index cab6d91..47496f4 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -97,14 +97,14 @@ void Dropdown::button_press(int x, int y, unsigned btn) if(!click_focus) { dropped = false; - state &= ~ACTIVE; + clear_state(ACTIVE); signal_ungrab_pointer.emit(); } } else if(btn==1) { dropped = true; - state |= ACTIVE; + set_state(ACTIVE); signal_grab_pointer.emit(); } } @@ -160,7 +160,7 @@ void Dropdown::list_item_selected(unsigned index, const std::string &item) if(dropped) { dropped = false; - state &= ~ACTIVE; + clear_state(ACTIVE); signal_ungrab_pointer.emit(); } diff --git a/source/indicator.cpp b/source/indicator.cpp index 568aafd..fba8166 100644 --- a/source/indicator.cpp +++ b/source/indicator.cpp @@ -10,10 +10,7 @@ Indicator::Indicator() void Indicator::set_active(bool a) { - if(a) - state |= ACTIVE; - else - state &= ~ACTIVE; + set_state(ACTIVE, (a ? ACTIVE : NORMAL)); } } // namespace GLtk diff --git a/source/slider.cpp b/source/slider.cpp index 7a0029e..de4d12e 100644 --- a/source/slider.cpp +++ b/source/slider.cpp @@ -48,7 +48,7 @@ void Slider::start_drag(int p) dragging = true; drag_start_pos = p; drag_start_value = value; - state |= ACTIVE; + set_state(ACTIVE); } void Slider::drag(int p) @@ -60,7 +60,7 @@ void Slider::drag(int p) void Slider::end_drag() { dragging = false; - state &= ~ACTIVE; + clear_state(ACTIVE); } diff --git a/source/toggle.cpp b/source/toggle.cpp index 6d2a10b..f2cf552 100644 --- a/source/toggle.cpp +++ b/source/toggle.cpp @@ -59,12 +59,12 @@ void Toggle::set_value(bool v) value = v; if(value) { - state |= ACTIVE; + set_state(ACTIVE); if(exclusive && parent) exclude_siblings(); } else - state &= ~ACTIVE; + clear_state(ACTIVE); } void Toggle::render_special(const Part &part) const @@ -115,10 +115,7 @@ Toggle &Toggle::Loader::get_object() const void Toggle::Loader::finish() { Toggle &tgl = get_object(); - if(tgl.value) - tgl.state |= ACTIVE; - else - tgl.state &= ~ACTIVE; + tgl.set_state(ACTIVE, (tgl.value ? ACTIVE : NORMAL)); } void Toggle::Loader::text(const string &t) diff --git a/source/widget.cpp b/source/widget.cpp index 6433316..b10b6b5 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -134,6 +134,11 @@ void Widget::set_focus() signal_request_focus.emit(); } +void Widget::set_state(State mask, State bits) +{ + state = (state&~mask)|bits; +} + void Widget::render() const { if(!style) @@ -157,22 +162,22 @@ void Widget::render() const void Widget::pointer_enter() { - state |= HOVER; + set_state(HOVER); } void Widget::pointer_leave() { - state &= ~HOVER; + clear_state(HOVER); } void Widget::focus_in() { - state |= FOCUS; + set_state(FOCUS); } void Widget::focus_out() { - state &= ~FOCUS; + clear_state(FOCUS); } diff --git a/source/widget.h b/source/widget.h index 665ca9d..9bdc2c0 100644 --- a/source/widget.h +++ b/source/widget.h @@ -97,6 +97,12 @@ public: bool is_focusable() const { return focusable; } void set_focus(); +protected: + void set_state(State s) { set_state(s, s); } + void clear_state(State s) { set_state(s, NORMAL); } + void set_state(State, State); + +public: void render() const; protected: virtual void render_special(const Part &) const { } -- 2.43.0