The Battle for Wesnoth  1.17.12+dev
mp_report.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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"
23 #include "gui/widgets/button.hpp"
25 #include "gui/widgets/text_box.hpp"
26 #include "gui/widgets/window.hpp"
28 
29 namespace gui2::dialogs
30 {
31 
32 REGISTER_DIALOG(mp_report)
33 
34 // static t_string std::array causes a core dump on start
35 const std::array<std::string, 3> mp_report::occurrence_locations = {"Lobby", "Whisper", "Game"};
36 
37 mp_report::mp_report(std::string& report_text)
38  : modal_dialog(window_id())
39  , report_text_(report_text)
40  , reportee_empty_(true)
41  , report_reason_empty_(true)
42 {
43 }
44 
46 {
47  std::vector<config> occurrence_location_entries;
48  occurrence_location_entries.emplace_back("label", _("Lobby"));
49  occurrence_location_entries.emplace_back("label", _("Whisper"));
50  occurrence_location_entries.emplace_back("label", _("Game"));
51 
52  find_widget<menu_button>(&win, "occurrence_location", false).set_values(occurrence_location_entries);
53 
54  button& ok = find_widget<button>(&win, "ok", false);
55  ok.set_active(false);
56 
57  text_box& reportee = find_widget<text_box>(&win, "reportee", false);
58  reportee.set_text_changed_callback(std::bind(&mp_report::reportee_changed, this, std::placeholders::_2));
59 
60  text_box& report_reason = find_widget<text_box>(&win, "report_reason", false);
61  report_reason.set_text_changed_callback(std::bind(&mp_report::report_reason_changed, this, std::placeholders::_2));
62 
63  win.set_exit_hook(window::exit_hook::on_ok, [this](window&) { return !reportee_empty_ && !report_reason_empty_; });
64 }
65 
67 {
69  {
70  const text_box& reportee = find_widget<const text_box>(&window, "reportee", false);
71  const text_box& report_reason = find_widget<const text_box>(&window, "report_reason", false);
72  const menu_button& occurrence_location = find_widget<const menu_button>(&window, "occurrence_location", false);
73  const std::string additional_information = find_widget<const text_box>(&window, "additional_information", false).get_value();
74 
75  std::ostringstream report;
76  report << "Reporting player '" << reportee.get_value() << "' for reason '" << report_reason.get_value() << "'."
77  << " Location of occurrence is '" << occurrence_locations[occurrence_location.get_value()] << "'.";
78  if(additional_information.size() > 0)
79  {
80  report << " Additional information provided: '" << additional_information << "'.";
81  }
82  report_text_ = report.str();
83  }
84 }
85 
86 void mp_report::reportee_changed(const std::string& text)
87 {
88  reportee_empty_ = text.empty();
89 
90  button& ok = find_widget<button>(get_window(), "ok", false);
92 }
93 
94 void mp_report::report_reason_changed(const std::string& text)
95 {
96  report_reason_empty_ = text.empty();
97 
98  button& ok = find_widget<button>(get_window(), "ok", false);
100 }
101 
102 } // namespace dialogs
window(const builder_window::window_resolution &definition)
< Needs to be initialized in show.
Definition: window.cpp:263
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:61
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: mp_report.cpp:66
std::string & report_text_
Definition: mp_report.hpp:51
std::string get_value() const
This file contains the window object, this object is a top level container which has the event manage...
static const std::array< std::string, 3 > occurrence_locations
Definition: mp_report.hpp:54
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: mp_report.cpp:45
static std::string _(const char *str)
Definition: gettext.hpp:93
Class for a single line text area.
Definition: text_box.hpp:141
void reportee_changed(const std::string &text)
Definition: mp_report.cpp:86
Run hook only if result is OK.
window * get_window()
Returns a pointer to the dialog&#39;s window.
mp_report(std::string &report_text_)
Definition: mp_report.cpp:37
void report_reason_changed(const std::string &text)
Definition: mp_report.cpp:94
int get_retval() const
Returns the cached window exit code.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:63
Abstract base class for all modal dialogs.
Simple push button.
Definition: button.hpp:36
Dialog was closed with the OK button.
Definition: retval.hpp:35
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66