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