Page 1 of 1

[Solved] Append in input mode, leave in input mode

Posted: Wed Jan 28, 2015 7:21 pm
by belchergb
I am editting a cell (it is in input mode). I want a macro (mapped to a key combination) that will:

1> retrieve the characters so far entered
2> append a fixed string to them
3> exit leaving the cell in input mode

Then I will finish typing in the cell and press enter. The cell would now have what I typed with the appended string effectively inserted at the point where I pressed the mapped key(s).

I have been unable to find a way to accomplish 1> while in input mode. I imagine I will need to find the data in the controller, but have not been able to so far. I suspect there may be some other trickery required, as well.

I also have been unable to find how to switch a cell in or out of input mode short of using DispatchHelper (ugh!).

Any ideas? Thanks.

Re: Append in input mode, leave in input mode

Posted: Wed Jan 28, 2015 7:51 pm
by B Marcelly
Paste from clipboard works well in input mode.

Re: Append in input mode, leave in input mode

Posted: Wed Jan 28, 2015 8:07 pm
by hanya

Code: Select all

Sub AppendSuffixOnCellInputMode
  oSelection = ThisComponent.getCurrentSelection()
  if oSelection.supportsService("com.sun.star.sheet.SheetCell") then
    oFound = GetCellAccessibleEditableParagraph()
    If not IsNull(oFound) Then
      oFound.insertText("_suffix", oFound.getCharacterCount())
    End If
  end if
End Sub

Function GetCellAccessibleEditableParagraph() As Variant
  oResult = Null
  oAccCtx = ThisComponent.getCurrentController().getFrame().getComponentWindow().getAccessibleContext()
  oDocumentPanelAcc = FindChildByRole(oAccCtx, com.sun.star.accessibility.AccessibleRole.SCROLL_PANE)
  If not IsNull(oDocumentPanelAcc) Then
    oDocumentAcc = FindChildByRole(oDocumentPanelAcc.getAccessibleContext(), com.sun.star.accessibility.AccessibleRole.DOCUMENT)
    If IsNull(oDocumentAcc) Then oDocumentAcc = FindChildByRole(oDocumentPanelAcc.getAccessibleContext(), com.sun.star.accessibility.AccessibleRole.DOCUMENT_SPREADSHEET)
    For i = 0 to oDocumentAcc.getAccessibleChildCount() -1 step 1
      oAccChild = oDocumentAcc.getAccessibleChild(i)
      If oAccChild.getAccessibleRole() = com.sun.star.accessibility.AccessibleRole.TEXT_FRAME Then
        if oAccChild.getAccessibleChildCount() = 1 then
          oAccPara = oAccChild.getAccessibleChild(0)
          if oAccPara.getAccessibleRole() = com.sun.star.accessibility.AccessibleRole.PARAGRAPH Then
            oResult = oAccPara
            exit for
          End If
        end if
      End If
    next
  End If
  GetCellAccessibleEditableParagraph = oResult
End Function

Function FindChildByRole(oCtx As Variant, nRole As Integer) As Variant
  oFound = NULL
  For i = 0 to oCtx.getAccessibleChildCount() - 1 step 1
    oChild = oCtx.getAccessibleChild(i)
    oChildCtx = oChild.getAccessibleContext()
    If oChildCtx.getAccessibleRole() = nRole then
      oFound = oChild
      exit for
    End If
  next
  FindChildByRole = oFound
End Function
I do not know the way for >3, exit from input mode.

Re: Append in input mode, leave in input mode

Posted: Thu Jan 29, 2015 3:10 am
by belchergb
My gawd! If that is what it takes to do what I want, it is no wonder I could not figure out how. I am going to have to study this just to see what it does.

It needs one thing more, if it can be done.

If I type "ASDF^" in the cell, where the carat, ^, represents the cursor or text entry point, then press my mapped key calling the macro, then "_suffix" is appended at the cursor point, giving "ASDF^_suffix", and it is still in input mode. Fabulous!

The problem is that when I continue typing, "MORE" for example, the characters go in at the cursor point, like "ASDFMORE^_suffix". The result I want is "ASDF_suffixMORE". I want the cursor moved to the end of the appended "_suffix" when the macro exits.

Is that possible?

Re: Append in input mode, leave in input mode

Posted: Thu Jan 29, 2015 4:11 am
by hanya
belchergb wrote:The problem is that when I continue typing, "MORE" for example, the characters go in at the cursor point, like "ASDFMORE^_suffix". The result I want is "ASDF_suffixMORE". I want the cursor moved to the end of the appended "_suffix" when the macro exits.
Add the following after the insertText call:

Code: Select all

oFound.setCaretPosition(oFound.getCharacterCount())
Useful interfaces on this object are:
http://www.openoffice.org/api/docs/comm ... eText.html
http://www.openoffice.org/api/docs/comm ... eText.html
Please Inspect the object for additional interfaces.

Re: [Solved] Append in input mode, leave in input mode

Posted: Thu Jan 29, 2015 4:37 am
by belchergb
Thank you, thank you! It is perfect.