]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/xrc/xmlres.h
making sure peer->Move refreshes properly, bringing in synch with carbon, applying...
[wxWidgets.git] / interface / wx / xrc / xmlres.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: xrc/xmlres.h
e54c96f1 3// Purpose: interface of wxXmlResource
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
18e8e19b
FM
9/**
10 Flags which can be used with wxXmlResource::wxXmlResource.
11*/
12enum wxXmlResourceFlags
13{
14 /** Translatable strings will be translated via _(). */
15 wxXRC_USE_LOCALE = 1,
16
17 /** Subclass property of object nodes will be ignored (useful for previews in XRC editors). */
18 wxXRC_NO_SUBCLASSING = 2,
19
20 /** Prevent the XRC files from being reloaded from disk in case they have been modified there
21 since being last loaded (may slightly speed up loading them). */
22 wxXRC_NO_RELOADING = 4
23};
24
25
23324ae1
FM
26/**
27 @class wxXmlResource
7c913512 28
23324ae1 29 This is the main class for interacting with the XML-based resource system.
7c913512 30
23324ae1
FM
31 The class holds XML resources from one or more .xml files, binary files or zip
32 archive files.
7c913512 33
336aecf1
FM
34 Note that this is a singleton class and you'll never allocate/deallocate it.
35 Just use the static wxXmlResource::Get() getter.
36
41e69d79 37 @see @ref overview_xrc, @ref overview_xrcformat
7c913512 38
23324ae1
FM
39 @library{wxxrc}
40 @category{xrc}
41*/
42class wxXmlResource : public wxObject
43{
44public:
23324ae1
FM
45 /**
46 Constructor.
f41d6c8c 47
18e8e19b
FM
48 @param filemask
49 The XRC file, archive file, or wildcard specification that will be
50 used to load all resource files inside a zip archive.
7c913512 51 @param flags
18e8e19b 52 One or more value of the ::wxXmlResourceFlags enumeration.
7c913512 53 @param domain
18e8e19b
FM
54 The name of the gettext catalog to search for translatable strings.
55 By default all loaded catalogs will be searched.
56 This provides a way to allow the strings to only come from a specific catalog.
23324ae1
FM
57 */
58 wxXmlResource(const wxString& filemask,
59 int flags = wxXRC_USE_LOCALE,
cfbe5614 60 const wxString& domain = wxEmptyString);
76e9224e
FM
61
62 /**
63 Constructor.
64
65 @param flags
66 One or more value of the ::wxXmlResourceFlags enumeration.
67 @param domain
68 The name of the gettext catalog to search for translatable strings.
69 By default all loaded catalogs will be searched.
70 This provides a way to allow the strings to only come from a specific catalog.
71 */
7c913512 72 wxXmlResource(int flags = wxXRC_USE_LOCALE,
cfbe5614 73 const wxString& domain = wxEmptyString);
23324ae1
FM
74
75 /**
76 Destructor.
77 */
adaaa686 78 virtual ~wxXmlResource();
23324ae1
FM
79
80 /**
18e8e19b
FM
81 Initializes only a specific handler (or custom handler).
82 Convention says that the handler name is equal to the control's name plus
83 'XmlHandler', for example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler.
84
85 The XML resource compiler (wxxrc) can create include file that contains
86 initialization code for all controls used within the resource.
87 Note that this handler must be allocated on the heap, since it will be
88 deleted by ClearHandlers() later.
23324ae1
FM
89 */
90 void AddHandler(wxXmlResourceHandler* handler);
91
92 /**
93 Attaches an unknown control to the given panel/window/dialog.
18e8e19b 94 Unknown controls are used in conjunction with \<object class="unknown"\>.
23324ae1
FM
95 */
96 bool AttachUnknownControl(const wxString& name,
97 wxWindow* control,
4cc4bfaf 98 wxWindow* parent = NULL);
23324ae1
FM
99
100 /**
18e8e19b
FM
101 Removes all handlers and deletes them (this means that any handlers
102 added using AddHandler() must be allocated on the heap).
23324ae1
FM
103 */
104 void ClearHandlers();
105
106 /**
18e8e19b
FM
107 Compares the XRC version to the argument.
108
109 Returns -1 if the XRC version is less than the argument,
110 +1 if greater, and 0 if they are equal.
23324ae1 111 */
18e8e19b 112 int CompareVersion(int major, int minor, int release, int revision) const;
23324ae1 113
cc378c2d
VZ
114 /**
115 Returns a string ID corresponding to the given numeric ID.
116
117 The string returned is such that calling GetXRCID() with it as
118 parameter yields @a numId. If there is no string identifier
119 corresponding to the given numeric one, an empty string is returned.
120
121 Notice that, unlike GetXRCID(), this function is slow as it checks all
122 of the identifiers used in XRC.
123
124 @since 2.9.0
125 */
ebd75440 126 static wxString FindXRCIDById(int numId);
cc378c2d 127
23324ae1
FM
128 /**
129 Gets the global resources object or creates one if none exists.
130 */
adaaa686 131 static wxXmlResource* Get();
23324ae1
FM
132
133 /**
134 Returns the domain (message catalog) that will be used to load
135 translatable strings in the XRC.
136 */
95b4a59e 137 const wxString& GetDomain() const;
23324ae1
FM
138
139 /**
18e8e19b
FM
140 Returns flags, which may be a bitlist of ::wxXmlResourceFlags
141 enumeration values.
23324ae1 142 */
adaaa686 143 int GetFlags() const;
23324ae1 144
23239d94
VZ
145 /**
146 Returns the wxXmlNode containing the definition of the object with the
147 given name or @NULL.
148
149 This function recursively searches all the loaded XRC files for an
150 object with the specified @a name. If the object is found, the
151 wxXmlNode corresponding to it is returned, so this function can be used
152 to access additional information defined in the XRC file and not used
153 by wxXmlResource itself, e.g. contents of application-specific XML
154 tags.
155
156 @param name
157 The name of the resource which must be unique for this function to
158 work correctly, if there is more than one resource with the given
159 name the choice of the one returned by this function is undefined.
160 @return
161 The node corresponding to the resource with the given name or @NULL.
162 */
163 const wxXmlNode *GetResourceNode(const wxString& name) const;
164
23324ae1 165 /**
18e8e19b 166 Returns version information (a.b.c.d = d + 256*c + 2562*b + 2563*a).
23324ae1 167 */
328f5751 168 long GetVersion() const;
23324ae1
FM
169
170 /**
18e8e19b
FM
171 Returns a numeric ID that is equivalent to the string ID used in an XML resource.
172
173 If an unknown @a str_id is requested (i.e. other than wxID_XXX or integer),
174 a new record is created which associates the given string with a number.
175
176 If @a value_if_not_found is @c wxID_NONE, the number is obtained via
e54c96f1 177 wxNewId(). Otherwise @a value_if_not_found is used.
23324ae1
FM
178 Macro @c XRCID(name) is provided for convenient use in event tables.
179 */
18e8e19b 180 static int GetXRCID(const wxString& str_id, int value_if_not_found = wxID_NONE);
23324ae1
FM
181
182 /**
18e8e19b
FM
183 Initializes handlers for all supported controls/windows.
184
185 This will make the executable quite big because it forces linking against
23324ae1
FM
186 most of the wxWidgets library.
187 */
188 void InitAllHandlers();
189
190 /**
191 Loads resources from XML files that match given filemask.
336aecf1
FM
192
193 Example:
194 @code
195 if (!wxXmlResource::Get()->Load("rc/*.xrc"))
196 wxLogError("Couldn't load resources!");
197 @endcode
198
04ae32cd
VS
199 @note
200 If wxUSE_FILESYS is enabled, this method understands wxFileSystem URLs
201 (see wxFileSystem::FindFirst()).
202
203 @note
204 If you are sure that the argument is name of single XRC file (rather
205 than an URL or a wildcard), use LoadFile() instead.
206
2bb9a404 207 @see LoadFile(), LoadAllFiles()
23324ae1
FM
208 */
209 bool Load(const wxString& filemask);
210
04ae32cd
VS
211 /**
212 Simpler form of Load() for loading a single XRC file.
213
214 @since 2.9.0
215
2bb9a404 216 @see Load(), LoadAllFiles()
04ae32cd
VS
217 */
218 bool LoadFile(const wxFileName& file);
219
2bb9a404
VS
220 /**
221 Loads all .xrc files from directory @a dirname.
222
223 Tries to load as many files as possible; if there's an error while
224 loading one file, it still attempts to load other files.
225
226 @since 2.9.0
227
228 @see LoadFile(), Load()
229 */
230 bool LoadAllFiles(const wxString& dirname);
231
23324ae1
FM
232 /**
233 Loads a bitmap resource from a file.
234 */
235 wxBitmap LoadBitmap(const wxString& name);
236
23324ae1 237 /**
18e8e19b
FM
238 Loads a dialog. @a parent points to parent window (if any).
239 */
240 wxDialog* LoadDialog(wxWindow* parent, const wxString& name);
241
242 /**
243 Loads a dialog. @a parent points to parent window (if any).
244
23324ae1 245 This form is used to finish creation of an already existing instance (the main
18e8e19b 246 reason for this is that you may want to use derived class with a new event table).
23324ae1 247 Example:
18e8e19b
FM
248
249 @code
250 MyDialog dlg;
336aecf1 251 wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
18e8e19b
FM
252 dlg.ShowModal();
253 @endcode
23324ae1 254 */
18e8e19b 255 bool LoadDialog(wxDialog* dlg, wxWindow* parent, const wxString& name);
23324ae1
FM
256
257 /**
258 Loads a frame.
259 */
260 bool LoadFrame(wxFrame* frame, wxWindow* parent,
261 const wxString& name);
262
263 /**
264 Loads an icon resource from a file.
265 */
266 wxIcon LoadIcon(const wxString& name);
267
268 /**
269 Loads menu from resource. Returns @NULL on failure.
270 */
271 wxMenu* LoadMenu(const wxString& name);
272
273 //@{
274 /**
275 Loads a menubar from resource. Returns @NULL on failure.
276 */
277 wxMenuBar* LoadMenuBar(wxWindow* parent, const wxString& name);
7c913512 278 wxMenuBar* LoadMenuBar(const wxString& name);
23324ae1
FM
279 //@}
280
281 //@{
282 /**
283 Load an object from the resource specifying both the resource name and the
284 class name.
18e8e19b
FM
285
286 The first overload lets you load nonstandard container windows and returns
287 @NULL on failure. The second one lets you finish the creation of an existing
23324ae1
FM
288 instance and returns @false on failure.
289 */
290 wxObject* LoadObject(wxWindow* parent, const wxString& name,
291 const wxString& classname);
7c913512
FM
292 bool LoadObject(wxObject* instance, wxWindow* parent,
293 const wxString& name,
294 const wxString& classname);
23324ae1
FM
295 //@}
296
23324ae1 297 /**
e64cadcb 298 Loads a panel. @a parent points to the parent window.
23324ae1
FM
299 */
300 wxPanel* LoadPanel(wxWindow* parent, const wxString& name);
18e8e19b
FM
301
302 /**
e64cadcb 303 Loads a panel. @a parent points to the parent window.
18e8e19b
FM
304 This form is used to finish creation of an already existing instance.
305 */
306 bool LoadPanel(wxPanel* panel, wxWindow* parent, const wxString& name);
23324ae1
FM
307
308 /**
309 Loads a toolbar.
310 */
311 wxToolBar* LoadToolBar(wxWindow* parent, const wxString& name);
312
313 /**
18e8e19b
FM
314 Sets the global resources object and returns a pointer to the previous one
315 (may be @NULL).
23324ae1 316 */
adaaa686 317 static wxXmlResource* Set(wxXmlResource* res);
23324ae1
FM
318
319 /**
320 Sets the domain (message catalog) that will be used to load
321 translatable strings in the XRC.
322 */
95b4a59e 323 void SetDomain(const wxString& domain);
23324ae1
FM
324
325 /**
18e8e19b 326 Sets flags (bitlist of ::wxXmlResourceFlags enumeration values).
23324ae1
FM
327 */
328 void SetFlags(int flags);
329
330 /**
18e8e19b
FM
331 This function unloads a resource previously loaded by Load().
332
23324ae1 333 Returns @true if the resource was successfully unloaded and @false if it
18e8e19b 334 hasn't been found in the list of loaded resources.
23324ae1
FM
335 */
336 bool Unload(const wxString& filename);
337};
338
339
e54c96f1 340
23324ae1
FM
341/**
342 @class wxXmlResourceHandler
7c913512 343
23324ae1
FM
344 wxXmlResourceHandler is an abstract base class for resource handlers
345 capable of creating a control from an XML node.
7c913512 346
f41d6c8c 347 See @ref overview_xrc for details.
7c913512 348
23324ae1
FM
349 @library{wxxrc}
350 @category{xrc}
351*/
352class wxXmlResourceHandler : public wxObject
353{
354public:
355 /**
356 Default constructor.
357 */
358 wxXmlResourceHandler();
359
360 /**
361 Destructor.
362 */
adaaa686 363 virtual ~wxXmlResourceHandler();
23324ae1
FM
364
365 /**
95b4a59e
FM
366 Creates an object (menu, dialog, control, ...) from an XML node.
367 Should check for validity. @a parent is a higher-level object
368 (usually window, dialog or panel) that is often necessary to
369 create the resource.
370
371 If @b instance is non-@NULL it should not create a new instance via
372 'new' but should rather use this one, and call its Create method.
23324ae1 373 */
95b4a59e
FM
374 wxObject* CreateResource(wxXmlNode* node, wxObject* parent,
375 wxObject* instance);
23324ae1
FM
376
377 /**
95b4a59e 378 Called from CreateResource after variables were filled.
23324ae1 379 */
95b4a59e 380 virtual wxObject* DoCreateResource() = 0;
23324ae1
FM
381
382 /**
383 Returns @true if it understands this node and can create
384 a resource from it, @false otherwise.
18e8e19b
FM
385
386 @note
387 You must not call any wxXmlResourceHandler methods except IsOfClass()
388 from this method! The instance is not yet initialized with node data
389 at the time CanHandle() is called and it is only safe to operate on
390 node directly or to call IsOfClass().
23324ae1 391 */
95b4a59e
FM
392 virtual bool CanHandle(wxXmlNode* node) = 0;
393
394 /**
395 Sets the parent resource.
396 */
397 void SetParentResource(wxXmlResource* res);
398
399
400protected:
401
402 /**
403 Add a style flag (e.g. @c wxMB_DOCKABLE) to the list of flags
404 understood by this handler.
405 */
406 void AddStyle(const wxString& name, int value);
407
408 /**
409 Add styles common to all wxWindow-derived classes.
410 */
411 void AddWindowStyles();
23324ae1
FM
412
413 /**
414 Creates children.
415 */
4cc4bfaf 416 void CreateChildren(wxObject* parent, bool this_hnd_only = false);
23324ae1
FM
417
418 /**
419 Helper function.
420 */
421 void CreateChildrenPrivately(wxObject* parent,
4cc4bfaf 422 wxXmlNode* rootnode = NULL);
23324ae1
FM
423
424 /**
425 Creates a resource from a node.
426 */
427 wxObject* CreateResFromNode(wxXmlNode* node, wxObject* parent,
4cc4bfaf 428 wxObject* instance = NULL);
23324ae1 429
23324ae1 430 /**
18e8e19b 431 Creates an animation (see wxAnimation) from the filename specified in @a param.
23324ae1 432 */
f8ebb70d 433 wxAnimation GetAnimation(const wxString& param = "animation");
23324ae1
FM
434
435 /**
23324ae1
FM
436 Gets a bitmap.
437 */
95b4a59e
FM
438 wxBitmap GetBitmap(const wxString& param = "bitmap",
439 const wxArtClient& defaultArtClient = wxART_OTHER,
440 wxSize size = wxDefaultSize);
23324ae1
FM
441
442 /**
f41d6c8c 443 Gets a bool flag (1, t, yes, on, true are @true, everything else is @false).
23324ae1 444 */
4cc4bfaf 445 bool GetBool(const wxString& param, bool defaultv = false);
23324ae1
FM
446
447 /**
3f5506cf 448 Gets colour in HTML syntax (\#RRGGBB).
23324ae1
FM
449 */
450 wxColour GetColour(const wxString& param,
18e8e19b 451 const wxColour& defaultColour = wxNullColour);
23324ae1
FM
452
453 /**
454 Returns the current file system.
455 */
95b4a59e 456 wxFileSystem& GetCurFileSystem();
23324ae1
FM
457
458 /**
459 Gets a dimension (may be in dialog units).
460 */
95b4a59e
FM
461 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0,
462 wxWindow* windowToUse = 0);
23324ae1
FM
463
464 /**
23324ae1
FM
465 Gets a font.
466 */
95b4a59e 467 wxFont GetFont(const wxString& param = "font");
23324ae1
FM
468
469 /**
470 Returns the XRCID.
471 */
4cc4bfaf 472 int GetID();
23324ae1
FM
473
474 /**
23324ae1
FM
475 Returns an icon.
476 */
95b4a59e
FM
477 wxIcon GetIcon(const wxString& param = "icon",
478 const wxArtClient& defaultArtClient = wxART_OTHER,
479 wxSize size = wxDefaultSize);
23324ae1
FM
480
481 /**
482 Gets the integer value from the parameter.
483 */
484 long GetLong(const wxString& param, long defaultv = 0);
485
486 /**
487 Returns the resource name.
488 */
489 wxString GetName();
490
491 /**
492 Gets node content from wxXML_ENTITY_NODE.
493 */
494 wxString GetNodeContent(wxXmlNode* node);
495
496 /**
497 Finds the node or returns @NULL.
498 */
499 wxXmlNode* GetParamNode(const wxString& param);
500
501 /**
502 Finds the parameter value or returns the empty string.
503 */
504 wxString GetParamValue(const wxString& param);
505
506 /**
23324ae1
FM
507 Gets the position (may be in dialog units).
508 */
f8ebb70d 509 wxPoint GetPosition(const wxString& param = "pos");
23324ae1
FM
510
511 /**
23324ae1
FM
512 Gets the size (may be in dialog units).
513 */
95b4a59e 514 wxSize GetSize(const wxString& param = "size", wxWindow* windowToUse = 0);
23324ae1
FM
515
516 /**
23324ae1 517 Gets style flags from text in form "flag | flag2| flag3 |..."
18e8e19b 518 Only understands flags added with AddStyle().
23324ae1 519 */
f8ebb70d 520 int GetStyle(const wxString& param = "style", int defaults = 0);
23324ae1
FM
521
522 /**
523 Gets text from param and does some conversions:
f41d6c8c
FM
524 - replaces \\n, \\r, \\t by respective characters (according to C syntax)
525 - replaces @c $ by @c and @c $$ by @c $ (needed for @c _File to @c File
526 translation because of XML syntax)
527 - calls wxGetTranslations (unless disabled in wxXmlResource)
23324ae1 528 */
95b4a59e 529 wxString GetText(const wxString& param, bool translate = true);
23324ae1
FM
530
531 /**
532 Check to see if a parameter exists.
533 */
534 bool HasParam(const wxString& param);
535
536 /**
18e8e19b
FM
537 Convenience function.
538 Returns @true if the node has a property class equal to classname,
23324ae1
FM
539 e.g. object class="wxDialog".
540 */
541 bool IsOfClass(wxXmlNode* node, const wxString& classname);
542
23324ae1
FM
543 /**
544 Sets common window options.
545 */
546 void SetupWindow(wxWindow* wnd);
547};
e54c96f1 548