{
     Apophysis Copyright (C) 2001-2004 Mark Townsend
     Apophysis Copyright (C) 2005-2006 Ronald Hordijk, Piotr Borys, Peter Sdobnov
     Apophysis Copyright (C) 2007-2008 Piotr Borys, Peter Sdobnov
     
     Apophysis "3D hack" Copyright (C) 2007-2008 Peter Sdobnov
     Apophysis "7X" Copyright (C) 2009-2010 Georg Kiehne
     Apophysis AV "Phoenix Edition" Copyright (C) 2021-2022 Alice V. Koryagina

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
unit FormFavorites;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls, Vcl.Buttons;

type
  TFavoritesForm = class(TForm)
    ScriptList: TListView;
    btnMoveUp: TSpeedButton;
    btnMoveDown: TSpeedButton;
    btnOK: TButton;
    btnCancel: TButton;
    btnAdd: TSpeedButton;
    btnRemove: TSpeedButton;
    btnSort: TSpeedButton;
    btnClear: TSpeedButton;
    procedure FormShow(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnRemoveClick(Sender: TObject);
    procedure ScriptListChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure btnMoveUpClick(Sender: TObject);
    procedure btnMoveDownClick(Sender: TObject);
    procedure ScriptListInfoTip(Sender: TObject; Item: TListItem;
      var InfoTip: string);
    procedure btnSortClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
  private
    { Private declarations }
  public
    Faves: TStringList;
    { Public declarations }
  end;

var
  FavoritesForm: TFavoritesForm;

implementation

uses Global, ScriptForm, Translation;

{$R *.DFM}

procedure TFavoritesForm.FormShow(Sender: TObject);
var
  ListItem: TListItem;
  i: integer;
  s: string;
begin
  Faves.Text := Favorites.Text;
  ScriptList.Items.Clear;
  for i := 0 to Favorites.Count - 1 do
  begin
    ListItem := ScriptList.Items.Add;
    s := ExtractFileName(Favorites[i]);
    s := Copy(s, 0, length(s) - Length(ExtractFileExt(s)));
    Listitem.Caption := s;
  end;
  if Favorites.Count <> 0 then
    ScriptList.Selected := ScriptList.Items[0];

  ScriptListChange(Sender, ScriptList.Selected, ctText); // AV
end;

procedure TFavoritesForm.btnOKClick(Sender: TObject);
begin
  ModalResult := mrOK;
  Faves.SaveToFile(AppPath + scriptFavsFilename);
end;

procedure TFavoritesForm.FormCreate(Sender: TObject);
begin
	btnOK.Caption := TextByKey('common-ok');
	btnCancel.Caption := TextByKey('common-cancel');
	self.Caption := TextByKey('favscripts-title');
	btnAdd.Caption := TextByKey('favscripts-add');
	btnRemove.Caption := TextByKey('favscripts-remove');
	btnMoveUp.Caption := TextByKey('favscripts-moveup');
	btnMoveDown.Caption := TextByKey('favscripts-movedown');
  btnSort.Caption := TextByKey('varorder-byname'); // AV
  btnClear.Caption := TextByKey('common-clear'); // AV

  Faves := TStringList.Create;
end;

procedure TFavoritesForm.FormDestroy(Sender: TObject);
begin
  Faves.Free;
end;

procedure TFavoritesForm.btnAddClick(Sender: TObject);
var
  ListItem: TListItem;
  i : integer;
  s: string;
begin
  s := AppPath + 'Scripts';
  if DirectoryExists(s) then 	
	  ScriptEditor.MainOpenDialog.InitialDir := s
  else
    ScriptEditor.MainOpenDialog.InitialDir := ParamFolder;
  ScriptEditor.MainOpenDialog.Filter := Format('%s|*.aposcript;*.asc|%s|*.*',
      [TextByKey('common-filter-scriptfiles'),
       TextByKey('common-filter-allfiles')]);
  if ScriptEditor.mainOpenDialog.Execute then
  begin
    for i := 0 to Faves.Count  - 1 do
    begin
      if ScriptEditor.MainOpenDialog.Filename = Faves[i] then exit;
    end;

    Faves.add(ScriptEditor.MainOpenDialog.Filename);
    ListItem := ScriptList.Items.Add;
    s := ExtractFileName(ScriptEditor.MainOpenDialog.Filename);
    s := Copy(s, 0, length(s) - Length(ExtractFileExt(s)));
    Listitem.Caption := s;
    ScriptList.Selected := ScriptList.Items[ScriptList.Items.Count - 1];
  end;

  ScriptListChange(Sender, ScriptList.Selected, ctText); // AV
end;

procedure TFavoritesForm.btnRemoveClick(Sender: TObject);
var
  i: integer;
begin
  if not assigned(ScriptList.Selected) then exit;  // AV
  i := ScriptList.Selected.Index;
  Faves.Delete(i);
  ScriptList.Items[i].delete;
  if ScriptList.Items.Count <> 0 then
    if i < ScriptList.Items.Count then
      ScriptList.Selected := ScriptList.Items[i]
    else
      ScriptList.Selected := ScriptList.Items[ScriptList.Items.Count - 1];

  ScriptListChange(Sender, ScriptList.Selected, ctText); // AV
end;

procedure TFavoritesForm.btnSortClick(Sender: TObject);
var
  scripts: TStringList;
  i : integer;
begin
  if (ScriptList.Items.Count <= 1) then exit;

  scripts := TStringList.Create;
  for i := 0 to Faves.Count - 1 do
    scripts.AddPair(ScriptList.Items[i].Caption, Faves[i]);
  ScriptList.Items.BeginUpdate;
  scripts.Sort;
  for i := 0 to Faves.Count  - 1 do
  begin
    ScriptList.Items[i].Caption := scripts.Names[i];
    Faves[i] := scripts.ValueFromIndex[i];
  end;
  ScriptList.Items.EndUpdate;
  scripts.Free;
  ScriptListChange(Sender, ScriptList.Selected, ctText); // AV
end;

procedure TFavoritesForm.ScriptListChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
var
  n: smallint;
  IsSel: boolean;
begin
  n := ScriptList.Items.Count;
  IsSel := assigned(ScriptList.Selected);

  btnRemove.Enabled := (n > 0) and IsSel;
  btnSort.Enabled := (n > 1);

  if (n <= 1) or (not IsSel) then
  begin
    btnMoveDown.Enabled := False;
    btnMoveUp.Enabled := False;
  end
  else begin
    if (Item.Index = n - 1) then
      btnMoveDown.Enabled := False
    else
      btnMoveDown.Enabled := True;
    if (Item.Index = 0) then
      btnMoveUp.Enabled := False
    else
      btnMoveUp.Enabled := True;
  end;
end;

procedure TFavoritesForm.ScriptListInfoTip(Sender: TObject; Item: TListItem;
  var InfoTip: string);
begin
  InfoTip := Faves[Item.Index];  // AV: show the corresponding full file name
end;

procedure TFavoritesForm.btnMoveUpClick(Sender: TObject);
var
  i: integer;
  s: string;
begin
  if not assigned(ScriptList.Selected) then exit;  // AV
  i := ScriptList.Selected.Index;
  s := Faves[i];
  Faves[i] := Faves[i - 1];
  Faves[i - 1] := s;
  s := ScriptList.Selected.Caption;
  ScriptList.Selected.Caption := ScriptList.Items[i - 1].Caption;
  ScriptList.Items[i - 1].Caption := s;
  ScriptList.Selected := ScriptList.Items[i - 1];
end;

procedure TFavoritesForm.btnClearClick(Sender: TObject);
begin
  ScriptList.Items.Clear;
  Faves.Clear;
  ScriptListChange(Sender, ScriptList.Selected, ctText); // AV
end;

procedure TFavoritesForm.btnMoveDownClick(Sender: TObject);
var
  i: integer;
  s: string;
begin
  if not assigned(ScriptList.Selected) then exit;  // AV
  i := ScriptList.Selected.Index;
  s := faves[i];
  Faves[i] := Faves[i + 1];
  Faves[i + 1] := s;
  s := ScriptList.Selected.Caption;
  ScriptList.Selected.Caption := ScriptList.Items[i + 1].Caption;
  ScriptList.Items[i + 1].Caption := s;
  ScriptList.Selected := ScriptList.Items[i + 1];
end;

end.

