home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / TeeChartPro / TeeChart5Delphi5Eval.exe / %MAINDIR% / Examples / Features / TeeDragPoint.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-10  |  4.7 KB  |  175 lines

  1. {******************************************}
  2. { TDragPointTool and Editor Dialog         }
  3. { Copyright (c) 2000 by David Berneda      }
  4. {******************************************}
  5. {$I teedefs.inc}
  6. unit TeeDragPoint;
  7.  
  8. interface
  9.  
  10. uses {$IFDEF LINUX}
  11.      LibC,
  12.      {$ELSE}
  13.      {$IFNDEF CLX}
  14.      Windows, Messages,
  15.      {$ENDIF}
  16.      {$ENDIF}
  17.      SysUtils, Classes,
  18.      {$IFDEF CLX}
  19.      QGraphics, QControls, QForms, QDialogs, QStdCtrls, QExtCtrls,
  20.      {$ELSE}
  21.      Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  22.      {$ENDIF}
  23.      TeeToolSeriesEdit, TeEngine, TeeTools;
  24.  
  25.  
  26. { DragPoint: A TeeChart Tool example.
  27.  
  28.   This tool component can be used to allow the end-user to
  29.   drag points of any Chart Series by using the mouse.
  30.  
  31.   This unit also includes a Form that is used as the editor dialog
  32.   for the tool. This form automatically shows at the Tools tab of the
  33.   TeeChart editor.
  34.  
  35.   Installation / use:
  36.  
  37.   1) Add this unit to a project or to a design-time package.
  38.  
  39.   2) Then, edit a Chart1 and go to "Tools" tab and Add a DragPoint tool.
  40.  
  41.   3) Set the tool Series property to a existing series (eg: Series1 ).
  42.  
  43.   4) Fill the series with points as usually.
  44.  
  45.   5) At run-time, use the left mouse button to click and drag a
  46.      Series point.
  47.  
  48. }
  49.  
  50. type
  51.   { The Tool editor dialog }
  52.   TDragPointToolEdit = class(TSeriesToolEditor)
  53.     RGStyle: TRadioGroup;
  54.     procedure FormShow(Sender: TObject);
  55.     procedure RGStyleClick(Sender: TObject);
  56.   private
  57.     { Private declarations }
  58.   public
  59.     { Public declarations }
  60.   end;
  61.  
  62.   TDragPointTool=class;
  63.  
  64.   TDragPointToolEvent=procedure(Sender:TDragPointTool; Index:Integer) of object;
  65.  
  66.   TDragPointStyle=(dsX,dsY,dsBoth);
  67.  
  68.   { Drag Point Tool }
  69.   TDragPointTool=class(TTeeCustomToolSeries)
  70.   private
  71.     FDragStyle: TDragPointStyle;
  72.     FOnDrag   : TDragPointToolEvent;
  73.  
  74.     IDragging : Integer;
  75.   protected
  76.     Procedure ChartMouseEvent( AEvent: TChartMouseEvent;
  77.                                Button:TMouseButton;
  78.                                Shift: TShiftState; X, Y: Integer); override;
  79.     class Function GetEditorClass:String; override;
  80.   public
  81.     Constructor Create(AOwner:TComponent); override;
  82.     class Function Description:String; override;
  83.   published
  84.     property Active;
  85.     property DragStyle:TDragPointStyle read FDragStyle write FDragStyle
  86.                                        default dsBoth;
  87.     property Series;
  88.     { events }
  89.     property OnDragPoint:TDragPointToolEvent read FOnDrag write FOnDrag;
  90.   end;
  91.  
  92. implementation
  93.  
  94. {$R *.DFM}
  95.  
  96. Uses Chart;
  97.  
  98. { TDragPointTool }
  99. Constructor TDragPointTool.Create(AOwner: TComponent);
  100. begin
  101.   inherited;
  102.   IDragging:=-1;
  103.   FDragStyle:=dsBoth;
  104. end;
  105.  
  106. { When the user clicks or moves the mouse... }
  107. procedure TDragPointTool.ChartMouseEvent(AEvent: TChartMouseEvent;
  108.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  109. begin
  110.   if Assigned(Series) then
  111.   Case AEvent of
  112.       cmeUp: IDragging:=-1;  { stop dragging on mouse up  }
  113.  
  114.     cmeMove: if IDragging<>-1 then { if dragging... }
  115.              With Series do
  116.              begin
  117.                { Change the Series point X and / or Y values... }
  118.                if (Self.DragStyle=dsX) or (Self.DragStyle=dsBoth) then
  119.                   XValue[IDragging]:=XScreenToValue(x);
  120.                if (Self.DragStyle=dsY) or (Self.DragStyle=dsBoth) then
  121.                   YValue[IDragging]:=YScreenToValue(y);
  122.  
  123.                { Notify the point values change... }
  124.                if Assigned(FOnDrag) then FOnDrag(Self,IDragging);
  125.              end;
  126.  
  127.     cmeDown: begin { when the mouse button is pressed... }
  128.                IDragging:=Series.Clicked(x,y);
  129.                { if clicked a Series point... }
  130.                if IDragging<>-1 then ParentChart.CancelMouse:=True;
  131.              end;
  132.   end;
  133. end;
  134.  
  135. class function TDragPointTool.Description: String;
  136. begin
  137.   result:='Drag Point';
  138. end;
  139.  
  140. class function TDragPointTool.GetEditorClass: String;
  141. begin
  142.   result:='TDragPointToolEdit';  { the editor dialog class name }
  143. end;
  144.  
  145. procedure TDragPointToolEdit.FormShow(Sender: TObject);
  146. begin
  147.   inherited;
  148.   Case TDragPointTool(Tool).DragStyle of
  149.     dsX : RGStyle.ItemIndex:=0;
  150.     dsY : RGStyle.ItemIndex:=1;
  151.   else
  152.     RGStyle.ItemIndex:=2;
  153.   end;
  154. end;
  155.  
  156. procedure TDragPointToolEdit.RGStyleClick(Sender: TObject);
  157. begin
  158.   With TDragPointTool(Tool) do
  159.   Case RGStyle.ItemIndex of
  160.     0: DragStyle:=dsX;
  161.     1: DragStyle:=dsY;
  162.   else
  163.      DragStyle:=dsBoth;
  164.   end;
  165. end;
  166.  
  167. { register both the tool and the tool editor dialog form }
  168. initialization
  169.   RegisterClass(TDragPointToolEdit);
  170.   RegisterTeeTools([TDragPointTool]);
  171. finalization
  172.   { un-register the tool }
  173.   UnRegisterTeeTools([TDragPointTool]);
  174. end.
  175.