16 #define GETTEXT_DOMAIN "wesnoth-lib" 38 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__ 39 #define LOG_HEADER LOG_SCOPE_HEADER + ':' 55 , list_builder_(list_builder)
58 , callback_order_change_()
94 int height_reduced = 0;
95 int width_reduced = 0;
102 for(; count; --count) {
114 if((height_reduced != 0 || width_reduced != 0) &&
get_item_count() != 0) {
115 resize_content(-width_reduced, -height_reduced, row_pos_x, row_pos_y);
148 bool resize_needed =
false;
189 bool resize_needed =
false;
195 for(std::size_t
i = 0;
i < shown.size(); ++
i) {
249 throw std::invalid_argument(
"invalid listbox index");
288 if(checkbox !=
nullptr) {
308 if(selected_item == -1) {
349 std::optional<unsigned> vertical_scrollbar_position, horizontal_scrollbar_position;
361 if(vertical_scrollbar_position && horizontal_scrollbar_position) {
366 }
else if(selected_item != -1) {
380 const int height_modification,
381 const int width_modification_pos,
382 const int height_modification_pos)
385 << width_modification <<
" height_modification " << height_modification <<
".\n";
388 width_modification, height_modification, width_modification_pos, height_modification_pos))
392 size.
x += width_modification;
393 size.
y += height_modification;
402 if(width_modification < 0 || height_modification < 0) {
424 if(size.
x < content.
x) {
444 std::vector<widget*> child_call_stack =
call_stack;
453 if(
const grid* header = find_widget<const grid>(&
get_grid(),
"_header_grid",
false,
false)) {
454 result.
y += header->get_best_size().y;
457 if(
const grid* footer = find_widget<const grid>(&
get_grid(),
"_footer_grid",
false,
false)) {
458 result.
y += footer->get_best_size().y;
544 const std::vector<std::map<std::string, string_map>>& list_data)
553 grid&
p = find_widget<grid>(
this,
"_header_grid",
false);
555 for(
unsigned i = 0, max = std::max(p.get_cols(), p.get_rows());
i < max; ++
i) {
563 if(
toggle_button* selectable = find_widget<toggle_button>(&p,
"sort_" + std::to_string(
i),
false,
false)) {
595 if(pair.first !=
nullptr && pair.first != &selectable) {
596 pair.first->
set_value(static_cast<unsigned int>(sort_order::type::none));
602 if(static_cast<unsigned int>(order) > orders_[column].second.size()) {
606 if(order == sort_order::type::none) {
609 order_by(orders_[column].second[static_cast<unsigned int>(order) - 1]);
645 grid& header_grid = find_widget<grid>(
this,
"_header_grid",
false);
647 selectable_item&
w = find_widget<selectable_item>(&header_grid,
"sort_" + std::to_string(sort_by.first),
false);
652 w.set_value(static_cast<int>(sort_by.second));
663 for(
unsigned int column = 0; column <
orders_.size(); ++column) {
667 if(w && sort != sort_order::type::none) {
668 return std::pair(column, sort);
672 return std::pair(-1, sort_order::type::none);
678 if(pair.first !=
nullptr) {
679 pair.first->set_value(static_cast<unsigned int>(sort_order::type::none));
690 const point s(size.
x, size.
y < best_height ? size.
y : best_height);
716 DBG_GUI_P <<
"Parsing listbox " <<
id <<
'\n';
718 load_resolutions<resolution>(cfg);
726 state.emplace_back(cfg.
child(
"state_enabled"));
727 state.emplace_back(cfg.
child(
"state_disabled"));
732 grid = std::make_shared<builder_grid>(child);
737 static std::vector<std::map<std::string, string_map>>
parse_list_data(
const config& data,
const unsigned int req_cols)
739 std::vector<std::map<std::string, string_map>> list_data;
742 auto cols = row.child_range(
"column");
744 VALIDATE(static_cast<unsigned>(cols.size()) == req_cols,
745 _(
"'list_data' must have the same number of columns as the 'list_definition'.")
748 for(
const auto&
c : cols) {
749 list_data.emplace_back();
751 for(
const auto&
i :
c.attribute_range()) {
752 list_data.back()[
""][
i.first] =
i.second;
755 for(
const auto&
w :
c.child_range(
"widget")) {
758 for(
const auto&
i :
w.attribute_range()) {
759 list_data.back()[
w[
"id"]][
i.first] =
i.second;
768 builder_listbox::builder_listbox(
const config& cfg)
774 , list_builder(nullptr)
776 , has_minimum_(cfg[
"has_minimum"].to_bool(true))
777 , has_maximum_(cfg[
"has_maximum"].to_bool(true))
780 header = std::make_shared<builder_grid>(
h);
784 footer = std::make_shared<builder_grid>(
f);
808 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.\n";
813 widget->init_grid(*conf->grid);
851 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.\n";
856 widget->init_grid(*conf->grid);
894 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.\n";
899 widget->init_grid(*conf->grid);
const order_pair get_active_sorting_option()
Define the common log macros for the gui toolkit.
virtual void handle_key_left_arrow(SDL_Keymod modifier, bool &handled)=0
Left arrow key pressed.
Base class of a resolution, contains the common keys for a resolution.
void keyboard_capture(widget *widget)
virtual void handle_key_up_arrow(SDL_Keymod modifier, bool &handled)=0
Up arrow key pressed.
virtual unsigned get_item_at_ordered(unsigned index_ordered) const =0
If a sort-order is being applied, maps from sorted to unsorted indicies.
void set_active_sorting_option(const order_pair &sort_by, const bool select_first=false)
Sorts the listbox by a pre-set sorting option.
Defines the exception classes for the layout algorithm.
Small abstract helper class.
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See widget::set_visible_rectangle.
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Helper class to block invalidate_layout.
std::vector< state_definition > state
virtual point calculate_best_size() const override
See widget::calculate_best_size.
virtual unsigned get_item_count() const =0
Returns the number of items.
virtual void handle_key_right_arrow(SDL_Keymod modifier, bool &handled)=0
Right arrow key pressed.
void update_visible_area_on_key_event(const KEY_SCROLL_DIRECTION direction)
Helper to update visible area after a key event.
virtual void set_item_shown(const unsigned index, const bool show)=0
Shows or hides an item.
void mark_as_unsorted()
Deactivates all sorting toggle buttons at the top, making the list look like it's not sorted...
virtual std::unique_ptr< widget > build() const override
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
const grid & get_grid() const
virtual void place(const point &origin, const point &size) override=0
See widget::place.
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
bool get_value_bool() const
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
virtual void set_order(const order_func &order)=0
virtual bool is_selected(const unsigned index) const =0
Returns whether the item is selected.
std::pair< int, sort_order::type > order_pair
virtual bool has_widget(const widget &widget) const override
See widget::has_widget.
child_itors child_range(config_key_type key)
std::function< bool(unsigned, unsigned)> order_func
void finalize(std::unique_ptr< generator_base > generator, builder_grid_const_ptr header, builder_grid_const_ptr footer, const std::vector< std::map< std::string, string_map >> &list_data)
Finishes the building initialization of the widget.
std::function< void(unsigned, sort_order::type)> callback_order_change_
void register_translatable_sorting_option(const int col, translatable_sorter_func_t f)
Registers a special sorting function specifically for translatable values.
virtual grid & item(const unsigned index)=0
Gets the grid of an item.
bool row_selected(const unsigned row)
Check if a row is selected.
virtual unsigned get_selected_item_count() const =0
Returns the number of selected items.
int get_selected_row() const
Returns the first selected row.
void handle_key_down_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
static std::string _(const char *str)
void set_row_active(const unsigned row, const bool active)
Makes a row active or inactive.
bool select_row(const unsigned row, const bool select=true)
Selects a row.
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.
resolution(const config &cfg)
bool select_row_at(const unsigned row, const bool select=true)
Selects a row at the given position, regardless of sorting order.
virtual point calculate_best_size() const override=0
See widget::calculate_best_size.
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Abstract base class for the generator.
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
listbox_definition(const config &cfg)
std::vector< std::map< std::string, string_map > > list_data
Listbox data.
virtual void set_content_size(const point &origin, const point &size) override
Inherited from scrollbar_container.
This file contains the settings handling of the widget library.
void clear()
Removes all the rows in the listbox, clearing it.
builder_grid_const_ptr list_builder_
Contains the builder for the new items.
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
void toggle_item(const unsigned index)
Toggles the selection state of an item.
static thread_local std::deque< std::string > call_stack
For printing error messages when WFL parsing or evaluation fails, this contains the names of the WFL ...
builder_grid_ptr list_builder
unsigned get_item_count() const
Returns the number of items in the listbox.
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.
Basic template class to generate new items.
bool any_rows_shown() const
void order_by_column(unsigned column, widget &widget)
builder_horizontal_listbox(const config &cfg)
bool update_content_size()
Request to update the size of the content after changing the content.
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.
builder_grid_listbox(const config &cfg)
virtual bool get_item_shown(const unsigned index) const =0
Returns whether the item is shown.
void list_item_clicked(widget &caller)
Function to call after the user clicked on a row.
virtual void place(const point &origin, const point &size) override
See widget::place.
builder_grid_ptr list_builder
void order_by(const generator_base::order_func &func)
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
std::vector< std::map< std::string, string_map > > list_data
Listbox data.
virtual int get_selected_item() const =0
Returns the selected item.
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
virtual void clear()=0
Deletes all items.
virtual grid & create_item(const int index, const builder_grid &list_builder, const string_map &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
static map_location::DIRECTION s
std::shared_ptr< const builder_grid > builder_grid_const_ptr
std::array< generator_base::order_func, 2 > generator_sort_array
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
void set_active(const bool active)
Activates all children.
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.
virtual void set_value(unsigned value, bool fire_event=false)=0
Select the styled_widget.
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
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.
boost::dynamic_bitset get_rows_shown() const
Returns a list of visible rows.
virtual void layout_children() override
See widget::layout_children.
std::shared_ptr< builder_grid > builder_grid_ptr
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
std::vector< std::map< std::string, string_map > > list_data
Listbox data.
Contains the SDL_Rect helper code.
virtual void delete_item(const unsigned index)=0
Deletes an item.
static std::optional< typename T::type > get_enum(const std::string value)
Convert a string into its enum equivalent.
static std::vector< std::map< std::string, string_map > > parse_list_data(const config &data, const unsigned int req_cols)
int icompare(const std::string &s1, const std::string &s2)
Case-insensitive lexicographical comparison.
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.
void remove_row(const unsigned row, unsigned count=1)
Removes a row in the listbox.
const bool is_horizontal_
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
void set_column_order(unsigned col, const generator_sort_array &func)
void invalidate_layout()
Updates the size of the window.
virtual std::unique_ptr< widget > build() const override
void handle_key_up_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
builder_grid_ptr list_builder
generator_base * generator_
Contains a pointer to the generator.
A config object defines a single node in a WML file, with access to child nodes.
virtual std::unique_ptr< widget > build() const override
std::function< std::string(const int)> translatable_sorter_func_t
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
base class of top level items, the only item which needs to store the final canvases to draw on...
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
virtual void place(const point &origin, const point &size) override
See widget::place.
Contains the implementation details for lexical_cast and shouldn't be used directly.
boost::dynamic_bitset get_items_shown() const
Returns the visibility of all the items as a bit set.
virtual unsigned get_value() const =0
Is the styled_widget selected?
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
std::pair< std::string, unsigned > item