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