Restored the old Classes by Category page to live side-by-side with the full Classes...
[wxWidgets.git] / docs / doxygen / mainpages / 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 @page page_multiplatform General Cross-Platform Development Tips
12
13 This chapter describes some tips related to cross-platform development.
14
15 @li @ref page_multiplatform_includefiles
16 @li @ref page_multiplatform_libraries
17 @li @ref page_multiplatform_configuration
18 @li @ref page_multiplatform_makefiles
19 @li @ref page_multiplatform_winresources
20 @li @ref page_multiplatform_allocatingobjects
21 @li @ref page_multiplatform_architecturedependency
22 @li @ref page_multiplatform_conditionalcompilation
23 @li @ref page_multiplatform_cpp
24 @li @ref page_multiplatform_filehandling
25 @li @ref page_multiplatform_reducingerr
26 @li @ref page_multiplatform_gui
27 @li @ref page_multiplatform_debug
28
29
30 <hr>
31
32
33 @section page_multiplatform_includefiles Include Files
34
35 The main include file is @c "wx/wx.h"; this includes the most commonly used
36 modules of wxWidgets.
37
38 To save on compilation time, include only those header files relevant to the
39 source file. If you are using @b precompiled headers, you should include the
40 following section before any other includes:
41
42 @verbatim
43 // For compilers that support precompilation, includes "wx.h".
44 #include <wx/wxprec.h>
45
46 #ifdef __BORLANDC__
47 # pragma hdrstop
48 #endif
49
50 #ifndef WX_PRECOMP
51 // Include your minimal set of headers here, or wx.h
52 # include <wx/wx.h>
53 #endif
54
55 ... now your other include files ...
56 @endverbatim
57
58 The file @c "wx/wxprec.h" includes @c "wx/wx.h". Although this incantation may
59 seem quirky, it is in fact the end result of a lot of experimentation, and
60 several Windows compilers to use precompilation which is largely automatic for
61 compilers with necessary support. Currently it is used for Visual C++
62 (including embedded Visual C++), Borland C++, Open Watcom C++, Digital Mars C++
63 and newer versions of GCC. Some compilers might need extra work from the
64 application developer to set the build environment up as necessary for the
65 support.
66
67
68
69 @section page_multiplatform_libraries Libraries
70
71 All ports of wxWidgets can create either a @b static library or a @b shared
72 library.
73
74 When a program is linked against a @e static library, the machine code from the
75 object files for any external functions used by the program is copied from the
76 library into the final executable.
77
78 @e Shared libraries are handled with a more advanced form of linking, which
79 makes the executable file smaller. They use the extension @c ".so" (Shared
80 Object) under Linux and @c ".dll" (Dynamic Link Library) under Windows.
81
82 An executable file linked against a shared library contains only a small table
83 of the functions it requires, instead of the complete machine code from the
84 object files for the external functions. Before the executable file starts
85 running, the machine code for the external functions is copied into memory from
86 the shared library file on disk by the operating system - a process referred to
87 as @e dynamic linking.
88
89 Dynamic linking makes executable files smaller and saves disk space, because
90 one copy of a library can be shared between multiple programs. Most operating
91 systems also provide a virtual memory mechanism which allows one copy of a
92 shared library in physical memory to be used by all running programs, saving
93 memory as well as disk space.
94
95 Furthermore, shared libraries make it possible to update a library without
96 recompiling the programs which use it (provided the interface to the library
97 does not change).
98
99 wxWidgets can also be built in @b multilib and @b monolithic variants. See the
100 @ref page_libs for more information on these.
101
102
103
104 @section page_multiplatform_configuration Configuration
105
106 When using project files and makefiles directly to build wxWidgets, options are
107 configurable in the file @c "wx/XXX/setup.h" where XXX is the required
108 platform (such as @c msw, @c motif, @c gtk, @c mac).
109
110 Some settings are a matter of taste, some help with platform-specific problems,
111 and others can be set to minimize the size of the library. Please see the
112 @c "setup.h" file and @c "install.txt" files for details on configuration.
113
114 When using the @c "configure" script to configure wxWidgets (on Unix and other
115 platforms where configure is available), the corresponding @c "setup.h" files
116 are generated automatically along with suitable makefiles.
117
118 When using the RPM packages (or DEB or other forms of @e binaries) for
119 installing wxWidgets on Linux, a correct @c "setup.h" is shipped in the package
120 and this must not be changed.
121
122
123
124 @section page_multiplatform_makefiles Makefiles
125
126 On Microsoft Windows, wxWidgets has a different set of makefiles for each
127 compiler, because each compiler's @c 'make' tool is slightly different. Popular
128 Windows compilers that we cater for, and the corresponding makefile extensions,
129 include: Microsoft Visual C++ (.vc), Borland C++ (.bcc), OpenWatcom C++ (.wat)
130 and MinGW/Cygwin (.gcc). Makefiles are provided for the wxWidgets library
131 itself, samples, demos, and utilities.
132
133 On Linux, Mac and OS/2, you use the @c 'configure' command to generate the
134 necessary makefiles. You should also use this method when building with
135 MinGW/Cygwin on Windows.
136
137 We also provide project files for some compilers, such as Microsoft VC++.
138 However, we recommend using makefiles to build the wxWidgets library itself,
139 because makefiles can be more powerful and less manual intervention is
140 required.
141
142 On Windows using a compiler other than MinGW/Cygwin, you would build the
143 wxWidgets library from the @c "build/msw" directory which contains the relevant
144 makefiles.
145
146 On Windows using MinGW/Cygwin, and on Unix, MacOS X and OS/2, you invoke
147 'configure' (found in the top-level of the wxWidgets source hierarchy), from
148 within a suitable empty directory for containing makefiles, object files and
149 libraries.
150
151 For details on using makefiles, configure, and project files, please see
152 @c "docs/xxx/install.txt" in your distribution, where @c "xxx" is the platform
153 of interest, such as @c msw, @c gtk, @c x11, @c mac.
154
155 All wxWidgets makefiles are generated using
156 @link http://www.bakefile.org Bakefile @endlink. wxWidgets also provides (in
157 the @c "build/bakefiles/wxpresets" folder) the wxWidgets bakefile presets.
158 These files allow you to create bakefiles for your own wxWidgets-based
159 applications very easily.
160
161
162
163 @section page_multiplatform_winresources Windows Resource Files
164
165 wxWidgets application compilation under MS Windows requires at least one extra
166 file: a resource file.
167
168 The least that must be defined in the Windows resource file (extension RC) is
169 the following statement:
170
171 @verbatim
172 #include "wx/msw/wx.rc"
173 @endverbatim
174
175 which includes essential internal wxWidgets definitions. The resource script
176 may also contain references to icons, cursors, etc., for example:
177
178 @verbatim
179 wxicon icon wx.ico
180 @endverbatim
181
182 The icon can then be referenced by name when creating a frame icon. See the
183 Microsoft Windows SDK documentation.
184
185 @note Include "wx.rc" @e after any ICON statements so programs that search your
186 executable for icons (such as the Program Manager) find your application
187 icon first.
188
189
190
191 @section page_multiplatform_allocatingobjects Allocating and Deleting wxWidgets Objects
192
193 In general, classes derived from wxWindow must dynamically allocated with
194 @e new and deleted with @e delete. If you delete a window, all of its children
195 and descendants will be automatically deleted, so you don't need to delete
196 these descendants explicitly.
197
198 When deleting a frame or dialog, use @b Destroy rather than @b delete so that
199 the wxWidgets delayed deletion can take effect. This waits until idle time
200 (when all messages have been processed) to actually delete the window, to avoid
201 problems associated with the GUI sending events to deleted windows.
202
203 Don't create a window on the stack, because this will interfere with delayed
204 deletion.
205
206 If you decide to allocate a C++ array of objects (such as wxBitmap) that may be
207 cleaned up by wxWidgets, make sure you delete the array explicitly before
208 wxWidgets has a chance to do so on exit, since calling @e delete on array
209 members will cause memory problems.
210
211 wxColour can be created statically: it is not automatically cleaned
212 up and is unlikely to be shared between other objects; it is lightweight
213 enough for copies to be made.
214
215 Beware of deleting objects such as a wxPen or wxBitmap if they are still in
216 use. Windows is particularly sensitive to this, so make sure you make calls
217 like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before
218 deleting a drawing object that may be in use. Code that doesn't do this will
219 probably work fine on some platforms, and then fail under Windows.
220
221
222
223 @section page_multiplatform_architecturedependency Architecture Dependency
224
225 A problem which sometimes arises from writing multi-platform programs is that
226 the basic C types are not defined the same on all platforms. This holds true
227 for both the length in bits of the standard types (such as int and long) as
228 well as their byte order, which might be little endian (typically on Intel
229 computers) or big endian (typically on some Unix workstations). wxWidgets
230 defines types and macros that make it easy to write architecture independent
231 code. The types are:
232
233 wxInt32, wxInt16, wxInt8, wxUint32, wxUint16 = wxWord, wxUint8 = wxByte
234
235 where wxInt32 stands for a 32-bit signed integer type etc. You can also check
236 which architecture the program is compiled on using the wxBYTE_ORDER define
237 which is either wxBIG_ENDIAN or wxLITTLE_ENDIAN (in the future maybe
238 wxPDP_ENDIAN as well).
239
240 The macros handling bit-swapping with respect to the applications endianness
241 are described in the @ref group_funcmacro_byteorder section.
242
243
244
245 @section page_multiplatform_conditionalcompilation Conditional Compilation
246
247 One of the purposes of wxWidgets is to reduce the need for conditional
248 compilation in source code, which can be messy and confusing to follow.
249 However, sometimes it is necessary to incorporate platform-specific features
250 (such as metafile use under MS Windows). The @ref page_wxusedef symbols listed
251 in the file @c setup.h may be used for this purpose, along with any
252 user-supplied ones.
253
254
255
256 @section page_multiplatform_cpp C++ Issues
257
258 The following documents some miscellaneous C++ issues.
259
260 @subsection page_multiplatform_cpp_templates Templates
261
262 wxWidgets does not use templates (except for some advanced features that are
263 switched off by default) since it is a notoriously unportable feature.
264
265 @subsection page_multiplatform_cpp_rtti Runtime Type Information (RTTI)
266
267 wxWidgets does not use C++ run-time type information since wxWidgets provides
268 its own run-time type information system, implemented using macros.
269
270 @subsection page_multiplatform_cpp_null Type of NULL
271
272 Some compilers (e.g. the native IRIX cc) define @NULL to be 0L so that no
273 conversion to pointers is allowed. Because of that, all these occurrences of
274 @NULL in the GTK+ port use an explicit conversion such as
275
276 @code
277 wxWindow *my_window = (wxWindow*) NULL;
278 @endcode
279
280 It is recommended to adhere to this in all code using wxWidgets as this make
281 the code (a bit) more portable.
282
283 @subsection page_multiplatform_cpp_precompiledheaders Precompiled Headers
284
285 Some compilers, such as Borland C++ and Microsoft C++, support precompiled
286 headers. This can save a great deal of compiling time. The recommended approach
287 is to precompile @c "wx.h", using this precompiled header for compiling both
288 wxWidgets itself and any wxWidgets applications. For Windows compilers, two
289 dummy source files are provided (one for normal applications and one for
290 creating DLLs) to allow initial creation of the precompiled header.
291
292 However, there are several downsides to using precompiled headers. One is that
293 to take advantage of the facility, you often need to include more header files
294 than would normally be the case. This means that changing a header file will
295 cause more recompilations (in the case of wxWidgets, everything needs to be
296 recompiled since everything includes @c "wx.h").
297
298 A related problem is that for compilers that don't have precompiled headers,
299 including a lot of header files slows down compilation considerably. For this
300 reason, you will find (in the common X and Windows parts of the library)
301 conditional compilation that under Unix, includes a minimal set of headers; and
302 when using Visual C++, includes @c "wx.h". This should help provide the optimal
303 compilation for each compiler, although it is biased towards the precompiled
304 headers facility available in Microsoft C++.
305
306
307
308 @section page_multiplatform_filehandling File Handling
309
310 When building an application which may be used under different environments,
311 one difficulty is coping with documents which may be moved to different
312 directories on other machines. Saving a file which has pointers to full
313 pathnames is going to be inherently unportable.
314
315 One approach is to store filenames on their own, with no directory information.
316 The application then searches into a list of standard paths (platform-specific)
317 through the use of wxStandardPaths.
318
319 Eventually you may want to use also the wxPathList class.
320
321 Nowadays the limitations of DOS 8+3 filenames doesn't apply anymore. Most
322 modern operating systems allow at least 255 characters in the filename; the
323 exact maximum length, as well as the characters allowed in the filenames, are
324 OS-specific so you should try to avoid extremely long (> 255 chars) filenames
325 and/or filenames with non-ANSI characters.
326
327 Another thing you need to keep in mind is that all Windows operating systems
328 are case-insensitive, while Unix operating systems (Linux, Mac, etc) are
329 case-sensitive.
330
331 Also, for text files, different OSes use different End Of Lines (EOL). Windows
332 uses CR+LF convention, Linux uses LF only, Mac CR only.
333
334 The wxTextFile, wxTextInputStream, wxTextOutputStream classes help to abstract
335 from these differences. Of course, there are also 3rd party utilities such as
336 @c dos2unix and @c unix2dos which do the EOL conversions.
337
338 See also the @ref group_funcmacro_file section of the reference manual for the
339 description of miscellaneous file handling functions.
340
341
342
343 @section page_multiplatform_reducingerr Reducing Programming Errors
344
345 @subsection page_multiplatform_reducingerr_useassert Use ASSERT
346
347 It is good practice to use ASSERT statements liberally, that check for
348 conditions that should or should not hold, and print out appropriate error
349 messages.
350
351 These can be compiled out of a non-debugging version of wxWidgets and your
352 application. Using ASSERT is an example of `defensive programming': it can
353 alert you to problems later on.
354
355 See wxASSERT() for more info.
356
357 @subsection page_multiplatform_reducingerr_usewxstring Use wxString in Preference to Character Arrays
358
359 Using wxString can be much safer and more convenient than using @c wxChar*.
360
361 You can reduce the possibility of memory leaks substantially, and it is much
362 more convenient to use the overloaded operators than functions such as
363 @c strcmp. wxString won't add a significant overhead to your program; the
364 overhead is compensated for by easier manipulation (which means less code).
365
366 The same goes for other data types: use classes wherever possible.
367
368
369
370 @section page_multiplatform_gui GUI Design
371
372 @li <b>Use Sizers:</b> Don't use absolute panel item positioning if you can
373 avoid it. Every platform's native controls have very different sizes.
374 Consider using the @ref overview_sizer instead.
375 @li <b>Use wxWidgets Resource Files:</b> Use @c XRC (wxWidgets resource files)
376 where possible, because they can be easily changed independently of source
377 code. See the @ref overview_xrc for more info.
378
379
380
381 @section page_multiplatform_debug Debugging
382
383 @subsection page_multiplatform_debug_positivethinking Positive Thinking
384
385 It is common to blow up the problem in one's imagination, so that it seems to
386 threaten weeks, months or even years of work. The problem you face may seem
387 insurmountable: but almost never is. Once you have been programming for some
388 time, you will be able to remember similar incidents that threw you into the
389 depths of despair. But remember, you always solved the problem, somehow!
390
391 Perseverance is often the key, even though a seemingly trivial problem can take
392 an apparently inordinate amount of time to solve. In the end, you will probably
393 wonder why you worried so much. That's not to say it isn't painful at the time.
394 Try not to worry -- there are many more important things in life.
395
396 @subsection page_multiplatform_debug_simplifyproblem Simplify the Problem
397
398 Reduce the code exhibiting the problem to the smallest program possible that
399 exhibits the problem. If it is not possible to reduce a large and complex
400 program to a very small program, then try to ensure your code doesn't hide the
401 problem (you may have attempted to minimize the problem in some way: but now
402 you want to expose it).
403
404 With luck, you can add a small amount of code that causes the program to go
405 from functioning to non-functioning state. This should give a clue to the
406 problem. In some cases though, such as memory leaks or wrong deallocation, this
407 can still give totally spurious results!
408
409 @subsection page_multiplatform_debug_usedebugger Use a Debugger
410
411 This sounds like facetious advice, but it is surprising how often people don't
412 use a debugger. Often it is an overhead to install or learn how to use a
413 debugger, but it really is essential for anything but the most trivial
414 programs.
415
416 @subsection page_multiplatform_debug_uselogging Use Logging Functions
417
418 There is a variety of logging functions that you can use in your program: see
419 @ref group_funcmacro_log.
420
421 Using tracing statements may be more convenient than using the debugger in some
422 circumstances (such as when your debugger doesn't support a lot of debugging
423 code, or you wish to print a bunch of variables).
424
425 @subsection page_multiplatform_debug_usedebuggingfacilities Use the wxWidgets Debugging Facilities
426
427 You can use wxDebugContext to check for memory leaks and corrupt memory: in
428 fact in debugging mode, wxWidgets will automatically check for memory leaks at
429 the end of the program if wxWidgets is suitably configured. Depending on the
430 operating system and compiler, more or less specific information about the
431 problem will be logged.
432
433 You should also use @ref group_funcmacro_debugging as part of a "defensive
434 programming" strategy, scattering wxASSERT()s liberally to test for problems in
435 your code as early as possible. Forward thinking will save a surprising amount
436 of time in the long run.
437
438 See the @ref overview_debugging for further information.
439
440 */
441