The Battle for Wesnoth  1.15.1+dev
mp_connect.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "preferences/game.hpp"
20 #include "gui/auxiliary/field.hpp"
22 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/listbox.hpp"
24 #include "gui/widgets/settings.hpp"
25 
26 #include "utils/functional.hpp"
27 
28 namespace gui2
29 {
30 namespace dialogs
31 {
32 
33 namespace
34 {
35 
36 /*WIKI
37  * @page = GUIWindowDefinitionWML
38  * @order = 2_mp_server_list
39  *
40  * == Multiplayer server list ==
41  *
42  * This shows the dialog with a list of predefined multiplayer servers.
43  *
44  * @begin{table}{dialog_widgets}
45  *
46  * server_list & & listbox & m &
47  * Listbox with the predefined servers to connect to. $
48  *
49  * -name & & styled_widget & o &
50  * Widget which shows the name of the server. $
51  *
52  * -address & & styled_widget & m &
53  * The address/host_name of the server. $
54  *
55  * @end{table}
56  */
57 
58 class mp_server_list : public modal_dialog
59 {
60 public:
61  mp_server_list() : host_name_()
62  {
63  }
64 
65  const std::string& host_name() const
66  {
67  return host_name_;
68  }
69 
70 private:
71  std::string host_name_;
72 
73  /** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
74  virtual const std::string& window_id() const override;
75 
76  /** Inherited from modal_dialog. */
77  virtual void pre_show(window& window) override;
78 
79  /** Inherited from modal_dialog. */
80  virtual void post_show(window& window) override;
81 };
82 
83 REGISTER_DIALOG(mp_server_list)
84 
85 void mp_server_list::pre_show(window& window)
86 {
87  set_restore(true);
88 
89  listbox& list = find_widget<listbox>(&window, "server_list", false);
90 
91  window.keyboard_capture(&list);
92 
93  const std::vector<game_config::server_info>& pref_servers
95 
96  for(const auto & server : pref_servers)
97  {
98 
99  std::map<std::string, string_map> data;
101 
102  item["label"] = server.name;
103  data.emplace("name", item);
104 
105  item["label"] = server.address;
106  data.emplace("address", item);
107 
108  list.add_row(data);
109  }
110 }
111 
112 void mp_server_list::post_show(window& window)
113 {
114  if(get_retval() == retval::OK) {
115 
116  const listbox& list
117  = find_widget<const listbox>(&window, "server_list", false);
118 
119  const grid* row = list.get_row_grid(list.get_selected_row());
120  assert(row);
121 
122  host_name_ = find_widget<const styled_widget>(row, "address", false).get_label();
123  }
124 }
125 
126 } // namespace
127 
128 /*WIKI
129  * @page = GUIWindowDefinitionWML
130  * @order = 2_mp_connect
131  *
132  * == Multiplayer connect ==
133  *
134  * This shows the dialog to the MP server to connect to.
135  *
136  * @begin{table}{dialog_widgets}
137  *
138  * start_table & & text_box & m &
139  * The name of the server to connect to. $
140  *
141  * list & & button & o &
142  * Shows a dialog with a list of predefined servers to connect to. $
143  *
144  * @end{table}
145  */
146 
147 REGISTER_DIALOG(mp_connect)
148 
149 static void
150 show_server_list(window& window, field_text* host_name)
151 {
152  assert(host_name);
153 
154  mp_server_list dlg;
155  dlg.show();
156 
157  if(dlg.get_retval() == retval::OK) {
158  host_name->set_widget_value(window, dlg.host_name());
159  }
160 }
161 
163  : host_name_(register_text("host_name",
164  true,
167  true))
168 {
169  set_restore(true);
170 }
171 
172 void mp_connect::pre_show(window& win)
173 {
174  assert(host_name_);
175 
176  // Set view list callback button.
177  if(button* btn = find_widget<button>(&win, "list", false, false)) {
178 
180  std::bind(show_server_list, std::ref(win), host_name_));
181  }
182 }
183 
185 {
186  return new mp_server_list();
187 }
188 
189 } // namespace dialogs
190 } // namespace gui2
std::string host_name_
Definition: mp_connect.cpp:71
static modal_dialog * mp_server_list_for_unit_test()
The unit test needs to be able to test the mp_connect dialog.
Definition: mp_connect.cpp:184
std::string network_host()
Definition: game.cpp:403
static void show_server_list(window &window, field_text *host_name)
Definition: mp_connect.cpp:150
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:136
field_text * host_name_
The host name of the selected servef.
Definition: mp_connect.hpp:43
void set_network_host(const std::string &host)
Definition: game.cpp:413
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
Generic file dialog.
Definition: field-fwd.hpp:22
void set_widget_value(window &window, CT value)
Sets the value of the field.
Definition: field.hpp:358
This file contains the settings handling of the widget library.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:233
const std::vector< game_config::server_info > & server_list()
Definition: game.cpp:386
Modify, read and display user preferences.
Various uncategorised dialogs.
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition: mp_connect.cpp:172
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
bool grid()
Definition: general.cpp:505
Abstract base class for all modal dialogs.
Simple push button.
Definition: button.hpp:35
Dialog was closed with the OK button.
Definition: retval.hpp:34
Specialized field class for text.
Definition: field.hpp:592
void set_restore(const bool restore)
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371