The Battle for Wesnoth  1.19.0-dev
chatbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 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 #pragma once
16 
17 #include "chat_events.hpp"
18 #include "chat_log.hpp"
21 
22 #include <map>
23 #include <string>
24 
25 class config;
26 
27 namespace gui2
28 {
29 
30 // ------------ WIDGET -----------{
31 
32 class listbox;
33 class multi_page;
34 class text_box;
35 
36 namespace implementation
37 {
38  struct builder_chatbox;
39 }
40 
42 {
43  lobby_chat_window(const std::string& name, bool whisper)
45  {
46  }
47 
48  std::string name;
49  bool whisper;
51 };
52 
54 {
56 
57 public:
58  explicit chatbox(const implementation::builder_chatbox& builder);
59 
60  /** See @ref styled_widget::set_active. */
61  virtual void set_active(const bool /*active*/) override
62  {
63  /* DO NOTHING */
64  }
65 
66  /** See @ref styled_widget::get_active. */
67  virtual bool get_active() const override
68  {
69  return true;
70  }
71 
72  /** See @ref styled_widget::get_state. */
73  virtual unsigned get_state() const override
74  {
75  return 0;
76  }
77 
78  void send_to_server(const ::config& cfg) override;
79 
80  /** @todo: remove */
81  void set_active_window_changed_callback(const std::function<void(void)>& f)
82  {
84  }
85 
86  void load_log(std::map<std::string, chatroom_log>& log, bool show_lobby);
87 
88 protected:
89  /**
90  * Initializes the internal sub-widget pointers.
91  * Should be called after initializing the widget's grid.
92  */
93  void finalize_setup();
94 
95  /** Inherited form @ref chat_handler */
96  virtual void user_relation_changed(const std::string& name) override;
97 
98  /** Inherited form @ref chat_handler */
99  virtual void add_chat_message(const std::time_t& time,
100  const std::string& speaker,
101  int side,
102  const std::string& message,
105 
106  /** Inherited form @ref chat_handler */
107  virtual void add_whisper_sent(const std::string& receiver,
108  const std::string& message) override;
109 
110  /** Inherited form @ref chat_handler */
111  virtual void add_whisper_received(const std::string& sender,
112  const std::string& message) override;
113 
114  /** Inherited form @ref chat_handler */
115  virtual void add_chat_room_message_sent(const std::string& room,
116  const std::string& message) override;
117 
118  /** Inherited form @ref chat_handler */
119  virtual void add_chat_room_message_received(const std::string& room,
120  const std::string& speaker,
121  const std::string& message) override;
122 
123 private:
125 
127 
129 
130  std::vector<lobby_chat_window> open_windows_;
131 
132  std::size_t active_window_;
133 
134  std::function<void(void)> active_window_changed_callback_;
135 
136  std::map<std::string, chatroom_log>* log_;
137 
138 public:
139  /** Static type getter that does not rely on the widget being constructed. */
140  static const std::string& type();
141 
142 private:
143  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
144  virtual const std::string& get_control_type() const override;
145 
146  /** See @ref container_base::set_self_active. */
147  virtual void set_self_active(const bool /*active*/) override
148  {
149  /* DO NOTHING */
150  }
151 
152  void chat_input_keypress_callback(const SDL_Keycode key);
153 
154  void append_to_chatbox(const std::string& text, const bool force_scroll = false);
155 
156  void append_to_chatbox(const std::string& text, std::size_t id, const bool force_scroll = false);
157 
158  /** @returns true if the whisper window for "name" is the active window. */
159  bool whisper_window_active(const std::string& name);
160 
161  /** @returns true if the room window for "room" is the active window. */
162  bool room_window_active(const std::string& room);
163 
164  /** Mark the whisper window for "name" as having one more pending message. */
165  void increment_waiting_whispers(const std::string& name);
166 
167  /** Mark the room window for "room" as having one more pending message. */
168  void increment_waiting_messages(const std::string& room);
169 
170  /** Add a whisper message to the whisper window. */
171  void add_whisper_window_whisper(const std::string& sender,
172  const std::string& message);
173 
174  /**
175  * Add a whisper message to the current window which is not the whisper window for "name".
176  */
177  void add_active_window_whisper(const std::string& sender,
178  const std::string& message,
179  const bool force_scroll = false);
180 
181  /** Add a message to the window for room "room". */
182  void add_room_window_message(const std::string& room,
183  const std::string& sender,
184  const std::string& message);
185 
186  /** Add a message to the window for room "room". */
187  void add_active_window_message(const std::string& sender,
188  const std::string& message,
189  const bool force_scroll = false);
190 
191  void close_window(std::size_t idx);
192 
193 public:
194  /** Inherited form @ref chat_handler */
195  virtual void send_chat_message(const std::string& message, bool allies_only) override;
196 
197  virtual void clear_messages() override;
198 
199  /**
200  * Switch to the window given by a valid pointer (e.g. received from a call
201  * to *_window_open)
202  */
204 
205  void switch_to_window(std::size_t id);
206 
207  void active_window_changed();
208 
209  /**
210  * Check if a room window for "room" is open, if open_new is true
211  * then it will be created if not found. If allow_close is false, the
212  * 'close' button will be disabled.
213  * @return valid ptr if the window was found or added, null otherwise
214  */
215  lobby_chat_window* room_window_open(const std::string& room,
216  const bool open_new, const bool allow_close = true);
217 
218  /**
219  * Check if a whisper window for user "name" is open, if open_new is true
220  * then it will be created if not found.
221  * @return valid ptr if the window was found or added, null otherwise
222  */
223  lobby_chat_window* whisper_window_open(const std::string& name, bool open_new);
224 
225  /**
226  * Helper function to find and open a new window, used by *_window_open
227  */
228  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);
229 
230  void close_window_button_callback(std::string room_name, bool& handled, bool& halt);
231 
232  void process_message(const ::config& data, bool whisper = false);
233 
234  void process_network_data(const ::config& data);
235 
236 private:
238 };
239 
240 // }---------- DEFINITION ---------{
241 
243 {
244  explicit chatbox_definition(const config& cfg);
245 
247  {
248  explicit resolution(const config& cfg);
249 
251  };
252 };
253 
254 // }---------- BUILDER -----------{
255 
256 namespace implementation
257 {
258 
260 {
261 public:
262  explicit builder_chatbox(const config& cfg);
263 
265 
266  virtual std::unique_ptr<widget> build() const override;
267 
268 private:
269 };
270 
271 } // namespace implementation
272 
273 // }------------ END --------------
274 
275 } // namespace gui2
double t
Definition: astarsearch.cpp:63
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
std::map< std::string, chatroom_log > * log_
Definition: chatbox.hpp:136
bool room_window_active(const std::string &room)
Definition: chatbox.cpp:372
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:378
text_box * chat_input_
Definition: chatbox.hpp:128
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:391
virtual void clear_messages() override
Definition: chatbox.cpp:254
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:512
void append_to_chatbox(const std::string &text, const bool force_scroll=false)
Definition: chatbox.cpp:211
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: chatbox.hpp:73
virtual void user_relation_changed(const std::string &name) override
Inherited form chat_handler.
Definition: chatbox.cpp:262
virtual void send_chat_message(const std::string &message, bool allies_only) override
Inherited form chat_handler.
Definition: chatbox.cpp:246
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:569
void close_window(std::size_t idx)
Definition: chatbox.cpp:520
void add_whisper_window_whisper(const std::string &sender, const std::string &message)
Add a whisper message to the whisper window.
Definition: chatbox.cpp:500
void close_window_button_callback(std::string room_name, bool &handled, bool &halt)
Definition: chatbox.cpp:454
virtual void add_chat_room_message_sent(const std::string &room, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:325
multi_page * chat_log_container_
Definition: chatbox.hpp:126
void process_message(const ::config &data, bool whisper=false)
Definition: chatbox.cpp:577
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:384
void switch_to_window(lobby_chat_window *t)
Switch to the window given by a valid pointer (e.g.
Definition: chatbox.cpp:122
std::size_t active_window_
Definition: chatbox.hpp:132
virtual void set_self_active(const bool) override
See container_base::set_self_active.
Definition: chatbox.hpp:147
virtual void add_whisper_sent(const std::string &receiver, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:289
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:269
void process_network_data(const ::config &data)
Definition: chatbox.cpp:627
void increment_waiting_whispers(const std::string &name)
Mark the whisper window for "name" as having one more pending message.
Definition: chatbox.cpp:470
chatbox(const implementation::builder_chatbox &builder)
Definition: chatbox.cpp:56
void set_active_window_changed_callback(const std::function< void(void)> &f)
Definition: chatbox.hpp:81
std::function< void(void)> active_window_changed_callback_
Definition: chatbox.hpp:134
listbox * roomlistbox_
Definition: chatbox.hpp:124
void increment_waiting_messages(const std::string &room)
Mark the room window for "room" as having one more pending message.
Definition: chatbox.cpp:484
void signal_handler_receive_keyboard_focus(const event::ui_event event)
Definition: chatbox.cpp:636
void send_to_server(const ::config &cfg) override
Definition: chatbox.cpp:465
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:340
void load_log(std::map< std::string, chatroom_log > &log, bool show_lobby)
Definition: chatbox.cpp:92
virtual void add_whisper_received(const std::string &sender, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:301
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: chatbox.hpp:61
virtual bool get_active() const override
See styled_widget::get_active.
Definition: chatbox.hpp:67
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:75
void chat_input_keypress_callback(const SDL_Keycode key)
Definition: chatbox.cpp:141
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:555
void active_window_changed()
Definition: chatbox.cpp:107
std::vector< lobby_chat_window > open_windows_
Definition: chatbox.hpp:130
bool whisper_window_active(const std::string &name)
Definition: chatbox.cpp:366
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:43
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
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:194
chatbox_definition(const config &cfg)
Definition: chatbox.cpp:646
builder_chatbox(const config &cfg)
Definition: chatbox.cpp:666
virtual std::unique_ptr< widget > build() const override
Definition: chatbox.cpp:671
virtual std::unique_ptr< widget > build() const=0
lobby_chat_window(const std::string &name, bool whisper)
Definition: chatbox.hpp:43
#define f