]> git.saurik.com Git - wxWidgets.git/blame - wxPython/docs/MigrationGuide.txt
API and etc. updates
[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)
110 self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
111
112I hope to be able to remove the need for using IDs even for menu
113events too...
114
115If you create your own custom event types and EVT_* functions, and you
116want to be able to use them with the Bind method above then you should
117change your EVT_* to be an instance of wxPyEventBinder instead of a
118function. If you used to have something like this::
119
120 myCustomEventType = wxNewEventType()
121 def EVT_MY_CUSTOM_EVENT(win, id, func):
122 win.Connect(id, -1, myCustomEventType, func)
123
124
125Change it like so::
126
6158f936
RD
127 myCustomEventType = wx.NewEventType()
128 EVT_MY_CUSTOM_EVENT = wx.PyEventBinder(myCustomEventType, 1)
d14a1e28
RD
129
130The second parameter is an integer in [0, 1, 2] that specifies the
131number of IDs that are needed to be passed to Connect.
132
133
134
135The wx Namespace
136----------------
137
138The second phase of the wx Namespace Transition has begun. That means
139that the real names of the classes and other symbols do not have the
140'wx' prefix and the modules are located in a Python package named
141wx. There is still a Python package named wxPython with modules
142that have the names with the wx prefix for backwards compatibility.
143Instead of dynamically changing the names at module load time like in
1442.4, the compatibility modules are generated at build time and contain
145assignment statements like this::
146
147 wxWindow = wx.core.Window
148
149Don't let the "core" in the name bother you. That and some other
150modules are implementation details, and everything that was in the
151wxPython.wx module before will still be in the wx package namespace
152after this change. So from your code you would use it as wx.Window.
153
154A few notes about how all of this was accomplished might be
155interesting... SWIG is now run twice for each module that it is
156generating code for. The first time it outputs an XML representaion
157of the parse tree, which can be up to 20MB and 300K lines in size!
158That XML is then run through a little Python script that creates a
159file full of SWIG %rename directives that take the wx off of the
160names, and also generates the Python compatibility file described
161above that puts the wx back on the names. SWIG is then run a second
162time to generate the C++ code to implement the extension module, and
163uses the %rename directives that were generated in the first step.
164
165Not every name is handled correctly (but the bulk of them are) and so
166some work has to be done by hand, especially for the reverse-renamers.
167So expect a few flaws here and there until everything gets sorted out.
168
169In summary, the wx package and names without the "wx" prefix are now
170the official form of the wxPython classes. For example::
171
172 import wx
173
174 class MyFrame(wx.Frame):
175 def __init__(self, parent, title):
176 wx.Frame.__init__(self, parent, -1, title)
177 p = wx.Panel(self, -1)
178 b = wx.Button(p, -1, "Do It", (10,10))
179 self.Bind(wx.EVT_BUTTON, self.JustDoIt, b)
180
181 def JustDoIt(self, evt):
182 print "It's done!"
183
184 app = wx.PySimpleApp()
185 f = MyFrame(None, "What's up?")
186 f.Show()
187 app.MainLoop()
188
189You shouldn't need to migrate all your modules over to use the new
190package and names right away as there are modules in place that try to
191provide as much backwards compatibility of the names as possible. If
82a074ce 192you rewrote the above sample using "from wxPython.wx import * ", the
d14a1e28
RD
193old wxNames, and the old style of event binding it will still work
194just fine.
195
196
197
198
199New wx.DC Methods
200-----------------
201
202Many of the Draw methods of wx.DC have alternate forms in C++ that take
203wxPoint or wxSize parameters (let's call these *Type A*) instead of
204the individual x, y, width, height, etc. parameters (and we'll call
205these *Type B*). In the rest of the library I normally made the *Type
206A* forms of the methods be the default method with the "normal" name,
207and had renamed the *Type B* forms of the methods to some similar
208name. For example in wx.Window we have these Python methods::
209
210 SetSize(size) # Type A
211 SetSizeWH(width, height) # Type B
212
213
214For various reasons the new *Type A* methods in wx.DC were never added
215and the existing *Type B* methods were never renamed. Now that lots
216of other things are also changing in wxPython it has been decided that
217it is a good time to also do the method renaming in wx.DC too in order
218to be consistent with the rest of the library. The methods in wx.DC
219that are affected are listed here::
220
221 FloodFillXY(x, y, colour, style = wx.FLOOD_SURFACE)
222 FloodFill(point, colour, style = wx.FLOOD_SURFACE)
223
224 GetPixelXY(x, y)
225 GetPixel(point)
226
227 DrawLineXY(x1, y1, x2, y2)
228 DrawLine(point1, point2)
229
230 CrossHairXY(x, y)
231 CrossHair(point)
232
233 DrawArcXY(x1, y1, x2, y2, xc, yc)
234 DrawArc(point1, point2, center)
235
236 DrawCheckMarkXY(x, y, width, height)
237 DrawCheckMark(rect)
238
239 DrawEllipticArcXY(x, y, w, h, start_angle, end_angle)
240 DrawEllipticArc(point, size, start_angle, end_angle)
241
242 DrawPointXY(x, y)
243 DrawPoint(point)
244
245 DrawRectangleXY(x, y, width, height)
246 DrawRectangle(point, size)
247 DrawRectangleRect(rect)
248
249 DrawRoundedRectangleXY(x, y, width, height, radius)
250 DrawRoundedRectangle(point, size, radius)
251 DrawRoundedRectangleRect(rect, radius)
252
253 DrawCircleXY(x, y, radius)
254 DrawCircle(point, radius)
255
256 DrawEllipseXY(x, y, width, height)
257 DrawEllipse(point, size)
258 DrawEllipseRect(rect)
259
260 DrawIconXY(icon, x, y)
261 DrawIcon(icon, point)
262
263 DrawBitmapXY(bmp, x, y, useMask = FALSE)
264 DrawBitmap(bmp, point, useMask = FALSE)
265
266 DrawTextXY(text, x, y)
267 DrawText(text, point)
268
269 DrawRotatedTextXY(text, x, y, angle)
270 DrawRotatedText(text, point, angle)
271
272
273 BlitXY(xdest, ydest, width, height, sourceDC, xsrc, ysrc,
274 rop = wxCOPY, useMask = FALSE, xsrcMask = -1, ysrcMask = -1)
275 Blit(destPt, size, sourceDC, srcPt,
276 rop = wxCOPY, useMask = FALSE, srcPtMask = wx.DefaultPosition)
277
82a074ce 278 SetClippingRegionXY(x, y, width, height)
4da6d35e
RD
279 SetClippingRegion(point, size)
280 SetClippingRect(rect)
281 SetClippingRegionAsRegion(region);
282
d14a1e28 283
4942342c
RD
284If you have code that draws on a DC and you are using the new wx
285namespace then you **will** get errors because of these changes, but
286it should be easy to fix the code. You can either change the name of
287the *Type B* method called to the names shown above, or just add
288parentheses around the parameters as needed to turn them into tuples
289and let the SWIG typemaps turn them into the wx.Point or wx.Size
290object that is expected. Then you will be calling the new *Type A*
291method. For example, if you had this code before::
d14a1e28
RD
292
293 dc.DrawRectangle(x, y, width, height)
294
295You could either continue to use the *Type B* method bu changing the
296name to DrawRectabgleXY, or just change it to the new *Type A* by
297adding some parentheses like this::
298
299 dc.DrawRectangle((x, y), (width, height))
300
301Or if you were already using a point and size::
302
303 dc.DrawRectangle(p.x, p.y, s.width, s.height)
304
305Then you can just simplify it like this::
306
307 dc.DrawRectangle(p, s)
308
4942342c
RD
309Now before you start yelling and screaming at me for breaking all your
310code, take note that I said above "...using the new wx namespace..."
311That's because if you are still importing from wxPython.wx then there
312are some classes defined there with Draw and etc. methods that have
3132.4 compatible signatures. However if/when the old wxPython.wx
314namespace is removed then these classes will be removed too so you
315should plam on migrating to the new namespace and new DC Draw methods
316before that time.
d14a1e28
RD
317
318
319
320Building, Extending and Embedding wxPython
321------------------------------------------
322
323wxPython's setup.py script now expects to use existing libraries for
324the contribs (gizmos, stc, xrc, etc.) rather than building local
325copies of them. If you build your own copies of wxPython please be
326aware that you now need to also build the ogl, stc, xrc, and gizmos
327libraries in addition to the main wx lib. [[TODO: update the
328BUILD.*.txt files too!]]
329
330The wxPython.h and other header files are now in
331.../wxPython/include/wx/wxPython instead of in wxPython/src. You should
332include it via the "wx/wxPython/wxPython.h" path and add
333.../wxPython/include to your list of include paths. [[TODO: Install
334these headers on Linux...]]
335
336You no longer need to call wxClassInfo::CleanUpClasses() and
337wxClassInfo::InitializeClasses() in your extensions or when embedding
338wxPython.
339
340
341
342
343Two (or Three!) Phase Create
344----------------------------
345
346If you use the Precreate/Create method of instantiating a window, (for
347example, to set an extended style flag, or for XRC handlers) then
348there is now a new method named PostCreate to help with transplanting
349the brain of the prewindow instance into the derived window instance.
350For example::
351
352 class MyDialog(wx.Dialog):
353 def __init__(self, parent, ID, title, pos, size, style):
354 pre = wx.PreDialog()
355 pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
356 pre.Create(parent, ID, title, pos, size, style)
357 self.PostCreate(pre)
358
359
360
361Sizers
362------
363
364The hack allowing the old "option" keyword parameter has been
365removed. If you use keyworkd args with wxSizer Add, Insert, or
366Prepend then you will need to use the "proportion" name instead of
367"option".
368
369When adding a spacer to a sizer you now need to use a wxSize or a
3702-integer sequence instead of separate width and height parameters.
371
372The wxGridBagSizer class (very similar to the RowColSizer in the
373library) has been added to C++ and wrapped for wxPython. It can also
374be used from XRC.
375
376You should not use AddWindow, AddSizer, AddSpacer (and similar for
377Insert, Prepend, and etc.) methods any longer. Just use Add and the
378wrappers will figure out what to do.
379
380
381
382Other Stuff
383-----------
384
385Instead of over a dozen separate extension modules linked together
386into a single extension module, the "core" module is now just a few
387extensions that are linked independently, and then merged together
388later into the main namespace via Python code.
389
390Because of the above, the "internal" module names have changed, but
391you shouldn't have been using them anyway so it shouldn't bother
392you. ;-)
393
394The wxPython.help module no longer exists and the classes therein are
395now part of the core module imported with wxPython.wx or the wx
396package.
397
398wxPyDefaultPosition and wxPyDefaultSize are gone. Use the
399wxDefaultPosition and wxDefaultSize objects instead.
400
401Similarly, the wxSystemSettings backwards compatibiility aliases for
402GetSystemColour, GetSystemFont and GetSystemMetric have also gone into
403the bit-bucket. Use GetColour, GetFont and GetMetric instead.
404
405
ed8e1ecb
RD
406The wx.NO_FULL_REPAINT_ON_RESIZE style is now the default style for
407all windows. The name still exists for compatibility, but it is set
408to zero. If you want to disable the setting (so it matches the old
409default) then you need to use the new wx.FULL_REPAINT_ON_RESIZE style
410flag otherwise only the freshly exposed areas of the window will be
411refreshed.
d14a1e28 412
1f9b31fc
RD
413wxPyTypeCast has been removed. Since we've had the OOR (Original
414Object Return) for a couple years now there should be no need to use
415wxPyTypeCast at all.
d14a1e28 416