]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/xrc.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 @page xrc_overview 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).
19 There are several advantages to using XRC resources.
22 Recompiling and linking an application is not necessary if the
24 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 You can choose between different alternative resource files at run time, if necessary.
28 The XRC format uses sizers for flexibility, allowing dialogs to be resizable
30 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 @ref xrcconcepts_overview
37 @ref binaryresourcefiles_overview
38 @ref embeddedresource_overview
39 @ref xrccppsample_overview
40 @ref xrcsample_overview
41 @ref xrcfileformat_overview
42 @ref xrccppheader_overview
43 @ref newresourcehandlers_overview
46 @section xrcconcepts XRC concepts
48 These are the typical steps for using XRC files in your application.
51 Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
52 If you are going to use @ref binaryresourcefiles_overview, install
53 wxFileSystem archive handler first with @c wxFileSystem::AddHandler(new wxArchiveFSHandler);
54 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 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 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).
62 To create an XRC file, you can use one of the following methods.
65 Create the file by hand;
66 use #wxDesigner, a commercial dialog designer/RAD tool;
67 use #DialogBlocks, a commercial dialog editor;
68 use #XRCed, a wxPython-based
69 dialog editor that you can find in the @c wxPython/tools subdirectory of the wxWidgets
71 use #wxGlade, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
74 A complete list of third-party tools that write to XRC can be found at #www.wxwidgets.org/lnk_tool.htm.
75 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.
80 You can also use @ref embeddedresource_overview
82 @section binaryresourcefiles Using binary resource files
84 To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
85 (the input XRC files) and the following switches and options:
88 -h (--help): show a help message
89 -v (--verbose): show verbose logging information
90 -c (--cpp-code): write C++ source rather than a XRS file
91 -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 -u (--uncompressed): do not compress XML files (C++ only)
94 -g (--gettext): output underscore-wrapped strings that poEdit or gettext can scan. Outputs to stdout, or a file if -o is used
95 -n (--function) name: specify C++ function name (use with -c)
96 -o (--output) filename: specify the output file, such as resource.xrs or resource.cpp
97 -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
115 #include wx/filesys.h
118 wxFileSystem::AddHandler(new wxArchiveFSHandler);
122 @section embeddedresource Using embedded resources
124 It is sometimes useful to embed resources in the executable itself instead
125 of loading an external file (e.g. when your app is small and consists only of one
126 exe file). XRC provides means to convert resources into regular C++ file that
127 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 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 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 xrcfileformat XRC file format
431 Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWidgets
434 @section 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).
442 For each top level window defined in the XRC file a C++ class definition is
443 generated, containing as class members the named widgets of the window.
444 A default constructor for each class is also generated. Inside the constructor
445 all XRC loading is done and all class members representing widgets are initialized.
446 A simple example will help understand how the scheme works. Suppose you have
447 a XRC file defining a top level window @c TestWnd_Base, which subclasses @c wxFrame (any
448 other class like @c wxDialog will do also), and has subwidgets @c wxTextCtrl A and @c wxButton B.
449 The XRC file and corresponding class definition in the header file will be something like:
453 resource version="2.3.0.1"
454 object class="wxFrame" name="TestWnd_Base"
457 object class="wxBoxSizer"
458 orientwxHORIZONTAL/orient
459 object class="sizeritem"
460 object class="wxTextCtrl" name="A"
461 labelTest label/label
464 object class="sizeritem"
465 object class="wxButton" name="B"
466 labelTest button/label
474 class TestWnd_Base : public wxFrame {
480 void InitWidgetsFromXRC(){
481 wxXmlResource::Get()-LoadObject(this,@NULL,"TestWnd","wxFrame");
482 A = XRCCTRL(*this,"A",wxTextCtrl);
483 B = XRCCTRL(*this,"B",wxButton);
487 InitWidgetsFromXRC();
492 The generated window class can be used as basis for the full window class. The
493 class members which represent widgets may be accessed by name instead of using
494 @c XRCCTRL every time you wish to reference them (note that they are @c protected class members),
495 though you must still use @c XRCID to refer to widget IDs in the event
500 #include "resource.h"
502 class TestWnd : public TestWnd_Base {
505 // A, B already initialised at this point
506 A-SetValue("Updated in TestWnd::TestWnd");
507 B-SetValue("Nice :)");
509 void OnBPressed(wxEvent& event){
512 DECLARE_EVENT_TABLE();
515 BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
516 EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
520 It is also possible to access the wxSizerItem of a sizer that is part of
521 a resource. This can be done using @c XRCSIZERITEM as shown. The
522 resource file can have something like this for a sizer item.
525 object class="spacer" name="area"
530 The code can then access the sizer item by using @c XRCSIZERITEM and
534 wxSizerItem* item = XRCSIZERITEM(*this, "area");
538 @section newresourcehandlers Adding new resource handlers
540 Adding a new resource handler is pretty easy.
541 Typically, to add an handler for the @c MyControl class, you'll want to create
542 the @c xh_mycontrol.h @c xh_mycontrol.cpp files.
543 The header needs to contains the @c MyControlXmlHandler class definition:
546 class MyControlXmlHandler : public wxXmlResourceHandler
551 MyControlXmlHandler();
553 // Creates the control and returns a pointer to it.
554 virtual wxObject *DoCreateResource();
556 // Returns @true if we know how to create a control for the given node.
557 virtual bool CanHandle(wxXmlNode *node);
559 // Register with wxWidgets' dynamic class subsystem.
560 DECLARE_DYNAMIC_CLASS(MyControlXmlHandler)
564 The implementation of your custom XML handler will typically look as:
567 // Register with wxWidgets' dynamic class subsystem.
568 IMPLEMENT_DYNAMIC_CLASS(MyControlXmlHandler, wxXmlResourceHandler)
570 MyControlXmlHandler::MyControlXmlHandler()
572 // this call adds support for all wxWindows class styles
573 // (e.g. wxBORDER_SIMPLE, wxBORDER_SUNKEN, wxWS_EX_* etc etc)
576 // if MyControl class supports e.g. MYCONTROL_DEFAULT_STYLE
578 // XRC_ADD_STYLE(MYCONTROL_DEFAULT_STYLE);
581 wxObject *MyControlXmlHandler::DoCreateResource()
583 // the following macro will init a pointer named "control"
584 // with a new instance of the MyControl class, but will NOT
586 XRC_MAKE_INSTANCE(control, MyControl)
588 // this is the point where you'll typically need to do the most
589 // important changes: here the control is created and initialized.
590 // You'll want to use the wxXmlResourceHandler's getters to
591 // do most of your work.
592 // If e.g. the MyControl::Create function looks like:
594 // bool MyControl::Create(wxWindow *parent, int id,
595 // const wxBitmap , const wxPoint ,
596 // const wxBitmap , const wxPoint ,
597 // const wxString , const wxFont ,
598 // const wxPoint , const wxSize ,
599 // long style = MYCONTROL_DEFAULT_STYLE,
600 // const wxString = wxT("MyControl"));
602 // then the XRC for your component should look like:
604 // object class="MyControl" name="some_name"
605 // first-bitmapfirst.xpm/first-bitmap
606 // second-bitmaptext.xpm/second-bitmap
607 // first-pos3,3/first-pos
608 // second-pos4,4/second-pos
609 // the-titlea title/the-title
611 // !-- the standard XRC tags for describing a font: size, style, weight, etc --
613 // !-- XRC also accepts other usual tags for wxWindow-derived classes:
614 // like e.g. name, style, size, position, etc --
617 // and the code to read your custom tags from the XRC file is just:
618 control-Create(m_parentAsWindow, GetID(),
619 GetBitmap(wxT("first-bitmap")),
620 GetPosition(wxT("first-pos")),
621 GetBitmap(wxT("second-bitmap")),
622 GetPosition(wxT("second-pos")),
623 GetText(wxT("the-title")),
624 GetFont(wxT("title-font")),
625 GetPosition(), GetSize(), GetStyle(), GetName());
627 SetupWindow(control);
632 bool MyControlXmlHandler::CanHandle(wxXmlNode *node)
634 // this function tells XRC system that this handler can parse
635 // the object class="MyControl" tags
636 return IsOfClass(node, wxT("MyControl"));
640 You may want to check the #wxXmlResourceHandler documentation
641 to see how many built-in getters it contains. It's very easy to retrieve also complex structures
642 out of XRC files using them.