{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
unit ecsnCodeTempl;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ecSyntAnal, ecSyntMemo, ExtCtrls, StdCtrls, ComCtrls;

type
  TSyntCodeTemplFrame = class(TFrame)
    Panel_Template: TPanel;
    Label31: TLabel;
    LV: TListView;
    Panel1: TPanel;
    Button4: TButton;
    Button6: TButton;
    Button5: TButton;
    Splitter6: TSplitter;
    Label32: TLabel;
    SyntaxMemo2: TSyntaxMemo;
    procedure LVChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure SyntaxMemo2Exit(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    FTemplates: TCodeTemplates;
    procedure SetTemplates(const Value: TCodeTemplates);
    procedure FillCodeTempl;
    procedure CheckTemplName(const s: string; ct: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    property Templates: TCodeTemplates read FTemplates write SetTemplates;
  end;

implementation

uses ecCodeTemplDlg;

{$R *.dfm}

{ TSyntCodeTemplFrame }

constructor TSyntCodeTemplFrame.Create(AOwner: TComponent);
begin
  inherited;
  Label31.Transparent := False;
  Label32.Transparent := False;
end;

procedure TSyntCodeTemplFrame.SetTemplates(const Value: TCodeTemplates);
begin
  FTemplates := Value;
  FillCodeTempl;
end;

procedure TSyntCodeTemplFrame.FillCodeTempl;
var i: integer;
    li: TListItem;
begin
  LV.Items.Clear;
  if FTemplates <> nil then
  for i := 0 to FTemplates.Count - 1 do
   begin
     li := LV.Items.Add;
     li.Caption := FTemplates[i].Name;
     li.SubItems.Add(FTemplates[i].Description);
     li.Checked := FTemplates[i].Advanced;
     li.Data := FTemplates[i];
   end;
end;

procedure TSyntCodeTemplFrame.CheckTemplName(const s: string; ct: TObject);
var i: integer;
begin
  if s = '' then
    raise Exception.Create('Shortcut name can not be empty');
  for i := 1 to Length(S) do
   if S[i] <= #32 then
    raise Exception.Create('Shortcut name can not contain Space charecters');
  if FTemplates <> nil then
  for i := 0 to FTemplates.Count - 1 do
   if FTemplates[i] <> ct then
    if SameText(FTemplates[i].Name, s) then
     raise Exception.CreateFmt('Shortcut name "%s" is already used', [s]);
end;

procedure TSyntCodeTemplFrame.LVChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  Button5.Enabled := LV.Selected <> nil;
  Button6.Enabled := LV.Selected <> nil;
  if LV.Selected <> nil then
    SyntaxMemo2.Lines.Text := TCodeTemplate(LV.Selected.Data).Code.Text
  else
    SyntaxMemo2.Lines.Clear;
  SyntaxMemo2.Modified := False;
  if (Change = ctState) and Assigned(Item.Data) then
   TCodeTemplate(Item.Data).Advanced := Item.Checked
end;

procedure TSyntCodeTemplFrame.SyntaxMemo2Exit(Sender: TObject);
begin
  if SyntaxMemo2.Modified and (LV.Selected <> nil) then
    TCodeTemplate(LV.Selected.Data).Code.Text := SyntaxMemo2.Lines.Text;
  SyntaxMemo2.Modified := False;
end;

procedure TSyntCodeTemplFrame.Button6Click(Sender: TObject);
begin
  if LV.Selected <> nil then
  if MessageBox(Handle, 'Do you want to Delete this Code Template?', 'Confirmation',
         MB_YESNO or MB_ICONWARNING) = IDYes then
    begin
      TCodeTemplate(LV.Selected.Data).Free;
      LV.Selected.Delete;
    end;
end;

procedure TSyntCodeTemplFrame.Button5Click(Sender: TObject);
var dlg: TAddTemplateDlg;
     ct: TCodeTemplate;
     li: TListItem;
begin
  li := LV.Selected;
  if li <> nil then ct := TCodeTemplate(LV.Selected.Data)
   else Exit;
  dlg := TAddTemplateDlg.Create(Self);
  dlg.Caption := 'Edit Code Template';
  try
   dlg.Edit1.Text := ct.Name;
   dlg.Edit2.Text := ct.Description;
   if dlg.ShowModal = mrOk then
    begin
      CheckTemplName(dlg.Edit1.Text, ct);
      ct.Name := dlg.Edit1.Text;
      ct.Description := dlg.Edit2.Text;
      li.Caption := ct.Name;
      li.SubItems.Clear;
      li.SubItems.Add(ct.Description);
    end;
  finally
   dlg.Free;
  end;
end;

procedure TSyntCodeTemplFrame.Button4Click(Sender: TObject);
var dlg: TAddTemplateDlg;
     ct: TCodeTemplate;
     li: TListItem;
begin
  if FTemplates = nil then Exit;
  dlg := TAddTemplateDlg.Create(Self);
  dlg.Caption := 'Add Code Template';
  try
   if dlg.ShowModal = mrOk then
    begin
      CheckTemplName(dlg.Edit1.Text, nil);
      ct := FTemplates.Add;
      ct.Name := dlg.Edit1.Text;
      ct.Description := dlg.Edit2.Text;
      li := LV.Items.Add;
      li.Caption := ct.Name;
      li.SubItems.Add(ct.Description);
      li.Data := ct;
      li.Selected := True;
    end;
  finally
   dlg.Free;
  end;
end;

end.

