2003-11-12 16:34:20 -05:00
|
|
|
"""
|
2003-12-22 14:09:54 -05:00
|
|
|
PyColourChooser
|
2003-11-12 16:34:20 -05:00
|
|
|
Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu>
|
2003-07-02 19:13:10 -04:00
|
|
|
|
2003-12-22 14:09:54 -05:00
|
|
|
This file is part of PyColourChooser.
|
2003-07-02 19:13:10 -04:00
|
|
|
|
2003-12-22 14:09:54 -05:00
|
|
|
This version of PyColourChooser is open source; you can redistribute it
|
2003-11-12 16:34:20 -05:00
|
|
|
and/or modify it under the licensed terms.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
"""
|
|
|
|
|
2003-12-16 19:34:40 -05:00
|
|
|
# 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
|
|
|
|
#
|
|
|
|
# o 2.5 compatability update.
|
|
|
|
#
|
2003-12-22 14:09:54 -05:00
|
|
|
# 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
|
|
|
|
#
|
|
|
|
# o wxPyColorChooser -> PyColorChooser
|
|
|
|
# o wxPyColourChooser -> PyColourChooser
|
|
|
|
#
|
2003-11-12 16:34:20 -05:00
|
|
|
|
2003-12-16 19:34:40 -05:00
|
|
|
import wx
|
|
|
|
|
|
|
|
class BitmapBuffer(wx.MemoryDC):
|
2003-11-12 16:34:20 -05:00
|
|
|
"""A screen buffer class.
|
|
|
|
|
|
|
|
This class implements a screen output buffer. Data is meant to
|
|
|
|
be drawn in the buffer class and then blitted directly to the
|
|
|
|
output device, or on-screen window.
|
|
|
|
"""
|
|
|
|
def __init__(self, width, height, colour):
|
|
|
|
"""Initialize the empty buffer object."""
|
2003-12-16 19:34:40 -05:00
|
|
|
wx.MemoryDC.__init__(self)
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.colour = colour
|
|
|
|
|
2003-12-16 19:34:40 -05:00
|
|
|
self.bitmap = wx.EmptyBitmap(self.width, self.height)
|
2003-11-12 16:34:20 -05:00
|
|
|
self.SelectObject(self.bitmap)
|
|
|
|
|
|
|
|
# Initialize the buffer to the background colour
|
2003-12-16 19:34:40 -05:00
|
|
|
self.SetBackground(wx.Brush(self.colour, wx.SOLID))
|
2003-11-12 16:34:20 -05:00
|
|
|
self.Clear()
|
|
|
|
|
|
|
|
# Make each logical unit of the buffer equal to 1 pixel
|
2003-12-16 19:34:40 -05:00
|
|
|
self.SetMapMode(wx.MM_TEXT)
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
def GetBitmap(self):
|
|
|
|
"""Returns the internal bitmap for direct drawing."""
|
|
|
|
return self.bitmap
|
|
|
|
|
2003-12-16 19:34:40 -05:00
|
|
|
class Canvas(wx.Window):
|
2003-11-12 16:34:20 -05:00
|
|
|
"""A canvas class for arbitrary drawing.
|
|
|
|
|
|
|
|
The Canvas class implements a window that allows for drawing
|
|
|
|
arbitrary graphics. It implements a double buffer scheme and
|
|
|
|
blits the off-screen buffer to the window during paint calls
|
|
|
|
by the windowing system for speed.
|
|
|
|
|
|
|
|
Some other methods for determining the canvas colour and size
|
|
|
|
are also provided.
|
|
|
|
"""
|
|
|
|
def __init__(self, parent, id,
|
2003-12-16 19:34:40 -05:00
|
|
|
pos=wx.DefaultPosition,
|
|
|
|
size=wx.DefaultSize,
|
|
|
|
style=wx.SIMPLE_BORDER):
|
2003-11-12 16:34:20 -05:00
|
|
|
"""Creates a canvas instance and initializes the off-screen
|
|
|
|
buffer. Also sets the handler for rendering the canvas
|
|
|
|
automatically via size and paint calls from the windowing
|
|
|
|
system."""
|
2003-12-16 19:34:40 -05:00
|
|
|
wx.Window.__init__(self, parent, id, pos, size, style)
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
# Perform an intial sizing
|
|
|
|
self.ReDraw()
|
|
|
|
|
|
|
|
# Register event handlers
|
2003-12-16 19:34:40 -05:00
|
|
|
self.Bind(wx.EVT_SIZE, self.onSize)
|
|
|
|
self.Bind(wx.EVT_PAINT, self.onPaint)
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
def MakeNewBuffer(self):
|
2003-12-16 19:34:40 -05:00
|
|
|
size = self.GetSize()
|
2003-11-12 16:34:20 -05:00
|
|
|
self.buffer = BitmapBuffer(size[0], size[1],
|
|
|
|
self.GetBackgroundColour())
|
|
|
|
|
|
|
|
def onSize(self, event):
|
|
|
|
"""Perform actual redraw to off-screen buffer only when the
|
|
|
|
size of the canvas has changed. This saves a lot of computation
|
|
|
|
since the same image can be re-used, provided the canvas size
|
|
|
|
hasn't changed."""
|
|
|
|
self.MakeNewBuffer()
|
|
|
|
self.DrawBuffer()
|
|
|
|
self.Refresh()
|
|
|
|
|
|
|
|
def ReDraw(self):
|
|
|
|
"""Explicitly tells the canvas to redraw it's contents."""
|
|
|
|
self.onSize(None)
|
|
|
|
|
|
|
|
def Refresh(self):
|
|
|
|
"""Re-draws the buffer contents on-screen."""
|
2003-12-16 19:34:40 -05:00
|
|
|
dc = wx.ClientDC(self)
|
2003-11-12 16:34:20 -05:00
|
|
|
self.Blit(dc)
|
|
|
|
|
|
|
|
def onPaint(self, event):
|
|
|
|
"""Renders the off-screen buffer on-screen."""
|
2003-12-16 19:34:40 -05:00
|
|
|
dc = wx.PaintDC(self)
|
2003-11-12 16:34:20 -05:00
|
|
|
self.Blit(dc)
|
|
|
|
|
|
|
|
def Blit(self, dc):
|
|
|
|
"""Performs the blit of the buffer contents on-screen."""
|
|
|
|
width, height = self.buffer.GetSize()
|
|
|
|
dc.BeginDrawing()
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.Blit(0, 0, width, height, self.buffer, 0, 0)
|
2003-11-12 16:34:20 -05:00
|
|
|
dc.EndDrawing()
|
|
|
|
|
|
|
|
def GetBoundingRect(self):
|
|
|
|
"""Returns a tuple that contains the co-ordinates of the
|
|
|
|
top-left and bottom-right corners of the canvas."""
|
2003-12-16 19:34:40 -05:00
|
|
|
x, y = self.GetPosition()
|
2003-11-12 16:34:20 -05:00
|
|
|
w, h = self.GetSize()
|
|
|
|
return(x, y + h, x + w, y)
|
|
|
|
|
|
|
|
def DrawBuffer(self):
|
|
|
|
"""Actual drawing function for drawing into the off-screen
|
|
|
|
buffer. To be overrideen in the implementing class. Do nothing
|
|
|
|
by default."""
|
|
|
|
pass
|