]> git.saurik.com Git - wxWidgets.git/blame_incremental - contrib/include/wx/xrc/xmlres.h
fixed typo (missing comma) in MIME test
[wxWidgets.git] / contrib / 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#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"
24#include "wx/filesys.h"
25#include "wx/bitmap.h"
26#include "wx/icon.h"
27
28#include "wx/xrc/xml.h"
29
30class WXDLLEXPORT wxMenu;
31class WXDLLEXPORT wxMenuBar;
32class WXDLLEXPORT wxDialog;
33class WXDLLEXPORT wxPanel;
34class WXDLLEXPORT wxWindow;
35class WXDLLEXPORT wxFrame;
36class WXDLLEXPORT wxToolBar;
37
38class WXXMLDLLEXPORT wxXmlResourceHandler;
39
40
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)
64
65class WXXMLDLLEXPORT wxXmlResourceDataRecord
66{
67public:
68 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
69 ~wxXmlResourceDataRecord() {delete Doc;}
70
71 wxString File;
72 wxXmlDocument *Doc;
73 wxDateTime Time;
74};
75
76
77#ifdef WXXMLISDLL
78WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
79#else
80WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
81#endif
82
83enum wxXmlResourceFlags
84{
85 wxXRC_USE_LOCALE = 1,
86 wxXRC_NO_SUBCLASSING = 2
87};
88
89// This class holds XML resources from one or more .xml files
90// (or derived forms, either binary or zipped -- see manual for
91// details).
92class WXXMLDLLEXPORT wxXmlResource : public wxObject
93{
94public:
95 // Constructor.
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);
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)
109 wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
110
111 // Destructor.
112 ~wxXmlResource();
113
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
120 // most of the wxWindows library.
121 void InitAllHandlers();
122
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
126 // (xmlres) can create include file that contains initialization code for
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.
137 wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
138
139 // Loads menubar from resource. Returns NULL on failure.
140 wxMenuBar *LoadMenuBar(const wxString& name) { return LoadMenuBar(NULL, name); }
141
142#if wxUSE_TOOLBAR
143 // Loads a toolbar.
144 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
145#endif
146
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
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();
157 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
158
159 // Loads a panel. panel points to parent window (if any).
160 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
161
162 // Loads a panel. panel points to parent window (if any). This form
163 // is used to finish creation of already existing instance.
164 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
165
166 // Loads a frame.
167 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
168
169 // Loads a bitmap resource from a file.
170 wxBitmap LoadBitmap(const wxString& name);
171
172 // Loads an icon resource from a file.
173 wxIcon LoadIcon(const wxString& name);
174
175 // Attaches an unknown control to the given panel/window/dialog.
176 // Unknown controls are used in conjunction with <object class="unknown">.
177 bool AttachUnknownControl(const wxString& name, wxWindow *control,
178 wxWindow *parent = NULL);
179
180 // Returns a numeric ID that is equivalent to the string id used in an XML
181 // resource. To be used in event tables.
182 // Macro XRCID is provided for convenience
183 static int GetXRCID(const wxChar *str_id);
184
185 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
186 long GetVersion() const { return m_version; }
187
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
191 { return GetVersion() -
192 (major*256*256*256 + minor*256*256 + release*256 + revision); }
193
194//// Singleton accessors.
195
196 // Gets the global resources object or creates one if none exists.
197 static wxXmlResource *Get();
198
199 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
200 static wxXmlResource *Set(wxXmlResource *res);
201
202protected:
203 // Scans the resources list for unloaded files and loads them. Also reloads
204 // files that have been modified since last loading.
205 void UpdateResources();
206
207 // Finds a resource (calls UpdateResources) and returns a node containing it.
208 wxXmlNode *FindResource(const wxString& name, const wxString& classname, bool recursive = FALSE);
209
210 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
211 wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive);
212
213 // Creates a resource from information in the given node.
214 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
215
216 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
217 int GetFlags() { return m_flags; }
218
219private:
220 long m_version;
221
222 int m_flags;
223 wxList m_handlers;
224 wxXmlResourceDataRecords m_data;
225#if wxUSE_FILESYSTEM
226 wxFileSystem m_curFileSystem;
227 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
228#endif
229
230 friend class wxXmlResourceHandler;
231
232 // singleton instance:
233 static wxXmlResource *ms_instance;
234};
235
236
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)
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)
246// END_EVENT_TABLE()
247
248#define XRCID(str_id) \
249 wxXmlResource::GetXRCID(wxT(str_id))
250
251
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;
257// wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
258// XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
259
260#ifdef __WXDEBUG__
261#define XRCCTRL(window, id, type) \
262 (wxDynamicCast((window).FindWindow(XRCID(id)), type))
263#else
264#define XRCCTRL(window, id, type) \
265 ((type*)((window).FindWindow(XRCID(id))))
266#endif
267
268// wxXmlResourceHandler is an abstract base class for resource handlers
269// capable of creating a control from an XML node.
270
271class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject
272{
273public:
274 // Constructor.
275 wxXmlResourceHandler();
276
277 // Destructor.
278 virtual ~wxXmlResourceHandler() {}
279
280 // Creates an object (menu, dialog, control, ...) from an XML node.
281 // Should check for validity.
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.
286 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
287 wxObject *instance);
288
289 // This one is called from CreateResource after variables
290 // were filled.
291 virtual wxObject *DoCreateResource() = 0;
292
293 // Returns TRUE if it understands this node and can create
294 // a resource from it, FALSE otherwise.
295 virtual bool CanHandle(wxXmlNode *node) = 0;
296
297 // Sets the parent resource.
298 void SetParentResource(wxXmlResource *res) { m_resource = res; }
299
300protected:
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
313 // Returns true if the node has a property class equal to classname,
314 // e.g. <object class="wxDialog">.
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
319 // The problem is, <tag>content<tag> is represented as
320 // wxXML_ENTITY_NODE name="tag", content=""
321 // |-- wxXML_TEXT_NODE or
322 // wxXML_CDATA_SECTION_NODE name="" content="content"
323 wxString GetNodeContent(wxXmlNode *node);
324
325 // Check to see if a parameter exists.
326 bool HasParam(const wxString& param);
327
328 // Finds the node or returns NULL.
329 wxXmlNode *GetParamNode(const wxString& param);
330
331 // Finds the parameter value or returns the empty string.
332 wxString GetParamValue(const wxString& param);
333
334 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
335 // understood by this handler.
336 void AddStyle(const wxString& name, int value);
337
338 // Add styles common to all wxWindow-derived classes.
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
345 // Gets text from param and does some conversions:
346 // - replaces \n, \r, \t by respective chars (according to C syntax)
347 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
348 // - calls wxGetTranslations (unless disabled in wxXmlResource)
349 wxString GetText(const wxString& param, bool translate = TRUE);
350
351 // Returns the XRCID.
352 int GetID();
353
354 // Returns the resource name.
355 wxString GetName();
356
357 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
358 bool GetBool(const wxString& param, bool defaultv = FALSE);
359
360 // Gets the integer value from the parameter.
361 long GetLong( const wxString& param, long defaultv = 0 );
362
363 // Gets colour in HTML syntax (#RRGGBB).
364 wxColour GetColour(const wxString& param);
365
366 // Gets the size (may be in dialog units).
367 wxSize GetSize(const wxString& param = wxT("size"));
368
369 // Gets the position (may be in dialog units).
370 wxPoint GetPosition(const wxString& param = wxT("pos"));
371
372 // Gets a dimension (may be in dialog units).
373 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
374
375 // Gets a bitmap.
376 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
377 wxSize size = wxDefaultSize);
378
379 // Gets an icon.
380 wxIcon GetIcon(const wxString& param = wxT("icon"),
381 wxSize size = wxDefaultSize);
382
383 // Gets a font.
384 wxFont GetFont(const wxString& param = wxT("font"));
385
386 // Sets common window options.
387 void SetupWindow(wxWindow *wnd);
388
389 // Creates children.
390 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
391
392 // Helper function.
393 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
394
395 // Creates a resource from a node.
396 wxObject *CreateResFromNode(wxXmlNode *node,
397 wxObject *parent, wxObject *instance = NULL)
398 { return m_resource->CreateResFromNode(node, parent, instance); }
399
400 // helper
401#if wxUSE_FILESYSTEM
402 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
403#endif
404};
405
406
407// Programmer-friendly macros for writing XRC handlers:
408
409#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
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;
417
418
419// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
420void wxXmlInitResourceModule();
421
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()
429#define XMLID XRCID
430#define XMLCTRL XRCCTRL
431
432
433#endif // _WX_XMLRES_H_