Page 1 of 2
Using an EventHandler on a Gadget
Posted: Thu Mar 05, 2009 3:15 pm
by Niffo
Hello,
I'm trying to "connect" an event handler to a Gadget using Carbon API (thanks to lexvictory !)
The code below works fine if the event handler is applied to a window (the commented line), but it is not working when applied to a Gadget

Does the GadgetID() returns the expected ID ?
If a guru of PB & MacOS is around ... it would be appreciated
Code: Select all
#kEventClassMouse = 'mous'
#kEventMouseDown = 1
#kEventMouseUp = 2
#kEventMouseMoved = 5
Structure EventTypeSpec
eventClass.l
eventKind.l
EndStructure
OpenWindow(0, 100,100, 300, 200, "Test Window")
ButtonGadget(0, 100, 100, 50, 50, "Test")
ProcedureCDLL CallBackEventsHandler(*nextHandler, theEvent, *CallBackProc)
Debug "*** CallBackEventsHandler !!! ***"
If *nexthandler : CallNextEventHandler_(*nextHandler, theEvent) : EndIf
EndProcedure
Define *EventHandlerUPP = NewEventHandlerUPP_(@CallBackEventsHandler())
Define eventTypes.EventTypeSpec
eventTypes\eventClass = #kEventClassMouse
eventtypes\eventKind = #kEventMouseDown
;InstallEventHandler_(GetWindowEventTarget_(WindowID(0)), *EventHandlerUPP, 1, @eventTypes, 0, @handlerref.l)
InstallEventHandler_(GetControlEventTarget_(GadgetID(0)), *EventHandlerUPP, 1, @eventTypes, 0, @handlerref.l)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Posted: Thu Mar 05, 2009 4:30 pm
by Erlend
I might be wrong but you might try kEventControlHit for buttons and such?
Posted: Fri Mar 06, 2009 9:17 am
by Niffo
Erlend wrote:I might be wrong but you might try kEventControlHit for buttons and such?
Ok, you're right, this event works well on a ButtonGadget, so my problem was not a GadgetID() problem. Thank you very much.
In fact, i need to detect mouse button down or mouse move on an image gadget or a container gadget, like i already do on Windows or Linux.
It seems that it is very difficult (impossible ?) to do that on mac os ... or maybe on a gadget type that does not exist in PB ?
Posted: Fri Mar 06, 2009 12:19 pm
by Erlend
I'm guessing you are creating some sort of multi platform drawing application, and need mouseinput?
In that case it's annoying that we don't have a "canvas" gadget for this and that the three main OS's PB supports have very different methods of handling events.
However maybe some of this might help, if you haven't read them already:
http://developer.apple.com/DOCUMENTATIO ... 3-CHDEDIHB
http://developer.apple.com/documentatio ... 6-BCIEAFBE
Posted: Mon Mar 09, 2009 9:57 am
by Niffo
Erlend wrote:I'm guessing you are creating some sort of multi platform drawing application, and need mouseinput?
Yes, it is exactly the case !
Erlend wrote:However maybe some of this might help, if you haven't read them already: ...
Many thanks for your interest in my problem. I can see that an event handler attached to a window triggers even when mouse is on controls. It should solve my problem if i filter the events and if i apply the good offset to the mouse coordinates.
I'm going to examine "Mouse Tracking Regions" too ...
Re: Using an EventHandler on a Gadget
Posted: Mon Mar 09, 2009 12:42 pm
by lexvictory
Niffo wrote:using Carbon API (thanks to lexvictory !)
Something to do with a pbl file I posted?
Posted: Mon Mar 09, 2009 3:11 pm
by Niffo
Yes, something to do with your post (
http://purebasic.fr/english/viewtopic.p ... 533#244533) who gave me the start point to find how to handle MacOS events

Posted: Thu May 07, 2009 6:25 pm
by Niffo
A solution :
Code: Select all
#kEventClassMouse = 'mous'
#kEventClassControl = 'cntl'
#kEventMouseDown = 1
#kEventMouseUp = 2
#kEventMouseMoved = 5
#kEventMouseDragged = 6
#kEventControlTrackingAreaEntered = 23
#kEventControlTrackingAreaExited = 24
ImportC "/System/Library/Frameworks/Carbon.framework/Carbon"
GetEventClass.i(inEvent.l)
HIViewNewTrackingArea(inView.l, inShape.l, inID.q, *outRef)
EndImport
Structure EventTypeSpec
eventClass.l
eventKind.l
EndStructure
OpenWindow(0, 100,100, 300, 200, "Test Window")
ContainerGadget(0, 100, 100, 50, 50, #PB_Container_Single)
ProcedureCDLL CallBackEventsHandler(*nextHandler, theEvent, *CallBackProc)
Static OnTheControl.b
Select GetEventClass(theEvent)
Case #kEventClassMouse
If OnTheControl
Select GetEventKind_(theEvent)
Case #kEventMouseDown
Debug "MouseDown"
Case #kEventMouseMoved, #kEventMouseDragged
Debug "MouseMove"
Case #kEventMouseUp
Debug "MouseUp"
EndSelect
EndIf
Case #kEventClassControl ;{
Select GetEventKind_(theEvent)
Case #kEventControlTrackingAreaEntered
OnTheControl = #True
Case #kEventControlTrackingAreaExited
OnTheControl = #False
EndSelect
EndSelect
If *nexthandler : CallNextEventHandler_(*nextHandler, theEvent) : EndIf
EndProcedure
; Window handler
EventHandlerUPP = NewEventHandlerUPP_(@CallBackEventsHandler())
Events_Count = 4
Dim eventTypes.EventTypeSpec(Events_Count - 1)
eventTypes(0)\eventClass = #kEventClassMouse
eventtypes(0)\eventKind = #kEventMouseDown
eventTypes(1)\eventClass = #kEventClassMouse
eventtypes(1)\eventKind = #kEventMouseMoved
eventTypes(2)\eventClass = #kEventClassMouse
eventtypes(2)\eventKind = #kEventMouseDragged
eventTypes(3)\eventClass = #kEventClassMouse
eventtypes(3)\eventKind = #kEventMouseUp
InstallEventHandler_(GetWindowEventTarget_(WindowID(0)), EventHandlerUPP, Events_Count, @eventtypes(), @CallBackEventsHandler(), @handlerref)
; Gadget hanlder
EventHandlerUPP = NewEventHandlerUPP_(@CallBackEventsHandler())
Events_Count = 2
Dim eventTypes.EventTypeSpec(Events_Count - 1)
eventTypes(0)\eventClass = #kEventClassControl
eventtypes(0)\eventKind = #kEventControlTrackingAreaEntered
eventTypes(1)\eventClass = #kEventClassControl
eventtypes(1)\eventKind = #kEventControlTrackingAreaExited
HIViewNewTrackingArea(GadgetID(0), #Null, 0, #Null)
InstallEventHandler_(GetControlEventTarget_(GadgetID(0)), EventHandlerUPP, Events_Count, @eventtypes(), @CallBackEventsHandler(), @handlerref)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Re: Using an EventHandler on a Gadget
Posted: Tue Nov 30, 2010 10:08 am
by cajomi
I have a problem:
At all times I have an error,
GetEventClass.i(inEvent.l) syntax error
When I delete the file (GetE....), and I have next error
HIViewNewTrackingArea(inView.l, inShape.l, inID.q, *outRef)
and so on
What can I do?
Re: Using an EventHandler on a Gadget
Posted: Tue Nov 30, 2010 9:20 pm
by Niffo
Do not copy/paste the code directly from Safari, it does not work. You have to paste in TextEdit before copy/paste in PB ... or better : install Firefox.
Re: Using an EventHandler on a Gadget
Posted: Wed Dec 01, 2010 8:37 am
by cajomi
I have tried, with no succes.
I am unsure, but somethings is wrong?
Johannes
Re: Using an EventHandler on a Gadget
Posted: Wed Dec 01, 2010 9:59 am
by jamirokwai
cajomi wrote:I have tried, with no succes.
I am unsure, but somethings is wrong?
Johannes
Hi Johannes,
I am not sure, if TextEdit will show the invisible part of the copied text, does ist?
Try TextWrangler (its for free). There you have to set the "Hide Invisibles" (in View -> Text Display).
If you copy and paste as suggested into TextWrangler, remove the then visible big, grey dots with
search and replace. Finally copy and paste the snippet over into PureBasic. Works for me for two
years without problems - although it's a bit annoying to have to do like this...
Re: Using an EventHandler on a Gadget
Posted: Wed Dec 01, 2010 10:22 am
by cajomi
A bit surprising, but it worked!
Johannes
Re: Using an EventHandler on a Gadget
Posted: Thu Mar 10, 2011 9:28 am
by cajomi
Hello again,
I need now a solutin to get more then one window opened and get the mouse events (the same tool).
What can I do?
Johannes
Re: Using an EventHandler on a Gadget
Posted: Thu Mar 10, 2011 10:00 am
by Niffo
What do you "need" exactly ?