]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xrc/xmlres.h
clear m_aTypes array in Clear() as well (patch 1165866)
[wxWidgets.git] / include / wx / xrc / xmlres.h
CommitLineData
56d2f750
VS
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
0c9ff74f 14#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
56d2f750
VS
15#pragma interface "xmlres.h"
16#endif
17
18#include "wx/defs.h"
a1e4ec87 19
621be1ec 20#if wxUSE_XRC
a1e4ec87 21
56d2f750
VS
22#include "wx/string.h"
23#include "wx/dynarray.h"
24#include "wx/datetime.h"
25#include "wx/list.h"
26#include "wx/gdicmn.h"
792064e9
VS
27#include "wx/filesys.h"
28#include "wx/bitmap.h"
29#include "wx/icon.h"
af1337b0 30#include "wx/artprov.h"
56d2f750 31
a90d7e68
VS
32#include "wx/xml/xml.h"
33
1ad83f6b
VS
34class WXDLLEXPORT wxMenu;
35class WXDLLEXPORT wxMenuBar;
36class WXDLLEXPORT wxDialog;
37class WXDLLEXPORT wxPanel;
38class WXDLLEXPORT wxWindow;
1486ed87 39class WXDLLEXPORT wxFrame;
792064e9 40class WXDLLEXPORT wxToolBar;
56d2f750 41
30dc3455
VS
42class WXDLLIMPEXP_XRC wxXmlResourceHandler;
43class WXDLLIMPEXP_XRC wxXmlSubclassFactory;
44class WXDLLIMPEXP_XRC wxXmlSubclassFactoriesList;
2b5f62a0 45class wxXmlResourceModule;
56d2f750 46
56d2f750 47
1ce70313
VS
48// These macros indicate current version of XML resources (this information is
49// encoded in root node of XRC file as "version" property).
50//
51// Rules for increasing version number:
30dc3455
VS
52// - change it only if you made incompatible change to the format. Addition
53// of new attribute to control handler is _not_ incompatible change, because
54// older versions of the library may ignore it.
1ce70313
VS
55// - if you change version number, follow these steps:
56// - set major, minor and release numbers to respective version numbers of
be5a51fb 57// the wxWidgets library (see wx/version.h)
30dc3455
VS
58// - reset revision to 0 unless the first three are same as before,
59// in which case you should increase revision by one
1ce70313 60#define WX_XMLRES_CURRENT_VERSION_MAJOR 2
984c33c9
VS
61#define WX_XMLRES_CURRENT_VERSION_MINOR 5
62#define WX_XMLRES_CURRENT_VERSION_RELEASE 3
63#define WX_XMLRES_CURRENT_VERSION_REVISION 0
64#define WX_XMLRES_CURRENT_VERSION_STRING _T("2.5.3.0")
1ce70313
VS
65
66#define WX_XMLRES_CURRENT_VERSION \
67 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
68 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
69 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
70 WX_XMLRES_CURRENT_VERSION_REVISION)
56d2f750 71
30dc3455 72class WXDLLIMPEXP_XRC wxXmlResourceDataRecord
56d2f750 73{
eb8671f2
VS
74public:
75 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
76 ~wxXmlResourceDataRecord() {delete Doc;}
77
78 wxString File;
79 wxXmlDocument *Doc;
80 wxDateTime Time;
56d2f750
VS
81};
82
ea89ec17 83
30dc3455
VS
84WX_DECLARE_USER_EXPORTED_OBJARRAY(wxXmlResourceDataRecord,
85 wxXmlResourceDataRecords,
86 WXDLLIMPEXP_XRC);
56d2f750 87
daa85ee3
VS
88enum wxXmlResourceFlags
89{
90 wxXRC_USE_LOCALE = 1,
648db587
VS
91 wxXRC_NO_SUBCLASSING = 2,
92 wxXRC_NO_RELOADING = 4
daa85ee3 93};
ea89ec17
RD
94
95// This class holds XML resources from one or more .xml files
56d2f750 96// (or derived forms, either binary or zipped -- see manual for
ea89ec17 97// details).
30dc3455 98class WXDLLIMPEXP_XRC wxXmlResource : public wxObject
56d2f750 99{
eb8671f2 100public:
273b399f 101 // Constructor.
daa85ee3
VS
102 // Flags: wxXRC_USE_LOCALE
103 // translatable strings will be translated via _()
104 // wxXRC_NO_SUBCLASSING
105 // subclass property of object nodes will be ignored
106 // (useful for previews in XRC editors)
107 wxXmlResource(int flags = wxXRC_USE_LOCALE);
273b399f
JS
108
109 // Constructor.
110 // Flags: wxXRC_USE_LOCALE
111 // translatable strings will be translated via _()
112 // wxXRC_NO_SUBCLASSING
113 // subclass property of object nodes will be ignored
114 // (useful for previews in XRC editors)
daa85ee3 115 wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
273b399f
JS
116
117 // Destructor.
eb8671f2 118 ~wxXmlResource();
92e898b0 119
eb8671f2
VS
120 // Loads resources from XML files that match given filemask.
121 // This method understands VFS (see filesys.h).
122 bool Load(const wxString& filemask);
123
124 // Initialize handlers for all supported controls/windows. This will
125 // make the executable quite big because it forces linking against
be5a51fb 126 // most of the wxWidgets library.
eb8671f2
VS
127 void InitAllHandlers();
128
273b399f 129 // Initialize only a specific handler (or custom handler). Convention says
30dc3455
VS
130 // that handler name is equal to the control's name plus 'XmlHandler', for
131 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
132 // compiler (xmlres) can create include file that contains initialization
133 // code for all controls used within the resource.
eb8671f2
VS
134 void AddHandler(wxXmlResourceHandler *handler);
135
92e898b0
RD
136 // Add a new handler at the begining of the handler list
137 void InsertHandler(wxXmlResourceHandler *handler);
138
eb8671f2
VS
139 // Removes all handlers
140 void ClearHandlers();
408ac1b8 141
2b5f62a0
VZ
142 // Registers subclasses factory for use in XRC. This function is not meant
143 // for public use, please see the comment above wxXmlSubclassFactory
144 // definition.
145 static void AddSubclassFactory(wxXmlSubclassFactory *factory);
eb8671f2
VS
146
147 // Loads menu from resource. Returns NULL on failure.
148 wxMenu *LoadMenu(const wxString& name);
149
150 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 151 wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
273b399f
JS
152
153 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 154 wxMenuBar *LoadMenuBar(const wxString& name) { return LoadMenuBar(NULL, name); }
56d2f750 155
792064e9 156#if wxUSE_TOOLBAR
273b399f 157 // Loads a toolbar.
eb8671f2 158 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
792064e9
VS
159#endif
160
273b399f
JS
161 // Loads a dialog. dlg points to parent window (if any).
162 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
163
164 // Loads a dialog. dlg points to parent window (if any). This form
eb8671f2
VS
165 // is used to finish creation of already existing instance (main reason
166 // for this is that you may want to use derived class with new event table)
167 // Example (typical usage):
168 // MyDialog dlg;
169 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
170 // dlg->ShowModal();
eb8671f2
VS
171 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
172
273b399f 173 // Loads a panel. panel points to parent window (if any).
eb8671f2 174 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
273b399f
JS
175
176 // Loads a panel. panel points to parent window (if any). This form
177 // is used to finish creation of already existing instance.
eb8671f2
VS
178 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
179
273b399f 180 // Loads a frame.
92e898b0 181 wxFrame *LoadFrame(wxWindow* parent, const wxString& name);
eb8671f2
VS
182 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
183
92e898b0
RD
184 // Load an object from the resource specifying both the resource name and
185 // the classname. This lets you load nonstandard container windows.
186 wxObject *LoadObject(wxWindow *parent, const wxString& name,
187 const wxString& classname);
188
189 // Load an object from the resource specifying both the resource name and
190 // the classname. This form lets you finish the creation of an existing
191 // instance.
192 bool LoadObject(wxObject *instance, wxWindow *parent, const wxString& name,
193 const wxString& classname);
194
273b399f 195 // Loads a bitmap resource from a file.
eb8671f2 196 wxBitmap LoadBitmap(const wxString& name);
273b399f
JS
197
198 // Loads an icon resource from a file.
eb8671f2 199 wxIcon LoadIcon(const wxString& name);
ea89ec17 200
273b399f
JS
201 // Attaches an unknown control to the given panel/window/dialog.
202 // Unknown controls are used in conjunction with <object class="unknown">.
e6c3f404
VS
203 bool AttachUnknownControl(const wxString& name, wxWindow *control,
204 wxWindow *parent = NULL);
eb8671f2 205
273b399f
JS
206 // Returns a numeric ID that is equivalent to the string id used in an XML
207 // resource. To be used in event tables.
5ed345b7
VS
208 // Macro XRCID is provided for convenience
209 static int GetXRCID(const wxChar *str_id);
ea89ec17 210
273b399f 211 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
1ce70313 212 long GetVersion() const { return m_version; }
ea89ec17 213
1ce70313
VS
214 // Compares resources version to argument. Returns -1 if resources version
215 // is less than the argument, +1 if greater and 0 if they equal.
216 int CompareVersion(int major, int minor, int release, int revision) const
ea89ec17 217 { return GetVersion() -
1ce70313 218 (major*256*256*256 + minor*256*256 + release*256 + revision); }
92e898b0 219
273b399f 220//// Singleton accessors.
92e898b0 221
273b399f 222 // Gets the global resources object or creates one if none exists.
824e8eaa 223 static wxXmlResource *Get();
273b399f
JS
224
225 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
824e8eaa 226 static wxXmlResource *Set(wxXmlResource *res);
eb8671f2 227
74c107ba 228 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
2b5f62a0
VZ
229 int GetFlags() const { return m_flags; }
230 // Set flags after construction.
231 void SetFlags(int flags) { m_flags = flags; }
74c107ba 232
eb8671f2 233protected:
273b399f 234 // Scans the resources list for unloaded files and loads them. Also reloads
eb8671f2 235 // files that have been modified since last loading.
d614f51b 236 bool UpdateResources();
eb8671f2 237
273b399f 238 // Finds a resource (calls UpdateResources) and returns a node containing it.
f80ea77b 239 wxXmlNode *FindResource(const wxString& name, const wxString& classname, bool recursive = false);
273b399f
JS
240
241 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
47793ab8 242 wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive);
eb8671f2 243
e5db1609
VS
244 // Creates a resource from information in the given node
245 // (Uses only 'handlerToUse' if != NULL)
246 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent,
247 wxObject *instance = NULL,
248 wxXmlResourceHandler *handlerToUse = NULL);
eb8671f2 249
eb8671f2 250private:
1ce70313 251 long m_version;
ea89ec17 252
daa85ee3 253 int m_flags;
eb8671f2
VS
254 wxList m_handlers;
255 wxXmlResourceDataRecords m_data;
792064e9 256#if wxUSE_FILESYSTEM
eb8671f2
VS
257 wxFileSystem m_curFileSystem;
258 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
792064e9
VS
259#endif
260
eb8671f2 261 friend class wxXmlResourceHandler;
2b5f62a0
VZ
262 friend class wxXmlResourceModule;
263
264 static wxXmlSubclassFactoriesList *ms_subclassFactories;
92e898b0 265
824e8eaa
VS
266 // singleton instance:
267 static wxXmlResource *ms_instance;
56d2f750
VS
268};
269
270
56d2f750
VS
271// This macro translates string identifier (as used in XML resource,
272// e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
be5a51fb 273// wxWidgets event tables.
56d2f750
VS
274// Example:
275// BEGIN_EVENT_TABLE(MyFrame, wxFrame)
5ed345b7
VS
276// EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
277// EVT_MENU(XRCID("about"), MyFrame::OnAbout)
278// EVT_MENU(XRCID("new"), MyFrame::OnNew)
279// EVT_MENU(XRCID("open"), MyFrame::OnOpen)
ea89ec17 280// END_EVENT_TABLE()
45a24817 281
5ed345b7
VS
282#define XRCID(str_id) \
283 wxXmlResource::GetXRCID(wxT(str_id))
45a24817
VS
284
285
45a24817
VS
286// This macro returns pointer to particular control in dialog
287// created using XML resources. You can use it to set/get values from
288// controls.
289// Example:
290// wxDialog dlg;
546b0006 291// wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
5ed345b7 292// XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
45a24817 293
5ed345b7 294#define XRCCTRL(window, id, type) \
ae688b64 295 (wxStaticCast((window).FindWindow(XRCID(id)), type))
56d2f750 296
273b399f
JS
297// wxXmlResourceHandler is an abstract base class for resource handlers
298// capable of creating a control from an XML node.
56d2f750 299
30dc3455 300class WXDLLIMPEXP_XRC wxXmlResourceHandler : public wxObject
56d2f750 301{
854e189f 302DECLARE_ABSTRACT_CLASS(wxXmlResourceHandler)
eb8671f2 303public:
273b399f 304 // Constructor.
eb8671f2 305 wxXmlResourceHandler();
273b399f
JS
306
307 // Destructor.
eb8671f2
VS
308 virtual ~wxXmlResourceHandler() {}
309
273b399f 310 // Creates an object (menu, dialog, control, ...) from an XML node.
eb8671f2 311 // Should check for validity.
273b399f
JS
312 // parent is a higher-level object (usually window, dialog or panel)
313 // that is often neccessary to create the resource.
314 // If instance is non-NULL it should not create a new instance via 'new' but
315 // should rather use this one, and call its Create method.
ea89ec17 316 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
eb8671f2
VS
317 wxObject *instance);
318
319 // This one is called from CreateResource after variables
273b399f 320 // were filled.
eb8671f2
VS
321 virtual wxObject *DoCreateResource() = 0;
322
f80ea77b
WS
323 // Returns true if it understands this node and can create
324 // a resource from it, false otherwise.
eb8671f2
VS
325 virtual bool CanHandle(wxXmlNode *node) = 0;
326
273b399f 327 // Sets the parent resource.
eb8671f2
VS
328 void SetParentResource(wxXmlResource *res) { m_resource = res; }
329
eb8671f2 330protected:
eb8671f2
VS
331 wxXmlResource *m_resource;
332 wxArrayString m_styleNames;
333 wxArrayInt m_styleValues;
334
335 // Variables (filled by CreateResource)
336 wxXmlNode *m_node;
337 wxString m_class;
338 wxObject *m_parent, *m_instance;
9a8d8c5a 339 wxWindow *m_parentAsWindow;
eb8671f2
VS
340
341 // --- Handy methods:
342
273b399f
JS
343 // Returns true if the node has a property class equal to classname,
344 // e.g. <object class="wxDialog">.
eb8671f2
VS
345 bool IsOfClass(wxXmlNode *node, const wxString& classname)
346 { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; }
347
348 // Gets node content from wxXML_ENTITY_NODE
273b399f 349 // The problem is, <tag>content<tag> is represented as
eb8671f2 350 // wxXML_ENTITY_NODE name="tag", content=""
ea89ec17 351 // |-- wxXML_TEXT_NODE or
eb8671f2
VS
352 // wxXML_CDATA_SECTION_NODE name="" content="content"
353 wxString GetNodeContent(wxXmlNode *node);
354
273b399f 355 // Check to see if a parameter exists.
ea89ec17 356 bool HasParam(const wxString& param);
eb8671f2 357
273b399f 358 // Finds the node or returns NULL.
eb8671f2 359 wxXmlNode *GetParamNode(const wxString& param);
273b399f
JS
360
361 // Finds the parameter value or returns the empty string.
eb8671f2
VS
362 wxString GetParamValue(const wxString& param);
363
273b399f
JS
364 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
365 // understood by this handler.
eb8671f2
VS
366 void AddStyle(const wxString& name, int value);
367
273b399f 368 // Add styles common to all wxWindow-derived classes.
eb8671f2
VS
369 void AddWindowStyles();
370
371 // Gets style flags from text in form "flag | flag2| flag3 |..."
372 // Only understads flags added with AddStyle
373 int GetStyle(const wxString& param = wxT("style"), int defaults = 0);
374
273b399f 375 // Gets text from param and does some conversions:
eb8671f2 376 // - replaces \n, \r, \t by respective chars (according to C syntax)
ee1046d1 377 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
eb8671f2 378 // - calls wxGetTranslations (unless disabled in wxXmlResource)
f80ea77b 379 wxString GetText(const wxString& param, bool translate = true);
eb8671f2 380
5ed345b7 381 // Returns the XRCID.
eb8671f2 382 int GetID();
273b399f
JS
383
384 // Returns the resource name.
eb8671f2
VS
385 wxString GetName();
386
f80ea77b
WS
387 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
388 bool GetBool(const wxString& param, bool defaultv = false);
eb8671f2 389
1df61962
VS
390 // Gets an integer value from the parameter.
391 long GetLong(const wxString& param, long defaultv = 0);
392
393 // Gets a float value from the parameter.
394 float GetFloat(const wxString& param, float defaultv = 0);
eb8671f2 395
273b399f 396 // Gets colour in HTML syntax (#RRGGBB).
eb8671f2
VS
397 wxColour GetColour(const wxString& param);
398
273b399f 399 // Gets the size (may be in dialog units).
eb8671f2 400 wxSize GetSize(const wxString& param = wxT("size"));
273b399f
JS
401
402 // Gets the position (may be in dialog units).
eb8671f2
VS
403 wxPoint GetPosition(const wxString& param = wxT("pos"));
404
273b399f 405 // Gets a dimension (may be in dialog units).
eb8671f2
VS
406 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
407
273b399f 408 // Gets a bitmap.
1ce70313 409 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
7a442f31 410 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2 411 wxSize size = wxDefaultSize);
273b399f
JS
412
413 // Gets an icon.
1ce70313 414 wxIcon GetIcon(const wxString& param = wxT("icon"),
7a442f31 415 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2
VS
416 wxSize size = wxDefaultSize);
417
273b399f 418 // Gets a font.
eb8671f2
VS
419 wxFont GetFont(const wxString& param = wxT("font"));
420
273b399f 421 // Sets common window options.
eb8671f2
VS
422 void SetupWindow(wxWindow *wnd);
423
273b399f 424 // Creates children.
f80ea77b 425 void CreateChildren(wxObject *parent, bool this_hnd_only = false);
273b399f
JS
426
427 // Helper function.
eb8671f2 428 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
273b399f
JS
429
430 // Creates a resource from a node.
ea89ec17 431 wxObject *CreateResFromNode(wxXmlNode *node,
eb8671f2
VS
432 wxObject *parent, wxObject *instance = NULL)
433 { return m_resource->CreateResFromNode(node, parent, instance); }
434
435 // helper
150e58d8 436#if wxUSE_FILESYSTEM
eb8671f2 437 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
150e58d8 438#endif
56d2f750
VS
439};
440
daa85ee3
VS
441
442// Programmer-friendly macros for writing XRC handlers:
443
444#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
daa85ee3
VS
445
446#define XRC_MAKE_INSTANCE(variable, classname) \
447 classname *variable = NULL; \
448 if (m_instance) \
449 variable = wxStaticCast(m_instance, classname); \
450 if (!variable) \
451 variable = new classname;
56d2f750
VS
452
453
daa85ee3 454// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
408ac1b8 455WXDLLIMPEXP_XRC void wxXmlInitResourceModule();
56d2f750 456
546b0006 457
2b5f62a0
VZ
458// This class is used to create instances of XRC "object" nodes with "subclass"
459// property. It is _not_ supposed to be used by XRC users, you should instead
be5a51fb 460// register your subclasses via wxWidgets' RTTI mechanism. This class is useful
2b5f62a0 461// only for language bindings developer who need a way to implement subclassing
be5a51fb 462// in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
30dc3455 463class WXDLLIMPEXP_XRC wxXmlSubclassFactory
2b5f62a0
VZ
464{
465public:
30dc3455
VS
466 // Try to create instance of given class and return it, return NULL on
467 // failure:
2b5f62a0
VZ
468 virtual wxObject *Create(const wxString& className) = 0;
469 virtual ~wxXmlSubclassFactory() {}
470};
471
472
92e898b0 473/* -------------------------------------------------------------------------
546b0006
VS
474 Backward compatibility macros. Do *NOT* use, they may disappear in future
475 versions of the XRC library!
476 ------------------------------------------------------------------------- */
cf21abe5
VS
477#if WXWIN_COMPATIBILITY_2_4
478 #define ADD_STYLE XRC_ADD_STYLE
479 #define wxTheXmlResource wxXmlResource::Get()
480 #define XMLID XRCID
481 #define XMLCTRL XRCCTRL
482 #define GetXMLID GetXRCID
483#endif
546b0006 484
621be1ec 485#endif // wxUSE_XRC
546b0006 486
56d2f750 487#endif // _WX_XMLRES_H_