(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)

unit AHMTTextFileView;

interface
{$I AHM.INC}
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, ComCtrls, StdCtrls, Menus,Printers,AHMDialogClass;

type
  TAHMTextFileView = class(TAHMDialogComponent)
  private
     fedcap,ffil : String;
     FEditColor : Tcolor;
     fonopen,fonsave,fonexit,fonexec : TNotifyevent;
     ffon : Tfont;
     N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11 : Boolean;
     FOpenFileOnExecute : Boolean;
     FWindowPosition : TPosition;
     FWindowHeight,FWindowWidth : Integer;
     FBorderIcons : TBorderIcons;
     fmi : TMainMenu;
     FWindowState : TWindowstate;
     fico : TIcon;
     FReadOnly : Boolean;
     Procedure SetMenu(value: TMainMenu);
     Procedure Setfont(value: Tfont);
     Procedure SetMenuFile(value : Boolean);
     Procedure SetMenuNew(value : Boolean);
     Procedure SetMenuSave(value : Boolean);
     Procedure SetMenuPrint(value : Boolean);
     Procedure SetMenuExit(value : Boolean);
     Procedure SetMenuOptions(value : Boolean);
     Procedure SetMenuWordwrap(value : Boolean);
     Procedure SetMenuFont(value : Boolean);
     Procedure SetMenuOpen(value : Boolean);
     Procedure SetMenuDividers(value : Boolean);
     Procedure SetMenuBackground(value : Boolean);
     Procedure SetBordericons(value : Tbordericons);
     Procedure SetIcon(value : Ticon);
  protected
  public
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
    Function execute : Boolean; override;
  published
    Property EditorCaption : String read fedcap write fedcap;
    Property EditColor : TColor read FEditColor write FEditColor default clwhite;
    Property EditFonts : TFont read ffon write Setfont;
    Property MenuFile : Boolean read N1 write SetMenufile default true;
    Property MenuOptions : Boolean read N2 write SetMenuOptions default true;
    Property MenuNew :Boolean read N3 write SetMenuNew default true;
    Property MenuOpen :Boolean read N4 write SetMenuOpen default true;
    Property MenuSave :Boolean read N5 write SetMenuSave default true;
    Property MenuPrint :Boolean read N6 write SetMenuPrint default true;
    Property MenuExit :Boolean read N7 write SetMenuExit default true;
    Property MenuWordWrap :Boolean read N8 write SetMenuWordWrap default true;
    Property MenuFont :Boolean read N9 write SetMenuFont default true;
    Property MenuBackground :Boolean read N10 write SetMenuBackground default true;
    Property MenuDividers :Boolean read N11 write SetMenuDividers default true;
    Property Menus : TMainMenu read fmi write SetMenu;
    Property OpenFilename : String read ffil write ffil;
    Property OpenFileOnExecute : Boolean read FOpenFileOnExecute write FOpenFileOnExecute default False;
    Property OnExecute : Tnotifyevent read fonexec write fonexec;
    Property OnExit : TNotifyevent read fonexit write fonexit;
    Property OnOpenFile : TNotifyevent read fonopen write fonopen;
    Property OnSaveFile : TNotifyevent read fonsave write fonsave;
    Property Windowstate : TWindowstate read FWindowState write FWindowState default wsNormal;
    Property WindowPosition : TPosition read FWindowPosition write FWindowPosition default poScreenCenter;
    Property BorderIcons : TBorderIcons read FBorderIcons write SetborderIcons default [];
    Property WindowWidth : Integer read FWindowWidth write FWindowWidth default 400;
    Property WindowHeight : Integer read FWindowHeight write FWindowHeight default 240;
    Property Icon : TIcon read fico write SetIcon;
    Property Readonly : Boolean read FReadOnly write FReadOnly default False;
  end;


implementation
uses AHMDialogConst;
type
  TAHMTextViewForm = class(TForm)
  private
    RichEdit1: TRichEdit;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Edit1: TMenuItem;
    New1: TMenuItem;
    Open1: TMenuItem;
    Save1: TMenuItem;
    Div1: TMenuItem;
    Print1: TMenuItem;
    Div2: TMenuItem;
    Exit1: TMenuItem;
    WordWrap1: TMenuItem;
    PrintDialog1: TPrintDialog;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Font1: TMenuItem;
    Div3: TMenuItem;
    FontDialog1: TFontDialog;
    Background1: TMenuItem;
    ColorDialog1: TColorDialog;
    procedure Print1Click(Sender: TObject);
    procedure New1Click(Sender: TObject);
    procedure Open1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Font1Click(Sender: TObject);
    procedure WordWrap1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Background1Click(Sender: TObject);
  public
    aexit,aopen,asave : TNotifyevent;
    { Public declarations }
    Procedure SetupForm;
  end;

procedure TAHMTextViewForm.SetupForm;
begin
  Left := 100;
  Top := 100;
  Width := 400;
  Height := 280;
  HorzScrollBar.Visible := False;
  VertScrollBar.Visible := False;
  Color := clBtnFace;
  FormStyle:=fsNormal;
  RichEdit1:= TRichEdit.Create(Self);
  with RichEdit1 do
  begin
    Parent:=Self;
    Align := alClient;
    HideSelection := False;
    HideScrollBars := False;
    PlainText := True;
    ScrollBars := ssBoth;
    TabOrder := 0;
    WantTabs := True;
  end;
  MainMenu1:= TMainMenu.Create(Self);
  with MainMenu1 do
  begin
     Parent:=Self;
     File1:=TMenuItem.Create(MainMenu1);
     with File1 do
     begin
       Caption := ahmrcdlgfile;
       MainMenu1.Items.Add(File1);
       GroupIndex := 1;
     end;
     New1:=TMenuItem.Create(MainMenu1);
     with New1 do
     begin
       Caption := ahmrcdlgnew;
       File1.Add(New1);
       OnClick := New1Click;
     end;
     Open1:=TMenuItem.Create(MainMenu1);
     with Open1 do
     begin
       Caption := ahmrcdlgopen;
       File1.Add(Open1);
       OnClick := Open1Click;
     end;
     Save1:=TMenuItem.Create(MainMenu1);
     with Save1 do
     begin
       Caption := ahmrcdlgsave;
       File1.Add(Save1);
       OnClick := Save1Click;
     end;
     Div1:=TMenuItem.Create(MainMenu1);
     with Div1 do
     begin
       Caption := '-';
       File1.Add(Div1);
     end;
     Print1:=TMenuItem.Create(MainMenu1);
     with Print1 do
     begin
       Caption := ahmrcdlgprint;
       File1.Add(Print1);
       OnClick := Print1Click;
     end;
     Div2:=TMenuItem.Create(MainMenu1);
     with Div2 do
     begin
       Caption := '-';
       File1.Add(Div2);
     end;
     Exit1:=TMenuItem.Create(MainMenu1);
     with Exit1 do
     begin
       Caption := ahmrcdlgexit;
       File1.Add(Exit1);
       OnClick := Exit1Click;
     end;
     Edit1:=TMenuItem.Create(MainMenu1);
     with Edit1 do
     begin
       Caption := ahmrcdlgedit;
       MainMenu1.Items.Add(Edit1);
       GroupIndex := 2;
     end;
     WordWrap1:=TMenuItem.Create(MainMenu1);
     with WordWrap1 do
     begin
       Caption := ahmrcdlgwordwrap;
       Edit1.Add(WordWrap1);
       OnClick := WordWrap1Click;
     end;
     Div3:=TMenuItem.Create(MainMenu1);
     with Div3 do
     begin
       Caption := '-';
       Edit1.Add(Div3);
     end;
     Font1:=TMenuItem.Create(MainMenu1);
     with Font1 do
     begin
       Caption := ahmrcdlgfont;
       Edit1.Add(Font1);
       OnClick := Font1Click;
     end;
     Background1:=TMenuItem.Create(MainMenu1);
     with Background1 do
     begin
       Caption := ahmrcdlgbackground;
       Edit1.Add(Background1);
       OnClick := Background1Click;
     end;
    Self.Menu := MainMenu1;
  end;
  PrintDialog1:= TPrintDialog.Create(Self);
  with PrintDialog1 do  Options := [poPageNums];
  OpenDialog1:= TOpenDialog.Create(Self);
  with OpenDialog1 do
  begin
    Filter := ahmrcdlgallfilter;
    Title := ahmrcdlgopenfile;
  end;
  SaveDialog1:= TSaveDialog.Create(self);
  with Savedialog1 do
  begin
    Filter := ahmrcdlgallfilter;
    Options := [ofPathMustExist];
    Title := ahmrcdlgsaveas;
  end;
  FontDialog1:= TFontDialog.Create(Self);
  with FontDialog1 do
  begin
    Device := fdBoth;
    MinFontSize := 0;
    MaxFontSize := 0;
  end;
  ColorDialog1:= TColorDialog.Create(Self);
  with ColorDialog1 do Options := [cdSolidColor]
end;

Constructor TAHMTextFileView.Create( AOwner: TComponent );
begin
  inherited Create(AOwner);
  fmi:=TMainMenu.create(Application);
  ffon:=TFont.create;
  fico:=TIcon.create;
  N1:=true; N2:=true; N3:=true; N4:=true; N5:=true;
  N6:=true; N7:=true; N8:=true; N9:=true;
  N10:=true; N11:=true; FOpenFileOnExecute:=false;
  FWindowHeight:=240; FWindowWidth:=400;
  FEditColor:=clwhite;
  FWindowState:=wsNormal;
  FWindowPosition:=poScreenCenter;
  FBorderIcons:=[];
  FReadOnly:=False;
end;

Destructor TAHMTextFileView.Destroy;
begin
  fico.free;
  ffon.free;
  inherited Destroy;
end;

Function TAHMTextFileView.execute : Boolean;
var AHMTextFileViewForm : TAHMTextViewForm;
begin
  AHMTextFileViewForm:=TAHMTextViewForm.CreateNew(Application);
  with AHMTextFileViewForm do
  try
    SetupForm;
    File1.visible:=N1;
    Edit1.visible:=N2;
    Div1.visible:=N11;
    Div2.visible:=N11;
    Div3.visible:=N11;
    Background1.visible:=N10;
    Font1.visible:=N9;
    WordWrap1.visible:=N8;
    Exit1.visible:=N7;
    Print1.visible:=N6;
    Save1.visible:=N5;
    Open1.visible:=N4;
    New1.visible:=N3;
    icon.assign(fico);
    richedit1.readonly:=FReadOnly;
    refresh;
    caption:=fedcap;
    richedit1.font.assign(ffon);
    richedit1.color:=FEditColor;
    richedit1.clear;
    mainmenu1.merge(fmi);
    if assigned(fonexec) then fonexec(self);
    aexit:=fonexit;
    width:=FWindowWidth;
    height:=FWindowHeight;
    asave:=fonsave;
    aopen:=fonopen;
    Bordericons:=(FBorderIcons);
    Windowstate:=FWindowState;
    Position:=FWindowPosition;
    if FOpenFileOnExecute=true then
    if ffil<>'' then
    begin
      if not(fileexists(ffil)) then showmessage(ahmrcdlgfiledoesnotexists)
      else richedit1.lines.loadfromfile(ffil);
    end
    else showmessage(ahmrcdlgfilenotopen);
    showmodal;
    result := true;
  finally
    free
  end;
end;

Procedure TAHMTextFileView.SetIcon(value : TIcon);
begin
  fico.assign(value);
end;

Procedure TAHMTextFileView.SetBordericons(value : TBordericons);
begin
  FBorderIcons:=(value);
end;

Procedure TAHMTextFileView.SetMenuFile(value:Boolean);
begin
  N1:=value;
end;

Procedure TAHMTextFileView.SetMenuOptions(value:Boolean);
begin
  N2:=value;
end;

Procedure TAHMTextFileView.SetMenuDividers(value:Boolean);
begin
  N11:=value;
end;

Procedure TAHMTextFileView.SetMenuNew(value:Boolean);
begin
  N3:=value;
end;

Procedure TAHMTextFileView.SetMenuOpen(value:Boolean);
begin
  N4:=value;
end;

Procedure TAHMTextFileView.SetMenuSave(value:Boolean);
begin
  N5:=value;
end;

Procedure TAHMTextFileView.SetMenuPrint(value:Boolean);
begin
  N6:=value;
end;

Procedure TAHMTextFileView.SetMenuExit(value:Boolean);
begin
  N7:=value;
end;

Procedure TAHMTextFileView.SetMenuWordWrap(value:Boolean);
begin
  N8:=value;
end;

Procedure TAHMTextFileView.SetMenuFont(value:Boolean);
begin
  N9:=value;
end;

Procedure TAHMTextFileView.SetMenuBackground(value:Boolean);
begin
  N10:=value;
end;

Procedure TAHMTextFileView.SetFont(value : Tfont);
begin
  ffon.assign(value);
end;

Procedure TAHMTextFileView.SetMenu(value : TMainMenu);
begin
  fmi:=value;
end;

procedure TAHMTextViewForm.Print1Click(Sender: TObject);
var
  Line: Integer;
  PrintText: TextFile;
begin
  if PrintDialog1.Execute then
  begin
    AssignPrn(PrintText);
    Rewrite(PrintText);
    Printer.Canvas.Font := Richedit1.Font;
    for Line := 0 to Richedit1.Lines.Count - 1 do Writeln(PrintText, Richedit1.Lines[Line]);
    CloseFile(PrintText);
  end;
end;

procedure TAHMTextViewForm.New1Click(Sender: TObject);
begin
  richedit1.lines.clear;
  caption:='';
end;

procedure TAHMTextViewForm.Open1Click(Sender: TObject);
begin
  if opendialog1.execute then
  begin
    richedit1.lines.clear;
    richedit1.lines.loadfromfile(opendialog1.filename);
    caption:=opendialog1.filename;
    if assigned(aopen) then aopen(self);
  end;
  caption:=uppercase(caption);
end;

procedure TAHMTextViewForm.Save1Click(Sender: TObject);
begin
  savedialog1.filename:=caption;
  if savedialog1.execute then
  begin
    richedit1.lines.savetofile(savedialog1.filename);
    caption:=savedialog1.filename;
    If assigned(asave) then asave(self);
  end;
  caption:=uppercase(caption);
end;

procedure TAHMTextViewForm.Font1Click(Sender: TObject);
begin
  if fontdialog1.execute then richedit1.font:=fontdialog1.font;
end;

procedure TAHMTextViewForm.WordWrap1Click(Sender: TObject);
begin
  if wordwrap1.checked=false then
  begin
    Wordwrap1.checked:=true;
    richedit1.wordwrap:=true;
  end
  else begin
    Wordwrap1.checked:=false;
    richedit1.wordwrap:=false;
  end;
end;

procedure TAHMTextViewForm.Exit1Click(Sender: TObject);
begin
  if assigned(aexit) then aexit(self);
  close;
end;

procedure TAHMTextViewForm.Background1Click(Sender: TObject);
begin
  If Colordialog1.execute then richedit1.color:=colordialog1.color;
end;

end.
