The Battle for Wesnoth  1.17.0-dev
mp_connect.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
19 
20 namespace game_config
21 {
22 struct server_info;
23 }
24 
25 namespace gui2
26 {
27 
28 class listbox;
29 
30 namespace dialogs
31 {
32 
33 /**
34  * @ingroup GUIWindowDefinitionWML
35  *
36  * This shows the dialog to the MP server to connect to.
37  * Key |Type |Mandatory|Description
38  * ------------------|--------------|---------|-----------
39  * start_table | text_box |yes |The name of the server to connect to.
40  * list | @ref button |no |Shows a dialog with a list of predefined servers to connect to.
41  */
42 class mp_connect : public modal_dialog
43 {
44  /** The unit test needs to be able to test the mp_connect dialog. */
45  friend modal_dialog* unit_test_mp_server_list();
46 
47 public:
48  mp_connect();
49 
50  /** The execute function. See @ref modal_dialog for more information. */
52 
53 private:
54  virtual const std::string& window_id() const override;
55 
56  virtual void pre_show(window& window) override;
57 
58  // Signal handlers
59 
60  void on_address_change();
61  void on_server_add();
62  void on_server_delete();
63  void on_server_select();
64 
65  // Auxiliary functions
66 
68  using server_list = std::vector<server_info>;
69 
70  void insert_into_server_listbox(listbox& listbox, const server_info& srv, int pos = -1);
71 
72  void select_first_match();
73 
74  class selection
75  {
76  public:
77  selection(mp_connect* owner, int row = -1)
78  : owner_(owner)
79  , row_(row)
80  {
81  }
82 
83  bool valid() const
84  {
85  return owner_ && row_ >= 0;
86  }
87 
88  bool user_defined() const
89  {
90  // An invalid selection is the same as one from the read-only list of
91  // built-in servers for interaction purposes since it can't be written to.
92  return valid() && std::size_t(row_) >= owner_->builtin_servers_.size();
93  }
94 
95  unsigned row() const;
96 
97  std::size_t relative_index() const;
98 
99  server_list& parent_list() const;
100 
101  server_info& get();
102 
103  private:
105  int row_;
106 
107  void must_be_valid() const
108  {
109  if(!valid()) {
110  throw std::out_of_range{"Invalid MP server selection"};
111  }
112  }
113  };
114 
115  selection current_selection();
116 
117  std::array<server_list*, 2> server_lists();
118 
119  /** The host name of the selected server. */
121 
124 };
125 
126 } // namespace dialogs
127 } // namespace gui2
selection(mp_connect *owner, int row=-1)
Definition: mp_connect.hpp:77
field_text * host_name_
The host name of the selected server.
Definition: mp_connect.hpp:120
Generic file dialog.
Definition: field-fwd.hpp:23
The listbox class.
Definition: listbox.hpp:43
This shows the dialog to the MP server to connect to.
Definition: mp_connect.hpp:42
Various uncategorised dialogs.
Game configuration data as global variables.
Definition: build_info.cpp:59
std::vector< server_info > server_list
Definition: mp_connect.hpp:68
Abstract base class for all modal dialogs.
Specialized field class for text.
Definition: field.hpp:593
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65