The Battle for Wesnoth  1.15.11+dev
mp_connect.hpp
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 #pragma once
16 
18 
19 namespace game_config
20 {
21 struct server_info;
22 }
23 
24 namespace gui2
25 {
26 
27 class listbox;
28 
29 namespace dialogs
30 {
31 
32 /**
33  * @ingroup GUIWindowDefinitionWML
34  *
35  * This shows the dialog to the MP server to connect to.
36  * Key |Type |Mandatory|Description
37  * ------------------|--------------|---------|-----------
38  * start_table | text_box |yes |The name of the server to connect to.
39  * list | @ref button |no |Shows a dialog with a list of predefined servers to connect to.
40  */
41 class mp_connect : public modal_dialog
42 {
43  /** The unit test needs to be able to test the mp_connect dialog. */
44  friend modal_dialog* unit_test_mp_server_list();
45 
46 public:
47  mp_connect();
48 
49  /** The execute function. See @ref modal_dialog for more information. */
51 
52 private:
53  virtual const std::string& window_id() const override;
54 
55  virtual void pre_show(window& window) override;
56 
57  // Signal handlers
58 
59  void on_address_change();
60  void on_server_add();
61  void on_server_delete();
62  void on_server_select();
63 
64  // Auxiliary functions
65 
67  using server_list = std::vector<server_info>;
68 
69  void insert_into_server_listbox(listbox& listbox, const server_info& srv, int pos = -1);
70 
71  void select_first_match();
72 
73  class selection
74  {
75  public:
76  selection(mp_connect* owner, int row = -1)
77  : owner_(owner)
78  , row_(row)
79  {
80  }
81 
82  bool valid() const
83  {
84  return owner_ && row_ >= 0;
85  }
86 
87  bool user_defined() const
88  {
89  // An invalid selection is the same as one from the read-only list of
90  // built-in servers for interaction purposes since it can't be written to.
91  return valid() && std::size_t(row_) >= owner_->builtin_servers_.size();
92  }
93 
94  unsigned row() const;
95 
96  std::size_t relative_index() const;
97 
98  server_list& parent_list() const;
99 
100  server_info& get();
101 
102  private:
104  int row_;
105 
106  void must_be_valid() const
107  {
108  if(!valid()) {
109  throw std::out_of_range{"Invalid MP server selection"};
110  }
111  }
112  };
113 
114  selection current_selection();
115 
116  std::array<server_list*, 2> server_lists();
117 
118  /** The host name of the selected server. */
120 
123 };
124 
125 } // namespace dialogs
126 } // namespace gui2
selection(mp_connect *owner, int row=-1)
Definition: mp_connect.hpp:76
field_text * host_name_
The host name of the selected server.
Definition: mp_connect.hpp:119
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:42
This shows the dialog to the MP server to connect to.
Definition: mp_connect.hpp:41
Various uncategorised dialogs.
Game configuration data as global variables.
Definition: build_info.cpp:58
std::vector< server_info > server_list
Definition: mp_connect.hpp:67
Abstract base class for all modal dialogs.
Specialized field class for text.
Definition: field.hpp:592
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64