sub classing wx.GraphicsContext

Hi,

The code below doesn't work because there is no __init__ for
wx.GraphicsContext.

class BitmapGraphicsContext(wx.GraphicsContext):
    """"""
    def __init__(self, size):
        self.bitmap = wx.EmptyBitmapRGBA(size[0], size[1], 0, 0, 0, 0)
        self.mdc = wx.MemoryDC(self.bitmap)
        wx.GraphicsContext.__init__(self)
        self.Create(self.mdc)

    def getBitmap(self):
        """"""
        return self.bitmap

    def free(self):
        self.mdc.SelectObject(wx.NullBitmap)
        del self.mdc

How do I solve this?
Similarly I would like to create two more classes:

class BitmapMemoryContext(wx.MemoryDC)
&
class BitmapCairoContext(cairo.GraphicsContext)

Prashant
Python 2.6.2
wxPython 2.8.10.1
Xp 32

King wrote:

The code below doesn't work because there is no __init__ for
wx.GraphicsContext.

Yes there is. We'd have to see exactly what your doing, and what errors you get:

http://wiki.wxpython.org/MakingSampleApps

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hi,

The code below doesn't work because there is no __init__ for
wx.GraphicsContext.

class BitmapGraphicsContext(wx.GraphicsContext):
     """"""
     def __init__(self, size):
         self.bitmap = wx.EmptyBitmapRGBA(size[0], size[1], 0, 0, 0, 0)
         self.mdc = wx.MemoryDC(self.bitmap)
         wx.GraphicsContext.__init__(self)
         self.Create(self.mdc)

     def getBitmap(self):
         """"""
         return self.bitmap

     def free(self):
         self.mdc.SelectObject(wx.NullBitmap)
         del self.mdc

You can't subclass the wx.GraphicsContext class in Python because of the way that the classes are designed. The actual context instance you are using is a hidden platform-specifc class that is instantiated and returned by the Create "factory" methods. In other words, wx.GraphicsContext just provides the interface, and the implementation is provided by another class that we can't see from Python, and which is a different class for every platform. The same is true for the Path, Matrix and other wxGC classes.

How do I solve this?

You could do it by making your own Facade class that has a wx.GraphicsContext attribute, sets up the buffer bitmap as needed, and then just delegates all the GC methods to this attribute. In other words, it uses the "has-a" OOP relationship instead of the "is-a" relationship.

Similarly I would like to create two more classes:

class BitmapMemoryContext(wx.MemoryDC)

How is that different than the wx.BufferedDC class that already exists?

···

On 1/13/10 5:23 AM, King wrote:

--
Robin Dunn
Software Craftsman

King wrote:

The code below doesn't work because there is no __init__ for
wx.GraphicsContext.

Yes there is.

Yes, but really No. :wink:

>>> gc = wx.GraphicsContext()
Traceback (most recent call last):
   File "<input>", line 1, in <module>
   File "//usr/local/lib/wxPython-unicode-2.8.10.1/lib/python2.5/site-packages/wx-2.8-mac-unicode/wx/_gdi.py", line 5492, in __init__
     def __init__(self): raise AttributeError, "No constructor defined"
AttributeError: No constructor defined

That's a trick that SWIG uses when there is no C++ constructor and it wants to ensure that the base class's __init__ is not invoked instead.

···

On 1/13/10 9:27 AM, Christopher Barker wrote:

--
Robin Dunn
Software Craftsman