
{*******************************************************}
{                                                       }
{       RichViewActions                                 }
{       Demo project.                                   }
{       You can use it as a basis for your              }
{       applications.                                   }
{                                                       }
{       Copyright (c) Sergey Tkachenko                  }
{       svt@trichview.com                               }
{       Copyright (c) Ilya Zelenskiy                    }
{       ilya@trichview.com                              }
{       http://www.trichview.com                        }
{                                                       }
{*******************************************************}


unit Unit3;

interface

{$I RV_Defs.inc}          // contains defines about Delphi compiler versions

{
  Remove dots from the defines below to enable support for Gif and Png.

  GifImage (by Anders Melander)
    http://www.torry.net/vcl/graphics/gif/gifimage.exe (original)
    http://www.trichview.com/resources/thirdparty/gifimage.zip (update)
  Note: for Delphi 2007+, use the built in TGifImage instead

  PngObject (by Gustavo Huffenbacher Daud)
    http://www.trichview.com/resources/thirdparty/pngimage.zip
  Note: for Delphi 2009+, use the built in TPngImage instead)
}

{.$DEFINE USE_GIFIMAGE}
{.$DEFINE USE_PNGOBJECT}


uses
  Windows, Messages, SysUtils,
  Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls,
  ActnList, StdActns, ImgList, ToolWin,
  Menus, ShellApi, Printers, ExtCtrls,

  RVUni, RVMisc, RVScroll, RichView, RVEdit,
  RVStyle, CRVFData, RVTable, RVItem, CRVData, RVFuncs, RVNote,

  RichViewActions, RVFontCombos, Ruler, RVRuler,
  RVALocalize, RVALocRuler,
  // RVAAddictLanguages,

  SRVPageScroll, SclRView, SclRVRuler, SRVToolWindow,
  SRVToolBar,
  SRVALocalize,

  {$IFDEF USE_GIFIMAGE}
  {$IFDEF RICHVIEWDEF2007}GifImg,{$ELSE}GifImage,{$ENDIF}
  {$ENDIF}
  {$IFDEF USE_PNGOBJECT}
  PngImage,
  {$ENDIF}
  RVTypes, SRVActions;

type
  TForm3 = class(TForm)
    MainMenu1: TMainMenu;
    mitFile: TMenuItem;
    mitEdit: TMenuItem;
    New1: TMenuItem;
    Load1: TMenuItem;
    N1: TMenuItem;
    mitExit: TMenuItem;
    Cut1: TMenuItem;
    Copy1: TMenuItem;
    n2: TMenuItem;
    mitFont: TMenuItem;
    Bold1: TMenuItem;
    Italic1: TMenuItem;
    Underline1: TMenuItem;
    N3: TMenuItem;
    mitPara: TMenuItem;
    Leftjustify1: TMenuItem;
    N4: TMenuItem;
    Undo1: TMenuItem;
    Redo1: TMenuItem;
    Changefont1: TMenuItem;
    Save1: TMenuItem;
    Strikeout1: TMenuItem;
    mitTable: TMenuItem;
    InsertTable1: TMenuItem;
    N5: TMenuItem;
    SelectAll1: TMenuItem;
    AlignLeft1: TMenuItem;
    AlignCenter1: TMenuItem;
    AlignRight1: TMenuItem;
    N6: TMenuItem;
    DecreaseIndent1: TMenuItem;
    IncreaseIndent1: TMenuItem;
    N7: TMenuItem;
    Justify1: TMenuItem;
    InsertRowAbove1: TMenuItem;
    InsertRowBelow1: TMenuItem;
    N8: TMenuItem;
    InsertColumnLeft1: TMenuItem;
    InsertColumnRight1: TMenuItem;
    N9: TMenuItem;
    N10: TMenuItem;
    DeleteRows1: TMenuItem;
    rvActionTableDeleteCols11: TMenuItem;
    N11: TMenuItem;
    MergeCells1: TMenuItem;
    SplitCells1: TMenuItem;
    N12: TMenuItem;
    mitTableSelect: TMenuItem;
    SelectTable1: TMenuItem;
    SelectColumns1: TMenuItem;
    SelectRows1: TMenuItem;
    SelectCell1: TMenuItem;
    mitFontStyle: TMenuItem;
    mitFontSize: TMenuItem;
    ShrinkFont1: TMenuItem;
    GrowFont1: TMenuItem;
    N13: TMenuItem;
    ShrinkFontByOnePoint1: TMenuItem;
    GrowFontByOnePoint1: TMenuItem;
    N14: TMenuItem;
    AllCapitals1: TMenuItem;
    Overline1: TMenuItem;
    N15: TMenuItem;
    Find1: TMenuItem;
    FindNext1: TMenuItem;
    Replace1: TMenuItem;
    CoolBar1: TCoolBar;
    ToolBar1: TToolBar;
    ToolButton11: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    btnQuickPrint: TToolButton;
    ToolButton17: TToolButton;
    ToolButton18: TToolButton;
    ToolButton19: TToolButton;
    ToolButton20: TToolButton;
    ToolButton21: TToolButton;
    ToolBar2: TToolBar;
    ToolButton41: TToolButton;
    ToolButton42: TToolButton;
    ToolButton43: TToolButton;
    ToolButton44: TToolButton;
    ToolButton49: TToolButton;
    ToolButton50: TToolButton;
    ToolButton51: TToolButton;
    ToolButton53: TToolButton;
    ToolButton54: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolBar3: TToolBar;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    ToolButton12: TToolButton;
    ToolButton14: TToolButton;
    ToolButton15: TToolButton;
    ToolButton16: TToolButton;
    ToolButton23: TToolButton;
    ToolButton24: TToolButton;
    ToolButton25: TToolButton;
    ToolButton26: TToolButton;
    ToolButton28: TToolButton;
    ToolButton8: TToolButton;
    ToolButton27: TToolButton;
    ToolButton29: TToolButton;
    ToolButton30: TToolButton;
    ToolButton31: TToolButton;
    ToolButton32: TToolButton;
    ToolButton33: TToolButton;
    ToolButton34: TToolButton;
    ToolButton35: TToolButton;
    N16: TMenuItem;
    ToolButton36: TToolButton;
    ToolButton37: TToolButton;
    extColor1: TMenuItem;
    ToolButton38: TToolButton;
    extBackgroundColor1: TMenuItem;
    N17: TMenuItem;
    ParagraphBackgroundColor1: TMenuItem;
    ToolButton39: TToolButton;
    ToolButton40: TToolButton;
    mitFormat: TMenuItem;
    BackgroundColor1: TMenuItem;
    DeleteTable1: TMenuItem;
    ToolButton45: TToolButton;
    N18: TMenuItem;
    rvActionParagraph11: TMenuItem;
    RVAControlPanel1: TRVAControlPanel;
    Font1: TMenuItem;
    FillColor1: TMenuItem;
    SaveAs1: TMenuItem;
    Export1: TMenuItem;
    mitInsert: TMenuItem;
    File1: TMenuItem;
    Picture1: TMenuItem;
    ToolButton46: TToolButton;
    PasteSpecial1: TMenuItem;
    N19: TMenuItem;
    SingleLineSpacing1: TMenuItem;
    N15LineSpacing1: TMenuItem;
    DoubleLineSpacing1: TMenuItem;
    N20: TMenuItem;
    InsertPageBreak1: TMenuItem;
    RemovePageBreak1: TMenuItem;
    N21: TMenuItem;
    mitTableAlignCellContents: TMenuItem;
    AlignCellToTheTop1: TMenuItem;
    AlignCellToTheMiddle1: TMenuItem;
    AlignCellToTheBottom1: TMenuItem;
    DefaultCellVerticalAlignment1: TMenuItem;
    ParagraphBorders1: TMenuItem;
    N22: TMenuItem;
    Properties1: TMenuItem;
    HorizontalLine1: TMenuItem;
    HypertextLink1: TMenuItem;
    ToolButton47: TToolButton;
    N23: TMenuItem;
    ableProperties1: TMenuItem;
    N24: TMenuItem;
    ShowGridLines1: TMenuItem;
    N25: TMenuItem;
    BulletsandNumbering1: TMenuItem;
    InsertSymbol1: TMenuItem;
    mitTableCellBorders: TMenuItem;
    LeftBorder1: TMenuItem;
    rvActionTableCellTopBorder11: TMenuItem;
    rvActionTableCellRightBorder11: TMenuItem;
    rvActionTableCellBottomBorder11: TMenuItem;
    rvActionTableCellAllBorders11: TMenuItem;
    rvActionTableCellNoBorders11: TMenuItem;
    ToolBar4: TToolBar;
    ToolButton48: TToolButton;
    ToolButton52: TToolButton;
    ToolButton55: TToolButton;
    ToolButton56: TToolButton;
    ToolButton58: TToolButton;
    ToolButton59: TToolButton;
    Bullets1: TMenuItem;
    Numbering1: TMenuItem;
    ToolButton57: TToolButton;
    ToolButton60: TToolButton;
    ToolButton61: TToolButton;
    ToolBar5: TToolBar;
    cmbFont: TRVFontComboBox;
    RVAPopupMenu1: TRVAPopupMenu;
    Background1: TMenuItem;
    ColorDialog1: TColorDialog;
    cmbFontSize: TRVFontSizeComboBox;
    N26: TMenuItem;
    CharacterCase1: TMenuItem;
    ToolButton62: TToolButton;
    ToolButton63: TToolButton;
    pmFakeDropDown: TPopupMenu;
    srvtwSearch: TSRVToolWindow;
    il1: TImageList;
    ilMenuV: TImageList;
    ilMenuH: TImageList;
    Panel1: TPanel;
    Panel3: TPanel;
    SclRVRuler1: TSclRVRuler;
    Panel4: TPanel;
    RVRulerItemSelector1: TRVRulerItemSelector;
    cmbZoom: TComboBox;
    fd: TFindDialog;
    rd: TReplaceDialog;
    pmTableIcon: TPopupMenu;
    SampleHintTablemenu1: TMenuItem;
    mitPageSetup: TMenuItem;
    mitPrint: TMenuItem;
    cmbUnits: TComboBox;
    ToolButton13: TToolButton;
    ToolButton22: TToolButton;
    ToolButton64: TToolButton;
    N27: TMenuItem;
    rvActionSubscript11: TMenuItem;
    rvActionSuperscript11: TMenuItem;
    SRVPageScroll1: TSRVPageScroll;
    stat1: TStatusBar;
    Button1: TButton;
    SclRVRuler2: TSclRVRuler;
    SRichViewEdit1: TSRichViewEdit;
    Splitter1: TSplitter;
    N28: TMenuItem;
    N29: TMenuItem;
    RemovePageBreak2: TMenuItem;
    ObjectPosition1: TMenuItem;
    PasteasText1: TMenuItem;
    N30: TMenuItem;
    mitTextFlow: TMenuItem;
    ClearTextFlowatLeftSide1: TMenuItem;
    ClearTextFlowatRightSide1: TMenuItem;
    ClearTextFlowatBothSides1: TMenuItem;
    NormalTextFlow1: TMenuItem;
    ToolBar6: TToolBar;
    ToolButton71: TToolButton;
    ToolButton72: TToolButton;
    ToolButton75: TToolButton;
    ToolButton76: TToolButton;
    ToolButton77: TToolButton;
    ToolButton78: TToolButton;
    btnPageFormat: TToolButton;
    pmPageFormat: TPopupMenu;
    A41: TMenuItem;
    A42: TMenuItem;
    A61: TMenuItem;
    Letter1: TMenuItem;
    Legal1: TMenuItem;
    N1000x1000mm1: TMenuItem;
    N31: TMenuItem;
    PageSetup1: TMenuItem;
    ToolButton65: TToolButton;
    ToolButton66: TToolButton;
    N32: TMenuItem;
    Footnote1: TMenuItem;
    Endnote1: TMenuItem;
    ReturntoNote1: TMenuItem;
    EditNote1: TMenuItem;
    Hide1: TMenuItem;
    ToolButton67: TToolButton;
    ToolButton68: TToolButton;
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure mitExitClick(Sender: TObject);
    procedure RichViewEdit1_Jump(Sender: TObject; id: Integer);
    procedure RichViewEdit1_ReadHyperlink(Sender: TCustomRichView;
      const Target, Extras: String; DocFormat: TRVLoadFormat; var StyleNo,
      ItemTag: Integer; var ItemName: TRVRawByteString);
    procedure cmbFontClick(Sender: TObject);
    procedure cmbKeyPress(Sender: TObject; var Key: Char);
    procedure RichViewEdit1_CurTextStyleChanged(Sender: TObject);
    procedure cmbFontSizeClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure RichViewEdit1_WriteHyperlink(Sender: TCustomRichView;
      id: Integer; RVData: TCustomRVData; ItemNo: Integer;
      SaveFormat: TRVSaveFormat; var Target, Extras: String);
    procedure RVAControlPanel1MarginsChanged(Sender: TrvAction;
      Edit: TCustomRichViewEdit);
    procedure RVAControlPanel1Download(Sender: TrvAction;
      const Source: String);
    procedure RichViewEdit1_KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure pmFakeDropDownPopup(Sender: TObject);
    procedure RichViewEdit1_KeyPress(Sender: TObject; var Key: Char);
    procedure cmbZoomChange(Sender: TObject);
    procedure SRichViewEdit1CurrentPageChange(Sender: TObject);
    procedure SRichViewEdit1HMenuClickButton(Sender: TObject;
      ToolButton: TSRVToolButton);
    procedure SRichViewEdit1PageScrolled(Sender: TObject);
    procedure SRichViewEdit1VMenuClickButton(Sender: TObject;
      ToolButton: TSRVToolButton);
    procedure SRichViewEdit1CaretMove(Sender: TObject);
    procedure srvtwSearchClickButton(Sender: TObject;
      ToolButton: TSRVToolButton);
    procedure fdFind(Sender: TObject);
    procedure SclRVRuler1MarginDblClick(Sender: TObject;
      Margin: TMarginType);
    procedure RVAControlPanel1BackgroundChange(Sender: TrvAction;
      Edit: TCustomRichViewEdit);
    procedure SRichViewEdit1TableIconClick(Sender: TSRichViewEdit;
      Button: TMouseButton; Shift: TShiftState; X, Y, PageNo: Integer);
    procedure RVAControlPanel1ViewChanged(Sender: TrvCustomAction;
      Edit: TCustomRichViewEdit);
    procedure SRichViewEdit1ZoomChanged(Sender: TObject);
    procedure cmbUnitsClick(Sender: TObject);
  private
    { Private declarations }
    VToolButtonIndex: Integer;
    UpdatingCombos: Boolean;
    procedure ColorPickerShow(Sender: TObject);
    procedure ColorPickerHide(Sender: TObject);
    procedure rvActionSave1DocumentFileChange(Sender: TObject;
      Editor: TCustomRichViewEdit; const FileName: String;
      FileFormat: TrvFileSaveFilter; IsNew: Boolean);
    procedure ApplicationHint(Sender: TObject);
    procedure Localize;
    procedure srvActionThumbnails1Executed(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

uses dmActionsSRV, Math;
{$R *.dfm}


procedure ShowInfo(const msg,cpt: String);
begin
  Application.MessageBox(PChar(msg),PChar(cpt),MB_OK or MB_ICONINFORMATION);
end;
{------------------------------------------------------------------------------}
procedure TForm3.FormCreate(Sender: TObject);
begin
  // Almost all these assignments could be done at design time in the Object Inspector
  // But in this demo we do not want to modify srvActionsResource
  // (and we recommend to use a copy of it in your applications)

  srvActionsResource.rvActionSave1.OnDocumentFileChange := rvActionSave1DocumentFileChange;
  //SRichViewEdit1.RichViewEdit.Style.FootnotePageReset := False;

  // Code for making color-picking buttons stay pressed while a
  // color-picker window is visible.
  srvActionsResource.rvActionColor1.OnShowColorPicker := ColorPickerShow;
  srvActionsResource.rvActionColor1.OnHideColorPicker := ColorPickerHide;
  srvActionsResource.rvActionParaColor1.OnShowColorPicker := ColorPickerShow;
  srvActionsResource.rvActionParaColor1.OnHideColorPicker := ColorPickerHide;
  srvActionsResource.rvActionFontColor1.OnShowColorPicker := ColorPickerShow;
  srvActionsResource.rvActionFontColor1.OnHideColorPicker := ColorPickerHide;
  srvActionsResource.rvActionFontBackColor1.OnShowColorPicker := ColorPickerShow;
  srvActionsResource.rvActionFontBackColor1.OnHideColorPicker := ColorPickerHide;

  // Index of the last clicked button in search toolwindow
  VToolButtonIndex := -1;

  // Initializing the action for showing/hiding thumbnails
  srvActionsResource.srvActionThumbnails1.PageScroll := SRVPageScroll1;
  srvActionsResource.srvActionThumbnails1.OnExecuted := srvActionThumbnails1Executed;

  // Delphi 4 and 5 do not have ActionComponent property for actions.
  // Coloring actions have a substitution - CallerControl property
  // It is ignored in Delphi 6+
  srvActionsResource.rvActionParaColor1.CallerControl := ToolButton39;
  srvActionsResource.rvActionFontBackColor1.CallerControl := ToolButton38;
  srvActionsResource.rvActionFontColor1.CallerControl := ToolButton36;

  {$IFDEF RICHVIEWDEF6}
  // AutoComplete feature causes OnClick generation when editing combo-box's text.
  // Since in OnClick we move input focus in RichViewEdit1, this effect is
  // undesirable
  cmbFont.AutoComplete := False;
  cmbFontSize.AutoComplete := False;
  {$ENDIF}

  // Applying the default language
  Localize;

  // Displaying hints on the status bar
  Application.OnHint := ApplicationHint;

  // measuring units
  RVA_ConvertToTwips(srvActionsResource);
  RVAControlPanel1.UnitsProgram := rvstuTwips;
  SRichViewEdit1.ConvertToTwips;

  cmbUnits.ItemIndex := ord(SRichViewEdit1.UnitsProgram);
  RVAControlPanel1.UnitsDisplay := SRichViewEdit1.UnitsProgram;

  // Loading initial file via ActionOpen (allowing to update user interface)
  srvActionsResource.rvActionOpen1.LoadFile(SRichViewEdit1.RichViewEdit,
    ExtractFilePath(Application.ExeName)+'readme.rvf', ffiRVF);

  {
  // alternative way to start
    srvActionsResource.rvActionNew1.ExecuteTarget(SRichViewEdit1.RichViewEdit);
  }

end;

{------------------- Working with document ------------------------------------}

// When document is created, saved, loaded...
procedure TForm3.rvActionSave1DocumentFileChange(Sender: TObject;
  Editor: TCustomRichViewEdit; const FileName: String;
  FileFormat: TrvFileSaveFilter; IsNew: Boolean);
var s: String;
begin
  s := ExtractFileName(FileName);
  srvActionsResource.srvActionPrint1.Title := s;
  srvActionsResource.srvActionQuickPrint1.Title := s;
  if IsNew then
    s := s+' (*)';
  Caption := s + ' - RichViewActionsTest';
end;

// Prompt for saving...
procedure TForm3.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := srvActionsResource.rvActionSave1.CanCloseDoc(SRichViewEdit1.RichViewEdit);
end;

procedure TForm3.mitExitClick(Sender: TObject);
begin
  Close;
end;

{--------------- Working with color-picking buttons ---------------------------}

// Code for making color-picking buttons pressed while
// a color-picker window is visible.
procedure TForm3.ColorPickerShow(Sender: TObject);
begin
  {$IFDEF RICHVIEWDEF6}
  if (Sender as TAction).ActionComponent is TToolButton then
    TToolButton(TAction(Sender).ActionComponent).Down := True;
  {$ELSE}
  if TrvActionCustomColor(Sender).CallerControl<>nil then
    TToolButton(TrvActionCustomColor(Sender).CallerControl).Down := True;
  {$ENDIF};
end;

procedure TForm3.ColorPickerHide(Sender: TObject);
begin
  {$IFDEF RICHVIEWDEF6}
  if (Sender as TAction).ActionComponent is TToolButton then
    TToolButton(TAction(Sender).ActionComponent).Down := False;
  {$ELSE}
  if TrvActionCustomColor(Sender).CallerControl<>nil then
    TToolButton(TrvActionCustomColor(Sender).CallerControl).Down := False;
  {$ENDIF}
end;

{-------------- Set of events for processing hypertext links ------------------}

// Hyperlink click
procedure TForm3.RichViewEdit1_Jump(Sender: TObject; id: Integer);
begin
  srvActionsResource.rvActionInsertHyperlink1.GoToLink(SRichViewEdit1.ActiveEditor, id);
end;

// Importing hyperlink
procedure TForm3.RichViewEdit1_ReadHyperlink(Sender: TCustomRichView;
  const Target, Extras: String; DocFormat: TRVLoadFormat; var StyleNo,
  ItemTag: Integer; var ItemName: TRVRawByteString);
var URL: String;
begin
  if DocFormat=rvlfURL then
    StyleNo :=
      srvActionsResource.rvActionInsertHyperlink1.GetHyperlinkStyleNo((Sender as TCustomRichViewEdit));
  URL := srvActionsResource.rvActionInsertHyperlink1.EncodeTarget(Target);
  ItemTag := Integer(StrNew(PChar(URL)));
end;

// Exporting hyperlink to RTF and HTML
procedure TForm3.RichViewEdit1_WriteHyperlink(Sender: TCustomRichView;
  id: Integer; RVData: TCustomRVData; ItemNo: Integer;
  SaveFormat: TRVSaveFormat; var Target, Extras: String);
begin
  Target := PChar(RVData.GetItemTag(ItemNo));
end;

// URL detection on typing
procedure TForm3.RichViewEdit1_KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key in [VK_SPACE, VK_RETURN, VK_TAB] then begin
    {
      // uncomment if you use Addict3
      RVA_Addict3AutoCorrect(SRichViewEdit1.ActiveEditor);
    }
    srvActionsResource.rvActionInsertHyperlink1.DetectURL(SRichViewEdit1.ActiveEditor);
    srvActionsResource.rvActionInsertHyperlink1.TerminateHyperlink(SRichViewEdit1.ActiveEditor);
  end;
end;

procedure TForm3.RichViewEdit1_KeyPress(Sender: TObject; var Key: Char);
begin
  {
  // uncomment if you use Addict3
  if (Key='''') or ((Key<>' ') and (Pos(Key, SRichViewEdit1.ActiveEditor.Delimiters)<>0)) then
    RVA_Addict3AutoCorrect(SRichViewEdit1.ActiveEditor);
  }
end;

{----------------------- Insert table popup      -----------------------------}
{ We use a trick: insert-table button has style tbsDropDown and assigned
  DropDownMenu (pmFakeDropDown). This menu is empty, but shows table size
  popup instead of itself }
procedure TForm3.pmFakeDropDownPopup(Sender: TObject);
begin
  srvActionsResource.rvActionInsertTable1.ShowTableSizeDialog(
    SRichViewEdit1.ActiveEditor, ToolButton12);
end;

{--------------------- Working with font combo-boxes --------------------------}

// Current font is changed. Updating the combo-boxes.
procedure TForm3.RichViewEdit1_CurTextStyleChanged(Sender: TObject);
var CurStyle: TFontInfo;
    rve: TCustomRichViewEdit;
begin
  UpdatingCombos := True;
  try
    rve := Sender as TCustomRichViewEdit;
    if rve = SRichViewEdit1.ActiveEditor then begin
      CurStyle := rve.Style.TextStyles[rve.CurTextStyleNo];
      cmbFont.Text := CurStyle.FontName;
      cmbFont.OnClick(Sender);
      cmbFontSize.FontName := CurStyle.FontName;
      cmbFontSize.Text := IntToStr(CurStyle.Size);
    end;
  finally
    UpdatingCombos := False;
  end;
end;

// Applying the font name
procedure TForm3.cmbFontClick(Sender: TObject);
var FontName: String;
begin
  if cmbFont.ItemIndex<0 then
    FontName := cmbFont.Text
  else
    FontName := cmbFont.Items[cmbFont.ItemIndex];
  if UpdatingCombos then
    exit;
  UpdatingCombos := True;
  cmbFontSize.FontName := FontName;
  UpdatingCombos := False;
  if cmbFont.ItemIndex<0 then
    cmbFont.ItemIndex := cmbFont.Items.IndexOf(FontName);
  if cmbFont.ItemIndex<0 then begin
      if Visible then beep
    end
  else
    with srvActionsResource.rvActionFontEx1 do begin
      UserInterface := False;
      ValidProperties := [rvfimFontName];
      Font.Name := FontName;
      Execute;
      UserInterface := True;
    end;
  SRichViewEdit1.SetFocus;
end;

// Changing the font size
procedure TForm3.cmbFontSizeClick(Sender: TObject);
var FontSize: Integer;
begin
  if UpdatingCombos then
    exit;
  try
    FontSize := StrToInt(cmbFontSize.Text);
    with srvActionsResource.rvActionFontEx1 do begin
      UserInterface := False;
      ValidProperties := [rvfimSize];
      Font.Size := FontSize;
      Execute;
      UserInterface := True;
    end;
  except
    if Visible then beep;
  end;
  SRichViewEdit1.SetFocus;
end;

// Code for pressing Enter or exiting the combo boxes
procedure TForm3.cmbKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then begin
    Key := #0;
    TComboBox(Sender).OnClick(Sender);
  end;
end;

{---------------------- Zooming combo box -------------------------------------}
// We could implement zooming using only actions, without a combo box
procedure TForm3.RVAControlPanel1ViewChanged(Sender: TrvCustomAction;
  Edit: TCustomRichViewEdit);
begin
  SRichViewEdit1ZoomChanged(Edit);
end;

procedure TForm3.SRichViewEdit1ZoomChanged(Sender: TObject);
begin
  UpdatingCombos := True;
  try
    case SRichViewEdit1.ViewProperty.ZoomMode of
      rvzmPageWidth:
        cmbZoom.ItemIndex := cmbZoom.Items.Count-2;
      rvzmFullPage:
        cmbZoom.ItemIndex := cmbZoom.Items.Count-1;
      rvzmCustom:
        cmbZoom.ItemIndex := cmbZoom.Items.IndexOf(
          IntToStr(Round(SRichViewEdit1.ViewProperty.ZoomPercent)));
    end;
  finally
    UpdatingCombos := False;
  end;
end;

// Applying zooming
procedure TForm3.cmbZoomChange(Sender: TObject);
//var Percent : Single;
begin
  if UpdatingCombos then
    exit;
  if cmbZoom.ItemIndex = cmbZoom.Items.Count-1 then
    SRichViewEdit1.ViewProperty.ZoomMode := rvzmFullPage
  else if cmbZoom.ItemIndex = cmbZoom.Items.Count-2 then
    SRichViewEdit1.ViewProperty.ZoomMode := rvzmPageWidth
  else if cmbZoom.ItemIndex>=0 then begin
    SRichViewEdit1.ViewProperty.ZoomPercent := StrToIntDef(cmbZoom.Text, 0);
    SRichViewEdit1.ViewProperty.ZoomMode := rvzmCustom;
  end;
  {
  // This code will not work, because TsrvActionZoomFullPage and
  // TsrvActionZoomPageWidth are disabled in a web mode

  if cmbZoom.ItemIndex = cmbZoom.Items.Count-1 then
    srvActionsResource.srvActionZoomFullPage1.Execute
  else if cmbZoom.ItemIndex = cmbZoom.Items.Count-2 then
    srvActionsResource.srvActionZoomPageWidth1.Execute
  else if cmbZoom.ItemIndex>=0 then begin
    Percent := srvActionsResource.srvActionZoom1.ZoomPercent;
    srvActionsResource.srvActionZoom1.ZoomPercent := StrToIntDef(cmbZoom.Text, 0);
    srvActionsResource.srvActionZoom1.Execute;
    srvActionsResource.srvActionZoom1.ZoomPercent := Percent;
  end;
  }
end;
{----------------------------- Units combo box --------------------------------}
// Applying units
procedure TForm3.cmbUnitsClick(Sender: TObject);
begin
  SRichViewEdit1.UnitsProgram := TRVUnits(cmbUnits.ItemIndex);
  RVAControlPanel1.UnitsDisplay := TRVUnits(cmbUnits.ItemIndex);
  SclRVRuler1.UnitsDisplay := TRulerUnits(cmbUnits.ItemIndex);
  SclRVRuler2.UnitsDisplay := TRulerUnits(cmbUnits.ItemIndex);  
end;

{------- Synchronizing ScaleRichView after non-editing operations -------------}
procedure TForm3.RVAControlPanel1MarginsChanged(Sender: TrvAction;
  Edit: TCustomRichViewEdit);
begin
  SRichViewEdit1.RichViewEdit.RVData.State :=
    SRichViewEdit1.RichViewEdit.RVData.State + [rvstSkipFormatting];
  SRichViewEdit1.SetRVMargins;
  SRichViewEdit1.RichViewEdit.Modified := False;

  cmbUnits.ItemIndex := ord(SRichViewEdit1.UnitsProgram);
  SclRVRuler1.UnitsDisplay := TRulerUnits(SRichViewEdit1.UnitsProgram);
  SclRVRuler2.UnitsDisplay := TRulerUnits(SRichViewEdit1.UnitsProgram);
  SRichViewEdit1.RichViewEdit.RVData.State :=
     SRichViewEdit1.RichViewEdit.RVData.State - [rvstSkipFormatting];
end;

procedure TForm3.RVAControlPanel1BackgroundChange(Sender: TrvAction;
  Edit: TCustomRichViewEdit);
begin
  SRichViewEdit1.Repaint;
end;

{---------------------------- Localization ------------------------------------}

procedure TForm3.Button1Click(Sender: TObject);
begin
  if RVA_ChooseLanguage then
    Localize;
end;

procedure TForm3.Localize;
var Index: Integer;
begin
  // Fonts
  Font.Charset            := RVA_GetCharset;
  stat1.Font.Charset := RVA_GetCharset;
  {$IFDEF RICHVIEWDEF6}
  Screen.HintFont.Charset := RVA_GetCharset;
  Screen.MenuFont.Charset := RVA_GetCharset;
  {$ENDIF}
  // Localizing all actions on srvActionsResource
  RVA_LocalizeForm(srvActionsResource);
  // Localizing all actions on this form
  RVA_LocalizeForm(Self);
  // Localizing the rulers
  RVALocalizeRuler(SclRVRuler1);
  RVALocalizeRuler(SclRVRuler2);
  // Localizing the editor
  SRVA_LocalizeSRichViewEdit(SRichViewEdit1);
  // Localizing the search tool window
  SRVA_LocalizeToolWindow(srvtwSearch);
  // Localizing menus and tool buttons
  mitFile.Caption := RVA_GetS(rvam_menu_File);
  mitEdit.Caption := RVA_GetS(rvam_menu_Edit);
  mitFont.Caption := RVA_GetS(rvam_menu_Font);
  mitPara.Caption := RVA_GetS(rvam_menu_Para);
  mitFormat.Caption := RVA_GetS(rvam_menu_Format);
  mitInsert.Caption := RVA_GetS(rvam_menu_Insert);
  mitTable.Caption := RVA_GetS(rvam_menu_Table);
  mitExit.Caption := RVA_GetS(rvam_menu_Exit);

  mitFontSize.Caption := RVA_GetS(rvam_menu_FontSize);
  mitFontStyle.Caption := RVA_GetS(rvam_menu_FontStyle);
  mitTextFlow.Caption := RVA_GetS(rvam_menu_TextFlow);

  mitTableSelect.Caption := RVA_GetS(rvam_menu_TableSelect);
  mitTableCellBorders.Caption := RVA_GetS(rvam_menu_TableCellBorders);
  mitTableAlignCellContents.Caption := RVA_GetS(rvam_menu_TableCellAlign);

  btnPageFormat.Caption := SRVA_GetS(srvam_menu_PageSize);
  btnPageFormat.Hint := RVADeleteAmp(btnPageFormat.Caption);

  // In your application, you can use either TrvActionFonts or TrvActionFontEx
  srvActionsResource.rvActionFonts1.Caption := srvActionsResource.rvActionFonts1.Caption+' (Standard)';
  srvActionsResource.rvActionFontEx1.Caption := srvActionsResource.rvActionFontEx1.Caption+' (Advanced)';

  // Localizing measuring units
  Index := cmbUnits.ItemIndex;
  RVA_TranslateUnits(cmbUnits.Items);
  cmbUnits.ItemIndex := Index;
  cmbUnits.ItemIndex := ord(SRichViewEdit1.UnitsProgram);

  // Localizing the zoom combo box

  Index := cmbZoom.ItemIndex;
  cmbZoom.Items[cmbZoom.Items.Count-2] := RVA_GetS(rvam_pp_PageWidth);
  cmbZoom.Items[cmbZoom.Items.Count-1] := RVA_GetS(rvam_pp_FullPage);
  cmbZoom.ItemIndex := Index;

  // Localizing toolbar in the horizontal scroll bar area
  TSRVToolButton(SRichViewEdit1.MenuHButtons.Items[0]).Hint := RVADeleteAmp(SRVA_GetS(srvam_act_Draft));
  TSRVToolButton(SRichViewEdit1.MenuHButtons.Items[1]).Hint := RVADeleteAmp(SRVA_GetS(srvam_act_WebLayout));
  TSRVToolButton(SRichViewEdit1.MenuHButtons.Items[2]).Hint := RVADeleteAmp(SRVA_GetS(srvam_act_PrintLayout));

  // Localizing text on the status bar
  SRichViewEdit1CurrentPageChange(SRichViewEdit1);
  SRichViewEdit1PageScrolled(SRichViewEdit1);
  SRichViewEdit1CaretMove(SRichViewEdit1);

  {
  // uncomment if you use Addict3. It's assumed that RVAddictSpell31
  // and RVThesaurus31 are on this form.
  RVAddictSpell31.UILanguage := GetAddictSpellLanguage(RVA_GetLanguageName);
  RVThesaurus31.UILanguage := GetAddictThesLanguage(RVA_GetLanguageName);
  }
end;

{---------------------- Live spelling with Addict 3 ---------------------------}
(*
// Add these events if you use Addict3
// (Assuming that you have RVAddictSpell31: TRVAddictSpell3 on the form)

// RichViewEdit1.OnSpellingCheck event
procedure TForm3.RichViewEdit1SpellingCheck(Sender: TCustomRichView;
  const AWord: String; StyleNo: Integer; var Misspelled: Boolean);
begin
  Misspelled := not RVAddictSpell31.WordAcceptable(AWord);
end;

// RVAddictSpell31.OnParserIgnoreWord: if Ignore All or Add buttons were pressed
// in spellchecker dialog, removing underlines from the ignored word
procedure TForm3.RVAddictSpell31ParserIgnoreWord(Sender: TObject;
  Before: Boolean; State: Integer);
begin
  if not Before and (State in [IgnoreState_IgnoreAll, IgnoreState_Add]) then
    SRichViewEdit1.RichViewEdit.LiveSpellingValidateWord(RVAddictSpell31.CurrentWord);
end;

// Besides, if you want spelling check starting when loading document, call
// SRichViewEdit1.RichViewEdit.StartLiveSpelling in rvActionFileOpen.OnOpenFile event
*)
{------------------------------ Status bar ------------------------------------}
// Displaying the index of the edited page
procedure TForm3.SRichViewEdit1CurrentPageChange(Sender: TObject);
begin
  stat1.Panels.Items[1].Text := Format(SRVA_GetS(srvam_PageMofN),
    [SRichViewEdit1.CurrentPage, SRichViewEdit1.PageCount]);
end;
// Displaying the index of the visible page
procedure TForm3.SRichViewEdit1PageScrolled(Sender: TObject);
begin
  stat1.Panels.Items[0].Text  := Format(SRVA_GetS(srvam_ViewPageX),
    [SRichViewEdit1.ScrolledPage]);
end;
// Displaying the current line and column
procedure TForm3.SRichViewEdit1CaretMove(Sender: TObject);
var Line, Column : Integer;
begin
  SRichViewEdit1.GetCurrentLineCol(Line, Column);
  stat1.Panels.Items[2].Text := Format(SRVA_GetS(srvam_LineX), [Line]);
  stat1.Panels.Items[3].Text := Format(SRVA_GetS(srvam_ColX), [Column]);
end;
// Displaying hints
procedure TForm3.ApplicationHint(Sender: TObject);
var s: String;
begin
  s := GetLongHint(Application.Hint);
  stat1.SimplePanel := s<>'';
  stat1.SimpleText := s;
  stat1.Repaint;
end;
{--------------------- Buttons on scroll-bars ---------------------------------}
// On horizontal scroll bar (view modes)
procedure TForm3.SRichViewEdit1HMenuClickButton(Sender: TObject;
  ToolButton: TSRVToolButton);
begin
  if ToolButton = nil then Exit;
  SRichViewEdit1.CanUpdate := False;
  case (ToolButton.Index) of
    0 : srvActionsResource.srvActionLayoutDraft1.Execute;
    1 : srvActionsResource.srvActionLayoutWeb1.Execute;
    2 : srvActionsResource.srvActionLayoutPrint1.Execute;
  end;
  SRichViewEdit1.CanUpdate := True;
end;
// On vertical scroll bar
procedure TForm3.SRichViewEdit1VMenuClickButton(Sender: TObject;
  ToolButton: TSRVToolButton);
var
     sbX, sbY : Integer;
     p : TPoint;
begin
  if ToolButton = nil then Exit;
  case ToolButton.Index of
    0 :  // "Up arrows" button, performs search to the top
      begin
          case VToolButtonIndex of
            SRV_TOOLWIN_PAGE, -1 : SRichViewEdit1.PriorCurPage;
            SRV_TOOLWIN_TABLE : SRichViewEdit1.PriorCurItem([rvsTable]);
            SRV_TOOLWIN_PICTURE : SRichViewEdit1.PriorCurItem([rvsPicture, rvsHotspot, rvsHotPicture]);
            SRV_TOOLWIN_HEADLINE : SRichViewEdit1.PriorCurHeading;
            SRV_TOOLWIN_HYPERLINK : SRichViewEdit1.PriorCurHyperlink;
            SRV_TOOLWIN_TEXT :
              begin
                fd.Options := fd.Options - [frDown];
                fdFind(nil);
              end;
            SRV_TOOLWIN_FOOTNOTE: SRichViewEdit1.PriorCurItem([rvsFootnote]);
            SRV_TOOLWIN_ENDNOTE: SRichViewEdit1.PriorCurItem([rvsEndnote]);
          end;
      end;
    1 : // "Circle" button, shows tool window
      begin
        sbX := SRichViewEdit1.MenuVertical.SRVToolBar.Width;
        sbY := SRichViewEdit1.MenuVertical.SRVToolBar.Height div 3;

        p := SRichViewEdit1.MenuVertical.SRVToolBar.ClientToScreen(Point(0, 0));
        p.Y := p.Y + sbY;

        srvtwSearch.Execute(Bounds(p.x, p.y, sbX, sbY));
      end;
    2 : // "Down arrows" button, performs search to the bottom
      begin
          case VToolButtonIndex of
            SRV_TOOLWIN_PAGE, -1 : SRichViewEdit1.NextCurPage;
            SRV_TOOLWIN_TABLE : SRichViewEdit1.NextCurItem([rvsTable]);
            SRV_TOOLWIN_PICTURE : SRichViewEdit1.NextCurItem([rvsPicture, rvsHotspot, rvsHotPicture]);
            SRV_TOOLWIN_HEADLINE : SRichViewEdit1.NextCurHeading;
            SRV_TOOLWIN_HYPERLINK : SRichViewEdit1.NextCurHyperlink;
            SRV_TOOLWIN_TEXT :
              begin
                fd.Options := fd.Options + [frDown];
                fdFind(nil);
              end;
            SRV_TOOLWIN_FOOTNOTE: SRichViewEdit1.NextCurItem([rvsFootnote]);
            SRV_TOOLWIN_ENDNOTE: SRichViewEdit1.NextCurItem([rvsEndnote]);
          end;
      end;
  end;
end;
// Button on the tool window is clicked
procedure TForm3.srvtwSearchClickButton(Sender: TObject;
  ToolButton: TSRVToolButton);
var
     s: String;
     posit: Integer;
begin
  if ToolButton = nil then begin
    VToolButtonIndex := -1;
    Exit;
    end
  else
    VToolButtonIndex := ToolButton.Index;
  if ToolButton.Index = SRV_TOOLWIN_TEXT then // starting text search
    begin
      SRichViewEdit1.SetFocus;
      fd.CloseDialog;
      if SRichViewEdit1.RichViewEdit.SelectionExists then
        begin
          s := SRichViewEdit1.RichViewEdit.GetSelText;
          posit := Pos(#13,s);
          if posit<>0 then
            s := Copy(s,1,posit-1);
          fd.FindText := s;
        end;
      fd.Execute;
    end
  else // starting search
    SRichViewEdit1VMenuClickButton(Sender,
      TSRVToolButton(SRichViewEdit1.MenuVButtons.Items[2]));
end;
// text search
procedure TForm3.fdFind(Sender: TObject);
begin
  if not SRichViewEdit1.RichViewEdit.SearchText(fd.FindText,GetRVESearchOptions(fd.Options)) then
    ShowInfo(Format(RVA_GetS(rvam_src_NotFound), [fd.FindText]), RVA_GetS(rvam_src_Complete));
  SRichViewEdit1.Repaint;
end;
{-------------------------------- Misc. ---------------------------------------}
// On dowloading image
procedure TForm3.RVAControlPanel1Download(Sender: TrvAction;
  const Source: String);
begin
  if Source='' then
    Application.Hint := ''
  else
    Application.Hint := 'Downloading '+Source+'...';
end;
// On clicking a table icon
procedure TForm3.SRichViewEdit1TableIconClick(Sender: TSRichViewEdit;
  Button: TMouseButton; Shift: TShiftState; X, Y, PageNo: Integer);
begin
  SRichViewEdit1.GetTableIconItem.SelectRows(0, SRichViewEdit1.GetTableIconItem.RowCount);
  SRichViewEdit1.Repaint;
end;
//  Double clicking on the rulers' margins shows the page setup dialog
procedure TForm3.SclRVRuler1MarginDblClick(Sender: TObject;
  Margin: TMarginType);
begin
  srvActionsResource.srvActionPageSetup1.ExecuteTarget(SRichViewEdit1);
end;
// After TsrvActionThumbnails is executed
procedure TForm3.srvActionThumbnails1Executed(Sender: TObject);
begin
  Splitter1.Visible := TsrvActionThumbnails(Sender).Checked;
  SRVPageScroll1.Visible := TsrvActionThumbnails(Sender).Checked;
end;


initialization

// Initializing graphics

{$IFDEF USE_GIFIMAGE}
  RegisterClass(TGifImage);
  RV_RegisterHTMLGraphicFormat(TGifImage);
{$ENDIF}
{$IFDEF USE_PNGOBJECT}
  {$IFDEF RICHVIEWDEF2009}
  // RegisterClass(TPngImage); - not necessary, because RegisterClassAlias registers
  RV_RegisterHTMLGraphicFormat(TPngImage);
  RV_RegisterPngGraphic(TPngImage);
  // allows loading old RVF files saved with TPNGObject instead of TPngImage
  RegisterClassAlias(TPngImage, 'TPNGObject');
  {$ELSE}
  RegisterClass(TPngObject);
  RV_RegisterHTMLGraphicFormat(TPngObject);
  RV_RegisterPngGraphic(TPngObject);
  {$ENDIF}
{$ENDIF}

// IMPORTANT: allowing RichViewActions and TRVAPopupMenu working with TSRichViewEdit
  RVA_GetRichViewEditFromPopupComponent :=
   SRVGetRichViewEditFromPopupComponent;
  RVA_GetRichViewEdit := SRVGetRichViewEdit;

end.