The Battle for Wesnoth  1.19.0-dev
core_selection.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
21 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/window.hpp"
24 
25 #include <functional>
26 
27 namespace gui2::dialogs
28 {
29 
30 REGISTER_DIALOG(core_selection)
31 
32 void core_selection::core_selected()
33 {
34  const int selected_row
35  = find_widget<listbox>(this, "core_list", false)
36  .get_selected_row();
37 
38  multi_page& pages
39  = find_widget<multi_page>(this, "core_details", false);
40 
41  pages.select_page(selected_row);
42 }
43 
45 {
46  /***** Setup core list. *****/
47  listbox& list = find_widget<listbox>(&window, "core_list", false);
48 
50 
51  window.keyboard_capture(&list);
52 
53  /***** Setup core details. *****/
54  multi_page& pages
55  = find_widget<multi_page>(&window, "core_details", false);
56 
57  for(const auto & core : cores_)
58  {
59  /*** Add list item ***/
60  widget_item list_item;
61  widget_data list_item_item;
62 
63  list_item["label"] = core["image"];
64  list_item_item.emplace("image", list_item);
65 
66  list_item["label"] = core["name"];
67  list_item_item.emplace("name", list_item);
68 
69  grid* grid = &list.add_row(list_item_item);
70  assert(grid);
71 
72  /*** Add detail item ***/
73  widget_item detail_item;
74  widget_data detail_page;
75 
76  detail_item["label"] = core["description"];
77  detail_item["use_markup"] = "true";
78  detail_page.emplace("description", detail_item);
79 
80  pages.add_page(detail_page);
81  }
82  list.select_row(choice_, true);
83 
84  core_selected();
85 }
86 
88 {
89  choice_ = find_widget<listbox>(&window, "core_list", false)
90  .get_selected_row();
91 }
92 
93 } // namespace dialogs
This shows the dialog which allows the user to choose which core to play.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
const std::vector< config > & cores_
Contains the config objects for all cores.
void core_selected()
Called when another core is selected.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Base container class.
Definition: grid.hpp:32
The listbox class.
Definition: listbox.hpp:43
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.
Definition: listbox.cpp:59
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:243
A multi page is a control that contains several 'pages' of which only one is visible.
Definition: multi_page.hpp:50
grid & add_page(const widget_item &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:104
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void keyboard_capture(widget *widget)
Definition: window.cpp:1221
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31