]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xrc/xmlres.h
corrected comment
[wxWidgets.git] / include / wx / xrc / xmlres.h
CommitLineData
56d2f750
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xmlres.h
3// Purpose: XML resources
4// Author: Vaclav Slavik
5// Created: 2000/03/05
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Vaclav Slavik
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_XMLRES_H_
12#define _WX_XMLRES_H_
13
14#ifdef __GNUG__
15#pragma interface "xmlres.h"
16#endif
17
18#include "wx/defs.h"
19#include "wx/string.h"
20#include "wx/dynarray.h"
21#include "wx/datetime.h"
22#include "wx/list.h"
23#include "wx/gdicmn.h"
792064e9
VS
24#include "wx/filesys.h"
25#include "wx/bitmap.h"
26#include "wx/icon.h"
56d2f750 27
999d9a9f 28#include "wx/xrc/xml.h"
ea89ec17 29
1ad83f6b
VS
30class WXDLLEXPORT wxMenu;
31class WXDLLEXPORT wxMenuBar;
32class WXDLLEXPORT wxDialog;
33class WXDLLEXPORT wxPanel;
34class WXDLLEXPORT wxWindow;
1486ed87 35class WXDLLEXPORT wxFrame;
792064e9 36class WXDLLEXPORT wxToolBar;
56d2f750 37
ea89ec17 38class WXXMLDLLEXPORT wxXmlResourceHandler;
56d2f750 39
56d2f750 40
1ce70313
VS
41// These macros indicate current version of XML resources (this information is
42// encoded in root node of XRC file as "version" property).
43//
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"
58
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)
56d2f750 64
ea89ec17 65class WXXMLDLLEXPORT wxXmlResourceDataRecord
56d2f750 66{
eb8671f2
VS
67public:
68 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
69 ~wxXmlResourceDataRecord() {delete Doc;}
70
71 wxString File;
72 wxXmlDocument *Doc;
73 wxDateTime Time;
56d2f750
VS
74};
75
ea89ec17
RD
76
77#ifdef WXXMLISDLL
1ad83f6b 78WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
ea89ec17
RD
79#else
80WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
81#endif
56d2f750 82
ea89ec17
RD
83
84// This class holds XML resources from one or more .xml files
56d2f750 85// (or derived forms, either binary or zipped -- see manual for
ea89ec17 86// details).
56d2f750 87
ea89ec17 88class WXXMLDLLEXPORT wxXmlResource : public wxObject
56d2f750 89{
eb8671f2
VS
90public:
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);
96 ~wxXmlResource();
97
98 // Loads resources from XML files that match given filemask.
99 // This method understands VFS (see filesys.h).
100 bool Load(const wxString& filemask);
101
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();
106
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
ea89ec17 110 // (xmlres) can create include file that contains initialization code for
eb8671f2
VS
111 // all controls used within the resource.
112 void AddHandler(wxXmlResourceHandler *handler);
113
114 // Removes all handlers
115 void ClearHandlers();
116
117 // Loads menu from resource. Returns NULL on failure.
118 wxMenu *LoadMenu(const wxString& name);
119
120 // Loads menubar from resource. Returns NULL on failure.
121 wxMenuBar *LoadMenuBar(const wxString& name);
56d2f750 122
792064e9 123#if wxUSE_TOOLBAR
eb8671f2
VS
124 // Loads toolbar
125 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
792064e9
VS
126#endif
127
eb8671f2
VS
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):
132 // MyDialog dlg;
133 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
134 // dlg->ShowModal();
135 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
136 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
137
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);
142
143 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
144
145 // Loads bitmap or icon resource from file:
146 wxBitmap LoadBitmap(const wxString& name);
147 wxIcon LoadIcon(const wxString& name);
ea89ec17 148
e6c3f404
VS
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);
eb8671f2
VS
153
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
00393283 157 static int GetXMLID(const wxChar *str_id);
ea89ec17 158
1ce70313
VS
159 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
160 long GetVersion() const { return m_version; }
ea89ec17 161
1ce70313
VS
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
ea89ec17 165 { return GetVersion() -
1ce70313 166 (major*256*256*256 + minor*256*256 + release*256 + revision); }
eb8671f2
VS
167
168protected:
169 // Scans resources list for unloaded files and loads them. Also reloads
170 // files that have been modified since last loading.
171 void UpdateResources();
172
173 // Finds resource (calls UpdateResources) and returns node containing it
174 wxXmlNode *FindResource(const wxString& name, const wxString& classname);
175
176 // Creates resource from info in given node:
177 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
178
eb8671f2
VS
179 bool GetUseLocale() { return m_useLocale; }
180
181private:
1ce70313 182 long m_version;
ea89ec17 183
eb8671f2
VS
184 bool m_useLocale;
185 wxList m_handlers;
186 wxXmlResourceDataRecords m_data;
792064e9 187#if wxUSE_FILESYSTEM
eb8671f2
VS
188 wxFileSystem m_curFileSystem;
189 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
792064e9
VS
190#endif
191
eb8671f2 192 friend class wxXmlResourceHandler;
56d2f750
VS
193};
194
195
196// Global instance of resource class. For your convenience.
5ab8bd57 197extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource;
56d2f750
VS
198
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.
202// Example:
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)
ea89ec17 208// END_EVENT_TABLE()
45a24817
VS
209
210#define XMLID(str_id) \
a559d708 211 wxXmlResource::GetXMLID(wxT(str_id))
45a24817
VS
212
213
45a24817
VS
214// This macro returns pointer to particular control in dialog
215// created using XML resources. You can use it to set/get values from
216// controls.
217// Example:
218// wxDialog dlg;
219// wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
a559d708 220// XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
45a24817 221
9c9fc51b
VS
222#ifdef __WXDEBUG__
223#define XMLCTRL(window, id, type) \
224 (wxDynamicCast((window).FindWindow(XMLID(id)), type))
225#else
45a24817
VS
226#define XMLCTRL(window, id, type) \
227 ((type*)((window).FindWindow(XMLID(id))))
9c9fc51b 228#endif
56d2f750
VS
229
230
ea89ec17 231class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject
56d2f750 232{
eb8671f2
VS
233public:
234 wxXmlResourceHandler();
235 virtual ~wxXmlResourceHandler() {}
236
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
ea89ec17 243 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
eb8671f2
VS
244 wxObject *instance);
245
246 // This one is called from CreateResource after variables
247 // were filled
248 virtual wxObject *DoCreateResource() = 0;
249
250 // Returns TRUE if it understands this node and can create
251 // resource from it, FALSE otherwise.
252 virtual bool CanHandle(wxXmlNode *node) = 0;
253
254 void SetParentResource(wxXmlResource *res) { m_resource = res; }
255
256
257protected:
258
259 wxXmlResource *m_resource;
260 wxArrayString m_styleNames;
261 wxArrayInt m_styleValues;
262
263 // Variables (filled by CreateResource)
264 wxXmlNode *m_node;
265 wxString m_class;
266 wxObject *m_parent, *m_instance;
267 wxWindow *m_parentAsWindow, *m_instanceAsWindow;
268
269 // --- Handy methods:
270
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; }
275
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=""
ea89ec17 279 // |-- wxXML_TEXT_NODE or
eb8671f2
VS
280 // wxXML_CDATA_SECTION_NODE name="" content="content"
281 wxString GetNodeContent(wxXmlNode *node);
282
283 // Check to see if a param exists
ea89ec17 284 bool HasParam(const wxString& param);
eb8671f2
VS
285
286 // Finds the node or returns NULL
287 wxXmlNode *GetParamNode(const wxString& param);
288 wxString GetParamValue(const wxString& param);
289
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);
293
294 // Add styles common to all wxWindow-derived classes
295 void AddWindowStyles();
296
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);
300
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);
306
307 // Return XMLID
308 int GetID();
309 wxString GetName();
310
311 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
312 bool GetBool(const wxString& param, bool defaultv = FALSE);
313
314 // Get integer value from param
315 long GetLong( const wxString& param, long defaultv = 0 );
316
317 // Get colour in HTML syntax (#RRGGBB)
318 wxColour GetColour(const wxString& param);
319
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"));
323
324 // Get dimension (may be in dlg units):
325 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
326
327 // Get bitmap:
1ce70313 328 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
eb8671f2 329 wxSize size = wxDefaultSize);
1ce70313 330 wxIcon GetIcon(const wxString& param = wxT("icon"),
eb8671f2
VS
331 wxSize size = wxDefaultSize);
332
333 // Get font:
334 wxFont GetFont(const wxString& param = wxT("font"));
335
336 // Sets common window options:
337 void SetupWindow(wxWindow *wnd);
338
339 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
340 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
ea89ec17 341 wxObject *CreateResFromNode(wxXmlNode *node,
eb8671f2
VS
342 wxObject *parent, wxObject *instance = NULL)
343 { return m_resource->CreateResFromNode(node, parent, instance); }
344
345 // helper
150e58d8 346#if wxUSE_FILESYSTEM
eb8671f2 347 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
150e58d8 348#endif
56d2f750
VS
349};
350
a559d708 351#define ADD_STYLE(style) AddStyle(wxT(#style), style)
56d2f750
VS
352
353
ea89ec17 354void wxXmlInitResourceModule();
56d2f750
VS
355
356#endif // _WX_XMLRES_H_