]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/colourchooser/canvas.py
Added wxAutoNSAutoreleasePool to Create(Tool|Status)Bar
[wxWidgets.git] / wxPython / wx / lib / colourchooser / canvas.py
CommitLineData
d14a1e28
RD
1"""
2wxPyColourChooser
3Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu>
1fded56b 4
d14a1e28 5This file is part of wxPyColourChooser.
1fded56b 6
d14a1e28
RD
7This version of wxPyColourChooser is open source; you can redistribute it
8and/or modify it under the licensed terms.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13"""
14
15from wxPython.wx import *
16
17class BitmapBuffer(wxMemoryDC):
18 """A screen buffer class.
19
20 This class implements a screen output buffer. Data is meant to
21 be drawn in the buffer class and then blitted directly to the
22 output device, or on-screen window.
23 """
24 def __init__(self, width, height, colour):
25 """Initialize the empty buffer object."""
26 wxMemoryDC.__init__(self)
27
28 self.width = width
29 self.height = height
30 self.colour = colour
31
32 self.bitmap = wxEmptyBitmap(self.width, self.height)
33 self.SelectObject(self.bitmap)
34
35 # Initialize the buffer to the background colour
36 self.SetBackground(wxBrush(self.colour, wxSOLID))
37 self.Clear()
38
39 # Make each logical unit of the buffer equal to 1 pixel
40 self.SetMapMode(wxMM_TEXT)
41
42 def GetBitmap(self):
43 """Returns the internal bitmap for direct drawing."""
44 return self.bitmap
45
46class Canvas(wxWindow):
47 """A canvas class for arbitrary drawing.
48
49 The Canvas class implements a window that allows for drawing
50 arbitrary graphics. It implements a double buffer scheme and
51 blits the off-screen buffer to the window during paint calls
52 by the windowing system for speed.
53
54 Some other methods for determining the canvas colour and size
55 are also provided.
56 """
57 def __init__(self, parent, id,
58 pos=wxDefaultPosition,
59 size=wxDefaultSize,
60 style=wxSIMPLE_BORDER):
61 """Creates a canvas instance and initializes the off-screen
62 buffer. Also sets the handler for rendering the canvas
63 automatically via size and paint calls from the windowing
64 system."""
65 wxWindow.__init__(self, parent, id, pos, size, style)
66
67 # Perform an intial sizing
68 self.ReDraw()
69
70 # Register event handlers
71 EVT_SIZE(self, self.onSize)
72 EVT_PAINT(self, self.onPaint)
73
74 def MakeNewBuffer(self):
75 size = self.GetSizeTuple()
76 self.buffer = BitmapBuffer(size[0], size[1],
77 self.GetBackgroundColour())
78
79 def onSize(self, event):
80 """Perform actual redraw to off-screen buffer only when the
81 size of the canvas has changed. This saves a lot of computation
82 since the same image can be re-used, provided the canvas size
83 hasn't changed."""
84 self.MakeNewBuffer()
85 self.DrawBuffer()
86 self.Refresh()
87
88 def ReDraw(self):
89 """Explicitly tells the canvas to redraw it's contents."""
90 self.onSize(None)
91
92 def Refresh(self):
93 """Re-draws the buffer contents on-screen."""
94 dc = wxClientDC(self)
95 self.Blit(dc)
96
97 def onPaint(self, event):
98 """Renders the off-screen buffer on-screen."""
99 dc = wxPaintDC(self)
100 self.Blit(dc)
101
102 def Blit(self, dc):
103 """Performs the blit of the buffer contents on-screen."""
104 width, height = self.buffer.GetSize()
105 dc.BeginDrawing()
7722248d 106 dc.Blit((0, 0), (width, height), self.buffer, (0, 0))
d14a1e28
RD
107 dc.EndDrawing()
108
109 def GetBoundingRect(self):
110 """Returns a tuple that contains the co-ordinates of the
111 top-left and bottom-right corners of the canvas."""
112 x, y = self.GetPositionTuple()
113 w, h = self.GetSize()
114 return(x, y + h, x + w, y)
115
116 def DrawBuffer(self):
117 """Actual drawing function for drawing into the off-screen
118 buffer. To be overrideen in the implementing class. Do nothing
119 by default."""
120 pass