Using an EventHandler on a Gadget

Mac OSX specific forum
Niffo
Enthusiast
Enthusiast
Posts: 500
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Using an EventHandler on a Gadget

Post 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
Last edited by Niffo on Thu Mar 10, 2011 9:58 am, edited 1 time in total.
Niffo
User avatar
Erlend
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Apr 19, 2004 8:22 pm
Location: NORWAY

Post by Erlend »

I might be wrong but you might try kEventControlHit for buttons and such?
Niffo
Enthusiast
Enthusiast
Posts: 500
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post 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 ?
Niffo
User avatar
Erlend
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Apr 19, 2004 8:22 pm
Location: NORWAY

Post 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
Niffo
Enthusiast
Enthusiast
Posts: 500
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post 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 ...
Niffo
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: Using an EventHandler on a Gadget

Post by lexvictory »

Niffo wrote:using Carbon API (thanks to lexvictory !)
:?:
Something to do with a pbl file I posted?
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
Niffo
Enthusiast
Enthusiast
Posts: 500
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post 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 ;)
Niffo
Niffo
Enthusiast
Enthusiast
Posts: 500
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post 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
Last edited by Niffo on Thu Mar 10, 2011 9:58 am, edited 1 time in total.
Niffo
cajomi
User
User
Posts: 45
Joined: Thu Dec 04, 2008 9:53 am
Location: Essen

Re: Using an EventHandler on a Gadget

Post 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?
Niffo
Enthusiast
Enthusiast
Posts: 500
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: Using an EventHandler on a Gadget

Post 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.
Niffo
cajomi
User
User
Posts: 45
Joined: Thu Dec 04, 2008 9:53 am
Location: Essen

Re: Using an EventHandler on a Gadget

Post by cajomi »

I have tried, with no succes.

I am unsure, but somethings is wrong?

Johannes
jamirokwai
Enthusiast
Enthusiast
Posts: 772
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Using an EventHandler on a Gadget

Post 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...
Regards,
JamiroKwai
cajomi
User
User
Posts: 45
Joined: Thu Dec 04, 2008 9:53 am
Location: Essen

Re: Using an EventHandler on a Gadget

Post by cajomi »

A bit surprising, but it worked!
Johannes
cajomi
User
User
Posts: 45
Joined: Thu Dec 04, 2008 9:53 am
Location: Essen

Re: Using an EventHandler on a Gadget

Post 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
Niffo
Enthusiast
Enthusiast
Posts: 500
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: Using an EventHandler on a Gadget

Post by Niffo »

What do you "need" exactly ?
Niffo
Post Reply