]>
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 PyColourChooser.
7 This version of PyColourChooser 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.
19 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
21 # o wxPyColorChooser -> PyColorChooser
22 # o wxPyColourChooser -> PyColourChooser
27 class BitmapBuffer(wx
.MemoryDC
):
28 """A screen buffer class.
30 This class implements a screen output buffer. Data is meant to
31 be drawn in the buffer class and then blitted directly to the
32 output device, or on-screen window.
34 def __init__(self
, width
, height
, colour
):
35 """Initialize the empty buffer object."""
36 wx
.MemoryDC
.__init
__(self
)
42 self
.bitmap
= wx
.EmptyBitmap(self
.width
, self
.height
)
43 self
.SelectObject(self
.bitmap
)
45 # Initialize the buffer to the background colour
46 self
.SetBackground(wx
.Brush(self
.colour
, wx
.SOLID
))
49 # Make each logical unit of the buffer equal to 1 pixel
50 self
.SetMapMode(wx
.MM_TEXT
)
53 """Returns the internal bitmap for direct drawing."""
56 class Canvas(wx
.Window
):
57 """A canvas class for arbitrary drawing.
59 The Canvas class implements a window that allows for drawing
60 arbitrary graphics. It implements a double buffer scheme and
61 blits the off-screen buffer to the window during paint calls
62 by the windowing system for speed.
64 Some other methods for determining the canvas colour and size
67 def __init__(self
, parent
, id,
68 pos
=wx
.DefaultPosition
,
70 style
=wx
.SIMPLE_BORDER
):
71 """Creates a canvas instance and initializes the off-screen
72 buffer. Also sets the handler for rendering the canvas
73 automatically via size and paint calls from the windowing
75 wx
.Window
.__init
__(self
, parent
, id, pos
, size
, style
)
77 # Perform an intial sizing
80 # Register event handlers
81 self
.Bind(wx
.EVT_SIZE
, self
.onSize
)
82 self
.Bind(wx
.EVT_PAINT
, self
.onPaint
)
84 def MakeNewBuffer(self
):
86 self
.buffer = BitmapBuffer(size
[0], size
[1],
87 self
.GetBackgroundColour())
89 def onSize(self
, event
):
90 """Perform actual redraw to off-screen buffer only when the
91 size of the canvas has changed. This saves a lot of computation
92 since the same image can be re-used, provided the canvas size
99 """Explicitly tells the canvas to redraw it's contents."""
103 """Re-draws the buffer contents on-screen."""
104 dc
= wx
.ClientDC(self
)
107 def onPaint(self
, event
):
108 """Renders the off-screen buffer on-screen."""
109 dc
= wx
.PaintDC(self
)
113 """Performs the blit of the buffer contents on-screen."""
114 width
, height
= self
.buffer.GetSize()
116 dc
.Blit(0, 0, width
, height
, self
.buffer, 0, 0)
119 def GetBoundingRect(self
):
120 """Returns a tuple that contains the co-ordinates of the
121 top-left and bottom-right corners of the canvas."""
122 x
, y
= self
.GetPosition()
123 w
, h
= self
.GetSize()
124 return(x
, y
+ h
, x
+ w
, y
)
126 def DrawBuffer(self
):
127 """Actual drawing function for drawing into the off-screen
128 buffer. To be overrideen in the implementing class. Do nothing