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