xml-based resource system overviewclasses: wxxmlresource, wxxmlresourcehandler the xml-based resource system, known as xrc, allows user interface elements such as dialogs, menu bars and toolbars, to be stored in text files and loaded into the application at run-time. xrc files can also be compiled into binary xrs files or c++ code (the former makes it possible to store all resources in a single file and the latter is useful when you want to embed the resources into the executable). there are several advantages to using xrc resources.
xrc was written by vaclav slavik.
xrc concepts
xrc conceptsthese are the typical steps for using xrc files in your application.
to create an xrc file, you can use one of the following methods.
a complete list of third-party tools that write to xrc can be found at www.wxwidgets.org/lnk_tool.htm. it is highly recommended that you use a resource editing tool, since it's fiddly writing xrc files by hand. you can use wxxmlresource::load in a number of ways. you can pass an xrc file (xml-based text resource file) or a zip-compressed file (extension zip or xrs) containing other xrc. you can also use embedded c++ resources
using binary resource filesto compile binary resource files, use the command-line wxrc utility. it takes one or more file parameters (the input xrc files) and the following switches and options:
for example: % wxrc resource.xrc % wxrc resource.xrc -o resource.xrs % wxrc resource.xrc -v -c -o resource.cppnote xrs file is essentially a renamed zip archive which means that you can manipulate it with standard zip tools. note that if you are using xrs files, you have to initialize the wxfilesystem archive handler first! it is a simple thing to do:
#include <wx/filesys.h> #include <wx/fs_arc.h> ... wxfilesystem::addhandler(new wxarchivefshandler); using embedded resourcesit is sometimes useful to embed resources in the executable itself instead of loading an external file (e.g. when your app is small and consists only of one exe file). xrc provides means to convert resources into regular c++ file that can be compiled and included in the executable. use the -c switch to wxrc utility to produce c++ file with embedded resources. this file will contain a function called initxmlresource (unless you override this with a command line switch). use it to load the resource:
extern void initxmlresource(); // defined in generated file ... wxxmlresource::get()->initallhandlers(); initxmlresource(); ... xrc c++ samplethis is the c++ source file (xrcdemo.cpp) for the xrc sample.
#include "wx/wx.h" #include "wx/image.h" #include "wx/xrc/xmlres.h" // the application icon #if defined(__wxgtk__) || defined(__wxmotif__) || defined(__wxmac__) #include "rc/appicon.xpm" #endif // ---------------------------------------------------------------------------- // private classes // ---------------------------------------------------------------------------- // define a new application type, each program should derive a class from wxapp class myapp : public wxapp { public: // override base class virtuals // ---------------------------- // this one is called on application startup and is a good place for the app // initialization (doing it here and not in the ctor allows to have an error // return: if oninit() returns false, the application terminates) virtual bool oninit(); }; // define a new frame type: this is going to be our main frame class myframe : public wxframe { public: // ctor(s) myframe(const wxstring& title, const wxpoint& pos, const wxsize& size); // event handlers (these functions should _not_ be virtual) void onquit(wxcommandevent& event); void onabout(wxcommandevent& event); void ondlg1(wxcommandevent& event); void ondlg2(wxcommandevent& event); private: // any class wishing to process wxwidgets events must use this macro declare_event_table() }; // ---------------------------------------------------------------------------- // event tables and other macros for wxwidgets // ---------------------------------------------------------------------------- begin_event_table(myframe, wxframe) evt_menu(xrcid("menu_quit"), myframe::onquit) evt_menu(xrcid("menu_about"), myframe::onabout) evt_menu(xrcid("menu_dlg1"), myframe::ondlg1) evt_menu(xrcid("menu_dlg2"), myframe::ondlg2) end_event_table() implement_app(myapp) // ---------------------------------------------------------------------------- // the application class // ---------------------------------------------------------------------------- // 'main program' equivalent: the program execution "starts" here bool myapp::oninit() { wximage::addhandler(new wxgifhandler); wxxmlresource::get()->initallhandlers(); wxxmlresource::get()->load("rc/resource.xrc"); myframe *frame = new myframe("xml resources demo", wxpoint(50, 50), wxsize(450, 340)); frame->show(true); return true; } // ---------------------------------------------------------------------------- // main frame // ---------------------------------------------------------------------------- // frame constructor myframe::myframe(const wxstring& title, const wxpoint& pos, const wxsize& size) : wxframe((wxframe *)null, -1, title, pos, size) { seticon(wxicon(appicon)); setmenubar(wxxmlresource::get()->loadmenubar("mainmenu")); settoolbar(wxxmlresource::get()->loadtoolbar(this, "toolbar")); } // event handlers void myframe::onquit(wxcommandevent& wxunused(event)) { // true is to force the frame to close close(true); } void myframe::onabout(wxcommandevent& wxunused(event)) { wxstring msg; msg.printf( _t("this is the about dialog of xml resources demo.\n") _t("welcome to %s"), wxversion_string); wxmessagebox(msg, "about xml resources demo", wxok | wxicon_information, this); } void myframe::ondlg1(wxcommandevent& wxunused(event)) { wxdialog dlg; wxxmlresource::get()->loaddialog(&dlg, this, "dlg1"); dlg.showmodal(); } void myframe::ondlg2(wxcommandevent& wxunused(event)) { wxdialog dlg; wxxmlresource::get()->loaddialog(&dlg, this, "dlg2"); dlg.showmodal(); } xrc resource file samplethis is the xml file (resource.xrc) for the xrc sample.
<?xml version="1.0"?> <resource version="2.3.0.1"> <object class="wxmenubar" name="mainmenu"> <style>wxmb_dockable</style> <object class="wxmenu" name="menu_file"> <label>_file</label> <style>wxmenu_tearoff</style> <object class="wxmenuitem" name="menu_about"> <label>_about...</label> <bitmap>filesave.gif</bitmap> </object> <object class="separator"/> <object class="wxmenuitem" name="menu_dlg1"> <label>dialog 1</label> </object> <object class="wxmenuitem" name="menu_dlg2"> <label>dialog 2</label> </object> <object class="separator"/> <object class="wxmenuitem" name="menu_quit"> <label>e_xit\talt-x</label> </object> </object> </object> <object class="wxtoolbar" name="toolbar"> <style>wxtb_flat|wxtb_dockable</style> <margins>2,2</margins> <object class="tool" name="menu_open"> <bitmap>fileopen.gif</bitmap> <tooltip>open catalog</tooltip> </object> <object class="tool" name="menu_save"> <bitmap>filesave.gif</bitmap> <tooltip>save catalog</tooltip> </object> <object class="tool" name="menu_update"> <bitmap>update.gif</bitmap> <tooltip>update catalog - synchronize it with sources</tooltip> </object> <separator/> <object class="tool" name="menu_quotes"> <bitmap>quotes.gif</bitmap> <toggle>1</toggle> <tooltip>display quotes around the string?</tooltip> </object> <object class="separator"/> <object class="tool" name="menu_fuzzy"> <bitmap>fuzzy.gif</bitmap> <tooltip>toggled if selected string is fuzzy translation</tooltip> <toggle>1</toggle> </object> </object> <object class="wxdialog" name="dlg1"> <object class="wxboxsizer"> <object class="sizeritem"> <object class="wxbitmapbutton"> <bitmap>fuzzy.gif</bitmap> <focus>fileopen.gif</focus> </object> </object> <object class="sizeritem"> <object class="wxpanel"> <object class="wxstatictext"> <label>fdgdfgdfgdfg</label> </object> <style>wxsunken_border</style> </object> <flag>wxalign_center</flag> </object> <object class="sizeritem"> <object class="wxbutton"> <label>buttonek</label> </object> <border>10d</border> <flag>wxall</flag> </object> <object class="sizeritem"> <object class="wxhtmlwindow"> <htmlcode><h1>hi,</h1>man</htmlcode> <size>100,45d</size> </object> </object> <object class="sizeritem"> <object class="wxnotebook"> <object class="notebookpage"> <object class="wxpanel"> <object class="wxboxsizer"> <object class="sizeritem"> <object class="wxhtmlwindow"> <htmlcode>hello, we are inside a <u>notebook</u>...</htmlcode> <size>50,50d</size> </object> <option>1</option> </object> </object> </object> <label>page</label> </object> <object class="notebookpage"> <object class="wxpanel"> <object class="wxboxsizer"> <object class="sizeritem"> <object class="wxhtmlwindow"> <htmlcode>hello, we are inside a <u>notebook</u>...</htmlcode> <size>50,50d</size> </object> </object> </object> </object> <label>page 2</label> </object> <usenotebooksizer>1</usenotebooksizer> </object> <flag>wxexpand</flag> </object> <orient>wxvertical</orient> </object> </object> <object class="wxdialog" name="dlg2"> <object class="wxboxsizer"> <orient>wxvertical</orient> <object class="sizeritem" name="dfgdfg"> <object class="wxtextctrl"> <size>200,200d</size> <style>wxte_multiline|wxsunken_border</style> <value>hello, this is an ordinary multiline\n textctrl....</value> </object> <option>1</option> <flag>wxexpand|wxall</flag> <border>10</border> </object> <object class="sizeritem"> <object class="wxboxsizer"> <object class="sizeritem"> <object class="wxbutton" name="wxid_ok"> <label>ok</label> <default>1</default> </object> </object> <object class="sizeritem"> <object class="wxbutton" name="wxid_cancel"> <label>cancel</label> </object> <border>10</border> <flag>wxleft</flag> </object> </object> <flag>wxleft|wxright|wxbottom|wxalign_right</flag> <border>10</border> </object> </object> <title>second testing dialog</title> </object> </resource> xrc file formatplease see technical note 14 (docs/tech/tn0014.txt) in your wxwidgets distribution.
c++ header file generationusing the -e switch together with -c, a c++ header file is written containing class definitions for the gui windows defined in the xrc file. this code generation can make it easier to use xrc and automate program development. the classes can be used as basis for development, freeing the programmer from dealing with most of the xrc specifics (e.g. xrcctrl). for each top level window defined in the xrc file a c++ class definition is generated, containing as class members the named widgets of the window. a default constructor for each class is also generated. inside the constructor all xrc loading is done and all class members representing widgets are initialized. a simple example will help understand how the scheme works. suppose you have a xrc file defining a top level window testwnd_base, which subclasses wxframe (any other class like wxdialog will do also), and has subwidgets wxtextctrl a and wxbutton b. the xrc file and corresponding class definition in the header file will be something like:
<?xml version="1.0"?> <resource version="2.3.0.1"> <object class="wxframe" name="testwnd_base"> <size>-1,-1</size> <title>test</title> <object class="wxboxsizer"> <orient>wxhorizontal</orient> <object class="sizeritem"> <object class="wxtextctrl" name="a"> <label>test label</label> </object> </object> <object class="sizeritem"> <object class="wxbutton" name="b"> <label>test button</label> </object> </object> </object> </object> </resource> class testwnd_base : public wxframe { protected: wxtextctrl* a; wxbutton* b; private: void initwidgetsfromxrc(){ wxxmlresource::get()->loadobject(this,null,"testwnd","wxframe"); a = xrcctrl(*this,"a",wxtextctrl); b = xrcctrl(*this,"b",wxbutton); } public: testwnd::testwnd(){ initwidgetsfromxrc(); } };the generated window class can be used as basis for the full window class. the class members which represent widgets may be accessed by name instead of using xrcctrl every time you wish to reference them (note that they are protected class members), though you must still use xrcid to refer to widget ids in the event table. example:
#include "resource.h" class testwnd : public testwnd_base { public: testwnd(){ // a, b already initialised at this point a->setvalue("updated in testwnd::testwnd"); b->setvalue("nice :)"); } void onbpressed(wxevent& event){ close(); } declare_event_table(); }; begin_event_table(testwnd,testwnd_base) evt_button(xrcid("b"),testwnd::onbpressed) end_event_table() adding new resource handlerscoming soon.
|