home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d12345 / CHEMPLOT.ZIP / TPlot / Plotaction.pas < prev    next >
Pascal/Delphi Source File  |  2000-11-26  |  7KB  |  218 lines

  1. unit Plotaction;
  2.  
  3. interface
  4.  
  5. uses
  6.   Comctrls, Classes, Dialogs, ExtDlgs, ActnList, Plot, Misc;
  7.  
  8. type
  9.   { Generic TPlot action }
  10.   TPlotAction = class(TAction)
  11.   private
  12.     FPlot: TPlot;
  13.     procedure SetPlot(Value: TPlot);
  14.   protected
  15.     function GetPlot(Target: TObject): TPlot; virtual;
  16.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  17.   public
  18.     function HandlesTarget(Target: TObject):Boolean; override;
  19.   published
  20.     property Plot: TPlot read FPlot write SetPlot;
  21.   end;
  22.  
  23.   TPlotActionList = class(TActionList)
  24.   private
  25.     FPlot: TPlot;
  26.     function ActionExists(ATag: Integer; ACaption: String): Boolean;
  27.     procedure SetPlot(Value: TPlot);
  28.   protected
  29.     function GetPlot(Target: TObject): TPlot; virtual;
  30.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  31.     procedure CreateActions;
  32.   public
  33.     constructor Create(AOwner: TComponent); override;
  34.     destructor Destroy; override;
  35.   published
  36.     property Plot: TPlot read FPlot write SetPlot;
  37.   end;
  38.  
  39. implementation
  40.  
  41. { Check target object is a Plot }
  42. function TPlotAction.GetPlot(Target: TObject): TPlot;
  43. begin
  44.   Result := Target as TPlot;
  45. end;
  46.  
  47. { Set the Plot component to affect }
  48. procedure TPlotAction.SetPlot(Value: TPlot);
  49. begin
  50.   if Value <> FPlot then
  51.   begin
  52.     FPlot := Value;
  53.     if Value <> nil then
  54.       Value.FreeNotification(Self);
  55.   end;
  56. end;
  57.  
  58. { Determine whether we can act on this target }
  59. function TPlotAction.HandlesTarget(Target: TObject): Boolean;
  60. begin
  61.   Result := ((Plot <> nil) and (Target = Plot)) or
  62.     ((Plot = nil) and (Target is TPlot));
  63. end;
  64.  
  65. { Note deletion of attached Plot component }
  66. procedure TPlotAction.Notification(AComponent: TComponent;
  67.   Operation: TOperation);
  68. begin
  69.   inherited Notification(AComponent, Operation);
  70.   if (Operation = opRemove) and (AComponent = Plot) then
  71.     Plot := nil;
  72. end;
  73.  
  74. {******************************************************************************}
  75. constructor TPlotActionList.Create(AOwner: TComponent);
  76. begin
  77.   inherited Create(AOwner);
  78.   FPlot := nil;
  79. end;
  80.  
  81. destructor TPlotActionList.Destroy;
  82. begin
  83.   inherited Destroy;
  84. end;
  85.  
  86. { Check target object is a Plot }
  87. function TPlotActionList.GetPlot(Target: TObject): TPlot;
  88. begin
  89.   Result := Target as TPlot;
  90. end;
  91.  
  92. { Set the Plot component to affect }
  93. procedure TPlotActionList.SetPlot(Value: TPlot);
  94. begin
  95.   if Value <> FPlot then
  96.   begin
  97.     if (Value = nil) then
  98.     begin
  99.       //FPlot.SetPlotActionList(TActionList(nil));
  100.       FPlot := Value;
  101.     end
  102.     else
  103.     begin
  104.       FPlot := Value;
  105.       Value.FreeNotification(Self);
  106.       CreateActions;
  107.       {SetUpOnClicks;}
  108.       //FPlot.SetPlotActionList(TActionList(Self));
  109.     end;
  110.   end;
  111. end;
  112.  
  113. { Note deletion of attached Plot component }
  114. procedure TPlotActionList.Notification(AComponent: TComponent;
  115.   Operation: TOperation);
  116. begin
  117.   inherited Notification(AComponent, Operation);
  118.   if (Operation = opRemove) and (AComponent = Plot) then
  119.     Plot := nil;
  120. end;
  121.  
  122. {------------------------------------------------------------------------------
  123.     Procedure: TPlotActionList.CreateActions
  124.   Description: creates popup menus that are accessible by right-click
  125.        Author: Mat Ballard
  126.  Date created: 12/1/1999
  127. Date modified: 04/20/2000 by Mat Ballard
  128.       Purpose: modularize user-interface code
  129.  Known Issues: this was a bitch to get right !
  130.  ------------------------------------------------------------------------------}
  131. procedure TPlotActionList.CreateActions;
  132. var
  133.   i, j: Word;
  134.   TempAction: TPlotAction;
  135.   TheName: String;
  136. begin
  137. {don't create actions when the Plot property is streamed in:}
  138.   if (csLoading in ComponentState) then exit;
  139.  
  140. {who needs more than 32 menus ?!}
  141.   if (FPlot.PlotPopupMenu.Items.Count > 32) then raise
  142.     EComponentError.CreateFmt('TPlotActionList.CreateActions: I cannot handle more than %d Sub-menus !',
  143.       [FPlot.PlotPopupMenu.Items.Count]);
  144.  
  145. {create the main actions:}
  146.   for i := 0 to FPlot.PlotPopupMenu.Items.Count-1 do
  147.   begin
  148. {don't re-create a menu if it already exists:}
  149.     if (not ActionExists(
  150.       FPlot.PlotPopupMenu.Items[i].Tag,
  151.       FPlot.PlotPopupMenu.Items[i].Caption)) then
  152.     begin
  153.       TempAction := TPlotAction.Create(Self);
  154.       TempAction.Caption := FPlot.PlotPopupMenu.Items[i].Caption;
  155.       TempAction.Name := Copy(TempAction.Caption, 2, 99) + 'Action';
  156.       TempAction.Tag := FPlot.PlotPopupMenu.Items[i].Tag;
  157.       TempAction.Category := Copy(TempAction.Caption, 2, 99);
  158.       TempAction.ActionList := Self;
  159.     end;
  160.   end;
  161.  
  162. {create the menus in each sub-menu:}
  163.   for i := 0 to FPlot.PlotPopupMenu.Items.Count-1 do
  164.   begin
  165.     for j := 0 to FPlot.PlotPopupMenu.Items[i].Count-1 do
  166.     begin
  167.       if (FPlot.PlotPopupMenu.Items[i].Items[j].Caption <> '-') and
  168.          (not ActionExists(
  169.            FPlot.PlotPopupMenu.Items[i].Items[j].Tag,
  170.            FPlot.PlotPopupMenu.Items[i].Items[j].Caption)) then
  171.       begin
  172.         TempAction := TPlotAction.Create(Self);
  173.         TempAction.Caption := FPlot.PlotPopupMenu.Items[i].Items[j].Caption;
  174.         TheName := CleanString(TempAction.Caption, '&');
  175.         TheName := CleanString(TheName, ' ');
  176.         TheName := CleanString(TheName, '.');
  177.         TheName := CleanString(TheName, '!');
  178.         TempAction.Name := TheName + 'Action';
  179.         TempAction.Tag := FPlot.PlotPopupMenu.Items[i].Items[j].Tag;
  180.         TempAction.Hint := FPlot.PlotPopupMenu.Items[i].Items[j].Hint;
  181.         TempAction.ImageIndex := FPlot.PlotPopupMenu.Items[i].Items[j].ImageIndex;
  182. {add the TempAction:}
  183.         TempAction.ActionList := Self;
  184.         TempAction.Category := Self.Actions[i].Category;
  185.       end;
  186.     end; {j over menu items}
  187.   end; {i over submenus}
  188. end;
  189.  
  190. {------------------------------------------------------------------------------
  191.      Function: TPlotAction.ActionExists
  192.   Description: Does this Action exist ? Based on Tag and Caption
  193.        Author: Mat Ballard
  194.  Date created: 04/25/2000
  195. Date modified: 04/25/2000 by Mat Ballard
  196.       Purpose: do we need to add a Action item ?
  197.  Return Value: Boolean;
  198.  Known Issues:
  199.  ------------------------------------------------------------------------------}
  200. function TPlotActionList.ActionExists(ATag: Integer; ACaption: String): Boolean;
  201. var
  202.   i: Integer;
  203. begin
  204.   for i := 0 to Self.ActionCount-1 do
  205.   begin
  206.     if ((Self.Actions[i].Tag = ATag) and
  207.         (TAction(Self.Actions[i]).Caption = ACaption)) then
  208.     begin
  209.       ActionExists := TRUE;
  210.       exit;
  211.     end;
  212.   end;
  213.   ActionExists := FALSE;
  214. end;
  215.  
  216.  
  217. end.
  218.