MouseOverGadget() ?

BlitzMax Forums/BlitzMax Programming/MouseOverGadget() ?

I might of missed something here but is there a way to find out which gadget the mouse is over?

What I have tried so far is:

* create a panel which covers the GUI area and give it PANEL_ACTIVE status to pick up mouse coordinates

* attach gadgets to the panel and pass their handle to a gadgetlist.

* wait for a MOUSELEAVE event (that is, mouse left the main panel and is over another gadget)

* compare mouse X/Y coordinates to gadget x/y/w/h values to see which gadget the mouse is over


This works 'OK' enough except it's easy to fool the system into thinking the mouse is still over a particular gadget.

Try the example here. Move around to see MouseOverGadget in action. If you run the mouse quickly from the panel to the statusbar (or lower) you'll see the system still reports the mouse to be over the panel.

Global gadgetlist:TList=New TList

' set up GUI stuff
win:TGadget = CreateWindow("MouseOverGadget()",10,10,250,170,Null,WINDOW_TITLEBAR|EVENT_WINDOWSIZE|WINDOW_STATUS)
mainpanel:TGadget=CreatePanel(0,0,win.ClientWidth(),win.ClientHeight(),win,PANEL_ACTIVE)
slider1:TGadget=CreateSlider(10,20,160,40,mainpanel,SLIDER_HORIZONTAL|SLIDER_TRACKBAR)
slider1.SetRange 0,10
button1:TGadget=CreateButton("button #1",20,90,80,22,mainpanel)
panel1:TGadget=CreatePanel(120,70,60,40,mainpanel,PANEL_BORDER,"panel 1")
panel1.SetColor $ff,$ff,$55

' add gadgets to list
gadgetlist.AddLast slider1
gadgetlist.AddLast button1
gadgetlist.AddLast panel1

' main loop
Repeat
	WaitEvent
	Select CurrentEvent.ID
		Case EVENT_WINDOWCLOSE ; Exit
		Case EVENT_MOUSEENTER
		panel1.SetColor $55,$55,$55
		button1.SetText "button #1"
		slider1.SetProp 0
		SetStatusText win,"Mouse in main window area"
		Case EVENT_MOUSELEAVE
		Select MouseOverGadget()
			Case slider1
			slider1.SetProp 5
			SetStatusText win,"Mouse over slider"
			Case button1
			button1.SetText "Hello mouse!"
			SetStatusText win,"Mouse over button"
			Case panel1
			panel1.SetColor $00,$ff,$ff
			SetStatusText win,"Mouse inside panel"
		End Select
	End Select
Forever

End

' report which gadget the mouse is currently over
Function MouseOverGadget:TGadget()
	Local mx=CurrentEvent.x
	Local my=CurrentEvent.y
	For Local g:TGadget=EachIn gadgetlist
		If mx>=g.xpos And mx<g.xpos+g.width
			If my>=g.ypos And my<g.ypos+g.height
				Return g
			EndIf
		EndIf
	Next
	Return Null
End Function


Ideally I would like a Win32/Mac/Linux friendly method which can correctly report which gadget the mouse is over.

I might of missed something here but is there a way to find out which gadget the mouse is over?
Not AFAIK. But you can fake it well enough with EVENT_GADGETENTER and EVENT_GADGETLEAVE (or whatever they're called).

I figure that the "SetGadgetFilter" command will do it in the future...

The filter function supplied is called by the Gadget with a TEvent and optional user context object. If the Function returns zero the event is filtered and not processed further by the system whereas a non zero return indicates event processing should proceed as normal.
Currently only EVENT_KEYDOWN, EVENT_KEYCHAR, events produced by TextArea and TextField gadgets can be filtered with the SetGadgetFilter command.