| 1 | {$INCLUDE Switches.inc}
|
|---|
| 2 | unit PVSB;
|
|---|
| 3 |
|
|---|
| 4 | interface
|
|---|
| 5 |
|
|---|
| 6 | uses
|
|---|
| 7 | {$IFDEF WINDOWS}
|
|---|
| 8 | Windows,
|
|---|
| 9 | {$ENDIF}
|
|---|
| 10 | Classes, Controls, Forms, LCLIntf, LCLType, LMessages, Messages, SysUtils,
|
|---|
| 11 | StdCtrls, Math;
|
|---|
| 12 |
|
|---|
| 13 | type
|
|---|
| 14 |
|
|---|
| 15 | { TPVScrollbar }
|
|---|
| 16 |
|
|---|
| 17 | TPVScrollBar = class
|
|---|
| 18 | private
|
|---|
| 19 | FOnUpdate: TNotifyEvent;
|
|---|
| 20 | ScrollBar: TScrollBar;
|
|---|
| 21 | FMax: Integer;
|
|---|
| 22 | function GetMax: Integer;
|
|---|
| 23 | function GetPageSize: Integer;
|
|---|
| 24 | function GetPosition: Integer;
|
|---|
| 25 | procedure ScrollBarChanged(Sender: TObject);
|
|---|
| 26 | procedure SetMax(AValue: Integer);
|
|---|
| 27 | procedure SetPageSize(AValue: Integer);
|
|---|
| 28 | procedure SetPosition(AValue: Integer);
|
|---|
| 29 | public
|
|---|
| 30 | constructor Create(Parent: TWinControl);
|
|---|
| 31 | destructor Destroy; override;
|
|---|
| 32 | procedure Init(Max, PageSize: Integer);
|
|---|
| 33 | procedure SetPos(Pos: Integer);
|
|---|
| 34 | function Process(const m: TMessage): boolean;
|
|---|
| 35 | function ProcessMouseWheel(Delta: Integer): Boolean;
|
|---|
| 36 | procedure Show(Visible: boolean);
|
|---|
| 37 | procedure EndSB;
|
|---|
| 38 | procedure SetBorderSpacing(Top, Right, Bottom: Integer);
|
|---|
| 39 | property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
|
|---|
| 40 | property Position: Integer read GetPosition write SetPosition;
|
|---|
| 41 | property Max: Integer read GetMax write SetMax;
|
|---|
| 42 | property PageSize: Integer read GetPageSize write SetPageSize;
|
|---|
| 43 | end;
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | implementation
|
|---|
| 47 |
|
|---|
| 48 | const
|
|---|
| 49 | Count: Integer = 0;
|
|---|
| 50 |
|
|---|
| 51 | procedure TPVScrollBar.Init(Max, PageSize: Integer);
|
|---|
| 52 | begin
|
|---|
| 53 | ScrollBar.PageSize := PageSize;
|
|---|
| 54 | ScrollBar.Min := 0;
|
|---|
| 55 | Self.Max := Max;
|
|---|
| 56 | ScrollBar.Position := 0;
|
|---|
| 57 | ScrollBar.Visible := Max >= ScrollBar.PageSize;
|
|---|
| 58 | end;
|
|---|
| 59 |
|
|---|
| 60 | procedure TPVScrollBar.SetPos(Pos: Integer);
|
|---|
| 61 | begin
|
|---|
| 62 | if Pos <> 0 then begin
|
|---|
| 63 | ScrollBar.Position := Pos;
|
|---|
| 64 | end;
|
|---|
| 65 | end;
|
|---|
| 66 |
|
|---|
| 67 | function TPVScrollBar.Process(const m: TMessage): boolean;
|
|---|
| 68 | var
|
|---|
| 69 | NewPos: integer;
|
|---|
| 70 | begin
|
|---|
| 71 | if Max < ScrollBar.PageSize then
|
|---|
| 72 | result := false
|
|---|
| 73 | else
|
|---|
| 74 | begin
|
|---|
| 75 | if (m.wParam and $ffff) in [SB_THUMBPOSITION, SB_THUMBTRACK] then
|
|---|
| 76 | begin
|
|---|
| 77 | result := ((m.wParam shr 16) and $ffff) <> ScrollBar.Position;
|
|---|
| 78 | ScrollBar.Position := (m.wParam shr 16) and $ffff;
|
|---|
| 79 | end else begin
|
|---|
| 80 | case (m.wParam and $ffff) of
|
|---|
| 81 | SB_LINEUP:
|
|---|
| 82 | NewPos := ScrollBar.Position - 1;
|
|---|
| 83 | SB_LINEDOWN:
|
|---|
| 84 | NewPos := ScrollBar.Position + 1;
|
|---|
| 85 | SB_PAGEUP:
|
|---|
| 86 | NewPos := ScrollBar.Position - ScrollBar.PageSize;
|
|---|
| 87 | SB_PAGEDOWN:
|
|---|
| 88 | NewPos := ScrollBar.Position + ScrollBar.PageSize;
|
|---|
| 89 | else
|
|---|
| 90 | NewPos := ScrollBar.Position
|
|---|
| 91 | end;
|
|---|
| 92 | if NewPos < 0 then
|
|---|
| 93 | NewPos := 0;
|
|---|
| 94 | if NewPos > Max - ScrollBar.PageSize + 1 then
|
|---|
| 95 | NewPos := Max - ScrollBar.PageSize + 1;
|
|---|
| 96 | result := NewPos <> ScrollBar.Position;
|
|---|
| 97 | if (NewPos <> ScrollBar.Position) or ((m.wParam and $ffff) = SB_ENDSCROLL) then
|
|---|
| 98 | begin
|
|---|
| 99 | ScrollBar.Position := NewPos;
|
|---|
| 100 | end;
|
|---|
| 101 | end;
|
|---|
| 102 | end;
|
|---|
| 103 | end;
|
|---|
| 104 |
|
|---|
| 105 | function TPVScrollBar.ProcessMouseWheel(Delta: Integer): Boolean;
|
|---|
| 106 | var
|
|---|
| 107 | NewPos: integer;
|
|---|
| 108 | begin
|
|---|
| 109 | if Max < ScrollBar.PageSize then Result := False
|
|---|
| 110 | else begin
|
|---|
| 111 | NewPos := ScrollBar.Position - Delta div 30;
|
|---|
| 112 | if NewPos < 0 then NewPos := 0;
|
|---|
| 113 | if NewPos > Max - ScrollBar.PageSize + 1 then
|
|---|
| 114 | NewPos := Max - ScrollBar.PageSize + 1;
|
|---|
| 115 | Result := NewPos <> ScrollBar.Position;
|
|---|
| 116 | if NewPos <> ScrollBar.Position then begin
|
|---|
| 117 | ScrollBar.Position := NewPos;
|
|---|
| 118 | end;
|
|---|
| 119 | end;
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | procedure TPVScrollBar.Show(Visible: boolean);
|
|---|
| 123 | begin
|
|---|
| 124 | if not Visible or (Max < ScrollBar.PageSize) then
|
|---|
| 125 | ScrollBar.Visible := False
|
|---|
| 126 | else ScrollBar.Visible := True;
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | procedure TPVScrollBar.EndSB;
|
|---|
| 130 | begin
|
|---|
| 131 | if Max < ScrollBar.PageSize then
|
|---|
| 132 | ScrollBar.Position := 0 // hidden
|
|---|
| 133 | else begin
|
|---|
| 134 | ScrollBar.Position := Max - ScrollBar.PageSize + 1;
|
|---|
| 135 | end;
|
|---|
| 136 | end;
|
|---|
| 137 |
|
|---|
| 138 | procedure TPVScrollBar.SetBorderSpacing(Top, Right, Bottom: Integer);
|
|---|
| 139 | begin
|
|---|
| 140 | ScrollBar.BorderSpacing.Top := Top;
|
|---|
| 141 | ScrollBar.BorderSpacing.Right := Right;
|
|---|
| 142 | ScrollBar.BorderSpacing.Bottom := Bottom;
|
|---|
| 143 | end;
|
|---|
| 144 |
|
|---|
| 145 | { TPVScrollbar }
|
|---|
| 146 |
|
|---|
| 147 | procedure TPVScrollBar.ScrollBarChanged(Sender: TObject);
|
|---|
| 148 | begin
|
|---|
| 149 | if Assigned(FOnUpdate) then FOnUpdate(Self);
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 | procedure TPVScrollBar.SetMax(AValue: Integer);
|
|---|
| 153 | begin
|
|---|
| 154 | FMax := AValue;
|
|---|
| 155 | ScrollBar.Max := Math.Max(0, FMax);
|
|---|
| 156 | end;
|
|---|
| 157 |
|
|---|
| 158 | procedure TPVScrollBar.SetPageSize(AValue: Integer);
|
|---|
| 159 | begin
|
|---|
| 160 | ScrollBar.PageSize := AValue;
|
|---|
| 161 | end;
|
|---|
| 162 |
|
|---|
| 163 | function TPVScrollBar.GetPosition: Integer;
|
|---|
| 164 | begin
|
|---|
| 165 | Result := ScrollBar.Position;
|
|---|
| 166 | end;
|
|---|
| 167 |
|
|---|
| 168 | function TPVScrollBar.GetMax: Integer;
|
|---|
| 169 | begin
|
|---|
| 170 | Result := FMax;
|
|---|
| 171 | end;
|
|---|
| 172 |
|
|---|
| 173 | function TPVScrollBar.GetPageSize: Integer;
|
|---|
| 174 | begin
|
|---|
| 175 | Result := ScrollBar.PageSize;
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | procedure TPVScrollBar.SetPosition(AValue: Integer);
|
|---|
| 179 | begin
|
|---|
| 180 | ScrollBar.Position := AValue;
|
|---|
| 181 | end;
|
|---|
| 182 |
|
|---|
| 183 | constructor TPVScrollBar.Create(Parent: TWinControl);
|
|---|
| 184 | begin
|
|---|
| 185 | Inc(Count);
|
|---|
| 186 | ScrollBar := TScrollBar.Create(Parent);
|
|---|
| 187 | ScrollBar.Kind := sbVertical;
|
|---|
| 188 | ScrollBar.Name := 'PVSB' + IntToStr(Count);
|
|---|
| 189 | ScrollBar.Align := alRight;
|
|---|
| 190 | ScrollBar.OnChange := ScrollBarChanged;
|
|---|
| 191 | ScrollBar.Parent := Parent;
|
|---|
| 192 | end;
|
|---|
| 193 |
|
|---|
| 194 | destructor TPVScrollBar.Destroy;
|
|---|
| 195 | begin
|
|---|
| 196 | FreeAndNil(ScrollBar);
|
|---|
| 197 | end;
|
|---|
| 198 |
|
|---|
| 199 | end.
|
|---|