]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/body.tex
don't make read-only text controls editable when enabled
[wxWidgets.git] / docs / latex / wx / body.tex
... / ...
CommitLineData
1\chapter{Introduction}\label{introduction}
2\pagenumbering{arabic}%
3\setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
4\setfooter{\thepage}{}{}{}{}{\thepage}%
5
6\section{What is wxWidgets?}\label{whatis}
7
8wxWidgets is a C++ framework providing GUI (Graphical User
9Interface) and other facilities on more than one platform. Version 2 and higher
10currently support all desktop versions of MS Windows, Unix with GTK+ 1.x or 2.x,
11Unix with Motif, Unix with just X11, Unix with DirectFB, Mac OS X, OS/2.
12
13wxWidgets was originally developed at the Artificial Intelligence
14Applications Institute, University of Edinburgh, for internal use,
15and was first made publicly available in 1992.
16Version 2 is a vastly improved version written and maintained by
17Julian Smart, Robert Roebling, Vadim Zeitlin, Vaclav Slavik and many others.
18
19This manual contains a class reference and topic overviews.
20For a selection of wxWidgets tutorials, please see the documentation page on the \urlref{wxWidgets web site}{http://www.wxwidgets.org}.
21
22Please note that in the following, ``MS Windows" often refers to all
23platforms related to Microsoft Windows, including 32-bit and 64-bit
24variants, unless otherwise stated. All trademarks are acknowledged.
25
26\section{Why another cross-platform development tool?}\label{why}
27
28wxWidgets was developed to provide a cheap and flexible way to maximize
29investment in GUI application development. While a number of commercial
30class libraries already existed for cross-platform development,
31none met all of the following criteria:
32
33\begin{enumerate}\itemsep=0pt
34\item low price;
35\item source availability;
36\item simplicity of programming;
37\item support for a wide range of compilers.
38\end{enumerate}
39
40Since wxWidgets was started, several other free or almost-free
41GUI frameworks have emerged. However, none has the range of
42features, flexibility, documentation and the well-established
43development team that wxWidgets has.
44
45As open source software, wxWidgets has benefited from comments,
46ideas, bug fixes, enhancements and the sheer enthusiasm of
47users. This gives wxWidgets a certain advantage over its
48commercial competitors (and over free libraries without an
49independent development team), plus a robustness against the
50transience of one individual or company. This openness and
51availability of source code is especially important when the
52future of thousands of lines of application code may depend upon
53the longevity of the underlying class library.
54
55Version 2 goes much further than previous versions in terms of
56generality and features, allowing applications to be produced
57that are often indistinguishable from those produced using
58single-platform toolkits such as Motif, GTK+ and MFC.
59
60The importance of using a platform-independent class library
61cannot be overstated, since GUI application development is very
62time-consuming, and sustained popularity of particular GUIs
63cannot be guaranteed. Code can very quickly become obsolete if
64it addresses the wrong platform or audience. wxWidgets helps to
65insulate the programmer from these winds of change. Although
66wxWidgets may not be suitable for every application (such as an
67OLE-intensive program), it provides access to most of the
68functionality a GUI program normally requires, plus many extras
69such as network programming, PostScript output, and HTML
70rendering; and it can of course be extended as needs dictate.
71As a bonus, it provides a far cleaner and easier programming
72interface than the native APIs. Programmers may find it
73worthwhile to use wxWidgets even if they are developing on only
74one platform.
75
76It is impossible to sum up the functionality of wxWidgets in a few paragraphs, but
77here are some of the benefits:
78
79\begin{itemize}\itemsep=0pt
80\item Low cost (free, in fact!)
81\item You get the source.
82\item Available on a variety of popular platforms.
83\item Works with almost all popular C++ compilers and Python.
84\item Over 50 example programs.
85\item Over 1000 pages of printable and on-line documentation.
86\item Includes Tex2RTF, to allow you to produce your own documentation
87in Windows Help, HTML and Word RTF formats.
88\item Simple-to-use, object-oriented API.
89\item Flexible event system.
90\item Graphics calls include lines, rounded rectangles, splines, polylines, etc.
91\item Constraint-based and sizer-based layouts.
92\item Print/preview and document/view architectures.
93\item Toolbar, notebook, tree control, advanced list control classes.
94\item PostScript generation under Unix, normal MS Windows printing on the PC.
95\item MDI (Multiple Document Interface) support.
96\item Can be used to create DLLs under Windows, dynamic libraries on Unix.
97\item Common dialogs for file browsing, printing, colour selection, etc.
98\item Under MS Windows, support for creating metafiles and copying
99them to the clipboard.
100\item An API for invoking help from applications.
101\item Ready-to-use HTML window (supporting a subset of HTML).
102\item Network support via a family of socket and protocol classes.
103\item Support for platform independent image processing.
104\item Built-in support for many file formats (BMP, PNG, JPEG, GIF, XPM, PNM, PCX).
105\end{itemize}
106
107\begin{comment}
108\section{Changes from version 2.0}\label{versionchanges20}
109
110These are a few of the differences between versions 2.0 and 2.2.
111
112Removals:
113
114\begin{itemize}\itemsep=0pt
115\item GTK 1.0 no longer supported.
116\end{itemize}
117
118Additions and changes:
119
120\begin{itemize}\itemsep=0pt
121\item Corrected many classes to conform better to documented behaviour.
122\item Added handlers for more image formats (Now GIF, JPEG, PCX, BMP, XPM, PNG, PNM).
123\item Improved support for socket and network functions.
124\item Support for different national font encodings.
125\item Sizer based layout system.
126\item HTML widget and help system.
127\item Added some controls (e.g. wxSpinCtrl) and supplemented many.
128\item Many optical improvements to GTK port.
129\item Support for menu accelerators in GTK port.
130\item Enhanced and improved support for scrolling, including child windows.
131\item Complete rewrite of clipboard and drag and drop classes.
132\item Improved support for ODBC databases.
133\item Improved tab traversal in dialogs.
134\end{itemize}
135\end{comment}
136
137\section{wxWidgets requirements}\label{requirements}
138
139To make use of wxWidgets, you currently need one of the following setups.
140
141(a) MS-Windows:
142
143\begin{enumerate}\itemsep=0pt
144\item A 32-bit or 64-bit PC running MS Windows.
145\item A Windows compiler: MS Visual C++ (embedded Visual C++ for wxWinCE
146port), Borland C++, Watcom C++, Cygwin, MinGW, Metrowerks CodeWarrior,
147Digital Mars C++. See {\tt install.txt} for details about compiler
148version supported.
149\item At least 100 MB of disk space for source tree and additional space for
150libraries and application building (depends on compiler and build settings).
151\end{enumerate}
152
153(b) Unix:
154
155\begin{enumerate}\itemsep=0pt
156\item Almost any C++ compiler, including GNU C++ (EGCS 1.1.1 or above).
157\item Almost any Unix workstation, and one of: GTK+ 1.2, GTK+ 2.0, Motif 1.2 or higher, Lesstif.
158If using the wxX11 port, no such widget set is required.
159\item At least 100 MB of disk space for source tree and additional space for
160libraries and application building (depends on compiler and build settings).
161\end{enumerate}
162
163(c) Mac OS/Mac OS X:
164
165\begin{enumerate}\itemsep=0pt
166\item A PowerPC Mac running Mac OS 8.6/9.x (eg. Classic) or Mac OS X 10.x.
167\item CodeWarrior 5.3, 6 or 7 for Classic Mac OS.
168\item The Apple Developer Tools (eg. GNU C++), CodeWarrior 7 or above for Mac OS X.
169\item At least 100 MB of disk space for source tree and additional space for
170libraries and application building (depends on compiler and build settings).
171\end{enumerate}
172
173\section{Availability and location of wxWidgets}\label{where}
174
175\winhelponly{wxWidgets is available by anonymous FTP and World Wide Web
176from ftp://biolpc22.york.ac.uk/pub and/or http://www.wxwidgets.org.}
177\winhelpignore{wxWidgets is available by anonymous FTP and World Wide Web
178from \urlref{ftp://biolpc22.york.ac.uk/pub}{ftp://biolpc22.york.ac.uk/pub}
179and/or \urlref{http://www.wxwidgets.org}{http://www.wxwidgets.org}.}
180
181You can also buy a CD-ROM using the form on the Web site.
182
183\section{Acknowledgements}\label{acknowledgements}
184
185Thanks are due to AIAI for being willing to release the original version of
186wxWidgets into the public domain, and to our patient partners.
187
188We would particularly like to thank the following for their contributions to wxWidgets, and the many others who have been involved in
189the project over the years. Apologies for any unintentional omissions from this list.
190
191Yiorgos Adamopoulos, Jamshid Afshar, Alejandro Aguilar-Sierra, AIAI,
192Patrick Albert, Karsten Ballueder, Mattia Barbon, Michael Bedward,
193Kai Bendorf, Yura Bidus, Keith Gary Boyce, Chris Breeze, Pete Britton,
194Ian Brown, C. Buckley, Marco Cavallini, Dmitri Chubraev, Robin Corbet, Cecil Coupe,
195Stefan Csomor, Andrew Davison, Gilles Depeyrot, Neil Dudman, Robin Dunn,
196Hermann Dunkel, Jos van Eijndhoven, Chris Elliott, David Elliott, Tom Felici,
197Thomas Fettig, Matthew Flatt, Pasquale Foggia, Josep Fortiana, Todd Fries,
198Dominic Gallagher, Guillermo Rodriguez Garcia, Wolfram Gloger, Norbert Grotz,
199Stefan Gunter, Bill Hale, Patrick Halke, Stefan Hammes, Guillaume Helle,
200Harco de Hilster, Kevin Hock, Cord Hockemeyer, Markus Holzem, Olaf Klein, Leif Jensen,
201Bart Jourquin, Guilhem Lavaux, Ron Lee, Jan Lessner, Nicholas Liebmann,
202Torsten Liermann, Per Lindqvist, Thomas Runge, Tatu M\"{a}nnist\"{o},
203Scott Maxwell, Thomas Myers, Oliver Niedung, Stefan Neis, Ryan Norton, Hernan Otero,
204Ian Perrigo, Timothy Peters, Giordano Pezzoli, Harri Pasanen, Thomaso Paoletti,
205Garrett Potts, Marcel Rasche, Robert Roebling, Dino Scaringella,
206Jobst Schmalenbach, Arthur Seaton, Paul Shirley, Wlodzimierz `ABX' Skiba,
207Vaclav Slavik, Julian Smart, Stein Somers, Petr Smilauer, Neil Smith,
208Kari Syst\"{a}, George Tasker, Arthur Tetzlaff-Deas, Jonathan Tonberg,
209Jyrki Tuomi, Janos Vegh, Andrea Venturoli, David Webster, Otto Wyss,
210Vadim Zeitlin, Xiaokun Zhu, Edward Zimmermann.
211
212`Graphplace', the basis for the wxGraphLayout library, is copyright Dr. Jos
213T.J. van Eijndhoven of Eindhoven University of Technology. The code has
214been used in wxGraphLayout with his permission.
215
216We also acknowledge the author of XFIG, the excellent Unix drawing tool,
217from the source of which we have borrowed some spline drawing code.
218His copyright is included below.
219
220{\it XFig2.1 is copyright (c) 1985 by Supoj Sutanthavibul. Permission to
221use, copy, modify, distribute, and sell this software and its
222documentation for any purpose is hereby granted without fee, provided
223that the above copyright notice appear in all copies and that both that
224copyright notice and this permission notice appear in supporting
225documentation, and that the name of M.I.T. not be used in advertising or
226publicity pertaining to distribution of the software without specific,
227written prior permission. M.I.T. makes no representations about the
228suitability of this software for any purpose. It is provided ``as is''
229without express or implied warranty.}
230
231\chapter{Multi-platform development with wxWidgets}\label{multiplat}
232\setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
233\setfooter{\thepage}{}{}{}{}{\thepage}%
234
235This chapter describes the practical details of using wxWidgets. Please
236see the file install.txt for up-to-date installation instructions, and
237changes.txt for differences between versions.
238
239\section{Include files}\label{includefiles}
240
241The main include file is {\tt "wx/wx.h"}; this includes the most commonly
242used modules of wxWidgets.
243
244To save on compilation time, include only those header files relevant to the
245source file. If you are using precompiled headers, you should include
246the following section before any other includes:
247
248\begin{verbatim}
249// For compilers that support precompilation, includes "wx.h".
250#include <wx/wxprec.h>
251
252#ifdef __BORLANDC__
253#pragma hdrstop
254#endif
255
256#ifndef WX_PRECOMP
257// Include your minimal set of headers here, or wx.h
258#include <wx/wx.h>
259#endif
260
261... now your other include files ...
262\end{verbatim}
263
264The file {\tt "wx/wxprec.h"} includes {\tt "wx/wx.h"}. Although this incantation
265may seem quirky, it is in fact the end result of a lot of experimentation,
266and several Windows compilers to use precompilation which is largely automatic for
267compilers with necessary support. Currently it is used for Visual C++ (including
268embedded Visual C++), Borland C++, Open Watcom C++, Digital Mars C++
269and newer versions of GCC.
270Some compilers might need extra work from the application developer to set the
271build environment up as necessary for the support.
272
273\section{Libraries}\label{libraries}
274
275Most ports of wxWidgets can create either a static library or a shared
276library. wxWidgets can also be built in multilib and monolithic variants.
277See the \helpref{libraries list}{librarieslist} for more
278information on these.
279
280\section{Configuration}\label{configuration}
281
282When using project files and makefiles directly to build wxWidgets,
283options are configurable in the file
284\rtfsp{\tt "wx/XXX/setup.h"} where XXX is the required platform (such as msw, motif, gtk, mac). Some
285settings are a matter of taste, some help with platform-specific problems, and
286others can be set to minimize the size of the library. Please see the setup.h file
287and {\tt install.txt} files for details on configuration.
288
289When using the 'configure' script to configure wxWidgets (on Unix and other platforms where
290configure is available), the corresponding setup.h files are generated automatically
291along with suitable makefiles. When using the RPM packages
292for installing wxWidgets on Linux, a correct setup.h is shipped in the package and
293this must not be changed.
294
295\section{Makefiles}\label{makefiles}
296
297On Microsoft Windows, wxWidgets has a different set of makefiles for each
298compiler, because each compiler's 'make' tool is slightly different.
299Popular Windows compilers that we cater for, and the corresponding makefile
300extensions, include: Microsoft Visual C++ (.vc), Borland C++ (.bcc),
301OpenWatcom C++ (.wat) and MinGW/Cygwin (.gcc). Makefiles are provided
302for the wxWidgets library itself, samples, demos, and utilities.
303
304On Linux, Mac and OS/2, you use the 'configure' command to
305generate the necessary makefiles. You should also use this method when
306building with MinGW/Cygwin on Windows.
307
308We also provide project files for some compilers, such as
309Microsoft VC++. However, we recommend using makefiles
310to build the wxWidgets library itself, because makefiles
311can be more powerful and less manual intervention is required.
312
313On Windows using a compiler other than MinGW/Cygwin, you would
314build the wxWidgets library from the build/msw directory
315which contains the relevant makefiles.
316
317On Windows using MinGW/Cygwin, and on Unix, MacOS X and OS/2, you invoke
318'configure' (found in the top-level of the wxWidgets source hierarchy),
319from within a suitable empty directory for containing makefiles, object files and
320libraries.
321
322For details on using makefiles, configure, and project files,
323please see docs/xxx/install.txt in your distribution, where
324xxx is the platform of interest, such as msw, gtk, x11, mac.
325
326\section{Windows-specific files}\label{windowsfiles}
327
328wxWidgets application compilation under MS Windows requires at least one
329extra file: a resource file.
330
331\subsection{Resource file}\label{resources}
332
333The least that must be defined in the Windows resource file (extension RC)
334is the following statement:
335
336\begin{verbatim}
337#include "wx/msw/wx.rc"
338\end{verbatim}
339
340which includes essential internal wxWidgets definitions. The resource script
341may also contain references to icons, cursors, etc., for example:
342
343\begin{verbatim}
344wxicon icon wx.ico
345\end{verbatim}
346
347The icon can then be referenced by name when creating a frame icon. See
348the MS Windows SDK documentation.
349
350\normalbox{Note: include wx.rc {\it after} any ICON statements
351so programs that search your executable for icons (such
352as the Program Manager) find your application icon first.}
353
354\section{Allocating and deleting wxWidgets objects}\label{allocatingobjects}
355
356In general, classes derived from wxWindow must dynamically allocated
357with {\it new} and deleted with {\it delete}. If you delete a window,
358all of its children and descendants will be automatically deleted,
359so you don't need to delete these descendants explicitly.
360
361When deleting a frame or dialog, use {\bf Destroy} rather than {\bf delete} so
362that the wxWidgets delayed deletion can take effect. This waits until idle time
363(when all messages have been processed) to actually delete the window, to avoid
364problems associated with the GUI sending events to deleted windows.
365
366Don't create a window on the stack, because this will interfere
367with delayed deletion.
368
369If you decide to allocate a C++ array of objects (such as wxBitmap) that may
370be cleaned up by wxWidgets, make sure you delete the array explicitly
371before wxWidgets has a chance to do so on exit, since calling {\it delete} on
372array members will cause memory problems.
373
374wxColour can be created statically: it is not automatically cleaned
375up and is unlikely to be shared between other objects; it is lightweight
376enough for copies to be made.
377
378Beware of deleting objects such as a wxPen or wxBitmap if they are still in use.
379Windows is particularly sensitive to this: so make sure you
380make calls like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before deleting
381a drawing object that may be in use. Code that doesn't do this will probably work
382fine on some platforms, and then fail under Windows.
383
384\section{Architecture dependency}\label{architecturedependency}
385
386A problem which sometimes arises from writing multi-platform programs is that
387the basic C types are not defined the same on all platforms. This holds true
388for both the length in bits of the standard types (such as int and long) as
389well as their byte order, which might be little endian (typically
390on Intel computers) or big endian (typically on some Unix workstations). wxWidgets
391defines types and macros that make it easy to write architecture independent
392code. The types are:
393
394wxInt32, wxInt16, wxInt8, wxUint32, wxUint16 = wxWord, wxUint8 = wxByte
395
396where wxInt32 stands for a 32-bit signed integer type etc. You can also check
397which architecture the program is compiled on using the wxBYTE\_ORDER define
398which is either wxBIG\_ENDIAN or wxLITTLE\_ENDIAN (in the future maybe wxPDP\_ENDIAN
399as well).
400
401The macros handling bit-swapping with respect to the applications endianness
402are described in the \helpref{Byte order macros}{byteordermacros} section.
403
404\section{Conditional compilation}\label{conditionalcompilation}
405
406One of the purposes of wxWidgets is to reduce the need for conditional
407compilation in source code, which can be messy and confusing to follow.
408However, sometimes it is necessary to incorporate platform-specific
409features (such as metafile use under MS Windows). The symbols
410listed in the file {\tt symbols.txt} may be used for this purpose,
411along with any user-supplied ones.
412
413\section{C++ issues}\label{cpp}
414
415The following documents some miscellaneous C++ issues.
416
417\subsection{Templates}\label{templates}
418
419wxWidgets does not use templates (except for some advanced features that
420are switched off by default) since it is a notoriously unportable feature.
421
422\subsection{RTTI}\label{rtti}
423
424wxWidgets does not use C++ run-time type information since wxWidgets provides
425its own run-time type information system, implemented using macros.
426
427\subsection{Type of NULL}\label{null}
428
429Some compilers (e.g. the native IRIX cc) define NULL to be 0L so that
430no conversion to pointers is allowed. Because of that, all these
431occurrences of NULL in the GTK+ port use an explicit conversion such
432as
433
434{\small
435\begin{verbatim}
436 wxWindow *my_window = (wxWindow*) NULL;
437\end{verbatim}
438}%
439
440It is recommended to adhere to this in all code using wxWidgets as
441this make the code (a bit) more portable.
442
443\subsection{Precompiled headers}\label{precompiledheaders}
444
445Some compilers, such as Borland C++ and Microsoft C++, support
446precompiled headers. This can save a great deal of compiling time. The
447recommended approach is to precompile {\tt "wx.h"}, using this
448precompiled header for compiling both wxWidgets itself and any
449wxWidgets applications. For Windows compilers, two dummy source files
450are provided (one for normal applications and one for creating DLLs)
451to allow initial creation of the precompiled header.
452
453However, there are several downsides to using precompiled headers. One
454is that to take advantage of the facility, you often need to include
455more header files than would normally be the case. This means that
456changing a header file will cause more recompilations (in the case of
457wxWidgets, everything needs to be recompiled since everything includes {\tt "wx.h"}!)
458
459A related problem is that for compilers that don't have precompiled
460headers, including a lot of header files slows down compilation
461considerably. For this reason, you will find (in the common
462X and Windows parts of the library) conditional
463compilation that under Unix, includes a minimal set of headers;
464and when using Visual C++, includes {\tt wx.h}. This should help provide
465the optimal compilation for each compiler, although it is
466biased towards the precompiled headers facility available
467in Microsoft C++.
468
469\section{File handling}\label{filehandling}
470
471When building an application which may be used under different
472environments, one difficulty is coping with documents which may be
473moved to different directories on other machines. Saving a file which
474has pointers to full pathnames is going to be inherently unportable. One
475approach is to store filenames on their own, with no directory
476information. The application searches through a number of locally
477defined directories to find the file. To support this, the class {\bf
478wxPathList} makes adding directories and searching for files easy, and
479the global function {\bf wxFileNameFromPath} allows the application to
480strip off the filename from the path if the filename must be stored.
481This has undesirable ramifications for people who have documents of the
482same name in different directories.
483
484As regards the limitations of DOS 8+3 single-case filenames versus
485unrestricted Unix filenames, the best solution is to use DOS filenames
486for your application, and also for document filenames {\it if} the user
487is likely to be switching platforms regularly. Obviously this latter
488choice is up to the application user to decide. Some programs (such as
489YACC and LEX) generate filenames incompatible with DOS; the best
490solution here is to have your Unix makefile rename the generated files
491to something more compatible before transferring the source to DOS.
492Transferring DOS files to Unix is no problem, of course, apart from EOL
493conversion for which there should be a utility available (such as
494dos2unix).
495
496See also the File Functions section of the reference manual for
497descriptions of miscellaneous file handling functions.
498
499\chapter{Utilities and libraries supplied with wxWidgets}\label{utilities}
500\setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
501\setfooter{\thepage}{}{}{}{}{\thepage}%
502
503In addition to the \helpref{wxWidgets libraries}{librarieslist}, some
504additional utilities are supplied in the \tt{utils} hierarchy.
505
506For other user-contributed packages, please see the Contributions page
507on the \urlref{wxWidgets Web site}{http://www.wxwidgets.org}.
508
509\begin{description}\itemsep=0pt
510\item[{\bf Helpview}]
511Helpview is a program for displaying wxWidgets HTML
512Help files. In many cases, you may wish to use the wxWidgets HTML
513Help classes from within your application, but this provides a
514handy stand-alone viewer. See \helpref{wxHTML Notes}{wxhtml} for more details.
515You can find it in {\tt samples/html/helpview}.
516\item[{\bf Tex2RTF}]
517Supplied with wxWidgets is a utility called Tex2RTF for converting\rtfsp
518\LaTeX\ manuals HTML, MS HTML Help, wxHTML Help, RTF, and Windows
519Help RTF formats. Tex2RTF is used for the wxWidgets manuals and can be used independently
520by authors wishing to create on-line and printed manuals from the same\rtfsp
521\LaTeX\ source. Please see the separate documentation for Tex2RTF.
522You can find it under {\tt utils/tex2rtf}.
523\item[{\bf Helpgen}]
524Helpgen takes C++ header files and generates a Tex2RTF-compatible
525documentation file for each class it finds, using comments as appropriate.
526This is a good way to start a reference for a set of classes.
527Helpgen can be found in {\tt utils/HelpGen}.
528\item[{\bf Emulator}]
529Xnest-based display emulator for X11-based PDA applications. On some
530systems, the Xnest window does not synchronise with the
531'skin' window. This program can be found in {\tt utils/emulator}.
532\end{description}
533
534\chapter{Programming strategies}\label{strategies}
535\setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
536\setfooter{\thepage}{}{}{}{}{\thepage}%
537
538This chapter is intended to list strategies that may be useful when
539writing and debugging wxWidgets programs. If you have any good tips,
540please submit them for inclusion here.
541
542\section{Strategies for reducing programming errors}\label{reducingerrors}
543
544\subsection{Use ASSERT}\label{useassert}
545
546It is good practice to use ASSERT statements liberally, that check for conditions
547that should or should not hold, and print out appropriate error messages.
548
549These can be compiled out of a non-debugging version of wxWidgets
550and your application. Using ASSERT is an example of `defensive programming':
551it can alert you to problems later on.
552
553See \helpref{wxASSERT}{wxassert} for more info.
554
555\subsection{Use wxString in preference to character arrays}\label{usewxstring}
556
557Using \helpref{wxString}{wxstring} can be much safer and more convenient than using wxChar *.
558
559You can reduce the possibility of memory leaks substantially, and it is much more
560convenient to use the overloaded operators than functions such as \tt{strcmp}.
561wxString won't add a significant overhead to your program; the overhead is compensated
562for by easier manipulation (which means less code).
563
564The same goes for other data types: use classes wherever possible.
565
566\section{Strategies for portability}\label{portability}
567
568\subsection{Use sizers}\label{usesizers}
569
570Don't use absolute panel item positioning if you can avoid it. Different GUIs have
571very differently sized panel items. Consider using the \helpref{sizers}{sizeroverview} instead.
572
573\subsection{Use wxWidgets resource files}\label{useresources}
574
575Use .xrc (wxWidgets resource files) where possible, because they can be easily changed
576independently of source code. See the \helpref{XRC overview}{xrcoverview} for more info.
577
578\section{Strategies for debugging}\label{debugstrategies}
579
580\subsection{Positive thinking}\label{positivethinking}
581
582It is common to blow up the problem in one's imagination, so that it seems to threaten
583weeks, months or even years of work. The problem you face may seem insurmountable:
584but almost never is. Once you have been programming for some time, you will be able
585to remember similar incidents that threw you into the depths of despair. But
586remember, you always solved the problem, somehow!
587
588Perseverance is often the key, even though a seemingly trivial problem
589can take an apparently inordinate amount of time to solve. In the end,
590you will probably wonder why you worried so much. That's not to say it
591isn't painful at the time. Try not to worry -- there are many more important
592things in life.
593
594\subsection{Simplify the problem}\label{simplifyproblem}
595
596Reduce the code exhibiting the problem to the smallest program possible
597that exhibits the problem. If it is not possible to reduce a large and
598complex program to a very small program, then try to ensure your code
599doesn't hide the problem (you may have attempted to minimize the problem
600in some way: but now you want to expose it).
601
602With luck, you can add a small amount of code that causes the program
603to go from functioning to non-functioning state. This should give a clue
604to the problem. In some cases though, such as memory leaks or wrong
605deallocation, this can still give totally spurious results!
606
607\subsection{Use a debugger}\label{usedebugger}
608
609This sounds like facetious advice, but it is surprising how often people
610don't use a debugger. Often it is an overhead to install or learn how to
611use a debugger, but it really is essential for anything but the most
612trivial programs.
613
614\subsection{Use logging functions}\label{uselogging}
615
616There is a variety of logging functions that you can use in your program:
617see \helpref{Logging functions}{logfunctions}.
618
619Using tracing statements may be more convenient than using the debugger
620in some circumstances (such as when your debugger doesn't support a lot
621of debugging code, or you wish to print a bunch of variables).
622
623\subsection{Use the wxWidgets debugging facilities}\label{usedebuggingfacilities}
624
625You can use \helpref{wxDebugContext}{wxdebugcontext} to check for
626memory leaks and corrupt memory: in fact in debugging mode, wxWidgets will
627automatically check for memory leaks at the end of the program if wxWidgets is suitably
628configured. Depending on the operating system and compiler, more or less
629specific information about the problem will be logged.
630
631You should also use \helpref{debug macros}{debugmacros} as part of a `defensive programming' strategy,
632scattering wxASSERTs liberally to test for problems in your code as early as possible. Forward thinking
633will save a surprising amount of time in the long run.
634
635See the \helpref{debugging overview}{debuggingoverview} for further information.
636