Page 1 of 1

Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 11:13 am
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).

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 11:31 am
by Olli
If you activate the canvas, what does it happen ?

Code: Select all

SetActiveGadget(2)

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 11:32 am
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

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 2:37 pm
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

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 4:12 pm
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?

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 5:34 pm
by RASHAD
In Windows using API you can track in ,track out

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 7:31 pm
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

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 8:01 pm
by Lord
Hi Rashad!

Your example works great.
I've inserted it in my code without any trouble.
Thank you for your help.

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Thu Feb 29, 2024 8:35 pm
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

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Fri Mar 01, 2024 9:31 am
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.

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Fri Mar 01, 2024 9:51 am
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)

Re: Canvas and #PB_Canvas_ClipMouse

Posted: Fri Mar 01, 2024 11:39 am
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.