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