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