1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
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"
28 #include "wx/xrc/xml.h"
30 class WXDLLEXPORT wxMenu
;
31 class WXDLLEXPORT wxMenuBar
;
32 class WXDLLEXPORT wxDialog
;
33 class WXDLLEXPORT wxPanel
;
34 class WXDLLEXPORT wxWindow
;
35 class WXDLLEXPORT wxFrame
;
36 class WXDLLEXPORT wxToolBar
;
38 class WXXMLDLLEXPORT wxXmlResourceHandler
;
41 // These macros indicate current version of XML resources (this information is
42 // encoded in root node of XRC file as "version" property).
44 // Rules for increasing version number:
45 // - change it only if you made incompatible change to the format. Addition of new
46 // attribute to control handler is _not_ incompatible change, because older
47 // versions of the library may ignore it.
48 // - if you change version number, follow these steps:
49 // - set major, minor and release numbers to respective version numbers of
50 // the wxWindows library (see wx/version.h)
51 // - reset revision to 0 unless the first three are same as before, in which
52 // case you should increase revision by one
53 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
54 #define WX_XMLRES_CURRENT_VERSION_MINOR 3
55 #define WX_XMLRES_CURRENT_VERSION_RELEASE 0
56 #define WX_XMLRES_CURRENT_VERSION_REVISION 1
57 #define WX_XMLRES_CURRENT_VERSION_STRING "2.3.0.1"
59 #define WX_XMLRES_CURRENT_VERSION \
60 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
61 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
62 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
63 WX_XMLRES_CURRENT_VERSION_REVISION)
65 class WXXMLDLLEXPORT wxXmlResourceDataRecord
68 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
69 ~wxXmlResourceDataRecord() {delete Doc
;}
78 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
80 WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
83 enum wxXmlResourceFlags
86 wxXRC_NO_SUBCLASSING
= 2
89 // This class holds XML resources from one or more .xml files
90 // (or derived forms, either binary or zipped -- see manual for
92 class WXXMLDLLEXPORT wxXmlResource
: public wxObject
96 // Flags: wxXRC_USE_LOCALE
97 // translatable strings will be translated via _()
98 // wxXRC_NO_SUBCLASSING
99 // subclass property of object nodes will be ignored
100 // (useful for previews in XRC editors)
101 wxXmlResource(int flags
= wxXRC_USE_LOCALE
);
104 // Flags: wxXRC_USE_LOCALE
105 // translatable strings will be translated via _()
106 // wxXRC_NO_SUBCLASSING
107 // subclass property of object nodes will be ignored
108 // (useful for previews in XRC editors)
109 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
);
114 // Loads resources from XML files that match given filemask.
115 // This method understands VFS (see filesys.h).
116 bool Load(const wxString
& filemask
);
118 // Initialize handlers for all supported controls/windows. This will
119 // make the executable quite big because it forces linking against
120 // most of the wxWindows library.
121 void InitAllHandlers();
123 // Initialize only a specific handler (or custom handler). Convention says
124 // that handler name is equal to the control's name plus 'XmlHandler', for example
125 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource compiler
126 // (xmlres) can create include file that contains initialization code for
127 // all controls used within the resource.
128 void AddHandler(wxXmlResourceHandler
*handler
);
130 // Removes all handlers
131 void ClearHandlers();
133 // Loads menu from resource. Returns NULL on failure.
134 wxMenu
*LoadMenu(const wxString
& name
);
136 // Loads menubar from resource. Returns NULL on failure.
137 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
139 // Loads menubar from resource. Returns NULL on failure.
140 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
144 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
147 // Loads a dialog. dlg points to parent window (if any).
148 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
150 // Loads a dialog. dlg points to parent window (if any). This form
151 // is used to finish creation of already existing instance (main reason
152 // for this is that you may want to use derived class with new event table)
153 // Example (typical usage):
155 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
157 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
159 // Loads a panel. panel points to parent window (if any).
160 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
162 // Loads a panel. panel points to parent window (if any). This form
163 // is used to finish creation of already existing instance.
164 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
167 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
169 // Loads a bitmap resource from a file.
170 wxBitmap
LoadBitmap(const wxString
& name
);
172 // Loads an icon resource from a file.
173 wxIcon
LoadIcon(const wxString
& name
);
175 // Attaches an unknown control to the given panel/window/dialog.
176 // Unknown controls are used in conjunction with <object class="unknown">.
177 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
178 wxWindow
*parent
= NULL
);
180 // Returns a numeric ID that is equivalent to the string id used in an XML
181 // resource. To be used in event tables.
182 // Macro XRCID is provided for convenience
183 static int GetXRCID(const wxChar
*str_id
);
185 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
186 long GetVersion() const { return m_version
; }
188 // Compares resources version to argument. Returns -1 if resources version
189 // is less than the argument, +1 if greater and 0 if they equal.
190 int CompareVersion(int major
, int minor
, int release
, int revision
) const
191 { return GetVersion() -
192 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
194 //// Singleton accessors.
196 // Gets the global resources object or creates one if none exists.
197 static wxXmlResource
*Get();
199 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
200 static wxXmlResource
*Set(wxXmlResource
*res
);
203 // Scans the resources list for unloaded files and loads them. Also reloads
204 // files that have been modified since last loading.
205 void UpdateResources();
207 // Finds a resource (calls UpdateResources) and returns a node containing it.
208 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= FALSE
);
210 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
211 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
213 // Creates a resource from information in the given node.
214 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
216 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
217 int GetFlags() { return m_flags
; }
224 wxXmlResourceDataRecords m_data
;
226 wxFileSystem m_curFileSystem
;
227 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
230 friend class wxXmlResourceHandler
;
232 // singleton instance:
233 static wxXmlResource
*ms_instance
;
237 // This macro translates string identifier (as used in XML resource,
238 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
239 // wxWindows event tables.
241 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
242 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
243 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
244 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
245 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
248 #define XRCID(str_id) \
249 wxXmlResource::GetXRCID(wxT(str_id))
252 // This macro returns pointer to particular control in dialog
253 // created using XML resources. You can use it to set/get values from
257 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
258 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
261 #define XRCCTRL(window, id, type) \
262 (wxDynamicCast((window).FindWindow(XRCID(id)), type))
264 #define XRCCTRL(window, id, type) \
265 ((type*)((window).FindWindow(XRCID(id))))
268 // wxXmlResourceHandler is an abstract base class for resource handlers
269 // capable of creating a control from an XML node.
271 class WXXMLDLLEXPORT wxXmlResourceHandler
: public wxObject
275 wxXmlResourceHandler();
278 virtual ~wxXmlResourceHandler() {}
280 // Creates an object (menu, dialog, control, ...) from an XML node.
281 // Should check for validity.
282 // parent is a higher-level object (usually window, dialog or panel)
283 // that is often neccessary to create the resource.
284 // If instance is non-NULL it should not create a new instance via 'new' but
285 // should rather use this one, and call its Create method.
286 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
289 // This one is called from CreateResource after variables
291 virtual wxObject
*DoCreateResource() = 0;
293 // Returns TRUE if it understands this node and can create
294 // a resource from it, FALSE otherwise.
295 virtual bool CanHandle(wxXmlNode
*node
) = 0;
297 // Sets the parent resource.
298 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
301 wxXmlResource
*m_resource
;
302 wxArrayString m_styleNames
;
303 wxArrayInt m_styleValues
;
305 // Variables (filled by CreateResource)
308 wxObject
*m_parent
, *m_instance
;
309 wxWindow
*m_parentAsWindow
, *m_instanceAsWindow
;
311 // --- Handy methods:
313 // Returns true if the node has a property class equal to classname,
314 // e.g. <object class="wxDialog">.
315 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
316 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
318 // Gets node content from wxXML_ENTITY_NODE
319 // The problem is, <tag>content<tag> is represented as
320 // wxXML_ENTITY_NODE name="tag", content=""
321 // |-- wxXML_TEXT_NODE or
322 // wxXML_CDATA_SECTION_NODE name="" content="content"
323 wxString
GetNodeContent(wxXmlNode
*node
);
325 // Check to see if a parameter exists.
326 bool HasParam(const wxString
& param
);
328 // Finds the node or returns NULL.
329 wxXmlNode
*GetParamNode(const wxString
& param
);
331 // Finds the parameter value or returns the empty string.
332 wxString
GetParamValue(const wxString
& param
);
334 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
335 // understood by this handler.
336 void AddStyle(const wxString
& name
, int value
);
338 // Add styles common to all wxWindow-derived classes.
339 void AddWindowStyles();
341 // Gets style flags from text in form "flag | flag2| flag3 |..."
342 // Only understads flags added with AddStyle
343 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
345 // Gets text from param and does some conversions:
346 // - replaces \n, \r, \t by respective chars (according to C syntax)
347 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
348 // - calls wxGetTranslations (unless disabled in wxXmlResource)
349 wxString
GetText(const wxString
& param
, bool translate
= TRUE
);
351 // Returns the XRCID.
354 // Returns the resource name.
357 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
358 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
360 // Gets the integer value from the parameter.
361 long GetLong( const wxString
& param
, long defaultv
= 0 );
363 // Gets colour in HTML syntax (#RRGGBB).
364 wxColour
GetColour(const wxString
& param
);
366 // Gets the size (may be in dialog units).
367 wxSize
GetSize(const wxString
& param
= wxT("size"));
369 // Gets the position (may be in dialog units).
370 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
372 // Gets a dimension (may be in dialog units).
373 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0);
376 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
377 wxSize size
= wxDefaultSize
);
380 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
381 wxSize size
= wxDefaultSize
);
384 wxFont
GetFont(const wxString
& param
= wxT("font"));
386 // Sets common window options.
387 void SetupWindow(wxWindow
*wnd
);
390 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= FALSE
);
393 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
395 // Creates a resource from a node.
396 wxObject
*CreateResFromNode(wxXmlNode
*node
,
397 wxObject
*parent
, wxObject
*instance
= NULL
)
398 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
402 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
407 // Programmer-friendly macros for writing XRC handlers:
409 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
411 #define XRC_MAKE_INSTANCE(variable, classname) \
412 classname *variable = NULL; \
414 variable = wxStaticCast(m_instance, classname); \
416 variable = new classname;
419 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
420 void wxXmlInitResourceModule();
423 /* -------------------------------------------------------------------------
424 Backward compatibility macros. Do *NOT* use, they may disappear in future
425 versions of the XRC library!
426 ------------------------------------------------------------------------- */
427 #define ADD_STYLE XRC_ADD_STYLE
428 #define wxTheXmlResource wxXmlResource::Get()
430 #define XMLCTRL XRCCTRL
431 #define GetXMLID GetXRCID
434 #endif // _WX_XMLRES_H_