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 
137  std::map<std::string, chatroom_log>* log_;
138 
139 public:
140  /** Static type getter that does not rely on the widget being constructed. */
141  static const std::string& type();
142 
143 private:
144  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
145  virtual const std::string& get_control_type() const override;
146 
147  /** See @ref container_base::set_self_active. */
148  virtual void set_self_active(const bool /*active*/) override
149  {
150  /* DO NOTHING */
151  }
152 
153  void chat_input_keypress_callback(const SDL_Keycode key);
154 
155  void append_to_chatbox(const std::string& text, const bool force_scroll = false);
156 
157  void append_to_chatbox(const std::string& text, std::size_t id, const bool force_scroll = false);
158 
159  /** @returns true if the whisper window for "name" is the active window. */
160  bool whisper_window_active(const std::string& name);
161 
162  /** @returns true if the room window for "room" is the active window. */
163  bool room_window_active(const std::string& room);
164 
165  /** Mark the whisper window for "name" as having one more pending message. */
166  void increment_waiting_whispers(const std::string& name);
167 
168  /** Mark the room window for "room" as having one more pending message. */
169  void increment_waiting_messages(const std::string& room);
170 
171  /** Add a whisper message to the whisper window. */
172  void add_whisper_window_whisper(const std::string& sender,
173  const std::string& message);
174 
175  /**
176  * Add a whisper message to the current window which is not the whisper window for "name".
177  */
178  void add_active_window_whisper(const std::string& sender,
179  const std::string& message,
180  const bool force_scroll = false);
181 
182  /** Add a message to the window for room "room". */
183  void add_room_window_message(const std::string& room,
184  const std::string& sender,
185  const std::string& message);
186 
187  /** Add a message to the window for room "room". */
188  void add_active_window_message(const std::string& sender,
189  const std::string& message,
190  const bool force_scroll = false);
191 
192  void close_window(std::size_t idx);
193 
194 public:
195  /** Inherited form @ref chat_handler */
196  virtual void send_chat_message(const std::string& message, bool allies_only) override;
197 
198  /**
199  * Switch to the window given by a valid pointer (e.g. received from a call
200  * to *_window_open)
201  */
202  void switch_to_window(lobby_chat_window* t);
203 
204  void switch_to_window(std::size_t id);
205 
206  void active_window_changed();
207 
208  /**
209  * Check if a room window for "room" is open, if open_new is true
210  * then it will be created if not found. If allow_close is false, the
211  * 'close' button will be disabled.
212  * @return valid ptr if the window was found or added, null otherwise
213  */
214  lobby_chat_window* room_window_open(const std::string& room,
215  const bool open_new, const bool allow_close = true);
216 
217  /**
218  * Check if a whisper window for user "name" is open, if open_new is true
219  * then it will be created if not found.
220  * @return valid ptr if the window was found or added, null otherwise
221  */
222  lobby_chat_window* whisper_window_open(const std::string& name, bool open_new);
223 
224  /**
225  * Helper function to find and open a new window, used by *_window_open
226  */
227  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);
228 
229  void close_window_button_callback(std::string room_name, bool& handled, bool& halt);
230 
231  void process_message(const ::config& data, bool whisper = false);
232 
233  void process_network_data(const ::config& data);
234 
235 private:
236  void signal_handler_receive_keyboard_focus(const event::ui_event event);
237 };
238 
239 // }---------- DEFINITION ---------{
240 
242 {
243  explicit chatbox_definition(const config& cfg);
244 
246  {
247  explicit resolution(const config& cfg);
248 
250  };
251 };
252 
253 // }---------- BUILDER -----------{
254 
255 namespace implementation
256 {
257 
259 {
260 public:
261  explicit builder_chatbox(const config& cfg);
262 
264 
265  virtual widget* build() const override;
266 
267 private:
268 };
269 
270 } // namespace implementation
271 
272 // }------------ END --------------
273 
274 } // 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:148
Base class for all widgets.
Definition: widget.hpp:49
std::map< std::string, chatroom_log > * log_
Definition: chatbox.hpp:137
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
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
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
void send_to_server(const config &data)
Attempts to send given data to server if a connection is open.
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