vb.netwinformsxpcomgeckofxhtml-editor

How to access nsIHTMLEditor interface in GeckoFX?


I am trying to make a WYSIWYG HTML-editor by embedding GeckoFX in a Windows Forms application in VB.NET.

The code goes like this:

Dim Gbrowser As New GeckoWebBrowser
Gbrowser.Navigate("about:blank")
...
Gbrowser.Navigate("javascript:void(document.body.contentEditable='true')")

How can I activate and access the nsIHTMLEditor interface from within my application?

Thank you.

UPDATE
This code does not work:

Dim hEditor As nsIHTMLEditor
hEditor = Xpcom.GetService(Of nsIHTMLEditor)("@mozilla.org/editor/htmleditor;1")
hEditor = Xpcom.QueryInterface(Of nsIHTMLEditor)(hEditor)
hEditor.DecreaseFontSize()

Error in the last line: HRESULT E_FAIL has been returned from a call to a COM component.


Solution

  • nsIHTMLEditor is likely a per browser instance rather than a global instance (like things returned by Xpcom.GetService)

    One can get a nsIEditor like this by (by supplying a Window instance)

    var editingSession = Xpcom.CreateInstance<nsIEditingSession>("@mozilla.org/editor/editingsession;1");
    nsIEditor editor = editingSession.GetEditorForWindow((nsIDOMWindow)Window.DomWindow);
    Marshal.ReleaseComObject(editingSession);
    

    (or you can just call the nsIEditor GeckoWebBrowser.Editor property.)

    You may be able to cast this nsIEditor to a nsIHtmlEditor (although I have yet to try it)

    GeckoWebBrowser browser = .....;
    // Untested code
    nsIHTMLEditor htmlEditor = (nsIHTMLEditor)browser.Editor;
    

    Update: The VB code from @GreenBear

    Dim gEditor As nsIHTMLEditor: 
    gEditor = Gbrowser.Editor: 
    gEditor.DecreaseFontSize()