]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourchooser/canvas.py
3 Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu>
5 This file is part of wxPyColourChooser.
7 This version of wxPyColourChooser is open source; you can redistribute it
8 and/or modify it under the licensed terms.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 from wxPython
.wx
import *
17 class BitmapBuffer(wxMemoryDC
):
18 """A screen buffer class.
20 This class implements a screen output buffer. Data is meant to
21 be drawn in the buffer class and then blitted directly to the
22 output device, or on-screen window.
24 def __init__(self
, width
, height
, colour
):
25 """Initialize the empty buffer object."""
26 wxMemoryDC
.__init
__(self
)
32 self
.bitmap
= wxEmptyBitmap(self
.width
, self
.height
)
33 self
.SelectObject(self
.bitmap
)
35 # Initialize the buffer to the background colour
36 self
.SetBackground(wxBrush(self
.colour
, wxSOLID
))
39 # Make each logical unit of the buffer equal to 1 pixel
40 self
.SetMapMode(wxMM_TEXT
)
43 """Returns the internal bitmap for direct drawing."""
46 class Canvas(wxWindow
):
47 """A canvas class for arbitrary drawing.
49 The Canvas class implements a window that allows for drawing
50 arbitrary graphics. It implements a double buffer scheme and
51 blits the off-screen buffer to the window during paint calls
52 by the windowing system for speed.
54 Some other methods for determining the canvas colour and size
57 def __init__(self
, parent
, id,
58 pos
=wxDefaultPosition
,
60 style
=wxSIMPLE_BORDER
):
61 """Creates a canvas instance and initializes the off-screen
62 buffer. Also sets the handler for rendering the canvas
63 automatically via size and paint calls from the windowing
65 wxWindow
.__init
__(self
, parent
, id, pos
, size
, style
)
67 # Perform an intial sizing
70 # Register event handlers
71 EVT_SIZE(self
, self
.onSize
)
72 EVT_PAINT(self
, self
.onPaint
)
74 def MakeNewBuffer(self
):
75 size
= self
.GetSizeTuple()
76 self
.buffer = BitmapBuffer(size
[0], size
[1],
77 self
.GetBackgroundColour())
79 def onSize(self
, event
):
80 """Perform actual redraw to off-screen buffer only when the
81 size of the canvas has changed. This saves a lot of computation
82 since the same image can be re-used, provided the canvas size
89 """Explicitly tells the canvas to redraw it's contents."""
93 """Re-draws the buffer contents on-screen."""
97 def onPaint(self
, event
):
98 """Renders the off-screen buffer on-screen."""
103 """Performs the blit of the buffer contents on-screen."""
104 width
, height
= self
.buffer.GetSize()
106 dc
.Blit((0, 0), (width
, height
), self
.buffer, (0, 0))
109 def GetBoundingRect(self
):
110 """Returns a tuple that contains the co-ordinates of the
111 top-left and bottom-right corners of the canvas."""
112 x
, y
= self
.GetPositionTuple()
113 w
, h
= self
.GetSize()
114 return(x
, y
+ h
, x
+ w
, y
)
116 def DrawBuffer(self
):
117 """Actual drawing function for drawing into the off-screen
118 buffer. To be overrideen in the implementing class. Do nothing