File: backend_wx.py

package info (click to toggle)
python-enable 3.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,392 kB
  • ctags: 17,135
  • sloc: cpp: 79,151; python: 29,601; makefile: 2,926; sh: 43
file content (316 lines) | stat: -rwxr-xr-x 10,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# some parts copyright 2002 by Space Telescope Science Institute
# All rights reserved.
# 
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#------------------------------------------------------------------------------

"""
This is the WX backend that draws into a normal WX graphics context using
the Image backend.  It detects the OS type and creates the appropriate
low-level support classes to enable Agg to draw into a WX memory buffer.
"""


# This automatically determines which platform-dependent WX backend should be
# loaded and exports the appropriate Canvas and CanvasWindow.

__all__ = ["GraphicsContext", "Canvas", "CanvasWindow", "CompiledPath",
    "font_metrics_provider"]

# Standard library imports.
import sys
from time import clock as now

# Major library imports.
import wx

# Local imports
from fonttools import Font

WidgetClass = wx.Window


class BaseWxCanvas(object):
    """
    All the WX backends ultimately use the Agg backend to do the actual drawing.
    However, they all differ in how they construct a memory buffer to hand to
    Agg.  This class defines the basic functionality expected of a Kiva Wx backend
    canvas.  Its concrete methods handle some of the interaction with wx, but
    it needs to be mixed in with an actual Wx drawable (e.g Window,
    Frame, GLCanvas, etc.).
    
    Subclasses must implement _create_kiva_gc and blit, and they will most
    likely provide their own __init__ that calls the Wx drawable-specific
    base constructor as well as this class's.
    """
    def __init__(self):
        wx.EVT_PAINT(self, self.OnPaint)
        wx.EVT_SIZE(self, self.OnSize)      
        wx.EVT_ERASE_BACKGROUND(self, self.OnErase)
        self.gc = None
        self.new_gc()

        self.blit_time = 0.01 # a decent guess for first call
        self.total_paint_time = 0.0
        self.clear_color = (1,1,1)
        self._do_draw = 0
        self._win_id = None
        self._dc = None
        return

    def _create_kiva_gc(self, size):
        """
        Returns a new backend-dependent GraphicsContext* instance of the
        given size.
        """
        raise NotImplementedError

    def blit(self, event):
        """
        The actual drawing call.  event is the wxEvent that triggered the
        paint/referesh call that resulted in blit being called.
        """
        raise NotImplementedError

    def size(self):
        return self.GetSizeTuple()    
    
    def paint(self, event):
        tt1 = now()
        if self.dirty:
            self.clear()
            self.draw()
        self.blit(event)
        tt2 = now()
        self.total_paint_time = tt2 - tt1
        return

    def new_gc(self, sz = None):
        if sz is None:
            sz = self.GetClientSizeTuple()
            if (sz[0] <= 0) or (sz[1] <= 0):
                sz = (100, 100)
        t1 = now()
        if self.gc is not None:
            del self.gc
            self.gc = None   # make sure self.gc is defined
        self.gc = self._create_kiva_gc(sz)
        t2 = now()
        self.new_gc_time = t2-t1
        self.dirty = 1
        return

    def clear(self):
        t1 = now()
        self.gc.clear(self.clear_color)
        t2 = now()
        self.clear_time = t2-t1
        return

    def draw(self):
        t1 = now()
        self.do_draw(self.gc)
        t2 = now()
        self.draw_time = t2-t1
        return
        
    def OnErase(self,event):
        pass
    
    def OnSize(self,event):
        # resize buffer bitmap and repaint.
        sz = self.GetClientSizeTuple()
        if sz != (self.gc.width(),self.gc.height()):
            self.new_gc(sz)
        event.Skip()
        return

    def OnPaint(self,event):
        self.paint(event)
        return

    def OnIdle(self, event):
        self.paint(event)
        self._do_draw = 0
        return
        


# Define a different base class depending on the platform.

if sys.platform == 'darwin':
    from mac import get_macport, ABCGI
    from mac.ABCGI import CGBitmapContext, CGImage, CGImageFile, \
        CGLayerContext, CGMutablePath

    # The Mac backend only supports numpy.
    import numpy as np

    if wx.VERSION[:2] == (2, 6):
        def gc_for_dc(dc):
            """ Return the CGContext corresponding to the given wx.DC.
            """
            port = get_macport(dc)
            return ABCGI.CGContextForPort(port)

    elif wx.VERSION[:2] == (2, 8):
        class UnflippingCGContext(ABCGI.CGContextInABox):
            """ Vertically flip the context to undo wx's flipping.
            """

            def __init__(self, *args, **kwds):
                ABCGI.CGContextInABox.__init__(self, *args, **kwds)
                self._begun = False

            def begin(self):
                if self._begun:
                    return
                self.save_state()
                self.translate_ctm(0, self.height())
                self.scale_ctm(1.0, -1.0)
                self._begun = True

            def end(self):
                if self._begun:
                    self.restore_state()
                    self._begun = False

        def gc_for_dc(dc):
            """ Return the CGContext corresponding to the given wx.DC.
            """
            pointer = get_macport(dc)
            gc = UnflippingCGContext(pointer, dc.GetSizeTuple())
            return gc


    CompiledPath = CGMutablePath
    Image = CGImageFile

    class GraphicsContext(CGLayerContext):
        def __init__(self, size_or_array, window_gc=None, *args, **kwds):
            gc = window_gc 
            if not gc:
                # Create a tiny base context to spawn the CGLayerContext from.
                # We are better off making our Layer from the window gc since
                # the data formats will match and so it will be faster to draw the
                # layer.
                gc = CGBitmapContext((1,1))
            if isinstance(size_or_array, np.ndarray):
                # Initialize the layer with an image.
                image = CGImage(size_or_array)
                width = image.width
                height = image.height
            else:
                # No initialization.
                image = None
                width, height = size_or_array
            CGLayerContext.__init__(self, gc,
                (width, height))
            if image is not None:
                self.draw_image(image)

        @classmethod
        def create_from_gc(klass, gc, size_or_array, *args, **kwds):
            return klass(size_or_array, gc, *args, **kwds)

    class Canvas(BaseWxCanvas, WidgetClass):
        """ Mac wx Kiva canvas.
        """
        def __init__(self, parent, id = 01, size = wx.DefaultSize):
            # need to init self.memDC before calling BaseWxCanvas.__init__ 
            self.memDC = wx.MemoryDC()
            self.size = (size.GetWidth(), size.GetHeight())
            WidgetClass.__init__(self, parent, id, wx.Point(0, 0), size, 
                                 wx.SUNKEN_BORDER | wx.WANTS_CHARS | \
                                 wx.FULL_REPAINT_ON_RESIZE )
            BaseWxCanvas.__init__(self)
            return
        
        def _create_kiva_gc(self, size):
            self.size = size
            self.bitmap = wx.EmptyBitmap(size[0], size[1])
            self.memDC.SelectObject(self.bitmap)
            gc = gc_for_dc(self.memDC)
            #gc.begin()
            #print " **** gc is:", gc
            return gc
        
        def blit(self, event):
            t1 = now()
            paintdc = wx.PaintDC(self)
            paintdc.Blit(0, 0, self.size[0], self.size[1],
                         self.memDC, 0, 0)
            t2 = now()
            self.blit_time = t2 - t1
            self.dirty = 0
            return
            
        def draw(self):
            t1 = now()
            self.gc.begin()
            self.do_draw(self.gc)
            self.gc.end()
            t2 = now()
            self.draw_time = t2-t1
            return
        

else:
    # the GraphicsContextSystem stuff should eventually be moved out of the
    # image backend.
    from backend_image import GraphicsContextSystem as GraphicsContext
    from agg import CompiledPath

    class Canvas(BaseWxCanvas, WidgetClass):
        "The basic wx Kiva canvas."
        def __init__(self, parent, id = -1, size = wx.DefaultSize):
            WidgetClass.__init__(self, parent, id, wx.Point(0, 0), size, 
                                 wx.SUNKEN_BORDER | wx.WANTS_CHARS | \
                                 wx.FULL_REPAINT_ON_RESIZE )
            BaseWxCanvas.__init__(self)
            return
    
        def _create_kiva_gc(self, size):
            return GraphicsContext(size)
    
        def blit(self, event):
            t1 = now()
    
            if self._dc is None:
                self._dc = wx.PaintDC(self)
            self.gc.pixel_map.draw_to_wxwindow(self, 0, 0)
            self._dc = None
    
            t2 = now()
            self.blit_time = t2-t1
            self.dirty = 0
            return

def font_metrics_provider():
    gc = GraphicsContext((1, 1))
    gc.set_font(Font())
    return gc

class _CanvasWindow(wx.Frame):
    def __init__(self, id=-1, title='Kiva Canvas', size=(600,800),
                 canvas_class=Canvas):
        parent = None
        wx.Frame.__init__(self, parent, id, title, size=size)
        self.canvas = canvas_class(self)
        self.Show(1)
        return

CanvasWindow = _CanvasWindow
try:
    from numpy import which
    if which[0]!='numpy':
        import gui_thread
        CanvasWindow = gui_thread.register(_CanvasWindow)
except ImportError:
    pass