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, 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{XRCed
}{http://www.mema.ucl.ac.be/~rolinsky/xrced/
}, a wxPython-based
88 dialog editor that you can find in the
{\tt wxPython/tools
} subdirectory of the wxWindows
90 \item use
\urlref{wxWorkshop
}{http://wxworkshop.sourceforge.net
} (under development);
91 \item use wxrcedit (
{\tt utils/contrib/wxrcedit
}) (under development);
92 \item convert WIN32 RC files to XRC with the tool in
{\tt contrib/utils/convertrc
}.
95 It is highly recommended that you use a tool such as wxDesigner, since it's fiddly writing
98 You can use
\helpref{wxXmlResource::Load
}{wxxmlresourceload
} in a number of ways.
99 You can pass an XRC file (XML-based text resource file)
100 or a
\helpref{zip-compressed file
}{binaryresourcefiles
} (extension ZIP or XRS) containing other XRC.
102 You can also use
\helpref{embedded C++ resources
}{embeddedresource
}
104 \subsection{Using binary resource files
}\label{binaryresourcefiles
}
106 To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
107 (the input XRC files) and the following switches and options:
108 \begin{itemize
}\itemsep=
0pt
109 \item -h (--help): show a help message
110 \item -v (--verbose): show verbose logging information
111 \item -c (--cpp-code): write C++ source rather than a XRS file
112 \item -u (--uncompressed): do not compress XML files (C++ only)
113 \item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
114 \item -n (--function) <name>: specify C++ function name (use with -c)
115 \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
116 \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
122 % wxrc resource.wrc -o resource.wrs
123 % wxrc resource.wrc -v -c -o resource.cpp
128 XRS file is esentially a renamed ZIP archive which means that you can manipulate
129 it with standard ZIP tools. Note that if you are using XRS files, you have
130 to initialize
\helpref{wxFileSystem
}{wxfilesystem
} ZIP handler first! It is a simple
133 #include <wx/filesys.h>
134 #include <wx/fs_zip.h>
136 wxFileSystem::AddHandler(new wxZipFSHandler);
139 \subsection{Using embedded resources
}\label{embeddedresource
}
141 It is sometimes useful to embed resources in the executable itself instead
142 of loading external file (e.g. when your app is small and consists only of one
143 exe file). XRC provides means to convert resources into regular C++ file that
144 can be compiled and included in the executable.
146 Use the
{\tt -c
} switch to
147 {\tt wxrc
} utility to produce C++ file with embedded resources. This file will
148 contain a function called
{\it InitXmlResource
} (unless you override this with
149 a command line switch). Use it to load the resource:
151 wxXmlResource::Get()->InitAllHandlers();
156 \subsection{XRC C++ sample
}\label{xrccppsample
}
158 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
162 #include "wx/image.h"
163 #include "wx/xrc/xmlres.h"
165 // the application icon
166 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
167 #include "rc/appicon.xpm"
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 // Define a new application type, each program should derive a class from wxApp
175 class MyApp : public wxApp
178 // override base class virtuals
179 // ----------------------------
181 // this one is called on application startup and is a good place for the app
182 // initialization (doing it here and not in the ctor allows to have an error
183 // return: if OnInit() returns false, the application terminates)
184 virtual bool OnInit();
187 // Define a new frame type: this is going to be our main frame
188 class MyFrame : public wxFrame
192 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
194 // event handlers (these functions should _not_ be virtual)
195 void OnQuit(wxCommandEvent& event);
196 void OnAbout(wxCommandEvent& event);
197 void OnDlg1(wxCommandEvent& event);
198 void OnDlg2(wxCommandEvent& event);
201 // any class wishing to process wxWindows events must use this macro
202 DECLARE_EVENT_TABLE()
205 // ----------------------------------------------------------------------------
206 // event tables and other macros for wxWindows
207 // ----------------------------------------------------------------------------
209 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
210 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
211 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
212 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
213 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
218 // ----------------------------------------------------------------------------
219 // the application class
220 // ----------------------------------------------------------------------------
222 // 'Main program' equivalent: the program execution "starts" here
225 wxImage::AddHandler(new wxGIFHandler);
226 wxXmlResource::Get()->InitAllHandlers();
227 wxXmlResource::Get()->Load("rc/resource.xrc");
229 MyFrame *frame = new MyFrame("XML resources demo",
230 wxPoint(
50,
50), wxSize(
450,
340));
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
240 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
241 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
243 SetIcon(wxICON(appicon));
245 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
246 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
250 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
252 // true is to force the frame to close
256 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
259 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
260 _T("Welcome to
%s"), wxVERSION_STRING);
262 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
265 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
268 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
272 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
275 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
280 \subsection{XRC resource file sample
}\label{xrcsample
}
282 This is the XML file (resource.xrc) for the XRC sample.
285 <?xml version="
1.0"?>
286 <resource version="
2.3.0.1">
287 <object class="wxMenuBar" name="mainmenu">
288 <style>wxMB_DOCKABLE</style>
289 <object class="wxMenu" name="menu_file">
291 <style>wxMENU_TEAROFF</style>
292 <object class="wxMenuItem" name="menu_about">
293 <label>_About...</label>
294 <bitmap>filesave.gif</bitmap>
296 <object class="separator"/>
297 <object class="wxMenuItem" name="menu_dlg1">
298 <label>Dialog
1</label>
300 <object class="wxMenuItem" name="menu_dlg2">
301 <label>Dialog
2</label>
303 <object class="separator"/>
304 <object class="wxMenuItem" name="menu_quit">
305 <label>E_xit
\tAlt-X</label>
309 <object class="wxToolBar" name="toolbar">
310 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
311 <margins>
2,
2</margins>
312 <object class="tool" name="menu_open">
313 <bitmap>fileopen.gif</bitmap>
314 <tooltip>Open catalog</tooltip>
316 <object class="tool" name="menu_save">
317 <bitmap>filesave.gif</bitmap>
318 <tooltip>Save catalog</tooltip>
320 <object class="tool" name="menu_update">
321 <bitmap>update.gif</bitmap>
322 <tooltip>Update catalog - synchronize it with sources</tooltip>
325 <object class="tool" name="menu_quotes">
326 <bitmap>quotes.gif</bitmap>
328 <tooltip>Display quotes around the string?</tooltip>
330 <object class="separator"/>
331 <object class="tool" name="menu_fuzzy">
332 <bitmap>fuzzy.gif</bitmap>
333 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
337 <object class="wxDialog" name="dlg1">
338 <object class="wxBoxSizer">
339 <object class="sizeritem">
340 <object class="wxBitmapButton">
341 <bitmap>fuzzy.gif</bitmap>
342 <focus>fileopen.gif</focus>
345 <object class="sizeritem">
346 <object class="wxPanel">
347 <object class="wxStaticText">
348 <label>fdgdfgdfgdfg</label>
350 <style>wxSUNKEN_BORDER</style>
352 <flag>wxALIGN_CENTER</flag>
354 <object class="sizeritem">
355 <object class="wxButton">
356 <label>Buttonek</label>
361 <object class="sizeritem">
362 <object class="wxHtmlWindow">
363 <htmlcode><h1>Hi,</h1>man</htmlcode>
367 <object class="sizeritem">
368 <object class="wxNotebook">
369 <object class="notebookpage">
370 <object class="wxPanel">
371 <object class="wxBoxSizer">
372 <object class="sizeritem">
373 <object class="wxHtmlWindow">
374 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
383 <object class="notebookpage">
384 <object class="wxPanel">
385 <object class="wxBoxSizer">
386 <object class="sizeritem">
387 <object class="wxHtmlWindow">
388 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
394 <label>Page
2</label>
396 <usenotebooksizer>
1</usenotebooksizer>
398 <flag>wxEXPAND</flag>
400 <orient>wxVERTICAL</orient>
403 <object class="wxDialog" name="dlg2">
404 <object class="wxBoxSizer">
405 <orient>wxVERTICAL</orient>
406 <object class="sizeritem" name="dfgdfg">
407 <object class="wxTextCtrl">
408 <size>
200,
200d</size>
409 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
410 <value>Hello, this is an ordinary multiline
\n textctrl....</value>
413 <flag>wxEXPAND|wxALL</flag>
416 <object class="sizeritem">
417 <object class="wxBoxSizer">
418 <object class="sizeritem">
419 <object class="wxButton" name="wxID_OK">
424 <object class="sizeritem">
425 <object class="wxButton" name="wxID_CANCEL">
426 <label>Cancel</label>
432 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
436 <title>Second testing dialog</title>
441 \subsection{XRC file format
}\label{xrcfileformat
}
443 Please see Technical Note
14 (docs/tech/tn0014.txt) in your wxWindows
446 \subsection{Adding new resource handlers
}\label{newresourcehandlers
}