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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|
<HTML>
<head><title>The wxWindows resource system</title></head>
<BODY BGCOLOR=#FFFFFF>
<A NAME="resourceformats"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx278.htm#overviews"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx302.htm#printingoverview"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx304.htm#runtimeclassoverview"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>
<H2>The wxWindows resource system</H2>
<P>
From version 1.61, wxWindows has an optional <I>resource file</I> facility,
which allows separation of dialog, menu, bitmap and icon specifications
from the application code.<P>
It is similar in principle to the Windows resource file (whose ASCII form is
suffixed .RC and whose binary form is suffixed .RES). The wxWindows resource
file is currently ASCII-only, suffixed .WXR. Note that under Windows,
the .WXR file does not <I>replace</I> the native Windows resource file,
it merely supplements it. There is no existing native resource format in X
(except for the defaults file, which has limited expressive power).<P>
Using wxWindows resources for panels and dialogs has an effect on how
you deal with panel item callbacks: you can't specify a callback function in
a resource file, so how do you achieve the same effect as with programmatic
panel construction? The solution is similar to that adopted by Windows, which
is to use the <I>parent</I> panel or dialog to intercept user events.<P>
From 1.61, wxWindows routes panel item events that do not have a callback
to the <A HREF="wx260.htm#wxwindowoncommand">OnCommand</A> member of the panel (or dialog). So, to use
panel or dialog resources, you need to derive a new class and override the
default (empty) OnCommand member. The first argument is a reference
to a wxWindow, and the second is a reference to a wxCommandEvent. Check the
name of the panel item that's generating an event by using the <A HREF="wx260.htm#wxwindowgetname">wxWindow::GetName</A>
function and a string comparison function such as <A HREF="wx266.htm#wxstringeq">wxStringEq</A>.
You may need to cast the reference to an appropriate specific type to perform
some operations.<P>
To obtain a pointer to a panel item when you only have the name (for example,
when you need to set a value of a text item from outside of the <B>OnCommand</B> function),
use the function <A HREF="wx271.htm#wxfindwindowbyname">wxFindWindowByName</A>.<P>
For details of functions for manipulating resource files and loading
user interface elements, see <A HREF="wx273.htm#resourcefuncs">wxWindows resource functions</A>.<P>
<A HREF="#topic1137">The format of a .WXR file</A><BR>
<A HREF="#topic1138">Dialog resource format</A><BR>
<A HREF="#topic1139">Menubar resource format</A><BR>
<A HREF="#topic1140">Bitmap resource format</A><BR>
<A HREF="#topic1141">Icon resource format</A><BR>
<A HREF="#topic1142">Resource format design issues</A><BR>
<A HREF="#topic1143">Compiling the resource system</A><BR>
<P>
<HR>
<A NAME="topic1137"></A>
<H3>The format of a .WXR file</H3>
<P>
A wxWindows resource file may look a little odd at first. It's C++
compatible, comprising mostly of static string variable declarations with
PrologIO syntax within the string.<P>
Here's a sample .WXR file:<P>
<PRE>
/*
* wxWindows Resource File
* Written by wxBuilder
*
*/
#include "noname.ids"
static char *aiai_resource = "bitmap(name = 'aiai_resource',\
bitmap = ['aiai', wxBITMAP_TYPE_BMP_RESOURCE, 'WINDOWS'],\
bitmap = ['aiai.xpm', wxBITMAP_TYPE_XPM, 'X']).";
static char *menuBar11 = "menu(name = 'menuBar11',\
menu = \
[\
['&File', 1, '', \
['&Open File', 2, 'Open a file'],\
['&Save File', 3, 'Save a file'],\
[],\
['E&xit', 4, 'Exit program']\
],\
['&Help', 5, '', \
['&About', 6, 'About this program']\
]\
]).";
static char *project_resource = "icon(name = 'project_resource',\
icon = ['project', wxBITMAP_TYPE_ICO_RESOURCE, 'WINDOWS'],\
icon = ['project_data', wxBITMAP_TYPE_XBM, 'X']).";
static char *panel3 = "dialog(name = 'panel3',\
style = '',\
title = 'untitled',\
button_font = [14, 'wxSWISS', 'wxNORMAL', 'wxBOLD', 0],\
label_font = [10, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0],\
x = 0, y = 37, width = 292, height = 164,\
control = [wxButton, 'OK', '', 'button5', 23, 34, -1, -1, 'aiai_resource'],\
control = [wxMessage, 'A Label', '', 'message7', 166, 61, -1, -1, 'aiai_resource'],\
control = [wxText, 'Text', 'wxVERTICAL_LABEL', 'text8', 24, 110, -1, -1]).";
</PRE>
As you can see, C++-style comments are allowed, and apparently include files
are supported too: but this is a special case, where the included file
is a file of defines shared by the C++ application code and resource file
to relate identifiers (such as FILE_OPEN) to integers.<P>
Each <I>resource object</I> is of standard PrologIO syntax, that is,
an object name such as <B>dialog</B> or <B>icon</B>, then an open
parenthesis, a list of comma-delimited attribute/value pairs, a closing
parenthesis, and a full stop. Backslashes are required to escape newlines,
for the benefit of C++ syntax. If double quotation marks are used to
delimit strings, they need to be escaped with backslash within a C++ string
(so it's easier to use single quotation marks instead).<P>
<HR>
<I>A note on PrologIO string syntax:</I> A string that begins with
an alphabetic character, and contains only alphanumeric characters,
hyphens and underscores, need not be quoted at all. Single quotes and double
quotes may be used to delimit more complex strings. In fact, single-quoted
and no-quoted strings are actually called <I>words</I>, but are treated
as strings for the purpose of the resource system.<HR><P>
<P>
A resource file like this is typically included in the application main file,
as if it were a normal C++ file. This eliminates the need for a separate
resource file to be distributed alongside the executable. However, the
resource file can be dynamically loaded if desired (for example by a non-C++
language such as CLIPS, Prolog or Python).<P>
Once included, the resources need to be 'parsed' (interpreted), because
so far the data is just a number of static string variables. The function
<B>::wxResourceParseData</B> is called early on in initialization of the application
(usually in <B>wxApp::OnInit</B>) with a variable as argument. This may need to be
called a number of times, one for each variable. However, more than one
resource 'object' can be stored in one string variable at a time, so you can
get all your resources into one variable if you want to.<P>
<B>::wxResourceParseData</B> parses the contents of the resource, ready for use
by functions such as <B>::wxResourceCreateBitmap</B> and <B>wxPanel::LoadFromResource</B>.<P>
If a wxWindows resource object (such as a bitmap resource) refers to a
C++ data structure, such as static XBM or XPM data, a further call (<B>
::wxResourceRegisterBitmapData</B>) needs to be made on initialization to tell
wxWindows about this data. The wxWindows resource object will refer to a
string identifier, such as 'project_data' in the example file above.
This identifier will be looked up in a table to get the C++ static data
to use for the bitmap or icon.<P>
In the C++ fragment below, the WXR resource file is included,
and appropriate resource initialization is carried out in <B>OnInit</B>.
Note that at this stage, no actual wxWindows dialogs, menus, bitmaps or
icons are created; their 'templates' are merely being set up for later
use.<P>
<PRE>
/*
* File: noname.cc
* Purpose: main application module, generated by wxBuilder.
*/
#include "wx.h"
#include "wx_help.h"
#include "noname.h"
// Includes the dialog, menu etc. resources
#include "noname.wxr"
// Includes XBM data
#include "project.xbm"
// Declare an instance of the application: allows the program to start
AppClass theApp;
// Called to initialize the program
wxFrame *AppClass::OnInit(void)
{
#ifdef wx_x
wxResourceRegisterBitmapData("project_data", project_bits, project_width, project_height);
#endif
wxResourceParseData(menuBar11);
wxResourceParseData(aiai_resource);
wxResourceParseData(project_resource);
wxResourceParseData(panel3);
...
}
</PRE>
<HR>
<A NAME="topic1138"></A>
<H3>Dialog resource format</H3>
<P>
A dialog resource object may be used for either panels or dialog boxes, and
consists of the following attributes. In the following, a <I>font specification</I>
is a list consisting of point size, family, style, weight, underlined, optional facename.<P>
<TABLE>
<TR><TD VALIGN=TOP>
Attribute
</TD>
<TD VALIGN=TOP>
Value
</TD></TR>
<TR><TD VALIGN=TOP>
name
</TD>
<TD VALIGN=TOP>
The name of the resource.
</TD></TR>
<TR><TD VALIGN=TOP>
style
</TD>
<TD VALIGN=TOP>
Optional dialog box or panel window style.
</TD></TR>
<TR><TD VALIGN=TOP>
title
</TD>
<TD VALIGN=TOP>
The title of the dialog box (unused if a panel).
</TD></TR>
.
<TR><TD VALIGN=TOP>
modal
</TD>
<TD VALIGN=TOP>
Whether modal: 1 if modal, 0 if modeless, absent if a panel resource.
</TD></TR>
<TR><TD VALIGN=TOP>
button_font
</TD>
<TD VALIGN=TOP>
The font used for control buttons: a list comprising point size (integer),
family (string), font style (string), font weight (string) and underlining (0 or 1).
</TD></TR>
<TR><TD VALIGN=TOP>
label_font
</TD>
<TD VALIGN=TOP>
The font used for control labels: a list comprising point size (integer),
family (string), font style (string), font weight (string) and underlining (0 or 1).
</TD></TR>
<TR><TD VALIGN=TOP>
x
</TD>
<TD VALIGN=TOP>
The x position of the dialog or panel.
</TD></TR>
<TR><TD VALIGN=TOP>
y
</TD>
<TD VALIGN=TOP>
The y position of the dialog or panel.
</TD></TR>
<TR><TD VALIGN=TOP>
width
</TD>
<TD VALIGN=TOP>
The width of the dialog or panel.
</TD></TR>
<TR><TD VALIGN=TOP>
height
</TD>
<TD VALIGN=TOP>
The height of the dialog or panel.
</TD></TR>
<TR><TD VALIGN=TOP>
background_colour
</TD>
<TD VALIGN=TOP>
The background colour of the dialog or panel. Only valid if the style includes wxUSER_COLOURS.
</TD></TR>
<TR><TD VALIGN=TOP>
label_colour
</TD>
<TD VALIGN=TOP>
The default label colour for the children of the dialog or panel. Only valid if the style includes wxUSER_COLOURS.
</TD></TR>
<TR><TD VALIGN=TOP>
button_colour
</TD>
<TD VALIGN=TOP>
The default button text colour for the children of the dialog or panel. Only valid if the style includes wxUSER_COLOURS.
</TD></TR>
<TR><TD VALIGN=TOP>
label_font
</TD>
<TD VALIGN=TOP>
Font spec
</TD></TR>
<TR><TD VALIGN=TOP>
button_font
</TD>
<TD VALIGN=TOP>
Font spec
</TD></TR>
</TABLE>
<P>
Then comes zero or more attributes named 'control' for each control
(panel item) on the dialog or panel. The value is a list of further
elements. In the table below, the names in the first column correspond to
the first element of the value list, and the second column details the
remaining elements of the list.<P>
<TABLE>
<TR><TD VALIGN=TOP>
Control
</TD>
<TD VALIGN=TOP>
Values
</TD></TR>
<TR><TD VALIGN=TOP>
wxButton
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, button bitmap resource (optional string), button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxCheckBox
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, default value (optional integer, 1 or 0), label font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxChoice
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, values (optional list of strings), label font spec, button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxComboBox
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, default text value, values (optional list of strings), label font spec, button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxGauge
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, value (optional integer), range (optional integer), label font spec, button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxGroupBox
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, label font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxListBox
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, values (optional list of strings), multiple (optional string, wxSINGLE or wxMULTIPLE),
label font spec, button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxMessage
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, message bitmap resource (optional string), label font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxMultiText
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, default value (optional string),
label font spec, button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxRadioBox
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, values (optional list of strings), number of rows or cols,
label font spec, button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxRadioButton
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, default value (optional integer, 1 or 0), label font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxScrollBar
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, value (optional integer),
page length (optional integer), object length (optional integer), view length (optional integer)
</TD></TR>
<TR><TD VALIGN=TOP>
wxSlider
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, value (optional integer), minimum (optional integer), maximum (optional integer),
label font spec, button font spec
</TD></TR>
<TR><TD VALIGN=TOP>
wxText
</TD>
<TD VALIGN=TOP>
title (string), window style (string), name (string), x, y, width, height, default value (optional string),
label font spec, button font spec
</TD></TR>
</TABLE>
<P>
<HR>
<A NAME="topic1139"></A>
<H3>Menubar resource format</H3>
<P>
A menubar resource object consists of the following attributes.<P>
<TABLE>
<TR><TD VALIGN=TOP>
Attribute
</TD>
<TD VALIGN=TOP>
Value
</TD></TR>
<TR><TD VALIGN=TOP>
name
</TD>
<TD VALIGN=TOP>
The name of the menubar resource.
</TD></TR>
<TR><TD VALIGN=TOP>
menu
</TD>
<TD VALIGN=TOP>
A list containing all the menus, as detailed below.
</TD></TR>
</TABLE>
<P>
The value of the <B>menu</B> attribute is a list of menu item specifications, where each menu
item specification is itself a list comprising:<P>
<UL>
<LI> title (a string)
<LI> menu item identifier (a string or non-zero integer, see below)
<LI> help string (optional)
<LI> 0 or 1 for the 'checkable' parameter (optional)
<LI> optionally, further menu item specifications if this item is a pulldown menu.
</UL>
<P>
If the menu item specification is the empty list ([]), this is interpreted as a menu separator.<P>
If further (optional) information is associated with each menu item in a future release of wxWindows,
it will be placed after the help string and before the optional pulldown menu specifications.<P>
Note that the menu item identifier must be an integer if the resource is being
included as C++ code and then parsed on initialisation. Unfortunately,
<TT>#</TT>define substitution is not performed inside strings, and
therefore the program cannot know the mapping. However, if the .WXR file
is being loaded dynamically, wxWindows will attempt to replace string
identifiers with <TT>#</TT>defined integers, because it is able to parse
the included <TT>#</TT>defines.<P>
<HR>
<A NAME="topic1140"></A>
<H3>Bitmap resource format</H3>
<P>
A bitmap resource object consists of a name attribute, and one or more <B>bitmap</B> attributes.
There can be more than one of these to allow specification of bitmaps that are optimum for the
platform and display.<P>
<UL>
<LI> Bitmap name or filename.
<LI> Type of bitmap; for example, wxBITMAP_TYPE_BMP_RESOURCE. See class reference under <B>wxBitmap</B> for
a full list).
<LI> Platform this bitmap is valid for; one of WINDOWS, X, MAC and ANY.
<LI> Number of colours (optional).
<LI> X resolution (optional).
<LI> Y resolution (optional).
</UL>
<P>
<HR>
<A NAME="topic1141"></A>
<H3>Icon resource format</H3>
<P>
An icon resource object consists of a name attribute, and one or more <B>icon</B> attributes.
There can be more than one of these to allow specification of icons that are optimum for the
platform and display.<P>
<UL>
<LI> Icon name or filename.
<LI> Type of icon; for example, wxBITMAP_TYPE_ICO_RESOURCE. See class reference under <B>wxBitmap</B> for
a full list).
<LI> Platform this bitmap is valid for; one of WINDOWS, X, MAC and ANY.
<LI> Number of colours (optional).
<LI> X resolution (optional).
<LI> Y resolution (optional).
</UL>
<P>
<HR>
<A NAME="topic1142"></A>
<H3>Resource format design issues</H3>
<P>
The .WXR file format is a recent addition and subject to change.
The use of an ASCII resource file format may seem rather inefficient, but this
choice has a number of advantages:<P>
<UL>
<LI> Since it is C++ compatible, it can be included into an application's source code,
eliminating the problems associated with distributing a separate resource file
with the executable. However, it can also be loaded dynamically from a file, which will be required
for non-C++ programs that use wxWindows.
<LI> No extra binary file format and separate converter need be maintained for the wxWindows project
(although others are welcome to add the equivalent of the Windows 'rc' resource
parser and a binary format).
<LI> It would be difficult to append a binary resource component onto an executable
in a portable way.
<LI> The file format is essentially the PrologIO object format, for which
a parser already exists, so parsing is easy. For those programs that use PrologIO
anyway, the size overhead of the parser is minimal.
</UL>
<P>
The disadvantages of the approach include:<P>
<UL>
<LI> Parsing adds a small execution overhead to program initialization.
<LI> Under 16-bit Windows especially, global data is at a premium.
Using a .RC resource table for some wxWindows resource data may be a partial solution,
although .RC strings are limited to 255 characters.
<LI> Without a resource preprocessor, it is not possible to substitute integers
for identifiers (so menu identifiers have to be written as integers in the resource
object, in addition to providing <TT>#</TT>defines for application code convenience).
</UL>
<P>
<HR>
<A NAME="topic1143"></A>
<H3>Compiling the resource system</H3>
<P>
To enable the resource system, set <B>wxUSE_WX_RESOURCES</B> to 1 in setup.h.
If your wxWindows makefile supports it, set the same name in the makefile to 1.<P>
</BODY></HTML>
|