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

unit ecMultReplSelectDlg;

interface

uses
  Windows, Forms, Controls, StdCtrls, Classes, ecMultRepl, ecSyntMemo;

type
  TMultiReplaceSelectDlg = class(TForm)
    ExecuteButton: TButton;
    Button2: TButton;
    CategorieComboBox: TComboBox;
    MultiReplaceComboBox: TComboBox;
    CategorieLabel: TLabel;
    MultiReplaceLabel: TLabel;
    HintMemo: TMemo;
    procedure CategorieComboBoxSelect(Sender: TObject);
    procedure ExecuteButtonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure MultiReplaceComboBoxSelect(Sender: TObject);
  private
    FMultiReplace: TecMultiReplace;
    FSyntaxMemo: TCustomSyntaxMemo;
    function GetCurrentReplaceGroup: TecReplaceGroup;
    procedure SetMultiReplace(const Value: TecMultiReplace); virtual;
    procedure SetSyntaxMemo(const Value: TCustomSyntaxMemo);
  protected
    procedure FillCategories;
    procedure FillMultiReplace;
  public
    property CurrentReplaceGroup: TecReplaceGroup read GetCurrentReplaceGroup;
  published
    property MultiReplace: TecMultiReplace read FMultiReplace write
      SetMultiReplace;
    property SyntaxMemo: TCustomSyntaxMemo read FSyntaxMemo write SetSyntaxMemo;
  end;

implementation

{$R *.dfm}

procedure TMultiReplaceSelectDlg.CategorieComboBoxSelect(Sender: TObject);
begin
  FillMultiReplace;
end;

function TMultiReplaceSelectDlg.GetCurrentReplaceGroup: TecReplaceGroup;
begin
  if (MultiReplaceComboBox.Items.Count > 0) then
    if (MultiReplaceComboBox.ItemIndex >= 0) and (MultiReplaceComboBox.ItemIndex
      < MultiReplaceComboBox.Items.Count) then
      Result:=
        TecReplaceGroup(MultiReplaceComboBox.Items.Objects[MultiReplaceComboBox.ItemIndex])
    else
      Result:= TecReplaceGroup(MultiReplaceComboBox.Items.Objects[0])
  else
    Result := nil;
end;

procedure TMultiReplaceSelectDlg.ExecuteButtonClick(Sender: TObject);
var
  ReplaceGroup: TecReplaceGroup;
begin
  ReplaceGroup := CurrentReplaceGroup;
  if Assigned(ReplaceGroup) and ReplaceGroup.IsEnabled(SyntaxMemo) then
    ReplaceGroup.Execute(SyntaxMemo);
end;

procedure TMultiReplaceSelectDlg.FillCategories;
var
  i: Integer;
begin
  CategorieComboBox.Items.Clear;
  if Assigned(MultiReplace) and Assigned(SyntaxMemo) then
  begin
    for i := 0 to MultiReplace.Items.Count - 1 do
      if MultiReplace.Items[i].IsEnabled(SyntaxMemo) then
        if CategorieComboBox.Items.IndexOf(MultiReplace.Items[i].Category) < 0
          then
          CategorieComboBox.Items.Add(MultiReplace.Items[i].Category);
    CategorieComboBox.ItemIndex := 0;
  end;
  CategorieComboBox.Enabled := CategorieComboBox.Items.Count > 0;
  FillMultiReplace;
end;

procedure TMultiReplaceSelectDlg.FillMultiReplace;
var
  Categorie: string;
  i: Integer;
begin
  MultiReplaceComboBox.Items.Clear;
  if (CategorieComboBox.Items.Count > 0) then
    if (CategorieComboBox.ItemIndex >= 0) and (CategorieComboBox.ItemIndex <
      CategorieComboBox.Items.Count) then
      Categorie := CategorieComboBox.Items[CategorieComboBox.ItemIndex]
    else
      Categorie := CategorieComboBox.Items[0]
  else
    Categorie := '';

  if Assigned(MultiReplace) and (Categorie <> '') then
  begin
    for i := 0 to MultiReplace.Items.Count - 1 do
      if MultiReplace.Items[i].IsEnabled(SyntaxMemo) then
        if Categorie = MultiReplace.Items[i].Category then
          MultiReplaceComboBox.Items.AddObject(MultiReplace.Items[i].DisplayName, MultiReplace.Items[i]);
    MultiReplaceComboBox.ItemIndex := 0;
  end;
  ExecuteButton.Enabled := MultiReplaceComboBox.Items.Count > 0;
  MultiReplaceComboBoxSelect(nil);
end;

procedure TMultiReplaceSelectDlg.FormShow(Sender: TObject);
begin
  FillCategories;
  if CategorieComboBox.Items.Count <= 1 then
    MultiReplaceComboBox.SetFocus;
end;

procedure TMultiReplaceSelectDlg.MultiReplaceComboBoxSelect(Sender: TObject);
begin
  if Assigned(CurrentReplaceGroup) then
    HintMemo.Text := CurrentReplaceGroup.Notes.Text
  else
    HintMemo.Text := '';
end;

procedure TMultiReplaceSelectDlg.SetMultiReplace(const Value: TecMultiReplace);
begin
  FMultiReplace := Value;
end;

procedure TMultiReplaceSelectDlg.SetSyntaxMemo(const Value: TCustomSyntaxMemo);
begin
  FSyntaxMemo := Value;
end;

end.

