[futurebasic] Re: [FB] Clickable Static Text or something?

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 2007 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Sat, 31 Mar 2007 22:38:49 +1200
Brian Heibert wrote:

> Is it possible to create a clickable static text or something
> I want to create something that looks and acts like a link that will
> do a LaunchURL

Carbon does not provide a control with the desired behaviour, but a  
static text control can be persuaded to become clickable. AFAIK this  
method has not been demonstrated in FB until now.

Below is a ready-to-run example. DoURLClick is called when the user  
clicks the URL.

A defect is that the text does not highlight when pressed. This could  
probably be fixed by handling additional events (_kEventControlClick,  
_kEventControlDraw) so as to draw the text in a different colour. A  
challenge for CarbonEvent experts...

Robert P.

Clickable URL, using the method of Apple's QA1380.
Adapted for FB by Robert P.    March 2007
include "Tlbx HIView.incl"
include "Tlbx CoreGraphics.incl"

local fn DoURLClick( c as ControlRef )
dim as OSStatus ignore
dim as Str255 url
dim as long @ actual

// retrieve the url text from the control
ignore = fn GetControlData( c, _kControlEditTextPart,  
_kControlEditTextTextTag, 255, @url[1], actual )
url[0] = actual // string length
// the 'open' command is a handy way to open URLs as well as files
open "unix", 222, "open " + url
close 222
end fn

local fn InstallClickableURLHandler( c as ControlRef )
dim as EventTypeSpec myEvents(2)
begin globals
dim as proc sURLEventUPP // 'static' var
end globals

myEvents.eventClass(0) = _kEventClassControl
myEvents.eventKind(0) = _kEventControlHitTest
myEvents.eventClass(1) = _kEventClassControl
myEvents.eventKind(1) = _kEventControlHit

long if ( sURLEventUPP == 0 )
sURLEventUPP = fn NewEventHandlerUPP( [Proc "ClickableURLHandler" +  
_FBprocToProcPtrOffset] )
end if
end fn = fn InstallEventHandler( fn GetControlEventTarget( c ),  
sURLEventUPP, 2, @myEvents(0), #0, #0 )

long if 0
enterproc fn ClickableURLHandler( nextHandler as EventHandlerCallRef,  
theEvent as EventRef, userData as ptr ) = OSStatus
dim as OSStatus result
dim as ControlRef @ c
dim as HIRect bounds
dim as HIPoint pt
dim as ControlPartCode @ part

result = _eventNotHandledErr
call GetEventParameter( theEvent, _kEventParamDirectObject,  
_typeControlRef, #0, sizeof( c ), #0, @c )

select fn GetEventKind( theEvent )

case _kEventControlHitTest
// static text control requires this handler in order to track the mouse
call HIViewGetBounds( c, @bounds )
call GetEventParameter( theEvent, _kEventParamMouseLocation,  
_typeHIPoint, #0, sizeof( pt ), #0, @pt )
part = _kControlNoPart
// if mouse over the control, send kControlButtonPart
if ( fn CGRectContainsPoint( bounds, pt ) ) then part =  
call SetEventParameter( theEvent, _kEventParamControlPart,  
_typeControlPartCode, sizeof( part ), @part )
result = _noErr

// here we act on a completed click-and-release, like a button click
case _kEventControlHit
fn DoURLClick( c )
result = _noErr

end select
exitproc = result
end if

local fn MakeClickableURL( btnNum as long, url as Str255, r as ^Rect )
dim as ControlFontStyleRec cfs
dim as Rect rCopy

rCopy = r
// make the text control
appearance button btnNum,,,,,, @rCopy, _kControlStaticTextProc
def SetButtonTextString( btnNum, url )
// set the text blue, underlined
cfs.flags = _kControlUseFaceMask _kControlUseForeColorMask
cfs.style = _ulineBit%
cfs.foreColor.red = 0
cfs.foreColor.green = 0
cfs.foreColor.blue = 0xffff
def SetButtonFontStyle( btnNum, cfs )
// install Carbon event handler on the control
fn InstallClickableURLHandler( button&( btnNum ) )
end fn

dim as Rect r
window 1, "Clickable URL"
SetRect( r, 20, 15, 390, 35 )
fn MakeClickableURL( 1, "http://developer.apple.com/qa/qa2004/ 
qa1380.html", r )

until 0