Canvas and #PB_Canvas_ClipMouse

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Canvas and #PB_Canvas_ClipMouse

Post by Lord »

Hi!

Just a simple question: What is missing in my code snippet to get the mouse
stopping at canvas/scrollarea border?

Code: Select all

OpenWindow(1, 10, 10, 1024, 768, "", #PB_Window_SystemMenu)
ScrollAreaGadget(1, 0, 0, WindowWidth(1), WindowHeight(1), 0, 0, 100)
  CanvasGadget(2, 0, 0, WindowWidth(1), WindowHeight(1), #PB_Canvas_ClipMouse)
CloseGadgetList()

Repeat
Until WindowEvent()=#PB_Event_CloseWindow
PB-Help says
#PB_Canvas_ClipMouse: Begrenzt die Maus auf das Gadget während eine Maus-Taste gedrückt ist. (Nicht auf MacOS und Linux Gtk3 unterstützt)
I'm on Win7 x64 with PB 6.04 LTS(x64).
Image
Olli
Addict
Addict
Posts: 1202
Joined: Wed May 27, 2020 12:26 pm

Re: Canvas and #PB_Canvas_ClipMouse

Post by Olli »

If you activate the canvas, what does it happen ?

Code: Select all

SetActiveGadget(2)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Canvas and #PB_Canvas_ClipMouse

Post by RASHAD »

Hi
PB 6.04 x86 Windows 11 x64

Code: Select all

OpenWindow(1, 10, 10, 1024, 768, "", #PB_Window_SystemMenu)
ScrollAreaGadget(1, 0, 0, WindowWidth(1), WindowHeight(1), WindowWidth(1), WindowHeight(1), 100)
  CanvasGadget(2, 0, 0, WindowWidth(1)-20, WindowHeight(1)-20,  #PB_Canvas_ClipMouse)
CloseGadgetList()

Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 2
      EndSelect
    EndSelect
Until Quit = 1
Egypt my love
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: Canvas and #PB_Canvas_ClipMouse

Post by Lord »

Hi Olli, hi Rashad!
Olli wrote: Thu Feb 29, 2024 11:31 am If you activate the canvas, what does it happen ?

Code: Select all

SetActiveGadget(2)
That's not the problem. :oops:
RASHAD wrote: Thu Feb 29, 2024 11:32 am Hi
PB 6.04 x86 Windows 11 x64

Code: Select all

OpenWindow(1, 10, 10, 1024, 768, "", #PB_Window_SystemMenu)
ScrollAreaGadget(1, 0, 0, WindowWidth(1), WindowHeight(1), WindowWidth(1), WindowHeight(1), 100)
  CanvasGadget(2, 0, 0, WindowWidth(1)-20, WindowHeight(1)-20,  #PB_Canvas_ClipMouse)
CloseGadgetList()

Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 2
      EndSelect
    EndSelect
Until Quit = 1
That seems to work. But...
The real problem was me.
First thing was that I had a ScrollAreaGadget with inner size of 0/0.
Later in my program it will be resized.
Thats why my short snipped shows the same unwanted behaviour.
The second problem (not with the short snippet) was, that I resize
the canvas to hold a larger image. So the canvas reached over the
ScrollAreaGadget and as a consequence the mouse will go over the
scrollarea/window.

Sorry for disturbing you. :oops: :oops: :oops:
Anyway thanks for answering
Image
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: Canvas and #PB_Canvas_ClipMouse

Post by Lord »

Hi, its me again with a question related to the problem above.

Is there a way to create a "ScrollAreaClipMouse" or would
this be a feature request?
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Canvas and #PB_Canvas_ClipMouse

Post by RASHAD »

In Windows using API you can track in ,track out
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Canvas and #PB_Canvas_ClipMouse

Post by RASHAD »

ScrollArea clip cursor
c to clip
Esc to release

Code: Select all

Global r.RECT

OpenWindow(0, 0, 0, 800,600, "",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

ScrollAreaGadget(0, 10,10, 780, 580,800,600, 10, #PB_ScrollArea_Single)
CloseGadgetList()
SetRect_(r,GadgetX(0,#PB_Gadget_ScreenCoordinate),GadgetY(0,#PB_Gadget_ScreenCoordinate),GadgetX(0,#PB_Gadget_ScreenCoordinate)+GadgetWidth(0),GadgetY(0,#PB_Gadget_ScreenCoordinate)+GadgetHeight(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
     Case #WM_KEYDOWN
      key = EventwParam()
      If key = 67
        ClipCursor_(r)
      ElseIf key = 27
        ClipCursor_(0)
      EndIf
  EndSelect
Until Quit = 1
End
Egypt my love
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: Canvas and #PB_Canvas_ClipMouse

Post by Lord »

Hi Rashad!

Your example works great.
I've inserted it in my code without any trouble.
Thank you for your help.
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Canvas and #PB_Canvas_ClipMouse

Post by RASHAD »

Hi Lord
You are welcome
Next more direct and more powerful

Code: Select all

OpenWindow(0, 0, 0, 800,600, "",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

ScrollAreaGadget(0, 10,10, 780, 580,800,600, 10, #PB_ScrollArea_Single)
CloseGadgetList()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
     Case #WM_KEYDOWN
      key = EventwParam()
      If key = 67
        GetWindowRect_(GadgetID(0),r.RECT)
        ClipCursor_(r)
      ElseIf key = 27
        ClipCursor_(0)
      EndIf
  EndSelect
Until Quit = 1
End
Egypt my love
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: Canvas and #PB_Canvas_ClipMouse

Post by Lord »

Hi Rashad!

Thanks again for your (always) tips and tricks.

I use your

Code: Select all

SetRect_(r,GadgetX(0,#PB_Gadget_ScreenCoordinate),GadgetY(0,#PB_Gadget_ScreenCoordinate),GadgetX(0,#PB_Gadget_ScreenCoordinate)+GadgetWidth(0),GadgetY(0,#PB_Gadget_ScreenCoordinate)+GadgetHeight(0))
to limit the mouse inside scroll area without scrollbars. Just by subtracting with and height of scrolllbars.
I do not use key strokes, just #PB_EventType_LeftButtonDown and #PB_EventType_LeftButtonUp in event handling of canvas events (BindGadgetEvents()).
So everything works fine.
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Canvas and #PB_Canvas_ClipMouse

Post by RASHAD »

Hi Lord
You can use the next and I think it's much better than SetRect_()
It will save you the calculations of the ScrollArea() Gadget dimensions
After all it's your private code :)
Glad to talk to you

Code: Select all

        GetWindowRect_(GadgetID(0),r.RECT)
        r\bottom - 20
        r\right - 20
        ClipCursor_(r)
Egypt my love
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: Canvas and #PB_Canvas_ClipMouse

Post by Lord »

Hi Rashad!
RASHAD wrote: Fri Mar 01, 2024 9:51 am ...

Code: Select all

        GetWindowRect_(GadgetID(0),r.RECT)
        r\bottom - 20
        r\right - 20
        ClipCursor_(r)
That is a really nice, elegant and short solution. :D
Thank you for your always inspiring coding examples.
Image
Post Reply