The Battle for Wesnoth  1.19.7+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() 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  unsigned row() const;
82 
83  std::size_t relative_index() const;
84 
85  server_list& parent_list() const;
86 
87  server_info& get();
88 
89  private:
91  int row_;
92 
93  void must_be_valid() const
94  {
95  if(!valid()) {
96  throw std::out_of_range{"Invalid MP server selection"};
97  }
98  }
99  };
100 
101  selection current_selection();
102 
103  std::array<server_list*, 2> server_lists();
104 
105  /** The host name of the selected server. */
107 
110 };
111 
112 } // namespace dialogs
113 } // 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:269
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:106
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() override
Actions to be taken before showing the window.
Definition: mp_connect.cpp:81
Specialized field class for text.
Definition: field.hpp:536
The listbox class.
Definition: listbox.hpp:41
#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.