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 # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
17 # o 2.5 compatability update.
22 class BitmapBuffer(wx
.MemoryDC
):
23 """A screen buffer class.
25 This class implements a screen output buffer. Data is meant to
26 be drawn in the buffer class and then blitted directly to the
27 output device, or on-screen window.
29 def __init__(self
, width
, height
, colour
):
30 """Initialize the empty buffer object."""
31 wx
.MemoryDC
.__init
__(self
)
37 self
.bitmap
= wx
.EmptyBitmap(self
.width
, self
.height
)
38 self
.SelectObject(self
.bitmap
)
40 # Initialize the buffer to the background colour
41 self
.SetBackground(wx
.Brush(self
.colour
, wx
.SOLID
))
44 # Make each logical unit of the buffer equal to 1 pixel
45 self
.SetMapMode(wx
.MM_TEXT
)
48 """Returns the internal bitmap for direct drawing."""
51 class Canvas(wx
.Window
):
52 """A canvas class for arbitrary drawing.
54 The Canvas class implements a window that allows for drawing
55 arbitrary graphics. It implements a double buffer scheme and
56 blits the off-screen buffer to the window during paint calls
57 by the windowing system for speed.
59 Some other methods for determining the canvas colour and size
62 def __init__(self
, parent
, id,
63 pos
=wx
.DefaultPosition
,
65 style
=wx
.SIMPLE_BORDER
):
66 """Creates a canvas instance and initializes the off-screen
67 buffer. Also sets the handler for rendering the canvas
68 automatically via size and paint calls from the windowing
70 wx
.Window
.__init
__(self
, parent
, id, pos
, size
, style
)
72 # Perform an intial sizing
75 # Register event handlers
76 self
.Bind(wx
.EVT_SIZE
, self
.onSize
)
77 self
.Bind(wx
.EVT_PAINT
, self
.onPaint
)
79 def MakeNewBuffer(self
):
81 self
.buffer = BitmapBuffer(size
[0], size
[1],
82 self
.GetBackgroundColour())
84 def onSize(self
, event
):
85 """Perform actual redraw to off-screen buffer only when the
86 size of the canvas has changed. This saves a lot of computation
87 since the same image can be re-used, provided the canvas size
94 """Explicitly tells the canvas to redraw it's contents."""
98 """Re-draws the buffer contents on-screen."""
99 dc
= wx
.ClientDC(self
)
102 def onPaint(self
, event
):
103 """Renders the off-screen buffer on-screen."""
104 dc
= wx
.PaintDC(self
)
108 """Performs the blit of the buffer contents on-screen."""
109 width
, height
= self
.buffer.GetSize()
111 dc
.Blit((0, 0), (width
, height
), self
.buffer, (0, 0))
114 def GetBoundingRect(self
):
115 """Returns a tuple that contains the co-ordinates of the
116 top-left and bottom-right corners of the canvas."""
117 x
, y
= self
.GetPosition()
118 w
, h
= self
.GetSize()
119 return(x
, y
+ h
, x
+ w
, y
)
121 def DrawBuffer(self
):
122 """Actual drawing function for drawing into the off-screen
123 buffer. To be overrideen in the implementing class. Do nothing