a way to check if toolbar button is being held by the mouse?

Just starting out? Need help? Post your questions and find answers here.
TRS-Eric
User
User
Posts: 16
Joined: Thu Apr 23, 2020 7:42 pm

a way to check if toolbar button is being held by the mouse?

Post by TRS-Eric »

Hello all. Is there a way to check if a toolbar button is actively being held by the mouse?

I want the user to click on a toolbar icon and hold the left button, and then move the mouse, and then I plan on getting the mouse position until they let go. However, I see the Event documentation doesn't cover this type of action.

I am only writing for Windows.
---
BASIC related discord: https://discord.gg/KS4en5y5j4
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: a way to check if toolbar button is being held by the mouse?

Post by Caronte3D »

I think you want the drag&drop feature
TRS-Eric
User
User
Posts: 16
Joined: Thu Apr 23, 2020 7:42 pm

Re: a way to check if toolbar button is being held by the mouse?

Post by TRS-Eric »

I don't think that will work as the toolbar has no drag&drop functionality and I can't call a private drag and drop until after the buttons already released.
---
BASIC related discord: https://discord.gg/KS4en5y5j4
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: a way to check if toolbar button is being held by the mouse?

Post by Demivec »

TRS-Eric wrote: Tue Dec 13, 2022 4:05 pm I don't think that will work as the toolbar has no drag&drop functionality and I can't call a private drag and drop until after the buttons already released.
Create the toolbar with the Canvas gadget and you can use the canvas's events to monitor the mouse according to your liking
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: a way to check if toolbar button is being held by the mouse?

Post by Caronte3D »

I havent used the toolbar yet, but if you can't use de normal drag&drop...

Make your own drag&drop:

Check the mouse button state.
If it's on the toolbar and it's pressed, save a flag (drag).
Then move the mouse
Now, If mouse button is released and drag flag is true, do something.
TRS-Eric
User
User
Posts: 16
Joined: Thu Apr 23, 2020 7:42 pm

Re: a way to check if toolbar button is being held by the mouse?

Post by TRS-Eric »

This was the first thing I tried, sadly there's no mouse down event for the toolbar.

I think I can come up with a simpler method, the user presses the toolbar button and then presses escape for the ok dialog box, and I'll just monitor the mouse position. I'll give that a try instead :)
---
BASIC related discord: https://discord.gg/KS4en5y5j4
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: a way to check if toolbar button is being held by the mouse?

Post by Caronte3D »

TRS-Eric wrote: Tue Dec 13, 2022 5:17 pm This was the first thing I tried, sadly there's no mouse down event for the toolbar.
You can check (globally) the mouse position and the mouse button state despite the gadget, so compare the position with the toolbar position, and so on...
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: a way to check if toolbar button is being held by the mouse?

Post by Caronte3D »

Anyway, the solution from Demivec should be better
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: a way to check if toolbar button is being held by the mouse?

Post by breeze4me »

Maybe like this way?

Code: Select all

UsePNGImageDecoder()

Procedure WindowCallback(hWnd, uMsg, wParam, lParam)
  Protected Result = #PB_ProcessPureBasicEvents
  Protected *lpnmtb.NMTOOLBAR, pt.POINT, idx
  
  Static iStartButton
  
  If uMsg = #WM_NOTIFY
    *lpnmtb = lParam
    If *lpnmtb And *lpnmtb\hdr\hwndFrom = ToolBarID(0)
      Select *lpnmtb\hdr\code
        Case #TBN_BEGINDRAG
          iStartButton = -1
          If GetCursorPos_(@pt)
            MapWindowPoints_(0, ToolBarID(0), pt, 1)
            iStartButton = SendMessage_(ToolBarID(0), #TB_HITTEST, 0, pt)
          EndIf
          
        Case #TBN_ENDDRAG
          If GetCursorPos_(@pt)
            MapWindowPoints_(0, ToolBarID(0), pt, 1)
            
            idx = SendMessage_(ToolBarID(0), #TB_HITTEST, 0, pt)
            If idx >= 0
              Debug "dropped on button " + idx
              
              If iStartButton = idx
                Debug "dropped on the same button."
              EndIf
              
            Else
              MapWindowPoints_(ToolBarID(0), WindowID(0), pt, 1)
              Debug "dropped outside: " + pt\x + "," +pt\y + " on window 0"
            EndIf
          EndIf
          
      EndSelect
    EndIf
  EndIf
  
  ProcedureReturn Result
EndProcedure

If OpenWindow(0, 100, 200, 195, 260, "ToolBar example", #PB_Window_SystemMenu | #PB_Window_SizeGadget)

  If CreateToolBar(0, WindowID(0))
    ToolBarImageButton(0, LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/ToolBar/New.png"))
    ToolBarImageButton(1, LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"))
    ToolBarImageButton(2, LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Save.png"))
    
    ToolBarSeparator()

    ToolBarImageButton(3, LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Cut.png"))
    ToolBarToolTip(0, 3, "Cut")
    
    ToolBarImageButton(4, LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Copy.png"))
    ToolBarToolTip(0, 4, "Copy")
    
    ToolBarImageButton(5, LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Paste.png"))
    ToolBarToolTip(0, 5, "Paste")
    
    ToolBarSeparator()

    ToolBarImageButton(6, LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Find.png"))
    ToolBarToolTip(0, 6, "Find a document")
  EndIf
  
  If CreateMenu(0, WindowID(0))
    MenuTitle("Project")
      MenuItem(0, "New")
      MenuItem(1, "Open")
      MenuItem(2, "Save")
  EndIf
  
  DisableToolBarButton(0, 2, 1)
  
  SetWindowCallback(@WindowCallback(), 0)
  
  Repeat
    Event = WaitWindowEvent()

    Select Event
    
      Case #PB_Event_Menu
        MessageRequester("Information", "ToolBar or Menu ID: "+Str(EventMenu()), 0)
      
      Case #PB_Event_CloseWindow
        Quit = 1
        
    EndSelect

  Until Quit = 1
  
EndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 6206
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: a way to check if toolbar button is being held by the mouse?

Post by mk-soft »

:wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply