{
********************************************************************************

SPackGui
Copyright (C) 2013 Geoffray Levasseur <geoffray.levasseurbrandin@numericable.fr>.
Copyright (C) <date> <add your name and mail address here>

http://www.geoffray-levasseur.org/
http://0.tuxfamilly.org/

This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software.  You can  use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability.

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and,  more generally, to use and operate it in the
same conditions as regards security.

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.

********************************************************************************

Description:
  configuration dialog for command line and network parameters

********************************************************************************
}
unit frmDisplaySettings;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  Buttons, ExtCtrls, StdCtrls, ColorBox;

type

  { TfDisplaySettings }

  TfDisplaySettings = class(TForm)
    btnCancel: TBitBtn;
    btnOk: TBitBtn;
    ckbNoColors: TCheckBox;
    ckbDisplayGrid: TCheckBox;
    clbUpdatablePackageBackground: TColorBox;
    clbPackageToRemoveBackground: TColorBox;
    clbUpdatablePackageFont: TColorBox;
    clbOutdatedPackageFont: TColorBox;
    clbOutdatedPackageBackground: TColorBox;
    clbPackageToInstallFont: TColorBox;
    clbPackageToInstallBackground: TColorBox;
    clbPackageToUpgradeFont: TColorBox;
    clbPackageToUpgradeBackground: TColorBox;
    clbPackageToRemoveFont: TColorBox;
    edDecimalSeparator: TEdit;
    edThousandsSeparator: TEdit;
    gbByteUnits: TGroupBox;
    gbNumbers: TGroupBox;
    gbListView: TGroupBox;
    lbDecimalSeparator: TLabel;
    lbPackageToInstall: TLabel;
    lbPackageToUpgradeFont: TLabel;
    lbPackageToUpgradeBackground: TLabel;
    lbPackageToUpgrade: TLabel;
    lbPackageToRemoveFont: TLabel;
    lbPackageToRemoveBackground: TLabel;
    lbPackageToRemove: TLabel;
    lbThousandsSeparator: TLabel;
    lbUpdatablePackage: TLabel;
    lbUpdatablePackageFont: TLabel;
    lbUpdatablePackageBackground: TLabel;
    lbOutdatedPackage: TLabel;
    lbOutdatedPackageFont: TLabel;
    lbOutdatedPackageBackground: TLabel;
    lbPackageToInstallFont: TLabel;
    lbPackageToInstallBackground: TLabel;
    lbUnitExample: TLabel;
    PageControl: TPageControl;
    rbMetricUnits: TRadioButton;
    rbIECUnits: TRadioButton;
    tsColors: TTabSheet;
    tsGeneral: TTabSheet;
    procedure btnOkClick(Sender: TObject);
    procedure edDecimalSeparatorChange(Sender: TObject);
    procedure edThousandsSeparatorChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure rbIECUnitsChange(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  fDisplaySettings: TfDisplaySettings;

implementation

{$R *.lfm}

uses
  uIconManager, uCommon, uStrings, uListViewColors, uDebug;

{ TfDisplaySettings }

procedure TfDisplaySettings.edThousandsSeparatorChange(Sender: TObject);
begin
  edThousandsSeparator.SelectAll;
end;

procedure TfDisplaySettings.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  try
    SaveWindowGeometry(Self);
  except
    PrintLnDbg(rsErrorCannotSaveConf, vlLow);
  end;
end;

procedure TfDisplaySettings.btnOkClick(Sender: TObject);
begin
  bUnitsAreDecimal := rbIECUnits.Checked = False;
  if edThousandsSeparator.Text <> '' then
    cThousandSep := edThousandsSeparator.Text[1]
  else
    cThousandSep := #0;
  cDecimalSep := edDecimalSeparator.Text[1];
  bShowGrig := ckbDisplayGrid.Checked;
  bNoColors := ckbNoColors.Checked;
  clUpdatablePackageFont := clbUpdatablePackageFont.Selected;
  clUpdatablePackageBack := clbUpdatablePackageBackground.Selected;
  clOutdatedPackageFont := clbOutdatedPackageFont.Selected;
  clOutdatedPackageBack := clbOutdatedPackageBackground.Selected;
  clPackageToInstallFont := clbPackageToInstallFont.Selected;
  clPackageToInstallBack := clbPackageToInstallBackground.Selected;
  clPackageToUpgradeFont := clbPackageToUpgradeFont.Selected;
  clPackageToUpgradeBack := clbPackageToUpgradeBackground.Selected;
  clPackageToRemoveFont := clbPackageToRemoveFont.Selected;
  clPackageToRemoveBack := clbPackageToRemoveBackground.Selected;
  SaveColors;
  iniMain.WriteInteger(rsConfSectionDisplay, rsConfNameThousandsSep,
    Ord(cThousandSep));
  iniMain.WriteInteger(rsConfSectionDisplay, rsConfNameDecimalSep,
    Ord(cDecimalSep));
  iniMain.WriteBool(rsConfSectionDisplay, rsConfNameDecimalUnits,
    bUnitsAreDecimal);
  iniMain.WriteBool(rsConfSectionDisplay, rsConfNameShowGrid, bShowGrig);
  iniMain.WriteBool(rsConfSectionDisplay, rsConfNameNoColors, bNoColors);
end;

procedure TfDisplaySettings.edDecimalSeparatorChange(Sender: TObject);
begin
  btnOk.Enabled := edDecimalSeparator.Text <> '';
  edDecimalSeparator.SelectAll;
end;

procedure TfDisplaySettings.FormCreate(Sender: TObject);
begin
  LoadWindowGeometry(Self);
  btnOk.Tag := icOk;
  btnCancel.Tag := icCancel;
  InitBtnGlyphs(Self);
  PageControl.ActivePageIndex := 0;
  if not bUnitsAreDecimal then
    rbIECUnits.Checked := True
  else
    rbMetricUnits.Checked := True;
  edThousandsSeparator.Text := cThousandSep;
  edDecimalSeparator.Text := cDecimalSep;
  ckbDisplayGrid.Checked := bShowGrig;
  ckbNoColors.Checked := bNoColors;
  clbUpdatablePackageFont.Selected := clUpdatablePackageFont;
  clbUpdatablePackageBackground.Selected := clUpdatablePackageBack;
  clbOutdatedPackageFont.Selected := clOutdatedPackageFont;
  clbOutdatedPackageBackground.Selected := clOutdatedPackageBack;
  clbPackageToInstallFont.Selected := clPackageToInstallFont;
  clbPackageToInstallBackground.Selected := clPackageToInstallBack;
  clbPackageToUpgradeFont.Selected := clPackageToUpgradeFont;
  clbPackageToUpgradeBackground.Selected := clPackageToUpgradeBack;
  clbPackageToRemoveFont.Selected := clPackageToRemoveFont;
  clbPackageToRemoveBackground.Selected := clPackageToRemoveBack;
  rbIECUnitsChange(nil);
end;

procedure TfDisplaySettings.rbIECUnitsChange(Sender: TObject);
var
  S: string;
begin
  if rbIECUnits.Checked then
    S := IntToStr(5000 div 1024) + cDecimalSep +
      Copy(IntToStr(5000 mod 1024), 1, 2) + ' ' + rsKiB
  else
    S := IntToStr(5000 div 1000) + ' ' + rsKB;
  lbUnitExample.Caption := Format(rsUnitExample, [S]);;
end;

end.

