[wxPython-users] BufferedDC for ScrolledWindow
Christopher Barker
Chris.Barker at noaa.gov
Mon Nov 20 10:18:01 PST 2006
Wynand Singels wrote:
> What I want to do now is let the user select a sub image by dragging a =
> rectangle over the displayed image.
this is often called a "rubber band box" if you google for that, you =
should find some discussion about it.
> When the the left button is clicked the OnLeftDown procedure saves the =
> position and captures the mouse to the panel.
> =
> In the OnMotion procedure I check if event.Dragging() and =
> event.LeftIsDown() are still TRUE. If they are we create another =
> buffered context and draw the rectangle.
Why another buffered context? all you need is to draw over the current =
image with a wxClientDC. The trick is to draw your rectangle with the =
LogicalFunction set to XOR. that way, you can draw it a second time, and =
the image will be restored. This is nice, because then all you need to =
do is draw two rectangles as the mouse moves, not blit then entire image =
twice.
I've enclosed the module that does this for wx.lib.floatcanvas -- it's =
specific to floatcanvas, but you should get the idea.
-Chris
-- =
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
"""
Part of the floatcanvas.Utilities package.
This module contains assorted GUI-related utilities that can be used
with FloatCanvas
So far, they are:
RubberBandBox: used to draw a RubberBand Box on the screen
"""
import wx
import FloatCanvas
class RubberBandBox:
"""
Class to provide a rubber band box that can be drawn on a Window
"""
def __init__(self, Canvas, CallBack, Tol=3D5):
"""
To initialize:
=
RubberBandBox(Canvas, CallBack)
Canvas: the FloatCanvas you want the Rubber band box to be used on
CallBack: is the method you want called when the mouse is
released. That method will be called, passing in a rect
parameter, where rect is: (Point, WH) of the rect in
world coords.
Tol: The tolerance for the smallest rectangle allowed. defaults
to 5. In pixels
Methods:
=
Enable() : Enables the Rubber Band Box (Binds the events)
=
Disable() : Enables the Rubber Band Box (Unbinds the events)
Attributes:
CallBack: The callback function, if it's replaced you need to
call Enable() again.
=
"""
self.Canvas =3D Canvas
self.CallBack =3D CallBack
self.Tol =3D Tol
=
self.Drawing =3D False
self.RBRect =3D None
self.StartPointWorld =3D None
return None
def Enable(self):
"""
Called when you want the rubber band box to be enabled
"""
# bind events:
self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove ) =
self.Canvas.Bind(FloatCanvas.EVT_LEFT_DOWN, self.OnLeftDown)
self.Canvas.Bind(FloatCanvas.EVT_LEFT_UP, self.OnLeftUp ) =
def Disable(self):
"""
Called when you don't want the rubber band box to be enabled
"""
# unbind events:
self.Canvas.Unbind(FloatCanvas.EVT_MOTION)
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DOWN)
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_UP)
def OnMove(self, event):
if self.Drawing:
x, y =3D self.StartPoint
Cornerx, Cornery =3D event.GetPosition()
w, h =3D ( Cornerx - x, Cornery - y)
if abs(w) > self.Tol and abs(h) > self.Tol:
# draw the RB box
dc =3D wx.ClientDC(self.Canvas)
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetLogicalFunction(wx.XOR)
if self.RBRect:
dc.DrawRectangle(*self.RBRect)
self.RBRect =3D (x, y, w, h )
dc.DrawRectangle(*self.RBRect)
event.Skip() # skip so that other events can catch these
def OnLeftDown(self, event):
# Start drawing
self.Drawing =3D True
self.StartPoint =3D event.GetPosition()
self.StartPointWorld =3D event.Coords
=
def OnLeftUp(self, event):
# Stop Drawing
if self.Drawing:
self.Drawing =3D False
if self.RBRect:
WH =3D event.Coords - self.StartPointWorld
wx.CallAfter(self.CallBack, (self.StartPointWorld, WH))
self.RBRect =3D None
self.StartPointWorld =3D None
More information about the wxpython-users
mailing list