1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma interface "xmlres.h"
22 #include "wx/string.h"
23 #include "wx/dynarray.h"
24 #include "wx/datetime.h"
26 #include "wx/gdicmn.h"
27 #include "wx/filesys.h"
28 #include "wx/bitmap.h"
30 #include "wx/artprov.h"
32 #include "wx/xml/xml.h"
34 class WXDLLEXPORT wxMenu
;
35 class WXDLLEXPORT wxMenuBar
;
36 class WXDLLEXPORT wxDialog
;
37 class WXDLLEXPORT wxPanel
;
38 class WXDLLEXPORT wxWindow
;
39 class WXDLLEXPORT wxFrame
;
40 class WXDLLEXPORT wxToolBar
;
42 class WXDLLIMPEXP_XRC wxXmlResourceHandler
;
43 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
;
44 class WXDLLIMPEXP_XRC wxXmlSubclassFactoriesList
;
45 class wxXmlResourceModule
;
48 // These macros indicate current version of XML resources (this information is
49 // encoded in root node of XRC file as "version" property).
51 // Rules for increasing version number:
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.
55 // - if you change version number, follow these steps:
56 // - set major, minor and release numbers to respective version numbers of
57 // the wxWidgets library (see wx/version.h)
58 // - reset revision to 0 unless the first three are same as before,
59 // in which case you should increase revision by one
60 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
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")
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)
72 class WXDLLIMPEXP_XRC wxXmlResourceDataRecord
75 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
76 ~wxXmlResourceDataRecord() {delete Doc
;}
84 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
,
85 wxXmlResourceDataRecords
,
88 enum wxXmlResourceFlags
91 wxXRC_NO_SUBCLASSING
= 2,
92 wxXRC_NO_RELOADING
= 4
95 // This class holds XML resources from one or more .xml files
96 // (or derived forms, either binary or zipped -- see manual for
98 class WXDLLIMPEXP_XRC wxXmlResource
: public wxObject
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 // wxXRC_NO_RELOADING
108 // don't check the modification time of the XRC files and
109 // reload them if they have changed on disk
110 wxXmlResource(int flags
= wxXRC_USE_LOCALE
);
113 // Flags: wxXRC_USE_LOCALE
114 // translatable strings will be translated via _()
115 // wxXRC_NO_SUBCLASSING
116 // subclass property of object nodes will be ignored
117 // (useful for previews in XRC editors)
118 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
);
123 // Loads resources from XML files that match given filemask.
124 // This method understands VFS (see filesys.h).
125 bool Load(const wxString
& filemask
);
127 // Unload resource from the given XML file (wildcards not allowed)
128 bool Unload(const wxString
& filename
);
130 // Initialize handlers for all supported controls/windows. This will
131 // make the executable quite big because it forces linking against
132 // most of the wxWidgets library.
133 void InitAllHandlers();
135 // Initialize only a specific handler (or custom handler). Convention says
136 // that handler name is equal to the control's name plus 'XmlHandler', for
137 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
138 // compiler (xmlres) can create include file that contains initialization
139 // code for all controls used within the resource.
140 void AddHandler(wxXmlResourceHandler
*handler
);
142 // Add a new handler at the begining of the handler list
143 void InsertHandler(wxXmlResourceHandler
*handler
);
145 // Removes all handlers
146 void ClearHandlers();
148 // Registers subclasses factory for use in XRC. This function is not meant
149 // for public use, please see the comment above wxXmlSubclassFactory
151 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
153 // Loads menu from resource. Returns NULL on failure.
154 wxMenu
*LoadMenu(const wxString
& name
);
156 // Loads menubar from resource. Returns NULL on failure.
157 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
159 // Loads menubar from resource. Returns NULL on failure.
160 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
164 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
167 // Loads a dialog. dlg points to parent window (if any).
168 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
170 // Loads a dialog. dlg points to parent window (if any). This form
171 // is used to finish creation of already existing instance (main reason
172 // for this is that you may want to use derived class with new event table)
173 // Example (typical usage):
175 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
177 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
179 // Loads a panel. panel points to parent window (if any).
180 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
182 // Loads a panel. panel points to parent window (if any). This form
183 // is used to finish creation of already existing instance.
184 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
187 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
188 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
190 // Load an object from the resource specifying both the resource name and
191 // the classname. This lets you load nonstandard container windows.
192 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
193 const wxString
& classname
);
195 // Load an object from the resource specifying both the resource name and
196 // the classname. This form lets you finish the creation of an existing
198 bool LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
,
199 const wxString
& classname
);
201 // Loads a bitmap resource from a file.
202 wxBitmap
LoadBitmap(const wxString
& name
);
204 // Loads an icon resource from a file.
205 wxIcon
LoadIcon(const wxString
& name
);
207 // Attaches an unknown control to the given panel/window/dialog.
208 // Unknown controls are used in conjunction with <object class="unknown">.
209 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
210 wxWindow
*parent
= NULL
);
212 // Returns a numeric ID that is equivalent to the string id used in an XML
213 // resource. To be used in event tables.
214 // Macro XRCID is provided for convenience
215 static int GetXRCID(const wxChar
*str_id
);
217 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
218 long GetVersion() const { return m_version
; }
220 // Compares resources version to argument. Returns -1 if resources version
221 // is less than the argument, +1 if greater and 0 if they equal.
222 int CompareVersion(int major
, int minor
, int release
, int revision
) const
223 { return GetVersion() -
224 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
226 //// Singleton accessors.
228 // Gets the global resources object or creates one if none exists.
229 static wxXmlResource
*Get();
231 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
232 static wxXmlResource
*Set(wxXmlResource
*res
);
234 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
235 int GetFlags() const { return m_flags
; }
236 // Set flags after construction.
237 void SetFlags(int flags
) { m_flags
= flags
; }
240 // Scans the resources list for unloaded files and loads them. Also reloads
241 // files that have been modified since last loading.
242 bool UpdateResources();
244 // Finds a resource (calls UpdateResources) and returns a node containing it.
245 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= false);
247 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
248 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
250 // Creates a resource from information in the given node
251 // (Uses only 'handlerToUse' if != NULL)
252 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
253 wxObject
*instance
= NULL
,
254 wxXmlResourceHandler
*handlerToUse
= NULL
);
256 // Helper of Load() and Unload(): returns the URL corresponding to the
257 // given file if it's indeed a file, otherwise returns the original string
259 static wxString
ConvertFileNameToURL(const wxString
& filename
);
261 // loading resources from archives is impossible without wxFileSystem
263 // Another helper: detect if the filename is a ZIP or XRS file
264 static bool IsArchive(const wxString
& filename
);
265 #endif // wxUSE_FILESYSTEM
272 wxXmlResourceDataRecords m_data
;
274 wxFileSystem m_curFileSystem
;
275 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
278 friend class wxXmlResourceHandler
;
279 friend class wxXmlResourceModule
;
281 static wxXmlSubclassFactoriesList
*ms_subclassFactories
;
283 // singleton instance:
284 static wxXmlResource
*ms_instance
;
288 // This macro translates string identifier (as used in XML resource,
289 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
290 // wxWidgets event tables.
292 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
293 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
294 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
295 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
296 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
299 #define XRCID(str_id) \
300 wxXmlResource::GetXRCID(wxT(str_id))
303 // This macro returns pointer to particular control in dialog
304 // created using XML resources. You can use it to set/get values from
308 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
309 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
311 #define XRCCTRL(window, id, type) \
312 (wxStaticCast((window).FindWindow(XRCID(id)), type))
314 // wxXmlResourceHandler is an abstract base class for resource handlers
315 // capable of creating a control from an XML node.
317 class WXDLLIMPEXP_XRC wxXmlResourceHandler
: public wxObject
319 DECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
)
322 wxXmlResourceHandler();
325 virtual ~wxXmlResourceHandler() {}
327 // Creates an object (menu, dialog, control, ...) from an XML node.
328 // Should check for validity.
329 // parent is a higher-level object (usually window, dialog or panel)
330 // that is often necessary to create the resource.
331 // If instance is non-NULL it should not create a new instance via 'new' but
332 // should rather use this one, and call its Create method.
333 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
336 // This one is called from CreateResource after variables
338 virtual wxObject
*DoCreateResource() = 0;
340 // Returns true if it understands this node and can create
341 // a resource from it, false otherwise.
342 virtual bool CanHandle(wxXmlNode
*node
) = 0;
344 // Sets the parent resource.
345 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
348 wxXmlResource
*m_resource
;
349 wxArrayString m_styleNames
;
350 wxArrayInt m_styleValues
;
352 // Variables (filled by CreateResource)
355 wxObject
*m_parent
, *m_instance
;
356 wxWindow
*m_parentAsWindow
;
358 // --- Handy methods:
360 // Returns true if the node has a property class equal to classname,
361 // e.g. <object class="wxDialog">.
362 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
363 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
365 // Gets node content from wxXML_ENTITY_NODE
366 // The problem is, <tag>content<tag> is represented as
367 // wxXML_ENTITY_NODE name="tag", content=""
368 // |-- wxXML_TEXT_NODE or
369 // wxXML_CDATA_SECTION_NODE name="" content="content"
370 wxString
GetNodeContent(wxXmlNode
*node
);
372 // Check to see if a parameter exists.
373 bool HasParam(const wxString
& param
);
375 // Finds the node or returns NULL.
376 wxXmlNode
*GetParamNode(const wxString
& param
);
378 // Finds the parameter value or returns the empty string.
379 wxString
GetParamValue(const wxString
& param
);
381 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
382 // understood by this handler.
383 void AddStyle(const wxString
& name
, int value
);
385 // Add styles common to all wxWindow-derived classes.
386 void AddWindowStyles();
388 // Gets style flags from text in form "flag | flag2| flag3 |..."
389 // Only understands flags added with AddStyle
390 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
392 // Gets text from param and does some conversions:
393 // - replaces \n, \r, \t by respective chars (according to C syntax)
394 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
395 // - calls wxGetTranslations (unless disabled in wxXmlResource)
396 wxString
GetText(const wxString
& param
, bool translate
= true);
398 // Returns the XRCID.
401 // Returns the resource name.
404 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
405 bool GetBool(const wxString
& param
, bool defaultv
= false);
407 // Gets an integer value from the parameter.
408 long GetLong(const wxString
& param
, long defaultv
= 0);
410 // Gets a float value from the parameter.
411 float GetFloat(const wxString
& param
, float defaultv
= 0);
413 // Gets colour in HTML syntax (#RRGGBB).
414 wxColour
GetColour(const wxString
& param
);
416 // Gets the size (may be in dialog units).
417 wxSize
GetSize(const wxString
& param
= wxT("size"),
418 wxWindow
*windowToUse
= NULL
);
420 // Gets the position (may be in dialog units).
421 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
423 // Gets a dimension (may be in dialog units).
424 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
425 wxWindow
*windowToUse
= NULL
);
428 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
429 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
430 wxSize size
= wxDefaultSize
);
433 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
434 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
435 wxSize size
= wxDefaultSize
);
438 wxFont
GetFont(const wxString
& param
= wxT("font"));
440 // Sets common window options.
441 void SetupWindow(wxWindow
*wnd
);
444 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false);
447 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
449 // Creates a resource from a node.
450 wxObject
*CreateResFromNode(wxXmlNode
*node
,
451 wxObject
*parent
, wxObject
*instance
= NULL
)
452 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
456 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
461 // Programmer-friendly macros for writing XRC handlers:
463 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
465 #define XRC_MAKE_INSTANCE(variable, classname) \
466 classname *variable = NULL; \
468 variable = wxStaticCast(m_instance, classname); \
470 variable = new classname;
473 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
474 WXDLLIMPEXP_XRC
void wxXmlInitResourceModule();
477 // This class is used to create instances of XRC "object" nodes with "subclass"
478 // property. It is _not_ supposed to be used by XRC users, you should instead
479 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
480 // only for language bindings developer who need a way to implement subclassing
481 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
482 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
485 // Try to create instance of given class and return it, return NULL on
487 virtual wxObject
*Create(const wxString
& className
) = 0;
488 virtual ~wxXmlSubclassFactory() {}
492 /* -------------------------------------------------------------------------
493 Backward compatibility macros. Do *NOT* use, they may disappear in future
494 versions of the XRC library!
495 ------------------------------------------------------------------------- */
496 #if WXWIN_COMPATIBILITY_2_4
497 #define ADD_STYLE XRC_ADD_STYLE
498 #define wxTheXmlResource wxXmlResource::Get()
500 #define XMLCTRL XRCCTRL
501 #define GetXMLID GetXRCID
506 #endif // _WX_XMLRES_H_