]> git.saurik.com Git - wxWidgets.git/blob - contrib/include/wx/xml/xmlres.h
XMLCTRL() macro now uses wxDynamicCast in debug builds
[wxWidgets.git] / contrib / include / wx / xml / xmlres.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xmlres.h
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
5 // Created: 2000/03/05
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_XMLRES_H_
12 #define _WX_XMLRES_H_
13
14 #ifdef __GNUG__
15 #pragma interface "xmlres.h"
16 #endif
17
18 #include "wx/defs.h"
19 #include "wx/string.h"
20 #include "wx/dynarray.h"
21 #include "wx/datetime.h"
22 #include "wx/list.h"
23 #include "wx/gdicmn.h"
24 #include "wx/filesys.h"
25 #include "wx/bitmap.h"
26 #include "wx/icon.h"
27
28 class WXDLLEXPORT wxMenu;
29 class WXDLLEXPORT wxMenuBar;
30 class WXDLLEXPORT wxDialog;
31 class WXDLLEXPORT wxPanel;
32 class WXDLLEXPORT wxWindow;
33 class WXDLLEXPORT wxFrame;
34 class WXDLLEXPORT wxToolBar;
35
36 class WXDLLEXPORT wxXmlResourceHandler;
37
38 #include "wx/xml/xml.h"
39
40 // These macros indicate current version of XML resources (this information is
41 // encoded in root node of XRC file as "version" property).
42 //
43 // Rules for increasing version number:
44 // - change it only if you made incompatible change to the format. Addition of new
45 // attribute to control handler is _not_ incompatible change, because older
46 // versions of the library may ignore it.
47 // - if you change version number, follow these steps:
48 // - set major, minor and release numbers to respective version numbers of
49 // the wxWindows library (see wx/version.h)
50 // - reset revision to 0 unless the first three are same as before, in which
51 // case you should increase revision by one
52 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
53 #define WX_XMLRES_CURRENT_VERSION_MINOR 3
54 #define WX_XMLRES_CURRENT_VERSION_RELEASE 0
55 #define WX_XMLRES_CURRENT_VERSION_REVISION 1
56 #define WX_XMLRES_CURRENT_VERSION_STRING "2.3.0.1"
57
58 #define WX_XMLRES_CURRENT_VERSION \
59 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
60 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
61 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
62 WX_XMLRES_CURRENT_VERSION_REVISION)
63
64 class WXDLLEXPORT wxXmlResourceDataRecord
65 {
66 public:
67 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
68 ~wxXmlResourceDataRecord() {delete Doc;}
69
70 wxString File;
71 wxXmlDocument *Doc;
72 wxDateTime Time;
73 };
74
75 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
76
77 // This class holds XML resources from one or more .xml files
78 // (or derived forms, either binary or zipped -- see manual for
79 // details).
80
81 class WXDLLEXPORT wxXmlResource : public wxObject
82 {
83 public:
84 // Ctor. If use_locale is TRUE, translatable strings are
85 // translated via _(). You can disable it by passing use_locale=FALSE
86 // (for example if you provide resource file for each locale)
87 wxXmlResource(bool use_locale = TRUE);
88 wxXmlResource(const wxString& filemask, bool use_locale = TRUE);
89 ~wxXmlResource();
90
91 // Loads resources from XML files that match given filemask.
92 // This method understands VFS (see filesys.h).
93 bool Load(const wxString& filemask);
94
95 // Initialize handlers for all supported controls/windows. This will
96 // make the executable quite big because it forces linking against
97 // most of wxWin library
98 void InitAllHandlers();
99
100 // Initialize only specific handler (or custom handler). Convention says
101 // that handler name is equal to control's name plus 'XmlHandler', e.g.
102 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
103 // (xmlres) can create include file that contains initialization code for
104 // all controls used within the resource.
105 void AddHandler(wxXmlResourceHandler *handler);
106
107 // Removes all handlers
108 void ClearHandlers();
109
110 // Loads menu from resource. Returns NULL on failure.
111 wxMenu *LoadMenu(const wxString& name);
112
113 // Loads menubar from resource. Returns NULL on failure.
114 wxMenuBar *LoadMenuBar(const wxString& name);
115
116 #if wxUSE_TOOLBAR
117 // Loads toolbar
118 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
119 #endif
120
121 // Loads dialog. dlg points to parent window (if any). Second form
122 // is used to finish creation of already existing instance (main reason
123 // for this is that you may want to use derived class with new event table)
124 // Example (typical usage):
125 // MyDialog dlg;
126 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
127 // dlg->ShowModal();
128 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
129 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
130
131 // Loads panel. panel points to parent window (if any). Second form
132 // is used to finish creation of already existing instance.
133 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
134 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
135
136 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
137
138 // Loads bitmap or icon resource from file:
139 wxBitmap LoadBitmap(const wxString& name);
140 wxIcon LoadIcon(const wxString& name);
141
142 // Attaches unknown control into given panel/window/dialog:
143 // (unknown controls are used in conjunction with <object class="unknown">)
144 bool AttachUnknownControl(const wxString& name, wxWindow *control,
145 wxWindow *parent = NULL);
146
147 // Returns numeric ID that is equivalent to string id used in XML
148 // resource. To be used in event tables
149 // Macro XMLID is provided for convenience
150 static int GetXMLID(const char *str_id);
151
152 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
153 long GetVersion() const { return m_version; }
154
155 // Compares resources version to argument. Returns -1 if resources version
156 // is less than the argument, +1 if greater and 0 if they equal.
157 int CompareVersion(int major, int minor, int release, int revision) const
158 { return GetVersion() -
159 (major*256*256*256 + minor*256*256 + release*256 + revision); }
160
161 protected:
162 // Scans resources list for unloaded files and loads them. Also reloads
163 // files that have been modified since last loading.
164 void UpdateResources();
165
166 // Finds resource (calls UpdateResources) and returns node containing it
167 wxXmlNode *FindResource(const wxString& name, const wxString& classname);
168
169 // Creates resource from info in given node:
170 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
171
172 // Remove nodes with property "platform" that does not
173 // match current platform
174 void ProcessPlatformProperty(wxXmlNode *node);
175
176 bool GetUseLocale() { return m_useLocale; }
177
178 private:
179 long m_version;
180
181 bool m_useLocale;
182 wxList m_handlers;
183 wxXmlResourceDataRecords m_data;
184 #if wxUSE_FILESYSTEM
185 wxFileSystem m_curFileSystem;
186 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
187 #endif
188
189 friend class wxXmlResourceHandler;
190 };
191
192
193 // Global instance of resource class. For your convenience.
194 extern wxXmlResource *wxTheXmlResource;
195
196 // This macro translates string identifier (as used in XML resource,
197 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
198 // wxWindows event tables.
199 // Example:
200 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
201 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
202 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
203 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
204 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
205 // END_EVENT_TABLE()
206
207 #define XMLID(str_id) \
208 wxXmlResource::GetXMLID(wxT(str_id))
209
210
211 // This macro returns pointer to particular control in dialog
212 // created using XML resources. You can use it to set/get values from
213 // controls.
214 // Example:
215 // wxDialog dlg;
216 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
217 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
218
219 #ifdef __WXDEBUG__
220 #define XMLCTRL(window, id, type) \
221 (wxDynamicCast((window).FindWindow(XMLID(id)), type))
222 #else
223 #define XMLCTRL(window, id, type) \
224 ((type*)((window).FindWindow(XMLID(id))))
225 #endif
226
227
228 class WXDLLEXPORT wxXmlResourceHandler : public wxObject
229 {
230 public:
231 wxXmlResourceHandler();
232 virtual ~wxXmlResourceHandler() {}
233
234 // Creates object (menu, dialog, control, ...) from XML node.
235 // Should check for validity.
236 // parent is higher-level object (usually window, dialog or panel)
237 // that is often neccessary to create resource
238 // if instance != NULL it should not create new instance via 'new' but
239 // rather use this one and call its Create method
240 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
241 wxObject *instance);
242
243 // This one is called from CreateResource after variables
244 // were filled
245 virtual wxObject *DoCreateResource() = 0;
246
247 // Returns TRUE if it understands this node and can create
248 // resource from it, FALSE otherwise.
249 virtual bool CanHandle(wxXmlNode *node) = 0;
250
251 void SetParentResource(wxXmlResource *res) { m_resource = res; }
252
253
254 protected:
255
256 wxXmlResource *m_resource;
257 wxArrayString m_styleNames;
258 wxArrayInt m_styleValues;
259
260 // Variables (filled by CreateResource)
261 wxXmlNode *m_node;
262 wxString m_class;
263 wxObject *m_parent, *m_instance;
264 wxWindow *m_parentAsWindow, *m_instanceAsWindow;
265
266 // --- Handy methods:
267
268 // Returns true if the node has property class equal to classname,
269 // e.g. <object class="wxDialog">
270 bool IsOfClass(wxXmlNode *node, const wxString& classname)
271 { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; }
272
273 // Gets node content from wxXML_ENTITY_NODE
274 // (the problem is, <tag>content<tag> is represented as
275 // wxXML_ENTITY_NODE name="tag", content=""
276 // |-- wxXML_TEXT_NODE or
277 // wxXML_CDATA_SECTION_NODE name="" content="content"
278 wxString GetNodeContent(wxXmlNode *node);
279
280 // Check to see if a param exists
281 bool HasParam(const wxString& param);
282
283 // Finds the node or returns NULL
284 wxXmlNode *GetParamNode(const wxString& param);
285 wxString GetParamValue(const wxString& param);
286
287 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
288 // understood by this handler
289 void AddStyle(const wxString& name, int value);
290
291 // Add styles common to all wxWindow-derived classes
292 void AddWindowStyles();
293
294 // Gets style flags from text in form "flag | flag2| flag3 |..."
295 // Only understads flags added with AddStyle
296 int GetStyle(const wxString& param = wxT("style"), int defaults = 0);
297
298 // Gets text from param and does some convertions:
299 // - replaces \n, \r, \t by respective chars (according to C syntax)
300 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
301 // - calls wxGetTranslations (unless disabled in wxXmlResource)
302 wxString GetText(const wxString& param);
303
304 // Return XMLID
305 int GetID();
306 wxString GetName();
307
308 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
309 bool GetBool(const wxString& param, bool defaultv = FALSE);
310
311 // Get integer value from param
312 long GetLong( const wxString& param, long defaultv = 0 );
313
314 // Get colour in HTML syntax (#RRGGBB)
315 wxColour GetColour(const wxString& param);
316
317 // Get size/position (may be in dlg units):
318 wxSize GetSize(const wxString& param = wxT("size"));
319 wxPoint GetPosition(const wxString& param = wxT("pos"));
320
321 // Get dimension (may be in dlg units):
322 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
323
324 // Get bitmap:
325 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
326 wxSize size = wxDefaultSize);
327 wxIcon GetIcon(const wxString& param = wxT("icon"),
328 wxSize size = wxDefaultSize);
329
330 // Get font:
331 wxFont GetFont(const wxString& param = wxT("font"));
332
333 // Sets common window options:
334 void SetupWindow(wxWindow *wnd);
335
336 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
337 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
338 wxObject *CreateResFromNode(wxXmlNode *node,
339 wxObject *parent, wxObject *instance = NULL)
340 { return m_resource->CreateResFromNode(node, parent, instance); }
341
342 // helper
343 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
344 };
345
346 #define ADD_STYLE(style) AddStyle(wxT(#style), style)
347
348
349
350 #endif // _WX_XMLRES_H_