1 /**
2  * @file
3  */
4 
5 /*
6 Copyright (C) 2002-2013 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 
23 */
24 
25 #pragma once
26 
27 #include "ui_nodes.h"
28 #include "node/ui_node_abstractnode.h"
29 
30 struct value_s;
31 struct uiBehaviour_t;
32 struct uiNode_t;
33 
34 /**
35  * @brief node behaviour, how a node work
36  * @sa virtualFunctions
37  */
38 struct uiBehaviour_t {
39 	/* behaviour attributes */
40 	const char* name;				/**< name of the behaviour: string type of a node */
41 	const char* extends;			/**< name of the extends behaviour */
42 	UINodePtr manager;				/**< manager of the behaviour */
43 	bool registration;				/**< True if we can define the behavior with registration function */
44 	bool isVirtual;					/**< true, if the node doesn't have any position on the screen */
45 	bool isFunction;				/**< true, if the node is a function */
46 	bool isAbstract;				/**< true, if we can't instantiate the behaviour */
47 	bool isInitialized;				/**< cache if we already have initialized the node behaviour */
48 	bool focusEnabled;				/**< true if the node can win the focus (should be use when type TAB) */
49 	bool drawItselfChild;			/**< if true, the node draw function must draw child, the core code will not do it */
50 
51 	const value_t** localProperties;	/**< list of properties of the node */
52 	int propertyCount;				/**< number of the properties into the propertiesList. Cache value to speedup search */
53 	intptr_t extraDataSize;			/**< Size of the extra data used (it come from "u" attribute) @note use intptr_t because we use the virtual inheritance function (see virtualFunctions) */
54 	uiBehaviour_t* super;			/**< link to the extended node */
55 #ifdef DEBUG
56 	int count;						/**< number of node allocated */
57 #endif
58 };
59 
60 /**
61  * @brief Signature of a function to bind a node method.
62  */
63 typedef void (*uiNodeMethod_t)(uiNode_t* node, const struct uiCallContext_s* context);
64 
65 /**
66  * @brief Register a property to a behaviour.
67  * It should not be used in the code
68  * @param behaviour Target behaviour
69  * @param name Name of the property
70  * @param type Type of the property
71  * @param pos position of the attribute (which store property memory) into the node structure
72  * @param size size of the attribute (which store property memory) into the node structure
73  * @see UI_RegisterNodeProperty
74  * @see UI_RegisterExtradataNodeProperty
75  * @return A link to the node property
76  */
77 const struct value_s* UI_RegisterNodePropertyPosSize_(uiBehaviour_t* behaviour, const char* name, int type, size_t pos, size_t size);
78 
79 /**
80  * @brief Initialize a property
81  * @param BEHAVIOUR behaviour Target behaviour
82  * @param NAME Name of the property
83  * @param TYPE Type of the property
84  * @param OBJECTTYPE Object type containing the property
85  * @param ATTRIBUTE Name of the attribute of the object containing data of the property
86  */
87 #define UI_RegisterNodeProperty(BEHAVIOUR, NAME, TYPE, OBJECTTYPE, ATTRIBUTE) UI_RegisterNodePropertyPosSize_(BEHAVIOUR, NAME, TYPE, offsetof(OBJECTTYPE, ATTRIBUTE), MEMBER_SIZEOF(OBJECTTYPE, ATTRIBUTE))
88 
89 /**
90  * @brief Return the offset of an extradata node attribute
91  * @param TYPE Extradata type
92  * @param MEMBER Attribute name
93  * @sa offsetof
94  */
95 #define UI_EXTRADATA_OFFSETOF_(TYPE, MEMBER) ((size_t) &((TYPE *)(UI_EXTRADATA_POINTER(0, TYPE)))->MEMBER)
96 
97 /**
98  * @brief Initialize a property from extradata of node
99  * @param BEHAVIOUR behaviour Target behaviour
100  * @param NAME Name of the property
101  * @param TYPE Type of the property
102  * @param EXTRADATATYPE Object type containing the property
103  * @param ATTRIBUTE Name of the attribute of the object containing data of the property
104  */
105 #define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE) UI_RegisterNodePropertyPosSize_(BEHAVIOUR, NAME, TYPE, UI_EXTRADATA_OFFSETOF_(EXTRADATATYPE, ATTRIBUTE), MEMBER_SIZEOF(EXTRADATATYPE, ATTRIBUTE))
106 
107 /**
108  * @brief Initialize a property which override an inherited property.
109  * It is yet only used for the documentation.
110  * @param BEHAVIOUR behaviour Target behaviour
111  * @param NAME Name of the property
112  */
113 #define UI_RegisterOveridedNodeProperty(BEHAVIOUR, NAME) ;
114 
115 /**
116  * @brief Register a node method to a behaviour.
117  * @param behaviour Target behaviour
118  * @param name Name of the property
119  * @param function function to execute the node method
120  * @return A link to the node property
121  */
122 const struct value_s* UI_RegisterNodeMethod(uiBehaviour_t* behaviour, const char* name, uiNodeMethod_t function);
123 
124 /**
125  * @brief Return a property from a node behaviour
126  * @return A property, else nullptr if not found.
127  */
128 const struct value_s* UI_GetPropertyFromBehaviour(const uiBehaviour_t* behaviour, const char* name) __attribute__ ((warn_unused_result));
129 
130 /**
131  * @brief Initialize a node behaviour memory, after registration, and before unsing it.
132  * @param behaviour Behaviour to initialize
133  */
134 void UI_InitializeNodeBehaviour(uiBehaviour_t* behaviour);
135