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
);
84 // This class holds XML resources from one or more .xml files
85 // (or derived forms, either binary or zipped -- see manual for
88 class WXXMLDLLEXPORT wxXmlResource
: public wxObject
91 // Ctor. If use_locale is TRUE, translatable strings are
92 // translated via _(). You can disable it by passing use_locale=FALSE
93 // (for example if you provide resource file for each locale)
94 wxXmlResource(bool use_locale
= TRUE
);
95 wxXmlResource(const wxString
& filemask
, bool use_locale
= TRUE
);
98 // Loads resources from XML files that match given filemask.
99 // This method understands VFS (see filesys.h).
100 bool Load(const wxString
& filemask
);
102 // Initialize handlers for all supported controls/windows. This will
103 // make the executable quite big because it forces linking against
104 // most of wxWin library
105 void InitAllHandlers();
107 // Initialize only specific handler (or custom handler). Convention says
108 // that handler name is equal to control's name plus 'XmlHandler', e.g.
109 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
110 // (xmlres) can create include file that contains initialization code for
111 // all controls used within the resource.
112 void AddHandler(wxXmlResourceHandler
*handler
);
114 // Removes all handlers
115 void ClearHandlers();
117 // Loads menu from resource. Returns NULL on failure.
118 wxMenu
*LoadMenu(const wxString
& name
);
120 // Loads menubar from resource. Returns NULL on failure.
121 wxMenuBar
*LoadMenuBar(const wxString
& name
);
125 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
128 // Loads dialog. dlg points to parent window (if any). Second form
129 // is used to finish creation of already existing instance (main reason
130 // for this is that you may want to use derived class with new event table)
131 // Example (typical usage):
133 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
135 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
136 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
138 // Loads panel. panel points to parent window (if any). Second form
139 // is used to finish creation of already existing instance.
140 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
141 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
143 bool LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
);
145 // Loads bitmap or icon resource from file:
146 wxBitmap
LoadBitmap(const wxString
& name
);
147 wxIcon
LoadIcon(const wxString
& name
);
149 // Attaches unknown control into given panel/window/dialog:
150 // (unknown controls are used in conjunction with <object class="unknown">)
151 bool AttachUnknownControl(const wxString
& name
, wxWindow
*control
,
152 wxWindow
*parent
= NULL
);
154 // Returns numeric ID that is equivalent to string id used in XML
155 // resource. To be used in event tables
156 // Macro XMLID is provided for convenience
157 static int GetXMLID(const wxChar
*str_id
);
159 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
160 long GetVersion() const { return m_version
; }
162 // Compares resources version to argument. Returns -1 if resources version
163 // is less than the argument, +1 if greater and 0 if they equal.
164 int CompareVersion(int major
, int minor
, int release
, int revision
) const
165 { return GetVersion() -
166 (major
*256*256*256 + minor
*256*256 + release
*256 + revision
); }
169 // Scans resources list for unloaded files and loads them. Also reloads
170 // files that have been modified since last loading.
171 void UpdateResources();
173 // Finds resource (calls UpdateResources) and returns node containing it
174 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
);
176 // Creates resource from info in given node:
177 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
179 bool GetUseLocale() { return m_useLocale
; }
186 wxXmlResourceDataRecords m_data
;
188 wxFileSystem m_curFileSystem
;
189 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
192 friend class wxXmlResourceHandler
;
196 // Global instance of resource class. For your convenience.
197 extern WXXMLDLLEXPORT wxXmlResource
*wxTheXmlResource
;
199 // This macro translates string identifier (as used in XML resource,
200 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
201 // wxWindows event tables.
203 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
204 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
205 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
206 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
207 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
210 #define XMLID(str_id) \
211 wxXmlResource::GetXMLID(wxT(str_id))
214 // This macro returns pointer to particular control in dialog
215 // created using XML resources. You can use it to set/get values from
219 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
220 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
223 #define XMLCTRL(window, id, type) \
224 (wxDynamicCast((window).FindWindow(XMLID(id)), type))
226 #define XMLCTRL(window, id, type) \
227 ((type*)((window).FindWindow(XMLID(id))))
231 class WXXMLDLLEXPORT wxXmlResourceHandler
: public wxObject
234 wxXmlResourceHandler();
235 virtual ~wxXmlResourceHandler() {}
237 // Creates object (menu, dialog, control, ...) from XML node.
238 // Should check for validity.
239 // parent is higher-level object (usually window, dialog or panel)
240 // that is often neccessary to create resource
241 // if instance != NULL it should not create new instance via 'new' but
242 // rather use this one and call its Create method
243 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
246 // This one is called from CreateResource after variables
248 virtual wxObject
*DoCreateResource() = 0;
250 // Returns TRUE if it understands this node and can create
251 // resource from it, FALSE otherwise.
252 virtual bool CanHandle(wxXmlNode
*node
) = 0;
254 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
259 wxXmlResource
*m_resource
;
260 wxArrayString m_styleNames
;
261 wxArrayInt m_styleValues
;
263 // Variables (filled by CreateResource)
266 wxObject
*m_parent
, *m_instance
;
267 wxWindow
*m_parentAsWindow
, *m_instanceAsWindow
;
269 // --- Handy methods:
271 // Returns true if the node has property class equal to classname,
272 // e.g. <object class="wxDialog">
273 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
274 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
276 // Gets node content from wxXML_ENTITY_NODE
277 // (the problem is, <tag>content<tag> is represented as
278 // wxXML_ENTITY_NODE name="tag", content=""
279 // |-- wxXML_TEXT_NODE or
280 // wxXML_CDATA_SECTION_NODE name="" content="content"
281 wxString
GetNodeContent(wxXmlNode
*node
);
283 // Check to see if a param exists
284 bool HasParam(const wxString
& param
);
286 // Finds the node or returns NULL
287 wxXmlNode
*GetParamNode(const wxString
& param
);
288 wxString
GetParamValue(const wxString
& param
);
290 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
291 // understood by this handler
292 void AddStyle(const wxString
& name
, int value
);
294 // Add styles common to all wxWindow-derived classes
295 void AddWindowStyles();
297 // Gets style flags from text in form "flag | flag2| flag3 |..."
298 // Only understads flags added with AddStyle
299 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
301 // Gets text from param and does some convertions:
302 // - replaces \n, \r, \t by respective chars (according to C syntax)
303 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
304 // - calls wxGetTranslations (unless disabled in wxXmlResource)
305 wxString
GetText(const wxString
& param
);
311 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
312 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
314 // Get integer value from param
315 long GetLong( const wxString
& param
, long defaultv
= 0 );
317 // Get colour in HTML syntax (#RRGGBB)
318 wxColour
GetColour(const wxString
& param
);
320 // Get size/position (may be in dlg units):
321 wxSize
GetSize(const wxString
& param
= wxT("size"));
322 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
324 // Get dimension (may be in dlg units):
325 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0);
328 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
329 wxSize size
= wxDefaultSize
);
330 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
331 wxSize size
= wxDefaultSize
);
334 wxFont
GetFont(const wxString
& param
= wxT("font"));
336 // Sets common window options:
337 void SetupWindow(wxWindow
*wnd
);
339 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= FALSE
);
340 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
341 wxObject
*CreateResFromNode(wxXmlNode
*node
,
342 wxObject
*parent
, wxObject
*instance
= NULL
)
343 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
346 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
349 #define ADD_STYLE(style) AddStyle(wxT(#style), style)
352 void wxXmlInitResourceModule();
354 #endif // _WX_XMLRES_H_