]> git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/devtips.h
added initial version of the Doxygen manual
[wxWidgets.git] / docs / doxygen / devtips.h
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
31
32 @section includefiles Include files
33
34 The main include file is {\tt "wx/wx.h"}; this includes the most commonly
35 used modules of wxWidgets.
36
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:
40
41 @verbatim
42 // For compilers that support precompilation, includes "wx.h".
43 #include <wx/wxprec.h>
44
45 #ifdef __BORLANDC__
46 #pragma hdrstop
47 #endif
48
49 #ifndef WX_PRECOMP
50 // Include your minimal set of headers here, or wx.h
51 #include <wx/wx.h>
52 #endif
53
54 ... now your other include files ...
55 @endverbatim
56
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.
65
66
67
68 @section libraries Libraries
69
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.
74
75
76
77 @section configuration Configuration
78
79 When using project files and makefiles directly to build wxWidgets,
80 options are configurable in the file
81 \rtfsp{\tt "wx/XXX/setup.h"} where XXX is the required platform (such as msw, motif, gtk, mac). Some
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.
85
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.
91
92
93
94 @section makefiles Makefiles
95
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.
102
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.
106
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.
111
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.
115
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.
120
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.
124
125
126
127 @section windowsfiles Windows-specific files
128
129 wxWidgets application compilation under MS Windows requires at least one
130 extra file: a resource file.
131
132 @subsection resources Resource file
133
134 The least that must be defined in the Windows resource file (extension RC)
135 is the following statement:
136
137 @verbatim
138 #include "wx/msw/wx.rc"
139 @endverbatim
140
141 which includes essential internal wxWidgets definitions. The resource script
142 may also contain references to icons, cursors, etc., for example:
143
144 @verbatim
145 wxicon icon wx.ico
146 @endverbatim
147
148 The icon can then be referenced by name when creating a frame icon. See
149 the MS Windows SDK documentation.
150
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.}
154
155
156
157 @section allocatingobjects Allocating and deleting wxWidgets objects
158
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.
163
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.
168
169 Don't create a window on the stack, because this will interfere
170 with delayed deletion.
171
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.
176
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.
180
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.
186
187
188
189 @section architecturedependency Architecture dependency
190
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
193 for both the length in bits of the standard types (such as int and long) as
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:
198
199 wxInt32, wxInt16, wxInt8, wxUint32, wxUint16 = wxWord, wxUint8 = wxByte
200
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).
205
206 The macros handling bit-swapping with respect to the applications endianness
207 are described in the \helpref{Byte order macros}{byteordermacros} section.
208
209
210
211 @section conditionalcompilation Conditional compilation
212
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
216 features (such as metafile use under MS Windows). The \helpref{wxUSE\_*}{wxusedef}
217 symbols listed in the file {\tt setup.h} may be used for this purpose,
218 along with any user-supplied ones.
219
220
221
222 @section cpp C++ issues
223
224 The following documents some miscellaneous C++ issues.
225
226 @subsection templates Templates
227
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.
230
231 @subsection rtti RTTI
232
233 wxWidgets does not use C++ run-time type information since wxWidgets provides
234 its own run-time type information system, implemented using macros.
235
236 @subsection null Type of NULL
237
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
240 occurrences of NULL in the GTK+ port use an explicit conversion such
241 as
242
243 {\small
244 @verbatim
245 wxWindow *my_window = (wxWindow*) NULL;
246 @endverbatim
247 }%
248
249 It is recommended to adhere to this in all code using wxWidgets as
250 this make the code (a bit) more portable.
251
252 @subsection precompiledheaders Precompiled headers
253
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.
261
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"}!)
267
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++.
277
278
279
280 @section filehandling File handling
281
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.
286
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}.
290
291 Eventually you may want to use also the \helpref{wxPathList}{wxpathlist} class.
292
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;
295 the exact maximum length, as well as the characters allowed in the filenames,
296 are OS-specific so you should try to avoid extremely long (> 255 chars) filenames
297 and/or filenames with non-ANSI characters.
298
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.
302
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.
305
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.
311
312 See also the \helpref{File Functions}{filefunctions} section of the reference
313 manual for the description of miscellaneous file handling functions.
314
315 */
316
317 /*!
318
319 @page utilities_page Utilities and libraries supplied with wxWidgets
320
321 In addition to the \helpref{wxWidgets libraries}{librarieslist}, some
322 additional utilities are supplied in the \tt{utils} hierarchy.
323
324 For other user-contributed packages, please see the Contributions page
325 on the \urlref{wxWidgets Web site}{http://www.wxwidgets.org}.
326
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
357
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
370
371 @subsection useassert Use ASSERT
372
373 It is good practice to use ASSERT statements liberally, that check for conditions
374 that should or should not hold, and print out appropriate error messages.
375
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.
379
380 See \helpref{wxASSERT}{wxassert} for more info.
381
382 @subsection usewxstring Use wxString in preference to character arrays
383
384 Using \helpref{wxString}{wxstring} can be much safer and more convenient than using wxChar *.
385
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}.
388 wxString won't add a significant overhead to your program; the overhead is compensated
389 for by easier manipulation (which means less code).
390
391 The same goes for other data types: use classes wherever possible.
392
393
394
395 @section portability Strategies for portability
396
397 @subsection usesizers Use sizers
398
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.
401
402 @subsection useresources Use wxWidgets resource files
403
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.
406
407
408
409 @section debugstrategies Strategies for debugging
410
411 @subsection positivethinking Positive thinking
412
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!
418
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.
424
425 @subsection simplifyproblem Simplify the problem
426
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).
432
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!
437
438 @subsection usedebugger Use a debugger
439
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.
444
445 @subsection uselogging Use logging functions
446
447 There is a variety of logging functions that you can use in your program:
448 see \helpref{Logging functions}{logfunctions}.
449
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).
453
454 @subsection usedebuggingfacilities Use the wxWidgets debugging facilities
455
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.
461
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.
465
466 See the \helpref{debugging overview}{debuggingoverview} for further information.
467
468 */