GUI2/Slider Base: formatting cleanup

This commit is contained in:
Charles Dang 2017-10-29 13:54:48 +11:00
parent 5a2162564f
commit 3e16df795e
2 changed files with 122 additions and 129 deletions

View file

@ -27,20 +27,20 @@
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__ #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':' #define LOG_HEADER LOG_SCOPE_HEADER + ':'
namespace { namespace
int rounded_division(int value, int new_base, int old_base) {
{ int rounded_division(int value, int new_base, int old_base)
if (old_base == 0) { {
if(old_base == 0) {
return new_base / 2; return new_base / 2;
} } else {
else {
return ::rounded_division(value * new_base, old_base); return ::rounded_division(value * new_base, old_base);
} }
}
} }
} // end anon namespace
namespace gui2 namespace gui2
{ {
slider_base::slider_base(const implementation::builder_styled_widget& builder, const std::string& control_type) slider_base::slider_base(const implementation::builder_styled_widget& builder, const std::string& control_type)
: styled_widget(builder, control_type) : styled_widget(builder, control_type)
, state_(ENABLED) , state_(ENABLED)
@ -53,16 +53,16 @@ slider_base::slider_base(const implementation::builder_styled_widget& builder, c
, positioner_length_(0) , positioner_length_(0)
, snap_(true) , snap_(true)
{ {
connect_signal<event::MOUSE_ENTER>(std::bind( connect_signal<event::MOUSE_ENTER>(
&slider_base::signal_handler_mouse_enter, this, _2, _3, _4)); std::bind(&slider_base::signal_handler_mouse_enter, this, _2, _3, _4));
connect_signal<event::MOUSE_MOTION>(std::bind( connect_signal<event::MOUSE_MOTION>(
&slider_base::signal_handler_mouse_motion, this, _2, _3, _4, _5)); std::bind(&slider_base::signal_handler_mouse_motion, this, _2, _3, _4, _5));
connect_signal<event::MOUSE_LEAVE>(std::bind( connect_signal<event::MOUSE_LEAVE>(
&slider_base::signal_handler_mouse_leave, this, _2, _3)); std::bind(&slider_base::signal_handler_mouse_leave, this, _2, _3));
connect_signal<event::LEFT_BUTTON_DOWN>(std::bind( connect_signal<event::LEFT_BUTTON_DOWN>(
&slider_base::signal_handler_left_button_down, this, _2, _3)); std::bind(&slider_base::signal_handler_left_button_down, this, _2, _3));
connect_signal<event::LEFT_BUTTON_UP>(std::bind( connect_signal<event::LEFT_BUTTON_UP>(
&slider_base::signal_handler_left_button_up, this, _2, _3)); std::bind(&slider_base::signal_handler_left_button_up, this, _2, _3));
} }
void slider_base::scroll(const scroll_mode scroll) void slider_base::scroll(const scroll_mode scroll)
@ -145,12 +145,11 @@ void slider_base::set_slider_position(int item_position)
void slider_base::update_canvas() void slider_base::update_canvas()
{ {
for(auto& tmp : get_canvases()) {
for(auto & tmp : get_canvases())
{
tmp.set_variable("positioner_offset", wfl::variant(positioner_offset_)); tmp.set_variable("positioner_offset", wfl::variant(positioner_offset_));
tmp.set_variable("positioner_length", wfl::variant(positioner_length_)); tmp.set_variable("positioner_length", wfl::variant(positioner_length_));
} }
set_is_dirty(true); set_is_dirty(true);
} }
@ -169,12 +168,12 @@ void slider_base::recalculate()
if(!get_length()) { if(!get_length()) {
return; return;
} }
assert(available_length() > 0); assert(available_length() > 0);
recalculate_positioner(); recalculate_positioner();
set_slider_position(item_position_); set_slider_position(item_position_);
} }
void slider_base::move_positioner(int new_offset) void slider_base::move_positioner(int new_offset)
@ -200,7 +199,6 @@ void slider_base::update_slider_position(slider_base::slider_position_t& pos)
} }
if(new_position != item_position_) { if(new_position != item_position_) {
item_position_ = new_position; item_position_ = new_position;
child_callback_positioner_moved(); child_callback_positioner_moved();
@ -209,9 +207,7 @@ void slider_base::update_slider_position(slider_base::slider_position_t& pos)
} }
} }
void slider_base::signal_handler_mouse_enter(const event::ui_event event, void slider_base::signal_handler_mouse_enter(const event::ui_event event, bool& handled, bool& halt)
bool& handled,
bool& halt)
{ {
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n"; DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
@ -219,10 +215,8 @@ void slider_base::signal_handler_mouse_enter(const event::ui_event event,
signal_handler_mouse_motion(event, handled, halt, get_mouse_position()); signal_handler_mouse_motion(event, handled, halt, get_mouse_position());
} }
void slider_base::signal_handler_mouse_motion(const event::ui_event event, void slider_base::signal_handler_mouse_motion(
bool& handled, const event::ui_event event, bool& handled, bool& halt, const point& coordinate)
bool& halt,
const point& coordinate)
{ {
DBG_GUI_E << LOG_HEADER << ' ' << event << " at " << coordinate << ".\n"; DBG_GUI_E << LOG_HEADER << ' ' << event << " at " << coordinate << ".\n";
@ -247,6 +241,7 @@ void slider_base::signal_handler_mouse_motion(const event::ui_event event,
if(!on_positioner(mouse)) { if(!on_positioner(mouse)) {
set_state(ENABLED); set_state(ENABLED);
} }
break; break;
case DISABLED: case DISABLED:
@ -258,23 +253,22 @@ void slider_base::signal_handler_mouse_motion(const event::ui_event event,
default: default:
assert(false); assert(false);
} }
handled = true; handled = true;
} }
void slider_base::signal_handler_mouse_leave(const event::ui_event event, void slider_base::signal_handler_mouse_leave(const event::ui_event event, bool& handled)
bool& handled)
{ {
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n"; DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
if(state_ == FOCUSED) { if(state_ == FOCUSED) {
set_state(ENABLED); set_state(ENABLED);
} }
handled = true; handled = true;
} }
void slider_base::signal_handler_left_button_down(const event::ui_event event, bool& handled)
void slider_base::signal_handler_left_button_down(const event::ui_event event,
bool& handled)
{ {
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n"; DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
@ -284,6 +278,7 @@ void slider_base::signal_handler_left_button_down(const event::ui_event event,
if(on_positioner(mouse)) { if(on_positioner(mouse)) {
assert(get_window()); assert(get_window());
drag_initial_mouse_ = mouse; drag_initial_mouse_ = mouse;
drag_initial_position_ = item_position_; drag_initial_position_ = item_position_;
drag_initial_offset_ = positioner_offset_ - offset_before(); drag_initial_offset_ = positioner_offset_ - offset_before();
@ -305,8 +300,7 @@ void slider_base::signal_handler_left_button_down(const event::ui_event event,
handled = true; handled = true;
} }
void slider_base::signal_handler_left_button_up(const event::ui_event event, void slider_base::signal_handler_left_button_up(const event::ui_event event, bool& handled)
bool& handled)
{ {
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n"; DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";

View file

@ -21,7 +21,6 @@
namespace gui2 namespace gui2
{ {
/** /**
* Base class for a scroll bar. * Base class for a scroll bar.
* *
@ -54,21 +53,21 @@ public:
enum scroll_mode { enum scroll_mode {
BEGIN, /**< Go to begin position. */ BEGIN, /**< Go to begin position. */
ITEM_BACKWARDS, /**< Go one item towards the begin. */ ITEM_BACKWARDS, /**< Go one item towards the begin. */
HALF_JUMP_BACKWARDS, /**< Go half the visible items towards the begin. HALF_JUMP_BACKWARDS, /**< Go half the visible items towards the begin. */
*/ JUMP_BACKWARDS, /**< Go the visible items towards the begin. */
JUMP_BACKWARDS, /**< Go the visibile items towards the begin. */
END, /**< Go to the end position. */ END, /**< Go to the end position. */
ITEM_FORWARD, /**< Go one item towards the end. */ ITEM_FORWARD, /**< Go one item towards the end. */
HALF_JUMP_FORWARD, /**< Go half the visible items towards the end. */ HALF_JUMP_FORWARD, /**< Go half the visible items towards the end. */
JUMP_FORWARD JUMP_FORWARD /**< Go the visible items towards the end. */
}; /**< Go the visible items towards the end. */ };
///container for the current position of a slider. /** Helper container for the slider's current position. */
struct slider_position_t struct slider_position_t
{ {
int offset; int offset;
int max_offset; int max_offset;
}; };
/** /**
* Sets the item position. * Sets the item position.
* *
@ -77,6 +76,7 @@ public:
* @param scroll 'step size' to scroll. * @param scroll 'step size' to scroll.
*/ */
void scroll(const scroll_mode scroll); void scroll(const scroll_mode scroll);
protected: protected:
/** Is the positioner at the beginning of the slider? */ /** Is the positioner at the beginning of the slider? */
bool at_begin() const bool at_begin() const
@ -120,13 +120,7 @@ public:
* *
* Note the order of the states must be the same as defined in settings.hpp. * Note the order of the states must be the same as defined in settings.hpp.
*/ */
enum state_t { enum state_t { ENABLED, DISABLED, PRESSED, FOCUSED, COUNT };
ENABLED,
DISABLED,
PRESSED,
FOCUSED,
COUNT
};
protected: protected:
/***** ***** ***** setters / getters for members ***** ****** *****/ /***** ***** ***** setters / getters for members ***** ****** *****/
@ -149,7 +143,7 @@ protected:
/** /**
* Note the position isn't guaranteed to be the wanted position * Note the position isn't guaranteed to be the wanted position
* the step size is honored. The value will be rouded down. * the step size is honored. The value will be rounded down.
*/ */
void set_slider_position(int item_position); void set_slider_position(int item_position);
@ -186,10 +180,14 @@ protected:
{ {
} }
virtual int jump_size() const { return 1; } virtual int jump_size() const
{
return 1;
}
private: private:
void set_state(const state_t state); void set_state(const state_t state);
/** /**
* Current state of the widget. * Current state of the widget.
* *
@ -233,7 +231,8 @@ private:
/** The current length of the positioner. */ /** The current length of the positioner. */
int positioner_length_; int positioner_length_;
/** whether the slider shoudl 'snap' into its supported values or not */
/** Whether the slider should 'snap' into its supported values or not. */
bool snap_; bool snap_;
/***** ***** ***** ***** Pure virtual functions ***** ***** ***** *****/ /***** ***** ***** ***** Pure virtual functions ***** ***** ***** *****/
@ -241,9 +240,15 @@ private:
/** Get the length of the slider. */ /** Get the length of the slider. */
virtual unsigned get_length() const = 0; virtual unsigned get_length() const = 0;
int available_length() const { return get_length() - offset_before() - offset_after(); } int available_length() const
{
return get_length() - offset_before() - offset_after();
}
int max_offset() const { return std::max(0, available_length() - positioner_length()); } int max_offset() const
{
return std::max(0, available_length() - positioner_length());
}
/** /**
* The number of pixels we can't use since they're used for borders. * The number of pixels we can't use since they're used for borders.
@ -290,17 +295,15 @@ private:
* This function is used to determine how much the positioner needs to be * This function is used to determine how much the positioner needs to be
* moved. * moved.
*/ */
virtual int get_length_difference(const point& original, virtual int get_length_difference(const point& original, const point& current) const = 0;
const point& current) const = 0;
/***** ***** ***** ***** Private functions ***** ***** ***** *****/ /***** ***** ***** ***** Private functions ***** ***** ***** *****/
/** /**
* Updates the slider. * Updates the slider.
* *
* Needs to be called when someting changes eg number of items * Needs to be called when someting changes eg number of items or available size.
* or available size. It can only be called once we have a size * It can only be called once we have a size otherwise we can't calculate a thing.
* otherwise we can't calulate a thing.
*/ */
void recalculate(); void recalculate();
@ -311,7 +314,10 @@ private:
*/ */
virtual int positioner_length() const = 0; virtual int positioner_length() const = 0;
void recalculate_positioner() { positioner_length_ = positioner_length(); } void recalculate_positioner()
{
positioner_length_ = positioner_length();
}
/** /**
* Moves the positioner. * Moves the positioner.
@ -322,22 +328,15 @@ private:
/***** ***** ***** signal handlers ***** ****** *****/ /***** ***** ***** signal handlers ***** ****** *****/
void signal_handler_mouse_enter(const event::ui_event event, void signal_handler_mouse_enter(const event::ui_event event, bool& handled, bool& halt);
bool& handled,
bool& halt);
void signal_handler_mouse_motion(const event::ui_event event, void signal_handler_mouse_motion(const event::ui_event event, bool& handled, bool& halt, const point& coordinate);
bool& handled,
bool& halt,
const point& coordinate);
void signal_handler_mouse_leave(const event::ui_event event, bool& handled); void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
void signal_handler_left_button_down(const event::ui_event event, void signal_handler_left_button_down(const event::ui_event event, bool& handled);
bool& handled);
void signal_handler_left_button_up(const event::ui_event event, void signal_handler_left_button_up(const event::ui_event event, bool& handled);
bool& handled);
}; };
} // namespace gui2 } // namespace gui2