Fix exception when canvas becomes null because of window resize, and avoid useless parameter+condition in GetLogicalDC wxPython4
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Sun, 05 Sep 2021 05:01:34 +0200
branchwxPython4
changeset 3304 3d736248ab3b
parent 3303 0ffb41625592
child 3305 ca11385ea5e9
Fix exception when canvas becomes null because of window resize, and avoid useless parameter+condition in GetLogicalDC
editors/Viewer.py
--- a/editors/Viewer.py	Thu Sep 02 22:18:14 2021 +0200
+++ b/editors/Viewer.py	Sun Sep 05 05:01:34 2021 +0200
@@ -825,15 +825,14 @@
     def GetViewScale(self):
         return self.ViewScale
 
-    def GetLogicalDC(self, buffered=False):
-        if buffered:
-            bitmap = wx.Bitmap(*self.Editor.GetClientSize())
-            dc = wx.MemoryDC(bitmap)
-        else:
-            dc = wx.ClientDC(self.Editor)
+    def PrepareDC(self, dc):
         dc.SetFont(self.GetFont())
         self.Editor.DoPrepareDC(dc)
         dc.SetUserScale(self.ViewScale[0], self.ViewScale[1])
+
+    def GetLogicalDC(self):
+        dc = wx.ClientDC(self.Editor)
+        self.PrepareDC(dc)
         return dc
 
     def RefreshRect(self, rect, eraseBackground=True):
@@ -3681,9 +3680,13 @@
                 self.rubberBand.Draw(dc)
 
     def OnPaint(self, event):
-        dc = self.GetLogicalDC(True)
+        event.Skip()
+        sx,sy = self.Editor.GetClientSize()
+        if sx <= 0 or sy <= 0 :
+            return
+        dc = wx.MemoryDC(wx.Bitmap(sx,sy))
+        self.PrepareDC(dc)
         self.DoDrawing(dc)
         wx.BufferedPaintDC(self.Editor, dc.GetAsBitmap())
         if self.Debug:
             DebugViewer.RefreshNewData(self)
-        event.Skip()