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. Version
2 currently
10 supports MS Windows (
16-bit, Windows
95 and Windows NT), Unix with GTK+, Unix with Motif,
11 and Mac. An OS/
2 port is in progress.
13 wxWindows was originally developed at the Artificial Intelligence
14 Applications Institute, University of Edinburgh, for internal use,
15 and was first made publicly available in
1993.
16 Version
2 is a vastly improved version written and maintained by
17 Julian Smart, Robert Roebling, Vadim Zeitlin and many others.
19 This manual discusses wxWindows in the context of multi-platform
20 development.
\helpignore{For more detail on the wxWindows version
2.0 API
21 (Application Programming Interface) please refer to the separate
22 wxWindows reference manual.
}
24 Please note that in the following, ``MS Windows" often refers to all
25 platforms related to Microsoft Windows, including
16-bit and
32-bit
26 variants, unless otherwise stated. All trademarks are acknowledged.
28 \section{Why another cross-platform development tool?
}
30 wxWindows was developed to provide a cheap and flexible way to maximize
31 investment in GUI application development. While a number of commercial
32 class libraries already existed for cross-platform development,
33 none met all of the following criteria:
35 \begin{enumerate
}\itemsep=
0pt
37 \item source availability;
38 \item simplicity of programming;
39 \item support for a wide range of compilers.
42 Since wxWindows was started, several other free or almost-free GUI frameworks have
43 emerged. However, none has the range of features, flexibility, documentation and the
44 well-established development team that wxWindows has.
46 As open source software, wxWindows has
47 benefited from comments, ideas, bug fixes, enhancements and the sheer
48 enthusiasm of users. This gives wxWindows a
49 certain advantage over its commercial competitors (and over free libraries
50 without an independent development team), plus a robustness against
51 the transience of one individual or company. This openness and
52 availability of source code is especially important when the future of
53 thousands of lines of application code may depend upon the longevity of
54 the underlying class library.
56 Version
2 goes much further than previous versions in terms of generality and features,
57 allowing applications to be produced
58 that are often indistinguishable from those produced using single-platform
59 toolkits such as Motif, GTK+ and MFC.
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 (such as an OLE-intensive program), it provides access to most of the functionality a
68 GUI program normally requires, plus many extras such as network programming,
69 PostScript output, and HTML rendering; and it can of course be extended as needs dictate. As a bonus, it provides
70 a far cleaner and easier programming interface than the native
71 APIs. Programmers may find it worthwhile to use wxWindows even if they
72 are developing on only one platform.
74 It is impossible to sum up the functionality of wxWindows in a few paragraphs, but
75 here are some of the benefits:
77 \begin{itemize
}\itemsep=
0pt
78 \item Low cost (free, in fact!)
79 \item You get the source.
80 \item Available on a variety of popular platforms.
81 \item Works with almost all popular C++ compilers and Python.
82 \item Over
50 example programs.
83 \item Over
1000 pages of printable and on-line documentation.
84 \item Includes Tex2RTF, to allow you to produce your own documentation
85 in Windows Help, HTML and Word RTF formats.
86 \item Simple-to-use, object-oriented API.
87 \item Flexible event system.
88 \item Graphics calls include lines, rounded rectangles, splines, polylines, etc.
89 \item Constraint-based and sizer-based layouts.
90 \item Print/preview and
document/view architectures.
91 \item Toolbar, notebook, tree control, advanced list control classes.
92 \item PostScript generation under Unix, normal MS Windows printing on the PC.
93 \item MDI (Multiple Document Interface) support.
94 \item Can be used to create DLLs under Windows, dynamic libraries on Unix.
95 \item Common dialogs for file browsing, printing, colour selection, etc.
96 \item Under MS Windows, support for creating metafiles and copying
97 them to the clipboard.
98 \item An API for invoking help from applications.
99 \item Ready-to-use HTML window (supporting a subset of HTML).
100 \item Dialog Editor for building dialogs.
101 \item Network support via a family of socket and protocol classes.
102 \item Support for platform independent image processing.
103 \item Built-in support for many file formats (BMP, PNG, JPEG, GIF, XPM, PNM, PCX).
107 \section{Changes from version
2.0}\label{versionchanges20
}
109 These are a few of the differences between versions
2.0 and
2.2.
113 \begin{itemize
}\itemsep=
0pt
114 \item GTK
1.0 no longer supported.
117 Additions and changes:
119 \begin{itemize
}\itemsep=
0pt
120 \item Corrected many classes to conform better to documented behaviour.
121 \item Added handlers for more image formats (Now GIF, JPEG, PCX, BMP, XPM, PNG, PNM).
122 \item Improved support for socket and network functions.
123 \item Support for different national font encodings.
124 \item Sizer based layout system.
125 \item HTML widget and help system.
126 \item Added some controls (e.g. wxSpinCtrl) and supplemented many.
127 \item Many optical improvements to GTK port.
128 \item Support for menu accelerators in GTK port.
129 \item Enhanced and improved support for scrolling, including child windows.
130 \item Complete rewrite of clipboard and drag and drop classes.
131 \item Improved support for ODBC databases.
132 \item Improved tab traversal in dialogs.
136 \section{wxWindows requirements
}\label{requirements
}
138 To make use of wxWindows, you currently need one of the following setups.
142 \begin{enumerate
}\itemsep=
0pt
143 \item A
486 or higher PC running MS Windows.
144 \item A Windows compiler: most are supported, but please see
{\tt install.txt
} for
145 details. Supported compilers include Microsoft Visual C++
4.0 or higher, Borland C++, Cygwin,
146 Metrowerks CodeWarrior.
147 \item At least
60 MB of disk space.
152 \begin{enumerate
}\itemsep=
0pt
153 \item Almost any C++ compiler, including GNU C++ (EGCS
1.1.1 or above).
154 \item Almost any Unix workstation, and one of: GTK+
1.2, Motif
1.2 or higher, Lesstif.
155 \item At least
60 MB of disk space.
160 \begin{enumerate
}\itemsep=
0pt
161 \item A PowerPC Mac running Mac OS
8.6/
9.x (eg. Classic) or Mac OS X
10.x.
162 \item CodeWarrior
5.3,
6 or
7 for Classic Mac OS.
163 \item The Apple Developer Tools (eg. GNU C++) or CodeWarrior
7 for Mac OS X.
164 \item At least
60 MB of disk space.
167 \section{Availability and location of wxWindows
}
169 \winhelponly{wxWindows is available by anonymous FTP and World Wide Web
170 from ftp://www.remstar.com/pub/wxwin and/or http://www.wxwindows.org.
}
171 \winhelpignore{wxWindows is available by anonymous FTP and World Wide Web
172 from
\urlref{ftp://www.remstar.com/pub/wxwin
}{ftp://www.remstar.com/pub/wxwin
}
173 and/or
\urlref{http://www.wxwindows.org
}{http://www.wxwindows.org
}.
}
175 You can also buy a CD-ROM using the form on the Web site.
177 \section{Acknowledgments
}
179 Thanks are due to AIAI for being willing to release the original version of
180 wxWindows into the public domain, and to our patient partners.
182 We would particularly like to thank the following for their contributions to wxWindows, and the many others who have been involved in
183 the project over the years. Apologies for any unintentional omissions from this list.
185 Yiorgos Adamopoulos, Jamshid Afshar, Alejandro Aguilar-Sierra, AIAI, Patrick Albert, Karsten Ballueder, Michael Bedward, Kai Bendorf, Yura Bidus, Keith
186 Gary Boyce, Chris Breeze, Pete Britton, Ian Brown, C. Buckley, Dmitri Chubraev, Robin Corbet, Cecil Coupe, Andrew Davison, Neil Dudman, Robin
187 Dunn, Hermann Dunkel, Jos van Eijndhoven, Tom Felici, Thomas Fettig, Matthew Flatt, Pasquale Foggia, Josep Fortiana, Todd Fries, Dominic Gallagher,
188 Guillermo Rodriguez Garcia, Wolfram Gloger, Norbert Grotz, Stefan Gunter, Bill Hale, Patrick Halke, Stefan Hammes, Guillaume Helle, Harco de Hilster, Cord Hockemeyer, Markus
189 Holzem, Olaf Klein, Leif Jensen, Bart Jourquin, Guilhem Lavaux, Jan Lessner, Nicholas Liebmann, Torsten Liermann, Per Lindqvist, Thomas Runge, Tatu
190 M\"
{a
}nnist\"
{o
}, Scott Maxwell, Thomas Myers, Oliver Niedung, Stefan Neis, Hernan Otero, Ian Perrigo, Timothy Peters, Giordano Pezzoli, Harri Pasanen, Thomaso Paoletti,
191 Garrett Potts, Marcel Rasche, Robert Roebling, Dino Scaringella, Jobst Schmalenbach, Arthur Seaton, Paul Shirley, Vaclav Slavik, Stein Somers, Petr Smilauer, Neil Smith,
192 Kari Syst\"
{a
}, Arthur Tetzlaff-Deas, Jonathan Tonberg, Jyrki Tuomi, David Webster, Janos Vegh, Andrea Venturoli, Vadim Zeitlin, Xiaokun Zhu, Edward Zimmermann.
194 `Graphplace', the basis for the wxGraphLayout library, is copyright Dr. Jos
195 T.J. van Eijndhoven of Eindhoven University of Technology. The code has
196 been used in wxGraphLayout with his permission.
198 We also acknowledge the author of XFIG, the excellent Unix drawing tool,
199 from the source of which we have borrowed some spline drawing code.
200 His copyright is included below.
202 {\it XFig2.1 is copyright (c)
1985 by Supoj Sutanthavibul. Permission to
203 use, copy, modify, distribute, and sell this software and its
204 documentation for any purpose is hereby granted without fee, provided
205 that the above copyright notice appear in all copies and that both that
206 copyright notice and this permission notice appear in supporting
207 documentation, and that the name of M.I.T. not be used in advertising or
208 publicity pertaining to distribution of the software without specific,
209 written prior permission. M.I.T. makes no representations about the
210 suitability of this software for any purpose. It is provided ``as is''
211 without express or implied warranty.
}
213 \chapter{Multi-platform development with wxWindows
}\label{multiplat
}
214 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
215 \setfooter{\thepage}{}{}{}{}{\thepage}%
217 This chapter describes the practical details of using wxWindows. Please
218 see the file install.txt for up-to-date installation instructions, and
219 changes.txt for differences between versions.
221 \section{Include files
}
223 The main include file is
{\tt "wx/wx.h"
}; this includes the most commonly
224 used modules of wxWindows.
226 To save on compilation time, include only those header files relevant to the
227 source file. If you are using precompiled headers, you should include
228 the following section before any other includes:
231 // For compilers that support precompilation, includes "wx.h".
232 #include <wx/wxprec.h>
239 // Include your minimal set of headers here, or wx.h
243 ... now your other include files ...
246 The file
{\tt "wx/wxprec.h"
} includes
{\tt "wx/wx.h"
}. Although this incantation
247 may seem quirky, it is in fact the end result of a lot of experimentation,
248 and several Windows compilers to use precompilation (those tested are Microsoft Visual C++, Borland C++
251 Borland precompilation is largely automatic. Visual C++ requires specification of
{\tt "wx/wxprec.h"
} as
252 the file to use for precompilation. Watcom C++ is automatic apart from the specification of
253 the .pch file. Watcom C++ is strange in requiring the precompiled header to be used only for
254 object files compiled in the same directory as that in which the precompiled header was created.
255 Therefore, the wxWindows Watcom C++ makefiles go through hoops deleting and recreating
256 a single precompiled header file for each module, thus preventing an accumulation of many
257 multi-megabyte .pch files.
261 The GTK and Motif ports of wxWindow can create either a static library or a shared
262 library on most Unix or Unix-like systems. The static library is called libwx
\_gtk.a
263 and libwx
\_motif.a whereas the name of the shared library is dependent on the
264 system it is created on and the version you are using. The library name for the
265 GTK version of wxWindows
2.2 on Linux and Solaris will be libwx
\_gtk-
2.2.so
.0.0.0,
266 on HP-UX, it will be libwx
\_gtk-
2.2.sl, on AIX just libwx
\_gtk.a etc.
268 Under Windows, use the library wx.lib (release) or wxd.lib (debug) for stand-alone Windows
269 applications, or wxdll.lib (wxdlld.lib) for creating DLLs.
271 \section{Configuration
}
273 Options are configurable in the file
274 \rtfsp{\tt "wx/XXX/setup.h"
} where XXX is the required platform (such as msw, motif, gtk, mac). Some
275 settings are a matter of taste, some help with platform-specific problems, and
276 others can be set to minimize the size of the library. Please see the setup.h file
277 and
{\tt install.txt
} files for details on configuration.
279 Under Unix (GTK and Motif) the corresponding setup.h files are generated automatically
280 when configuring the wxWindows using the "configure" script. When using the RPM packages
281 for installing wxWindows on Linux, a correct setup.h is shipped in the package and
282 this must not be changed.
286 At the moment there is no attempt to make Unix makefiles and
287 PC makefiles compatible, i.e. one makefile is required for
288 each environment. The Unix ports use a sophisticated system based
289 on the GNU autoconf tool and this system will create the
290 makefiles as required on the respective platform. Although the
291 makefiles are not identical in Windows, Mac and Unix, care has
292 been taken to make them relatively similar so that moving from
293 one platform to another will be painless.
295 Sample makefiles for Unix (suffix .unx), MS C++ (suffix .DOS and .NT), Borland
296 C++ (.BCC and .B32) and Symantec C++ (.SC) are included for the library, demos
299 The controlling makefile for wxWindows is in the MS-Windows
300 directory
{\tt src/msw
} for the different Windows compiler and
301 in the build directory when using the Unix ports. The build
302 directory can be chosen by the user. It is the directory in
303 which the "configure" script is run. This can be the normal
304 base directory (by running
{\tt ./configure
} there) or any other
305 directory (e.g.
{\tt ../configure
} after creating a build-directory
306 in the directory level above the base directory).
308 Please see the platform-specific
{\tt install.txt
} file for further details.
310 \section{Windows-specific files
}
312 wxWindows application compilation under MS Windows requires at least two
313 extra files, resource and module definition files.
315 \subsection{Resource file
}\label{resources
}
317 The least that must be defined in the Windows resource file (extension RC)
318 is the following statement:
321 rcinclude "wx/msw/wx.rc"
324 which includes essential internal wxWindows definitions. The resource script
325 may also contain references to icons, cursors, etc., for example:
331 The icon can then be referenced by name when creating a frame icon. See
332 the MS Windows SDK documentation.
334 \normalbox{Note: include wx.rc
{\it after
} any ICON statements
335 so programs that search your executable for icons (such
336 as the Program Manager) find your application icon first.
}
338 \subsection{Module definition file
}
340 A module definition file (extension DEF) is required for
16-bit applications, and
341 looks like the following:
348 CODE PRELOAD MOVEABLE DISCARDABLE
349 DATA PRELOAD MOVEABLE MULTIPLE
354 The only lines which will usually have to be changed per application are
355 NAME and DESCRIPTION.
357 \section{Allocating and deleting wxWindows objects
}
359 In general, classes derived from wxWindow must dynamically allocated
360 with
{\it new
} and deleted with
{\it delete
}. If you delete a window,
361 all of its children and descendants will be automatically deleted,
362 so you don't need to delete these descendants explicitly.
364 When deleting a frame or dialog, use
{\bf Destroy
} rather than
{\bf delete
} so
365 that the wxWindows delayed deletion can take effect. This waits until idle time
366 (when all messages have been processed) to actually delete the window, to avoid
367 problems associated with the GUI sending events to deleted windows.
369 Don't create a window on the stack, because this will interfere
370 with delayed deletion.
372 If you decide to allocate a C++ array of objects (such as wxBitmap) that may
373 be cleaned up by wxWindows, make sure you delete the array explicitly
374 before wxWindows has a chance to do so on exit, since calling
{\it delete
} on
375 array members will cause memory problems.
377 wxColour can be created statically: it is not automatically cleaned
378 up and is unlikely to be shared between other objects; it is lightweight
379 enough for copies to be made.
381 Beware of deleting objects such as a wxPen or wxBitmap if they are still in use.
382 Windows is particularly sensitive to this: so make sure you
383 make calls like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before deleting
384 a drawing object that may be in use. Code that doesn't do this will probably work
385 fine on some platforms, and then fail under Windows.
387 \section{Architecture dependency
}
389 A problem which sometimes arises from writing multi-platform programs is that
390 the basic C types are not defined the same on all platforms. This holds true
391 for both the length in bits of the standard types (such as int and long) as
392 well as their byte order, which might be little endian (typically
393 on Intel computers) or big endian (typically on some Unix workstations). wxWindows
394 defines types and macros that make it easy to write architecture independent
397 wxInt32, wxInt16, wxInt8, wxUint32, wxUint16 = wxWord, wxUint8 = wxByte
399 where wxInt32 stands for a
32-bit signed integer type etc. You can also check
400 which architecture the program is compiled on using the wxBYTE
\_ORDER define
401 which is either wxBIG
\_ENDIAN or wxLITTLE
\_ENDIAN (in the future maybe wxPDP
\_ENDIAN
404 The macros handling bit-swapping with respect to the applications endianness
405 are described in the
\helpref{Macros
}{macros
} section.
407 \section{Conditional compilation
}
409 One of the purposes of wxWindows is to reduce the need for conditional
410 compilation in source code, which can be messy and confusing to follow.
411 However, sometimes it is necessary to incorporate platform-specific
412 features (such as metafile use under MS Windows). The symbols
413 listed in the file
{\tt symbols.txt
} may be used for this purpose,
414 along with any user-supplied ones.
418 The following documents some miscellaneous C++ issues.
420 \subsection{Templates
}
422 wxWindows does not use templates since it is a notoriously unportable feature.
426 wxWindows does not use run-time type information since wxWindows provides
427 its own run-time type information system, implemented using macros.
429 \subsection{Type of NULL
}
431 Some compilers (e.g. the native IRIX cc) define NULL to be
0L so that
432 no conversion to pointers is allowed. Because of that, all these
433 occurrences of NULL in the GTK port use an explicit conversion such
438 wxWindow *my_window = (wxWindow*) NULL;
442 It is recommended to adhere to this in all code using wxWindows as
443 this make the code (a bit) more portable.
445 \subsection{Precompiled headers
}
447 Some compilers, such as Borland C++ and Microsoft C++, support
448 precompiled headers. This can save a great deal of compiling time. The
449 recommended approach is to precompile
{\tt "wx.h"
}, using this
450 precompiled header for compiling both wxWindows itself and any
451 wxWindows applications. For Windows compilers, two dummy source files
452 are provided (one for normal applications and one for creating DLLs)
453 to allow initial creation of the precompiled header.
455 However, there are several downsides to using precompiled headers. One
456 is that to take advantage of the facility, you often need to include
457 more header files than would normally be the case. This means that
458 changing a header file will cause more recompilations (in the case of
459 wxWindows, everything needs to be recompiled since everything includes
{\tt "wx.h"
}!)
461 A related problem is that for compilers that don't have precompiled
462 headers, including a lot of header files slows down compilation
463 considerably. For this reason, you will find (in the common
464 X and Windows parts of the library) conditional
465 compilation that under Unix, includes a minimal set of headers;
466 and when using Visual C++, includes
{\tt wx.h
}. This should help provide
467 the optimal compilation for each compiler, although it is
468 biased towards the precompiled headers facility available
471 \section{File handling
}
473 When building an application which may be used under different
474 environments, one difficulty is coping with documents which may be
475 moved to different directories on other machines. Saving a file which
476 has pointers to full pathnames is going to be inherently unportable. One
477 approach is to store filenames on their own, with no directory
478 information. The application searches through a number of locally
479 defined directories to find the file. To support this, the class
{\bf
480 wxPathList
} makes adding directories and searching for files easy, and
481 the global function
{\bf wxFileNameFromPath
} allows the application to
482 strip off the filename from the path if the filename must be stored.
483 This has undesirable ramifications for people who have documents of the
484 same name in different directories.
486 As regards the limitations of DOS
8+
3 single-case filenames versus
487 unrestricted Unix filenames, the best solution is to use DOS filenames
488 for your application, and also for
document filenames
{\it if
} the user
489 is likely to be switching platforms regularly. Obviously this latter
490 choice is up to the application user to decide. Some programs (such as
491 YACC and LEX) generate filenames incompatible with DOS; the best
492 solution here is to have your Unix makefile rename the generated files
493 to something more compatible before transferring the source to DOS.
494 Transferring DOS files to Unix is no problem, of course, apart from EOL
495 conversion for which there should be a utility available (such as
498 See also the File Functions section of the reference manual for
499 descriptions of miscellaneous file handling functions.
502 \chapter{Utilities supplied with wxWindows
}\label{utilities
}
503 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
504 \setfooter{\thepage}{}{}{}{}{\thepage}%
506 A number of `extras' are supplied with wxWindows, to complement
507 the GUI functionality in the main class library. These are found
508 below the utils directory and usually have their own source, library
509 and documentation directories. For other user-contributed packages,
510 see the directory ftp://www.remstar.com/pub/wxwin/contrib, which is
511 more easily accessed via the Contributions page on the Web site.
513 \section{wxHelp
}\label{wxhelp
}
515 wxHelp is a stand-alone program, written using wxWindows,
516 for displaying hypertext help. It is necessary since not all target
517 systems (notably X) supply an adequate
518 standard for on-line help. wxHelp is modeled on the MS Windows help
519 system, with contents, search and browse buttons, but does not reformat
520 text to suit the size of window, as WinHelp does, and its input files
521 are uncompressed ASCII with some embedded font commands and an .xlp
522 extension. Most wxWindows documentation (user manuals and class
523 references) is supplied in wxHelp format, and also in Windows Help
524 format. The wxWindows
2.0 project will presently use an HTML widget
525 in a new and improved wxHelp implementation, under X.
527 Note that an application can be programmed to use Windows Help under
528 MS Windows, and wxHelp under X. An alternative help viewer under X is
529 Mosaic, a World Wide Web viewer that uses HTML as its native hypertext
530 format. However, this is not currently integrated with wxWindows
533 wxHelp works in two modes---edit and end-user. In edit mode, an ASCII
534 file may be marked up with different fonts and colours, and divided into
535 sections. In end-user mode, no editing is possible, and the user browses
536 principally by clicking on highlighted blocks.
538 When an application invokes wxHelp, subsequent sections, blocks or
539 files may be viewed using the same instance of wxHelp since the two
540 programs are linked using wxWindows interprocess communication
541 facilities. When the application exits, that application's instance of
542 wxHelp may be made to exit also. See the
{\bf wxHelpControllerBase
} entry in the
543 reference section for how an application controls wxHelp.
545 \section{Tex2RTF
}\label{textortf
}
547 Supplied with wxWindows is a utility called Tex2RTF for converting
\rtfsp
548 \LaTeX\ manuals to the following formats:
552 wxWindows help system format (XLP).
554 Rich Text Format suitable for importing into a word processor.
555 \item[Windows Help RTF
]
556 Rich Text Format suitable for compiling into a WinHelp HLP file with the
559 HTML is the native format for Mosaic, the main hypertext viewer for
560 the World Wide Web. Since it is freely available it is a good candidate
561 for being the wxWindows help system under X, as an alternative to wxHelp.
564 Tex2RTF is used for the wxWindows manuals and can be used independently
565 by authors wishing to create on-line and printed manuals from the same
\rtfsp
566 \LaTeX\ source. Please see the separate documentation for Tex2RTF.
568 \section{wxTreeLayout
}
570 This is a simple class library for drawing trees in a reasonably pretty
571 fashion. It provides only minimal default drawing capabilities, since
572 the algorithm is meant to be used for implementing custom tree-based
575 Directed graphs may also be drawn using this library, if cycles are
576 removed before the nodes and arcs are passed to the algorithm.
578 Tree displays are used in many applications: directory browsers,
579 hypertext systems, class browsers, and decision trees are a few
582 See the separate manual and the directory utils/wxtree.
586 \chapter{Programming strategies
}\label{strategies
}
587 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
588 \setfooter{\thepage}{}{}{}{}{\thepage}%
590 This chapter is intended to list strategies that may be useful when
591 writing and debugging wxWindows programs. If you have any good tips,
592 please submit them for inclusion here.
594 \section{Strategies for reducing programming errors
}
596 \subsection{Use ASSERT
}
598 Although I haven't done this myself within wxWindows, it is good
599 practice to use ASSERT statements liberally, that check for conditions that
600 should or should not hold, and print out appropriate error messages.
601 These can be compiled out of a non-debugging version of wxWindows
602 and your application. Using ASSERT is an example of `defensive programming':
603 it can alert you to problems later on.
605 \subsection{Use wxString in preference to character arrays
}
607 Using wxString can be much safer and more convenient than using char *.
608 Again, I haven't practiced what I'm preaching, but I'm now trying to use
609 wxString wherever possible. You can reduce the possibility of memory
610 leaks substantially, and it is much more convenient to use the overloaded
611 operators than functions such as strcmp. wxString won't add a significant
612 overhead to your program; the overhead is compensated for by easier
613 manipulation (which means less code).
615 The same goes for other data types: use classes wherever possible.
617 \section{Strategies for portability
}
619 \subsection{Use relative positioning or constraints
}
621 Don't use absolute panel item positioning if you can avoid it. Different GUIs have
622 very differently sized panel items. Consider using the constraint system, although this
623 can be complex to program.
625 Alternatively, you could use alternative .wrc (wxWindows resource files) on different
626 platforms, with slightly different dimensions in each. Or space your panel items out
629 \subsection{Use wxWindows resource files
}
631 Use .wrc (wxWindows resource files) where possible, because they can be easily changed
632 independently of source code. Bitmap resources can be set up to load different
633 kinds of bitmap depending on platform (see the section on resource files).
635 \section{Strategies for debugging
}\label{debugstrategies
}
637 \subsection{Positive thinking
}
639 It is common to blow up the problem in one's imagination, so that it seems to threaten
640 weeks, months or even years of work. The problem you face may seem insurmountable:
641 but almost never is. Once you have been programming for some time, you will be able
642 to remember similar incidents that threw you into the depths of despair. But
643 remember, you always solved the problem, somehow!
645 Perseverance is often the key, even though a seemingly trivial problem
646 can take an apparently inordinate amount of time to solve. In the end,
647 you will probably wonder why you worried so much. That's not to say it
648 isn't painful at the time. Try not to worry -- there are many more important
651 \subsection{Simplify the problem
}
653 Reduce the code exhibiting the problem to the smallest program possible
654 that exhibits the problem. If it is not possible to reduce a large and
655 complex program to a very small program, then try to ensure your code
656 doesn't hide the problem (you may have attempted to minimize the problem
657 in some way: but now you want to expose it).
659 With luck, you can add a small amount of code that causes the program
660 to go from functioning to non-functioning state. This should give a clue
661 to the problem. In some cases though, such as memory leaks or wrong
662 deallocation, this can still give totally spurious results!
664 \subsection{Use a debugger
}
666 This sounds like facetious advice, but it is surprising how often people
667 don't use a debugger. Often it is an overhead to install or learn how to
668 use a debugger, but it really is essential for anything but the most
671 \subsection{Use logging functions
}
673 There is a variety of logging functions that you can use in your program:
674 see
\helpref{Logging functions
}{logfunctions
}.
676 Using tracing statements may be more convenient than using the debugger
677 in some circumstances (such as when your debugger doesn't support a lot
678 of debugging code, or you wish to print a bunch of variables).
680 \subsection{Use the wxWindows debugging facilities
}
682 You can use wxDebugContext to check for
683 memory leaks and corrupt memory: in fact in debugging mode, wxWindows will
684 automatically check for memory leaks at the end of the program if wxWindows is suitably
685 configured. Depending on the operating system and compiler, more or less
686 specific information about the problem will be logged.
688 You should also use
\helpref{debug macros
}{debugmacros
} as part of a `defensive programming' strategy,
689 scattering wxASSERTs liberally to test for problems in your code as early as possible. Forward thinking
690 will save a surprising amount of time in the long run.
692 See the
\helpref{debugging overview
}{debuggingoverview
} for further information.
694 \subsection{Check Windows debug messages
}
696 Under Windows, it is worth running your program with
697 \urlref{DbgView
}{http://www.sysinternals.com
} running or
698 some other program that shows Windows-generated debug messages. It is
699 possible it will show invalid handles being used. You may have fun seeing
700 what commercial programs cause these normally hidden errors! Microsoft
701 recommend using the debugging version of Windows, which shows up even
702 more problems. However, I doubt it is worth the hassle for most
703 applications. wxWindows is designed to minimize the possibility of such
704 errors, but they can still happen occasionally, slipping through unnoticed
705 because they are not severe enough to cause a crash.
707 \subsection{Genetic mutation
}
709 If we had sophisticated genetic algorithm tools that could be applied
710 to programming, we could use them. Until then, a common -- if rather irrational --
711 technique is to just make arbitrary changes to the code until something
712 different happens. You may have an intuition why a change will make a difference;
713 otherwise, just try altering the order of code, comment lines out, anything
714 to get over an impasse. Obviously, this is usually a last resort.