]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/devtips.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Cross-platform development page of the Doxygen manual
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 @page multiplatform_page Multi-platform development with wxWidgets
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.
20 @li @ref configuration
23 @li @ref allocatingobjects
24 @li @ref architecturedependency
25 @li @ref conditionalcompilation
32 @section includefiles Include files
34 The main include file is {\tt "wx/wx.h"}; this includes the most commonly
35 used modules of wxWidgets.
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:
42 // For compilers that support precompilation, includes "wx.h".
43 #include <wx/wxprec.h>
50 // Include your minimal set of headers here, or wx.h
54 ... now your other include files ...
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.
68 @section libraries Libraries
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
77 @section configuration Configuration
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.
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.
94 @section makefiles Makefiles
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.
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.
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.
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.
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
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.
127 @section windowsfiles Windows-specific files
129 wxWidgets application compilation under MS Windows requires at least one
130 extra file: a resource file.
132 @subsection resources Resource file
134 The least that must be defined in the Windows resource file (extension RC)
135 is the following statement:
138 #include "wx/msw/wx.rc"
141 which includes essential internal wxWidgets definitions. The resource script
142 may also contain references to icons, cursors, etc., for example:
148 The icon can then be referenced by name when creating a frame icon. See
149 the MS Windows SDK documentation.
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.}
157 @section allocatingobjects Allocating and deleting wxWidgets objects
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.
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.
169 Don't create a window on the stack, because this will interfere
170 with delayed deletion.
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.
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.
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.
189 @section architecturedependency Architecture dependency
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
199 wxInt32, wxInt16, wxInt8, wxUint32, wxUint16 = wxWord, wxUint8 = wxByte
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
206 The macros handling bit-swapping with respect to the applications endianness
207 are described in the \helpref{Byte order macros}{byteordermacros} section.
211 @section conditionalcompilation Conditional compilation
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.
222 @section cpp C++ issues
224 The following documents some miscellaneous C++ issues.
226 @subsection templates Templates
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.
231 @subsection rtti RTTI
233 wxWidgets does not use C++ run-time type information since wxWidgets provides
234 its own run-time type information system, implemented using macros.
236 @subsection null Type of NULL
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
245 wxWindow *my_window = (wxWindow*) NULL;
249 It is recommended to adhere to this in all code using wxWidgets as
250 this make the code (a bit) more portable.
252 @subsection precompiledheaders Precompiled headers
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.
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"}!)
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
280 @section filehandling File handling
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.
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}.
291 Eventually you may want to use also the \helpref{wxPathList}{wxpathlist} class.
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.
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
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.
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.
312 See also the \helpref{File Functions}{filefunctions} section of the reference
313 manual for the description of miscellaneous file handling functions.
319 @page utilities_page Utilities and libraries supplied with wxWidgets
321 In addition to the \helpref{wxWidgets libraries}{librarieslist}, some
322 additional utilities are supplied in the \tt{utils} hierarchy.
324 For other user-contributed packages, please see the Contributions page
325 on the \urlref{wxWidgets Web site}{http://www.wxwidgets.org}.
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}.
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}.
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}.
356 @page strategies_page Programming strategies
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.
362 @li @ref reducingerrors
365 @li @ref debugstrategies
369 @section reducingerrors Strategies for reducing programming errors
371 @subsection useassert Use ASSERT
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.
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.
380 See \helpref{wxASSERT}{wxassert} for more info.
382 @subsection usewxstring Use wxString in preference to character arrays
384 Using \helpref{wxString}{wxstring} can be much safer and more convenient than using wxChar *.
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).
391 The same goes for other data types: use classes wherever possible.
395 @section portability Strategies for portability
397 @subsection usesizers Use sizers
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.
402 @subsection useresources Use wxWidgets resource files
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.
409 @section debugstrategies Strategies for debugging
411 @subsection positivethinking Positive thinking
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!
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
425 @subsection simplifyproblem Simplify the problem
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).
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!
438 @subsection usedebugger Use a debugger
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
445 @subsection uselogging Use logging functions
447 There is a variety of logging functions that you can use in your program:
448 see \helpref{Logging functions}{logfunctions}.
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).
454 @subsection usedebuggingfacilities Use the wxWidgets debugging facilities
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.
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.
466 See the \helpref{debugging overview}{debuggingoverview} for further information.