]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/xrc/xmlres.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxXmlResource
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
9 Flags which can be used with wxXmlResource::wxXmlResource.
11 enum wxXmlResourceFlags
13 /** Translatable strings will be translated via _(). */
16 /** Subclass property of object nodes will be ignored (useful for previews in XRC editors). */
17 wxXRC_NO_SUBCLASSING
= 2,
19 /** Prevent the XRC files from being reloaded from disk in case they have been modified there
20 since being last loaded (may slightly speed up loading them). */
21 wxXRC_NO_RELOADING
= 4
28 This is the main class for interacting with the XML-based resource system.
30 The class holds XML resources from one or more .xml files, binary files or zip
33 Note that this is a singleton class and you'll never allocate/deallocate it.
34 Just use the static wxXmlResource::Get() getter.
36 @see @ref overview_xrc, @ref overview_xrcformat
41 class wxXmlResource
: public wxObject
48 The XRC file, archive file, or wildcard specification that will be
49 used to load all resource files inside a zip archive.
51 One or more value of the ::wxXmlResourceFlags enumeration.
53 The name of the gettext catalog to search for translatable strings.
54 By default all loaded catalogs will be searched.
55 This provides a way to allow the strings to only come from a specific catalog.
57 wxXmlResource(const wxString
& filemask
,
58 int flags
= wxXRC_USE_LOCALE
,
59 const wxString
& domain
= wxEmptyString
);
65 One or more value of the ::wxXmlResourceFlags enumeration.
67 The name of the gettext catalog to search for translatable strings.
68 By default all loaded catalogs will be searched.
69 This provides a way to allow the strings to only come from a specific catalog.
71 wxXmlResource(int flags
= wxXRC_USE_LOCALE
,
72 const wxString
& domain
= wxEmptyString
);
77 virtual ~wxXmlResource();
80 Initializes only a specific handler (or custom handler).
81 Convention says that the handler name is equal to the control's name plus
82 'XmlHandler', for example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler.
84 The XML resource compiler (wxxrc) can create include file that contains
85 initialization code for all controls used within the resource.
86 Note that this handler must be allocated on the heap, since it will be
87 deleted by ClearHandlers() later.
89 void AddHandler(wxXmlResourceHandler
* handler
);
92 Add a new handler at the begining of the handler list.
94 void InsertHandler(wxXmlResourceHandler
*handler
);
98 Attaches an unknown control to the given panel/window/dialog.
99 Unknown controls are used in conjunction with \<object class="unknown"\>.
101 bool AttachUnknownControl(const wxString
& name
,
103 wxWindow
* parent
= NULL
);
106 Removes all handlers and deletes them (this means that any handlers
107 added using AddHandler() must be allocated on the heap).
109 void ClearHandlers();
112 Registers subclasses factory for use in XRC. This is useful only for
113 language bindings developers who need a way to implement subclassing in
114 wxWidgets ports that don't support wxRTTI (e.g. wxPython).
116 static void AddSubclassFactory(wxXmlSubclassFactory
*factory
);
120 Compares the XRC version to the argument.
122 Returns -1 if the XRC version is less than the argument,
123 +1 if greater, and 0 if they are equal.
125 int CompareVersion(int major
, int minor
, int release
, int revision
) const;
128 Returns a string ID corresponding to the given numeric ID.
130 The string returned is such that calling GetXRCID() with it as
131 parameter yields @a numId. If there is no string identifier
132 corresponding to the given numeric one, an empty string is returned.
134 Notice that, unlike GetXRCID(), this function is slow as it checks all
135 of the identifiers used in XRC.
139 static wxString
FindXRCIDById(int numId
);
142 Gets the global resources object or creates one if none exists.
144 static wxXmlResource
* Get();
147 Returns the domain (message catalog) that will be used to load
148 translatable strings in the XRC.
150 const wxString
& GetDomain() const;
153 Returns flags, which may be a bitlist of ::wxXmlResourceFlags
156 int GetFlags() const;
159 Returns the wxXmlNode containing the definition of the object with the
162 This function recursively searches all the loaded XRC files for an
163 object with the specified @a name. If the object is found, the
164 wxXmlNode corresponding to it is returned, so this function can be used
165 to access additional information defined in the XRC file and not used
166 by wxXmlResource itself, e.g. contents of application-specific XML
170 The name of the resource which must be unique for this function to
171 work correctly, if there is more than one resource with the given
172 name the choice of the one returned by this function is undefined.
174 The node corresponding to the resource with the given name or @NULL.
176 const wxXmlNode
*GetResourceNode(const wxString
& name
) const;
179 Returns version information (a.b.c.d = d + 256*c + 2562*b + 2563*a).
181 long GetVersion() const;
184 Returns a numeric ID that is equivalent to the string ID used in an XML resource.
186 If an unknown @a str_id is requested (i.e. other than wxID_XXX or integer),
187 a new record is created which associates the given string with a number.
189 If @a value_if_not_found is @c wxID_NONE, the number is obtained via
190 wxNewId(). Otherwise @a value_if_not_found is used.
192 Macro @c XRCID(name) is provided for convenient use in event tables.
194 @note IDs returned by XRCID() cannot be used with the <tt>EVT_*_RANGE</tt>
195 macros, because the order in which they are assigned to symbolic @a name
196 values is not guaranteed.
198 static int GetXRCID(const wxString
& str_id
, int value_if_not_found
= wxID_NONE
);
201 Initializes handlers for all supported controls/windows.
203 This will make the executable quite big because it forces linking against
204 most of the wxWidgets library.
206 void InitAllHandlers();
209 Loads resources from XML files that match given filemask.
213 if (!wxXmlResource::Get()->Load("rc/*.xrc"))
214 wxLogError("Couldn't load resources!");
218 If wxUSE_FILESYS is enabled, this method understands wxFileSystem URLs
219 (see wxFileSystem::FindFirst()).
222 If you are sure that the argument is name of single XRC file (rather
223 than an URL or a wildcard), use LoadFile() instead.
225 @see LoadFile(), LoadAllFiles()
227 bool Load(const wxString
& filemask
);
230 Simpler form of Load() for loading a single XRC file.
234 @see Load(), LoadAllFiles()
236 bool LoadFile(const wxFileName
& file
);
239 Loads all .xrc files from directory @a dirname.
241 Tries to load as many files as possible; if there's an error while
242 loading one file, it still attempts to load other files.
246 @see LoadFile(), Load()
248 bool LoadAllFiles(const wxString
& dirname
);
251 Loads a bitmap resource from a file.
253 wxBitmap
LoadBitmap(const wxString
& name
);
256 Loads a dialog. @a parent points to parent window (if any).
258 wxDialog
* LoadDialog(wxWindow
* parent
, const wxString
& name
);
261 Loads a dialog. @a parent points to parent window (if any).
263 This form is used to finish creation of an already existing instance (the main
264 reason for this is that you may want to use derived class with a new event table).
269 wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
273 bool LoadDialog(wxDialog
* dlg
, wxWindow
* parent
, const wxString
& name
);
276 Loads a frame from the resource. @a parent points to parent window (if any).
278 wxFrame
*LoadFrame(wxWindow
* parent
, const wxString
& name
);
281 Loads the contents of a frame onto an existing wxFrame.
283 This form is used to finish creation of an already existing instance
284 (the main reason for this is that you may want to use derived class
285 with a new event table).
287 bool LoadFrame(wxFrame
* frame
, wxWindow
* parent
,
288 const wxString
& name
);
291 Loads an icon resource from a file.
293 wxIcon
LoadIcon(const wxString
& name
);
296 Loads menu from resource. Returns @NULL on failure.
298 wxMenu
* LoadMenu(const wxString
& name
);
302 Loads a menubar from resource. Returns @NULL on failure.
304 wxMenuBar
* LoadMenuBar(wxWindow
* parent
, const wxString
& name
);
305 wxMenuBar
* LoadMenuBar(const wxString
& name
);
310 Load an object from the resource specifying both the resource name and the
313 The first overload lets you load nonstandard container windows and returns
314 @NULL on failure. The second one lets you finish the creation of an existing
315 instance and returns @false on failure.
317 In either case, only the resources defined at the top level of XRC
318 files can be loaded by this function, use LoadObjectRecursively() if
319 you need to load an object defined deeper in the hierarchy.
321 wxObject
* LoadObject(wxWindow
* parent
, const wxString
& name
,
322 const wxString
& classname
);
323 bool LoadObject(wxObject
* instance
, wxWindow
* parent
,
324 const wxString
& name
,
325 const wxString
& classname
);
330 Load an object from anywhere in the resource tree.
332 These methods are similar to LoadObject() but may be used to load an
333 object from anywhere in the resource tree and not only the top level.
334 Note that you will very rarely need to do this as in normal use the
335 entire container window (defined at the top level) is loaded and not
336 its individual children but this method can be useful in some
337 particular situations.
341 wxObject
* LoadObjectRecursively(wxWindow
* parent
,
342 const wxString
& name
,
343 const wxString
& classname
);
344 bool LoadObjectRecursively(wxObject
* instance
, wxWindow
* parent
,
345 const wxString
& name
,
346 const wxString
& classname
);
350 Loads a panel. @a parent points to the parent window.
352 wxPanel
* LoadPanel(wxWindow
* parent
, const wxString
& name
);
355 Loads a panel. @a parent points to the parent window.
356 This form is used to finish creation of an already existing instance.
358 bool LoadPanel(wxPanel
* panel
, wxWindow
* parent
, const wxString
& name
);
363 wxToolBar
* LoadToolBar(wxWindow
* parent
, const wxString
& name
);
366 Sets the global resources object and returns a pointer to the previous one
369 static wxXmlResource
* Set(wxXmlResource
* res
);
372 Sets the domain (message catalog) that will be used to load
373 translatable strings in the XRC.
375 void SetDomain(const wxString
& domain
);
378 Sets flags (bitlist of ::wxXmlResourceFlags enumeration values).
380 void SetFlags(int flags
);
383 This function unloads a resource previously loaded by Load().
385 Returns @true if the resource was successfully unloaded and @false if it
386 hasn't been found in the list of loaded resources.
388 bool Unload(const wxString
& filename
);
392 Reports error in XRC resources to the user.
394 Any errors in XRC input files should be reported using this method
395 (or its wxXmlResourceHandler::ReportError() equivalent). Unlike
396 wxLogError(), this method presents the error to the user in a more
397 usable form. In particular, the output is compiler-like and contains
398 information about the exact location of the error.
400 @param context XML node the error occurred in or relates to. This can
401 be @NULL, but should be the most specific node possible,
402 as its line number is what is reported to the user.
403 @param message Text of the error message. This string should always
404 be in English (i.e. not wrapped in _()). It shouldn't
405 be a sentence -- it should start with lower-case letter
406 and shouldn't have a trailing period or exclamation
411 @see wxXmlResourceHandler::ReportError(), DoReportError()
413 void ReportError(const wxXmlNode
*context
, const wxString
& message
);
416 Implementation of XRC resources errors reporting.
418 This method is called by ReportError() and shouldn't be called
419 directly; use ReportError() or wxXmlResourceHandler::ReportError()
422 Default implementation uses wxLogError().
424 @param xrcFile File the error occurred in or empty string if it
425 couldn't be determined.
426 @param position XML node where the error occurred or @NULL if it
427 couldn't be determined.
428 @param message Text of the error message. See ReportError()
429 documentation for details of the string's format.
432 You may override this method in a derived class to customize errors
433 reporting. If you do so, you'll need to either use the derived class
434 in all your code or call wxXmlResource::Set() to change the global
435 wxXmlResource instance to your class.
441 virtual void DoReportError(const wxString
& xrcFile
, const wxXmlNode
*position
,
442 const wxString
& message
);
448 @class wxXmlResourceHandler
450 wxXmlResourceHandler is an abstract base class for resource handlers
451 capable of creating a control from an XML node.
453 See @ref overview_xrc for details.
458 class wxXmlResourceHandler
: public wxObject
464 wxXmlResourceHandler();
469 virtual ~wxXmlResourceHandler();
472 Creates an object (menu, dialog, control, ...) from an XML node.
473 Should check for validity. @a parent is a higher-level object
474 (usually window, dialog or panel) that is often necessary to
477 If @b instance is non-@NULL it should not create a new instance via
478 'new' but should rather use this one, and call its Create method.
480 wxObject
* CreateResource(wxXmlNode
* node
, wxObject
* parent
,
484 Called from CreateResource after variables were filled.
486 virtual wxObject
* DoCreateResource() = 0;
489 Returns @true if it understands this node and can create
490 a resource from it, @false otherwise.
493 You must not call any wxXmlResourceHandler methods except IsOfClass()
494 from this method! The instance is not yet initialized with node data
495 at the time CanHandle() is called and it is only safe to operate on
496 node directly or to call IsOfClass().
498 virtual bool CanHandle(wxXmlNode
* node
) = 0;
501 Sets the parent resource.
503 void SetParentResource(wxXmlResource
* res
);
509 Add a style flag (e.g. @c wxMB_DOCKABLE) to the list of flags
510 understood by this handler.
512 void AddStyle(const wxString
& name
, int value
);
515 Add styles common to all wxWindow-derived classes.
517 void AddWindowStyles();
522 void CreateChildren(wxObject
* parent
, bool this_hnd_only
= false);
527 void CreateChildrenPrivately(wxObject
* parent
,
528 wxXmlNode
* rootnode
= NULL
);
531 Creates a resource from a node.
533 wxObject
* CreateResFromNode(wxXmlNode
* node
, wxObject
* parent
,
534 wxObject
* instance
= NULL
);
537 Creates an animation (see wxAnimation) from the filename specified in @a param.
539 wxAnimation
* GetAnimation(const wxString
& param
= "animation");
544 wxBitmap
GetBitmap(const wxString
& param
= "bitmap",
545 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
546 wxSize size
= wxDefaultSize
);
548 Gets a bitmap from an XmlNode.
552 wxBitmap
GetBitmap(const wxXmlNode
* node
,
553 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
554 wxSize size
= wxDefaultSize
);
557 Gets a bool flag (1, t, yes, on, true are @true, everything else is @false).
559 bool GetBool(const wxString
& param
, bool defaultv
= false);
562 Gets colour in HTML syntax (\#RRGGBB).
564 wxColour
GetColour(const wxString
& param
,
565 const wxColour
& defaultColour
= wxNullColour
);
568 Returns the current file system.
570 wxFileSystem
& GetCurFileSystem();
573 Gets a dimension (may be in dialog units).
575 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
576 wxWindow
* windowToUse
= 0);
581 If the given @a param is not present or has empty value, @a dir is
582 returned by default. Otherwise the value of the parameter is parsed and
583 a warning is generated if it's not one of @c wxLEFT, @c wxTOP, @c
584 wxRIGHT or @c wxBOTTOM.
588 wxDirection
GetDirection(const wxString
& param
, wxDirection dir
= wxLEFT
);
593 wxFont
GetFont(const wxString
& param
= "font");
603 wxIcon
GetIcon(const wxString
& param
= "icon",
604 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
605 wxSize size
= wxDefaultSize
);
608 Gets an icon from an XmlNode.
612 wxIcon
GetIcon(const wxXmlNode
* node
,
613 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
614 wxSize size
= wxDefaultSize
);
617 Returns an icon bundle.
620 Bundles can be loaded either with stock IDs or from files that contain
621 more than one image (e.g. Windows icon files). If a file contains only
622 single image, a bundle with only one icon will be created.
626 wxIconBundle
GetIconBundle(const wxString
& param
,
627 const wxArtClient
& defaultArtClient
= wxART_OTHER
);
630 Creates an image list from the @a param markup data.
633 The new instance of wxImageList or @NULL if no data is found.
637 wxImageList
*GetImageList(const wxString
& param
= "imagelist");
640 Gets the integer value from the parameter.
642 long GetLong(const wxString
& param
, long defaultv
= 0);
645 Gets a float value from the parameter.
647 float GetFloat(const wxString
& param
, float defaultv
= 0);
650 Returns the resource name.
655 Gets node content from wxXML_ENTITY_NODE.
657 wxString
GetNodeContent(wxXmlNode
* node
);
660 Finds the node or returns @NULL.
662 wxXmlNode
* GetParamNode(const wxString
& param
);
665 Finds the parameter value or returns the empty string.
667 wxString
GetParamValue(const wxString
& param
);
670 Returns the node parameter value.
674 wxString
GetParamValue(const wxXmlNode
* node
);
677 Gets the position (may be in dialog units).
679 wxPoint
GetPosition(const wxString
& param
= "pos");
682 Gets the size (may be in dialog units).
684 wxSize
GetSize(const wxString
& param
= "size", wxWindow
* windowToUse
= 0);
687 Gets style flags from text in form "flag | flag2| flag3 |..."
688 Only understands flags added with AddStyle().
690 int GetStyle(const wxString
& param
= "style", int defaults
= 0);
693 Gets text from param and does some conversions:
694 - replaces \\n, \\r, \\t by respective characters (according to C syntax)
695 - replaces @c $ by @c and @c $$ by @c $ (needed for @c _File to @c File
696 translation because of XML syntax)
697 - calls wxGetTranslations (unless disabled in wxXmlResource)
699 wxString
GetText(const wxString
& param
, bool translate
= true);
702 Check to see if a parameter exists.
704 bool HasParam(const wxString
& param
);
707 Convenience function.
708 Returns @true if the node has a property class equal to classname,
709 e.g. object class="wxDialog".
711 bool IsOfClass(wxXmlNode
* node
, const wxString
& classname
);
714 Sets common window options.
716 void SetupWindow(wxWindow
* wnd
);
719 Reports error in XRC resources to the user.
721 See wxXmlResource::ReportError() for more information.
725 void ReportError(wxXmlNode
*context
, const wxString
& message
);
728 Like ReportError(wxXmlNode*, const wxString&), but uses the node
729 of currently processed object (m_node) as the context.
733 void ReportError(const wxString
& message
);
736 Like ReportError(wxXmlNode*, const wxString&), but uses the node
737 of parameter @a param of the currently processed object as the context.
738 This is convenience function for reporting errors in particular
743 void ReportParamError(const wxString
& param
, const wxString
& message
);
747 After CreateResource has been called this will return the current
748 wxXmlResource object.
752 wxXmlResource
* GetResource() const;
755 After CreateResource has been called this will return the XML node
760 wxXmlNode
* GetNode() const;
763 After CreateResource has been called this will return the class name of
764 the XML resource node being processed.
768 wxString
GetClass() const;
771 After CreateResource has been called this will return the current
772 item's parent, if any.
776 wxObject
* GetParent() const;
779 After CreateResource has been called this will return the instance that
780 the XML resource content should be created upon, if it has already been
781 created. If @NULL then the handler should create the object itself.
785 wxObject
* GetInstance() const;
788 After CreateResource has been called this will return the item's parent
793 wxWindow
* GetParentAsWindow() const;