15 #define GETTEXT_DOMAIN "wesnoth-lib" 38 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__ 39 #define LOG_HEADER LOG_SCOPE_HEADER + ':' 54 const
bool has_minimum,
55 const
bool has_maximum,
60 , list_builder_(list_builder)
63 , callback_order_change_()
99 int height_reduced = 0;
100 int width_reduced = 0;
107 for(; count; --count) {
119 if((height_reduced != 0 || width_reduced != 0) &&
get_item_count() != 0) {
120 resize_content(-width_reduced, -height_reduced, row_pos_x, row_pos_y);
153 bool resize_needed =
false;
194 bool resize_needed =
false;
200 for(std::size_t
i = 0;
i < shown.size(); ++
i) {
254 throw std::invalid_argument(
"invalid listbox index");
293 if(checkbox !=
nullptr) {
313 if(selected_item == -1) {
354 std::optional<unsigned> vertical_scrollbar_position, horizontal_scrollbar_position;
366 if(vertical_scrollbar_position && horizontal_scrollbar_position) {
371 }
else if(selected_item != -1) {
385 const int height_modification,
386 const int width_modification_pos,
387 const int height_modification_pos)
390 << width_modification <<
" height_modification " << height_modification <<
".\n";
393 width_modification, height_modification, width_modification_pos, height_modification_pos))
397 size.
x += width_modification;
398 size.
y += height_modification;
407 if(width_modification < 0 || height_modification < 0) {
429 if(size.
x < content.
x) {
449 std::vector<widget*> child_call_stack =
call_stack;
458 if(
const grid* header = find_widget<const grid>(&
get_grid(),
"_header_grid",
false,
false)) {
459 result.
y += header->get_best_size().y;
462 if(
const grid* footer = find_widget<const grid>(&
get_grid(),
"_footer_grid",
false,
false)) {
463 result.
y += footer->get_best_size().y;
548 const std::vector<std::map<std::string, string_map>>& list_data)
559 grid&
p = find_widget<grid>(
this,
"_header_grid",
false);
561 for(
unsigned i = 0, max = std::max(p.get_cols(), p.get_rows());
i < max; ++
i) {
569 if(
toggle_button* selectable = find_widget<toggle_button>(&p,
"sort_" + std::to_string(
i),
false,
false)) {
597 if(pair.first !=
nullptr && pair.first != &selectable) {
604 if(static_cast<unsigned int>(order.v) > orders_[column].second.size()) {
611 order_by(orders_[column].second[order.v - 1]);
647 grid& header_grid = find_widget<grid>(
this,
"_header_grid",
false);
649 selectable_item&
w = find_widget<selectable_item>(&header_grid,
"sort_" + std::to_string(sort_by.first),
false);
654 w.set_value(static_cast<int>(sort_by.second.v));
665 for(
unsigned int column = 0; column <
orders_.size(); ++column) {
669 return std::make_pair(column, static_cast<SORT_ORDER::type>(w->
get_value()));
679 if(pair.first !=
nullptr) {
691 const point s(size.
x, size.
y < best_height ? size.
y : best_height);
717 DBG_GUI_P <<
"Parsing listbox " <<
id <<
'\n';
719 load_resolutions<resolution>(cfg);
727 state.emplace_back(cfg.
child(
"state_enabled"));
728 state.emplace_back(cfg.
child(
"state_disabled"));
733 grid = std::make_shared<builder_grid>(child);
738 static std::vector<std::map<std::string, string_map>>
parse_list_data(
const config& data,
const unsigned int req_cols)
740 std::vector<std::map<std::string, string_map>> list_data;
743 auto cols = row.child_range(
"column");
745 VALIDATE(static_cast<unsigned>(cols.size()) == req_cols,
746 _(
"'list_data' must have the same number of columns as the 'list_definition'.")
749 for(
const auto&
c : cols) {
750 list_data.emplace_back();
752 for(
const auto&
i :
c.attribute_range()) {
753 list_data.back()[
""][
i.first] =
i.second;
756 for(
const auto&
w :
c.child_range(
"widget")) {
759 for(
const auto&
i :
w.attribute_range()) {
760 list_data.back()[
w[
"id"]][
i.first] =
i.second;
769 builder_listbox::builder_listbox(
const config& cfg)
775 , list_builder(nullptr)
777 , has_minimum_(cfg[
"has_minimum"].to_bool(true))
778 , has_maximum_(cfg[
"has_maximum"].to_bool(true))
781 header = std::make_shared<builder_grid>(
h);
785 footer = std::make_shared<builder_grid>(
f);
809 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.\n";
851 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.\n";
893 DBG_GUI_G <<
"Window builder: placed listbox '" <<
id <<
"' with definition '" <<
definition <<
"'.\n";
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 widget * build() const override
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...
std::pair< int, preferences::SORT_ORDER > order_pair
preferences::SORT_ORDER SORT_ORDER
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.
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 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.
void swap_grid(grid *g, grid *content_grid, widget *widget, const std::string &id)
Swaps an item in a grid for another one.
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.
virtual widget * build() const override
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.
Sent by a widget to notify others its contents or state are modified.
virtual point calculate_best_size() const override=0
See widget::calculate_best_size.
void finalize(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.
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
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
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 init_grid(const builder_grid &grid_builder)
Initializes and builds the grid.
void toggle_item(const unsigned index)
Toggles the selection state of an item.
virtual void create_items(const int index, const builder_grid &list_builder, const std::vector< string_map > &data, const std::function< void(widget &)> &callback)=0
Creates one or more new item(s).
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.
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.
virtual widget * build() const override
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.
Default, unset return value.
std::function< void(unsigned, preferences::SORT_ORDER)> callback_order_change_
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::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.
std::shared_ptr< const builder_grid > builder_grid_const_ptr
boost::dynamic_bitset get_rows_shown() const
Returns a list of visible rows.
virtual void layout_children() override
See widget::layout_children.
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::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.
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
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.
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.
std::shared_ptr< builder_grid > builder_grid_ptr
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
static std::deque< std::string > call_stack
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?
std::pair< std::string, unsigned > item