]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xrc/xmlres.h
rewrote wxTextFile::OnRead to handle variable-length encodings correctly
[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
daa85ee3
VS
83enum wxXmlResourceFlags
84{
85 wxXRC_USE_LOCALE = 1,
86 wxXRC_NO_SUBCLASSING = 2
87};
ea89ec17
RD
88
89// This class holds XML resources from one or more .xml files
56d2f750 90// (or derived forms, either binary or zipped -- see manual for
ea89ec17 91// details).
ea89ec17 92class WXXMLDLLEXPORT wxXmlResource : public wxObject
56d2f750 93{
eb8671f2 94public:
daa85ee3
VS
95 // Ctor.
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);
eb8671f2
VS
103 ~wxXmlResource();
104
105 // Loads resources from XML files that match given filemask.
106 // This method understands VFS (see filesys.h).
107 bool Load(const wxString& filemask);
108
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();
113
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
ea89ec17 117 // (xmlres) can create include file that contains initialization code for
eb8671f2
VS
118 // all controls used within the resource.
119 void AddHandler(wxXmlResourceHandler *handler);
120
121 // Removes all handlers
122 void ClearHandlers();
123
124 // Loads menu from resource. Returns NULL on failure.
125 wxMenu *LoadMenu(const wxString& name);
126
127 // Loads menubar from resource. Returns NULL on failure.
4a1b9596
VS
128 wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
129 wxMenuBar *LoadMenuBar(const wxString& name) { return LoadMenuBar(NULL, name); }
56d2f750 130
792064e9 131#if wxUSE_TOOLBAR
eb8671f2
VS
132 // Loads toolbar
133 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
792064e9
VS
134#endif
135
eb8671f2
VS
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):
140 // MyDialog dlg;
141 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
142 // dlg->ShowModal();
143 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
144 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
145
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);
150
151 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
152
153 // Loads bitmap or icon resource from file:
154 wxBitmap LoadBitmap(const wxString& name);
155 wxIcon LoadIcon(const wxString& name);
ea89ec17 156
e6c3f404
VS
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);
eb8671f2
VS
161
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
00393283 165 static int GetXMLID(const wxChar *str_id);
ea89ec17 166
1ce70313
VS
167 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
168 long GetVersion() const { return m_version; }
ea89ec17 169
1ce70313
VS
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
ea89ec17 173 { return GetVersion() -
1ce70313 174 (major*256*256*256 + minor*256*256 + release*256 + revision); }
eb8671f2
VS
175
176protected:
177 // Scans resources list for unloaded files and loads them. Also reloads
178 // files that have been modified since last loading.
179 void UpdateResources();
180
181 // Finds resource (calls UpdateResources) and returns node containing it
47793ab8
VS
182 wxXmlNode *FindResource(const wxString& name, const wxString& classname, bool recursive = FALSE);
183 wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive);
eb8671f2
VS
184
185 // Creates resource from info in given node:
186 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
187
daa85ee3 188 int GetFlags() { return m_flags; }
eb8671f2
VS
189
190private:
1ce70313 191 long m_version;
ea89ec17 192
daa85ee3 193 int m_flags;
eb8671f2
VS
194 wxList m_handlers;
195 wxXmlResourceDataRecords m_data;
792064e9 196#if wxUSE_FILESYSTEM
eb8671f2
VS
197 wxFileSystem m_curFileSystem;
198 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
792064e9
VS
199#endif
200
eb8671f2 201 friend class wxXmlResourceHandler;
56d2f750
VS
202};
203
204
205// Global instance of resource class. For your convenience.
5ab8bd57 206extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource;
56d2f750
VS
207
208// This macro translates string identifier (as used in XML resource,
209// e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
210// wxWindows event tables.
211// Example:
212// BEGIN_EVENT_TABLE(MyFrame, wxFrame)
213// EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
214// EVT_MENU(XMLID("about"), MyFrame::OnAbout)
215// EVT_MENU(XMLID("new"), MyFrame::OnNew)
216// EVT_MENU(XMLID("open"), MyFrame::OnOpen)
ea89ec17 217// END_EVENT_TABLE()
45a24817
VS
218
219#define XMLID(str_id) \
a559d708 220 wxXmlResource::GetXMLID(wxT(str_id))
45a24817
VS
221
222
45a24817
VS
223// This macro returns pointer to particular control in dialog
224// created using XML resources. You can use it to set/get values from
225// controls.
226// Example:
227// wxDialog dlg;
228// wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
a559d708 229// XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
45a24817 230
9c9fc51b
VS
231#ifdef __WXDEBUG__
232#define XMLCTRL(window, id, type) \
233 (wxDynamicCast((window).FindWindow(XMLID(id)), type))
234#else
45a24817
VS
235#define XMLCTRL(window, id, type) \
236 ((type*)((window).FindWindow(XMLID(id))))
9c9fc51b 237#endif
56d2f750
VS
238
239
ea89ec17 240class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject
56d2f750 241{
eb8671f2
VS
242public:
243 wxXmlResourceHandler();
244 virtual ~wxXmlResourceHandler() {}
245
246 // Creates object (menu, dialog, control, ...) from XML node.
247 // Should check for validity.
248 // parent is higher-level object (usually window, dialog or panel)
249 // that is often neccessary to create resource
250 // if instance != NULL it should not create new instance via 'new' but
251 // rather use this one and call its Create method
ea89ec17 252 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
eb8671f2
VS
253 wxObject *instance);
254
255 // This one is called from CreateResource after variables
256 // were filled
257 virtual wxObject *DoCreateResource() = 0;
258
259 // Returns TRUE if it understands this node and can create
260 // resource from it, FALSE otherwise.
261 virtual bool CanHandle(wxXmlNode *node) = 0;
262
263 void SetParentResource(wxXmlResource *res) { m_resource = res; }
264
265
266protected:
267
268 wxXmlResource *m_resource;
269 wxArrayString m_styleNames;
270 wxArrayInt m_styleValues;
271
272 // Variables (filled by CreateResource)
273 wxXmlNode *m_node;
274 wxString m_class;
275 wxObject *m_parent, *m_instance;
276 wxWindow *m_parentAsWindow, *m_instanceAsWindow;
277
278 // --- Handy methods:
279
280 // Returns true if the node has property class equal to classname,
281 // e.g. <object class="wxDialog">
282 bool IsOfClass(wxXmlNode *node, const wxString& classname)
283 { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; }
284
285 // Gets node content from wxXML_ENTITY_NODE
286 // (the problem is, <tag>content<tag> is represented as
287 // wxXML_ENTITY_NODE name="tag", content=""
ea89ec17 288 // |-- wxXML_TEXT_NODE or
eb8671f2
VS
289 // wxXML_CDATA_SECTION_NODE name="" content="content"
290 wxString GetNodeContent(wxXmlNode *node);
291
292 // Check to see if a param exists
ea89ec17 293 bool HasParam(const wxString& param);
eb8671f2
VS
294
295 // Finds the node or returns NULL
296 wxXmlNode *GetParamNode(const wxString& param);
297 wxString GetParamValue(const wxString& param);
298
299 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
300 // understood by this handler
301 void AddStyle(const wxString& name, int value);
302
303 // Add styles common to all wxWindow-derived classes
304 void AddWindowStyles();
305
306 // Gets style flags from text in form "flag | flag2| flag3 |..."
307 // Only understads flags added with AddStyle
308 int GetStyle(const wxString& param = wxT("style"), int defaults = 0);
309
310 // Gets text from param and does some convertions:
311 // - replaces \n, \r, \t by respective chars (according to C syntax)
312 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
313 // - calls wxGetTranslations (unless disabled in wxXmlResource)
314 wxString GetText(const wxString& param);
315
316 // Return XMLID
317 int GetID();
318 wxString GetName();
319
320 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
321 bool GetBool(const wxString& param, bool defaultv = FALSE);
322
323 // Get integer value from param
324 long GetLong( const wxString& param, long defaultv = 0 );
325
326 // Get colour in HTML syntax (#RRGGBB)
327 wxColour GetColour(const wxString& param);
328
329 // Get size/position (may be in dlg units):
330 wxSize GetSize(const wxString& param = wxT("size"));
331 wxPoint GetPosition(const wxString& param = wxT("pos"));
332
333 // Get dimension (may be in dlg units):
334 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
335
336 // Get bitmap:
1ce70313 337 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
eb8671f2 338 wxSize size = wxDefaultSize);
1ce70313 339 wxIcon GetIcon(const wxString& param = wxT("icon"),
eb8671f2
VS
340 wxSize size = wxDefaultSize);
341
342 // Get font:
343 wxFont GetFont(const wxString& param = wxT("font"));
344
345 // Sets common window options:
346 void SetupWindow(wxWindow *wnd);
347
348 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
349 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
ea89ec17 350 wxObject *CreateResFromNode(wxXmlNode *node,
eb8671f2
VS
351 wxObject *parent, wxObject *instance = NULL)
352 { return m_resource->CreateResFromNode(node, parent, instance); }
353
354 // helper
150e58d8 355#if wxUSE_FILESYSTEM
eb8671f2 356 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
150e58d8 357#endif
56d2f750
VS
358};
359
daa85ee3
VS
360
361// Programmer-friendly macros for writing XRC handlers:
362
363#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
364#define ADD_STYLE XRC_ADD_STYLE /* deprecated, don't use!! */
365
366#define XRC_MAKE_INSTANCE(variable, classname) \
367 classname *variable = NULL; \
368 if (m_instance) \
369 variable = wxStaticCast(m_instance, classname); \
370 if (!variable) \
371 variable = new classname;
56d2f750
VS
372
373
daa85ee3 374// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
ea89ec17 375void wxXmlInitResourceModule();
56d2f750
VS
376
377#endif // _WX_XMLRES_H_