action.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. {%MainUnit classes.pp}
  2. {
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************}
  12. {* TBasicActionLink *}
  13. {****************************************************************************}
  14. constructor TBasicActionLink.Create(AClient: TObject);
  15. begin
  16. inherited Create;
  17. AssignClient(AClient);
  18. end;
  19. procedure TBasicActionLink.AssignClient(AClient: TObject);
  20. begin
  21. end;
  22. destructor TBasicActionLink.Destroy;
  23. begin
  24. if FAction <> nil then
  25. FAction.UnRegisterChanges(Self);
  26. inherited Destroy;
  27. end;
  28. procedure TBasicActionLink.Change;
  29. begin
  30. if Assigned(OnChange) then
  31. OnChange(FAction);
  32. end;
  33. function TBasicActionLink.Execute(AComponent: TComponent): Boolean;
  34. begin
  35. if not Assigned(FAction) then
  36. exit;
  37. FAction.ActionComponent := AComponent;
  38. try
  39. Result := FAction.Execute;
  40. finally
  41. if FAction <> nil then
  42. FAction.ActionComponent := nil;
  43. end;
  44. end;
  45. procedure TBasicActionLink.SetAction(Value: TBasicAction);
  46. begin
  47. if Value <> FAction then
  48. begin
  49. if FAction <> nil then FAction.UnRegisterChanges(Self);
  50. FAction := Value;
  51. if Value <> nil then Value.RegisterChanges(Self);
  52. end;
  53. end;
  54. function TBasicActionLink.IsOnExecuteLinked: Boolean;
  55. begin
  56. Result := True;
  57. end;
  58. procedure TBasicActionLink.SetOnExecute(Value: TNotifyEvent);
  59. begin
  60. end;
  61. function TBasicActionLink.Update: Boolean;
  62. begin
  63. if Assigned(Faction) then
  64. Result := FAction.Update
  65. else
  66. Result:=False;
  67. end;
  68. {****************************************************************************}
  69. {* TBasicAction *}
  70. {****************************************************************************}
  71. constructor TBasicAction.Create(AOwner: TComponent);
  72. begin
  73. inherited Create(AOwner);
  74. FClients := TFpList.Create;
  75. end;
  76. destructor TBasicAction.Destroy;
  77. begin
  78. inherited Destroy;
  79. while FClients.Count > 0 do
  80. UnRegisterChanges(TBasicActionLink(FClients.Last));
  81. FClients.Free;
  82. end;
  83. function TBasicAction.HandlesTarget(Target: TObject): Boolean;
  84. begin
  85. Result := False;
  86. end;
  87. procedure TBasicAction.ExecuteTarget(Target: TObject);
  88. begin
  89. end;
  90. procedure TBasicAction.UpdateTarget(Target: TObject);
  91. begin
  92. end;
  93. function TBasicAction.Execute: Boolean;
  94. begin
  95. if Assigned(FOnExecute) then
  96. begin
  97. FOnExecute(Self);
  98. Result := True;
  99. end
  100. else
  101. Result := False;
  102. end;
  103. function TBasicAction.Update: Boolean;
  104. begin
  105. if Assigned(FOnUpdate) then
  106. begin
  107. FOnUpdate(Self);
  108. Result := True;
  109. end
  110. else
  111. Result := False;
  112. end;
  113. function TBasicAction.Suspended: Boolean;
  114. begin
  115. Result:=False;
  116. end;
  117. procedure TBasicAction.SetOnExecute(Value: TNotifyEvent);
  118. var
  119. I: Integer;
  120. begin
  121. if (TMethod(Value).Code <> TMethod(OnExecute).Code) or
  122. (TMethod(Value).Data <> TMethod(OnExecute).Data) then
  123. begin
  124. for I := 0 to FClients.Count - 1 do
  125. TBasicActionLink(FClients[I]).SetOnExecute(Value);
  126. FOnExecute := Value;
  127. Change;
  128. end;
  129. end;
  130. function TBasicAction.ClientCount: Integer;
  131. begin
  132. Result:=FClients.Count;
  133. end;
  134. function TBasicAction.GetClient(Idx: Integer): TObject;
  135. begin
  136. Result:=TObject(FClients[Idx]);
  137. end;
  138. procedure TBasicAction.SetActionComponent(AValue: TComponent);
  139. begin
  140. if FActionComponent=AValue then Exit;
  141. if Assigned(FActionComponent) then
  142. FActionComponent.RemoveFreeNotification(Self);
  143. FActionComponent:=AValue;
  144. if Assigned(FActionComponent) then
  145. FActionComponent.FreeNotification(Self);
  146. end;
  147. procedure TBasicAction.Change;
  148. begin
  149. if Assigned(FOnChange) then
  150. FOnChange(Self);
  151. end;
  152. procedure TBasicAction.Notification(AComponent: TComponent;
  153. Operation: TOperation);
  154. begin
  155. inherited Notification(AComponent, Operation);
  156. if (Operation<>OpRemove) then
  157. exit;
  158. if (AComponent=FActionComponent) then
  159. FActionComponent:=nil;
  160. end;
  161. procedure TBasicAction.RegisterChanges(Value: TBasicActionLink);
  162. begin
  163. Value.FAction := Self;
  164. FClients.Add(Value);
  165. end;
  166. procedure TBasicAction.UnRegisterChanges(Value: TBasicActionLink);
  167. var
  168. I: Integer;
  169. begin
  170. for I := 0 to FClients.Count - 1 do
  171. if TBasicActionLink(FClients[I]) = Value then
  172. begin
  173. Value.FAction := nil;
  174. FClients.Delete(I);
  175. break;
  176. end;
  177. end;