The Battle for Wesnoth  1.17.0-dev
chatbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2021
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 #pragma once
16 
17 #include "chat_events.hpp"
18 #include "chat_log.hpp"
22 
23 #include <map>
24 #include <string>
25 
26 class config;
27 
28 namespace gui2
29 {
30 
31 // ------------ WIDGET -----------{
32 
33 class listbox;
34 class multi_page;
35 class text_box;
36 
37 namespace implementation
38 {
39  struct builder_chatbox;
40 }
41 
43 {
44  lobby_chat_window(const std::string& name, bool whisper)
45  : name(name), whisper(whisper), pending_messages(0)
46  {
47  }
48 
49  std::string name;
50  bool whisper;
52 };
53 
55 {
57 
58 public:
59  explicit chatbox(const implementation::builder_chatbox& builder);
60 
61  /** See @ref styled_widget::set_active. */
62  virtual void set_active(const bool /*active*/) override
63  {
64  /* DO NOTHING */
65  }
66 
67  /** See @ref styled_widget::get_active. */
68  virtual bool get_active() const override
69  {
70  return true;
71  }
72 
73  /** See @ref styled_widget::get_state. */
74  virtual unsigned get_state() const override
75  {
76  return 0;
77  }
78 
79  void send_to_server(const ::config& cfg) override;
80 
81  /** @todo: remove */
82  void set_active_window_changed_callback(const std::function<void(void)>& f)
83  {
84  active_window_changed_callback_ = f;
85  }
86 
87  void load_log(std::map<std::string, chatroom_log>& log, bool show_lobby);
88 
89 protected:
90  /**
91  * Initializes the internal sub-widget pointers.
92  * Should be called after initializing the widget's grid.
93  */
94  void finalize_setup();
95 
96  /** Inherited form @ref chat_handler */
97  virtual void user_relation_changed(const std::string& name) override;
98 
99  /** Inherited form @ref chat_handler */
100  virtual void add_chat_message(const std::time_t& time,
101  const std::string& speaker,
102  int side,
103  const std::string& message,
106 
107  /** Inherited form @ref chat_handler */
108  virtual void add_whisper_sent(const std::string& receiver,
109  const std::string& message) override;
110 
111  /** Inherited form @ref chat_handler */
112  virtual void add_whisper_received(const std::string& sender,
113  const std::string& message) override;
114 
115  /** Inherited form @ref chat_handler */
116  virtual void add_chat_room_message_sent(const std::string& room,
117  const std::string& message) override;
118 
119  /** Inherited form @ref chat_handler */
120  virtual void add_chat_room_message_received(const std::string& room,
121  const std::string& speaker,
122  const std::string& message) override;
123 
124 private:
126 
128 
130 
131  std::vector<lobby_chat_window> open_windows_;
132 
133  std::size_t active_window_;
134 
135  std::function<void(void)> active_window_changed_callback_;
136 
138 
139  std::map<std::string, chatroom_log>* log_;
140 
141 public:
142  /** Static type getter that does not rely on the widget being constructed. */
143  static const std::string& type();
144 
145 private:
146  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
147  virtual const std::string& get_control_type() const override;
148 
149  /** See @ref container_base::set_self_active. */
150  virtual void set_self_active(const bool /*active*/) override
151  {
152  /* DO NOTHING */
153  }
154 
155  void chat_input_keypress_callback(const SDL_Keycode key);
156 
157  void append_to_chatbox(const std::string& text, const bool force_scroll = false);
158 
159  void append_to_chatbox(const std::string& text, std::size_t id, const bool force_scroll = false);
160 
161  /** @returns true if the whisper window for "name" is the active window. */
162  bool whisper_window_active(const std::string& name);
163 
164  /** @returns true if the room window for "room" is the active window. */
165  bool room_window_active(const std::string& room);
166 
167  /** Mark the whisper window for "name" as having one more pending message. */
168  void increment_waiting_whispers(const std::string& name);
169 
170  /** Mark the room window for "room" as having one more pending message. */
171  void increment_waiting_messages(const std::string& room);
172 
173  /** Add a whisper message to the whisper window. */
174  void add_whisper_window_whisper(const std::string& sender,
175  const std::string& message);
176 
177  /**
178  * Add a whisper message to the current window which is not the whisper window for "name".
179  */
180  void add_active_window_whisper(const std::string& sender,
181  const std::string& message,
182  const bool force_scroll = false);
183 
184  /** Add a message to the window for room "room". */
185  void add_room_window_message(const std::string& room,
186  const std::string& sender,
187  const std::string& message);
188 
189  /** Add a message to the window for room "room". */
190  void add_active_window_message(const std::string& sender,
191  const std::string& message,
192  const bool force_scroll = false);
193 
194  void close_window(std::size_t idx);
195 
196 public:
197  /** Inherited form @ref chat_handler */
198  virtual void send_chat_message(const std::string& message, bool allies_only) override;
199 
200  /**
201  * Switch to the window given by a valid pointer (e.g. received from a call
202  * to *_window_open)
203  */
204  void switch_to_window(lobby_chat_window* t);
205 
206  void switch_to_window(std::size_t id);
207 
208  void active_window_changed();
209 
210  /**
211  * Get the room* corresponding to the currently active window, or nullptr
212  * if a whisper window is active at the moment
213  */
214  mp::room_info* active_window_room();
215 
216  /**
217  * Check if a room window for "room" is open, if open_new is true
218  * then it will be created if not found. If allow_close is false, the
219  * 'close' button will be disabled.
220  * @return valid ptr if the window was found or added, null otherwise
221  */
222  lobby_chat_window* room_window_open(const std::string& room,
223  const bool open_new, const bool allow_close = true);
224 
225  /**
226  * Check if a whisper window for user "name" is open, if open_new is true
227  * then it will be created if not found.
228  * @return valid ptr if the window was found or added, null otherwise
229  */
230  lobby_chat_window* whisper_window_open(const std::string& name, bool open_new);
231 
232  /**
233  * Helper function to find and open a new window, used by *_window_open
234  */
235  lobby_chat_window* find_or_create_window(const std::string& name, const bool whisper, const bool open_new, const bool allow_close, const std::string& initial_text);
236 
237  void close_window_button_callback(std::string room_name, bool& handled, bool& halt);
238 
239  void process_message(const ::config& data, bool whisper = false);
240 
241  void process_network_data(const ::config& data);
242 
243 private:
244  void signal_handler_receive_keyboard_focus(const event::ui_event event);
245 };
246 
247 // }---------- DEFINITION ---------{
248 
250 {
251  explicit chatbox_definition(const config& cfg);
252 
254  {
255  explicit resolution(const config& cfg);
256 
258  };
259 };
260 
261 // }---------- BUILDER -----------{
262 
263 namespace implementation
264 {
265 
267 {
268 public:
269  explicit builder_chatbox(const config& cfg);
270 
272 
273  virtual widget* build() const override;
274 
275 private:
276 };
277 
278 } // namespace implementation
279 
280 // }------------ END --------------
281 
282 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
virtual widget * build() const =0
multi_page * chat_log_container_
Definition: chatbox.hpp:127
Main class to show messages to the user.
Definition: message.hpp:35
virtual void set_self_active(const bool) override
See container_base::set_self_active.
Definition: chatbox.hpp:150
Base class for all widgets.
Definition: widget.hpp:49
std::map< std::string, chatroom_log > * log_
Definition: chatbox.hpp:139
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: chatbox.hpp:62
std::vector< lobby_chat_window > open_windows_
Definition: chatbox.hpp:131
lobby_chat_window(const std::string &name, bool whisper)
Definition: chatbox.hpp:44
std::size_t active_window_
Definition: chatbox.hpp:133
text_box * chat_input_
Definition: chatbox.hpp:129
Class for a single line text area.
Definition: text_box.hpp:141
mp::chat_info chat_info_
Definition: chatbox.hpp:137
Generic file dialog.
Definition: field-fwd.hpp:23
The listbox class.
Definition: listbox.hpp:43
void set_active_window_changed_callback(const std::function< void(void)> &f)
Definition: chatbox.hpp:82
std::function< void(void)> active_window_changed_callback_
Definition: chatbox.hpp:135
virtual bool get_active() const override
See styled_widget::get_active.
Definition: chatbox.hpp:68
listbox * roomlistbox_
Definition: chatbox.hpp:125
This class represents the information a client has about a room.
Definition: lobby_data.hpp:68
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: chatbox.hpp:74
A generic container base class.
#define f
double t
Definition: astarsearch.cpp:65
A multi page is a control that contains several &#39;pages&#39; of which only one is visible.
Definition: multi_page.hpp:49
point resolution()
Definition: general.cpp:393
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:48