The Battle for Wesnoth  1.15.9+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  /** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
54  virtual const std::string& window_id() const override;
55 
56  /** Inherited from modal_dialog. */
57  virtual void pre_show(window& window) override;
58 
59  // Signal handlers
60 
61  void on_address_change();
62  void on_server_add();
63  void on_server_delete();
64  void on_server_select();
65 
66  // Auxiliary functions
67 
69  using server_list = std::vector<server_info>;
70 
71  void insert_into_server_listbox(listbox& listbox, const server_info& srv, int pos = -1);
72 
73  void select_first_match();
74 
75  class selection
76  {
77  public:
78  selection(mp_connect* owner, int row = -1)
79  : owner_(owner)
80  , row_(row)
81  {
82  }
83 
84  bool valid() const
85  {
86  return owner_ && row_ >= 0;
87  }
88 
89  bool user_defined() const
90  {
91  // An invalid selection is the same as one from the read-only list of
92  // built-in servers for interaction purposes since it can't be written to.
93  return valid() && std::size_t(row_) >= owner_->builtin_servers_.size();
94  }
95 
96  unsigned row() const;
97 
98  std::size_t relative_index() const;
99 
100  server_list& parent_list() const;
101 
102  server_info& get();
103 
104  private:
106  int row_;
107 
108  void must_be_valid() const
109  {
110  if(!valid()) {
111  throw std::out_of_range{"Invalid MP server selection"};
112  }
113  }
114  };
115 
116  selection current_selection();
117 
118  std::array<server_list*, 2> server_lists();
119 
120  /** The host name of the selected server. */
122 
125 };
126 
127 } // namespace dialogs
128 } // namespace gui2
selection(mp_connect *owner, int row=-1)
Definition: mp_connect.hpp:78
field_text * host_name_
The host name of the selected server.
Definition: mp_connect.hpp:121
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:56
std::vector< server_info > server_list
Definition: mp_connect.hpp:69
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