Right click Editor Gadget

Just starting out? Need help? Post your questions and find answers here.
coco2
Enthusiast
Enthusiast
Posts: 254
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Right click Editor Gadget

Post 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.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 536
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Right click Editor Gadget

Post 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
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
Shardik
Addict
Addict
Posts: 1840
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Right click Editor Gadget

Post by Shardik »

coco2
Enthusiast
Enthusiast
Posts: 254
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Right click Editor Gadget

Post 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.
Little John
Addict
Addict
Posts: 4007
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Right click Editor Gadget

Post 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
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups
firace
Enthusiast
Enthusiast
Posts: 728
Joined: Wed Nov 09, 2011 8:58 am

Re: Right click Editor Gadget

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4108
Joined: Sun Apr 12, 2009 6:27 am

Re: Right click Editor Gadget

Post 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
Egypt my love
Little John
Addict
Addict
Posts: 4007
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Right click Editor Gadget

Post 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!
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups
Post Reply