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"
28 #include "wx/animate.h"
29 #include "wx/vector.h"
31 #include "wx/xml/xml.h"
33 class WXDLLIMPEXP_FWD_CORE wxMenu
;
34 class WXDLLIMPEXP_FWD_CORE wxMenuBar
;
35 class WXDLLIMPEXP_FWD_CORE wxDialog
;
36 class WXDLLIMPEXP_FWD_CORE wxPanel
;
37 class WXDLLIMPEXP_FWD_CORE wxWindow
;
38 class WXDLLIMPEXP_FWD_CORE wxFrame
;
39 class WXDLLIMPEXP_FWD_CORE wxToolBar
;
41 class WXDLLIMPEXP_FWD_XRC wxXmlResourceHandler
;
42 class WXDLLIMPEXP_FWD_XRC wxXmlSubclassFactory
;
43 class wxXmlSubclassFactories
;
44 class wxXmlResourceModule
;
45 class wxXmlResourceDataRecords
;
47 // make VC++ happy: otherwise it gives warnings when using this type for a
48 // member of DLL-exported wxXmlResource class in shared builds
49 template class WXDLLIMPEXP_FWD_XRC wxVector
<wxXmlResourceHandler
*>;
51 // These macros indicate current version of XML resources (this information is
52 // encoded in root node of XRC file as "version" property).
54 // Rules for increasing version number:
55 // - change it only if you made incompatible change to the format. Addition
56 // of new attribute to control handler is _not_ incompatible change, because
57 // older versions of the library may ignore it.
58 // - if you change version number, follow these steps:
59 // - set major, minor and release numbers to respective version numbers of
60 // the wxWidgets library (see wx/version.h)
61 // - reset revision to 0 unless the first three are same as before,
62 // in which case you should increase revision by one
63 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
64 #define WX_XMLRES_CURRENT_VERSION_MINOR 5
65 #define WX_XMLRES_CURRENT_VERSION_RELEASE 3
66 #define WX_XMLRES_CURRENT_VERSION_REVISION 0
67 #define WX_XMLRES_CURRENT_VERSION_STRING _T("2.5.3.0")
69 #define WX_XMLRES_CURRENT_VERSION \
70 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
71 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
72 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
73 WX_XMLRES_CURRENT_VERSION_REVISION)
75 enum wxXmlResourceFlags
78 wxXRC_NO_SUBCLASSING
= 2,
79 wxXRC_NO_RELOADING
= 4
82 // This class holds XML resources from one or more .xml files
83 // (or derived forms, either binary or zipped -- see manual for
85 class WXDLLIMPEXP_XRC wxXmlResource
: public wxObject
89 // Flags: wxXRC_USE_LOCALE
90 // translatable strings will be translated via _()
91 // using the given domain if specified
92 // wxXRC_NO_SUBCLASSING
93 // subclass property of object nodes will be ignored
94 // (useful for previews in XRC editors)
96 // don't check the modification time of the XRC files and
97 // reload them if they have changed on disk
98 wxXmlResource(int flags
= wxXRC_USE_LOCALE
,
99 const wxString
& domain
= wxEmptyString
);
102 // Flags: wxXRC_USE_LOCALE
103 // translatable strings will be translated via _()
104 // using the given domain if specified
105 // wxXRC_NO_SUBCLASSING
106 // subclass property of object nodes will be ignored
107 // (useful for previews in XRC editors)
108 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
,
109 const wxString
& domain
= wxEmptyString
);
112 virtual ~wxXmlResource();
114 wxXmlNode
*GetFirstRoot();
116 // Loads resources from XML files that match given filemask.
117 // This method understands VFS (see filesys.h).
118 bool Load(const wxString
& filemask
);
120 // Unload resource from the given XML file (wildcards not allowed)
121 bool Unload(const wxString
& filename
);
123 // Initialize handlers for all supported controls/windows. This will
124 // make the executable quite big because it forces linking against
125 // most of the wxWidgets library.
126 void InitAllHandlers();
128 // Initialize only a specific handler (or custom handler). Convention says
129 // that handler name is equal to the control's name plus 'XmlHandler', for
130 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
131 // compiler (xmlres) can create include file that contains initialization
132 // code for all controls used within the resource.
133 void AddHandler(wxXmlResourceHandler
*handler
);
135 // Add a new handler at the begining of the handler list
136 void InsertHandler(wxXmlResourceHandler
*handler
);
138 // Removes all handlers
139 void ClearHandlers();
141 // Registers subclasses factory for use in XRC. This function is not meant
142 // for public use, please see the comment above wxXmlSubclassFactory
144 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
146 // Loads menu from resource. Returns NULL on failure.
147 wxMenu
*LoadMenu(const wxString
& name
);
149 // Loads menubar from resource. Returns NULL on failure.
150 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
152 // Loads menubar from resource. Returns NULL on failure.
153 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
157 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
160 // Loads a dialog. dlg points to parent window (if any).
161 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
163 // Loads a dialog. dlg points to parent window (if any). This form
164 // is used to finish creation of already existing instance (main reason
165 // for this is that you may want to use derived class with new event table)
166 // Example (typical usage):
168 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
170 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
172 // Loads a panel. panel points to parent window (if any).
173 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
175 // Loads a panel. panel points to parent window (if any). This form
176 // is used to finish creation of already existing instance.
177 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
180 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
181 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
183 // Load an object from the resource specifying both the resource name and
184 // the classname. This lets you load nonstandard container windows.
185 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
186 const wxString
& classname
);
188 // Load an object from the resource specifying both the resource name and
189 // the classname. This form lets you finish the creation of an existing
191 bool LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
,
192 const wxString
& classname
);
194 // Loads a bitmap resource from a file.
195 wxBitmap
LoadBitmap(const wxString
& name
);
197 // Loads an icon resource from a file.
198 wxIcon
LoadIcon(const wxString
& name
);
200 // Attaches an unknown control to the given panel/window/dialog.
201 // Unknown controls are used in conjunction with <object class="unknown">.
202 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
203 wxWindow
*parent
= NULL
);
205 // Returns a numeric ID that is equivalent to the string ID used in an XML
206 // resource. If an unknown str_id is requested (i.e. other than wxID_XXX
207 // or integer), a new record is created which associates the given string
208 // with a number. If value_if_not_found == wxID_NONE, the number is obtained via
209 // wxWindow::NewControlId(). Otherwise value_if_not_found is used.
210 // Macro XRCID(name) is provided for convenient use in event tables.
211 static int GetXRCID(const wxString
& str_id
, int value_if_not_found
= wxID_NONE
)
212 { return DoGetXRCID(str_id
.mb_str(), value_if_not_found
); }
214 // version for internal use only
215 static int DoGetXRCID(const char *str_id
, int value_if_not_found
= wxID_NONE
);
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
; }
239 // Get/Set the domain to be passed to the translation functions, defaults
240 // to empty string (no domain).
241 const wxString
& GetDomain() const { return m_domain
; }
242 void SetDomain(const wxString
& domain
);
245 // Scans the resources list for unloaded files and loads them. Also reloads
246 // files that have been modified since last loading.
247 bool UpdateResources();
249 // Finds a resource (calls UpdateResources) and returns a node containing it.
250 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= false);
252 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
253 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
255 // Creates a resource from information in the given node
256 // (Uses only 'handlerToUse' if != NULL)
257 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
258 wxObject
*instance
= NULL
,
259 wxXmlResourceHandler
*handlerToUse
= NULL
);
261 // Helper of Load() and Unload(): returns the URL corresponding to the
262 // given file if it's indeed a file, otherwise returns the original string
264 static wxString
ConvertFileNameToURL(const wxString
& filename
);
266 // loading resources from archives is impossible without wxFileSystem
268 // Another helper: detect if the filename is a ZIP or XRS file
269 static bool IsArchive(const wxString
& filename
);
270 #endif // wxUSE_FILESYSTEM
273 wxXmlResourceDataRecords
& Data() { return *m_data
; }
274 const wxXmlResourceDataRecords
& Data() const { return *m_data
; }
280 wxVector
<wxXmlResourceHandler
*> m_handlers
;
281 wxXmlResourceDataRecords
*m_data
;
283 wxFileSystem m_curFileSystem
;
284 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
287 // domain to pass to translation functions, if any.
290 friend class wxXmlResourceHandler
;
291 friend class wxXmlResourceModule
;
293 static wxXmlSubclassFactories
*ms_subclassFactories
;
295 // singleton instance:
296 static wxXmlResource
*ms_instance
;
300 // This macro translates string identifier (as used in XML resource,
301 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
302 // wxWidgets event tables.
304 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
305 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
306 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
307 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
308 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
311 #define XRCID(str_id) \
312 wxXmlResource::DoGetXRCID(str_id)
315 // This macro returns pointer to particular control in dialog
316 // created using XML resources. You can use it to set/get values from
320 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
321 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
323 #define XRCCTRL(window, id, type) \
324 (wxStaticCast((window).FindWindow(XRCID(id)), type))
326 // This macro returns pointer to sizer item
329 // <object class="spacer" name="area">
330 // <size>400, 300</size>
333 // wxSizerItem* item = XRCSIZERITEM(*this, wxT("area"))
335 #define XRCSIZERITEM(window, id) \
336 ((window).GetSizer() ? (window).GetSizer()->GetItemById(id) : NULL)
338 // wxXmlResourceHandler is an abstract base class for resource handlers
339 // capable of creating a control from an XML node.
341 class WXDLLIMPEXP_XRC wxXmlResourceHandler
: public wxObject
343 DECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
)
346 wxXmlResourceHandler();
349 virtual ~wxXmlResourceHandler() {}
351 // Creates an object (menu, dialog, control, ...) from an XML node.
352 // Should check for validity.
353 // parent is a higher-level object (usually window, dialog or panel)
354 // that is often necessary to create the resource.
355 // If instance is non-NULL it should not create a new instance via 'new' but
356 // should rather use this one, and call its Create method.
357 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
360 // This one is called from CreateResource after variables
362 virtual wxObject
*DoCreateResource() = 0;
364 // Returns true if it understands this node and can create
365 // a resource from it, false otherwise.
366 virtual bool CanHandle(wxXmlNode
*node
) = 0;
368 // Sets the parent resource.
369 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
372 wxXmlResource
*m_resource
;
373 wxArrayString m_styleNames
;
374 wxArrayInt m_styleValues
;
376 // Variables (filled by CreateResource)
379 wxObject
*m_parent
, *m_instance
;
380 wxWindow
*m_parentAsWindow
;
382 // --- Handy methods:
384 // Returns true if the node has a property class equal to classname,
385 // e.g. <object class="wxDialog">.
386 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
);
388 // Gets node content from wxXML_ENTITY_NODE
389 // The problem is, <tag>content<tag> is represented as
390 // wxXML_ENTITY_NODE name="tag", content=""
391 // |-- wxXML_TEXT_NODE or
392 // wxXML_CDATA_SECTION_NODE name="" content="content"
393 wxString
GetNodeContent(wxXmlNode
*node
);
395 // Check to see if a parameter exists.
396 bool HasParam(const wxString
& param
);
398 // Finds the node or returns NULL.
399 wxXmlNode
*GetParamNode(const wxString
& param
);
401 // Finds the parameter value or returns the empty string.
402 wxString
GetParamValue(const wxString
& param
);
404 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
405 // understood by this handler.
406 void AddStyle(const wxString
& name
, int value
);
408 // Add styles common to all wxWindow-derived classes.
409 void AddWindowStyles();
411 // Gets style flags from text in form "flag | flag2| flag3 |..."
412 // Only understands flags added with AddStyle
413 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
415 // Gets text from param and does some conversions:
416 // - replaces \n, \r, \t by respective chars (according to C syntax)
417 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
418 // - calls wxGetTranslations (unless disabled in wxXmlResource)
419 wxString
GetText(const wxString
& param
, bool translate
= true);
421 // Returns the XRCID.
424 // Returns the resource name.
427 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
428 bool GetBool(const wxString
& param
, bool defaultv
= false);
430 // Gets an integer value from the parameter.
431 long GetLong(const wxString
& param
, long defaultv
= 0);
433 // Gets a float value from the parameter.
434 float GetFloat(const wxString
& param
, float defaultv
= 0);
436 // Gets colour in HTML syntax (#RRGGBB).
437 wxColour
GetColour(const wxString
& param
, const wxColour
& defaultv
= wxNullColour
);
439 // Gets the size (may be in dialog units).
440 wxSize
GetSize(const wxString
& param
= wxT("size"),
441 wxWindow
*windowToUse
= NULL
);
443 // Gets the position (may be in dialog units).
444 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
446 // Gets a dimension (may be in dialog units).
447 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
448 wxWindow
*windowToUse
= NULL
);
451 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
452 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
453 wxSize size
= wxDefaultSize
);
456 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
457 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
458 wxSize size
= wxDefaultSize
);
460 #if wxUSE_ANIMATIONCTRL
461 // Gets an animation.
462 wxAnimation
GetAnimation(const wxString
& param
= wxT("animation"));
466 wxFont
GetFont(const wxString
& param
= wxT("font"));
468 // Sets common window options.
469 void SetupWindow(wxWindow
*wnd
);
472 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false);
475 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
477 // Creates a resource from a node.
478 wxObject
*CreateResFromNode(wxXmlNode
*node
,
479 wxObject
*parent
, wxObject
*instance
= NULL
)
480 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
484 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
489 // Programmer-friendly macros for writing XRC handlers:
491 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
493 #define XRC_MAKE_INSTANCE(variable, classname) \
494 classname *variable = NULL; \
496 variable = wxStaticCast(m_instance, classname); \
498 variable = new classname;
501 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
502 WXDLLIMPEXP_XRC
void wxXmlInitResourceModule();
505 // This class is used to create instances of XRC "object" nodes with "subclass"
506 // property. It is _not_ supposed to be used by XRC users, you should instead
507 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
508 // only for language bindings developer who need a way to implement subclassing
509 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
510 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
513 // Try to create instance of given class and return it, return NULL on
515 virtual wxObject
*Create(const wxString
& className
) = 0;
516 virtual ~wxXmlSubclassFactory() {}
520 /* -------------------------------------------------------------------------
521 Backward compatibility macros. Do *NOT* use, they may disappear in future
522 versions of the XRC library!
523 ------------------------------------------------------------------------- */
527 #endif // _WX_XMLRES_H_