The Battle for Wesnoth  1.19.0-dev
register_widget.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 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 /**
21  * Registers a widget.
22  *
23  * Call this function to register a widget. Use this macro in the
24  * implementation, inside the gui2 namespace.
25  *
26  * See @ref gui2::register_widget for more information.
27  *
28  * @note When the type is foo_definition, the id "foo" and no special key best
29  * use RESISTER_WIDGET(foo) instead.
30  *
31  * @param type Class type of the window to register.
32  * @param id Id of the widget
33  * @param key The id to load if differs from id.
34  */
35 #define REGISTER_WIDGET3(type, id, key) \
36  namespace \
37  { \
38  namespace ns_##type##id \
39  { \
40  struct register_helper \
41  { \
42  register_helper() \
43  { \
44  register_widget(#id, \
45  [](const config& cfg) { return std::make_shared<type>(cfg); }, key); \
46  \
47  register_widget_builder(#id, \
48  [](const config& cfg) { return std::make_shared<implementation::builder_##id>(cfg); }); \
49  } \
50  }; \
51  \
52  static struct register_helper register_helper; \
53  } \
54  }
55 
56 /**
57  * Wrapper for REGISTER_WIDGET3.
58  *
59  * "Calls" REGISTER_WIDGET3(id_definition, id, nullptr)
60  */
61 #define REGISTER_WIDGET(id) \
62  REGISTER_WIDGET3(id##_definition, id, nullptr) \
63  \
64  const std::string& id::type() \
65  { \
66  static const std::string result(#id); \
67  return result; \
68  } \
69  \
70  const std::string& id::get_control_type() const \
71  { \
72  return id::type(); \
73  }