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 // Remove nodes with property "platform" that does not
180 // match current platform
181 void ProcessPlatformProperty(wxXmlNode
*node
);
183 bool GetUseLocale() { return m_useLocale
; }
190 wxXmlResourceDataRecords m_data
;
192 wxFileSystem m_curFileSystem
;
193 wxFileSystem
& GetCurFileSystem() { return m_curFileSystem
; }
196 friend class wxXmlResourceHandler
;
200 // Global instance of resource class. For your convenience.
201 extern WXXMLDLLEXPORT wxXmlResource
*wxTheXmlResource
;
203 // This macro translates string identifier (as used in XML resource,
204 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
205 // wxWindows event tables.
207 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
208 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
209 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
210 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
211 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
214 #define XMLID(str_id) \
215 wxXmlResource::GetXMLID(wxT(str_id))
218 // This macro returns pointer to particular control in dialog
219 // created using XML resources. You can use it to set/get values from
223 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
224 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
227 #define XMLCTRL(window, id, type) \
228 (wxDynamicCast((window).FindWindow(XMLID(id)), type))
230 #define XMLCTRL(window, id, type) \
231 ((type*)((window).FindWindow(XMLID(id))))
235 class WXXMLDLLEXPORT wxXmlResourceHandler
: public wxObject
238 wxXmlResourceHandler();
239 virtual ~wxXmlResourceHandler() {}
241 // Creates object (menu, dialog, control, ...) from XML node.
242 // Should check for validity.
243 // parent is higher-level object (usually window, dialog or panel)
244 // that is often neccessary to create resource
245 // if instance != NULL it should not create new instance via 'new' but
246 // rather use this one and call its Create method
247 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
250 // This one is called from CreateResource after variables
252 virtual wxObject
*DoCreateResource() = 0;
254 // Returns TRUE if it understands this node and can create
255 // resource from it, FALSE otherwise.
256 virtual bool CanHandle(wxXmlNode
*node
) = 0;
258 void SetParentResource(wxXmlResource
*res
) { m_resource
= res
; }
263 wxXmlResource
*m_resource
;
264 wxArrayString m_styleNames
;
265 wxArrayInt m_styleValues
;
267 // Variables (filled by CreateResource)
270 wxObject
*m_parent
, *m_instance
;
271 wxWindow
*m_parentAsWindow
, *m_instanceAsWindow
;
273 // --- Handy methods:
275 // Returns true if the node has property class equal to classname,
276 // e.g. <object class="wxDialog">
277 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
278 { return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
; }
280 // Gets node content from wxXML_ENTITY_NODE
281 // (the problem is, <tag>content<tag> is represented as
282 // wxXML_ENTITY_NODE name="tag", content=""
283 // |-- wxXML_TEXT_NODE or
284 // wxXML_CDATA_SECTION_NODE name="" content="content"
285 wxString
GetNodeContent(wxXmlNode
*node
);
287 // Check to see if a param exists
288 bool HasParam(const wxString
& param
);
290 // Finds the node or returns NULL
291 wxXmlNode
*GetParamNode(const wxString
& param
);
292 wxString
GetParamValue(const wxString
& param
);
294 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
295 // understood by this handler
296 void AddStyle(const wxString
& name
, int value
);
298 // Add styles common to all wxWindow-derived classes
299 void AddWindowStyles();
301 // Gets style flags from text in form "flag | flag2| flag3 |..."
302 // Only understads flags added with AddStyle
303 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0);
305 // Gets text from param and does some convertions:
306 // - replaces \n, \r, \t by respective chars (according to C syntax)
307 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
308 // - calls wxGetTranslations (unless disabled in wxXmlResource)
309 wxString
GetText(const wxString
& param
);
315 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
316 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
318 // Get integer value from param
319 long GetLong( const wxString
& param
, long defaultv
= 0 );
321 // Get colour in HTML syntax (#RRGGBB)
322 wxColour
GetColour(const wxString
& param
);
324 // Get size/position (may be in dlg units):
325 wxSize
GetSize(const wxString
& param
= wxT("size"));
326 wxPoint
GetPosition(const wxString
& param
= wxT("pos"));
328 // Get dimension (may be in dlg units):
329 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0);
332 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
333 wxSize size
= wxDefaultSize
);
334 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
335 wxSize size
= wxDefaultSize
);
338 wxFont
GetFont(const wxString
& param
= wxT("font"));
340 // Sets common window options:
341 void SetupWindow(wxWindow
*wnd
);
343 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= FALSE
);
344 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
345 wxObject
*CreateResFromNode(wxXmlNode
*node
,
346 wxObject
*parent
, wxObject
*instance
= NULL
)
347 { return m_resource
->CreateResFromNode(node
, parent
, instance
); }
350 wxFileSystem
& GetCurFileSystem() { return m_resource
->GetCurFileSystem(); }
353 #define ADD_STYLE(style) AddStyle(wxT(#style), style)
356 void wxXmlInitResourceModule();
358 #endif // _WX_XMLRES_H_