home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d123456 / CHEMPLOT.ZIP / TPlot / Plottoolbar.pas < prev    next >
Pascal/Delphi Source File  |  2001-07-26  |  14KB  |  445 lines

  1. unit Plottoolbar;
  2.  
  3. {$I Plot.inc}
  4.  
  5. { this might fix the "class not found" stuff:
  6. initialization
  7.   RegisterClass(TPlotToolBar);}
  8.  
  9. {-----------------------------------------------------------------------------
  10. The contents of this file are subject to the Q Public License
  11. ("QPL"); you may not use this file except in compliance
  12. with the QPL. You may obtain a copy of the QPL from 
  13. the file QPL.html in this distribution, derived from:
  14.  
  15. http://www.trolltech.com/products/download/freelicense/license.html
  16.  
  17. The QPL prohibits development of proprietary software. 
  18. There is a Professional Version of this software available for this. 
  19. Contact sales@chemware.hypermart.net for more information.
  20.  
  21. Software distributed under the QPL is distributed on an "AS IS" basis,
  22. WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the QPL for
  23. the specific language governing rights and limitations under the QPL.
  24.  
  25. The Original Code is: Plottoolbar.pas, released 12 May 2001.
  26.  
  27. The Initial Developer of the Original Code is Mat Ballard.
  28. Portions created by Mat Ballard are Copyright (C) 1999 Mat Ballard.
  29. Portions created by Microsoft are Copyright (C) 1998, 1999 Microsoft Corp.
  30. All Rights Reserved.
  31.  
  32. Contributor(s): Mat Ballard                 e-mail: mat.ballard@chemware.hypermart.net.
  33.  
  34. Last Modified: 03/25/2001
  35. Current Version: 2.00
  36.  
  37. You may retrieve the latest version of this file from:
  38.  
  39.         http://Chemware.hypermart.net/
  40.  
  41. This work was created with the Project JEDI VCL guidelines:
  42.  
  43.         http://www.delphi-jedi.org/Jedi:VCLVCL
  44.  
  45. in mind. 
  46.  
  47. Purpose:
  48. An additional user interface for TPlot.
  49.  
  50.  
  51. Known Issues: Does not work when component is added from the IDE.
  52.               exception EClassNotFound: class TToolBar not found".
  53.               even adding a NoButtons property, and modifying CreateToolButtons,
  54.               so that 54 buttons DO exist, does not work. 
  55.  
  56. -----------------------------------------------------------------------------}
  57. interface
  58.  
  59. uses
  60.   Classes, SysUtils,
  61. {$IFDEF WINDOWS}
  62.   WinTypes, WinProcs,
  63.   ComCtrls, Controls,
  64. {$ENDIF}
  65. {$IFDEF WIN32}
  66.   Windows,
  67.   ComCtrls, Controls, Dialogs,
  68. {$ENDIF}
  69. {$IFDEF LINUX}
  70.   QComCtrls, QControls, QDialogs,
  71. {$ENDIF}
  72.  
  73.   Plotdefs, Plot, Misc, Plottooledit;
  74.  
  75. type
  76.   TPlotToolBar = class(TToolBar)
  77.   private
  78.     FCanConfigure: Boolean;
  79.     //FNoButtons: Integer;
  80.     FPlot: TPlot;
  81.  
  82.     function ToolButtonExists(ATag: Integer; ACaption: String): Boolean;
  83.   protected
  84.     function GetPlot(Target: TObject): TPlot; virtual;
  85. {GetPlot is part of the Notification mechanism for linking components.}
  86.     procedure SetPlot(Value: TPlot);
  87. {This sets the Plot, and so creates the buttons.}
  88.     {function GetNoButtons: Integer;
  89.     procedure SetNoButtons(Value: Integer);}
  90. {This creates the buttons.}
  91.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  92. {Notification is part of the Notification mechanism for linking components.}
  93.     procedure CreateToolButtons;
  94. {This creates all the ToolButtons. It bascially "steals" them from the
  95.  TPlot.PlotPopupMenu.}
  96.     procedure DidMouseDown(Sender: TObject; Button: TMouseButton;
  97.       Shift: TShiftState; X, Y: Integer);
  98. {This is the target OnClick method of all the ToolButtons.
  99.  It calls the MouseDown for a right click.}
  100.     procedure MouseDown(Button: TMouseButton;
  101.       Shift: TShiftState; X, Y: Integer); override;
  102. {This calls the ancestor for a left click, or runs the ToolEditor for a right click.}
  103.   public
  104.     constructor Create(AOwner: TComponent); override;
  105. {The standard constructor, in which the ToolButtonOptions are created and other
  106.  properties set.}
  107.     destructor Destroy; override;
  108. {The standard destructor, in which the ToolButtonOptions are freed.}
  109.     procedure ApplyChanges(Sender: TObject);
  110. {This applies any changes (to ToolButton visibility) from the ToolEditor to this
  111.  component.}
  112.     procedure ApplyOptions(Value: TPopupOptions);
  113. {This applies any changes (to ToolButton visibility) from TPlot to this
  114.  component.}
  115.  
  116.     {procedure ShowArrangement;}
  117. {This (leads to) the display of the ToolEditor as a design-time property editor.}
  118.   published
  119.     {property Arrangement: string read FArrangement write FArrangement {$IFDEF DELPHI2_UP . stored False .$ENDIF}
  120. {This is used to invoke the ToolEditor as a design-time property editor.
  121.  See ShowArrangement.}
  122.     property CanConfigure: Boolean read FCanConfigure write FCanConfigure;
  123. {Can the user configure the ToolBar ?}
  124.     //property NoButtons: Integer read GetNoButtons write SetNoButtons;
  125.  
  126.     property Plot: TPlot read FPlot write SetPlot;
  127. {This is the Plot to which this ToolBar is connected.}
  128. end;
  129.  
  130. implementation
  131.  
  132. {Constructor and Destructor ---------------------------------------------------}
  133. {------------------------------------------------------------------------------
  134.   Constructor: TPlotToolBar.Create
  135.   Description: standard Constructor
  136.        Author: Mat Ballard
  137.  Date created: 04/25/2000
  138. Date modified: 04/25/2000 by Mat Ballard
  139.       Purpose: sets the Plot Property
  140.  Known Issues:
  141.  ------------------------------------------------------------------------------}
  142. constructor TPlotToolBar.Create(AOwner: TComponent);
  143. begin
  144.   inherited Create(AOwner);
  145.   FPlot := nil;
  146.   ShowHint := TRUE;
  147.   AutoSize := TRUE;
  148.   FCanConfigure := TRUE;
  149. end;
  150.  
  151. destructor TPlotToolBar.Destroy;
  152. begin
  153.   inherited Destroy;
  154. end;
  155.  
  156. {------------------------------------------------------------------------------
  157.      Function: TPlotToolBar.GetPlot;
  158.   Description: Check target object is a Plot
  159.        Author: Mat Ballard
  160.  Date created: 04/20/2001
  161. Date modified: 05/02/2001 by Mat Ballard
  162.       Purpose: design-time user interface
  163.  Known Issues:
  164.  ------------------------------------------------------------------------------}
  165. function TPlotToolBar.GetPlot(Target: TObject): TPlot;
  166. begin
  167.   Result := Target as TPlot;
  168. end;
  169.  
  170. {------------------------------------------------------------------------------
  171.     Procedure: TPlotToolBar.SetPlot;
  172.   Description: sets the Plot
  173.        Author: Mat Ballard
  174.  Date created: 04/20/2001
  175. Date modified: 05/02/2001 by Mat Ballard
  176.       Purpose: design-time user interface
  177.  Known Issues:
  178.  ------------------------------------------------------------------------------}
  179. procedure TPlotToolBar.SetPlot(Value: TPlot);
  180. begin
  181.   if (csDesigning in ComponentState) then
  182.   begin
  183.     ShowMessage(sSetPlot1);
  184.     exit;
  185.   end;
  186.  
  187.   if Value <> FPlot then
  188.   begin
  189.     if (Value = nil) then
  190.     begin
  191.       FPlot := Value;
  192.       FPlot.SetPlotToolBar(TToolBar(nil));
  193.     end
  194.     else
  195.     begin
  196.       FPlot := Value;
  197. {$IFDEF DELPHI1}
  198.       Value.Notification(Self, opInsert); {???}
  199. {$ELSE}
  200.       Value.FreeNotification(Self);
  201. {$ENDIF}
  202.       CreateToolButtons;
  203.       FPlot.SetPlotToolBar(TToolBar(Self));
  204.     end;
  205.   end;
  206. end;
  207.  
  208. { Note deletion of attached Plot component }
  209. procedure TPlotToolBar.Notification(AComponent: TComponent;
  210.   Operation: TOperation);
  211. begin
  212.   inherited Notification(AComponent, Operation);
  213.   if (Operation = opRemove) and (AComponent = Plot) then
  214.     FPlot := nil;
  215. end;
  216.  
  217. {------------------------------------------------------------------------------
  218.     Procedure: TPlotToolBar.CreateToolButtons
  219.   Description: creates tool buttons
  220.        Author: Mat Ballard
  221.  Date created: 11/18/2000
  222. Date modified: 07/25/2001 by Mat Ballard
  223.       Purpose: modularize user-interface code
  224.  Known Issues:
  225.  ------------------------------------------------------------------------------}
  226. procedure TPlotToolBar.CreateToolButtons;
  227. var
  228.   i, j: Word;
  229.   TempToolButton: TToolButton;
  230.   TheName: String;
  231. {$IFDEF COMPILER35}
  232.   Index: Integer;
  233. {$ENDIF}
  234. begin
  235. {don't create ToolButtons when the Plot property is streamed in:}
  236.   if (csLoading in ComponentState) then exit;
  237.   if (FPlot = nil) then exit;
  238.  
  239. {who needs more than 32 menus ?!}
  240.   if (FPlot.PlotPopupMenu.Items.Count > 32) then raise
  241.     EComponentError.CreateFmt(sCreateToolButtons1, [FPlot.PlotPopupMenu.Items.Count]);
  242.  
  243. {Create the main ToolButtons, which correspond to each sub-menu,
  244.  then the menu items within each submenu.
  245.  Note the bizzare order required by Delphi}
  246. {$IFDEF DELPHI}
  247.   for i := FPlot.PlotPopupMenu.Items.Count-1 downto 0 do
  248.   begin
  249.     for j := FPlot.PlotPopupMenu.Items[i].Count-1 downto 0 do
  250. {$ENDIF}
  251. {$IFDEF BCB}
  252.   for i := 0 to FPlot.PlotPopupMenu.Items.Count-1 do
  253.   begin
  254.     for j := 0 to FPlot.PlotPopupMenu.Items[i].Count-1 do
  255. {$ENDIF}
  256. {$IFDEF KYLIX}
  257.   for i := 0 to FPlot.PlotPopupMenu.Items.Count-1 do
  258.   begin
  259.     for j := 0 to FPlot.PlotPopupMenu.Items[i].Count-1 do
  260. {$ENDIF}
  261.     begin
  262.       if (FPlot.PlotPopupMenu.Items[i].Items[j].Caption <> '-') then
  263.       begin
  264.         if (not ToolButtonExists(
  265.            FPlot.PlotPopupMenu.Items[i].Items[j].Tag,
  266.            FPlot.PlotPopupMenu.Items[i].Items[j].Caption)) then
  267.         begin
  268.           TempToolButton := TToolButton.Create(Self);
  269.           TempToolButton.Caption := FPlot.PlotPopupMenu.Items[i].Items[j].Caption;
  270.           TempToolButton.Name := Format('b%d', [FPlot.PlotPopupMenu.Items[i].Items[j].Tag]);
  271.           TempToolButton.Style := tbsButton;
  272.           TempToolButton.Tag := FPlot.PlotPopupMenu.Items[i].Items[j].Tag;
  273.           TempToolButton.Hint := TheName + ' - ' + FPlot.PlotPopupMenu.Items[i].Items[j].Hint;
  274. {$IFDEF COMPILER4_UP}
  275.           TempToolButton.ImageIndex := FPlot.PlotPopupMenu.Items[i].Items[j].ImageIndex;
  276. {$ENDIF}
  277.           TempToolButton.OnClick := FPlot.PlotPopupMenu.Items[i].Items[j].OnClick;
  278. {When the ToolButton is right-clicked, DidMouseDown calls MouseDown, which fires up the ToolEditor.}
  279.           TempToolButton.OnMouseDown := DidMouseDown;
  280. {add the TempToolButton:}
  281.           TempToolButton.Parent := Self;
  282.         end;
  283.       end;
  284.     end; {j over menu items}
  285.     if (i <> 0) then
  286.     begin
  287.       TempToolButton := TToolButton.Create(Self);
  288.       TempToolButton.Style := tbsSeparator;
  289.       //TempToolButton.Wrap := TRUE;
  290.       TempToolButton.Width := 8;
  291.       TempToolButton.Height := 8;
  292. {add the seperator TempToolButton:}
  293.       TempToolButton.Parent := Self;
  294.     end
  295.   end; {i over submenus}
  296. {$IFDEF COMPILER35}
  297.   Index := 0;
  298.   for i := 0 to Self.ButtonCount-1 do
  299.   begin
  300.     if (Self.Buttons[i].Style = tbsButton) then
  301.     begin
  302.       Self.Buttons[i].ImageIndex := Index;
  303.       Inc(Index);
  304.     end;
  305.   end;
  306. {$ENDIF}
  307. end;
  308.  
  309. {------------------------------------------------------------------------------
  310.      Function: TPlotToolBar.ToolButtonExists
  311.   Description: Does this ToolButton exist ? Based on Tag and Caption
  312.        Author: Mat Ballard
  313.  Date created: 04/25/2000
  314. Date modified: 04/25/2000 by Mat Ballard
  315.       Purpose: do we need to add a ToolButton item ?
  316.  Return Value: Boolean;
  317.  Known Issues:
  318.  ------------------------------------------------------------------------------}
  319. function TPlotToolBar.ToolButtonExists(ATag: Integer; ACaption: String): Boolean;
  320. var
  321.   i: Integer;
  322. begin
  323. {$IFDEF MSWINDOWS}
  324.   for i := 0 to Self.ButtonCount-1 do
  325.   begin
  326.     if ((Self.Buttons[i].Tag = ATag) and
  327.         (TToolButton(Self.Buttons[i]).Caption = ACaption)) then
  328. {$ENDIF}
  329. {$IFDEF LINUX}
  330.   for i := 0 to Self.ControlCount-1 do
  331.   begin
  332.     if ((Self.Controls[i].Tag = ATag) and
  333.         (TToolButton(Self.Controls[i]).Caption = ACaption)) then
  334. {$ENDIF}
  335.     begin
  336.       ToolButtonExists := TRUE;
  337.       exit;
  338.     end;
  339.   end;
  340.   ToolButtonExists := FALSE;
  341. end;
  342.  
  343. procedure TPlotToolBar.DidMouseDown(Sender: TObject; Button: TMouseButton;
  344.   Shift: TShiftState; X, Y: Integer);
  345. begin
  346.   if (Button = mbRight) then
  347.     MouseDown(Button, Shift, X, Y);
  348. end;
  349.  
  350. procedure TPlotToolBar.MouseDown(Button: TMouseButton;
  351.   Shift: TShiftState; X, Y: Integer);
  352. var
  353.   ToolBarEditForm: TToolBarEditForm;
  354. begin
  355.   if ((Button = mbRight) and (FCanConfigure)) then
  356.   begin
  357.     ToolBarEditForm := TToolBarEditForm.Create(nil);
  358.     ToolBarEditForm.SetupButtons(TToolBar(Self));
  359.     ToolBarEditForm.ShowModal;
  360.     ToolBarEditForm.Free;
  361.   end
  362.   else
  363.     inherited MouseDown(Button, Shift, X, Y);
  364. end;
  365.  
  366. procedure TPlotToolBar.ApplyChanges(Sender: TObject);
  367. var
  368.   i: Integer;
  369. {$IFDEF LINUX}
  370.   j,
  371.   TargetIndex,
  372.   TheTag: Integer;
  373. {$ENDIF}
  374. begin
  375.   for i := 0 to Self.ButtonCount-1 do
  376.   begin
  377. {$IFDEF MSWINDOWS}
  378.     Self.Buttons[i].Visible :=
  379.       TToolBar(Sender).Buttons[i].Visible;
  380. {$ENDIF}
  381. {$IFDEF LINUX}
  382.     TargetIndex := -1;
  383.     TheTag := TToolButton(TToolBar(Sender).Controls[i]).Tag;
  384.     for j := 0 to Self.ButtonCount-1 do
  385.     begin
  386.       if (TToolButton(Self.Controls[j]).Tag = TheTag) then
  387.       begin
  388.         TargetIndex := j;
  389.         break;
  390.       end;
  391.     end;
  392.     Self.Controls[TargetIndex].Visible :=
  393.       TToolBar(Sender).Controls[i].Visible;
  394. {$ENDIF}
  395.   end;
  396.   {  for i := 0 to Ord(High(TMainMenus)) do
  397.     FPlotPopUpMenu.Items[i].Visible :=
  398.       FPlotPopUpMenu.Items[i].Visible and
  399.         (TMainMenus(i) in FOptions.Menu);}
  400. end;
  401.  
  402. procedure TPlotToolBar.ApplyOptions(Value: TPopupOptions);
  403. var
  404.   i, j, Index: Integer;
  405. begin
  406.   if (FPlot = nil) then exit;
  407.  
  408.   for Index := 0 to Self.ButtonCount-1 do
  409.   begin
  410. {$IFDEF MSWINDOWS}
  411.     if (FPlot.GetIndicesFromTag(Self.Buttons[Index].Tag, i, j)) then
  412.     begin
  413.       case i of
  414.         Ord(mnuFile): Self.Buttons[Index].Visible :=
  415.           (TFileMenus(j) in Value.File_);
  416.         Ord(mnuEdit): Self.Buttons[Index].Visible :=
  417.           (TEditMenus(j) in Value.Edit);
  418.         Ord(mnuView): Self.Buttons[Index].Visible :=
  419.           (TViewMenus(j) in Value.View);
  420.         Ord(mnuCalc): Self.Buttons[Index].Visible :=
  421.           (TCalcMenus(j) in Value.Calc);
  422.       end; {case}
  423.     end; {GetIndicesFromTag}
  424. {$ENDIF}
  425. {$IFDEF LINUX}
  426.     if (FPlot.GetIndicesFromTag(Self.Controls[Index].Tag, i, j)) then
  427.     begin
  428.       case i of
  429.         Ord(mnuFile): Self.Controls[Index].Visible :=
  430.           (TFileMenus(j) in Value.File_);
  431.         Ord(mnuEdit): Self.Controls[Index].Visible :=
  432.           (TEditMenus(j) in Value.Edit);
  433.         Ord(mnuView): Self.Controls[Index].Visible :=
  434.           (TViewMenus(j) in Value.View);
  435.         Ord(mnuCalc): Self.Controls[Index].Visible :=
  436.           (TCalcMenus(j) in Value.Calc);
  437.       end; {case}
  438.     end; {GetIndicesFromTag}
  439. {$ENDIF}
  440.   end; {for Index}
  441. end;
  442.  
  443.  
  444. end.
  445.