1 % Note: -e/C++ header generation documentation added by
2 % Eduardo Marques <edrdo@netcabo.pt>
4 \section{XML-based resource system overview
}\label{xrcoverview
}
6 Classes:
\helpref{wxXmlResource
}{wxxmlresource
},
\helpref{wxXmlResourceHandler
}{wxxmlresourcehandler
}
8 The XML-based resource system, known as XRC, allows user interface elements such as
9 dialogs, menu bars and toolbars, to be stored in text files and loaded into
10 the application at run-time. XRC files can also be compiled into binary XRS files or C++
11 code (the former makes it possible to store all resources in a single file and the latter
12 is useful when you want to embed the resources into the executable).
14 There are several advantages to using XRC resources.
16 \begin{itemize
}\itemsep=
0pt
17 \item Recompiling and linking an application is not necessary if the
19 \item If you use a dialog designer that generates C++ code, it can be hard
20 to reintegrate this into existing C++ code. Separation of resources and code
21 is a more elegant solution.
22 \item You can choose between different alternative resource files at run time, if necessary.
23 \item The XRC format uses sizers for flexibility, allowing dialogs to be resizable
25 \item The XRC format is a wxWidgets standard,
26 and can be generated or postprocessed by any program that understands it. As it is based
27 on the XML standard, existing XML editors can be used for simple editing purposes.
30 XRC was written by Vaclav Slavik.
32 \subsection{XRC concepts
}\label{xrcconcepts
}
34 These are the typical steps for using XRC files in your application.
36 \begin{itemize
}\itemsep=
0pt
37 \item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
38 \item If you are going to use
\helpref{XRS files
}{binaryresourcefiles
}, install
39 wxFileSystem archive handler first with
{\tt wxFileSystem::AddHandler(new wxArchiveFSHandler);
}
40 \item call
{\tt wxXmlResource::Get()->InitAllHandlers()
} from your wxApp::OnInit function,
41 and then call
{\tt wxXmlResource::Get()->Load("myfile.xrc")
} to load the resource file;
42 \item to create a dialog from a resource, create it using the default constructor, and then
43 load it using for example
{\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");
}
44 \item set up event tables as usual but use the
{\tt XRCID(str)
} macro to translate from XRC string names
45 to a suitable integer identifier, for example
{\tt EVT
\_MENU(XRCID("quit"), MyFrame::OnQuit)
}.
48 To create an XRC file, you can use one of the following methods.
50 \begin{itemize
}\itemsep=
0pt
51 \item Create the file by hand;
52 \item use
\urlref{wxDesigner
}{http://www.roebling.de
}, a commercial dialog designer/RAD tool;
53 \item use
\urlref{DialogBlocks
}{http://www.anthemion.co.uk/dialogblocks
}, a commercial dialog editor;
54 \item use
\urlref{XRCed
}{http://xrced.sf.net
}, a wxPython-based
55 dialog editor that you can find in the
{\tt wxPython/tools
} subdirectory of the wxWidgets
57 \item use
\urlref{wxGlade
}{http://wxglade.sf.net
}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
60 A complete list of third-party tools that write to XRC can be found at
\urlref{www.wxwidgets.org/lnk
\_tool.htm
}{http://www.wxwidgets.org/lnk
\_tool.htm
}.
62 It is highly recommended that you use a resource editing tool, since it's fiddly writing
65 You can use
\helpref{wxXmlResource::Load
}{wxxmlresourceload
} in a number of ways.
66 You can pass an XRC file (XML-based text resource file)
67 or a
\helpref{zip-compressed file
}{binaryresourcefiles
} (extension ZIP or XRS) containing other XRC.
69 You can also use
\helpref{embedded C++ resources
}{embeddedresource
}
71 \subsection{Using binary resource files
}\label{binaryresourcefiles
}
73 To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
74 (the input XRC files) and the following switches and options:
75 \begin{itemize
}\itemsep=
0pt
76 \item -h (--help): show a help message
77 \item -v (--verbose): show verbose logging information
78 \item -c (--cpp-code): write C++ source rather than a XRS file
79 \item -e (--extra-cpp-code): if used together with -c, generates C++ header file
80 containing class definitions for the windows defined by the XRC file (see special subsection)
81 \item -u (--uncompressed): do not compress XML files (C++ only)
82 \item -g (--gettext): output underscore-wrapped strings that poEdit or gettext can scan. Outputs to stdout, or a file if -o is used
83 \item -n (--function) <name>: specify C++ function name (use with -c)
84 \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
85 \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
91 % wxrc resource.xrc -o resource.xrs
92 % wxrc resource.xrc -v -c -o resource.cpp
97 XRS file is essentially a renamed ZIP archive which means that you can manipulate
98 it with standard ZIP tools. Note that if you are using XRS files, you have
99 to initialize the
\helpref{wxFileSystem
}{wxfilesystem
} archive handler first! It is a simple
103 #include <wx/filesys.h>
104 #include <wx/fs_arc.h>
106 wxFileSystem::AddHandler(new wxArchiveFSHandler);
109 \subsection{Using embedded resources
}\label{embeddedresource
}
111 It is sometimes useful to embed resources in the executable itself instead
112 of loading an external file (e.g. when your app is small and consists only of one
113 exe file). XRC provides means to convert resources into regular C++ file that
114 can be compiled and included in the executable.
116 Use the
{\tt -c
} switch to
117 {\tt wxrc
} utility to produce C++ file with embedded resources. This file will
118 contain a function called
{\it InitXmlResource
} (unless you override this with
119 a command line switch). Use it to load the resource:
122 extern void InitXmlResource(); // defined in generated file
124 wxXmlResource::Get()->InitAllHandlers();
129 \subsection{XRC C++ sample
}\label{xrccppsample
}
131 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
135 #include "wx/image.h"
136 #include "wx/xrc/xmlres.h"
138 // the application icon
139 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
140 #include "rc/appicon.xpm"
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 // Define a new application type, each program should derive a class from wxApp
148 class MyApp : public wxApp
151 // override base class virtuals
152 // ----------------------------
154 // this one is called on application startup and is a good place for the app
155 // initialization (doing it here and not in the ctor allows to have an error
156 // return: if OnInit() returns false, the application terminates)
157 virtual bool OnInit();
160 // Define a new frame type: this is going to be our main frame
161 class MyFrame : public wxFrame
165 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
167 // event handlers (these functions should _not_ be virtual)
168 void OnQuit(wxCommandEvent& event);
169 void OnAbout(wxCommandEvent& event);
170 void OnDlg1(wxCommandEvent& event);
171 void OnDlg2(wxCommandEvent& event);
174 // any class wishing to process wxWidgets events must use this macro
175 DECLARE_EVENT_TABLE()
178 // ----------------------------------------------------------------------------
179 // event tables and other macros for wxWidgets
180 // ----------------------------------------------------------------------------
182 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
183 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
184 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
185 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
186 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
191 // ----------------------------------------------------------------------------
192 // the application class
193 // ----------------------------------------------------------------------------
195 // 'Main program' equivalent: the program execution "starts" here
198 wxImage::AddHandler(new wxGIFHandler);
199 wxXmlResource::Get()->InitAllHandlers();
200 wxXmlResource::Get()->Load("rc/resource.xrc");
202 MyFrame *frame = new MyFrame("XML resources demo",
203 wxPoint(
50,
50), wxSize(
450,
340));
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
213 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
214 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
216 SetIcon(wxICON(appicon));
218 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
219 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
223 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
225 // true is to force the frame to close
229 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
232 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
233 _T("Welcome to
%s"), wxVERSION_STRING);
235 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
238 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
241 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
245 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
248 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
253 \subsection{XRC resource file sample
}\label{xrcsample
}
255 This is the XML file (resource.xrc) for the XRC sample.
258 <?xml version="
1.0"?>
259 <resource version="
2.3.0.1">
260 <object class="wxMenuBar" name="mainmenu">
261 <style>wxMB_DOCKABLE</style>
262 <object class="wxMenu" name="menu_file">
264 <style>wxMENU_TEAROFF</style>
265 <object class="wxMenuItem" name="menu_about">
266 <label>_About...</label>
267 <bitmap>filesave.gif</bitmap>
269 <object class="separator"/>
270 <object class="wxMenuItem" name="menu_dlg1">
271 <label>Dialog
1</label>
273 <object class="wxMenuItem" name="menu_dlg2">
274 <label>Dialog
2</label>
276 <object class="separator"/>
277 <object class="wxMenuItem" name="menu_quit">
278 <label>E_xit
\tAlt-X</label>
282 <object class="wxToolBar" name="toolbar">
283 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
284 <margins>
2,
2</margins>
285 <object class="tool" name="menu_open">
286 <bitmap>fileopen.gif</bitmap>
287 <tooltip>Open catalog</tooltip>
289 <object class="tool" name="menu_save">
290 <bitmap>filesave.gif</bitmap>
291 <tooltip>Save catalog</tooltip>
293 <object class="tool" name="menu_update">
294 <bitmap>update.gif</bitmap>
295 <tooltip>Update catalog - synchronize it with sources</tooltip>
298 <object class="tool" name="menu_quotes">
299 <bitmap>quotes.gif</bitmap>
301 <tooltip>Display quotes around the string?</tooltip>
303 <object class="separator"/>
304 <object class="tool" name="menu_fuzzy">
305 <bitmap>fuzzy.gif</bitmap>
306 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
310 <object class="wxDialog" name="dlg1">
311 <object class="wxBoxSizer">
312 <object class="sizeritem">
313 <object class="wxBitmapButton">
314 <bitmap>fuzzy.gif</bitmap>
315 <focus>fileopen.gif</focus>
318 <object class="sizeritem">
319 <object class="wxPanel">
320 <object class="wxStaticText">
321 <label>fdgdfgdfgdfg</label>
323 <style>wxSUNKEN_BORDER</style>
325 <flag>wxALIGN_CENTER</flag>
327 <object class="sizeritem">
328 <object class="wxButton">
329 <label>Buttonek</label>
334 <object class="sizeritem">
335 <object class="wxHtmlWindow">
336 <htmlcode><h1>Hi,</h1>man</htmlcode>
340 <object class="sizeritem">
341 <object class="wxNotebook">
342 <object class="notebookpage">
343 <object class="wxPanel">
344 <object class="wxBoxSizer">
345 <object class="sizeritem">
346 <object class="wxHtmlWindow">
347 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
356 <object class="notebookpage">
357 <object class="wxPanel">
358 <object class="wxBoxSizer">
359 <object class="sizeritem">
360 <object class="wxHtmlWindow">
361 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
367 <label>Page
2</label>
369 <usenotebooksizer>
1</usenotebooksizer>
371 <flag>wxEXPAND</flag>
373 <orient>wxVERTICAL</orient>
376 <object class="wxDialog" name="dlg2">
377 <object class="wxBoxSizer">
378 <orient>wxVERTICAL</orient>
379 <object class="sizeritem" name="dfgdfg">
380 <object class="wxTextCtrl">
381 <size>
200,
200d</size>
382 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
383 <value>Hello, this is an ordinary multiline
\n textctrl....</value>
386 <flag>wxEXPAND|wxALL</flag>
389 <object class="sizeritem">
390 <object class="wxBoxSizer">
391 <object class="sizeritem">
392 <object class="wxButton" name="wxID_OK">
397 <object class="sizeritem">
398 <object class="wxButton" name="wxID_CANCEL">
399 <label>Cancel</label>
405 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
409 <title>Second testing dialog</title>
414 \subsection{XRC file format
}\label{xrcfileformat
}
416 Please see Technical Note
14 (docs/tech/tn0014.txt) in your wxWidgets
419 \subsection{C++ header file generation
}\label{xrccppheader
}
421 Using the
{\tt -e
} switch together with
{\tt -c
}, a C++ header file is written
422 containing class definitions for the GUI windows defined in the XRC file.
423 This code generation can make it easier to use XRC and automate program
425 The classes can be used as basis for development, freeing the
426 programmer from dealing with most of the XRC specifics (e.g.
{\tt XRCCTRL
}).
428 For each top level window defined in the XRC file a C++ class definition is
429 generated, containing as class members the named widgets of the window.
430 A default constructor for each class is also generated. Inside the constructor
431 all XRC loading is done and all class members representing widgets are initialized.
433 A simple example will help understand how the scheme works. Suppose you have
434 a XRC file defining a top level window
{\tt TestWnd
\_Base}, which subclasses
{\tt wxFrame
} (any
435 other class like
{\tt wxDialog
} will do also), and has subwidgets
{\tt wxTextCtrl
} A and
{\tt wxButton
} B.
436 The XRC file and corresponding class definition in the header file will be something like:
439 <?xml version="
1.0"?>
440 <resource version="
2.3.0.1">
441 <object class="wxFrame" name="TestWnd_Base">
444 <object class="wxBoxSizer">
445 <orient>wxHORIZONTAL</orient>
446 <object class="sizeritem">
447 <object class="wxTextCtrl" name="A">
448 <label>Test label</label>
451 <object class="sizeritem">
452 <object class="wxButton" name="B">
453 <label>Test button</label>
461 class TestWnd_Base : public wxFrame
{
467 void InitWidgetsFromXRC()
{
468 wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
469 A = XRCCTRL
(*this,"A",wxTextCtrl);
470 B = XRCCTRL(*this,"B",wxButton);
474 InitWidgetsFromXRC();
479 The generated window class can be used as basis for the full window class. The
480 class members which represent widgets may be accessed by name instead of using
481 {\tt XRCCTRL} every time you wish to reference them (note that they are {\tt protected} class members),
482 though you must still use {\tt XRCID} to refer to widget IDs in the event
488 #include "resource.h"
490 class TestWnd : public TestWnd_Base {
493 // A, B already initialised at this point
494 A->SetValue("Updated in TestWnd::TestWnd");
495 B->SetValue("Nice :)");
497 void OnBPressed(wxEvent& event){
500 DECLARE_EVENT_TABLE();
503 BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
504 EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
511 \subsection{Adding new resource handlers}\label{newresourcehandlers}
513 Adding a new resource handler is pretty easy.
514 Typically, to add an handler for the {\tt MyControl} class, you'll want to create
515 the {\tt xh\_mycontrol.h} {\tt xh\_mycontrol.cpp} files.
517 The header needs to contains the {\tt MyControlXmlHandler} class definition:
520 class MyControlXmlHandler : public wxXmlResourceHandler
525 MyControlXmlHandler();
527 // Creates the control and returns a pointer to it.
528 virtual wxObject *DoCreateResource();
530 // Returns true if we know how to create a control for the given node.
531 virtual bool CanHandle(wxXmlNode *node);
533 // Register with wxWidgets' dynamic class subsystem.
534 DECLARE_DYNAMIC_CLASS(MyControlXmlHandler)
538 The implementation of your custom XML handler will typically look as:
541 // Register with wxWidgets' dynamic class subsystem.
542 IMPLEMENT_DYNAMIC_CLASS(MyControlXmlHandler, wxXmlResourceHandler)
544 MyControlXmlHandler::MyControlXmlHandler()
546 // this call adds support for all wxWindows class styles
547 // (e.g. wxSIMPLE_BORDER, wxSUNKEN_BORDER, wxWS_EX_* etc etc)
550 // if MyControl class supports e.g. MYCONTROL_DEFAULT_STYLE
552 // XRC_ADD_STYLE(MYCONTROL_DEFAULT_STYLE);
555 wxObject *MyControlXmlHandler::DoCreateResource()
557 // the following macro will init a pointer named "control"
558 // with a new instance of the MyControl class, but will NOT
560 XRC_MAKE_INSTANCE(control, MyControl)
562 // this is the point where you'll typically need to do the most
563 // important changes: here the control is created and initialized.
564 // You'll want to use the wxXmlResourceHandler's getters to
565 // do most of your work.
566 // If e.g. the MyControl::Create function looks like:
568 // bool MyControl::Create(wxWindow *parent, int id,
569 // const wxBitmap &first, const wxPoint &posFirst,
570 // const wxBitmap &second, const wxPoint &posSecond,
571 // const wxString &theTitle, const wxFont &titleFont,
572 // const wxPoint &pos, const wxSize &size,
573 // long style = MYCONTROL_DEFAULT_STYLE,
574 // const wxString &name = wxT("MyControl"));
576 // then the XRC for your component should look like:
578 // <object class="MyControl" name="some_name">
579 // <first-bitmap>first.xpm</first-bitmap>
580 // <second-bitmap>text.xpm</second-bitmap>
581 // <first-pos>3,3</first-pos>
582 // <second-pos>4,4</second-pos>
583 // <the-title>a title</the-title>
585 // <!-- the standard XRC tags for describing a font: <size>, <style>, <weight>, etc -->
587 // <!-- XRC also accepts other usual tags for wxWindow-derived classes:
588 // like e.g. <name>, <style>, <size>, <position>, etc -->
591 // and the code to read your custom tags from the XRC file is just:
592 control->Create(m_parentAsWindow, GetID(),
593 GetBitmap(wxT("first-bitmap")),
594 GetPosition(wxT("first-pos")),
595 GetBitmap(wxT("second-bitmap")),
596 GetPosition(wxT("second-pos")),
597 GetText(wxT("the-title")),
598 GetFont(wxT("title-font")),
599 GetPosition(), GetSize(), GetStyle(), GetName());
601 SetupWindow(control);
606 bool MyControlXmlHandler::CanHandle(wxXmlNode *node)
608 // this function tells XRC system that this handler can parse
609 // the <object class="MyControl"> tags
610 return IsOfClass(node, wxT("MyControl"));
614 You may want to check the \helpref{wxXmlResourceHandler}{wxxmlresourcehandler} documentation
615 to see how many built-in getters it contains. It's very easy to retrieve also complex structures
616 out of XRC files using them.