unit PasEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, ComCtrls, ToolWin,clipbrd, FileCtrl,
   Buttons, ImgList,RichEdit2;

type
  TCommandEvent = procedure ( comId: integer; values: string; valueI: integer ) of object;

  TPasMemo = class(TRichEdit98)
  protected
    procedure WMChar(var Message: TWMChar); message WM_CHAR;
  public
  end;

  TMacroForm = class(TForm)
    StatusBar1: TStatusBar;
    ScrollBox1: TScrollBox;
    Panel1: TPanel;
    Panel2: TPanel;
    Bevel1: TBevel;
    ImageList1: TImageList;
    FileListBox1: TFileListBox;
    OpenDialog1: TOpenDialog;
    FontDialog1: TFontDialog;
    Panel3: TPanel;
    Bevel2: TBevel;
    Bevel3: TBevel;
    ComboBox1: TComboBox;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    procedure ComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Macroname : string;
    MDirectory: string;
    Memo: TPasMemo;
    Procedure UpdateCombo;
    Procedure InitCombo;

  end;

var
  MacroForm: TMacroForm;
  OnCommand : TCommandEvent;
  

implementation

{$R *.DFM}

{$IFDEF DELPHI4}
 // uses ImgList;
{$ENDIF}
{$IFDEF DELPHI5}
 // uses ImgList;
{$ENDIF}


Procedure TMacroForm.UpdateCombo;
var a: integer;
begin
  Combobox1.items.Clear;
  if (MDirectory <> '') then begin
    FilelistBox1.Directory := MDirectory;
    FilelistBox1.Update;
    for a:= 0 to FilelistBox1.items.Count - 1 do
    begin
      Combobox1.items.add
        (copy(FilelistBox1.items[a],1,length(FilelistBox1.items[a])-4));
    end;
  end;
end;

Procedure TMacroForm.InitCombo;
begin
  Memo.lines.Clear;
  if Combobox1.items.count > 0 then
  begin
    combobox1.itemindex := 0;
    Memo.lines.loadfromfile(mdirectory+combobox1.text+'.cmf');
    macroname := combobox1.text;
  end;
end;


procedure TMacroForm.ComboBox1Change(Sender: TObject);
begin
  Memo.lines.loadfromfile(mdirectory+combobox1.text+'.cmf');
  macroname := combobox1.text;
end;

procedure TMacroForm.FormCreate(Sender: TObject);
begin
  memo := TPasMemo.Create(self);
  memo.visible := false;
  memo.Parent  := scrollbox1;
  memo.Ctl3D := false;
  memo.BorderStyle := Forms.bsNone;
  memo.Align := alCLient;
  memo.visible := true;
  memo.Font.name := 'Tahoma';
  memo.font.size := 8;
  memo.Wantreturns := True;
  memo.WantTabs := True;
  memo.WordWrap := False;
  memo.AutoURLDetect := adDefault;


  macroname := '';
  mdirectory := '';
  width := 400;
  height := 330;
  ImageList1.GetBitmap(0,SpeedButton1.Glyph);
  ImageList1.GetBitmap(1,SpeedButton2.Glyph);
  ImageList1.GetBitmap(2,SpeedButton3.Glyph);
  ImageList1.GetBitmap(3,SpeedButton4.Glyph);
  ImageList1.GetBitmap(4,SpeedButton5.Glyph);
  ImageList1.GetBitmap(5,SpeedButton6.Glyph);

end;


procedure TMacroForm.FormShow(Sender: TObject);
begin
  setforegroundwindow(handle);
end;

procedure TMacroForm.SpeedButton1Click(Sender: TObject);
var Istring : string;
begin
  IString:= InputBox('New Macro', 'Enter a name for the new Macro', 'Macro1');
  if IString = '' then exit;
  MacroName := IString;
  memo.lines.clear;
  memo.Lines.savetofile(mdirectory+macroname+'.cmf');
  updatecombo;
  Combobox1.Itemindex := Combobox1.items.indexof(macroname);
end;

procedure TMacroForm.SpeedButton2Click(Sender: TObject);
begin
  If opendialog1.execute then
  begin
    memo.lines.loadfromfile(opendialog1.filename);
  end;
end;

procedure TMacroForm.SpeedButton3Click(Sender: TObject);
begin
  if Macroname = '' then exit;
  memo.Lines.savetofile(mdirectory+macroname+'.cmf');
end;

procedure TMacroForm.SpeedButton4Click(Sender: TObject);
begin
  If macroname = '' then exit;
  If messagedlg(
  'The macro '+Macroname+' will be deleted',mtConfirmation,[mbOk,mbCancel],0) = mrOk
  then
  begin
    memo.lines.clear;
    deletefile(mdirectory+macroname+'.cmf');
    updatecombo;
    initcombo;
  end;

end;

procedure TMacroForm.SpeedButton5Click(Sender: TObject);
begin
   if assigned(OnCommand) then OnCommand(0,memo.Lines.Text,0);
  //CompObj.script := memo.Lines;
  //CompObj.Compile('Self',CompObj);
  //CompObj.Run;

end;

procedure TMacroForm.SpeedButton6Click(Sender: TObject);
begin
  if fontdialog1.execute then
    memo.font := fontdialog1.font;
end;


{ TPasMemo }
procedure TPasMemo.WMChar(var Message: TWMChar);
var tab: String;
cPos: TPoint;
cLine,xLine: String;
l,i:Integer;
Key: Char;
begin
  Key := Char(Message.CharCode);
  if (Key = Char(VK_RETURN)) then begin
    inherited;
    cPOs := CaretPos;
    xLine := Lines[cPos.y-1];
    cLine := TrimLeft(xLine);
    l := Length(xLine)-Length(cLine);
    tab := '';
    for i := 1 to l do tab := tab+' ';
    SetSelTextBuf(pchar(tab))
  end else if (Key = Char(VK_TAB)) then begin
    tab := '  ';
    SetSelTextBuf(pchar(tab));
  end else begin
    inherited;
  end;
end;

end.
