]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xrc/xmlres.h
warning for 2.5.3
[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
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 #ifdef WXMAKINGDLL_XRC
35 #define WXDLLIMPEXP_XRC WXEXPORT
36 #elif defined(WXUSINGDLL)
37 #define WXDLLIMPEXP_XRC WXIMPORT
38 #else // not making nor using DLL
39 #define WXDLLIMPEXP_XRC
40 #endif
41
42 class WXDLLEXPORT wxMenu;
43 class WXDLLEXPORT wxMenuBar;
44 class WXDLLEXPORT wxDialog;
45 class WXDLLEXPORT wxPanel;
46 class WXDLLEXPORT wxWindow;
47 class WXDLLEXPORT wxFrame;
48 class WXDLLEXPORT wxToolBar;
49
50 class WXDLLIMPEXP_XRC wxXmlResourceHandler;
51 class WXDLLIMPEXP_XRC wxXmlSubclassFactory;
52 class WXDLLIMPEXP_XRC wxXmlSubclassFactoriesList;
53 class wxXmlResourceModule;
54
55
56 // These macros indicate current version of XML resources (this information is
57 // encoded in root node of XRC file as "version" property).
58 //
59 // Rules for increasing version number:
60 // - change it only if you made incompatible change to the format. Addition
61 // of new attribute to control handler is _not_ incompatible change, because
62 // older versions of the library may ignore it.
63 // - if you change version number, follow these steps:
64 // - set major, minor and release numbers to respective version numbers of
65 // the wxWidgets library (see wx/version.h)
66 // - reset revision to 0 unless the first three are same as before,
67 // in which case you should increase revision by one
68 #define WX_XMLRES_CURRENT_VERSION_MAJOR 2
69 #define WX_XMLRES_CURRENT_VERSION_MINOR 3
70 #define WX_XMLRES_CURRENT_VERSION_RELEASE 0
71 #define WX_XMLRES_CURRENT_VERSION_REVISION 1
72 #define WX_XMLRES_CURRENT_VERSION_STRING _T("2.3.0.1")
73
74 #define WX_XMLRES_CURRENT_VERSION \
75 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
76 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
77 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
78 WX_XMLRES_CURRENT_VERSION_REVISION)
79
80 class WXDLLIMPEXP_XRC wxXmlResourceDataRecord
81 {
82 public:
83 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
84 ~wxXmlResourceDataRecord() {delete Doc;}
85
86 wxString File;
87 wxXmlDocument *Doc;
88 wxDateTime Time;
89 };
90
91
92 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxXmlResourceDataRecord,
93 wxXmlResourceDataRecords,
94 WXDLLIMPEXP_XRC);
95
96 enum wxXmlResourceFlags
97 {
98 wxXRC_USE_LOCALE = 1,
99 wxXRC_NO_SUBCLASSING = 2,
100 wxXRC_NO_RELOADING = 4
101 };
102
103 // This class holds XML resources from one or more .xml files
104 // (or derived forms, either binary or zipped -- see manual for
105 // details).
106 class WXDLLIMPEXP_XRC wxXmlResource : public wxObject
107 {
108 public:
109 // Constructor.
110 // Flags: wxXRC_USE_LOCALE
111 // translatable strings will be translated via _()
112 // wxXRC_NO_SUBCLASSING
113 // subclass property of object nodes will be ignored
114 // (useful for previews in XRC editors)
115 wxXmlResource(int flags = wxXRC_USE_LOCALE);
116
117 // Constructor.
118 // Flags: wxXRC_USE_LOCALE
119 // translatable strings will be translated via _()
120 // wxXRC_NO_SUBCLASSING
121 // subclass property of object nodes will be ignored
122 // (useful for previews in XRC editors)
123 wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
124
125 // Destructor.
126 ~wxXmlResource();
127
128 // Loads resources from XML files that match given filemask.
129 // This method understands VFS (see filesys.h).
130 bool Load(const wxString& filemask);
131
132 // Initialize handlers for all supported controls/windows. This will
133 // make the executable quite big because it forces linking against
134 // most of the wxWidgets library.
135 void InitAllHandlers();
136
137 // Initialize only a specific handler (or custom handler). Convention says
138 // that handler name is equal to the control's name plus 'XmlHandler', for
139 // example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource
140 // compiler (xmlres) can create include file that contains initialization
141 // code for all controls used within the resource.
142 void AddHandler(wxXmlResourceHandler *handler);
143
144 // Add a new handler at the begining of the handler list
145 void InsertHandler(wxXmlResourceHandler *handler);
146
147 // Removes all handlers
148 void ClearHandlers();
149
150 // Registers subclasses factory for use in XRC. This function is not meant
151 // for public use, please see the comment above wxXmlSubclassFactory
152 // definition.
153 static void AddSubclassFactory(wxXmlSubclassFactory *factory);
154
155 // Loads menu from resource. Returns NULL on failure.
156 wxMenu *LoadMenu(const wxString& name);
157
158 // Loads menubar from resource. Returns NULL on failure.
159 wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
160
161 // Loads menubar from resource. Returns NULL on failure.
162 wxMenuBar *LoadMenuBar(const wxString& name) { return LoadMenuBar(NULL, name); }
163
164 #if wxUSE_TOOLBAR
165 // Loads a toolbar.
166 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
167 #endif
168
169 // Loads a dialog. dlg points to parent window (if any).
170 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
171
172 // Loads a dialog. dlg points to parent window (if any). This form
173 // is used to finish creation of already existing instance (main reason
174 // for this is that you may want to use derived class with new event table)
175 // Example (typical usage):
176 // MyDialog dlg;
177 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
178 // dlg->ShowModal();
179 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
180
181 // Loads a panel. panel points to parent window (if any).
182 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
183
184 // Loads a panel. panel points to parent window (if any). This form
185 // is used to finish creation of already existing instance.
186 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
187
188 // Loads a frame.
189 wxFrame *LoadFrame(wxWindow* parent, const wxString& name);
190 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
191
192 // Load an object from the resource specifying both the resource name and
193 // the classname. This lets you load nonstandard container windows.
194 wxObject *LoadObject(wxWindow *parent, const wxString& name,
195 const wxString& classname);
196
197 // Load an object from the resource specifying both the resource name and
198 // the classname. This form lets you finish the creation of an existing
199 // instance.
200 bool LoadObject(wxObject *instance, wxWindow *parent, const wxString& name,
201 const wxString& classname);
202
203 // Loads a bitmap resource from a file.
204 wxBitmap LoadBitmap(const wxString& name);
205
206 // Loads an icon resource from a file.
207 wxIcon LoadIcon(const wxString& name);
208
209 // Attaches an unknown control to the given panel/window/dialog.
210 // Unknown controls are used in conjunction with <object class="unknown">.
211 bool AttachUnknownControl(const wxString& name, wxWindow *control,
212 wxWindow *parent = NULL);
213
214 // Returns a numeric ID that is equivalent to the string id used in an XML
215 // resource. To be used in event tables.
216 // Macro XRCID is provided for convenience
217 static int GetXRCID(const wxChar *str_id);
218
219 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
220 long GetVersion() const { return m_version; }
221
222 // Compares resources version to argument. Returns -1 if resources version
223 // is less than the argument, +1 if greater and 0 if they equal.
224 int CompareVersion(int major, int minor, int release, int revision) const
225 { return GetVersion() -
226 (major*256*256*256 + minor*256*256 + release*256 + revision); }
227
228 //// Singleton accessors.
229
230 // Gets the global resources object or creates one if none exists.
231 static wxXmlResource *Get();
232
233 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
234 static wxXmlResource *Set(wxXmlResource *res);
235
236 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
237 int GetFlags() const { return m_flags; }
238 // Set flags after construction.
239 void SetFlags(int flags) { m_flags = flags; }
240
241 protected:
242 // Scans the resources list for unloaded files and loads them. Also reloads
243 // files that have been modified since last loading.
244 bool UpdateResources();
245
246 // Finds a resource (calls UpdateResources) and returns a node containing it.
247 wxXmlNode *FindResource(const wxString& name, const wxString& classname, bool recursive = false);
248
249 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
250 wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive);
251
252 // Creates a resource from information in the given node
253 // (Uses only 'handlerToUse' if != NULL)
254 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent,
255 wxObject *instance = NULL,
256 wxXmlResourceHandler *handlerToUse = NULL);
257
258 private:
259 long m_version;
260
261 int m_flags;
262 wxList m_handlers;
263 wxXmlResourceDataRecords m_data;
264 #if wxUSE_FILESYSTEM
265 wxFileSystem m_curFileSystem;
266 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
267 #endif
268
269 friend class wxXmlResourceHandler;
270 friend class wxXmlResourceModule;
271
272 static wxXmlSubclassFactoriesList *ms_subclassFactories;
273
274 // singleton instance:
275 static wxXmlResource *ms_instance;
276 };
277
278
279 // This macro translates string identifier (as used in XML resource,
280 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
281 // wxWidgets event tables.
282 // Example:
283 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
284 // EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
285 // EVT_MENU(XRCID("about"), MyFrame::OnAbout)
286 // EVT_MENU(XRCID("new"), MyFrame::OnNew)
287 // EVT_MENU(XRCID("open"), MyFrame::OnOpen)
288 // END_EVENT_TABLE()
289
290 #define XRCID(str_id) \
291 wxXmlResource::GetXRCID(wxT(str_id))
292
293
294 // This macro returns pointer to particular control in dialog
295 // created using XML resources. You can use it to set/get values from
296 // controls.
297 // Example:
298 // wxDialog dlg;
299 // wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
300 // XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
301
302 #ifdef __WXDEBUG__
303 #define XRCCTRL(window, id, type) \
304 (wxDynamicCast((window).FindWindow(XRCID(id)), type))
305 #else
306 #define XRCCTRL(window, id, type) \
307 ((type*)((window).FindWindow(XRCID(id))))
308 #endif
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 neccessary 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, *m_instanceAsWindow;
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 understads 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 the integer value from the parameter.
404 long GetLong( const wxString& param, long defaultv = 0 );
405
406 // Gets colour in HTML syntax (#RRGGBB).
407 wxColour GetColour(const wxString& param);
408
409 // Gets the size (may be in dialog units).
410 wxSize GetSize(const wxString& param = wxT("size"));
411
412 // Gets the position (may be in dialog units).
413 wxPoint GetPosition(const wxString& param = wxT("pos"));
414
415 // Gets a dimension (may be in dialog units).
416 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
417
418 // Gets a bitmap.
419 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
420 const wxArtClient& defaultArtClient = wxART_OTHER,
421 wxSize size = wxDefaultSize);
422
423 // Gets an icon.
424 wxIcon GetIcon(const wxString& param = wxT("icon"),
425 const wxArtClient& defaultArtClient = wxART_OTHER,
426 wxSize size = wxDefaultSize);
427
428 // Gets a font.
429 wxFont GetFont(const wxString& param = wxT("font"));
430
431 // Sets common window options.
432 void SetupWindow(wxWindow *wnd);
433
434 // Creates children.
435 void CreateChildren(wxObject *parent, bool this_hnd_only = false);
436
437 // Helper function.
438 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
439
440 // Creates a resource from a node.
441 wxObject *CreateResFromNode(wxXmlNode *node,
442 wxObject *parent, wxObject *instance = NULL)
443 { return m_resource->CreateResFromNode(node, parent, instance); }
444
445 // helper
446 #if wxUSE_FILESYSTEM
447 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
448 #endif
449 };
450
451
452 // Programmer-friendly macros for writing XRC handlers:
453
454 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
455
456 #define XRC_MAKE_INSTANCE(variable, classname) \
457 classname *variable = NULL; \
458 if (m_instance) \
459 variable = wxStaticCast(m_instance, classname); \
460 if (!variable) \
461 variable = new classname;
462
463
464 // FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
465 WXDLLIMPEXP_XRC void wxXmlInitResourceModule();
466
467
468 // This class is used to create instances of XRC "object" nodes with "subclass"
469 // property. It is _not_ supposed to be used by XRC users, you should instead
470 // register your subclasses via wxWidgets' RTTI mechanism. This class is useful
471 // only for language bindings developer who need a way to implement subclassing
472 // in wxWidgets ports that don't support wxRTTI (e.g. wxPython).
473 class WXDLLIMPEXP_XRC wxXmlSubclassFactory
474 {
475 public:
476 // Try to create instance of given class and return it, return NULL on
477 // failure:
478 virtual wxObject *Create(const wxString& className) = 0;
479 virtual ~wxXmlSubclassFactory() {}
480 };
481
482
483 /* -------------------------------------------------------------------------
484 Backward compatibility macros. Do *NOT* use, they may disappear in future
485 versions of the XRC library!
486 ------------------------------------------------------------------------- */
487 #if WXWIN_COMPATIBILITY_2_4
488 #define ADD_STYLE XRC_ADD_STYLE
489 #define wxTheXmlResource wxXmlResource::Get()
490 #define XMLID XRCID
491 #define XMLCTRL XRCCTRL
492 #define GetXMLID GetXRCID
493 #endif
494
495 #endif // wxUSE_XRC
496
497 #endif // _WX_XMLRES_H_