1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/xrc/xmlres.h
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/arrstr.h"
21 #include "wx/datetime.h"
23 #include "wx/gdicmn.h"
24 #include "wx/filesys.h"
25 #include "wx/bitmap.h"
27 #include "wx/artprov.h"
28 #include "wx/colour.h"
29 #include "wx/animate.h"
30 #include "wx/vector.h"
32 #include "wx/xrc/xmlreshandler.h"
34 class WXDLLIMPEXP_FWD_BASE wxFileName
;
36 class WXDLLIMPEXP_FWD_CORE wxIconBundle
;
37 class WXDLLIMPEXP_FWD_CORE wxImageList
;
38 class WXDLLIMPEXP_FWD_CORE wxMenu
;
39 class WXDLLIMPEXP_FWD_CORE wxMenuBar
;
40 class WXDLLIMPEXP_FWD_CORE wxDialog
;
41 class WXDLLIMPEXP_FWD_CORE wxPanel
;
42 class WXDLLIMPEXP_FWD_CORE wxWindow
;
43 class WXDLLIMPEXP_FWD_CORE wxFrame
;
44 class WXDLLIMPEXP_FWD_CORE wxToolBar
;
46 class WXDLLIMPEXP_FWD_XML wxXmlDocument
;
47 class WXDLLIMPEXP_FWD_XML wxXmlNode
;
48 class WXDLLIMPEXP_FWD_XRC wxXmlSubclassFactory
;
49 class wxXmlSubclassFactories
;
50 class wxXmlResourceModule
;
51 class wxXmlResourceDataRecords
;
53 // These macros indicate current version of XML resources (this information is
54 // encoded in root node of XRC file as "version" property).
56 // Rules for increasing version number:
57 // - change it only if you made incompatible change to the format. Addition
58 // of new attribute to control handler is _not_ incompatible change, because
59 // older versions of the library may ignore it.
60 // - if you change version number, follow these steps:
61 // - set major, minor and release numbers to respective version numbers of
62 // the wxWidgets library (see wx/version.h)
63 // - reset revision to 0 unless the first three are same as before,
64 // in which case you should increase revision by one
65 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
66 #define WX_XMLRES_CURRENT_VERSION_MINOR 5
67 #define WX_XMLRES_CURRENT_VERSION_RELEASE 3
68 #define WX_XMLRES_CURRENT_VERSION_REVISION 0
69 #define WX_XMLRES_CURRENT_VERSION_STRING wxT("2.5.3.0")
71 #define WX_XMLRES_CURRENT_VERSION \
72 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
73 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
74 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
75 WX_XMLRES_CURRENT_VERSION_REVISION)
77 enum wxXmlResourceFlags
80 wxXRC_NO_SUBCLASSING
= 2,
81 wxXRC_NO_RELOADING
= 4
84 // This class holds XML resources from one or more .xml files
85 // (or derived forms, either binary or zipped -- see manual for
87 class WXDLLIMPEXP_XRC wxXmlResource
: public wxObject
91 // Flags: wxXRC_USE_LOCALE
92 // translatable strings will be translated via _()
93 // using the given domain if specified
94 // wxXRC_NO_SUBCLASSING
95 // subclass property of object nodes will be ignored
96 // (useful for previews in XRC editors)
98 // don't check the modification time of the XRC files and
99 // reload them if they have changed on disk
100 wxXmlResource(int flags
= wxXRC_USE_LOCALE
,
101 const wxString
& domain
= wxEmptyString
);
104 // Flags: wxXRC_USE_LOCALE
105 // translatable strings will be translated via _()
106 // using the given domain if specified
107 // wxXRC_NO_SUBCLASSING
108 // subclass property of object nodes will be ignored
109 // (useful for previews in XRC editors)
110 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
,
111 const wxString
& domain
= wxEmptyString
);
114 virtual ~wxXmlResource();
116 // Loads resources from XML files that match given filemask.
117 // This method understands wxFileSystem URLs if wxUSE_FILESYS.
118 bool Load(const wxString
& filemask
);
120 // Loads resources from single XRC file.
121 bool LoadFile(const wxFileName
& file
);
123 // Loads all XRC files from a directory.
124 bool LoadAllFiles(const wxString
& dirname
);
126 // Unload resource from the given XML file (wildcards not allowed)
127 bool Unload(const wxString
& filename
);
129 // Initialize handlers for all supported controls/windows. This will
130 // make the executable quite big because it forces linking against
131 // most of the wxWidgets library.
132 void InitAllHandlers();
134 // Initialize only a specific handler (or custom handler). Convention says
135 // that handler name is equal to the control's name plus 'XmlHandler', for
136 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
137 // compiler (xmlres) can create include file that contains initialization
138 // code for all controls used within the resource.
139 void AddHandler(wxXmlResourceHandler
*handler
);
141 // Add a new handler at the beginning of the handler list
142 void InsertHandler(wxXmlResourceHandler
*handler
);
144 // Removes all handlers
145 void ClearHandlers();
147 // Registers subclasses factory for use in XRC. This function is not meant
148 // for public use, please see the comment above wxXmlSubclassFactory
150 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
152 // Loads menu from resource. Returns NULL on failure.
153 wxMenu
*LoadMenu(const wxString
& name
);
155 // Loads menubar from resource. Returns NULL on failure.
156 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
158 // Loads menubar from resource. Returns NULL on failure.
159 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
163 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
166 // Loads a dialog. dlg points to parent window (if any).
167 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
169 // Loads a dialog. dlg points to parent window (if any). This form
170 // is used to finish creation of already existing instance (main reason
171 // for this is that you may want to use derived class with new event table)
172 // Example (typical usage):
174 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
176 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
178 // Loads a panel. panel points to parent window (if any).
179 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
181 // Loads a panel. panel points to parent window (if any). This form
182 // is used to finish creation of already existing instance.
183 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
186 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
187 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
189 // Load an object from the resource specifying both the resource name and
190 // the classname. This lets you load nonstandard container windows.
191 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
192 const wxString
& classname
)
194 return DoLoadObject(parent
, name
, classname
, false /* !recursive */);
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
,
202 const wxString
& name
,
203 const wxString
& classname
)
205 return DoLoadObject(instance
, parent
, name
, classname
, false);
208 // These versions of LoadObject() look for the object with the given name
209 // recursively (breadth first) and can be used to instantiate an individual
210 // control defined anywhere in an XRC file. No check is done that the name
211 // is unique, it's up to the caller to ensure this.
212 wxObject
*LoadObjectRecursively(wxWindow
*parent
,
213 const wxString
& name
,
214 const wxString
& classname
)
216 return DoLoadObject(parent
, name
, classname
, true /* recursive */);
219 bool LoadObjectRecursively(wxObject
*instance
,
221 const wxString
& name
,
222 const wxString
& classname
)
224 return DoLoadObject(instance
, parent
, name
, classname
, true);
227 // Loads a bitmap resource from a file.
228 wxBitmap
LoadBitmap(const wxString
& name
);
230 // Loads an icon resource from a file.
231 wxIcon
LoadIcon(const wxString
& name
);
233 // Attaches an unknown control to the given panel/window/dialog.
234 // Unknown controls are used in conjunction with <object class="unknown">.
235 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
236 wxWindow
*parent
= NULL
);
238 // Returns a numeric ID that is equivalent to the string ID used in an XML
239 // resource. If an unknown str_id is requested (i.e. other than wxID_XXX
240 // or integer), a new record is created which associates the given string
241 // with a number. If value_if_not_found == wxID_NONE, the number is obtained via
242 // wxWindow::NewControlId(). Otherwise value_if_not_found is used.
243 // Macro XRCID(name) is provided for convenient use in event tables.
244 static int GetXRCID(const wxString
& str_id
, int value_if_not_found
= wxID_NONE
)
245 { return DoGetXRCID(str_id
.mb_str(), value_if_not_found
); }
247 // version for internal use only
248 static int DoGetXRCID(const char *str_id
, int value_if_not_found
= wxID_NONE
);
251 // Find the string ID with the given numeric value, returns an empty string
252 // if no such ID is found.
254 // Notice that unlike GetXRCID(), which is fast, this operation is slow as
255 // it checks all the IDs used in XRC.
256 static wxString
FindXRCIDById(int numId
);
259 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
260 long GetVersion() const { return m_version
; }
262 // Compares resources version to argument. Returns -1 if resources version
263 // is less than the argument, +1 if greater and 0 if they equal.
264 int CompareVersion(int major
, int minor
, int release
, int revision
) const
265 { return GetVersion() -
266 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
268 //// Singleton accessors.
270 // Gets the global resources object or creates one if none exists.
271 static wxXmlResource
*Get();
273 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
274 static wxXmlResource
*Set(wxXmlResource
*res
);
276 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
277 int GetFlags() const { return m_flags
; }
278 // Set flags after construction.
279 void SetFlags(int flags
) { m_flags
= flags
; }
281 // Get/Set the domain to be passed to the translation functions, defaults
282 // to empty string (no domain).
283 const wxString
& GetDomain() const { return m_domain
; }
284 void SetDomain(const wxString
& domain
);
287 // This function returns the wxXmlNode containing the definition of the
288 // object with the given name or NULL.
290 // It can be used to access additional information defined in the XRC file
291 // and not used by wxXmlResource itself.
292 const wxXmlNode
*GetResourceNode(const wxString
& name
) const
293 { return GetResourceNodeAndLocation(name
, wxString(), true); }
296 // reports input error at position 'context'
297 void ReportError(const wxXmlNode
*context
, const wxString
& message
);
299 // override this in derived class to customize errors reporting
300 virtual void DoReportError(const wxString
& xrcFile
, const wxXmlNode
*position
,
301 const wxString
& message
);
303 // Load the contents of a single file and returns its contents as a new
304 // wxXmlDocument (which will be owned by caller) on success or NULL.
305 wxXmlDocument
*DoLoadFile(const wxString
& file
);
307 // Scans the resources list for unloaded files and loads them. Also reloads
308 // files that have been modified since last loading.
309 bool UpdateResources();
312 // Common implementation of GetResourceNode() and FindResource(): searches
313 // all top-level or all (if recursive == true) nodes if all loaded XRC
314 // files and returns the node, if found, as well as the path of the file it
315 // was found in if path is non-NULL
316 wxXmlNode
*GetResourceNodeAndLocation(const wxString
& name
,
317 const wxString
& classname
,
318 bool recursive
= false,
319 wxString
*path
= NULL
) const;
322 // Note that these functions are used outside of wxWidgets itself, e.g.
323 // there are several known cases of inheriting from wxXmlResource just to
324 // be able to call FindResource() so we keep them for compatibility even if
325 // their names are not really consistent with GetResourceNode() public
326 // function and FindResource() is also non-const because it changes the
327 // current path of m_curFileSystem to ensure that relative paths work
328 // correctly when CreateResFromNode() is called immediately afterwards
329 // (something const public function intentionally does not do)
331 // Returns the node containing the resource with the given name and class
332 // name unless it's empty (then any class matches) or NULL if not found.
333 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
,
334 bool recursive
= false);
336 // Helper function used by FindResource() to look under the given node.
337 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
,
338 const wxString
& classname
, bool recursive
) const;
340 // Creates a resource from information in the given node
341 // (Uses only 'handlerToUse' if != NULL)
342 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
343 wxObject
*instance
= NULL
,
344 wxXmlResourceHandler
*handlerToUse
= NULL
)
346 return node
? DoCreateResFromNode(*node
, parent
, instance
, handlerToUse
)
350 // Helper of Load() and Unload(): returns the URL corresponding to the
351 // given file if it's indeed a file, otherwise returns the original string
353 static wxString
ConvertFileNameToURL(const wxString
& filename
);
355 // loading resources from archives is impossible without wxFileSystem
357 // Another helper: detect if the filename is a ZIP or XRS file
358 static bool IsArchive(const wxString
& filename
);
359 #endif // wxUSE_FILESYSTEM
362 wxXmlResourceDataRecords
& Data() { return *m_data
; }
363 const wxXmlResourceDataRecords
& Data() const { return *m_data
; }
365 // the real implementation of CreateResFromNode(): this should be only
366 // called if node is non-NULL
367 wxObject
*DoCreateResFromNode(wxXmlNode
& node
,
370 wxXmlResourceHandler
*handlerToUse
= NULL
);
372 // common part of LoadObject() and LoadObjectRecursively()
373 wxObject
*DoLoadObject(wxWindow
*parent
,
374 const wxString
& name
,
375 const wxString
& classname
,
377 bool DoLoadObject(wxObject
*instance
,
379 const wxString
& name
,
380 const wxString
& classname
,
387 wxVector
<wxXmlResourceHandler
*> m_handlers
;
388 wxXmlResourceDataRecords
*m_data
;
390 wxFileSystem m_curFileSystem
;
391 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
394 // domain to pass to translation functions, if any.
397 friend class wxXmlResourceHandlerImpl
;
398 friend class wxXmlResourceModule
;
399 friend class wxIdRangeManager
;
400 friend class wxIdRange
;
402 static wxXmlSubclassFactories
*ms_subclassFactories
;
404 // singleton instance:
405 static wxXmlResource
*ms_instance
;
409 // This macro translates string identifier (as used in XML resource,
410 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
411 // wxWidgets event tables.
413 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
414 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
415 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
416 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
417 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
420 #define XRCID(str_id) \
421 wxXmlResource::DoGetXRCID(str_id)
424 // This macro returns pointer to particular control in dialog
425 // created using XML resources. You can use it to set/get values from
429 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
430 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
432 #define XRCCTRL(window, id, type) \
433 (wxStaticCast((window).FindWindow(XRCID(id)), type))
435 // This macro returns pointer to sizer item
438 // <object class="spacer" name="area">
439 // <size>400, 300</size>
442 // wxSizerItem* item = XRCSIZERITEM(*this, "area")
444 #define XRCSIZERITEM(window, id) \
445 ((window).GetSizer() ? (window).GetSizer()->GetItemById(XRCID(id)) : NULL)
448 // wxXmlResourceHandlerImpl is the back-end of the wxXmlResourceHander class to
449 // really implementing all its functionality. It is defined in the "xrc"
450 // library unlike wxXmlResourceHandler itself which is defined in "core" to
451 // allow inheriting from it in the code from the other libraries too.
453 class WXDLLIMPEXP_XRC wxXmlResourceHandlerImpl
: public wxXmlResourceHandlerImplBase
457 wxXmlResourceHandlerImpl(wxXmlResourceHandler
*handler
);
460 virtual ~wxXmlResourceHandlerImpl() {}
462 // Creates an object (menu, dialog, control, ...) from an XML node.
463 // Should check for validity.
464 // parent is a higher-level object (usually window, dialog or panel)
465 // that is often necessary to create the resource.
466 // If instance is non-NULL it should not create a new instance via 'new' but
467 // should rather use this one, and call its Create method.
468 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
472 // --- Handy methods:
474 // Returns true if the node has a property class equal to classname,
475 // e.g. <object class="wxDialog">.
476 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
) const;
478 // Gets node content from wxXML_ENTITY_NODE
479 // The problem is, <tag>content<tag> is represented as
480 // wxXML_ENTITY_NODE name="tag", content=""
481 // |-- wxXML_TEXT_NODE or
482 // wxXML_CDATA_SECTION_NODE name="" content="content"
483 wxString
GetNodeContent(const wxXmlNode
*node
);
485 // Check to see if a parameter exists.
486 bool HasParam(const wxString
& param
);
488 // Finds the node or returns NULL.
489 wxXmlNode
*GetParamNode(const wxString
& param
);
491 // Finds the parameter value or returns the empty string.
492 wxString
GetParamValue(const wxString
& param
);
494 // Returns the parameter value from given node.
495 wxString
GetParamValue(const wxXmlNode
* node
);
497 // Gets style flags from text in form "flag | flag2| flag3 |..."
498 // Only understands flags added with AddStyle
499 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
501 // Gets text from param and does some conversions:
502 // - replaces \n, \r, \t by respective chars (according to C syntax)
503 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
504 // - calls wxGetTranslations (unless disabled in wxXmlResource)
505 wxString
GetText(const wxString
& param
, bool translate
= true);
507 // Returns the XRCID.
510 // Returns the resource name.
513 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
514 bool GetBool(const wxString
& param
, bool defaultv
= false);
516 // Gets an integer value from the parameter.
517 long GetLong(const wxString
& param
, long defaultv
= 0);
519 // Gets a float value from the parameter.
520 float GetFloat(const wxString
& param
, float defaultv
= 0);
522 // Gets colour in HTML syntax (#RRGGBB).
523 wxColour
GetColour(const wxString
& param
, const wxColour
& defaultv
= wxNullColour
);
525 // Gets the size (may be in dialog units).
526 wxSize
GetSize(const wxString
& param
= wxT("size"),
527 wxWindow
*windowToUse
= NULL
);
529 // Gets the position (may be in dialog units).
530 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
532 // Gets a dimension (may be in dialog units).
533 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
534 wxWindow
*windowToUse
= NULL
);
536 // Gets a direction, complains if the value is invalid.
537 wxDirection
GetDirection(const wxString
& param
, wxDirection dir
= wxLEFT
);
540 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
541 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
542 wxSize size
= wxDefaultSize
);
544 // Gets a bitmap from an XmlNode.
545 wxBitmap
GetBitmap(const wxXmlNode
* node
,
546 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
547 wxSize size
= wxDefaultSize
);
550 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
551 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
552 wxSize size
= wxDefaultSize
);
554 // Gets an icon from an XmlNode.
555 wxIcon
GetIcon(const wxXmlNode
* node
,
556 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
557 wxSize size
= wxDefaultSize
);
559 // Gets an icon bundle.
560 wxIconBundle
GetIconBundle(const wxString
& param
,
561 const wxArtClient
& defaultArtClient
= wxART_OTHER
);
563 // Gets an image list.
564 wxImageList
*GetImageList(const wxString
& param
= wxT("imagelist"));
566 #if wxUSE_ANIMATIONCTRL
567 // Gets an animation.
568 wxAnimation
GetAnimation(const wxString
& param
= wxT("animation"));
572 wxFont
GetFont(const wxString
& param
= wxT("font"), wxWindow
* parent
= NULL
);
574 // Gets the value of a boolean attribute (only "0" and "1" are valid values)
575 bool GetBoolAttr(const wxString
& attr
, bool defaultv
);
578 // Sets common window options.
579 void SetupWindow(wxWindow
*wnd
);
582 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false);
585 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
587 // Creates a resource from a node.
588 wxObject
*CreateResFromNode(wxXmlNode
*node
,
589 wxObject
*parent
, wxObject
*instance
= NULL
);
593 wxFileSystem
& GetCurFileSystem();
596 // reports input error at position 'context'
597 void ReportError(wxXmlNode
*context
, const wxString
& message
);
598 // reports input error at m_node
599 void ReportError(const wxString
& message
);
600 // reports input error when parsing parameter with given name
601 void ReportParamError(const wxString
& param
, const wxString
& message
);
605 // Programmer-friendly macros for writing XRC handlers:
607 #define XRC_MAKE_INSTANCE(variable, classname) \
608 classname *variable = NULL; \
610 variable = wxStaticCast(m_instance, classname); \
612 variable = new classname;
615 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
616 WXDLLIMPEXP_XRC
void wxXmlInitResourceModule();
619 // This class is used to create instances of XRC "object" nodes with "subclass"
620 // property. It is _not_ supposed to be used by XRC users, you should instead
621 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
622 // only for language bindings developer who need a way to implement subclassing
623 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
624 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
627 // Try to create instance of given class and return it, return NULL on
629 virtual wxObject
*Create(const wxString
& className
) = 0;
630 virtual ~wxXmlSubclassFactory() {}
634 /* -------------------------------------------------------------------------
635 Backward compatibility macros. Do *NOT* use, they may disappear in future
636 versions of the XRC library!
637 ------------------------------------------------------------------------- */
641 #endif // _WX_XMLRES_H_