@@ -80,15 +80,15 @@ def blit(self, bbox=None):
8080 x = int (l )
8181 y = int (self .bitmap .GetHeight () - t )
8282
83- srcBmp = _convert_agg_to_wx_bitmap (self .get_renderer (), bbox )
83+ srcBmp = _convert_agg_to_wx_bitmap (self .get_renderer (), None )
8484 srcDC = wx .MemoryDC ()
8585 srcDC .SelectObject (srcBmp )
8686
8787 destDC = wx .MemoryDC ()
8888 destDC .SelectObject (self .bitmap )
8989
9090 destDC .BeginDrawing ()
91- destDC .Blit (x , y , int (w ), int (h ), srcDC , 0 , 0 )
91+ destDC .Blit (x , y , int (w ), int (h ), srcDC , x , y )
9292 destDC .EndDrawing ()
9393
9494 destDC .SelectObject (wx .NullBitmap )
@@ -145,7 +145,7 @@ def new_figure_manager(num, *args, **kwargs):
145145
146146
147147#
148- # agg/wxPython image conversion functions
148+ # agg/wxPython image conversion functions (wxPython <= 2.6)
149149#
150150
151151def _py_convert_agg_to_wx_image (agg , bbox ):
@@ -185,7 +185,7 @@ def _py_convert_agg_to_wx_bitmap(agg, bbox):
185185
186186def _clipped_image_as_bitmap (image , bbox ):
187187 """
188- Convert the region of a wx.Image described by bbox to a wx.Bitmap.
188+ Convert the region of a wx.Image bounded by bbox to a wx.Bitmap.
189189 """
190190 l , b , width , height = bbox .get_bounds ()
191191 r = l + width
@@ -211,19 +211,94 @@ def _clipped_image_as_bitmap(image, bbox):
211211 return destBmp
212212
213213
214+ #
215+ # agg/wxPython image conversion functions (wxPython >= 2.8)
216+ #
217+
218+ def _py_WX28_convert_agg_to_wx_image (agg , bbox ):
219+ """
220+ Convert the region of the agg buffer bounded by bbox to a wx.Image. If
221+ bbox is None, the entire buffer is converted.
222+
223+ Note: agg must be a backend_agg.RendererAgg instance.
224+ """
225+ if bbox is None :
226+ # agg => rgb -> image
227+ image = wx .EmptyImage (int (agg .width ), int (agg .height ))
228+ image .SetData (agg .tostring_rgb ())
229+ return image
230+ else :
231+ # agg => rgba buffer -> bitmap => clipped bitmap => image
232+ return wx .ImageFromBitmap (_WX28_clipped_agg_as_bitmap (agg , bbox ))
233+
234+
235+ def _py_WX28_convert_agg_to_wx_bitmap (agg , bbox ):
236+ """
237+ Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If
238+ bbox is None, the entire buffer is converted.
239+
240+ Note: agg must be a backend_agg.RendererAgg instance.
241+ """
242+ if bbox is None :
243+ # agg => rgba buffer -> bitmap
244+ return wx .BitmapFromBufferRGBA (int (agg .width ), int (agg .height ),
245+ agg .buffer_rgba (0 , 0 ))
246+ else :
247+ # agg => rgba buffer -> bitmap => clipped bitmap
248+ return _WX28_clipped_agg_as_bitmap (agg , bbox )
249+
250+
251+ def _WX28_clipped_agg_as_bitmap (agg , bbox ):
252+ """
253+ Convert the region of a the agg buffer bounded by bbox to a wx.Bitmap.
254+
255+ Note: agg must be a backend_agg.RendererAgg instance.
256+ """
257+ l , b , width , height = bbox .get_bounds ()
258+ r = l + width
259+ t = b + height
260+
261+ srcBmp = wx .BitmapFromBufferRGBA (int (agg .width ), int (agg .height ),
262+ agg .buffer_rgba (0 , 0 ))
263+ srcDC = wx .MemoryDC ()
264+ srcDC .SelectObject (srcBmp )
265+
266+ destBmp = wx .EmptyBitmap (int (width ), int (height ))
267+ destDC = wx .MemoryDC ()
268+ destDC .SelectObject (destBmp )
269+
270+ destDC .BeginDrawing ()
271+ x = int (l )
272+ y = int (int (agg .height ) - t )
273+ destDC .Blit (0 , 0 , int (width ), int (height ), srcDC , x , y )
274+ destDC .EndDrawing ()
275+
276+ srcDC .SelectObject (wx .NullBitmap )
277+ destDC .SelectObject (wx .NullBitmap )
278+
279+ return destBmp
280+
281+
214282def _use_accelerator (state ):
215283 """
216- Enable or disable the WXAgg accelerator, if it is present.
284+ Enable or disable the WXAgg accelerator, if it is present and is also
285+ compatible with whatever version of wxPython is in use.
217286 """
218287 global _convert_agg_to_wx_image
219288 global _convert_agg_to_wx_bitmap
220289
221- if state and _wxagg is not None :
222- _convert_agg_to_wx_image = _wxagg .convert_agg_to_wx_image
223- _convert_agg_to_wx_bitmap = _wxagg .convert_agg_to_wx_bitmap
290+ if getattr (wx , '__version__' , '0.0' )[0 :3 ] < '2.8' :
291+ # wxPython < 2.8, so use the C++ accelerator or the Python routines
292+ if state and _wxagg is not None :
293+ _convert_agg_to_wx_image = _wxagg .convert_agg_to_wx_image
294+ _convert_agg_to_wx_bitmap = _wxagg .convert_agg_to_wx_bitmap
295+ else :
296+ _convert_agg_to_wx_image = _py_convert_agg_to_wx_image
297+ _convert_agg_to_wx_bitmap = _py_convert_agg_to_wx_bitmap
224298 else :
225- _convert_agg_to_wx_image = _py_convert_agg_to_wx_image
226- _convert_agg_to_wx_bitmap = _py_convert_agg_to_wx_bitmap
299+ # wxPython >= 2.8, so use the accelerated Python routines
300+ _convert_agg_to_wx_image = _py_WX28_convert_agg_to_wx_image
301+ _convert_agg_to_wx_bitmap = _py_WX28_convert_agg_to_wx_bitmap
227302
228303
229304# try to load the WXAgg accelerator
@@ -234,4 +309,3 @@ def _use_accelerator(state):
234309
235310# if it's present, use it
236311_use_accelerator (True )
237-
0 commit comments