Add a text item at the beginning of a TRichView

General TRichView support forum. Please post your questions here
Post Reply
jonjon
Posts: 467
Joined: Sat Aug 27, 2005 4:19 pm

Add a text item at the beginning of a TRichView

Post by jonjon »

I must be blind (and never needed it before) but I can't seem to find out how to add an item at the beginning of a formatted TRichView.

My problem is that I was adding the item for the header, then inserting another TRichView's content, then adding an item for the footer. But when I inserted the TRichView, I lost the background information so now I'm loading the TRichView then adding the footer and... I can't find how to add the header above everything else :(

Thank you for any help.

John.
Sergey Tkachenko
Site Admin
Posts: 17565
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Yes, there is only one documented method allowing to add content at the specified place in TRichView: InsertRVFFromStream.

Using undocumented methods, you can insert text item at the specified position using this code:

Code: Select all

use RVItem;

procedure InsertText(rv: TCustomRichView; ItemNo: Integer;   const s: String; StyleNo, ParaNo: Integer);
var item: TRVTextItemInfo;
begin
    item := RichViewTextItemClass.Create(RVData);
    item.StyleNo := StyleNo;
    item.ParaNo := ParaNo;
    item.Inserting(rv.RVData, s, False);
    rv.RVData.Items.InsertObject(ItemNo, s, item);
    item.Inserted(rv.RVData, ItemNo);
end;
Using to insert at the beginning:
InsertText(RichView1, 0, 'Title', StyleNo, ParaNo);

This code assumes that rv.Style.TextStyles[StyleNo].Unicode = False. If you need code for Unicode text, let me know.
jonjon
Posts: 467
Joined: Sat Aug 27, 2005 4:19 pm

Post by jonjon »

Thank you Sergey,

I'm sorry but I will also need the unicode version as I have no way of knowing if the user added ANSI or unicode text to the header.

Thank you very much.

Best regards,

John.
Sergey Tkachenko
Site Admin
Posts: 17565
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Code: Select all

uses CRVData, RVUni;

procedure InsertText(rv: TCustomRichView; ItemNo: Integer;  s: String; StyleNo, ParaNo: Integer); 
var item: TRVTextItemInfo; 
begin 
    item := RichViewTextItemClass.Create(RVData); 
    item.StyleNo := StyleNo; 
    item.ParaNo := ParaNo; 
    if  rv.Style.TextStyles[StyleNo].Unicode then begin
      s := RVU_AnsiToUnicode(rv.RVData.GetStyleCodePage(StyleNo), s);
      item.ItemOptions := item.ItemOptions+[rvioUnicode];
    end;
    item.Inserting(rv.RVData, s, False); 
    rv.RVData.Items.InsertObject(ItemNo, s, item); 
    item.Inserted(rv.RVData, ItemNo); 
end;
Or, if you want WideString parameter

Code: Select all

procedure InsertTextW(rv: TCustomRichView; ItemNo: Integer; const s: WideString; StyleNo, ParaNo: Integer); 
var item: TRVTextItemInfo; 
      s2: String;
begin 
    item := RichViewTextItemClass.Create(RVData); 
    item.StyleNo := StyleNo; 
    item.ParaNo := ParaNo; 
    if  rv.Style.TextStyles[StyleNo].Unicode then begin
      s2 := RVU_GetRawUnicode(s);
      item.ItemOptions := item.ItemOptions+[rvioUnicode];
      end
    else
      s2 := RVU_UnicodeToAnsi(rv.RVData.GetStyleCodePage(StyleNo), RVU_GetRawUnicode(s));
    item.Inserting(rv.RVData, s2, False); 
    rv.RVData.Items.InsertObject(ItemNo, s2, item); 
    item.Inserted(rv.RVData, ItemNo); 
end;
jonjon
Posts: 467
Joined: Sat Aug 27, 2005 4:19 pm

Post by jonjon »

Thank you very much Sergey,

I will try that as soon as possible.

Best regards,

John.
Post Reply