home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / WhiteAnts / PROGRESS.ZIP / MAINFRM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-01-09  |  3.8 KB  |  132 lines

  1. unit MainFrm;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Progress, Buttons, StdCtrls, Gauges, ExtCtrls;
  8.  
  9. {
  10.   This project shows the use of the TProgressor and TProgressObserver classes.
  11.  
  12.   For more details about how to use these classes and examples look through
  13.   the unit DemoOps and this unit MainFrm.
  14. }
  15.  
  16. type
  17.   TMainForm = class(TForm)
  18.     StatusPanel: TPanel;
  19.     ProgressPanel: TPanel;
  20.     Gauge: TGauge;
  21.     ProgressLabel: TLabel;
  22.     StatusLine: TPanel;
  23.     StatusLabel: TLabel;
  24.     ToolBar: TPanel;
  25.     RunBtn: TSpeedButton;
  26.     CoarseObserver: TProgressObserver;
  27.     AbortBtn: TSpeedButton;
  28.     FineObserver: TProgressObserver;
  29.     FineLabel: TLabel;
  30.     procedure CoarseObserverDescriptionChange(Sender: TObject);
  31.     procedure CoarseObserverProgressChange(Sender: TObject);
  32.     procedure CoarseObserverFinish(Sender: TObject);
  33.     procedure CoarseObserverStart(Sender: TObject);
  34.     procedure RunBtnClick(Sender: TObject);
  35.     procedure AbortBtnClick(Sender: TObject);
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure ShowHint(Sender: TObject);
  38.     procedure FineObserverProgressChange(Sender: TObject);
  39.   private
  40.   public
  41.   end;
  42.  
  43. var
  44.   MainForm: TMainForm;
  45.  
  46. implementation
  47.  
  48. uses DemoOps;
  49.  
  50. {$R *.DFM}
  51.  
  52. procedure TMainForm.CoarseObserverDescriptionChange(Sender: TObject);
  53. begin
  54.   { Use the class function TProgressor.Instance to read the (changed) description
  55.     We could also have called one of the observer's Description properties
  56.     which simply wrap the TProgressor's description property }
  57.   ProgressLabel.Caption := TProgressor.Instance.Description;
  58. end;
  59.  
  60. procedure TMainForm.CoarseObserverProgressChange(Sender: TObject);
  61. begin
  62.   { Here we use one of the Observers to read the (changed) progress which
  63.     wrap the TProgressor's Progress property.
  64.     Also demonstrate the Accuracy property of the observers, the
  65.     gauge is updated with accuracy of 10 percent }
  66.  
  67.   Gauge.Progress := CoarseObserver.Progress;
  68. end;
  69.  
  70. procedure TMainForm.CoarseObserverFinish(Sender: TObject);
  71. begin
  72.   { Simply make the complete panel invisible when progress has finished.
  73.     The align properties of the panels will take care of the rest }
  74.   ProgressPanel.Visible := False;
  75. end;
  76.  
  77. procedure TMainForm.CoarseObserverStart(Sender: TObject);
  78. begin
  79.   { Simply make the complete panel visible when progress starts.
  80.     The align properties of the panels will take care of the rest }
  81.   ProgressPanel.Visible := True;
  82. end;
  83.  
  84. procedure TMainForm.RunBtnClick(Sender: TObject);
  85. begin
  86.   { avoid re-entrancy due to calls to Application.ProcessMessages by
  87.     disabling the RunBtn while running.
  88.     Protect this state with a try finally statement to make sure
  89.     the RunBtn get's enabled again after the operation finished OR
  90.     an exception was raised }
  91.   RunBtn.Enabled := False;
  92.   AbortBtn.Enabled := True;
  93.   try
  94.  
  95.     Application.ProcessMessages;
  96.  
  97.     { Simulate action }
  98.     LengthyOperation;
  99.  
  100.   finally
  101.     { make sure to return to normal state again }
  102.     RunBtn.Enabled := True;
  103.     AbortBtn.Enabled := False;
  104.   end;
  105. end;
  106.  
  107. procedure TMainForm.AbortBtnClick(Sender: TObject);
  108. begin
  109.   { operation is aborted. The operations check the state of Aborted
  110.     The progressor sets Aborted to False the first time StartProgress is called }
  111.   CoarseObserver.Aborted := True;
  112. end;
  113.  
  114. procedure TMainForm.FormCreate(Sender: TObject);
  115. begin
  116.   { Show hint in status line }
  117.   Application.OnHint := ShowHint;
  118.   ShowHint(Self);
  119. end;
  120.  
  121. procedure TMainForm.ShowHint(Sender: TObject);
  122. begin
  123.   StatusLabel.Caption := Application.Hint;
  124. end;
  125.  
  126. procedure TMainForm.FineObserverProgressChange(Sender: TObject);
  127. begin
  128.   FineLabel.Caption := IntToStr(FineObserver.Progress);
  129. end;
  130.  
  131. end.
  132.