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