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
);
102 wxXmlResource(const wxString
& filemask
, int flags
= wxXRC_USE_LOCALE
);
105 // Loads resources from XML files that match given filemask.
106 // This method understands VFS (see filesys.h).
107 bool Load(const wxString
& filemask
);
109 // Initialize handlers for all supported controls/windows. This will
110 // make the executable quite big because it forces linking against
111 // most of wxWin library
112 void InitAllHandlers();
114 // Initialize only specific handler (or custom handler). Convention says
115 // that handler name is equal to control's name plus 'XmlHandler', e.g.
116 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
117 // (xmlres) can create include file that contains initialization code for
118 // all controls used within the resource.
119 void AddHandler(wxXmlResourceHandler
*handler
);
121 // Removes all handlers
122 void ClearHandlers();
124 // Loads menu from resource. Returns NULL on failure.
125 wxMenu
*LoadMenu(const wxString
& name
);
127 // Loads menubar from resource. Returns NULL on failure.
128 wxMenuBar
*LoadMenuBar(wxWindow
*parent
, const wxString
& name
);
129 wxMenuBar
*LoadMenuBar(const wxString
& name
) { return LoadMenuBar(NULL
, name
); }
133 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
136 // Loads dialog. dlg points to parent window (if any). Second form
137 // is used to finish creation of already existing instance (main reason
138 // for this is that you may want to use derived class with new event table)
139 // Example (typical usage):
141 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
143 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
144 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
146 // Loads panel. panel points to parent window (if any). Second form
147 // is used to finish creation of already existing instance.
148 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
149 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
151 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
153 // Loads bitmap or icon resource from file:
154 wxBitmap
LoadBitmap(const wxString
& name
);
155 wxIcon
LoadIcon(const wxString
& name
);
157 // Attaches unknown control into given panel/window/dialog:
158 // (unknown controls are used in conjunction with <object class="unknown">)
159 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
160 wxWindow
*parent
= NULL
);
162 // Returns numeric ID that is equivalent to string id used in XML
163 // resource. To be used in event tables
164 // Macro XMLID is provided for convenience
165 static int GetXMLID(const wxChar
*str_id
);
167 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
168 long GetVersion() const { return m_version
; }
170 // Compares resources version to argument. Returns -1 if resources version
171 // is less than the argument, +1 if greater and 0 if they equal.
172 int CompareVersion(int major
, int minor
, int release
, int revision
) const
173 { return GetVersion() -
174 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
176 // Singleton accessors:
178 // Gets global resources object or create one if none exists
179 static wxXmlResource
*Get();
180 // Sets global resources object and returns pointer to previous one (may be NULL).
181 static wxXmlResource
*Set(wxXmlResource
*res
);
184 // Scans resources list for unloaded files and loads them. Also reloads
185 // files that have been modified since last loading.
186 void UpdateResources();
188 // Finds resource (calls UpdateResources) and returns node containing it
189 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
, bool recursive
= FALSE
);
190 wxXmlNode
*DoFindResource(wxXmlNode
*parent
, const wxString
& name
, const wxString
& classname
, bool recursive
);
192 // Creates resource from info in given node:
193 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
195 int GetFlags() { return m_flags
; }
202 wxXmlResourceDataRecords m_data
;
204 wxFileSystem m_curFileSystem
;
205 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
208 friend class wxXmlResourceHandler
;
210 // singleton instance:
211 static wxXmlResource
*ms_instance
;
215 // This macro translates string identifier (as used in XML resource,
216 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
217 // wxWindows event tables.
219 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
220 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
221 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
222 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
223 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
226 #define XMLID(str_id) \
227 wxXmlResource::GetXMLID(wxT(str_id))
230 // This macro returns pointer to particular control in dialog
231 // created using XML resources. You can use it to set/get values from
235 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
236 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
239 #define XMLCTRL(window, id, type) \
240 (wxDynamicCast((window).FindWindow(XMLID(id)), type))
242 #define XMLCTRL(window, id, type) \
243 ((type*)((window).FindWindow(XMLID(id))))
247 class WXXMLDLLEXPORT wxXmlResourceHandler
: public wxObject
250 wxXmlResourceHandler();
251 virtual ~wxXmlResourceHandler() {}
253 // Creates object (menu, dialog, control, ...) from XML node.
254 // Should check for validity.
255 // parent is higher-level object (usually window, dialog or panel)
256 // that is often neccessary to create resource
257 // if instance != NULL it should not create new instance via 'new' but
258 // rather use this one and call its Create method
259 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
262 // This one is called from CreateResource after variables
264 virtual wxObject
*DoCreateResource() = 0;
266 // Returns TRUE if it understands this node and can create
267 // resource from it, FALSE otherwise.
268 virtual bool CanHandle(wxXmlNode
*node
) = 0;
270 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
274 wxXmlResource
*m_resource
;
275 wxArrayString m_styleNames
;
276 wxArrayInt m_styleValues
;
278 // Variables (filled by CreateResource)
281 wxObject
*m_parent
, *m_instance
;
282 wxWindow
*m_parentAsWindow
, *m_instanceAsWindow
;
284 // --- Handy methods:
286 // Returns true if the node has property class equal to classname,
287 // e.g. <object class="wxDialog">
288 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
289 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
291 // Gets node content from wxXML_ENTITY_NODE
292 // (the problem is, <tag>content<tag> is represented as
293 // wxXML_ENTITY_NODE name="tag", content=""
294 // |-- wxXML_TEXT_NODE or
295 // wxXML_CDATA_SECTION_NODE name="" content="content"
296 wxString
GetNodeContent(wxXmlNode
*node
);
298 // Check to see if a param exists
299 bool HasParam(const wxString
& param
);
301 // Finds the node or returns NULL
302 wxXmlNode
*GetParamNode(const wxString
& param
);
303 wxString
GetParamValue(const wxString
& param
);
305 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
306 // understood by this handler
307 void AddStyle(const wxString
& name
, int value
);
309 // Add styles common to all wxWindow-derived classes
310 void AddWindowStyles();
312 // Gets style flags from text in form "flag | flag2| flag3 |..."
313 // Only understads flags added with AddStyle
314 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
316 // Gets text from param and does some convertions:
317 // - replaces \n, \r, \t by respective chars (according to C syntax)
318 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
319 // - calls wxGetTranslations (unless disabled in wxXmlResource)
320 wxString
GetText(const wxString
& param
);
326 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
327 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
329 // Get integer value from param
330 long GetLong( const wxString
& param
, long defaultv
= 0 );
332 // Get colour in HTML syntax (#RRGGBB)
333 wxColour
GetColour(const wxString
& param
);
335 // Get size/position (may be in dlg units):
336 wxSize
GetSize(const wxString
& param
= wxT("size"));
337 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
339 // Get dimension (may be in dlg units):
340 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0);
343 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
344 wxSize size
= wxDefaultSize
);
345 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
346 wxSize size
= wxDefaultSize
);
349 wxFont
GetFont(const wxString
& param
= wxT("font"));
351 // Sets common window options:
352 void SetupWindow(wxWindow
*wnd
);
354 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= FALSE
);
355 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
356 wxObject
*CreateResFromNode(wxXmlNode
*node
,
357 wxObject
*parent
, wxObject
*instance
= NULL
)
358 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
362 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
367 // Programmer-friendly macros for writing XRC handlers:
369 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
371 #define XRC_MAKE_INSTANCE(variable, classname) \
372 classname *variable = NULL; \
374 variable = wxStaticCast(m_instance, classname); \
376 variable = new classname;
379 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
380 void wxXmlInitResourceModule();
383 /* -------------------------------------------------------------------------
384 Backward compatibility macros. Do *NOT* use, they may disappear in future
385 versions of the XRC library!
386 ------------------------------------------------------------------------- */
387 #define ADD_STYLE XRC_ADD_STYLE
388 #define wxTheXmlResource wxXmlResource::Get()
392 #endif // _WX_XMLRES_H_