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