unit U_Animate;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, U_BaseCommon, U_Common, RzPrgres, siComp,
  siLngLnk;


type
  TF_Animate = class(TForm)
    ProgressBar: TProgressBar;
    Animate: TAnimate;
    StatusBar: TStatusBar;
    pbState: TRzProgressBar;
    Timer_Stop: TTimer;
    lng_Forms: TsiLangLinked;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormHide(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer_StopTimer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FCurrPos: Integer;
  protected
    FLockedFormHandles: TList;
  public
    { Public declarations }

    //GAnimateInclude : TAnimateInclude;
    GMaxProgressPos: Integer;

    procedure LockVisibleForms;
    procedure UnLockForms;
    procedure StartAnimate(ACapt: String; ACommonAVI: TCommonAVI;
                AAnimateInclude: TAnimateInclude; AAsModal: Boolean = false);
    procedure StartProgress(ACapt: String; ACommonAVI: TCommonAVI; AMaxPos: Integer);
    procedure SetProgressPos(APos: integer);
    procedure SetStatusDir(AStatusDir: String);
    procedure SetStatusCompon(AStatusCompon: String);
    procedure StepProgress;
    procedure StopAnimate;
  end;
{
var
  F_Animate: TF_Animate;}

implementation

{$R *.dfm}

{ TF_Animate }


procedure TF_Animate.FormCreate(Sender: TObject);
begin
  FLockedFormHandles := TList.Create;

  GMaxProgressPos := -1;
end;

procedure TF_Animate.LockVisibleForms;
var
  i: integer;
  Form: TCustomForm;
begin
  FLockedFormHandles.Clear;
  for i := 0 to Screen.CustomFormCount - 1 do
  begin
    Form := Screen.CustomForms[i];
    if Form is TForm then
      //if Form.ClassName = 'TFSCS_Main' then
      //if Form.ClassName <> 'TF_MakeEditCrossConnection' then
      if Form <> Self then
        if TForm(Form).Visible then
          if IsWindowEnabled(Form.Handle) then
          begin
            FLockedFormHandles.Add(Pointer(Form.Handle));
            EnableWindow(Form.Handle, False);
          end;
  end;
end;

procedure TF_Animate.UnLockForms;
var
  i: Integer;
begin
  for i := FLockedFormHandles.Count - 1 downto 0 do
    EnableWindow(HWnd(FLockedFormHandles[i]), true);
  //for i := 0 to FLockedFormHandles.Count - 1 do
  //  EnableWindow(HWnd(FLockedFormHandles[i]), true);

  FLockedFormHandles.Clear;
end;

procedure TF_Animate.StartAnimate(ACapt: String; ACommonAVI: TCommonAVI;
  AAnimateInclude: TAnimateInclude; AAsModal: Boolean = false);
begin
  Caption := ACapt;
  Animate.CommonAVI := ACommonAVI;
  Animate.Active := true;

  pbState.Visible := false;
  ProgressBar.Visible := false;
  StatusBar.Visible := false;

  pbState.Percent := 0;

  Screen.Cursor := crHourGlass;
  Application.ProcessMessages;
  if AAsModal then
    ShowModal
  else
    Show;

  case AAnimateInclude of
    aiProgressBar: pbState.Show; //ProgressBar.Show;
    aiStatusBar  : StatusBar.Show;
  end;


  ProgressBar.Update;
  StatusBar.Update;

  LockVisibleForms;
  Application.ProcessMessages;
end;

procedure TF_Animate.StartProgress(ACapt: String; ACommonAVI: TCommonAVI; AMaxPos: Integer);
begin
  Caption := ACapt;
  Animate.CommonAVI := ACommonAVI;
  Animate.Active := true;

  pbState.Visible := false;
  ProgressBar.Visible := false;
  StatusBar.Visible := false;

  pbState.Percent := 0;
  FCurrPos := 0;
  GMaxProgressPos := AMaxPos;

  Screen.Cursor := crHourGlass;
  ProcessMessagesEx;

  Show;
  pbState.Show;

  ProcessMessagesEx;
end;

procedure TF_Animate.SetProgressPos(APos: integer);
begin
  APos := Round(APos / GMaxProgressPos * 100);
  if APos > 100 then
    APos := 100;
  pbState.Percent := APos;

  //ProgressBar.Position := APos;
  Application.ProcessMessages;
end;



procedure TF_Animate.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Animate.Active := false;
    ProgressBar.Position := 0;
    StatusBar.Panels[0].Text := '';
    StatusBar.Panels[1].Text := '';
    Screen.Cursor := crDefault;
end;


procedure TF_Animate.FormHide(Sender: TObject);
begin
    // ##### При Следующем вsзове, Отображать эту форму в средине Главной #####
    //Position := poMainFormCenter;
end;

procedure TF_Animate.SetStatusCompon(AStatusCompon: String);
begin
    StatusBar.Panels[1].Text := AStatusCompon;
    StatusBar.Update;
end;

procedure TF_Animate.SetStatusDir(AStatusDir: String);
begin
    StatusBar.Panels[0].Text := AStatusDir;
    StatusBar.Update;
end;

procedure TF_Animate.StepProgress;
begin
  Inc(FCurrPos);
  pbState.Percent := Round(FCurrPos / GMaxProgressPos * 100);
  ProcessMessagesEx;
end;


procedure TF_Animate.StopAnimate;
begin
  try
    pbState.Percent := 100;
    Timer_Stop.Enabled := true;
    Application.ProcessMessages;
    Sleep(Timer_Stop.Interval);

    UnLockForms;
  finally
    UnLockForms;
  end;
end;

procedure TF_Animate.Timer_StopTimer(Sender: TObject);
begin
  TTimer(Sender).Enabled := false;
  Close;
end;

procedure TF_Animate.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FLockedFormHandles);
end;

end.
