The Battle for Wesnoth  1.15.0-dev
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"
17 #include "chat_log.hpp"
21 
22 #include <map>
23 #include <string>
24 
25 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 
88  {
89  lobby_info_ = &i;
90  }
91 
93  {
94  wesnothd_connection_ = &c;
95  }
96 
97  void load_log(std::map<std::string, chatroom_log>& log, bool show_lobby);
98 
99 protected:
100  /**
101  * Initializes the internal sub-widget pointers.
102  * Should be called after initializing the widget's grid.
103  */
104  void finalize_setup();
105 
106  /** Inherited form @ref chat_handler */
107  virtual void user_relation_changed(const std::string& name) override;
108 
109  /** Inherited form @ref chat_handler */
110  virtual void add_chat_message(const std::time_t& time,
111  const std::string& speaker,
112  int side,
113  const std::string& message,
116 
117  /** Inherited form @ref chat_handler */
118  virtual void add_whisper_sent(const std::string& receiver,
119  const std::string& message) override;
120 
121  /** Inherited form @ref chat_handler */
122  virtual void add_whisper_received(const std::string& sender,
123  const std::string& message) override;
124 
125  /** Inherited form @ref chat_handler */
126  virtual void add_chat_room_message_sent(const std::string& room,
127  const std::string& message) override;
128 
129  /** Inherited form @ref chat_handler */
130  virtual void add_chat_room_message_received(const std::string& room,
131  const std::string& speaker,
132  const std::string& message) override;
133 
134 private:
136 
138 
140 
141  std::vector<lobby_chat_window> open_windows_;
142 
143  std::size_t active_window_;
144 
145  std::function<void(void)> active_window_changed_callback_;
146 
148 
150 
151  std::map<std::string, chatroom_log>* log_;
152 
153  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
154  virtual const std::string& get_control_type() const override;
155 
156  /** See @ref container_base::set_self_active. */
157  virtual void set_self_active(const bool /*active*/) override
158  {
159  /* DO NOTHING */
160  }
161 
162  void chat_input_keypress_callback(const SDL_Keycode key);
163 
164  void append_to_chatbox(const std::string& text, const bool force_scroll = false);
165 
166  void append_to_chatbox(const std::string& text, std::size_t id, const bool force_scroll = false);
167 
168  /** @returns true if the whisper window for "name" is the active window. */
169  bool whisper_window_active(const std::string& name);
170 
171  /** @returns true if the room window for "room" is the active window. */
172  bool room_window_active(const std::string& room);
173 
174  /** Mark the whisper window for "name" as having one more pending message. */
175  void increment_waiting_whispers(const std::string& name);
176 
177  /** Mark the room window for "room" as having one more pending message. */
178  void increment_waiting_messages(const std::string& room);
179 
180  /** Add a whisper message to the whisper window. */
181  void add_whisper_window_whisper(const std::string& sender,
182  const std::string& message);
183 
184  /**
185  * Add a whisper message to the current window which is not the whisper window for "name".
186  */
187  void add_active_window_whisper(const std::string& sender,
188  const std::string& message,
189  const bool force_scroll = false);
190 
191  /** Add a message to the window for room "room". */
192  void add_room_window_message(const std::string& room,
193  const std::string& sender,
194  const std::string& message);
195 
196  /** Add a message to the window for room "room". */
197  void add_active_window_message(const std::string& sender,
198  const std::string& message,
199  const bool force_scroll = false);
200 
201  void close_window(std::size_t idx);
202 
203 public:
204  /** Inherited form @ref chat_handler */
205  virtual void send_chat_message(const std::string& message, bool allies_only) override;
206 
207  /**
208  * Switch to the window given by a valid pointer (e.g. received from a call
209  * to *_window_open)
210  */
211  void switch_to_window(lobby_chat_window* t);
212 
213  void switch_to_window(std::size_t id);
214 
215  void active_window_changed();
216 
217  /**
218  * Get the room* corresponding to the currently active window, or nullptr
219  * if a whisper window is active at the moment
220  */
221  mp::room_info* active_window_room();
222 
223  /**
224  * Check if a room window for "room" is open, if open_new is true
225  * then it will be created if not found. If allow_close is false, the
226  * 'close' button will be disabled.
227  * @return valid ptr if the window was found or added, null otherwise
228  */
229  lobby_chat_window* room_window_open(const std::string& room,
230  const bool open_new, const bool allow_close = true);
231 
232  /**
233  * Check if a whisper window for user "name" is open, if open_new is true
234  * then it will be created if not found.
235  * @return valid ptr if the window was found or added, null otherwise
236  */
237  lobby_chat_window* whisper_window_open(const std::string& name, bool open_new);
238 
239  /**
240  * Helper function to find and open a new window, used by *_window_open
241  */
242  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);
243 
244  void close_window_button_callback(std::string room_name, bool& handled, bool& halt);
245 
246  void process_room_join(const ::config& data);
247 
248  void process_room_part(const ::config& data);
249 
250  void process_room_query_response(const ::config& data);
251 
252  void process_message(const ::config& data, bool whisper = false);
253 
254  void process_network_data(const ::config& data);
255 
256 private:
257  void signal_handler_receive_keyboard_focus(const event::ui_event event);
258 };
259 
260 // }---------- DEFINITION ---------{
261 
263 {
264  explicit chatbox_definition(const config& cfg);
265 
267  {
268  explicit resolution(const config& cfg);
269 
271  };
272 };
273 
274 // }---------- BUILDER -----------{
275 
276 namespace implementation
277 {
278 
280 {
281 public:
282  explicit builder_chatbox(const config& cfg);
283 
285 
286  widget* build() const;
287 
288 private:
289 };
290 
291 } // namespace implementation
292 
293 // }------------ END --------------
294 
295 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
wesnothd_connection * wesnothd_connection_
Definition: chatbox.hpp:149
virtual widget * build() const =0
multi_page * chat_log_container_
Definition: chatbox.hpp:137
void set_lobby_info(mp::lobby_info &i)
Definition: chatbox.hpp:87
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:157
Base class for all widgets.
Definition: widget.hpp:47
std::map< std::string, chatroom_log > * log_
Definition: chatbox.hpp:151
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:62
std::vector< lobby_chat_window > open_windows_
Definition: chatbox.hpp:141
lobby_chat_window(const std::string &name, bool whisper)
Definition: chatbox.hpp:44
std::size_t active_window_
Definition: chatbox.hpp:143
text_box * chat_input_
Definition: chatbox.hpp:139
Class for a single line text area.
Definition: text_box.hpp:121
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:42
void set_active_window_changed_callback(const std::function< void(void)> &f)
Definition: chatbox.hpp:82
mp::lobby_info * lobby_info_
Definition: chatbox.hpp:147
A class that represents a TCP/IP connection to the wesnothd server.
std::function< void(void)> active_window_changed_callback_
Definition: chatbox.hpp:145
virtual bool get_active() const override
See styled_widget::get_active.
Definition: chatbox.hpp:68
listbox * roomlistbox_
Definition: chatbox.hpp:135
std::size_t i
Definition: function.cpp:933
This class represents the information a client has about a room.
Definition: lobby_data.hpp:67
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: chatbox.hpp:74
A generic container base class.
#define f
double t
Definition: astarsearch.cpp:63
The multi page class.
Definition: multi_page.hpp:35
void set_wesnothd_connection(wesnothd_connection &c)
Definition: chatbox.hpp:92
point resolution()
Definition: general.cpp:375
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:47