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.
This commit is contained in:
Robin Dunn
2018-05-31 16:04:53 -07:00
parent 4b1ecf2c25
commit 63841e478d
3 changed files with 45 additions and 12 deletions

View File

@@ -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)

View File

@@ -46,7 +46,8 @@ def run():
module.addHeaderCode('#include <wx/gdicmn.h>')
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():

View File

@@ -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)