| Version: 3.3.1

Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
wxXmlResource Class Reference
Class List by Category » XML Based Resource System (XRC)

`#include <wx/xrc/xmlres.h>`

Inheritance diagram for wxXmlResource:
[legend]

## Detailed Description

This is the main class for interacting with the XML-based resource system.

The class holds XML resources from one or more .xml files, binary files or zip archive files.

Note that this is a singleton class and you'll never allocate/deallocate it. Just use the static wxXmlResource::Get() getter.

See also
  XML Based Resource System (XRC), XRC File Format

##

Library: wxXRC
Category: XML Based Resource System (XRC)

## Public Member Functions
| wxXmlResource (const wxString &filemask, int flags=wxXRC_USE_LOCALE, const wxString &domain=wxEmptyString)
| Constructor. More...
| wxXmlResource (int flags=wxXRC_USE_LOCALE, const wxString &domain=wxEmptyString)
| Constructor. More...
virtual | ~wxXmlResource ()
| Destructor. More...
void | AddHandler (wxXmlResourceHandler *handler)
| Initializes only a specific handler (or custom handler). More...
void | InsertHandler (wxXmlResourceHandler *handler)
| Add a new handler at the beginning of the handler list. More...
bool | AttachUnknownControl (const wxString &name, wxWindow *control, wxWindow *parent=nullptr)
| Attaches an unknown control to the given panel/window/dialog. More...
void | ClearHandlers ()
| Removes all handlers and deletes them (this means that any handlers added using AddHandler() must be allocated on the heap). More...
int | CompareVersion (int major, int minor, int release, int revision) const
| Compares the XRC version to the argument. More...
void | EnableFeature (const wxString &feature)
| Add a feature considered to be enabled. More...
const wxString & | GetDomain () const
| Returns the domain (message catalog) that will be used to load translatable strings in the XRC. More...
int | GetFlags () const
| Returns flags, which may be a bitlist of wxXmlResourceFlags enumeration values. More...
const wxXmlNode * | GetResourceNode (const wxString &name) const
| Returns the wxXmlNode containing the definition of the object with the given name or nullptr. More...
long | GetVersion () const
| Returns version information (a.b.c.d = d + 256*c + 2562*b + 2563*a). More...
void | InitAllHandlers ()
| Initializes handlers for all supported controls/windows. More...
bool | Load (const wxString &filemask)
| Loads resources from XML files that match given filemask. More...
bool | LoadDocument (wxXmlDocument *doc, const wxString &name=wxString())
| Load resources from the XML document containing them. More...
bool | LoadFile (const wxFileName &file)
| Simpler form of Load() for loading a single XRC file. More...
bool | LoadAllFiles (const wxString &dirname)
| Loads all .xrc files from directory dirname. More...
wxBitmap | LoadBitmap (const wxString &name)
| Loads a bitmap resource from a file. More...
wxDialog * | LoadDialog (wxWindow *parent, const wxString &name)
| Loads a dialog. More...
bool | LoadDialog (wxDialog *dlg, wxWindow *parent, const wxString &name)
| Loads a dialog. More...
wxFrame * | LoadFrame (wxWindow *parent, const wxString &name)
| Loads a frame from the resource. More...
bool | LoadFrame (wxFrame *frame, wxWindow *parent, const wxString &name)
| Loads the contents of a frame onto an existing wxFrame. More...
wxIcon | LoadIcon (const wxString &name)
| Loads an icon resource from a file. More...
wxMenu * | LoadMenu (const wxString &name)
| Loads menu from resource. More...
wxMenuBar * | LoadMenuBar (wxWindow *parent, const wxString &name)
| Loads a menubar from resource. More...
wxMenuBar * | LoadMenuBar (const wxString &name)
wxObject * | LoadObject (wxWindow *parent, const wxString &name, const wxString &classname)
| Load an object from the resource specifying both the resource name and the class name. More...
bool | LoadObject (wxObject *instance, wxWindow *parent, const wxString &name, const wxString &classname)
wxObject * | LoadObjectRecursively (wxWindow *parent, const wxString &name, const wxString &classname)
| Load an object from anywhere in the resource tree. More...
bool | LoadObjectRecursively (wxObject *instance, wxWindow *parent, const wxString &name, const wxString &classname)
wxPanel * | LoadPanel (wxWindow *parent, const wxString &name)
| Loads a panel. More...
bool | LoadPanel (wxPanel *panel, wxWindow *parent, const wxString &name)
| Loads a panel. More...
wxToolBar * | LoadToolBar (wxWindow *parent, const wxString &name)
| Loads a toolbar. More...
void | SetDomain (const wxString &domain)
| Sets the domain (message catalog) that will be used to load translatable strings in the XRC. More...
void | SetFlags (int flags)
| Sets flags (bitlist of wxXmlResourceFlags enumeration values). More...
bool | Unload (const wxString &filename)
| This function unloads a resource previously loaded by Load(). More...
Public Member Functions inherited from wxObject
| wxObject ()
| Default ctor; initializes to nullptr the internal reference data. More...
| wxObject (const wxObject &other)
| Copy ctor. More...
virtual | ~wxObject ()
| Destructor. More...
virtual wxClassInfo * | GetClassInfo () const
| This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar). More...
wxObjectRefData * | GetRefData () const
| Returns the wxObject::m_refData pointer, i.e. the data referenced by this object. More...
bool | IsKindOf (const wxClassInfo *info) const
| Determines whether this class is a subclass of (or the same class as) the given class. More...
bool | IsSameAs (const wxObject &obj) const
| Returns true if this object has the same data pointer as obj. More...
void | Ref (const wxObject &clone)
| Makes this object refer to the data in clone. More...
void | SetRefData (wxObjectRefData *data)
| Sets the wxObject::m_refData pointer. More...
void | UnRef ()
| Decrements the reference count in the associated data, and if it is zero, deletes the data. More...
void | UnShare ()
| This is the same of AllocExclusive() but this method is public. More...
void | operator delete (void *buf)
| The delete operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined. More...
void * | operator new (size_t size, const wxString &filename=nullptr, int lineNum=0)
| The new operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined. More...

## Static Public Member Functions
static void | AddSubclassFactory (wxXmlSubclassFactory *factory)
| Registers subclasses factory for use in XRC. More...
static wxString | FindXRCIDById (int numId)
| Returns a string ID corresponding to the given numeric ID. More...
static wxXmlResource * | Get ()
| Gets the global resources object or creates one if none exists. More...
static int | GetXRCID (const wxString &str_id, int value_if_not_found=wxID_NONE)
| Returns a numeric ID that is equivalent to the string ID used in an XML resource. More...
static wxXmlResource * | Set (wxXmlResource *res)
| Sets the global resources object and returns a pointer to the previous one (may be nullptr). More...

## Protected Member Functions
void | ReportError (const wxXmlNode *context, const wxString &message)
| Reports error in XRC resources to the user. More...
virtual void | DoReportError (const wxString &xrcFile, const wxXmlNode *position, const wxString &message)
| Implementation of XRC resources errors reporting. More...
Protected Member Functions inherited from wxObject
void | AllocExclusive ()
| Ensure that this object's data is not shared with any other object. More...
virtual wxObjectRefData * | CreateRefData () const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and returns it. More...
virtual wxObjectRefData * | CloneRefData (const wxObjectRefData *data) const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and initializes it copying data. More...

## Additional Inherited Members
Protected Attributes inherited from wxObject
wxObjectRefData * | m_refData
| Pointer to an object which is the object's reference-counted data. More...

## Constructor & Destructor Documentation

## ◆ wxXmlResource() [1/2]

wxXmlResource::wxXmlResource | ( | const wxString & | filemask,
|  | int | flags = `wxXRC_USE_LOCALE`,
|  | const wxString & | domain = `wxEmptyString`
| ) |  |

Constructor.

Parameters

  filemask | The XRC file, archive file, or wildcard specification that will be used to load all resource files inside a zip archive.
  flags | One or more value of the wxXmlResourceFlags enumeration.
  domain | The name of the gettext catalog to search for translatable strings. By default all loaded catalogs will be searched. This provides a way to allow the strings to only come from a specific catalog.

## ◆ wxXmlResource() [2/2]

wxXmlResource::wxXmlResource | ( | int | flags = `wxXRC_USE_LOCALE`,
|  | const wxString & | domain = `wxEmptyString`
| ) |  |

Constructor.

Parameters

  flags | One or more value of the wxXmlResourceFlags enumeration.
  domain | The name of the gettext catalog to search for translatable strings. By default all loaded catalogs will be searched. This provides a way to allow the strings to only come from a specific catalog.

## ◆ ~wxXmlResource()

virtual wxXmlResource::~wxXmlResource | ( |  | ) |
| virtual

Destructor.

## Member Function Documentation

## ◆ AddHandler()

void wxXmlResource::AddHandler | ( | wxXmlResourceHandler * | handler | ) |

Initializes only a specific handler (or custom handler).

Convention says that the handler name is equal to the control's name plus 'XmlHandler', for example wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler.

The XML resource compiler (wxxrc) can create include file that contains initialization code for all controls used within the resource. Note that this handler must be allocated on the heap, since it will be deleted by ClearHandlers() later.

## ◆ AddSubclassFactory()

static void wxXmlResource::AddSubclassFactory | ( | wxXmlSubclassFactory * | factory | ) |
| static

Registers subclasses factory for use in XRC.

This is useful only for language bindings developers who need a way to implement subclassing in wxWidgets ports that don't support wxRTTI (e.g. wxPython).

## ◆ AttachUnknownControl()

bool wxXmlResource::AttachUnknownControl | ( | const wxString & | name,
|  | wxWindow * | control,
|  | wxWindow * | parent = `nullptr`
| ) |  |

Attaches an unknown control to the given panel/window/dialog.

Unknown controls are used in conjunction with <object class="unknown">.

## ◆ ClearHandlers()

void wxXmlResource::ClearHandlers | ( |  | ) |

Removes all handlers and deletes them (this means that any handlers added using AddHandler() must be allocated on the heap).

## ◆ CompareVersion()

int wxXmlResource::CompareVersion | ( | int | major,
|  | int | minor,
|  | int | release,
|  | int | revision
| ) |  | const

Compares the XRC version to the argument.

Returns -1 if the XRC version is less than the argument, +1 if greater, and 0 if they are equal.

## ◆ DoReportError()

virtual void wxXmlResource::DoReportError | ( | const wxString & | xrcFile,
|  | const wxXmlNode * | position,
|  | const wxString & | message
| ) |  |
| protected virtual

Implementation of XRC resources errors reporting.

This method is called by ReportError() and shouldn't be called directly; use ReportError() or wxXmlResourceHandler::ReportError() to log errors.

Default implementation uses wxLogError().

Parameters

  xrcFile | File the error occurred in or empty string if it couldn't be determined.
  position | XML node where the error occurred or nullptr if it couldn't be determined.
  message | Text of the error message. See ReportError() documentation for details of the string's format.

Note
  You may override this method in a derived class to customize errors reporting. If you do so, you'll need to either use the derived class in all your code or call wxXmlResource::Set() to change the global wxXmlResource instance to your class.

Since
  2.9.0

See also
  ReportError()

## ◆ EnableFeature()

void wxXmlResource::EnableFeature | ( | const wxString & | feature | ) |

Add a feature considered to be enabled.

Objects in XRC documents may have a "feature" attribute, as explained in the Feature-specific Content. By default, none of the features is enabled and so all objects with this attribute are discarded. Calling this function marks the given feature as being enabled and affects the subsequent calls to LoadDocument() and related functions, which will keep, rather than discard, any nodes using the given feature string in their "feature" attribute.

This function can be called multiple times to enable more than one feature.

Since
  3.3.0

## ◆ FindXRCIDById()

static wxString wxXmlResource::FindXRCIDById | ( | int | numId | ) |
| static

Returns a string ID corresponding to the given numeric ID.

The string returned is such that calling GetXRCID() with it as parameter yields numId. If there is no string identifier corresponding to the given numeric one, an empty string is returned.

Notice that, unlike GetXRCID(), this function is slow as it checks all of the identifiers used in XRC.

Since
  2.9.0

## ◆ Get()

static wxXmlResource* wxXmlResource::Get | ( |  | ) |
| static

Gets the global resources object or creates one if none exists.

## ◆ GetDomain()

const wxString& wxXmlResource::GetDomain | ( |  | ) | const

Returns the domain (message catalog) that will be used to load translatable strings in the XRC.

## ◆ GetFlags()

int wxXmlResource::GetFlags | ( |  | ) | const

Returns flags, which may be a bitlist of wxXmlResourceFlags enumeration values.

## ◆ GetResourceNode()

const wxXmlNode* wxXmlResource::GetResourceNode | ( | const wxString & | name | ) | const

Returns the wxXmlNode containing the definition of the object with the given name or nullptr.

This function recursively searches all the loaded XRC files for an object with the specified name. If the object is found, the wxXmlNode corresponding to it is returned, so this function can be used to access additional information defined in the XRC file and not used by wxXmlResource itself, e.g. contents of application-specific XML tags.

Parameters

  name | The name of the resource which must be unique for this function to work correctly, if there is more than one resource with the given name the choice of the one returned by this function is undefined.

Returns
  The node corresponding to the resource with the given name or nullptr.

## ◆ GetVersion()

long wxXmlResource::GetVersion | ( |  | ) | const

Returns version information (a.b.c.d = d + 256*c + 2562*b + 2563*a).

## ◆ GetXRCID()

static int wxXmlResource::GetXRCID | ( | const wxString & | str_id,
|  | int | value_if_not_found = `wxID_NONE`
| ) |  |
| static

Returns a numeric ID that is equivalent to the string ID used in an XML resource.

If an unknown str_id is requested (i.e. other than wxID_XXX or integer), a new record is created which associates the given string with a number.

If value_if_not_found is `wxID_NONE`, the number is obtained via wxNewId(). Otherwise value_if_not_found is used.

Macro `XRCID(name)` is provided for convenient use in event tables.

Note
  IDs returned by XRCID() cannot be used with the `EVT_*_RANGE` macros, because the order in which they are assigned to symbolic name values is not guaranteed.

## ◆ InitAllHandlers()

void wxXmlResource::InitAllHandlers | ( |  | ) |

Initializes handlers for all supported controls/windows.

This will make the executable quite big because it forces linking against most of the wxWidgets library.

## ◆ InsertHandler()

void wxXmlResource::InsertHandler | ( | wxXmlResourceHandler * | handler | ) |

Add a new handler at the beginning of the handler list.

## ◆ Load()

bool wxXmlResource::Load | ( | const wxString & | filemask | ) |

Loads resources from XML files that match given filemask.

Example:

if (!wxXmlResource::Get()->Load("rc/*.xrc"))
wxLogError("Couldn't load resources!");
wxXmlResource::Load
bool Load(const wxString &filemask)
Loads resources from XML files that match given filemask.
wxXmlResource::Get
static wxXmlResource * Get()
Gets the global resources object or creates one if none exists.
wxLogError
void wxLogError(const char *formatString,...)
The functions to use for error messages, i.e.

Note
  If wxUSE_FILESYS is enabled, this method understands wxFileSystem URLs (see wxFileSystem::FindFirst()).
  If you are sure that the argument is name of single XRC file (rather than an URL or a wildcard), use LoadFile() instead.

See also
  LoadFile(), LoadAllFiles()

## ◆ LoadAllFiles()

bool wxXmlResource::LoadAllFiles | ( | const wxString & | dirname | ) |

Loads all .xrc files from directory dirname.

Tries to load as many files as possible; if there's an error while loading one file, it still attempts to load other files.

Since
  2.9.0

See also
  LoadFile(), Load()

## ◆ LoadBitmap()

wxBitmap wxXmlResource::LoadBitmap | ( | const wxString & | name | ) |

Loads a bitmap resource from a file.

## ◆ LoadDialog() [1/2]

bool wxXmlResource::LoadDialog | ( | wxDialog * | dlg,
|  | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads a dialog.

parent points to parent window (if any).

This form is used to finish creation of an already existing instance (the main reason for this is that you may want to use derived class with a new event table). Example:

MyDialog dlg;
wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
dlg.ShowModal();
wxDialog::ShowModal
virtual int ShowModal()
Shows an application-modal dialog.
wxXmlResource::LoadDialog
wxDialog * LoadDialog(wxWindow *parent, const wxString &name)
Loads a dialog.

## ◆ LoadDialog() [2/2]

wxDialog* wxXmlResource::LoadDialog | ( | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads a dialog.

parent points to parent window (if any).

## ◆ LoadDocument()

bool wxXmlResource::LoadDocument | ( | wxXmlDocument * | doc,
|  | const wxString & | name = `wxString()`
| ) |  |

Load resources from the XML document containing them.

This can be useful when XRC contents comes from some place other than a file or, more generally, an URL, as it can still be read into a wxMemoryInputStream and then wxXmlDocument can be created from this stream and used with this function.

For example:

const char* const xrc_data = ...; // Retrieve it from wherever.
wxMemoryInputStream mis(xrc_data, strlen(xrc_data));
wxScopedPtr<wxXmlDocument> xmlDoc(new wxXmlDocument(mis, "UTF-8"));
if ( !xmlDoc->IsOk() )
{
... handle invalid XML here ...
return;
}
if ( !wxXmlResource::Get()->LoadDocument(xmlDoc.release()) )
{
... handle invalid XRC here ...
return;
}
... use the just loaded XRC as usual ...
wxMemoryInputStream
This class allows using all methods taking a wxInputStream reference to read in-memory data.
Definition: mstream.h:99
wxScopedPtr
Deprecated equivalent of standard unique_ptr template class.
Definition: scopedptr.h:97
wxXmlDocument
This class holds XML data/document as parsed by XML parser in the root node.
Definition: xml.h:713
wxXmlResource::LoadDocument
bool LoadDocument(wxXmlDocument *doc, const wxString &name=wxString())
Load resources from the XML document containing them.

Parameters

  doc | A valid, i.e. non-null, document pointer ownership of which is passed to wxXmlResource, i.e. this pointer can't be used after this function returns.
  name | The name argument is optional, but may be provided if you plan to call Unload() later. It doesn't need to be an existing file or even conform to the usual form of file names as it is not interpreted in any way by wxXmlResource, but it should be unique among the other documents and file names used if specified.

Returns
  true on success, false if the document couldn't be loaded (note that doc is still destroyed in this case to avoid memory leaks).

See also
  Load(), LoadFile()

Since
  3.1.6

## ◆ LoadFile()

bool wxXmlResource::LoadFile | ( | const wxFileName & | file | ) |

Simpler form of Load() for loading a single XRC file.

Since
  2.9.0

See also
  Load(), LoadAllFiles(), LoadDocument()

## ◆ LoadFrame() [1/2]

bool wxXmlResource::LoadFrame | ( | wxFrame * | frame,
|  | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads the contents of a frame onto an existing wxFrame.

This form is used to finish creation of an already existing instance (the main reason for this is that you may want to use derived class with a new event table).

## ◆ LoadFrame() [2/2]

wxFrame* wxXmlResource::LoadFrame | ( | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads a frame from the resource.

parent points to parent window (if any).

## ◆ LoadIcon()

wxIcon wxXmlResource::LoadIcon | ( | const wxString & | name | ) |

Loads an icon resource from a file.

## ◆ LoadMenu()

wxMenu* wxXmlResource::LoadMenu | ( | const wxString & | name | ) |

Loads menu from resource.

Returns nullptr on failure.

## ◆ LoadMenuBar() [1/2]

wxMenuBar* wxXmlResource::LoadMenuBar | ( | const wxString & | name | ) |

## ◆ LoadMenuBar() [2/2]

wxMenuBar* wxXmlResource::LoadMenuBar | ( | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads a menubar from resource.

Returns nullptr on failure.

## ◆ LoadObject() [1/2]

bool wxXmlResource::LoadObject | ( | wxObject * | instance,
|  | wxWindow * | parent,
|  | const wxString & | name,
|  | const wxString & | classname
| ) |  |

## ◆ LoadObject() [2/2]

wxObject* wxXmlResource::LoadObject | ( | wxWindow * | parent,
|  | const wxString & | name,
|  | const wxString & | classname
| ) |  |

Load an object from the resource specifying both the resource name and the class name.

The first overload lets you load nonstandard container windows and returns nullptr on failure. The second one lets you finish the creation of an existing instance and returns false on failure.

In either case, only the resources defined at the top level of XRC files can be loaded by this function, use LoadObjectRecursively() if you need to load an object defined deeper in the hierarchy.

## ◆ LoadObjectRecursively() [1/2]

bool wxXmlResource::LoadObjectRecursively | ( | wxObject * | instance,
|  | wxWindow * | parent,
|  | const wxString & | name,
|  | const wxString & | classname
| ) |  |

## ◆ LoadObjectRecursively() [2/2]

wxObject* wxXmlResource::LoadObjectRecursively | ( | wxWindow * | parent,
|  | const wxString & | name,
|  | const wxString & | classname
| ) |  |

Load an object from anywhere in the resource tree.

These methods are similar to LoadObject() but may be used to load an object from anywhere in the resource tree and not only the top level. Note that you will very rarely need to do this as in normal use the entire container window (defined at the top level) is loaded and not its individual children but this method can be useful in some particular situations.

Since
  2.9.1

## ◆ LoadPanel() [1/2]

bool wxXmlResource::LoadPanel | ( | wxPanel * | panel,
|  | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads a panel.

parent points to the parent window. This form is used to finish creation of an already existing instance.

## ◆ LoadPanel() [2/2]

wxPanel* wxXmlResource::LoadPanel | ( | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads a panel.

parent points to the parent window.

## ◆ LoadToolBar()

wxToolBar* wxXmlResource::LoadToolBar | ( | wxWindow * | parent,
|  | const wxString & | name
| ) |  |

Loads a toolbar.

## ◆ ReportError()

void wxXmlResource::ReportError | ( | const wxXmlNode * | context,
|  | const wxString & | message
| ) |  |
| protected

Reports error in XRC resources to the user.

Any errors in XRC input files should be reported using this method (or its wxXmlResourceHandler::ReportError() equivalent). Unlike wxLogError(), this method presents the error to the user in a more usable form. In particular, the output is compiler-like and contains information about the exact location of the error.

Parameters

  context | XML node the error occurred in or relates to. This can be nullptr, but should be the most specific node possible, as its line number is what is reported to the user.
  message | Text of the error message. This string should always be in English (i.e. not wrapped in _()). It shouldn't be a sentence – it should start with lower-case letter and shouldn't have a trailing period or exclamation point.

Since
  2.9.0

See also
  wxXmlResourceHandler::ReportError(), DoReportError()

## ◆ Set()

static wxXmlResource* wxXmlResource::Set | ( | wxXmlResource * | res | ) |
| static

Sets the global resources object and returns a pointer to the previous one (may be nullptr).

## ◆ SetDomain()

void wxXmlResource::SetDomain | ( | const wxString & | domain | ) |

Sets the domain (message catalog) that will be used to load translatable strings in the XRC.

## ◆ SetFlags()

void wxXmlResource::SetFlags | ( | int | flags | ) |

Sets flags (bitlist of wxXmlResourceFlags enumeration values).

## ◆ Unload()

bool wxXmlResource::Unload | ( | const wxString & | filename | ) |

This function unloads a resource previously loaded by Load().

Returns true if the resource was successfully unloaded and false if it hasn't been found in the list of loaded resources.