The Battle for Wesnoth  1.19.5+dev
mp_report.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include <functional>
18 
20 
21 #include "gettext.hpp"
22 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/text_box.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 namespace gui2::dialogs
28 {
29 
30 REGISTER_DIALOG(mp_report)
31 
32 // static t_string std::array causes a core dump on start
33 const std::array<std::string, 3> mp_report::occurrence_locations = {"Lobby", "Whisper", "Game"};
34 
35 mp_report::mp_report(std::string& report_text)
36  : modal_dialog(window_id())
37  , report_text_(report_text)
38  , reportee_empty_(true)
39  , report_reason_empty_(true)
40 {
41 }
42 
44 {
45  std::vector<config> occurrence_location_entries;
46  occurrence_location_entries.emplace_back("label", _("Lobby"));
47  occurrence_location_entries.emplace_back("label", _("Whisper"));
48  occurrence_location_entries.emplace_back("label", _("Game"));
49 
50  find_widget<menu_button>("occurrence_location").set_values(occurrence_location_entries);
51 
52  button& ok = find_widget<button>("ok");
53  ok.set_active(false);
54 
55  text_box& reportee = find_widget<text_box>("reportee");
56  reportee.set_text_changed_callback(std::bind(&mp_report::reportee_changed, this, std::placeholders::_2));
57 
58  text_box& report_reason = find_widget<text_box>("report_reason");
59  report_reason.set_text_changed_callback(std::bind(&mp_report::report_reason_changed, this, std::placeholders::_2));
60 
62 }
63 
65 {
67  {
68  const text_box& reportee = find_widget<const text_box>("reportee");
69  const text_box& report_reason = find_widget<const text_box>("report_reason");
70  const menu_button& occurrence_location = find_widget<const menu_button>("occurrence_location");
71  const std::string additional_information = find_widget<const text_box>("additional_information").get_value();
72 
73  std::ostringstream report;
74  report << "Reporting player '" << reportee.get_value() << "' for reason '" << report_reason.get_value() << "'."
75  << " Location of occurrence is '" << occurrence_locations[occurrence_location.get_value()] << "'.";
76  if(additional_information.size() > 0)
77  {
78  report << " Additional information provided: '" << additional_information << "'.";
79  }
80  report_text_ = report.str();
81  }
82 }
83 
84 void mp_report::reportee_changed(const std::string& text)
85 {
86  reportee_empty_ = text.empty();
87 
88  button& ok = find_widget<button>("ok");
90 }
91 
92 void mp_report::report_reason_changed(const std::string& text)
93 {
94  report_reason_empty_ = text.empty();
95 
96  button& ok = find_widget<button>("ok");
98 }
99 
100 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:64
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
mp_report(std::string &report_text_)
Definition: mp_report.cpp:35
static const std::array< std::string, 3 > occurrence_locations
Definition: mp_report.hpp:42
virtual void post_show() override
Actions to be taken after the window has been shown.
Definition: mp_report.cpp:64
void report_reason_changed(const std::string &text)
Definition: mp_report.cpp:92
std::string & report_text_
Definition: mp_report.hpp:39
virtual void pre_show() override
Actions to be taken before showing the window.
Definition: mp_report.cpp:43
void reportee_changed(const std::string &text)
Definition: mp_report.cpp:84
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:55
std::string get_value() const
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
void set_exit_hook(exit_hook mode, std::function< bool(window &)> func)
Sets the window's exit hook.
Definition: window.hpp:449
@ on_ok
Run hook only if result is OK.
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(editor_edit_unit)
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35