Page 1 of 1

Right click Editor Gadget

Posted: Sat Mar 19, 2016 6:39 am
by coco2
How do I detect if right click event has happened on an editor gadget? I only want the popup menu to show if the mouse is over the editor.

Re: Right click Editor Gadget

Posted: Sat Mar 19, 2016 9:06 am
by BasicallyPure
The editor gadget does not support right click events.
If you are using windows you can generate them like this.

Code: Select all

#MainWin = 0
#EdGad   = 1
#PopMenu = 2
#MenuItemQuit = 3

Procedure WinCallback(hWnd, uMsg, WParam, LParam) 
   
   If WParam = #WM_RBUTTONDOWN
      If GetActiveGadget() = #EdGad
         PostEvent(#PB_Event_Gadget, #MainWin, #EdGad, #PB_EventType_RightClick)
      EndIf
   EndIf
   
   ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure

OpenWindow(#MainWin,0,0,800,600,"", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
SetWindowColor(#MainWin,$F9F1CC)
SetWindowCallback(@WinCallback())
EditorGadget(#EdGad,10,10,600,400)
CreatePopupMenu(#PopMenu)
   MenuItem(#MenuItemQuit,"Quit")

Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
         Break
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #EdGad
               If EventType() = #PB_EventType_RightClick
                  DisplayPopupMenu(#PopMenu,WindowID(#MainWin))
               EndIf
         EndSelect
      Case #PB_Event_Menu
         If EventMenu() = #MenuItemQuit
            Break
         EndIf
   EndSelect
ForEver

Re: Right click Editor Gadget

Posted: Sat Mar 19, 2016 11:53 am
by Shardik

Re: Right click Editor Gadget

Posted: Sat Mar 19, 2016 9:49 pm
by coco2
Thanks for the replies. I would have thought it should be included in PureBasic. It was the first program I ever made and it was a simple one, click a button to generate some text in an editor.

Re: Right click Editor Gadget

Posted: Sat Jul 24, 2021 2:51 pm
by Little John
Hi BasicallyPure,

thank you for the code. It works fine here on Windows 10.

However, when using it with an EditorGadget that is inside a PanelGasget, it does not work as expected: After clicking at a context menu item, the intended action does not take place, but the contect menu appears again instead. And when clicking again at the same menu item, then the intended action is performed twice, if possible -- not possible in this example with "Break", but try with

Code: Select all

SendMessage_(GadgetID(#EdGad), #WM_PASTE, 0, 0)
To demonstrate the issue, please see the following code, that contains only 2 additional lines.
Does anyone know how to solve the problem?

Code: Select all

#MainWin = 0
#EdGad   = 1
#PopMenu = 2
#MenuItemQuit = 3

Procedure WinCallback(hWnd, uMsg, WParam, LParam) 
   If WParam = #WM_RBUTTONDOWN
      If GetActiveGadget() = #EdGad
         PostEvent(#PB_Event_Gadget, #MainWin, #EdGad, #PB_EventType_RightClick)
      EndIf
   EndIf
   
   ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure

OpenWindow(#MainWin,0,0,800,600,"", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
SetWindowColor(#MainWin,$F9F1CC)
SetWindowCallback(@WinCallback())

PanelGadget(0, 10, 10, 600, 400)   ; <= added
AddGadgetItem(0, 1, "new")         ; <= added

EditorGadget(#EdGad,0,0,600,400)
CreatePopupMenu(#PopMenu)
MenuItem(#MenuItemQuit,"Quit")

Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
         Break
         
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #EdGad
               If EventType() = #PB_EventType_RightClick
                  DisplayPopupMenu(#PopMenu,WindowID(#MainWin))
               EndIf
         EndSelect
         
      Case #PB_Event_Menu
         If EventMenu() = #MenuItemQuit
            Break
         EndIf
   EndSelect
ForEver

Re: Right click Editor Gadget

Posted: Sat Jul 24, 2021 3:07 pm
by firace
Hi Little John, try out this solution:

Code: Select all

#MainWin = 0
#EdGad   = 1
#PopMenu = 2
#MenuItemQuit = 3



Procedure EditorCallback(hwnd, msg, wparam, lparam)
  Shared oldproc
  Protected result = CallWindowProc_(oldproc, hwnd, msg, wparam, lparam)
  
  If  msg = #WM_RBUTTONDOWN
    PostEvent(#PB_Event_Gadget, #MainWin, #EdGad, #PB_EventType_RightClick)
    
  EndIf
  ProcedureReturn result
EndProcedure



OpenWindow(#MainWin,0,0,800,600,"", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
SetWindowColor(#MainWin,$F9F1CC)

PanelGadget(220, 10, 10, 600, 400)   ; <= added
AddGadgetItem(220, 1, "new")         ; <= added

EditorGadget(#EdGad,0,0,600,400)

oldproc = SetWindowLongPtr_(GadgetID(#EdGad), #GWL_WNDPROC, @EditorCallback())

CreatePopupMenu(#PopMenu)
MenuItem(#MenuItemQuit,"Quit")

Repeat
  e = WaitWindowEvent()
  Select e
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #EdGad
          If EventType() = #PB_EventType_RightClick
            DisplayPopupMenu(#PopMenu,WindowID(#MainWin))
          EndIf
      EndSelect
      
    Case #PB_Event_Menu
      If EventMenu() = #MenuItemQuit
        Break
      EndIf
  EndSelect
Forever

Re: Right click Editor Gadget

Posted: Sat Jul 24, 2021 3:31 pm
by RASHAD
Hi
BasicallyPure not active for long time
Try

Code: Select all

#MainWin = 0
#EdGad   = 1
#PopMenu = 2
#MenuItemQuit = 3

Procedure WinCallback(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_CONTEXTMENU
      Select wParam
        Case GadgetID(#EdGad)         
          DisplayPopupMenu(0, WindowID(0))          
          
      EndSelect
  EndSelect
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(#MainWin,0,0,800,600,"", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
SetWindowColor(#MainWin,$F9F1CC)
SetWindowCallback(@WinCallback())

PanelGadget(0, 10, 10, 600, 400)   ; <= added
AddGadgetItem(0, 1, "new")         ; <= added

EditorGadget(#EdGad,0,0,600,400)
If CreatePopupMenu(0)
  MenuItem(1, "Cut")
  MenuItem(2, "Copy")
  MenuItem(3, "Paste")
  MenuBar()
  OpenSubMenu("Options")
  MenuItem(4, "Window...")
  MenuItem(5, "Gadget...")
  CloseSubMenu()
  MenuBar()
  MenuItem( 6, "Quit")
EndIf    

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Menu
      
      Select EventMenu()  ; To see which menu has been selected
          
        Case 1 ; Cut
          SendMessage_(GadgetID(#EdGad),#WM_CUT,0,0)
          
        Case 2 ; Copy
          SendMessage_(GadgetID(#EdGad),#WM_COPY,0,0)
          
        Case 3 ; Paste
          SendMessage_(GadgetID(#EdGad),#WM_PASTE,0,0)
          
        Case 6 ; Quit
          Q = 1
          
      EndSelect  
  EndSelect
Until Q = 1

Re: Right click Editor Gadget

Posted: Sat Jul 24, 2021 3:56 pm
by Little John
Hi firace and RASHAD,

both of your solutions work like a charm here!
Now I can add a context menu to my program. :D
THANK YOU VERY MUCH!