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