]> git.saurik.com Git - wxWidgets.git/blame - contrib/include/wx/xrc/xmlres.h
mention the key code changes
[wxWidgets.git] / contrib / 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"
af1337b0 27#include "wx/artprov.h"
56d2f750 28
999d9a9f 29#include "wx/xrc/xml.h"
ea89ec17 30
1ad83f6b
VS
31class WXDLLEXPORT wxMenu;
32class WXDLLEXPORT wxMenuBar;
33class WXDLLEXPORT wxDialog;
34class WXDLLEXPORT wxPanel;
35class WXDLLEXPORT wxWindow;
1486ed87 36class WXDLLEXPORT wxFrame;
792064e9 37class WXDLLEXPORT wxToolBar;
56d2f750 38
ea89ec17 39class WXXMLDLLEXPORT wxXmlResourceHandler;
56d2f750 40
56d2f750 41
1ce70313
VS
42// These macros indicate current version of XML resources (this information is
43// encoded in root node of XRC file as "version" property).
44//
45// Rules for increasing version number:
46// - change it only if you made incompatible change to the format. Addition of new
47// attribute to control handler is _not_ incompatible change, because older
48// versions of the library may ignore it.
49// - if you change version number, follow these steps:
50// - set major, minor and release numbers to respective version numbers of
51// the wxWindows library (see wx/version.h)
52// - reset revision to 0 unless the first three are same as before, in which
53// case you should increase revision by one
54#define WX_XMLRES_CURRENT_VERSION_MAJOR 2
55#define WX_XMLRES_CURRENT_VERSION_MINOR 3
56#define WX_XMLRES_CURRENT_VERSION_RELEASE 0
57#define WX_XMLRES_CURRENT_VERSION_REVISION 1
58#define WX_XMLRES_CURRENT_VERSION_STRING "2.3.0.1"
59
60#define WX_XMLRES_CURRENT_VERSION \
61 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
62 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
63 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
64 WX_XMLRES_CURRENT_VERSION_REVISION)
56d2f750 65
ea89ec17 66class WXXMLDLLEXPORT wxXmlResourceDataRecord
56d2f750 67{
eb8671f2
VS
68public:
69 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
70 ~wxXmlResourceDataRecord() {delete Doc;}
71
72 wxString File;
73 wxXmlDocument *Doc;
74 wxDateTime Time;
56d2f750
VS
75};
76
ea89ec17
RD
77
78#ifdef WXXMLISDLL
1ad83f6b 79WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
ea89ec17
RD
80#else
81WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
82#endif
56d2f750 83
daa85ee3
VS
84enum wxXmlResourceFlags
85{
86 wxXRC_USE_LOCALE = 1,
87 wxXRC_NO_SUBCLASSING = 2
88};
ea89ec17
RD
89
90// This class holds XML resources from one or more .xml files
56d2f750 91// (or derived forms, either binary or zipped -- see manual for
ea89ec17 92// details).
ea89ec17 93class WXXMLDLLEXPORT wxXmlResource : public wxObject
56d2f750 94{
eb8671f2 95public:
273b399f 96 // Constructor.
daa85ee3
VS
97 // Flags: wxXRC_USE_LOCALE
98 // translatable strings will be translated via _()
99 // wxXRC_NO_SUBCLASSING
100 // subclass property of object nodes will be ignored
101 // (useful for previews in XRC editors)
102 wxXmlResource(int flags = wxXRC_USE_LOCALE);
273b399f
JS
103
104 // Constructor.
105 // Flags: wxXRC_USE_LOCALE
106 // translatable strings will be translated via _()
107 // wxXRC_NO_SUBCLASSING
108 // subclass property of object nodes will be ignored
109 // (useful for previews in XRC editors)
daa85ee3 110 wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
273b399f
JS
111
112 // Destructor.
eb8671f2 113 ~wxXmlResource();
824e8eaa 114
eb8671f2
VS
115 // Loads resources from XML files that match given filemask.
116 // This method understands VFS (see filesys.h).
117 bool Load(const wxString& filemask);
118
119 // Initialize handlers for all supported controls/windows. This will
120 // make the executable quite big because it forces linking against
273b399f 121 // most of the wxWindows library.
eb8671f2
VS
122 void InitAllHandlers();
123
273b399f
JS
124 // Initialize only a specific handler (or custom handler). Convention says
125 // that handler name is equal to the control's name plus 'XmlHandler', for example
126 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource compiler
ea89ec17 127 // (xmlres) can create include file that contains initialization code for
eb8671f2
VS
128 // all controls used within the resource.
129 void AddHandler(wxXmlResourceHandler *handler);
130
131 // Removes all handlers
132 void ClearHandlers();
133
134 // Loads menu from resource. Returns NULL on failure.
135 wxMenu *LoadMenu(const wxString& name);
136
137 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 138 wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
273b399f
JS
139
140 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 141 wxMenuBar *LoadMenuBar(const wxString& name) { return LoadMenuBar(NULL, name); }
56d2f750 142
792064e9 143#if wxUSE_TOOLBAR
273b399f 144 // Loads a toolbar.
eb8671f2 145 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
792064e9
VS
146#endif
147
273b399f
JS
148 // Loads a dialog. dlg points to parent window (if any).
149 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
150
151 // Loads a dialog. dlg points to parent window (if any). This form
eb8671f2
VS
152 // is used to finish creation of already existing instance (main reason
153 // for this is that you may want to use derived class with new event table)
154 // Example (typical usage):
155 // MyDialog dlg;
156 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
157 // dlg->ShowModal();
eb8671f2
VS
158 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
159
273b399f 160 // Loads a panel. panel points to parent window (if any).
eb8671f2 161 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
273b399f
JS
162
163 // Loads a panel. panel points to parent window (if any). This form
164 // is used to finish creation of already existing instance.
eb8671f2
VS
165 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
166
273b399f 167 // Loads a frame.
eb8671f2
VS
168 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
169
273b399f 170 // Loads a bitmap resource from a file.
eb8671f2 171 wxBitmap LoadBitmap(const wxString& name);
273b399f
JS
172
173 // Loads an icon resource from a file.
eb8671f2 174 wxIcon LoadIcon(const wxString& name);
ea89ec17 175
273b399f
JS
176 // Attaches an unknown control to the given panel/window/dialog.
177 // Unknown controls are used in conjunction with <object class="unknown">.
e6c3f404
VS
178 bool AttachUnknownControl(const wxString& name, wxWindow *control,
179 wxWindow *parent = NULL);
eb8671f2 180
273b399f
JS
181 // Returns a numeric ID that is equivalent to the string id used in an XML
182 // resource. To be used in event tables.
5ed345b7
VS
183 // Macro XRCID is provided for convenience
184 static int GetXRCID(const wxChar *str_id);
ea89ec17 185
273b399f 186 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
1ce70313 187 long GetVersion() const { return m_version; }
ea89ec17 188
1ce70313
VS
189 // Compares resources version to argument. Returns -1 if resources version
190 // is less than the argument, +1 if greater and 0 if they equal.
191 int CompareVersion(int major, int minor, int release, int revision) const
ea89ec17 192 { return GetVersion() -
1ce70313 193 (major*256*256*256 + minor*256*256 + release*256 + revision); }
824e8eaa 194
273b399f 195//// Singleton accessors.
824e8eaa 196
273b399f 197 // Gets the global resources object or creates one if none exists.
824e8eaa 198 static wxXmlResource *Get();
273b399f
JS
199
200 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
824e8eaa 201 static wxXmlResource *Set(wxXmlResource *res);
eb8671f2
VS
202
203protected:
273b399f 204 // Scans the resources list for unloaded files and loads them. Also reloads
eb8671f2
VS
205 // files that have been modified since last loading.
206 void UpdateResources();
207
273b399f 208 // Finds a resource (calls UpdateResources) and returns a node containing it.
47793ab8 209 wxXmlNode *FindResource(const wxString& name, const wxString& classname, bool recursive = FALSE);
273b399f
JS
210
211 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
47793ab8 212 wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive);
eb8671f2 213
273b399f 214 // Creates a resource from information in the given node.
eb8671f2
VS
215 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
216
273b399f 217 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
daa85ee3 218 int GetFlags() { return m_flags; }
eb8671f2
VS
219
220private:
1ce70313 221 long m_version;
ea89ec17 222
daa85ee3 223 int m_flags;
eb8671f2
VS
224 wxList m_handlers;
225 wxXmlResourceDataRecords m_data;
792064e9 226#if wxUSE_FILESYSTEM
eb8671f2
VS
227 wxFileSystem m_curFileSystem;
228 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
792064e9
VS
229#endif
230
eb8671f2 231 friend class wxXmlResourceHandler;
824e8eaa
VS
232
233 // singleton instance:
234 static wxXmlResource *ms_instance;
56d2f750
VS
235};
236
237
56d2f750
VS
238// This macro translates string identifier (as used in XML resource,
239// e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
240// wxWindows event tables.
241// Example:
242// BEGIN_EVENT_TABLE(MyFrame, wxFrame)
5ed345b7
VS
243// EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
244// EVT_MENU(XRCID("about"), MyFrame::OnAbout)
245// EVT_MENU(XRCID("new"), MyFrame::OnNew)
246// EVT_MENU(XRCID("open"), MyFrame::OnOpen)
ea89ec17 247// END_EVENT_TABLE()
45a24817 248
5ed345b7
VS
249#define XRCID(str_id) \
250 wxXmlResource::GetXRCID(wxT(str_id))
45a24817
VS
251
252
45a24817
VS
253// This macro returns pointer to particular control in dialog
254// created using XML resources. You can use it to set/get values from
255// controls.
256// Example:
257// wxDialog dlg;
546b0006 258// wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
5ed345b7 259// XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
45a24817 260
9c9fc51b 261#ifdef __WXDEBUG__
5ed345b7
VS
262#define XRCCTRL(window, id, type) \
263 (wxDynamicCast((window).FindWindow(XRCID(id)), type))
9c9fc51b 264#else
5ed345b7
VS
265#define XRCCTRL(window, id, type) \
266 ((type*)((window).FindWindow(XRCID(id))))
9c9fc51b 267#endif
56d2f750 268
273b399f
JS
269// wxXmlResourceHandler is an abstract base class for resource handlers
270// capable of creating a control from an XML node.
56d2f750 271
ea89ec17 272class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject
56d2f750 273{
eb8671f2 274public:
273b399f 275 // Constructor.
eb8671f2 276 wxXmlResourceHandler();
273b399f
JS
277
278 // Destructor.
eb8671f2
VS
279 virtual ~wxXmlResourceHandler() {}
280
273b399f 281 // Creates an object (menu, dialog, control, ...) from an XML node.
eb8671f2 282 // Should check for validity.
273b399f
JS
283 // parent is a higher-level object (usually window, dialog or panel)
284 // that is often neccessary to create the resource.
285 // If instance is non-NULL it should not create a new instance via 'new' but
286 // should rather use this one, and call its Create method.
ea89ec17 287 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
eb8671f2
VS
288 wxObject *instance);
289
290 // This one is called from CreateResource after variables
273b399f 291 // were filled.
eb8671f2
VS
292 virtual wxObject *DoCreateResource() = 0;
293
294 // Returns TRUE if it understands this node and can create
273b399f 295 // a resource from it, FALSE otherwise.
eb8671f2
VS
296 virtual bool CanHandle(wxXmlNode *node) = 0;
297
273b399f 298 // Sets the parent resource.
eb8671f2
VS
299 void SetParentResource(wxXmlResource *res) { m_resource = res; }
300
eb8671f2 301protected:
eb8671f2
VS
302 wxXmlResource *m_resource;
303 wxArrayString m_styleNames;
304 wxArrayInt m_styleValues;
305
306 // Variables (filled by CreateResource)
307 wxXmlNode *m_node;
308 wxString m_class;
309 wxObject *m_parent, *m_instance;
310 wxWindow *m_parentAsWindow, *m_instanceAsWindow;
311
312 // --- Handy methods:
313
273b399f
JS
314 // Returns true if the node has a property class equal to classname,
315 // e.g. <object class="wxDialog">.
eb8671f2
VS
316 bool IsOfClass(wxXmlNode *node, const wxString& classname)
317 { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; }
318
319 // Gets node content from wxXML_ENTITY_NODE
273b399f 320 // The problem is, <tag>content<tag> is represented as
eb8671f2 321 // wxXML_ENTITY_NODE name="tag", content=""
ea89ec17 322 // |-- wxXML_TEXT_NODE or
eb8671f2
VS
323 // wxXML_CDATA_SECTION_NODE name="" content="content"
324 wxString GetNodeContent(wxXmlNode *node);
325
273b399f 326 // Check to see if a parameter exists.
ea89ec17 327 bool HasParam(const wxString& param);
eb8671f2 328
273b399f 329 // Finds the node or returns NULL.
eb8671f2 330 wxXmlNode *GetParamNode(const wxString& param);
273b399f
JS
331
332 // Finds the parameter value or returns the empty string.
eb8671f2
VS
333 wxString GetParamValue(const wxString& param);
334
273b399f
JS
335 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
336 // understood by this handler.
eb8671f2
VS
337 void AddStyle(const wxString& name, int value);
338
273b399f 339 // Add styles common to all wxWindow-derived classes.
eb8671f2
VS
340 void AddWindowStyles();
341
342 // Gets style flags from text in form "flag | flag2| flag3 |..."
343 // Only understads flags added with AddStyle
344 int GetStyle(const wxString& param = wxT("style"), int defaults = 0);
345
273b399f 346 // Gets text from param and does some conversions:
eb8671f2 347 // - replaces \n, \r, \t by respective chars (according to C syntax)
ee1046d1 348 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
eb8671f2 349 // - calls wxGetTranslations (unless disabled in wxXmlResource)
ee1046d1 350 wxString GetText(const wxString& param, bool translate = TRUE);
eb8671f2 351
5ed345b7 352 // Returns the XRCID.
eb8671f2 353 int GetID();
273b399f
JS
354
355 // Returns the resource name.
eb8671f2
VS
356 wxString GetName();
357
273b399f 358 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
eb8671f2
VS
359 bool GetBool(const wxString& param, bool defaultv = FALSE);
360
273b399f 361 // Gets the integer value from the parameter.
eb8671f2
VS
362 long GetLong( const wxString& param, long defaultv = 0 );
363
273b399f 364 // Gets colour in HTML syntax (#RRGGBB).
eb8671f2
VS
365 wxColour GetColour(const wxString& param);
366
273b399f 367 // Gets the size (may be in dialog units).
eb8671f2 368 wxSize GetSize(const wxString& param = wxT("size"));
273b399f
JS
369
370 // Gets the position (may be in dialog units).
eb8671f2
VS
371 wxPoint GetPosition(const wxString& param = wxT("pos"));
372
273b399f 373 // Gets a dimension (may be in dialog units).
eb8671f2
VS
374 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
375
273b399f 376 // Gets a bitmap.
1ce70313 377 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
7a442f31 378 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2 379 wxSize size = wxDefaultSize);
273b399f
JS
380
381 // Gets an icon.
1ce70313 382 wxIcon GetIcon(const wxString& param = wxT("icon"),
7a442f31 383 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2
VS
384 wxSize size = wxDefaultSize);
385
273b399f 386 // Gets a font.
eb8671f2
VS
387 wxFont GetFont(const wxString& param = wxT("font"));
388
273b399f 389 // Sets common window options.
eb8671f2
VS
390 void SetupWindow(wxWindow *wnd);
391
273b399f 392 // Creates children.
eb8671f2 393 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
273b399f
JS
394
395 // Helper function.
eb8671f2 396 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
273b399f
JS
397
398 // Creates a resource from a node.
ea89ec17 399 wxObject *CreateResFromNode(wxXmlNode *node,
eb8671f2
VS
400 wxObject *parent, wxObject *instance = NULL)
401 { return m_resource->CreateResFromNode(node, parent, instance); }
402
403 // helper
150e58d8 404#if wxUSE_FILESYSTEM
eb8671f2 405 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
150e58d8 406#endif
56d2f750
VS
407};
408
daa85ee3
VS
409
410// Programmer-friendly macros for writing XRC handlers:
411
412#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
daa85ee3
VS
413
414#define XRC_MAKE_INSTANCE(variable, classname) \
415 classname *variable = NULL; \
416 if (m_instance) \
417 variable = wxStaticCast(m_instance, classname); \
418 if (!variable) \
419 variable = new classname;
56d2f750
VS
420
421
daa85ee3 422// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
ea89ec17 423void wxXmlInitResourceModule();
56d2f750 424
546b0006
VS
425
426/* -------------------------------------------------------------------------
427 Backward compatibility macros. Do *NOT* use, they may disappear in future
428 versions of the XRC library!
429 ------------------------------------------------------------------------- */
430#define ADD_STYLE XRC_ADD_STYLE
431#define wxTheXmlResource wxXmlResource::Get()
5ed345b7
VS
432#define XMLID XRCID
433#define XMLCTRL XRCCTRL
11032cd9 434#define GetXMLID GetXRCID
546b0006
VS
435
436
56d2f750 437#endif // _WX_XMLRES_H_