How to determin, if selected text is inside a table cell?

General TRichView support forum. Please post your questions here
Post Reply
Bulvaye
Posts: 10
Joined: Wed Oct 12, 2005 2:08 pm

How to determin, if selected text is inside a table cell?

Post by Bulvaye »

Hello,

let me explain, what I'm going to do:

My RVEdit will load any RichText files with or w/o tables. Each document contains macro names like "{$name}", which will be replaced by the program with certain strings. So far so good.

Now, it's necessary to determin, if a specific macro name is inside a table cell or not. If it is inside a table cell, the whole table row should be duplicated instead of only relpacing the macro name with a text string.

So - How can I implement this feature? There are the following things to do for me (as far as I think):

1) Determin, if a text item (or the current selection or the caret) is inside a table cell.

2) Select and copy the whole row, this determinde cell belongs to

3) Inserting the copied row after this row.


Can anybody help me? Especially how to realize 1)

Thank you for any hints! :wink:
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

If you are really asking for the location of the SELECTED text, it's easy: the selected text is in the cell if table.InplaceEditor<>nil.

This table can be found by GetCurrentItemEx method, for example.
Saving row: table.SaveRowsToStream.
Loading row: InsertRows, then (undocumented) LoadFromStreamEx.

table.LoadFromStreamEx(Stream: TStream; StartRow: Integer);

StartRow - the top row where the stream content will be loaded.
Bulvaye
Posts: 10
Joined: Wed Oct 12, 2005 2:08 pm

Post by Bulvaye »

Thank you, this hint helped me a lot! :D

Now, trying to replace the copied cell text with another string, I get some problems. The cell text is selected but inserting a new text, does not overwrite the selected cell text but inserts the new text to the top if the document. Have a look to my code, please:

Code: Select all

    
var Item: TCustomRVItemInfo;
    RV: TCustomRichViewEdit;
    Row, Col: Integer;
    Stream: TStream;
begin
  RVEdit.GetCurrentItemEx(TRVTableItemInfo, RV, Item);

  if Item is TRVTableItemInfo then
  begin
    (Item as TRVTableItemInfo).GetEditedCell(Row, Col);

    Stream := TMemoryStream.Create;
    with Item as TRVTableItemInfo do
      try
        SaveRowsToStream(Stream, Row, 1);
        InsertRowsBelow(1);
        Stream.Position := 0;
        LoadFromStreamEx(Stream, Row+1);
        RV.Format;
        Cells[Row+1, Col].SelectAll;
        RV.InsertText('Sample Text');
      finally
        Stream.Free;
      end;
  end;
end;
What do I wrong?
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Call Cells[Row+1, Col].Edit before Cells[Row+1, Col].SelectAll;
Calling cell.Edit is necessary in the editor before selecting in this cell.

Note 1: Calling BeginItemModify before SaveRowsToStream and EndItemModify may be faster than calling Format.

Note 2: Please call RV.ClearUndo after this code, because this code is a mix of viewer-style and editing-style methods, so it cannot be undone correctly.
Bulvaye
Posts: 10
Joined: Wed Oct 12, 2005 2:08 pm

Post by Bulvaye »

Thank you,

but how have I to call "BeginItemModify"? It's no member of the Item class itself. I found this method as member of the TCustomRichViewEdit, but what is the ItemNo of my "Item as TRVTableItemInfo" and what is the meaning of the var ModifyData?
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

These are methods of RichViewEdit, not of the table class.
They must be called for the editor that directly contains the table (it is returned by GetCurrentItemEx, in RV in your code).

The purpose of BeginItemModify-EndItemModify is replacing Format.
They try to reformat only the table, not the whole document, so they are faster.
The meaning of the parameter is not so important. It is something calculated in BeginItemModify and used in EndItemModify for more efficient processing.
Post Reply