The Battle for Wesnoth  1.17.4+dev
chatbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 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 #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  virtual void clear_messages() 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  * Check if a room window for "room" is open, if open_new is true
212  * then it will be created if not found. If allow_close is false, the
213  * 'close' button will be disabled.
214  * @return valid ptr if the window was found or added, null otherwise
215  */
216  lobby_chat_window* room_window_open(const std::string& room,
217  const bool open_new, const bool allow_close = true);
218 
219  /**
220  * Check if a whisper window for user "name" is open, if open_new is true
221  * then it will be created if not found.
222  * @return valid ptr if the window was found or added, null otherwise
223  */
224  lobby_chat_window* whisper_window_open(const std::string& name, bool open_new);
225 
226  /**
227  * Helper function to find and open a new window, used by *_window_open
228  */
229  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);
230 
231  void close_window_button_callback(std::string room_name, bool& handled, bool& halt);
232 
233  void process_message(const ::config& data, bool whisper = false);
234 
235  void process_network_data(const ::config& data);
236 
237 private:
238  void signal_handler_receive_keyboard_focus(const event::ui_event event);
239 };
240 
241 // }---------- DEFINITION ---------{
242 
244 {
245  explicit chatbox_definition(const config& cfg);
246 
248  {
249  explicit resolution(const config& cfg);
250 
252  };
253 };
254 
255 // }---------- BUILDER -----------{
256 
257 namespace implementation
258 {
259 
261 {
262 public:
263  explicit builder_chatbox(const config& cfg);
264 
266 
267  virtual std::unique_ptr<widget> build() const override;
268 
269 private:
270 };
271 
272 } // namespace implementation
273 
274 // }------------ END --------------
275 
276 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
multi_page * chat_log_container_
Definition: chatbox.hpp:127
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
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
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:45
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
virtual std::unique_ptr< widget > build() const =0
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.
std::shared_ptr< builder_grid > builder_grid_ptr
#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:399
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:60
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.