1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
18 #include "wx/string.h"
19 #include "wx/dynarray.h"
20 #include "wx/datetime.h"
22 #include "wx/gdicmn.h"
23 #include "wx/filesys.h"
24 #include "wx/bitmap.h"
26 #include "wx/artprov.h"
28 #include "wx/xml/xml.h"
30 class WXDLLEXPORT wxMenu
;
31 class WXDLLEXPORT wxMenuBar
;
32 class WXDLLEXPORT wxDialog
;
33 class WXDLLEXPORT wxPanel
;
34 class WXDLLEXPORT wxWindow
;
35 class WXDLLEXPORT wxFrame
;
36 class WXDLLEXPORT wxToolBar
;
38 class WXDLLIMPEXP_XRC wxXmlResourceHandler
;
39 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
;
40 class WXDLLIMPEXP_XRC wxXmlSubclassFactoriesList
;
41 class wxXmlResourceModule
;
44 // These macros indicate current version of XML resources (this information is
45 // encoded in root node of XRC file as "version" property).
47 // Rules for increasing version number:
48 // - change it only if you made incompatible change to the format. Addition
49 // of new attribute to control handler is _not_ incompatible change, because
50 // older versions of the library may ignore it.
51 // - if you change version number, follow these steps:
52 // - set major, minor and release numbers to respective version numbers of
53 // the wxWidgets library (see wx/version.h)
54 // - reset revision to 0 unless the first three are same as before,
55 // in which case you should increase revision by one
56 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
57 #define WX_XMLRES_CURRENT_VERSION_MINOR 5
58 #define WX_XMLRES_CURRENT_VERSION_RELEASE 3
59 #define WX_XMLRES_CURRENT_VERSION_REVISION 0
60 #define WX_XMLRES_CURRENT_VERSION_STRING _T("2.5.3.0")
62 #define WX_XMLRES_CURRENT_VERSION \
63 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
64 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
65 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
66 WX_XMLRES_CURRENT_VERSION_REVISION)
68 class WXDLLIMPEXP_XRC wxXmlResourceDataRecord
71 wxXmlResourceDataRecord() : Doc(NULL
) {
73 Time
= wxDateTime::Now();
76 ~wxXmlResourceDataRecord() {delete Doc
;}
86 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
,
87 wxXmlResourceDataRecords
,
90 enum wxXmlResourceFlags
93 wxXRC_NO_SUBCLASSING
= 2,
94 wxXRC_NO_RELOADING
= 4
97 // This class holds XML resources from one or more .xml files
98 // (or derived forms, either binary or zipped -- see manual for
100 class WXDLLIMPEXP_XRC wxXmlResource
: public wxObject
104 // Flags: wxXRC_USE_LOCALE
105 // translatable strings will be translated via _()
106 // wxXRC_NO_SUBCLASSING
107 // subclass property of object nodes will be ignored
108 // (useful for previews in XRC editors)
109 // wxXRC_NO_RELOADING
110 // don't check the modification time of the XRC files and
111 // reload them if they have changed on disk
112 wxXmlResource(int flags
= wxXRC_USE_LOCALE
);
115 // Flags: wxXRC_USE_LOCALE
116 // translatable strings will be translated via _()
117 // wxXRC_NO_SUBCLASSING
118 // subclass property of object nodes will be ignored
119 // (useful for previews in XRC editors)
120 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
);
125 // Loads resources from XML files that match given filemask.
126 // This method understands VFS (see filesys.h).
127 bool Load(const wxString
& filemask
);
129 // Unload resource from the given XML file (wildcards not allowed)
130 bool Unload(const wxString
& filename
);
132 // Initialize handlers for all supported controls/windows. This will
133 // make the executable quite big because it forces linking against
134 // most of the wxWidgets library.
135 void InitAllHandlers();
137 // Initialize only a specific handler (or custom handler). Convention says
138 // that handler name is equal to the control's name plus 'XmlHandler', for
139 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
140 // compiler (xmlres) can create include file that contains initialization
141 // code for all controls used within the resource.
142 void AddHandler(wxXmlResourceHandler
*handler
);
144 // Add a new handler at the begining of the handler list
145 void InsertHandler(wxXmlResourceHandler
*handler
);
147 // Removes all handlers
148 void ClearHandlers();
150 // Registers subclasses factory for use in XRC. This function is not meant
151 // for public use, please see the comment above wxXmlSubclassFactory
153 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
155 // Loads menu from resource. Returns NULL on failure.
156 wxMenu
*LoadMenu(const wxString
& name
);
158 // Loads menubar from resource. Returns NULL on failure.
159 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
161 // Loads menubar from resource. Returns NULL on failure.
162 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
166 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
169 // Loads a dialog. dlg points to parent window (if any).
170 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
172 // Loads a dialog. dlg points to parent window (if any). This form
173 // is used to finish creation of already existing instance (main reason
174 // for this is that you may want to use derived class with new event table)
175 // Example (typical usage):
177 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
179 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
181 // Loads a panel. panel points to parent window (if any).
182 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
184 // Loads a panel. panel points to parent window (if any). This form
185 // is used to finish creation of already existing instance.
186 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
189 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
190 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
192 // Load an object from the resource specifying both the resource name and
193 // the classname. This lets you load nonstandard container windows.
194 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
195 const wxString
& classname
);
197 // Load an object from the resource specifying both the resource name and
198 // the classname. This form lets you finish the creation of an existing
200 bool LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
,
201 const wxString
& classname
);
203 // Loads a bitmap resource from a file.
204 wxBitmap
LoadBitmap(const wxString
& name
);
206 // Loads an icon resource from a file.
207 wxIcon
LoadIcon(const wxString
& name
);
209 // Attaches an unknown control to the given panel/window/dialog.
210 // Unknown controls are used in conjunction with <object class="unknown">.
211 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
212 wxWindow
*parent
= NULL
);
214 // Returns a numeric ID that is equivalent to the string id used in an XML
215 // resource. To be used in event tables.
216 // Macro XRCID is provided for convenience
217 static int GetXRCID(const wxChar
*str_id
);
219 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
220 long GetVersion() const { return m_version
; }
222 // Compares resources version to argument. Returns -1 if resources version
223 // is less than the argument, +1 if greater and 0 if they equal.
224 int CompareVersion(int major
, int minor
, int release
, int revision
) const
225 { return GetVersion() -
226 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
228 //// Singleton accessors.
230 // Gets the global resources object or creates one if none exists.
231 static wxXmlResource
*Get();
233 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
234 static wxXmlResource
*Set(wxXmlResource
*res
);
236 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
237 int GetFlags() const { return m_flags
; }
238 // Set flags after construction.
239 void SetFlags(int flags
) { m_flags
= flags
; }
242 // Scans the resources list for unloaded files and loads them. Also reloads
243 // files that have been modified since last loading.
244 bool UpdateResources();
246 // Finds a resource (calls UpdateResources) and returns a node containing it.
247 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= false);
249 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
250 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
252 // Creates a resource from information in the given node
253 // (Uses only 'handlerToUse' if != NULL)
254 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
255 wxObject
*instance
= NULL
,
256 wxXmlResourceHandler
*handlerToUse
= NULL
);
258 // Helper of Load() and Unload(): returns the URL corresponding to the
259 // given file if it's indeed a file, otherwise returns the original string
261 static wxString
ConvertFileNameToURL(const wxString
& filename
);
263 // loading resources from archives is impossible without wxFileSystem
265 // Another helper: detect if the filename is a ZIP or XRS file
266 static bool IsArchive(const wxString
& filename
);
267 #endif // wxUSE_FILESYSTEM
274 wxXmlResourceDataRecords m_data
;
276 wxFileSystem m_curFileSystem
;
277 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
280 friend class wxXmlResourceHandler
;
281 friend class wxXmlResourceModule
;
283 static wxXmlSubclassFactoriesList
*ms_subclassFactories
;
285 // singleton instance:
286 static wxXmlResource
*ms_instance
;
290 // This macro translates string identifier (as used in XML resource,
291 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
292 // wxWidgets event tables.
294 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
295 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
296 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
297 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
298 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
301 #define XRCID(str_id) \
302 wxXmlResource::GetXRCID(wxT(str_id))
305 // This macro returns pointer to particular control in dialog
306 // created using XML resources. You can use it to set/get values from
310 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
311 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
313 #define XRCCTRL(window, id, type) \
314 (wxStaticCast((window).FindWindow(XRCID(id)), type))
316 // wxXmlResourceHandler is an abstract base class for resource handlers
317 // capable of creating a control from an XML node.
319 class WXDLLIMPEXP_XRC wxXmlResourceHandler
: public wxObject
321 DECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
)
324 wxXmlResourceHandler();
327 virtual ~wxXmlResourceHandler() {}
329 // Creates an object (menu, dialog, control, ...) from an XML node.
330 // Should check for validity.
331 // parent is a higher-level object (usually window, dialog or panel)
332 // that is often necessary to create the resource.
333 // If instance is non-NULL it should not create a new instance via 'new' but
334 // should rather use this one, and call its Create method.
335 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
338 // This one is called from CreateResource after variables
340 virtual wxObject
*DoCreateResource() = 0;
342 // Returns true if it understands this node and can create
343 // a resource from it, false otherwise.
344 virtual bool CanHandle(wxXmlNode
*node
) = 0;
346 // Sets the parent resource.
347 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
350 wxXmlResource
*m_resource
;
351 wxArrayString m_styleNames
;
352 wxArrayInt m_styleValues
;
354 // Variables (filled by CreateResource)
357 wxObject
*m_parent
, *m_instance
;
358 wxWindow
*m_parentAsWindow
;
360 // --- Handy methods:
362 // Returns true if the node has a property class equal to classname,
363 // e.g. <object class="wxDialog">.
364 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
365 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
367 // Gets node content from wxXML_ENTITY_NODE
368 // The problem is, <tag>content<tag> is represented as
369 // wxXML_ENTITY_NODE name="tag", content=""
370 // |-- wxXML_TEXT_NODE or
371 // wxXML_CDATA_SECTION_NODE name="" content="content"
372 wxString
GetNodeContent(wxXmlNode
*node
);
374 // Check to see if a parameter exists.
375 bool HasParam(const wxString
& param
);
377 // Finds the node or returns NULL.
378 wxXmlNode
*GetParamNode(const wxString
& param
);
380 // Finds the parameter value or returns the empty string.
381 wxString
GetParamValue(const wxString
& param
);
383 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
384 // understood by this handler.
385 void AddStyle(const wxString
& name
, int value
);
387 // Add styles common to all wxWindow-derived classes.
388 void AddWindowStyles();
390 // Gets style flags from text in form "flag | flag2| flag3 |..."
391 // Only understands flags added with AddStyle
392 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
394 // Gets text from param and does some conversions:
395 // - replaces \n, \r, \t by respective chars (according to C syntax)
396 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
397 // - calls wxGetTranslations (unless disabled in wxXmlResource)
398 wxString
GetText(const wxString
& param
, bool translate
= true);
400 // Returns the XRCID.
403 // Returns the resource name.
406 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
407 bool GetBool(const wxString
& param
, bool defaultv
= false);
409 // Gets an integer value from the parameter.
410 long GetLong(const wxString
& param
, long defaultv
= 0);
412 // Gets a float value from the parameter.
413 float GetFloat(const wxString
& param
, float defaultv
= 0);
415 // Gets colour in HTML syntax (#RRGGBB).
416 wxColour
GetColour(const wxString
& param
);
418 // Gets the size (may be in dialog units).
419 wxSize
GetSize(const wxString
& param
= wxT("size"),
420 wxWindow
*windowToUse
= NULL
);
422 // Gets the position (may be in dialog units).
423 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
425 // Gets a dimension (may be in dialog units).
426 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
427 wxWindow
*windowToUse
= NULL
);
430 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
431 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
432 wxSize size
= wxDefaultSize
);
435 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
436 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
437 wxSize size
= wxDefaultSize
);
440 wxFont
GetFont(const wxString
& param
= wxT("font"));
442 // Sets common window options.
443 void SetupWindow(wxWindow
*wnd
);
446 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false);
449 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
451 // Creates a resource from a node.
452 wxObject
*CreateResFromNode(wxXmlNode
*node
,
453 wxObject
*parent
, wxObject
*instance
= NULL
)
454 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
458 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
463 // Programmer-friendly macros for writing XRC handlers:
465 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
467 #define XRC_MAKE_INSTANCE(variable, classname) \
468 classname *variable = NULL; \
470 variable = wxStaticCast(m_instance, classname); \
472 variable = new classname;
475 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
476 WXDLLIMPEXP_XRC
void wxXmlInitResourceModule();
479 // This class is used to create instances of XRC "object" nodes with "subclass"
480 // property. It is _not_ supposed to be used by XRC users, you should instead
481 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
482 // only for language bindings developer who need a way to implement subclassing
483 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
484 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
487 // Try to create instance of given class and return it, return NULL on
489 virtual wxObject
*Create(const wxString
& className
) = 0;
490 virtual ~wxXmlSubclassFactory() {}
494 /* -------------------------------------------------------------------------
495 Backward compatibility macros. Do *NOT* use, they may disappear in future
496 versions of the XRC library!
497 ------------------------------------------------------------------------- */
498 #if WXWIN_COMPATIBILITY_2_4
499 #define ADD_STYLE XRC_ADD_STYLE
500 #define wxTheXmlResource wxXmlResource::Get()
502 #define XMLCTRL XRCCTRL
503 #define GetXMLID GetXRCID
508 #endif // _WX_XMLRES_H_