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