unit Unit1;

interface

{------------------------------------------------------------------------------
 Implementing "fields"
 The main property settings:
 - rvoTagsArePChars in srv.RichViewEdit..Options (so each item can have associated string)
 - RVStyle1.TextStyles collection has two styles (for static text and for fields)
   Especially look at Protection properties
 - srv.RichViewEdit. is set to "use predefined set of styles" (right click on
   srv.RichViewEdit. at design time, then choose "Settings")
 - AcceptDragDropFormats = []

------------------------------------------------------------------------------ }

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, RVStyle, RVScroll, RichView, RVEdit, Clipbrd, StdCtrls,
  CRVData, RVTable, SclRView;

type
  TForm1 = class(TForm)
    Button1: TButton;
    srv: TSRichViewEdit;
    rvs: TRVStyle;
    procedure FormCreate(Sender: TObject);
    procedure srvPaste(Sender: TCustomRichViewEdit;
      var DoDefault: Boolean);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetFieldLocation(RootRVData: TCustomRVData; const field: String;
                           var RVData: TCustomRVData;
                           var ItemNo: Integer): Boolean;
var i,r,c: Integer;
    table: TRVTableItemInfo;
begin
  Result := False;
  for i := 0 to RootRVData.ItemCount-1 do
    if RootRVData.GetItemStyle(i)=rvsTable then begin
      table := TRVTableItemInfo(RootRVData.GetItem(i));
      for r := 0 to table.Rows.Count-1 do
        for c := 0 to table.Rows[r].Count-1 do
          if table.Cells[r,c]<>nil then begin
            Result := GetFieldLocation(table.Cells[r,c].GetRVData, field, RVData, ItemNo);
            if Result then
              exit;
          end
      end
    else if PChar(RootRVData.GetItemTag(i)) = field then begin
      ItemNo := i;
      RVData := RootRVData.GetSourceRVData;
      Result := True;
      exit;
    end;
end;

function GetFieldValue(rv: TCustomRichView; const field: String): String;
var RVData: TCustomRVData;
    ItemNo: Integer;
begin
  if GetFieldLocation(rv.RVData, field, RVData, ItemNo) then
    Result := RVData.GetItemText(ItemNo)
  else
    Result := '';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  srv.SetMargin(25, 25, 25, 25);

  srv.RichViewEdit.Clear;
  srv.RichViewEdit.AddNL('2 + 2 =', 1,0);
  srv.RichViewEdit.AddNLTag('  ', 0,-1, Integer(StrNew('answer1')));
  srv.RichViewEdit.AddNL('?', 1,-1);
  srv.RichViewEdit.AddNL('3 + 3 =', 1,0);
  srv.RichViewEdit.AddNLTag('  ', 0,-1, Integer(StrNew('answer2')));
  srv.RichViewEdit.AddNL('?', 1,-1);
  srv.RichViewEdit.Format;
  srv.RichViewEdit.SetSelectionBounds(1,2,1,2);
end;

procedure TForm1.srvPaste(Sender: TCustomRichViewEdit;
  var DoDefault: Boolean);
var s: String;
begin
  // allowing to paste only one line text
  s := Clipboard.AsText;
  if (Pos(#10,s)=0) and (Pos(#13,s)=0) then
    srv.RichViewEdit.InsertText(s,False);
  DoDefault := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var s: String;
begin
  s := GetFieldValue(srv.RichViewEdit, 'answer1');
  if StrToIntDef(Trim(s), 0)<>4 then begin
    Application.MessageBox('Wrong! Try again','Test', 0);
    exit;
  end;
  s := GetFieldValue(srv.RichViewEdit, 'answer2');
  if StrToIntDef(Trim(s), 0)<>6 then begin
    Application.MessageBox('Wrong! Try again','Test', 0);
    exit;
  end;
  Application.MessageBox('Test is passed','Test', 0);
end;

end.
