1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma interface "xmlres.h"
22 #include "wx/string.h"
23 #include "wx/dynarray.h"
24 #include "wx/datetime.h"
26 #include "wx/gdicmn.h"
27 #include "wx/filesys.h"
28 #include "wx/bitmap.h"
30 #include "wx/artprov.h"
32 #include "wx/xml/xml.h"
34 class WXDLLEXPORT wxMenu
;
35 class WXDLLEXPORT wxMenuBar
;
36 class WXDLLEXPORT wxDialog
;
37 class WXDLLEXPORT wxPanel
;
38 class WXDLLEXPORT wxWindow
;
39 class WXDLLEXPORT wxFrame
;
40 class WXDLLEXPORT wxToolBar
;
42 class WXDLLIMPEXP_XRC wxXmlResourceHandler
;
43 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
;
44 class WXDLLIMPEXP_XRC wxXmlSubclassFactoriesList
;
45 class wxXmlResourceModule
;
48 // These macros indicate current version of XML resources (this information is
49 // encoded in root node of XRC file as "version" property).
51 // Rules for increasing version number:
52 // - change it only if you made incompatible change to the format. Addition
53 // of new attribute to control handler is _not_ incompatible change, because
54 // older versions of the library may ignore it.
55 // - if you change version number, follow these steps:
56 // - set major, minor and release numbers to respective version numbers of
57 // the wxWidgets library (see wx/version.h)
58 // - reset revision to 0 unless the first three are same as before,
59 // in which case you should increase revision by one
60 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
61 #define WX_XMLRES_CURRENT_VERSION_MINOR 5
62 #define WX_XMLRES_CURRENT_VERSION_RELEASE 3
63 #define WX_XMLRES_CURRENT_VERSION_REVISION 0
64 #define WX_XMLRES_CURRENT_VERSION_STRING _T("2.5.3.0")
66 #define WX_XMLRES_CURRENT_VERSION \
67 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
68 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
69 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
70 WX_XMLRES_CURRENT_VERSION_REVISION)
72 class WXDLLIMPEXP_XRC wxXmlResourceDataRecord
75 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
76 ~wxXmlResourceDataRecord() {delete Doc
;}
84 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
,
85 wxXmlResourceDataRecords
,
88 enum wxXmlResourceFlags
91 wxXRC_NO_SUBCLASSING
= 2,
92 wxXRC_NO_RELOADING
= 4
95 // This class holds XML resources from one or more .xml files
96 // (or derived forms, either binary or zipped -- see manual for
98 class WXDLLIMPEXP_XRC wxXmlResource
: public wxObject
102 // Flags: wxXRC_USE_LOCALE
103 // translatable strings will be translated via _()
104 // wxXRC_NO_SUBCLASSING
105 // subclass property of object nodes will be ignored
106 // (useful for previews in XRC editors)
107 wxXmlResource(int flags
= wxXRC_USE_LOCALE
);
110 // Flags: wxXRC_USE_LOCALE
111 // translatable strings will be translated via _()
112 // wxXRC_NO_SUBCLASSING
113 // subclass property of object nodes will be ignored
114 // (useful for previews in XRC editors)
115 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
);
120 // Loads resources from XML files that match given filemask.
121 // This method understands VFS (see filesys.h).
122 bool Load(const wxString
& filemask
);
124 // Initialize handlers for all supported controls/windows. This will
125 // make the executable quite big because it forces linking against
126 // most of the wxWidgets library.
127 void InitAllHandlers();
129 // Initialize only a specific handler (or custom handler). Convention says
130 // that handler name is equal to the control's name plus 'XmlHandler', for
131 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
132 // compiler (xmlres) can create include file that contains initialization
133 // code for all controls used within the resource.
134 void AddHandler(wxXmlResourceHandler
*handler
);
136 // Add a new handler at the begining of the handler list
137 void InsertHandler(wxXmlResourceHandler
*handler
);
139 // Removes all handlers
140 void ClearHandlers();
142 // Registers subclasses factory for use in XRC. This function is not meant
143 // for public use, please see the comment above wxXmlSubclassFactory
145 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
147 // Loads menu from resource. Returns NULL on failure.
148 wxMenu
*LoadMenu(const wxString
& name
);
150 // Loads menubar from resource. Returns NULL on failure.
151 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
153 // Loads menubar from resource. Returns NULL on failure.
154 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
158 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
161 // Loads a dialog. dlg points to parent window (if any).
162 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
164 // Loads a dialog. dlg points to parent window (if any). This form
165 // is used to finish creation of already existing instance (main reason
166 // for this is that you may want to use derived class with new event table)
167 // Example (typical usage):
169 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
171 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
173 // Loads a panel. panel points to parent window (if any).
174 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
176 // Loads a panel. panel points to parent window (if any). This form
177 // is used to finish creation of already existing instance.
178 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
181 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
182 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
184 // Load an object from the resource specifying both the resource name and
185 // the classname. This lets you load nonstandard container windows.
186 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
187 const wxString
& classname
);
189 // Load an object from the resource specifying both the resource name and
190 // the classname. This form lets you finish the creation of an existing
192 bool LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
,
193 const wxString
& classname
);
195 // Loads a bitmap resource from a file.
196 wxBitmap
LoadBitmap(const wxString
& name
);
198 // Loads an icon resource from a file.
199 wxIcon
LoadIcon(const wxString
& name
);
201 // Attaches an unknown control to the given panel/window/dialog.
202 // Unknown controls are used in conjunction with <object class="unknown">.
203 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
204 wxWindow
*parent
= NULL
);
206 // Returns a numeric ID that is equivalent to the string id used in an XML
207 // resource. To be used in event tables.
208 // Macro XRCID is provided for convenience
209 static int GetXRCID(const wxChar
*str_id
);
211 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
212 long GetVersion() const { return m_version
; }
214 // Compares resources version to argument. Returns -1 if resources version
215 // is less than the argument, +1 if greater and 0 if they equal.
216 int CompareVersion(int major
, int minor
, int release
, int revision
) const
217 { return GetVersion() -
218 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
220 //// Singleton accessors.
222 // Gets the global resources object or creates one if none exists.
223 static wxXmlResource
*Get();
225 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
226 static wxXmlResource
*Set(wxXmlResource
*res
);
228 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
229 int GetFlags() const { return m_flags
; }
230 // Set flags after construction.
231 void SetFlags(int flags
) { m_flags
= flags
; }
234 // Scans the resources list for unloaded files and loads them. Also reloads
235 // files that have been modified since last loading.
236 bool UpdateResources();
238 // Finds a resource (calls UpdateResources) and returns a node containing it.
239 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= false);
241 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
242 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
244 // Creates a resource from information in the given node
245 // (Uses only 'handlerToUse' if != NULL)
246 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
247 wxObject
*instance
= NULL
,
248 wxXmlResourceHandler
*handlerToUse
= NULL
);
255 wxXmlResourceDataRecords m_data
;
257 wxFileSystem m_curFileSystem
;
258 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
261 friend class wxXmlResourceHandler
;
262 friend class wxXmlResourceModule
;
264 static wxXmlSubclassFactoriesList
*ms_subclassFactories
;
266 // singleton instance:
267 static wxXmlResource
*ms_instance
;
271 // This macro translates string identifier (as used in XML resource,
272 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
273 // wxWidgets event tables.
275 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
276 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
277 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
278 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
279 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
282 #define XRCID(str_id) \
283 wxXmlResource::GetXRCID(wxT(str_id))
286 // This macro returns pointer to particular control in dialog
287 // created using XML resources. You can use it to set/get values from
291 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
292 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
294 #define XRCCTRL(window, id, type) \
295 (wxStaticCast((window).FindWindow(XRCID(id)), type))
297 // wxXmlResourceHandler is an abstract base class for resource handlers
298 // capable of creating a control from an XML node.
300 class WXDLLIMPEXP_XRC wxXmlResourceHandler
: public wxObject
302 DECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
)
305 wxXmlResourceHandler();
308 virtual ~wxXmlResourceHandler() {}
310 // Creates an object (menu, dialog, control, ...) from an XML node.
311 // Should check for validity.
312 // parent is a higher-level object (usually window, dialog or panel)
313 // that is often neccessary to create the resource.
314 // If instance is non-NULL it should not create a new instance via 'new' but
315 // should rather use this one, and call its Create method.
316 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
319 // This one is called from CreateResource after variables
321 virtual wxObject
*DoCreateResource() = 0;
323 // Returns true if it understands this node and can create
324 // a resource from it, false otherwise.
325 virtual bool CanHandle(wxXmlNode
*node
) = 0;
327 // Sets the parent resource.
328 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
331 wxXmlResource
*m_resource
;
332 wxArrayString m_styleNames
;
333 wxArrayInt m_styleValues
;
335 // Variables (filled by CreateResource)
338 wxObject
*m_parent
, *m_instance
;
339 wxWindow
*m_parentAsWindow
;
341 // --- Handy methods:
343 // Returns true if the node has a property class equal to classname,
344 // e.g. <object class="wxDialog">.
345 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
346 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
348 // Gets node content from wxXML_ENTITY_NODE
349 // The problem is, <tag>content<tag> is represented as
350 // wxXML_ENTITY_NODE name="tag", content=""
351 // |-- wxXML_TEXT_NODE or
352 // wxXML_CDATA_SECTION_NODE name="" content="content"
353 wxString
GetNodeContent(wxXmlNode
*node
);
355 // Check to see if a parameter exists.
356 bool HasParam(const wxString
& param
);
358 // Finds the node or returns NULL.
359 wxXmlNode
*GetParamNode(const wxString
& param
);
361 // Finds the parameter value or returns the empty string.
362 wxString
GetParamValue(const wxString
& param
);
364 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
365 // understood by this handler.
366 void AddStyle(const wxString
& name
, int value
);
368 // Add styles common to all wxWindow-derived classes.
369 void AddWindowStyles();
371 // Gets style flags from text in form "flag | flag2| flag3 |..."
372 // Only understands flags added with AddStyle
373 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
375 // Gets text from param and does some conversions:
376 // - replaces \n, \r, \t by respective chars (according to C syntax)
377 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
378 // - calls wxGetTranslations (unless disabled in wxXmlResource)
379 wxString
GetText(const wxString
& param
, bool translate
= true);
381 // Returns the XRCID.
384 // Returns the resource name.
387 // Gets a bool flag (1, t, yes, on, true are true, everything else is false).
388 bool GetBool(const wxString
& param
, bool defaultv
= false);
390 // Gets an integer value from the parameter.
391 long GetLong(const wxString
& param
, long defaultv
= 0);
393 // Gets a float value from the parameter.
394 float GetFloat(const wxString
& param
, float defaultv
= 0);
396 // Gets colour in HTML syntax (#RRGGBB).
397 wxColour
GetColour(const wxString
& param
);
399 // Gets the size (may be in dialog units).
400 wxSize
GetSize(const wxString
& param
= wxT("size"),
401 wxWindow
*windowToUse
= NULL
);
403 // Gets the position (may be in dialog units).
404 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
406 // Gets a dimension (may be in dialog units).
407 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
408 wxWindow
*windowToUse
= NULL
);
411 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
412 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
413 wxSize size
= wxDefaultSize
);
416 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
417 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
418 wxSize size
= wxDefaultSize
);
421 wxFont
GetFont(const wxString
& param
= wxT("font"));
423 // Sets common window options.
424 void SetupWindow(wxWindow
*wnd
);
427 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false);
430 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
432 // Creates a resource from a node.
433 wxObject
*CreateResFromNode(wxXmlNode
*node
,
434 wxObject
*parent
, wxObject
*instance
= NULL
)
435 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
439 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
444 // Programmer-friendly macros for writing XRC handlers:
446 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
448 #define XRC_MAKE_INSTANCE(variable, classname) \
449 classname *variable = NULL; \
451 variable = wxStaticCast(m_instance, classname); \
453 variable = new classname;
456 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
457 WXDLLIMPEXP_XRC
void wxXmlInitResourceModule();
460 // This class is used to create instances of XRC "object" nodes with "subclass"
461 // property. It is _not_ supposed to be used by XRC users, you should instead
462 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
463 // only for language bindings developer who need a way to implement subclassing
464 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
465 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
468 // Try to create instance of given class and return it, return NULL on
470 virtual wxObject
*Create(const wxString
& className
) = 0;
471 virtual ~wxXmlSubclassFactory() {}
475 /* -------------------------------------------------------------------------
476 Backward compatibility macros. Do *NOT* use, they may disappear in future
477 versions of the XRC library!
478 ------------------------------------------------------------------------- */
479 #if WXWIN_COMPATIBILITY_2_4
480 #define ADD_STYLE XRC_ADD_STYLE
481 #define wxTheXmlResource wxXmlResource::Get()
483 #define XMLCTRL XRCCTRL
484 #define GetXMLID GetXRCID
489 #endif // _WX_XMLRES_H_