]> git.saurik.com Git - wxWidgets.git/blame - wxPython/docs/MigrationGuide.txt
added tech note about writing unit tests
[wxWidgets.git] / wxPython / docs / MigrationGuide.txt
CommitLineData
d14a1e28
RD
1============================
2wxPython 2.5 Migration Guide
3============================
4
5This document will help explain some of the major changes in wxPython
62.5 and let you know what you need to do to adapt your programs to
7those changes. Be sure to also check in the CHANGES.txt file like
8usual to see info about the not so major changes and other things that
9have been added to wxPython.
10
11
12
13Module Initialization
14---------------------
15
16The import-startup-bootstrap process employed by wxPython was changed
17such that wxWindows and the underlying gui toolkit are **not**
18initialized until the wx.App object is created (but before wx.App.OnInit
19is called.) This was required because of some changes that were made
20to the C++ wxApp class.
21
22There are both benefits and potential problems with this change. The
23benefits are that you can import wxPython without requiring access to
24a GUI (for checking version numbers, etc.) and that in a
25multi-threaded environment the thread that creates the app object will
26now be the GUI thread instead of the one that imports wxPython. Some
27potential problems are that the C++ side of the "stock-objects"
28(wx.BLUE_PEN, wx.TheColourDatabase, etc.) are not initialized until
29the wx.App object is created, so you should not use them until after
61563ef3
RD
30you have created your wx.App object. If you do then an exception will
31be raised telling you that the C++ object has not bene initialized
32yet.
d14a1e28
RD
33
34Also, you will probably not be able to do any kind of GUI or bitmap
35operation unless you first have created an app object, (even on
36Windows where most anything was possible before.)
37
38
39
40SWIG 1.3
41--------
42
43wxPython is now using SWIG 1.3.x from CVS (with several of my own
44customizations added that I hope to get folded back into the main SWIG
45distribution.) This has some far reaching ramifications:
46
47 All classes derive from object and so all are now "new-style
48 classes"
49
50 Public data members of the C++ classes are wrapped as Python
51 properties using property() instead of using __getattr__/__setattr__
52 like before. Normally you shouldn't notice any difference, but if
53 you were previously doing something with __getattr__/__setattr__
54 in derived classes then you may have to adjust things.
55
56 Static C++ methods are wrapped using the staticmethod()
57 feature of Python and so are accessible as ClassName.MethodName
58 as expected. They are still available as top level functions
59 ClassName_MethodName as before.
60
61 The relationship between the wxFoo and wxFooPtr classes have
62 changed for the better. Specifically, all instances that you see
63 will be wxFoo even if they are created internally using wxFooPtr,
64 because wxFooPtr.__init__ will change the instance's __class__ as
65 part of the initialization. If you have any code that checks
66 class type using something like isinstance(obj, wxFooPtr) you will
67 need to change it to isinstance(obj, wxFoo).
68
69
70
71Binding Events
72--------------
73
74All of the EVT_* functions are now instances of the wx.PyEventBinder
75class. They have a __call__ method so they can still be used as
76functions like before, but making them instances adds some
77flexibility.
78
79wx.EvtHandler (the base class for wx.Window) now has a Bind method that
80makes binding events to windows a little easier. Here is its
81definition and docstring::
82
83 def Bind(self, event, handler, source=None, id=wxID_ANY, id2=wxID_ANY):
84 """
85 Bind an event to an event handler.
86
87 event One of the EVT_* objects that specifies the
88 type of event to bind.
89
90 handler A callable object to be invoked when the event
91 is delivered to self. Pass None to disconnect an
92 event handler.
93
94 source Sometimes the event originates from a different window
95 than self, but you still want to catch it in self. (For
96 example, a button event delivered to a frame.) By
97 passing the source of the event, the event handling
98 system is able to differentiate between the same event
99 type from different controls.
100
101 id,id2 Used for menu IDs or for event types that require a
102 range of IDs
103
104 """
105
106Some examples of its use::
107
108 self.Bind(wx.EVT_SIZE, self.OnSize)
109 self.Bind(wx.EVT_BUTTON, self.OnButtonClick, theButton)
c8000995
RD
110 self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
111
112
113The wx.Menu methods that add items to a wx.Menu have been modified
114such that they return a reference to the wx.MenuItem that was created.
115Additionally menu items and toolbar items have been modified to
116automatically generate a new ID if -1 is given, similar to using -1
117with window classess. This means that you can create menu or toolbar
118items and event bindings without having to predefine a unique menu ID,
119although you still can use IDs just like before if you want. For
120example, these are all equivallent other than ID values::
121
122 1.
123 item = menu.Append(-1, "E&xit", "Terminate the App")
124 self.Bind(wx.EVT_MENU, self.OnExit, item)
125
126 2.
127 item = menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App")
128 self.Bind(wx.EVT_MENU, self.OnExit, item)
d14a1e28 129
c8000995
RD
130 3.
131 menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App")
132 self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
133
134
d14a1e28
RD
135If you create your own custom event types and EVT_* functions, and you
136want to be able to use them with the Bind method above then you should
137change your EVT_* to be an instance of wxPyEventBinder instead of a
138function. If you used to have something like this::
139
140 myCustomEventType = wxNewEventType()
141 def EVT_MY_CUSTOM_EVENT(win, id, func):
142 win.Connect(id, -1, myCustomEventType, func)
143
144
145Change it like so::
146
6158f936
RD
147 myCustomEventType = wx.NewEventType()
148 EVT_MY_CUSTOM_EVENT = wx.PyEventBinder(myCustomEventType, 1)
d14a1e28
RD
149
150The second parameter is an integer in [0, 1, 2] that specifies the
151number of IDs that are needed to be passed to Connect.
152
153
154
c8000995
RD
155
156
d14a1e28
RD
157The wx Namespace
158----------------
159
160The second phase of the wx Namespace Transition has begun. That means
161that the real names of the classes and other symbols do not have the
162'wx' prefix and the modules are located in a Python package named
163wx. There is still a Python package named wxPython with modules
164that have the names with the wx prefix for backwards compatibility.
165Instead of dynamically changing the names at module load time like in
1662.4, the compatibility modules are generated at build time and contain
167assignment statements like this::
168
169 wxWindow = wx.core.Window
170
171Don't let the "core" in the name bother you. That and some other
172modules are implementation details, and everything that was in the
173wxPython.wx module before will still be in the wx package namespace
174after this change. So from your code you would use it as wx.Window.
175
176A few notes about how all of this was accomplished might be
177interesting... SWIG is now run twice for each module that it is
178generating code for. The first time it outputs an XML representaion
179of the parse tree, which can be up to 20MB and 300K lines in size!
180That XML is then run through a little Python script that creates a
181file full of SWIG %rename directives that take the wx off of the
182names, and also generates the Python compatibility file described
183above that puts the wx back on the names. SWIG is then run a second
184time to generate the C++ code to implement the extension module, and
185uses the %rename directives that were generated in the first step.
186
187Not every name is handled correctly (but the bulk of them are) and so
188some work has to be done by hand, especially for the reverse-renamers.
189So expect a few flaws here and there until everything gets sorted out.
190
191In summary, the wx package and names without the "wx" prefix are now
192the official form of the wxPython classes. For example::
193
194 import wx
195
196 class MyFrame(wx.Frame):
197 def __init__(self, parent, title):
198 wx.Frame.__init__(self, parent, -1, title)
199 p = wx.Panel(self, -1)
200 b = wx.Button(p, -1, "Do It", (10,10))
201 self.Bind(wx.EVT_BUTTON, self.JustDoIt, b)
202
203 def JustDoIt(self, evt):
204 print "It's done!"
205
206 app = wx.PySimpleApp()
207 f = MyFrame(None, "What's up?")
208 f.Show()
209 app.MainLoop()
210
211You shouldn't need to migrate all your modules over to use the new
212package and names right away as there are modules in place that try to
213provide as much backwards compatibility of the names as possible. If
82a074ce 214you rewrote the above sample using "from wxPython.wx import * ", the
d14a1e28
RD
215old wxNames, and the old style of event binding it will still work
216just fine.
217
218
219
220
221New wx.DC Methods
222-----------------
223
224Many of the Draw methods of wx.DC have alternate forms in C++ that take
225wxPoint or wxSize parameters (let's call these *Type A*) instead of
226the individual x, y, width, height, etc. parameters (and we'll call
227these *Type B*). In the rest of the library I normally made the *Type
228A* forms of the methods be the default method with the "normal" name,
229and had renamed the *Type B* forms of the methods to some similar
230name. For example in wx.Window we have these Python methods::
231
232 SetSize(size) # Type A
233 SetSizeWH(width, height) # Type B
234
235
236For various reasons the new *Type A* methods in wx.DC were never added
237and the existing *Type B* methods were never renamed. Now that lots
238of other things are also changing in wxPython it has been decided that
239it is a good time to also do the method renaming in wx.DC too in order
240to be consistent with the rest of the library. The methods in wx.DC
241that are affected are listed here::
242
243 FloodFillXY(x, y, colour, style = wx.FLOOD_SURFACE)
244 FloodFill(point, colour, style = wx.FLOOD_SURFACE)
245
246 GetPixelXY(x, y)
247 GetPixel(point)
248
249 DrawLineXY(x1, y1, x2, y2)
250 DrawLine(point1, point2)
251
252 CrossHairXY(x, y)
253 CrossHair(point)
254
255 DrawArcXY(x1, y1, x2, y2, xc, yc)
256 DrawArc(point1, point2, center)
257
258 DrawCheckMarkXY(x, y, width, height)
259 DrawCheckMark(rect)
260
261 DrawEllipticArcXY(x, y, w, h, start_angle, end_angle)
262 DrawEllipticArc(point, size, start_angle, end_angle)
263
264 DrawPointXY(x, y)
265 DrawPoint(point)
266
267 DrawRectangleXY(x, y, width, height)
268 DrawRectangle(point, size)
269 DrawRectangleRect(rect)
270
271 DrawRoundedRectangleXY(x, y, width, height, radius)
272 DrawRoundedRectangle(point, size, radius)
273 DrawRoundedRectangleRect(rect, radius)
274
275 DrawCircleXY(x, y, radius)
276 DrawCircle(point, radius)
277
278 DrawEllipseXY(x, y, width, height)
279 DrawEllipse(point, size)
280 DrawEllipseRect(rect)
281
282 DrawIconXY(icon, x, y)
283 DrawIcon(icon, point)
284
285 DrawBitmapXY(bmp, x, y, useMask = FALSE)
286 DrawBitmap(bmp, point, useMask = FALSE)
287
288 DrawTextXY(text, x, y)
289 DrawText(text, point)
290
291 DrawRotatedTextXY(text, x, y, angle)
292 DrawRotatedText(text, point, angle)
293
294
295 BlitXY(xdest, ydest, width, height, sourceDC, xsrc, ysrc,
296 rop = wxCOPY, useMask = FALSE, xsrcMask = -1, ysrcMask = -1)
297 Blit(destPt, size, sourceDC, srcPt,
298 rop = wxCOPY, useMask = FALSE, srcPtMask = wx.DefaultPosition)
299
82a074ce 300 SetClippingRegionXY(x, y, width, height)
4da6d35e
RD
301 SetClippingRegion(point, size)
302 SetClippingRect(rect)
303 SetClippingRegionAsRegion(region);
304
d14a1e28 305
4942342c
RD
306If you have code that draws on a DC and you are using the new wx
307namespace then you **will** get errors because of these changes, but
308it should be easy to fix the code. You can either change the name of
309the *Type B* method called to the names shown above, or just add
310parentheses around the parameters as needed to turn them into tuples
311and let the SWIG typemaps turn them into the wx.Point or wx.Size
312object that is expected. Then you will be calling the new *Type A*
313method. For example, if you had this code before::
d14a1e28
RD
314
315 dc.DrawRectangle(x, y, width, height)
316
317You could either continue to use the *Type B* method bu changing the
318name to DrawRectabgleXY, or just change it to the new *Type A* by
319adding some parentheses like this::
320
321 dc.DrawRectangle((x, y), (width, height))
322
323Or if you were already using a point and size::
324
325 dc.DrawRectangle(p.x, p.y, s.width, s.height)
326
327Then you can just simplify it like this::
328
329 dc.DrawRectangle(p, s)
330
4942342c
RD
331Now before you start yelling and screaming at me for breaking all your
332code, take note that I said above "...using the new wx namespace..."
333That's because if you are still importing from wxPython.wx then there
334are some classes defined there with Draw and etc. methods that have
3352.4 compatible signatures. However if/when the old wxPython.wx
336namespace is removed then these classes will be removed too so you
e75fd8a4 337should plan on migrating to the new namespace and new DC Draw methods
4942342c 338before that time.
d14a1e28
RD
339
340
341
342Building, Extending and Embedding wxPython
343------------------------------------------
344
345wxPython's setup.py script now expects to use existing libraries for
346the contribs (gizmos, stc, xrc, etc.) rather than building local
347copies of them. If you build your own copies of wxPython please be
348aware that you now need to also build the ogl, stc, xrc, and gizmos
349libraries in addition to the main wx lib. [[TODO: update the
350BUILD.*.txt files too!]]
351
352The wxPython.h and other header files are now in
353.../wxPython/include/wx/wxPython instead of in wxPython/src. You should
354include it via the "wx/wxPython/wxPython.h" path and add
355.../wxPython/include to your list of include paths. [[TODO: Install
356these headers on Linux...]]
357
358You no longer need to call wxClassInfo::CleanUpClasses() and
359wxClassInfo::InitializeClasses() in your extensions or when embedding
360wxPython.
361
362
363
364
365Two (or Three!) Phase Create
366----------------------------
367
368If you use the Precreate/Create method of instantiating a window, (for
369example, to set an extended style flag, or for XRC handlers) then
370there is now a new method named PostCreate to help with transplanting
371the brain of the prewindow instance into the derived window instance.
372For example::
373
374 class MyDialog(wx.Dialog):
375 def __init__(self, parent, ID, title, pos, size, style):
376 pre = wx.PreDialog()
377 pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
378 pre.Create(parent, ID, title, pos, size, style)
379 self.PostCreate(pre)
380
381
382
383Sizers
384------
385
e6a5dac6
RD
386The hack allowing the old "option" keyword parameter has been removed.
387If you use keyworkd args with wxSizer Add, Insert, or Prepend methods
388then you will need to use the "proportion" name instead of "option".
d14a1e28
RD
389
390When adding a spacer to a sizer you now need to use a wxSize or a
3912-integer sequence instead of separate width and height parameters.
392
393The wxGridBagSizer class (very similar to the RowColSizer in the
394library) has been added to C++ and wrapped for wxPython. It can also
395be used from XRC.
396
397You should not use AddWindow, AddSizer, AddSpacer (and similar for
398Insert, Prepend, and etc.) methods any longer. Just use Add and the
399wrappers will figure out what to do.
400
401
402
403Other Stuff
404-----------
405
406Instead of over a dozen separate extension modules linked together
407into a single extension module, the "core" module is now just a few
408extensions that are linked independently, and then merged together
409later into the main namespace via Python code.
410
e6a5dac6
RD
411Because of the above and also because of the way the new SWIG works,
412the "internal" module names have changed, but you shouldn't have been
413using them anyway so it shouldn't bother you. ;-)
d14a1e28 414
e6a5dac6
RD
415The help module no longer exists and the classes therein are now part
416of the core module imported with wxPython.wx or the wx package.
d14a1e28
RD
417
418wxPyDefaultPosition and wxPyDefaultSize are gone. Use the
419wxDefaultPosition and wxDefaultSize objects instead.
420
421Similarly, the wxSystemSettings backwards compatibiility aliases for
422GetSystemColour, GetSystemFont and GetSystemMetric have also gone into
423the bit-bucket. Use GetColour, GetFont and GetMetric instead.
424
425
ed8e1ecb
RD
426The wx.NO_FULL_REPAINT_ON_RESIZE style is now the default style for
427all windows. The name still exists for compatibility, but it is set
428to zero. If you want to disable the setting (so it matches the old
429default) then you need to use the new wx.FULL_REPAINT_ON_RESIZE style
430flag otherwise only the freshly exposed areas of the window will be
431refreshed.
d14a1e28 432
1f9b31fc
RD
433wxPyTypeCast has been removed. Since we've had the OOR (Original
434Object Return) for a couple years now there should be no need to use
435wxPyTypeCast at all.
d14a1e28 436
e6a5dac6
RD
437If you use the old wxPython package and wxPython.wx namespace then
438there are compatibility aliases for much of the above items.
78862f24
RD
439
440The wxWave class has been renamed to wxSound, and now has a slightly
441different API.