How to enable right click contextual menu in textarea?

I am trying to enable windows default right click contextual menu in textarea, just like in right click in textfield, but cannot found a way after various searching.

How to do it?, i only need default windows one which are having Copy, Paste, select all,etc.

Implement ConstructContextualMenu & ContextualMenuAction

There are no settings for the inspector to do it automatically?

I need to implement it by myself?

Found the solution after search through forums!,

Here it is, probably will be useful to others :

Function ConstructContextualMenu(base as MenuItem, x as Integer, y as Integer) Handles ConstructContextualMenu as Boolean
  base.AddMenu(New MenuItem("Copy"))
  base.AddMenu(New MenuItem("Cut"))
  base.AddMenu(New MenuItem("Paste"))
  base.AddMenu(New MenuItem("Delete"))
  base.AddMenu(New MenuItem("Select All"))
End Function
Function ContextualMenuAction(hitItem as MenuItem) Handles ContextualMenuAction as Boolean
  Dim clippy As new Clipboard
  Select Case hititem.Text
  Case "Copy"
    clippy.text = Me.SelectedText.ToText
  Case "Cut"
    if me.sellength=0 then return true
    dim t as string, i, curPos as integer
    me.copy
    curPos=me.selstart
    for i=0 to me.selstart
      t=t+mid(me.text, i,1)
    next i
    for i=me.selstart+me.SelLength+1 to len(me.text)
      t=t+mid(me.text, i, 1)
      if i>len(me.text) then exit for
    next i
    me.text=t
    me.selstart=curPos
    me.selLength=0
  Case "Paste"
    Me.Paste
  Case "Delete"
    if me.sellength=0 then return true
    dim t as string, i, curPos as integer
    curPos=me.selstart
    for i=0 to me.selstart
      t=t+mid(me.text, i,1)
    next i
    for i=me.selstart+me.SelLength+1 to len(me.text)
      t=t+mid(me.text, i, 1)
      if i>len(me.text) then exit for
    next i
    me.text=t
    me.selstart=curPos
    me.selLength=0
  Case "Select All"
    me.selstart=0
    me.SelLength=me.text.Len
  End Select
  
  Return True
End Function

Or read the docs (Internet) / Language Reference ([LOCAL].

Or in your Xojo folder see Example Projects --> Desktop --> Menus --> ConstructContextualMenu.xojo_binary_project.

The only problem with implementing:

#If TargetWindows Then
  
  WOComments.SetFocus
  
  If IsContextualClick Then
    Var m As MenuItem
    m = EditMenu.Clone.PopUp
  End If
  
#EndIf

The normal context menu for Spell Checking doesn’t occur. Only the EditMenu.Clone.PopUp shows.

How do I have both Cut/Copy/Paste and the Spell Check options appear in one PupUp for a TextArea?

To do what automatically? How can it know what you want in any particular contextual menu.

lol, the normal contextual menu expected for a Text control, like one in TextField :roll_eyes: He already aswer, Xojo has not the standard contextual menu in the Text Area, so it has to be added manually.

It forces a limitation but you could do something like this in the ConstructContextualMenu event

If Me.SelectedText.Length > 0 Then
  Var m As MenuItem
  m = EditMenu.Clone.PopUp
  Return True
End If

Return False

This doesn’t really show both in the same menu but will show the Editmenu clone if there is text selected in the TextArea and the spell checker if there is no text selected (assuming the cursor is in a misspelled word).

So Paste won’t appear when no selection is made.
I don’t think there’s an ideal solution, other than getting the handle of the menu and inserting items (I don’t remember right now whether Xojo provides the handle, though).

That’s true but if there is anything to Paste, the keyboard shortcut still works. I agree that it’s not a great solution.

True, as the shortcuts come from the Edit menu, which stays there (so the Copy and Cut shortcuts always work as well).
The real problem is only the lack of a customisable contextual menu.

For those finding this thread online, here’s an update of Aditya’s kind code to make it compatible with DesktopTextArea.

Function ConstructContextualMenu(base As DesktopMenuItem, x As Integer, y As Integer) Handles ConstructContextualMenu as Boolean
  #If TargetWindows
    // Add menus
    base.AddMenu(New MenuItem("Cut"))
    base.AddMenu(New MenuItem("Copy"))
    base.AddMenu(New MenuItem("Paste"))
    base.AddMenu(New MenuItem("Delete"))
    base.AddMenu(New DesktopMenuItem( "-" ))
    base.AddMenu(New MenuItem("Select All"))
    
    Return True
  #EndIf
  
End Function
Function ContextualMenuItemSelected(selectedItem As DesktopMenuItem) Handles ContextualMenuItemSelected as Boolean
  #If targetwindows
    
    Dim clippy As New Clipboard
    
    Select Case selecteditem.Text
    Case "Copy"
      clippy.Text = Me.SelectedText.ToText
    Case "Cut"
      If Me.SelectedText.Length=0 Then Return True
      Dim t As String, i, curPos As Integer
      Me.copy
      curPos=Me.SelectionStart
      For i=0 To Me.SelectionStart
        t=t+Mid(Me.Text, i,1)
      Next i
      For i=Me.SelectionStart+Me.SelectedText.Length+1 To Len(Me.Text)
        t=t+Mid(Me.Text, i, 1)
        If i>Len(Me.Text) Then Exit For
      Next i
      Me.Text=t
      Me.SelectionStart=curPos
      Me.SelectionLength=0
    Case "Paste"
      Me.Paste
    Case "Delete"
      If Me.SelectedText.Length=0 Then Return True
      Dim t As String, i, curPos As Integer
      curPos=Me.SelectionStart
      For i=0 To Me.SelectionStart
        t=t+Mid(Me.Text, i,1)
      Next i
      For i=Me.SelectionStart+Me.SelectedText.Length+1 To Len(Me.Text)
        t=t+Mid(Me.Text, i, 1)
        If i>Len(Me.Text) Then Exit For
      Next i
      Me.Text=t
      Me.SelectionStart=curPos
      Me.SelectionLength=0
    Case "Select All"
      Me.SelectionStart=0
      Me.SelectionLength=Me.Text.Length
    End Select
    
    Return True
  #EndIf
End Function

(Edited to fix/remove double popup workaround)

1 Like

Interesting. Do they enable themselves automatically? (sorry, I don’t have a Windows setup on hand to try).

Windows finally supports disabled separators in menus?

Thanks for sharing.

Yes. But they don’t currently disable themselves appropriately if there’s no selection, etc.

Sorry, I should’ve asked with “correctly” instead of “automatically”.
This was indeed my question: do these items enable/disable as they should?

So, basically, it’s reinventing what the OS already does.
For example, a TextArea supports pasting RTF data natively. You’ll have to add support for that too, and know each flavour you’re supposed to support, to not change user’s habits.

You’re right. This is probably better. I clone the Edit menu and then remove my extraneous menu items.

Function ConstructContextualMenu(base As DesktopMenuItem, x As Integer, y As Integer) Handles ConstructContextualMenu as Boolean
  #If TargetWindows
    Var m As DesktopMenuItem
    m = EditMenu.Clone
    
    If M = Nil Then Return False
    
    For i As Integer = m.count-1 DownTo 8
      m.RemoveMenuAt(i)
    Next
    
    Call m.PopUp()
    
    Return True
  #EndIf
  
  
End Function

No ContextualMenuItemSelected event needed.

1 Like