16 #define GETTEXT_DOMAIN "wesnoth-lib"
36 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
37 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
53 , list_builder_(list_builder)
55 , callback_order_change_()
91 int height_reduced = 0;
92 int width_reduced = 0;
99 for(; count; --count) {
111 if((height_reduced != 0 || width_reduced != 0) &&
get_item_count() != 0) {
112 resize_content(-width_reduced, -height_reduced, row_pos_x, row_pos_y);
145 bool resize_needed =
false;
186 bool resize_needed =
false;
192 for(std::size_t
i = 0;
i < shown.size(); ++
i) {
246 throw std::invalid_argument(
"invalid listbox index");
285 if(checkbox !=
nullptr) {
305 if(selected_item == -1) {
346 utils::optional<unsigned> vertical_scrollbar_position, horizontal_scrollbar_position;
358 if(vertical_scrollbar_position && horizontal_scrollbar_position) {
363 }
else if(selected_item != -1) {
377 const int height_modification,
378 const int width_modification_pos,
379 const int height_modification_pos)
382 << width_modification <<
" height_modification " << height_modification <<
".";
385 width_modification, height_modification, width_modification_pos, height_modification_pos))
389 size.x += width_modification;
390 size.y += height_modification;
397 if(width_modification < 0 || height_modification < 0) {
419 if(
size.x < content.x) {
433 if(
const grid* header = find_widget<const grid>(&
get_grid(),
"_header_grid",
false,
false)) {
434 result.y += header->get_best_size().y;
437 if(
const grid* footer = find_widget<const grid>(&
get_grid(),
"_footer_grid",
false,
false)) {
438 result.y += footer->get_best_size().y;
524 const std::vector<widget_data>& list_data)
533 grid&
p = find_widget<grid>(
this,
"_header_grid",
false);
535 for(
unsigned i = 0, max = std::max(
p.get_cols(),
p.get_rows());
i < max; ++
i) {
543 if(
toggle_button* selectable = find_widget<toggle_button>(&
p,
"sort_" + std::to_string(
i),
false,
false)) {
575 if(pair.first !=
nullptr && pair.first != &selectable) {
576 pair.first->
set_value(
static_cast<unsigned int>(sort_order::type::none));
582 if(
static_cast<unsigned int>(order) >
orders_[column].second.size()) {
586 if(order == sort_order::type::none) {
589 order_by(
orders_[column].second[
static_cast<unsigned int>(order) - 1]);
624 grid& header_grid = find_widget<grid>(
this,
"_header_grid",
false);
626 selectable_item&
w = find_widget<selectable_item>(&header_grid,
"sort_" + std::to_string(sort_by.first),
false);
631 w.set_value(
static_cast<int>(sort_by.second));
642 for(
unsigned int column = 0; column <
orders_.size(); ++column) {
647 if(sort != sort_order::type::none) {
648 return std::pair(column, sort);
652 return std::pair(-1, sort_order::type::none);
658 if(pair.first !=
nullptr) {
659 pair.first->set_value(
static_cast<unsigned int>(sort_order::type::none));
700 load_resolutions<resolution>(cfg);
712 grid = std::make_shared<builder_grid>(child);
719 std::vector<widget_data> list_data;
721 for(
const auto& row :
data.child_range(
"row")) {
722 auto cols = row.child_range(
"column");
724 VALIDATE(
static_cast<unsigned>(cols.size()) == req_cols,
725 _(
"'list_data' must have the same number of columns as the 'list_definition'.")
728 for(
const auto&
c : cols) {
729 list_data.emplace_back();
731 for(
const auto&
i :
c.attribute_range()) {
732 list_data.back()[
""][
i.first] =
i.second;
735 for(
const auto&
w :
c.child_range(
"widget")) {
738 for(
const auto&
i :
w.attribute_range()) {
739 list_data.back()[
w[
"id"]][
i.first] =
i.second;
748 builder_listbox::builder_listbox(
const config& cfg)
754 , list_builder(nullptr)
756 , has_minimum_(cfg[
"has_minimum"].to_bool(true))
757 , has_maximum_(cfg[
"has_maximum"].to_bool(true))
758 , allow_selection_(cfg[
"allow_selection"].to_bool(true))
761 header = std::make_shared<builder_grid>(*
h);
765 footer = std::make_shared<builder_grid>(*
f);
789 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.";
794 widget->init_grid(*conf->grid);
806 , list_builder(nullptr)
808 , has_minimum_(cfg[
"has_minimum"].to_bool(true))
809 , has_maximum_(cfg[
"has_maximum"].to_bool(true))
832 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.";
837 widget->init_grid(*conf->grid);
849 , list_builder(nullptr)
851 , has_minimum_(cfg[
"has_minimum"].to_bool(true))
852 , has_maximum_(cfg[
"has_maximum"].to_bool(true))
875 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.";
880 widget->init_grid(*conf->grid);
A config object defines a single node in a WML file, with access to child nodes.
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
const grid & get_grid() const
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Abstract base class for the generator.
std::function< bool(unsigned, unsigned)> order_func
virtual void set_order(const order_func &order)=0
virtual void handle_key_left_arrow(SDL_Keymod modifier, bool &handled)=0
Left arrow key pressed.
virtual unsigned get_selected_item_count() const =0
Returns the number of selected items.
virtual void handle_key_right_arrow(SDL_Keymod modifier, bool &handled)=0
Right arrow key pressed.
virtual void set_item_shown(const unsigned index, const bool show)=0
Shows or hides an item.
virtual void delete_item(const unsigned index)=0
Deletes an item.
boost::dynamic_bitset get_items_shown() const
Returns the visibility of all the items as a bit set.
virtual void handle_key_up_arrow(SDL_Keymod modifier, bool &handled)=0
Up arrow key pressed.
virtual void place(const point &origin, const point &size) override=0
See widget::place.
virtual grid & create_item(const int index, const builder_grid &list_builder, const widget_item &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
virtual point calculate_best_size() const override=0
See widget::calculate_best_size.
virtual void handle_key_down_arrow(SDL_Keymod modifier, bool &handled)=0
Down arrow key pressed.
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
virtual grid & item(const unsigned index)=0
Gets the grid of an item.
virtual unsigned get_item_count() const =0
Returns the number of items.
static std::unique_ptr< generator_base > build(const bool has_minimum, const bool has_maximum, const placement placement, const bool select)
Create a new generator.
void toggle_item(const unsigned index)
Toggles the selection state of an item.
virtual bool is_selected(const unsigned index) const =0
Returns whether the item is selected.
virtual bool get_item_shown(const unsigned index) const =0
Returns whether the item is shown.
virtual unsigned get_item_at_ordered(unsigned index_ordered) const =0
If a sort-order is being applied, maps from sorted to unsorted indicies.
virtual void clear()=0
Deletes all items.
virtual int get_selected_item() const =0
Returns the selected item.
Basic template class to generate new items.
virtual void create_items(const int index, const builder_grid &list_builder, const std::vector< widget_data > &data, const std::function< void(widget &)> &callback) override
Inherited from generator_base.
void set_active(const bool active)
Activates all children.
virtual bool has_widget(const widget &widget) const override
See widget::has_widget.
virtual void place(const point &origin, const point &size) override
See widget::place.
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See widget::set_visible_rectangle.
void update_layout()
Updates internal layout.
const bool is_horizontal_
void list_item_clicked(widget &caller)
Function to call after the user clicked on a row.
void mark_as_unsorted()
Deactivates all sorting toggle buttons at the top, making the list look like it's not sorted.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
void set_row_active(const unsigned row, const bool active)
Makes a row active or inactive.
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
void set_active_sorting_option(const order_pair &sort_by, const bool select_first=false)
Sorts the listbox by a pre-set sorting option.
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
void order_by_column(unsigned column, widget &widget)
std::pair< int, sort_order::type > order_pair
void resize_content(const int width_modification, const int height_modification, const int width_modification_pos=-1, const int height_modification_pos=-1)
Resizes the content.
void order_by(const generator_base::order_func &func)
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
std::function< std::string(const int)> translatable_sorter_func_t
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
void register_translatable_sorting_option(const int col, translatable_sorter_func_t f)
Registers a special sorting function specifically for translatable values.
virtual void set_content_size(const point &origin, const point &size) override
Inherited from scrollbar_container.
virtual void place(const point &origin, const point &size) override
See widget::place.
void update_visible_area_on_key_event(const KEY_SCROLL_DIRECTION direction)
Helper to update visible area after a key event.
generator_base * generator_
Contains a pointer to the generator.
std::function< void(unsigned, sort_order::type)> callback_order_change_
bool select_row(const unsigned row, const bool select=true)
Selects a row.
bool update_content_size()
Request to update the size of the content after changing the content.
boost::dynamic_bitset get_rows_shown() const
Returns a list of visible rows.
bool any_rows_shown() const
void finalize(std::unique_ptr< generator_base > generator, builder_grid_const_ptr header, builder_grid_const_ptr footer, const std::vector< widget_data > &list_data)
Finishes the building initialization of the widget.
void handle_key_down_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
void remove_row(const unsigned row, unsigned count=1)
Removes a row in the listbox.
void clear()
Removes all the rows in the listbox, clearing it.
void set_column_order(unsigned col, const generator_sort_array &func)
int get_selected_row() const
Returns the first selected row.
unsigned get_item_count() const
Returns the number of items in the listbox.
bool select_row_at(const unsigned row, const bool select=true)
Selects a row at the given position, regardless of sorting order.
const order_pair get_active_sorting_option()
builder_grid_const_ptr list_builder_
Contains the builder for the new items.
bool row_selected(const unsigned row)
Check if a row is selected.
void handle_key_up_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Small abstract helper class.
virtual void set_value(unsigned value, bool fire_event=false)=0
Select the styled_widget.
virtual unsigned get_value() const =0
Is the styled_widget selected?
bool get_value_bool() const
Helper class to block invalidate_layout.
base class of top level items, the only item which needs to store the final canvases to draw on.
void keyboard_capture(widget *widget)
void invalidate_layout()
Updates the size of the window.
static std::string _(const char *str)
Define the common log macros for the gui toolkit.
void point(int x, int y)
Draw a single point.
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
static std::vector< widget_data > parse_list_data(const config &data, const unsigned int req_cols)
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
std::shared_ptr< builder_grid > builder_grid_ptr
std::array< generator_base::order_func, 2 > generator_sort_array
std::map< std::string, widget_item > widget_data
std::map< std::string, t_string > widget_item
void swap_grid(grid *g, grid *content_grid, std::unique_ptr< widget > widget, const std::string &id)
Swaps an item in a grid for another one.
std::shared_ptr< const builder_grid > builder_grid_const_ptr
std::pair< std::string, unsigned > item
Contains the implementation details for lexical_cast and shouldn't be used directly.
int icompare(const std::string &s1, const std::string &s2)
Case-insensitive lexicographical comparison.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Contains the SDL_Rect helper code.
builder_grid_listbox(const config &cfg)
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
virtual std::unique_ptr< widget > build() const override
std::vector< widget_data > list_data
Listbox data.
builder_grid_ptr list_builder
virtual std::unique_ptr< widget > build() const override
std::vector< widget_data > list_data
Listbox data.
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
builder_horizontal_listbox(const config &cfg)
builder_grid_ptr list_builder
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
std::vector< widget_data > list_data
Listbox data.
virtual std::unique_ptr< widget > build() const override
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
builder_grid_ptr list_builder
resolution(const config &cfg)
listbox_definition(const config &cfg)
std::vector< state_definition > state
An abstract description of a rectangle with integer coordinates.
bool overlaps(const SDL_Rect &r) const
Whether the given rectangle and this rectangle overlap.
static constexpr utils::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
static map_location::DIRECTION s
std::string missing_mandatory_wml_tag(const std::string §ion, const std::string &tag)
Returns a standard message for a missing wml child (tag).
std::string missing_mandatory_wml_key(const std::string §ion, const std::string &key, const std::string &primary_key, const std::string &primary_value)
Returns a standard message for a missing wml key (attribute).
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)
#define VALIDATE(cond, message)
The macro to use for the validation of WML.