1
votes

I am using NSTextView for the user to type their response. I have turned on continuous spell check and the incorrect words have a red line under them. I have a shared SpellChecker which I am using. But I want the user to do a right click and correct the spelling. I have the following questions -

  1. In Text edit, as soon as you do a right click, it highlights the word, how can I achieve the same ?. I am planning to get the selectedrange and pass it to the spellchecker.

  2. Once the spellcheck suggestions are added to the context menu, it still shows up when I go to the next word, so should I always clear the context menu before adding any new item ?.

  3. I am using Monobjc, and when I execute the following code, I get an exception -

    var t = Menu.ItemArray;

    foreach(var s in t)

    {

    var menuItem = s.CastAs<NSMenuItem>(); //exception

    }

I am basically try to get individual NSMenuItem to check if it spelling suggestions or something else.

EDIT - Added stacktrace

System.InvalidCastException: Cannot cast from source type to destination type.
  at example.test.MacOS.UI.HTML.ESView.MenuForEvent (Monobjc.Cocoa.NSEvent theEvent) [0x00000] in <filename unknown>:0 
  at Monobjc.Dynamic.Proxies.example.test.MacOS.UI.HTML.ESView.MenuForEvent_Monobjc_Cocoa_NSEvent (IntPtr receiver, IntPtr selector, IntPtr ) [0x00000] in <filename unknown>:0 
  at (wrapper native-to-managed) Monobjc.Dynamic.Proxies.example.test.MacOS.UI.HTML.ESView:MenuForEvent_Monobjc_Cocoa_NSEvent (intptr,intptr,intptr)
  at (wrapper managed-to-native) E5EEC20A:pinvoke (intptr,intptr,intptr)
  at E5EEC20A.objc_msgSendSuper (IntPtr receiver, IntPtr selector, System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at Monobjc.Bridge.Generators.DynamicMessagingGenerator.SendMessage (System.String message, IntPtr receiver, IntPtr selector, System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at Monobjc.ObjectiveCRuntime.SendMessageSuper (IManagedWrapper receiver, Monobjc.Class cls, System.String selector, System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at Monobjc.Id.SendMessageSuper (Monobjc.Class cls, System.String selector, System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at example.test.MacOS.UI.App.Application.SendEvent (Monobjc.Cocoa.NSEvent theEvent) [0x00000] in <filename unknown>:0 
  at Monobjc.Dynamic.Proxies.example.test.MacOS.UI.App.Application.SendEvent_Monobjc_Cocoa_NSEvent (IntPtr receiver, IntPtr selector, IntPtr ) [0x00000] in <filename unknown>:0 
  at (wrapper native-to-managed) Monobjc.Dynamic.Proxies.example.test.MacOS.UI.App.Application:SendEvent_Monobjc_Cocoa_NSEvent (intptr,intptr,intptr)
  at (wrapper managed-to-native) Monobjc.ObjectiveCRuntime:objc_msgSend (intptr,intptr)
  at Monobjc.ObjectiveCRuntime.SendMessage (IManagedWrapper receiver, System.String selector) [0x00000] in <filename unknown>:0 
  at Monobjc.Cocoa.NSApplication.Run () [0x00000] in <filename unknown>:0 
  at example.test.MacOS.UI.App.Application.RunApplication () [0x00000] in <filename unknown>:0 
  at example.test.MacOS.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 

EDIT - Step 3

Question 3 worked based on Laurent's suggestion.

1
Can you post the stack trace of the exception ?Laurent Etiemble
Can you try to use the "numberOfItems" and "itemAtIndex" methods within an indexed loop ?Laurent Etiemble
@Laurent - Sure I will try that out. Also can you please suggest an answer for points 1 and 2 which I have asked above.Prashant

1 Answers

3
votes

For point 1)

  • NSTextView.SelectedRange returns the first selected range. If no text is selected the range's length is 0 and the range's location is the index of the character where the cursor lies.
  • Then, get a reference to the string context (NSTextView.TextStorage.String) and search for whitespace before and after the cursor location. You will get the range for the word to select.
  • Use NSTextView.SelectedRange to specify the new selection range.

For point 2)

You have a total control over the Menu instance returned when a right-click is done. So if you add items to it, you have to remove them by yourself.

For point 3)

It looks like a bug. As mention in the comment, use the NumberOfItems and ItemAtIndexmethods within an indexed loop.