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