(* (c) Copyright 2000 - Holger Dors
   =======================================

   This file is part of RazorLame,
   a front-end for the LAME MP3 Encoder.

   See license.txt for details.             *)

unit progress;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ExtCtrls, Menus;

type
  TFormProgress = class(TForm)
    PanelStatus: TPanel;
    LabelStatus: TLabel;
    LabelStatusRemaining: TLabel;
    LabelBatchRemaining: TLabel;
    LabelBatch: TLabel;
    LabelWorking: TLabel;
    LabelOutput: TLabel;
    ProgressBarStatus: TProgressBar;
    ProgressBarBatch: TProgressBar;
    PanelButtons: TPanel;
    ButtonCancel: TButton;
    CheckBoxShutdown: TCheckBox;
    LabelStopWhenDone: TLabel;
    ButtonTray: TButton;
    LabelBatchElapsed: TLabel;
    LabelBatchEstimated: TLabel;
    LabelRemainingLabel: TLabel;
    LabelElapsedLabel: TLabel;
    LabelestimatedLabel: TLabel;
    LabelStatusRemainingLabel: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure ButtonTrayClick(Sender: TObject);
  private
    { Private declarations }
    procedure ResetFormControls;
    procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;
  public
    { Public declarations }
    ExternalClosed: Boolean;
  end;

var
  FormProgress: TFormProgress;

implementation

{$R *.DFM}

uses UtilFuncs, Globals, ResStr, Main, About;

procedure TFormProgress.ResetFormControls;
begin
  ProgressBarStatus.Position := 0;
  ProgressBarBatch.Position := 0;
  LabelStatus.Caption := ID_STATUS + '0%';
  LabelBatch.Caption := ID_BATCH + '0%';
  LabelStatusRemaining.Caption := '';
  LabelBatchRemaining.Caption := '';
  LabelBatchElapsed.Caption := '';
  LabelBatchEstimated.Caption := '';
  LabelWorking.Caption := Format(ID_WORKING, [0, 0, '']);
  LabelOutput.Caption := ID_OUTPUT;
  LabelStopWhenDone.Visible := false;
end;

procedure TFormProgress.FormCreate(Sender: TObject);
begin
  //-- do some init stuff for controls on the form
  ProgressBarStatus.Max := 1000;
  ProgressBarBatch.Max := 1000;
  ResetFormControls;
  ExternalClosed := false;
end;

procedure TFormProgress.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
var
  liResult: Integer;
begin
  CanClose := true;
  if not ExternalClosed then
  begin
    liResult := MyMsgDlg(MSG_ABORTENCODING, MSG_QUESTION, mtConfirmation,
      mbYesNoCancel, [BTN_NOW, BTN_DONE]);

    CanClose := (liResult = ID_YES);
    Global.StopWhenDone := (liResult = ID_NO);

    LabelStopWhenDone.Visible := Global.StopWhenDone;

    //-- if batch was through by the time the user answered
    //-- the messagebox, close the form anyway,
    //-- no matter what the user answered to the question
    if ExternalClosed then
    begin
      CanClose := true;
      ModalResult := mrCancel;
    end;
  end;
end;

procedure TFormProgress.ButtonTrayClick(Sender: TObject);
begin
  Self.Hide;
  FormMain.Hide;
  ShowWindow(Application.Handle, SW_HIDE);
  FormMain.TrayIcon.Active := true;
end;

procedure TFormProgress.WMSysCommand(var Message: TMessage);
begin
  if Message.wParam = SC_MINIMIZE then
    ShowWindow(Application.Handle, SW_MINIMIZE)
  else
    inherited;
end;

end.

