{
     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.
}
//{$D-,L-,O+,Q-,R-,Y-,S-}

unit Options;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ExtCtrls, Buttons, Registry, CheckLst,
  MMSystem, FileCtrl, StrUtils, ShellAPI, ShlObj, Vcl.Samples.Spin;

type
  TOptionsForm = class(TForm)
    btnOK: TButton;
    btnCancel: TButton;
    OpenDialog: TOpenDialog;
    Tabs: TPageControl;
    GeneralPage: TTabSheet;
    btnLanguage: TSpeedButton;
    pnlJPEGQuality: TPanel;
    chkConfirmDel: TCheckBox;
    chkConfirmExit: TCheckBox;
    chkConfirmStopRender: TCheckBox;
    cbUseTemplate: TCheckBox;
    cbMissingPlugin: TCheckBox;
    pnlMultithreading: TPanel;
    cbNrTheads: TComboBox;
    pnlPNGTransparency: TPanel;
    grpGuidelines: TGroupBox;
    cbGL: TCheckBox;
    pnlCenterLine: TPanel;
    shCenterLine: TShape;
    pnlThirdsLine: TPanel;
    shThirdsLine: TShape;
    pnlGRLine: TPanel;
    shGRLine: TShape;
    pnlCenter: TPanel;
    pnlThirds: TPanel;
    pnlGoldenRatio: TPanel;
    Panel46: TPanel;
    txtLanguageFile: TComboBox;
    cbPNGTransparency: TComboBox;
    txtJPEGquality: TComboBox;
    chkEngLayout: TCheckBox;
    chkConfirmClearScript: TCheckBox;
    GroupBox15: TGroupBox;
    btnBrowseSound: TSpeedButton;
    btnPlay: TSpeedButton;
    Label44: TLabel;
    txtSoundFile: TEdit;
    chkPlaysound: TCheckBox;
    chkShowRenderStats: TCheckBox;
    chkShowRenderImage: TCheckBox;
    chkAutoSaveXML: TCheckBox;
    chkApplyFlatten: TCheckBox;
    EditorPage: TTabSheet;
    GroupBox1: TGroupBox;
    chkUseXFormColor: TCheckBox;
    chkHelpers: TCheckBox;
    chkShowAllXforms: TCheckBox;
    GroupBox21: TGroupBox;
    chkAxisLock: TCheckBox;
    chkExtendedEdit: TCheckBox;
    chkXaosRebuild: TCheckBox;
    chkResetCoefs: TCheckBox;
    chkResetLinear: TCheckBox;
    grpEditorColors: TGroupBox;
    pnlBackground: TPanel;
    pnlReferenceC: TPanel;
    pnlHelpers: TPanel;
    pnlGrid: TPanel;
    pnlBackColor: TPanel;
    shBackground: TShape;
    pnlReference: TPanel;
    shRef: TShape;
    pnlHelpersColor: TPanel;
    shHelpers: TShape;
    pnlGridColor1: TPanel;
    shGC1: TShape;
    pnlGridColor2: TPanel;
    shGC2: TShape;
    pnlReflection: TPanel;
    pnlReflectionC: TPanel;
    shReflection: TShape;
    chkEnableEditorPreview: TCheckBox;
    Panel48: TPanel;
    tbEPTransparency: TTrackBar;
    DisplayPage: TTabSheet;
    GroupBox2: TGroupBox;
    pnlLowQ: TPanel;
    pnlMediumQ: TPanel;
    pnlHighQ: TPanel;
    txtHighQuality: TEdit;
    txtMediumQuality: TEdit;
    txtLowQuality: TEdit;
    grpRendering: TGroupBox;
    pnlDensity: TPanel;
    pnlGamma: TPanel;
    pnlBright: TPanel;
    pnlVibrancy: TPanel;
    pnlRelGamma: TPanel;
    pnlOSample: TPanel;
    pnlFilter: TPanel;
    txtGammaThreshold: TEdit;
    txtFilterRadius: TEdit;
    txtOversample: TEdit;
    txtVibrancy: TEdit;
    txtBrightness: TEdit;
    txtGamma: TEdit;
    txtSampleDensity: TEdit;
    pnlFuse: TPanel;
    txtFuse: TSpinEdit;
    pnlContrast: TPanel;
    txtContrast: TEdit;
    GroupBox7: TGroupBox;
    pnSTile: TPanel;
    pnRTile: TPanel;
    pnHexTile: TPanel;
    txtHexTile: TEdit;
    txtRTile: TEdit;
    txtSTile: TEdit;
    rgRotationMode: TRadioGroup;
    rgZoomingMode: TRadioGroup;
    GroupBox20: TGroupBox;
    Label48: TLabel;
    chkShowTransparency: TCheckBox;
    chkExtendMainPreview: TCheckBox;
    pnlExtension: TPanel;
    cbExtendPercent: TComboBox;
    chkUseSmallThumbs: TCheckBox;
    rgEnumMode: TRadioGroup;
    RandomPage: TTabSheet;
    gpNumberOfTransforms: TGroupBox;
    udMinXforms: TUpDown;
    udMaxXForms: TUpDown;
    Panel15: TPanel;
    Panel16: TPanel;
    txtMaxXforms: TEdit;
    txtMinXForms: TEdit;
    gpFlameTitlePrefix: TGroupBox;
    udBatchSize: TUpDown;
    chkKeepBackground: TCheckBox;
    Panel11: TPanel;
    Panel12: TPanel;
    txtBatchSize: TEdit;
    txtRandomPrefix: TEdit;
    pnlRandBackground: TPanel;
    pnlRandBackColor: TPanel;
    shRandBackColor: TShape;
    gpMutationTransforms: TGroupBox;
    udMinMutate: TUpDown;
    udMaxMutate: TUpDown;
    Panel13: TPanel;
    Panel14: TPanel;
    txtMaxMutate: TEdit;
    txtMinMutate: TEdit;
    gpForcedSymmetry: TGroupBox;
    udSymOrder: TUpDown;
    udSymNVars: TUpDown;
    Panel17: TPanel;
    Panel18: TPanel;
    Panel19: TPanel;
    txtSymNVars: TEdit;
    txtSymOrder: TEdit;
    cmbSymType: TComboBox;
    grpGradient: TRadioGroup;
    GroupBox16: TGroupBox;
    btnGradientsFile: TSpeedButton;
    txtGradientsFile: TEdit;
    VariationsPage: TTabSheet;
    btnSetAll: TButton;
    btnClearAll: TButton;
    btnInvert: TButton;
    VarsPages: TPageControl;
    tabRandomVars: TTabSheet;
    clbVarEnabled: TCheckListBox;
    tabFavouriteVars: TTabSheet;
    clbVarFavourite: TCheckListBox;
    GradientPage: TTabSheet;
    GroupBox13: TGroupBox;
    Panel28: TPanel;
    Panel29: TPanel;
    txtTryLength: TEdit;
    txtNumtries: TEdit;
    GroupBox17: TGroupBox;
    udMinHue: TUpDown;
    Panel20: TPanel;
    Panel21: TPanel;
    udMaxHue: TUpDown;
    txtMaxHue: TEdit;
    txtMinHue: TEdit;
    GroupBox18: TGroupBox;
    Panel22: TPanel;
    Panel23: TPanel;
    udMinSat: TUpDown;
    txtMinSat: TEdit;
    udmaxSat: TUpDown;
    txtMaxSat: TEdit;
    GroupBox22: TGroupBox;
    Panel24: TPanel;
    Panel25: TPanel;
    udMinLum: TUpDown;
    txtMinLum: TEdit;
    udMaxLum: TUpDown;
    txtMaxLum: TEdit;
    GroupBox23: TGroupBox;
    Panel26: TPanel;
    Panel27: TPanel;
    udMinNodes: TUpDown;
    txtMinNodes: TEdit;
    udMaxNodes: TUpDown;
    txtMaxNodes: TEdit;
    rgColorBlend: TRadioGroup;
    chkEqualStripes: TCheckBox;
    UPRPage: TTabSheet;
    chkAdjustDensity: TCheckBox;
    GroupBox11: TGroupBox;
    Panel37: TPanel;
    Panel38: TPanel;
    txtUPRHeight: TEdit;
    txtUPRWidth: TEdit;
    GroupBox9: TGroupBox;
    Panel34: TPanel;
    Panel35: TPanel;
    Panel36: TPanel;
    txtUPROversample: TEdit;
    txtUPRFilterRadius: TEdit;
    txtFIterDensity: TEdit;
    GroupBox4: TGroupBox;
    Panel30: TPanel;
    Panel31: TPanel;
    txtFCFile: TEdit;
    txtFCIdent: TEdit;
    GroupBox5: TGroupBox;
    Panel32: TPanel;
    Panel33: TPanel;
    txtFFFile: TEdit;
    txtFFIdent: TEdit;
    PathsPage: TTabSheet;
    btnDefParams: TSpeedButton;
    btnSmooth: TSpeedButton;
    btnDefLibrary: TSpeedButton;
    btnRenderer: TSpeedButton;
    btnHelp: TSpeedButton;
    Label49: TLabel;
    btnFindDefaultSaveFile: TSpeedButton;
    btnPluginPath: TSpeedButton;
    btnSShotPath: TSpeedButton;
    btnDefScript: TSpeedButton;
    chkRememberLastOpen: TCheckBox;
    Panel39: TPanel;
    txtDefParameterFile: TEdit;
    Panel40: TPanel;
    txtDefSmoothFile: TEdit;
    Panel41: TPanel;
    Panel42: TPanel;
    Panel43: TPanel;
    txtLibrary: TEdit;
    txtRenderer: TEdit;
    txtHelp: TEdit;
    cbEnableAutosave: TCheckBox;
    Panel44: TPanel;
    txtDefaultSaveFile: TEdit;
    Panel45: TPanel;
    cbFreq: TComboBox;
    GroupBox3: TGroupBox;
    btnChaotica: TSpeedButton;
    Panel47: TPanel;
    txtChaotica: TEdit;
    Panel49: TPanel;
    Panel50: TPanel;
    txtPluginFolder: TEdit;
    pnlScreenShot: TPanel;
    txtSShotFolder: TEdit;
    pnlScriptFile: TPanel;
    txtDefScript: TEdit;
    chkSyncTriangles: TCheckBox;
    gbNotifications: TGroupBox;
    gbDefaults: TGroupBox;
    chkOldPaletteFormat: TCheckBox;
    chkRandomTemplates: TCheckBox;
    gbGradDefaults: TGroupBox;
    chkConfirmResetUndo: TCheckBox;
    gbAnimation: TGroupBox;
    pnlFPS: TPanel;
    pnlAnimPrefix: TPanel;
    edAnimPrefix: TEdit;
    seFPS: TSpinEdit;
    pnlFrameExt: TPanel;
    cbFrameExt: TComboBox;
    chkCreateAnimFolder: TCheckBox;
    procedure chkEnableEditorPreviewClick(Sender: TObject);
    procedure btnChaoticaClick(Sender: TObject);
    procedure btnLanguageClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure btnDefParamsClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSmoothClick(Sender: TObject);
    procedure cmbSymTypeChange(Sender: TObject);
    procedure btnSetAllClick(Sender: TObject);
    procedure btnClearAllClick(Sender: TObject);
    procedure txtMinNodesChange(Sender: TObject);
    procedure txtMaxNodesChange(Sender: TObject);
    procedure txtMaxHueChange(Sender: TObject);
    procedure txtMaxSatChange(Sender: TObject);
    procedure txtMaxLumChange(Sender: TObject);
    procedure txtMinHueChange(Sender: TObject);
    procedure txtMinSatChange(Sender: TObject);
    procedure txtMinLumChange(Sender: TObject);
    procedure txtMinXFormsChange(Sender: TObject);
    procedure txtMaxXformsChange(Sender: TObject);
    procedure txtMinMutateChange(Sender: TObject);
    procedure txtMaxMutateChange(Sender: TObject);
    procedure btnRendererClick(Sender: TObject);
    procedure btnDefLibraryClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure pnlBackColorClick(Sender: TObject);
    procedure pnlReferenceClick(Sender: TObject);
    procedure pnlGridColor1Click(Sender: TObject);
    procedure pnlGridColor2Click(Sender: TObject);
    procedure pnlHelpersColorClick(Sender: TObject);
    procedure btnBrowseSoundClick(Sender: TObject);
    procedure pnlCenterLineClick(Sender: TObject);
    procedure pnlThirdsLineClick(Sender: TObject);
    procedure pnlGRLineClick(Sender: TObject);
    procedure btnPlayClick(Sender: TObject);
    procedure btnGradientsFileClick(Sender: TObject);
    procedure shCenterLineMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure shThirdsLineMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure shGRLineMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure UpdateShapeColors;
    procedure shBackgroundMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure shGC1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure shGC2MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure shRefMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure shHelpersMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure chkRememberLastOpenClick(Sender: TObject);
    procedure btnFindDefaultSaveFileClick(Sender: TObject);
    procedure cbEnableAutosaveClick(Sender: TObject);
    procedure btnHelpClick(Sender: TObject);
    procedure cbGLClick(Sender: TObject);
    procedure btnPluginPathClick(Sender: TObject);
    procedure shReflectionMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pnlReflectionCClick(Sender: TObject);
    procedure RenderPanelsDblClick(Sender: TObject);
    procedure GradientPanelsDblClick(Sender: TObject);
    procedure NumFieldKeyPress(Sender: TObject; var Key: Char);
    procedure NumFloatKeyPress(Sender: TObject; var Key: Char);
    procedure TilePanelDblClick(Sender: TObject);
    procedure btnSShotPathClick(Sender: TObject);
    procedure btnDefScriptClick(Sender: TObject);
    procedure DefaultPathsDblCick(Sender: TObject);
    procedure pnlRandBackColorClick(Sender: TObject);
    procedure shRandBackColorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure chkKeepBackgroundClick(Sender: TObject);
    procedure btnInvertClick(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  OptionsForm: TOptionsForm;

implementation

{$R *.DFM}

uses
  Main, Global, Editor, ControlPoint, XFormMan, Adjust, FormRender, Translation;

procedure TOptionsForm.UpdateShapeColors;
begin
  shBackground.Brush.Color := pnlBackColor.Color;
  shGC1.Brush.Color := pnlGridColor1.Color;
  shGC2.Brush.Color := pnlGridColor2.Color;
  shRef.Brush.Color := pnlReference.Color;
  shReflection.Brush.Color := pnlReflectionC.Color;  // AV
  shHelpers.Brush.Color := pnlHelpersColor.Color;
  shCenterLine.Brush.Color := pnlCenterLine.Color;
  shThirdsLine.Brush.Color := pnlThirdsLine.Color;
  shGRLine.Brush.Color := pnlGRLine.Color;
  shRandBackColor.Brush.Color := pnlRandBackColor.Color;  // AV
end;

procedure TOptionsForm.FormShow(Sender: TObject);
var
  Registry: TRegistry;
  i, j: integer;
  s1, s2: string;
begin
  { Read position from registry }
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_CURRENT_USER;
    if Registry.OpenKey('Software\' + APP_NAME + '\Forms\Options', False) then
    begin
      if Registry.ValueExists('Left') then
        OptionsForm.Left := Registry.ReadInteger('Left');
      if Registry.ValueExists('Top') then
        OptionsForm.Top := Registry.ReadInteger('Top');
    end;
    Registry.CloseKey;
  finally
    Registry.Free;
  end;

  { General tab }
  txtDefParameterFile.Text := DefFlameFile;
  txtDefSmoothFile.Text := defSmoothPaletteFile;
  txtNumtries.text := IntToStr(Numtries);
  txtTryLength.text := IntToStr(Trylength);
  udBatchSize.Position := BatchSize;

  if NrTreads <= 1 then  
    cbNrTheads.ItemIndex := 0  // AV: MT off
  else if (NrTreads <= UseNrThreads) then
	  cbNrTheads.ItemIndex := NrTreads - 1  // AV: MT on
  else
    cbNrTheads.ItemIndex := UseNrThreads - 1;  // AV: incorrect MT value
  (* begin   // AV: what if we'd have 100 threads? :)
    //cbNrTheads.text := intTostr(NrTreads);
    // Hack
    cbNrTheads.ItemIndex := 0;
    case NrTreads of
      2: cbNrTheads.ItemIndex := 1;
      3: cbNrTheads.ItemIndex := 2;
      4: cbNrTheads.ItemIndex := 3;
      5: cbNrTheads.ItemIndex := 4;
      6: cbNrTheads.ItemIndex := 5;
      7: cbNrTheads.ItemIndex := 6;
      8: cbNrTheads.ItemIndex := 7;
      9: cbNrTheads.ItemIndex := 8;
      10: cbNrTheads.ItemIndex := 9;
      11: cbNrTheads.ItemIndex := 10;
      12: cbNrTheads.ItemIndex := 11;
    end;
  end; *)

  chkConfirmDel.Checked := ConfirmDelete;
  chkOldPaletteFormat.Checked := OldPaletteFormat;
  chkConfirmExit.Checked := ConfirmExit;
  chkConfirmStopRender.Checked := ConfirmStopRender;
  chkConfirmClearScript.Checked := ConfirmClearScript; // AV
  chkConfirmResetUndo.Checked := ConfirmResetUndo; // AV
  chkRememberLastOpen.Checked := RememberLastOpenFile;
  chkUseSmallThumbs.Checked := UseSmallThumbnails;
  cbUseTemplate.Checked := AlwaysCreateBlankFlame;
  cbMissingPlugin.Checked := WarnOnMissingPlugin;
  // cbEmbedThumbs.Checked := EmbedThumbnails;
  chkEngLayout.Checked := SetEngLayout; // AV
  chkAutoSaveXML.Checked := AutoSaveXML; // AV
  chkEqualStripes.Checked := EqualStripes;
  chkRandomTemplates.Checked := RandomizeTemplates;
  chkApplyFlatten.Checked := ApplyFlatten;

  rgRotationMode.ItemIndex := MainForm_RotationMode;
  if PreserveQuality then
    rgZoomingMode.ItemIndex := 0
  else
    rgZoomingMode.ItemIndex := 1;
  rgEnumMode.ItemIndex := FlameEnumMode;  // AV

  txtJPEGQuality.text := IntToStr(JPEGQuality);

  chkPlaySound.Checked := PlaySoundOnRenderComplete;
  txtSoundFile.Text := RenderCompleteSoundFile;

  { Animation }
  seFPS.Value := AnimFPS;
  edAnimPrefix.Text := defAnimPrefix;
  cbFrameExt.ItemIndex := defFrameExt;
  chkCreateAnimFolder.Checked := CreateAnimFolder;

  { Editor }
  chkUseXFormColor.checked := UseTransformColors;
  chkHelpers.Checked := HelpersEnabled;
  chkExtendedEdit.Checked := ExtEditEnabled;
  chkAxisLock.Checked := TransformAxisLock;
  chkXaosRebuild.Checked := RebuildXaosLinks;
  chkShowAllXforms.Checked := ShowAllXforms;
  chkEnableEditorPreview.Checked := EnableEditorPreview;
  tbEPTransparency.Position := EditorPreviewTransparency;
  chkEnableEditorPreviewClick(self);
  chkResetCoefs.Checked := AllowResetCoefs;
  chkResetLinear.Checked := AllowResetLinear;
  chkSyncTriangles.Checked := UseTriangleSync;

  { Display tab }
  txtSampleDensity.Text := FloatToStr(defSampleDensity);
  txtGamma.Text := FloatToStr(defGamma);
  txtBrightness.Text := FloatToStr(defBrightness);
  txtContrast.Text := FloatToStr(defContrast); // AV
  txtVibrancy.Text := FloatToStr(defVibrancy);
  txtOversample.Text := IntToStr(defOversample);
  txtFilterRadius.Text := FloatToStr(defFilterRadius);
  txtGammaThreshold.Text := FloatToStr(defGammaThreshold);
  txtFuse.Value := Fuse;  // AV

  txtLowQuality.Text := FloatToStr(prevLowQuality);
  txtMediumQuality.Text := FloatToStr(prevMediumQuality);
  txtHighQuality.Text := FloatToStr(prevHighQuality);

  txtHexTile.Text := FloatToStr(HexTR);  // AV
  txtSTile.Text := FloatToStr(Round6(SquareTR));  // AV
  txtRTile.Text := FloatToStr(RhombTR);  // AV

  pnlBackColor.Color := EditorBkgColor;
  pnlGridColor1.Color := GridColor1;
  pnlGridColor2.Color := GridColor2;
  pnlReference.color := ReferenceTriangleColor;
  pnlReflectionC.Color := FlipColor;   // AV
  pnlHelpersColor.Color := HelpersColor;   // AV

  cbPNGTransparency.ItemIndex :=  PNGTransparency;
  chkShowTransparency.Checked := ShowTransparency;
  cbExtendPercent.Text := FloatToStr((MainPreviewScale - 1) / 0.02);
  chkExtendMainPreview.Checked := ExtendMainPreview;

  chkShowRenderStats.Checked := ShowRenderStats;
  chkShowRenderImage.Checked := ShowRenderImage;  // AV

  { Random tab }
  udMinXforms.Position := randMinTransforms;
  udMaxXforms.Position := randMaxTransforms;
  udMinMutate.Position := mutantMinTransforms;
  udMaxMutate.Position := mutantMaxTransforms;
  txtRandomPrefix.text := RandomPrefix;
  chkKeepbackground.Checked := KeepBackground;
  cmbSymType.ItemIndex := SymmetryType;
  if (SymmetryType = 0) or (SymmetryType = 1) then
  begin
    txtSymOrder.enabled := false;
    txtSymNVars.enabled := false;
  end;
  udSymOrder.Position := SymmetryOrder;
  udSymNVars.Position := SymmetryNVars;

  pnlRandBackColor.Color := RandBackColor;  // AV
  chkKeepBackgroundClick(nil); // AV

  { Variations tab }
  for i := 0 to NRVAR -1 do
  begin
    clbVarEnabled.Checked[i] := Variations[i];
    clbVarFavourite.Checked[i] := FavouriteVariations[i]; // AV
  end;

  { Gradient tab }
  grpGradient.ItemIndex := randGradient;
  txtGradientsFile.Text := randGradientFile;
  udMinNodes.Position := MinNodes;
  udMaxNodes.Position := MaxNodes;
  udMinHue.Position := MinHue;
  udMinSat.Position := MinSat;
  udMinLum.Position := MinLum;
  udMaxHue.Position := MaxHue;
  udMaxSat.Position := MaxSat;
  udMaxLum.Position := MaxLum;
  rgColorBlend.ItemIndex := randColorBlend;

  { UPR tab }
  txtFIterDensity.text := IntToStr(UPRSampleDensity);
  txtUPRFilterRadius.text := FloatToStr(UPRFilterRadius);
  txtUPROversample.text := IntToStr(UPROversample);
  txtFCIdent.text := UPRColoringIdent;
  txtFCFile.text := UPRColoringFile;
  txtFFIdent.text := UPRFormulaIdent;
  txtFFFile.text := UPRFormulaFile;
  txtUPRWidth.text := IntToStr(UPRWidth);
  txtUPRHeight.text := IntToStr(UPRHeight);
  chkAdjustDensity.checked := UPRAdjustDensity;

  txtRenderer.Text := flam3Path;

  txtHelp.Text := HelpPath;
  txtLibrary.text := defLibrary;
  txtDefScript.Text := defScriptFile; // AV

  cbEnableAutosave.Checked := AutoSaveEnabled;
  txtDefaultSaveFile.Text := AutoSavePath;
  cbFreq.ItemIndex := AutoSaveFreq;

  cbEnableAutosaveClick(nil);

  pnlCenterLine.Color := LineCenterColor;
  pnlThirdsLine.Color := LineThirdsColor;
  pnlGRLine.Color := LineGRColor;
  cbGL.Checked := EnableGuides;
  cbGLClick(nil);
  txtChaotica.Text := ChaoticaPath;

  txtPluginFolder.Text := PluginPath;
  txtSShotFolder.Text := ScreenShotPath;

  UpdateShapeColors;

  j := -1;
  txtLanguageFile.Items.Clear;
  for i := 0 to AvailableLanguages.Count-1 do
  begin
    if AvailableLanguages.Strings[i] = '' then begin
      txtLanguageFile.Items.Add('Default (English)');
    end else begin
      LanguageInfo(AvailableLanguages.Strings[i], s1, s2);
      if (s2 <> '') then s1 := s2 + ' (' + s1 + ')';
      txtLanguageFile.Items.Add(s1);
    end;
    if (lowercase(AvailableLanguages.Strings[i]) = lowercase(languagefile)) then
      j := i;
  end;
  txtLanguageFile.ItemIndex := j;
end;

procedure TOptionsForm.GradientPanelsDblClick(Sender: TObject);
begin  // AV: restore defaults
  case TPanel(Sender).Tag of
     0: txtMinHue.Text := '0';
     1: txtMaxHue.Text := '600';
     2: txtMinSat.Text := '0';
     3: txtMaxSat.Text := '100';
     4: txtMinLum.Text := '1';
     5: txtMaxLum.Text := '100';
     6: txtMinNodes.Text := '2';
     7: txtMaxNodes.Text := '10';
     8: txtNumTries.Text := '10';
     9: txtTryLength.Text := '100000';
  end;
end;

procedure TOptionsForm.NumFieldKeyPress(Sender: TObject; var Key: Char);
begin
 if not CharinSet(Key,['0'..'9', #8, #13])
     then Key:= #0;
end;

procedure TOptionsForm.NumFloatKeyPress(Sender: TObject; var Key: Char);
begin
 if (Key = ',') then Key := '.';
 if not CharinSet(Key,['0'..'9', #8, #13, '.'])
     then Key:= #0;
end;

procedure TOptionsForm.btnOKClick(Sender: TObject);
var
  vars: boolean;
  i: integer;
  warn: boolean;
begin

  { Variations tab }
  { Get option values from controls. Disallow bad values }
  vars := false;
  for i := 0 to NRVAR-1 do begin
    Variations[i] := clbVarEnabled.Checked[i];
    vars := vars or Variations[i];
    FavouriteVariations[i] := clbVarFavourite.Checked[i]; // AV
  end;
  if vars = false then
    Variations[0] := true;

  warn := (SetEngLayout <> chkEngLayout.Checked);
  { AV: eliminate "List index out of bounds" error and memory leak
    if 32- and 64-bit Apophysis paths are different }
  i := txtLanguageFile.ItemIndex;
  if (i >= 0) then
    warn := warn or (LanguageFile <> AvailableLanguages[i]); // AV

  { General tab }  
  JPEGQuality := StrToIntDef(txtJPEGQuality.text, 100);
  if JPEGQuality > 100 then JPEGQuality := 100;
  if JPEGQuality < 1 then JPEGQuality := 100;
  
  Numtries := StrToIntDef(txtNumtries.text, 1);
  if NumTries < 1 then Numtries := 1;
  Trylength := StrToIntDef(txtTrylength.text, 100);
  if Trylength < 100 then trylength := 100;

  BatchSize := udBatchSize.Position;
  if BatchSize < 1 then BatchSize := 1;
  if BatchSize > 300 then BatchSize := 300;

  PNGTransparency := cbPNGTransparency.ItemIndex;
  ShowTransparency := chkShowTransparency.Checked;

  // NrTreads := StrToIntDef(cbNrTheads.text, 0); // AV: why? user cannot change this text!
  NrTreads := cbNrTheads.ItemIndex + 1;  // AV

  ConfirmDelete := chkConfirmDel.Checked;
  OldPaletteFormat := chkOldPaletteFormat.Checked;
  ConfirmExit := chkConfirmExit.Checked;
  ConfirmStopRender := chkConfirmStopRender.Checked;
  RememberLastOpenFile := chkRememberLastOpen.Checked;
  UseSmallThumbnails := chkUseSmallThumbs.Checked;
  AlwaysCreateBlankFlame := cbUseTemplate.Checked;
  // EmbedThumbnails := cbEmbedThumbs.Checked;
  WarnOnMissingPlugin := cbMissingPlugin.Checked;
  SetEngLayout := chkEngLayout.Checked;  // AV
  ConfirmClearScript := chkConfirmClearScript.Checked; // AV
  ConfirmResetUndo := chkConfirmResetUndo.Checked; // AV
  AutoSaveXML := chkAutoSaveXML.Checked; // AV
  EqualStripes := chkEqualStripes.Checked; // AV
  RandomizeTemplates := chkRandomTemplates.Checked;
  ApplyFlatten := chkApplyFlatten.Checked;
  { AV: eliminate "List index out of bounds" error and memory leak
    if 32- and 64-bit Apophysis paths are different }
  if (i >= 0) then LanguageFile := AvailableLanguages.Strings[i];

  MainForm_RotationMode := rgRotationMode.ItemIndex;
  PreserveQuality := (rgZoomingMode.ItemIndex = 0);
  FlameEnumMode := rgEnumMode.ItemIndex;  // AV

  LineCenterColor := pnlCenterLine.Color;
  LineThirdsColor := pnlThirdsLine.Color;
  LineGRColor := pnlGRLine.Color;
  EnableGuides := cbGL.Checked;

  { Animation  }
  AnimFPS := seFPS.Value;
  defAnimPrefix := edAnimPrefix.Text;
  defFrameExt := cbFrameExt.ItemIndex;
  CreateAnimFolder := chkCreateAnimFolder.Checked;

 { Editor  }
  UseTransformColors := chkUseXFormColor.checked;
  HelpersEnabled := chkHelpers.Checked;
  ShowAllXforms := chkShowAllXforms.Checked;

  ExtEditEnabled := chkExtendedEdit.Checked;
  TransformAxisLock := chkAxisLock.Checked;
  RebuildXaosLinks := chkXaosRebuild.Checked;
  EnableEditorPreview := chkEnableEditorPreview.Checked;
  EditorPreviewTransparency := tbEPTransparency.Position;
  AllowResetCoefs := chkResetCoefs.Checked;
  AllowResetLinear := chkResetLinear.Checked;
  UseTriangleSync := chkSyncTriangles.Checked;

  { Display tab }
   // AV: replaced all try-except blocks by a more convenient StrToFloatDef

   defSampleDensity := StrToFloatDef(txtSampleDensity.Text, 5);
   if defSampleDensity > 100 then
      defSampleDensity := 100
   else if defSampleDensity <= 0 then
      defSampleDensity := 0.1;

   defGamma := StrToFloatDef(txtGamma.Text, 4);
   if defGamma < 0.1 then
      defGamma := 0.1
   else if defGamma > 10 then
      defGamma := 10;

   defBrightness := StrToFloatDef(txtBrightness.Text, 4);
   if defBrightness < 0.1 then
      defBrightness := 0.1
   else if defBrightness > 100 then
      defBrightness := 100;

   defContrast := StrToFloatDef(txtContrast.Text, 1);
   if defContrast < 0.1 then
      defContrast := 0.1
   else if defContrast > 10 then
      defContrast := 10;

  defVibrancy := StrToFloatDef(txtVibrancy.Text, 1);
  if defVibrancy < 0.1 then defVibrancy := 0.1;

  defFilterRadius := StrToFloatDef(txtFilterRadius.Text, 0.2);
  if defFilterRadius <= 0 then defFilterRadius := 0.1;

  defGammaThreshold := StrToFloatDef(txtGammaThreshold.Text, 0);
  if defGammaThreshold < 0 then defGammaThreshold := 0;

  defOversample := StrToIntDef(txtOversample.Text, 1);
  if defOversample > 4 then
    defOversample := 4
  else if defOversample < 1 then
    defOversample := 1;

  prevLowQuality := StrToFloatDef(txtLowQuality.Text, 0.1);
  if prevLowQuality > 100 then
    prevLowQuality := 100
  else if prevLowQuality < 0.01 then
    prevLowQuality := 0.1;

  prevMediumQuality := StrToFloatDef(txtMediumQuality.Text, 1);
  if prevMediumQuality > 500 then
    prevMediumQuality := 500
  else if prevMediumQuality < 0.01 then
    prevMediumQuality := 0.01;

  prevHighQuality := StrToFloatDef(txtHighQuality.Text, 5);
  if prevHighQuality > 1000 then
    prevHighQuality := 1000
  else if prevHighQuality < 0.01 then
    prevHighQuality := 0.01;

  Fuse := txtFuse.Value;  // AV

  HexTR := StrToFloatDef(txtHexTile.Text, 1);  // AV
  if HexTR < 0.1 then HexTR := 0.1;

  SquareTR := StrToFloatDef(txtSTile.Text, 1);  // AV
  if SquareTR < 0.1 then SquareTR := 0.1;

  RhombTR := StrToFloatDef(txtRTile.Text, 1);  // AV
  if RhombTR < 0.1 then RhombTR := 0.1;

  MainPreviewScale := 1 + 0.02 * StrToFloatDef(cbExtendPercent.Text, 0);
  if MainPreviewScale < 1 then
    MainPreviewScale := 1
  else if MainPreviewScale > 5 then
    MainPreviewScale := 5;
  ExtendMainPreview := chkExtendMainPreview.Checked;

  ShowRenderStats := chkShowRenderStats.Checked;
  ShowRenderImage := chkShowRenderImage.Checked; // AV

  { Random tab }
  randMinTransforms := udMinXforms.Position;
  randMaxTransforms := udMaxXforms.Position;
  mutantMinTransforms := udMinMutate.Position;
  mutantMaxTransforms := udMaxMutate.Position;
  RandomPrefix := txtRandomPrefix.text;
  SymmetryType := cmbSymType.ItemIndex;
  SymmetryOrder := udSymOrder.Position;
  SymmetryNVars := udSymNVars.Position;
  KeepBackground := chkKeepbackground.Checked;

  {Gradient tab }
  randGradient := grpGradient.ItemIndex;
  randGradientFile := txtGradientsFile.Text;
  MinNodes := udMinNodes.Position;
  MaxNodes := udMaxNodes.Position;
  MinHue := udMinHue.Position;
  MinSat := udMinSat.Position;
  MinLum := udMinLum.Position;
  MaxHue := udMaxHue.Position;
  MaxSat := udMaxSat.Position;
  MaxLum := udMaxLum.Position;
  randColorBlend := rgColorBlend.ItemIndex;

  { UPR options }
  UPRSampleDensity := StrToInt(txtFIterDensity.text);
  UPRFilterRadius := StrToFloat(txtUPRFilterRadius.text);
  UPROversample := StrToInt(txtUPROversample.text);
  UPRColoringIdent := txtFCIdent.text;
  UPRColoringFile := txtFCFile.text;
  UPRFormulaIdent := txtFFIdent.text;
  UPRFormulaFile := txtFFFile.text;
  UPRAdjustDensity := chkAdjustDensity.checked;
  UPRWidth := StrToInt(txtUPRWidth.text);
  UPRHeight := StrToInt(txtUPRHeight.text);

  flam3Path := txtRenderer.text;

  {Paths}
  defLibrary := txtLibrary.text;
  // if (not RememberLastOpenFile) then  // AV: fixed
  defFlameFile := txtDefParameterFile.Text;
  defSmoothPaletteFile := txtDefSmoothFile.Text;
  PlaySoundOnRenderComplete := chkPlaySound.Checked;
  RenderCompleteSoundFile := txtSoundFile.Text;
  HelpPath := txtHelp.Text;
  ChaoticaPath := txtChaotica.text;
  ScreenShotPath := txtSShotFolder.Text;  // AV
  if (RightStr(ScreenShotPath, 1) <> '\') and (ScreenShotPath <> '') then
    ScreenShotPath := ScreenShotPath + '\';
  defScriptFile := txtDefScript.Text; // AV

  PluginPath := txtPluginFolder.Text;
  if (PluginPath = '') or (not DirectoryExists(PluginPath)) then
    PluginPath := ExtractFilePath(Application.ExeName);  // AV
  if (RightStr(PluginPath, 1) <> '\') and (PluginPath <> '') then
    PluginPath := PluginPath + '\';

  AutoSaveEnabled := cbEnableAutosave.Checked;
  AutoSavePath := txtDefaultSaveFile.Text;
  AutoSaveFreq := cbFreq.ItemIndex;  // AV:

  MainForm.mnuExportFLame.Enabled := FileExists(flam3Path);
  MainForm.mnuExportChaotica.Enabled := FileExists(chaoticaPath + '\chaotica.exe');

  if (warn) then
    Application.MessageBox(PChar(TextByKey('options-restartnotice')),
      ApophysisSVN, MB_ICONWARNING);

  ModalResult := mrOK; // AV: was Close;
end;

procedure TOptionsForm.btnDefParamsClick(Sender: TObject);
begin
  OpenDialog.Filter := TextByKey('common-filter-flamefiles') + '|*.flame|' +
                      TextBykey('common-filter-allfiles') + '|*.*';
  OpenDialog.InitialDir := ParamFolder; // AV
  OpenDialog.FileName := '';
  if OpenDialog.Execute then
    txtDefParameterFile.text := OpenDialog.FileName;
end;

procedure TOptionsForm.btnDefScriptClick(Sender: TObject);
begin
  OpenDialog.Filter := TextByKey('common-filter-scriptfiles') + '|*.aposcript;*.asc|'
                     + TextBykey('common-filter-allfiles') + '|*.*';;
  if DirectoryExists(ScriptPath) then
    OpenDialog.InitialDir := ScriptPath + '\'
  else
    OpenDialog.InitialDir := ExtractFilePath(Application.ExeName);
  OpenDialog.FileName := '';
  OpenDialog.DefaultExt := 'asc';
  if OpenDialog.Execute then
    txtDefScript.Text := OpenDialog.FileName;
end;

procedure TOptionsForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
  Registry: TRegistry;
begin
  { Write position to registry }
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_CURRENT_USER;
    if Registry.OpenKey('\Software\' + APP_NAME + '\Forms\Options', True) then
    begin
      Registry.WriteInteger('Top', OptionsForm.Top);
      Registry.WriteInteger('Left', OptionsForm.Left);
    end;
  finally
    Registry.Free;
  end;
end;

procedure TOptionsForm.btnSmoothClick(Sender: TObject);
begin
  OpenDialog.Filter := TextByKey('common-filter-gradientfiles') +'|*.gradient;*.ugr|'
                    + TextBykey('common-filter-allfiles') + '|*.*';
  OpenDialog.InitialDir := ExtractFilePath(defSmoothPaletteFile);
  OpenDialog.FileName := '';
  OpenDialog.DefaultExt := 'ugr';
  if OpenDialog.Execute then
    txtDefSmoothFile.text := OpenDialog.FileName;;
end;

procedure TOptionsForm.btnSShotPathClick(Sender: TObject);  // AV
var sspath: string;
begin
  sspath := ScreenShotPath;
  if SelectDirectory(TextByKey('options-tab-environment-screenshotshint'), '',
      sspath, [sdNewFolder, sdShowEdit, sdNewUI, sdValidateDir], Self) then
    txtSShotFolder.Text := IfThen(RightStr(sspath, 1)='\', sspath, sspath + '\');
end;

procedure TOptionsForm.cmbSymTypeChange(Sender: TObject);
begin
  if (cmbSymType.ItemIndex = 0) or (cmbSymType.ItemIndex = 1) then
  begin
    txtSymOrder.enabled := false;
    txtSymNVars.enabled := false;
  end else
  begin
    txtSymOrder.enabled := true;
    txtSymNVars.enabled := true;
  end;
end;

procedure TOptionsForm.DefaultPathsDblCick(Sender: TObject);
begin   // AV: reset paths
  case TPanel(Sender).Tag of
     0: txtDefParameterFile.Text := '';
     1: txtDefSmoothFile.Text := AppPath + 'Gradients\SmoothPalette.ugr';
     2: txtLibrary.Text := AppPath + 'Functions.asc';
     3: txtRenderer.Text := GetEnvVarValue('PROGRAMFILES') + '\flam3-3.0.1\flam3-render.exe';
     4: txtHelp.Text := AppPath + 'Apophysis AV.chm';
     5: begin
          txtPluginFolder.Text :=  AppPath + 'Plugins\';
          if FileExists(AppPath + 'ApoPluginSrc.dat') then
             DeleteFile(AppPath + 'ApoPluginSrc.dat'); // delete old string
        end;
     6: txtSShotFolder.Text := AppPath + 'ScreenShots\';
     7: txtDefScript.Text := '';
     8: txtDefaultSaveFile.Text := AppPath + 'autosave.flame';
  end;
end;

procedure TOptionsForm.btnSetAllClick(Sender: TObject);
var
  i: integer;
begin
  if VarsPages.ActivePageIndex = 0 then  // AV
    for i := 0 to NRVAR - 1 do clbVarEnabled.Checked[i] := True
    //clbVarEnabled.CheckAll(cbChecked)
  else
    for i := 0 to NRVAR - 1 do clbVarFavourite.Checked[i] := True;
    //clbVarFavourite.CheckAll(cbChecked);
end;

procedure TOptionsForm.btnClearAllClick(Sender: TObject);
var
  i: integer;
begin
  if VarsPages.ActivePageIndex = 0 then  // AV
    for i := 0 to NRVAR - 1 do clbVarEnabled.Checked[i] := False
    //clbVarEnabled.CheckAll(cbUnchecked)
  else
    for i := 0 to NRVAR - 1 do clbVarFavourite.Checked[i] := False;
    //clbVarFavourite.CheckAll(cbUnchecked);
end;

procedure TOptionsForm.txtMinNodesChange(Sender: TObject);
begin
  if StrToInt(txtMinNodes.Text) > udMaxNodes.position then
    udMaxNodes.Position := StrToInt(txtMinNodes.Text);
end;

procedure TOptionsForm.txtMaxNodesChange(Sender: TObject);
begin
  if StrToInt(txtMaxNodes.Text) < udMinNodes.position then
    udMinNodes.Position := StrToInt(txtMaxNodes.Text);
end;

procedure TOptionsForm.txtMaxHueChange(Sender: TObject);
begin
  if StrToInt(txtMaxHue.Text) < udMinHue.position then
    udMinHue.Position := StrToInt(txtMaxHue.Text);
end;

procedure TOptionsForm.txtMaxSatChange(Sender: TObject);
begin
  if StrToInt(txtMaxSat.Text) < udMinSat.position then
    udMinSat.Position := StrToInt(txtMaxSat.Text);
end;

procedure TOptionsForm.txtMaxLumChange(Sender: TObject);
begin
  if StrToInt(txtMaxLum.Text) < udMinLum.position then
    udMinLum.Position := StrToInt(txtMaxLum.Text);
end;

procedure TOptionsForm.txtMinHueChange(Sender: TObject);
begin
  if StrToInt(txtMinHue.Text) > udMaxHue.position then
    udMaxHue.Position := StrToInt(txtMinHue.Text);
end;

procedure TOptionsForm.txtMinSatChange(Sender: TObject);
begin
  if StrToInt(txtMinSat.Text) > udMaxSat.position then
    udMaxSat.Position := StrToInt(txtMinSat.Text);
end;

procedure TOptionsForm.txtMinLumChange(Sender: TObject);
begin
  if StrToInt(txtMinLum.Text) > udMaxLum.position then
    udMaxLum.Position := StrToInt(txtMinLum.Text);
end;

procedure TOptionsForm.txtMinXFormsChange(Sender: TObject);
begin
  if StrToInt(txtMinXForms.Text) > udMaxXForms.position then
    udMaxXFOrms.Position := StrToInt(txtMinXForms.Text);
end;

procedure TOptionsForm.txtMaxXformsChange(Sender: TObject);
begin
  if StrToInt(txtMaxXForms.Text) < udMinXForms.position then
    udMinXForms.Position := StrToInt(txtMaxXforms.Text);
end;

procedure TOptionsForm.txtMinMutateChange(Sender: TObject);
begin
  if StrToInt(txtMinMutate.Text) > udMaxMutate.position then
    udMaxMutate.Position := StrToInt(txtMinMutate.Text);
end;

procedure TOptionsForm.txtMaxMutateChange(Sender: TObject);
begin
  if StrToInt(txtMaxMutate.Text) < udMinMutate.position then
    udMinMutate.Position := StrToInt(txtMaxMutate.Text);
end;

procedure TOptionsForm.btnRendererClick(Sender: TObject);
begin
  OpenDialog.Filter := TextBykey('common-filter-allfiles') + '|*.exe';
  if FileExists(flam3Path) then
    OpenDialog.InitialDir := ExtractFilePath(flam3Path)
  else
    OpenDialog.InitialDir := GetEnvironmentVariable('PROGRAMFILES');
  OpenDialog.FileName := 'flam3-render.exe'; // AV
  OpenDialog.DefaultExt := 'exe';  // AV
  if OpenDialog.Execute then
    txtRenderer.Text := OpenDialog.FileName;
end;

procedure TOptionsForm.btnDefLibraryClick(Sender: TObject);
begin
  OpenDialog.Filter := TextByKey('common-filter-scriptfiles') + '|*.aposcript;*.asc|'
                      + TextBykey('common-filter-allfiles') + '|*.*';
  OpenDialog.InitialDir := ExtractFilePath(defLibrary);
  OpenDialog.FileName := '';
  OpenDialog.DefaultExt := '.asc';
  if OpenDialog.Execute then
    txtLibrary.Text := OpenDialog.FileName;
end;

procedure TOptionsForm.TilePanelDblClick(Sender: TObject);
begin
  case TPanel(Sender).Tag of
     0: txtSTile.Text := '1.414214';
     1: txtRTile.Text := '1';
     2: txtHexTile.Text := '2';
  end;
end;

procedure TOptionsForm.FormCreate(Sender: TObject);
var
  i: integer;
begin
  // AV: to use only actual number of cores in the drop-down list
  try
    for i := 2 to GetCpuCount do
      cbNrTheads.Items.Add(IntToStr(i));
  except
    cbNrTheads.Items.Clear;
    for i := 1 to 12 do
      cbNrTheads.Items.Add(IntToStr(i));
  end;
  UseNrThreads := cbNrTheads.Items.Count; // save nr processor cores

  btnOK.Caption := TextByKey('common-ok');
	btnCancel.Caption := TextByKey('common-cancel');
	pnlLowQ.Caption := TextByKey('common-lowquality');
	pnlMediumQ.Caption := TextByKey('common-mediumquality');
	pnlHighQ.Caption := TextByKey('common-highquality');
  btnDefParams.Hint := TextByKey('common-browse');
  btnSmooth.Hint := btnDefParams.Hint;
  btnDefLibrary.Hint := btnDefParams.Hint;
  btnRenderer.Hint := btnDefParams.Hint;
  btnHelp.Hint := btnDefParams.Hint;
  btnPluginPath.Hint := btnDefParams.Hint;
  btnSShotPath.Hint := btnDefParams.Hint;
  btnChaotica.Hint := btnDefParams.Hint;
  btnFindDefaultSaveFile.Hint := btnDefParams.Hint;
  btnBrowseSound.Hint := btnDefParams.Hint;
  btnDefScript.Hint := btnDefParams.Hint;
  btnLanguage.Hint := btnDefParams.Hint;
	Panel37.Caption := TextByKey('common-width');
	Panel38.Caption := TextByKey('common-height');
	Panel31.Caption := TextByKey('common-filename');
	Panel33.Caption := TextByKey('common-filename');
	Panel44.Caption := TextByKey('common-filename');
	pnlFilter.Caption := TextByKey('common-filterradius');
	Panel35.Caption := TextByKey('common-filterradius');
	pnlDensity.Caption := TextByKey('common-density');
	Panel34.Caption := TextByKey('common-density');
	pnlOSample.Caption := TextByKey('common-oversample');
	Panel36.Caption := TextByKey('common-oversample');
	pnlGamma.Caption := TextByKey('common-gamma');
	pnlBright.Caption := TextByKey('common-brightness');
	pnlVibrancy.Caption := TextByKey('common-vibrancy');
  pnlContrast.Caption := TextByKey('common-contrast');  // AV
	pnlRelGamma.Caption := TextByKey('common-gammathreshold');
	cbPNGTransparency.Items[1] := TextByKey('common-enabled');
	cbPNGTransparency.Items[0] := TextByKey('common-disabled');
	Panel13.Caption := TextByKey('common-minimum');
	Panel15.Caption := TextByKey('common-minimum');
	Panel21.Caption := TextByKey('common-minimum');
	Panel22.Caption := TextByKey('common-minimum');
	Panel25.Caption := TextByKey('common-minimum');
	Panel26.Caption := TextByKey('common-minimum');
	Panel14.Caption := TextByKey('common-maximum');
	Panel16.Caption := TextByKey('common-maximum');
	Panel20.Caption := TextByKey('common-maximum');
	Panel23.Caption := TextByKey('common-maximum');
	Panel24.Caption := TextByKey('common-maximum');
	Panel27.Caption := TextByKey('common-maximum');
	Label49.Caption := TextByKey('common-minutes');
  Panel47.Caption := TextByKey('common-filename');
  Panel50.Caption := TextByKey('options-tab-general-pluginpath');
  Panel48.Caption := TextByKey('options-tab-editor-previewtransparency');
  gbDefaults.Caption := TextByKey('options-tab-general-defaults');
  gbGradDefaults.Caption := TextByKey('options-tab-general-defaults');
  gbNotifications.Caption := TextByKey('options-tab-general-notifications');
  pnlScreenShot.Caption := TextByKey('options-tab-environment-screenshots');
  pnlScriptFile.Caption := TextByKey('options-tab-environment-defaultscript');
  chkEnableEditorPreview.Caption := TextByKey('options-tab-editor-enablepreview');
  chkAutoSaveXML.Caption := TextByKey('options-tab-general-autosavepasted');
	self.Caption := TextByKey('options-title');
	GeneralPage.Caption := TextByKey('options-tab-general-title');
  Panel46.Caption := TextByKey('options-tab-general-language');
	pnlMultithreading.Caption := TextByKey('options-tab-general-multithreading');
	cbNrTheads.Items[0] := TextByKey('options-tab-general-multithreading-off');
	pnlJPEGQuality.Caption := TextByKey('options-tab-general-jpegquality');
	pnlPNGTransparency.Caption := TextByKey('options-tab-general-pngtransparency');
	chkShowRenderStats.Caption := TextByKey('options-tab-general-showextendedstatistics');
  chkShowRenderImage.Caption := TextByKey('options-tab-general-showrenderimage');
	chkConfirmDel.Caption := TextByKey('options-tab-general-confirmdelete');
	chkConfirmExit.Caption := TextByKey('options-tab-general-confirmexit');
	chkconfirmStopRender.Caption := TextByKey('options-tab-general-confirmrenderstop');
  chkConfirmClearScript.Caption := TextByKey('options-tab-general-confirmclearscript');
  chkConfirmResetUndo.Caption := TextByKey('options-tab-general-confirmresetundo');
	chkOldPaletteFormat.Caption := TextByKey('options-tab-general-oldgradientformat');
	cbUseTemplate.Caption := TextByKey('options-tab-general-alwaysblankflame');
  chkEqualStripes.Caption := TextByKey('options-tab-gradient-equalstripes');
	cbMissingplugin.Caption := TextByKey('options-tab-general-enablemissingpluginswarning');
	// cbEmbedThumbs.Caption := TextByKey('options-tab-general-enablethumbnailembedding');
  chkEngLayout.Caption := TextByKey('options-tab-general-setenglayout');
  chkRandomTemplates.Caption := TextByKey('options-tab-general-templaterandcolor');
  chkApplyFlatten.Caption := TextByKey('options-tab-general-autoflatten');
	rgRotationMode.Caption := TextByKey('options-tab-general-rotatemode');
	rgRotationMode.Items[0] := TextByKey('options-tab-general-rotateimage');
	rgRotationMode.Items[1] := TextByKey('options-tab-general-rotateframe');
	rgZoomingMode.Caption := TextByKey('options-tab-general-zoommode');
	rgZoomingMode.Items[0] := TextByKey('options-tab-general-preservequality');
	rgZoomingMode.Items[1] := TextByKey('options-tab-general-preservespeed');
	rgEnumMode.Caption := TextByKey('options-tab-general-enummode');
	rgEnumMode.Items[0] := TextByKey('options-tab-general-enum0');
	rgEnumMode.Items[1] := TextByKey('options-tab-general-enum1');
	grpGuidelines.Caption := TextByKey('options-tab-general-guides');
	cbGl.Caption := TextByKey('options-tab-general-enableguides');
	pnlCenter.Caption := TextByKey('options-tab-general-guidecentercolor');
	pnlThirds.Caption := TextByKey('options-tab-general-guidethirdscolor');
	pnlGoldenRatio.Caption := TextByKey('options-tab-general-guidegoldenratiocolor');
	EditorPage.Caption := TextByKey('options-tab-editor-title');
	GroupBox1.Caption := TextByKey('options-tab-editor-editorgraph');
	GroupBox21.Caption := TextByKey('options-tab-editor-editordefaults');
	chkUseXFormColor.Caption := TextByKey('options-tab-editor-usetransformcolor');
	chkHelpers.Caption := TextByKey('options-tab-editor-helperlines');
	chkShowAllXForms.Caption := TextByKey('options-tab-editor-alwaysshowbothtransformtypes');
  chkResetCoefs.Caption := TextByKey('options-tab-editor-resetcoefs'); // AV
  chkResetLinear.Caption := TextByKey('options-tab-editor-resetlinear'); // AV
	pnlBackground.Caption := TextByKey('options-tab-editor-backgroundcolor');
	pnlGrid.Caption := TextByKey('options-tab-editor-gridcolors');
	pnlReferenceC.Caption := TextByKey('options-tab-editor-referencecolor');
	pnlHelpers.Caption := TextByKey('options-tab-editor-helpercolors');
  pnlReflection.Caption := TextByKey('editor-tab-triangle-reflection');
	chkExtendedEdit.Caption := TextByKey('options-tab-editor-extendededit');
	chkAxisLock.Caption := TextByKey('options-tab-editor-locktransformaxes');
	chkXaosRebuild.Caption := TextByKey('options-tab-editor-rebuildxaoslinks');
	DisplayPage.Caption := TextByKey('options-tab-display-title');
	grpRendering.Caption := TextByKey('options-tab-display-rendering');
	GroupBox2.Caption := TextByKey('options-tab-display-previewdensity');
	GroupBox20.Caption := TextByKey('options-tab-display-mainpreview');
	chkExtendMainPreview.Caption := TextByKey('options-tab-display-extendpreviewbuffer');
	pnlExtension.Caption := TextByKey('options-tab-display-extenspreviewbufferlabel');
	chkShowTransparency.Caption := TextByKey('options-tab-display-showtransparency');
	chkUseSmallThumbs.Caption := TextByKey('options-tab-display-usesmallthumbs');
	RandomPage.Caption := TextByKey('options-tab-random-title');
	gpNumberOfTransforms.Caption := TextByKey('options-tab-random-numberoftransforms');
	gpMutationTransforms.Caption := TextByKey('options-tab-random-mutationtransforms');
	gpFlameTitlePrefix.Caption := TextByKey('options-tab-random-randombatch');
	gpForcedSymmetry.Caption := TextByKey('options-tab-random-forcedsymmetry');
	Panel11.Caption := TextByKey('options-tab-random-batchsize');
	Panel12.Caption := TextByKey('options-tab-random-titleprefix');
	chkKeepBackground.Caption := TextByKey('options-tab-random-keepbackground');
	Panel17.Caption := TextByKey('options-tab-random-symtype');
	Panel18.Caption := TextByKey('options-tab-random-symorder');
	Panel19.Caption := TextByKey('options-tab-random-symlimit');
	cmbSymType.Items[0] := TextByKey('options-tab-random-type-none');
	cmbSymType.Items[1] := TextByKey('options-tab-random-type-bilateral');
	cmbSymType.Items[2] := TextByKey('options-tab-random-type-rotational');
	cmbSymType.Items[3] := TextByKey('options-tab-random-type-dihedral');
	grpGradient.Caption := TextByKey('options-tab-random-onrandom');
	grpGradient.Items[0] := TextByKey('options-tab-random-userandom');
	grpGradient.Items[1] := TextByKey('options-tab-random-usedefault');
	grpGradient.Items[2] := TextByKey('options-tab-random-usecurrent');
	grpGradient.Items[3] := TextByKey('options-tab-random-randomcalculated');
	grpGradient.Items[4] := TextByKey('options-tab-random-randomfromfile');
	rgColorBlend.Caption := TextByKey('options-tab-gradient-colorblend');
	rgColorBlend.Items[0] := TextByKey('options-tab-gradient-rgbblend');
	rgColorBlend.Items[1] := TextByKey('options-tab-gradient-hsvblend');
	rgColorBlend.Items[2] := TextByKey('options-tab-gradient-noblend');
	GroupBox16.Caption := TextByKey('options-tab-random-filetouse');
	VariationsPage.Caption := TextByKey('options-tab-variations-title');
  tabRandomVars.Caption := TextByKey('options-tab-variations-random');  // AV
  tabFavouriteVars.Caption := TextByKey('options-tab-variations-faves'); // AV
	btnSetAll.Caption := TextByKey('options-tab-variations-setall');
	btnClearAll.Caption := TextByKey('options-tab-variations-clearall');
  btnInvert.Caption := TextByKey('options-tab-variations-invert');
	GradientPage.Caption := TextByKey('options-tab-gradient-title');
	GroupBox23.Caption := TextByKey('options-tab-gradient-numberofnodes');
	GroupBox13.Caption := TextByKey('options-tab-gradient-smoothpalette');
	GroupBox17.Caption := TextByKey('options-tab-gradient-huebetween');
	GroupBox18.Caption := TextByKey('options-tab-gradient-satbetween');
	GroupBox22.Caption := TextByKey('options-tab-gradient-lumbetween');
	Panel28.Caption := TextByKey('options-tab-gradient-numtries');
	Panel29.Caption := TextByKey('options-tab-gradient-trylength');
	UPRPage.Caption := TextByKey('options-tab-upr-title');
	GroupBox9.Caption := TextByKey('options-tab-upr-paramdefaults');
	GroupBox4.Caption := TextByKey('options-tab-upr-coloralgorithm');
	GroupBox11.Caption := TextByKey('options-tab-upr-uprsize');
	GroupBox5.Caption := TextByKey('options-tab-upr-formula');
	Panel30.Caption := TextByKey('options-tab-upr-identifier');
	Panel32.Caption := TextByKey('options-tab-upr-identifier');
	chkAdjustDensity.Caption := TextByKey('options-tab-upr-adjustdensity');
  PathsPage.Caption := TextByKey('options-tab-environment-title');
	Panel39.Caption := TextByKey('options-tab-environment-defaultparams');
	Panel40.Caption := TextByKey('options-tab-environment-smoothpalette');
	Panel41.Caption := TextByKey('options-tab-environment-functionlib');
	//Panel42.Caption := TextByKey('options-tab-environment-exportrenderer');
	Panel43.Caption := TextByKey('options-tab-environment-helpfile');
	chkRememberLastOpen.Caption := TextByKey('options-tab-environment-rememberlastopen');
	cbEnableAutosave.Caption := TextByKey('options-tab-environment-autosave');
	panel45.Caption := TextByKey('options-tab-environment-savefrequency');
  grpEditorColors.Caption := TextByKey('editor-tab-color-title');
  GroupBox15.Caption := TextByKey('options-tab-general-onrendercomplete');
  chkPlaySound.Caption := TextByKey('options-tab-general-playsound');
  btnPlay.Hint := TextByKey('options-tab-general-playhint');
  Label44.Caption := TextByKey('options-tab-general-soundfile');
  pnlFuse.Caption := TextByKey('options-tab-display-fuse');
  pnHexTile.Caption := TextByKey('main-menu-flame-hextile');
  pnRTile.Caption := TextByKey('main-menu-flame-rhombustile');
  pnSTile.Caption := TextByKey('main-menu-flame-squaretile');
  GroupBox7.Caption := TextByKey('options-tab-display-tileradius');
  pnlRandBackground.Caption := TextByKey('options-tab-editor-backgroundcolor');
  chkSyncTriangles.Caption := TextByKey('options-tab-editor-synctriangles');

  gbAnimation.Caption := TextByKey('animate-animation');
  pnlFPS.Caption := TextByKey('animate-fps');
  pnlAnimPrefix.Caption := TextByKey('animate-prefix');
  pnlFrameExt.Caption := TextByKey('animate-graphicext');
  chkCreateAnimFolder.Caption := TextByKey('options-tab-general-createanimdir') ;

  for i:= 0 to NRVAR - 1 do begin
    clbVarEnabled.Items.Add(varnames(i));
    clbVarFavourite.Items.Add(varnames(i)); // AV
  end;

  Tabs.ActivePageIndex := 0;  // AV
  VarsPages.ActivePageIndex := 0; // AV
end;

procedure TOptionsForm.pnlCenterLineClick(Sender: TObject);
begin
  if (not cbGL.Checked) then exit;
  AdjustForm.ColorDialog.Color := pnlCenterLine.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlCenterLine.Color := AdjustForm.ColorDialog.Color;
    LineCenterColor := Integer(pnlCenterLine.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlThirdsLineClick(Sender: TObject);
begin
  if (not cbGL.Checked) then exit;
  AdjustForm.ColorDialog.Color := pnlThirdsLine.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlThirdsLine.Color := AdjustForm.ColorDialog.Color;
    LineThirdsColor := Integer(pnlThirdsLine.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.RenderPanelsDblClick(Sender: TObject);
begin   // AV: reset values
  case TPanel(Sender).Tag of
     0: txtSampleDensity.Text := '5';
     1: txtGamma.Text := '4';
     2: txtBrightness.Text := '4';
     3: txtVibrancy.Text := '1';
     4: txtGammaThreshold.Text := '0.01';
     5: txtOversample.Text := '1';
     6: txtFilterRadius.Text := '0.2';
     7: txtFuse.Value := 15;
     8: txtLowQuality.Text := '0.1';
     9: txtMediumQuality.Text := '1';
     10: txtHighQuality.Text := '5';
     11: txtContrast.Text := '1';
  end;
end;

procedure TOptionsForm.pnlGRLineClick(Sender: TObject);
begin
  if (not cbGL.Checked) then exit;
  AdjustForm.ColorDialog.Color := pnlGRLine.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlGRLine.Color := AdjustForm.ColorDialog.Color;
    LineGRColor := Integer(pnlGRLine.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlBackColorClick(Sender: TObject);
begin
  AdjustForm.ColorDialog.Color := pnlBackColor.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlBackColor.Color := AdjustForm.ColorDialog.Color;
    EditorBkgColor := Integer(pnlBackColor.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlRandBackColorClick(Sender: TObject);
begin
  AdjustForm.ColorDialog.Color := pnlRandBackColor.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlRandBackColor.Color := AdjustForm.ColorDialog.Color;
    RandBackColor := Integer(pnlRandBackColor.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlReferenceClick(Sender: TObject);
begin
  AdjustForm.ColorDialog.Color := pnlReference.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlReference.Color := AdjustForm.ColorDialog.Color;
    ReferenceTriangleColor := Integer(pnlReference.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlReflectionCClick(Sender: TObject);
begin
  AdjustForm.ColorDialog.Color := pnlReflectionC.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlReflectionC.Color := AdjustForm.ColorDialog.Color;
    FlipColor := Integer(pnlReflectionC.Color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlGridColor1Click(Sender: TObject);
begin
  AdjustForm.ColorDialog.Color := pnlGridColor1.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlGridColor1.Color := AdjustForm.ColorDialog.Color;
    GridColor1 := Integer(pnlGridColor1.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlGridColor2Click(Sender: TObject);
begin
  AdjustForm.ColorDialog.Color := pnlGridColor2.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlGridColor2.Color := AdjustForm.ColorDialog.Color;
    GridColor2 := Integer(pnlGridColor2.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.pnlHelpersColorClick(Sender: TObject);
begin
  AdjustForm.ColorDialog.Color := pnlHelpersColor.Color;
  if AdjustForm.ColorDialog.Execute then
  begin
    pnlHelpersColor.Color := AdjustForm.ColorDialog.Color;
    HelpersColor := Integer(pnlHelpersColor.color);
    UpdateShapeColors;
  end;
end;

procedure TOptionsForm.btnBrowseSoundClick(Sender: TObject);
var
  fn:string;
begin
  if RenderCompleteSoundFile = '' then  // AV
    OpenDialog.InitialDir := GetEnvironmentVariable('WINDIR') + '\Media\'
  else
    OpenDialog.InitialDir := ExtractFilePath(RenderCompleteSoundFile);
  OpenDialog.Filter := 'Waveform files (*.wav)|*.wav';
  OpenDialog.FileName := '';
  OpenDialog.DefaultExt := 'wav';
  OpenDialog.Title := TextByKey('common-openwav');
  if OpenDialog.Execute then
    txtSoundFile.text := OpenDialog.FileName;
  OpenDialog.Title := ''; // AV: reset caption to default "Open..."
end;

procedure TOptionsForm.btnPlayClick(Sender: TObject);
begin
  if txtSoundFile.text <> '' then
    sndPlaySound(PChar(txtSoundFile.text), SND_FILENAME or SND_ASYNC)
  else
    sndPlaySound(pchar(SND_ALIAS_SYSTEMASTERISK), SND_ALIAS_ID or SND_NOSTOP or SND_ASYNC);
end;

procedure TOptionsForm.btnPluginPathClick(Sender: TObject);
var
  TitleName : string;
  lpItemID : PItemIDList;
  BrowseInfo : TBrowseInfo;
  DisplayName : array[0..MAX_PATH] of char;
  TempPath : array[0..MAX_PATH] of char;
begin
  FillChar(BrowseInfo, sizeof(TBrowseInfo), #0);
  BrowseInfo.hwndOwner := self.Handle;
  BrowseInfo.pszDisplayName := @DisplayName;
  TitleName := TextByKey('options-selectplugins');
  BrowseInfo.lpszTitle := PChar(TitleName);
  BrowseInfo.ulFlags := BIF_RETURNONLYFSDIRS;
  lpItemID := SHBrowseForFolder(BrowseInfo);
  if lpItemId <> nil then begin
    SHGetPathFromIDList(lpItemID, TempPath);
    txtPluginFolder.Text := TempPath;
    GlobalFreePtr(lpItemID);
  end;
end;

procedure TOptionsForm.btnGradientsFileClick(Sender: TObject);
begin
  OpenDialog.Filter := TextByKey('common-filter-gradientfiles') + '|*.gradient;*.ugr|'
                     + TextBykey('common-filter-allfiles') + '|*.*';
  OpenDialog.InitialDir := ExtractFilePath(randGradientFile);
  OpenDialog.FileName := '';
  OpenDialog.DefaultExt := 'ugr';
  if OpenDialog.Execute then
    txtGradientsFile.text := OpenDialog.FileName;
end;

procedure TOptionsForm.shBackgroundMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  pnlBackColorClick(Sender);
end;

procedure TOptionsForm.shCenterLineMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  pnlCenterLineClick(Sender);
end;

procedure TOptionsForm.shThirdsLineMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  pnlThirdsLineClick(Sender);
end;

procedure TOptionsForm.shGRLineMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  pnlGRLineClick(Sender);
end;

procedure TOptionsForm.shGC1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlGridColor1Click(Sender);
end;

procedure TOptionsForm.shGC2MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlGridColor2Click(Sender);
end;

procedure TOptionsForm.shRandBackColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  pnlRandBackColorClick(Sender);
end;

procedure TOptionsForm.shReflectionMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   pnlReflectionCClick(Sender);
end;

procedure TOptionsForm.shRefMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlReferenceClick(Sender);
end;

procedure TOptionsForm.shHelpersMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  pnlHelpersColorClick(Sender);
end;

procedure TOptionsForm.chkRememberLastOpenClick(Sender: TObject);
begin
  if chkRememberLastOpen.Checked then begin
    txtDefParameterFile.Enabled := false;
    txtDefParameterFile.Text := ''; //LastOpenFile;
    btnDefParams.Enabled := false;
  end else begin
    txtDefParameterFile.Enabled := true;
    btnDefParams.Enabled := true;
  end;

  Panel39.Enabled := txtDefParameterFile.Enabled;

  if (Panel39.Enabled) then Panel39.Font.Color := clWindowText
  else Panel39.Font.Color := clGrayText;
end;

procedure TOptionsForm.btnFindDefaultSaveFileClick(Sender: TObject);
begin
  OpenDialog.Filter := TextByKey('common-filter-flamefiles') + '|*.flame|' +
                       TextBykey('common-filter-allfiles') + '|*.*';
  OpenDialog.FileName := '';
  if OpenDialog.Execute then
    txtDefaultSaveFile.Text := OpenDialog.FileName;
end;

procedure TOptionsForm.cbEnableAutosaveClick(Sender: TObject);
begin
  Panel44.Enabled := cbEnableAutoSave.Checked;
  Panel45.Enabled := cbEnableAutoSave.Checked;
  Label49.Enabled := cbEnableAutoSave.Checked;
  if (Panel44.Enabled) then Panel44.Font.Color := clWindowText
  else Panel44.Font.Color := clGrayText;
  if (Panel45.Enabled) then Panel45.Font.Color := clWindowText
  else Panel45.Font.Color := clGrayText;
  txtDefaultSaveFile.Enabled := cbEnableAutoSave.Checked;
  btnFindDefaultSaveFile.Enabled := cbEnableAutoSave.Checked;
  cbFreq.Enabled := cbEnableAutoSave.Checked;
end;

procedure TOptionsForm.btnHelpClick(Sender: TObject);
begin
  OpenDialog.Filter := TextBykey('common-filter-allfiles') + '|*.*';
  OpenDialog.InitialDir := ExtractFilePath(helpPath);
  OpenDialog.FileName := '';
  OpenDialog.DefaultExt := '';
  if OpenDialog.Execute then
    txtHelp.text := OpenDialog.FileName;
end;

procedure TOptionsForm.btnInvertClick(Sender: TObject);
var
  i: integer;
begin
  if VarsPages.ActivePageIndex = 0 then  // AV
    for i := 0 to NRVAR - 1 do
      clbVarEnabled.Checked[i] := not clbVarEnabled.Checked[i]
  else    // AV
    for i := 0 to NRVAR - 1 do
      clbVarFavourite.Checked[i] := not clbVarFavourite.Checked[i];
end;

procedure TOptionsForm.cbGLClick(Sender: TObject);
begin
  pnlCenter.Enabled := cbGL.Checked;
  pnlThirds.Enabled := cbGL.Checked;
  pnlGoldenRatio.Enabled := cbGL.Checked;

  if (pnlCenter.Enabled) then pnlCenter.Font.Color := clWindowText
  else pnlCenter.Font.Color := clGrayText;

  if (pnlThirds.Enabled) then pnlThirds.Font.Color := clWindowText
  else pnlThirds.Font.Color := clGrayText;

  if (pnlGoldenRatio.Enabled) then pnlGoldenRatio.Font.Color := clWindowText
  else pnlGoldenRatio.Font.Color := clGrayText;
end;

procedure TOptionsForm.btnLanguageClick(Sender: TObject);
var fn, fn2, s1, s2:string; i : integer;
begin
  OpenDialog.Filter := 'Extensible Markup Language Files (*.xml)|*.xml';
  OpenDialog.InitialDir := ExtractFilePath(helpPath);
  OpenDialog.FileName := '';
  OpenDialog.DefaultExt := '';
  if OpenDialog.Execute then
  begin
    fn := OpenDialog.FileName; // AV
    fn2 := AppPath + 'Languages\' + ExtractFileName(fn);
    LanguageInfo(fn, s1, s2);
    if s1 <> '' then begin
      if not DirectoryExists(AppPath + 'Languages\') then
        CreateDir(AppPath + 'Languages\');
      if (lowercase(ExtractFilePath(fn)) <> lowercase(AppPath + 'Languages\')) then
        CopyFile(PChar(fn), PChar(fn2), False);
      AvailableLanguages.Add(fn2);
      i := AvailableLanguages.Count - 1;
      if (s2 <> '') then
        s1 := s2 + ' (' + s1 + ')';
      txtLanguageFile.Items.Add(s1);
      txtLanguageFile.ItemIndex := txtLanguageFile.Items.Count - 1;
    end else
      raise Exception.Create(TextByKey('common-invalidformat')); // AV
  end;
end;

procedure TOptionsForm.btnChaoticaClick(Sender: TObject);
var fn: string;
begin
  fn := ChaoticaPath;
  if SelectDirectory(fn, [sdAllowCreate, sdPerformCreate, sdPrompt], 0) then
  begin
    txtChaotica.Text := fn;
    if not FileExists(fn + '\chaotica.exe') then
    begin
      MessageBox(0, PCHAR(TextByKey('main-status-nochaotica')),
            ApophysisSVN, MB_ICONHAND or MB_OK);
      txtChaotica.Text := ChaoticaPath;
      fn := ChaoticaPath;
    end;
  end;
end;

procedure TOptionsForm.chkEnableEditorPreviewClick(Sender: TObject);
begin
  Panel48.Enabled := chkEnableEditorPreview.Checked;
  if chkEnableEditorPreview.Checked then
    Panel48.Font.Color := clWindowText
  else Panel48.Font.Color := clGrayText;
  tbEPTransparency.Enabled := chkEnableEditorPreview.Checked;
end;

procedure TOptionsForm.chkKeepBackgroundClick(Sender: TObject);
begin
  pnlRandBackGround.Enabled := not chkKeepBackground.Checked;
  pnlRandBackColor.Enabled := not chkKeepBackground.Checked;

  if (pnlRandBackGround.Enabled) then
    pnlRandBackGround.Font.Color := clWindowText
  else
    pnlRandBackGround.Font.Color := clGrayText;
end;

end.

