@@ -548,7 +548,7 @@ def get_wxcolour(self, color):
548
548
alpha = int (a ))
549
549
550
550
551
- class FigureCanvasWx (FigureCanvasBase , wx .Panel ):
551
+ class _FigureCanvasWxBase (FigureCanvasBase , wx .Panel ):
552
552
"""
553
553
The FigureCanvas contains the figure and does event handling.
554
554
@@ -709,23 +709,11 @@ def draw_idle(self):
709
709
"""
710
710
DEBUG_MSG ("draw_idle()" , 1 , self )
711
711
self ._isDrawn = False # Force redraw
712
-
713
712
# Triggering a paint event is all that is needed to defer drawing
714
713
# until later. The platform will send the event when it thinks it is
715
714
# a good time (usually as soon as there are no other events pending).
716
715
self .Refresh (eraseBackground = False )
717
716
718
- def draw (self , drawDC = None ):
719
- """
720
- Render the figure using RendererWx instance renderer, or using a
721
- previously defined renderer if none is specified.
722
- """
723
- DEBUG_MSG ("draw()" , 1 , self )
724
- self .renderer = RendererWx (self .bitmap , self .figure .dpi )
725
- self .figure .draw (self .renderer )
726
- self ._isDrawn = True
727
- self .gui_repaint (drawDC = drawDC )
728
-
729
717
def new_timer (self , * args , ** kwargs ):
730
718
"""
731
719
Creates a new backend-specific subclass of
@@ -838,94 +826,13 @@ def gui_repaint(self, drawDC=None, origin='WX'):
838
826
filetypes ['xpm' ] = 'X pixmap'
839
827
840
828
def print_figure (self , filename , * args , ** kwargs ):
841
- # Use pure Agg renderer to draw
842
- FigureCanvasBase .print_figure (self , filename , * args , ** kwargs )
843
- # Restore the current view; this is needed because the
844
- # artist contains methods rely on particular attributes
845
- # of the rendered figure for determining things like
846
- # bounding boxes.
847
- if self ._isDrawn :
848
- self .draw ()
849
-
850
- def print_bmp (self , filename , * args , ** kwargs ):
851
- return self ._print_image (filename , wx .BITMAP_TYPE_BMP , * args , ** kwargs )
852
-
853
- if not _has_pil :
854
- def print_jpeg (self , filename , * args , ** kwargs ):
855
- return self ._print_image (filename , wx .BITMAP_TYPE_JPEG ,
856
- * args , ** kwargs )
857
- print_jpg = print_jpeg
858
-
859
- def print_pcx (self , filename , * args , ** kwargs ):
860
- return self ._print_image (filename , wx .BITMAP_TYPE_PCX , * args , ** kwargs )
861
-
862
- def print_png (self , filename , * args , ** kwargs ):
863
- return self ._print_image (filename , wx .BITMAP_TYPE_PNG , * args , ** kwargs )
864
-
865
- if not _has_pil :
866
- def print_tiff (self , filename , * args , ** kwargs ):
867
- return self ._print_image (filename , wx .BITMAP_TYPE_TIF ,
868
- * args , ** kwargs )
869
- print_tif = print_tiff
870
-
871
- def print_xpm (self , filename , * args , ** kwargs ):
872
- return self ._print_image (filename , wx .BITMAP_TYPE_XPM , * args , ** kwargs )
873
-
874
- def _print_image (self , filename , filetype , * args , ** kwargs ):
875
- origBitmap = self .bitmap
876
-
877
- l , b , width , height = self .figure .bbox .bounds
878
- width = int (math .ceil (width ))
879
- height = int (math .ceil (height ))
880
-
881
- self .bitmap = wxc .EmptyBitmap (width , height )
882
-
883
- renderer = RendererWx (self .bitmap , self .figure .dpi )
884
-
885
- gc = renderer .new_gc ()
886
-
887
- self .figure .draw (renderer )
888
-
889
- # image is the object that we call SaveFile on.
890
- image = self .bitmap
891
- # set the JPEG quality appropriately. Unfortunately, it is only
892
- # possible to set the quality on a wx.Image object. So if we
893
- # are saving a JPEG, convert the wx.Bitmap to a wx.Image,
894
- # and set the quality.
895
- if filetype == wx .BITMAP_TYPE_JPEG :
896
- jpeg_quality = kwargs .get ('quality' ,
897
- rcParams ['savefig.jpeg_quality' ])
898
- image = self .bitmap .ConvertToImage ()
899
- image .SetOption (wx .IMAGE_OPTION_QUALITY , str (jpeg_quality ))
900
-
901
- # Now that we have rendered into the bitmap, save it
902
- # to the appropriate file type and clean up
903
- if isinstance (filename , six .string_types ):
904
- if not image .SaveFile (filename , filetype ):
905
- DEBUG_MSG ('print_figure() file save error' , 4 , self )
906
- raise RuntimeError (
907
- 'Could not save figure to %s\n ' %
908
- (filename ))
909
- elif is_writable_file_like (filename ):
910
- if not isinstance (image , wx .Image ):
911
- image = image .ConvertToImage ()
912
- if not image .SaveStream (filename , filetype ):
913
- DEBUG_MSG ('print_figure() file save error' , 4 , self )
914
- raise RuntimeError (
915
- 'Could not save figure to %s\n ' %
916
- (filename ))
917
-
918
- # Restore everything to normal
919
- self .bitmap = origBitmap
920
-
921
- # Note: draw is required here since bits of state about the
922
- # last renderer are strewn about the artist draw methods. Do
923
- # not remove the draw without first verifying that these have
924
- # been cleaned up. The artist contains() methods will fail
925
- # otherwise.
829
+ super (_FigureCanvasWxBase , self ).print_figure (
830
+ filename , * args , ** kwargs )
831
+ # Restore the current view; this is needed because the artist contains
832
+ # methods rely on particular attributes of the rendered figure for
833
+ # determining things like bounding boxes.
926
834
if self ._isDrawn :
927
835
self .draw ()
928
- self .Refresh ()
929
836
930
837
def _onPaint (self , evt ):
931
838
"""
@@ -1007,14 +914,16 @@ def _get_key(self, evt):
1007
914
def _onKeyDown (self , evt ):
1008
915
"""Capture key press."""
1009
916
key = self ._get_key (evt )
1010
- evt .Skip ()
1011
917
FigureCanvasBase .key_press_event (self , key , guiEvent = evt )
918
+ if self :
919
+ evt .Skip ()
1012
920
1013
921
def _onKeyUp (self , evt ):
1014
922
"""Release key."""
1015
923
key = self ._get_key (evt )
1016
- evt .Skip ()
1017
924
FigureCanvasBase .key_release_event (self , key , guiEvent = evt )
925
+ if self :
926
+ evt .Skip ()
1018
927
1019
928
def _set_capture (self , capture = True ):
1020
929
"""control wx mouse capture """
@@ -1152,6 +1061,101 @@ def _onEnter(self, evt):
1152
1061
FigureCanvasBase .enter_notify_event (self , guiEvent = evt )
1153
1062
1154
1063
1064
+ class FigureCanvasWx (_FigureCanvasWxBase ):
1065
+ # Rendering to a Wx canvas using the deprecated Wx renderer.
1066
+
1067
+ def draw (self , drawDC = None ):
1068
+ """
1069
+ Render the figure using RendererWx instance renderer, or using a
1070
+ previously defined renderer if none is specified.
1071
+ """
1072
+ DEBUG_MSG ("draw()" , 1 , self )
1073
+ self .renderer = RendererWx (self .bitmap , self .figure .dpi )
1074
+ self .figure .draw (self .renderer )
1075
+ self ._isDrawn = True
1076
+ self .gui_repaint (drawDC = drawDC )
1077
+
1078
+ def print_bmp (self , filename , * args , ** kwargs ):
1079
+ return self ._print_image (filename , wx .BITMAP_TYPE_BMP , * args , ** kwargs )
1080
+
1081
+ if not _has_pil :
1082
+ def print_jpeg (self , filename , * args , ** kwargs ):
1083
+ return self ._print_image (filename , wx .BITMAP_TYPE_JPEG ,
1084
+ * args , ** kwargs )
1085
+ print_jpg = print_jpeg
1086
+
1087
+ def print_pcx (self , filename , * args , ** kwargs ):
1088
+ return self ._print_image (filename , wx .BITMAP_TYPE_PCX , * args , ** kwargs )
1089
+
1090
+ def print_png (self , filename , * args , ** kwargs ):
1091
+ return self ._print_image (filename , wx .BITMAP_TYPE_PNG , * args , ** kwargs )
1092
+
1093
+ if not _has_pil :
1094
+ def print_tiff (self , filename , * args , ** kwargs ):
1095
+ return self ._print_image (filename , wx .BITMAP_TYPE_TIF ,
1096
+ * args , ** kwargs )
1097
+ print_tif = print_tiff
1098
+
1099
+ def print_xpm (self , filename , * args , ** kwargs ):
1100
+ return self ._print_image (filename , wx .BITMAP_TYPE_XPM , * args , ** kwargs )
1101
+
1102
+ def _print_image (self , filename , filetype , * args , ** kwargs ):
1103
+ origBitmap = self .bitmap
1104
+
1105
+ l , b , width , height = self .figure .bbox .bounds
1106
+ width = int (math .ceil (width ))
1107
+ height = int (math .ceil (height ))
1108
+
1109
+ self .bitmap = wxc .EmptyBitmap (width , height )
1110
+
1111
+ renderer = RendererWx (self .bitmap , self .figure .dpi )
1112
+
1113
+ gc = renderer .new_gc ()
1114
+
1115
+ self .figure .draw (renderer )
1116
+
1117
+ # image is the object that we call SaveFile on.
1118
+ image = self .bitmap
1119
+ # set the JPEG quality appropriately. Unfortunately, it is only
1120
+ # possible to set the quality on a wx.Image object. So if we
1121
+ # are saving a JPEG, convert the wx.Bitmap to a wx.Image,
1122
+ # and set the quality.
1123
+ if filetype == wx .BITMAP_TYPE_JPEG :
1124
+ jpeg_quality = kwargs .get ('quality' ,
1125
+ rcParams ['savefig.jpeg_quality' ])
1126
+ image = self .bitmap .ConvertToImage ()
1127
+ image .SetOption (wx .IMAGE_OPTION_QUALITY , str (jpeg_quality ))
1128
+
1129
+ # Now that we have rendered into the bitmap, save it
1130
+ # to the appropriate file type and clean up
1131
+ if isinstance (filename , six .string_types ):
1132
+ if not image .SaveFile (filename , filetype ):
1133
+ DEBUG_MSG ('print_figure() file save error' , 4 , self )
1134
+ raise RuntimeError (
1135
+ 'Could not save figure to %s\n ' %
1136
+ (filename ))
1137
+ elif is_writable_file_like (filename ):
1138
+ if not isinstance (image , wx .Image ):
1139
+ image = image .ConvertToImage ()
1140
+ if not image .SaveStream (filename , filetype ):
1141
+ DEBUG_MSG ('print_figure() file save error' , 4 , self )
1142
+ raise RuntimeError (
1143
+ 'Could not save figure to %s\n ' %
1144
+ (filename ))
1145
+
1146
+ # Restore everything to normal
1147
+ self .bitmap = origBitmap
1148
+
1149
+ # Note: draw is required here since bits of state about the
1150
+ # last renderer are strewn about the artist draw methods. Do
1151
+ # not remove the draw without first verifying that these have
1152
+ # been cleaned up. The artist contains() methods will fail
1153
+ # otherwise.
1154
+ if self ._isDrawn :
1155
+ self .draw ()
1156
+ self .Refresh ()
1157
+
1158
+
1155
1159
########################################################################
1156
1160
#
1157
1161
# The following functions and classes are for pylab compatibility
@@ -1228,7 +1232,7 @@ def _get_toolbar(self, statbar):
1228
1232
return toolbar
1229
1233
1230
1234
def get_canvas (self , fig ):
1231
- return FigureCanvasWx (self , - 1 , fig )
1235
+ return type ( self . canvas ) (self , - 1 , fig )
1232
1236
1233
1237
def get_figure_manager (self ):
1234
1238
DEBUG_MSG ("get_figure_manager()" , 1 , self )
0 commit comments