summaryrefslogtreecommitdiff
path: root/src/gui/fpg_scrollbar.pas
blob: 1ec78952c136f9ba712aa4dad0d46fb2fb2b2ea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2014 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      Defines Scrollbar controls.
}

unit fpg_scrollbar;

{$mode objfpc}{$H+}

{
  TODO:
    * Create property to enable dynamic sizing of slider button length.
}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_main,
  fpg_widget;

type
  TScrollNotifyEvent = procedure(Sender: TObject; position: integer) of object;

  TfpgScrollStyle = (ssNone, ssHorizontal, ssVertical, ssAutoBoth, ssBothVisible);
  
  TfpgScrollBarPart = (sbpNone, sbpUpBack, sbpPageUpBack, sbpSlider, sbpDownForward, sbpPageDownForward);


  TfpgScrollBar = class(TfpgWidget)
  private
    FLargeChange: Integer;
    FScrollbarDownPart: TfpgScrollBarPart;
    FRecalc: Boolean;
    procedure   SetMax(const AValue: integer);
    procedure   SetMin(const AValue: integer);
    procedure   SetSBPosition(const AValue: integer);
    procedure   Step(ASteps: Integer);
    procedure   StepPage(ASteps: Integer);
    procedure   StepStart;
    procedure   StepEnd;
  protected
    FMax: integer;
    FMin: integer;
    FPageSize: integer;
    FPosition: integer;
    FScrollStep: integer;
    FSliderPos: TfpgCoord;
    FSliderLength: TfpgCoord;
    FSliderDragPos: TfpgCoord;
    FSliderDragStart: TfpgCoord;
    FScrollTimer: TfpgTimer;
    FActiveButtonRect: TfpgRect;
    FMousePosition: TPoint;
    FOnScroll: TScrollNotifyEvent;
    procedure   ScrollTimer(Sender: TObject);
    procedure   DrawButton(x, y, w, h: TfpgCoord; const imgname: string; Pressed: Boolean = False; const ButtonEnabled: Boolean= True); virtual;
    procedure   DrawSlider(recalc: boolean); virtual;
    procedure   HandleLMouseDown(x, y: integer; shiftstate: TShiftState); override;
    procedure   HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
    procedure   HandleMouseMove(x, y: integer; btnstate: word; shiftstate: TShiftState); override;
    procedure   HandleMouseScroll(x, y: integer; shiftstate: TShiftState; delta: smallint); override;
    procedure   HandlePaint; override;
    procedure   HandleResize(AWidth, AHeight: TfpgCoord); override;
    procedure   PositionChange(d: integer);
  public
    Orientation: TOrientation;
    SliderSize: double;  // 0-1
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   RepaintSlider;
    procedure   LineUp;
    procedure   LineDown;
    procedure   PageUp;
    procedure   PageDown;
    property    PageSize: integer read FPageSize write FPageSize default 5;
    property    Position: integer read FPosition write SetSBPosition default 10;
    property    ScrollStep: integer read FScrollStep write FScrollStep default 1;
//    property    LargeChange: Integer read FLargeChange write FLargeChange default 0;
    property    Min: integer read FMin write SetMin default 0;
    property    Max: integer read FMax write SetMax default 100;
    property    OnScroll: TScrollNotifyEvent read FOnScroll write FOnScroll;
  published
    property    Align;
  end;


implementation

const
  cMinSliderLength = 20;

{ TfpgScrollBar }

constructor TfpgScrollBar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FScrollTimer := TfpgTimer.Create(500);
  FScrollTimer.Enabled := False;
  FScrollTimer.OnTimer := @ScrollTimer;
  Orientation   := orVertical;
  FMin          := 0;
  FMax          := 100;
  FPosition     := 10;
  SliderSize    := 0.5;
  FOnScroll     := nil;
  FSliderPos    := 0;
  FSliderLength := cMinSliderLength;
  FScrollStep   := 1;
  FPageSize     := 5;
  FLargeChange  := 0;
  FRecalc := True;
end;

destructor TfpgScrollBar.Destroy;
begin
  FScrollTimer.Free;
  inherited Destroy;
end;

procedure TfpgScrollBar.HandlePaint;
begin
  if Orientation = orVertical then
  begin
    DrawButton(0, 0, Width, Width, 'sys.sb.up', (FScrollbarDownPart = sbpUpBack) and (FPosition <> FMin), (FPosition <> FMin) and (Parent.Enabled));
    DrawButton(0, Height-Width, Width, Width, 'sys.sb.down', (FScrollbarDownPart = sbpDownForward) and (FPosition <> FMax), (FPosition <> FMax) and (Parent.Enabled));
  end
  else
  begin
    DrawButton(0, 0, Height, Height, 'sys.sb.left', (FScrollbarDownPart = sbpUpBack) and (FPosition <> FMin), (FPosition <> FMin) and (Parent.Enabled));
    DrawButton(Width-Height, 0, Height, Height, 'sys.sb.right', (FScrollbarDownPart = sbpDownForward) and (FPosition <> FMax), (FPosition <> FMax) and (Parent.Enabled));
  end;
  DrawSlider(FRecalc);
  FRecalc := False;
end;

procedure TfpgScrollBar.HandleResize(AWidth, AHeight: TfpgCoord);
begin
  inherited HandleResize(AWidth, AHeight);
  FRecalc := True;
end;

procedure TfpgScrollBar.RepaintSlider;
begin
  if not HasHandle then
    Exit; //==>
  FRecalc := True;
  Invalidate;
end;

procedure TfpgScrollBar.LineUp;
begin
  Step(-1);
end;

procedure TfpgScrollBar.LineDown;
begin
  Step(1);
end;

procedure TfpgScrollBar.PageUp;
begin
  StepPage(-1);
end;

procedure TfpgScrollBar.PageDown;
begin
  StepPage(1);
end;

procedure TfpgScrollBar.SetMax(const AValue: integer);
begin
  if AValue = FMax then
    Exit;
  if AValue < FMin then
    FMax := FMin
  else
    FMax := AValue;
  if FPosition > FMax then
    StepEnd;
end;

procedure TfpgScrollBar.SetMin(const AValue: integer);
begin
  if AValue = FMin then
    Exit;
  if AValue > FMax then
    FMin := FMax
  else
    FMin := AValue;
  if FPosition < FMin then
    StepStart;
end;

procedure TfpgScrollBar.SetSBPosition(const AValue: integer);
begin
  if AValue < FMin then
    FPosition := FMin
  else if AValue > FMax then
    FPosition := FMax
  else
    FPosition := AValue;

  if HasHandle then
    Invalidate;
end;

procedure TfpgScrollBar.Step(ASteps: Integer);
begin
  PositionChange(FScrollStep*ASteps);
end;

procedure TfpgScrollBar.StepPage(ASteps: Integer);
begin
  PositionChange(ASteps*FPageSize);
end;

procedure TfpgScrollBar.StepStart;
begin
  SetSBPosition(FMin)
end;

procedure TfpgScrollBar.StepEnd;
begin
  SetSBPosition(FMax);
end;

procedure TfpgScrollBar.ScrollTimer(Sender: TObject);
   function WithinActiveButton: Boolean;
   begin
     Result := (FMousePosition.X < FActiveButtonRect.Right)
           and (FMousePosition.X > FActiveButtonRect.Left)
           and (FMousePosition.Y < FActiveButtonRect.Bottom)
           and (FMousePosition.Y > FActiveButtonRect.Top);
   end;
   function WithinPageArea(IsBefore: Boolean): Boolean;
   begin
     case Orientation of
       orVertical:
         if IsBefore then
           Result := (FMousePosition.X > -1)
                 and (FMousePosition.X < Width)
                 and (FMousePosition.Y < FSliderPos + Width)
                 and (FMousePosition.Y > Width)
         else
           Result := (FMousePosition.X > -1)
                 and (FMousePosition.X < Width)
                 and (FMousePosition.Y < Height - Width)
                 and (FMousePosition.Y > Width + FSliderPos + FSliderLength);
       orHorizontal:
         if IsBefore then
           Result := (FMousePosition.X > Height)
                 and (FMousePosition.X < FSliderPos + Height)
                 and (FMousePosition.Y < Height)
                 and (FMousePosition.Y > -1)
         else
           Result := (FMousePosition.X > Height + FSliderPos + FSliderLength)
                 and (FMousePosition.X < Width - Height)
                 and (FMousePosition.Y < Height)
                 and (FMousePosition.Y > -1);
     end;
   end;
begin

  case FScrollbarDownPart of
    sbpDownForward,
    sbpUpBack           : FScrollTimer.Interval := 25;
    sbpPageDownForward,
    sbpPageUpBack       : FScrollTimer.Interval := 50;
  end;

  case FScrollbarDownPart of
    sbpUpBack:
      begin
        if WithinActiveButton then
          Step(-1);
        if Position = FMin then
          FScrollTimer.Enabled := False;
      end;
    sbpDownForward:
      begin
        if WithinActiveButton then
          Step(1);
        if Position = FMax then
          FScrollTimer.Enabled := False;
      end;
    sbpPageUpBack:
      begin
        if (Position = FMin) or not WithinPageArea(True) then
          FScrollTimer.Enabled := False
        else
          StepPage(-1);
      end;
    sbpPageDownForward:
      begin
        if (Position = FMax) or not WithinPageArea(False) then
          FScrollTimer.Enabled := False
        else
          StepPage(1);
      end;
  else
    FScrollTimer.Enabled := False;
  end;
end;

// only called from inside HandlePaint so no need for BeginDraw..EndDraw calls
procedure TfpgScrollBar.DrawButton(x, y, w, h: TfpgCoord; const imgname: string; Pressed: Boolean = False; const ButtonEnabled: Boolean= True);
var
  img, imgdisabled: TfpgImage;
  dx: integer;
  dy: integer;
begin
  if Pressed then
  begin
    Canvas.DrawButtonFace(x, y, w, h, [btfIsEmbedded, btfIsPressed]);
    dx := 1;
    dy := 1;
  end
  else
  begin
    Canvas.DrawButtonFace(x, y, w, h, [btfIsEmbedded]);
    dx := 0;
    dy := 0;
  end;
  Canvas.SetColor(clText1);
  img := fpgImages.GetImage(imgname);
  if img <> nil then
  begin
    if ButtonEnabled then
      Canvas.DrawImage(x + w div 2 - (img.Width div 2) + dx, y + h div 2 - (img.Height div 2) + dy, img)
    else
    begin
      imgdisabled := img.CreateDisabledImage;
      Canvas.DrawImage(x + w div 2 - (img.Width div 2) + dx, y + h div 2 - (img.Height div 2) + dy, imgdisabled);
      imgdisabled.Free;
    end;
  end;
end;

// only called from inside HandlePaint so no need for BeginDraw..EndDraw calls
procedure TfpgScrollBar.DrawSlider(recalc: boolean);
var
  area: TfpgCoord;
  mm: TfpgCoord;
begin
  if SliderSize > 1 then
    SliderSize := 1;

  Canvas.SetColor(clScrollBar);

  if Orientation = orVertical then
  begin
    Canvas.FillRectangle(0, Width, Width, Height - (2 * Width));
    area := Height - (Width shl 1);
  end
  else
  begin
    Canvas.FillRectangle(Height, 0, Width - (2 * Height), Height);
    area := Width - (Height shl 1);
  end;

  if recalc then
  begin
    if FPosition > FMax then
      FPosition := FMax;
    if FPosition < FMin then
      FPosition := FMin;

    FSliderLength := Trunc(area * SliderSize);
    //FSliderLength := Trunc((width/area) * (fmax /area  ));
    if FSliderLength < cMinSliderLength then
      FSliderLength := cMinSliderLength;
    if FSliderLength > area then
      FSliderLength := area;
    area := area - FSliderLength;
    mm   := FMax - FMin;
    if mm = 0 then
      FSliderPos := 0
    else
      FSliderPos := Trunc(area * ((FPosition - FMin) / mm));
  end;

  // Paint the area between the buttons and the Slider
  if Orientation = orVertical then
  begin
    if FScrollbarDownPart in [{sbpUpBack,} sbpPageUpBack] then
    begin
      Canvas.SetColor(clShadow1);
      Canvas.FillRectangle(0, Width, Width, FSliderPos);
      Canvas.SetColor(clScrollBar);
    end
    else if FScrollbarDownPart in [{sbpDownForward,} sbpPageDownForward] then
    begin
      Canvas.SetColor(clShadow1);
      Canvas.FillRectangle(0, Width + FSliderPos + FSliderLength, Width, Height - (2 * Width) - (FSliderPos + FSliderLength));
      Canvas.SetColor(clScrollBar);
    end;
  end
  else
  begin
    if FScrollbarDownPart in [{sbpUpBack,} sbpPageUpBack] then
    begin
      Canvas.SetColor(clShadow1);
      Canvas.FillRectangle(Height, 0, FSliderPos, Height);
      Canvas.SetColor(clScrollBar);
    end
    else if FScrollbarDownPart in [{sbpDownForward,} sbpPageDownForward] then
    begin
      Canvas.SetColor(clShadow1);
      Canvas.FillRectangle(Height + FSliderPos + FSliderLength, 0, Width - (2 * Height) - (FSliderPos + FSliderLength), Height);
      Canvas.SetColor(clScrollBar);
    end;
  end;

  // Paint the slider button
  if Orientation = orVertical then
    Canvas.DrawButtonFace(0, Width + FSliderPos, Width, FSliderLength, [btfIsEmbedded])
  else
    Canvas.DrawButtonFace(Height + FSliderPos, 0, FSliderLength, Height, [btfIsEmbedded]);
end;

procedure TfpgScrollBar.HandleLMouseDown(x, y: integer; shiftstate: TShiftState);
var
  lPos: TfpgCoord;
begin
  inherited;
  CaptureMouse;

  if Orientation = orVertical then
  begin
    if y <= Width then
    begin
      // Up button has been pressed
      Step(-1);
      FScrollbarDownPart := sbpUpBack;
      FActiveButtonRect.SetRect(0, 0, Width, Width);
    end
    else if y >= Height - Width then
    begin
      // Down button has been pressed
      Step(1);
      FScrollbarDownPart := sbpDownForward;
      FActiveButtonRect.SetRect(0,Height-Width, Width, Height);
    end
    else if (y >= (Width + FSliderPos)) and (y <= Width + FSliderPos + FSliderLength) then
    begin
      FScrollbarDownPart := sbpSlider;
      FSliderDragPos  := y;
    end
    else if (y > Width) and (y < (Width + FSliderPos)) then
    begin
      // Clicked between Up button and Slider
      FScrollbarDownPart := sbpPageUpBack;
      StepPage(-1);
    end
    else if (y < (Height - Width)) and (y > (Width + FSliderPos + FSliderLength)) then
    begin
      // Clicked between Down button and Slider
      FScrollbarDownPart := sbpPageDownForward;
      StepPage(1);
    end;
  end
  else
  begin
    if x <= Height then
    begin
      // Left button has been pressed
      StepPage(-1);
      FScrollbarDownPart := sbpUpBack;
      FActiveButtonRect.SetRect(0, 0, Height, Height);
    end
    else if x >= Width - Height then
    begin
      // Right button has been pressed
      StepPage(1);
      FScrollbarDownPart := sbpDownForward;
      FActiveButtonRect.SetRect(Width-Height, 0, Width, Height);
    end
    else if (x >= (Height + FSliderPos)) and (x <= Height + FSliderPos + FSliderLength) then
    begin
      FScrollbarDownPart := sbpSlider;
      FSliderDragPos  := x;
    end
    else if (x > Height) and (x < (Height + FSliderPos)) then
    begin
      // Clicked between Left button and Slider
      FScrollbarDownPart := sbpPageUpBack;
      StepPage(-1);
    end
    else if (x < (Width - Height)) and (x > (Height + FSliderPos + FSliderLength)) then
    begin
      // Clicked between the Right button and Slider
      FScrollbarDownPart := sbpPageDownForward;
      StepPage(1);
    end;
  end;
  
  if FScrollbarDownPart = sbpSlider then
  begin
    FSliderDragStart := FSliderPos;
    Invalidate;
  end
  else if not (FScrollbarDownPart in [sbpNone, sbpSlider]) then
  begin
    FScrollTimer.Interval := 300;
    FScrollTimer.Enabled := True;
    Invalidate;
  end;
end;

procedure TfpgScrollBar.HandleLMouseUp(x, y: integer; shiftstate: TShiftState);
var
  WasPressed: Boolean;
begin
  inherited;
  ReleaseMouse;
  
  WasPressed := FScrollbarDownPart <> sbpNone;
  FScrollTimer.Enabled := False;

  FScrollbarDownPart := sbpNone;
  
  if WasPressed then
    Invalidate;
end;

procedure TfpgScrollBar.HandleMouseMove(x, y: integer; btnstate: word; shiftstate: TShiftState);
var
  d: integer;
  area: integer;
  newp: integer;
  ppos: integer;
begin
  inherited HandleMouseMove(x, y, btnstate, shiftstate);

  FMousePosition.X := x;
  FMousePosition.Y := y;
  
  if (FScrollbarDownPart <> sbpSlider) or ((btnstate and MOUSE_LEFT) = 0) then
    Exit;

  if Orientation = orVertical then
  begin
    d    := y - FSliderDragPos;
    area := Height - (Width shl 1) - FSliderLength;
  end
  else
  begin
    d    := x - FSliderDragPos;
    area := Width - (Height shl 1) - FSliderLength;
  end;

  ppos       := FSliderPos;
  FSliderPos := FSliderDragStart + d;

  if FSliderPos < 0 then
    FSliderPos := 0;
  if FSliderPos > area then
    FSliderPos := area;

  if ppos <> FSliderPos then
    Invalidate;

  if area <> 0 then
    newp := FMin + Trunc((FMax - FMin)  * (FSliderPos / area))
  else
    newp := FMin;

  if newp <> FPosition then
  begin
    Position := newp;
    if Assigned(FOnScroll) then
      FOnScroll(self, FPosition);
  end;
end;

procedure TfpgScrollBar.HandleMouseScroll(x, y: integer; shiftstate: TShiftState;
  delta: smallint);
begin
  inherited HandleMouseScroll(x, y, shiftstate, delta);
  Step(delta);
end;

procedure TfpgScrollBar.PositionChange(d: integer);
begin
  FPosition := FPosition + d;
  if FPosition < FMin then
    FPosition := FMin;
  if FPosition > FMax then
    FPosition := FMax;

  if Visible then
  begin
    FRecalc := True;
    Invalidate;
  end;

  if Assigned(FOnScroll) then
    FOnScroll(self, FPosition);
end;

end.