From 63841e478dd4c30eddd46246ea1b5761c9ca83ec Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Thu, 31 May 2018 16:04:53 -0700 Subject: [PATCH] Add a wx.GraphicsContext.Create overload for wx.AutoBufferedPaintDC, and let the C++ compiler sort out whether it is really a wxPaintDC or a wxBufferedPaintDC. --- CHANGES.rst | 2 ++ etg/graphics.py | 34 ++++++++++++++++++++++------------ unittests/test_graphics.py | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a503e315..e6f5e91e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -109,6 +109,8 @@ Changes in this release include the following: * Override SetForegroundColour and SetBackgroundColour in MaskedEditMixin (#808) +* Add an explicit wx.GraphicsContext.Create overload for wx.AutoBufferedPaintDC. (#783) + diff --git a/etg/graphics.py b/etg/graphics.py index 92db5eb0..c5b69a52 100644 --- a/etg/graphics.py +++ b/etg/graphics.py @@ -46,7 +46,8 @@ def run(): module.addHeaderCode('#include ') - def markFactories(klass): + def markCreateFactories(klass): + """Mark all Create methods as factories""" for func in klass.allItems(): if isinstance(func, etgtools.FunctionDef) \ and func.name.startswith('Create') \ @@ -64,20 +65,16 @@ def run(): #--------------------------------------------- c = module.find('wxGraphicsContext') assert isinstance(c, etgtools.ClassDef) - markFactories(c) tools.removeVirtuals(c) c.abstract = True c.mustHaveApp() - - # Ensure that the target DC or image lives as long as the GC does. NOTE: - # Since the Creates are static methods there is no self to associate the - # extra reference with, but since they are factories then that extra - # reference will be held by the return value of the factory instead. - for m in c.find('Create').all(): - for p in m.items: - if 'DC' in p.name or p.name == 'image': - p.keepReference = True + c.addCppMethod('wxGraphicsContext*', 'Create', '(wxAutoBufferedPaintDC* autoPaintDC /KeepReference/)', + pyArgsString='(autoPaintDC) -> GraphicsContext', + isStatic=True, + body="""\ + return wxGraphicsContext::Create(autoPaintDC); + """) m = c.find('Create').findOverload('wxEnhMetaFileDC') m.find('metaFileDC').type = 'const wxMetafileDC&' @@ -92,6 +89,19 @@ def run(): return NULL; """) + markCreateFactories(c) + + # Ensure that the target DC or image passed to Create lives as long as the + # GC does. NOTE: Since the Creates are static methods there is no self to + # associate the extra reference with, but since they are factories then + # that extra reference will be held by the return value of the factory + # instead. + for m in c.find('Create').all(): + for p in m.items: + if 'DC' in p.name or p.name == 'image': + p.keepReference = True + + c.find('GetSize.width').out = True c.find('GetSize.height').out = True c.find('GetDPI.dpiX').out = True @@ -190,7 +200,7 @@ def run(): #--------------------------------------------- c = module.find('wxGraphicsRenderer') tools.removeVirtuals(c) - markFactories(c) + markCreateFactories(c) c.abstract = True for m in c.find('CreateContext').all(): diff --git a/unittests/test_graphics.py b/unittests/test_graphics.py index 26cb520d..fefe8ff4 100644 --- a/unittests/test_graphics.py +++ b/unittests/test_graphics.py @@ -23,6 +23,27 @@ class graphics_Tests(wtc.WidgetTestCase): gc = wx.GraphicsContext.Create(img) self.assertTrue(gc.IsOk()) + def test_gcCreate4(self): + class MyPanel(wx.Panel): + def __init__(self, parent): + super(MyPanel, self).__init__(parent) + self.SetBackgroundStyle(wx.BG_STYLE_PAINT) + self.Bind(wx.EVT_PAINT, self.OnPaint) + self.painted = False + self.gcIsOk = False + + def OnPaint(self, evt): + dc = wx.AutoBufferedPaintDC(self) + gc = wx.GraphicsContext.Create(dc) + self.gcIsOk = gc.IsOk() + self.painted = True + + panel = MyPanel(self.frame) + self.myUpdate(panel) + self.assertTrue(panel.painted) + self.assertTrue(panel.gcIsOk) + + def test_gcCreateBitmap(self): self.waitFor(50) gc = wx.GraphicsContext.Create(self.frame)