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