Page 1 of 1

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

Posted: Tue Dec 13, 2022 5:07 am
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.

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

Posted: Tue Dec 13, 2022 9:27 am
by Caronte3D
I think you want the drag&drop feature

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

Posted: Tue Dec 13, 2022 4:05 pm
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.

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

Posted: Tue Dec 13, 2022 4:53 pm
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

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

Posted: Tue Dec 13, 2022 5:05 pm
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.

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

Posted: Tue Dec 13, 2022 5:17 pm
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 :)

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

Posted: Tue Dec 13, 2022 6:15 pm
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...

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

Posted: Tue Dec 13, 2022 6:17 pm
by Caronte3D
Anyway, the solution from Demivec should be better

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

Posted: Tue Dec 13, 2022 6:43 pm
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

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

Posted: Tue Dec 13, 2022 7:06 pm
by mk-soft
:wink: