]>
Commit | Line | Data |
---|---|---|
d14a1e28 | 1 | """ |
d4b73b1b | 2 | PyColourChooser |
d14a1e28 | 3 | Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu> |
1fded56b | 4 | |
d4b73b1b | 5 | This file is part of PyColourChooser. |
1fded56b | 6 | |
d4b73b1b | 7 | This version of PyColourChooser is open source; you can redistribute it |
d14a1e28 RD |
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 | ||
b881fc78 RD |
15 | # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
16 | # | |
17 | # o 2.5 compatability update. | |
18 | # | |
d4b73b1b RD |
19 | # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
20 | # | |
21 | # o wxPyColorChooser -> PyColorChooser | |
22 | # o wxPyColourChooser -> PyColourChooser | |
23 | # | |
d14a1e28 | 24 | |
b881fc78 RD |
25 | import wx |
26 | ||
27 | class BitmapBuffer(wx.MemoryDC): | |
d14a1e28 RD |
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.""" | |
b881fc78 | 36 | wx.MemoryDC.__init__(self) |
d14a1e28 RD |
37 | |
38 | self.width = width | |
39 | self.height = height | |
40 | self.colour = colour | |
41 | ||
b881fc78 | 42 | self.bitmap = wx.EmptyBitmap(self.width, self.height) |
d14a1e28 RD |
43 | self.SelectObject(self.bitmap) |
44 | ||
45 | # Initialize the buffer to the background colour | |
b881fc78 | 46 | self.SetBackground(wx.Brush(self.colour, wx.SOLID)) |
d14a1e28 RD |
47 | self.Clear() |
48 | ||
49 | # Make each logical unit of the buffer equal to 1 pixel | |
b881fc78 | 50 | self.SetMapMode(wx.MM_TEXT) |
d14a1e28 RD |
51 | |
52 | def GetBitmap(self): | |
53 | """Returns the internal bitmap for direct drawing.""" | |
54 | return self.bitmap | |
55 | ||
b881fc78 | 56 | class Canvas(wx.Window): |
d14a1e28 RD |
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, | |
b881fc78 RD |
68 | pos=wx.DefaultPosition, |
69 | size=wx.DefaultSize, | |
70 | style=wx.SIMPLE_BORDER): | |
d14a1e28 RD |
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.""" | |
b881fc78 | 75 | wx.Window.__init__(self, parent, id, pos, size, style) |
d14a1e28 RD |
76 | |
77 | # Perform an intial sizing | |
78 | self.ReDraw() | |
79 | ||
80 | # Register event handlers | |
b881fc78 RD |
81 | self.Bind(wx.EVT_SIZE, self.onSize) |
82 | self.Bind(wx.EVT_PAINT, self.onPaint) | |
d14a1e28 RD |
83 | |
84 | def MakeNewBuffer(self): | |
b881fc78 | 85 | size = self.GetSize() |
d14a1e28 RD |
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.""" | |
b881fc78 | 104 | dc = wx.ClientDC(self) |
d14a1e28 RD |
105 | self.Blit(dc) |
106 | ||
107 | def onPaint(self, event): | |
108 | """Renders the off-screen buffer on-screen.""" | |
b881fc78 | 109 | dc = wx.PaintDC(self) |
d14a1e28 RD |
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() | |
d7403ad2 | 116 | dc.Blit(0, 0, width, height, self.buffer, 0, 0) |
d14a1e28 RD |
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.""" | |
b881fc78 | 122 | x, y = self.GetPosition() |
d14a1e28 RD |
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 |