Scrollbar mode code cleanup

This commit is contained in:
Charles Dang 2016-09-12 08:52:32 +11:00
parent 905e1a00a3
commit 62f864da02
7 changed files with 75 additions and 68 deletions

View file

@ -27,6 +27,13 @@ namespace gui2
namespace implementation
{
static std::map<std::string, scrollbar_mode> scrollbar_mode_map = {
{ "always", scrollbar_mode::ALWAYS_VISIBLE },
{ "never", scrollbar_mode::ALWAYS_INVISIBLE },
{ "auto", scrollbar_mode::AUTO_VISIBLE },
{ "initial_auto", scrollbar_mode::AUTO_VISIBLE_FIRST_RUN },
};
unsigned get_v_align(const std::string& v_align)
{
if(v_align == "top") {
@ -110,22 +117,20 @@ unsigned read_flags(const config& cfg)
return flags;
}
tscrollbar_container::tscrollbar_mode
get_scrollbar_mode(const std::string& scrollbar_mode)
scrollbar_mode get_scrollbar_mode(const std::string& scrollbar_mode)
{
if(scrollbar_mode == "always") {
return tscrollbar_container::always_visible;
} else if(scrollbar_mode == "never") {
return tscrollbar_container::always_invisible;
} else if(scrollbar_mode == "auto") {
return tscrollbar_container::auto_visible;
} else {
if(!scrollbar_mode.empty() && scrollbar_mode != "initial_auto") {
ERR_GUI_E << "Invalid scrollbar mode '" << scrollbar_mode
<< "' falling back to 'initial_auto'.\n";
}
return tscrollbar_container::auto_visible_first_run;
if(scrollbar_mode.empty()) {
return tscrollbar_container::AUTO_VISIBLE_FIRST_RUN;
}
if(scrollbar_mode_map.find(scrollbar_mode) == scrollbar_mode_map.end()) {
ERR_GUI_E << "Invalid scrollbar mode '" << scrollbar_mode << "'."
<< "Falling back to 'initial_auto'." << std::endl;
return tscrollbar_container::AUTO_VISIBLE_FIRST_RUN;
}
return scrollbar_mode_map[scrollbar_mode];
}
int get_retval(const std::string& retval_id,

View file

@ -28,6 +28,8 @@ namespace gui2
namespace implementation
{
using scrollbar_mode = tscrollbar_container::tscrollbar_mode;
/**
* Returns the vertical alignment.
*
@ -71,8 +73,7 @@ unsigned read_flags(const config& cfg);
*
* @returns The scrollbar mode flags.
*/
tscrollbar_container::tscrollbar_mode
get_scrollbar_mode(const std::string& scrollbar_mode);
scrollbar_mode get_scrollbar_mode(const std::string& scrollbar_mode);
/**
* Returns the return value for a widget.

View file

@ -87,7 +87,7 @@ public:
void bind(twindow& window) {
msg_label = &find_widget<tscroll_label>(&window, "msg", false);
msg_label->set_use_markup(true);
msg_label->set_vertical_scrollbar_mode(tscrollbar_container::always_visible);
msg_label->set_vertical_scrollbar_mode(tscrollbar_container::ALWAYS_VISIBLE);
msg_label->set_label("");
}

View file

@ -68,8 +68,8 @@ const std::map<std::string, tscrollbar_::tscroll>& scroll_lookup()
tscrollbar_container::tscrollbar_container(const unsigned canvas_count)
: tcontainer_(canvas_count)
, state_(ENABLED)
, vertical_scrollbar_mode_(auto_visible_first_run)
, horizontal_scrollbar_mode_(auto_visible_first_run)
, vertical_scrollbar_mode_(AUTO_VISIBLE_FIRST_RUN)
, horizontal_scrollbar_mode_(AUTO_VISIBLE_FIRST_RUN)
, vertical_scrollbar_grid_(nullptr)
, horizontal_scrollbar_grid_(nullptr)
, vertical_scrollbar_(nullptr)
@ -125,12 +125,12 @@ void tscrollbar_container::layout_initialise(const bool full_initialisation)
assert(vertical_scrollbar_grid_);
switch(vertical_scrollbar_mode_) {
case always_visible:
case ALWAYS_VISIBLE:
vertical_scrollbar_grid_->set_visible(
twidget::tvisible::visible);
break;
case auto_visible:
case AUTO_VISIBLE:
vertical_scrollbar_grid_->set_visible(
twidget::tvisible::hidden);
break;
@ -142,12 +142,12 @@ void tscrollbar_container::layout_initialise(const bool full_initialisation)
assert(horizontal_scrollbar_grid_);
switch(horizontal_scrollbar_mode_) {
case always_visible:
case ALWAYS_VISIBLE:
horizontal_scrollbar_grid_->set_visible(
twidget::tvisible::visible);
break;
case auto_visible:
case AUTO_VISIBLE:
horizontal_scrollbar_grid_->set_visible(
twidget::tvisible::hidden);
break;
@ -188,7 +188,7 @@ void tscrollbar_container::request_reduce_height(const unsigned maximum_height)
return;
}
if(vertical_scrollbar_mode_ == always_invisible) {
if(vertical_scrollbar_mode_ == ALWAYS_INVISIBLE) {
DBG_GUI_L << LOG_HEADER << " request failed due to scrollbar mode.\n";
return;
}
@ -253,7 +253,7 @@ void tscrollbar_container::request_reduce_width(const unsigned maximum_width)
return;
}
if(horizontal_scrollbar_mode_ == always_invisible) {
if(horizontal_scrollbar_mode_ == ALWAYS_INVISIBLE) {
DBG_GUI_L << LOG_HEADER << " request failed due to scrollbar mode.\n";
return;
}
@ -276,7 +276,7 @@ void tscrollbar_container::request_reduce_width(const unsigned maximum_width)
}
// If showing the scrollbar increased the width, hide and abort.
if(horizontal_scrollbar_mode_ == auto_visible_first_run && scrollbar_size.x
if(horizontal_scrollbar_mode_ == AUTO_VISIBLE_FIRST_RUN && scrollbar_size.x
> size.x) {
horizontal_scrollbar_grid_->set_visible(twidget::tvisible::invisible);
@ -356,7 +356,7 @@ set_scrollbar_mode(tgrid* scrollbar_grid,
{
assert(scrollbar_grid && scrollbar);
if(scrollbar_mode == tscrollbar_container::always_invisible) {
if(scrollbar_mode == tscrollbar_container::ALWAYS_INVISIBLE) {
scrollbar_grid->set_visible(twidget::tvisible::invisible);
return;
}
@ -365,7 +365,7 @@ set_scrollbar_mode(tgrid* scrollbar_grid,
scrollbar->set_item_position(0);
scrollbar->set_visible_items(visible_items);
if(scrollbar_mode == tscrollbar_container::auto_visible) {
if(scrollbar_mode == tscrollbar_container::AUTO_VISIBLE) {
const bool scrollbar_needed = items > visible_items;
@ -404,7 +404,7 @@ adjust_scrollbar_mode(tgrid* scrollbar_grid,
//Casts insertion_pos to an unsigned so negative values are interpreted as 'at end'
const bool inserted_before_visible_area = is_inserted_before(static_cast<unsigned>(insertion_pos), items_before, previous_item_position, visible_items);
if(scrollbar_mode == tscrollbar_container::always_invisible) {
if(scrollbar_mode == tscrollbar_container::ALWAYS_INVISIBLE) {
scrollbar_grid->set_visible(twidget::tvisible::invisible);
return;
}
@ -414,7 +414,7 @@ adjust_scrollbar_mode(tgrid* scrollbar_grid,
//scrollbar->set_item_position(0);
scrollbar->set_visible_items(visible_items);
if(scrollbar_mode == tscrollbar_container::auto_visible) {
if(scrollbar_mode == tscrollbar_container::AUTO_VISIBLE) {
const bool scrollbar_needed = items_after > visible_items;
@ -540,7 +540,7 @@ bool tscrollbar_container::disable_click_dismiss() const
bool tscrollbar_container::content_resize_request(const bool force_sizing)
{
/**
* @todo Try to handle auto_visible_first_run here as well.
* @todo Try to handle AUTO_VISIBLE_FIRST_RUN here as well.
*
* Handling it here makes the code a bit more complex but allows to not
* reserve space for scrollbars, which will look nicer in the MP lobby.
@ -576,8 +576,8 @@ bool tscrollbar_container::content_resize_request(const bool force_sizing)
if(best_size.x > size.x) {
DBG_GUI_L << LOG_HEADER << " content too wide.\n";
if(horizontal_scrollbar_mode_ == always_invisible
|| (horizontal_scrollbar_mode_ == auto_visible_first_run
if(horizontal_scrollbar_mode_ == ALWAYS_INVISIBLE
|| (horizontal_scrollbar_mode_ == AUTO_VISIBLE_FIRST_RUN
&& horizontal_scrollbar_grid_->get_visible()
== twidget::tvisible::invisible)) {
@ -592,8 +592,8 @@ bool tscrollbar_container::content_resize_request(const bool force_sizing)
if(best_size.y > size.y) {
DBG_GUI_L << LOG_HEADER << " content too high.\n";
if(vertical_scrollbar_mode_ == always_invisible
|| (vertical_scrollbar_mode_ == auto_visible_first_run
if(vertical_scrollbar_mode_ == ALWAYS_INVISIBLE
|| (vertical_scrollbar_mode_ == AUTO_VISIBLE_FIRST_RUN
&& vertical_scrollbar_grid_->get_visible()
== twidget::tvisible::invisible)) {
@ -676,8 +676,8 @@ bool tscrollbar_container::content_resize_width(const int width_modification, co
}
assert(horizontal_scrollbar_ && horizontal_scrollbar_grid_);
if(horizontal_scrollbar_mode_ == always_invisible
|| (horizontal_scrollbar_mode_ == auto_visible_first_run
if(horizontal_scrollbar_mode_ == ALWAYS_INVISIBLE
|| (horizontal_scrollbar_mode_ == AUTO_VISIBLE_FIRST_RUN
&& horizontal_scrollbar_grid_->get_visible()
== twidget::tvisible::invisible)) {
@ -726,8 +726,8 @@ bool tscrollbar_container::content_resize_height(const int height_modification,
}
assert(vertical_scrollbar_ && vertical_scrollbar_grid_);
if(vertical_scrollbar_mode_ == always_invisible
|| (vertical_scrollbar_mode_ == auto_visible_first_run
if(vertical_scrollbar_mode_ == ALWAYS_INVISIBLE
|| (vertical_scrollbar_mode_ == AUTO_VISIBLE_FIRST_RUN
&& vertical_scrollbar_grid_->get_visible()
== twidget::tvisible::invisible)) {
@ -1117,12 +1117,12 @@ void tscrollbar_container::scrollbar_moved()
assert(vertical_scrollbar_ && horizontal_scrollbar_);
/*** Update the content location. ***/
const int x_offset = horizontal_scrollbar_mode_ == always_invisible
const int x_offset = horizontal_scrollbar_mode_ == ALWAYS_INVISIBLE
? 0
: horizontal_scrollbar_->get_item_position()
* horizontal_scrollbar_->get_step_size();
const int y_offset = vertical_scrollbar_mode_ == always_invisible
const int y_offset = vertical_scrollbar_mode_ == ALWAYS_INVISIBLE
? 0
: vertical_scrollbar_->get_item_position()
* vertical_scrollbar_->get_step_size();

View file

@ -60,29 +60,30 @@ public:
/** The way to handle the showing or hiding of the scrollbar. */
enum tscrollbar_mode {
always_visible, /**<
* The scrollbar is always shown, whether
* needed or not.
*/
always_invisible, /**<
* The scrollbar is never shown even not
* when needed. There's also no space
* reserved for the scrollbar.
*/
auto_visible, /**<
* The scrollbar is shown when the number of
* items is larger as the visible items. The
* space for the scrollbar is always
* reserved, just in case it's needed after
* the initial sizing (due to adding items).
*/
auto_visible_first_run /**<
* Like auto_visible, but when not needed
* upon the initial layout phase, the bars
* are not shown and no space is reserved
* for them. (The algorithm hides them by
* default.
*/
/**
* The scrollbar is always shown, whether needed or not.
*/
ALWAYS_VISIBLE,
/**
* The scrollbar is never shown even notwhen needed. There's also no space
* reserved for the scrollbar.
*/
ALWAYS_INVISIBLE,
/**
* The scrollbar is shown when the number of items is larger as the visible items.
* The space for the scrollbar is always reserved, just in case it's needed after
* the initial sizing (due to adding items).
*/
AUTO_VISIBLE,
/**
* Like AUTO_VISIBLE, but when not needed upon the initial layout phase, the bars
* are not shown and no space is reserved for them. (The algorithm hides them by
* default.
*/
AUTO_VISIBLE_FIRST_RUN,
};
/***** ***** ***** ***** layout functions ***** ***** ***** *****/
@ -303,11 +304,11 @@ protected:
* * positive values increase height.
* @param width_modification_pos
* The position where the additional content was
* inserted/removed, defaults to -1 whcih means
* inserted/removed, defaults to -1 whcih means
* 'at end'
* @param height_modification_po
* The position where the additional content was
* inserted/removed, defaults to -1 whcih means
* inserted/removed, defaults to -1 whcih means
* 'at end'
*
* @returns True is wanted modification is accepted false

View file

@ -245,7 +245,7 @@ public:
*
* @param full_initialisation For widgets with scrollbars it hides them
* unless the mode is
* @ref tscrollbar_mode::always_visible. For
* @ref tscrollbar_mode::ALWAYS_VISIBLE. For
* other widgets this flag is a @em NOP.
*/
virtual void layout_initialise(const bool full_initialisation);

View file

@ -1581,7 +1581,7 @@ twindow_definition::tresolution::tresolution(const config& cfg)
* - Clear the internal best size cache for all widgets.
* - For widgets with scrollbars hide them unless the
* @ref gui2::tscrollbar_container::tscrollbar_mode "scrollbar_mode" is
* always_visible or auto_visible.
* ALWAYS_VISIBLE or AUTO_VISIBLE.
* - Handle shared sizes:
* - Height and width:
* - Get the best size for all widgets that share height and width.