The Battle for Wesnoth  1.17.17+dev
mp_report.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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 
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
Simple push button.
Definition: button.hpp:37
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:65
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
window * get_window()
Returns a pointer to the dialog's window.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: mp_report.cpp:66
mp_report(std::string &report_text_)
Definition: mp_report.cpp:37
static const std::array< std::string, 3 > occurrence_locations
Definition: mp_report.hpp:54
void report_reason_changed(const std::string &text)
Definition: mp_report.cpp:94
std::string & report_text_
Definition: mp_report.hpp:51
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: mp_report.cpp:45
void reportee_changed(const std::string &text)
Definition: mp_report.cpp:86
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:62
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:84
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.
Class for a single line text area.
Definition: text_box.hpp:142
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
void set_exit_hook(exit_hook mode, std::function< bool(window &)> func)
Sets the window's exit hook.
Definition: window.hpp:412
@ 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...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35