]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xrc/xmlres.h
declare wxEntry() as taking char **Argv, not char *argv[]
[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
ab7ce33c 14#if defined(__GNUG__) && !defined(__APPLE__)
56d2f750
VS
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();
92e898b0 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
92e898b0
RD
131 // Add a new handler at the begining of the handler list
132 void InsertHandler(wxXmlResourceHandler *handler);
133
eb8671f2
VS
134 // Removes all handlers
135 void ClearHandlers();
136
137 // Loads menu from resource. Returns NULL on failure.
138 wxMenu *LoadMenu(const wxString& name);
139
140 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 141 wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
273b399f
JS
142
143 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 144 wxMenuBar *LoadMenuBar(const wxString& name) { return LoadMenuBar(NULL, name); }
56d2f750 145
792064e9 146#if wxUSE_TOOLBAR
273b399f 147 // Loads a toolbar.
eb8671f2 148 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
792064e9
VS
149#endif
150
273b399f
JS
151 // Loads a dialog. dlg points to parent window (if any).
152 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
153
154 // Loads a dialog. dlg points to parent window (if any). This form
eb8671f2
VS
155 // is used to finish creation of already existing instance (main reason
156 // for this is that you may want to use derived class with new event table)
157 // Example (typical usage):
158 // MyDialog dlg;
159 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
160 // dlg->ShowModal();
eb8671f2
VS
161 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
162
273b399f 163 // Loads a panel. panel points to parent window (if any).
eb8671f2 164 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
273b399f
JS
165
166 // Loads a panel. panel points to parent window (if any). This form
167 // is used to finish creation of already existing instance.
eb8671f2
VS
168 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
169
273b399f 170 // Loads a frame.
92e898b0 171 wxFrame *LoadFrame(wxWindow* parent, const wxString& name);
eb8671f2
VS
172 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
173
92e898b0
RD
174 // Load an object from the resource specifying both the resource name and
175 // the classname. This lets you load nonstandard container windows.
176 wxObject *LoadObject(wxWindow *parent, const wxString& name,
177 const wxString& classname);
178
179 // Load an object from the resource specifying both the resource name and
180 // the classname. This form lets you finish the creation of an existing
181 // instance.
182 bool LoadObject(wxObject *instance, wxWindow *parent, const wxString& name,
183 const wxString& classname);
184
273b399f 185 // Loads a bitmap resource from a file.
eb8671f2 186 wxBitmap LoadBitmap(const wxString& name);
273b399f
JS
187
188 // Loads an icon resource from a file.
eb8671f2 189 wxIcon LoadIcon(const wxString& name);
ea89ec17 190
273b399f
JS
191 // Attaches an unknown control to the given panel/window/dialog.
192 // Unknown controls are used in conjunction with <object class="unknown">.
e6c3f404
VS
193 bool AttachUnknownControl(const wxString& name, wxWindow *control,
194 wxWindow *parent = NULL);
eb8671f2 195
273b399f
JS
196 // Returns a numeric ID that is equivalent to the string id used in an XML
197 // resource. To be used in event tables.
5ed345b7
VS
198 // Macro XRCID is provided for convenience
199 static int GetXRCID(const wxChar *str_id);
ea89ec17 200
273b399f 201 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
1ce70313 202 long GetVersion() const { return m_version; }
ea89ec17 203
1ce70313
VS
204 // Compares resources version to argument. Returns -1 if resources version
205 // is less than the argument, +1 if greater and 0 if they equal.
206 int CompareVersion(int major, int minor, int release, int revision) const
ea89ec17 207 { return GetVersion() -
1ce70313 208 (major*256*256*256 + minor*256*256 + release*256 + revision); }
92e898b0 209
273b399f 210//// Singleton accessors.
92e898b0 211
273b399f 212 // Gets the global resources object or creates one if none exists.
824e8eaa 213 static wxXmlResource *Get();
273b399f
JS
214
215 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
824e8eaa 216 static wxXmlResource *Set(wxXmlResource *res);
eb8671f2 217
74c107ba
VS
218 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
219 int GetFlags() { return m_flags; }
220
eb8671f2 221protected:
273b399f 222 // Scans the resources list for unloaded files and loads them. Also reloads
eb8671f2
VS
223 // files that have been modified since last loading.
224 void UpdateResources();
225
273b399f 226 // Finds a resource (calls UpdateResources) and returns a node containing it.
47793ab8 227 wxXmlNode *FindResource(const wxString& name, const wxString& classname, bool recursive = FALSE);
273b399f
JS
228
229 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
47793ab8 230 wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive);
eb8671f2 231
273b399f 232 // Creates a resource from information in the given node.
eb8671f2
VS
233 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
234
eb8671f2 235private:
1ce70313 236 long m_version;
ea89ec17 237
daa85ee3 238 int m_flags;
eb8671f2
VS
239 wxList m_handlers;
240 wxXmlResourceDataRecords m_data;
792064e9 241#if wxUSE_FILESYSTEM
eb8671f2
VS
242 wxFileSystem m_curFileSystem;
243 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
792064e9
VS
244#endif
245
eb8671f2 246 friend class wxXmlResourceHandler;
92e898b0 247
824e8eaa
VS
248 // singleton instance:
249 static wxXmlResource *ms_instance;
56d2f750
VS
250};
251
252
56d2f750
VS
253// This macro translates string identifier (as used in XML resource,
254// e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
255// wxWindows event tables.
256// Example:
257// BEGIN_EVENT_TABLE(MyFrame, wxFrame)
5ed345b7
VS
258// EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
259// EVT_MENU(XRCID("about"), MyFrame::OnAbout)
260// EVT_MENU(XRCID("new"), MyFrame::OnNew)
261// EVT_MENU(XRCID("open"), MyFrame::OnOpen)
ea89ec17 262// END_EVENT_TABLE()
45a24817 263
5ed345b7
VS
264#define XRCID(str_id) \
265 wxXmlResource::GetXRCID(wxT(str_id))
45a24817
VS
266
267
45a24817
VS
268// This macro returns pointer to particular control in dialog
269// created using XML resources. You can use it to set/get values from
270// controls.
271// Example:
272// wxDialog dlg;
546b0006 273// wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
5ed345b7 274// XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
45a24817 275
9c9fc51b 276#ifdef __WXDEBUG__
5ed345b7
VS
277#define XRCCTRL(window, id, type) \
278 (wxDynamicCast((window).FindWindow(XRCID(id)), type))
9c9fc51b 279#else
5ed345b7
VS
280#define XRCCTRL(window, id, type) \
281 ((type*)((window).FindWindow(XRCID(id))))
9c9fc51b 282#endif
56d2f750 283
273b399f
JS
284// wxXmlResourceHandler is an abstract base class for resource handlers
285// capable of creating a control from an XML node.
56d2f750 286
ea89ec17 287class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject
56d2f750 288{
eb8671f2 289public:
273b399f 290 // Constructor.
eb8671f2 291 wxXmlResourceHandler();
273b399f
JS
292
293 // Destructor.
eb8671f2
VS
294 virtual ~wxXmlResourceHandler() {}
295
273b399f 296 // Creates an object (menu, dialog, control, ...) from an XML node.
eb8671f2 297 // Should check for validity.
273b399f
JS
298 // parent is a higher-level object (usually window, dialog or panel)
299 // that is often neccessary to create the resource.
300 // If instance is non-NULL it should not create a new instance via 'new' but
301 // should rather use this one, and call its Create method.
ea89ec17 302 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
eb8671f2
VS
303 wxObject *instance);
304
305 // This one is called from CreateResource after variables
273b399f 306 // were filled.
eb8671f2
VS
307 virtual wxObject *DoCreateResource() = 0;
308
309 // Returns TRUE if it understands this node and can create
273b399f 310 // a resource from it, FALSE otherwise.
eb8671f2
VS
311 virtual bool CanHandle(wxXmlNode *node) = 0;
312
273b399f 313 // Sets the parent resource.
eb8671f2
VS
314 void SetParentResource(wxXmlResource *res) { m_resource = res; }
315
eb8671f2 316protected:
eb8671f2
VS
317 wxXmlResource *m_resource;
318 wxArrayString m_styleNames;
319 wxArrayInt m_styleValues;
320
321 // Variables (filled by CreateResource)
322 wxXmlNode *m_node;
323 wxString m_class;
324 wxObject *m_parent, *m_instance;
325 wxWindow *m_parentAsWindow, *m_instanceAsWindow;
326
327 // --- Handy methods:
328
273b399f
JS
329 // Returns true if the node has a property class equal to classname,
330 // e.g. <object class="wxDialog">.
eb8671f2
VS
331 bool IsOfClass(wxXmlNode *node, const wxString& classname)
332 { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; }
333
334 // Gets node content from wxXML_ENTITY_NODE
273b399f 335 // The problem is, <tag>content<tag> is represented as
eb8671f2 336 // wxXML_ENTITY_NODE name="tag", content=""
ea89ec17 337 // |-- wxXML_TEXT_NODE or
eb8671f2
VS
338 // wxXML_CDATA_SECTION_NODE name="" content="content"
339 wxString GetNodeContent(wxXmlNode *node);
340
273b399f 341 // Check to see if a parameter exists.
ea89ec17 342 bool HasParam(const wxString& param);
eb8671f2 343
273b399f 344 // Finds the node or returns NULL.
eb8671f2 345 wxXmlNode *GetParamNode(const wxString& param);
273b399f
JS
346
347 // Finds the parameter value or returns the empty string.
eb8671f2
VS
348 wxString GetParamValue(const wxString& param);
349
273b399f
JS
350 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
351 // understood by this handler.
eb8671f2
VS
352 void AddStyle(const wxString& name, int value);
353
273b399f 354 // Add styles common to all wxWindow-derived classes.
eb8671f2
VS
355 void AddWindowStyles();
356
357 // Gets style flags from text in form "flag | flag2| flag3 |..."
358 // Only understads flags added with AddStyle
359 int GetStyle(const wxString& param = wxT("style"), int defaults = 0);
360
273b399f 361 // Gets text from param and does some conversions:
eb8671f2 362 // - replaces \n, \r, \t by respective chars (according to C syntax)
ee1046d1 363 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
eb8671f2 364 // - calls wxGetTranslations (unless disabled in wxXmlResource)
ee1046d1 365 wxString GetText(const wxString& param, bool translate = TRUE);
eb8671f2 366
5ed345b7 367 // Returns the XRCID.
eb8671f2 368 int GetID();
273b399f
JS
369
370 // Returns the resource name.
eb8671f2
VS
371 wxString GetName();
372
273b399f 373 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
eb8671f2
VS
374 bool GetBool(const wxString& param, bool defaultv = FALSE);
375
273b399f 376 // Gets the integer value from the parameter.
eb8671f2
VS
377 long GetLong( const wxString& param, long defaultv = 0 );
378
273b399f 379 // Gets colour in HTML syntax (#RRGGBB).
eb8671f2
VS
380 wxColour GetColour(const wxString& param);
381
273b399f 382 // Gets the size (may be in dialog units).
eb8671f2 383 wxSize GetSize(const wxString& param = wxT("size"));
273b399f
JS
384
385 // Gets the position (may be in dialog units).
eb8671f2
VS
386 wxPoint GetPosition(const wxString& param = wxT("pos"));
387
273b399f 388 // Gets a dimension (may be in dialog units).
eb8671f2
VS
389 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
390
273b399f 391 // Gets a bitmap.
1ce70313 392 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
7a442f31 393 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2 394 wxSize size = wxDefaultSize);
273b399f
JS
395
396 // Gets an icon.
1ce70313 397 wxIcon GetIcon(const wxString& param = wxT("icon"),
7a442f31 398 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2
VS
399 wxSize size = wxDefaultSize);
400
273b399f 401 // Gets a font.
eb8671f2
VS
402 wxFont GetFont(const wxString& param = wxT("font"));
403
273b399f 404 // Sets common window options.
eb8671f2
VS
405 void SetupWindow(wxWindow *wnd);
406
273b399f 407 // Creates children.
eb8671f2 408 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
273b399f
JS
409
410 // Helper function.
eb8671f2 411 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
273b399f
JS
412
413 // Creates a resource from a node.
ea89ec17 414 wxObject *CreateResFromNode(wxXmlNode *node,
eb8671f2
VS
415 wxObject *parent, wxObject *instance = NULL)
416 { return m_resource->CreateResFromNode(node, parent, instance); }
417
418 // helper
150e58d8 419#if wxUSE_FILESYSTEM
eb8671f2 420 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
150e58d8 421#endif
56d2f750
VS
422};
423
daa85ee3
VS
424
425// Programmer-friendly macros for writing XRC handlers:
426
427#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
daa85ee3
VS
428
429#define XRC_MAKE_INSTANCE(variable, classname) \
430 classname *variable = NULL; \
431 if (m_instance) \
432 variable = wxStaticCast(m_instance, classname); \
433 if (!variable) \
434 variable = new classname;
56d2f750
VS
435
436
daa85ee3 437// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
ea89ec17 438void wxXmlInitResourceModule();
56d2f750 439
546b0006 440
92e898b0 441/* -------------------------------------------------------------------------
546b0006
VS
442 Backward compatibility macros. Do *NOT* use, they may disappear in future
443 versions of the XRC library!
444 ------------------------------------------------------------------------- */
445#define ADD_STYLE XRC_ADD_STYLE
446#define wxTheXmlResource wxXmlResource::Get()
5ed345b7
VS
447#define XMLID XRCID
448#define XMLCTRL XRCCTRL
11032cd9 449#define GetXMLID GetXRCID
546b0006
VS
450
451
56d2f750 452#endif // _WX_XMLRES_H_