The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
chatbox.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 - 2025
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 <set>
24#include <string>
25
26class config;
27
28namespace gui2
29{
30
31// ------------ WIDGET -----------{
32
33class listbox;
34class multi_page;
35class text_box;
36
37namespace 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
58public:
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 /**
90 * Supplies an additional set of names to offer for auto-completion, on top of
91 * whatever @ref mp::get_lobby_info reports.
92 *
93 * The main lobby's user list (@ref mp::lobby_info) is only kept up to date while
94 * the main lobby screen is shown; a host or player sitting in the game staging
95 * screen no longer receives lobby-wide roster updates from the server, so
96 * callers there should keep this synchronised with their own live player/observer
97 * list instead.
98 */
99 void set_available_users(const std::set<std::string>& names)
100 {
102 }
103
104protected:
105 /**
106 * Initializes the internal sub-widget pointers.
107 * Should be called after initializing the widget's grid.
108 */
109 void finalize_setup();
110
111 /** Inherited form @ref chat_handler */
112 virtual void user_relation_changed(const std::string& name) override;
113
114 /** Inherited form @ref chat_handler */
115 virtual void add_chat_message(const std::chrono::system_clock::time_point& time,
116 const std::string& speaker,
117 int side,
118 const std::string& message,
121
122 /** Inherited form @ref chat_handler */
123 virtual void add_whisper_sent(const std::string& receiver,
124 const std::string& message) override;
125
126 /** Inherited form @ref chat_handler */
127 virtual void add_whisper_received(const std::string& sender,
128 const std::string& message) override;
129
130 /** Inherited form @ref chat_handler */
131 virtual void add_chat_room_message_sent(const std::string& room,
132 const std::string& message) override;
133
134 /** Inherited form @ref chat_handler */
135 virtual void add_chat_room_message_received(const std::string& room,
136 const std::string& speaker,
137 const std::string& message) override;
138
139private:
141
143
145
146 std::vector<lobby_chat_window> open_windows_;
147
148 std::size_t active_window_;
149
150 std::function<void(void)> active_window_changed_callback_;
151
152 std::map<std::string, chatroom_log>* log_;
153
154 std::set<std::string> available_users_;
155
156public:
157 /** Static type getter that does not rely on the widget being constructed. */
158 static const std::string& type();
159
160private:
161 /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
162 virtual const std::string& get_control_type() const override;
163
164 /** See @ref container_base::set_self_active. */
165 virtual void set_self_active(const bool /*active*/) override
166 {
167 /* DO NOTHING */
168 }
169
170 void chat_input_keypress_callback(const SDL_Keycode key);
171
172 void append_to_chatbox(const std::string& text, const bool force_scroll = false);
173
174 void append_to_chatbox(const std::string& text, std::size_t id, const bool force_scroll = false);
175
176 /** @returns true if the whisper window for "name" is the active window. */
177 bool whisper_window_active(const std::string& name);
178
179 /** @returns true if the room window for "room" is the active window. */
180 bool room_window_active(const std::string& room);
181
182 /** Mark the whisper window for "name" as having one more pending message. */
183 void increment_waiting_whispers(const std::string& name);
184
185 /** Mark the room window for "room" as having one more pending message. */
186 void increment_waiting_messages(const std::string& room);
187
188 /** Add a whisper message to the whisper window. */
189 void add_whisper_window_whisper(const std::string& sender,
190 const std::string& message);
191
192 /**
193 * Add a whisper message to the current window which is not the whisper window for "name".
194 */
195 void add_active_window_whisper(const std::string& sender,
196 const std::string& message,
197 const bool force_scroll = false);
198
199 /** Add a message to the window for room "room". */
200 void add_room_window_message(const std::string& room,
201 const std::string& sender,
202 const std::string& message);
203
204 /** Add a message to the window for room "room". */
205 void add_active_window_message(const std::string& sender,
206 const std::string& message,
207 const bool force_scroll = false);
208
209 void close_window(std::size_t idx);
210
211public:
212 /** Inherited form @ref chat_handler */
213 virtual void send_chat_message(const std::string& message, bool allies_only) override;
214
215 virtual void clear_messages() override;
216
217 /**
218 * Switch to the window given by a valid pointer (e.g. received from a call
219 * to *_window_open)
220 */
222
223 void switch_to_window(std::size_t id);
224
226
227 /**
228 * Check if a room window for "room" is open, if open_new is true
229 * then it will be created if not found. If allow_close is false, the
230 * 'close' button will be disabled.
231 * @return valid ptr if the window was found or added, null otherwise
232 */
233 lobby_chat_window* room_window_open(const std::string& room,
234 const bool open_new, const bool allow_close = true);
235
236 /**
237 * Check if a whisper window for user "name" is open, if open_new is true
238 * then it will be created if not found.
239 * @return valid ptr if the window was found or added, null otherwise
240 */
241 lobby_chat_window* whisper_window_open(const std::string& name, bool open_new);
242
243 /**
244 * Helper function to find and open a new window, used by *_window_open
245 */
246 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);
247
248 void close_window_button_callback(const std::string& room_name, bool& handled, bool& halt);
249
250 void process_message(const ::config& data, bool whisper = false);
251
252 void process_network_data(const ::config& data);
253
254private:
256};
257
258// }---------- DEFINITION ---------{
259
261{
262 explicit chatbox_definition(const config& cfg);
263
265 {
266 explicit resolution(const config& cfg);
267
269 };
270};
271
272// }---------- BUILDER -----------{
273
274namespace implementation
275{
276
278{
279public:
280 explicit builder_chatbox(const config& cfg);
281
283
284 virtual std::unique_ptr<widget> build() const override;
285
286private:
287};
288
289} // namespace implementation
290
291// }------------ END --------------
292
293} // namespace gui2
double t
std::vector< std::string > names
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
std::map< std::string, chatroom_log > * log_
Definition chatbox.hpp:152
bool room_window_active(const std::string &room)
Definition chatbox.cpp:373
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:379
text_box * chat_input_
Definition chatbox.hpp:144
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:392
virtual void clear_messages() override
Definition chatbox.cpp:259
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:510
void append_to_chatbox(const std::string &text, const bool force_scroll=false)
Definition chatbox.cpp:218
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition chatbox.hpp:74
void set_available_users(const std::set< std::string > &names)
Supplies an additional set of names to offer for auto-completion, on top of whatever mp::get_lobby_in...
Definition chatbox.hpp:99
virtual void user_relation_changed(const std::string &name) override
Inherited form chat_handler.
Definition chatbox.cpp:267
virtual void send_chat_message(const std::string &message, bool allies_only) override
Inherited form chat_handler.
Definition chatbox.cpp:253
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:567
void close_window(std::size_t idx)
Definition chatbox.cpp:518
void add_whisper_window_whisper(const std::string &sender, const std::string &message)
Add a whisper message to the whisper window.
Definition chatbox.cpp:498
virtual void add_chat_room_message_sent(const std::string &room, const std::string &message) override
Inherited form chat_handler.
Definition chatbox.cpp:326
multi_page * chat_log_container_
Definition chatbox.hpp:142
void process_message(const ::config &data, bool whisper=false)
Definition chatbox.cpp:575
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:385
void switch_to_window(lobby_chat_window *t)
Switch to the window given by a valid pointer (e.g.
Definition chatbox.cpp:130
void close_window_button_callback(const std::string &room_name, bool &handled, bool &halt)
Definition chatbox.cpp:455
std::size_t active_window_
Definition chatbox.hpp:148
virtual void set_self_active(const bool) override
See container_base::set_self_active.
Definition chatbox.hpp:165
virtual void add_whisper_sent(const std::string &receiver, const std::string &message) override
Inherited form chat_handler.
Definition chatbox.cpp:290
void process_network_data(const ::config &data)
Definition chatbox.cpp:625
void increment_waiting_whispers(const std::string &name)
Mark the whisper window for "name" as having one more pending message.
Definition chatbox.cpp:468
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:150
std::set< std::string > available_users_
Definition chatbox.hpp:154
listbox * roomlistbox_
Definition chatbox.hpp:140
void increment_waiting_messages(const std::string &room)
Mark the room window for "room" as having one more pending message.
Definition chatbox.cpp:482
void signal_handler_receive_keyboard_focus(const event::ui_event event)
Definition chatbox.cpp:634
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void send_to_server(const ::config &cfg) override
Definition chatbox.cpp:463
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:341
void load_log(std::map< std::string, chatroom_log > &log, bool show_lobby)
Definition chatbox.cpp:90
virtual void add_whisper_received(const std::string &sender, const std::string &message) override
Inherited form chat_handler.
Definition chatbox.cpp:302
virtual void add_chat_message(const std::chrono::system_clock::time_point &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:274
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
void finalize_setup()
Initializes the internal sub-widget pointers.
Definition chatbox.cpp:73
void chat_input_keypress_callback(const SDL_Keycode key)
Definition chatbox.cpp:149
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:553
void active_window_changed()
Definition chatbox.cpp:115
std::vector< lobby_chat_window > open_windows_
Definition chatbox.hpp:146
bool whisper_window_active(const std::string &name)
Definition chatbox.cpp:367
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:41
A widget that allows the user to input text in single line.
Definition text_box.hpp:125
const config * cfg
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:188
virtual std::unique_ptr< widget > build() const override
Definition chatbox.cpp:669
virtual std::unique_ptr< widget > build() const=0
lobby_chat_window(const std::string &name, bool whisper)
Definition chatbox.hpp:44
#define f