source: tags/1.3.1/LocalPlayer/PVSB.pas

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