]>
Commit | Line | Data |
---|---|---|
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 | ||
8 | wxWindows is a C++ framework providing GUI (Graphical User | |
bd0df01f JS |
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. | |
a660d684 KB |
11 | |
12 | wxWindows was originally developed at the Artificial Intelligence | |
13 | Applications Institute, University of Edinburgh, for internal use. | |
14 | wxWindows has been released into the public domain in the hope | |
15 | that others will also find it useful. Version 2.0 is written and | |
bd0df01f | 16 | maintained by Julian Smart, Robert Roebling and others. |
a660d684 KB |
17 | |
18 | This manual discusses wxWindows in the context of multi-platform | |
19 | development.\helpignore{For more detail on the wxWindows version 2.0 API | |
20 | (Application Programming Interface) please refer to the separate | |
21 | wxWindows reference manual.} | |
22 | ||
23 | Please note that in the following, ``MS Windows" often refers to all | |
24 | platforms related to Microsoft Windows, including 16-bit and 32-bit | |
25 | variants, unless otherwise stated. All trademarks are acknowledged. | |
26 | ||
27 | \section{Why another cross-platform development tool?} | |
28 | ||
29 | wxWindows was developed to provide a cheap and flexible way to maximize | |
30 | investment in GUI application development. While a number of commercial | |
31 | class libraries already exist for cross-platform development, | |
32 | none 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 | ||
41 | As public domain software and a project open to everyone, wxWindows has | |
42 | benefited from comments, ideas, bug fixes, enhancements and the sheer | |
43 | enthusiasm of users, especially via the Internet. This gives wxWindows a | |
44 | certain advantage over its commercial brothers, and a robustness against | |
45 | the transience of one individual or company. This openness and | |
46 | availability of source code is especially important when the future of | |
47 | thousands of lines of application code may depend upon the longevity of | |
48 | the underlying class library. | |
49 | ||
50 | In writing wxWindows, completeness has sometimes been traded for | |
51 | portability and simplicity of programming. Version 2.0 goes much | |
52 | further than previous versions in terms of generality and features, | |
53 | allowing applications to be produced | |
54 | that are often indistinguishable from those produced using single-platform | |
55 | toolkits | |
56 | such as Motif and MFC. | |
57 | ||
bd0df01f JS |
58 | wxWindows 2.0 currently maps to two native APIs: GTK and |
59 | MS Windows. Motif, Xt and Mac ports are also in preparation. | |
a660d684 KB |
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, it provides access to most of the functionality a | |
68 | GUI program normally requires, plus some extras such as form | |
69 | construction, interprocess communication and PostScript output, and | |
70 | can of course be extended as needs dictate. As a bonus, it provides | |
71 | a cleaner programming interface than the native | |
72 | APIs. Programmers may find it worthwhile to use wxWindows even if they | |
73 | are developing on only one platform. | |
74 | ||
75 | It is impossible to sum up the functionality of wxWindows in a few paragraphs, but | |
76 | here 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. | |
bd0df01f | 82 | \item Over 700 pages of printable and on-line documentation. |
a660d684 KB |
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 | |
bd0df01f | 88 | \item Easy, object-oriented interprocess comms (DDE subset) under Unix and |
a660d684 | 89 | MS Windows. |
bd0df01f | 90 | \item Encapsulated PostScript generation under Unix, normal MS Windows printing on the |
a660d684 | 91 | PC. |
bd0df01f | 92 | \item MDI support under Windows and GTK. |
a660d684 KB |
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 | |
96 | them 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 | ||
103 | These are a few of the major differences between versions 1.xx and 2.0. | |
104 | ||
105 | Removals: | |
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 | |
113 | can 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 | ||
118 | Additions 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 | |
127 | virtual 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 | |
130 | mouse clicks or key presses on controls can now be intercepted; | |
131 | \item the status bar is now a separate class, wxStatusBar, and is | |
132 | implemented 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 | |
135 | without ifdefing; | |
136 | \item new dialogs: wxPageSetupDialog, wxFileDialog, wxDirDialog, | |
137 | wxMessageDialog, wxSingleChoiceDialog, wxTextEntryDialog; | |
138 | \item GDI objects are reference-counted and are now passed to most functions | |
139 | by reference, making memory management far easier; | |
140 | \item wxSystemSettings class allows querying for various system-wide properties | |
141 | such 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 | |
144 | same API; | |
145 | \item device contexts are no longer accessed using wxWindow::GetDC - they are created | |
146 | temporarily 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 | |
149 | event 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 | |
151 | the relationship between controls and application data; | |
152 | \item the documentation has been revised, with more cross-referencing. | |
153 | \end{itemize} | |
154 | ||
155 | Platform-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 | |
161 | borders; | |
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 | ||
167 | To make use of wxWindows, you currently need one or both of the | |
168 | following setups. | |
169 | ||
170 | (a) PC: | |
171 | ||
172 | \begin{enumerate}\itemsep=0pt | |
173 | \item A 486 or higher PC running MS Windows. | |
bd0df01f JS |
174 | \item One of Microsoft Visual C++ 4.0 or higher, Borland C++, Gnu-Win32. |
175 | \item At least 60 MB of disk space. | |
a660d684 KB |
176 | \end{enumerate} |
177 | ||
bd0df01f | 178 | (b) Unix: |
a660d684 KB |
179 | |
180 | \begin{enumerate}\itemsep=0pt | |
181 | \item Almost any C++ compiler, including GNU C++. | |
bd0df01f JS |
182 | \item Almost any Unix workstation, and GTK 1.0 or higher. |
183 | \item At least 60 MB of disk space. | |
a660d684 KB |
184 | \end{enumerate} |
185 | ||
186 | \section{Availability and location of wxWindows} | |
187 | ||
188 | wxWindows is currently available from the Artificial Intelligence | |
189 | Applications Institute by anonymous FTP and World Wide Web: | |
190 | ||
191 | \begin{verbatim} | |
192 | ftp://ftp.aiai.ed.ac.uk/pub/packages/wxwin | |
193 | http://web.ukonline.co.uk/julian.smart/wxwin | |
194 | \end{verbatim} | |
195 | ||
196 | \section{Acknowledgments} | |
197 | ||
bd0df01f JS |
198 | Thanks are due to AIAI for being willing to release the original version of |
199 | wxWindows into the public domain, and to our patient wives Harriet, Tanja and others. | |
200 | ||
201 | We would particularly like to thank the following for their contributions to wxWindows, and the many others who have been involved in | |
202 | the project over the years. Apologies for any unintentional omissions from this list. | |
203 | ||
204 | Yiorgos Adamopoulos, Jamshid Afshar, Alejandro Aguilar-Sierra, AIAI, Patrick Albert, Karsten Ballueder, Michael Bedward, Kai Bendorf, Yura Bidus, Keith | |
205 | Gary Boyce, Chris Breeze, Pete Britton, Ian Brown, C. Buckley, Dmitri Chubraev, Robin Corbet, Cecil Coupe, Andrew Davison, Neil Dudman, Robin | |
206 | Dunn, Hermann Dunkel, Jos van Eijndhoven, Tom Felici, Thomas Fettig, Matthew Flatt, Pasquale Foggia, Josep Fortiana, Todd Fries, Dominic Gallagher, | |
207 | Wolfram Gloger, Norbert Grotz, Stefan Gunter, Bill Hale, Patrick Halke, Stefan Hammes, Guillaume Helle, Harco de Hilster, Cord Hockemeyer, Markus | |
208 | Holzem, Olaf Klein, Leif Jensen, Bart Jourquin, Guilhem Lavaux, Jan Lessner, Nicholas Liebmann, Torsten Liermann, Per Lindqvist, Thomas Runge, Tatu | |
209 | M\"{a}nnist\"{o}, Scott Maxwell, Thomas Myers, Oliver Niedung, Hernan Otero, Ian Perrigo, Timothy Peters, Giordano Pezzoli, Harri Pasanen, Thomaso Paoletti, | |
210 | Garrett Potts, Marcel Rasche, Robert Roebling, Dino Scaringella, Jobst Schmalenbach, Arthur Seaton, Paul Shirley, Stein Somers, Petr Smilauer, Neil Smith, | |
211 | Kari Syst\"{a}, Arthur Tetzlaff-Deas, Jonathan Tonberg, Jyrki Tuomi, Janos Vegh, Andrea Venturoli, Vadim Zeitlin, Xiaokun Zhu, Edward Zimmermann. | |
a660d684 KB |
212 | |
213 | `Graphplace', the basis for the wxGraphLayout library, is copyright Dr. Jos | |
214 | T.J. van Eijndhoven of Eindhoven University of Technology. The code has | |
215 | been used in wxGraphLayout with his permission. | |
216 | ||
bd0df01f | 217 | We also acknowledge the author of XFIG, the excellent Unix drawing tool, |
a660d684 KB |
218 | from the source of which we have borrowed some spline drawing code. |
219 | His copyright is included below. | |
220 | ||
221 | {\it XFig2.1 is copyright (c) 1985 by Supoj Sutanthavibul. Permission to | |
222 | use, copy, modify, distribute, and sell this software and its | |
223 | documentation for any purpose is hereby granted without fee, provided | |
224 | that the above copyright notice appear in all copies and that both that | |
225 | copyright notice and this permission notice appear in supporting | |
226 | documentation, and that the name of M.I.T. not be used in advertising or | |
227 | publicity pertaining to distribution of the software without specific, | |
228 | written prior permission. M.I.T. makes no representations about the | |
229 | suitability of this software for any purpose. It is provided ``as is'' | |
230 | without express or implied warranty.} | |
231 | ||
232 | \chapter{Multi-platform development with wxWindows}\label{multiplat} | |
233 | \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}% | |
234 | \setfooter{\thepage}{}{}{}{}{\thepage}% | |
235 | ||
236 | This chapter describes the practical details of using wxWindows. Please | |
237 | see the file install.txt for up-to-date installation instructions, and | |
238 | changes.txt for differences between versions. | |
239 | ||
240 | \section{Include files} | |
241 | ||
bd0df01f | 242 | The main include file is {\tt "wx/wx.h"}; this includes the most commonly |
a660d684 KB |
243 | used modules of wxWindows. |
244 | ||
245 | To save on compilation time, include only those header files relevant to the | |
246 | source file. If you are using precompiled headers, you should include | |
247 | the following section before any other includes: | |
248 | ||
249 | \begin{verbatim} | |
250 | // For compilers that support precompilation, includes "wx.h". | |
bd0df01f | 251 | #include <wx/wxprec.h> |
a660d684 KB |
252 | |
253 | #ifdef __BORLANDC__ | |
254 | #pragma hdrstop | |
255 | #endif | |
256 | ||
257 | #ifndef WX_PRECOMP | |
bd0df01f JS |
258 | // Include your minimal set of headers here, or wx.h |
259 | #include <wx/wx.h> | |
a660d684 KB |
260 | #endif |
261 | ||
262 | ... now your other include files ... | |
263 | \end{verbatim} | |
264 | ||
bd0df01f | 265 | The file {\tt "wx/wxprec.h"} includes {\tt "wx/wx.h"}. Although this incantation |
a660d684 KB |
266 | may seem quirky, it is in fact the end result of a lot of experimentation, |
267 | and several Windows compilers to use precompilation (those tested are Microsoft Visual C++, Borland C++ | |
268 | and Watcom C++). | |
269 | ||
bd0df01f | 270 | Borland precompilation is largely automatic. Visual C++ requires specification of {\tt "wx/wxprec.h"} as |
a660d684 KB |
271 | the file to use for precompilation. Watcom C++ is automatic apart from the specification of |
272 | the .pch file. Watcom C++ is strange in requiring the precompiled header to be used only for | |
273 | object files compiled in the same directory as that in which the precompiled header was created. | |
274 | Therefore, the wxWindows Watcom C++ makefiles go through hoops deleting and recreating | |
275 | a single precompiled header file for each module, thus preventing an accumulation of many | |
276 | multi-megabyte .pch files. | |
277 | ||
278 | \section{Libraries} | |
279 | ||
bd0df01f JS |
280 | Please the wxGTK documentation for use of the Unix version of wxWindows. |
281 | Under Windows, use the library wx.lib for stand-alone Windows | |
a660d684 KB |
282 | applications, or wxdll.lib for creating DLLs. |
283 | ||
284 | \section{Configuration} | |
285 | ||
286 | The following lists the options configurable in the file | |
bd0df01f | 287 | \rtfsp{\tt "wx/msw/setup.h"} and {\tt "wx/gtk/setup.h"} Some settings are a matter |
a660d684 KB |
288 | of taste, some help with platform-specific problems, and |
289 | others can be set to minimize the size of the library. | |
290 | ||
291 | \subsection{General features} | |
292 | ||
293 | \begin{twocollist}\itemsep=0pt | |
294 | \twocolitem{USE\_CLIPBOARD}{If 1, clipboard code is compiled (Windows only).} | |
295 | \twocolitem{USE\_CONSTRAINTS}{If 1, the constaint-based window layout system is compiled.} | |
296 | \twocolitem{USE\_DOC\_VIEW\_ARCHITECTURE}{If 1, wxDocument, wxView and related classes are compiled.} | |
297 | \twocolitem{USE\_DYNAMIC\_CLASSES}{If 1, the run-time class macros and classes are compiled. Recommended, | |
298 | and necessary for the document/view framework.} | |
a660d684 KB |
299 | \twocolitem{USE\_HELP}{If 1, interface to help system is compiled.} |
300 | \twocolitem{USE\_GAUGE}{If 1, the wxGauge class compiled.} | |
301 | \twocolitem{USE\_GLOBAL\_MEMORY\_OPERATORS}{If 1, redefines global new and delete operators to be compatible | |
302 | with the extended arguments of the debugging wxObject new and delete operators. If this causes problems | |
303 | for your compiler, set to 0.} | |
a660d684 KB |
304 | \twocolitem{USE\_IPC}{If 1, interprocess communication code is compiled.} |
305 | \twocolitem{USE\_MEMORY\_TRACING}{If 1, enables debugging versions of wxObject::new and wxObject::delete | |
306 | if the value of DEBUG is defined to more than 0.} | |
307 | \twocolitem{USE\_METAFILE}{If 1, Windows Metafile code is compiled.} | |
a660d684 KB |
308 | \twocolitem{USE\_POSTSCRIPT}{If 1, PostScript code is compiled.} |
309 | \twocolitem{USE\_POSTSCRIPT\_ARCHITECTURE\_IN\_MSW}{Set to 1 to enable the printing architecture | |
310 | to make use of either native Windows printing facilities, or the wxPostScriptDC class depending | |
311 | on the wxApp::SetPrintMode setting.} | |
312 | \twocolitem{USE\_PRINTING\_ARCHITECTURE}{If 1, wxPrinter, wxPrintout and related classes are compiled | |
313 | for the print/preview framework.} | |
314 | \twocolitem{USE\_RESOURCES}{If 1, win.ini or .Xdefaults-style resource read/write code is compiled.} | |
a660d684 | 315 | \twocolitem{USE\_WX\_RESOURCES}{If 1, wxWindows resource file (.WXR) code is compiled.} |
a660d684 KB |
316 | \end{twocollist} |
317 | ||
318 | \subsection{Windows and NT features} | |
319 | ||
320 | \begin{twocollist} | |
321 | \twocolitem{CTL3D}{CTL3D should only be used for 16-bit Windows programs. | |
322 | On Windows 95 and NT, native 3D effects are used. If you want to | |
323 | use it and don't already have CTL3D installed, copy the files in | |
324 | contrib/ctl3d to appropriate places (ctl3dv2.lib/ctl3d32.lib into your compiler lib | |
325 | directory, ctl3d.h into an include directory, and ctl3dv2.dll into | |
326 | windows/system). You may need to find a compiler-specific version of ctl3dv2.lib | |
327 | or ctl3d32.lib. Define CTL3D to be 1 in wx\_setup.h and link your executables with ctl3dv2.lib | |
328 | or ctl3d32.lib.} | |
a660d684 KB |
329 | \twocolitem{USE\_ODBC}{If 1, compiles wxDatabase and wxRecordSet classes for ODBC |
330 | access. Requires sql.h, sqlext.h files if set to 1 (see topic on database support).} | |
331 | \end{twocollist} | |
332 | ||
333 | \section{Makefiles} | |
334 | ||
bd0df01f | 335 | At the moment there is no attempt to make Unix makefiles and |
a660d684 KB |
336 | PC makefiles compatible, i.e. one makefile is required for |
337 | each environment. | |
338 | ||
bd0df01f | 339 | Sample makefiles for Unix (suffix .UNX), MS C++ (suffix .DOS and .NT), Borland |
a660d684 KB |
340 | C++ (.BCC) and Symantec C++ (.SC) are included for the library, demos |
341 | and utilities. The NT, Borland and Symantec makefiles cannot be | |
342 | guaranteed to be up-to-date since the author does not have | |
343 | these compilers. | |
344 | ||
345 | The controlling makefile for wxWindows is in the platform-specific | |
346 | directory, such as {\tt src/msw} or {\tt src/x}. This makefile will | |
347 | recursively execute the makefile in {\tt src/base}. | |
348 | ||
349 | \subsection{Windows makefiles} | |
350 | ||
351 | For Microsoft C++, normally it is only necessary to type {\tt nmake -f | |
352 | makefile.dos} (or an alias or batch file which does this). By default, | |
353 | binaries are made with debugging information, and no optimization. Use | |
354 | FINAL=1 on the command line to remove debugging information (this only | |
355 | really necessary at the link stage), and DLL=1 to make a DLL version of | |
356 | the library, if building a library. | |
357 | ||
bd0df01f | 358 | \subsection{Unix makefiles} |
a660d684 KB |
359 | |
360 | TODO. | |
361 | ||
362 | Debugging information is included by default; you may add DEBUG= as an | |
bd0df01f | 363 | argument to make to compile without it, or use the Unix {\bf strip} |
a660d684 KB |
364 | command to remove debugging information from an executable. |
365 | ||
366 | \normalbox{{\it Important note:} Most compiler flags are kept centrally in | |
367 | src/make.env, which is included by all other makefiles. This is the | |
368 | file to edit to tailor wxWindows compilation to your environment.} | |
369 | ||
370 | \section{Windows-specific files} | |
371 | ||
372 | wxWindows application compilation under MS Windows requires at least two | |
373 | extra files, resource and module definition files. | |
374 | ||
375 | \subsection{Resource file}\label{resources} | |
376 | ||
377 | The least that must be defined in the Windows resource file (extension RC) | |
378 | is the following statement: | |
379 | ||
380 | \begin{verbatim} | |
bd0df01f | 381 | rcinclude "wx/msw/wx.rc" |
a660d684 KB |
382 | \end{verbatim} |
383 | ||
384 | which includes essential internal wxWindows definitions. The resource script | |
385 | may also contain references to icons, cursors, etc., for example: | |
386 | ||
387 | \begin{verbatim} | |
388 | wxicon icon wx.ico | |
389 | \end{verbatim} | |
390 | ||
391 | The icon can then be referenced by name when creating a frame icon. See | |
392 | the MS Windows SDK documentation. | |
393 | ||
394 | \normalbox{Note: include wx.rc {\it after} any ICON statements | |
395 | so programs that search your executable for icons (such | |
396 | as the Program Manager) find your application icon first.} | |
397 | ||
398 | \subsection{Module definition file} | |
399 | ||
bd0df01f JS |
400 | A module definition file (extension DEF) is required for 16-bit applications, and |
401 | looks like the following: | |
a660d684 KB |
402 | |
403 | \begin{verbatim} | |
404 | NAME Hello | |
405 | DESCRIPTION 'Hello' | |
406 | EXETYPE WINDOWS | |
407 | STUB 'WINSTUB.EXE' | |
408 | CODE PRELOAD MOVEABLE DISCARDABLE | |
409 | DATA PRELOAD MOVEABLE MULTIPLE | |
410 | HEAPSIZE 1024 | |
411 | STACKSIZE 8192 | |
412 | \end{verbatim} | |
413 | ||
414 | The only lines which will usually have to be changed per application are | |
415 | NAME and DESCRIPTION. | |
416 | ||
a660d684 KB |
417 | \subsection{Allocating and deleting wxWindows objects} |
418 | ||
419 | In general, classes derived from wxWindow must dynamically allocated | |
420 | with {\it new} and deleted with {\it delete}. If you delete a window, | |
421 | all of its children and descendants will be automatically deleted, | |
422 | so you don't need to delete these descendants explicitly. | |
423 | ||
bd0df01f JS |
424 | When deleting a frame or dialog, use {\bf Destroy} rather than {\bf delete} so |
425 | that the wxWindows delayed deletion can take effect. This waits until idle time | |
426 | (when all messages have been processed) to actually delete the window, to avoid | |
427 | problems associated with the GUI sending events to deleted windows. | |
a660d684 | 428 | |
bd0df01f JS |
429 | Don't create a window on the stack, because this will interfere |
430 | with delayed deletion. | |
a660d684 KB |
431 | |
432 | If you decide to allocate a C++ array of objects (such as wxBitmap) that may | |
433 | be cleaned up by wxWindows, make sure you delete the array explicitly | |
434 | before wxWindows has a chance to do so on exit, since calling {\it delete} on | |
435 | array members will cause memory problems. | |
436 | ||
437 | wxColour can be created statically: it is not automatically cleaned | |
438 | up and is unlikely to be shared between other objects; it is lightweight | |
439 | enough for copies to be made. | |
440 | ||
441 | Beware of deleting objects such as a wxPen or wxBitmap if they are still in use. | |
442 | Windows is particularly sensitive to this: so make sure you | |
bd0df01f | 443 | make calls like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before deleting |
a660d684 KB |
444 | a drawing object that may be in use. Code that doesn't do this will probably work |
445 | fine on some platforms, and then fail under Windows. | |
446 | ||
a660d684 KB |
447 | \section{Conditional compilation} |
448 | ||
449 | One of the purposes of wxWindows is to reduce the need for conditional | |
450 | compilation in source code, which can be messy and confusing to follow. | |
451 | However, sometimes it is necessary to incorporate platform-specific | |
452 | features (such as metafile use under MS Windows). The following identifiers | |
453 | may be used for this purpose, along with any user-supplied ones: | |
454 | ||
bd0df01f | 455 | {\bf GUIs:} |
a660d684 KB |
456 | |
457 | \begin{verbatim} | |
bd0df01f JS |
458 | __X__ any X, but not GTK |
459 | __WXMOTIF__ Motif | |
460 | __WXGTK__ GTK | |
461 | __WXMSW__ Any Windows | |
462 | __MAC__ MacOS | |
463 | __UNIX__ any Unix | |
464 | __WIN95__ GUI for Windows 95 and above; NT 4.0 and above. | |
465 | __WIN32__ WIN32 API | |
466 | __NT__ Windows NT | |
467 | __CURSES__ CURSES | |
a660d684 KB |
468 | \end{verbatim} |
469 | ||
bd0df01f | 470 | {\bf OSes:} |
a660d684 | 471 | |
bd0df01f JS |
472 | \begin{verbatim} |
473 | __HPUX__ | |
474 | __SVR4__ | |
475 | __SYSV__ | |
476 | __LINUX__ | |
477 | __SGI__ | |
478 | __ULTRIX__ | |
479 | __BSD__ | |
480 | __VMS__ | |
481 | __SUN__ Any Sun | |
482 | __SUNOS__ | |
483 | __SOLARIS__ | |
484 | __ALPHA__ | |
485 | __AIX__ | |
486 | __DATA_GENERAL__ | |
487 | __OSF__ | |
488 | __FREEBSD__ | |
489 | \end{verbatim} | |
a660d684 | 490 | |
bd0df01f | 491 | {\bf Compilers:} |
a660d684 | 492 | |
bd0df01f JS |
493 | \begin{verbatim} |
494 | __GNUWIN32__ Gnu-Win32 compiler | |
495 | __DJGPP__ DJGPP | |
496 | __GNUG__ Gnu C++ on any platform | |
497 | __BORLANDC__ Borland C++ | |
498 | __WATCOMC__ Watcom C++ | |
499 | __SYMANTECC__ Symantec C++ | |
500 | __VISUALC__ VC++ | |
501 | __SUNCC__ | |
502 | \end{verbatim} | |
503 | ||
504 | {\bf wxWindows modes:} | |
a660d684 | 505 | |
bd0df01f JS |
506 | \begin{verbatim} |
507 | __WXDEBUG__ usage: #ifdef __DEBUG__ (=> debug mode, else => release) | |
508 | \end{verbatim} | |
a660d684 | 509 | |
b8de493f | 510 | |
a660d684 KB |
511 | \section{C++ issues} |
512 | ||
bd0df01f | 513 | The following documents some miscellaneous C++ issues. |
a660d684 KB |
514 | |
515 | \subsection{Templates} | |
516 | ||
bd0df01f | 517 | wxWindows does not use templates since it is a notoriously unportable feature. |
a660d684 KB |
518 | |
519 | \subsection{Precompiled headers} | |
520 | ||
521 | Some compilers, such as Borland C++ and Microsoft C++, support | |
522 | precompiled headers. This can save a great deal of compiling time. The | |
523 | recommended approach is to precompile {\tt ``wx.h''}, using this | |
524 | precompiled header for compiling both wxWindows itself and any | |
525 | wxWindows applications. For Windows compilers, two dummy source files | |
526 | are provided (one for normal applications and one for creating DLLs) | |
527 | to allow initial creation of the precompiled header. | |
528 | ||
529 | However, there are several downsides to using precompiled headers. One | |
530 | is that to take advantage of the facility, you often need to include | |
531 | more header files than would normally be the case. This means that | |
532 | changing a header file will cause more recompilations (in the case of | |
533 | wxWindows, everything needs to be recompiled since everything includes | |
534 | {\tt ``wx.h''}!) | |
535 | ||
536 | A related problem is that for compilers that don't have precompiled | |
537 | headers, including a lot of header files slows down compilation | |
538 | considerably. For this reason, you will find (in the common | |
539 | X and Windows parts of the library) conditional | |
bd0df01f | 540 | compilation that under Unix, includes a minimal set of headers; |
a660d684 KB |
541 | and when using Visual C++, includes {\tt wx.h}. This should help provide |
542 | the optimal compilation for each compiler, although it is | |
543 | biassed towards the precompiled headers facility available | |
544 | in Microsoft C++. | |
545 | ||
546 | \section{File handling} | |
547 | ||
548 | When building an application which may be used under different | |
549 | environments, one difficulty is coping with documents which may be | |
550 | moved to different directories on other machines. Saving a file which | |
551 | has pointers to full pathnames is going to be inherently unportable. One | |
552 | approach is to store filenames on their own, with no directory | |
553 | information. The application searches through a number of locally | |
554 | defined directories to find the file. To support this, the class {\bf | |
555 | wxPathList} makes adding directories and searching for files easy, and | |
556 | the global function {\bf FileNameFromPath} allows the application to | |
557 | strip off the filename from the path if the filename must be stored. | |
558 | This has undesirable ramifications for people who have documents of the | |
559 | same name in different directories. | |
560 | ||
561 | As regards the limitations of DOS 8+3 single-case filenames versus | |
bd0df01f | 562 | unrestricted Unix filenames, the best solution is to use DOS filenames |
a660d684 KB |
563 | for your application, and also for document filenames {\it if} the user |
564 | is likely to be switching platforms regularly. Obviously this latter | |
565 | choice is up to the application user to decide. Some programs (such as | |
566 | YACC and LEX) generate filenames incompatible with DOS; the best | |
bd0df01f | 567 | solution here is to have your Unix makefile rename the generated files |
a660d684 | 568 | to something more compatible before transferring the source to DOS. |
bd0df01f | 569 | Transferring DOS files to Unix is no problem, of course, apart from EOL |
a660d684 KB |
570 | conversion for which there should be a utility available (such as |
571 | dos2unix). | |
572 | ||
573 | See also the File Functions section of the reference manual for | |
574 | descriptions of miscellaneous file handling functions. | |
575 | ||
576 | \chapter{Utilities supplied with wxWindows}\label{utilities} | |
577 | \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}% | |
578 | \setfooter{\thepage}{}{}{}{}{\thepage}% | |
579 | ||
580 | A number of `extras' are supplied with wxWindows, to complement | |
581 | the GUI functionality in the main class library. These are found | |
582 | below the utils directory and usually have their own source, library | |
583 | and documentation directories. For larger user-contributed packages, | |
584 | see the directory /pub/packages/wxwin/contrib. | |
585 | ||
586 | \section{wxHelp}\label{wxhelp} | |
587 | ||
588 | wxHelp is a stand-alone program, written using wxWindows, | |
589 | for displaying hypertext help. It is necessary since not all target | |
590 | systems (notably X) supply an adequate | |
591 | standard for on-line help. wxHelp is modelled on the MS Windows help | |
592 | system, with contents, search and browse buttons, but does not reformat | |
593 | text to suit the size of window, as WinHelp does, and its input files | |
594 | are uncompressed ASCII with some embedded font commands and an .xlp | |
595 | extension. Most wxWindows documentation (user manuals and class | |
596 | references) is supplied in wxHelp format, and also in Windows Help | |
bd0df01f JS |
597 | format. The wxWindows 2.0 project will presently use an HTML widget |
598 | in a new and improved wxHelp implementation, under X. | |
a660d684 KB |
599 | |
600 | Note that an application can be programmed to use Windows Help under | |
601 | MS Windows, and wxHelp under X. An alternative help viewer under X is | |
602 | Mosaic, a World Wide Web viewer that uses HTML as its native hypertext | |
603 | format. However, this is not currently integrated with wxWindows | |
604 | applications. | |
605 | ||
606 | wxHelp works in two modes---edit and end-user. In edit mode, an ASCII | |
607 | file may be marked up with different fonts and colours, and divided into | |
608 | sections. In end-user mode, no editing is possible, and the user browses | |
609 | principally by clicking on highlighted blocks. | |
610 | ||
611 | When an application invokes wxHelp, subsequent sections, blocks or | |
612 | files may be viewed using the same instance of wxHelp since the two | |
613 | programs are linked using wxWindows interprocess communication | |
614 | facilities. When the application exits, that application's instance of | |
fe604ccd | 615 | wxHelp may be made to exit also. See the {\bf wxHelpControllerBase} entry in the |
a660d684 KB |
616 | reference section for how an application controls wxHelp. |
617 | ||
618 | \section{Tex2RTF}\label{textortf} | |
619 | ||
620 | Supplied with wxWindows is a utility called Tex2RTF for converting\rtfsp | |
621 | \LaTeX\ manuals to the following formats: | |
622 | ||
623 | \begin{description} | |
624 | \item[wxHelp] | |
625 | wxWindows help system format (XLP). | |
626 | \item[Linear RTF] | |
627 | Rich Text Format suitable for importing into a word processor. | |
628 | \item[Windows Help RTF] | |
629 | Rich Text Format suitable for compiling into a WinHelp HLP file with the | |
630 | help compiler. | |
631 | \item[HTML] | |
632 | HTML is the native format for Mosaic, the main hypertext viewer for | |
633 | the World Wide Web. Since it is freely available it is a good candidate | |
634 | for being the wxWindows help system under X, as an alternative to wxHelp. | |
635 | \end{description} | |
636 | ||
637 | Tex2RTF is used for the wxWindows manuals and can be used independently | |
638 | by authors wishing to create on-line and printed manuals from the same\rtfsp | |
639 | \LaTeX\ source. Please see the separate documentation for Tex2RTF. | |
640 | ||
641 | \section{wxTreeLayout} | |
642 | ||
643 | This is a simple class library for drawing trees in a reasonably pretty | |
644 | fashion. It provides only minimal default drawing capabilities, since | |
645 | the algorithm is meant to be used for implementing custom tree-based | |
646 | tools. | |
647 | ||
648 | Directed graphs may also be drawn using this library, if cycles are | |
649 | removed before the nodes and arcs are passed to the algorithm. | |
650 | ||
651 | Tree displays are used in many applications: directory browsers, | |
652 | hypertext systems, class browsers, and decision trees are a few | |
653 | possibilities. | |
654 | ||
655 | See the separate manual and the directory utils/wxtree. | |
656 | ||
657 | \section{wxGraphLayout} | |
658 | ||
659 | The wxGraphLayout class is based on a tool called `graphplace' by Dr. | |
660 | Jos T.J. van Eijndhoven of Eindhoven University of Technology. Given a | |
661 | (possibly cyclic) directed graph, it does its best to lay out the nodes | |
662 | in a sensible manner. There are many applications (such as diagramming) | |
663 | where it is required to display a graph with no human intervention. Even | |
664 | if manual repositioning is later required, this algorithm can make a good | |
665 | first attempt. | |
666 | ||
667 | See the separate manual and the directory utils/wxgraph. | |
668 | ||
a660d684 KB |
669 | \section{Colours}\label{coloursampler} |
670 | ||
671 | A colour sampler for viewing colours and their names on each | |
672 | platform. | |
673 | ||
674 | % | |
675 | \chapter{Tutorial}\label{tutorial} | |
676 | \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}% | |
677 | \setfooter{\thepage}{}{}{}{}{\thepage}% | |
678 | ||
679 | To be written. | |
680 | ||
681 | \chapter{Programming strategies}\label{strategies} | |
682 | \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}% | |
683 | \setfooter{\thepage}{}{}{}{}{\thepage}% | |
684 | ||
685 | This chapter is intended to list strategies that may be useful when | |
686 | writing and debugging wxWindows programs. If you have any good tips, | |
687 | please submit them for inclusion here. | |
688 | ||
689 | \section{Strategies for reducing programming errors} | |
690 | ||
691 | \subsection{Use ASSERT} | |
692 | ||
693 | Although I haven't done this myself within wxWindows, it is good | |
694 | practice to use ASSERT statements liberally, that check for conditions that | |
695 | should or should not hold, and print out appropriate error messages. | |
696 | These can be compiled out of a non-debugging version of wxWindows | |
697 | and your application. Using ASSERT is an example of `defensive programming': | |
698 | it can alert you to problems later on. | |
699 | ||
700 | \subsection{Use wxString in preference to character arrays} | |
701 | ||
702 | Using wxString can be much safer and more convenient than using char *. | |
703 | Again, I haven't practised what I'm preaching, but I'm now trying to use | |
704 | wxString wherever possible. You can reduce the possibility of memory | |
705 | leaks substantially, and it's much more convenient to use the overloaded | |
706 | operators than functions such as strcmp. wxString won't add a significant | |
707 | overhead to your program; the overhead is compensated for by easier | |
708 | manipulation (which means less code). | |
709 | ||
710 | The same goes for other data types: use classes wherever possible. | |
711 | ||
712 | \section{Strategies for portability} | |
713 | ||
714 | \subsection{Use relative positioning or constraints} | |
715 | ||
716 | Don't use absolute panel item positioning if you can avoid it. Different GUIs have | |
717 | very differently sized panel items. Consider using the constraint system, although this | |
718 | can be complex to program. If you needs are simple, the default relative positioning | |
719 | behaviour may be adequate (using default position values and wxPanel::NewLine). | |
720 | ||
721 | Alternatively, you could use alternative .wrc (wxWindows resource files) on different | |
722 | platforms, with slightly different dimensions in each. Or space your panel items out | |
723 | to avoid problems. | |
724 | ||
725 | \subsection{Use wxWindows resource files} | |
726 | ||
727 | Use .wrc (wxWindows resource files) where possible, because they can be easily changed | |
728 | independently of source code. Bitmap resources can be set up to load different | |
729 | kinds of bitmap depending on platform (see the section on resource files). | |
730 | ||
731 | \section{Strategies for debugging} | |
732 | ||
733 | \subsection{Positive thinking} | |
734 | ||
735 | It's common to blow up the problem in one's imagination, so that it seems to threaten | |
736 | weeks, months or even years of work. The problem you face may seem insurmountable: | |
737 | but almost never is. Once you have been programming for some time, you will be able | |
738 | to remember similar incidents that threw you into the depths of despair. But | |
739 | remember, you always solved the problem, somehow! | |
740 | ||
741 | Perseverance is often the key, even though a seemingly trivial problem | |
742 | can take an apparently inordinate amount of time to solve. In the end, | |
743 | you will probably wonder why you worried so much. That's not to say it | |
744 | isn't painful at the time. Try not to worry -- there are many more important | |
745 | things in life. | |
746 | ||
747 | \subsection{Simplify the problem} | |
748 | ||
749 | Reduce the code exhibiting the problem to the smallest program possible | |
750 | that exhibits the problem. If it is not possible to reduce a large and | |
751 | complex program to a very small program, then try to ensure your code | |
752 | doesn't hide the problem (you may have attempted to minimize the problem | |
753 | in some way: but now you want to expose it). | |
754 | ||
755 | With luck, you can add a small amount of code that causes the program | |
756 | to go from functioning to non-functioning state. This should give a clue | |
757 | to the problem. In some cases though, such as memory leaks or wrong | |
758 | deallocation, this can still give totally spurious results! | |
759 | ||
760 | \subsection{Genetic mutation} | |
761 | ||
762 | If we had sophisticated genetic algorithm tools that could be applied | |
763 | to programming, we could use them. Until then, a common -- if rather irrational -- | |
764 | technique is to just make arbitrary changes to the code until something | |
765 | different happens. You may have an intuition why a change will make a difference; | |
766 | otherwise, just try altering the order of code, comment lines out, anything | |
767 | to get over an impasse. Obviously, this is usually a last resort. | |
768 | ||
769 | \subsection{Use a debugger} | |
770 | ||
771 | This sounds like facetious advice, but it's surprising how often people | |
772 | don't use a debugger. Often it's an overhead to install or learn how to | |
773 | use a debugger, but it really is essential for anything but the most | |
774 | trivial programs. Some platforms don't allow for debugging, such | |
775 | as WIN32s under Windows 3.x. In this case, you might be advised to | |
776 | debug under 16-bit Windows and when you're confident, compile for | |
777 | WIN32s. In fact WIN32s can be very strict about bad memory handling, | |
778 | so testing out under WIN32s is a good thing to do even if you're | |
779 | not going to distribute this version. (Unless you've got a good memory checking, | |
780 | utility, of course!) Tracking bugs under WIN32s can involve a lot of debug message | |
781 | insertion and relinking, so make sure your compiler has a fast linker | |
782 | (e.g. Watcom, Symantec). | |
783 | ||
784 | \subsection{Use tracing code} | |
785 | ||
786 | You can use wxDebugMsg statements (or the wxDebugStreamBuf class) to | |
787 | output to a debugging window such as DBWIN under Windows, or standard | |
788 | error under X. If compiling in DEBUG mode, you can use TRACE statements | |
789 | that will be compiled out of the final build of your application. | |
790 | ||
791 | Using tracing statements may be more convenient than using the debugger | |
792 | in some circumstances (such as when your debugger doesn't support a lot | |
793 | of debugging code, or you wish to print a bunch of variables). | |
794 | ||
795 | \subsection{Use wxObject::Dump and the wxDebugContext class} | |
796 | ||
797 | It's good practice to implement the Dump member function for all | |
798 | classes derived from wxObject. You can then make use of wxDebugContext | |
799 | to dump out information on all objects in the program, if DEBUG is | |
800 | defined to be more than zero. You can use wxDebugContext to check for | |
801 | memory leaks and corrupt memory. See the debugging topic in the | |
802 | reference manual for more information. | |
803 | ||
804 | \subsection{Check Windows debug messages} | |
805 | ||
806 | Under Windows, it's worth running your program with DBWIN running or | |
807 | some other program that shows Windows-generated debug messages. It's | |
808 | possible it'll show invalid handles being used. You may have fun seeing | |
809 | what commercial programs cause these normally hidden errors! Microsoft | |
810 | recommend using the debugging version of Windows, which shows up even | |
811 | more problems. However, I doubt it's worth the hassle for most | |
812 | applications. wxWindows is designed to minimize the possibility of such | |
813 | errors, but they can still happen occasionally, slipping through unnoticed | |
814 | because they are not severe enough to cause a crash. |