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(__APPLE__)
15 #pragma interface "xmlres.h"
19 #include "wx/string.h"
20 #include "wx/dynarray.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"
29 #include "wx/xrc/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 WXXMLDLLEXPORT wxXmlResourceHandler
;
42 // These macros indicate current version of XML resources (this information is
43 // encoded in root node of XRC file as "version" property).
45 // Rules for increasing version number:
46 // - change it only if you made incompatible change to the format. Addition of new
47 // attribute to control handler is _not_ incompatible change, because older
48 // versions of the library may ignore it.
49 // - if you change version number, follow these steps:
50 // - set major, minor and release numbers to respective version numbers of
51 // the wxWindows library (see wx/version.h)
52 // - reset revision to 0 unless the first three are same as before, in which
53 // case you should increase revision by one
54 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
55 #define WX_XMLRES_CURRENT_VERSION_MINOR 3
56 #define WX_XMLRES_CURRENT_VERSION_RELEASE 0
57 #define WX_XMLRES_CURRENT_VERSION_REVISION 1
58 #define WX_XMLRES_CURRENT_VERSION_STRING "2.3.0.1"
60 #define WX_XMLRES_CURRENT_VERSION \
61 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
62 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
63 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
64 WX_XMLRES_CURRENT_VERSION_REVISION)
66 class WXXMLDLLEXPORT wxXmlResourceDataRecord
69 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
70 ~wxXmlResourceDataRecord() {delete Doc
;}
79 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
81 WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
84 enum wxXmlResourceFlags
87 wxXRC_NO_SUBCLASSING
= 2
90 // This class holds XML resources from one or more .xml files
91 // (or derived forms, either binary or zipped -- see manual for
93 class WXXMLDLLEXPORT wxXmlResource
: public wxObject
97 // Flags: wxXRC_USE_LOCALE
98 // translatable strings will be translated via _()
99 // wxXRC_NO_SUBCLASSING
100 // subclass property of object nodes will be ignored
101 // (useful for previews in XRC editors)
102 wxXmlResource(int flags
= wxXRC_USE_LOCALE
);
105 // Flags: wxXRC_USE_LOCALE
106 // translatable strings will be translated via _()
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
);
115 // Loads resources from XML files that match given filemask.
116 // This method understands VFS (see filesys.h).
117 bool Load(const wxString
& filemask
);
119 // Initialize handlers for all supported controls/windows. This will
120 // make the executable quite big because it forces linking against
121 // most of the wxWindows library.
122 void InitAllHandlers();
124 // Initialize only a specific handler (or custom handler). Convention says
125 // that handler name is equal to the control's name plus 'XmlHandler', for example
126 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource compiler
127 // (xmlres) can create include file that contains initialization code for
128 // all controls used within the resource.
129 void AddHandler(wxXmlResourceHandler
*handler
);
131 // Add a new handler at the begining of the handler list
132 void InsertHandler(wxXmlResourceHandler
*handler
);
134 // Removes all handlers
135 void ClearHandlers();
137 // Loads menu from resource. Returns NULL on failure.
138 wxMenu
*LoadMenu(const wxString
& name
);
140 // Loads menubar from resource. Returns NULL on failure.
141 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
143 // Loads menubar from resource. Returns NULL on failure.
144 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
148 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
151 // Loads a dialog. dlg points to parent window (if any).
152 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
154 // Loads a dialog. dlg points to parent window (if any). This form
155 // is used to finish creation of already existing instance (main reason
156 // for this is that you may want to use derived class with new event table)
157 // Example (typical usage):
159 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
161 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
163 // Loads a panel. panel points to parent window (if any).
164 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
166 // Loads a panel. panel points to parent window (if any). This form
167 // is used to finish creation of already existing instance.
168 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
171 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
172 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
174 // Load an object from the resource specifying both the resource name and
175 // the classname. This lets you load nonstandard container windows.
176 wxObject
*LoadObject(wxWindow
*parent
, const wxString
& name
,
177 const wxString
& classname
);
179 // Load an object from the resource specifying both the resource name and
180 // the classname. This form lets you finish the creation of an existing
182 bool LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
,
183 const wxString
& classname
);
185 // Loads a bitmap resource from a file.
186 wxBitmap
LoadBitmap(const wxString
& name
);
188 // Loads an icon resource from a file.
189 wxIcon
LoadIcon(const wxString
& name
);
191 // Attaches an unknown control to the given panel/window/dialog.
192 // Unknown controls are used in conjunction with <object class="unknown">.
193 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
194 wxWindow
*parent
= NULL
);
196 // Returns a numeric ID that is equivalent to the string id used in an XML
197 // resource. To be used in event tables.
198 // Macro XRCID is provided for convenience
199 static int GetXRCID(const wxChar
*str_id
);
201 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
202 long GetVersion() const { return m_version
; }
204 // Compares resources version to argument. Returns -1 if resources version
205 // is less than the argument, +1 if greater and 0 if they equal.
206 int CompareVersion(int major
, int minor
, int release
, int revision
) const
207 { return GetVersion() -
208 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
210 //// Singleton accessors.
212 // Gets the global resources object or creates one if none exists.
213 static wxXmlResource
*Get();
215 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
216 static wxXmlResource
*Set(wxXmlResource
*res
);
218 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
219 int GetFlags() { return m_flags
; }
222 // Scans the resources list for unloaded files and loads them. Also reloads
223 // files that have been modified since last loading.
224 void UpdateResources();
226 // Finds a resource (calls UpdateResources) and returns a node containing it.
227 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= FALSE
);
229 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
230 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
232 // Creates a resource from information in the given node.
233 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
240 wxXmlResourceDataRecords m_data
;
242 wxFileSystem m_curFileSystem
;
243 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
246 friend class wxXmlResourceHandler
;
248 // singleton instance:
249 static wxXmlResource
*ms_instance
;
253 // This macro translates string identifier (as used in XML resource,
254 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
255 // wxWindows event tables.
257 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
258 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
259 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
260 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
261 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
264 #define XRCID(str_id) \
265 wxXmlResource::GetXRCID(wxT(str_id))
268 // This macro returns pointer to particular control in dialog
269 // created using XML resources. You can use it to set/get values from
273 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
274 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
277 #define XRCCTRL(window, id, type) \
278 (wxDynamicCast((window).FindWindow(XRCID(id)), type))
280 #define XRCCTRL(window, id, type) \
281 ((type*)((window).FindWindow(XRCID(id))))
284 // wxXmlResourceHandler is an abstract base class for resource handlers
285 // capable of creating a control from an XML node.
287 class WXXMLDLLEXPORT wxXmlResourceHandler
: public wxObject
291 wxXmlResourceHandler();
294 virtual ~wxXmlResourceHandler() {}
296 // Creates an object (menu, dialog, control, ...) from an XML node.
297 // Should check for validity.
298 // parent is a higher-level object (usually window, dialog or panel)
299 // that is often neccessary to create the resource.
300 // If instance is non-NULL it should not create a new instance via 'new' but
301 // should rather use this one, and call its Create method.
302 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
305 // This one is called from CreateResource after variables
307 virtual wxObject
*DoCreateResource() = 0;
309 // Returns TRUE if it understands this node and can create
310 // a resource from it, FALSE otherwise.
311 virtual bool CanHandle(wxXmlNode
*node
) = 0;
313 // Sets the parent resource.
314 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
317 wxXmlResource
*m_resource
;
318 wxArrayString m_styleNames
;
319 wxArrayInt m_styleValues
;
321 // Variables (filled by CreateResource)
324 wxObject
*m_parent
, *m_instance
;
325 wxWindow
*m_parentAsWindow
, *m_instanceAsWindow
;
327 // --- Handy methods:
329 // Returns true if the node has a property class equal to classname,
330 // e.g. <object class="wxDialog">.
331 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
332 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
334 // Gets node content from wxXML_ENTITY_NODE
335 // The problem is, <tag>content<tag> is represented as
336 // wxXML_ENTITY_NODE name="tag", content=""
337 // |-- wxXML_TEXT_NODE or
338 // wxXML_CDATA_SECTION_NODE name="" content="content"
339 wxString
GetNodeContent(wxXmlNode
*node
);
341 // Check to see if a parameter exists.
342 bool HasParam(const wxString
& param
);
344 // Finds the node or returns NULL.
345 wxXmlNode
*GetParamNode(const wxString
& param
);
347 // Finds the parameter value or returns the empty string.
348 wxString
GetParamValue(const wxString
& param
);
350 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
351 // understood by this handler.
352 void AddStyle(const wxString
& name
, int value
);
354 // Add styles common to all wxWindow-derived classes.
355 void AddWindowStyles();
357 // Gets style flags from text in form "flag | flag2| flag3 |..."
358 // Only understads flags added with AddStyle
359 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
361 // Gets text from param and does some conversions:
362 // - replaces \n, \r, \t by respective chars (according to C syntax)
363 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
364 // - calls wxGetTranslations (unless disabled in wxXmlResource)
365 wxString
GetText(const wxString
& param
, bool translate
= TRUE
);
367 // Returns the XRCID.
370 // Returns the resource name.
373 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
374 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
376 // Gets the integer value from the parameter.
377 long GetLong( const wxString
& param
, long defaultv
= 0 );
379 // Gets colour in HTML syntax (#RRGGBB).
380 wxColour
GetColour(const wxString
& param
);
382 // Gets the size (may be in dialog units).
383 wxSize
GetSize(const wxString
& param
= wxT("size"));
385 // Gets the position (may be in dialog units).
386 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
388 // Gets a dimension (may be in dialog units).
389 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0);
392 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
393 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
394 wxSize size
= wxDefaultSize
);
397 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
398 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
399 wxSize size
= wxDefaultSize
);
402 wxFont
GetFont(const wxString
& param
= wxT("font"));
404 // Sets common window options.
405 void SetupWindow(wxWindow
*wnd
);
408 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= FALSE
);
411 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
413 // Creates a resource from a node.
414 wxObject
*CreateResFromNode(wxXmlNode
*node
,
415 wxObject
*parent
, wxObject
*instance
= NULL
)
416 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
420 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
425 // Programmer-friendly macros for writing XRC handlers:
427 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
429 #define XRC_MAKE_INSTANCE(variable, classname) \
430 classname *variable = NULL; \
432 variable = wxStaticCast(m_instance, classname); \
434 variable = new classname;
437 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
438 void wxXmlInitResourceModule();
441 /* -------------------------------------------------------------------------
442 Backward compatibility macros. Do *NOT* use, they may disappear in future
443 versions of the XRC library!
444 ------------------------------------------------------------------------- */
445 #define ADD_STYLE XRC_ADD_STYLE
446 #define wxTheXmlResource wxXmlResource::Get()
448 #define XMLCTRL XRCCTRL
449 #define GetXMLID GetXRCID
452 #endif // _WX_XMLRES_H_