]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/devtips.h
add meta to manifests
[wxWidgets.git] / docs / doxygen / devtips.h
CommitLineData
15b6757b
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: devtips.h
3// Purpose: Cross-platform development page of the Doxygen manual
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9
10/*!
11
12 @page multiplatform_page Multi-platform development with wxWidgets
13
14 This chapter describes the practical details of using wxWidgets. Please
15 see the file install.txt for up-to-date installation instructions, and
16 changes.txt for differences between versions.
17
18 @li @ref includefiles
19 @li @ref libraries
20 @li @ref configuration
21 @li @ref makefiles
22 @li @ref windowsfiles
23 @li @ref allocatingobjects
24 @li @ref architecturedependency
25 @li @ref conditionalcompilation
26 @li @ref cpp
27 @li @ref filehandling
28
29 <hr>
30
36c9828f 31
15b6757b 32 @section includefiles Include files
36c9828f 33
15b6757b
FM
34 The main include file is {\tt "wx/wx.h"}; this includes the most commonly
35 used modules of wxWidgets.
36c9828f 36
15b6757b
FM
37 To save on compilation time, include only those header files relevant to the
38 source file. If you are using precompiled headers, you should include
39 the following section before any other includes:
36c9828f 40
15b6757b
FM
41 @verbatim
42 // For compilers that support precompilation, includes "wx.h".
43 #include <wx/wxprec.h>
36c9828f 44
15b6757b
FM
45 #ifdef __BORLANDC__
46 #pragma hdrstop
47 #endif
36c9828f 48
15b6757b
FM
49 #ifndef WX_PRECOMP
50 // Include your minimal set of headers here, or wx.h
51 #include <wx/wx.h>
52 #endif
36c9828f 53
15b6757b
FM
54 ... now your other include files ...
55 @endverbatim
36c9828f 56
15b6757b
FM
57 The file {\tt "wx/wxprec.h"} includes {\tt "wx/wx.h"}. Although this incantation
58 may seem quirky, it is in fact the end result of a lot of experimentation,
59 and several Windows compilers to use precompilation which is largely automatic for
60 compilers with necessary support. Currently it is used for Visual C++ (including
61 embedded Visual C++), Borland C++, Open Watcom C++, Digital Mars C++
62 and newer versions of GCC.
63 Some compilers might need extra work from the application developer to set the
64 build environment up as necessary for the support.
36c9828f 65
15b6757b
FM
66
67
68 @section libraries Libraries
36c9828f 69
15b6757b
FM
70 Most ports of wxWidgets can create either a static library or a shared
71 library. wxWidgets can also be built in multilib and monolithic variants.
72 See the \helpref{libraries list}{librarieslist} for more
73 information on these.
36c9828f 74
15b6757b
FM
75
76
77 @section configuration Configuration
36c9828f 78
15b6757b
FM
79 When using project files and makefiles directly to build wxWidgets,
80 options are configurable in the file
36c9828f 81 \rtfsp{\tt "wx/XXX/setup.h"} where XXX is the required platform (such as msw, motif, gtk, mac). Some
15b6757b
FM
82 settings are a matter of taste, some help with platform-specific problems, and
83 others can be set to minimize the size of the library. Please see the setup.h file
84 and {\tt install.txt} files for details on configuration.
36c9828f 85
15b6757b
FM
86 When using the 'configure' script to configure wxWidgets (on Unix and other platforms where
87 configure is available), the corresponding setup.h files are generated automatically
88 along with suitable makefiles. When using the RPM packages
89 for installing wxWidgets on Linux, a correct setup.h is shipped in the package and
90 this must not be changed.
36c9828f 91
15b6757b
FM
92
93
94 @section makefiles Makefiles
36c9828f 95
15b6757b
FM
96 On Microsoft Windows, wxWidgets has a different set of makefiles for each
97 compiler, because each compiler's 'make' tool is slightly different.
98 Popular Windows compilers that we cater for, and the corresponding makefile
99 extensions, include: Microsoft Visual C++ (.vc), Borland C++ (.bcc),
100 OpenWatcom C++ (.wat) and MinGW/Cygwin (.gcc). Makefiles are provided
101 for the wxWidgets library itself, samples, demos, and utilities.
36c9828f 102
15b6757b
FM
103 On Linux, Mac and OS/2, you use the 'configure' command to
104 generate the necessary makefiles. You should also use this method when
105 building with MinGW/Cygwin on Windows.
36c9828f 106
15b6757b
FM
107 We also provide project files for some compilers, such as
108 Microsoft VC++. However, we recommend using makefiles
109 to build the wxWidgets library itself, because makefiles
110 can be more powerful and less manual intervention is required.
36c9828f 111
15b6757b
FM
112 On Windows using a compiler other than MinGW/Cygwin, you would
113 build the wxWidgets library from the build/msw directory
114 which contains the relevant makefiles.
36c9828f 115
15b6757b
FM
116 On Windows using MinGW/Cygwin, and on Unix, MacOS X and OS/2, you invoke
117 'configure' (found in the top-level of the wxWidgets source hierarchy),
118 from within a suitable empty directory for containing makefiles, object files and
119 libraries.
36c9828f 120
15b6757b
FM
121 For details on using makefiles, configure, and project files,
122 please see docs/xxx/install.txt in your distribution, where
123 xxx is the platform of interest, such as msw, gtk, x11, mac.
36c9828f 124
15b6757b
FM
125
126
127 @section windowsfiles Windows-specific files
36c9828f 128
15b6757b
FM
129 wxWidgets application compilation under MS Windows requires at least one
130 extra file: a resource file.
36c9828f 131
15b6757b 132 @subsection resources Resource file
36c9828f 133
15b6757b
FM
134 The least that must be defined in the Windows resource file (extension RC)
135 is the following statement:
36c9828f 136
15b6757b
FM
137 @verbatim
138 #include "wx/msw/wx.rc"
139 @endverbatim
36c9828f 140
15b6757b
FM
141 which includes essential internal wxWidgets definitions. The resource script
142 may also contain references to icons, cursors, etc., for example:
36c9828f 143
15b6757b
FM
144 @verbatim
145 wxicon icon wx.ico
146 @endverbatim
36c9828f 147
15b6757b
FM
148 The icon can then be referenced by name when creating a frame icon. See
149 the MS Windows SDK documentation.
36c9828f 150
15b6757b
FM
151 \normalbox{Note: include wx.rc {\it after} any ICON statements
152 so programs that search your executable for icons (such
153 as the Program Manager) find your application icon first.}
36c9828f 154
15b6757b
FM
155
156
157 @section allocatingobjects Allocating and deleting wxWidgets objects
36c9828f 158
15b6757b
FM
159 In general, classes derived from wxWindow must dynamically allocated
160 with {\it new} and deleted with {\it delete}. If you delete a window,
161 all of its children and descendants will be automatically deleted,
162 so you don't need to delete these descendants explicitly.
36c9828f 163
15b6757b
FM
164 When deleting a frame or dialog, use {\bf Destroy} rather than {\bf delete} so
165 that the wxWidgets delayed deletion can take effect. This waits until idle time
166 (when all messages have been processed) to actually delete the window, to avoid
167 problems associated with the GUI sending events to deleted windows.
36c9828f 168
15b6757b
FM
169 Don't create a window on the stack, because this will interfere
170 with delayed deletion.
36c9828f 171
15b6757b
FM
172 If you decide to allocate a C++ array of objects (such as wxBitmap) that may
173 be cleaned up by wxWidgets, make sure you delete the array explicitly
174 before wxWidgets has a chance to do so on exit, since calling {\it delete} on
175 array members will cause memory problems.
36c9828f 176
15b6757b
FM
177 wxColour can be created statically: it is not automatically cleaned
178 up and is unlikely to be shared between other objects; it is lightweight
179 enough for copies to be made.
36c9828f 180
15b6757b
FM
181 Beware of deleting objects such as a wxPen or wxBitmap if they are still in use.
182 Windows is particularly sensitive to this: so make sure you
183 make calls like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before deleting
184 a drawing object that may be in use. Code that doesn't do this will probably work
185 fine on some platforms, and then fail under Windows.
36c9828f 186
15b6757b
FM
187
188
189 @section architecturedependency Architecture dependency
36c9828f 190
15b6757b
FM
191 A problem which sometimes arises from writing multi-platform programs is that
192 the basic C types are not defined the same on all platforms. This holds true
36c9828f 193 for both the length in bits of the standard types (such as int and long) as
15b6757b
FM
194 well as their byte order, which might be little endian (typically
195 on Intel computers) or big endian (typically on some Unix workstations). wxWidgets
196 defines types and macros that make it easy to write architecture independent
197 code. The types are:
36c9828f 198
15b6757b 199 wxInt32, wxInt16, wxInt8, wxUint32, wxUint16 = wxWord, wxUint8 = wxByte
36c9828f 200
15b6757b
FM
201 where wxInt32 stands for a 32-bit signed integer type etc. You can also check
202 which architecture the program is compiled on using the wxBYTE\_ORDER define
203 which is either wxBIG\_ENDIAN or wxLITTLE\_ENDIAN (in the future maybe wxPDP\_ENDIAN
204 as well).
36c9828f 205
15b6757b
FM
206 The macros handling bit-swapping with respect to the applications endianness
207 are described in the \helpref{Byte order macros}{byteordermacros} section.
36c9828f 208
15b6757b
FM
209
210
211 @section conditionalcompilation Conditional compilation
36c9828f 212
15b6757b
FM
213 One of the purposes of wxWidgets is to reduce the need for conditional
214 compilation in source code, which can be messy and confusing to follow.
215 However, sometimes it is necessary to incorporate platform-specific
36c9828f 216 features (such as metafile use under MS Windows). The \helpref{wxUSE\_*}{wxusedef}
15b6757b
FM
217 symbols listed in the file {\tt setup.h} may be used for this purpose,
218 along with any user-supplied ones.
36c9828f 219
15b6757b
FM
220
221
222 @section cpp C++ issues
36c9828f 223
15b6757b 224 The following documents some miscellaneous C++ issues.
36c9828f 225
15b6757b 226 @subsection templates Templates
36c9828f 227
15b6757b
FM
228 wxWidgets does not use templates (except for some advanced features that
229 are switched off by default) since it is a notoriously unportable feature.
36c9828f 230
15b6757b 231 @subsection rtti RTTI
36c9828f 232
15b6757b
FM
233 wxWidgets does not use C++ run-time type information since wxWidgets provides
234 its own run-time type information system, implemented using macros.
36c9828f 235
15b6757b 236 @subsection null Type of NULL
36c9828f 237
15b6757b
FM
238 Some compilers (e.g. the native IRIX cc) define NULL to be 0L so that
239 no conversion to pointers is allowed. Because of that, all these
36c9828f 240 occurrences of NULL in the GTK+ port use an explicit conversion such
15b6757b 241 as
36c9828f 242
15b6757b
FM
243 {\small
244 @verbatim
245 wxWindow *my_window = (wxWindow*) NULL;
246 @endverbatim
247 }%
36c9828f 248
15b6757b
FM
249 It is recommended to adhere to this in all code using wxWidgets as
250 this make the code (a bit) more portable.
36c9828f 251
15b6757b 252 @subsection precompiledheaders Precompiled headers
36c9828f 253
15b6757b
FM
254 Some compilers, such as Borland C++ and Microsoft C++, support
255 precompiled headers. This can save a great deal of compiling time. The
256 recommended approach is to precompile {\tt "wx.h"}, using this
257 precompiled header for compiling both wxWidgets itself and any
258 wxWidgets applications. For Windows compilers, two dummy source files
259 are provided (one for normal applications and one for creating DLLs)
260 to allow initial creation of the precompiled header.
36c9828f 261
15b6757b
FM
262 However, there are several downsides to using precompiled headers. One
263 is that to take advantage of the facility, you often need to include
264 more header files than would normally be the case. This means that
265 changing a header file will cause more recompilations (in the case of
266 wxWidgets, everything needs to be recompiled since everything includes {\tt "wx.h"}!)
36c9828f 267
15b6757b
FM
268 A related problem is that for compilers that don't have precompiled
269 headers, including a lot of header files slows down compilation
270 considerably. For this reason, you will find (in the common
271 X and Windows parts of the library) conditional
272 compilation that under Unix, includes a minimal set of headers;
273 and when using Visual C++, includes {\tt wx.h}. This should help provide
274 the optimal compilation for each compiler, although it is
275 biased towards the precompiled headers facility available
276 in Microsoft C++.
36c9828f 277
15b6757b
FM
278
279
280 @section filehandling File handling
36c9828f 281
15b6757b
FM
282 When building an application which may be used under different
283 environments, one difficulty is coping with documents which may be
284 moved to different directories on other machines. Saving a file which
285 has pointers to full pathnames is going to be inherently unportable.
36c9828f 286
15b6757b
FM
287 One approach is to store filenames on their own, with no directory
288 information. The application then searches into a list of standard
289 paths (platform-specific) through the use of \helpref{wxStandardPaths}{wxstandardpaths}.
36c9828f 290
15b6757b 291 Eventually you may want to use also the \helpref{wxPathList}{wxpathlist} class.
36c9828f 292
15b6757b
FM
293 Nowadays the limitations of DOS 8+3 filenames doesn't apply anymore.
294 Most modern operating systems allow at least 255 characters in the filename;
36c9828f 295 the exact maximum length, as well as the characters allowed in the filenames,
15b6757b
FM
296 are OS-specific so you should try to avoid extremely long (> 255 chars) filenames
297 and/or filenames with non-ANSI characters.
36c9828f 298
15b6757b
FM
299 Another thing you need to keep in mind is that all Windows operating systems
300 are case-insensitive, while Unix operating systems (Linux, Mac, etc) are
301 case-sensitive.
36c9828f 302
15b6757b
FM
303 Also, for text files, different OSes use different End Of Lines (EOL).
304 Windows uses CR+LF convention, Linux uses LF only, Mac CR only.
36c9828f 305
15b6757b
FM
306 The \helpref{wxTextFile}{wxtextfile},\helpref{wxTextInputStream}{wxtextinputstream},
307 \helpref{wxTextOutputStream}{wxtextoutputstream} classes help to abstract
308 from these differences.
309 Of course, there are also 3rd party utilities such as \tt{dos2unix} and \tt{unix2dos}
310 which do the EOL conversions.
36c9828f
FM
311
312 See also the \helpref{File Functions}{filefunctions} section of the reference
15b6757b 313 manual for the description of miscellaneous file handling functions.
36c9828f 314
15b6757b
FM
315*/
316
317/*!
318
319 @page utilities_page Utilities and libraries supplied with wxWidgets
36c9828f 320
15b6757b
FM
321 In addition to the \helpref{wxWidgets libraries}{librarieslist}, some
322 additional utilities are supplied in the \tt{utils} hierarchy.
36c9828f 323
15b6757b
FM
324 For other user-contributed packages, please see the Contributions page
325 on the \urlref{wxWidgets Web site}{http://www.wxwidgets.org}.
36c9828f 326
15b6757b
FM
327 \begin{description}\itemsep=0pt
328 \item[{\bf Helpview}]
329 Helpview is a program for displaying wxWidgets HTML
330 Help files. In many cases, you may wish to use the wxWidgets HTML
331 Help classes from within your application, but this provides a
332 handy stand-alone viewer. See \helpref{wxHTML Notes}{wxhtml} for more details.
333 You can find it in {\tt samples/html/helpview}.
334 \item[{\bf Tex2RTF}]
335 Supplied with wxWidgets is a utility called Tex2RTF for converting\rtfsp
336 \LaTeX\ manuals HTML, MS HTML Help, wxHTML Help, RTF, and Windows
337 Help RTF formats. Tex2RTF is used for the wxWidgets manuals and can be used independently
338 by authors wishing to create on-line and printed manuals from the same\rtfsp
339 \LaTeX\ source. Please see the separate documentation for Tex2RTF.
340 You can find it under {\tt utils/tex2rtf}.
341 \item[{\bf Helpgen}]
342 Helpgen takes C++ header files and generates a Tex2RTF-compatible
343 documentation file for each class it finds, using comments as appropriate.
344 This is a good way to start a reference for a set of classes.
345 Helpgen can be found in {\tt utils/HelpGen}.
346 \item[{\bf Emulator}]
347 Xnest-based display emulator for X11-based PDA applications. On some
348 systems, the Xnest window does not synchronise with the
349 'skin' window. This program can be found in {\tt utils/emulator}.
350 \end{description}
351
352*/
353
354/*!
355
356 @page strategies_page Programming strategies
36c9828f 357
15b6757b
FM
358 This chapter is intended to list strategies that may be useful when
359 writing and debugging wxWidgets programs. If you have any good tips,
360 please submit them for inclusion here.
361
362 @li @ref reducingerrors
363 @li @ref cpp
364 @li @ref portability
365 @li @ref debugstrategies
366
367 <hr>
368
369 @section reducingerrors Strategies for reducing programming errors
36c9828f 370
15b6757b 371 @subsection useassert Use ASSERT
36c9828f
FM
372
373 It is good practice to use ASSERT statements liberally, that check for conditions
15b6757b 374 that should or should not hold, and print out appropriate error messages.
36c9828f 375
15b6757b
FM
376 These can be compiled out of a non-debugging version of wxWidgets
377 and your application. Using ASSERT is an example of `defensive programming':
378 it can alert you to problems later on.
36c9828f 379
15b6757b 380 See \helpref{wxASSERT}{wxassert} for more info.
36c9828f 381
15b6757b 382 @subsection usewxstring Use wxString in preference to character arrays
36c9828f 383
15b6757b 384 Using \helpref{wxString}{wxstring} can be much safer and more convenient than using wxChar *.
36c9828f 385
15b6757b
FM
386 You can reduce the possibility of memory leaks substantially, and it is much more
387 convenient to use the overloaded operators than functions such as \tt{strcmp}.
36c9828f 388 wxString won't add a significant overhead to your program; the overhead is compensated
15b6757b 389 for by easier manipulation (which means less code).
36c9828f 390
15b6757b 391 The same goes for other data types: use classes wherever possible.
36c9828f 392
15b6757b
FM
393
394
395 @section portability Strategies for portability
36c9828f 396
15b6757b 397 @subsection usesizers Use sizers
36c9828f 398
15b6757b
FM
399 Don't use absolute panel item positioning if you can avoid it. Different GUIs have
400 very differently sized panel items. Consider using the \helpref{sizers}{sizeroverview} instead.
36c9828f 401
15b6757b 402 @subsection useresources Use wxWidgets resource files
36c9828f 403
15b6757b
FM
404 Use .xrc (wxWidgets resource files) where possible, because they can be easily changed
405 independently of source code. See the \helpref{XRC overview}{xrcoverview} for more info.
36c9828f 406
15b6757b
FM
407
408
409 @section debugstrategies Strategies for debugging
36c9828f 410
15b6757b 411 @subsection positivethinking Positive thinking
36c9828f 412
15b6757b
FM
413 It is common to blow up the problem in one's imagination, so that it seems to threaten
414 weeks, months or even years of work. The problem you face may seem insurmountable:
415 but almost never is. Once you have been programming for some time, you will be able
416 to remember similar incidents that threw you into the depths of despair. But
417 remember, you always solved the problem, somehow!
36c9828f 418
15b6757b
FM
419 Perseverance is often the key, even though a seemingly trivial problem
420 can take an apparently inordinate amount of time to solve. In the end,
421 you will probably wonder why you worried so much. That's not to say it
422 isn't painful at the time. Try not to worry -- there are many more important
423 things in life.
36c9828f 424
15b6757b 425 @subsection simplifyproblem Simplify the problem
36c9828f 426
15b6757b
FM
427 Reduce the code exhibiting the problem to the smallest program possible
428 that exhibits the problem. If it is not possible to reduce a large and
429 complex program to a very small program, then try to ensure your code
430 doesn't hide the problem (you may have attempted to minimize the problem
431 in some way: but now you want to expose it).
36c9828f 432
15b6757b
FM
433 With luck, you can add a small amount of code that causes the program
434 to go from functioning to non-functioning state. This should give a clue
435 to the problem. In some cases though, such as memory leaks or wrong
436 deallocation, this can still give totally spurious results!
36c9828f 437
15b6757b 438 @subsection usedebugger Use a debugger
36c9828f 439
15b6757b
FM
440 This sounds like facetious advice, but it is surprising how often people
441 don't use a debugger. Often it is an overhead to install or learn how to
442 use a debugger, but it really is essential for anything but the most
443 trivial programs.
36c9828f 444
15b6757b 445 @subsection uselogging Use logging functions
36c9828f 446
15b6757b
FM
447 There is a variety of logging functions that you can use in your program:
448 see \helpref{Logging functions}{logfunctions}.
36c9828f 449
15b6757b
FM
450 Using tracing statements may be more convenient than using the debugger
451 in some circumstances (such as when your debugger doesn't support a lot
452 of debugging code, or you wish to print a bunch of variables).
36c9828f 453
15b6757b 454 @subsection usedebuggingfacilities Use the wxWidgets debugging facilities
36c9828f 455
15b6757b
FM
456 You can use \helpref{wxDebugContext}{wxdebugcontext} to check for
457 memory leaks and corrupt memory: in fact in debugging mode, wxWidgets will
458 automatically check for memory leaks at the end of the program if wxWidgets is suitably
459 configured. Depending on the operating system and compiler, more or less
460 specific information about the problem will be logged.
36c9828f 461
15b6757b
FM
462 You should also use \helpref{debug macros}{debugmacros} as part of a `defensive programming' strategy,
463 scattering wxASSERTs liberally to test for problems in your code as early as possible. Forward thinking
464 will save a surprising amount of time in the long run.
36c9828f 465
15b6757b
FM
466 See the \helpref{debugging overview}{debuggingoverview} for further information.
467
468*/