<commit_before>{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2014 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    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.

    Original author: <NAME>

    Description:
      Defines a ToggleBox control. A Checkbox like control that has an
      animated bar that slides side to side when toggled.
}
unit fpg_toggle;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_main,
  fpg_stylemanager,
  fpg_checkbox;

type

  TfpgToggle = class(TfpgCheckBox)
  private
    FCheckedTextColor: TfpgColor;
    FToggleWidth: TfpgCoord;
    FToggleButtonWidth: TfpgCoord;
    FAnimateTimer: TfpgTimer;
    FCheckedCaption: TfpgString;
    FCheckedColor: TfpgColor;
    FSliderPosition: TfpgCoord;
    FPaintedSliderPosition: TfpgCoord;
    FUnCheckedCaption: TfpgString;
    FUnCheckedColor: TfpgColor;
    FUnCheckedTextColor: TfpgColor;
    FUseAnimation: Boolean;
    procedure   SetCheckedCaption(AValue: TfpgString);
    procedure   SetCheckedColor(AValue: TfpgColor);
    procedure   SetCheckedTextColor(AValue: TfpgColor);
    procedure   SetToggleWidth(AValue: TfpgCoord);
    procedure   SetUnCheckedCaption(AValue: TfpgString);
    procedure   SetUnCheckedColor(AValue: TfpgColor);
    procedure   AnimateTimer(Sender: TObject);
    procedure   SetUnCheckedTextColor(AValue: TfpgColor);
    function    ToggleLeft: TfpgCoord; inline;
  protected
    procedure   HandlePaint; override;
    procedure   HandleCheckChanged; override;
    procedure   HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
  published
    property    UseAnimation: Boolean read FUseAnimation write FUseAnimation;
    property    ToggleWidth: TfpgCoord read FToggleWidth write SetToggleWidth default 45;
    property    CheckedCaption : TfpgString read FCheckedCaption write SetCheckedCaption;
    property    CheckedColor: TfpgColor read FCheckedColor write SetCheckedColor default clLime;
    property    CheckedTextColor: TfpgColor read FCheckedTextColor write SetCheckedTextColor default clHilite2;
    property    UnCheckedCaption: TfpgString read FUnCheckedCaption write SetUnCheckedCaption;
    property    UnCheckedColor: TfpgColor read FUnCheckedColor write SetUnCheckedColor default clWindowBackground;
    property    UnCheckedTextColor: TfpgColor read FUnCheckedTextColor write SetUnCheckedTextColor default clText1;
  end;

implementation

{ TfpgToggle }

procedure TfpgToggle.SetCheckedColor(AValue: TfpgColor);
begin
  if FCheckedColor=AValue then Exit;
  FCheckedColor:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetCheckedTextColor(AValue: TfpgColor);
begin
  if FCheckedTextColor=AValue then Exit;
  FCheckedTextColor:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetToggleWidth(AValue: TfpgCoord);
begin
  if FToggleWidth=AValue then Exit;
  FToggleWidth:=AValue;
  FToggleButtonWidth:=AValue - 10;
  Invalidate;
end;

procedure TfpgToggle.SetCheckedCaption(AValue: TfpgString);
begin
  if FCheckedCaption=AValue then Exit;
  FCheckedCaption:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetUnCheckedCaption(AValue: TfpgString);
begin
  if FUnCheckedCaption=AValue then Exit;
  FUnCheckedCaption:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetUnCheckedColor(AValue: TfpgColor);
begin
  if FUnCheckedColor=AValue then Exit;
  FUnCheckedColor:=AValue;
  Invalidate;
end;

procedure TfpgToggle.AnimateTimer(Sender: TObject);
begin
  if csDestroying in ComponentState then
    Exit;
  if not Checked then
  begin // not checked
    Dec(FSliderPosition, 1);
    if FSliderPosition < 1 then
      FSliderPosition:=0;
  end
  else  // checked
  begin
    Inc(FSliderPosition);
    if FSliderPosition >= FToggleWidth - FToggleButtonWidth -2then
      FSliderPosition := FToggleWidth - FToggleButtonWidth -2;
  end;
  Invalidate;
end;

procedure TfpgToggle.SetUnCheckedTextColor(AValue: TfpgColor);
begin
  if FUnCheckedTextColor=AValue then Exit;
  FUnCheckedTextColor:=AValue;
  Invalidate;
end;

function TfpgToggle.ToggleLeft: TfpgCoord;
begin
  if BoxLayout = tbLeftBox then
    Result := 1
  else
    Result := Width - FToggleWidth;
end;

procedure TfpgToggle.HandlePaint;
var
  ToggleText: TfpgString;
  PaintColor: TFPColor;
  TextEnabled: TfpgTextFlags;
  BvlWdth: TfpgCoord;
  ButtonRect: TfpgRect;
begin
  Canvas.Clear(BackgroundColor);

  // Text

  if Enabled then
    TextEnabled := []
  else
    TextEnabled := [txtDisabled];

  BvlWdth := fpgStyleManager.Style.GetBevelWidth;
<commit_msg>toggle: fixes minor bug where selected font isn't used.<commit_after>{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2014 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    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.

    Original author: <NAME>

    Description:
      Defines a ToggleBox control. A Checkbox like control that has an
      animated bar that slides side to side when toggled.
}
unit fpg_toggle;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_main,
  fpg_stylemanager,
  fpg_checkbox;

type

  TfpgToggle = class(TfpgCheckBox)
  private
    FCheckedTextColor: TfpgColor;
    FToggleWidth: TfpgCoord;
    FToggleButtonWidth: TfpgCoord;
    FAnimateTimer: TfpgTimer;
    FCheckedCaption: TfpgString;
    FCheckedColor: TfpgColor;
    FSliderPosition: TfpgCoord;
    FPaintedSliderPosition: TfpgCoord;
    FUnCheckedCaption: TfpgString;
    FUnCheckedColor: TfpgColor;
    FUnCheckedTextColor: TfpgColor;
    FUseAnimation: Boolean;
    procedure   SetCheckedCaption(AValue: TfpgString);
    procedure   SetCheckedColor(AValue: TfpgColor);
    procedure   SetCheckedTextColor(AValue: TfpgColor);
    procedure   SetToggleWidth(AValue: TfpgCoord);
    procedure   SetUnCheckedCaption(AValue: TfpgString);
    procedure   SetUnCheckedColor(AValue: TfpgColor);
    procedure   AnimateTimer(Sender: TObject);
    procedure   SetUnCheckedTextColor(AValue: TfpgColor);
    function    ToggleLeft: TfpgCoord; inline;
  protected
    procedure   HandlePaint; override;
    procedure   HandleCheckChanged; override;
    procedure   HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
  published
    property    UseAnimation: Boolean read FUseAnimation write FUseAnimation;
    property    ToggleWidth: TfpgCoord read FToggleWidth write SetToggleWidth default 45;
    property    CheckedCaption : TfpgString read FCheckedCaption write SetCheckedCaption;
    property    CheckedColor: TfpgColor read FCheckedColor write SetCheckedColor default clLime;
    property    CheckedTextColor: TfpgColor read FCheckedTextColor write SetCheckedTextColor default clHilite2;
    property    UnCheckedCaption: TfpgString read FUnCheckedCaption write SetUnCheckedCaption;
    property    UnCheckedColor: TfpgColor read FUnCheckedColor write SetUnCheckedColor default clWindowBackground;
    property    UnCheckedTextColor: TfpgColor read FUnCheckedTextColor write SetUnCheckedTextColor default clText1;
  end;

implementation

{ TfpgToggle }

procedure TfpgToggle.SetCheckedColor(AValue: TfpgColor);
begin
  if FCheckedColor=AValue then Exit;
  FCheckedColor:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetCheckedTextColor(AValue: TfpgColor);
begin
  if FCheckedTextColor=AValue then Exit;
  FCheckedTextColor:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetToggleWidth(AValue: TfpgCoord);
begin
  if FToggleWidth=AValue then Exit;
  FToggleWidth:=AValue;
  FToggleButtonWidth:=AValue - 10;
  Invalidate;
end;

procedure TfpgToggle.SetCheckedCaption(AValue: TfpgString);
begin
  if FCheckedCaption=AValue then Exit;
  FCheckedCaption:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetUnCheckedCaption(AValue: TfpgString);
begin
  if FUnCheckedCaption=AValue then Exit;
  FUnCheckedCaption:=AValue;
  Invalidate;
end;

procedure TfpgToggle.SetUnCheckedColor(AValue: TfpgColor);
begin
  if FUnCheckedColor=AValue then Exit;
  FUnCheckedColor:=AValue;
  Invalidate;
end;

procedure TfpgToggle.AnimateTimer(Sender: TObject);
begin
  if csDestroying in ComponentState then
    Exit;
  if not Checked then
  begin // not checked
    Dec(FSliderPosition, 1);
    if FSliderPosition < 1 then
      FSliderPosition:=0;
  end
  else  // checked
  begin
    Inc(FSliderPosition);
    if FSliderPosition >= FToggleWidth - FToggleButtonWidth -2then
      FSliderPosition := FToggleWidth - FToggleButtonWidth -2;
  end;
  Invalidate;
end;

procedure TfpgToggle.SetUnCheckedTextColor(AValue: TfpgColor);
begin
  if FUnCheckedTextColor=AValue then Exit;
  FUnCheckedTextColor:=AValue;
  Invalidate;
end;

function TfpgToggle.ToggleLeft: TfpgCoord;
begin
  if BoxLayout = tbLeftBox then
    Result := 1
  else
    Result := Width - FToggleWidth;
end;

procedure TfpgToggle.HandlePaint;
var
  ToggleText: TfpgString;
  PaintColor: TFPColor;
  TextEnabled: TfpgTextFlags;
  BvlWdth: TfpgCoord;
  ButtonRect: TfpgRect;
begin
  Canvas.Clear(BackgroundColor);

  // Text
  Canvas.SetFont(Font);
  if Enabled then
    TextEnabled := []
  else
    TextEnabled := [txtDisabled];

  BvlWdth := fpgStyleManager.Style.GetBevelWidth;

{'fraction_non_alphanumeric': 0.0622154779969651, 'fraction_numerical': 0.004046535154274153, 'mean_word_length': 4.519821328866555, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6164009', 'n_tokens_mistral': 3200, 'n_tokens_neox': 3111, 'n_words': 827}