]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | ============================ |
2 | wxPython 2.5 Migration Guide | |
3 | ============================ | |
4 | ||
5 | This document will help explain some of the major changes in wxPython | |
6 | 2.5 and let you know what you need to do to adapt your programs to | |
33ab916f | 7 | those changes. Be sure to also check in the CHANGES_ file like |
d14a1e28 RD |
8 | usual to see info about the not so major changes and other things that |
9 | have been added to wxPython. | |
10 | ||
33ab916f RD |
11 | .. _CHANGES: CHANGES.html |
12 | ||
d14a1e28 | 13 | |
e8a71fa0 RD |
14 | wxName Change |
15 | ------------- | |
16 | ||
17 | The **wxWindows** project and library is now known as | |
18 | **wxWidgets**. Please see here_ for more details. | |
19 | ||
29bfe46b | 20 | .. _here: http://www.wxwidgets.org/name.htm |
e8a71fa0 RD |
21 | |
22 | This won't really affect wxPython all that much, other than the fact | |
23 | that the wxwindows.org domain name will be changing to wxwidgets.org, | |
24 | so mail list, CVS, and etc. addresses will be changing. We're going | |
25 | to try and smooth the transition as much as possible, but I wanted you | |
26 | all to be aware of this change if you run into any issues. | |
27 | ||
28 | ||
d14a1e28 RD |
29 | |
30 | Module Initialization | |
31 | --------------------- | |
32 | ||
33 | The import-startup-bootstrap process employed by wxPython was changed | |
e8a71fa0 | 34 | such that wxWidgets and the underlying gui toolkit are **not** |
d14a1e28 RD |
35 | initialized until the wx.App object is created (but before wx.App.OnInit |
36 | is called.) This was required because of some changes that were made | |
37 | to the C++ wxApp class. | |
38 | ||
39 | There are both benefits and potential problems with this change. The | |
40 | benefits are that you can import wxPython without requiring access to | |
41 | a GUI (for checking version numbers, etc.) and that in a | |
42 | multi-threaded environment the thread that creates the app object will | |
43 | now be the GUI thread instead of the one that imports wxPython. Some | |
44 | potential problems are that the C++ side of the "stock-objects" | |
45 | (wx.BLUE_PEN, wx.TheColourDatabase, etc.) are not initialized until | |
46 | the wx.App object is created, so you should not use them until after | |
61563ef3 | 47 | you have created your wx.App object. If you do then an exception will |
cb2d8b77 | 48 | be raised telling you that the C++ object has not been initialized |
61563ef3 | 49 | yet. |
d14a1e28 RD |
50 | |
51 | Also, you will probably not be able to do any kind of GUI or bitmap | |
52 | operation unless you first have created an app object, (even on | |
53 | Windows where most anything was possible before.) | |
54 | ||
55 | ||
56 | ||
57 | SWIG 1.3 | |
58 | -------- | |
59 | ||
60 | wxPython is now using SWIG 1.3.x from CVS (with several of my own | |
61 | customizations added that I hope to get folded back into the main SWIG | |
62 | distribution.) This has some far reaching ramifications: | |
63 | ||
64 | All classes derive from object and so all are now "new-style | |
d7403ad2 RD |
65 | classes." This also allows you to use mixin classes that are |
66 | new-style and to use properties, staticmethod, etc. | |
d14a1e28 RD |
67 | |
68 | Public data members of the C++ classes are wrapped as Python | |
d7403ad2 RD |
69 | properties using property() instead of using |
70 | __getattr__/__setattr__ hacks like before. Normally you shouldn't | |
71 | notice any difference, but if you were previously doing something | |
72 | with __getattr__/__setattr__ in derived classes then you may have | |
73 | to adjust things. | |
74 | ||
75 | Static C++ methods are wrapped using the staticmethod() feature of | |
76 | Python and so are accessible as ClassName.MethodName as expected. | |
77 | They are still also available as top level functions named like | |
d14a1e28 RD |
78 | ClassName_MethodName as before. |
79 | ||
80 | The relationship between the wxFoo and wxFooPtr classes have | |
81 | changed for the better. Specifically, all instances that you see | |
d7403ad2 RD |
82 | will be wx.Foo even if they are created internally using wx.FooPtr, |
83 | because wx.FooPtr.__init__ will change the instance's __class__ as | |
d14a1e28 | 84 | part of the initialization. If you have any code that checks |
d7403ad2 RD |
85 | class type using something like isinstance(obj, wx.FooPtr) you will |
86 | need to change it to isinstance(obj, wx.Foo). | |
d14a1e28 RD |
87 | |
88 | ||
89 | ||
90 | Binding Events | |
91 | -------------- | |
92 | ||
93 | All of the EVT_* functions are now instances of the wx.PyEventBinder | |
94 | class. They have a __call__ method so they can still be used as | |
95 | functions like before, but making them instances adds some | |
29bfe46b | 96 | flexibility that I expect to take advantave of in the future. |
d14a1e28 RD |
97 | |
98 | wx.EvtHandler (the base class for wx.Window) now has a Bind method that | |
99 | makes binding events to windows a little easier. Here is its | |
100 | definition and docstring:: | |
101 | ||
102 | def Bind(self, event, handler, source=None, id=wxID_ANY, id2=wxID_ANY): | |
103 | """ | |
104 | Bind an event to an event handler. | |
105 | ||
106 | event One of the EVT_* objects that specifies the | |
107 | type of event to bind. | |
108 | ||
109 | handler A callable object to be invoked when the event | |
110 | is delivered to self. Pass None to disconnect an | |
111 | event handler. | |
112 | ||
113 | source Sometimes the event originates from a different window | |
114 | than self, but you still want to catch it in self. (For | |
115 | example, a button event delivered to a frame.) By | |
116 | passing the source of the event, the event handling | |
117 | system is able to differentiate between the same event | |
118 | type from different controls. | |
119 | ||
120 | id,id2 Used for menu IDs or for event types that require a | |
121 | range of IDs | |
122 | ||
123 | """ | |
124 | ||
125 | Some examples of its use:: | |
126 | ||
127 | self.Bind(wx.EVT_SIZE, self.OnSize) | |
128 | self.Bind(wx.EVT_BUTTON, self.OnButtonClick, theButton) | |
c8000995 RD |
129 | self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT) |
130 | ||
131 | ||
132 | The wx.Menu methods that add items to a wx.Menu have been modified | |
133 | such that they return a reference to the wx.MenuItem that was created. | |
134 | Additionally menu items and toolbar items have been modified to | |
135 | automatically generate a new ID if -1 is given, similar to using -1 | |
136 | with window classess. This means that you can create menu or toolbar | |
137 | items and event bindings without having to predefine a unique menu ID, | |
138 | although you still can use IDs just like before if you want. For | |
e8a71fa0 RD |
139 | example, these are all equivallent other than their specific ID |
140 | values:: | |
c8000995 RD |
141 | |
142 | 1. | |
143 | item = menu.Append(-1, "E&xit", "Terminate the App") | |
144 | self.Bind(wx.EVT_MENU, self.OnExit, item) | |
145 | ||
146 | 2. | |
147 | item = menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App") | |
148 | self.Bind(wx.EVT_MENU, self.OnExit, item) | |
d14a1e28 | 149 | |
c8000995 RD |
150 | 3. |
151 | menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App") | |
152 | self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT) | |
153 | ||
154 | ||
d14a1e28 RD |
155 | If you create your own custom event types and EVT_* functions, and you |
156 | want to be able to use them with the Bind method above then you should | |
d7403ad2 | 157 | change your EVT_* to be an instance of wx.PyEventBinder instead of a |
29bfe46b | 158 | function. For example, if you used to have something like this:: |
d14a1e28 RD |
159 | |
160 | myCustomEventType = wxNewEventType() | |
161 | def EVT_MY_CUSTOM_EVENT(win, id, func): | |
162 | win.Connect(id, -1, myCustomEventType, func) | |
163 | ||
164 | ||
165 | Change it like so:: | |
166 | ||
6158f936 RD |
167 | myCustomEventType = wx.NewEventType() |
168 | EVT_MY_CUSTOM_EVENT = wx.PyEventBinder(myCustomEventType, 1) | |
d14a1e28 RD |
169 | |
170 | The second parameter is an integer in [0, 1, 2] that specifies the | |
171 | number of IDs that are needed to be passed to Connect. | |
172 | ||
d7403ad2 RD |
173 | **[Changed in 2.5.1.6]** There is also an Unbind method added to |
174 | wx.EvtHandler that can be used to disconenct event handlers. It looks | |
175 | like this:: | |
176 | ||
177 | def Unbind(self, event, source=None, id=wx.ID_ANY, id2=wx.ID_ANY): | |
178 | """ | |
179 | Disconencts the event handler binding for event from self. | |
180 | Returns True if successful. | |
181 | """ | |
d14a1e28 RD |
182 | |
183 | ||
c8000995 RD |
184 | |
185 | ||
d14a1e28 RD |
186 | The wx Namespace |
187 | ---------------- | |
188 | ||
189 | The second phase of the wx Namespace Transition has begun. That means | |
190 | that the real names of the classes and other symbols do not have the | |
191 | 'wx' prefix and the modules are located in a Python package named | |
192 | wx. There is still a Python package named wxPython with modules | |
193 | that have the names with the wx prefix for backwards compatibility. | |
194 | Instead of dynamically changing the names at module load time like in | |
195 | 2.4, the compatibility modules are generated at build time and contain | |
196 | assignment statements like this:: | |
197 | ||
d7403ad2 | 198 | wxWindow = wx._core.Window |
d14a1e28 | 199 | |
d7403ad2 | 200 | Don't let the "_core" in the name bother you. That and some other |
d14a1e28 RD |
201 | modules are implementation details, and everything that was in the |
202 | wxPython.wx module before will still be in the wx package namespace | |
d7403ad2 RD |
203 | after this change. So from your code you would use it as wx.Window or |
204 | wxWindow if you import from the wxPython.wx module. | |
d14a1e28 RD |
205 | |
206 | A few notes about how all of this was accomplished might be | |
207 | interesting... SWIG is now run twice for each module that it is | |
208 | generating code for. The first time it outputs an XML representaion | |
209 | of the parse tree, which can be up to 20MB and 300K lines in size! | |
210 | That XML is then run through a little Python script that creates a | |
211 | file full of SWIG %rename directives that take the wx off of the | |
212 | names, and also generates the Python compatibility file described | |
213 | above that puts the wx back on the names. SWIG is then run a second | |
214 | time to generate the C++ code to implement the extension module, and | |
215 | uses the %rename directives that were generated in the first step. | |
216 | ||
217 | Not every name is handled correctly (but the bulk of them are) and so | |
218 | some work has to be done by hand, especially for the reverse-renamers. | |
219 | So expect a few flaws here and there until everything gets sorted out. | |
220 | ||
221 | In summary, the wx package and names without the "wx" prefix are now | |
222 | the official form of the wxPython classes. For example:: | |
223 | ||
224 | import wx | |
225 | ||
226 | class MyFrame(wx.Frame): | |
227 | def __init__(self, parent, title): | |
228 | wx.Frame.__init__(self, parent, -1, title) | |
229 | p = wx.Panel(self, -1) | |
230 | b = wx.Button(p, -1, "Do It", (10,10)) | |
231 | self.Bind(wx.EVT_BUTTON, self.JustDoIt, b) | |
232 | ||
233 | def JustDoIt(self, evt): | |
234 | print "It's done!" | |
235 | ||
236 | app = wx.PySimpleApp() | |
237 | f = MyFrame(None, "What's up?") | |
238 | f.Show() | |
239 | app.MainLoop() | |
240 | ||
241 | You shouldn't need to migrate all your modules over to use the new | |
242 | package and names right away as there are modules in place that try to | |
243 | provide as much backwards compatibility of the names as possible. If | |
82a074ce | 244 | you rewrote the above sample using "from wxPython.wx import * ", the |
d14a1e28 RD |
245 | old wxNames, and the old style of event binding it will still work |
246 | just fine. | |
247 | ||
248 | ||
249 | ||
250 | ||
251 | New wx.DC Methods | |
252 | ----------------- | |
253 | ||
d7403ad2 RD |
254 | **[Changed in 2.5.1.6]** In wxPython 2.5.1.5 there was a new |
255 | implementation of the wx.DC Draw and other methods that broke | |
256 | backwards compatibility in the name of consistency. That change has | |
257 | been reverted and the wx.DC Draw methods with 2.4 compatible | |
258 | signatures have been restored. In addition a new set of methods have | |
259 | been added that take wx.Point and/or wx.Size parameters instead of | |
260 | separate integer parameters. The Draw and etc. methods now available | |
51b2943a | 261 | in the wx.DC class are:: |
d14a1e28 | 262 | |
d14a1e28 | 263 | |
51b2943a RD |
264 | FloodFill(self, x, y, colour, style = wx.FLOOD_SURFACE) |
265 | FoodFillPoint(self, pt, colour, style = wx.FLOOD_SURFACE) | |
d14a1e28 | 266 | |
51b2943a RD |
267 | GetPixel(self, x,y) |
268 | GetPixelPoint(self, pt) | |
d7403ad2 | 269 | |
51b2943a RD |
270 | DrawLine(self, x1, y1, x2, y2) |
271 | DrawLinePoint(self, pt1, pt2) | |
d14a1e28 | 272 | |
51b2943a RD |
273 | CrossHair(self, x, y) |
274 | CrossHairPoint(self, pt) | |
d14a1e28 | 275 | |
51b2943a RD |
276 | DrawArc(self, x1, y1, x2, y2, xc, yc) |
277 | DrawArcPoint(self, pt1, pt2, centre) | |
d14a1e28 | 278 | |
51b2943a RD |
279 | DrawCheckMark(self, x, y, width, height) |
280 | DrawCheckMarkRect(self, rect) | |
d14a1e28 | 281 | |
51b2943a RD |
282 | DrawEllipticArc(self, x, y, w, h, sa, ea) |
283 | DrawEllipticArcPointSize(self, pt, sz, sa, ea) | |
d14a1e28 | 284 | |
51b2943a RD |
285 | DrawPoint(self, x, y) |
286 | DrawPointPoint(self, pt) | |
d14a1e28 | 287 | |
51b2943a RD |
288 | DrawRectangle(self, x, y, width, height) |
289 | DrawRectangleRect(self, rect) | |
290 | DrawRectanglePointSize(self, pt, sz) | |
d14a1e28 | 291 | |
51b2943a RD |
292 | DrawRoundedRectangle(self, x, y, width, height, radius) |
293 | DrawRoundedRectangleRect(self, r, radius) | |
294 | DrawRoundedRectanglePointSize(self, pt, sz, radius) | |
d14a1e28 | 295 | |
51b2943a RD |
296 | DrawCircle(self, x, y, radius) |
297 | DrawCirclePoint(self, pt, radius) | |
d14a1e28 | 298 | |
51b2943a RD |
299 | DrawEllipse(self, x, y, width, height) |
300 | DrawEllipseRect(self, rect) | |
301 | DrawEllipsePointSize(self, pt, sz) | |
d14a1e28 | 302 | |
51b2943a RD |
303 | DrawIcon(self, icon, x, y) |
304 | DrawIconPoint(self, icon, pt) | |
d14a1e28 | 305 | |
51b2943a RD |
306 | DrawBitmap(self, bmp, x, y, useMask = False) |
307 | DrawBitmapPoint(self, bmp, pt, useMask = False) | |
d14a1e28 | 308 | |
51b2943a RD |
309 | DrawText(self, text, x, y) |
310 | DrawTextPoint(self, text, pt) | |
d14a1e28 | 311 | |
51b2943a RD |
312 | DrawRotatedText(self, text, x, y, angle) |
313 | DrawRotatedTextPoint(self, text, pt, angle) | |
d14a1e28 | 314 | |
51b2943a | 315 | bool Blit(self, xdest, ydest, width, height, sourceDC, xsrc, ysrc, |
d7403ad2 | 316 | rop = wx.COPY, useMask = False, xsrcMask = -1, ysrcMask = -1) |
51b2943a | 317 | BlitPointSize(self, destPt, sz, sourceDC, srcPt, rop = wx.COPY, |
d7403ad2 | 318 | useMask = False, srcPtMask = wxDefaultPosition) |
4da6d35e | 319 | |
d14a1e28 | 320 | |
51b2943a RD |
321 | SetClippingRegion(self, x, y, width, height) |
322 | SetClippingRegionPointSize(self, pt, sz) | |
323 | SetClippingRegionAsRegion(self, region) | |
324 | SetClippingRect(self, rect) | |
d14a1e28 | 325 | |
d14a1e28 RD |
326 | |
327 | ||
328 | ||
329 | ||
330 | Building, Extending and Embedding wxPython | |
331 | ------------------------------------------ | |
332 | ||
333 | wxPython's setup.py script now expects to use existing libraries for | |
334 | the contribs (gizmos, stc, xrc, etc.) rather than building local | |
335 | copies of them. If you build your own copies of wxPython please be | |
336 | aware that you now need to also build the ogl, stc, xrc, and gizmos | |
29bfe46b | 337 | libraries in addition to the main wx lib. |
d14a1e28 RD |
338 | |
339 | The wxPython.h and other header files are now in | |
9ec83f8d RD |
340 | .../wxPython/include/wx/wxPython instead of in wxPython/src. You |
341 | should include it via the "wx/wxPython/wxPython.h" path and add | |
29bfe46b RD |
342 | .../wxPython/include to your list of include paths. On OSX and |
343 | unix-like systems the wxPython headers are installed to the same place | |
9ec83f8d RD |
344 | that the wxWidgets headers are installed, so if you are building |
345 | wxPython compatible extensions on those platforms then your include | |
346 | path should already be set properly. | |
29bfe46b RD |
347 | |
348 | If you are also using SWIG for your extension then you'll need to | |
349 | adapt how the wxPython .i files are imported into your .i files. See | |
350 | the wxPython sources for examples. Your modules will need to at least | |
351 | ``%import core.i``, and possibly others if you need the definition of | |
9ec83f8d RD |
352 | other classes. Since you will need them to build your modules using |
353 | SWIG, the main wxPython .i files are also installed with the wxPython | |
354 | headers in an i_files sibdirectory. It should be enough to pass a | |
355 | -I/pathname on the command line for SWIG to find the files. | |
29bfe46b RD |
356 | |
357 | The bulk of wxPython's setup.py has been moved to another module, | |
358 | wx/build/config.py. This module will be installed as part of wxPython | |
359 | so 3rd party modules that wish to use the same setup/configuration | |
360 | code can do so simply by importing this module from their own setup.py | |
361 | scripts using ``import wx.build.config``. | |
d14a1e28 RD |
362 | |
363 | You no longer need to call wxClassInfo::CleanUpClasses() and | |
364 | wxClassInfo::InitializeClasses() in your extensions or when embedding | |
365 | wxPython. | |
366 | ||
29bfe46b RD |
367 | The usage of wxPyBeginAllowThreads and wxPyEndAllowThreads has changed |
368 | slightly. wxPyBeginAllowThreads now returns a boolean value that must | |
369 | be passed to the coresponding wxPyEndAllowThreads function call. This | |
370 | is to help do the RightThing when calls to these two functions are | |
371 | nested, or if calls to external code in other extension modules that | |
372 | are wrapped in the standard Py_(BEGIN|END)_ALLOW_THERADS may result in | |
373 | wx event handlers being called (such as during the call to | |
374 | os.startfile.) | |
d14a1e28 RD |
375 | |
376 | ||
377 | ||
378 | Two (or Three!) Phase Create | |
379 | ---------------------------- | |
380 | ||
381 | If you use the Precreate/Create method of instantiating a window, (for | |
382 | example, to set an extended style flag, or for XRC handlers) then | |
383 | there is now a new method named PostCreate to help with transplanting | |
384 | the brain of the prewindow instance into the derived window instance. | |
385 | For example:: | |
386 | ||
387 | class MyDialog(wx.Dialog): | |
388 | def __init__(self, parent, ID, title, pos, size, style): | |
389 | pre = wx.PreDialog() | |
390 | pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP) | |
391 | pre.Create(parent, ID, title, pos, size, style) | |
392 | self.PostCreate(pre) | |
393 | ||
394 | ||
395 | ||
396 | Sizers | |
397 | ------ | |
398 | ||
e6a5dac6 | 399 | The hack allowing the old "option" keyword parameter has been removed. |
9ec83f8d | 400 | If you use keyword args with w.xSizer Add, Insert, or Prepend methods |
d7403ad2 RD |
401 | then you will need to use the ``proportion`` name instead of |
402 | ``option``. (The ``proportion`` keyword was also allowed in 2.4.2.4.) | |
d14a1e28 | 403 | |
29bfe46b | 404 | When adding a spacer to a sizer you now need to use a wx.Size or a |
d14a1e28 | 405 | 2-integer sequence instead of separate width and height parameters. |
d7403ad2 RD |
406 | This was optionally allowed in 2.4, but now it is required. This |
407 | allows for more consistency in how you add the various types of items | |
408 | to a sizer. The first parameter defines the item (instead of the | |
409 | possibily first two, depending on if you are doing a spacer or not,) | |
410 | and that item can either be a window, a sizer or a spacer (which can | |
411 | be a sequence or a wx.Size.) Removing the option for separate width | |
412 | and height parameters greatly simplified the wrapper code. | |
d14a1e28 | 413 | |
29bfe46b | 414 | The wx.GridBagSizer class (very similar to the RowColSizer in the |
d14a1e28 RD |
415 | library) has been added to C++ and wrapped for wxPython. It can also |
416 | be used from XRC. | |
417 | ||
418 | You should not use AddWindow, AddSizer, AddSpacer (and similar for | |
419 | Insert, Prepend, and etc.) methods any longer. Just use Add and the | |
d7403ad2 RD |
420 | wrappers will figure out what to do. **[Changed in 2.5.1.6]** |
421 | AddWindow, AddSize, AddSpacer and etc. will now issue a | |
422 | DeprecationWarning. | |
d14a1e28 | 423 | |
95fed4d8 RD |
424 | **[Changed in 2.5.1.6]** wx.ADJUST_MINSIZE is now the default |
425 | behaviour for window items in sizers. This means that the item's | |
ffcb969e RD |
426 | GetMinSize and/or GetBestSize will be called when calculating layout |
427 | and the return value from that will be used for the minimum size. The | |
428 | wx.FIXED_MINSIZE flag was added that will cause the sizer to *not* | |
429 | call window methods to determine the new best size, instead the | |
430 | minsize that the window had when added to the sizer (or the size the | |
431 | window was created with) will always be used. When a window is added | |
432 | to a sizer it's initial size, if any, is set as the window's minimal | |
d7403ad2 RD |
433 | size using SetSizeHints if there isn't already a minimal size. If you |
434 | would like the sizer to use something other than the window's initial | |
435 | size as the minimum then you can give it a new minimum by calling its | |
436 | SetSizeHints method. | |
437 | ||
95fed4d8 | 438 | |
d14a1e28 | 439 | |
dd346b94 RD |
440 | PlatformInfo |
441 | ------------ | |
442 | ||
443 | Added wx.PlatformInfo which is a tuple containing strings that | |
444 | describe the platform and build options of wxPython. This lets you | |
445 | know more about the build than just the __WXPORT__ value that | |
446 | wx.Platform contains, such as if it is a GTK2 build. For example, | |
447 | instead of:: | |
448 | ||
449 | if wx.Platform == "__WXGTK__": | |
450 | ... | |
451 | ||
452 | you should do this:: | |
453 | ||
454 | if "__WXGTK__" in wx.PlatformInfo: | |
455 | ... | |
456 | ||
457 | and you can specifically check for a wxGTK2 build by looking for | |
458 | "gtk2" in wx.PlatformInfo. Unicode builds are also detectable this | |
459 | way. If there are any other platform/toolkit/build flags that make | |
460 | sense to add to this tuple please let me know. | |
461 | ||
462 | BTW, wx.Platform will probably be deprecated in the future. | |
463 | ||
464 | ||
d14a1e28 | 465 | |
b7c75283 RD |
466 | ActiveX |
467 | ------- | |
468 | ||
469 | Lindsay Mathieson's newest wxActiveX_ class has been wrapped into a new | |
470 | extension module called wx.activex. It is very generic and dynamic | |
471 | and should allow hosting of arbitray ActiveX controls within your | |
472 | wxPython apps. So far I've tested it with IE, PDF, and Flash | |
473 | controls, (and there are new samples in the demo and also library | |
474 | modules supporting these.) | |
475 | ||
476 | .. _wxActiveX: http://members.optusnet.com.au/~blackpaw1/wxactivex.html | |
477 | ||
478 | The new wx.activex module contains a bunch of code, but the most | |
479 | important things to look at are ActiveXWindow and ActiveXEvent. | |
480 | ActiveXWindow derives from wxWindow and the constructor accepts a | |
481 | CLSID for the ActiveX Control that should be created. (There is also | |
482 | a CLSID class that can convert from a progID or a CLSID String.) The | |
483 | ActiveXWindow class simply adds methods that allow you to query some | |
484 | of the TypeInfo exposed by the ActiveX object, and also to get/set | |
485 | properties or call methods by name. The Python implementation | |
486 | automatically handles converting parameters and return values to/from | |
487 | the types expected by the ActiveX code as specified by the TypeInfo, | |
488 | (just bool, integers, floating point, strings and None/Empty so far, | |
489 | but more can be handled later.) | |
490 | ||
491 | That's pretty much all there is to the class, as I mentioned before it | |
492 | is very generic and dynamic. Very little is hard-coded and everything | |
493 | that is done with the actual ActiveX control is done at runtime and | |
494 | referenced by property or method name. Since Python is such a dynamic | |
495 | language this is a very good match. I thought for a while about doing | |
496 | some Python black-magic and making the specific methods/properties of | |
497 | the actual ActiveX control "appear" at runtime, but then decided that | |
498 | it would be better and more understandable to do it via subclassing. | |
499 | So there is a utility class in wx.activex that given an existing | |
500 | ActiveXWindow instance can generate a .py module containing a derived | |
501 | class with real methods and properties that do the Right Thing to | |
502 | reflect those calls to the real ActiveX control. There is also a | |
503 | script/tool module named genaxmodule that given a CLSID or progID and | |
504 | a class name, will generate the module for you. There are a few | |
b098694c | 505 | examples of the output of this tool in the wx.lib package, see |
b7c75283 RD |
506 | iewin.py, pdfwin.py and flashwin.py. |
507 | ||
508 | Currently the genaxmodule tool will tweak some of the names it | |
509 | generates, but this can be controled if you would like to do it | |
510 | differently by deriving your own class from GernerateAXModule, | |
511 | overriding some methods and then using this class from a tool like | |
512 | genaxmodule. [TODO: make specifying a new class on genaxmodule's | |
513 | command-line possible.] The current default behavior is that any | |
514 | event names that start with "On" will have the "On" dropped, property | |
515 | names are converted to all lower case, and if any name is a Python | |
516 | keyword it will have an underscore appended to it. GernerateAXModule | |
517 | does it's best when generating the code in the new module, but it can | |
518 | only be as good as the TypeInfo data available from the ActiveX | |
519 | control so sometimes some tweaking will be needed. For example, the | |
520 | IE web browser control defines the Flags parameter of the Navigate2 | |
521 | method as required, but MSDN says it is optional. | |
522 | ||
523 | It is intended that this new wx.activex module will replace both the | |
524 | older version of Lindsay's code available in iewin.IEHtmlWindow, and | |
525 | also the wx.lib.activexwraper module. Probably the biggest | |
b098694c | 526 | differences you'll ecounter in migrating activexwrapper-based code |
b7c75283 RD |
527 | (besides events working better without causing deadlocks) is that |
528 | events are no longer caught by overriding methods in your derived | |
529 | class. Instead ActiveXWindow uses the wx event system and you bind | |
530 | handlers for the ActiveX events exactly the same way you do for any wx | |
531 | event. There is just one extra step needed and that is creating an | |
532 | event ID from the ActiveX event name, and if you use the genaxmodule | |
533 | tool then this extra step will be handled for you there. For example, | |
534 | for the StatusTextChange event in the IE web browser control, this | |
535 | code is generated for you:: | |
536 | ||
537 | wxEVT_StatusTextChange = wx.activex.RegisterActiveXEvent('StatusTextChange') | |
538 | EVT_StatusTextChange = wx.PyEventBinder(wxEVT_StatusTextChange, 1) | |
539 | ||
540 | and you would use it in your code like this:: | |
541 | ||
542 | self.Bind(iewin.EVT_StatusTextChange, self.UpdateStatusText, self.ie) | |
543 | ||
544 | When the event happens and your event handler function is called the | |
545 | event properties from the ActiveX control (if any) are converted to | |
546 | attributes of the event object passed to the handler. (Can you say | |
547 | 'event' any more times in a single sentence? ;-) ) For example the | |
548 | StatusTextChange event will also send the text that should be put into | |
549 | the status line as an event parameter named "Text" and you can access | |
b098694c | 550 | it your handlers as an attribute of the event object like this:: |
b7c75283 RD |
551 | |
552 | def UpdateStatusText(self, evt): | |
553 | self.SetStatusText(evt.Text) | |
554 | ||
b098694c RD |
555 | Usually these event object attributes should be considered read-only, |
556 | but some will be defined by the TypeInfo as output parameters. In | |
557 | those cases if you modify the event object's attribute then that value | |
558 | will be returned to the ActiveX control. For example, to prevent a | |
559 | new window from being opened by the IE web browser control you can do | |
560 | this in the handler for the iewin.EVT_NewWindow2 event:: | |
561 | ||
562 | def OnNewWindow2(self, evt): | |
563 | evt.Cancel = True | |
b7c75283 | 564 | |
29bfe46b | 565 | So how do you know what methods, events and properties that an ActiveX |
b7c75283 RD |
566 | control supports? There is a funciton in wx.activex named GetAXInfo |
567 | that returns a printable summary of the TypeInfo from the ActiveX | |
568 | instance passed in. You can use this as an example of how to browse | |
569 | the TypeInfo provided, and there is also a copy of this function's | |
570 | output appended as a comment to the modules produced by the | |
571 | genaxmodule tool. Beyond that you'll need to consult the docs | |
572 | provided by the makers of the ActiveX control that you are using. | |
573 | ||
574 | ||
575 | ||
26779945 RD |
576 | Obsolete Modules |
577 | ---------------- | |
d14a1e28 RD |
578 | |
579 | Instead of over a dozen separate extension modules linked together | |
580 | into a single extension module, the "core" module is now just a few | |
581 | extensions that are linked independently, and then merged together | |
582 | later into the main namespace via Python code. | |
583 | ||
e6a5dac6 RD |
584 | Because of the above and also because of the way the new SWIG works, |
585 | the "internal" module names have changed, but you shouldn't have been | |
26779945 RD |
586 | using them anyway so it shouldn't bother you. ;-) In case you were |
587 | erroneously using them in 2.4, here are the internal extension modules | |
588 | no longer exist: | |
589 | ||
590 | * clip_dnd | |
591 | * cmndlgs | |
592 | * controls | |
593 | * controls2 | |
594 | * events | |
595 | * filesys | |
596 | * fonts | |
597 | * frames | |
598 | * gdi | |
599 | * image | |
600 | * mdi | |
601 | * misc | |
602 | * misc2 | |
603 | * printfw | |
604 | * sizers | |
605 | * stattool | |
606 | * streams | |
607 | * utils | |
608 | * windows | |
609 | * windows2 | |
610 | * windows3 | |
611 | ||
612 | They have been replaced by the following, but please remember that | |
d7403ad2 RD |
613 | these are just "implementation details" and you should really be using |
614 | the objects in these modules only via the wx or wxPython.wx packages: | |
26779945 RD |
615 | |
616 | * _core | |
617 | * _gdi | |
618 | * _windows | |
619 | * _controls | |
620 | * _misc | |
621 | ||
d14a1e28 | 622 | |
e6a5dac6 RD |
623 | The help module no longer exists and the classes therein are now part |
624 | of the core module imported with wxPython.wx or the wx package. | |
d14a1e28 | 625 | |
26779945 RD |
626 | |
627 | ||
628 | ||
629 | Other Stuff | |
630 | ----------- | |
631 | ||
d14a1e28 RD |
632 | wxPyDefaultPosition and wxPyDefaultSize are gone. Use the |
633 | wxDefaultPosition and wxDefaultSize objects instead. | |
634 | ||
635 | Similarly, the wxSystemSettings backwards compatibiility aliases for | |
636 | GetSystemColour, GetSystemFont and GetSystemMetric have also gone into | |
637 | the bit-bucket. Use GetColour, GetFont and GetMetric instead. | |
638 | ||
c878ceea RD |
639 | Use the Python True/False constants instead of the true, TRUE, false, |
640 | FALSE that used to be provided with wxPython. | |
641 | ||
642 | Use None instead of the ancient and should have been removed a long | |
643 | time ago wx.NULL alias. | |
644 | ||
d7403ad2 RD |
645 | wx.TreeCtrl.GetFirstChild no longer needs to be passed the cookie |
646 | variable as the 2nd parameter. It still returns it though, for use | |
647 | with GetNextChild. | |
d14a1e28 | 648 | |
ed8e1ecb RD |
649 | The wx.NO_FULL_REPAINT_ON_RESIZE style is now the default style for |
650 | all windows. The name still exists for compatibility, but it is set | |
651 | to zero. If you want to disable the setting (so it matches the old | |
652 | default) then you need to use the new wx.FULL_REPAINT_ON_RESIZE style | |
653 | flag otherwise only the freshly exposed areas of the window will be | |
654 | refreshed. | |
d14a1e28 | 655 | |
1f9b31fc RD |
656 | wxPyTypeCast has been removed. Since we've had the OOR (Original |
657 | Object Return) for a couple years now there should be no need to use | |
658 | wxPyTypeCast at all. | |
d14a1e28 | 659 | |
e6a5dac6 RD |
660 | If you use the old wxPython package and wxPython.wx namespace then |
661 | there are compatibility aliases for much of the above items. | |
78862f24 RD |
662 | |
663 | The wxWave class has been renamed to wxSound, and now has a slightly | |
664 | different API. | |
ce32c85b | 665 | |
d7403ad2 RD |
666 | wx.TaskbarIcon works on wxGTK-based platforms (for some window |
667 | managers,) however you have to manage it a little bit more than you | |
668 | did before. Basically, the app will treat it like a top-level frame | |
669 | in that if the wx.TaskBarIcon still exists when all the frames are | |
670 | closed then the app will still not exit. You need to ensure that the | |
671 | wx.TaskBarIcon is destroyed when your last Frame is closed. For | |
672 | wxPython apps it is usually enough if your main frame object holds the | |
673 | only reference to the wx.TaskBarIcon, then when the frame is closed | |
674 | Python reference counting takes care of the rest. | |
45d67f33 | 675 | |
33ab916f RD |
676 | Before Python 2.3 it was possible to pass a floating point object as a |
677 | parameter to a function that expected an integer, and the | |
678 | PyArg_ParseTuple family of functions would automatically convert to | |
679 | integer by truncating the fractional portion of the number. With | |
680 | Python 2.3 that behavior was deprecated and a deprecation warning is | |
681 | raised when you pass a floating point value, (for example, calling | |
d7403ad2 | 682 | wx.DC.DrawLine with floats for the position and size,) and lots of |
33ab916f RD |
683 | developers using wxPython had to scramble to change their code to call |
684 | int() before calling wxPython methods. Recent changes in SWIG have | |
685 | moved the conversion out of PyArg_ParseTuple to custom code that SWIG | |
686 | generates. Since the default conversion fragment was a little too | |
687 | strict and didn't generate a very meaningful exception when it failed, | |
688 | I decided to use a custom fragment instead, and it turned out that | |
689 | it's very easy to allow floats to be converted again just like they | |
690 | used to be. So, in a nutshell, any numeric type that can be | |
691 | converted to an integer is now legal to be passed to SWIG wrapped | |
692 | functions in wxPython for parameters that are expecting an integer. | |
693 | If the object is not already an integer then it will be asked to | |
694 | convert itself to one. A similar conversion fragment is in place for | |
695 | parameters that expect floating point values. | |
c878ceea RD |
696 | |
697 | **[Changed in 2.5.1.6]** The MaskedEditCtrl modules have been moved | |
698 | to their own sub-package, wx.lib.masked. See the docstrings and demo | |
699 | for changes in capabilities, usage, etc. | |
700 | ||
d7403ad2 RD |
701 | **[Changed in 2.5.1.6]** wx.MaskColour constructor has been deprecated |
702 | and will raise a DeprecationWarning if used. The main wx.Mask | |
703 | constructor has been modified to be compatible with wx.MaskColour so | |
704 | you should use it instead. |