action.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. end;
  66. {****************************************************************************}
  67. {* TBasicAction *}
  68. {****************************************************************************}
  69. constructor TBasicAction.Create(AOwner: TComponent);
  70. begin
  71. inherited Create(AOwner);
  72. FClients := TFpList.Create;
  73. end;
  74. destructor TBasicAction.Destroy;
  75. begin
  76. inherited Destroy;
  77. while FClients.Count > 0 do
  78. UnRegisterChanges(TBasicActionLink(FClients.Last));
  79. FClients.Free;
  80. end;
  81. function TBasicAction.HandlesTarget(Target: TObject): Boolean;
  82. begin
  83. Result := False;
  84. end;
  85. procedure TBasicAction.ExecuteTarget(Target: TObject);
  86. begin
  87. end;
  88. procedure TBasicAction.UpdateTarget(Target: TObject);
  89. begin
  90. end;
  91. function TBasicAction.Execute: Boolean;
  92. begin
  93. if Assigned(FOnExecute) then
  94. begin
  95. FOnExecute(Self);
  96. Result := True;
  97. end
  98. else
  99. Result := False;
  100. end;
  101. function TBasicAction.Update: Boolean;
  102. begin
  103. if Assigned(FOnUpdate) then
  104. begin
  105. FOnUpdate(Self);
  106. Result := True;
  107. end
  108. else
  109. Result := False;
  110. end;
  111. function TBasicAction.Suspended: Boolean;
  112. begin
  113. Result:=False;
  114. end;
  115. procedure TBasicAction.SetOnExecute(Value: TNotifyEvent);
  116. var
  117. I: Integer;
  118. begin
  119. if (TMethod(Value).Code <> TMethod(OnExecute).Code) or
  120. (TMethod(Value).Data <> TMethod(OnExecute).Data) then
  121. begin
  122. for I := 0 to FClients.Count - 1 do
  123. TBasicActionLink(FClients[I]).SetOnExecute(Value);
  124. FOnExecute := Value;
  125. Change;
  126. end;
  127. end;
  128. function TBasicAction.ClientCount: Integer;
  129. begin
  130. Result:=FClients.Count;
  131. end;
  132. function TBasicAction.GetClient(Idx: Integer): TObject;
  133. begin
  134. Result:=TObject(FClients[Idx]);
  135. end;
  136. procedure TBasicAction.SetActionComponent(AValue: TComponent);
  137. begin
  138. if FActionComponent=AValue then Exit;
  139. if Assigned(FActionComponent) then
  140. FActionComponent.RemoveFreeNotification(Self);
  141. FActionComponent:=AValue;
  142. if Assigned(FActionComponent) then
  143. FActionComponent.FreeNotification(Self);
  144. end;
  145. procedure TBasicAction.Change;
  146. begin
  147. if Assigned(FOnChange) then
  148. FOnChange(Self);
  149. end;
  150. procedure TBasicAction.Notification(AComponent: TComponent;
  151. Operation: TOperation);
  152. begin
  153. inherited Notification(AComponent, Operation);
  154. if (Operation<>OpRemove) then
  155. exit;
  156. if (AComponent=FActionComponent) then
  157. FActionComponent:=nil;
  158. end;
  159. procedure TBasicAction.RegisterChanges(Value: TBasicActionLink);
  160. begin
  161. Value.FAction := Self;
  162. FClients.Add(Value);
  163. end;
  164. procedure TBasicAction.UnRegisterChanges(Value: TBasicActionLink);
  165. var
  166. I: Integer;
  167. begin
  168. for I := 0 to FClients.Count - 1 do
  169. if TBasicActionLink(FClients[I]) = Value then
  170. begin
  171. Value.FAction := nil;
  172. FClients.Delete(I);
  173. break;
  174. end;
  175. end;