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