Detecting right-clicks on ButtonGadgets

Share your advanced PureBasic knowledge/code with the community.
aaron
Enthusiast
Enthusiast
Posts: 267
Joined: Mon Apr 19, 2004 3:04 am
Location: Canada
Contact:

Post by aaron »

Sorry to dig up an old thread, but I'm having troubles with this code.... when I try using it with panels, I don't get anything meaningful back from the ChildWindowFromPoint_ API command.

Anyone have any idea? :cry: I'm an API idiot....

Code: Select all

Global window0,btn0,btn1,button_test

Procedure ButtonCallback(hWnd, Message, wParam, lParam)
  Select message
    Case #WM_PARENTNOTIFY
      If GetAsyncKeyState_(#VK_RBUTTON)
        Select ChildWindowFromPoint_(WindowID(window0),WindowMouseX(window0),WindowMouseY(window0))
        Case GadgetID(btn0)
          Debug "Right button down on button 0"
        Case GadgetID(btn1)
          Debug "Right button down on button 1"
        EndSelect
      EndIf 
    Case #WM_CONTEXTMENU
      Select ChildWindowFromPoint_(WindowID(window0),WindowMouseX(window0),WindowMouseY(window0))
        Case GadgetID(btn0)
          Debug "Right button up on button 0"
        Case GadgetID(btn1)
          Debug "Right button up on button 1"
      EndSelect
  EndSelect
  Result = #PB_ProcessPureBasicEvents
  ProcedureReturn Result
EndProcedure

window0=OpenWindow(#PB_Any,0,0,400,300,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(window0))
PanelGadget(Panel_main, 0, 0, 490, 390)
AddGadgetItem(Panel_main, 1, "panel1")
btn0=ButtonGadget(#PB_Any,120,80,160,46," Button 0")
btn1=ButtonGadget(#PB_Any,120,140,160,46," Button 1")
AddGadgetItem(Panel_main, 2, "panel2")

SetWindowCallback(@ButtonCallback())
Repeat   
  ev=WaitWindowEvent()
   If ev=#PB_Event_Gadget
      ; Do something useful
  EndIf
Until ev=#PB_Event_CloseWindow 
End 
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Panels are a special case where child windows are involved as the panel items will cause a doubling up of messages such as #WM_PARENTNOTIFY. As a result, you have to filter these panel-item window handles out of tests like this. This will catch right-clicks on all your buttons, whether on a panelgadget or not:

Code: Select all

Global panel_main,window0,btn0,btn1,button_test 

Procedure ButtonCallback(hWnd, Message, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 
  Select message 
    Case #WM_PARENTNOTIFY 
      If wParam & $FFFF = #WM_RBUTTONDOWN And hwnd = WindowID(window0)
        result=0
        Select WindowFromPoint_(DesktopMouseX(),DesktopMouseY()) 
        Case GadgetID(btn0) 
          Debug "Right button down on button 0" 
        Case GadgetID(btn1) 
          Debug "Right button down on button 1" 
        EndSelect 
      EndIf 
    Case #WM_CONTEXTMENU 
      result=0
      Select WindowFromPoint_(DesktopMouseX(),DesktopMouseY()) 
        Case GadgetID(btn0) 
          Debug "Right button up on button 0" 
        Case GadgetID(btn1) 
          Debug "Right button up on button 1" 
      EndSelect 
  EndSelect 
  ProcedureReturn Result 
EndProcedure 

window0=OpenWindow(#PB_Any,0,0,400,300,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(window0)) 
panel_main = PanelGadget(#PB_Any, 0, 0, 490, 390) 
AddGadgetItem(Panel_main, 0, "panel1") 
btn0=ButtonGadget(#PB_Any,120,80,160,46," Button 0") 
btn1=ButtonGadget(#PB_Any,120,140,160,46," Button 1") 
AddGadgetItem(Panel_main, 1, "panel2") 

SetWindowCallback(@ButtonCallback()) 
Repeat    
  ev=WaitWindowEvent() 
   If ev=#PB_Event_Gadget 
      ; Do something useful 
  EndIf 
Until ev=#PB_Event_CloseWindow 
End 
Last edited by netmaestro on Wed Dec 06, 2006 9:10 am, edited 1 time in total.
BERESHEIT
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Post by Psychophanta »

Nice tips.
Netmaestro, this last yours doesn't work here.
DoubleDutch wrote:I don't understand why buttons can't always have the #PB_EventType_RightClick enabled too?

This would be far more consistant with the other gadgets.

Fred, is there a reason for this?

If its because the Mac only has one button, most people I know who have Macs go out and buy a two button mouse straight away (the Mac OS supports 2 buttons).

-Anthony
I think a good frontend OS should allow unlimited number of buttons on every control, not only 2: LMB and RMB...
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I made a couple small tweaks, dunno if it helps. I can't tell if it's having an effect or not as both versions are working here.
BERESHEIT
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Post by Psychophanta »

netmaestro wrote:I made a couple small tweaks, dunno if it helps. I can't tell if it's having an effect or not as both versions are working here.
It works now :)
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
aaron
Enthusiast
Enthusiast
Posts: 267
Joined: Mon Apr 19, 2004 3:04 am
Location: Canada
Contact:

Post by aaron »

Thanks netmaestro! Works great here. :D I'm back in business. :P
Post Reply