Controls, Documents, Items in TRichView

<< Click to display table of contents >>

Controls, Documents, Items in TRichView

There are three main families of classes.

1. Controls, descendants of TCustomRichView.

See the diagram of class hierarchy.

The main visual controls are: TRichView, TRichViewEdit.

In VCL and Lazarus, also: TDBRichView, TDBRichViewEdit.

2. Documents (objects containing items), descendants of TCustomRVData.

Documents are objects, but not components.

Each RichView or RichViewEdit has such document accessible as RVData property.

They have the most of methods of RichView controls. For example, calling

MyRichView.AddNL('text',0,0)

is equivalent to

MyRichView.RVData.AddNL('text',0,0)

Tables have cells. Cells are also documents containing items, cells are descendants of TCustomRVFormattedData.

3. Items of documents, descendants of TCustomRVItemInfo.

Items are objects, but not components.

Working with basic item types (text, images, horizontal lines, etc.) typically doesn't require direct access to the item object. Instead, methods for getting items and changing their properties are used, which are available in components (TRichView and TRichViewEdit) or in documents.

However, some document items don't have dedicated methods (with the exception of methods for working with additional integer and string properties). Working with such items requires retrieving their object and calling its methods. The most common example of such an item is tables (TRVTableItemInfo).

Documents contain items, some items can contain documents. For example, tables (items) contain cells (documents). Footnotes, endnotes, sidenotes, text boxes (items) also contain a document, but it is not displayed by TRichView (it is displayed when printing in TRVPrint or in ScaleRichView).

There is an important case: cell editing. When editing cell, RichViewEdit creates a special editor (also TRichViewEdit) on the top of the cell. While editing, all items in the cell are moved in this editor. When editing is finished, items are moved back to the cell.

At any time, a document containing cell items is accessible as cell.GetRVData (if cell is not edited, it is the cell itself; if edited, it is RVData of its inplace editor).

Example 1

This example shows that the same procedure can be applied to the main document and table cells.

This example converts all text to upper case

procedure AllUpperCase(RVData: TCustomRVData);

var i,r,c: Integer;

    s: String;

    table: TRVTableItemInfo;

begin

for i := 0 to RVData.ItemCount-1 do

  if RVData.GetItemStyle(i)>=0 then

  begin 

    // this is a text item

    s := RVData.GetItemText(i); 

    s := AnsiUpperCase(s);

    RVData.SetItemText(i,s); 

  end

  else if RVData.GetItemStyle(i) is TRVTableItemInfo then

  begin

    table := TRVTableItemInfo(RVData.GetItem(i));

    for r := 0 to table.RowCount-1 do

      for c := 0 to table.ColCount-1 do

        if table.Cells[r,c]<>nil then

          AllUpperCase(table.Cells[r,c].GetRVData);

  end;

end;

Call:

AllUpperCase(MyRichView.RVData);

MyRichView.Format;

 

Example 2

The same task, using different methods

// This procedure will be called for each RichView item 

procedure TForm1.EnumItemsProc(RVData: TCustomRVData;

  ItemNo: Integer;  var UserData1: Integer;

  const UserData2: TRVUnicodeStringvar ContinueEnum: Boolean); 

var s: String;

begin 

  if RVData.GetItemStyle(ItemNo)>=0 then begin

    s := RVData.GetItemText(i);

    s := AnsiUpperCase(s);

    RVData.SetItemText(ItemNo,s);

  end;

  ContinueEnum := True; 

end

Call:

var v: Integer; 

 

v := 0;

// RVData.EnumItems calls the specified procedure (EnumItems)

// for each items in RVData and its subdocuments (table cells)

// The second and the third parameters are user defined data,

// they will be passed in the procedure as UserData1 and UserData2

MyRichView.RVData.EnumItems(EnumItemsProc, v, ''); 

MyRichView.Format;

The advantage of using RVData.EnumItems is more clear and compact code.

The disadvantage: you cannot use it if you need to add or delete items.