//---------------------------------------------------------------------------
// Important settings:
//    rvoTagsArePChars in RichViewEdit1->Options
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma link "RVEdit"
#pragma link "RichView"
#pragma link "RVScroll"
#pragma link "RVStyle"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void GetFieldItemNo(TCustomRichView* rv, const AnsiString field, int& ItemNo)
{
  if (ItemNo>=0 && ItemNo<rv->ItemCount &&
     AnsiString(PChar(rv->GetItemTag(ItemNo))) == field)
    return;
  ItemNo = -1;
  for (int i=0; i<rv->ItemCount; i++)
    if (AnsiString(PChar(rv->GetItemTag(i))) == field)
    {
      ItemNo = i;
      return;
    }
}
//---------------------------------------------------------------------------
AnsiString GetFieldValue(TCustomRichView* rv, const AnsiString field,
  int& LastItemNo)
{
  GetFieldItemNo(rv, field, LastItemNo);
  if (LastItemNo>=0)
    return rv->GetItemTextA(LastItemNo);
  else
    return "";
}
//---------------------------------------------------------------------------
void SetFieldValue(TCustomRichViewEdit* rv, const AnsiString field,
  const AnsiString value, int& LastItemNo)
{
  GetFieldItemNo(rv, field, LastItemNo);
  if (LastItemNo>=0)
    rv->SetItemTextEdA(LastItemNo, value);
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  RichViewEdit1->Clear();
  RichViewEdit1->AddNLATag("Insert fields here",0,0,0);
  RichViewEdit1->Format();
}
//---------------------------------------------------------------------------
void TForm1::InsertField(const AnsiString field, const AnsiString value,
  int & LastItemNo)
{
  if (GetFieldValue(RichViewEdit1, field, LastItemNo)!="")
  {
    Application->MessageBox("Field already exists!","",0);
    return;
  }
  int StyleNo = RichViewEdit1->CurParaStyleNo;
  RichViewEdit1->CurTextStyleNo = 6;
  RichViewEdit1->InsertStringTag(value, (int)StrNew(field.c_str()));
  RichViewEdit1->CurTextStyleNo = StyleNo;
  RichViewEdit1->SetFocus();
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  InsertField("name", Edit1->Text, LastNameItemNo);	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  InsertField("date", Edit2->Text, LastDateItemNo);	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
  RichViewEdit1->ApplyTextStyle(0);
  RichViewEdit1->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
  if (Freeze)
    return;
  Freeze = true;
  try
  {
    SetFieldValue(RichViewEdit1, "name", Edit1->Text, LastNameItemNo);
  }
  catch(...)
  {
  }
  Freeze = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2Change(TObject *Sender)
{
  if (Freeze)
    return;
  Freeze = true;
  try
  {
    SetFieldValue(RichViewEdit1, "date", Edit2->Text, LastDateItemNo);
  }
  catch(...)
  {
  }
  Freeze = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichViewEdit1Change(TObject *Sender)
{
  if (Freeze)
    return;
  Freeze = true;
  try
  {
    AnsiString s = GetFieldValue(RichViewEdit1, "name", LastNameItemNo);
    if (s!="")
      Edit1->Text = s;
    s = GetFieldValue(RichViewEdit1, "date", LastDateItemNo);
    if (s!="")
      Edit2->Text = s;
  }
  catch(...)
  {
  }
  Freeze = false;
}
//---------------------------------------------------------------------------
