]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourchooser/canvas.py
wx.StaticBox and siblings creation order, and other fixes from Kevin Ollivier
[wxWidgets.git] / wxPython / wx / lib / colourchooser / canvas.py
1 """
2 PyColourChooser
3 Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu>
4
5 This file is part of PyColourChooser.
6
7 This version of PyColourChooser 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 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
20 #
21 # o wxPyColorChooser -> PyColorChooser
22 # o wxPyColourChooser -> PyColourChooser
23 #
24
25 import wx
26
27 class BitmapBuffer(wx.MemoryDC):
28 """A screen buffer class.
29
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.
33 """
34 def __init__(self, width, height, colour):
35 """Initialize the empty buffer object."""
36 wx.MemoryDC.__init__(self)
37
38 self.width = width
39 self.height = height
40 self.colour = colour
41
42 self.bitmap = wx.EmptyBitmap(self.width, self.height)
43 self.SelectObject(self.bitmap)
44
45 # Initialize the buffer to the background colour
46 self.SetBackground(wx.Brush(self.colour, wx.SOLID))
47 self.Clear()
48
49 # Make each logical unit of the buffer equal to 1 pixel
50 self.SetMapMode(wx.MM_TEXT)
51
52 def GetBitmap(self):
53 """Returns the internal bitmap for direct drawing."""
54 return self.bitmap
55
56 class Canvas(wx.Window):
57 """A canvas class for arbitrary drawing.
58
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.
63
64 Some other methods for determining the canvas colour and size
65 are also provided.
66 """
67 def __init__(self, parent, id,
68 pos=wx.DefaultPosition,
69 size=wx.DefaultSize,
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
74 system."""
75 wx.Window.__init__(self, parent, id, pos, size, style)
76
77 # Perform an intial sizing
78 self.ReDraw()
79
80 # Register event handlers
81 self.Bind(wx.EVT_SIZE, self.onSize)
82 self.Bind(wx.EVT_PAINT, self.onPaint)
83
84 def MakeNewBuffer(self):
85 size = self.GetSize()
86 self.buffer = BitmapBuffer(size[0], size[1],
87 self.GetBackgroundColour())
88
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
93 hasn't changed."""
94 self.MakeNewBuffer()
95 self.DrawBuffer()
96 self.Refresh()
97
98 def ReDraw(self):
99 """Explicitly tells the canvas to redraw it's contents."""
100 self.onSize(None)
101
102 def Refresh(self):
103 """Re-draws the buffer contents on-screen."""
104 dc = wx.ClientDC(self)
105 self.Blit(dc)
106
107 def onPaint(self, event):
108 """Renders the off-screen buffer on-screen."""
109 dc = wx.PaintDC(self)
110 self.Blit(dc)
111
112 def Blit(self, dc):
113 """Performs the blit of the buffer contents on-screen."""
114 width, height = self.buffer.GetSize()
115 dc.BeginDrawing()
116 dc.Blit((0, 0), (width, height), self.buffer, (0, 0))
117 dc.EndDrawing()
118
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)
125
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
129 by default."""
130 pass