The Battle for Wesnoth  1.19.0-dev
simple_item_selector.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
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/button.hpp"
22 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/window.hpp"
24 
25 namespace gui2::dialogs
26 {
27 
28 REGISTER_DIALOG(simple_item_selector)
29 
30 simple_item_selector::simple_item_selector(const std::string& title,
31  const std::string& message,
32  const list_type& items,
33  bool title_uses_markup,
34  bool message_uses_markup)
35  : modal_dialog(window_id())
36  , index_(-1)
37  , single_button_(false)
38  , items_(items)
39  , ok_label_()
40  , cancel_label_()
41 {
42  register_label("title", true, title, title_uses_markup);
43  register_label("message", true, message, message_uses_markup);
44 }
45 
47 {
48  listbox& list = find_widget<listbox>(&window, "listbox", false);
49  window.keyboard_capture(&list);
50 
51  for(const auto & it : items_)
52  {
54  widget_item column;
55 
56  column["label"] = it;
57  data.emplace("item", column);
58 
59  list.add_row(data);
60  }
61 
62  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
63  list.select_row(index_);
64  }
65 
66  index_ = -1;
67 
68  button& button_ok = find_widget<button>(&window, "ok", false);
69  button& button_cancel = find_widget<button>(&window, "cancel", false);
70 
71  if(!ok_label_.empty()) {
72  button_ok.set_label(ok_label_);
73  }
74 
75  if(!cancel_label_.empty()) {
76  button_cancel.set_label(cancel_label_);
77  }
78 
79  if(single_button_) {
81  }
82 }
83 
85 {
87  index_ = find_widget<listbox>(&window, "listbox", false).get_selected_row();
88  }
89 }
90 
91 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
Main class to show messages to the user.
Definition: message.hpp:36
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
A simple one-column listbox with OK and Cancel buttons.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
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
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:124
virtual void set_label(const t_string &text)
void set_visible(const visibility visible)
Definition: widget.cpp:470
@ invisible
The user set the widget invisible, that means:
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:1215
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(tod_new_schedule)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
const std::vector< std::string > items
std::string_view data
Definition: picture.cpp:194