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

unit ecDictDlg;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ecSpell, ComCtrls, ToolWin, ActnList, ImgList;

type
  TDictionariesDlg = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    LV: TListView;
    ActionList1: TActionList;
    ImageList2: TImageList;
    actAddDict: TAction;
    actSaveDict: TAction;
    ToolButton4: TToolButton;
    actDelDict: TAction;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    actExit: TAction;
    OD: TOpenDialog;
    SD: TSaveDialog;
    procedure actExitExecute(Sender: TObject);
    procedure actDelDictExecute(Sender: TObject);
    procedure actDelDictUpdate(Sender: TObject);
    procedure actAddDictExecute(Sender: TObject);
    procedure LVEdited(Sender: TObject; Item: TListItem; var S: String);
    procedure LVChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure actSaveDictExecute(Sender: TObject);
    procedure actSaveDictUpdate(Sender: TObject);
  private
    FDict: TecDictionary;
    procedure FillList;
    procedure FillItem(LI: TListItem; Dict: TecDictionaryWords);
    procedure SetDict(const Value: TecDictionary);
  protected
    procedure ReadState(Reader: TReader); override;
  public
    property Dictionary: TecDictionary read FDict write SetDict;
  end;

implementation

{$R *.dfm}

{ TDictionariesDlg }

procedure TDictionariesDlg.FillItem(LI: TListItem;
  Dict: TecDictionaryWords);
begin
  li.Caption := Dict.Title;
  li.Checked := Dict.Active;
  li.SubItems.Clear;
  li.SubItems.Add(Dict.FileName);
  li.SubItems.Add(IntToStr(Dict.Words.Count));
  li.Data := Dict;
end;

procedure TDictionariesDlg.FillList;
var i: integer;
begin
  if FDict <> nil then
   begin
     LV.Items.BeginUpdate;
     try
       LV.Items.Clear;
       if FDict <> nil then
       for i := 0 to FDict.Count - 1 do
         FillItem(LV.Items.Add, FDict[i]);
     finally
       LV.Items.EndUpdate;
     end;
   end;
end;

procedure TDictionariesDlg.SetDict(const Value: TecDictionary);
begin
  if FDict <> Value then
   begin
    FDict := Value;
    FillList;
   end;
end;

procedure TDictionariesDlg.actExitExecute(Sender: TObject);
begin
  Close;
end;

procedure TDictionariesDlg.actDelDictExecute(Sender: TObject);
begin
  TObject(LV.Selected.Data).Free;
  LV.Selected.Data := nil;
  LV.Selected.Delete;
end;

procedure TDictionariesDlg.actDelDictUpdate(Sender: TObject);
begin
  actDelDict.Enabled := (FDict <> nil) and (LV.Selected <> nil) and
                 FDict.CanDelete(TecDictionaryWords(LV.Selected.Data));
end;

procedure TDictionariesDlg.actAddDictExecute(Sender: TObject);
var dict: TecDictionaryWords;
begin
  if (FDict <> nil) and OD.Execute then
   begin
    dict := FDict.Add('New dictionary', OD.FileName);
    if not dict.SortedSource then
     if MessageBox(Handle, 'Save in sorted format to optimize dictionary loading?',
       'Confirmation', MB_YESNO) = IDYes then
      begin
        SD.FileName := ExtractFileName(dict.FileName);
        if SD.Execute then
         dict.Save(SD.FileName);
      end;
    FillItem(LV.Items.Add, dict);
   end;
end;

procedure TDictionariesDlg.LVEdited(Sender: TObject; Item: TListItem;
  var S: String);
begin
  TecDictionaryWords(Item.Data).Title := S;
end;

procedure TDictionariesDlg.LVChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  if (Change = ctState) and Assigned(Item.Data) then
   begin
     TecDictionaryWords(Item.Data).Active := Item.Checked;
     FillItem(Item, TecDictionaryWords(Item.Data));
   end;
end;

procedure TDictionariesDlg.actSaveDictExecute(Sender: TObject);
begin
  if SD.Execute then
    TecDictionaryWords(LV.Selected.Data).Save(SD.FileName);
end;

procedure TDictionariesDlg.actSaveDictUpdate(Sender: TObject);
begin
  actSaveDict.Enabled := LV.Selected <> nil;
end;

procedure TDictionariesDlg.ReadState(Reader: TReader);
begin
  inherited;
  BorderStyle := bsSizeable;
end;

end.
