The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
chatbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include "chat_events.hpp"
20 
21 #include <string>
22 
23 class config;
25 
26 namespace gui2
27 {
28 
29 // ------------ WIDGET -----------{
30 
31 class listbox;
32 class multi_page;
33 class text_box;
34 
35 namespace implementation
36 {
37  struct builder_chatbox;
38 }
39 
41 {
43  : name(name), whisper(whisper), pending_messages(0)
44  {
45  }
46 
48  bool whisper;
50 };
51 
53 {
55 
56 public:
57  explicit chatbox(const implementation::builder_chatbox& builder);
58 
59  /** See @ref styled_widget::set_active. */
60  virtual void set_active(const bool /*active*/) override
61  {
62  /* DO NOTHING */
63  }
64 
65  /** See @ref styled_widget::get_active. */
66  virtual bool get_active() const override
67  {
68  return true;
69  }
70 
71  /** See @ref styled_widget::get_state. */
72  virtual unsigned get_state() const override
73  {
74  return 0;
75  }
76 
77  void send_to_server(const ::config& cfg) override;
78 
79  /** @todo: remove */
80  void set_active_window_changed_callback(const std::function<void(void)>& f)
81  {
83  }
84 
86  {
87  lobby_info_ = &i;
88  }
89 
91  {
93  }
94 
95 protected:
96  /**
97  * Initializes the internal sub-widget pointers.
98  * Should be called after initializing the widget's grid.
99  */
100  void finalize_setup();
101 
102  /** Inherited form @ref chat_handler */
103  virtual void user_relation_changed(const std::string& name) override;
104 
105  /** Inherited form @ref chat_handler */
106  virtual void add_chat_message(const time_t& time,
107  const std::string& speaker,
108  int side,
109  const std::string& message,
112 
113  /** Inherited form @ref chat_handler */
114  virtual void add_whisper_sent(const std::string& receiver,
115  const std::string& message) override;
116 
117  /** Inherited form @ref chat_handler */
118  virtual void add_whisper_received(const std::string& sender,
119  const std::string& message) override;
120 
121  /** Inherited form @ref chat_handler */
122  virtual void add_chat_room_message_sent(const std::string& room,
123  const std::string& message) override;
124 
125  /** Inherited form @ref chat_handler */
126  virtual void add_chat_room_message_received(const std::string& room,
127  const std::string& speaker,
128  const std::string& message) override;
129 
130 private:
132 
134 
136 
137  std::vector<lobby_chat_window> open_windows_;
138 
140 
141  std::function<void(void)> active_window_changed_callback_;
142 
144 
146 
147  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
148  virtual const std::string& get_control_type() const override;
149 
150  /** See @ref container_base::set_self_active. */
151  virtual void set_self_active(const bool /*active*/) override
152  {
153  /* DO NOTHING */
154  }
155 
156  void chat_input_keypress_callback(const SDL_Keycode key);
157 
158  void append_to_chatbox(const std::string& text, const bool force_scroll = false);
159 
160  void append_to_chatbox(const std::string& text, size_t id, const bool force_scroll = false);
161 
162  /** @returns true if the whisper window for "name" is the active window. */
164 
165  /** @returns true if the room window for "room" is the active window. */
166  bool room_window_active(const std::string& room);
167 
168  /** Mark the whisper window for "name" as having one more pending message. */
170 
171  /** Mark the room window for "room" as having one more pending message. */
172  void increment_waiting_messages(const std::string& room);
173 
174  /** Add a whisper message to the whisper window. */
175  void add_whisper_window_whisper(const std::string& sender,
176  const std::string& message);
177 
178  /**
179  * Add a whisper message to the current window which is not the whisper window for "name".
180  */
181  void add_active_window_whisper(const std::string& sender,
182  const std::string& message,
183  const bool force_scroll = false);
184 
185  /** Add a message to the window for room "room". */
186  void add_room_window_message(const std::string& room,
187  const std::string& sender,
188  const std::string& message);
189 
190  /** Add a message to the window for room "room". */
191  void add_active_window_message(const std::string& sender,
192  const std::string& message,
193  const bool force_scroll = false);
194 
195  void close_window(size_t idx);
196 
197 public:
198  /** Inherited form @ref chat_handler */
199  virtual void send_chat_message(const std::string& message, bool allies_only) override;
200 
201  /**
202  * Switch to the window given by a valid pointer (e.g. received from a call
203  * to *_window_open)
204  */
206 
207  void switch_to_window(size_t id);
208 
209  void active_window_changed();
210 
211  /**
212  * Get the room* corresponding to the currently active window, or nullptr
213  * if a whisper window is active at the moment
214  */
216 
217  /**
218  * Check if a room window for "room" is open, if open_new is true
219  * then it will be created if not found. If allow_close is false, the
220  * 'close' button will be disabled.
221  * @return valid ptr if the window was found or added, null otherwise
222  */
224  const bool open_new, const bool allow_close = true);
225 
226  /**
227  * Check if a whisper window for user "name" is open, if open_new is true
228  * then it will be created if not found.
229  * @return valid ptr if the window was found or added, null otherwise
230  */
231  lobby_chat_window* whisper_window_open(const std::string& name, bool open_new);
232 
233  /**
234  * Helper function to find and open a new window, used by *_window_open
235  */
236  lobby_chat_window* find_or_create_window(const std::string& name, const bool whisper, const bool open_new, const bool allow_close);
237 
238  void close_window_button_callback(std::string room_name, bool& handled, bool& halt);
239 
240  void process_room_join(const ::config& data);
241 
242  void process_room_part(const ::config& data);
243 
244  void process_room_query_response(const ::config& data);
245 
246  void process_message(const ::config& data, bool whisper = false);
247 
248  void process_network_data(const ::config& data);
249 
250 private:
252 };
253 
254 // }---------- DEFINITION ---------{
255 
257 {
258  explicit chatbox_definition(const config& cfg);
259 
261  {
262  explicit resolution(const config& cfg);
263 
265  };
266 };
267 
268 // }---------- BUILDER -----------{
269 
270 namespace implementation
271 {
272 
274 {
275 public:
276  explicit builder_chatbox(const config& cfg);
277 
279 
280  widget* build() const;
281 
282 private:
283 };
284 
285 } // namespace implementation
286 
287 // }------------ END --------------
288 
289 } // namespace gui2
void switch_to_window(lobby_chat_window *t)
Switch to the window given by a valid pointer (e.g.
Definition: chatbox.cpp:109
void active_window_changed()
Definition: chatbox.cpp:94
Base class of a resolution, contains the common keys for a resolution.
wesnothd_connection * wesnothd_connection_
Definition: chatbox.hpp:145
void increment_waiting_messages(const std::string &room)
Mark the room window for "room" as having one more pending message.
Definition: chatbox.cpp:462
virtual widget * build() const =0
std::vector< char_t > string
multi_page * chat_log_container_
Definition: chatbox.hpp:133
void set_lobby_info(mp::lobby_info &i)
Definition: chatbox.hpp:85
bool room_window_active(const std::string &room)
Definition: chatbox.cpp:351
size_t active_window_
Definition: chatbox.hpp:139
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:545
void signal_handler_receive_keyboard_focus(const event::ui_event event)
Definition: chatbox.cpp:736
Main class to show messages to the user.
Definition: message.hpp:34
virtual void set_self_active(const bool) override
See container_base::set_self_active.
Definition: chatbox.hpp:151
chatbox_definition(const config &cfg)
Definition: chatbox.cpp:746
Base class for all widgets.
Definition: widget.hpp:48
This class represents the collective information the client has about the players and games on the se...
Definition: lobby_info.hpp:30
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: chatbox.hpp:60
std::vector< lobby_chat_window > open_windows_
Definition: chatbox.hpp:137
void process_room_query_response(const ::config &data)
Definition: chatbox.cpp:645
void send_to_server(const ::config &cfg) override
Definition: chatbox.cpp:441
lobby_chat_window(const std::string &name, bool whisper)
Definition: chatbox.hpp:42
void close_window(size_t idx)
Definition: chatbox.cpp:498
bool whisper_window_active(const std::string &name)
Definition: chatbox.cpp:345
text_box * chat_input_
Definition: chatbox.hpp:135
Class for a single line text area.
Definition: text_box.hpp:121
void process_room_part(const ::config &data)
Definition: chatbox.cpp:624
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:42
void process_message(const ::config &data, bool whisper=false)
Definition: chatbox.cpp:690
void set_active_window_changed_callback(const std::function< void(void)> &f)
Definition: chatbox.hpp:80
void close_window_button_callback(std::string room_name, bool &handled, bool &halt)
Definition: chatbox.cpp:430
mp::lobby_info * lobby_info_
Definition: chatbox.hpp:143
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:362
A class that represents a TCP/IP connection to the wesnothd server.
virtual void add_chat_message(const 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:232
virtual void send_chat_message(const std::string &message, bool allies_only) override
Inherited form chat_handler.
Definition: chatbox.cpp:217
std::function< void(void)> active_window_changed_callback_
Definition: chatbox.hpp:141
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:559
virtual bool get_active() const override
See styled_widget::get_active.
Definition: chatbox.hpp:66
chatbox(const implementation::builder_chatbox &builder)
Definition: chatbox.cpp:57
void process_room_join(const ::config &data)
Definition: chatbox.cpp:577
listbox * roomlistbox_
Definition: chatbox.hpp:131
mp::room_info * active_window_room()
Get the room* corresponding to the currently active window, or nullptr if a whisper window is active ...
Definition: chatbox.cpp:567
void process_network_data(const ::config &data)
Definition: chatbox.cpp:721
virtual void user_relation_changed(const std::string &name) override
Inherited form chat_handler.
Definition: chatbox.cpp:225
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void increment_waiting_whispers(const std::string &name)
Mark the whisper window for "name" as having one more pending message.
Definition: chatbox.cpp:448
void chat_input_keypress_callback(const SDL_Keycode key)
Definition: chatbox.cpp:125
virtual void add_whisper_received(const std::string &sender, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:266
size_t i
Definition: function.cpp:933
This class represents the information a client has about a room.
Definition: lobby_data.hpp:66
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: chatbox.hpp:72
A generic container base class.
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:312
void append_to_chatbox(const std::string &text, const bool force_scroll=false)
Definition: chatbox.cpp:190
#define f
double t
Definition: astarsearch.cpp:64
The multi page class.
Definition: multi_page.hpp:35
lobby_chat_window * find_or_create_window(const std::string &name, const bool whisper, const bool open_new, const bool allow_close)
Helper function to find and open a new window, used by *_window_open.
Definition: chatbox.cpp:367
virtual void add_chat_room_message_sent(const std::string &room, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:292
void set_wesnothd_connection(wesnothd_connection &c)
Definition: chatbox.hpp:90
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
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:490
virtual void add_whisper_sent(const std::string &receiver, const std::string &message) override
Inherited form chat_handler.
Definition: chatbox.cpp:252
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mock_char c
void finalize_setup()
Initializes the internal sub-widget pointers.
Definition: chatbox.cpp:77
std::shared_ptr< builder_grid > builder_grid_ptr
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:357
Contains the implementation details for lexical_cast and shouldn't be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:55
builder_chatbox(const config &cfg)
Definition: chatbox.cpp:768
void add_whisper_window_whisper(const std::string &sender, const std::string &message)
Add a whisper message to the whisper window.
Definition: chatbox.cpp:478