]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/xrc.h
749d539dda3e10f4bb9b0986a103495971c47f92
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 @page overview_xrc XML-based resource system overview
13 Classes: #wxXmlResource, #wxXmlResourceHandler
14 The XML-based resource system, known as XRC, allows user interface elements such as
15 dialogs, menu bars and toolbars, to be stored in text files and loaded into
16 the application at run-time. XRC files can also be compiled into binary XRS files or C++
17 code (the former makes it possible to store all resources in a single file and the latter
18 is useful when you want to embed the resources into the executable).
20 There are several advantages to using XRC resources.
22 @li Recompiling and linking an application is not necessary if the
24 @li If you use a dialog designer that generates C++ code, it can be hard
25 to reintegrate this into existing C++ code. Separation of resources and code
26 is a more elegant solution.
27 @li You can choose between different alternative resource files at run time, if necessary.
28 @li The XRC format uses sizers for flexibility, allowing dialogs to be resizable
30 @li The XRC format is a wxWidgets standard,
31 and can be generated or postprocessed by any program that understands it. As it is based
32 on the XML standard, existing XML editors can be used for simple editing purposes.
35 XRC was written by Vaclav Slavik.
36 @li @ref overview_xrcconcepts
37 @li @ref overview_binaryresourcefiles
38 @li @ref overview_embeddedresource
39 @li @ref overview_xrccppsample
40 @li @ref overview_xrcsample
41 @li @ref overview_xrcfileformat
42 @li @ref overview_xrccppheader
43 @li @ref overview_newresourcehandlers
46 @section overview_xrcconcepts XRC concepts
48 These are the typical steps for using XRC files in your application.
51 @li Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
52 @li If you are going to use @ref binaryresourcefiles_overview, install
53 wxFileSystem archive handler first with @c wxFileSystem::AddHandler(new wxArchiveFSHandler);
54 @li call @c wxXmlResource::Get()-InitAllHandlers() from your wxApp::OnInit function,
55 and then call @c wxXmlResource::Get()-Load("myfile.xrc") to load the resource file;
56 @li to create a dialog from a resource, create it using the default constructor, and then
57 load it using for example @c wxXmlResource::Get()-LoadDialog(dlg, this, "dlg1");
58 @li set up event tables as usual but use the @c XRCID(str) macro to translate from XRC string names
59 to a suitable integer identifier, for example @c EVT_MENU(XRCID("quit"), MyFrame::OnQuit).
61 To create an XRC file, you can use one of the following methods.
63 @li Create the file by hand;
64 @li use #wxDesigner, a commercial dialog designer/RAD tool;
65 @li use #DialogBlocks, a commercial dialog editor;
66 @li use #XRCed, a wxPython-based
67 dialog editor that you can find in the @c wxPython/tools subdirectory of the wxWidgets
69 @li use #wxGlade, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
72 A complete list of third-party tools that write to XRC can be found at #www.wxwidgets.org/lnk_tool.htm.
74 It is highly recommended that you use a resource editing tool, since it's fiddly writing
77 You can use wxXmlResource::Load in a number of ways.
78 You can pass an XRC file (XML-based text resource file)
79 or a @ref binaryresourcefiles_overview (extension ZIP or XRS) containing other XRC.
81 You can also use @ref embeddedresource_overview
83 @section overview_binaryresourcefiles Using binary resource files
85 To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
86 (the input XRC files) and the following switches and options:
88 @li -h (--help): show a help message
89 @li -v (--verbose): show verbose logging information
90 @li -c (--cpp-code): write C++ source rather than a XRS file
91 @li -e (--extra-cpp-code): if used together with -c, generates C++ header file
92 containing class definitions for the windows defined by the XRC file (see special subsection)
93 @li -u (--uncompressed): do not compress XML files (C++ only)
94 @li -g (--gettext): output underscore-wrapped strings that poEdit or gettext can scan. Outputs to stdout, or a file if -o is used
95 @li -n (--function) name: specify C++ function name (use with -c)
96 @li -o (--output) filename: specify the output file, such as resource.xrs or resource.cpp
97 @li -l (--list-of-handlers) filename: output a list of necessary handlers to this file
104 % wxrc resource.xrc -o resource.xrs
105 % wxrc resource.xrc -v -c -o resource.cpp
109 XRS file is essentially a renamed ZIP archive which means that you can manipulate
110 it with standard ZIP tools. Note that if you are using XRS files, you have
111 to initialize the #wxFileSystem archive handler first! It is a simple
114 #include wx/filesys.h
117 wxFileSystem::AddHandler(new wxArchiveFSHandler);
121 @section overview_embeddedresource Using embedded resources
123 It is sometimes useful to embed resources in the executable itself instead
124 of loading an external file (e.g. when your app is small and consists only of one
125 exe file). XRC provides means to convert resources into regular C++ file that
126 can be compiled and included in the executable.
128 Use the @c -c switch to
129 @c wxrc utility to produce C++ file with embedded resources. This file will
130 contain a function called @e InitXmlResource (unless you override this with
131 a command line switch). Use it to load the resource:
134 extern void InitXmlResource(); // defined in generated file
136 wxXmlResource::Get()-InitAllHandlers();
142 @section overview_xrccppsample XRC C++ sample
144 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
148 #include "wx/image.h"
149 #include "wx/xrc/xmlres.h"
151 // the application icon
152 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
153 #include "rc/appicon.xpm"
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 // Define a new application type, each program should derive a class from wxApp
161 class MyApp : public wxApp
164 // override base class virtuals
165 // ----------------------------
167 // this one is called on application startup and is a good place for the app
168 // initialization (doing it here and not in the ctor allows to have an error
169 // return: if OnInit() returns @false, the application terminates)
170 virtual bool OnInit();
173 // Define a new frame type: this is going to be our main frame
174 class MyFrame : public wxFrame
178 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
180 // event handlers (these functions should _not_ be virtual)
181 void OnQuit(wxCommandEvent& event);
182 void OnAbout(wxCommandEvent& event);
183 void OnDlg1(wxCommandEvent& event);
184 void OnDlg2(wxCommandEvent& event);
187 // any class wishing to process wxWidgets events must use this macro
188 DECLARE_EVENT_TABLE()
191 // ----------------------------------------------------------------------------
192 // event tables and other macros for wxWidgets
193 // ----------------------------------------------------------------------------
195 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
196 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
197 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
198 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
199 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
204 // ----------------------------------------------------------------------------
205 // the application class
206 // ----------------------------------------------------------------------------
208 // 'Main program' equivalent: the program execution "starts" here
211 wxImage::AddHandler(new wxGIFHandler);
212 wxXmlResource::Get()-InitAllHandlers();
213 wxXmlResource::Get()-Load("rc/resource.xrc");
215 MyFrame *frame = new MyFrame("XML resources demo",
216 wxPoint(50, 50), wxSize(450, 340));
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
226 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
227 : wxFrame((wxFrame *)@NULL, -1, title, pos, size)
229 SetIcon(wxICON(appicon));
231 SetMenuBar(wxXmlResource::Get()-LoadMenuBar("mainmenu"));
232 SetToolBar(wxXmlResource::Get()-LoadToolBar(this, "toolbar"));
236 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
238 // @true is to force the frame to close
242 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
245 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
246 _T("Welcome to %s"), wxVERSION_STRING);
248 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
251 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
254 wxXmlResource::Get()-LoadDialog(, this, "dlg1");
258 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
261 wxXmlResource::Get()-LoadDialog(, this, "dlg2");
267 @section overview_xrcsample XRC resource file sample
269 This is the XML file (resource.xrc) for the XRC sample.
273 resource version="2.3.0.1"
274 object class="wxMenuBar" name="mainmenu"
275 stylewxMB_DOCKABLE/style
276 object class="wxMenu" name="menu_file"
278 stylewxMENU_TEAROFF/style
279 object class="wxMenuItem" name="menu_about"
281 bitmapfilesave.gif/bitmap
283 object class="separator"/
284 object class="wxMenuItem" name="menu_dlg1"
287 object class="wxMenuItem" name="menu_dlg2"
290 object class="separator"/
291 object class="wxMenuItem" name="menu_quit"
292 labelE_xit\tAlt-X/label
296 object class="wxToolBar" name="toolbar"
297 stylewxTB_FLAT|wxTB_DOCKABLE/style
299 object class="tool" name="menu_open"
300 bitmapfileopen.gif/bitmap
301 tooltipOpen catalog/tooltip
303 object class="tool" name="menu_save"
304 bitmapfilesave.gif/bitmap
305 tooltipSave catalog/tooltip
307 object class="tool" name="menu_update"
308 bitmapupdate.gif/bitmap
309 tooltipUpdate catalog - synchronize it with sources/tooltip
312 object class="tool" name="menu_quotes"
313 bitmapquotes.gif/bitmap
315 tooltipDisplay quotes around the string?/tooltip
317 object class="separator"/
318 object class="tool" name="menu_fuzzy"
319 bitmapfuzzy.gif/bitmap
320 tooltipToggled if selected string is fuzzy translation/tooltip
324 object class="wxDialog" name="dlg1"
325 object class="wxBoxSizer"
326 object class="sizeritem"
327 object class="wxBitmapButton"
328 bitmapfuzzy.gif/bitmap
329 focusfileopen.gif/focus
332 object class="sizeritem"
333 object class="wxPanel"
334 object class="wxStaticText"
335 labelfdgdfgdfgdfg/label
337 stylewxBORDER\_SUNKEN/style
339 flagwxALIGN_CENTER/flag
341 object class="sizeritem"
342 object class="wxButton"
348 object class="sizeritem"
349 object class="wxHtmlWindow"
350 htmlcodeh1Hi,/h1man/htmlcode
354 object class="sizeritem"
355 object class="wxNotebook"
356 object class="notebookpage"
357 object class="wxPanel"
358 object class="wxBoxSizer"
359 object class="sizeritem"
360 object class="wxHtmlWindow"
361 htmlcodeHello, we are inside a uNOTEBOOK/u.../htmlcode
370 object class="notebookpage"
371 object class="wxPanel"
372 object class="wxBoxSizer"
373 object class="sizeritem"
374 object class="wxHtmlWindow"
375 htmlcodeHello, we are inside a uNOTEBOOK/u.../htmlcode
383 usenotebooksizer1/usenotebooksizer
387 orientwxVERTICAL/orient
390 object class="wxDialog" name="dlg2"
391 object class="wxBoxSizer"
392 orientwxVERTICAL/orient
393 object class="sizeritem" name="dfgdfg"
394 object class="wxTextCtrl"
396 stylewxTE_MULTILINE|wxBORDER_SUNKEN/style
397 valueHello, this is an ordinary multiline\n textctrl..../value
400 flagwxEXPAND|wxALL/flag
403 object class="sizeritem"
404 object class="wxBoxSizer"
405 object class="sizeritem"
406 object class="wxButton" name="wxID_OK"
411 object class="sizeritem"
412 object class="wxButton" name="wxID_CANCEL"
419 flagwxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT/flag
423 titleSecond testing dialog/title
429 @section overview_xrcfileformat XRC file format
431 Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWidgets
434 @section overview_xrccppheader C++ header file generation
436 Using the @c -e switch together with @c -c, a C++ header file is written
437 containing class definitions for the GUI windows defined in the XRC file.
438 This code generation can make it easier to use XRC and automate program
440 The classes can be used as basis for development, freeing the
441 programmer from dealing with most of the XRC specifics (e.g. @c XRCCTRL).
443 For each top level window defined in the XRC file a C++ class definition is
444 generated, containing as class members the named widgets of the window.
445 A default constructor for each class is also generated. Inside the constructor
446 all XRC loading is done and all class members representing widgets are initialized.
448 A simple example will help understand how the scheme works. Suppose you have
449 a XRC file defining a top level window @c TestWnd_Base, which subclasses @c wxFrame (any
450 other class like @c wxDialog will do also), and has subwidgets @c wxTextCtrl A and @c wxButton B.
451 The XRC file and corresponding class definition in the header file will be something like:
455 resource version="2.3.0.1"
456 object class="wxFrame" name="TestWnd_Base"
459 object class="wxBoxSizer"
460 orientwxHORIZONTAL/orient
461 object class="sizeritem"
462 object class="wxTextCtrl" name="A"
463 labelTest label/label
466 object class="sizeritem"
467 object class="wxButton" name="B"
468 labelTest button/label
476 class TestWnd_Base : public wxFrame {
482 void InitWidgetsFromXRC(){
483 wxXmlResource::Get()-LoadObject(this,@NULL,"TestWnd","wxFrame");
484 A = XRCCTRL(*this,"A",wxTextCtrl);
485 B = XRCCTRL(*this,"B",wxButton);
489 InitWidgetsFromXRC();
494 The generated window class can be used as basis for the full window class. The
495 class members which represent widgets may be accessed by name instead of using
496 @c XRCCTRL every time you wish to reference them (note that they are @c protected class members),
497 though you must still use @c XRCID to refer to widget IDs in the event
502 #include "resource.h"
504 class TestWnd : public TestWnd_Base {
507 // A, B already initialised at this point
508 A-SetValue("Updated in TestWnd::TestWnd");
509 B-SetValue("Nice :)");
511 void OnBPressed(wxEvent& event){
514 DECLARE_EVENT_TABLE();
517 BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
518 EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
522 It is also possible to access the wxSizerItem of a sizer that is part of
523 a resource. This can be done using @c XRCSIZERITEM as shown. The
524 resource file can have something like this for a sizer item.
527 object class="spacer" name="area"
532 The code can then access the sizer item by using @c XRCSIZERITEM and
536 wxSizerItem* item = XRCSIZERITEM(*this, "area");
540 @section overview_newresourcehandlers Adding new resource handlers
542 Adding a new resource handler is pretty easy.
543 Typically, to add an handler for the @c MyControl class, you'll want to create
544 the @c xh_mycontrol.h @c xh_mycontrol.cpp files.
546 The header needs to contains the @c MyControlXmlHandler class definition:
549 class MyControlXmlHandler : public wxXmlResourceHandler
554 MyControlXmlHandler();
556 // Creates the control and returns a pointer to it.
557 virtual wxObject *DoCreateResource();
559 // Returns @true if we know how to create a control for the given node.
560 virtual bool CanHandle(wxXmlNode *node);
562 // Register with wxWidgets' dynamic class subsystem.
563 DECLARE_DYNAMIC_CLASS(MyControlXmlHandler)
567 The implementation of your custom XML handler will typically look as:
570 // Register with wxWidgets' dynamic class subsystem.
571 IMPLEMENT_DYNAMIC_CLASS(MyControlXmlHandler, wxXmlResourceHandler)
573 MyControlXmlHandler::MyControlXmlHandler()
575 // this call adds support for all wxWindows class styles
576 // (e.g. wxBORDER_SIMPLE, wxBORDER_SUNKEN, wxWS_EX_* etc etc)
579 // if MyControl class supports e.g. MYCONTROL_DEFAULT_STYLE
581 // XRC_ADD_STYLE(MYCONTROL_DEFAULT_STYLE);
584 wxObject *MyControlXmlHandler::DoCreateResource()
586 // the following macro will init a pointer named "control"
587 // with a new instance of the MyControl class, but will NOT
589 XRC_MAKE_INSTANCE(control, MyControl)
591 // this is the point where you'll typically need to do the most
592 // important changes: here the control is created and initialized.
593 // You'll want to use the wxXmlResourceHandler's getters to
594 // do most of your work.
595 // If e.g. the MyControl::Create function looks like:
597 // bool MyControl::Create(wxWindow *parent, int id,
598 // const wxBitmap , const wxPoint ,
599 // const wxBitmap , const wxPoint ,
600 // const wxString , const wxFont ,
601 // const wxPoint , const wxSize ,
602 // long style = MYCONTROL_DEFAULT_STYLE,
603 // const wxString = wxT("MyControl"));
605 // then the XRC for your component should look like:
607 // object class="MyControl" name="some_name"
608 // first-bitmapfirst.xpm/first-bitmap
609 // second-bitmaptext.xpm/second-bitmap
610 // first-pos3,3/first-pos
611 // second-pos4,4/second-pos
612 // the-titlea title/the-title
614 // !-- the standard XRC tags for describing a font: size, style, weight, etc --
616 // !-- XRC also accepts other usual tags for wxWindow-derived classes:
617 // like e.g. name, style, size, position, etc --
620 // and the code to read your custom tags from the XRC file is just:
621 control-Create(m_parentAsWindow, GetID(),
622 GetBitmap(wxT("first-bitmap")),
623 GetPosition(wxT("first-pos")),
624 GetBitmap(wxT("second-bitmap")),
625 GetPosition(wxT("second-pos")),
626 GetText(wxT("the-title")),
627 GetFont(wxT("title-font")),
628 GetPosition(), GetSize(), GetStyle(), GetName());
630 SetupWindow(control);
635 bool MyControlXmlHandler::CanHandle(wxXmlNode *node)
637 // this function tells XRC system that this handler can parse
638 // the object class="MyControl" tags
639 return IsOfClass(node, wxT("MyControl"));
643 You may want to check the #wxXmlResourceHandler documentation
644 to see how many built-in getters it contains. It's very easy to retrieve also complex structures
645 out of XRC files using them.