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