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