unit uMenuScriptEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Menus, AdvMemo, Buttons, ExtCtrls, Advmws;

type
  TTMSMenuScriptEdit = class(TForm)
    Panel1: TPanel;
    spbCut: TSpeedButton;
    spbPaste: TSpeedButton;
    spbSave: TSpeedButton;
    spbOpen: TSpeedButton;
    spbCopy: TSpeedButton;
    spbUndo: TSpeedButton;
    spbRedo: TSpeedButton;
    spbNew: TSpeedButton;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Panel2: TPanel;
    TreeView1: TTreeView;
    AdvMemo1: TAdvMemo;
    Splitter1: TSplitter;
    AdvJSMemoStyler1: TAdvJSMemoStyler;
    AdvJSMemoStyler2: TAdvJSMemoStyler;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure spbNewClick(Sender: TObject);
    procedure spbOpenClick(Sender: TObject);
    procedure spbSaveClick(Sender: TObject);
    procedure spbCutClick(Sender: TObject);
    procedure spbCopyClick(Sender: TObject);
    procedure spbPasteClick(Sender: TObject);
    procedure spbUndoClick(Sender: TObject);
    procedure spbRedoClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
  private
    { Private declarations }
  public
    { Public declarations }
    MenuNames: TStringList;
    MenuScripts: TStringList;
    EditMenu:string;
    procedure AddToTree(tn: TTreeNode; MenuItem: TMenuItem);
    procedure Select;
  end;

var
  TMSMenuScriptEdit: TTMSMenuScriptEdit;

implementation

{$R *.dfm}

procedure TTMSMenuScriptEdit.AddToTree(tn: TTreeNode; MenuItem: TMenuItem);
var
  i: Integer;
  ntn: TTreeNode;
  s,res: string;
begin
  s := MenuItem.Caption;

  res := '';
  for i := 1 to length(s) do
  begin
    if (s[i] = '&') then
    begin
      if (i < length(s)) and (s[i + 1] = '&') then
        res := res + s[i];
    end
    else
      res := res + s[i];
  end;

  if Assigned(tn) then
  begin
    ntn := TreeView1.Items.AddChild(tn,res+' ('+MenuItem.Name+')')
  end
  else
  begin
    ntn := TreeView1.Items.Add(nil,res+' ('+MenuItem.Name+')');
  end;

  for i := 1 to MenuItem.Count do
  begin
    AddToTree(ntn,MenuItem.Items[i - 1]);
  end;
end;

procedure TTMSMenuScriptEdit.FormCreate(Sender: TObject);
begin
  EditMenu := '';

  MenuNames := TStringList.Create;
  MenuScripts := TStringList.Create;
end;

procedure TTMSMenuScriptEdit.TreeView1Click(Sender: TObject);
begin
  Select;
end;

procedure TTMSMenuScriptEdit.Select;
var
  s: string;
  i: Integer;
begin
  if EditMenu <> '' then
  begin
    i := MenuNames.IndexOf(EditMenu);
    if (i <> -1) then
      MenuScripts[i] := AdvMemo1.Lines.Text
    else
    begin
      MenuNames.Add(EditMenu);
      MenuScripts.Add(AdvMemo1.Lines.Text);
    end;
  end;

  if Assigned(TreeView1.Selected) then
  begin
    s := TreeView1.Selected.Text;
    if pos('(', s) > 0 then
    begin
      delete(s,1,pos('(',s));
      delete(s,length(s),1);
      EditMenu := s;
      i := MenuNames.IndexOf(EditMenu);
      if i <> -1 then
      begin
        AdvMemo1.Lines.Text := MenuScripts[i];
        AdvMemo1.CurX := 0;
        AdvMemo1.CurY := 0;
        AdvMemo1.Refresh;
      end
      else
      begin
        AdvMemo1.Clear;
        AdvMemo1.Refresh;
      end;
    end;
  end;
end;

procedure TTMSMenuScriptEdit.spbNewClick(Sender: TObject);
begin
  AdvMemo1.Clear;
end;

procedure TTMSMenuScriptEdit.spbOpenClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    AdvMemo1.Lines.LoadFromFile(OpenDialog1.FileName);
  end;
end;

procedure TTMSMenuScriptEdit.spbSaveClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
  begin
    AdvMemo1.Lines.SaveToFile(SaveDialog1.FileName);
  end;

end;

procedure TTMSMenuScriptEdit.spbCutClick(Sender: TObject);
begin
  AdvMemo1.CutToClipBoard;
end;

procedure TTMSMenuScriptEdit.spbCopyClick(Sender: TObject);
begin
  AdvMemo1.CopyToClipBoard;
end;

procedure TTMSMenuScriptEdit.spbPasteClick(Sender: TObject);
begin
  AdvMemo1.PasteFromClipBoard;
end;

procedure TTMSMenuScriptEdit.spbUndoClick(Sender: TObject);
begin
  AdvMemo1.Undo;
end;

procedure TTMSMenuScriptEdit.spbRedoClick(Sender: TObject);
begin
  AdvMemo1.Redo;
end;

procedure TTMSMenuScriptEdit.FormDestroy(Sender: TObject);
begin
  MenuNames.Free;
  MenuScripts.Free;
end;

procedure TTMSMenuScriptEdit.SpeedButton1Click(Sender: TObject);
begin
  Select;
  ModalResult := mrOk;
end;

procedure TTMSMenuScriptEdit.SpeedButton2Click(Sender: TObject);
begin
  ModalResult := mrCancel;
end;


procedure TTMSMenuScriptEdit.TreeView1Change(Sender: TObject;
  Node: TTreeNode);
begin
  Select;
end;

end.
