Merging data from one TRichViewEdit into another

General TRichView support forum. Please post your questions here
martindholmes
Posts: 131
Joined: Mon Aug 29, 2005 12:03 pm

Post by martindholmes »

I've posted a test project here:

http://www.mholmes.com/test/test_rve.zip

If you type something in the first box, and something in the second box, then press the Paste button, the content of the first box will be transferred to the second. If you then press Return at the end of the second box and start typing, you'll see that the style is now the style from the first box, not the second. The desired behaviour would be that the default style from the second box is preserved even after copying content from the first box.

This is a Delphi 2005 project, but it's simple enough to build on any recent Delphi, I think.

Cheers,
Martin
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

This behavior is expected.

rvsOne.TextStyles[0].NextStyleNo = 0, i.e. it points to itself. It will still point to itself after adding this style to rvsTwo from InsertRVFFromStreamEd.

When you change something on the editor, the style at the caret position becomes current.
If you want to prevent it, save the value of the current style before the insertion, then restore:

Code: Select all

procedure TForm1.btnPasteClick(Sender: TObject);
var AStream: TMemoryStream;
    StyleNo: Integer;
begin
  AStream := TMemoryStream.Create;
  try
    StyleNo := rveTwo.CurTextStyleNo;
    rveOne.SaveRVFToStream(AStream, False);
    AStream.Position := 0;
    rveTwo.InsertRVFFromStreamEd(AStream);
    rveTwo.SetFocus;
    rveTwo.CurTextStyleNo := StyleNo;
  finally
    FreeAndNil(AStream);
  end;
end;
martindholmes
Posts: 131
Joined: Mon Aug 29, 2005 12:03 pm

Post by martindholmes »

That doesn't really make sense to me. NextStyleNo is 0, but the style of the inserted text is not 0; it's whatever style number it acquires when it's been merged into the rveTwo's RVStyle. In other words, 0 should be the first text style in rvsTwo, not the first style in rvsOne. So:

rvsOne text style #0 is green bold.

rvsTwo text style #0 is black non-bold.

Green bold text is inserted from rveOne into rveTwo, which should cause a new style to be added to rvsTwo for the green text. That green text style would not be number 0.

That style should have NextStyleNo = 0.

In rvsTwo, #0 should be black non-bold, shouldn't it? So pressing return after the green text insertion should result in black, non-bold text.

Cheers,
Martin
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Adjusting references on style merging follows different logic:
- numeric values of references are not important, but properties of referred styles are important.

rvsOne.TextStyles[0].NextStyleNo points to green text. So, when the user presses Enter, new paragraph has a green text. After merging in rvsTwo, this situation is not changed: when the user presses Enter after this style, new paragraph has a green text. Otherwise, properties of the original style would not be retained after merging.

Technically, if the style A is mapped to style A', and NextStyleNo for the style B is equal to A before merging, it will be equal to A' after merging.

Sorry, but I think that this is the only correct way to merge styles.
martindholmes
Posts: 131
Joined: Mon Aug 29, 2005 12:03 pm

Post by martindholmes »

I understand your position. It seemed unintuitive to me, but it's logical in its own way.

Cheers,
Martin
Post Reply