Page 1 of 1

Mouse Wheel not working in Canvas Gadget

Posted: Tue Aug 16, 2022 8:06 pm
by MightyMAC
Hi there,

I just found out that the mouse wheel is not detected (#PB_EventType_MouseWheel) when using the #PB_Canvas_Container flag in a Canvas Gadget. I modified the Canvas Gadget example from the PureBasic manual to show this:

Code: Select all

  If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    ; This works:
    CanvasGadget(0, 10, 10, 200, 200)
    
    ; This doesn't work:
    ;CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Container)
    ;CloseGadgetList()
    
    Color=RGB(Random(255), Random(255), Random(255))

    Repeat
      Event = WaitWindowEvent()
          
      If Event = #PB_Event_Gadget And EventGadget() = 0 
        If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
          If StartDrawing(CanvasOutput(0))
            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            
            Circle(x, y, 10, Color)
            StopDrawing()
          EndIf
          
        ElseIf EventType() = #PB_EventType_MouseWheel
          z = GetGadgetAttribute(0, #PB_Canvas_WheelDelta)
          
          If z<>0
            Color=RGB(Random(255), Random(255), Random(255))
          EndIf
          
          Debug "Wheel!"
        EndIf
      EndIf    
      
    Until Event = #PB_Event_CloseWindow
  EndIf
Could anyone confirm that it is not working with this flag, before I post in the Bug forum, please? Thanks!

Tested on Ubuntu 20.04, PB 5.73 and PB 6.00.

Re: Mouse Wheel not working in Canvas Gadget

Posted: Tue Aug 16, 2022 9:46 pm
by Justin
It does not work here either, here is a workaround:

Code: Select all

Structure GdkEventScroll_ Align #PB_Structure_AlignC
	type.l
	window.i
	sendEvent.b
	time.l
	x.d
	y.d
	state.l
	direction.l
	device.i
	xRoot.d
	yRoot.d
	deltaX.d
	deltaY.d
EndStructure

Structure GdkEvent_ Align #PB_Structure_AlignC
	StructureUnion
		type.l
		any.GdkEventAny
		expose.GdkEventExpose
		visibility.GdkEventVisibility
		motion.GdkEventMotion
		button.GdkEventButton
		; 		touch.GdkEventTouch
		scroll.GdkEventScroll_
		key.GdkEventKey
		crossing.GdkEventCrossing
		focus_change.GdkEventFocus
		configure.GdkEventConfigure
		property.GdkEventProperty
		selection.GdkEventSelection
		; 		owner_change.GdkEventOwnerChange
		proximity.GdkEventProximity
		dnd.GdkEventDND
		window_state.GdkEventWindowState
		setting.GdkEventSetting
		; 		grab_broken.GdkEventGrabBroken
		; 		touchpad_swipe.GdkEventTouchpadSwipe
		; 		touchpad_pinch.GdkEventTouchpadPinch
		; 		pad_button.GdkEventPadButton
		; 		pad_axis.GdkEventPadAxis
		; 		pad_group_mode.GdkEventPadGroupMode
	EndStructureUnion
EndStructure

  
  ProcedureC on_scroll(wd.i, *ev.GdkEvent_, id_canvas.i)
  	Debug "scr "
  	Debug (*ev\scroll\deltaY) * -1
  EndProcedure
  
  #ID_CANVAS = 0
  
  If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    ; This works:
;     CanvasGadget(0, 10, 10, 200, 200)
    
;     This doesn't work:
    CanvasGadget(#ID_CANVAS, 10, 10, 200, 200, #PB_Canvas_Container)
    g_signal_connect_(GadgetID(0), "scroll-event", @on_scroll(), #ID_CANVAS) ;fix
    CloseGadgetList()
    
    Color=RGB(Random(255), Random(255), Random(255))

    Repeat
      Event = WaitWindowEvent()
          
      If Event = #PB_Event_Gadget And EventGadget() = 0 
        If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
          If StartDrawing(CanvasOutput(0))
            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            
            Circle(x, y, 10, Color)
            StopDrawing()
          EndIf
          
        ElseIf EventType() = #PB_EventType_MouseWheel
          z = GetGadgetAttribute(0, #PB_Canvas_WheelDelta)
          
          If z<>0
            Color=RGB(Random(255), Random(255), Random(255))
          EndIf
          
          Debug "Wheel!"
          Debug z
        EndIf
      EndIf    
      
    Until Event = #PB_Event_CloseWindow
  EndIf


Re: Mouse Wheel not working in Canvas Gadget

Posted: Wed Aug 17, 2022 9:23 am
by Lord
This works too:

Code: Select all

  If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    ; This works:
;     CanvasGadget(0, 10, 10, 200, 200)
    
    ; This doesn't work:
    CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Container|#PB_Canvas_Keyboard ) ; <---- 
    CloseGadgetList()
    
    Color=RGB(Random(255), Random(255), Random(255))

    Repeat
      Event = WaitWindowEvent()
          
      If Event = #PB_Event_Gadget And EventGadget() = 0 
        If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
          If StartDrawing(CanvasOutput(0))
            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            
            Circle(x, y, 10, Color)
            StopDrawing()
          EndIf
          
        ElseIf EventType() = #PB_EventType_MouseWheel
          z = GetGadgetAttribute(0, #PB_Canvas_WheelDelta)
          
          If z<>0
            Color=RGB(Random(255), Random(255), Random(255))
          EndIf
          
          Debug "Wheel!"
        EndIf
      EndIf    
      
    Until Event = #PB_Event_CloseWindow
  EndIf

Re: Mouse Wheel not working in Canvas Gadget

Posted: Wed Aug 17, 2022 8:17 pm
by MightyMAC
Thank you both for testing!

@Justin: Thanks for the workaround! But I would rather not use OS specific API in my project, but it's good to know it and if there is no other way I have no choice.

@Lord: That's weird, because in the project where I discovered that the mouse wheel is not working I am using #PB_Canvas_Container|#PB_Canvas_Keyboard, but if I remove #PB_Canvas_Container it works. The changed example you posted doesn't work here on my computer.

The question is: Is this expected behaviour? Reading the manual and comparing it to the behaviour in Windows it is not, so I think I should post this in the bugs forum.

Re: Mouse Wheel not working in Canvas Gadget

Posted: Wed Aug 17, 2022 8:31 pm
by Marc56us
In my case it works in all cases.
Note that there is a Windows setting that allows an object to scroll when the mouse passes over it even if it doesn't have focus (and even if the window doesn't have focus either).
This is very useful for example to scroll the help even if it is partly covered by the IDE.
I don't know where it is set, it's one of the first things I do with every new PC.
It is possible that this also influences PB ?