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

  1. {
  2. +----------------------------------------------------------------------------+
  3. |                                      ⌐  ⌐                                  |
  4. |                                    ⌐⌐ ⌐ ⌐ ⌐                                |
  5. |                                 ⌐⌐⌐ ⌐   ⌐  ⌐                               |
  6. |                                 ⌐⌐    ⌐ ⌐   ⌐                              |
  7. |                  ⌐             ⌐⌐     ⌐  ⌐                                 |
  8. |                 ⌐ ⌐            ⌐⌐⌐    ⌐⌐  ⌐                                |
  9. |             ⌐⌐  ⌐  ⌐      ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐  ⌐                                    |
  10. |            ⌐  ⌐⌐  ⌐⌐      ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐                                  |
  11. |            ⌐ ⌐⌐⌐ ⌐⌐⌐⌐⌐⌐ ⌐⌐⌐ ⌐   ⌐⌐⌐⌐⌐⌐⌐⌐                                   |
  12. |           ⌐ ⌐⌐ ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐ ⌐   ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐      Copyright ⌐ 1996-1997 by:  |
  13. |           ⌐ ⌐⌐⌐⌐⌐⌐ ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐  ⌐ ⌐⌐⌐⌐⌐ ⌐⌐                                 |
  14. |          ⌐ ⌐⌐⌐⌐⌐⌐⌐   ⌐⌐⌐⌐⌐ ⌐⌐⌐⌐    ⌐⌐ ⌐⌐ ⌐      WHITE ANTS SYSTEMHOUSE BV  |
  15. |         ⌐  ⌐⌐⌐⌐⌐⌐⌐ ⌐⌐⌐ ⌐⌐⌐ ⌐⌐ ⌐       ⌐⌐⌐⌐      Geleen 12                  |
  16. |         ⌐ ⌐⌐⌐⌐⌐⌐⌐    ⌐   ⌐⌐   ⌐⌐⌐       ⌐       8032 GB Zwolle             |
  17. |           ⌐⌐⌐⌐⌐⌐     ⌐            ⌐ ⌐           Netherlands                |
  18. |      ⌐⌐⌐  ⌐⌐⌐⌐⌐      ⌐     ⌐⌐     ⌐  ⌐                                     |
  19. |            ⌐⌐       ⌐              ⌐  ⌐⌐⌐ ⌐     Tel. +31 38 453 86 31      |
  20. |      ⌐              ⌐              ⌐            Fax. +31 38 453 41 22      |
  21. |      ⌐             ⌐               ⌐⌐                                      |
  22. |    ⌐              ⌐                  ⌐⌐         www.whiteants.com          |
  23. |  ⌐⌐              ⌐                     ⌐ ⌐      support@whiteants.com      |
  24. |                 ⌐                                                          |
  25. +----------------------------------------------------------------------------+
  26.   file     : Progress
  27.   version  : 1.0
  28.   comment  : 
  29.   date     : 09-01-97
  30.   time     : 12:30:32
  31.   author   : G. Beuze
  32.   compiler : Delphi 1.0
  33. +----------------------------------------------------------------------------+
  34. | DISCLAIMER:                                                                |
  35. | THIS SOURCE IS FREEWARE. YOU ARE ALLOWED TO USE IT IN YOUR OWN PROJECTS    |
  36. | WITHOUT ANY RESTRICTIONS. YOU ARE NOT ALLOWED TO SELL THE SOURCE CODE.     |
  37. | THERE IS NO WARRANTY AT ALL - YOU USE IT ON YOUR OWN RISC. WHITE ANTS DOES |
  38. | NOT ASSUME ANY RESPONSIBILITY FOR ANY DAMAGE OR ANY LOSS OF TIME OR MONEY  |
  39. | DUE THE USE OF ANY PART OF THIS SOURCE CODE.                               |
  40. +----------------------------------------------------------------------------+
  41. }
  42.  
  43. unit Progress;
  44.  
  45. interface
  46.  
  47. uses Classes, SysUtils, IntLists, Controls;
  48.  
  49. type
  50.   TProgressObserver = class;
  51.     
  52.   TProgressor = class (TObject)
  53.   private
  54.     FAborted: Boolean;
  55.     FBusyCursor: TCursor;
  56.     FCursorStack: TIntList;
  57.     FDescription: PString;
  58.     FDescrStack: TStrings;
  59.     FObservers: TList;
  60.     FOrgCursor: TCursor;
  61.     FProgress: Integer;
  62.     FProgressBusy: Boolean;
  63.     FProgressStack: TIntList;
  64.   protected
  65.     constructor CreateInstance;
  66.     procedure DescriptionChange;
  67.     function GetAborted: Boolean;
  68.     function GetDescription: String;
  69.     procedure PopProgress;
  70.     procedure ProgressChange;
  71.     procedure ProgressEnd;
  72.     procedure ProgressStart;
  73.     procedure PushProgress;
  74.     procedure SetBusyCursor(Value: TCursor);
  75.     procedure SetDescription(const Value: String);
  76.     procedure SetProgress(Value: Integer);
  77.   public
  78.     constructor Create;
  79.     destructor Destroy; override;
  80.     procedure Abort;
  81.     procedure EndProgress;
  82.     class function Instance: TProgressor;
  83.     procedure RegisterObserver(Observer: TProgressObserver);
  84.     procedure StartProgress(AProgress: Integer; const ADescription: String);
  85.     procedure StartProgressDef;
  86.     procedure UnregisterObserver(Observer: TProgressObserver);
  87.     property Aborted: Boolean read GetAborted write FAborted;
  88.     property BusyCursor: TCursor read FBusyCursor write SetBusyCursor;
  89.     property Description: String read GetDescription write SetDescription;
  90.     property Progress: Integer read FProgress write SetProgress;
  91.   end;
  92.  
  93.   TProgressObserver = class (TComponent)
  94.   private
  95.     FAccuracy: Integer;
  96.     FEnabled: Boolean;
  97.     FLastProgress: Integer;
  98.     FOnDescriptionChange: TNotifyEvent;
  99.     FOnFinish: TNotifyEvent;
  100.     FOnProgressChange: TNotifyEvent;
  101.     FOnStart: TNotifyEvent;
  102.   protected
  103.     procedure DescriptionChange;
  104.     function GetAborted: Boolean;
  105.     function GetBusyCursor: TCursor;
  106.     function GetDescription: String;
  107.     function GetProgress: Integer;
  108.     function GetProgressor: TProgressor;
  109.     procedure ProgressChange;
  110.     procedure ProgressEnd;
  111.     procedure ProgressStart;
  112.     procedure SetAborted(Value: Boolean);
  113.     procedure SetAccuracy(Value: Integer);
  114.     procedure SetBusyCursor(Value: TCursor);
  115.     procedure SetDescription(Value: String);
  116.     procedure SetProgress(Value: Integer);
  117.     property Progressor: TProgressor read GetProgressor;
  118.   public
  119.     constructor Create(AOwner: TComponent); override;
  120.     destructor Destroy; override;
  121.     procedure Abort;
  122.     procedure EndProgress;
  123.     procedure StartProgress(AProgress: Integer; const ADescription: String);
  124.     procedure StartProgressDef;
  125.     property Aborted: Boolean read GetAborted write SetAborted;
  126.     property Description: String read GetDescription write SetDescription;
  127.     property Progress: Integer read GetProgress write SetProgress default 0;
  128.   published
  129.     property Accuracy: Integer read FAccuracy write SetAccuracy default 5;
  130.     property BusyCursor: TCursor read GetBusyCursor write SetBusyCursor default 
  131.         crHourGlass;
  132.     property Enabled: Boolean read FEnabled write FEnabled default True;
  133.     property OnDescriptionChange: TNotifyEvent read FOnDescriptionChange write 
  134.         FOnDescriptionChange;
  135.     property OnFinish: TNotifyEvent read FOnFinish write FOnFinish;
  136.     property OnProgressChange: TNotifyEvent read FOnProgressChange write 
  137.         FOnProgressChange;
  138.     property OnStart: TNotifyEvent read FOnStart write FOnStart;
  139.   end;
  140.  
  141.  
  142. procedure Register;
  143.  
  144. implementation
  145.  
  146. uses StrUtils, NumUtils, Forms;
  147.  
  148. procedure Register;
  149. begin
  150.   RegisterComponents('White Ants', [TProgressObserver]);
  151. end;
  152.  
  153. { FProgressor stores TProgressor singleton instance }
  154. const 
  155.   FProgressor: TProgressor = nil;
  156.   
  157. {
  158. ********************************* TProgressor **********************************
  159. }
  160. constructor TProgressor.Create;
  161. begin
  162.   inherited Create;
  163.   raise Exception.Create('Access TProgressor through Instance');
  164. end;
  165.  
  166. constructor TProgressor.CreateInstance;
  167. begin
  168.   inherited Create;
  169.   FCursorStack := TIntList.Create;
  170.   FDescrStack := TStringList.Create;
  171.   FObservers := TList.Create;
  172.   FProgressStack := TIntList.Create;
  173.   FBusyCursor := crHourGlass;
  174.   FProgressor := Self;
  175.   FOrgCursor := crDefault;
  176. end;
  177.  
  178. destructor TProgressor.Destroy;
  179. begin
  180.   FCursorStack.Free;
  181.   DisposeStr(FDescription);
  182.   FDescrStack.Free;
  183.   FObservers.Free;
  184.   FProgressStack.Free;
  185.   if FProgressor = Self then FProgressor := nil;
  186.   inherited Destroy;
  187. end;
  188.  
  189. procedure TProgressor.Abort;
  190. begin
  191.   Aborted := True;
  192. end;
  193.  
  194. procedure TProgressor.DescriptionChange;
  195. var
  196.   I: Integer;
  197.   Obs: TProgressObserver;
  198. begin
  199.   for I := 0 to FObservers.Count - 1 do
  200.   begin
  201.     Obs := FObservers[I];
  202.     if Obs.Enabled then Obs.DescriptionChange;
  203.   end;
  204. end;
  205.  
  206. procedure TProgressor.EndProgress;
  207. begin
  208.   FProgressBusy := FProgressStack.Count > 0;
  209.   if FProgressBusy then PopProgress;
  210.   if not FProgressBusy then ProgressEnd;
  211. end;
  212.  
  213. function TProgressor.GetAborted: Boolean;
  214. begin
  215.   Result := FAborted or Application.Terminated;
  216. end;
  217.  
  218. function TProgressor.GetDescription: String;
  219. begin
  220.   Result := StringValue(FDescription);
  221. end;
  222.  
  223. class function TProgressor.Instance: TProgressor;
  224. begin
  225.   Result := FProgressor;
  226.   if not Assigned(Result) then
  227.     Result := TProgressor.CreateInstance;
  228. end;
  229.  
  230. procedure TProgressor.PopProgress;
  231. var
  232.   Cnt,AProgress: Integer;
  233. begin
  234.   Cnt := FProgressStack.Count;
  235.   if Cnt > 0 then
  236.   begin
  237.     Progress := FProgressStack.Items[Pred(Cnt)];
  238.     FProgressStack.Delete(Pred(Cnt));
  239.   end;
  240.   Cnt := FDescrStack.Count;
  241.   if Cnt > 0 then
  242.   begin
  243.     Description := FDescrStack[Pred(Cnt)];
  244.     FDescrStack.Delete(Pred(Cnt));
  245.   end;
  246.   Cnt := FCursorStack.Count;
  247.   if Cnt > 0 then
  248.   begin
  249.     Screen.Cursor := TCursor(FCursorStack[Pred(Cnt)]);
  250.     FCursorStack.Delete(Pred(Cnt));
  251.   end;
  252. end;
  253.  
  254. procedure TProgressor.ProgressChange;
  255. var
  256.   I: Integer;
  257.   Obs: TProgressObserver;
  258. begin
  259.   for I := 0 to FObservers.Count - 1 do
  260.   begin
  261.     Obs := FObservers[I];
  262.     if Obs.Enabled then Obs.ProgressChange;
  263.   end;
  264. end;
  265.  
  266. procedure TProgressor.ProgressEnd;
  267. var
  268.   I: Integer;
  269.   Obs: TProgressObserver;
  270. begin
  271.   Screen.Cursor := FOrgCursor;
  272.   for I := 0 to FObservers.Count - 1 do
  273.   begin
  274.     Obs := FObservers[I];
  275.     if Obs.Enabled then Obs.ProgressEnd;
  276.   end;
  277. end;
  278.  
  279. procedure TProgressor.ProgressStart;
  280. var
  281.   I: Integer;
  282.   Obs: TProgressObserver;
  283. begin
  284.   FAborted := False;
  285.   FOrgCursor := Screen.Cursor;
  286.   Screen.Cursor := FBusyCursor;
  287.   for I := 0 to FObservers.Count - 1 do
  288.   begin
  289.     Obs := FObservers[I];
  290.     if Obs.Enabled then Obs.ProgressStart;
  291.   end;
  292. end;
  293.  
  294. procedure TProgressor.PushProgress;
  295. begin
  296.   FProgressStack.Add(Progress);
  297.   FDescrStack.Add(Description);
  298.   FCursorStack.Add(Ord(Screen.Cursor));
  299. end;
  300.  
  301. procedure TProgressor.RegisterObserver(Observer: TProgressObserver);
  302. begin
  303.   if FObservers.IndexOf(Observer) = -1 then
  304.     FObservers.Add(Observer);
  305. end;
  306.  
  307. procedure TProgressor.SetBusyCursor(Value: TCursor);
  308. begin
  309.   FBusyCursor := Value;
  310.   if FProgressBusy then Screen.Cursor := FBusyCursor;
  311. end;
  312.  
  313. procedure TProgressor.SetDescription(const Value: String);
  314. begin
  315.   if Value <> Description then
  316.   begin
  317.     AssignStr(FDescription, Value);
  318.     DescriptionChange;
  319.   end;
  320. end;
  321.  
  322. procedure TProgressor.SetProgress(Value: Integer);
  323. begin
  324.   if Value <> FProgress then
  325.   begin
  326.     FProgress := Value;
  327.     ProgressChange;
  328.   end;
  329. end;
  330.  
  331. procedure TProgressor.StartProgress(AProgress: Integer; const ADescription: 
  332.     String);
  333. var
  334.   WasBusy: Boolean;
  335. begin
  336.   WasBusy := FProgressBusy;
  337.   FProgressBusy := True;
  338.   if WasBusy then PushProgress;
  339.   Progress := AProgress;
  340.   Description := ADescription;
  341.   if not WasBusy then ProgressStart;
  342. end;
  343.  
  344. procedure TProgressor.StartProgressDef;
  345. begin
  346.   StartProgress(0, '');
  347. end;
  348.  
  349. procedure TProgressor.UnregisterObserver(Observer: TProgressObserver);
  350. begin
  351.   FObservers.Remove(Observer);
  352. end;
  353.  
  354. {
  355. ****************************** TProgressObserver *******************************
  356. }
  357. constructor TProgressObserver.Create(AOwner: TComponent);
  358. begin
  359.   inherited Create(AOwner);
  360.   FEnabled := True;
  361.   FAccuracy := 5;
  362.   Progressor.RegisterObserver(Self);
  363. end;
  364.  
  365. destructor TProgressObserver.Destroy;
  366. begin
  367.   Progressor.UnregisterObserver(Self);
  368.   inherited Destroy;
  369. end;
  370.  
  371. procedure TProgressObserver.Abort;
  372. begin
  373.   Progressor.Abort;
  374. end;
  375.  
  376. procedure TProgressObserver.DescriptionChange;
  377. begin
  378.   if Assigned(FOnDescriptionChange) then FOnDescriptionChange(Self);
  379. end;
  380.  
  381. procedure TProgressObserver.EndProgress;
  382. begin
  383.   Progressor.EndProgress;
  384. end;
  385.  
  386. function TProgressObserver.GetAborted: Boolean;
  387. begin
  388.   Result := Progressor.Aborted;
  389. end;
  390.  
  391. function TProgressObserver.GetBusyCursor: TCursor;
  392. begin
  393.   Result := Progressor.BusyCursor;
  394. end;
  395.  
  396. function TProgressObserver.GetDescription: String;
  397. begin
  398.   Result := Progressor.Description;
  399. end;
  400.  
  401. function TProgressObserver.GetProgress: Integer;
  402. begin
  403.   Result := Progressor.Progress;
  404. end;
  405.  
  406. function TProgressObserver.GetProgressor: TProgressor;
  407. begin
  408.   Result := TProgressor.Instance;
  409. end;
  410.  
  411. procedure TProgressObserver.ProgressChange;
  412. var
  413.   NewProgress: Integer;
  414. begin
  415.   NewProgress := Progress;
  416.   if (FLastProgress div FAccuracy) <> (NewProgress div FAccuracy) then
  417.   begin
  418.     FLastProgress := NewProgress;
  419.     if Assigned(FOnProgressChange) then FOnProgressChange(Self);
  420.   end;
  421. end;
  422.  
  423. procedure TProgressObserver.ProgressEnd;
  424. begin
  425.   if Assigned(FOnFinish) then FOnFinish(Self);
  426. end;
  427.  
  428. procedure TProgressObserver.ProgressStart;
  429. begin
  430.   if Assigned(FOnStart) then FOnStart(Self);
  431. end;
  432.  
  433. procedure TProgressObserver.SetAborted(Value: Boolean);
  434. begin
  435.   Progressor.Aborted := Value;
  436. end;
  437.  
  438. procedure TProgressObserver.SetAccuracy(Value: Integer);
  439. begin
  440.   FAccuracy := LimitToRange(Value, 1, 50);
  441. end;
  442.  
  443. procedure TProgressObserver.SetBusyCursor(Value: TCursor);
  444. begin
  445.   Progressor.BusyCursor := Value;
  446. end;
  447.  
  448. procedure TProgressObserver.SetDescription(Value: String);
  449. begin
  450.   Progressor.Description := Value;
  451. end;
  452.  
  453. procedure TProgressObserver.SetProgress(Value: Integer);
  454. begin
  455.   Progressor.Progress := Value;
  456. end;
  457.  
  458. procedure TProgressObserver.StartProgress(AProgress: Integer; const 
  459.     ADescription: String);
  460. begin
  461.   Progressor.StartProgress(AProgress, ADescription);
  462. end;
  463.  
  464. procedure TProgressObserver.StartProgressDef;
  465. begin
  466.   Progressor.StartProgressDef;
  467. end;
  468.  
  469.  
  470. procedure ShutDown; far;
  471. begin
  472.   FProgressor.Free;
  473. end;
  474.  
  475. initialization
  476.   AddExitProc(ShutDown);
  477. end.
  478.  
  479.