I want to use the Text Object Model to insert a row in a table at the caret position in a rich text control using the C programming language (Pelles C).
I tried the following code:
HRESULT hr;
ITextRange2 *textRange2 = NULL;
ITextRow *textRow = NULL;
hr = TextDocument2->lpVtbl->Range2(TextDocument2, 0, 0, &textRange2);
hr = textRange2->lpVtbl->GetRow(textRange2, &textRow);
hr = textRow->lpVtbl->Insert(textRow, numbRows);
hr = textRow->lpVtbl->Release(textRow);
textRange2->lpVtbl->Release(textRange2);
However, this inserts a row at the top of the table instead of inserting a row above or below the row where the caret is located. I am convinced that the line:
hr = TextDocument2->lpVtbl->Range2(TextDocument2, 0, 0, &textRange2);
locates the row containing the caret as I have used this to delete a row containing the caret.
I am using the TOM.h file from https://github.com/tpn/winsdk-10/blob/master/Include/10.0.14393.0/um/TOM.h
How should I amend the code to insert a row at the current caret position?
**** Update****
After hours of experimentation, I have come up with the following solution to insert a row in a table. The new row is inserted below the row containing the caret.
void TOM_tblInsertRow(long numbRows){
ITextRange2 *textRange2 = NULL;
ITextRow *textRow = NULL;
long rowIndex = 0;
TextDocument2->lpVtbl->Range2(TextDocument2, 0, 0, &textRange2);
ITextSelection *textSelection = NULL;
TextDocument2->lpVtbl->GetSelection(TextDocument2, &textSelection);
textSelection->lpVtbl->GetIndex(textSelection, tomSelectionRow, &rowIndex);
textSelection->lpVtbl->Release(textSelection);
textRange2->lpVtbl->Move(textRange2, tomSelectionRow, rowIndex, &rowIndex);
textRange2->lpVtbl->GetRow(textRange2, &textRow);
textRow->lpVtbl->Insert(textRow, numbRows);
textRow->lpVtbl->Release(textRow);
textRange2->lpVtbl->Release(textRange2);
}
However, this will not insert a row if the caret is in the last row of the table. How can I overcome this issue?