Right click on HyperLinkGadget?

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Right click on HyperLinkGadget?

Post by Lebostein »

I want open a pop menu after a right click on a HyperLinkGadget. But a HyperLinkGadget generates no events...
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Right click on HyperLinkGadget?

Post by RSBasic »

Code: Select all

EnableExplicit

Global HyperLinkCB

Procedure HyperLinkCB(hWnd, Msg, wParam, lParam)
  
  Select Msg
    Case #WM_RBUTTONUP
      DisplayPopupMenu(1, WindowID(0))
  EndSelect
  
  ProcedureReturn CallWindowProc_(HyperLinkCB, hWnd, Msg, wParam, lParam)
EndProcedure

If CreatePopupImageMenu(1)
  MenuItem(1, "Hello Lebostein")
EndIf

If OpenWindow(0, 0, 0, 500, 250, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  HyperLinkGadget(1, 10, 10, WindowWidth(0)-20, 20, "http://www.google.de", RGB(0, 0, 255))
  
  HyperLinkCB = SetWindowLongPtr_(GadgetID(1), #GWL_WNDPROC, @HyperLinkCB())
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
(only Windows)
Image
Image
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Right click on HyperLinkGadget?

Post by Shardik »

The following cross-platform example detects a right click onto a HyperLinkGadget and displays a popup menu. It was successfully tested with PB 5.44 x86 in ASCII and Unicode mode on these operating systems:
- MacOS 10.6.8 'Snow Leopard'
- MacOS 10.12.3 'Sierra' (also tested with PB 5.44 x64 in ASCII and Unicode mode)
- Linux Mint 18 'Sarah' x86 Cinnamon with GTK2 and GTK3
- Lubuntu 14.04 x86 LXDE with GTK2 and GTK3
- PCLinuxOS 2014.05 MiniMe x86 KDE with GTK2 and GTK3
- Windows XP SP3
- Windows 7 SP1 x64 (also tested with PB 5.44 x64 in ASCII and Unicode mode)
- Windows 8.1 x64 (also tested with PB 5.44 x64 in ASCII and Unicode mode)

Unfortunately on Linux GTK2 and GTK3 seem to behave differently: using GTK3 you currently have to keep the right mouse button depressed and select the popup menu entry. Otherwise the popup menu will be closed when releasing the right mouse button... :(

Code: Select all

EnableExplicit

#Event_RightClick = #PB_Event_FirstCustomValue

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    ProcedureC HyperLinkCallback(*Widget.GtkWidget,
      *EventButton.GdkEventButton, UserData.I)

      If *EventButton\button = 3
        PostEvent(#Event_RightClick)
      EndIf
    EndProcedure
  CompilerCase #PB_OS_MacOS
    Define SubclassedHyperLink.I

    ProcedureC HyperLinkCallback()
      PostEvent(#Event_RightClick)
    EndProcedure
  CompilerCase #PB_OS_Windows
    Define DefaultHyperLinkCallback.I

    Procedure HyperLinkCallback(WindowHandle.I, Msg.I, WParam.I, LParam.I)
      Shared DefaultHyperLinkCallback.I

      If Msg = #WM_RBUTTONUP
        PostEvent(#Event_RightClick)
      EndIf

      ProcedureReturn CallWindowProc_(DefaultHyperLinkCallback.I,
        WindowHandle.I, Msg.I, WParam.I, LParam.I)
    EndProcedure
CompilerEndSelect

OpenWindow(0, 270, 100, 220, 65, "HyperLinkGadget")
HyperLinkGadget(0, 70, 20, 74, 30, "HyperLink", $FF, #PB_HyperLink_Underline)
SetGadgetColor(0, #PB_Gadget_FrontColor, $FF0000)

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    g_signal_connect_(gtk_widget_get_parent_(GadgetID(0)),
      "button-press-event", @HyperLinkCallback(), 0)
  CompilerCase #PB_OS_MacOS
    SubclassedHyperLink = objc_allocateClassPair_(CocoaMessage(0,
      GadgetID(0), "class"), "SubclassedHyperLink", 0)
    object_setClass_(GadgetID(0), SubclassedHyperLink)
    objc_registerClassPair_(SubclassedHyperLink)
    class_addMethod_(SubclassedHyperLink,
      sel_registerName_("rightMouseDown:"), @HyperLinkCallback(), "v@:@@")
  CompilerCase #PB_OS_Windows
    DefaultHyperLinkCallback.I = SetWindowLongPtr_(GadgetID(0), #GWL_WNDPROC,
      @HyperLinkCallback())
CompilerEndSelect

If CreatePopupMenu(0)
  MenuItem(0, "Right click menu")
EndIf

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #Event_RightClick
      DisplayPopupMenu(0, WindowID(0))
  EndSelect
ForEver
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Re: Right click on HyperLinkGadget?

Post by Lebostein »

Thanks!

I hope Fred will make the gadget more flexible one day....
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Right click on HyperLinkGadget?

Post by IdeasVacuum »

You could simply fake it with a CanvasGadget 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: Right click on HyperLinkGadget?

Post by Jac de Lad »

How can I use this in windows with multiple hyperlinks?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Right click on HyperLinkGadget?

Post by RASHAD »

Hi
For Windows

Code: Select all

Hand = LoadCursor_(0, #IDC_HAND)

If OpenWindow(0, 0, 0, 270, 160, "HyperlinkGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  HyperLinkGadget(0, 10, 10, 0,20,"Red HyperLink", RGB(255,0,0))
  gw = GadgetWidth(0,#PB_Gadget_RequiredSize)
  ResizeGadget(0,#PB_Ignore,#PB_Ignore,gw,20)
  HyperLinkGadget(1, 10, 40, 0,20,"Arial Underlined Green HyperLink", RGB(0,255,0), #PB_HyperLink_Underline)
  SetGadgetFont(1, LoadFont(0, "Arial", 12))
  gw = GadgetWidth(1,#PB_Gadget_RequiredSize)
  ResizeGadget(1,#PB_Ignore,#PB_Ignore,gw,20)
  If CreatePopupMenu(0)
    MenuItem(1, "Open")
    MenuItem(2, "Save")
    MenuItem(3, "Save as")
    MenuItem(4, "Quit")
    MenuBar()
    OpenSubMenu("Recent files")
    MenuItem(5, "PureBasic.exe")
    MenuItem(6, "Test.txt")
    CloseSubMenu()
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1            
            text$ = GetGadgetText(gad)
            Debug text$
            
          Case 4
            Quit = 1
        EndSelect
        
      Case #WM_RBUTTONDOWN
        If GetCursor_() = Hand
          GetCursorPos_ (@p.POINT)
          wwin= WindowFromPoint_(p\y << 32 + p\x)
          gad = GetDlgCtrlID_(wwin)
          DisplayPopupMenu(0, WindowID(0))
        EndIf
    EndSelect
  Until Quit = 1
EndIf
Edit : Bug fixed
Last edited by RASHAD on Fri Jan 08, 2021 4:15 am, edited 1 time in total.
Egypt my love
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: Right click on HyperLinkGadget?

Post by Jac de Lad »

Oh, thanks rashad. That's one very easy way.
...but how do I get to know which gadget was clicked? Eventgadget() doesn't help.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Right click on HyperLinkGadget?

Post by RASHAD »

Previous post updated
Egypt my love
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: Right click on HyperLinkGadget?

Post by Jac de Lad »

Ah, thanks again.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Right click on HyperLinkGadget?

Post by RASHAD »

You are welcome
Egypt my love
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: Right click on HyperLinkGadget?

Post by Jac de Lad »

Just to mention, when reading the handle of the hyperlinkgadget which got the click, we don't need the hand anymore.
Post Reply