marcp marcp - 1 year ago 76
C++ Question

How to load arbitrary XRC

I want to build a tool to display/debug my XRC files. I know there are tools that can do this already, but it's a learning exercise and will eventually be used within a larger tool.

I'm using a wxFileDialog to choose a file and store the name in a wxTextControl. Then on another button click I load the XRC. The problem is that wxXMLResource.LoadDialog requires the name of the dialog to load, and for an arbitrary XRC file I don't know it.

void XRCLoad::ChooseFile(wxCommandEvent& event) {
wxFileDialog* OpenDialog = new wxFileDialog(
this, _("Choose a file to open"),
wxEmptyString, wxEmptyString, _("Dialog files (*.xrc)|*.xrc"),
wxFD_OPEN, wxDefaultPosition);

if (OpenDialog->ShowModal() == wxID_OK){
ebFile->SetValue( OpenDialog->GetPath());


void XRCLoad::LoadXRC(wxCommandEvent& event)
if (wxXmlResource::Get()->LoadFile(ebFile->GetValue())){
wxDialog dlg;
wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("XXXXX")); // what should XXXX be?

A quick perusal of the wxFormBuilder code shows that they load the file as xml and then parse the tree, but in that case they have to do it that way since they are deconstructing it. That seems unnecessarily roundabout in my case.

I just want to display the dialog found in a file, which in my case there will only be one of. I either need to find out it's name by inspecting the wxXMLResource, find it's name some other way, or (preferably) load the dialog without knowing its name.

Answer Source

This can't be done using just wxXmlResource API, you will indeed need to parse XML just to find out what is defined in your XRC file. Notice that, generally speaking, it can contain 0, 1 or more dialogs as well as an arbitrary number of other objects (e.g. frames, menus, icons, ...).

Also note that an XRC file can contain custom object types, loaded using custom XRC handlers in the program code, and if you want to handle those, you will also need to scan the XML file for their presence and register some placeholder XRC handler for them -- it won't be able to load them in any meaningful sense, of course, but without it the dialog containing such elements would fail to load at all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download