]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/body.tex
8b8c35c2de7373636c74a86a0e9b7042daa483e3
[wxWidgets.git] / docs / latex / wx / body.tex
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 wxWindows?}
7
8 wxWindows is a C++ framework providing GUI (Graphical User
9 Interface) and other facilities on more than one platform. Version 2.0 currently
10 supports MS Windows (16-bit, Windows 95 and Windows NT) and GTK+, with Motif
11 and Mac ports in an advanced state.
12
13 wxWindows was originally developed at the Artificial Intelligence
14 Applications Institute, University of Edinburgh, for internal use.
15 wxWindows has been released into the public domain in the hope
16 that others will also find it useful. Version 2.0 is written and
17 maintained by Julian Smart, Robert Roebling and others.
18
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.}
23
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.
27
28 \section{Why another cross-platform development tool?}
29
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:
34
35 \begin{enumerate}\itemsep=0pt
36 \item low price;
37 \item source availability;
38 \item simplicity of programming;
39 \item support for a wide range of compilers.
40 \end{enumerate}
41
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.
45
46 As public domain software and a project open to everyone, wxWindows has
47 benefited from comments, ideas, bug fixes, enhancements and the sheer
48 enthusiasm of users, especially via the Internet. 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.
55
56 Version 2.0 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 and MFC.
60
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 some extras such as network programming
69 and PostScript output, and can of course be extended as needs dictate. As a bonus, it provides
70 a cleaner 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.
73
74 It is impossible to sum up the functionality of wxWindows in a few paragraphs, but
75 here are some of the benefits:
76
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.
82 \item Several example programs.
83 \item Over 700 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 layout option.
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
93 PC.
94 \item MDI (Multiple Document Interface) support.
95 \item Can be used to create DLLs under Windows, dynamic libraries on Unix.
96 \item Common dialogs for file browsing, printing, colour selection, etc.
97 \item Under MS Windows, support for creating metafiles and copying
98 them to the clipboard.
99 \item An API for invoking help from applications.
100 \item Dialog Editor for building dialogs.
101 \item Network support via a family of socket and protocol classes.
102 \end{itemize}
103
104 \section{Changes from version 1.xx}\label{versionchanges}
105
106 These are a few of the major differences between versions 1.xx and 2.0.
107
108 Removals:
109
110 \begin{itemize}\itemsep=0pt
111 \item XView is no longer supported;
112 \item all controls (panel items) no longer have labels attached to them;
113 \item wxForm has been removed;
114 \item wxCanvasDC, wxPanelDC removed (replaced by wxClientDC, wxWindowDC, wxPaintDC which
115 can be used for any window);
116 \item wxMultiText, wxTextWindow, wxText removed and replaced by wxTextCtrl;
117 \item classes no longer divided into generic and platform-specific parts, for efficiency.
118 \end{itemize}
119
120 Additions and changes:
121
122 \begin{itemize}\itemsep=0pt
123 \item class hierarchy changed, and restrictions about subwindow nesting lifted;
124 \item header files reorganised to conform to normal C++ standards;
125 \item classes less dependent on each another, to reduce executable size;
126 \item wxString used instead of char* wherever possible;
127 \item the number of separate but mandatory utilities reduced;
128 \item the event system has been overhauled, with
129 virtual functions and callbacks being replaced with MFC-like event tables;
130 \item new controls, such as wxTreeCtrl, wxListCtrl, wxSpinButton;
131 \item less inconsistency about what events can be handled, so for example
132 mouse clicks or key presses on controls can now be intercepted;
133 \item the status bar is now a separate class, wxStatusBar, and is
134 implemented in generic wxWindows code;
135 \item some renaming of controls for greater consistency;
136 \item wxBitmap has the notion of bitmap handlers to allow for extension to new formats
137 without ifdefing;
138 \item new dialogs: wxPageSetupDialog, wxFileDialog, wxDirDialog,
139 wxMessageDialog, wxSingleChoiceDialog, wxTextEntryDialog;
140 \item GDI objects are reference-counted and are now passed to most functions
141 by reference, making memory management far easier;
142 \item wxSystemSettings class allows querying for various system-wide properties
143 such as dialog font, colours, user interface element sizes, and so on;
144 \item better platform look and feel conformance;
145 \item toolbar functionality now separated out into a family of classes with the
146 same API;
147 \item device contexts are no longer accessed using wxWindow::GetDC - they are created
148 temporarily with the window as an argument;
149 \item events from sliders and scrollbars can be handled more flexibly;
150 \item the handling of window close events has been changed in line with the new
151 event system, but backward {\bf OnClose} compatibility has been retained;
152 \item the concept of {\it validator} has been added to allow much easier coding of
153 the relationship between controls and application data;
154 \item the documentation has been revised, with more cross-referencing.
155 \end{itemize}
156
157 Platform-specific changes:
158
159 \begin{itemize}\itemsep=0pt
160 \item The Windows header file (windows.h) is no longer included by wxWindows headers;
161 \item wx.dll supported under Visual C++;
162 \item the full range of Windows 95 window decorations are supported, such as modal frame
163 borders;
164 \item MDI classes brought out of wxFrame into separate classes, and made more flexible.
165 \end{itemize}
166
167 \section{wxWindows requirements}\label{requirements}
168
169 To make use of wxWindows, you currently need one or both of the
170 following setups.
171
172 (a) PC:
173
174 \begin{enumerate}\itemsep=0pt
175 \item A 486 or higher PC running MS Windows.
176 \item A Windows compiler: most are supported, but please see {\tt install.txt} for
177 details. Supported compilers include Microsoft Visual C++ 4.0 or higher, Borland C++, Cygwin,
178 Metrowerks CodeWarrior.
179 \item At least 60 MB of disk space.
180 \end{enumerate}
181
182 (b) Unix:
183
184 \begin{enumerate}\itemsep=0pt
185 \item Almost any C++ compiler, including GNU C++.
186 \item Almost any Unix workstation, and one of: GTK+ 1.0, Motif 1.2 or higher, Lesstif.
187 \item At least 60 MB of disk space.
188 \end{enumerate}
189
190 \section{Availability and location of wxWindows}
191
192 wxWindows is currently available from the Artificial Intelligence
193 Applications Institute by anonymous FTP and World Wide Web:
194
195 \begin{verbatim}
196 ftp://www.remstar.com/pub/wxwin
197 http://wxwin.home.ml.org
198 \end{verbatim}
199
200 \section{Acknowledgments}
201
202 Thanks are due to AIAI for being willing to release the original version of
203 wxWindows into the public domain, and to our patient partners.
204
205 We would particularly like to thank the following for their contributions to wxWindows, and the many others who have been involved in
206 the project over the years. Apologies for any unintentional omissions from this list.
207
208 Yiorgos Adamopoulos, Jamshid Afshar, Alejandro Aguilar-Sierra, AIAI, Patrick Albert, Karsten Ballueder, Michael Bedward, Kai Bendorf, Yura Bidus, Keith
209 Gary Boyce, Chris Breeze, Pete Britton, Ian Brown, C. Buckley, Dmitri Chubraev, Robin Corbet, Cecil Coupe, Andrew Davison, Neil Dudman, Robin
210 Dunn, Hermann Dunkel, Jos van Eijndhoven, Tom Felici, Thomas Fettig, Matthew Flatt, Pasquale Foggia, Josep Fortiana, Todd Fries, Dominic Gallagher,
211 Wolfram Gloger, Norbert Grotz, Stefan Gunter, Bill Hale, Patrick Halke, Stefan Hammes, Guillaume Helle, Harco de Hilster, Cord Hockemeyer, Markus
212 Holzem, Olaf Klein, Leif Jensen, Bart Jourquin, Guilhem Lavaux, Jan Lessner, Nicholas Liebmann, Torsten Liermann, Per Lindqvist, Thomas Runge, Tatu
213 M\"{a}nnist\"{o}, Scott Maxwell, Thomas Myers, Oliver Niedung, Hernan Otero, Ian Perrigo, Timothy Peters, Giordano Pezzoli, Harri Pasanen, Thomaso Paoletti,
214 Garrett Potts, Marcel Rasche, Robert Roebling, Dino Scaringella, Jobst Schmalenbach, Arthur Seaton, Paul Shirley, Stein Somers, Petr Smilauer, Neil Smith,
215 Kari Syst\"{a}, Arthur Tetzlaff-Deas, Jonathan Tonberg, Jyrki Tuomi, Janos Vegh, Andrea Venturoli, Vadim Zeitlin, Xiaokun Zhu, Edward Zimmermann.
216
217 `Graphplace', the basis for the wxGraphLayout library, is copyright Dr. Jos
218 T.J. van Eijndhoven of Eindhoven University of Technology. The code has
219 been used in wxGraphLayout with his permission.
220
221 We also acknowledge the author of XFIG, the excellent Unix drawing tool,
222 from the source of which we have borrowed some spline drawing code.
223 His copyright is included below.
224
225 {\it XFig2.1 is copyright (c) 1985 by Supoj Sutanthavibul. Permission to
226 use, copy, modify, distribute, and sell this software and its
227 documentation for any purpose is hereby granted without fee, provided
228 that the above copyright notice appear in all copies and that both that
229 copyright notice and this permission notice appear in supporting
230 documentation, and that the name of M.I.T. not be used in advertising or
231 publicity pertaining to distribution of the software without specific,
232 written prior permission. M.I.T. makes no representations about the
233 suitability of this software for any purpose. It is provided ``as is''
234 without express or implied warranty.}
235
236 \chapter{Multi-platform development with wxWindows}\label{multiplat}
237 \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
238 \setfooter{\thepage}{}{}{}{}{\thepage}%
239
240 This chapter describes the practical details of using wxWindows. Please
241 see the file install.txt for up-to-date installation instructions, and
242 changes.txt for differences between versions.
243
244 \section{Include files}
245
246 The main include file is {\tt "wx/wx.h"}; this includes the most commonly
247 used modules of wxWindows.
248
249 To save on compilation time, include only those header files relevant to the
250 source file. If you are using precompiled headers, you should include
251 the following section before any other includes:
252
253 \begin{verbatim}
254 // For compilers that support precompilation, includes "wx.h".
255 #include <wx/wxprec.h>
256
257 #ifdef __BORLANDC__
258 #pragma hdrstop
259 #endif
260
261 #ifndef WX_PRECOMP
262 // Include your minimal set of headers here, or wx.h
263 #include <wx/wx.h>
264 #endif
265
266 ... now your other include files ...
267 \end{verbatim}
268
269 The file {\tt "wx/wxprec.h"} includes {\tt "wx/wx.h"}. Although this incantation
270 may seem quirky, it is in fact the end result of a lot of experimentation,
271 and several Windows compilers to use precompilation (those tested are Microsoft Visual C++, Borland C++
272 and Watcom C++).
273
274 Borland precompilation is largely automatic. Visual C++ requires specification of {\tt "wx/wxprec.h"} as
275 the file to use for precompilation. Watcom C++ is automatic apart from the specification of
276 the .pch file. Watcom C++ is strange in requiring the precompiled header to be used only for
277 object files compiled in the same directory as that in which the precompiled header was created.
278 Therefore, the wxWindows Watcom C++ makefiles go through hoops deleting and recreating
279 a single precompiled header file for each module, thus preventing an accumulation of many
280 multi-megabyte .pch files.
281
282 \section{Libraries}
283
284 Please the wxGTK or wxMotif documentation for use of the Unix version of wxWindows.
285 Under Windows, use the library wx.lib for stand-alone Windows
286 applications, or wxdll.lib for creating DLLs.
287
288 \section{Configuration}
289
290 Options are configurable in the file
291 \rtfsp{\tt "wx/XXX/setup.h"} where XXX is the required platform (such as msw, motif, gtk, mac). Some settings are a matter
292 of taste, some help with platform-specific problems, and
293 others can be set to minimize the size of the library. Please see the setup.h file
294 and {\tt install.txt} files for details on configuration.
295
296 \section{Makefiles}
297
298 At the moment there is no attempt to make Unix makefiles and
299 PC makefiles compatible, i.e. one makefile is required for
300 each environment. wxGTK has its own configure system which can also
301 be used with wxMotif, although wxMotif has a simple makefile system of its own.
302
303 Sample makefiles for Unix (suffix .UNX), MS C++ (suffix .DOS and .NT), Borland
304 C++ (.BCC and .B32) and Symantec C++ (.SC) are included for the library, demos
305 and utilities.
306
307 The controlling makefile for wxWindows is in the platform-specific
308 directory, such as {\tt src/msw} or {\tt src/motif}.
309
310 Please see the platform-specific {\tt install.txt} file for further details.
311
312 \section{Windows-specific files}
313
314 wxWindows application compilation under MS Windows requires at least two
315 extra files, resource and module definition files.
316
317 \subsection{Resource file}\label{resources}
318
319 The least that must be defined in the Windows resource file (extension RC)
320 is the following statement:
321
322 \begin{verbatim}
323 rcinclude "wx/msw/wx.rc"
324 \end{verbatim}
325
326 which includes essential internal wxWindows definitions. The resource script
327 may also contain references to icons, cursors, etc., for example:
328
329 \begin{verbatim}
330 wxicon icon wx.ico
331 \end{verbatim}
332
333 The icon can then be referenced by name when creating a frame icon. See
334 the MS Windows SDK documentation.
335
336 \normalbox{Note: include wx.rc {\it after} any ICON statements
337 so programs that search your executable for icons (such
338 as the Program Manager) find your application icon first.}
339
340 \subsection{Module definition file}
341
342 A module definition file (extension DEF) is required for 16-bit applications, and
343 looks like the following:
344
345 \begin{verbatim}
346 NAME Hello
347 DESCRIPTION 'Hello'
348 EXETYPE WINDOWS
349 STUB 'WINSTUB.EXE'
350 CODE PRELOAD MOVEABLE DISCARDABLE
351 DATA PRELOAD MOVEABLE MULTIPLE
352 HEAPSIZE 1024
353 STACKSIZE 8192
354 \end{verbatim}
355
356 The only lines which will usually have to be changed per application are
357 NAME and DESCRIPTION.
358
359 \subsection{Allocating and deleting wxWindows objects}
360
361 In general, classes derived from wxWindow must dynamically allocated
362 with {\it new} and deleted with {\it delete}. If you delete a window,
363 all of its children and descendants will be automatically deleted,
364 so you don't need to delete these descendants explicitly.
365
366 When deleting a frame or dialog, use {\bf Destroy} rather than {\bf delete} so
367 that the wxWindows delayed deletion can take effect. This waits until idle time
368 (when all messages have been processed) to actually delete the window, to avoid
369 problems associated with the GUI sending events to deleted windows.
370
371 Don't create a window on the stack, because this will interfere
372 with delayed deletion.
373
374 If you decide to allocate a C++ array of objects (such as wxBitmap) that may
375 be cleaned up by wxWindows, make sure you delete the array explicitly
376 before wxWindows has a chance to do so on exit, since calling {\it delete} on
377 array members will cause memory problems.
378
379 wxColour can be created statically: it is not automatically cleaned
380 up and is unlikely to be shared between other objects; it is lightweight
381 enough for copies to be made.
382
383 Beware of deleting objects such as a wxPen or wxBitmap if they are still in use.
384 Windows is particularly sensitive to this: so make sure you
385 make calls like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before deleting
386 a drawing object that may be in use. Code that doesn't do this will probably work
387 fine on some platforms, and then fail under Windows.
388
389 \section{Conditional compilation}
390
391 One of the purposes of wxWindows is to reduce the need for conditional
392 compilation in source code, which can be messy and confusing to follow.
393 However, sometimes it is necessary to incorporate platform-specific
394 features (such as metafile use under MS Windows). The symbols
395 listed in the file {\tt symbols.txt} may be used for this purpose,
396 along with any user-supplied ones.
397
398 \section{C++ issues}
399
400 The following documents some miscellaneous C++ issues.
401
402 \subsection{Templates}
403
404 wxWindows does not use templates since it is a notoriously unportable feature.
405
406 \subsection{Precompiled headers}
407
408 Some compilers, such as Borland C++ and Microsoft C++, support
409 precompiled headers. This can save a great deal of compiling time. The
410 recommended approach is to precompile {\tt "wx.h"}, using this
411 precompiled header for compiling both wxWindows itself and any
412 wxWindows applications. For Windows compilers, two dummy source files
413 are provided (one for normal applications and one for creating DLLs)
414 to allow initial creation of the precompiled header.
415
416 However, there are several downsides to using precompiled headers. One
417 is that to take advantage of the facility, you often need to include
418 more header files than would normally be the case. This means that
419 changing a header file will cause more recompilations (in the case of
420 wxWindows, everything needs to be recompiled since everything includes {\tt "wx.h"}!)
421
422 A related problem is that for compilers that don't have precompiled
423 headers, including a lot of header files slows down compilation
424 considerably. For this reason, you will find (in the common
425 X and Windows parts of the library) conditional
426 compilation that under Unix, includes a minimal set of headers;
427 and when using Visual C++, includes {\tt wx.h}. This should help provide
428 the optimal compilation for each compiler, although it is
429 biassed towards the precompiled headers facility available
430 in Microsoft C++.
431
432 \section{File handling}
433
434 When building an application which may be used under different
435 environments, one difficulty is coping with documents which may be
436 moved to different directories on other machines. Saving a file which
437 has pointers to full pathnames is going to be inherently unportable. One
438 approach is to store filenames on their own, with no directory
439 information. The application searches through a number of locally
440 defined directories to find the file. To support this, the class {\bf
441 wxPathList} makes adding directories and searching for files easy, and
442 the global function {\bf wxFileNameFromPath} allows the application to
443 strip off the filename from the path if the filename must be stored.
444 This has undesirable ramifications for people who have documents of the
445 same name in different directories.
446
447 As regards the limitations of DOS 8+3 single-case filenames versus
448 unrestricted Unix filenames, the best solution is to use DOS filenames
449 for your application, and also for document filenames {\it if} the user
450 is likely to be switching platforms regularly. Obviously this latter
451 choice is up to the application user to decide. Some programs (such as
452 YACC and LEX) generate filenames incompatible with DOS; the best
453 solution here is to have your Unix makefile rename the generated files
454 to something more compatible before transferring the source to DOS.
455 Transferring DOS files to Unix is no problem, of course, apart from EOL
456 conversion for which there should be a utility available (such as
457 dos2unix).
458
459 See also the File Functions section of the reference manual for
460 descriptions of miscellaneous file handling functions.
461
462 \begin{comment}
463 \chapter{Utilities supplied with wxWindows}\label{utilities}
464 \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
465 \setfooter{\thepage}{}{}{}{}{\thepage}%
466
467 A number of `extras' are supplied with wxWindows, to complement
468 the GUI functionality in the main class library. These are found
469 below the utils directory and usually have their own source, library
470 and documentation directories. For other user-contributed packages,
471 see the directory ftp://www.remstar.com/pub/wxwin/contrib, which is
472 more easily accessed via the Contributions page on the Web site.
473
474 \section{wxHelp}\label{wxhelp}
475
476 wxHelp is a stand-alone program, written using wxWindows,
477 for displaying hypertext help. It is necessary since not all target
478 systems (notably X) supply an adequate
479 standard for on-line help. wxHelp is modelled on the MS Windows help
480 system, with contents, search and browse buttons, but does not reformat
481 text to suit the size of window, as WinHelp does, and its input files
482 are uncompressed ASCII with some embedded font commands and an .xlp
483 extension. Most wxWindows documentation (user manuals and class
484 references) is supplied in wxHelp format, and also in Windows Help
485 format. The wxWindows 2.0 project will presently use an HTML widget
486 in a new and improved wxHelp implementation, under X.
487
488 Note that an application can be programmed to use Windows Help under
489 MS Windows, and wxHelp under X. An alternative help viewer under X is
490 Mosaic, a World Wide Web viewer that uses HTML as its native hypertext
491 format. However, this is not currently integrated with wxWindows
492 applications.
493
494 wxHelp works in two modes---edit and end-user. In edit mode, an ASCII
495 file may be marked up with different fonts and colours, and divided into
496 sections. In end-user mode, no editing is possible, and the user browses
497 principally by clicking on highlighted blocks.
498
499 When an application invokes wxHelp, subsequent sections, blocks or
500 files may be viewed using the same instance of wxHelp since the two
501 programs are linked using wxWindows interprocess communication
502 facilities. When the application exits, that application's instance of
503 wxHelp may be made to exit also. See the {\bf wxHelpControllerBase} entry in the
504 reference section for how an application controls wxHelp.
505
506 \section{Tex2RTF}\label{textortf}
507
508 Supplied with wxWindows is a utility called Tex2RTF for converting\rtfsp
509 \LaTeX\ manuals to the following formats:
510
511 \begin{description}
512 \item[wxHelp]
513 wxWindows help system format (XLP).
514 \item[Linear RTF]
515 Rich Text Format suitable for importing into a word processor.
516 \item[Windows Help RTF]
517 Rich Text Format suitable for compiling into a WinHelp HLP file with the
518 help compiler.
519 \item[HTML]
520 HTML is the native format for Mosaic, the main hypertext viewer for
521 the World Wide Web. Since it is freely available it is a good candidate
522 for being the wxWindows help system under X, as an alternative to wxHelp.
523 \end{description}
524
525 Tex2RTF is used for the wxWindows manuals and can be used independently
526 by authors wishing to create on-line and printed manuals from the same\rtfsp
527 \LaTeX\ source. Please see the separate documentation for Tex2RTF.
528
529 \section{wxTreeLayout}
530
531 This is a simple class library for drawing trees in a reasonably pretty
532 fashion. It provides only minimal default drawing capabilities, since
533 the algorithm is meant to be used for implementing custom tree-based
534 tools.
535
536 Directed graphs may also be drawn using this library, if cycles are
537 removed before the nodes and arcs are passed to the algorithm.
538
539 Tree displays are used in many applications: directory browsers,
540 hypertext systems, class browsers, and decision trees are a few
541 possibilities.
542
543 See the separate manual and the directory utils/wxtree.
544
545 \section{wxGraphLayout}
546
547 The wxGraphLayout class is based on a tool called `graphplace' by Dr.
548 Jos T.J. van Eijndhoven of Eindhoven University of Technology. Given a
549 (possibly cyclic) directed graph, it does its best to lay out the nodes
550 in a sensible manner. There are many applications (such as diagramming)
551 where it is required to display a graph with no human intervention. Even
552 if manual repositioning is later required, this algorithm can make a good
553 first attempt.
554
555 See the separate manual and the directory utils/wxgraph.
556
557 \section{Colours}\label{coloursampler}
558
559 A colour sampler for viewing colours and their names on each
560 platform.
561
562 %
563 \chapter{Tutorial}\label{tutorial}
564 \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
565 \setfooter{\thepage}{}{}{}{}{\thepage}%
566
567 To be written.
568 \end{comment}
569
570 \chapter{Programming strategies}\label{strategies}
571 \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
572 \setfooter{\thepage}{}{}{}{}{\thepage}%
573
574 This chapter is intended to list strategies that may be useful when
575 writing and debugging wxWindows programs. If you have any good tips,
576 please submit them for inclusion here.
577
578 \section{Strategies for reducing programming errors}
579
580 \subsection{Use ASSERT}
581
582 Although I haven't done this myself within wxWindows, it is good
583 practice to use ASSERT statements liberally, that check for conditions that
584 should or should not hold, and print out appropriate error messages.
585 These can be compiled out of a non-debugging version of wxWindows
586 and your application. Using ASSERT is an example of `defensive programming':
587 it can alert you to problems later on.
588
589 \subsection{Use wxString in preference to character arrays}
590
591 Using wxString can be much safer and more convenient than using char *.
592 Again, I haven't practised what I'm preaching, but I'm now trying to use
593 wxString wherever possible. You can reduce the possibility of memory
594 leaks substantially, and it's much more convenient to use the overloaded
595 operators than functions such as strcmp. wxString won't add a significant
596 overhead to your program; the overhead is compensated for by easier
597 manipulation (which means less code).
598
599 The same goes for other data types: use classes wherever possible.
600
601 \section{Strategies for portability}
602
603 \subsection{Use relative positioning or constraints}
604
605 Don't use absolute panel item positioning if you can avoid it. Different GUIs have
606 very differently sized panel items. Consider using the constraint system, although this
607 can be complex to program.
608
609 Alternatively, you could use alternative .wrc (wxWindows resource files) on different
610 platforms, with slightly different dimensions in each. Or space your panel items out
611 to avoid problems.
612
613 \subsection{Use wxWindows resource files}
614
615 Use .wrc (wxWindows resource files) where possible, because they can be easily changed
616 independently of source code. Bitmap resources can be set up to load different
617 kinds of bitmap depending on platform (see the section on resource files).
618
619 \section{Strategies for debugging}\label{debugstrategies}
620
621 \subsection{Positive thinking}
622
623 It's common to blow up the problem in one's imagination, so that it seems to threaten
624 weeks, months or even years of work. The problem you face may seem insurmountable:
625 but almost never is. Once you have been programming for some time, you will be able
626 to remember similar incidents that threw you into the depths of despair. But
627 remember, you always solved the problem, somehow!
628
629 Perseverance is often the key, even though a seemingly trivial problem
630 can take an apparently inordinate amount of time to solve. In the end,
631 you will probably wonder why you worried so much. That's not to say it
632 isn't painful at the time. Try not to worry -- there are many more important
633 things in life.
634
635 \subsection{Simplify the problem}
636
637 Reduce the code exhibiting the problem to the smallest program possible
638 that exhibits the problem. If it is not possible to reduce a large and
639 complex program to a very small program, then try to ensure your code
640 doesn't hide the problem (you may have attempted to minimize the problem
641 in some way: but now you want to expose it).
642
643 With luck, you can add a small amount of code that causes the program
644 to go from functioning to non-functioning state. This should give a clue
645 to the problem. In some cases though, such as memory leaks or wrong
646 deallocation, this can still give totally spurious results!
647
648 \subsection{Use a debugger}
649
650 This sounds like facetious advice, but it's surprising how often people
651 don't use a debugger. Often it's an overhead to install or learn how to
652 use a debugger, but it really is essential for anything but the most
653 trivial programs.
654
655 \subsection{Use logging functions}
656
657 There is a variety of logging functions that you can use in your program:
658 see \helpref{Logging functions}{logfunctions}.
659
660 Using tracing statements may be more convenient than using the debugger
661 in some circumstances (such as when your debugger doesn't support a lot
662 of debugging code, or you wish to print a bunch of variables).
663
664 \subsection{Use the wxWindows debugging facilities}
665
666 You can use wxDebugContext to check for
667 memory leaks and corrupt memory: in fact in debugging mode, wxWindows will
668 automatically check for memory leaks at the end of the program if wxWindows is suitably
669 configured. Depending on the operating system and compiler, more or less
670 specific information about the problem will be logged.
671
672 You should also use \helpref{debug macros}{debugmacros} as part of a `defensive programming' strategy,
673 scattering wxASSERTs liberally to test for problems in your code as early as possible. Forward thinking
674 will save a surprising amount of time in the long run.
675
676 See the \helpref{debugging overview}{debuggingoverview} for further information.
677
678 \subsection{Check Windows debug messages}
679
680 Under Windows, it's worth running your program with DBWIN running or
681 some other program that shows Windows-generated debug messages. It's
682 possible it'll show invalid handles being used. You may have fun seeing
683 what commercial programs cause these normally hidden errors! Microsoft
684 recommend using the debugging version of Windows, which shows up even
685 more problems. However, I doubt it's worth the hassle for most
686 applications. wxWindows is designed to minimize the possibility of such
687 errors, but they can still happen occasionally, slipping through unnoticed
688 because they are not severe enough to cause a crash.
689
690 \subsection{Genetic mutation}
691
692 If we had sophisticated genetic algorithm tools that could be applied
693 to programming, we could use them. Until then, a common -- if rather irrational --
694 technique is to just make arbitrary changes to the code until something
695 different happens. You may have an intuition why a change will make a difference;
696 otherwise, just try altering the order of code, comment lines out, anything
697 to get over an impasse. Obviously, this is usually a last resort.
698