Right click on Editor

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

Right click on Editor

Post by es_91 »

It is still not possible to perform and catch a right click event on an EditorGadget....

PB_EventType_RightClick
:mrgreen:
BarryG
Addict
Addict
Posts: 4136
Joined: Thu Apr 18, 2019 8:17 am

Re: Right click on Editor

Post by BarryG »

flashbob
User
User
Posts: 92
Joined: Sat May 11, 2024 4:04 pm

Re: Right click on Editor

Post by flashbob »

Perhaps this helps:

Code: Select all

EnableExplicit
Enumeration FormGadget
  #myPopup
  #myGadget
EndEnumeration

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  If uMsg = #WM_CONTEXTMENU
    If wParam = GadgetID(#myGadget) 
      DisplayPopupMenu(#myPopup, WindowID(0),DesktopMouseX()+5, DesktopMouseY())           
    EndIf
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0, 0, 0, 600, 400,"Right Click Edit Gadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreatePopupMenu(#myPopup) 
  MenuItem(0, "Menu 1")
  MenuItem(1, "Menu 2")
  MenuItem(2, "Menu 3")
  
EditorGadget(#myGadget, 10, 10, 580, 350)
SetWindowCallback(@WndProc())  

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Right click on Editor

Post by mk-soft »

Global WM_ContextMenu

Code: Select all

EnableExplicit

Enumeration #PB_EventType_FirstCustomValue
  #MyEventType_ContextMenu
EndEnumeration

Enumeration FormMenu
  #myPopupEdit
  #myPopupList
EndEnumeration

Enumeration FormMenuItems
  #myMenuEdit1
  #myMenuEdit2
  #myMenuEdit3
  #myMenuList1
EndEnumeration

Enumeration FormGadget
  #myEditGadget
  #myListGadget
EndEnumeration

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_CONTEXTMENU
      PostEvent(#PB_Event_Gadget, EventWindow(), GetDlgCtrlID_(wParam), #MyEventType_ContextMenu)
  EndSelect
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0, 0, 0, 600, 500,"Right Click Edit Gadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

CreatePopupMenu(#myPopupEdit) 
MenuItem(#myMenuEdit1, "Menu Edit 1")
MenuItem(#myMenuEdit2, "Menu Edit 2")
MenuItem(#myMenuEdit3, "Menu Edit 3")

CreatePopupMenu(#myPopupList) 
MenuItem(#myMenuList1, "Menu List 1")

EditorGadget(#myEditGadget, 10, 10, 580, 250)
ListViewGadget(#myListGadget, 10, 270, 580, 200)

SetWindowCallback(@WndProc())  

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #myEditGadget
          Select EventType()
            Case #MyEventType_ContextMenu
              DisplayPopupMenu(#myPopupEdit, WindowID(0),DesktopMouseX()+5, DesktopMouseY())
          EndSelect
        Case #myListGadget
          Select EventType()
            Case #MyEventType_ContextMenu
              DisplayPopupMenu(#myPopupList, WindowID(0),DesktopMouseX()+5, DesktopMouseY())
          EndSelect
          
      EndSelect
      
  EndSelect
ForEver

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Axolotl
Addict
Addict
Posts: 804
Joined: Wed Dec 31, 2008 3:36 pm

Re: Right click on Editor

Post by Axolotl »

great examples guys.
We just have to bear in mind that the context menu can also be generated from the keyboard (i.e. #VK_APPS key or SHIFT+F10) and the mouse can be positioned anywhere on the screen.
I have taken the liberty of revising your example. Details can be found in the code.

Code: Select all

EnableExplicit

Enumeration #PB_EventType_FirstCustomValue
  #MyEventType_ContextMenu
EndEnumeration

Enumeration FormMenu
  #myPopupEdit
  #myPopupList
EndEnumeration

Enumeration FormMenuItems
  #myMenuEdit1
  #myMenuEdit2
  #myMenuEdit3
  #myMenuList1
EndEnumeration

Enumeration FormGadget
  #myEditGadget
  #myListGadget
EndEnumeration

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_CONTEXTMENU
      ; !! add an additional parameter lParam (for the position) 
      ;    so we can detect if the context menu is generated from the keyboard 
      PostEvent(#PB_Event_Gadget, EventWindow(), GetDlgCtrlID_(wParam), #MyEventType_ContextMenu, lParam) 
  EndSelect 
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure


Procedure.w GET_X_LPARAM(lParam)  ; return X.w (signed) 
  ProcedureReturn lParam 
EndProcedure 

Procedure.w GET_Y_LPARAM(lParam)  ; return Y.w (signed) 
  ProcedureReturn lParam >> 16 
EndProcedure 


Procedure ShowPopupMenu(Menu, Window, Gadget, LParam) 
  Protected menupos.POINT 

  menupos\x = GET_X_LPARAM(LParam) 
  menupos\y = GET_Y_LPARAM(LParam) 

  If menupos\x = -1 And menupos\y = -1    ; <-- #VK_APPS key or Shift + F10 pressed 
    menupos\x = GadgetX(Gadget) + 16  
    menupos\y = GadgetY(Gadget) + 16
    ClientToScreen_(WindowID(Window), @menupos)  ; <-- adjust to screen coordinates 
  EndIf 
  DisplayPopupMenu(Menu, WindowID(Window), menupos\x + 5, menupos\y) 
EndProcedure 


OpenWindow(0, 0, 0, 600, 500,"Right Click Edit Gadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

CreatePopupMenu(#myPopupEdit) 
MenuItem(#myMenuEdit1, "Menu Edit 1")
MenuItem(#myMenuEdit2, "Menu Edit 2")
MenuItem(#myMenuEdit3, "Menu Edit 3")

CreatePopupMenu(#myPopupList) 
MenuItem(#myMenuList1, "Menu List 1")

EditorGadget(#myEditGadget, 10, 10, 580, 250)
ListViewGadget(#myListGadget, 10, 270, 580, 200)

SetWindowCallback(@WndProc())  

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #myEditGadget
          Select EventType()
            Case #MyEventType_ContextMenu
            ; DisplayPopupMenu(#myPopupEdit, WindowID(0),DesktopMouseX()+5, DesktopMouseY())
              ShowPopupMenu(#myPopupEdit, 0, #myEditGadget, EventData()) ; EventData() is LPARAM see above !!! 
              
          EndSelect
        Case #myListGadget
          Select EventType()
            Case #MyEventType_ContextMenu
            ; DisplayPopupMenu(#myPopupList, WindowID(0),DesktopMouseX()+5, DesktopMouseY())
              ShowPopupMenu(#myPopupList, 0, #myListGadget, EventData()) ; EventData() is LPARAM see above !!! 
          EndSelect
          
      EndSelect
      
  EndSelect
ForEver
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply