~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to components/tachart/tanavigation.pas

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{
 
2
 *****************************************************************************
 
3
 *                                                                           *
 
4
 *  See the file COPYING.modifiedLGPL.txt, included in this distribution,    *
 
5
 *  for details about the copyright.                                         *
 
6
 *                                                                           *
 
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
 
 
13
Authors: Alexander Klenin
 
14
 
 
15
}
 
16
 
 
17
unit TANavigation;
 
18
 
 
19
{$H+}
 
20
 
 
21
interface
 
22
 
 
23
uses
 
24
  Classes, Controls, Graphics, StdCtrls, TAChartUtils, TAGraph;
 
25
 
 
26
type
 
27
 
 
28
  { TChartNavScrollBar }
 
29
 
 
30
  TChartNavScrollBar = class (TCustomScrollBar)
 
31
  private
 
32
    FAutoPageSize: Boolean;
 
33
    FChart: TChart;
 
34
    FListener: TListener;
 
35
    procedure ChartExtentChanged(ASender: TObject);
 
36
    procedure SetAutoPageSize(AValue: Boolean);
 
37
    procedure SetChart(AValue: TChart);
 
38
  protected
 
39
    procedure Scroll(
 
40
      AScrollCode: TScrollCode; var AScrollPos: Integer); override;
 
41
  public
 
42
    constructor Create(AOwner: TComponent); override;
 
43
    destructor Destroy; override;
 
44
  published
 
45
    property AutoPageSize: Boolean
 
46
      read FAutoPageSize write SetAutoPageSize default false;
 
47
    property Chart: TChart read FChart write SetChart;
 
48
  published
 
49
    property Align;
 
50
    property Anchors;
 
51
    property BidiMode;
 
52
    property BorderSpacing;
 
53
    property Constraints;
 
54
    property DragCursor;
 
55
    property DragKind;
 
56
    property DragMode;
 
57
    property Enabled;
 
58
    property Kind;
 
59
    property LargeChange;
 
60
    property Max;
 
61
    property Min;
 
62
    property PageSize;
 
63
    property ParentBidiMode;
 
64
    property ParentShowHint;
 
65
    property PopupMenu;
 
66
    property Position;
 
67
    property ShowHint;
 
68
    property SmallChange;
 
69
    property TabOrder;
 
70
    property TabStop;
 
71
    property Visible;
 
72
  published
 
73
    property OnChange;
 
74
    property OnContextPopup;
 
75
    property OnDragDrop;
 
76
    property OnDragOver;
 
77
    property OnEndDrag;
 
78
    property OnEnter;
 
79
    property OnExit;
 
80
    property OnKeyDown;
 
81
    property OnKeyPress;
 
82
    property OnKeyUp;
 
83
    property OnScroll;
 
84
    property OnStartDrag;
 
85
    property OnUTF8KeyPress;
 
86
  end;
 
87
 
 
88
  { TChartNavPanel }
 
89
 
 
90
  TChartNavPanel = class(TCustomControl)
 
91
  private
 
92
    FIsDragging: Boolean;
 
93
    FLogicalExtentRect: TRect;
 
94
    FOffset: TDoublePoint;
 
95
    FOldCursor: TCursor;
 
96
    FPrevPoint: TDoublePoint;
 
97
    FScale: TDoublePoint;
 
98
    procedure ChartExtentChanged(ASender: TObject);
 
99
  private
 
100
    FAllowDragNavigation: Boolean;
 
101
    FChart: TChart;
 
102
    FDragCursor: TCursor;
 
103
    FFullExtentPen: TPen;
 
104
    FListener: TListener;
 
105
    FLogicalExtentPen: TPen;
 
106
    FMiniMap: Boolean;
 
107
    FProportional: Boolean;
 
108
    FShift: TShiftState;
 
109
    procedure SetChart(AValue: TChart);
 
110
    procedure SetDragCursor(AValue: TCursor);
 
111
    procedure SetFullExtentPen(AValue: TPen);
 
112
    procedure SetLogicalExtentPen(AValue: TPen);
 
113
    procedure SetMiniMap(AValue: Boolean);
 
114
    procedure SetProportional(AValue: Boolean);
 
115
  protected
 
116
    procedure MouseDown(
 
117
      AButton: TMouseButton; AShift: TShiftState; AX, AY: Integer); override;
 
118
    procedure MouseMove(AShift: TShiftState; AX, AY: Integer); override;
 
119
    procedure MouseUp(
 
120
      AButton: TMouseButton; AShift: TShiftState; AX, AY: Integer); override;
 
121
  public
 
122
    constructor Create(AOwner: TComponent); override;
 
123
    destructor Destroy; override;
 
124
    procedure Paint; override;
 
125
  published
 
126
    property AllowDragNavigation: Boolean
 
127
      read FAllowDragNavigation write FAllowDragNavigation default true;
 
128
    property Chart: TChart read FChart write SetChart;
 
129
    property DragCursor: TCursor read FDragCursor write SetDragCursor default crSizeAll;
 
130
    property FullExtentPen: TPen read FFullExtentPen write SetFullExtentPen;
 
131
    property LogicalExtentPen: TPen read FLogicalExtentPen write SetLogicalExtentPen;
 
132
    property MiniMap: Boolean read FMiniMap write SetMiniMap default false;
 
133
    property Proportional: Boolean read FProportional write SetProportional default false;
 
134
    property Shift: TShiftState read FShift write FShift default [ssLeft];
 
135
  published
 
136
    property Align;
 
137
  end;
 
138
 
 
139
procedure Register;
 
140
 
 
141
implementation
 
142
 
 
143
uses
 
144
  Forms, SysUtils, TAGeometry;
 
145
 
 
146
procedure Register;
 
147
begin
 
148
  RegisterComponents(
 
149
    CHART_COMPONENT_IDE_PAGE, [TChartNavScrollBar, TChartNavPanel]);
 
150
end;
 
151
 
 
152
{ TChartNavScrollBar }
 
153
 
 
154
procedure TChartNavScrollBar.ChartExtentChanged(ASender: TObject);
 
155
var
 
156
  fe, le: TDoubleRect;
 
157
  fw, lw: Double;
 
158
begin
 
159
  Unused(ASender);
 
160
  if Chart = nil then exit;
 
161
  fe := Chart.GetFullExtent;
 
162
  le := Chart.LogicalExtent;
 
163
  if le = EmptyExtent then
 
164
    le := fe;
 
165
  case Kind of
 
166
    sbHorizontal: begin
 
167
      fw := fe.b.X - fe.a.X;
 
168
      if fw <= 0 then
 
169
        Position := 0
 
170
      else
 
171
        Position := Round(WeightedAverage(Min, Max, (le.a.X - fe.a.X) / fw));
 
172
      lw := le.b.X - le.a.X;
 
173
    end;
 
174
    sbVertical: begin
 
175
      fw := fe.b.Y - fe.a.Y;
 
176
      if fw <= 0 then
 
177
        Position := 0
 
178
      else
 
179
        Position := Round(WeightedAverage(Max, Min, (le.a.Y - fe.a.Y) / fw));
 
180
      lw := le.b.Y - le.a.Y;
 
181
    end;
 
182
  end;
 
183
  if AutoPageSize and not (csDesigning in ComponentState) then
 
184
    PageSize := Round(lw / fw * (Max - Min));
 
185
end;
 
186
 
 
187
constructor TChartNavScrollBar.Create(AOwner: TComponent);
 
188
begin
 
189
  inherited Create(AOwner);
 
190
  FListener := TListener.Create(@FChart, @ChartExtentChanged);
 
191
end;
 
192
 
 
193
destructor TChartNavScrollBar.Destroy;
 
194
begin
 
195
  FreeAndNil(FListener);
 
196
  inherited Destroy;
 
197
end;
 
198
 
 
199
procedure TChartNavScrollBar.Scroll(
 
200
  AScrollCode: TScrollCode; var AScrollPos: Integer);
 
201
var
 
202
  fe, le: TDoubleRect;
 
203
  d, w: Double;
 
204
begin
 
205
  inherited Scroll(AScrollCode, AScrollPos);
 
206
  if Chart = nil then exit;
 
207
  w := Max - Min;
 
208
  if w = 0 then exit;
 
209
  fe := Chart.GetFullExtent;
 
210
  le := Chart.LogicalExtent;
 
211
  if le = EmptyExtent then
 
212
    le := fe;
 
213
  case Kind of
 
214
    sbHorizontal: begin
 
215
      d := WeightedAverage(fe.a.X, fe.b.X, Position / w);
 
216
      le.b.X += d - le.a.X;
 
217
      le.a.X := d;
 
218
    end;
 
219
    sbVertical: begin
 
220
      d := WeightedAverage(fe.b.Y, fe.a.Y, Position / w);
 
221
      le.b.Y += d - le.a.Y;
 
222
      le.a.Y := d;
 
223
    end;
 
224
  end;
 
225
  Chart.LogicalExtent := le;
 
226
  // Focused ScrollBar is glitchy under Win32, especially after PageSize change.
 
227
  if (GetParentForm(Chart) <> nil) and GetParentForm(Chart).Active then
 
228
    Chart.SetFocus;
 
229
end;
 
230
 
 
231
procedure TChartNavScrollBar.SetAutoPageSize(AValue: Boolean);
 
232
begin
 
233
  if FAutoPageSize = AValue then exit;
 
234
  FAutoPageSize := AValue;
 
235
  ChartExtentChanged(Self);
 
236
end;
 
237
 
 
238
procedure TChartNavScrollBar.SetChart(AValue: TChart);
 
239
begin
 
240
  if FChart = AValue then exit;
 
241
 
 
242
  if FListener.IsListening then
 
243
    FChart.ExtentBroadcaster.Unsubscribe(FListener);
 
244
  FChart := AValue;
 
245
  if FChart <> nil then
 
246
    FChart.ExtentBroadcaster.Subscribe(FListener);
 
247
  ChartExtentChanged(Self);
 
248
end;
 
249
 
 
250
{ TChartNavPanel }
 
251
 
 
252
procedure TChartNavPanel.ChartExtentChanged(ASender: TObject);
 
253
begin
 
254
  Unused(ASender);
 
255
  Invalidate;
 
256
end;
 
257
 
 
258
constructor TChartNavPanel.Create(AOwner: TComponent);
 
259
const
 
260
  DEF_WIDTH = 40;
 
261
  DEF_HEIGHT = 20;
 
262
begin
 
263
  inherited Create(AOwner);
 
264
  FListener := TListener.Create(@FChart, @ChartExtentChanged);
 
265
  FFullExtentPen := TPen.Create;
 
266
  FFullExtentPen.OnChange := @ChartExtentChanged;
 
267
  FLogicalExtentPen := TPen.Create;
 
268
  FLogicalExtentPen.OnChange := @ChartExtentChanged;
 
269
  FLogicalExtentRect := Rect(0, 0, 0, 0);
 
270
  Width := DEF_WIDTH;
 
271
  Height := DEF_HEIGHT;
 
272
  FAllowDragNavigation := true;
 
273
  FDragCursor := crSizeAll;
 
274
  FShift := [ssLeft];
 
275
end;
 
276
 
 
277
destructor TChartNavPanel.Destroy;
 
278
begin
 
279
  FreeAndNil(FListener);
 
280
  FreeAndNil(FFullExtentPen);
 
281
  FreeAndNil(FLogicalExtentPen);
 
282
  inherited Destroy;
 
283
end;
 
284
 
 
285
procedure TChartNavPanel.MouseDown(
 
286
  AButton: TMouseButton; AShift: TShiftState; AX, AY: Integer);
 
287
begin
 
288
  if (Chart <> nil) and AllowDragNavigation then begin
 
289
    FPrevPoint := (DoublePoint(AX, Height - AY) - FOffset) / FScale;
 
290
    FIsDragging :=
 
291
      (AShift = Shift) and IsPointInRect(Point(AX, AY), FLogicalExtentRect);
 
292
    if FIsDragging then begin
 
293
      FOldCursor := Cursor;
 
294
      Cursor := DragCursor;
 
295
    end;
 
296
  end;
 
297
  inherited MouseDown(AButton, AShift, AX, AY);
 
298
end;
 
299
 
 
300
procedure TChartNavPanel.MouseMove(AShift: TShiftState; AX, AY: Integer);
 
301
var
 
302
  p: TDoublePoint;
 
303
  le: TDoubleRect;
 
304
begin
 
305
  if (Chart <> nil) and FIsDragging then begin
 
306
    p := (DoublePoint(AX, Height - AY) - FOffset) / FScale;
 
307
    le := Chart.LogicalExtent;
 
308
    le.a += p - FPrevPoint;
 
309
    le.b += p - FPrevPoint;
 
310
    Chart.LogicalExtent := le;
 
311
    FPrevPoint := p;
 
312
  end;
 
313
  inherited MouseMove(AShift, AX, AY);
 
314
end;
 
315
 
 
316
procedure TChartNavPanel.MouseUp(
 
317
  AButton: TMouseButton; AShift: TShiftState; AX, AY: Integer);
 
318
begin
 
319
  if FIsDragging then
 
320
    Cursor := FOldCursor;
 
321
  FIsDragging := false;
 
322
  inherited MouseUp(AButton, AShift, AX, AY);
 
323
end;
 
324
 
 
325
procedure TChartNavPanel.Paint;
 
326
 
 
327
  function GraphRect(ARect: TDoubleRect): TRect;
 
328
  begin
 
329
    with ARect do begin
 
330
      a := a * FScale + FOffset;
 
331
      b := b * FScale + FOffset;
 
332
      Result := Rect(
 
333
        Round(a.X), Height - Round(b.Y), Round(b.X), Height - Round(a.Y));
 
334
    end;
 
335
  end;
 
336
 
 
337
var
 
338
  fe, le, ext: TDoubleRect;
 
339
  sz: TDoublePoint;
 
340
  oldAxisVisible: Boolean;
 
341
  feRect: TRect;
 
342
begin
 
343
  if Chart = nil then exit;
 
344
  fe := Chart.GetFullExtent;
 
345
  le := Chart.LogicalExtent;
 
346
  if le = EmptyExtent then
 
347
    le := fe;
 
348
  ext := fe;
 
349
  ExpandRect(ext, le.a);
 
350
  ExpandRect(ext, le.b);
 
351
  sz := ext.b - ext.a;
 
352
  if (sz.X <= 0) or (sz.Y <= 0) then exit;
 
353
  FScale := DoublePoint(Width, Height) / sz;
 
354
  FOffset := ZeroDoublePoint;
 
355
  if Proportional then begin
 
356
    if FScale.X < FScale.Y then begin
 
357
      FScale.Y := FScale.X;
 
358
      FOffset.Y := (Height - sz.Y * FScale.Y) / 2;
 
359
    end
 
360
    else begin
 
361
      FScale.X := FScale.Y;
 
362
      FOffset.X := (Width - sz.X * FScale.X) / 2;
 
363
    end;
 
364
  end;
 
365
  FOffset -= ext.a * FScale;
 
366
 
 
367
  feRect := GraphRect(fe);
 
368
  if MiniMap then begin
 
369
    oldAxisVisible := Chart.AxisVisible;
 
370
    Chart.AxisVisible := false;
 
371
    Chart.PaintOnAuxCanvas(Canvas, feRect);
 
372
    Chart.AxisVisible := oldAxisVisible;
 
373
  end
 
374
  else begin
 
375
    Canvas.Brush.Color := Chart.BackColor;
 
376
    Canvas.Brush.Style := bsSolid;
 
377
    Canvas.FillRect(ClientRect);
 
378
  end;
 
379
  Canvas.Brush.Style := bsClear;
 
380
  Canvas.Pen := FullExtentPen;
 
381
  Canvas.Rectangle(feRect);
 
382
  Canvas.Pen := LogicalExtentPen;
 
383
  FLogicalExtentRect := GraphRect(le);
 
384
  Canvas.Rectangle(FLogicalExtentRect);
 
385
end;
 
386
 
 
387
procedure TChartNavPanel.SetChart(AValue: TChart);
 
388
begin
 
389
  if FChart = AValue then exit;
 
390
 
 
391
  if FListener.IsListening then
 
392
    FChart.ExtentBroadcaster.Unsubscribe(FListener);
 
393
  FChart := AValue;
 
394
  if FChart <> nil then
 
395
    FChart.ExtentBroadcaster.Subscribe(FListener);
 
396
  ChartExtentChanged(Self);
 
397
end;
 
398
 
 
399
procedure TChartNavPanel.SetDragCursor(AValue: TCursor);
 
400
begin
 
401
  if FDragCursor = AValue then exit;
 
402
  FDragCursor := AValue;
 
403
  if MouseCapture then
 
404
    Cursor := FDragCursor;
 
405
end;
 
406
 
 
407
procedure TChartNavPanel.SetFullExtentPen(AValue: TPen);
 
408
begin
 
409
  if FFullExtentPen = AValue then exit;
 
410
  FFullExtentPen := AValue;
 
411
  Invalidate;
 
412
end;
 
413
 
 
414
procedure TChartNavPanel.SetLogicalExtentPen(AValue: TPen);
 
415
begin
 
416
  if FLogicalExtentPen = AValue then exit;
 
417
  FLogicalExtentPen := AValue;
 
418
  Invalidate;
 
419
end;
 
420
 
 
421
procedure TChartNavPanel.SetMiniMap(AValue: Boolean);
 
422
begin
 
423
  if FMiniMap = AValue then exit;
 
424
  FMiniMap := AValue;
 
425
  Invalidate;
 
426
end;
 
427
 
 
428
procedure TChartNavPanel.SetProportional(AValue: Boolean);
 
429
begin
 
430
  if FProportional = AValue then exit;
 
431
  FProportional := AValue;
 
432
  Invalidate;
 
433
end;
 
434
 
 
435
end.
 
436