]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/txrc.tex
added and documented wxSize::IsFullySpecified() and SetDefaults()
[wxWidgets.git] / docs / latex / wx / txrc.tex
... / ...
CommitLineData
1% Note: -e/C++ header generation documentation added by
2% Eduardo Marques <edrdo@netcabo.pt>
3%
4\section{XML-based resource system overview}\label{xrcoverview}
5
6Classes: \helpref{wxXmlResource}{wxxmlresource}, \helpref{wxXmlResourceHandler}{wxxmlresourcehandler}
7
8{\bf IMPORTANT NOTE:} XRC is not yet a part of the core wxWindows library, so
9please see the next section for how to compile and link it. Otherwise if you
10try to use it, you will get link errors.
11
12The XML-based resource system, known as XRC, allows user interface elements such as
13dialogs, menu bars and toolbars, to be stored in text files and loaded into
14the application at run-time. XRC files can also be compiled into binary XRS files or C++
15code (the former makes it possible to store all resources in since file and the latter
16is useful when you want to embed the resources into the executable).
17
18There are several advantages to using XRC resources.
19
20\begin{itemize}\itemsep=0pt
21\item Recompiling and linking an application is not necessary if the
22resources change.
23\item If you use a dialog designers that generates C++ code, it can be hard
24to reintegrate this into existing C++ code. Separation of resources and code
25is 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
28and highly portable.
29\item The XRC format is a wxWindows standard,
30and can be generated or postprocessed by any program that understands it. As it is based
31on the XML standard, existing XML editors can be used for simple editing purposes.
32\end{itemize}
33
34XRC was written by Vaclav Slavik.
35
36\subsection{Compiling and using XRC}\label{compilingxrc}
37
38XRC can be found under the 'contrib' hierarchy, in the following directories:
39
40\begin{verbatim}
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)
46\end{verbatim}
47
48To compile XRC:
49
50\begin{itemize}\itemsep=0pt
51\item Under Windows using VC++, open the contrib/src/xrc/XrcVC.dsw project
52and compile. Also compile contrib/utils/wxrc using wxBase if you wish to compile
53resource files.
54\item Under Unix, XRC should be configured when you configured
55wxWindows. Make XRC by changing directory to contrib/src/xrc and
56type 'make'. Similarly compile contrib/utils/wxrc using wxBase if you wish to compile
57resource files. {\bf Note:} there is currently a
58problem with the wxWindows build system that means that
59only the static version of library can be built at present.
60\end{itemize}
61
62To use XRC:
63
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.
67\end{itemize}
68
69\subsection{XRC concepts}\label{xrcconcepts}
70
71These are the typical steps for using XRC files in your application.
72
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
76wxFileSystem ZIP handler first with {\tt wxFileSystem::AddHandler(new wxZipFSHandler);}
77\item call {\tt wxXmlResource::Get()->InitAllHandlers()} from your wxApp::OnInit function,
78and 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
80load 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
82to a suitable integer identifier, for example {\tt EVT\_MENU(XRCID("quit"), MyFrame::OnQuit)}.
83\end{itemize}
84
85To create an XRC file, you can use one of the following methods.
86
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
92dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWindows
93CVS archive;
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}.
97\end{itemize}
98
99A complete list of third-party tools that write to XRC can be found at \urlref{www.wxwindows.org/lnk\_tool.htm}{http://www.wxwindows.org/lnk\_tool.in}.
100
101It is highly recommended that you use a resource editing tool, since it's fiddly writing
102XRC files by hand.
103
104You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways.
105You can pass an XRC file (XML-based text resource file)
106or a \helpref{zip-compressed file}{binaryresourcefiles} (extension ZIP or XRS) containing other XRC.
107
108You can also use \helpref{embedded C++ resources}{embeddedresource}
109
110\subsection{Using binary resource files}\label{binaryresourcefiles}
111
112To 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
119containing 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
125\end{itemize}
126
127For example:
128\begin{verbatim}
129 % wxrc resource.wrc
130 % wxrc resource.wrc -o resource.wrs
131 % wxrc resource.wrc -v -c -o resource.cpp
132\end{verbatim}
133
134\wxheading{Note}
135
136XRS file is esentially a renamed ZIP archive which means that you can manipulate
137it with standard ZIP tools. Note that if you are using XRS files, you have
138to initialize \helpref{wxFileSystem}{wxfilesystem} ZIP handler first! It is a simple
139thing to do:
140
141\begin{verbatim}
142 #include <wx/filesys.h>
143 #include <wx/fs_zip.h>
144 ...
145 wxFileSystem::AddHandler(new wxZipFSHandler);
146\end{verbatim}
147
148\subsection{Using embedded resources}\label{embeddedresource}
149
150It is sometimes useful to embed resources in the executable itself instead
151of loading external file (e.g. when your app is small and consists only of one
152exe file). XRC provides means to convert resources into regular C++ file that
153can be compiled and included in the executable.
154
155Use the {\tt -c} switch to
156{\tt wxrc} utility to produce C++ file with embedded resources. This file will
157contain a function called {\it InitXmlResource} (unless you override this with
158a command line switch). Use it to load the resource:
159
160\begin{verbatim}
161 extern void InitXMLResource(); // defined in generated file
162 ...
163 wxXmlResource::Get()->InitAllHandlers();
164 InitXmlResource();
165 ...
166\end{verbatim}
167
168\subsection{XRC C++ sample}\label{xrccppsample}
169
170This is the C++ source file (xrcdemo.cpp) for the XRC sample.
171
172\begin{verbatim}
173#include "wx/wx.h"
174#include "wx/image.h"
175#include "wx/xrc/xmlres.h"
176
177// the application icon
178#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
179 #include "rc/appicon.xpm"
180#endif
181
182// ----------------------------------------------------------------------------
183// private classes
184// ----------------------------------------------------------------------------
185
186// Define a new application type, each program should derive a class from wxApp
187class MyApp : public wxApp
188{
189public:
190 // override base class virtuals
191 // ----------------------------
192
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();
197};
198
199// Define a new frame type: this is going to be our main frame
200class MyFrame : public wxFrame
201{
202public:
203 // ctor(s)
204 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
205
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);
211
212private:
213 // any class wishing to process wxWindows events must use this macro
214 DECLARE_EVENT_TABLE()
215};
216
217// ----------------------------------------------------------------------------
218// event tables and other macros for wxWindows
219// ----------------------------------------------------------------------------
220
221BEGIN_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)
226END_EVENT_TABLE()
227
228IMPLEMENT_APP(MyApp)
229
230// ----------------------------------------------------------------------------
231// the application class
232// ----------------------------------------------------------------------------
233
234// 'Main program' equivalent: the program execution "starts" here
235bool MyApp::OnInit()
236{
237 wxImage::AddHandler(new wxGIFHandler);
238 wxXmlResource::Get()->InitAllHandlers();
239 wxXmlResource::Get()->Load("rc/resource.xrc");
240
241 MyFrame *frame = new MyFrame("XML resources demo",
242 wxPoint(50, 50), wxSize(450, 340));
243 frame->Show(true);
244 return true;
245}
246
247// ----------------------------------------------------------------------------
248// main frame
249// ----------------------------------------------------------------------------
250
251// frame constructor
252MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
253 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
254{
255 SetIcon(wxICON(appicon));
256
257 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
258 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
259}
260
261// event handlers
262void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
263{
264 // true is to force the frame to close
265 Close(true);
266}
267
268void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
269{
270 wxString msg;
271 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
272 _T("Welcome to %s"), wxVERSION_STRING);
273
274 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
275}
276
277void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
278{
279 wxDialog dlg;
280 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
281 dlg.ShowModal();
282}
283
284void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
285{
286 wxDialog dlg;
287 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
288 dlg.ShowModal();
289}
290\end{verbatim}
291
292\subsection{XRC resource file sample}\label{xrcsample}
293
294This is the XML file (resource.xrc) for the XRC sample.
295
296\begin{verbatim}
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">
302 <label>_File</label>
303 <style>wxMENU_TEAROFF</style>
304 <object class="wxMenuItem" name="menu_about">
305 <label>_About...</label>
306 <bitmap>filesave.gif</bitmap>
307 </object>
308 <object class="separator"/>
309 <object class="wxMenuItem" name="menu_dlg1">
310 <label>Dialog 1</label>
311 </object>
312 <object class="wxMenuItem" name="menu_dlg2">
313 <label>Dialog 2</label>
314 </object>
315 <object class="separator"/>
316 <object class="wxMenuItem" name="menu_quit">
317 <label>E_xit\tAlt-X</label>
318 </object>
319 </object>
320 </object>
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>
327 </object>
328 <object class="tool" name="menu_save">
329 <bitmap>filesave.gif</bitmap>
330 <tooltip>Save catalog</tooltip>
331 </object>
332 <object class="tool" name="menu_update">
333 <bitmap>update.gif</bitmap>
334 <tooltip>Update catalog - synchronize it with sources</tooltip>
335 </object>
336 <separator/>
337 <object class="tool" name="menu_quotes">
338 <bitmap>quotes.gif</bitmap>
339 <toggle>1</toggle>
340 <tooltip>Display quotes around the string?</tooltip>
341 </object>
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>
346 <toggle>1</toggle>
347 </object>
348 </object>
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>
355 </object>
356 </object>
357 <object class="sizeritem">
358 <object class="wxPanel">
359 <object class="wxStaticText">
360 <label>fdgdfgdfgdfg</label>
361 </object>
362 <style>wxSUNKEN_BORDER</style>
363 </object>
364 <flag>wxALIGN_CENTER</flag>
365 </object>
366 <object class="sizeritem">
367 <object class="wxButton">
368 <label>Buttonek</label>
369 </object>
370 <border>10d</border>
371 <flag>wxALL</flag>
372 </object>
373 <object class="sizeritem">
374 <object class="wxHtmlWindow">
375 <htmlcode>&lt;h1&gt;Hi,&lt;/h1&gt;man</htmlcode>
376 <size>100,45d</size>
377 </object>
378 </object>
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 &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
387 <size>50,50d</size>
388 </object>
389 <option>1</option>
390 </object>
391 </object>
392 </object>
393 <label>Page</label>
394 </object>
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 &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
401 <size>50,50d</size>
402 </object>
403 </object>
404 </object>
405 </object>
406 <label>Page 2</label>
407 </object>
408 <usenotebooksizer>1</usenotebooksizer>
409 </object>
410 <flag>wxEXPAND</flag>
411 </object>
412 <orient>wxVERTICAL</orient>
413 </object>
414 </object>
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>
423 </object>
424 <option>1</option>
425 <flag>wxEXPAND|wxALL</flag>
426 <border>10</border>
427 </object>
428 <object class="sizeritem">
429 <object class="wxBoxSizer">
430 <object class="sizeritem">
431 <object class="wxButton" name="wxID_OK">
432 <label>Ok</label>
433 <default>1</default>
434 </object>
435 </object>
436 <object class="sizeritem">
437 <object class="wxButton" name="wxID_CANCEL">
438 <label>Cancel</label>
439 </object>
440 <border>10</border>
441 <flag>wxLEFT</flag>
442 </object>
443 </object>
444 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
445 <border>10</border>
446 </object>
447 </object>
448 <title>Second testing dialog</title>
449 </object>
450</resource>
451\end{verbatim}
452
453\subsection{XRC file format}\label{xrcfileformat}
454
455Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWindows
456distribution.
457
458\subsection{C++ header file generation}\label{xrccppheader}
459
460Using the {\tt -e} switch together with {\tt -c}, a C++ header file is written
461containing class definitions for the GUI windows defined in the XRC file.
462This code generation can make it easier to use XRC and automate program
463development.
464The classes can be used as basis for development, freeing the
465programmer from dealing with most of the XRC specifities (e.g. {\tt XRCCTRL}).
466
467For each top level window defined in the XRC file a C++ class definition is
468generated, containing as class members the named widgets of the window.
469A default constructor for each class is also generated. Inside the constructor
470all XRC loading is done and all class members representing widgets are initialized.
471
472A simple example will help understand how the scheme works. Suppose you have
473a XRC file defining a top level window {\tt TestWnd\_Base}, which subclasses {\tt wxFrame} (any
474other class like {\tt wxDialog} will do also), and has subwidgets {\tt wxTextCtrl} A and {\tt wxButton} B.
475The XRC file and corresponding class definition in the header file will be something like:
476
477\begin{verbatim}
478<?xml version="1.0"?>
479<resource version="2.3.0.1">
480 <object class="wxFrame" name="TestWnd_Base">
481 <size>-1,-1</size>
482 <title>Test</title>
483 <object class="wxBoxSizer">
484 <orient>wxHORIZONTAL</orient>
485 <object class="sizeritem">
486 <object class="wxTextCtrl" name="A">
487 <label>Test label</label>
488 </object>
489 </object>
490 <object class="sizeritem">
491 <object class="wxButton" name="B">
492 <label>Test button</label>
493 </object>
494 </object>
495 </object>
496 </object>
497</resource>
498
499
500class TestWnd_Base : public wxFrame {
501protected:
502 wxTextCtrl* A;
503 wxButton* B;
504
505private:
506 void InitWidgetsFromXRC(){
507 wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
508 A = XRCCTRL(*this,"A",wxTextCtrl);
509 B = XRCCTRL(*this,"B",wxButton);
510 }
511public:
512TestWnd::TestWnd(){
513 InitWidgetsFromXRC();
514 }
515};
516\end{verbatim}
517
518The generated window class can be used as basis for the full window class. The
519class 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),
521though you must still use {\tt XRCID} to refer to widget ids in the event
522table.
523
524Example:
525
526\begin{verbatim}
527#include "resource.h"
528
529class TestWnd : public TestWnd_Base {
530 public:
531 TestWnd(){
532 // A, B already initialised at this point
533 A->SetValue("Updated in TestWnd::TestWnd");
534 B->SetValue("Nice :)");
535 }
536 void OnBPressed(wxEvent& event){
537 Close();
538 }
539 DECLARE_EVENT_TABLE();
540};
541
542BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
543EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
544END_EVENT_TABLE()
545
546\end{verbatim}
547
548\subsection{Adding new resource handlers}\label{newresourcehandlers}
549
550Coming soon.
551