1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>wxWidgets: XML Based Resource System (XRC)</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page_container">
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0" style="width: 100%;">
<tbody>
<tr>
<td id="projectlogo">
<a href="http://www.wxwidgets.org/" target="_new">
<img alt="wxWidgets" src="logo.png"/>
</a>
</td>
<td style="padding-left: 0.5em; text-align: right;">
<span id="projectnumber">Version: 3.0.2</span>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.8.2 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Categories</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="index.html">Documentation</a></li><li class="navelem"><a class="el" href="page_topics.html">Programming Guides</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">XML Based Resource System (XRC) </div> </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#overview_xrc_gettingstarted">Getting Started with XRC</a></li>
<li class="level1"><a href="#overview_xrc_xrcsample">The XRC sample</a></li>
<li class="level1"><a href="#overview_xrc_binaryresourcefiles">Binary Resource Files</a></li>
<li class="level1"><a href="#overview_xrc_embeddedresource">Using Embedded Resources</a></li>
<li class="level1"><a href="#overview_xrc_cppheader">C++ header file generation</a></li>
<li class="level1"><a href="#overview_xrc_newresourcehandlers">Adding New Resource Handlers</a></li>
</ul>
</div>
<div class="textblock"><p>The XML-based resource system, known as XRC, allows user interface elements such as dialogs, menu bars and toolbars, to be stored in text files and loaded into the application at run-time.</p>
<p>XRC files can also be compiled into binary XRS files or C++ code (the former makes it possible to store all resources in a single file and the latter is useful when you want to embed the resources into the executable).</p>
<p>There are several advantages to using XRC resources:</p>
<ul>
<li>Recompiling and linking an application is not necessary if the resources change. </li>
<li>If you use a dialog designer that generates C++ code, it can be hard to reintegrate this into existing C++ code. Separation of resources and code is a more elegant solution. </li>
<li>You can choose between different alternative resource files at run time, if necessary. </li>
<li>The XRC format uses sizers for flexibility, allowing dialogs to be resizable and highly portable. </li>
<li>The XRC format is a wxWidgets standard, and can be generated or postprocessed by any program that understands it. As it is based on the XML standard, existing XML editors can be used for simple editing purposes.</li>
</ul>
<p>XRC was written by Vaclav Slavik.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="classwx_xml_resource.html" title="This is the main class for interacting with the XML-based resource system.">wxXmlResource</a>, <a class="el" href="classwx_xml_resource_handler.html" title="wxSizerXmlHandler is a class for resource handlers capable of creating a wxSizer object from an XML n...">wxXmlResourceHandler</a>, <a class="el" href="overview_xrcformat.html">XRC File Format</a></dd></dl>
<h1><a class="anchor" id="overview_xrc_gettingstarted"></a>
Getting Started with XRC</h1>
<p><b> Creating an XRC file </b></p>
<p>You will need to write an XRC file. Though this <em>can</em> be done by hand in a text editor, for all but the smallest files it is advisable to use a specialised tool. Examples of these include:</p>
<p><em>Non-free</em>: </p>
<ul>
<li>wxDesigner <a href="http://www.wxdesigner-software.de/">http://www.wxdesigner-software.de/</a>, a commercial dialog designer/RAD tool. </li>
<li>DialogBlocks <a href="http://www.anthemion.co.uk/dialogblocks/">http://www.anthemion.co.uk/dialogblocks/</a>, a commercial dialog editor.</li>
</ul>
<p><em>Free:</em> </p>
<ul>
<li>XRCed <a href="http://xrced.sf.net/">http://xrced.sf.net/</a>, a wxPython-based dialog editor that you can find in the wxPython/tools subdirectory of the wxWidgets SVN archive. </li>
<li>wxFormBuilder <a href="http://wxformbuilder.org/">http://wxformbuilder.org/</a>, a C++-based dialog editor that can output C++, XRC or python.</li>
</ul>
<p>There's a more complete list at <a href="http://www.wxwidgets.org/wiki/index.php/Tools">http://www.wxwidgets.org/wiki/index.php/Tools</a></p>
<p>This small demonstration XRC file contains a simple dialog: </p>
<div class="fragment"><div class="line"><?xml version=<span class="stringliteral">"1.0"</span> ?></div>
<div class="line"><resource version=<span class="stringliteral">"2.3.0.1"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxDialog"</span> name=<span class="stringliteral">"SimpleDialog"</span>></div>
<div class="line"> <title>Simple dialog</title></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxBoxSizer"</span>></div>
<div class="line"> <orient><a class="code" href="defs_8h.html#a1e6994f40bd9cb140e292afb165af971a2134d1cb8cf605532880c118190695b5">wxVERTICAL</a></orient></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"sizeritem"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxTextCtrl"</span> name=<span class="stringliteral">"text"</span>/></div>
<div class="line"> <option>1</option></div>
<div class="line"> <flag><a class="code" href="defs_8h.html#ac0f30319732dcceda470516918ff3556a066863c82a2c08f6f53b663de87c60b6">wxALL</a>|<a class="code" href="defs_8h.html#a27b6b668631a5f79f24d474af7d9b388a0d831bd6985c81f9bbc4830cc0c2e7df">wxEXPAND</a></flag></div>
<div class="line"> <border>10</border></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"sizeritem"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxBoxSizer"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"sizeritem"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxButton"</span> name=<span class="stringliteral">"clickme_btn"</span>></div>
<div class="line"> <label>Click</label></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> <flag><a class="code" href="defs_8h.html#ac0f30319732dcceda470516918ff3556a2b2d18b748d21a493f82e589f1a05746">wxRIGHT</a></flag></div>
<div class="line"> <border>10</border></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"sizeritem"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxButton"</span> name=<span class="stringliteral">"wxID_OK"</span>></div>
<div class="line"> <label>OK</label></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> <flag><a class="code" href="defs_8h.html#ac0f30319732dcceda470516918ff3556aab66c1f81b54102c884b1307840d323c">wxLEFT</a></flag></div>
<div class="line"> <border>10</border></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> <orient><a class="code" href="defs_8h.html#a1e6994f40bd9cb140e292afb165af971ab96154c324ed87cc17e9b227f9248c77">wxHORIZONTAL</a></orient></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> <flag><a class="code" href="defs_8h.html#ac0f30319732dcceda470516918ff3556a066863c82a2c08f6f53b663de87c60b6">wxALL</a>|<a class="code" href="defs_8h.html#a543dd017a172dc316253a8a1f351dde9a8f3585b9e89360459fa853740a351015">wxALIGN_CENTRE</a></flag></div>
<div class="line"> <border>10</border></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"></resource></div>
</div><!-- fragment --><p>You can keep all your XRC elements together in one file, or split them between several.</p>
<p><b> Loading XRC files </b></p>
<p>Before you can use XRC in an app, it must first be loaded. This code fragment shows how to load a single XRC file "resource.xrc" from the current working directory, plus all the *.xrc files contained in the subdirectory "rc".</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include "<a class="code" href="xmlres_8h.html">wx/xrc/xmlres.h</a>"</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">bool</span> MyApp::OnInit()</div>
<div class="line">{</div>
<div class="line"> ...</div>
<div class="line"> <a class="code" href="classwx_xml_resource.html#ae9a487479dcd6204d12fbbf7be497561" title="Gets the global resources object or creates one if none exists.">wxXmlResource::Get</a>()-><a class="code" href="classwx_xml_resource.html#a1fea48a0061b93247b4869a55877862b" title="Initializes handlers for all supported controls/windows.">InitAllHandlers</a>();</div>
<div class="line"></div>
<div class="line"> <a class="code" href="classwx_xml_resource.html#ae9a487479dcd6204d12fbbf7be497561" title="Gets the global resources object or creates one if none exists.">wxXmlResource::Get</a>()-><a class="code" href="classwx_xml_resource.html#ac44a714ff459b7f3bd6c78d27482bb60" title="Loads resources from XML files that match given filemask.">Load</a>(<span class="stringliteral">"resource.xrc"</span>);</div>
<div class="line"> <a class="code" href="classwx_xml_resource.html#ae9a487479dcd6204d12fbbf7be497561" title="Gets the global resources object or creates one if none exists.">wxXmlResource::Get</a>()-><a class="code" href="classwx_xml_resource.html#abd3231181713edc32ecbeef72807baf8" title="Loads all .xrc files from directory dirname.">LoadAllFiles</a>(<span class="stringliteral">"rc"</span>);</div>
<div class="line"> ...</div>
<div class="line">}</div>
</div><!-- fragment --><p>It's normal to load any XRC files at the beginning of an app. Though it is possible to unload a file later, it's seldom necessary.</p>
<p><b> Using an XRC item </b></p>
<p>The XRC file(s) are now loaded into the app's virtual filesystem. From there, you must do another sort of load when you want to use an individual object. Yes, it's confusingly named, but you first Load() the file, and later load each top-level object when its needed.</p>
<p>This is how you would use the above simple dialog in your code.</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> MyClass::ShowDialog()</div>
<div class="line">{</div>
<div class="line"> <a class="code" href="classwx_dialog.html" title="A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the ...">wxDialog</a> dlg;</div>
<div class="line"> <span class="keywordflow">if</span> (<a class="code" href="classwx_xml_resource.html#ae9a487479dcd6204d12fbbf7be497561" title="Gets the global resources object or creates one if none exists.">wxXmlResource::Get</a>()->LoadDialog(&dlg, NULL, <span class="stringliteral">"SimpleDialog"</span>))</div>
<div class="line"> dlg.<a class="code" href="classwx_dialog.html#a6e078c3d0653f75ad3c34a37c0b54637" title="Shows an application-modal dialog.">ShowModal</a>();</div>
<div class="line">}</div>
</div><!-- fragment --><p>See how simple the code is. All the instantiation is done invisibly by the XRC system.</p>
<p>Though you'll most often use <a class="el" href="classwx_xml_resource.html#aa9f2603cb55bfb2c696972be9875e229" title="Loads a dialog.">wxXmlResource::LoadDialog</a>, there are also equivalents that load a frame, a menu etc; and the generic <a class="el" href="classwx_xml_resource.html#a92fb76bed2c93c283c940c0a02e0a543" title="Load an object from the resource specifying both the resource name and the class name.">wxXmlResource::LoadObject</a>. See <a class="el" href="classwx_xml_resource.html" title="This is the main class for interacting with the XML-based resource system.">wxXmlResource</a> for more details.</p>
<p><b> Accessing XRC child controls </b></p>
<p>The last section showed how to load top-level windows like dialogs, but what about child windows like the <a class="el" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a> named "text" that the dialog contains? You can't 'load' an individual child control in the same way. Instead you use the XRCCTRL macro to get a pointer to the child. To expand the previous code:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> MyClass::ShowDialog()</div>
<div class="line">{</div>
<div class="line"> <a class="code" href="classwx_dialog.html" title="A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the ...">wxDialog</a> dlg;</div>
<div class="line"> <span class="keywordflow">if</span> (!<a class="code" href="classwx_xml_resource.html#ae9a487479dcd6204d12fbbf7be497561" title="Gets the global resources object or creates one if none exists.">wxXmlResource::Get</a>()->LoadDialog(&dlg, NULL, <span class="stringliteral">"SimpleDialog"</span>))</div>
<div class="line"> <span class="keywordflow">return</span>;</div>
<div class="line"></div>
<div class="line"> <a class="code" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a>* pText = XRCCTRL(dlg, <span class="stringliteral">"text"</span>, <a class="code" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a>);</div>
<div class="line"> <span class="keywordflow">if</span> (pText)</div>
<div class="line"> pText-><a class="code" href="classwx_text_entry.html#a8c52ab71f51c8f80556c2c8e763cbca1" title="Sets the new text control value.">ChangeValue</a>(<span class="stringliteral">"This is a simple dialog"</span>);</div>
<div class="line"></div>
<div class="line"> dlg.<a class="code" href="classwx_dialog.html#a6e078c3d0653f75ad3c34a37c0b54637" title="Shows an application-modal dialog.">ShowModal</a>();</div>
<div class="line">}</div>
</div><!-- fragment --><p>XRCCTRL takes a reference to the parent container and uses <a class="el" href="classwx_window.html#a3468ef382c612b5f8e91c7c18d00a69f" title="Find a child of this window, by id.">wxWindow::FindWindow</a> to search inside it for a <a class="el" href="classwx_window.html" title="wxWindow is the base class for all windows and represents any visible object on screen.">wxWindow</a> with the supplied name (here "text"). It returns a pointer to that control, cast to the type in the third parameter; so a similar effect could be obtained by writing:</p>
<div class="fragment"><div class="line">pText = (<a class="code" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a>*)(dlg.<a class="code" href="classwx_window.html#adbe4eea02f615313c12a2c00557a44c3" title="Find a window by its name (as given in a window constructor or Create() function call).">FindWindowByName</a>(<span class="stringliteral">"text"</span>));</div>
</div><!-- fragment --><p><b> XRC and IDs </b></p>
<p>The ID of a control is often needed, e.g. for use in an event table or with <a class="el" href="classwx_evt_handler.html#a0f30c8fa5583b4a5f661897d63de3b62" title="Binds the given function, functor or method dynamically with the event.">wxEvtHandler::Bind</a>. It can easily be found by passing the name of the control to the XRCID macro:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> MyClass::ShowDialog()</div>
<div class="line">{</div>
<div class="line"> <a class="code" href="classwx_dialog.html" title="A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the ...">wxDialog</a> dlg;</div>
<div class="line"> <span class="keywordflow">if</span> (!<a class="code" href="classwx_xml_resource.html#ae9a487479dcd6204d12fbbf7be497561" title="Gets the global resources object or creates one if none exists.">wxXmlResource::Get</a>()->LoadDialog(&dlg, NULL, <span class="stringliteral">"SimpleDialog"</span>))</div>
<div class="line"> <span class="keywordflow">return</span>;</div>
<div class="line"></div>
<div class="line"> XRCCTRL(dlg, <span class="stringliteral">"text"</span>, <a class="code" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a>)-><a class="code" href="classwx_evt_handler.html#a0f30c8fa5583b4a5f661897d63de3b62" title="Binds the given function, functor or method dynamically with the event.">Bind</a>(wxEVT_COMMAND_TEXT_UPDATED,</div>
<div class="line"> wxTextEventHandler(MyClass::OnTextEntered), <span class="keyword">this</span>, XRCID(<span class="stringliteral">"text"</span>));</div>
<div class="line"></div>
<div class="line"> XRCCTRL(dlg, <span class="stringliteral">"clickme_btn"</span>, <a class="code" href="classwx_button.html" title="A button is a control that contains a text string, and is one of the most common elements of a GUI...">wxButton</a>)->Bind(wxEVT_COMMAND_BUTTON_CLICKED,</div>
<div class="line"> wxCommandEventHandler(MyClass::OnClickme), <span class="keyword">this</span>, XRCID(<span class="stringliteral">"clickme_btn"</span>));</div>
<div class="line"></div>
<div class="line"> dlg.<a class="code" href="classwx_dialog.html#a6e078c3d0653f75ad3c34a37c0b54637" title="Shows an application-modal dialog.">ShowModal</a>();</div>
<div class="line">}</div>
</div><!-- fragment --><p>A few points to note: </p>
<ul>
<li>The value of the int returned by XRCID("foo") is guaranteed to be unique within an app. </li>
<li>However that value isn't predictable, and you shouldn't rely on it being consistent between runs. It certainly won't be the same in different apps. </li>
<li><a class="el" href="page_stockitems.html">Stock Items</a> such as wxID_OK work correctly without requiring XRCID (because, internally, XRCID("wxID_OK") is mapped to wxID_OK). </li>
<li>Both XRCID and XRCCTRL use the 'name' of the control (as in <a class="el" href="classwx_window.html#aab1b302c4bdabd134ce8d401dbaaf990" title="Returns the window's name.">wxWindow::GetName</a>). This is different from the label that the user sees on e.g. a <a class="el" href="classwx_button.html" title="A button is a control that contains a text string, and is one of the most common elements of a GUI...">wxButton</a>.</li>
</ul>
<p><b> Subclassing in XRC </b></p>
<p>You will often want to use subclassed wx controls in your code. There are three ways to do this from XRC: </p>
<ul>
<li>Very rarely you might need to <a class="el" href="overview_xrcformat.html#overview_xrcformat_extending_custom">create your own wxXmlResourceHandler</a> </li>
<li>Occasionally <a class="el" href="classwx_xml_resource.html#ab550cd29efdc7afdf72fd0a1990d0074" title="Attaches an unknown control to the given panel/window/dialog.">wxXmlResource::AttachUnknownControl</a> may be best. See <a class="el" href="overview_xrcformat.html#overview_xrcformat_extending_unknown">Unknown Objects</a> </li>
<li>Usually though, the simple 'subclass' keyword will suffice.</li>
</ul>
<p>Suppose you wanted the <a class="el" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a> named "text" to be created as your derived class MyTextCtrl. The only change needed in the XRC file would be in this line:</p>
<div class="fragment"><div class="line"><<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxTextCtrl"</span> name=<span class="stringliteral">"text"</span> subclass=<span class="stringliteral">"MyTextCtrl"</span>/></div>
</div><!-- fragment --><p>The only change in your code would be to use MyTextCtrl in XRCCTRL. However for the subclass to be created successfully, it's important to ensure that it uses wxWidget's RTTI mechanism: see <a class="el" href="overview_xrcformat.html#overview_xrcformat_extending_subclass">Subclassing</a> for the details.</p>
<h1><a class="anchor" id="overview_xrc_xrcsample"></a>
The XRC sample</h1>
<p>A major resource for learning how to use XRC is the <a class="el" href="page_samples.html#page_samples_xrc">XRC Sample</a>. This demonstrates all of the standard uses of XRC, and some of the less common ones. It is strongly suggested that you run it, and look at the well-commented source code to see how it works.</p>
<h1><a class="anchor" id="overview_xrc_binaryresourcefiles"></a>
Binary Resource Files</h1>
<p>To compile binary resource files, use the command-line <code>wxrc</code> utility. It takes one or more file parameters (the input XRC files) and the following switches and options:</p>
<ul>
<li>-h (–help): Show a help message. </li>
<li>-v (–verbose): Show verbose logging information. </li>
<li>-c (–cpp-code): Write C++ source rather than a XRS file. </li>
<li>-e (–extra-cpp-code): If used together with -c, generates C++ header file containing class definitions for the windows defined by the XRC file (see special subsection). </li>
<li>-u (–uncompressed): Do not compress XML files (C++ only). </li>
<li>-g (–gettext): Output underscore-wrapped strings that poEdit or gettext can scan. Outputs to stdout, or a file if -o is used. </li>
<li>-n (–function) <name>: Specify C++ function name (use with -c). </li>
<li>-o (–output) <filename>: Specify the output file, such as resource.xrs or resource.cpp. </li>
<li>-l (–list-of-handlers) <filename>: Output a list of necessary handlers to this file.</li>
</ul>
<p>For example:</p>
<div class="fragment"><div class="line">$ wxrc resource.xrc</div>
<div class="line">$ wxrc resource.xrc -o resource.xrs</div>
<div class="line">$ wxrc resource.xrc -v -c -o resource.cpp</div>
</div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>XRS file is essentially a renamed ZIP archive which means that you can manipulate it with standard ZIP tools. Note that if you are using XRS files, you have to initialize the <a class="el" href="classwx_file_system.html" title="This class provides an interface for opening files on different file systems.">wxFileSystem</a> archive handler first! It is a simple thing to do:</dd></dl>
<div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="filesys_8h.html">wx/filesys.h</a>></span></div>
<div class="line"><span class="preprocessor">#include <<a class="code" href="fs__arc_8h.html">wx/fs_arc.h</a>></span></div>
<div class="line">...</div>
<div class="line">wxFileSystem::AddHandler(<span class="keyword">new</span> <a class="code" href="classwx_archive_f_s_handler.html" title="A file system handler for accessing files inside of archives.">wxArchiveFSHandler</a>);</div>
</div><!-- fragment --><h1><a class="anchor" id="overview_xrc_embeddedresource"></a>
Using Embedded Resources</h1>
<p>It is sometimes useful to embed resources in the executable itself instead of loading an external file (e.g. when your app is small and consists only of one exe file). XRC provides means to convert resources into regular C++ file that can be compiled and included in the executable.</p>
<p>Use the <code>-c</code> switch to <code>wxrc</code> utility to produce C++ file with embedded resources. This file will contain a function called <code>InitXmlResource</code> (unless you override this with a command line switch). Use it to load the resource:</p>
<div class="fragment"><div class="line"><span class="keyword">extern</span> <span class="keywordtype">void</span> InitXmlResource(); <span class="comment">// defined in generated file</span></div>
<div class="line">...</div>
<div class="line">wxXmlResource::Get()->InitAllHandlers();</div>
<div class="line">InitXmlResource();</div>
<div class="line">...</div>
</div><!-- fragment --><h1><a class="anchor" id="overview_xrc_cppheader"></a>
C++ header file generation</h1>
<p>Using the <code>-e</code> switch together with <code>-c</code>, a C++ header file is written containing class definitions for the GUI windows defined in the XRC file. This code generation can make it easier to use XRC and automate program development. The classes can be used as basis for development, freeing the programmer from dealing with most of the XRC specifics (e.g. <code>XRCCTRL</code>).</p>
<p>For each top level window defined in the XRC file a C++ class definition is generated, containing as class members the named widgets of the window. A default constructor for each class is also generated. Inside the constructor all XRC loading is done and all class members representing widgets are initialized.</p>
<p>A simple example will help understand how the scheme works. Suppose you have a XRC file defining a top level window <code>TestWnd_Base</code>, which subclasses <a class="el" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a> (any other class like <code><a class="el" href="classwx_dialog.html" title="A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the ...">wxDialog</a></code> will do also), and has subwidgets <a class="el" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a> A and <a class="el" href="classwx_button.html" title="A button is a control that contains a text string, and is one of the most common elements of a GUI...">wxButton</a> B.</p>
<p>The XRC file and corresponding class definition in the header file will be something like:</p>
<div class="fragment"><div class="line"><?xml version=<span class="stringliteral">"1.0"</span>?></div>
<div class="line"><resource version=<span class="stringliteral">"2.3.0.1"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxFrame"</span> name=<span class="stringliteral">"TestWnd_Base"</span>></div>
<div class="line"> <size>-1,-1</size></div>
<div class="line"> <title>Test</title></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxBoxSizer"</span>></div>
<div class="line"> <orient><a class="code" href="defs_8h.html#a1e6994f40bd9cb140e292afb165af971ab96154c324ed87cc17e9b227f9248c77">wxHORIZONTAL</a></orient></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"sizeritem"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxTextCtrl"</span> name=<span class="stringliteral">"A"</span>></div>
<div class="line"> <label>Test label</label></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"sizeritem"</span>></div>
<div class="line"> <<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"wxButton"</span> name=<span class="stringliteral">"B"</span>></div>
<div class="line"> <label>Test button</label></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"> </<span class="keywordtype">object</span>></div>
<div class="line"></resource></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>TestWnd_Base : <span class="keyword">public</span> <a class="code" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">protected</span>:</div>
<div class="line"> <a class="code" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a>* A;</div>
<div class="line"> <a class="code" href="classwx_button.html" title="A button is a control that contains a text string, and is one of the most common elements of a GUI...">wxButton</a>* B;</div>
<div class="line"></div>
<div class="line"><span class="keyword">private</span>:</div>
<div class="line"> <span class="keywordtype">void</span> InitWidgetsFromXRC()</div>
<div class="line"> {</div>
<div class="line"> <a class="code" href="classwx_xml_resource.html#ae9a487479dcd6204d12fbbf7be497561" title="Gets the global resources object or creates one if none exists.">wxXmlResource::Get</a>()-><a class="code" href="classwx_xml_resource.html#a92fb76bed2c93c283c940c0a02e0a543" title="Load an object from the resource specifying both the resource name and the class name.">LoadObject</a>(<span class="keyword">this</span>, NULL, <span class="stringliteral">"TestWnd"</span>, <span class="stringliteral">"wxFrame"</span>);</div>
<div class="line"> A = XRCCTRL(*<span class="keyword">this</span>, <span class="stringliteral">"A"</span>, <a class="code" href="classwx_text_ctrl.html" title="A text control allows text to be displayed and edited.">wxTextCtrl</a>);</div>
<div class="line"> B = XRCCTRL(*<span class="keyword">this</span>, <span class="stringliteral">"B"</span>, <a class="code" href="classwx_button.html" title="A button is a control that contains a text string, and is one of the most common elements of a GUI...">wxButton</a>);</div>
<div class="line"> }</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line"> TestWnd::TestWnd()</div>
<div class="line"> {</div>
<div class="line"> InitWidgetsFromXRC();</div>
<div class="line"> }</div>
<div class="line">};</div>
</div><!-- fragment --><p>The generated window class can be used as basis for the full window class. The class members which represent widgets may be accessed by name instead of using <code>XRCCTRL</code> every time you wish to reference them (note that they are <code>protected</code> class members), though you must still use <code>XRCID</code> to refer to widget IDs in the event table.</p>
<p>Example:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include "resource.h"</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>TestWnd : <span class="keyword">public</span> TestWnd_Base</div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line"> TestWnd()</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// A, B already initialised at this point</span></div>
<div class="line"> A->SetValue(<span class="stringliteral">"Updated in TestWnd::TestWnd"</span>);</div>
<div class="line"> B->SetValue(<span class="stringliteral">"Nice :)"</span>);</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordtype">void</span> OnBPressed(<a class="code" href="classwx_event.html" title="An event is a structure holding information about an event passed to a callback or member function...">wxEvent</a>& event)</div>
<div class="line"> {</div>
<div class="line"> Close();</div>
<div class="line"> }</div>
<div class="line"> DECLARE_EVENT_TABLE();</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line">BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)</div>
<div class="line"> EVT_BUTTON(XRCID("B"), TestWnd::OnBPressed)</div>
<div class="line">END_EVENT_TABLE()</div>
</div><!-- fragment --><p>It is also possible to access the <a class="el" href="classwx_sizer_item.html" title="The wxSizerItem class is used to track the position, size and other attributes of each item managed b...">wxSizerItem</a> of a sizer that is part of a resource. This can be done using <code>XRCSIZERITEM</code> as shown.</p>
<p>The resource file can have something like this for a sizer item.</p>
<div class="fragment"><div class="line"><<span class="keywordtype">object</span> <span class="keyword">class</span>=<span class="stringliteral">"spacer"</span> name=<span class="stringliteral">"area"</span>></div>
<div class="line"> <size>400, 300</size></div>
<div class="line"></<span class="keywordtype">object</span>></div>
</div><!-- fragment --><p>The code can then access the sizer item by using <code>XRCSIZERITEM</code> and <code>XRCID</code> together.</p>
<div class="fragment"><div class="line"><a class="code" href="classwx_sizer_item.html" title="The wxSizerItem class is used to track the position, size and other attributes of each item managed b...">wxSizerItem</a>* item = XRCSIZERITEM(*<span class="keyword">this</span>, <span class="stringliteral">"area"</span>);</div>
</div><!-- fragment --><h1><a class="anchor" id="overview_xrc_newresourcehandlers"></a>
Adding New Resource Handlers</h1>
<p>Adding a new resource handler is pretty easy.</p>
<p>Typically, to add an handler for the <code>MyControl</code> class, you'll want to create the <code>xh_mycontrol.h</code> and <code>xh_mycontrol.cpp</code> files.</p>
<p>The header needs to contains the <code>MyControlXmlHandler</code> class definition:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>MyControlXmlHandler : <span class="keyword">public</span> <a class="code" href="classwx_xml_resource_handler.html" title="wxSizerXmlHandler is a class for resource handlers capable of creating a wxSizer object from an XML n...">wxXmlResourceHandler</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line"> <span class="comment">// Constructor.</span></div>
<div class="line"> MyControlXmlHandler();</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Creates the control and returns a pointer to it.</span></div>
<div class="line"> <span class="keyword">virtual</span> <a class="code" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a> *<a class="code" href="classwx_xml_resource_handler.html#aea4c926eb4842302568d4d063960e2fc" title="Called from CreateResource after variables were filled.">DoCreateResource</a>();</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Returns true if we know how to create a control for the given node.</span></div>
<div class="line"> <span class="keyword">virtual</span> <span class="keywordtype">bool</span> <a class="code" href="classwx_xml_resource_handler.html#af0c078bc565c86b6ba33f860bfc053ed" title="Returns true if it understands this node and can create a resource from it, false otherwise...">CanHandle</a>(<a class="code" href="classwx_xml_node.html" title="Represents a node in an XML document.">wxXmlNode</a> *node);</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Register with wxWidgets' dynamic class subsystem.</span></div>
<div class="line"> DECLARE_DYNAMIC_CLASS(MyControlXmlHandler)</div>
<div class="line">};</div>
</div><!-- fragment --><p>The implementation of your custom XML handler will typically look as:</p>
<div class="fragment"><div class="line"><span class="comment">// Register with wxWidgets' dynamic class subsystem.</span></div>
<div class="line">IMPLEMENT_DYNAMIC_CLASS(MyControlXmlHandler, <a class="code" href="classwx_xml_resource_handler.html" title="wxSizerXmlHandler is a class for resource handlers capable of creating a wxSizer object from an XML n...">wxXmlResourceHandler</a>)</div>
<div class="line"></div>
<div class="line">MyControlXmlHandler::MyControlXmlHandler()</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// this call adds support for all wxWidgets class styles</span></div>
<div class="line"> <span class="comment">// (e.g. wxBORDER_SIMPLE, wxBORDER_SUNKEN, wxWS_EX_* etc etc)</span></div>
<div class="line"> AddWindowStyles();</div>
<div class="line"></div>
<div class="line"> <span class="comment">// if MyControl class supports e.g. MYCONTROL_DEFAULT_STYLE</span></div>
<div class="line"> <span class="comment">// you should use:</span></div>
<div class="line"> <span class="comment">// XRC_ADD_STYLE(MYCONTROL_DEFAULT_STYLE);</span></div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><a class="code" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a> *MyControlXmlHandler::DoCreateResource()</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// the following macro will init a pointer named "control"</span></div>
<div class="line"> <span class="comment">// with a new instance of the MyControl class, but will NOT</span></div>
<div class="line"> <span class="comment">// Create() it!</span></div>
<div class="line"> XRC_MAKE_INSTANCE(control, MyControl)</div>
<div class="line"></div>
<div class="line"> <span class="comment">// this is the point where you'll typically need to do the most</span></div>
<div class="line"> <span class="comment">// important changes: here the control is created and initialized.</span></div>
<div class="line"> <span class="comment">// You'll want to use the wxXmlResourceHandler's getters to</span></div>
<div class="line"> <span class="comment">// do most of your work.</span></div>
<div class="line"> <span class="comment">// If e.g. the MyControl::Create function looks like:</span></div>
<div class="line"> <span class="comment">//</span></div>
<div class="line"> <span class="comment">// bool MyControl::Create(wxWindow *parent, int id,</span></div>
<div class="line"> <span class="comment">// const wxBitmap &first, const wxPoint &posFirst,</span></div>
<div class="line"> <span class="comment">// const wxBitmap &second, const wxPoint &posSecond,</span></div>
<div class="line"> <span class="comment">// const wxString &theTitle, const wxFont &titleFont,</span></div>
<div class="line"> <span class="comment">// const wxPoint &pos, const wxSize &size,</span></div>
<div class="line"> <span class="comment">// long style = MYCONTROL_DEFAULT_STYLE,</span></div>
<div class="line"> <span class="comment">// const wxString &name = wxT("MyControl"));</span></div>
<div class="line"> <span class="comment">//</span></div>
<div class="line"> <span class="comment">// Then the XRC for your component should look like:</span></div>
<div class="line"> <span class="comment">//</span></div>
<div class="line"> <span class="comment">// <object class="MyControl" name="some_name"></span></div>
<div class="line"> <span class="comment">// <first-bitmap>first.xpm</first-bitmap></span></div>
<div class="line"> <span class="comment">// <second-bitmap>text.xpm</second-bitmap></span></div>
<div class="line"> <span class="comment">// <first-pos>3,3</first-pos></span></div>
<div class="line"> <span class="comment">// <second-pos>4,4</second-pos></span></div>
<div class="line"> <span class="comment">// <the-title>a title</the-title></span></div>
<div class="line"> <span class="comment">// <title-font></span></div>
<div class="line"> <span class="comment">// <!-- Standard XRC tags for a font: <size>, <style>, <weight>, etc --></span></div>
<div class="line"> <span class="comment">// </title-font></span></div>
<div class="line"> <span class="comment">// <!-- XRC also accepts other usual tags for wxWindow-derived classes:</span></div>
<div class="line"> <span class="comment">// like e.g. <name>, <style>, <size>, <position>, etc --></span></div>
<div class="line"> <span class="comment">// </object></span></div>
<div class="line"> <span class="comment">//</span></div>
<div class="line"> <span class="comment">// And the code to read your custom tags from the XRC file is just:</span></div>
<div class="line"> control->Create(m_parentAsWindow, GetID(),</div>
<div class="line"> GetBitmap(<a class="code" href="group__group__funcmacro__string.html#ga437ea6ba615b75dac8603e96ec864160" title="This macro can be used with character and string literals (in other words, 'x' or "foo") to automatic...">wxT</a>("first-bitmap")),</div>
<div class="line"> GetPosition(<a class="code" href="group__group__funcmacro__string.html#ga437ea6ba615b75dac8603e96ec864160" title="This macro can be used with character and string literals (in other words, 'x' or "foo") to automatic...">wxT</a>("first-pos")),</div>
<div class="line"> GetBitmap(<a class="code" href="group__group__funcmacro__string.html#ga437ea6ba615b75dac8603e96ec864160" title="This macro can be used with character and string literals (in other words, 'x' or "foo") to automatic...">wxT</a>("second-bitmap")),</div>
<div class="line"> GetPosition(<a class="code" href="group__group__funcmacro__string.html#ga437ea6ba615b75dac8603e96ec864160" title="This macro can be used with character and string literals (in other words, 'x' or "foo") to automatic...">wxT</a>("second-pos")),</div>
<div class="line"> GetText(<a class="code" href="group__group__funcmacro__string.html#ga437ea6ba615b75dac8603e96ec864160" title="This macro can be used with character and string literals (in other words, 'x' or "foo") to automatic...">wxT</a>("the-title")),</div>
<div class="line"> GetFont(<a class="code" href="group__group__funcmacro__string.html#ga437ea6ba615b75dac8603e96ec864160" title="This macro can be used with character and string literals (in other words, 'x' or "foo") to automatic...">wxT</a>("title-font")),</div>
<div class="line"> GetPosition(), GetSize(), GetStyle(), GetName());</div>
<div class="line"></div>
<div class="line"> SetupWindow(control);</div>
<div class="line"></div>
<div class="line"> return control;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">bool</span> MyControlXmlHandler::CanHandle(<a class="code" href="classwx_xml_node.html" title="Represents a node in an XML document.">wxXmlNode</a> *node)</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// this function tells XRC system that this handler can parse</span></div>
<div class="line"> <span class="comment">// the <object class="MyControl"> tags</span></div>
<div class="line"> <span class="keywordflow">return</span> IsOfClass(node, <a class="code" href="group__group__funcmacro__string.html#ga437ea6ba615b75dac8603e96ec864160" title="This macro can be used with character and string literals (in other words, 'x' or "foo") to automatic...">wxT</a>(<span class="stringliteral">"MyControl"</span>));</div>
<div class="line">}</div>
</div><!-- fragment --><p>You may want to check the <a class="el" href="classwx_xml_resource_handler.html" title="wxSizerXmlHandler is a class for resource handlers capable of creating a wxSizer object from an XML n...">wxXmlResourceHandler</a> documentation to see how many built-in getters it contains. It's very easy to retrieve also complex structures out of XRC files using them. </p>
</div></div><!-- contents -->
<address class="footer">
<small>
Generated on Thu Nov 27 2014 13:46:42 for wxWidgets by <a href="http://www.doxygen.org/index.html" target="_new">Doxygen</a> 1.8.2
</small>
</address>
<script src="wxwidgets.js" type="text/javascript"></script>
</div><!-- #page_container -->
</body>
</html>
|