How to right click buttongadget?

Just starting out? Need help? Post your questions and find answers here.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

How to right click buttongadget?

Post by SkyManager »

Hi, is it possible to Right click buttonGadget?

Code: Select all

Procedure WhenBtnRClicked()
  Debug "RClick"
EndProcedure
OpenWindow(0, 0, 0, 100, 100, "Test")
ButtonGadget(1, 0, 0, 40, 25, "Btn")
BindEvent(#PB_Event_Gadget, @WhenBtnRClicked(),  0, 1, #PB_EventType_RightButtonDown)
Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Debug "LClick"
        EndSelect
     EndSelect
Until WinQuit
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How to right click buttongadget?

Post by RSBasic »

Only with API.
For Windows:

Code: Select all

EnableExplicit

Global ButtonCB

Procedure ButtonCB(hWnd, Message, wParam, lParam)
  Select Message
    Case #WM_RBUTTONUP
      Debug "Right clicked"
  EndSelect
  
  ProcedureReturn CallWindowProc_(ButtonCB, hWnd, Message, wParam, lParam)
EndProcedure

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(1, 10, 10, 100, 20, "Button", 0)
  
  ButtonCB = SetWindowLongPtr_(GadgetID(1), #GWL_WNDPROC, @ButtonCB())
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
Image
Image
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: How to right click buttongadget?

Post by SkyManager »

Window only :?: :!:
I am planning to implement in Mac
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How to right click buttongadget?

Post by RSBasic »

Can you write in your signature that you use MacOS?
Then you have to wait for the user "Shardik". He can offer you a solution.
Image
Image
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: How to right click buttongadget?

Post by SkyManager »

I think I will investigate using canvas gadget to replace the button gadget.
:?
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How to right click buttongadget?

Post by RSBasic »

Yes, this is the better solution for platform independence.

\\Edit:
But wait, I found a code by MacOS Master Shardik: http://www.purebasic.fr/english/viewtop ... 52#p365352
Image
Image
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to right click buttongadget?

Post by Shardik »

RSBasic wrote:But wait, I found a code by MacOS Master Shardik: http://www.purebasic.fr/english/viewtop ... 52#p365352
Thank you for your kind words about me, Ray. But the only one justified to be designated as "MacOS Master" is Wilbert who programmed the CocoaMessage() function in Objective-C which Fred then integrated in PureBasic. This CocoaMessage() function allows other MacOS programmers to utilize the huge object oriented Cocoa framework with ease in procedural PureBasic programs!

Unfortunately your posted link is an old example using the deprecated Carbon framework (the last PureBasic version to support the Carbon framework as a subsystem was 5.11 x86). The following example uses the Cocoa framework and detects a left and right click. I have tested this example successfully on
- MacOS 10.6.8 'Snow Leopard' with PB 5.62 x86
- MacOS 10.13.4 'High Sierra' with PB 5.62 x86 and x64

Code: Select all

EnableExplicit

Define CurrentEvent.I
Define EventType.I
Define Point.NSPoint
Define SharedApplication = CocoaMessage(0, 0, "NSApplication sharedApplication")
Define View.I

OpenWindow(0, 270, 100, 270, 60, "Detect right click onto button")
ButtonGadget(0, 20, 20, 230, 25, "Click me with left or right button!")

Repeat
  CurrentEvent = CocoaMessage(0, SharedApplication, "currentEvent")
  
  If CurrentEvent
    EventType = CocoaMessage(0, CurrentEvent, "type")

    ; ----- Check if right mosue button was clicked and released
    If EventType = #NSRightMouseUp
      ; ----- Check if our button was clicked
      CocoaMessage(@Point, CurrentEvent, "locationInWindow")
      View = CocoaMessage(0, CocoaMessage(0, WindowID(0), "contentView"),
        "hitTest:@", @Point)

      If View = GadgetID(0)
        Debug "Right click on button"
      EndIf
    EndIf
  EndIf

  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0
        If EventType() = #PB_EventType_LeftClick
          Debug "Left click on button"
        EndIf
      EndIf
  EndSelect
ForEver
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: How to right click buttongadget?

Post by mestnyi »

I only look inside the purebasic cycle there are examples.
Post Reply