GetGadgetColumns(), GadgetMouseX(), GadgetMouseY()

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

GetGadgetColumns(), GadgetMouseX(), GadgetMouseY()

Post by infratec »

Hi,

since many people are 'mouse pushers' and don't use the keyboard, I wanted to implement a copy to clipboard possibility for a ListIconGadget().
For that I needed to know over which column the mouse is located.

Maybe it is useful for somebody else:

Code: Select all

Procedure.i GetGadgetColumns(Gadget.i)
  
  Protected.i Result
  
  
  If IsGadget(Gadget)
    While GetGadgetItemAttribute(Gadget, -1, #PB_ListIcon_ColumnWidth, Result) <> 0
      Result + 1
    Wend
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i GadgetMouseX()
  
  Protected.i Result, ActiveWindow, ActiveGadget
  
  
  Result = -1
  ActiveWindow = GetActiveWindow()
  ActiveGadget = GetActiveGadget()
  
  If ActiveWindow >= 0 And ActiveGadget >= 0
    Result = WindowMouseX(ActiveWindow) - GadgetX(ActiveGadget, #PB_Gadget_WindowCoordinate)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i GadgetMouseY()
  
  Protected.i Result, ActiveWindow, ActiveGadget
  
  
  Result = -1
  ActiveWindow = GetActiveWindow()
  ActiveGadget = GetActiveGadget()
  
  If ActiveWindow >= 0 And ActiveGadget >= 0
    Result = WindowMouseY(ActiveWindow) - GadgetY(ActiveGadget, #PB_Gadget_WindowCoordinate)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




OpenWindow(0, 0, 0, 400, 200, "Demo", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

ListIconGadget(0, 10, 10, 380, 180, "Name", 100, #PB_ListIcon_FullRowSelect)
AddGadgetColumn(0, 1, "MAC", 110)
AddGadgetColumn(0, 2, "Address", 100)

AddGadgetItem(0, -1, "RMS100-HC" + #LF$ + "00-10-6C-00-12-34" + #LF$ + "127.0.0.2")
AddGadgetItem(0, -1, "RMS100-HC-M" + #LF$ + "00-10-6C-00-12-35" + #LF$ + "127.0.0.3")

SetActiveGadget(0)

AddKeyboardShortcut(0, #PB_Shortcut_F1, 0)
AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_C, 1)
AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_N, 2)
AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_M, 3)
AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_A, 4)

Exit = #False
Repeat
  Event = WaitWindowEvent(10)
  
  Select Event
    Case #PB_Event_Menu
          Select EventMenu()
            Case 0
              CopyText$ = "Double click for open the device in the web browser" + #LF$ + #LF$
              CopyText$ + "CTRL+C to copy the complete line into the clipboard" + #LF$
              CopyText$ + "CTRL+N to copy the name into the clipboard" + #LF$
              CopyText$ + "CTRL+M to copy the MAC into the clipboard" + #LF$
              CopyText$ + "CTRL+A to copy the address into the clipboard" + #LF$
              MessageRequester("Help", CopyText$)
              
            Case 1
              CopyText$ = GetGadgetItemText(0, GetGadgetState(0), 0)
              CopyText$ + GetGadgetItemText(0, GetGadgetState(0), 1)
              CopyText$ + GetGadgetItemText(0, GetGadgetState(0), 2)
              CopyText$ = Trim(CopyText$)
              SetClipboardText(CopyText$)
              
            Case 2
              CopyText$ = GetGadgetItemText(0, GetGadgetState(0), 0)
              CopyText$ = Trim(CopyText$)
              SetClipboardText(CopyText$)
              
            Case 3
              CopyText$ = GetGadgetItemText(0, GetGadgetState(0), 1)
              CopyText$ = Trim(CopyText$)
              CopyText$ = RemoveString(CopyText$, "-")
              SetClipboardText(CopyText$)
              
            Case 4
              CopyText$ = GetGadgetItemText(0, GetGadgetState(0), 2)
              CopyText$ = Trim(CopyText$)
              SetClipboardText(CopyText$)
              
          EndSelect
          
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_RightClick
          If GetGadgetState(0) >= 0
            If IsMenu(0)
              FreeMenu(0)
            EndIf
            CreatePopupMenu(0)
            GadgetMouseXPos = GadgetMouseX()
            GadgetColumns = GetGadgetColumns(0) - 1
            GadgetColumnHelp = 0
            For i = 0 To GadgetColumns
              GadgetColumnHelp + GetGadgetItemAttribute(0, -1, #PB_ListIcon_ColumnWidth, i)
              If GadgetColumnHelp > GadgetMouseXPos
                Break
              EndIf
            Next i
            Select i
              Case 0
                MenuItem(2, "copy name")
              Case 1
                MenuItem(3, "copy MAC")
              Case 2
                MenuItem(4, "copy address")
            EndSelect
            DisplayPopupMenu(0, WindowID(0))
          EndIf
      EndSelect
      
    Case #PB_Event_CloseWindow
      Exit = #True
      
  EndSelect
  
Until Exit
Bernd
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: GetGadgetColumns(), GadgetMouseX(), GadgetMouseY()

Post by RASHAD »

Hi Bernd
I liked the idea
Also liked the code

But if you increased the width of any column till the next column is outside the visible area you will get wrong answer from the hidden column after scrolling the hal. scroll bar
Need some more effort mate :P
Egypt my love
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: GetGadgetColumns(), GadgetMouseX(), GadgetMouseY()

Post by Vera »

Thank you Bernd :-)

That really comes in handy for my small search tool to allow picking out specific results from the list apart from exporting the whole result list.

hint: copying the whole row needs some separation between the items

cheers ~ Vera
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: GetGadgetColumns(), GadgetMouseX(), GadgetMouseY()

Post by Rescator »

FIY a "mouse pusher" may include those who use:

A Mouse
A Trackball
A 3DNavigator
A Touchpad
A Touchscreen
A Drawingpad
A Digital Pen
A Lightpen / A Lightgun
A Joystick
A Gamepad
A Steamcontroller (which has two "touchpads")
A Disabilitydevice of some sort.
A device type I forgot to mention, there are some weird MIDI / USB devices out there, DJ proximity laser sensors?.
A whatever it is that neural interfaces is grouped as, they emulate/simulate a mouse moving and clicking it seems.

With the exception of Joysticks and Gamepads the rest tend to default to mouse compatible behavior, they either pose as a mouse or they send mouse events. A few might also send keyboard events or emulate a keyboard too though.

So dismissing "mousers" or "mouse dwellers" or "mouse pushers" (how many nicknames are there really?) may surprisingly reduce your userbase.

I know, crazy huh? Even just typing this post I realized there is way more types of mouselike devices than I recalled.
Post Reply