The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mp_connect.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "game_preferences.hpp"
20 #include "gui/auxiliary/field.hpp"
21 #include "gui/dialogs/dialog.hpp"
22 #include "gui/widgets/button.hpp"
23 #ifdef GUI2_EXPERIMENTAL_LISTBOX
24 #include "gui/widgets/list.hpp"
25 #else
26 #include "gui/widgets/listbox.hpp"
27 #endif
28 #include "gui/widgets/settings.hpp"
29 
30 #include "utils/functional.hpp"
31 #include "video.hpp"
32 
33 namespace gui2
34 {
35 
36 namespace
37 {
38 
39 /*WIKI
40  * @page = GUIWindowDefinitionWML
41  * @order = 2_mp_server_list
42  *
43  * == Multiplayer server list ==
44  *
45  * This shows the dialog with a list of predefined multiplayer servers.
46  *
47  * @begin{table}{dialog_widgets}
48  *
49  * server_list & & listbox & m &
50  * Listbox with the predefined servers to connect to. $
51  *
52  * -name & & control & o &
53  * Widget which shows the name of the server. $
54  *
55  * -address & & control & m &
56  * The address/host_name of the server. $
57  *
58  * @end{table}
59  */
60 
61 class tmp_server_list : public tdialog
62 {
63 public:
64  tmp_server_list() : host_name_()
65  {
66  }
67 
68  const std::string& host_name() const
69  {
70  return host_name_;
71  }
72 
73 private:
75 
76  /** Inherited from tdialog, implemented by REGISTER_DIALOG. */
77  virtual const std::string& window_id() const;
78 
79  /** Inherited from tdialog. */
80  void pre_show(twindow& window);
81 
82  /** Inherited from tdialog. */
83  void post_show(twindow& window);
84 };
85 
86 REGISTER_DIALOG(mp_server_list)
87 
88 void tmp_server_list::pre_show(twindow& window)
89 {
90  set_restore(true);
91 
92  tlistbox& list = find_widget<tlistbox>(&window, "server_list", false);
93 
94  window.keyboard_capture(&list);
95 
96  const std::vector<game_config::server_info>& pref_servers
98 
99  for(const auto & server : pref_servers)
100  {
101 
102  std::map<std::string, string_map> data;
103  string_map item;
104 
105  item["label"] = server.name;
106  data.insert(std::make_pair("name", item));
107 
108  item["label"] = server.address;
109  data.insert(std::make_pair("address", item));
110 
111  list.add_row(data);
112  }
113 }
114 
115 void tmp_server_list::post_show(twindow& window)
116 {
117  if(get_retval() == twindow::OK) {
118 
119  const tlistbox& list
120  = find_widget<const tlistbox>(&window, "server_list", false);
121 
122  const tgrid* row = list.get_row_grid(list.get_selected_row());
123  assert(row);
124 
125  host_name_ = find_widget<const tcontrol>(row, "address", false).label();
126  }
127 }
128 
129 } // namespace
130 
131 /*WIKI
132  * @page = GUIWindowDefinitionWML
133  * @order = 2_mp_connect
134  *
135  * == Multiplayer connect ==
136  *
137  * This shows the dialog to the MP server to connect to.
138  *
139  * @begin{table}{dialog_widgets}
140  *
141  * start_table & & text_box & m &
142  * The name of the server to connect to. $
143  *
144  * list & & button & o &
145  * Shows a dialog with a list of predefined servers to connect to. $
146  *
147  * @end{table}
148  */
149 
150 REGISTER_DIALOG(mp_connect)
151 
152 static void
153 show_server_list(CVideo& video, twindow& window, tfield_text* host_name)
154 {
155  assert(host_name);
156 
157  tmp_server_list dlg;
158  dlg.show(video);
159 
160  if(dlg.get_retval() == twindow::OK) {
161  host_name->set_widget_value(window, dlg.host_name());
162  }
163 }
164 
166  : host_name_(register_text("host_name",
167  true,
170  true))
171 {
172  set_restore(true);
173 }
174 
176 {
177  assert(host_name_);
178 
179  // Set view list callback button.
180  if(tbutton* button = find_widget<tbutton>(&window, "list", false, false)) {
181 
183  std::bind(show_server_list,
184  std::ref(window.video()),
185  std::ref(window),
186  host_name_));
187  }
188 }
189 
191 {
192  return new tmp_server_list();
193 }
194 
195 } // namespace gui2
void pre_show(twindow &window)
Inherited from tdialog.
Definition: mp_connect.cpp:175
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
std::string host_name_
Definition: mp_connect.cpp:74
tfield_text * host_name_
The host name of the selected servef.
Definition: mp_connect.hpp:39
CVideo & video()
Definition: window.hpp:411
Definition: video.hpp:58
std::string network_host()
REGISTER_DIALOG(label_settings)
int get_retval(const std::string &retval_id, const int retval, const std::string &id)
Returns the return value for a widget.
Definition: helper.cpp:131
void connect_signal_mouse_left_click(tdispatcher &dispatcher, const tsignal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.hpp:710
void set_network_host(const std::string &host)
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
-file util.hpp
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
Simple push button.
Definition: button.hpp:32
Dialog is closed with ok button.
Definition: window.hpp:125
This file contains the settings handling of the widget library.
Abstract base class for all dialogs.
Definition: dialog.hpp:121
void set_restore(const bool restore)
Definition: dialog.hpp:171
const std::vector< game_config::server_info > & server_list()
Modify, read and display user preferences.
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
static tdialog * mp_server_list_for_unit_test()
The unit test needs to be able to test the tmp_connect dialog.
Definition: mp_connect.cpp:190
GLenum GLenum GLvoid * row
Definition: glew.h:3805
GLenum GLint ref
Definition: glew.h:1813
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Specialized field class for text.
Definition: field.hpp:588
static void show_server_list(CVideo &video, twindow &window, tfield_text *host_name)
Definition: mp_connect.cpp:153