{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
{$I ecSyntEdit.INC}

unit ecMacroCmdEditDlg;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ecStrUtils;

type
  TMacroCmdEditDlg = class(TForm)
    Label1: TLabel;
    List: TListView;
    Label2: TLabel;
    DataEdit: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    procedure FillCmd;
  public
    { Public declarations }
  end;

function EditMacroCommand(var Cmd: integer; var Data: ecString): Boolean;

implementation

{$R *.dfm}

uses ecKeyMap, ecCmdConst;

function EditMacroCommand(var Cmd: integer; var Data: ecString): Boolean;
var i: integer;
begin
  with TMacroCmdEditDlg.Create(nil) do
    try
      DataEdit.Text := Data;
      for i := 0 to List.Items.Count - 1 do
        if integer(List.Items[i].Data) = cmd then
          begin
            List.Items[i].Selected := True;
            List.Items[i].MakeVisible(False);
            Break;
          end;
      Result := ShowModal = mrOK;
      if Result then
        begin
          Cmd := integer(List.Selected.Data);
          Data := DataEdit.Text;
        end;
    finally
      Free;
    end;
end;

procedure TMacroCmdEditDlg.FillCmd;
var i: integer;
begin
  List.Items.BeginUpdate;
  try
    with DefaultKeyMapping do
    for i := 0 to Items.Count - 1 do
      with List.Items.Add do
        begin
          Caption := IntToStr(Items[i].Command);
          SubItems.Text := Items[i].DisplayName;
          Data := TObject(Items[i].Command);
        end;
  finally
    List.Items.EndUpdate;
  end;
end;

procedure TMacroCmdEditDlg.FormCreate(Sender: TObject);
begin
  FillCmd;
end;

procedure TMacroCmdEditDlg.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
var S: string;
    k: integer;
begin
  if ModalResult = mrOk then
    begin
      if List.Selected = nil then
        raise Exception.Create('Command is not selected');
      case integer(List.Selected.Data) of
        smDropMarker,
        smGotoXY:
          try
            S := DataEdit.Text;
            if (S = '') and (integer(List.Selected.Data) = smDropMarker) then
              Exit;
            k := Pos(';', S);
            if k = 0 then Abort;
            StrToInt(Copy(S, 1, k - 1));
            StrToInt(Copy(S, k + 1,  Length(S) - k));
          except
            raise Exception.Create('Invalid data format. Must be:'#13#10'0;0');
          end;
        smChar:
          if Length(DataEdit.Text) <> 1 then
            raise Exception.Create('Data must be of one character');
        smString,
        smMacroPlay: ;
        else
          if DataEdit.Text <> '' then
            raise Exception.Create('This command does not support data');
      end;
    end;
end;

end.
