unit uViewRichEditor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, Menus, cxLookAndFeelPainters, cxGraphics, cxSpinEdit,
  cxSpinButton, cxMaskEdit, cxFontNameComboBox, cxControls, cxContainer, cxEdit,
  cxTextEdit, cxDropDownEdit, cxColorComboBox, StdCtrls, cxButtons, ComCtrls,
  ToolWin, ImgList, ActnList, TBXExtItems, TB2ExtItems, TB2Item, TBX, TB2Dock,
  TB2Toolbar, PngImageList, cxMemo, cxRichEdit, ExtCtrls, DB, uDAInterfaces,
  uDADataTable, cxDBRichEdit;

type
  IViewRichEditor = interface(IViewBase)
    ['{C29F4555-A57A-436D-BF23-2E2F013BC13D}']
  end;


  TfrViewRichEditor = class(TfrViewBase, IViewRichEditor)
    ActionList: TActionList;
    actEditCut: TAction;
    actEditCopy: TAction;
    actEditPaste: TAction;
    actEditFont: TAction;
    actBullets: TAction;
    pmColorSchemes: TPopupMenu;
    miStandard: TMenuItem;
    miHTML: TMenuItem;
    miWeb: TMenuItem;
    miWebSorted: TMenuItem;
    ToolbarImages: TImageList;
    TBXDock1: TTBXDock;
    TBXToolbar1: TTBXToolbar;
    bBold: TTBXItem;
    bItalic: TTBXItem;
    TBXSeparatorItem1: TTBXSeparatorItem;
    bUnderline: TTBXItem;
    TBXSeparatorItem2: TTBXSeparatorItem;
    TBXItem5: TTBXItem;
    TBXItem6: TTBXItem;
    TBXItem7: TTBXItem;
    TBXSeparatorItem4: TTBXSeparatorItem;
    bAlignLeft: TTBXItem;
    bAlignCenter: TTBXItem;
    bAlignRight: TTBXItem;
    LargeImages: TPngImageList;
    SmallImages: TPngImageList;
    TBControlItem1: TTBControlItem;
    fcbFontName: TcxFontNameComboBox;
    TBXColorItem1: TTBXColorItem;
    RichEdit: TcxDBRichEdit;
    TBControlItem2: TTBControlItem;
    meFontSize: TcxMaskEdit;
    TBControlItem3: TTBControlItem;
    cxSpinButton: TcxSpinButton;
    TBXSeparatorItem3: TTBXSeparatorItem;
    TBXSeparatorItem5: TTBXSeparatorItem;
    bBullets: TTBXItem;
    procedure actBulletsExecute(Sender: TObject);
    procedure actEditCutExecute(Sender: TObject);
    procedure actEditCopyExecute(Sender: TObject);
    procedure actEditPasteExecute(Sender: TObject);
    procedure RichEditPropertiesChange(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);
    procedure RichEditPropertiesSelectionChange(Sender: TObject);
    procedure cxFontNameComboBox1PropertiesChange(Sender: TObject);
    procedure CustomViewCreate(Sender: TObject);
    procedure fcbFontNamePropertiesInitPopup(Sender: TObject);
    procedure meFontSizePropertiesChange(Sender: TObject);
    procedure bBoldClick(Sender: TObject);
    procedure bItalicClick(Sender: TObject);
    procedure bUnderlineClick(Sender: TObject);
    procedure bAlignLeftClick(Sender: TObject);
    procedure bAlignCenterClick(Sender: TObject);
    procedure bAlignRightClick(Sender: TObject);
    procedure bBulletsClick(Sender: TObject);
  private
    FUpdating: Boolean;
    FChanged: Boolean;
    function CurrText: TTextAttributes;
  public
    { Public declarations }
  end;

implementation

{$R *.dfm}

procedure TfrViewRichEditor.actBulletsExecute(Sender: TObject);
begin
  if FUpdating or (RichEdit = nil) then Exit;
  RichEdit.Paragraph.Numbering := TNumberingStyle(actBullets.Checked);
end;

procedure TfrViewRichEditor.actEditCopyExecute(Sender: TObject);
begin
  RichEdit.CopyToClipboard;
end;

procedure TfrViewRichEditor.actEditCutExecute(Sender: TObject);
begin
  RichEdit.CutToClipboard;
end;

procedure TfrViewRichEditor.actEditPasteExecute(Sender: TObject);
begin
  RichEdit.PasteFromClipboard;
end;

procedure TfrViewRichEditor.bAlignCenterClick(Sender: TObject);
begin
  if FUpdating or (RichEdit = nil) then Exit;
  RichEdit.Paragraph.Alignment := TAlignment(TControl(Sender).Tag);
end;

procedure TfrViewRichEditor.bAlignLeftClick(Sender: TObject);
begin
  if FUpdating or (RichEdit = nil) then Exit;
  RichEdit.Paragraph.Alignment := TAlignment(TControl(Sender).Tag);
end;

procedure TfrViewRichEditor.bAlignRightClick(Sender: TObject);
begin
  if FUpdating or (RichEdit = nil) then Exit;
  RichEdit.Paragraph.Alignment := TAlignment(TControl(Sender).Tag);
end;

procedure TfrViewRichEditor.bBoldClick(Sender: TObject);
begin
  if FUpdating then Exit;
  if bBold.Checked then
    CurrText.Style := CurrText.Style + [fsBold]
  else
    CurrText.Style := CurrText.Style - [fsBold];
end;

procedure TfrViewRichEditor.bBulletsClick(Sender: TObject);
begin
  if FUpdating or (RichEdit = nil) then Exit;
  RichEdit.Paragraph.Numbering := TNumberingStyle(bBullets.Checked);
end;

procedure TfrViewRichEditor.bItalicClick(Sender: TObject);
begin
  if FUpdating then Exit;
  if bItalic.Checked then
    CurrText.Style := CurrText.Style + [fsItalic]
  else
    CurrText.Style := CurrText.Style - [fsItalic];
end;

procedure TfrViewRichEditor.bUnderlineClick(Sender: TObject);
begin
  if FUpdating then Exit;
  if bUnderline.Checked then
    CurrText.Style := CurrText.Style + [fsUnderline]
  else
    CurrText.Style := CurrText.Style - [fsUnderline];
end;

function TfrViewRichEditor.CurrText: TTextAttributes;
begin
{  if RichEdit.SelLength > 0 then}
    Result := RichEdit.SelAttributes
{  else
    Result := RichEdit.DefAttributes;}
end;

procedure TfrViewRichEditor.CustomViewCreate(Sender: TObject);
begin
  inherited;
  CurrText.Name := 'Tahoma';
  CurrText.Size := 11;
  RichEditPropertiesSelectionChange(Self);
end;

procedure TfrViewRichEditor.CustomViewShow(Sender: TObject);
begin
  inherited;
  FChanged := False;
end;

procedure TfrViewRichEditor.cxFontNameComboBox1PropertiesChange(
  Sender: TObject);
begin
  if FUpdating then Exit;
  CurrText.Name := TcxFontNameComboBox(Sender).Text;
end;

procedure TfrViewRichEditor.fcbFontNamePropertiesInitPopup(Sender: TObject);
begin
  inherited;
  fcbFontName.Properties.FontPreview.FontStyle := CurrText.Style;
end;

procedure TfrViewRichEditor.meFontSizePropertiesChange(Sender: TObject);
begin
  if FUpdating then Exit;
  if meFontSize.EditText <> '' then
    CurrText.Size := StrToInt(meFontSize.EditText)
  else
    CurrText.Size := 0;
end;

procedure TfrViewRichEditor.RichEditPropertiesChange(Sender: TObject);
begin
  FChanged := True;
end;

procedure TfrViewRichEditor.RichEditPropertiesSelectionChange(Sender: TObject);
begin
  with RichEdit.Paragraph do
  try
    FUpdating := True;
    bBold.Checked := fsBold in CurrText.Style;
    bItalic.Checked := fsItalic in CurrText.Style;
    bUnderline.Checked := fsUnderline in CurrText.Style;
    bBullets.Checked := Boolean(Numbering);
    meFontSize.Text := IntToStr(CurrText.Size);
    fcbFontName.EditValue := CurrText.Name;
    //cxColorComboBox.EditValue := CurrText.Color;
    case Ord(Alignment) of
      0: begin
          bAlignLeft.Checked := True;
         end;
      1: begin
          bAlignRight.Checked := True;
         end;
      2: begin
          bAlignCenter.Checked := True;
         end;
    end;
  finally
    FUpdating := False;
  end;
end;

end.
