The Battle for Wesnoth  1.15.5+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 class mp_connect : public modal_dialog
33 {
34  /** The unit test needs to be able to test the mp_connect dialog. */
35  friend modal_dialog* unit_test_mp_server_list();
36 
37 public:
38  mp_connect();
39 
40  /** The execute function. See @ref modal_dialog for more information. */
42 
43 private:
44  /** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
45  virtual const std::string& window_id() const override;
46 
47  /** Inherited from modal_dialog. */
48  virtual void pre_show(window& window) override;
49 
50  // Signal handlers
51 
52  void on_address_change();
53  void on_server_add();
54  void on_server_delete();
55  void on_server_select();
56 
57  // Auxiliary functions
58 
60  using server_list = std::vector<server_info>;
61 
62  void insert_into_server_listbox(listbox& listbox, const server_info& srv, int pos = -1);
63 
64  void select_first_match();
65 
66  class selection
67  {
68  public:
69  selection(mp_connect* owner, int row = -1)
70  : owner_(owner)
71  , row_(row)
72  {
73  }
74 
75  bool valid() const
76  {
77  return owner_ && row_ >= 0;
78  }
79 
80  bool user_defined() const
81  {
82  // An invalid selection is the same as one from the read-only list of
83  // built-in servers for interaction purposes since it can't be written to.
84  return valid() && std::size_t(row_) >= owner_->builtin_servers_.size();
85  }
86 
87  unsigned row() const;
88 
89  std::size_t relative_index() const;
90 
91  server_list& parent_list() const;
92 
93  server_info& get();
94 
95  private:
97  int row_;
98 
99  void must_be_valid() const
100  {
101  if(!valid()) {
102  throw std::out_of_range{"Invalid MP server selection"};
103  }
104  }
105  };
106 
107  selection current_selection();
108 
109  std::array<server_list*, 2> server_lists();
110 
111  /** The host name of the selected server. */
113 
116 };
117 
118 } // namespace dialogs
119 } // namespace gui2
selection(mp_connect *owner, int row=-1)
Definition: mp_connect.hpp:69
field_text * host_name_
The host name of the selected server.
Definition: mp_connect.hpp:112
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
Various uncategorised dialogs.
Game configuration data as global variables.
Definition: build_info.cpp:55
std::vector< server_info > server_list
Definition: mp_connect.hpp:60
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:62