unit TextReport;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, Menus, Buttons, ToolWin, ClipBrd, SharedData,
  SharedCode, MiscRoutines;

type
  TReportForm = class(TForm)
    Edit: TRichEdit;
    ToolBar: TToolBar;
    EditPopup: TPopupMenu;
    SelectAllItem: TMenuItem;
    CutItem: TMenuItem;
    CopyItem: TMenuItem;
    PasteItem: TMenuItem;
    ClearItem: TMenuItem;
    SaveAsItem: TMenuItem;
    SaveDialog: TSaveDialog;
    UndoItem: TMenuItem;
    SaveItem: TMenuItem;
    ReportMenu: TMainMenu;
    ReportEditMenu: TMenuItem;
    ReportUndoItem: TMenuItem;
    ReportSelectAllItem: TMenuItem;
    ReportCutItem: TMenuItem;
    ReportCopyItem: TMenuItem;
    ReportPasteItem: TMenuItem;
    ReportClearItem: TMenuItem;
    ReportExportMenu: TMenuItem;
    ReportSaveAsItem: TMenuItem;
    ReportSaveItem: TMenuItem;
    SelectAllButton: TToolButton;
    ILNormal: TImageList;
    ILHot: TImageList;
    ILDisabled: TImageList;
    DeselectButton: TToolButton;
    ToolButton1: TToolButton;
    CutButton: TToolButton;
    CopyButton: TToolButton;
    PasteButton: TToolButton;
    UndoButton: TToolButton;
    ToolButton3: TToolButton;
    ClearButton: TToolButton;
    ToolButton2: TToolButton;
    SaveButton: TToolButton;
    SaveAsButton: TToolButton;
    ReportDeselectItem: TMenuItem;
    DeselectItem: TMenuItem;
    FontDialog: TFontDialog;
    FormatMenu: TMenuItem;
    FontItem: TMenuItem;
    BackColorItem: TMenuItem;
    ColorDialog: TColorDialog;
    procedure SelectAllItemClick(Sender: TObject);
    procedure CutItemClick(Sender: TObject);
    procedure CopyItemClick(Sender: TObject);
    procedure PasteItemClick(Sender: TObject);
    procedure ClearItemClick(Sender: TObject);
    procedure SaveAsItemClick(Sender: TObject);
    procedure UndoItemClick(Sender: TObject);
    procedure SaveItemClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ReportDeselectItemClick(Sender: TObject);
    procedure ReportEditMenuClick(Sender: TObject);
    procedure EditMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FontItemClick(Sender: TObject);
    procedure BackColorItemClick(Sender: TObject);
  private
    { Private declarations }
  public
    FileName  : string;
    Plain     : Boolean;
    function  Save:Boolean;
    function  SaveAs:Boolean;
    { Public declarations }
  end;

var
  ReportForm: TReportForm;

implementation

{$R *.DFM}

uses MainUnit, Setup;

function TReportForm.SaveAs;
var
  DriveDir,Name,Ext : string;
begin
  Result:=False;
  if(FileName<>'') then
  begin
    SplitPath(SaveDialog.FileName,DriveDir,Name,Ext);
    SaveDialog.FileName:=Name;
  end;
  if(SaveDialog.Execute) then
  begin
    SplitPath(SaveDialog.FileName,DriveDir,Name,Ext);
    Caption:='Отчет - ['+Name+']';
    SaveItem.Enabled:=True;
    SaveButton.Enabled:=True;
    if(Ext='') then
    begin
      case SaveDialog.FilterIndex of
        1 : Ext:='rtf';
        2 : Ext:='txt';
      end;
    end;
    FileName:=MakePath(DriveDir,Name,Ext);
    Assert(FileName<>'');
    Plain:=Ext<>'rtf';
    Result:=Save;
  end;
end;

function TReportForm.Save;
begin
  if(FileName<>'') then
  begin
    Edit.PlainText:=Plain;
    Edit.Lines.SaveToFile(FileName);
    Edit.Modified:=False;
    Edit.PlainText:=False;
    Result:=True;
    ActiveControl:=Edit;
  end
  else
    Result:=SaveAs;
end;

procedure TReportForm.SelectAllItemClick(Sender: TObject);
begin
  Edit.SelectAll;
  ActiveControl:=Edit;
end;

procedure TReportForm.CutItemClick(Sender: TObject);
begin
  if(Edit.SelLength>0) then
    Edit.CutToClipboard;
  ActiveControl:=Edit;
end;

procedure TReportForm.CopyItemClick(Sender: TObject);
begin
  if(Edit.SelLength>0) then
    Edit.CopyToClipboard;
  ActiveControl:=Edit;
end;

procedure TReportForm.PasteItemClick(Sender: TObject);
begin
  if(Clipboard.HasFormat(CF_TEXT)) then
    Edit.PasteFromClipboard;
  ActiveControl:=Edit;
end;

procedure TReportForm.ClearItemClick(Sender: TObject);
begin
  Edit.SelectAll;
  Edit.ClearSelection;
  ActiveControl:=Edit;
end;

procedure TReportForm.SaveAsItemClick(Sender: TObject);
begin
  SaveAs;
end;

procedure TReportForm.UndoItemClick(Sender: TObject);
begin
  if(Edit.Perform(EM_CANUNDO,0,0)<>0) then
    Edit.Perform(EM_UNDO,0,0);
  ActiveControl:=Edit;
end;

procedure TReportForm.SaveItemClick(Sender: TObject);
begin
  Save;
end;

procedure TReportForm.FormCreate(Sender: TObject);
begin
  FileName:='';
  Plain:=False;
  ActiveControl:=Edit;
  Edit.Color:=AppSetup.ReportFlags.BackColor;
  Edit.Font.Handle:=CreateFontIndirect(AppSetup.ReportFlags.Font);
  Edit.Font.Color:=AppSetup.ReportFlags.FontColor;
end;

procedure TReportForm.ReportDeselectItemClick(Sender: TObject);
begin
  Edit.SelLength:=0;
end;

procedure TReportForm.ReportEditMenuClick(Sender: TObject);
begin
  ReportPasteItem.Enabled:=Clipboard.HasFormat(CF_TEXT);
end;

procedure TReportForm.EditMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  PasteItem.Enabled:=Clipboard.HasFormat(CF_TEXT);
end;

procedure TReportForm.FontItemClick(Sender: TObject);
begin
  FontDialog.Font:=Edit.Font;
  if(FontDialog.Execute) then
  begin
    Edit.Font:=FontDialog.Font;
    GetObject(Edit.Font.Handle,sizeof(TLogFont),
      @AppSetup.ReportFlags.Font);
    AppSetup.ReportFlags.FontColor:=Edit.Font.Color;
  end;
end;

procedure TReportForm.BackColorItemClick(Sender: TObject);
begin
  ColorDialog.Color:=Edit.Color;
  if(ColorDialog.Execute) then
  begin
    Edit.Color:=ColorDialog.Color;
    AppSetup.ReportFlags.BackColor:=Edit.Color;
  end;
end;

end.
