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