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 {\bf IMPORTANT NOTE:
} XRC is not yet a part of the core wxWidgets library, so
9 please see the next section for how to compile and link it. Otherwise if you
10 try to use it, you will get link errors.
12 The XML-based resource system, known as XRC, allows user interface elements such as
13 dialogs, menu bars and toolbars, to be stored in text files and loaded into
14 the application at run-time. XRC files can also be compiled into binary XRS files or C++
15 code (the former makes it possible to store all resources in since file and the latter
16 is useful when you want to embed the resources into the executable).
18 There are several advantages to using XRC resources.
20 \begin{itemize
}\itemsep=
0pt
21 \item Recompiling and linking an application is not necessary if the
23 \item If you use a dialog designers that generates C++ code, it can be hard
24 to reintegrate this into existing C++ code. Separation of resources and code
25 is a more elegant solution.
26 \item You can choose between different alternative resource files at run time, if necessary.
27 \item The XRC format uses sizers for flexibility, allowing dialogs to be resizable
29 \item The XRC format is a wxWidgets standard,
30 and can be generated or postprocessed by any program that understands it. As it is based
31 on the XML standard, existing XML editors can be used for simple editing purposes.
34 XRC was written by Vaclav Slavik.
36 \subsection{Compiling and using XRC
}\label{compilingxrc
}
38 XRC can be found under the 'contrib' hierarchy, in the following directories:
41 contrib/src/xrc ; XRC source
42 contrib/include/wx/xrc ; XRC headers
43 contrib/samples/xrc ; XRC sample
44 contrib/utils/wxrc ; XRC resource compiler
45 contrib/utils/wxrcedit ; XRC editor (in progress)
50 \begin{itemize
}\itemsep=
0pt
51 \item Under Windows using VC++, open the contrib/src/xrc/XrcVC.dsw project
52 and compile. Also compile contrib/utils/wxrc using wxBase if you wish to compile
54 \item Under Unix, XRC should be configured when you configured
55 wxWidgets. Make XRC by changing directory to contrib/src/xrc and
56 type 'make'. Similarly compile contrib/utils/wxrc using wxBase if you wish to compile
57 resource files.
{\bf Note:
} there is currently a
58 problem with the wxWidgets build system that means that
59 only the static version of library can be built at present.
64 \begin{itemize
}\itemsep=
0pt
65 \item Under Windows using VC++, link with wxxrc
[d
].lib.
66 \item Under Unix, link with libwxxrc
[d
].a.
69 \subsection{XRC concepts
}\label{xrcconcepts
}
71 These are the typical steps for using XRC files in your application.
73 \begin{itemize
}\itemsep=
0pt
74 \item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
75 \item If you are going to use
\helpref{XRS files
}{binaryresourcefiles
}, install
76 wxFileSystem ZIP handler first with
{\tt wxFileSystem::AddHandler(new wxZipFSHandler);
}
77 \item call
{\tt wxXmlResource::Get()->InitAllHandlers()
} from your wxApp::OnInit function,
78 and then call
{\tt wxXmlResource::Get()->Load("myfile.xrc")
} to load the resource file;
79 \item to create a dialog from a resource, create it using the default constructor, and then
80 load using for example
{\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");
}
81 \item set up event tables as usual but use the
{\tt XRCID(str)
} macro to translate from XRC string names
82 to a suitable integer identifier, for example
{\tt EVT
\_MENU(XRCID("quit"), MyFrame::OnQuit)
}.
85 To create an XRC file, you can use one of the following methods.
87 \begin{itemize
}\itemsep=
0pt
88 \item Create the file by hand;
89 \item use
\urlref{wxDesigner
}{http://www.roebling.de
}, a commercial dialog designer/RAD tool;
90 \item use
\urlref{DialogBlocks
}{http://www.anthemion.co.uk/dialogblocks
}, a commercial dialog editor;
91 \item use
\urlref{XRCed
}{http://xrced.sf.net
}, a wxPython-based
92 dialog editor that you can find in the
{\tt wxPython/tools
} subdirectory of the wxWidgets
94 \item use
\urlref{Glade
}{http://wxglade.sf.net
}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
95 \item use wxrcedit (
{\tt utils/contrib/wxrcedit
}) (under development);
96 \item convert WIN32 RC files to XRC with the tool in
{\tt contrib/utils/convertrc
}.
99 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.in
}.
101 It is highly recommended that you use a resource editing tool, since it's fiddly writing
104 You can use
\helpref{wxXmlResource::Load
}{wxxmlresourceload
} in a number of ways.
105 You can pass an XRC file (XML-based text resource file)
106 or a
\helpref{zip-compressed file
}{binaryresourcefiles
} (extension ZIP or XRS) containing other XRC.
108 You can also use
\helpref{embedded C++ resources
}{embeddedresource
}
110 \subsection{Using binary resource files
}\label{binaryresourcefiles
}
112 To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
113 (the input XRC files) and the following switches and options:
114 \begin{itemize
}\itemsep=
0pt
115 \item -h (--help): show a help message
116 \item -v (--verbose): show verbose logging information
117 \item -c (--cpp-code): write C++ source rather than a XRS file
118 \item -e (--extra-cpp-code): if used together with -c, generates C++ header file
119 containing class definitions for the windows defined by the XRC file (see special subsection)
120 \item -u (--uncompressed): do not compress XML files (C++ only)
121 \item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
122 \item -n (--function) <name>: specify C++ function name (use with -c)
123 \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
124 \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
130 % wxrc resource.wrc -o resource.wrs
131 % wxrc resource.wrc -v -c -o resource.cpp
136 XRS file is esentially a renamed ZIP archive which means that you can manipulate
137 it with standard ZIP tools. Note that if you are using XRS files, you have
138 to initialize
\helpref{wxFileSystem
}{wxfilesystem
} ZIP handler first! It is a simple
142 #include <wx/filesys.h>
143 #include <wx/fs_zip.h>
145 wxFileSystem::AddHandler(new wxZipFSHandler);
148 \subsection{Using embedded resources
}\label{embeddedresource
}
150 It is sometimes useful to embed resources in the executable itself instead
151 of loading external file (e.g. when your app is small and consists only of one
152 exe file). XRC provides means to convert resources into regular C++ file that
153 can be compiled and included in the executable.
155 Use the
{\tt -c
} switch to
156 {\tt wxrc
} utility to produce C++ file with embedded resources. This file will
157 contain a function called
{\it InitXmlResource
} (unless you override this with
158 a command line switch). Use it to load the resource:
161 extern void InitXMLResource(); // defined in generated file
163 wxXmlResource::Get()->InitAllHandlers();
168 \subsection{XRC C++ sample
}\label{xrccppsample
}
170 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
174 #include "wx/image.h"
175 #include "wx/xrc/xmlres.h"
177 // the application icon
178 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
179 #include "rc/appicon.xpm"
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 // Define a new application type, each program should derive a class from wxApp
187 class MyApp : public wxApp
190 // override base class virtuals
191 // ----------------------------
193 // this one is called on application startup and is a good place for the app
194 // initialization (doing it here and not in the ctor allows to have an error
195 // return: if OnInit() returns false, the application terminates)
196 virtual bool OnInit();
199 // Define a new frame type: this is going to be our main frame
200 class MyFrame : public wxFrame
204 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
206 // event handlers (these functions should _not_ be virtual)
207 void OnQuit(wxCommandEvent& event);
208 void OnAbout(wxCommandEvent& event);
209 void OnDlg1(wxCommandEvent& event);
210 void OnDlg2(wxCommandEvent& event);
213 // any class wishing to process wxWidgets events must use this macro
214 DECLARE_EVENT_TABLE()
217 // ----------------------------------------------------------------------------
218 // event tables and other macros for wxWidgets
219 // ----------------------------------------------------------------------------
221 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
222 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
223 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
224 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
225 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
230 // ----------------------------------------------------------------------------
231 // the application class
232 // ----------------------------------------------------------------------------
234 // 'Main program' equivalent: the program execution "starts" here
237 wxImage::AddHandler(new wxGIFHandler);
238 wxXmlResource::Get()->InitAllHandlers();
239 wxXmlResource::Get()->Load("rc/resource.xrc");
241 MyFrame *frame = new MyFrame("XML resources demo",
242 wxPoint(
50,
50), wxSize(
450,
340));
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
252 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
253 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
255 SetIcon(wxICON(appicon));
257 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
258 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
262 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
264 // true is to force the frame to close
268 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
271 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
272 _T("Welcome to
%s"), wxVERSION_STRING);
274 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
277 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
280 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
284 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
287 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
292 \subsection{XRC resource file sample
}\label{xrcsample
}
294 This is the XML file (resource.xrc) for the XRC sample.
297 <?xml version="
1.0"?>
298 <resource version="
2.3.0.1">
299 <object class="wxMenuBar" name="mainmenu">
300 <style>wxMB_DOCKABLE</style>
301 <object class="wxMenu" name="menu_file">
303 <style>wxMENU_TEAROFF</style>
304 <object class="wxMenuItem" name="menu_about">
305 <label>_About...</label>
306 <bitmap>filesave.gif</bitmap>
308 <object class="separator"/>
309 <object class="wxMenuItem" name="menu_dlg1">
310 <label>Dialog
1</label>
312 <object class="wxMenuItem" name="menu_dlg2">
313 <label>Dialog
2</label>
315 <object class="separator"/>
316 <object class="wxMenuItem" name="menu_quit">
317 <label>E_xit
\tAlt-X</label>
321 <object class="wxToolBar" name="toolbar">
322 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
323 <margins>
2,
2</margins>
324 <object class="tool" name="menu_open">
325 <bitmap>fileopen.gif</bitmap>
326 <tooltip>Open catalog</tooltip>
328 <object class="tool" name="menu_save">
329 <bitmap>filesave.gif</bitmap>
330 <tooltip>Save catalog</tooltip>
332 <object class="tool" name="menu_update">
333 <bitmap>update.gif</bitmap>
334 <tooltip>Update catalog - synchronize it with sources</tooltip>
337 <object class="tool" name="menu_quotes">
338 <bitmap>quotes.gif</bitmap>
340 <tooltip>Display quotes around the string?</tooltip>
342 <object class="separator"/>
343 <object class="tool" name="menu_fuzzy">
344 <bitmap>fuzzy.gif</bitmap>
345 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
349 <object class="wxDialog" name="dlg1">
350 <object class="wxBoxSizer">
351 <object class="sizeritem">
352 <object class="wxBitmapButton">
353 <bitmap>fuzzy.gif</bitmap>
354 <focus>fileopen.gif</focus>
357 <object class="sizeritem">
358 <object class="wxPanel">
359 <object class="wxStaticText">
360 <label>fdgdfgdfgdfg</label>
362 <style>wxSUNKEN_BORDER</style>
364 <flag>wxALIGN_CENTER</flag>
366 <object class="sizeritem">
367 <object class="wxButton">
368 <label>Buttonek</label>
373 <object class="sizeritem">
374 <object class="wxHtmlWindow">
375 <htmlcode><h1>Hi,</h1>man</htmlcode>
379 <object class="sizeritem">
380 <object class="wxNotebook">
381 <object class="notebookpage">
382 <object class="wxPanel">
383 <object class="wxBoxSizer">
384 <object class="sizeritem">
385 <object class="wxHtmlWindow">
386 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
395 <object class="notebookpage">
396 <object class="wxPanel">
397 <object class="wxBoxSizer">
398 <object class="sizeritem">
399 <object class="wxHtmlWindow">
400 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
406 <label>Page
2</label>
408 <usenotebooksizer>
1</usenotebooksizer>
410 <flag>wxEXPAND</flag>
412 <orient>wxVERTICAL</orient>
415 <object class="wxDialog" name="dlg2">
416 <object class="wxBoxSizer">
417 <orient>wxVERTICAL</orient>
418 <object class="sizeritem" name="dfgdfg">
419 <object class="wxTextCtrl">
420 <size>
200,
200d</size>
421 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
422 <value>Hello, this is an ordinary multiline
\n textctrl....</value>
425 <flag>wxEXPAND|wxALL</flag>
428 <object class="sizeritem">
429 <object class="wxBoxSizer">
430 <object class="sizeritem">
431 <object class="wxButton" name="wxID_OK">
436 <object class="sizeritem">
437 <object class="wxButton" name="wxID_CANCEL">
438 <label>Cancel</label>
444 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
448 <title>Second testing dialog</title>
453 \subsection{XRC file format
}\label{xrcfileformat
}
455 Please see Technical Note
14 (docs/tech/tn0014.txt) in your wxWidgets
458 \subsection{C++ header file generation
}\label{xrccppheader
}
460 Using the
{\tt -e
} switch together with
{\tt -c
}, a C++ header file is written
461 containing class definitions for the GUI windows defined in the XRC file.
462 This code generation can make it easier to use XRC and automate program
464 The classes can be used as basis for development, freeing the
465 programmer from dealing with most of the XRC specifities (e.g.
{\tt XRCCTRL
}).
467 For each top level window defined in the XRC file a C++ class definition is
468 generated, containing as class members the named widgets of the window.
469 A default constructor for each class is also generated. Inside the constructor
470 all XRC loading is done and all class members representing widgets are initialized.
472 A simple example will help understand how the scheme works. Suppose you have
473 a XRC file defining a top level window
{\tt TestWnd
\_Base}, which subclasses
{\tt wxFrame
} (any
474 other class like
{\tt wxDialog
} will do also), and has subwidgets
{\tt wxTextCtrl
} A and
{\tt wxButton
} B.
475 The XRC file and corresponding class definition in the header file will be something like:
478 <?xml version="
1.0"?>
479 <resource version="
2.3.0.1">
480 <object class="wxFrame" name="TestWnd_Base">
483 <object class="wxBoxSizer">
484 <orient>wxHORIZONTAL</orient>
485 <object class="sizeritem">
486 <object class="wxTextCtrl" name="A">
487 <label>Test label</label>
490 <object class="sizeritem">
491 <object class="wxButton" name="B">
492 <label>Test button</label>
500 class TestWnd_Base : public wxFrame
{
506 void InitWidgetsFromXRC()
{
507 wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
508 A = XRCCTRL
(*this,"A",wxTextCtrl);
509 B = XRCCTRL(*this,"B",wxButton);
513 InitWidgetsFromXRC();
518 The generated window class can be used as basis for the full window class. The
519 class members which represent widgets may be accessed by name instead of using
520 {\tt XRCCTRL} every time you wish to reference them (note that they are {\tt protected} class members),
521 though you must still use {\tt XRCID} to refer to widget ids in the event
527 #include "resource.h"
529 class TestWnd : public TestWnd_Base {
532 // A, B already initialised at this point
533 A->SetValue("Updated in TestWnd::TestWnd");
534 B->SetValue("Nice :)");
536 void OnBPressed(wxEvent& event){
539 DECLARE_EVENT_TABLE();
542 BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
543 EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
548 \subsection{Adding new resource handlers}\label{newresourcehandlers}