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/datetime.h"
22 #include "wx/gdicmn.h"
23 #include "wx/filesys.h"
24 #include "wx/bitmap.h"
26 #include "wx/artprov.h"
27 #include "wx/colour.h"
29 #include "wx/xml/xml.h"
31 class WXDLLEXPORT wxMenu
;
32 class WXDLLEXPORT wxMenuBar
;
33 class WXDLLEXPORT wxDialog
;
34 class WXDLLEXPORT wxPanel
;
35 class WXDLLEXPORT wxWindow
;
36 class WXDLLEXPORT wxFrame
;
37 class WXDLLEXPORT wxToolBar
;
39 class WXDLLIMPEXP_XRC wxXmlResourceHandler
;
40 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
;
41 class WXDLLIMPEXP_XRC wxXmlSubclassFactoriesList
;
42 class wxXmlResourceModule
;
45 // These macros indicate current version of XML resources (this information is
46 // encoded in root node of XRC file as "version" property).
48 // Rules for increasing version number:
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.
52 // - if you change version number, follow these steps:
53 // - set major, minor and release numbers to respective version numbers of
54 // the wxWidgets library (see wx/version.h)
55 // - reset revision to 0 unless the first three are same as before,
56 // in which case you should increase revision by one
57 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
58 #define WX_XMLRES_CURRENT_VERSION_MINOR 5
59 #define WX_XMLRES_CURRENT_VERSION_RELEASE 3
60 #define WX_XMLRES_CURRENT_VERSION_REVISION 0
61 #define WX_XMLRES_CURRENT_VERSION_STRING _T("2.5.3.0")
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)
69 class WXDLLIMPEXP_XRC wxXmlResourceDataRecord
72 wxXmlResourceDataRecord() : Doc(NULL
) {
74 Time
= wxDateTime::Now();
77 ~wxXmlResourceDataRecord() {delete Doc
;}
87 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
,
88 wxXmlResourceDataRecords
,
91 enum wxXmlResourceFlags
94 wxXRC_NO_SUBCLASSING
= 2,
95 wxXRC_NO_RELOADING
= 4
98 // This class holds XML resources from one or more .xml files
99 // (or derived forms, either binary or zipped -- see manual for
101 class WXDLLIMPEXP_XRC wxXmlResource
: public wxObject
105 // Flags: wxXRC_USE_LOCALE
106 // translatable strings will be translated via _()
107 // using the given domain if specified
108 // wxXRC_NO_SUBCLASSING
109 // subclass property of object nodes will be ignored
110 // (useful for previews in XRC editors)
111 // wxXRC_NO_RELOADING
112 // don't check the modification time of the XRC files and
113 // reload them if they have changed on disk
114 wxXmlResource(int flags
= wxXRC_USE_LOCALE
,
115 const wxString
& domain
=wxEmptyString
);
118 // Flags: wxXRC_USE_LOCALE
119 // translatable strings will be translated via _()
120 // using the given domain if specified
121 // wxXRC_NO_SUBCLASSING
122 // subclass property of object nodes will be ignored
123 // (useful for previews in XRC editors)
124 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
,
125 const wxString
& domain
=wxEmptyString
);
130 // Loads resources from XML files that match given filemask.
131 // This method understands VFS (see filesys.h).
132 bool Load(const wxString
& filemask
);
134 // Unload resource from the given XML file (wildcards not allowed)
135 bool Unload(const wxString
& filename
);
137 // Initialize handlers for all supported controls/windows. This will
138 // make the executable quite big because it forces linking against
139 // most of the wxWidgets library.
140 void InitAllHandlers();
142 // Initialize only a specific handler (or custom handler). Convention says
143 // that handler name is equal to the control's name plus 'XmlHandler', for
144 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
145 // compiler (xmlres) can create include file that contains initialization
146 // code for all controls used within the resource.
147 void AddHandler(wxXmlResourceHandler
*handler
);
149 // Add a new handler at the begining of the handler list
150 void InsertHandler(wxXmlResourceHandler
*handler
);
152 // Removes all handlers
153 void ClearHandlers();
155 // Registers subclasses factory for use in XRC. This function is not meant
156 // for public use, please see the comment above wxXmlSubclassFactory
158 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
160 // Loads menu from resource. Returns NULL on failure.
161 wxMenu
*LoadMenu(const wxString
& name
);
163 // Loads menubar from resource. Returns NULL on failure.
164 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
166 // Loads menubar from resource. Returns NULL on failure.
167 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
171 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
174 // Loads a dialog. dlg points to parent window (if any).
175 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
177 // Loads a dialog. dlg points to parent window (if any). This form
178 // is used to finish creation of already existing instance (main reason
179 // for this is that you may want to use derived class with new event table)
180 // Example (typical usage):
182 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
184 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
186 // Loads a panel. panel points to parent window (if any).
187 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
189 // Loads a panel. panel points to parent window (if any). This form
190 // is used to finish creation of already existing instance.
191 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
194 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
195 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
197 // Load an object from the resource specifying both the resource name and
198 // the classname. This lets you load nonstandard container windows.
199 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
200 const wxString
& classname
);
202 // Load an object from the resource specifying both the resource name and
203 // the classname. This form lets you finish the creation of an existing
205 bool LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
,
206 const wxString
& classname
);
208 // Loads a bitmap resource from a file.
209 wxBitmap
LoadBitmap(const wxString
& name
);
211 // Loads an icon resource from a file.
212 wxIcon
LoadIcon(const wxString
& name
);
214 // Attaches an unknown control to the given panel/window/dialog.
215 // Unknown controls are used in conjunction with <object class="unknown">.
216 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
217 wxWindow
*parent
= NULL
);
219 // Returns a numeric ID that is equivalent to the string ID used in an XML
220 // resource. If an unknown str_id is requested (i.e. other than wxID_XXX
221 // or integer), a new record is created which associates the given string
222 // with a number. If value_if_not_found == wxID_NONE, the number is obtained via
223 // wxNewId(). Otherwise value_if_not_found is used.
224 // Macro XRCID(name) is provided for convenient use in event tables.
225 static int GetXRCID(const wxChar
*str_id
, int value_if_not_found
= wxID_NONE
);
227 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
228 long GetVersion() const { return m_version
; }
230 // Compares resources version to argument. Returns -1 if resources version
231 // is less than the argument, +1 if greater and 0 if they equal.
232 int CompareVersion(int major
, int minor
, int release
, int revision
) const
233 { return GetVersion() -
234 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
236 //// Singleton accessors.
238 // Gets the global resources object or creates one if none exists.
239 static wxXmlResource
*Get();
241 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
242 static wxXmlResource
*Set(wxXmlResource
*res
);
244 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
245 int GetFlags() const { return m_flags
; }
246 // Set flags after construction.
247 void SetFlags(int flags
) { m_flags
= flags
; }
249 // Get/Set the domain to be passed to the translation functions, defaults to NULL.
250 wxChar
* GetDomain() const { return m_domain
; }
251 void SetDomain(const wxChar
* domain
);
254 // Scans the resources list for unloaded files and loads them. Also reloads
255 // files that have been modified since last loading.
256 bool UpdateResources();
258 // Finds a resource (calls UpdateResources) and returns a node containing it.
259 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= false);
261 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
262 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
264 // Creates a resource from information in the given node
265 // (Uses only 'handlerToUse' if != NULL)
266 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
267 wxObject
*instance
= NULL
,
268 wxXmlResourceHandler
*handlerToUse
= NULL
);
270 // Helper of Load() and Unload(): returns the URL corresponding to the
271 // given file if it's indeed a file, otherwise returns the original string
273 static wxString
ConvertFileNameToURL(const wxString
& filename
);
275 // loading resources from archives is impossible without wxFileSystem
277 // Another helper: detect if the filename is a ZIP or XRS file
278 static bool IsArchive(const wxString
& filename
);
279 #endif // wxUSE_FILESYSTEM
286 wxXmlResourceDataRecords m_data
;
288 wxFileSystem m_curFileSystem
;
289 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
292 // domain to pass to translation functions, if any.
295 friend class wxXmlResourceHandler
;
296 friend class wxXmlResourceModule
;
298 static wxXmlSubclassFactoriesList
*ms_subclassFactories
;
300 // singleton instance:
301 static wxXmlResource
*ms_instance
;
305 // This macro translates string identifier (as used in XML resource,
306 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
307 // wxWidgets event tables.
309 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
310 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
311 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
312 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
313 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
316 #define XRCID(str_id) \
317 wxXmlResource::GetXRCID(wxT(str_id))
320 // This macro returns pointer to particular control in dialog
321 // created using XML resources. You can use it to set/get values from
325 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
326 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
328 #define XRCCTRL(window, id, type) \
329 (wxStaticCast((window).FindWindow(XRCID(id)), type))
331 // wxXmlResourceHandler is an abstract base class for resource handlers
332 // capable of creating a control from an XML node.
334 class WXDLLIMPEXP_XRC wxXmlResourceHandler
: public wxObject
336 DECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
)
339 wxXmlResourceHandler();
342 virtual ~wxXmlResourceHandler() {}
344 // Creates an object (menu, dialog, control, ...) from an XML node.
345 // Should check for validity.
346 // parent is a higher-level object (usually window, dialog or panel)
347 // that is often necessary to create the resource.
348 // If instance is non-NULL it should not create a new instance via 'new' but
349 // should rather use this one, and call its Create method.
350 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
353 // This one is called from CreateResource after variables
355 virtual wxObject
*DoCreateResource() = 0;
357 // Returns true if it understands this node and can create
358 // a resource from it, false otherwise.
359 virtual bool CanHandle(wxXmlNode
*node
) = 0;
361 // Sets the parent resource.
362 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
365 wxXmlResource
*m_resource
;
366 wxArrayString m_styleNames
;
367 wxArrayInt m_styleValues
;
369 // Variables (filled by CreateResource)
372 wxObject
*m_parent
, *m_instance
;
373 wxWindow
*m_parentAsWindow
;
375 // --- Handy methods:
377 // Returns true if the node has a property class equal to classname,
378 // e.g. <object class="wxDialog">.
379 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
);
381 // Gets node content from wxXML_ENTITY_NODE
382 // The problem is, <tag>content<tag> is represented as
383 // wxXML_ENTITY_NODE name="tag", content=""
384 // |-- wxXML_TEXT_NODE or
385 // wxXML_CDATA_SECTION_NODE name="" content="content"
386 wxString
GetNodeContent(wxXmlNode
*node
);
388 // Check to see if a parameter exists.
389 bool HasParam(const wxString
& param
);
391 // Finds the node or returns NULL.
392 wxXmlNode
*GetParamNode(const wxString
& param
);
394 // Finds the parameter value or returns the empty string.
395 wxString
GetParamValue(const wxString
& param
);
397 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
398 // understood by this handler.
399 void AddStyle(const wxString
& name
, int value
);
401 // Add styles common to all wxWindow-derived classes.
402 void AddWindowStyles();
404 // Gets style flags from text in form "flag | flag2| flag3 |..."
405 // Only understands flags added with AddStyle
406 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
408 // Gets text from param and does some conversions:
409 // - replaces \n, \r, \t by respective chars (according to C syntax)
410 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
411 // - calls wxGetTranslations (unless disabled in wxXmlResource)
412 wxString
GetText(const wxString
& param
, bool translate
= true);
414 // Returns the XRCID.
417 // Returns the resource name.
420 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
421 bool GetBool(const wxString
& param
, bool defaultv
= false);
423 // Gets an integer value from the parameter.
424 long GetLong(const wxString
& param
, long defaultv
= 0);
426 // Gets a float value from the parameter.
427 float GetFloat(const wxString
& param
, float defaultv
= 0);
429 // Gets colour in HTML syntax (#RRGGBB).
430 wxColour
GetColour(const wxString
& param
, const wxColour
& defaultv
= wxNullColour
);
432 // Gets the size (may be in dialog units).
433 wxSize
GetSize(const wxString
& param
= wxT("size"),
434 wxWindow
*windowToUse
= NULL
);
436 // Gets the position (may be in dialog units).
437 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
439 // Gets a dimension (may be in dialog units).
440 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
441 wxWindow
*windowToUse
= NULL
);
444 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
445 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
446 wxSize size
= wxDefaultSize
);
449 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
450 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
451 wxSize size
= wxDefaultSize
);
454 wxFont
GetFont(const wxString
& param
= wxT("font"));
456 // Sets common window options.
457 void SetupWindow(wxWindow
*wnd
);
460 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false);
463 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
465 // Creates a resource from a node.
466 wxObject
*CreateResFromNode(wxXmlNode
*node
,
467 wxObject
*parent
, wxObject
*instance
= NULL
)
468 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
472 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
477 // Programmer-friendly macros for writing XRC handlers:
479 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
481 #define XRC_MAKE_INSTANCE(variable, classname) \
482 classname *variable = NULL; \
484 variable = wxStaticCast(m_instance, classname); \
486 variable = new classname;
489 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
490 WXDLLIMPEXP_XRC
void wxXmlInitResourceModule();
493 // This class is used to create instances of XRC "object" nodes with "subclass"
494 // property. It is _not_ supposed to be used by XRC users, you should instead
495 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
496 // only for language bindings developer who need a way to implement subclassing
497 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
498 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
501 // Try to create instance of given class and return it, return NULL on
503 virtual wxObject
*Create(const wxString
& className
) = 0;
504 virtual ~wxXmlSubclassFactory() {}
508 /* -------------------------------------------------------------------------
509 Backward compatibility macros. Do *NOT* use, they may disappear in future
510 versions of the XRC library!
511 ------------------------------------------------------------------------- */
512 #if WXWIN_COMPATIBILITY_2_4
513 #define ADD_STYLE XRC_ADD_STYLE
514 #define wxTheXmlResource wxXmlResource::Get()
516 #define XMLCTRL XRCCTRL
517 #define GetXMLID GetXRCID
522 #endif // _WX_XMLRES_H_