Merge 'remotes/trunk'
[0ad.git] / source / gui / GUIbase.h
blob553e94b9e150ec7eb599c043a93e5800c85469e8
1 /* Copyright (C) 2016 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
19 GUI Core, stuff that the whole GUI uses
21 --Overview--
23 Contains defines, includes, types etc that the whole
24 GUI should have included.
26 --More info--
28 Check GUI.h
32 #ifndef INCLUDED_GUIBASE
33 #define INCLUDED_GUIBASE
35 #include <map>
36 #include <vector>
38 #include "ps/CStr.h"
39 #include "ps/Errors.h"
40 // I would like to just forward declare CSize, but it doesn't
41 // seem to be defined anywhere in the predefined header.
42 #include "ps/Shapes.h"
44 class IGUIObject;
46 // Object settings setups
47 // Setup an object's ConstructObject function
48 #define GUI_OBJECT(obj) \
49 public: \
50 static IGUIObject* ConstructObject() { return new obj(); }
53 /**
54 * Message types.
55 * @see SGUIMessage
57 enum EGUIMessageType
59 GUIM_MOUSE_OVER,
60 GUIM_MOUSE_ENTER,
61 GUIM_MOUSE_LEAVE,
62 GUIM_MOUSE_PRESS_LEFT,
63 GUIM_MOUSE_PRESS_RIGHT,
64 GUIM_MOUSE_DOWN_LEFT,
65 GUIM_MOUSE_DOWN_RIGHT,
66 GUIM_MOUSE_DBLCLICK_LEFT,
67 GUIM_MOUSE_DBLCLICK_LEFT_ITEM, // Triggered when doubleclicking on a list item
68 GUIM_MOUSE_DBLCLICK_RIGHT,
69 GUIM_MOUSE_RELEASE_LEFT,
70 GUIM_MOUSE_RELEASE_RIGHT,
71 GUIM_MOUSE_WHEEL_UP,
72 GUIM_MOUSE_WHEEL_DOWN,
73 GUIM_SETTINGS_UPDATED, // SGUIMessage.m_Value = name of setting
74 GUIM_PRESSED,
75 GUIM_RELEASED,
76 GUIM_DOUBLE_PRESSED,
77 GUIM_MOUSE_MOTION,
78 GUIM_LOAD, // Called when an object is added to the GUI.
79 GUIM_GOT_FOCUS,
80 GUIM_LOST_FOCUS,
81 GUIM_PRESSED_MOUSE_RIGHT,
82 GUIM_DOUBLE_PRESSED_MOUSE_RIGHT,
83 GUIM_TAB // Used by CInput
86 /**
87 * Message send to IGUIObject::HandleMessage() in order
88 * to give life to Objects manually with
89 * a derived HandleMessage().
91 struct SGUIMessage
93 SGUIMessage(EGUIMessageType _type) : type(_type), skipped(false) {}
94 SGUIMessage(EGUIMessageType _type, const CStr& _value) : type(_type), value(_value), skipped(false) {}
96 /**
97 * This method can be used to allow other event handlers to process this GUI event,
98 * by default an event is not skipped (only the first handler will process it).
100 * @param skip true to allow further event handling, false to prevent it
102 void Skip(bool skip = true) { skipped = skip; }
105 * Describes what the message regards
107 EGUIMessageType type;
110 * Optional data
112 CStr value;
115 * Flag that specifies if object skipped handling the event
117 bool skipped;
121 * Recurse restrictions, when we recurse, if an object
122 * is hidden for instance, you might want it to skip
123 * the children also
124 * Notice these are flags! and we don't really need one
125 * for no restrictions, because then you'll just enter 0
127 enum
129 GUIRR_HIDDEN = 0x00000001,
130 GUIRR_DISABLED = 0x00000010,
131 GUIRR_GHOST = 0x00000100
134 // Text alignments
135 enum EAlign { EAlign_Left, EAlign_Right, EAlign_Center };
136 enum EVAlign { EVAlign_Top, EVAlign_Bottom, EVAlign_Center };
138 // Typedefs
139 typedef std::map<CStr, IGUIObject*> map_pObjects;
140 typedef std::vector<IGUIObject*> vector_pObjects;
142 // Icon, you create them in the XML file with root element <setup>
143 // you use them in text owned by different objects... Such as CText.
144 struct SGUIIcon
146 SGUIIcon() : m_CellID(0) {}
148 // Sprite name of icon
149 CStr m_SpriteName;
151 // Size
152 CSize m_Size;
154 // Cell of texture to use; ignored unless the texture has specified cell-size
155 int m_CellID;
159 * Client Area is a rectangle relative to a parent rectangle
161 * You can input the whole value of the Client Area by
162 * string. Like used in the GUI.
164 class CClientArea
166 public:
167 CClientArea();
168 CClientArea(const CStr& Value);
169 CClientArea(const CRect& pixel, const CRect& percent);
171 /// Pixel modifiers
172 CRect pixel;
174 /// Percent modifiers
175 CRect percent;
178 * Get client area rectangle when the parent is given
180 CRect GetClientArea(const CRect& parent) const;
183 * The ClientArea can be set from a string looking like:
185 * "0 0 100% 100%"
186 * "50%-10 50%-10 50%+10 50%+10"
188 * i.e. First percent modifier, then + or - and the pixel modifier.
189 * Although you can use just the percent or the pixel modifier. Notice
190 * though that the percent modifier must always be the first when
191 * both modifiers are inputted.
193 * @return true if success, false if failure. If false then the client area
194 * will be unchanged.
196 bool SetClientArea(const CStr& Value);
198 bool operator==(const CClientArea& other) const
200 return pixel == other.pixel && percent == other.percent;
205 ERROR_GROUP(GUI);
207 ERROR_TYPE(GUI, NullObjectProvided);
208 ERROR_TYPE(GUI, InvalidSetting);
209 ERROR_TYPE(GUI, OperationNeedsGUIObject);
210 ERROR_TYPE(GUI, NameAmbiguity);
211 ERROR_TYPE(GUI, ObjectNeedsName);
213 #endif // INCLUDED_GUIBASE