unit FormCategorySelect;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Acronym, FormEx;

type

  { TFormCategorySelect }

  TFormCategorySelect = class(TFormEx)
    ButtonOk: TButton;
    ButtonCancel: TButton;
    ListBox1: TListBox;
    procedure ListBox1DblClick(Sender: TObject);
    procedure ListBox1KeyPress(Sender: TObject; var Key: Char);
  public
    AcronymDb: TAcronymDb;
    procedure Load(RemoveItems: TStrings);
  end;

resourcestring
  SCategory = 'Category';
  SRemoveCategory = 'Remove categories';
  SRemoveCategoryQuery = 'Do you really want to remove selected categories?';


implementation

{$R *.lfm}

{ TFormCategorySelect }

procedure TFormCategorySelect.ListBox1DblClick(Sender: TObject);
begin
  ButtonOk.Click;
end;

procedure TFormCategorySelect.ListBox1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then ButtonOk.Click;
end;

procedure TFormCategorySelect.Load(RemoveItems: TStrings);
var
  Index: Integer;
  I: Integer;
begin
  with AcronymDb do begin
    ListBox1.Sorted := False;
    while ListBox1.Items.Count < Categories.Count do
      ListBox1.Items.Add('');
    while ListBox1.Items.Count > Categories.Count do
      ListBox1.Items.Delete(ListBox1.Items.Count - 1);
    for I := 0 to Categories.Count - 1 do begin
      ListBox1.Items.Strings[I] := Categories[I].Name;
      ListBox1.Items.Objects[I] := Categories[I];
      ListBox1.Selected[I] := False;
    end;
    ListBox1.Sorted := True;
    for I := 0 to RemoveItems.Count - 1 do begin
      Index := ListBox1.Items.IndexOf(RemoveItems[I]);
      if Index <> -1 then ListBox1.Items.Delete(Index);
    end;
  end;
end;

end.

