/*=============================================================================}
{ Demo: how to load RVF file saved in demo editor.                             }
{ Sergey Tkachenko                                                             }
{------------------------------------------------------------------------------}
{ Providing pictures and controls on request from RichView is not supported in }
{ this demo.                                                                   }
{=============================================================================*/
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma link "RichView"
#pragma link "RVScroll"
#pragma link "RVStyle"
#pragma link "RVTable"
#pragma link "RVTInplace"
#pragma resource "*.dfm"

/*
Notes about loading from RVF files:
1. In simplest cases you can just write: RichView1->LoadRVF(<file name>);
2. If file contains inserted Delphi Controls, these controls must be registered
   with RegisterClasses functions before loading (see FormCreate below)
3. If file contains images from image lists, you need to process
   OnRVFImageListNeeded event (see RichView1RVFImageListNeeded below)
   If you have several image lists, you can distinguish them using
   ImageListTag parameter of this event.
4. You must have the same (or compatible) TRVStyle object assigned to
   RichView1->Style as in editor.
   Otherwise, you need to set option "Allow adding styles dynamically"
   both in richview which saves and in richview which loads RVF
   (right-click RichView in C++Builder IDE, choose "Settings" in the context menu)
5. If some items in RVF file have character strings associated as items' tags
   (rvoTagsArePChars was in editor's Options), you need to set rvoTagsArePChars
   in RichView1->Options.
*/

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TComponentClass Classes[3] = { __classid(TButton), __classid(TEdit), __classid(TOleContainer) };
  RegisterClasses(Classes,2);
  OpenDialog1->InitialDir = ExtractFilePath(Application->ExeName)+"..\\..\\..\\Editors\\Editor 1\\";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if (OpenDialog1->Execute())
  {
     // Clearing and deleting unused text styles.
    // If RVF contains collections of styles, it is not necessary,
    // because collections from the file will replace the existing collections
    RichView1->Clear();
    RichView1->DeleteUnusedStyles(true, true, true);
    // Resetting background.
    // If RVF contains background information and rvfoLoadBack is in RVFOptions,
    // it is not necessary
    RichView1->BackgroundBitmap = NULL;
    // Resetting layout properties.
    // If RVF contains layout information and rvfoLoadLayout is in RVFOptions,
    // it is not necessary
    RichView1->LeftMargin = 5;
    RichView1->TopMargin = 5;
    RichView1->RightMargin = 5;
    RichView1->BottomMargin = 5;
    RichView1->MinTextWidth = 0;
    RichView1->MaxTextWidth = 0;
    // Loading
    if (!RichView1->LoadRVF(OpenDialog1->FileName))
      Application->MessageBox(L"Error Loading File", NULL, MB_OK);
    // Formatting (necessary before displaying)
    RichView1->Format();
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichView1RVFImageListNeeded(TCustomRichView *Sender,
    int ImageListTag, TCustomImageList *&il)
{
  il = ImageList1;
}
//---------------------------------------------------------------------------
