unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ShellApi,
  Dialogs, StdCtrls, ComCtrls, RVStyle, RVScroll,
  RichView, RVFuncs, ExtCtrls, RVEdit, CRVData, CRVFData, RVOfficeCnv, RVTypes,
  GifImg, PngImage;

type
  TfrmMain = class(TForm)
    od: TOpenDialog;
    rvs: TRVStyle;
    rve: TRichViewEdit;
    Panel1: TPanel;
    btnOpen: TButton;
    rvc: TRVOfficeConverter;
    btnSave: TButton;
    sd: TSaveDialog;
    pb: TProgressBar;
    procedure btnOpenClick(Sender: TObject);
    procedure rvcConverting(Sender: TObject; Percent: Integer);
    procedure btnSaveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure rveJump(Sender: TObject; id: Integer);
    procedure rveWriteHyperlink(Sender: TCustomRichView; id: Integer;
      RVData: TCustomRVData; ItemNo: Integer; SaveFormat: TRVSaveFormat;
      var Target, Extras: string);
    procedure rveReadHyperlink(Sender: TCustomRichView; const Target,
      Extras: string; DocFormat: TRVLoadFormat; var StyleNo, ItemTag: Integer;
      var ItemName: TRVRawByteString);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

{====================== Import and export =====================================}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  od.Filter := 'RTF (Built-In RichView Import)|*.rtf|'+rvc.GetImportFilter;
  od.InitialDir := ExtractFilePath(Application.ExeName)+'test\';
  sd.Filter := 'RTF (Built-In RichView Export)|*.rtf|'+rvc.GetExportFilter;

  rve.Clear;
  rve.AddNL('This demo shows how to use Microsoft® Office text converters.',0,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;
end;
{------------------------------------------------------------------------------}
procedure TfrmMain.btnOpenClick(Sender: TObject);
var r: Boolean;
begin
  if not od.Execute then
    exit;
  rve.Clear;
  rve.Format;
  rve.Update;

  rve.Clear;
  if od.FilterIndex=1 then
    r := rve.LoadRTF(od.FileName)
  else begin
    pb.Visible := True;
    r:= rvc.ImportRV(od.FileName, rve, od.FilterIndex-2);
  end;
  if not r then
    Application.MessageBox('Error','Error',0);
  pb.Visible := False;
  rve.Format;
  rve.SetFocus;
end;
{------------------------------------------------------------------------------}
procedure TfrmMain.btnSaveClick(Sender: TObject);
var r: Boolean;
begin
  if not sd.Execute then
    exit;
  pb.Visible := True;
  if sd.FilterIndex=1 then
    r := rve.SaveRTF(sd.FileName,False)
  else begin
    pb.Visible := True;
    r:= rvc.ExportRV(sd.FileName, rve, sd.FilterIndex-2);
  end;
  if not r then
    Application.MessageBox('Error','Error',0);
  pb.Visible := False;
  rve.SetFocus;
end;
{------------------------------------------------------------------------------}
procedure TfrmMain.rvcConverting(Sender: TObject; Percent: Integer);
begin
  pb.Position := Percent;
end;
{============================ Hypertext =======================================}
// We store URLs in tags
procedure TfrmMain.rveReadHyperlink(Sender: TCustomRichView; const Target,
  Extras: string; DocFormat: TRVLoadFormat; var StyleNo, ItemTag: Integer;
  var ItemName: TRVRawByteString);
begin
  ItemTag := Integer(StrNew(PChar(Target)));
end;
{------------------------------------------------------------------------------}
procedure TfrmMain.rveWriteHyperlink(Sender: TCustomRichView; id: Integer;
  RVData: TCustomRVData; ItemNo: Integer; SaveFormat: TRVSaveFormat; var Target,
  Extras: string);
begin
  Target := PChar(RVData.GetItemTag(ItemNo));
end;
{------------------------------------------------------------------------------}
procedure TfrmMain.rveJump(Sender: TObject; id: Integer);
var ItemNo: Integer;
    RVData: TCustomRVFormattedData;
    s: String;
begin
  rve.GetJumpPointLocation(id, RVData, ItemNo);
  s := PChar(RVData.GetItemTag(ItemNo));
  ShellExecute(0, 'open', PChar(s), nil, nil, SW_SHOW);
end;

{==============================================================================}

initialization
  RegisterClass(TGifImage);
  RegisterClass(TPngImage);


end.
