Don't crash while in the lobby when receiving an error IQ stanza without an error...
[0ad.git] / source / gui / GUIbase.h
blob43df71abea0232c6844d879ec930790566fe7104
1 /* Copyright (C) 2017 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_LEFT_ITEM,
64 GUIM_MOUSE_PRESS_RIGHT,
65 GUIM_MOUSE_DOWN_LEFT,
66 GUIM_MOUSE_DOWN_RIGHT,
67 GUIM_MOUSE_DBLCLICK_LEFT,
68 GUIM_MOUSE_DBLCLICK_LEFT_ITEM, // Triggered when doubleclicking on a list item
69 GUIM_MOUSE_DBLCLICK_RIGHT,
70 GUIM_MOUSE_RELEASE_LEFT,
71 GUIM_MOUSE_RELEASE_RIGHT,
72 GUIM_MOUSE_WHEEL_UP,
73 GUIM_MOUSE_WHEEL_DOWN,
74 GUIM_SETTINGS_UPDATED, // SGUIMessage.m_Value = name of setting
75 GUIM_PRESSED,
76 GUIM_RELEASED,
77 GUIM_DOUBLE_PRESSED,
78 GUIM_MOUSE_MOTION,
79 GUIM_LOAD, // Called when an object is added to the GUI.
80 GUIM_GOT_FOCUS,
81 GUIM_LOST_FOCUS,
82 GUIM_PRESSED_MOUSE_RIGHT,
83 GUIM_DOUBLE_PRESSED_MOUSE_RIGHT,
84 GUIM_TAB, // Used by CInput
85 GUIM_TEXTEDIT
88 /**
89 * Message send to IGUIObject::HandleMessage() in order
90 * to give life to Objects manually with
91 * a derived HandleMessage().
93 struct SGUIMessage
95 SGUIMessage(EGUIMessageType _type) : type(_type), skipped(false) {}
96 SGUIMessage(EGUIMessageType _type, const CStr& _value) : type(_type), value(_value), skipped(false) {}
98 /**
99 * This method can be used to allow other event handlers to process this GUI event,
100 * by default an event is not skipped (only the first handler will process it).
102 * @param skip true to allow further event handling, false to prevent it
104 void Skip(bool skip = true) { skipped = skip; }
107 * Describes what the message regards
109 EGUIMessageType type;
112 * Optional data
114 CStr value;
117 * Flag that specifies if object skipped handling the event
119 bool skipped;
123 * Recurse restrictions, when we recurse, if an object
124 * is hidden for instance, you might want it to skip
125 * the children also
126 * Notice these are flags! and we don't really need one
127 * for no restrictions, because then you'll just enter 0
129 enum
131 GUIRR_HIDDEN = 0x00000001,
132 GUIRR_DISABLED = 0x00000010,
133 GUIRR_GHOST = 0x00000100
136 // Text alignments
137 enum EAlign { EAlign_Left, EAlign_Right, EAlign_Center };
138 enum EVAlign { EVAlign_Top, EVAlign_Bottom, EVAlign_Center };
140 // Typedefs
141 typedef std::map<CStr, IGUIObject*> map_pObjects;
142 typedef std::vector<IGUIObject*> vector_pObjects;
144 // Icon, you create them in the XML file with root element <setup>
145 // you use them in text owned by different objects... Such as CText.
146 struct SGUIIcon
148 SGUIIcon() : m_CellID(0) {}
150 // Sprite name of icon
151 CStr m_SpriteName;
153 // Size
154 CSize m_Size;
156 // Cell of texture to use; ignored unless the texture has specified cell-size
157 int m_CellID;
161 * Client Area is a rectangle relative to a parent rectangle
163 * You can input the whole value of the Client Area by
164 * string. Like used in the GUI.
166 class CClientArea
168 public:
169 CClientArea();
170 CClientArea(const CStr& Value);
171 CClientArea(const CRect& pixel, const CRect& percent);
173 /// Pixel modifiers
174 CRect pixel;
176 /// Percent modifiers
177 CRect percent;
180 * Get client area rectangle when the parent is given
182 CRect GetClientArea(const CRect& parent) const;
185 * The ClientArea can be set from a string looking like:
187 * "0 0 100% 100%"
188 * "50%-10 50%-10 50%+10 50%+10"
190 * i.e. First percent modifier, then + or - and the pixel modifier.
191 * Although you can use just the percent or the pixel modifier. Notice
192 * though that the percent modifier must always be the first when
193 * both modifiers are inputted.
195 * @return true if success, false if failure. If false then the client area
196 * will be unchanged.
198 bool SetClientArea(const CStr& Value);
200 bool operator==(const CClientArea& other) const
202 return pixel == other.pixel && percent == other.percent;
207 ERROR_GROUP(GUI);
209 ERROR_TYPE(GUI, NullObjectProvided);
210 ERROR_TYPE(GUI, InvalidSetting);
211 ERROR_TYPE(GUI, OperationNeedsGUIObject);
212 ERROR_TYPE(GUI, NameAmbiguity);
213 ERROR_TYPE(GUI, ObjectNeedsName);
215 #endif // INCLUDED_GUIBASE