The Battle for Wesnoth  1.19.7+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 
20 #include "gui/widgets/button.hpp"
21 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/window.hpp"
23 
24 namespace gui2::dialogs
25 {
26 
27 REGISTER_DIALOG(simple_item_selector)
28 
29 simple_item_selector::simple_item_selector(const std::string& title,
30  const std::string& message,
31  const list_type& items,
32  bool title_uses_markup,
33  bool message_uses_markup)
34  : modal_dialog(window_id())
35  , index_(-1)
36  , single_button_(false)
37  , items_(items)
38  , ok_label_()
39  , cancel_label_()
40 {
41  register_label("title", true, title, title_uses_markup);
42  register_label("message", true, message, message_uses_markup);
43 }
44 
46 {
47  listbox& list = find_widget<listbox>("listbox");
48  keyboard_capture(&list);
49 
50  for(const auto & it : items_)
51  {
53  widget_item column;
54 
55  column["label"] = it;
56  data.emplace("item", column);
57 
58  list.add_row(data);
59  }
60 
61  if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
62  list.select_row(index_);
63  }
64 
65  index_ = -1;
66 
67  button& button_ok = find_widget<button>("ok");
68  button& button_cancel = find_widget<button>("cancel");
69 
70  if(!ok_label_.empty()) {
71  button_ok.set_label(ok_label_);
72  }
73 
74  if(!cancel_label_.empty()) {
75  button_cancel.set_label(cancel_label_);
76  }
77 
78  if(single_button_) {
80  }
81 }
82 
84 {
86  index_ = find_widget<listbox>("listbox").get_selected_row();
87  }
88 }
89 
90 } // 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.
virtual void pre_show() override
Actions to be taken before showing the window.
virtual void post_show() override
Actions to be taken after the window has been shown.
The listbox class.
Definition: listbox.hpp:41
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:92
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:280
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:159
virtual void set_label(const t_string &text)
void set_visible(const visibility visible)
Definition: widget.cpp:479
@ invisible
The user set the widget invisible, that means:
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(editor_edit_unit)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::string_view data
Definition: picture.cpp:178