Because of its built-in drawing methods, the Canvas control is often
used to
create custom interface controls. But while the Canvas control has event
handlers for most mouse events, it doesn't have an event handler for
DoubleClick events. Fortunately, you can add a double-click event
handler to
a Canvas control easily. Basically, you're going to create a new class
based
on Canvas and add a double-click event to that. You can then use the new
class anytime you need a Canvas with a double-click event.
To create a new Canvas class with a DoubleClick event handler, do this:
1. Add a new class to your project.
2. Set the Super property of the new class to "Canvas".
3. Change the name of this new class to "DoubleClickCanvas".
A double-click occurs when two clicks occur within the users
double-click
time (set in the Mouse control panel on both Macintosh and Windows) and
within five pixels of each other. So, you'll need a few properties to
store
when and where the last click occurred.
4. Add a new property with the following declaration and mark it as
private:
lastClickTicks as Integer
5. Add a new property with the following declaration and mark it as
private:
lastClickX as Integer
6. Add a new property with the following declaration and mark it as
private:
lastClickY as Integer
Since the Canvas control doesn't have a DoubleClick event, you will
need to
add one.
7. Add a new event to your class by choosing New Event from the Edit
menu
and enter "DoubleClick" as the event name.
Double-clicks occur on MouseUp. In order for the mouseUp event to fire,
you
must return True in the MouseDown event.
8. In the MouseDown event, add the following code:
Return True
In the MouseUp event, you will need to determine what the users
double-click
time is. This value is represented on both the Mac and Windows in
ticks. A
tick is 1/60th of a second. Since there isn't a built-in function for
this,
you'll need to make a toolbox call. The mouseUp event code below makes
the
appropriate toolbox call for both Macintosh and Windows. It then
compares
the time of the users last click to the time of the current click and
compares the location of the users last click to the location of the
current
click.
9. Add the following code to the MouseUp event:
dim doubleClickTime, currentClickTicks as Integer
#if targetMacOS then
Declare Function GetDblTime Lib "InterfaceLib" () as Integer
Inline68K("2EB802F0")
doubleClickTime = GetDblTime()
#endif
#if targetWin32 then
Declare Function GetDoubleClickTime Lib "User32.DLL" () as Integer
doubleClickTime = GetDoubleClickTime()
#endif
currentClickTicks = ticks
//if the two clicks happened close enough together in time
if (currentClickTicks - lastClickTicks) <= doubleClickTime then
//if the two clicks occured close enough together in space
if abs(X - lastClickX) <= 5 and abs(Y - LastClickY) <= 5 then
DoubleClick //a double click has occured so call the event
end if
end if
lastClickTicks = currentClickTicks
lastClickX = X
lastClickY = Y
10. Now to test out your new DoubleClickCanvas, drag the class from the
Project window to a window in your project to create an instance of it.
11. Double-click on the canvas you just added to your window to open the
Code Editor. Notice that the canvas has a DoubleClick event handler. In
this
event handler, add the following code:
BEEP
Now run your project. You will notice that your code beeps when you
double-click. If you double-click too slowly or too fast, or your
clicks are
too far apart, no beep will be heard. You now have a totally reusable
canvas
that can detect and handle double-clicks.
Note regarding Carbon/MacOSX:
The GetDblTime toolbox call is Carbon compatible. However, in order to
call
it under Carbon, you must change "InterfaceLib" in the Declare
statement to
"CarbonLib".
--
Geoff Perlman
President & CEO
REAL Software, Inc.
http://www.realsoftware.com
mailto:geoff at realsoftware dot com
Phone: 512-263-1233 x711
Fax: 512-263-1441
- - - - - - - - - -
Got a useful tip to share? Send it to us at:
REALbasic-tips at lists dot realsoftware dot com dot
For list commands, send "Help" in the body of a message to
<requests at lists dot realsoftware dot com>
|