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