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


// Use this define, if you want to enable Gif support
// For C++Builder 4-2006: with Anders Melander's TGifImage
//   http://www.torry.net/vcl/graphics/gif/gifimage.exe (original)
//   http://www.trichview.com/resources/thirdparty/gifimage.zip (update)
// For C++Builder 2007 or newer: with C++Builder's TGifImage
//#define USEGIFIMAGE

#include "MainFrm.h"
#ifdef USEGIFIMAGE
#include "GifImage.hpp" // for C++Builder 2007, change to GifImg.hpp
#pragma link "GifImage" // for C++Builder 2007, change to GifImg
#endif

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




TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
    : TForm(Owner)
{
#ifdef USEGIFIMAGE
  RegisterClass(__classid(TGifImage));
#endif
}
//---------------------------------------------------------------------------
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->AddNLATag("This demo shows how to use Microsoft® Office text converters.",0,0,0);
  rve->AddNLATag("First items in filters (file masks) of the file-selection dialogs are RTF import/export "
            "by the component itself. Others use converters.",0,0,0);
  rve->AddNLATag("For HTML import testing, look at test/test.htm.",0,0,0);
  rve->AddNLATag("After loading, Ctrl+click any hypertext link to view its target.",0,0,0);
  rve->AddNLATag("You'll see option for HTML export via converters. You can use it, but native RichView HTML"
            " export gives much better results.",0,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, TRVRawByteString &ItemName)
{
  ItemTag = (int)StrNew(Target.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::rveWriteHyperlink(TCustomRichView *Sender, int id,
	TCustomRVData *RVData, int ItemNo, TRVSaveFormat SaveFormat,
	AnsiString &Target, AnsiString &Extras)
{
  Target = (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);
}
//---------------------------------------------------------------------------
