The Battle for Wesnoth  1.19.2+dev
mp_connect.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 class mp_connect : public modal_dialog
34 {
35  /** The unit test needs to be able to test the mp_connect dialog. */
37 
38 public:
39  mp_connect();
40 
41  /** The execute function. See @ref modal_dialog for more information. */
43 
44 private:
45  virtual const std::string& window_id() const override;
46 
47  virtual void pre_show(window& window) override;
48 
49  // Signal handlers
50 
51  void on_address_change();
52  void on_server_add();
53  void on_server_delete();
54  void on_server_select();
55 
56  // Auxiliary functions
57 
59  using server_list = std::vector<server_info>;
60 
61  void insert_into_server_listbox(listbox& listbox, const server_info& srv, int pos = -1);
62 
63  void select_first_match();
64 
65  class selection
66  {
67  public:
68  selection(mp_connect* owner, int row = -1)
69  : owner_(owner)
70  , row_(row)
71  {
72  }
73 
74  bool valid() const
75  {
76  return owner_ && row_ >= 0;
77  }
78 
79  bool user_defined() const
80  {
81  // An invalid selection is the same as one from the read-only list of
82  // built-in servers for interaction purposes since it can't be written to.
83  return valid() && std::size_t(row_) >= owner_->builtin_servers_.size();
84  }
85 
86  unsigned row() const;
87 
88  std::size_t relative_index() const;
89 
90  server_list& parent_list() const;
91 
92  server_info& get();
93 
94  private:
96  int row_;
97 
98  void must_be_valid() const
99  {
100  if(!valid()) {
101  throw std::out_of_range{"Invalid MP server selection"};
102  }
103  }
104  };
105 
106  selection current_selection();
107 
108  std::array<server_list*, 2> server_lists();
109 
110  /** The host name of the selected server. */
112 
115 };
116 
117 } // namespace dialogs
118 } // namespace gui2
Abstract base class for all modal dialogs.
selection(mp_connect *owner, int row=-1)
Definition: mp_connect.hpp:68
server_list & parent_list() const
Definition: mp_connect.cpp:270
void insert_into_server_listbox(listbox &listbox, const server_info &srv, int pos=-1)
Definition: mp_connect.cpp:102
virtual const std::string & window_id() const override
The execute function.
std::vector< server_info > server_list
Definition: mp_connect.hpp:59
std::array< server_list *, 2 > server_lists()
Definition: mp_connect.cpp:76
field_text * host_name_
The host name of the selected server.
Definition: mp_connect.hpp:111
friend modal_dialog * unit_test_mp_server_list()
The unit test needs to be able to test the mp_connect dialog.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: mp_connect.cpp:81
Specialized field class for text.
Definition: field.hpp:537
The listbox class.
Definition: listbox.hpp:43
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
#define DEFINE_SIMPLE_EXECUTE_WRAPPER(dialog)
Adds a bare-bonesstatic execute function to a dialog class that immediately invokes and return the re...
Various uncategorised dialogs.
Game configuration data as global variables.
Definition: build_info.cpp:61
Generic file dialog.