The Battle for Wesnoth  1.17.17+dev
chatbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 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 #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)
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  {
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  */
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:
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
double t
Definition: astarsearch.cpp:65
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
std::map< std::string, chatroom_log > * log_
Definition: chatbox.hpp:137
bool room_window_active(const std::string &room)
Definition: chatbox.cpp:374
lobby_chat_window * room_window_open(const std::string &room, const bool open_new, const bool allow_close=true)
Check if a room window for "room" is open, if open_new is true then it will be created if not found.
Definition: chatbox.cpp:380
text_box * chat_input_
Definition: chatbox.hpp:129
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)
Helper function to find and open a new window, used by *_window_open.
Definition: chatbox.cpp:393
virtual void clear_messages() override
Definition: chatbox.cpp:256
void add_active_window_whisper(const std::string &sender, const std::string &message, const bool force_scroll=false)
Add a whisper message to the current window which is not the whisper window for "name".
Definition: chatbox.cpp:514
void append_to_chatbox(const std::string &text, const bool force_scroll=false)
Definition: chatbox.cpp:213
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: chatbox.hpp:74
virtual void user_relation_changed(const std::string &name) override
Inherited form chat_handler.
Definition: chatbox.cpp:264
virtual void send_chat_message(const std::string &message, bool allies_only) override
Inherited form chat_handler.
Definition: chatbox.cpp:248
void add_active_window_message(const std::string &sender, const std::string &message, const bool force_scroll=false)
Add a message to the window for room "room".
Definition: chatbox.cpp:571
void close_window(std::size_t idx)
Definition: chatbox.cpp:522
void add_whisper_window_whisper(const std::string &sender, const std::string &message)
Add a whisper message to the whisper window.
Definition: chatbox.cpp:502
void close_window_button_callback(std::string room_name, bool &handled, bool &halt)
Definition: chatbox.cpp:456
virtual void add_chat_room_message_sent(const std::string &room, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:327
multi_page * chat_log_container_
Definition: chatbox.hpp:127
void process_message(const ::config &data, bool whisper=false)
Definition: chatbox.cpp:579
lobby_chat_window * whisper_window_open(const std::string &name, bool open_new)
Check if a whisper window for user "name" is open, if open_new is true then it will be created if not...
Definition: chatbox.cpp:386
void switch_to_window(lobby_chat_window *t)
Switch to the window given by a valid pointer (e.g.
Definition: chatbox.cpp:124
std::size_t active_window_
Definition: chatbox.hpp:133
virtual void set_self_active(const bool) override
See container_base::set_self_active.
Definition: chatbox.hpp:148
virtual void add_whisper_sent(const std::string &receiver, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:291
virtual void add_chat_message(const std::time_t &time, const std::string &speaker, int side, const std::string &message, events::chat_handler::MESSAGE_TYPE type=events::chat_handler::MESSAGE_PRIVATE) override
Inherited form chat_handler.
Definition: chatbox.cpp:271
void process_network_data(const ::config &data)
Definition: chatbox.cpp:629
void increment_waiting_whispers(const std::string &name)
Mark the whisper window for "name" as having one more pending message.
Definition: chatbox.cpp:472
chatbox(const implementation::builder_chatbox &builder)
Definition: chatbox.cpp:58
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
listbox * roomlistbox_
Definition: chatbox.hpp:125
void increment_waiting_messages(const std::string &room)
Mark the room window for "room" as having one more pending message.
Definition: chatbox.cpp:486
void signal_handler_receive_keyboard_focus(const event::ui_event event)
Definition: chatbox.cpp:638
void send_to_server(const ::config &cfg) override
Definition: chatbox.cpp:467
virtual void add_chat_room_message_received(const std::string &room, const std::string &speaker, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:342
void load_log(std::map< std::string, chatroom_log > &log, bool show_lobby)
Definition: chatbox.cpp:94
virtual void add_whisper_received(const std::string &sender, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:303
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: chatbox.hpp:62
virtual bool get_active() const override
See styled_widget::get_active.
Definition: chatbox.hpp:68
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void finalize_setup()
Initializes the internal sub-widget pointers.
Definition: chatbox.cpp:77
void chat_input_keypress_callback(const SDL_Keycode key)
Definition: chatbox.cpp:143
void add_room_window_message(const std::string &room, const std::string &sender, const std::string &message)
Add a message to the window for room "room".
Definition: chatbox.cpp:557
void active_window_changed()
Definition: chatbox.cpp:109
std::vector< lobby_chat_window > open_windows_
Definition: chatbox.hpp:131
bool whisper_window_active(const std::string &name)
Definition: chatbox.cpp:368
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
A generic container base class.
Main class to show messages to the user.
Definition: message.hpp:36
The listbox class.
Definition: listbox.hpp:46
A multi page is a control that contains several 'pages' of which only one is visible.
Definition: multi_page.hpp:50
Class for a single line text area.
Definition: text_box.hpp:142
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::string_view data
Definition: picture.cpp:199
chatbox_definition(const config &cfg)
Definition: chatbox.cpp:648
builder_chatbox(const config &cfg)
Definition: chatbox.cpp:668
virtual std::unique_ptr< widget > build() const override
Definition: chatbox.cpp:673
virtual std::unique_ptr< widget > build() const=0
lobby_chat_window(const std::string &name, bool whisper)
Definition: chatbox.hpp:44
Base class of a resolution, contains the common keys for a resolution.
#define f