The Battle for Wesnoth  1.19.0-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"
23 #include "gui/widgets/button.hpp"
25 #include "gui/widgets/text_box.hpp"
26 #include "gui/widgets/window.hpp"
27 
28 namespace gui2::dialogs
29 {
30 
31 REGISTER_DIALOG(mp_report)
32 
33 // static t_string std::array causes a core dump on start
34 const std::array<std::string, 3> mp_report::occurrence_locations = {"Lobby", "Whisper", "Game"};
35 
36 mp_report::mp_report(std::string& report_text)
37  : modal_dialog(window_id())
38  , report_text_(report_text)
39  , reportee_empty_(true)
40  , report_reason_empty_(true)
41 {
42 }
43 
45 {
46  std::vector<config> occurrence_location_entries;
47  occurrence_location_entries.emplace_back("label", _("Lobby"));
48  occurrence_location_entries.emplace_back("label", _("Whisper"));
49  occurrence_location_entries.emplace_back("label", _("Game"));
50 
51  find_widget<menu_button>(&win, "occurrence_location", false).set_values(occurrence_location_entries);
52 
53  button& ok = find_widget<button>(&win, "ok", false);
54  ok.set_active(false);
55 
56  text_box& reportee = find_widget<text_box>(&win, "reportee", false);
57  reportee.set_text_changed_callback(std::bind(&mp_report::reportee_changed, this, std::placeholders::_2));
58 
59  text_box& report_reason = find_widget<text_box>(&win, "report_reason", false);
60  report_reason.set_text_changed_callback(std::bind(&mp_report::report_reason_changed, this, std::placeholders::_2));
61 
63 }
64 
66 {
68  {
69  const text_box& reportee = find_widget<const text_box>(&window, "reportee", false);
70  const text_box& report_reason = find_widget<const text_box>(&window, "report_reason", false);
71  const menu_button& occurrence_location = find_widget<const menu_button>(&window, "occurrence_location", false);
72  const std::string additional_information = find_widget<const text_box>(&window, "additional_information", false).get_value();
73 
74  std::ostringstream report;
75  report << "Reporting player '" << reportee.get_value() << "' for reason '" << report_reason.get_value() << "'."
76  << " Location of occurrence is '" << occurrence_locations[occurrence_location.get_value()] << "'.";
77  if(additional_information.size() > 0)
78  {
79  report << " Additional information provided: '" << additional_information << "'.";
80  }
81  report_text_ = report.str();
82  }
83 }
84 
85 void mp_report::reportee_changed(const std::string& text)
86 {
87  reportee_empty_ = text.empty();
88 
89  button& ok = find_widget<button>(get_window(), "ok", false);
91 }
92 
93 void mp_report::report_reason_changed(const std::string& text)
94 {
95  report_reason_empty_ = text.empty();
96 
97  button& ok = find_widget<button>(get_window(), "ok", false);
99 }
100 
101 } // 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:63
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:65
mp_report(std::string &report_text_)
Definition: mp_report.cpp:36
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:93
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:44
void reportee_changed(const std::string &text)
Definition: mp_report.cpp:85
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:59
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:81
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:63
void set_exit_hook(exit_hook mode, std::function< bool(window &)> func)
Sets the window's exit hook.
Definition: window.hpp:453
@ 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