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