]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | """ |
2 | wxPyColourChooser | |
3 | Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu> | |
1fded56b | 4 | |
d14a1e28 | 5 | This file is part of wxPyColourChooser. |
1fded56b | 6 | |
d14a1e28 RD |
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 | ||
b881fc78 RD |
15 | # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
16 | # | |
17 | # o 2.5 compatability update. | |
18 | # | |
d14a1e28 | 19 | |
b881fc78 RD |
20 | import wx |
21 | ||
22 | class BitmapBuffer(wx.MemoryDC): | |
d14a1e28 RD |
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.""" | |
b881fc78 | 31 | wx.MemoryDC.__init__(self) |
d14a1e28 RD |
32 | |
33 | self.width = width | |
34 | self.height = height | |
35 | self.colour = colour | |
36 | ||
b881fc78 | 37 | self.bitmap = wx.EmptyBitmap(self.width, self.height) |
d14a1e28 RD |
38 | self.SelectObject(self.bitmap) |
39 | ||
40 | # Initialize the buffer to the background colour | |
b881fc78 | 41 | self.SetBackground(wx.Brush(self.colour, wx.SOLID)) |
d14a1e28 RD |
42 | self.Clear() |
43 | ||
44 | # Make each logical unit of the buffer equal to 1 pixel | |
b881fc78 | 45 | self.SetMapMode(wx.MM_TEXT) |
d14a1e28 RD |
46 | |
47 | def GetBitmap(self): | |
48 | """Returns the internal bitmap for direct drawing.""" | |
49 | return self.bitmap | |
50 | ||
b881fc78 | 51 | class Canvas(wx.Window): |
d14a1e28 RD |
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, | |
b881fc78 RD |
63 | pos=wx.DefaultPosition, |
64 | size=wx.DefaultSize, | |
65 | style=wx.SIMPLE_BORDER): | |
d14a1e28 RD |
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.""" | |
b881fc78 | 70 | wx.Window.__init__(self, parent, id, pos, size, style) |
d14a1e28 RD |
71 | |
72 | # Perform an intial sizing | |
73 | self.ReDraw() | |
74 | ||
75 | # Register event handlers | |
b881fc78 RD |
76 | self.Bind(wx.EVT_SIZE, self.onSize) |
77 | self.Bind(wx.EVT_PAINT, self.onPaint) | |
d14a1e28 RD |
78 | |
79 | def MakeNewBuffer(self): | |
b881fc78 | 80 | size = self.GetSize() |
d14a1e28 RD |
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.""" | |
b881fc78 | 99 | dc = wx.ClientDC(self) |
d14a1e28 RD |
100 | self.Blit(dc) |
101 | ||
102 | def onPaint(self, event): | |
103 | """Renders the off-screen buffer on-screen.""" | |
b881fc78 | 104 | dc = wx.PaintDC(self) |
d14a1e28 RD |
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() | |
7722248d | 111 | dc.Blit((0, 0), (width, height), self.buffer, (0, 0)) |
d14a1e28 RD |
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.""" | |
b881fc78 | 117 | x, y = self.GetPosition() |
d14a1e28 RD |
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 |