1 \section{XML-based resource system overview
}\label{xrcoverview
}
3 Classes:
\helpref{wxXmlResource
}{wxxmlresource
},
\helpref{wxXmlResourceHandler
}{wxxmlresourcehandler
}
5 {\bf IMPORTANT NOTE:
} XRC is not yet a part of the core wxWindows library, so
6 please see the next section for how to compile and link it. Otherwise if you
7 try to use it, you will get link errors.
9 The XML-based resource system, known as XRC, allows user interface elements such as
10 dialogs, menu bars and toolbars, to be stored in text files and loaded into
11 the application at run-time. XRC files can also be compiled into binary XRS files or C++
12 code (the former makes it possible to store all resources in since file and the latter
13 is useful when you want to embed the resources into the executable).
15 There are several advantages to using XRC resources.
17 \begin{itemize
}\itemsep=
0pt
18 \item Recompiling and linking an application is not necessary if the
20 \item If you use a dialog designers that generates C++ code, it can be hard
21 to reintegrate this into existing C++ code. Separation of resources and code
22 is a more elegant solution.
23 \item You can choose between different alternative resource files at run time, if necessary.
24 \item The XRC format uses sizers for flexibility, allowing dialogs to be resizable
26 \item The XRC format is a wxWindows standard,
27 and can be generated or postprocessed by any program that understands it. As it is based
28 on the XML standard, existing XML editors can be used for simple editing purposes.
31 XRC was written by Vaclav Slavik.
33 \subsection{Compiling and using XRC
}\label{compilingxrc
}
35 XRC can be found under the 'contrib' hierarchy, in the following directories:
38 contrib/src/xrc ; XRC source
39 contrib/include/wx/xrc ; XRC headers
40 contrib/samples/xrc ; XRC sample
41 contrib/utils/wxrc ; XRC resource compiler
42 contrib/utils/wxrcedit ; XRC editor (in progress)
47 \begin{itemize
}\itemsep=
0pt
48 \item Under Windows using VC++, open the contrib/src/xrc/XrcVC.dsw project
49 and compile. Also compile contrib/utils/wxrc using wxBase if you wish to compile
51 \item Under Unix, XRC should be configured when you configured
52 wxWindows. Make XRC by changing directory to contrib/src/xrc and
53 type 'make'. Similarly compile contrib/utils/wxrc using wxBase if you wish to compile
54 resource files.
{\bf Note:
} there is currently a
55 problem with the wxWindows build system that means that
56 only the static version of library can be built at present.
61 \begin{itemize
}\itemsep=
0pt
62 \item Under Windows using VC++, link with wxxrc
[d
].lib.
63 \item Under Unix, link with libwxxrc
[d
].a.
66 \subsection{XRC concepts
}\label{xrcconcepts
}
68 These are the typical steps for using XRC files in your application.
70 \begin{itemize
}\itemsep=
0pt
71 \item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
72 \item If you are going to use
\helpref{XRS files
}{binaryresourcefiles
}, install
73 wxFileSystem ZIP handler first with
{\tt wxFileSystem::AddHandler(new wxZipFSHandler);
}
74 \item call
{\tt wxXmlResource::Get()->InitAllHandlers()
} from your wxApp::OnInit function,
75 and then call
{\tt wxXmlResource::Get()->Load("myfile.xrc")
} to load the resource file;
76 \item to create a dialog from a resource, create it using the default constructor, and then
77 load using for example
{\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");
}
78 \item set up event tables as usual but use the
{\tt XRCID(str)
} macro to translate from XRC string names
79 to a suitable integer identifier, for example
{\tt EVT
\_MENU(XRCID("quit"), MyFrame::OnQuit)
}.
82 To create an XRC file, you can use one of the following methods.
84 \begin{itemize
}\itemsep=
0pt
85 \item Create the file by hand;
86 \item use
\urlref{wxDesigner
}{http://www.roebling.de
}, a commercial dialog designer/RAD tool;
87 \item use
\urlref{DialogBlocks
}{http://www.anthemion.co.uk/dialogblocks
}, a commercial dialog editor;
88 \item use
\urlref{XRCed
}{http://xrced.sf.net
}, a wxPython-based
89 dialog editor that you can find in the
{\tt wxPython/tools
} subdirectory of the wxWindows
91 \item use
\urlref{Glade
}{http://wxglade.sf.net
}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
92 \item use wxrcedit (
{\tt utils/contrib/wxrcedit
}) (under development);
93 \item convert WIN32 RC files to XRC with the tool in
{\tt contrib/utils/convertrc
}.
96 A 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
}.
98 It is highly recommended that you use a resource editing tool, since it's fiddly writing
101 You can use
\helpref{wxXmlResource::Load
}{wxxmlresourceload
} in a number of ways.
102 You can pass an XRC file (XML-based text resource file)
103 or a
\helpref{zip-compressed file
}{binaryresourcefiles
} (extension ZIP or XRS) containing other XRC.
105 You can also use
\helpref{embedded C++ resources
}{embeddedresource
}
107 \subsection{Using binary resource files
}\label{binaryresourcefiles
}
109 To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
110 (the input XRC files) and the following switches and options:
111 \begin{itemize
}\itemsep=
0pt
112 \item -h (--help): show a help message
113 \item -v (--verbose): show verbose logging information
114 \item -c (--cpp-code): write C++ source rather than a XRS file
115 \item -u (--uncompressed): do not compress XML files (C++ only)
116 \item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
117 \item -n (--function) <name>: specify C++ function name (use with -c)
118 \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
119 \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
125 % wxrc resource.wrc -o resource.wrs
126 % wxrc resource.wrc -v -c -o resource.cpp
131 XRS file is esentially a renamed ZIP archive which means that you can manipulate
132 it with standard ZIP tools. Note that if you are using XRS files, you have
133 to initialize
\helpref{wxFileSystem
}{wxfilesystem
} ZIP handler first! It is a simple
136 #include <wx/filesys.h>
137 #include <wx/fs_zip.h>
139 wxFileSystem::AddHandler(new wxZipFSHandler);
142 \subsection{Using embedded resources
}\label{embeddedresource
}
144 It is sometimes useful to embed resources in the executable itself instead
145 of loading external file (e.g. when your app is small and consists only of one
146 exe file). XRC provides means to convert resources into regular C++ file that
147 can be compiled and included in the executable.
149 Use the
{\tt -c
} switch to
150 {\tt wxrc
} utility to produce C++ file with embedded resources. This file will
151 contain a function called
{\it InitXmlResource
} (unless you override this with
152 a command line switch). Use it to load the resource:
154 extern void InitXMLResource(); // defined in generated file
156 wxXmlResource::Get()->InitAllHandlers();
161 \subsection{XRC C++ sample
}\label{xrccppsample
}
163 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
167 #include "wx/image.h"
168 #include "wx/xrc/xmlres.h"
170 // the application icon
171 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
172 #include "rc/appicon.xpm"
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 // Define a new application type, each program should derive a class from wxApp
180 class MyApp : public wxApp
183 // override base class virtuals
184 // ----------------------------
186 // this one is called on application startup and is a good place for the app
187 // initialization (doing it here and not in the ctor allows to have an error
188 // return: if OnInit() returns false, the application terminates)
189 virtual bool OnInit();
192 // Define a new frame type: this is going to be our main frame
193 class MyFrame : public wxFrame
197 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
199 // event handlers (these functions should _not_ be virtual)
200 void OnQuit(wxCommandEvent& event);
201 void OnAbout(wxCommandEvent& event);
202 void OnDlg1(wxCommandEvent& event);
203 void OnDlg2(wxCommandEvent& event);
206 // any class wishing to process wxWindows events must use this macro
207 DECLARE_EVENT_TABLE()
210 // ----------------------------------------------------------------------------
211 // event tables and other macros for wxWindows
212 // ----------------------------------------------------------------------------
214 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
215 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
216 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
217 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
218 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
223 // ----------------------------------------------------------------------------
224 // the application class
225 // ----------------------------------------------------------------------------
227 // 'Main program' equivalent: the program execution "starts" here
230 wxImage::AddHandler(new wxGIFHandler);
231 wxXmlResource::Get()->InitAllHandlers();
232 wxXmlResource::Get()->Load("rc/resource.xrc");
234 MyFrame *frame = new MyFrame("XML resources demo",
235 wxPoint(
50,
50), wxSize(
450,
340));
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
245 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
246 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
248 SetIcon(wxICON(appicon));
250 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
251 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
255 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
257 // true is to force the frame to close
261 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
264 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
265 _T("Welcome to
%s"), wxVERSION_STRING);
267 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
270 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
273 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
277 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
280 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
285 \subsection{XRC resource file sample
}\label{xrcsample
}
287 This is the XML file (resource.xrc) for the XRC sample.
290 <?xml version="
1.0"?>
291 <resource version="
2.3.0.1">
292 <object class="wxMenuBar" name="mainmenu">
293 <style>wxMB_DOCKABLE</style>
294 <object class="wxMenu" name="menu_file">
296 <style>wxMENU_TEAROFF</style>
297 <object class="wxMenuItem" name="menu_about">
298 <label>_About...</label>
299 <bitmap>filesave.gif</bitmap>
301 <object class="separator"/>
302 <object class="wxMenuItem" name="menu_dlg1">
303 <label>Dialog
1</label>
305 <object class="wxMenuItem" name="menu_dlg2">
306 <label>Dialog
2</label>
308 <object class="separator"/>
309 <object class="wxMenuItem" name="menu_quit">
310 <label>E_xit
\tAlt-X</label>
314 <object class="wxToolBar" name="toolbar">
315 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
316 <margins>
2,
2</margins>
317 <object class="tool" name="menu_open">
318 <bitmap>fileopen.gif</bitmap>
319 <tooltip>Open catalog</tooltip>
321 <object class="tool" name="menu_save">
322 <bitmap>filesave.gif</bitmap>
323 <tooltip>Save catalog</tooltip>
325 <object class="tool" name="menu_update">
326 <bitmap>update.gif</bitmap>
327 <tooltip>Update catalog - synchronize it with sources</tooltip>
330 <object class="tool" name="menu_quotes">
331 <bitmap>quotes.gif</bitmap>
333 <tooltip>Display quotes around the string?</tooltip>
335 <object class="separator"/>
336 <object class="tool" name="menu_fuzzy">
337 <bitmap>fuzzy.gif</bitmap>
338 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
342 <object class="wxDialog" name="dlg1">
343 <object class="wxBoxSizer">
344 <object class="sizeritem">
345 <object class="wxBitmapButton">
346 <bitmap>fuzzy.gif</bitmap>
347 <focus>fileopen.gif</focus>
350 <object class="sizeritem">
351 <object class="wxPanel">
352 <object class="wxStaticText">
353 <label>fdgdfgdfgdfg</label>
355 <style>wxSUNKEN_BORDER</style>
357 <flag>wxALIGN_CENTER</flag>
359 <object class="sizeritem">
360 <object class="wxButton">
361 <label>Buttonek</label>
366 <object class="sizeritem">
367 <object class="wxHtmlWindow">
368 <htmlcode><h1>Hi,</h1>man</htmlcode>
372 <object class="sizeritem">
373 <object class="wxNotebook">
374 <object class="notebookpage">
375 <object class="wxPanel">
376 <object class="wxBoxSizer">
377 <object class="sizeritem">
378 <object class="wxHtmlWindow">
379 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
388 <object class="notebookpage">
389 <object class="wxPanel">
390 <object class="wxBoxSizer">
391 <object class="sizeritem">
392 <object class="wxHtmlWindow">
393 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
399 <label>Page
2</label>
401 <usenotebooksizer>
1</usenotebooksizer>
403 <flag>wxEXPAND</flag>
405 <orient>wxVERTICAL</orient>
408 <object class="wxDialog" name="dlg2">
409 <object class="wxBoxSizer">
410 <orient>wxVERTICAL</orient>
411 <object class="sizeritem" name="dfgdfg">
412 <object class="wxTextCtrl">
413 <size>
200,
200d</size>
414 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
415 <value>Hello, this is an ordinary multiline
\n textctrl....</value>
418 <flag>wxEXPAND|wxALL</flag>
421 <object class="sizeritem">
422 <object class="wxBoxSizer">
423 <object class="sizeritem">
424 <object class="wxButton" name="wxID_OK">
429 <object class="sizeritem">
430 <object class="wxButton" name="wxID_CANCEL">
431 <label>Cancel</label>
437 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
441 <title>Second testing dialog</title>
446 \subsection{XRC file format
}\label{xrcfileformat
}
448 Please see Technical Note
14 (docs/tech/tn0014.txt) in your wxWindows
451 \subsection{Adding new resource handlers
}\label{newresourcehandlers
}