1 \chapter{Introduction
}\label{introduction
}
2 \pagenumbering{arabic
}%
3 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
4 \setfooter{\thepage}{}{}{}{}{\thepage}%
6 \section{What is wxWindows?
}
8 wxWindows is a C++ framework providing GUI (Graphical User
9 Interface) and other facilities on more than one platform. It currently
10 supports subsets of Motif, Xt and MS Windows (
16-bit, Windows
95 and Windows NT).
12 wxWindows was originally developed at the Artificial Intelligence
13 Applications Institute, University of Edinburgh, for internal use.
14 wxWindows has been released into the public domain in the hope
15 that others will also find it useful. Version
2.0 is written and
16 maintained by Julian Smart and Markus Holzem, with support from users.
18 This manual discusses wxWindows in the context of multi-platform
19 development.
\helpignore{For more detail on the wxWindows version
2.0 API
20 (Application Programming Interface) please refer to the separate
21 wxWindows reference manual.
}
23 Please note that in the following, ``MS Windows" often refers to all
24 platforms related to Microsoft Windows, including
16-bit and
32-bit
25 variants, unless otherwise stated. All trademarks are acknowledged.
27 \section{Why another cross-platform development tool?
}
29 wxWindows was developed to provide a cheap and flexible way to maximize
30 investment in GUI application development. While a number of commercial
31 class libraries already exist for cross-platform development,
32 none met all of the following criteria:
34 \begin{enumerate
}\itemsep=
0pt
36 \item source availability;
37 \item simplicity of programming;
38 \item support for a wide range of compilers.
41 As public domain software and a project open to everyone, wxWindows has
42 benefited from comments, ideas, bug fixes, enhancements and the sheer
43 enthusiasm of users, especially via the Internet. This gives wxWindows a
44 certain advantage over its commercial brothers, and a robustness against
45 the transience of one individual or company. This openness and
46 availability of source code is especially important when the future of
47 thousands of lines of application code may depend upon the longevity of
48 the underlying class library.
50 In writing wxWindows, completeness has sometimes been traded for
51 portability and simplicity of programming. Version
2.0 goes much
52 further than previous versions in terms of generality and features,
53 allowing applications to be produced
54 that are often indistinguishable from those produced using single-platform
56 such as Motif and MFC.
58 wxWindows
2.0 currently maps to two native APIs: Motif and
59 MS Windows. An Xt port is also in preparation.
61 The importance of using a platform-independent class library cannot be
62 overstated, since GUI application development is very time-consuming,
63 and sustained popularity of particular GUIs cannot be guaranteed.
64 Code can very quickly become obsolete if it addresses the wrong
65 platform or audience. wxWindows helps to insulate the programmer from
66 these winds of change. Although wxWindows may not be suitable for
67 every application, it provides access to most of the functionality a
68 GUI program normally requires, plus some extras such as form
69 construction, interprocess communication and PostScript output, and
70 can of course be extended as needs dictate. As a bonus, it provides
71 a cleaner programming interface than the native
72 APIs. Programmers may find it worthwhile to use wxWindows even if they
73 are developing on only one platform.
75 It is impossible to sum up the functionality of wxWindows in a few paragraphs, but
76 here are some of the benefits:
78 \begin{itemize
}\itemsep=
0pt
79 \item Low cost (free, in fact!)
80 \item You get the source.
81 \item Several example programs.
82 \item Over
200 pages of printable and on-line documentation.
83 \item Simple-to-use, object-oriented API.
84 \item Graphics calls include splines, polylines, rounded rectangles, etc.
85 \item Constraint-based layout option.
86 \item Print/preview and
document/view architectures.
87 \item Status line facility, toolbar
88 \item Easy, object-oriented interprocess comms (DDE subset) under UNIX and
90 \item Encapsulated PostScript generation under UNIX, normal MS Windows printing on the
92 \item MDI support under Windows.
93 \item Can be used to create DLLs under Windows, dynamic libraries on the Sun.
94 \item Common dialogs for file browsing, printing, colour selection, etc.
95 \item Under MS Windows, support for creating metafiles and copying
96 them to the clipboard.
97 \item Hypertext help facility, with an API for invocation from applications.
98 \item Dialog Editor for building dialogs.
101 \section{Changes from version
1.xx
}\label{versionchanges
}
103 These are a few of the major differences between versions
1.xx and
2.0.
107 \begin{itemize
}\itemsep=
0pt
108 \item XView is no longer supported;
109 \item Mac is not yet supported;
110 \item all controls (panel items) no longer have labels attached to them;
111 \item wxForm removed;
112 \item wxCanvasDC, wxPanelDC removed (replaced by wxClientDC, wxWindowDC, wxPaintDC which
113 can be used for any window);
114 \item wxMultiText, wxTextWindow, wxText removed and replaced by wxTextCtrl;
115 \item classes no longer divided into generic and platform-specific parts, for efficiency.
118 Additions and changes:
120 \begin{itemize
}\itemsep=
0pt
121 \item class hierarchy changed, and restrictions about subwindow nesting lifted;
122 \item header files reorganised to conform to normal C++ standards;
123 \item classes less dependent on each another, to reduce executable size;
124 \item wxString used instead of char* wherever possible;
125 \item the number of separate but mandatory utilities reduced;
126 \item the event system has been overhauled, with
127 virtual functions and callbacks being replaced with MFC-like event tables;
128 \item new controls, such as wxTreeCtrl, wxListCtrl, wxSpinButton;
129 \item less inconsistency about what events can be handled, so for example
130 mouse clicks or key presses on controls can now be intercepted;
131 \item the status bar is now a separate class, wxStatusBar, and is
132 implemented in generic wxWindows code;
133 \item some renaming of controls for greater consistency;
134 \item wxBitmap has the notion of bitmap handlers to allow for extension to new formats
136 \item new dialogs: wxPageSetupDialog, wxFileDialog, wxDirDialog,
137 wxMessageDialog, wxSingleChoiceDialog, wxTextEntryDialog;
138 \item GDI objects are reference-counted and are now passed to most functions
139 by reference, making memory management far easier;
140 \item wxSystemSettings class allows querying for various system-wide properties
141 such as dialog font, colours, user interface element sizes, and so on;
142 \item better platform look and feel conformance;
143 \item toolbar functionality now separated out into a family of classes with the
145 \item device contexts are no longer accessed using wxWindow::GetDC - they are created
146 temporarily with the window as an argument;
147 \item events from sliders and scrollbars can be handled more flexibly;
148 \item the handling of window close events has been changed in line with the new
149 event system, but backward
{\bf OnClose
} compatibility has been retained;
150 \item the concept of
{\it validator
} has been added to allow much easier coding of
151 the relationship between controls and application data;
152 \item the documentation has been revised, with more cross-referencing.
155 Platform-specific changes:
157 \begin{itemize
}\itemsep=
0pt
158 \item The Windows header file (windows.h) is no longer included by wxWindows headers;
159 \item wx.dll supported under Visual C++;
160 \item the full range of Windows
95 window decorations are supported, such as modal frame
162 \item MDI classes brought out of wxFrame into separate classes, and made more flexible.
165 \section{wxWindows requirements
}\label{requirements
}
167 To make use of wxWindows, you currently need one or both of the
172 \begin{enumerate
}\itemsep=
0pt
173 \item A
486 or higher PC running MS Windows.
174 \item One of Microsoft Visual C++, Borland C++, Watcom C++, MetroWerks C++,
175 Symantec C++, GNU-WIN32.
176 \item At least
30 MB of disk space.
181 \begin{enumerate
}\itemsep=
0pt
182 \item Almost any C++ compiler, including GNU C++.
183 \item Almost any UNIX workstation (VMS is supported too) and Motif
1.2 or higher (not necessary
185 \item At least
30 MB of disk space.
188 \section{Availability and location of wxWindows
}
190 wxWindows is currently available from the Artificial Intelligence
191 Applications Institute by anonymous FTP and World Wide Web:
194 ftp://ftp.aiai.ed.ac.uk/pub/packages/wxwin
195 http://web.ukonline.co.uk/julian.smart/wxwin
198 \section{Acknowledgments
}
200 Thanks are due to the AIAI for being willing to release wxWindows into
201 the public domain, and to our patient wives Harriet and Tanja.
203 The Internet has been an essential prop when coming up against tricky
204 problems. Thanks to those who answered our
205 queries or submitted bug fixes and enhancements; wxWindows is very
208 Hermann Dunkel contributed XPM support; Arthur Seaton wrote the memory
209 checking code; Olaf Klein and Patrick Halke wrote the ODBC classes;
210 Harri Pasanen and Robin Dunn wrote wxPython and contributed to the
213 Markus Holzem write the Xt port. Jonathan Tonberg, Bill Hale,
214 Cecil Coupe, Thomaso Paoletti, Thomas Fettig, and others slaved away
215 writing the Mac port. Keith Gary Boyce ported wxWindows to the free
216 GNU-WIN32 compiler, refusing to give up when shortcuts were suggested.
218 Many thanks also to: Timothy Peters, Jamshid Afshar, Patrick Albert, C. Buckley,
219 Robin Corbet, Harco de Hilster, Josep Fortiana, Torsten Liermann, Tatu
220 M\"
{a
}nnist\"
{o
}, Ian Perrigo, Giordano Pezzoli, Petr Smilauer, Neil Smith,
221 Kari Syst\"
{a
}, Jyrki Tuomi, Edward Zimmermann, Ian Brown, and many
224 `Graphplace', the basis for the wxGraphLayout library, is copyright Dr. Jos
225 T.J. van Eijndhoven of Eindhoven University of Technology. The code has
226 been used in wxGraphLayout with his permission.
228 We also acknowledge the author of XFIG, the excellent UNIX drawing tool,
229 from the source of which we have borrowed some spline drawing code.
230 His copyright is included below.
232 {\it XFig2.1 is copyright (c)
1985 by Supoj Sutanthavibul. Permission to
233 use, copy, modify, distribute, and sell this software and its
234 documentation for any purpose is hereby granted without fee, provided
235 that the above copyright notice appear in all copies and that both that
236 copyright notice and this permission notice appear in supporting
237 documentation, and that the name of M.I.T. not be used in advertising or
238 publicity pertaining to distribution of the software without specific,
239 written prior permission. M.I.T. makes no representations about the
240 suitability of this software for any purpose. It is provided ``as is''
241 without express or implied warranty.
}
243 \chapter{Multi-platform development with wxWindows
}\label{multiplat
}
244 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
245 \setfooter{\thepage}{}{}{}{}{\thepage}%
247 This chapter describes the practical details of using wxWindows. Please
248 see the file install.txt for up-to-date installation instructions, and
249 changes.txt for differences between versions.
251 \section{Include files
}
253 The main include file is
{\tt "wx.h"
}; this includes the most commonly
254 used modules of wxWindows.
256 To save on compilation time, include only those header files relevant to the
257 source file. If you are using precompiled headers, you should include
258 the following section before any other includes:
261 // For compilers that support precompilation, includes "wx.h".
269 ... include minimum set of files necessary here ...
272 ... now your other include files ...
275 The file
{\tt "wx
\_prec.h"
} includes
{\tt "wx.h"
}. Although this incantation
276 may seem quirky, it is in fact the end result of a lot of experimentation,
277 and several Windows compilers to use precompilation (those tested are Microsoft Visual C++, Borland C++
280 Borland precompilation is largely automatic. Visual C++ requires specification of
{\tt "wx
\_prec.h"
} as
281 the file to use for precompilation. Watcom C++ is automatic apart from the specification of
282 the .pch file. Watcom C++ is strange in requiring the precompiled header to be used only for
283 object files compiled in the same directory as that in which the precompiled header was created.
284 Therefore, the wxWindows Watcom C++ makefiles go through hoops deleting and recreating
285 a single precompiled header file for each module, thus preventing an accumulation of many
286 multi-megabyte .pch files.
290 Under UNIX, use the library libwx
\_motif.a
291 (Motif). Under Windows, use the library wx.lib for stand-alone Windows
292 applications, or wxdll.lib for creating DLLs.
294 \section{Configuration
}
296 The following lists the options configurable in the file
297 \rtfsp{\tt include/base/wx
\_setup.h.
} Some settings are a matter
298 of taste, some help with platform-specific problems, and
299 others can be set to minimize the size of the library.
301 \subsection{General features
}
303 \begin{twocollist
}\itemsep=
0pt
304 \twocolitem{USE
\_CLIPBOARD}{If
1, clipboard code is compiled (Windows only).
}
305 \twocolitem{USE
\_CONSTRAINTS}{If
1, the constaint-based window layout system is compiled.
}
306 \twocolitem{USE
\_DOC\_VIEW\_ARCHITECTURE}{If
1, wxDocument, wxView and related classes are compiled.
}
307 \twocolitem{USE
\_DYNAMIC\_CLASSES}{If
1, the run-time class macros and classes are compiled. Recommended,
308 and necessary for the
document/view framework.
}
309 \twocolitem{USE
\_EXTENDED\_STATICS}{If
1, wxStaticItem code is compiled for enhanced panel decorative items.
310 Not rigorously tested, and not documented.
}
311 \twocolitem{USE
\_HELP}{If
1, interface to help system is compiled.
}
312 \twocolitem{USE
\_GAUGE}{If
1, the wxGauge class compiled.
}
313 \twocolitem{USE
\_GLOBAL\_MEMORY\_OPERATORS}{If
1, redefines global new and delete operators to be compatible
314 with the extended arguments of the debugging wxObject new and delete operators. If this causes problems
315 for your compiler, set to
0.
}
316 \twocolitem{USE
\_GNU\_WXSTRING}{If
1, the enhanced GNU wxString and regular expression class are compiled
317 in place of the normal wxString class. See contrib/wxstring for details.
}
318 \twocolitem{USE
\_IMAGE\_LOADING\_IN\_MSW}{Use code to allow dynamic .BMP loading
320 \twocolitem{USE
\_IMAGE\_LOADING\_IN\_X}{Use code in utils/image to allow dynamic .BMP/.GIF loading
322 \twocolitem{USE
\_RESOURCE\_LOADING\_IN\_MSW}{Use code to allow dynamic .ICO/.CUR loading
324 \twocolitem{USE
\_IPC}{If
1, interprocess communication code is compiled.
}
325 \twocolitem{USE
\_MEMORY\_TRACING}{If
1, enables debugging versions of wxObject::new and wxObject::delete
326 if the value of DEBUG is defined to more than
0.
}
327 \twocolitem{USE
\_METAFILE}{If
1, Windows Metafile code is compiled.
}
328 \twocolitem{USE
\_PANEL\_IN\_PANEL}{If
1, experimental panel-in-panel code is used
329 for common dialog boxes. Not recommended, since tab traversal can suffer.
}
330 \twocolitem{USE
\_POSTSCRIPT}{If
1, PostScript code is compiled.
}
331 \twocolitem{USE
\_POSTSCRIPT\_ARCHITECTURE\_IN\_MSW}{Set to
1 to enable the printing architecture
332 to make use of either native Windows printing facilities, or the wxPostScriptDC class depending
333 on the wxApp::SetPrintMode setting.
}
334 \twocolitem{USE
\_PRINTING\_ARCHITECTURE}{If
1, wxPrinter, wxPrintout and related classes are compiled
335 for the print/preview framework.
}
336 \twocolitem{USE
\_RESOURCES}{If
1, win.ini or .Xdefaults-style resource read/write code is compiled.
}
337 \twocolitem{USE
\_SCROLLBAR}{If
1, wxScrollBar class is compiled. Not rigorously tested, and not documented.
}
338 \twocolitem{USE
\_SPLINES}{If
1, spline code is compiled.
}
339 \twocolitem{USE
\_TOOLBAR}{If
1, the wxToolBar class is compiled.
}
340 \twocolitem{USE
\_TYPEDEFS}{If
1, a typedef will be used for wxPoint instead of
341 a class declaration, to reduce overhead and avoid a Microsoft C++ memory bug.
}
342 \twocolitem{USE
\_VLBOX}{If
1, wxVirtListBox code is compiled for a virtual listbox item.
343 Not rigorously tested, and not documented.
}
344 \twocolitem{USE
\_WX\_RESOURCES}{If
1, wxWindows resource file (.WXR) code is compiled.
}
345 \twocolitem{USE
\_XFIG\_SPLINE\_CODE}{If
1, XFig-derived code is used for spline
346 drawing. If
0, AIAI code is used, which is slower.
}
347 \twocolitem{USE
\_XPM\_IN\_X}{If
1, XPM (colour pixmap) facilities will be compiled and used
348 in wxBitmap under X.
}
349 \twocolitem{USE
\_XPM\_IN\_MSW}{If
1, XPM (colour pixmap) facilities will be compiled and used
350 in wxBitmap under MS Windows.
}
353 \subsection{X features
}
356 \twocolitem{DEFAULT
\_FILE\_SELECTOR\_SIZE}{Let Motif choose the size of
357 XmFileSelectionBox. Otherwise, size is
500x600.
}
358 \twocolitem{PIXEL0
\_DISABLE}{Define to disallow allocation of pixel
0 (wxXOR problem).
}
359 \twocolitem{USE
\_GADGETS}{Use gadgets where possible rather than Widgets for items.
360 Default is to use Gadgets.
}
361 \twocolitem{USE
\_BUTTON\_GADGET}{Use gadgets for buttons. This can intefere with
362 default button selection, so the default is zero.
}
365 \subsection{Windows and NT features
}
368 \twocolitem{CTL3D
}{CTL3D should only be used for
16-bit Windows programs.
369 On Windows
95 and NT, native
3D effects are used. If you want to
370 use it and don't already have CTL3D installed, copy the files in
371 contrib/ctl3d to appropriate places (ctl3dv2.lib/ctl3d32.lib into your compiler lib
372 directory, ctl3d.h into an include directory, and ctl3dv2.dll into
373 windows/system). You may need to find a compiler-specific version of ctl3dv2.lib
374 or ctl3d32.lib. Define CTL3D to be
1 in wx
\_setup.h and link your executables with ctl3dv2.lib
376 \twocolitem{USE
\_ITSY\_BITSY}{If
1, compiles in code to support tiny window titlebars.
}
377 \twocolitem{USE
\_ODBC}{If
1, compiles wxDatabase and wxRecordSet classes for ODBC
378 access. Requires sql.h, sqlext.h files if set to
1 (see topic on database support).
}
383 At the moment there is no attempt to make UNIX makefiles and
384 PC makefiles compatible, i.e. one makefile is required for
387 Sample makefiles for UNIX (suffix .UNX), MS C++ (suffix .DOS and .NT), Borland
388 C++ (.BCC) and Symantec C++ (.SC) are included for the library, demos
389 and utilities. The NT, Borland and Symantec makefiles cannot be
390 guaranteed to be up-to-date since the author does not have
393 The controlling makefile for wxWindows is in the platform-specific
394 directory, such as
{\tt src/msw
} or
{\tt src/x
}. This makefile will
395 recursively execute the makefile in
{\tt src/base
}.
397 \subsection{Windows makefiles
}
399 For Microsoft C++, normally it is only necessary to type
{\tt nmake -f
400 makefile.dos
} (or an alias or batch file which does this). By default,
401 binaries are made with debugging information, and no optimization. Use
402 FINAL=
1 on the command line to remove debugging information (this only
403 really necessary at the link stage), and DLL=
1 to make a DLL version of
404 the library, if building a library.
406 \subsection{UNIX makefiles
}
410 Debugging information is included by default; you may add DEBUG= as an
411 argument to make to compile without it, or use the UNIX
{\bf strip
}
412 command to remove debugging information from an executable.
414 \normalbox{{\it Important note:
} Most compiler flags are kept centrally in
415 src/make.env, which is included by all other makefiles. This is the
416 file to edit to tailor wxWindows compilation to your environment.
}
418 \section{Windows-specific files
}
420 wxWindows application compilation under MS Windows requires at least two
421 extra files, resource and module definition files.
423 \subsection{Resource file
}\label{resources
}
425 The least that must be defined in the Windows resource file (extension RC)
426 is the following statement:
432 which includes essential internal wxWindows definitions. The resource script
433 may also contain references to icons, cursors, etc., for example:
439 The icon can then be referenced by name when creating a frame icon. See
440 the MS Windows SDK documentation.
442 \normalbox{Note: include wx.rc
{\it after
} any ICON statements
443 so programs that search your executable for icons (such
444 as the Program Manager) find your application icon first.
}
446 \subsection{Module definition file
}
448 A module definition file (extension DEF) looks like the following:
455 CODE PRELOAD MOVEABLE DISCARDABLE
456 DATA PRELOAD MOVEABLE MULTIPLE
461 The only lines which will usually have to be changed per application are
462 NAME and DESCRIPTION.
464 \section{Memory models and memory allocation
}\label{memorymodels
}
466 Under UNIX, memory allocation isn't a problem. Under Windows, the only
467 really viable way to go is to use the large model, which uses the global
468 heap instead of the local heap for memory allocation. Unless more than
469 one read-write data segment is used,
% (see \helpref{large data}{largedata}
470 large model programs may still have multiple instances under MS
471 C/C++
7. Microsoft give the following guidelines for producing
472 multiple-instance large model programs:
474 \begin{itemize
}\itemsep=
0pt
475 \item Do not use
{\tt /ND
} to name extra data segments unless the segment is READONLY.
476 \item Use the .DEF file to mark extra data segments READONLY.
477 \item Do not use
\_\_far or FAR to mark data items.
478 \item Use
{\tt /PACKDATA
} to combine data segments.
479 \item Use
{\tt /Gt65500 /Gx
} to force all data into the default data segment.
482 Even with the single-instance limitation, the productivity benefit is
483 worth it in the majority of cases. Note that some other multi-platform
484 class libraries also have this restriction. (If more than one instance
485 really is required, create several copies of the program with different
488 Having chosen the large model, just use C++ `new', `delete' (and if
489 necessary `malloc' and `free') in the normal way. The only restrictions
490 now encountered are a maximum of
64 KB for a single program segment and
491 for a single data item, unless huge model is selected.
493 For Borland users, use the data threshold switch, and the following is
496 \begin{itemize
}\itemsep=
0pt
497 \item Check ``Automatic Far Data Segments"
498 \item Check ``Put Constant Strings into Code Segment"
501 See also the Frequently Asked Questions
document for further details
502 on using Borland with wxWindows.
504 \subsection{Allocating and deleting wxWindows objects
}
506 In general, classes derived from wxWindow must dynamically allocated
507 with
{\it new
} and deleted with
{\it delete
}. If you delete a window,
508 all of its children and descendants will be automatically deleted,
509 so you don't need to delete these descendants explicitly.
511 Don't statically create a window unless you know that the window
512 cannot be deleted dynamically. Modal dialogs, such as those used
513 in the
{\tt dialogs
} sample, can usually be created statically,
514 if you know that the OK or Cancel button does not destroy the dialog.
516 Most drawing objects, such as wxPen, wxBrush, wxFont, and wxBitmap, should be
517 created dynamically. They are cleaned up automatically on program exit.
518 wxColourMap is an exception to this rule (currently). In particular,
519 do not attempt to create these objects globally before OnInit() has a chance
520 to be called, because wxWindows might not have done essential internal initialisation
521 (including creation of lists containing all instances of wxPen, wxBrush etc.)
523 If you decide to allocate a C++ array of objects (such as wxBitmap) that may
524 be cleaned up by wxWindows, make sure you delete the array explicitly
525 before wxWindows has a chance to do so on exit, since calling
{\it delete
} on
526 array members will cause memory problems.
528 wxColour can be created statically: it is not automatically cleaned
529 up and is unlikely to be shared between other objects; it is lightweight
530 enough for copies to be made.
532 Beware of deleting objects such as a wxPen or wxBitmap if they are still in use.
533 Windows is particularly sensitive to this: so make sure you
534 make calls like wxDC::SetPen(NULL) or wxDC::SelectObject(NULL) before deleting
535 a drawing object that may be in use. Code that doesn't do this will probably work
536 fine on some platforms, and then fail under Windows.
538 \section{Dynamic Link Libraries
}
540 wxWindows may be used to produce DLLs which run under MS Windows. Note that
541 this is not the same thing as having wxWindows as a DLL, which is not
542 currently possible. For Microsoft C++, use the makefile with the argument DLL=
1 to produce
543 a version of the wxWindows library which may be used in a DLL application.
544 There is a bug in Microsoft C++ which makes the compiler complain about returned floats,
545 which goes away when the
{\tt /Os
} option is used, which is why that flag is
548 For making wxWindows as a Sun dynamic library, there are comments in the
549 UNIX makefile for the appropriate flags for AT\&T C++. Sorry, I haven't
550 investigated the flags needed for other compilers.
552 \section{Conditional compilation
}
554 One of the purposes of wxWindows is to reduce the need for conditional
555 compilation in source code, which can be messy and confusing to follow.
556 However, sometimes it is necessary to incorporate platform-specific
557 features (such as metafile use under MS Windows). The following identifiers
558 may be used for this purpose, along with any user-supplied ones:
561 \item {\tt wx
\_x} - for code which should work under any X toolkit
562 \item {\tt wx
\_motif} - for code which should work under Motif only
563 \item {\tt wx
\_msw} - for code which should work under Microsoft Windows only
564 \item {\tt wx
\_xt} - for code which should work under Xt only
572 (void)wxMessageBox("Sorry, metafiles not available under X.");
577 wxMetaFile *mf = dc.Close();
584 \section{Building on-line help
}
586 wxWindows has its own help system from version
1.30: wxHelp. It can be
587 used to view the wxWindows class library reference, and also to provide
588 on-line help for your wxWindows applications. The API, made accessible
589 by including
{\tt wx
\_help.h
}, allows you to load files and display
590 specific sections, using DDE to communicate between the application and
593 wxHelp files can be marked up by hand from ASCII files within wxHelp,
594 or may be generated from other files, as is the case with the wxWindows
597 It is possible to use the platform-specific help
598 system (e.g. WinHelp) instead of wxHelp.
600 See
{\tt install.txt
}, the wxHelp documentation (in
{\tt
601 utils/wxhelp/docs
}) and
\helpref{wxHelp
}{wxhelp
} for further details.
605 There are cases where a C++ program will compile and run fine under one
606 environment, and then fail to compile using a different compiler. Some
607 caveats are given below, from experience with the GNU C++ compiler (GCC)
608 and MS C/C++ compiler version
7.
610 \subsection{Templates
}
612 wxWindows does not use templates for two main reasons: one, it is a
613 notoriously unportable feature, and two, the author is irrationally
614 suspicious of them and prefers to use casts. More compilers are
615 now implementing templates, and so it will probably be safe to use
616 them soon without fear of portability problems.
618 \subsection{Precompiled headers
}
620 Some compilers, such as Borland C++ and Microsoft C++, support
621 precompiled headers. This can save a great deal of compiling time. The
622 recommended approach is to precompile
{\tt ``wx.h''
}, using this
623 precompiled header for compiling both wxWindows itself and any
624 wxWindows applications. For Windows compilers, two dummy source files
625 are provided (one for normal applications and one for creating DLLs)
626 to allow initial creation of the precompiled header.
628 However, there are several downsides to using precompiled headers. One
629 is that to take advantage of the facility, you often need to include
630 more header files than would normally be the case. This means that
631 changing a header file will cause more recompilations (in the case of
632 wxWindows, everything needs to be recompiled since everything includes
635 A related problem is that for compilers that don't have precompiled
636 headers, including a lot of header files slows down compilation
637 considerably. For this reason, you will find (in the common
638 X and Windows parts of the library) conditional
639 compilation that under UNIX, includes a minimal set of headers;
640 and when using Visual C++, includes
{\tt wx.h
}. This should help provide
641 the optimal compilation for each compiler, although it is
642 biassed towards the precompiled headers facility available
645 \section{File handling
}
647 When building an application which may be used under different
648 environments, one difficulty is coping with documents which may be
649 moved to different directories on other machines. Saving a file which
650 has pointers to full pathnames is going to be inherently unportable. One
651 approach is to store filenames on their own, with no directory
652 information. The application searches through a number of locally
653 defined directories to find the file. To support this, the class
{\bf
654 wxPathList
} makes adding directories and searching for files easy, and
655 the global function
{\bf FileNameFromPath
} allows the application to
656 strip off the filename from the path if the filename must be stored.
657 This has undesirable ramifications for people who have documents of the
658 same name in different directories.
660 As regards the limitations of DOS
8+
3 single-case filenames versus
661 unrestricted UNIX filenames, the best solution is to use DOS filenames
662 for your application, and also for
document filenames
{\it if
} the user
663 is likely to be switching platforms regularly. Obviously this latter
664 choice is up to the application user to decide. Some programs (such as
665 YACC and LEX) generate filenames incompatible with DOS; the best
666 solution here is to have your UNIX makefile rename the generated files
667 to something more compatible before transferring the source to DOS.
668 Transferring DOS files to UNIX is no problem, of course, apart from EOL
669 conversion for which there should be a utility available (such as
672 See also the File Functions section of the reference manual for
673 descriptions of miscellaneous file handling functions.
675 \chapter{Utilities supplied with wxWindows
}\label{utilities
}
676 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
677 \setfooter{\thepage}{}{}{}{}{\thepage}%
679 A number of `extras' are supplied with wxWindows, to complement
680 the GUI functionality in the main class library. These are found
681 below the utils directory and usually have their own source, library
682 and documentation directories. For larger user-contributed packages,
683 see the directory /pub/packages/wxwin/contrib.
685 \section{wxHelp
}\label{wxhelp
}
687 wxHelp is a stand-alone program, written using wxWindows,
688 for displaying hypertext help. It is necessary since not all target
689 systems (notably X) supply an adequate
690 standard for on-line help. wxHelp is modelled on the MS Windows help
691 system, with contents, search and browse buttons, but does not reformat
692 text to suit the size of window, as WinHelp does, and its input files
693 are uncompressed ASCII with some embedded font commands and an .xlp
694 extension. Most wxWindows documentation (user manuals and class
695 references) is supplied in wxHelp format, and also in Windows Help
698 Note that an application can be programmed to use Windows Help under
699 MS Windows, and wxHelp under X. An alternative help viewer under X is
700 Mosaic, a World Wide Web viewer that uses HTML as its native hypertext
701 format. However, this is not currently integrated with wxWindows
704 wxHelp works in two modes---edit and end-user. In edit mode, an ASCII
705 file may be marked up with different fonts and colours, and divided into
706 sections. In end-user mode, no editing is possible, and the user browses
707 principally by clicking on highlighted blocks.
709 When an application invokes wxHelp, subsequent sections, blocks or
710 files may be viewed using the same instance of wxHelp since the two
711 programs are linked using wxWindows interprocess communication
712 facilities. When the application exits, that application's instance of
713 wxHelp may be made to exit also. See the
{\bf wxHelpControllerBase
} entry in the
714 reference section for how an application controls wxHelp.
716 \section{Tex2RTF
}\label{textortf
}
718 Supplied with wxWindows is a utility called Tex2RTF for converting
\rtfsp
719 \LaTeX\ manuals to the following formats:
723 wxWindows help system format (XLP).
725 Rich Text Format suitable for importing into a word processor.
726 \item[Windows Help RTF
]
727 Rich Text Format suitable for compiling into a WinHelp HLP file with the
730 HTML is the native format for Mosaic, the main hypertext viewer for
731 the World Wide Web. Since it is freely available it is a good candidate
732 for being the wxWindows help system under X, as an alternative to wxHelp.
735 Tex2RTF is used for the wxWindows manuals and can be used independently
736 by authors wishing to create on-line and printed manuals from the same
\rtfsp
737 \LaTeX\ source. Please see the separate documentation for Tex2RTF.
739 \section{wxTreeLayout
}
741 This is a simple class library for drawing trees in a reasonably pretty
742 fashion. It provides only minimal default drawing capabilities, since
743 the algorithm is meant to be used for implementing custom tree-based
746 Directed graphs may also be drawn using this library, if cycles are
747 removed before the nodes and arcs are passed to the algorithm.
749 Tree displays are used in many applications: directory browsers,
750 hypertext systems, class browsers, and decision trees are a few
753 See the separate manual and the directory utils/wxtree.
755 \section{wxGraphLayout
}
757 The wxGraphLayout class is based on a tool called `graphplace' by Dr.
758 Jos T.J. van Eijndhoven of Eindhoven University of Technology. Given a
759 (possibly cyclic) directed graph, it does its best to lay out the nodes
760 in a sensible manner. There are many applications (such as diagramming)
761 where it is required to display a graph with no human intervention. Even
762 if manual repositioning is later required, this algorithm can make a good
765 See the separate manual and the directory utils/wxgraph.
767 \section{wxImage
}\label{wximage
}
769 This is a collection of GIF/BMP/XBM bitmap loading and displaying
772 \section{MFUTILS
}\label{mfutils
}
774 A very modest step towards reading Windows metafiles on the
775 any platform. Julian Smart's ClockWorks program demonstrates
776 how extremely simple metafiles may be read and displayed (in this
777 case, to be used as clock hands).
779 \section{Colours
}\label{coloursampler
}
781 A colour sampler for viewing colours and their names on each
785 \chapter{Tutorial
}\label{tutorial
}
786 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
787 \setfooter{\thepage}{}{}{}{}{\thepage}%
791 \chapter{Programming strategies
}\label{strategies
}
792 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
793 \setfooter{\thepage}{}{}{}{}{\thepage}%
795 This chapter is intended to list strategies that may be useful when
796 writing and debugging wxWindows programs. If you have any good tips,
797 please submit them for inclusion here.
799 \section{Strategies for reducing programming errors
}
801 \subsection{Use ASSERT
}
803 Although I haven't done this myself within wxWindows, it is good
804 practice to use ASSERT statements liberally, that check for conditions that
805 should or should not hold, and print out appropriate error messages.
806 These can be compiled out of a non-debugging version of wxWindows
807 and your application. Using ASSERT is an example of `defensive programming':
808 it can alert you to problems later on.
810 \subsection{Use wxString in preference to character arrays
}
812 Using wxString can be much safer and more convenient than using char *.
813 Again, I haven't practised what I'm preaching, but I'm now trying to use
814 wxString wherever possible. You can reduce the possibility of memory
815 leaks substantially, and it's much more convenient to use the overloaded
816 operators than functions such as strcmp. wxString won't add a significant
817 overhead to your program; the overhead is compensated for by easier
818 manipulation (which means less code).
820 The same goes for other data types: use classes wherever possible.
822 \section{Strategies for portability
}
824 \subsection{Use relative positioning or constraints
}
826 Don't use absolute panel item positioning if you can avoid it. Different GUIs have
827 very differently sized panel items. Consider using the constraint system, although this
828 can be complex to program. If you needs are simple, the default relative positioning
829 behaviour may be adequate (using default position values and wxPanel::NewLine).
831 Alternatively, you could use alternative .wrc (wxWindows resource files) on different
832 platforms, with slightly different dimensions in each. Or space your panel items out
835 \subsection{Use wxWindows resource files
}
837 Use .wrc (wxWindows resource files) where possible, because they can be easily changed
838 independently of source code. Bitmap resources can be set up to load different
839 kinds of bitmap depending on platform (see the section on resource files).
841 \section{Strategies for debugging
}
843 \subsection{Positive thinking
}
845 It's common to blow up the problem in one's imagination, so that it seems to threaten
846 weeks, months or even years of work. The problem you face may seem insurmountable:
847 but almost never is. Once you have been programming for some time, you will be able
848 to remember similar incidents that threw you into the depths of despair. But
849 remember, you always solved the problem, somehow!
851 Perseverance is often the key, even though a seemingly trivial problem
852 can take an apparently inordinate amount of time to solve. In the end,
853 you will probably wonder why you worried so much. That's not to say it
854 isn't painful at the time. Try not to worry -- there are many more important
857 \subsection{Simplify the problem
}
859 Reduce the code exhibiting the problem to the smallest program possible
860 that exhibits the problem. If it is not possible to reduce a large and
861 complex program to a very small program, then try to ensure your code
862 doesn't hide the problem (you may have attempted to minimize the problem
863 in some way: but now you want to expose it).
865 With luck, you can add a small amount of code that causes the program
866 to go from functioning to non-functioning state. This should give a clue
867 to the problem. In some cases though, such as memory leaks or wrong
868 deallocation, this can still give totally spurious results!
870 \subsection{Genetic mutation
}
872 If we had sophisticated genetic algorithm tools that could be applied
873 to programming, we could use them. Until then, a common -- if rather irrational --
874 technique is to just make arbitrary changes to the code until something
875 different happens. You may have an intuition why a change will make a difference;
876 otherwise, just try altering the order of code, comment lines out, anything
877 to get over an impasse. Obviously, this is usually a last resort.
879 \subsection{Use a debugger
}
881 This sounds like facetious advice, but it's surprising how often people
882 don't use a debugger. Often it's an overhead to install or learn how to
883 use a debugger, but it really is essential for anything but the most
884 trivial programs. Some platforms don't allow for debugging, such
885 as WIN32s under Windows
3.x. In this case, you might be advised to
886 debug under
16-bit Windows and when you're confident, compile for
887 WIN32s. In fact WIN32s can be very strict about bad memory handling,
888 so testing out under WIN32s is a good thing to do even if you're
889 not going to distribute this version. (Unless you've got a good memory checking,
890 utility, of course!) Tracking bugs under WIN32s can involve a lot of debug message
891 insertion and relinking, so make sure your compiler has a fast linker
892 (e.g. Watcom, Symantec).
894 \subsection{Use tracing code
}
896 You can use wxDebugMsg statements (or the wxDebugStreamBuf class) to
897 output to a debugging window such as DBWIN under Windows, or standard
898 error under X. If compiling in DEBUG mode, you can use TRACE statements
899 that will be compiled out of the final build of your application.
901 Using tracing statements may be more convenient than using the debugger
902 in some circumstances (such as when your debugger doesn't support a lot
903 of debugging code, or you wish to print a bunch of variables).
905 \subsection{Use wxObject::Dump and the wxDebugContext class
}
907 It's good practice to implement the Dump member function for all
908 classes derived from wxObject. You can then make use of wxDebugContext
909 to dump out information on all objects in the program, if DEBUG is
910 defined to be more than zero. You can use wxDebugContext to check for
911 memory leaks and corrupt memory. See the debugging topic in the
912 reference manual for more information.
914 \subsection{Check Windows debug messages
}
916 Under Windows, it's worth running your program with DBWIN running or
917 some other program that shows Windows-generated debug messages. It's
918 possible it'll show invalid handles being used. You may have fun seeing
919 what commercial programs cause these normally hidden errors! Microsoft
920 recommend using the debugging version of Windows, which shows up even
921 more problems. However, I doubt it's worth the hassle for most
922 applications. wxWindows is designed to minimize the possibility of such
923 errors, but they can still happen occasionally, slipping through unnoticed
924 because they are not severe enough to cause a crash.