How Can I Build a Style Programatically ?

General TRichView support forum. Please post your questions here
Post Reply
andyhill
Posts: 28
Joined: Tue May 23, 2006 8:22 pm

How Can I Build a Style Programatically ?

Post by andyhill »

How Can I Build a Style Programatically and use when inserting line (need to use TextStyle with Bold Attribute) ?

rvs2.ResetTextStyles;
rvs2.ResetParaStyles;
//
rvs2.TextStyles.AddFont('Times New Roman', 12, clBlack, clWhite, [want bold]);
rvs2.ParaStyles.Add;
rvs2.ParaStyles[0].Alignment:= rvaCenter;
rve2.Style:= rvs2;
//
rve2.Clear;
rve2.AddNL('Test 1', 0, 0);
rve2.AddNL('Test 2', 0, 0);
rve2.AddNL('', 0, 0);
rve2.AddNL(ClientName, 0, 0);
rve2.AddNL('CLIENT NOTES', 0, 0);
rve2.AddNL('', 0, 0);

Fails to use my TextStyle ?

append other rve's here
andyhill
Posts: 28
Joined: Tue May 23, 2006 8:22 pm

Merge rve streams and files

Post by andyhill »

function TfRichView.RVCopy(RVSource, RVDest: TCustomRichView): Boolean;
var
MyStream: TMemoryStream;
begin
try
MyStream:= TMemoryStream.Create;
try
RVSource.SaveRVFToStream(MyStream, False);
MyStream.Position:= 0;
Result:= RVDest.InsertRVFFromStream(MyStream, RVDest.ItemCount);
RVDest.FormatTail;
except
Result:= False;
end;
finally
MyStream.Free;
end;
end;

...

PrintIt:= psd.Execute;
if PrintIt then begin
//
ClientName:= '';
case fMain.Table0.FieldByName('Gender').AsInteger of
1: ClientName:= 'Mr ';
2: ClientName:= 'Mrs ';
3: ClientName:= 'Ms ';
4: ClientName:= 'Miss ';
end;
ClientName:= ClientName+fMain.Table0.FieldByName('FirstName').AsString+' '+fMain.Table0.FieldByName('LastName').AsString;
//////////////////////////////////////////////
// Clear rvs2 Styles
rvs2.ResetTextStyles;
rvs2.ResetParaStyles;
//////////////////////////////////////////////
// Add Text Style for heading
rvs2.TextStyles.AddFont('Times New Roman', 12, clBlack, clWhite, []); // !!! I Want Bold ?
//////////////////////////////////////////////
// Add Paragraph Styles
rvs2.ParaStyles.Add;
rvs2.ParaStyles[0].Alignment:= rvaCenter;
rvs2.ParaStyles.Add;
rvs2.ParaStyles[1].Alignment:= rvaLeft;
//////////////////////////////////////////////
// Assign rvs2
rve2.Style:= rvs2;
rve2.Clear;
//////////////////////////////////////////////
// Add text
rve2.AddNL('Business Name', 0, 0);
rve2.AddNL('Business Address', 0, 0);
rve2.AddNL('', 0, 0);
rve2.AddNL(ClientName, 0, 0);
rve2.AddNL('CLIENT NOTES', 0, 0);
rve2.AddNL('', 0, 0);
//////////////////////////////////////////////
// Append rve to rve2
RVCopy(rve, rve2);
rve2.AddNL('', 0, 0);
//////////////////////////////////////////////
// Add Table to rve2
table:= TRVTableItemInfo.CreateEx(3, 1, rve2.RVData);
table.BorderStyle:= rvtbRaisedColor;
table.CellBorderStyle:= rvtbLoweredColor;
table.BorderWidth:= 1;
table.CellBorderWidth:= 1;
table.CellPadding:= 5;
table.CellVSpacing:= 1;
table.CellHSpacing:= 1;
table.BorderVSpacing:= 1;
table.BorderHSpacing:= 1;
//////////////////////////////////////////////
// Size table
for r:= 0 to table.Rows.Count-1 do begin
for c:= 0 to table.Rows[r].Count-1 do begin
table.Cells[r, c].BestWidth:= 700; // !!! I want page width
end;
//
{ !!! I want to add rve3 stream, rve4 file and rtf file into this table (3 rows [one per item]) ?
//////////////////////////////////////////////
// Row 1
if r = 0 then begin
with table.Cells[r, 0] do begin
Color:= clInfoBk;
Clear;
RVCopy(rve3, rve2); // !!! NOT THIS WAY ???
end;
end;
}
end;
//////////////////////////////////////////////
// Add table to rve2
rve2.AddItem('', table); // !!! I want to add table Left Aligned ?
//////////////////////////////////////////////
// Assign to rvprint
RVPrint1.AssignSource(rve2);
//////////////////////////////////////////////
// Set margins
RVPrint1.LeftMarginMM:= 10;
RVPrint1.RightMarginMM:= 10;
RVPrint1.TopMarginMM:= 10;
RVPrint1.BottomMarginMM:= 10;
//////////////////////////////////////////////
// Format
RVPrint1.FormatPages(rvdoALL);
//////////////////////////////////////////////
// Print
if RVPrint1.PagesCount > 0 then RVPrint1.Print('LBAnalysis', 1, False);
//
end;
Sergey Tkachenko
Site Admin
Posts: 17555
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

ResetTextStyles and ResetParaStyles do not clear styles. They assign default styles to them (styles that you can see when you place a new TRVStyle component on the form).

Use
rvs2.TextStyles.Clear;
rvs2.ParaStyles.Clear;
andyhill
Posts: 28
Joined: Tue May 23, 2006 8:22 pm

Post by andyhill »

Thank you Seregy.

You did not explain:-

1) How to add Bold Attribute to text style ?

2) How to select Text Attribute when using ADDNL ?

3) How to insert rv stream/file into table cell, either during table construction -or- locating table and cell in editor ?
andyhill
Posts: 28
Joined: Tue May 23, 2006 8:22 pm

Insert RVF File into Table Cell Programatically

Post by andyhill »

rve2.AddItem('MyTempTable', table);
//
for i:= 0 to rve2.TopLevelEditor.ItemCount -1 do begin
ItemInfo:= rve2.TopLevelEditor.GetItem(i);
if ItemInfo.ItemText = 'MyTempTable' then begin
ItemNo:= rve2.TopLevelEditor.GetItemNo(ItemInfo);
Offs:= rve2.TopLevelEditor.GetOffsBeforeItem(ItemNo);
rve2.TopLevelEditor.SetSelectionBounds(ItemNo, Offs, ItemNo, Offs); // FAILS (I want to select ROWn, CELLn)
rve2.TopLevelEditor.InsertRVFFromFileEd('D:\D2011\Editor\test.rvf');
end;
end;
//
Sergey Tkachenko
Site Admin
Posts: 17555
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Well, it's much easier to answer to questions that were asked :)

1) Specify Style=[fsBold]. You can use AddFont, or just Add and then assign properties, for example:

Code: Select all

with rvs2.TextStyles.Add do begin
  FontName := 'Times New Roman';
  Size := 12;
  Color := clBlack, 
  BackColor := clWhite,
  Style := [fsBold];
end;
2) rve2.AddNL has StyleNo parameter - index in rvs2.TextStyles (assuming that rve2.Style = rvs2). You use 0 in your first code sample, so all text is added using rvs2.TextStyles[0]. If you change ResetTextStyles to TextStyles.Clear, then rvs2 will have only one text style, added by AddFont, and your code will use this style.
Sergey Tkachenko
Site Admin
Posts: 17555
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

3) If you want to load RVF to a cell, there a problem: cells do not support loading RVF containing text/paragraph/list styles. A workaround is shown in this demo: http://www.trichview.com/support/files/copyfromcell.zip

However, if you want to use editing method for inserting RVF in the caret position, it's no problem.

In your code, you insert the table directly in rve2:

Code: Select all

rve2.AddItem('MyTempTable', table); 
So, when searching for this table, you need to use methods of rve2, not rve2.TopLevelEditor. Otherwise, if the caret is in a table cell, the code searches in this table cell.

Code: Select all

var i: Integer;
  table: TRVTableItemInfo;
  RVData: TCustomRVFormattedData;

for i:= 0 to rve2.ItemCount -1 do
  if (rve2.GetItemStyle(i)=rvsTable) and (rve2.GetItemText(i)='MyTempTable') then begin
    table := TRVTableItemInfo(rve2.GetItem(i));
    // 1. Before working with selection in a table cell, activate its editing.
    // It can be done by table.EditCell or by Cell.Edit.
    table.EditCell(ROW, COLUMN); 
    // 2. Cell is in an editable state.
    // However, the caret is not necessary inside it.
    // It may be in a cell of some table inside this cell.
    // Moving the caret to the beginning of this cell
    RVData :=  TCustomRVFormattedData(table.Cells[ROW, COLUMN].GetRVData);
    RVData.SetSelectionBounds(0, RVData.GetOffsBeforeItem(0), 0, RVData.GetOffsBeforeItem(0));
    // 3. Now the caret is in our cell, so rve2.TopLevelEditor edits this cell.
    rve2.TopLevelEditor.InsertRVFFromFileEd('D:\D2011\Editor\test.rvf'); 
    exit;
  end;
Post Reply