]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourchooser/canvas.py
Added FindById
[wxWidgets.git] / wxPython / wx / lib / colourchooser / canvas.py
1 """
2 wxPyColourChooser
3 Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu>
4
5 This file is part of wxPyColourChooser.
6
7 This version of wxPyColourChooser is open source; you can redistribute it
8 and/or modify it under the licensed terms.
9
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.
13 """
14
15 # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
16 #
17 # o 2.5 compatability update.
18 #
19
20 import wx
21
22 class BitmapBuffer(wx.MemoryDC):
23 """A screen buffer class.
24
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.
28 """
29 def __init__(self, width, height, colour):
30 """Initialize the empty buffer object."""
31 wx.MemoryDC.__init__(self)
32
33 self.width = width
34 self.height = height
35 self.colour = colour
36
37 self.bitmap = wx.EmptyBitmap(self.width, self.height)
38 self.SelectObject(self.bitmap)
39
40 # Initialize the buffer to the background colour
41 self.SetBackground(wx.Brush(self.colour, wx.SOLID))
42 self.Clear()
43
44 # Make each logical unit of the buffer equal to 1 pixel
45 self.SetMapMode(wx.MM_TEXT)
46
47 def GetBitmap(self):
48 """Returns the internal bitmap for direct drawing."""
49 return self.bitmap
50
51 class Canvas(wx.Window):
52 """A canvas class for arbitrary drawing.
53
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.
58
59 Some other methods for determining the canvas colour and size
60 are also provided.
61 """
62 def __init__(self, parent, id,
63 pos=wx.DefaultPosition,
64 size=wx.DefaultSize,
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
69 system."""
70 wx.Window.__init__(self, parent, id, pos, size, style)
71
72 # Perform an intial sizing
73 self.ReDraw()
74
75 # Register event handlers
76 self.Bind(wx.EVT_SIZE, self.onSize)
77 self.Bind(wx.EVT_PAINT, self.onPaint)
78
79 def MakeNewBuffer(self):
80 size = self.GetSize()
81 self.buffer = BitmapBuffer(size[0], size[1],
82 self.GetBackgroundColour())
83
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
88 hasn't changed."""
89 self.MakeNewBuffer()
90 self.DrawBuffer()
91 self.Refresh()
92
93 def ReDraw(self):
94 """Explicitly tells the canvas to redraw it's contents."""
95 self.onSize(None)
96
97 def Refresh(self):
98 """Re-draws the buffer contents on-screen."""
99 dc = wx.ClientDC(self)
100 self.Blit(dc)
101
102 def onPaint(self, event):
103 """Renders the off-screen buffer on-screen."""
104 dc = wx.PaintDC(self)
105 self.Blit(dc)
106
107 def Blit(self, dc):
108 """Performs the blit of the buffer contents on-screen."""
109 width, height = self.buffer.GetSize()
110 dc.BeginDrawing()
111 dc.Blit((0, 0), (width, height), self.buffer, (0, 0))
112 dc.EndDrawing()
113
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)
120
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
124 by default."""
125 pass