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"
31 #include "wx/xml/xml.h"
33 class WXDLLEXPORT wxMenu
;
34 class WXDLLEXPORT wxMenuBar
;
35 class WXDLLEXPORT wxDialog
;
36 class WXDLLEXPORT wxPanel
;
37 class WXDLLEXPORT wxWindow
;
38 class WXDLLEXPORT wxFrame
;
39 class WXDLLEXPORT wxToolBar
;
41 class WXDLLIMPEXP_XRC wxXmlResourceHandler
;
42 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
;
43 class WXDLLIMPEXP_XRC wxXmlSubclassFactoriesList
;
44 class wxXmlResourceModule
;
47 // These macros indicate current version of XML resources (this information is
48 // encoded in root node of XRC file as "version" property).
50 // Rules for increasing version number:
51 // - change it only if you made incompatible change to the format. Addition
52 // of new attribute to control handler is _not_ incompatible change, because
53 // older versions of the library may ignore it.
54 // - if you change version number, follow these steps:
55 // - set major, minor and release numbers to respective version numbers of
56 // the wxWidgets library (see wx/version.h)
57 // - reset revision to 0 unless the first three are same as before,
58 // in which case you should increase revision by one
59 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
60 #define WX_XMLRES_CURRENT_VERSION_MINOR 5
61 #define WX_XMLRES_CURRENT_VERSION_RELEASE 3
62 #define WX_XMLRES_CURRENT_VERSION_REVISION 0
63 #define WX_XMLRES_CURRENT_VERSION_STRING _T("2.5.3.0")
65 #define WX_XMLRES_CURRENT_VERSION \
66 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
67 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
68 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
69 WX_XMLRES_CURRENT_VERSION_REVISION)
71 class WXDLLIMPEXP_XRC wxXmlResourceDataRecord
74 wxXmlResourceDataRecord() : Doc(NULL
) {
76 Time
= wxDateTime::Now();
79 ~wxXmlResourceDataRecord() {delete Doc
;}
89 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
,
90 wxXmlResourceDataRecords
,
93 enum wxXmlResourceFlags
96 wxXRC_NO_SUBCLASSING
= 2,
97 wxXRC_NO_RELOADING
= 4
100 // This class holds XML resources from one or more .xml files
101 // (or derived forms, either binary or zipped -- see manual for
103 class WXDLLIMPEXP_XRC wxXmlResource
: public wxObject
107 // Flags: wxXRC_USE_LOCALE
108 // translatable strings will be translated via _()
109 // using the given domain if specified
110 // wxXRC_NO_SUBCLASSING
111 // subclass property of object nodes will be ignored
112 // (useful for previews in XRC editors)
113 // wxXRC_NO_RELOADING
114 // don't check the modification time of the XRC files and
115 // reload them if they have changed on disk
116 wxXmlResource(int flags
= wxXRC_USE_LOCALE
,
117 const wxString
& domain
= wxEmptyString
);
120 // Flags: wxXRC_USE_LOCALE
121 // translatable strings will be translated via _()
122 // using the given domain if specified
123 // wxXRC_NO_SUBCLASSING
124 // subclass property of object nodes will be ignored
125 // (useful for previews in XRC editors)
126 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
,
127 const wxString
& domain
= wxEmptyString
);
130 virtual ~wxXmlResource();
132 wxXmlNode
*GetFirstRoot();
134 // Loads resources from XML files that match given filemask.
135 // This method understands VFS (see filesys.h).
136 bool Load(const wxString
& filemask
);
138 // Unload resource from the given XML file (wildcards not allowed)
139 bool Unload(const wxString
& filename
);
141 // Initialize handlers for all supported controls/windows. This will
142 // make the executable quite big because it forces linking against
143 // most of the wxWidgets library.
144 void InitAllHandlers();
146 // Initialize only a specific handler (or custom handler). Convention says
147 // that handler name is equal to the control's name plus 'XmlHandler', for
148 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
149 // compiler (xmlres) can create include file that contains initialization
150 // code for all controls used within the resource.
151 void AddHandler(wxXmlResourceHandler
*handler
);
153 // Add a new handler at the begining of the handler list
154 void InsertHandler(wxXmlResourceHandler
*handler
);
156 // Removes all handlers
157 void ClearHandlers();
159 // Registers subclasses factory for use in XRC. This function is not meant
160 // for public use, please see the comment above wxXmlSubclassFactory
162 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
164 // Loads menu from resource. Returns NULL on failure.
165 wxMenu
*LoadMenu(const wxString
& name
);
167 // Loads menubar from resource. Returns NULL on failure.
168 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
170 // Loads menubar from resource. Returns NULL on failure.
171 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
175 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
178 // Loads a dialog. dlg points to parent window (if any).
179 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
181 // Loads a dialog. dlg points to parent window (if any). This form
182 // is used to finish creation of already existing instance (main reason
183 // for this is that you may want to use derived class with new event table)
184 // Example (typical usage):
186 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
188 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
190 // Loads a panel. panel points to parent window (if any).
191 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
193 // Loads a panel. panel points to parent window (if any). This form
194 // is used to finish creation of already existing instance.
195 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
198 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
199 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
201 // Load an object from the resource specifying both the resource name and
202 // the classname. This lets you load nonstandard container windows.
203 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
204 const wxString
& classname
);
206 // Load an object from the resource specifying both the resource name and
207 // the classname. This form lets you finish the creation of an existing
209 bool LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
,
210 const wxString
& classname
);
212 // Loads a bitmap resource from a file.
213 wxBitmap
LoadBitmap(const wxString
& name
);
215 // Loads an icon resource from a file.
216 wxIcon
LoadIcon(const wxString
& name
);
218 // Attaches an unknown control to the given panel/window/dialog.
219 // Unknown controls are used in conjunction with <object class="unknown">.
220 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
221 wxWindow
*parent
= NULL
);
223 // Returns a numeric ID that is equivalent to the string ID used in an XML
224 // resource. If an unknown str_id is requested (i.e. other than wxID_XXX
225 // or integer), a new record is created which associates the given string
226 // with a number. If value_if_not_found == wxID_NONE, the number is obtained via
227 // wxWindow::NewControlId(). Otherwise value_if_not_found is used.
228 // Macro XRCID(name) is provided for convenient use in event tables.
229 static int GetXRCID(const wxString
& str_id
, int value_if_not_found
= wxID_NONE
)
230 { return DoGetXRCID(str_id
.mb_str(), value_if_not_found
); }
232 // version for internal use only
233 static int DoGetXRCID(const char *str_id
, int value_if_not_found
= wxID_NONE
);
235 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
236 long GetVersion() const { return m_version
; }
238 // Compares resources version to argument. Returns -1 if resources version
239 // is less than the argument, +1 if greater and 0 if they equal.
240 int CompareVersion(int major
, int minor
, int release
, int revision
) const
241 { return GetVersion() -
242 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
244 //// Singleton accessors.
246 // Gets the global resources object or creates one if none exists.
247 static wxXmlResource
*Get();
249 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
250 static wxXmlResource
*Set(wxXmlResource
*res
);
252 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
253 int GetFlags() const { return m_flags
; }
254 // Set flags after construction.
255 void SetFlags(int flags
) { m_flags
= flags
; }
257 // Get/Set the domain to be passed to the translation functions, defaults
258 // to empty string (no domain).
259 const wxString
& GetDomain() const { return m_domain
; }
260 void SetDomain(const wxString
& domain
);
263 // Scans the resources list for unloaded files and loads them. Also reloads
264 // files that have been modified since last loading.
265 bool UpdateResources();
267 // Finds a resource (calls UpdateResources) and returns a node containing it.
268 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= false);
270 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
271 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
273 // Creates a resource from information in the given node
274 // (Uses only 'handlerToUse' if != NULL)
275 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
276 wxObject
*instance
= NULL
,
277 wxXmlResourceHandler
*handlerToUse
= NULL
);
279 // Helper of Load() and Unload(): returns the URL corresponding to the
280 // given file if it's indeed a file, otherwise returns the original string
282 static wxString
ConvertFileNameToURL(const wxString
& filename
);
284 // loading resources from archives is impossible without wxFileSystem
286 // Another helper: detect if the filename is a ZIP or XRS file
287 static bool IsArchive(const wxString
& filename
);
288 #endif // wxUSE_FILESYSTEM
295 wxXmlResourceDataRecords m_data
;
297 wxFileSystem m_curFileSystem
;
298 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
301 // domain to pass to translation functions, if any.
304 friend class wxXmlResourceHandler
;
305 friend class wxXmlResourceModule
;
307 static wxXmlSubclassFactoriesList
*ms_subclassFactories
;
309 // singleton instance:
310 static wxXmlResource
*ms_instance
;
314 // This macro translates string identifier (as used in XML resource,
315 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
316 // wxWidgets event tables.
318 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
319 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
320 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
321 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
322 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
325 #define XRCID(str_id) \
326 wxXmlResource::DoGetXRCID(str_id)
329 // This macro returns pointer to particular control in dialog
330 // created using XML resources. You can use it to set/get values from
334 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
335 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
337 #define XRCCTRL(window, id, type) \
338 (wxStaticCast((window).FindWindow(XRCID(id)), type))
340 // wxXmlResourceHandler is an abstract base class for resource handlers
341 // capable of creating a control from an XML node.
343 class WXDLLIMPEXP_XRC wxXmlResourceHandler
: public wxObject
345 DECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
)
348 wxXmlResourceHandler();
351 virtual ~wxXmlResourceHandler() {}
353 // Creates an object (menu, dialog, control, ...) from an XML node.
354 // Should check for validity.
355 // parent is a higher-level object (usually window, dialog or panel)
356 // that is often necessary to create the resource.
357 // If instance is non-NULL it should not create a new instance via 'new' but
358 // should rather use this one, and call its Create method.
359 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
362 // This one is called from CreateResource after variables
364 virtual wxObject
*DoCreateResource() = 0;
366 // Returns true if it understands this node and can create
367 // a resource from it, false otherwise.
368 virtual bool CanHandle(wxXmlNode
*node
) = 0;
370 // Sets the parent resource.
371 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
374 wxXmlResource
*m_resource
;
375 wxArrayString m_styleNames
;
376 wxArrayInt m_styleValues
;
378 // Variables (filled by CreateResource)
381 wxObject
*m_parent
, *m_instance
;
382 wxWindow
*m_parentAsWindow
;
384 // --- Handy methods:
386 // Returns true if the node has a property class equal to classname,
387 // e.g. <object class="wxDialog">.
388 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
);
390 // Gets node content from wxXML_ENTITY_NODE
391 // The problem is, <tag>content<tag> is represented as
392 // wxXML_ENTITY_NODE name="tag", content=""
393 // |-- wxXML_TEXT_NODE or
394 // wxXML_CDATA_SECTION_NODE name="" content="content"
395 wxString
GetNodeContent(wxXmlNode
*node
);
397 // Check to see if a parameter exists.
398 bool HasParam(const wxString
& param
);
400 // Finds the node or returns NULL.
401 wxXmlNode
*GetParamNode(const wxString
& param
);
403 // Finds the parameter value or returns the empty string.
404 wxString
GetParamValue(const wxString
& param
);
406 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
407 // understood by this handler.
408 void AddStyle(const wxString
& name
, int value
);
410 // Add styles common to all wxWindow-derived classes.
411 void AddWindowStyles();
413 // Gets style flags from text in form "flag | flag2| flag3 |..."
414 // Only understands flags added with AddStyle
415 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
417 // Gets text from param and does some conversions:
418 // - replaces \n, \r, \t by respective chars (according to C syntax)
419 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
420 // - calls wxGetTranslations (unless disabled in wxXmlResource)
421 wxString
GetText(const wxString
& param
, bool translate
= true);
423 // Returns the XRCID.
426 // Returns the resource name.
429 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
430 bool GetBool(const wxString
& param
, bool defaultv
= false);
432 // Gets an integer value from the parameter.
433 long GetLong(const wxString
& param
, long defaultv
= 0);
435 // Gets a float value from the parameter.
436 float GetFloat(const wxString
& param
, float defaultv
= 0);
438 // Gets colour in HTML syntax (#RRGGBB).
439 wxColour
GetColour(const wxString
& param
, const wxColour
& defaultv
= wxNullColour
);
441 // Gets the size (may be in dialog units).
442 wxSize
GetSize(const wxString
& param
= wxT("size"),
443 wxWindow
*windowToUse
= NULL
);
445 // Gets the position (may be in dialog units).
446 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
448 // Gets a dimension (may be in dialog units).
449 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
450 wxWindow
*windowToUse
= NULL
);
453 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
454 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
455 wxSize size
= wxDefaultSize
);
458 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
459 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
460 wxSize size
= wxDefaultSize
);
462 #if wxUSE_ANIMATIONCTRL
463 // Gets an animation.
464 wxAnimation
GetAnimation(const wxString
& param
= wxT("animation"));
468 wxFont
GetFont(const wxString
& param
= wxT("font"));
470 // Sets common window options.
471 void SetupWindow(wxWindow
*wnd
);
474 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false);
477 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
479 // Creates a resource from a node.
480 wxObject
*CreateResFromNode(wxXmlNode
*node
,
481 wxObject
*parent
, wxObject
*instance
= NULL
)
482 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
486 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
491 // Programmer-friendly macros for writing XRC handlers:
493 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
495 #define XRC_MAKE_INSTANCE(variable, classname) \
496 classname *variable = NULL; \
498 variable = wxStaticCast(m_instance, classname); \
500 variable = new classname;
503 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
504 WXDLLIMPEXP_XRC
void wxXmlInitResourceModule();
507 // This class is used to create instances of XRC "object" nodes with "subclass"
508 // property. It is _not_ supposed to be used by XRC users, you should instead
509 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
510 // only for language bindings developer who need a way to implement subclassing
511 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
512 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
515 // Try to create instance of given class and return it, return NULL on
517 virtual wxObject
*Create(const wxString
& className
) = 0;
518 virtual ~wxXmlSubclassFactory() {}
522 /* -------------------------------------------------------------------------
523 Backward compatibility macros. Do *NOT* use, they may disappear in future
524 versions of the XRC library!
525 ------------------------------------------------------------------------- */
529 #endif // _WX_XMLRES_H_