//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "MainFrm.h"
//---------------------------------------------------------------------------
#pragma link "RVEdit"
#pragma link "RichView"
#pragma link "RVScroll"
#pragma link "RVStyle"
#pragma link "RVOfficeCnv"
#pragma resource "*.dfm"

// If you want to enable GIF support
// with Anders Melander's TGifImage http://www.melander.dk/delphi/gifimage/
// Open RVGif.pas from this project and remove dot from line
// {.$DEFINE USEGIFIMAGE}


TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
  od->Filter = "RTF (Built-In RichView Import)|*.rtf|"+rvc->GetImportFilter();
  sd->Filter = "RTF (Built-In RichView Export)|*.rtf|"+rvc->GetExportFilter();

  rve->Clear();
  rve->Add("This demo shows how to use Microsoft® Office text converters.",0);
  rve->AddNL("First items in filters (file masks) of the file-selection dialogs are RTF import/export "
            "by the component itself. Others use converters.",0,0);
  rve->AddNL("For HTML import testing, look at test/test.htm.",0,0);
  rve->AddNL("After loading, Ctrl+click any hypertext link to view its target.",0,0);
  rve->AddNL("You'll see option for HTML export via converters. You can use it, but native RichView HTML"
            " export gives much better results.",0,0);
  rve->Format();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnOpenClick(TObject *Sender)
{
  if (!od->Execute())
    return;

  rve->Clear();
  rve->Format();
  rve->Update();

  rve->Clear();
  bool r;
  if (od->FilterIndex==1)
    r = rve->LoadRTF(od->FileName);
  else
  {
    pb->Visible = true;
    r = rvc->ImportRV(od->FileName, rve, od->FilterIndex-2);
  }
  if (!r)
    Application->MessageBox("Error","Error",0);
  pb->Visible = false;
  rve->Format();
  rve->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSaveClick(TObject *Sender)
{
  if (!sd->Execute())
    return;
  pb->Visible = true;
  bool r;
  if (sd->FilterIndex==1)
    r = rve->SaveRTF(sd->FileName,false);
  else
  {
    pb->Visible = true;
    r = rvc->ExportRV(sd->FileName, rve, sd->FilterIndex-2);
  }
  if (!r)
    Application->MessageBox("Error","Error",0);
  pb->Visible = false;
  rve->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::rvcConverting(TObject *Sender, int Percent)
{
  pb->Position = Percent;    
}
//==================== Hypertext ============================================
// We store URLs in tags
void __fastcall TfrmMain::rveReadHyperlink(TCustomRichView *Sender,
    const AnsiString Target, const AnsiString Extras, TRVLoadFormat DocFormat,
    int &StyleNo, int &ItemTag, AnsiString &ItemName)
{
  ItemTag = (int)StrNew(Target.c_str());    
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::rveURLNeeded(TCustomRichView *Sender, int id,
    AnsiString &url)
{
  int ItemNo;
  TCustomRVFormattedData* RVData;

  rve->GetJumpPointLocation(id, RVData, ItemNo);
  url = (char*)RVData->GetItemTag(ItemNo);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::rveJump(TObject *Sender, int id)
{
  int ItemNo;
  TCustomRVFormattedData* RVData;

  rve->GetJumpPointLocation(id, RVData, ItemNo);
  AnsiString s = (char*)RVData->GetItemTag(ItemNo);
  Application->MessageBox(s.c_str(), "Link", MB_OK | MB_ICONINFORMATION);
}

