home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Source / Vcl / STDACTNS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-01-26  |  6.9 KB  |  278 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {       Copyright (c) 1998 Inprise Corporation          }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit StdActns;
  11.  
  12. interface
  13.  
  14. uses Classes, ActnList, StdCtrls, Forms;
  15.  
  16. type
  17.  
  18. { Hint actions }
  19.  
  20.   THintAction = class(TCustomAction)
  21.   public
  22.     constructor Create(AOwner: TComponent); override;
  23.   published
  24.     property Hint;
  25.   end;
  26.  
  27. { Edit actions }
  28.  
  29.   TEditAction = class(TAction)
  30.   private
  31.     FControl: TCustomEdit;
  32.     procedure SetControl(Value: TCustomEdit);
  33.   protected
  34.     function GetControl(Target: TObject): TCustomEdit; virtual;
  35.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  36.   public
  37.     function HandlesTarget(Target: TObject): Boolean; override;
  38.     procedure UpdateTarget(Target: TObject); override;
  39.     property Control: TCustomEdit read FControl write SetControl;
  40.   end;
  41.  
  42.   TEditCut = class(TEditAction)
  43.   public
  44.     procedure ExecuteTarget(Target: TObject); override;
  45.   end;
  46.  
  47.   TEditCopy = class(TEditAction)
  48.   public
  49.     procedure ExecuteTarget(Target: TObject); override;
  50.   end;
  51.  
  52.   TEditPaste = class(TEditAction)
  53.   public
  54.     procedure UpdateTarget(Target: TObject); override;
  55.     procedure ExecuteTarget(Target: TObject); override;
  56.   end;
  57.  
  58. { MDI Window actions }
  59.  
  60.   TWindowAction = class(TAction)
  61.   private
  62.     FForm: TForm;
  63.     procedure SetForm(Value: TForm);
  64.   protected
  65.     function GetForm(Target: TObject): TForm; virtual;
  66.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  67.   public
  68.     function HandlesTarget(Target: TObject): Boolean; override;
  69.     procedure UpdateTarget(Target: TObject); override;
  70.     property Form: TForm read FForm write SetForm;
  71.   end;
  72.  
  73.   TWindowClose = class(TWindowAction)
  74.   public
  75.     procedure ExecuteTarget(Target: TObject); override;
  76.     procedure UpdateTarget(Target: TObject); override;
  77.   end;
  78.  
  79.   TWindowCascade = class(TWindowAction)
  80.   public
  81.     procedure ExecuteTarget(Target: TObject); override;
  82.   end;
  83.  
  84.   TWindowTileHorizontal = class(TWindowAction)
  85.   public
  86.     procedure ExecuteTarget(Target: TObject); override;
  87.   end;
  88.  
  89.   TWindowTileVertical = class(TWindowAction)
  90.   public
  91.     procedure ExecuteTarget(Target: TObject); override;
  92.   end;
  93.  
  94.   TWindowMinimizeAll = class(TWindowAction)
  95.   public
  96.     procedure ExecuteTarget(Target: TObject); override;
  97.   end;
  98.  
  99.   TWindowArrange = class(TWindowAction)
  100.   public
  101.     procedure ExecuteTarget(Target: TObject); override;
  102.   end;
  103.  
  104. implementation
  105.  
  106. uses Windows, Messages, Clipbrd;
  107.  
  108. { THintAction }
  109.  
  110. constructor THintAction.Create(AOwner: TComponent);
  111. begin
  112.   inherited Create(AOwner);
  113.   DisableIfNoHandler := False;
  114. end;
  115.  
  116. { TEditAction }
  117.  
  118. function TEditAction.GetControl(Target: TObject): TCustomEdit;
  119. begin
  120.   { We could hard cast Target as a TCustomEdit since HandlesTarget "should" be
  121.     called before ExecuteTarget and UpdateTarget, however, we're being safe. }
  122.   Result := Target as TCustomEdit;
  123. end;
  124.  
  125. function TEditAction.HandlesTarget(Target: TObject): Boolean;
  126. begin
  127.   Result := ((Control <> nil) and (Target = Control) or
  128.     (Control = nil) and (Target is TCustomEdit)) and TCustomEdit(Target).Focused;
  129. end;
  130.  
  131. procedure TEditAction.Notification(AComponent: TComponent;
  132.   Operation: TOperation);
  133. begin
  134.   inherited Notification(AComponent, Operation);
  135.   if (Operation = opRemove) and (AComponent = Control) then Control := nil;
  136. end;
  137.  
  138. procedure TEditAction.UpdateTarget(Target: TObject);
  139. begin
  140.   if (Self is TEditCut) or (Self is TEditCopy) then
  141.     Enabled := GetControl(Target).SelLength > 0;
  142. end;
  143.  
  144. procedure TEditAction.SetControl(Value: TCustomEdit);
  145. begin
  146.   if Value <> FControl then
  147.   begin
  148.     FControl := Value;
  149.     if Value <> nil then Value.FreeNotification(Self);
  150.   end;
  151. end;
  152.  
  153. { TEditCopy }
  154.  
  155. procedure TEditCopy.ExecuteTarget(Target: TObject);
  156. begin
  157.   GetControl(Target).CopyToClipboard;
  158. end;
  159.  
  160. { TEditCut }
  161.  
  162. procedure TEditCut.ExecuteTarget(Target: TObject);
  163. begin
  164.   GetControl(Target).CutToClipboard;
  165. end;
  166.  
  167. { TEditPaste }
  168.  
  169. procedure TEditPaste.ExecuteTarget(Target: TObject);
  170. begin
  171.    GetControl(Target).PasteFromClipboard;
  172. end;
  173.  
  174. procedure TEditPaste.UpdateTarget(Target: TObject);
  175. begin
  176.   Enabled := Clipboard.HasFormat(CF_TEXT);
  177. end;
  178.  
  179. { TWindowAction }
  180.  
  181. function TWindowAction.GetForm(Target: TObject): TForm;
  182. begin
  183.   { We could hard cast Target as a TForm since HandlesTarget "should" be called
  184.     before ExecuteTarget and UpdateTarget, however, we're being safe. }
  185.   Result := (Target as TForm);
  186. end;
  187.  
  188. function TWindowAction.HandlesTarget(Target: TObject): Boolean;
  189. begin
  190.   Result := ((Form <> nil) and (Target = Form) or
  191.     (Form = nil) and (Target is TForm)) and
  192.     (TForm(Target).FormStyle = fsMDIForm);
  193. end;
  194.  
  195. procedure TWindowAction.Notification(AComponent: TComponent;
  196.   Operation: TOperation);
  197. begin
  198.   inherited Notification(AComponent, Operation);
  199.   if (Operation = opRemove) and (AComponent = Form) then Form := nil;
  200. end;
  201.  
  202. procedure TWindowAction.UpdateTarget(Target: TObject);
  203. begin
  204.   Enabled := GetForm(Target).MDIChildCount > 0;
  205. end;
  206.  
  207. procedure TWindowAction.SetForm(Value: TForm);
  208. begin
  209.   if Value <> FForm then
  210.   begin
  211.     FForm := Value;
  212.     if Value <> nil then Value.FreeNotification(Self);
  213.   end;
  214. end;
  215.  
  216. { TWindowClose }
  217.  
  218. procedure TWindowClose.ExecuteTarget(Target: TObject);
  219. begin
  220.   with GetForm(Target) do
  221.     if ActiveMDIChild <> nil then ActiveMDIChild.Close;
  222. end;
  223.  
  224. procedure TWindowClose.UpdateTarget(Target: TObject);
  225. begin
  226.   Enabled := GetForm(Target).ActiveMDIChild <> nil;
  227. end;
  228.  
  229. { TWindowCascade }
  230.  
  231. procedure TWindowCascade.ExecuteTarget(Target: TObject);
  232. begin
  233.   GetForm(Target).Cascade;
  234. end;
  235.  
  236. { TWindowTileHorizontal }
  237.  
  238. procedure DoTile(Form: TForm; TileMode: TTileMode);
  239. const
  240.   TileParams: array[TTileMode] of Word = (MDITILE_HORIZONTAL, MDITILE_VERTICAL);
  241. begin
  242.   if (Form.FormStyle = fsMDIForm) and (Form.ClientHandle <> 0) then
  243.     SendMessage(Form.ClientHandle, WM_MDITILE, TileParams[TileMode], 0);
  244. end;
  245.  
  246. procedure TWindowTileHorizontal.ExecuteTarget(Target: TObject);
  247. begin
  248.   DoTile(GetForm(Target), tbHorizontal);
  249. end;
  250.  
  251. { TWindowTileVertical }
  252.  
  253. procedure TWindowTileVertical.ExecuteTarget(Target: TObject);
  254. begin
  255.   DoTile(GetForm(Target), tbVertical);
  256. end;
  257.  
  258. { TWindowMinimizeAll }
  259.  
  260. procedure TWindowMinimizeAll.ExecuteTarget(Target: TObject);
  261. var
  262.   I: Integer;
  263. begin
  264.   { Must be done backwards through the MDIChildren array }
  265.   with GetForm(Target) do
  266.     for I := MDIChildCount - 1 downto 0 do
  267.       MDIChildren[I].WindowState := wsMinimized;
  268. end;
  269.  
  270. { TWindowArrange }
  271.  
  272. procedure TWindowArrange.ExecuteTarget(Target: TObject);
  273. begin
  274.   GetForm(Target).ArrangeIcons;
  275. end;
  276.  
  277. end.
  278.