[futurebasic] Re: [FB] User Events in FBtoC

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2008 : Group Archive : Group : All Groups

From: Bernie <fblist.bw@...>
Date: Wed, 30 Jan 2008 17:22:14 +0100
Steve wrote:

> Is there an equivalent function to FB's user Event function?
> I want to generate an event for a window and use the Event handlers  
> that were installed for that window to process the event.


Well, I've never done this before, so I'm not sure if it's the best  
(or even recommended) way to do it. Maybe RP will come up with a  
better solution.

'------------
include "Tlbx CarbonEvents.incl"

_myUserEventClass = _"mUEC"
_myUserEventKind = 1


local mode
local fn PostWindowUserEvent( w as WindowRef, eventClass as UInt32,  
eventKind as UInt32 )
'~'1
dim as EventRef @ theEvent

long if ( fn CreateEvent( _kCFAllocatorDefault, eventClass, eventKind,  
0.0, _kEventAttributeNone, @theEvent ) == _noErr )
call SendEventToEventTarget( theEvent, fn GetWindowEventTarget( w ) )
ReleaseEvent( theEvent )
end if
end fn


#if def _FBtoC

local fn MyWindowEventHandler( nextHandler as EventHandlerCallRef,  
theEvent as EventRef, w as WindowRef )
'~'1
dim as OSStatus result : result = _eventNotHandledErr

select ( fn GetEventClass( theEvent ) )
case _kEventClassWindow
select ( fn GetEventKind( theEvent ) )
case _kEventWindowClose : end
end select

case _myUserEventClass
select ( fn GetEventKind( theEvent ) )
case _myUserEventKind

beep

result = _noErr
end select
end select
end fn = result

local fn InstallMyWindowEventHandler( w as WindowRef )
'~'1
dim as EventTypeSpec eventSpec(1)

eventSpec.eventClass(0) = _kEventClassWindow
eventSpec.eventKind(0) = _kEventWindowClose
eventSpec.eventClass(1) = _myUserEventClass
eventSpec.eventKind(1) = _myUserEventKind
end fn = fn InstallEventHandler( fn GetWindowEventTarget( w ), @fn  
MyWindowEventHandler, 2, @eventSpec(0), #w, #0 )


local fn MyControlHitHandler( nextHandler as EventHandlerCallRef,  
theEvent as EventRef, c as ControlRef )
'~'1
fn PostWindowUserEvent( fn GetControlOwner( c ), _myUserEventClass,  
_myUserEventKind )
end fn = _noErr

local fn InstallMyControlHitHandler( c as ControlRef )
'~'1
dim as EventTypeSpec eventSpec

eventSpec.eventClass = _kEventClassControl
eventSpec.eventKind  = _kEventControlHit
end fn = fn InstallEventHandler( fn GetControlEventTarget( c ), @fn  
MyControlHitHandler, 1, @eventSpec, #c, #0 )

#else

long if 0
"MyWindowEventHandler"
enterproc fn MyWindowEventHandler( nextHandler as EventHandlerCallRef,  
theEvent as EventRef, w as WindowRef ) = OSStatus
'~'1
dim as OSStatus result : result = _eventNotHandledErr

select ( fn GetEventClass( theEvent ) )
case _kEventClassWindow
select ( fn GetEventKind( theEvent ) )
case _kEventWindowClose : end
end select

case _myUserEventClass
select ( fn GetEventKind( theEvent ) )
case _myUserEventKind

beep

result = _noErr
end select
end select
exitproc = result
end if

local fn InstallMyWindowEventHandler( w as WindowRef )
'~'1
dim as EventTypeSpec eventSpec(1)
begin globals
dim as pointer sMyWindowEventHandlerUPP
end globals

long if ( sMyWindowEventHandlerUPP == 0 )
sMyWindowEventHandlerUPP = fn NewEventHandlerUPP( [proc  
"MyWindowEventHandler" + _FBprocToProcPtrOffset] )
end if
eventSpec.eventClass(0) = _kEventClassWindow
eventSpec.eventKind(0) = _kEventWindowClose
eventSpec.eventClass(1) = _myUserEventClass
eventSpec.eventKind(1) = _myUserEventKind
end fn = fn InstallEventHandler( fn GetWindowEventTarget( w ),  
sMyWindowEventHandlerUPP, 2, @eventSpec(0), #w, #0 )


long if 0
"MyControlHitHandler"
enterproc fn MyControlHitHandler( nextHandler as EventHandlerCallRef,  
theEvent as EventRef, c as ControlRef )
'~'1
fn PostWindowUserEvent( fn GetControlOwner( c ), _myUserEventClass,  
_myUserEventKind )
exitproc = _noErr
end if

local fn InstallMyControlHitHandler( c as ControlRef )
'~'1
dim as EventTypeSpec eventSpec

eventSpec.eventClass = _kEventClassControl
eventSpec.eventKind  = _kEventControlHit
begin globals
dim as pointer sMyControlHitHandlerUPP
end globals
long if ( sMyControlHitHandlerUPP == 0 )
sMyControlHitHandlerUPP = fn NewEventHandlerUPP( [proc  
"MyControlHitHandler" + _FBprocToProcPtrOffset] )
end if
end fn = fn InstallEventHandler( fn GetControlEventTarget( c ),  
sMyControlHitHandlerUPP, 1, @eventSpec, #c, #0 )

#endif// def _FBtoC


dim as Rect r

SetRect( r, 0, 44, 480, 404 )
appearance window 1, "My Window", @r, _kDocumentWindowClass,  
_kWindowCloseBoxAttribute _kWindowStandardHandlerAttribute
fn InstallMyWindowEventHandler( window(_wndRef) )

SetRect( r, 151, 320, 328, 340 )
appearance button 1,,,,, "Post window user event", @r,  
_kControlPushButtonProc
fn InstallMyControlHitHandler( button&(1) )


RunApplicationEventLoop()
'------------

Bernie