]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/porting/porting.tex
Added wizard sample.
[wxWidgets.git] / docs / latex / porting / porting.tex
1 \documentstyle[a4,makeidx,verbatim,texhelp,fancyhea,mysober,mytitle]{report}
2 \newcommand{\indexit}[1]{#1\index{#1}}%
3 \newcommand{\pipe}[0]{$\|$\ }%
4 \definecolour{black}{0}{0}{0}%
5 \definecolour{cyan}{0}{255}{255}%
6 \definecolour{green}{0}{255}{0}%
7 \definecolour{magenta}{255}{0}{255}%
8 \definecolour{red}{255}{0}{0}%
9 \definecolour{blue}{0}{0}{200}%
10 \definecolour{yellow}{255}{255}{0}%
11 \definecolour{white}{255}{255}{255}%
12 \input psbox.tex
13 \parskip=10pt
14 \parindent=0pt
15 \title{Guide to porting applications from wxWindows 1.xx to 2.0}
16 \author{Julian Smart}
17 \date{March 1999}
18 \makeindex
19 \begin{document}
20 \maketitle
21 \pagestyle{fancyplain}
22 \bibliographystyle{plain}
23 \setheader{{\it CONTENTS}}{}{}{}{}{{\it CONTENTS}}
24 \setfooter{\thepage}{}{}{}{}{\thepage}%
25 \pagenumbering{roman}
26 \tableofcontents
27 %
28 \chapter{About this document}\label{about}
29 \pagenumbering{arabic}%
30 \setheader{{\it Porting guide}}{}{}{}{}{{\it Porting guide}}%
31 \setfooter{\thepage}{}{}{}{}{\thepage}%
32
33 This document gives guidelines and tips for porting applications from
34 version 1.xx of wxWindows to version 2.0.
35
36 The first section offers tips for writing 1.xx applications in a way to
37 minimize porting time. The following sections detail the changes and
38 how you can modify your application to be 2.0-compliant.
39
40 You may be worrying that porting to 2.0 will be a lot of work,
41 particularly if you have only recently started using 1.xx. In fact,
42 the wxWindows 2.0 API has far more in common with 1.xx than it has differences.
43 The main challenges are using the new event system, doing without the default
44 panel item layout, and the lack of automatic labels in some controls.
45
46 Please don't be freaked out by the jump to 2.0! For one thing, 1.xx is still available
47 and will be supported by the user community for some time. And when you have
48 changed to 2.0, we hope that you will appreciate the benefits in terms
49 of greater flexibility, better user interface aesthetics, improved C++ conformance,
50 improved compilation speed, and many other enhancements. The revised architecture
51 of 2.0 will ensure that wxWindows can continue to evolve for the forseeable
52 future.
53
54 {\it Please note that this document is a work in progress.}
55
56 \chapter{Preparing for version 2.0}\label{preparing}
57
58 Even before compiling with version 2.0, there's also a lot you can do right now to make porting
59 relatively simple. Here are a few tips.
60
61 \begin{itemize}
62 \item {\bf Use constraints or .wxr resources} for layout, rather than the default layout scheme.
63 Constraints should be the same in 2.0, and resources will be translated.
64 \item {\bf Use separate wxMessage items} instead of labels for wxText, wxMultiText,
65 wxChoice, wxComboBox. These labels will disappear in 2.0. Use separate
66 wxMessages whether you're creating controls programmatically or using
67 the dialog editor. The future dialog editor will be able to translate
68 from old to new more accurately if labels are separated out.
69 \item {\bf Parameterise functions that use wxDC} or derivatives, i.e. make the wxDC
70 an argument to all functions that do drawing. Minimise the use of
71 wxWindow::GetDC and definitely don't store wxDCs long-term
72 because in 2.0, you can't use GetDC() and wxDCs are not persistent.
73 You will use wxClientDC, wxPaintDC stack objects instead. Minimising
74 the use of GetDC() will ensure that there are very few places you
75 have to change drawing code for 2.0.
76 \item {\bf Don't set GDI objects} (wxPen, wxBrush etc.) in windows or wxCanvasDCs before they're
77 needed (e.g. in constructors) - do so within your drawing routine instead. In
78 2.0, these settings will only take effect between the construction and destruction
79 of temporary wxClient/PaintDC objects.
80 \item {\bf Don't rely} on arguments to wxDC functions being floating point - they will
81 be 32-bit integers in 2.0.
82 \item {\bf Don't use the wxCanvas member functions} that duplicate wxDC functions, such as SetPen and DrawLine, since
83 they are going.
84 \item {\bf Using member callbacks} called from global callback functions will make the transition
85 easier - see the FAQ
86 for some notes on using member functions for callbacks. wxWindows 2.0 will banish global
87 callback functions (and OnMenuCommand), and nearly all event handling will be done by functions taking a single event argument.
88 So in future you will have code like:
89
90 {\small\begin{verbatim}
91 void MyFrame::OnOK(wxCommandEvent& event)
92 {
93 ...
94 }
95 \end{verbatim}
96 }%
97
98 You may find that writing the extra code to call a member function isn't worth it at this stage,
99 but the option is there.
100 \item {\bf Use wxString wherever possible.} 2.0 replaces char * with wxString
101 in most cases, and if you use wxString to receive strings returned from
102 wxWindows functions (except when you need to save the pointer if deallocation is required), there should
103 be no conversion problems later on.
104 \item Be aware that under Windows, {\bf font sizes will change} to match standard Windows
105 font sizes (for example, a 12-point font will appear bigger than before). Write your application
106 to be flexible where fonts are concerned.
107 Don't rely on fonts being similarly-sized across platforms, as they were (by chance) between
108 Windows and X under wxWindows 1.66. Yes, this is not easy... but I think it's better to conform to the
109 standards of each platform, and currently the size difference makes it difficult to
110 conform to Windows UI standards. You may eventually wish to build in a global 'fudge-factor' to compensate
111 for size differences. The old font sizing will still be available via wx\_setup.h, so do not panic...
112 \item {\bf Consider dropping wxForm usage}:
113 wxPropertyFormView can be used in a wxForm-like way, except that you specify a pre-constructed panel
114 or dialog; or you can use a wxPropertyListView to show attributes in a scrolling list - you don't even need
115 to lay panel items out.
116
117 Because wxForm uses a number of features to be dropped in wxWindows 2.0, it cannot be
118 supported in the future, at least in its present state.
119 \item {\bf When creating a wxListBox}, put the wxLB\_SINGLE, wxLB\_MULTIPLE, wxLB\_EXTENDED styles in the window style parameter, and put
120 zero in the {\it multiple} parameter. The {\it multiple} parameter will be removed in 2.0.
121 \item {\bf For MDI applications}, don't reply on MDI being run-time-switchable in the way that the
122 MDI sample is. In wxWindows 2.0, MDI functionality is separated into distinct classes.
123 \end{itemize}
124
125 \chapter{The new event system}\label{eventsystem}
126
127 The way that events are handled has been radically changed in wxWindows 2.0. Please
128 read the topic `Event handling overview' in the wxWindows 2.0 manual for background
129 on this.
130
131 \section{Callbacks}
132
133 Instead of callbacks for panel items, menu command events, control commands and other events are directed to
134 the originating window, or an ancestor, or an event handler that has been plugged into the window
135 or its ancestor. Event handlers always have one argument, a derivative of wxEvent.
136
137 For menubar commands, the {\bf OnMenuCommand} member function will be replaced by a series of separate member functions,
138 each of which responds to a particular command. You need to add these (non-virtual) functions to your
139 frame class, add a DECLARE\_EVENT\_TABLE entry to the class, and then add an event table to
140 your implementation file, as a BEGIN\_EVENT\_TABLE and END\_EVENT\_TABLE block. The
141 individual event mapping macros will be of the form:
142
143 \begin{verbatim}
144 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
145 EVT_MENU(MYAPP_NEW, MyFrame::OnNew)
146 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
147 END_EVENT_TABLE()
148 \end{verbatim}
149
150 Control commands, such as button commands, can be routed to a derived button class,
151 the parent window, or even the frame. Here, you use a function of the form EVT\_BUTTON(id, func).
152 Similar macros exist for other control commands.
153
154 \section{Other events}
155
156 To intercept other events, you used to override virtual functions, such as OnSize. Now, while you can use
157 the OnSize name for such event handlers (or any other name of your choice), it has only a single argument
158 (wxSizeEvent) and must again be `mapped' using the EVT\_SIZE macro. The same goes for all other events,
159 including OnClose (although in fact you can still use the old, virtual form of OnClose for the time being).
160
161 \chapter{Class hierarchy}\label{classhierarchy}
162
163 The class hierarchy has changed somewhat. wxToolBar and wxButtonBar
164 classes have been split into several classes, and are derived from wxControl (which was
165 called wxItem). wxPanel derives from wxWindow instead of from wxCanvas, which has
166 disappeared in favour of wxScrolledWindow (since all windows are now effectively canvases
167 which can be drawn into). The status bar has become a class in its own right, wxStatusBar.
168
169 There are new MDI classes so that wxFrame does not have to be overloaded with this
170 functionality.
171
172 There are new device context classes, with wxPanelDC and wxCanvasDC disappearing.
173 See \helpref{Device contexts and painting}{dc}.
174
175 \chapter{GDI objects}\label{gdiobjects}
176
177 These objects - instances of classes such as wxPen, wxBrush, wxBitmap (but not wxColour) -
178 are now implemented with reference-counting. This makes assignment a very cheap operation,
179 and also means that management of the resource is largely automatic. You now pass {\it references} to
180 objects to functions such as wxDC::SetPen, not pointers, so you will need to derefence your pointers.
181 The device context does not store a copy of the pen
182 itself, but takes a copy of it (via reference counting), and the object's data gets freed up
183 when the reference count goes to zero. The application does not have to worry so much about
184 who the object belongs to: it can pass the reference, then destroy the object without
185 leaving a dangling pointer inside the device context.
186
187 For the purposes of code migration, you can use the old style of object management - maintaining
188 pointers to GDI objects, and using the FindOrCreate... functions. However, it is preferable to
189 keep this explicit management to a minimum, instead creating objects on the fly as needed, on the stack,
190 unless this causes too much of an overhead in your application.
191
192 At a minimum, you will have to make sure that calls to SetPen, SetBrush etc. work. Also, where you pass NULL to these
193 functions, you will need to use an identifier such as wxNullPen or wxNullBrush.
194
195 \chapter{Dialogs and controls}\label{dialogscontrols}
196
197 \wxheading{Labels}
198
199 Most controls no longer have labels and values as they used to in 1.xx. Instead, labels
200 should be created separately using wxStaticText (the new name for wxMessage). This will
201 need some reworking of dialogs, unfortunately; programmatic dialog creation that doesn't
202 use constraints will be especially hard-hit. Perhaps take this opportunity to make more
203 use of dialog resources or constraints. Or consider using the wxPropertyListView class
204 which can do away with dialog layout issues altogether by presenting a list of editable
205 properties.
206
207 \wxheading{Constructors}
208
209 All window constructors have two main changes, apart from the label issue mentioned above.
210 Windows now have integer identifiers; and position and size are now passed as wxPoint and
211 wxSize objects. In addition, some windows have a wxValidator argument.
212
213 \wxheading{Show versus ShowModal}
214
215 If you have used or overridden the {\bf wxDialog::Show} function in the past, you may find
216 that modal dialogs no longer work as expected. This is because the function for modal showing
217 is now {\bf wxDialog:ShowModal}. This is part of a more fundamental change in which a
218 control may tell the dialog that it caused the dismissal of a dialog, by
219 calling {\bf wxDialog::EndModal} or {\bf wxWindow::SetReturnCode}. Using this
220 information, {\bf ShowModal} now returns the id of the control that caused dismissal,
221 giving greater feedback to the application than just TRUE or FALSE.
222
223 If you overrode or called {\bf wxDialog::Show}, use {\bf ShowModal} and test for a returned identifier,
224 commonly wxID\_OK or wxID\_CANCEL.
225
226 \wxheading{wxItem}
227
228 This is renamed wxControl.
229
230 \wxheading{wxText, wxMultiText and wxTextWindow}
231
232 These classes no longer exist and are replaced by the single class wxTextCtrl.
233 Multi-line text items are created using the wxTE\_MULTILINE style.
234
235 \wxheading{wxButton}
236
237 Bitmap buttons are now a separate class, instead of being part of wxBitmap.
238
239 \wxheading{wxMessage}
240
241 Bitmap messages are now a separate class, wxStaticBitmap, and wxMessage
242 is renamed wxStaticText.
243
244 \wxheading{wxGroupBox}
245
246 wxGroupBox is renamed wxStaticBox.
247
248 \wxheading{wxForm}
249
250 Note that wxForm is no longer supported in wxWindows 2.0. Consider using the wxPropertyFormView class
251 instead, which takes standard dialogs and panels and associates controls with property objects.
252 You may also find that the new validation method, combined with dialog resources, is easier
253 and more flexible than using wxForm.
254
255 \chapter{Device contexts and painting}\label{dc}
256
257 In wxWindows 2.0, device contexts are used for drawing into, as per 1.xx, but the way
258 they are accessed and constructed is a bit different.
259
260 You no longer use {\bf GetDC} to access device contexts for panels, dialogs and canvases.
261 Instead, you create a temporary device context, which means that any window or control can be drawn
262 into. The sort of device context you create depends on where your code is called from. If
263 painting within an {\bf OnPaint} handler, you create a wxPaintDC. If not within an {\bf OnPaint} handler,
264 you use a wxClientDC or wxWindowDC. You can still parameterise your drawing code so that it
265 doesn't have to worry about what sort of device context to create - it uses the DC it is passed
266 from other parts of the program.
267
268 You {\bf must } create a wxPaintDC if you define an OnPaint handler, even if you do not
269 actually use this device context, or painting will not work correctly under Windows.
270
271 If you used device context functions with wxPoint or wxIntPoint before, please note
272 that wxPoint now contains integer members, and there is a new class wxRealPoint. wxIntPoint
273 no longer exists.
274
275 wxMetaFile and wxMetaFileDC have been renamed to wxMetafile and wxMetafileDC.
276
277 \chapter{Miscellaneous}
278
279 \section{Strings}
280
281 wxString has replaced char* in the majority of cases. For passing strings into functions,
282 this should not normally require you to change your code if the syntax is otherwise the
283 same. This is because C++ will automatically convert a char* or const char* to a wxString by virtue
284 of appropriate wxString constructors.
285
286 However, when a wxString is returned from a function in wxWindows 2.0 where a char* was
287 returned in wxWindows 1.xx, your application will need to be changed. Usually you can
288 simplify your application's allocation and deallocation of memory for the returned string,
289 and simply assign the result to a wxString object. For example, replace this:
290
291 {\small\begin{verbatim}
292 char* s = wxFunctionThatReturnsString();
293 s = copystring(s); // Take a copy in case it's temporary
294 .... // Do something with it
295 delete[] s;
296 \end{verbatim}
297 }
298
299 with this:
300
301 {\small\begin{verbatim}
302 wxString s = wxFunctionThatReturnsString();
303 .... // Do something with it
304 \end{verbatim}
305 }
306
307 To indicate an empty return value or a problem, a function may return either the
308 empty string (``") or a null string. You can check for a null string with wxString::IsNull().
309
310 \section{Use of const}
311
312 The {\bf const} keyword is now used to denote constant functions that do not affect the
313 object, and for function arguments to denote that the object passed cannot be changed.
314
315 This should not affect your application except for where you are overriding virtual functions
316 which now have a different signature. If functions are not being called which were previously,
317 check whether there is a parameter mismatch (or function type mismatch) involving consts.
318
319 Try to use the {\bf const} keyword in your own code where possible.
320
321 \chapter{Backward compatibility}\label{compat}
322
323 Some wxWindows 1.xx functionality has been left to ease the transition to 2.0. This functionality
324 (usually) only works if you compile with WXWIN\_COMPATIBILITY set to 1 in setup.h.
325
326 Mostly this defines old names to be the new names (e.g. wxRectangle is defined to be wxRect).
327
328 \chapter{Quick reference}\label{quickreference}
329
330 This section allows you to quickly find features that
331 need to be converted.
332
333 \section{Include files}
334
335 Use the form:
336
337 \begin{verbatim}
338 #include <wx/wx.h>
339 #include <wx/button.h>
340 \end{verbatim}
341
342 For precompiled header support, use this form:
343
344 \begin{verbatim}
345 // For compilers that support precompilation, includes "wx.h".
346 #include <wx/wxprec.h>
347
348 #ifdef __BORLANDC__
349 #pragma hdrstop
350 #endif
351
352 // Any files you want to include if not precompiling by including
353 // the whole of <wx/wx.h>
354 #ifndef WX_PRECOMP
355 #include <stdio.h>
356 #include <wx/setup.h>
357 #include <wx/bitmap.h>
358 #include <wx/brush.h>
359 #endif
360
361 // Any files you want to include regardless of precompiled headers
362 #include <wx/toolbar.h>
363 \end{verbatim}
364
365 \section{IPC classes}
366
367 These are now separated out into wxDDEServer/Client/Connection (Windows only) and wxTCPServer/Client/Connection
368 (Windows and Unix). Take care to use wxString for your overridden function arguments, instead of char*, as per
369 the documentation.
370
371 \section{MDI style frames}
372
373 MDI is now implemented as a family of separate classes, so you can't switch to MDI just by
374 using a different frame style. Please see the documentation for the MDI frame classes, and the MDI
375 sample may be helpful too.
376
377 \section{OnActivate}
378
379 Replace the arguments with one wxActivateEvent\& argument, make sure the function isn't virtual,
380 and add an EVT\_ACTIVATE event table entry.
381
382 \section{OnChar}
383
384 This is now a non-virtual function, with the same wxKeyEvent\& argument as before.
385 Add an EVT\_CHAR macro to the event table
386 for your window, and the implementation of your function will need very few changes.
387
388 \section{OnClose}
389
390 The old virtual function OnClose is now obsolete.
391 Add an OnCloseWindow event handler using an EVT\_CLOSE event table entry. For details
392 about window destruction, see the Windows Deletion Overview in the manual. This is a subtle
393 topic so please read it very carefully. Basically, OnCloseWindow is now responsible for
394 destroying a window with Destroy(), but the default implementation (for example for wxDialog) may not
395 destroy the window, so to be sure, always provide this event handler so it's obvious what's going on.
396
397 \section{OnEvent}
398
399 This is now a non-virtual function, with the same wxMouseEvent\& argument as before. However
400 you may wish to rename it OnMouseEvent. Add an EVT\_MOUSE\_EVENTS macro to the event table
401 for your window, and the implementation of your function will need very few changes.
402 However, if you wish to intercept different events using different functions, you can
403 specify specific events in your event table, such as EVT\_LEFT\_DOWN.
404
405 Your OnEvent function is likely to have references to GetDC(), so make sure you create
406 a wxClientDC instead. See \helpref{Device contexts}{dc}.
407
408 If you are using a wxScrolledWindow (formerly wxCanvas), you should call
409 PrepareDC(dc) to set the correct translation for the current scroll position.
410
411 \section{OnMenuCommand}
412
413 You need to replace this virtual function with a series of non-virtual functions, one for
414 each case of your old switch statement. Each function takes a wxCommandEvent\& argument.
415 Create an event table for your frame
416 containing EVT\_MENU macros, and insert DECLARE\_EVENT\_TABLE() in your frame class, as
417 per the samples.
418
419 \section{OnPaint}
420
421 This is now a non-virtual function, with a wxPaintEvent\& argument.
422 Add an EVT\_PAINT macro to the event table
423 for your window.
424
425 Your function {\it must} create a wxPaintDC object, instead of using GetDC to
426 obtain the device context.
427
428 If you are using a wxScrolledWindow (formerly wxCanvas), you should call
429 PrepareDC(dc) to set the correct translation for the current scroll position.
430
431 \section{OnSize}
432
433 Replace the arguments with one wxSizeEvent\& argument, make it non-virtual, and add to your
434 event table using EVT\_SIZE.
435
436 \section{wxApp definition}
437
438 The definition of OnInit has changed. Return a bool value, not a wxFrame.
439
440 Also, do {\it not} declare a global application object. Instead, use the macros
441 DECLARE\_APP and IMPLEMENT\_APP as per the samples. Remove any occurrences of IMPLEMENT\_WXWIN\_MAIN:
442 this is subsumed in IMPLEMENT\_APP.
443
444 \section{wxButton}
445
446 For bitmap buttons, use wxBitmapButton.
447
448 \section{wxCanvas}
449
450 Change the name to wxScrolledWindow.
451
452 \section{wxDialogBox}
453
454 Change the name to wxDialog, and for modal dialogs, use ShowModal instead of Show.
455
456 \section{wxDialog::Show}
457
458 If you used {\bf Show} to show a modal dialog or to override the standard
459 modal dialog {\bf Show}, use {\bf ShowModal} instead.
460
461 \wxheading{See also}
462
463 \helpref{Dialogs and controls}{dialogscontrols}
464
465 \section{wxForm}
466
467 Sorry, this class is no longer available. Try using the wxPropertyListView or wxPropertyFormView class
468 instead, or use .wxr files and validators.
469
470 \section{wxPoint}
471
472 The old wxPoint is called wxRealPoint, and wxPoint now uses integers.
473
474 \section{wxRectangle}
475
476 This is now called wxRect.
477
478 \section{wxScrollBar}
479
480 The function names have changed for this class: please refer to the documentation for wxScrollBar. Instead
481 of setting properties individually, you will call SetScrollbar with several parameters.
482
483 \section{wxText, wxMultiText, wxTextWindow}
484
485 Change all these to wxTextCtrl. Add the window style wxTE\_MULTILINE if you
486 wish to have a multi-line text control.
487
488 \section{wxToolBar}
489
490 This name is an alias for the most popular form of toolbar for your platform. There is now a family
491 of toolbar classes, with for example wxToolBar95, wxToolBarMSW and wxToolBarSimple classes existing
492 under Windows 95.
493
494 Toolbar management is supported by frames, so calling wxFrame::CreateToolBar and adding tools is usually
495 enough, and the SDI or MDI frame will manage the positioning for you. The client area of the frame is the space
496 left over when the menu bar, toolbar and status bar have been taken into account.
497
498 \end{document}