summaryrefslogtreecommitdiff
path: root/installer/scroll.pas
blob: 803c071d621fe63d643587201990c5c8eed244de (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
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2000 by B'rczi, G˙bor
    member of the Free Pascal development team

    Support objects for the install program

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    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.

 **********************************************************************}
unit Scroll;

interface

uses Objects,
     FVConsts,
     Drivers,Views,App;

const
    CScrollBoxBackground = #6;

type
    PScrollBoxBackground = ^TScrollBoxBackground;
    TScrollBoxBackground = object(TBackground)
      function GetPalette: PPalette; virtual;
    end;

    PScrollBox = ^TScrollBox;
    TScrollBox = object(TGroup)
      Delta,Limit: TPoint;
      HScrollBar,VScrollBar: PScrollBar;
      Background: PScrollBoxBackground;
      constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
      procedure InitBackground; virtual;
      procedure HandleEvent(var Event: TEvent); virtual;
      procedure ChangeBounds(var Bounds: TRect); virtual;
      procedure ScrollDraw; virtual;
      procedure ScrollTo(X, Y: Sw_Integer);
      procedure SetLimit(X, Y: Sw_Integer);
      procedure SetState(AState: Word; Enable: Boolean); virtual;
      procedure TrackCursor;
      procedure Draw; virtual;
      function  ClipChilds: boolean; virtual;
      procedure BeforeInsert(P: PView); virtual;
      procedure AfterInsert(P: PView); virtual;
      procedure AfterDelete(P: PView); virtual;
    private
      DrawLock: Byte;
      DrawFlag: Boolean;
      ScrollFlag : boolean;
      procedure CheckDraw;
      procedure UpdateLimits;
      procedure ShiftViews(DX,DY: sw_integer);
    end;

implementation

function TScrollBoxBackground.GetPalette: PPalette;
const P: string[length(CScrollBoxBackground)] = CScrollBoxBackground;
begin
  GetPalette:=@P;
end;

constructor TScrollBox.Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
begin
  inherited Init(Bounds);
  EventMask:=EventMask or evBroadcast;
  HScrollBar:=AHScrollBar; VScrollBar:=AVScrollBar;
  InitBackground;
  if Assigned(Background) then Insert(Background);
  ReDraw;
end;

procedure TScrollBox.InitBackground;
var R: TRect;
begin
  GetExtent(R);
  New(Background, Init(R,' '));
end;

procedure TScrollBox.HandleEvent(var Event: TEvent);
begin
  if (Event.What=evBroadcast) and (Event.Command=cmCursorChanged) then
    TrackCursor;
   If (Event.What = evBroadcast) AND
     (Event.Command = cmScrollBarChanged) AND         { Scroll bar change }
     Not ScrollFlag AND
     ((Event.InfoPtr = HScrollBar) OR                 { Our scrollbar? }
      (Event.InfoPtr = VScrollBar)) Then ScrollDraw;  { Redraw scroller }
  inherited HandleEvent(Event);
end;

procedure TScrollBox.ChangeBounds(var Bounds: TRect);
begin
  SetBounds(Bounds);
  Inc(DrawLock);
  SetLimit(Limit.X, Limit.Y);
  Dec(DrawLock);
  DrawFlag := False;
  DrawView;
end;

procedure TScrollBox.CheckDraw;
begin
  if (DrawLock = 0) and DrawFlag then
  begin
    DrawFlag := False;
    ReDraw; DrawView;
  end;
end;

procedure TScrollBox.ScrollDraw;
var
  D: TPoint;
begin
  if HScrollBar <> nil then
   D.X := HScrollBar^.Value
  else
   D.X := 0;
  if VScrollBar <> nil then
   D.Y := VScrollBar^.Value
  else
   D.Y := 0;
  if (D.X <> Delta.X) or (D.Y <> Delta.Y) then
   begin
     SetCursor(Cursor.X + Delta.X - D.X, Cursor.Y + Delta.Y - D.Y);
     ScrollTo(D.X,D.Y);
     if DrawLock <> 0 then
      DrawFlag := True
     else
      DrawView;
   end;
end;


procedure TScrollBox.ScrollTo(X, Y: Sw_Integer);
var DX,DY: sw_integer;
    PrevScrollFlag : boolean;
begin
  Inc(DrawLock);
  DX:=Delta.X-X;
  DY:=Delta.Y-Y;
  PrevScrollFlag:=ScrollFlag;
  ScrollFlag:=true;

  if HScrollBar <> nil then
   HScrollBar^.SetValue(X);
  if VScrollBar <> nil then
   VScrollBar^.SetValue(Y);
  ScrollFlag:=PrevScrollFlag;
  ShiftViews(DX,DY);
  Dec(DrawLock);
  CheckDraw;
end;

procedure TScrollBox.ShiftViews(DX,DY: sw_integer);
  procedure DoShift(P: PView);
  begin
    P^.MoveTo(P^.Origin.X+DX,P^.Origin.Y+DY);
  end;
begin
  ForEach(@DoShift);
  Delta.X:=Delta.X-DX;
  Delta.Y:=Delta.Y-DY;
end;

procedure TScrollBox.SetLimit(X, Y: Sw_Integer);
begin
  Limit.X := X;
  Limit.Y := Y;
  Inc(DrawLock);
  if HScrollBar <> nil then
    HScrollBar^.SetParams(HScrollBar^.Value, HScrollBar^.Min, HScrollBar^.Max, HScrollBar^.PgStep, HScrollBar^.ArStep);
  if VScrollBar <> nil then
    VScrollBar^.SetParams(VScrollBar^.Value, VScrollBar^.Min, VScrollBar^.Max, VScrollBar^.PgStep, VScrollBar^.ArStep);
  Dec(DrawLock);
  CheckDraw;
end;

procedure TScrollBox.SetState(AState: Word; Enable: Boolean);
  procedure ShowSBar(SBar: PScrollBar);
  begin
    if (SBar <> nil) then
      if GetState(sfActive + sfSelected) then
        SBar^.Show
      else
        SBar^.Hide;
  end;
var OState: word;
begin
  OState:=State;
  inherited SetState(AState, Enable);
  if AState and (sfActive + sfSelected) <> 0 then
   begin
     ShowSBar(HScrollBar);
     ShowSBar(VScrollBar);
   end;
  if ((OState xor State) and (sfFocused))<>0 then
    TrackCursor;
end;

procedure TScrollBox.TrackCursor;
var V: PView;
    P,ND: TPoint;
begin
  V:=Current;
  if (not Assigned(V)) then Exit;
  P.X:=V^.Origin.X+V^.Cursor.X;
  P.Y:=V^.Origin.Y+V^.Cursor.Y;
  ND:=Delta;
  if (P.X<0) then
    Dec(ND.X,-P.X)
  else
    if (P.X>=Size.X) then
      Inc(ND.X,P.X-(Size.X-1));
  if (P.Y<0) then
    Dec(ND.Y,-P.Y)
  else
    if (P.Y>=Size.Y) then
      Inc(ND.Y,P.Y-(Size.Y-1));
  if (ND.X<>Delta.X) or (ND.Y<>Delta.Y) then
    ScrollTo(ND.X,ND.Y);
end;

function TScrollBox.ClipChilds: boolean;
begin
  ClipChilds:=false;
end;

procedure TScrollBox.BeforeInsert(P: PView);
begin
  if Assigned(P) then
    P^.MoveTo(P^.Origin.X-Delta.X,P^.Origin.Y-Delta.Y);
end;

procedure TScrollBox.AfterInsert(P: PView);
begin
  UpdateLimits;
end;

procedure TScrollBox.AfterDelete(P: PView);
begin
  { UpdateLimits;
    removed because it creates GPF PM }
end;

procedure TScrollBox.Draw;
begin
  inherited Draw;
end;

procedure TScrollBox.UpdateLimits;
var Max: TPoint;

  procedure Check(P: PView);
  var O: TPoint;
  begin
    O.X:=P^.Origin.X+P^.Size.X+Delta.X;
    O.Y:=P^.Origin.Y+P^.Size.Y+Delta.Y;
    if O.X>Max.X then
      Max.X:=O.X;
    if O.Y>Max.Y then
      Max.Y:=O.Y;
  end;

begin
  Max.X:=0; Max.Y:=0;
  ForEach(@Check);
  if (Max.X<>Limit.X) or (Max.Y<>Limit.Y) then
    SetLimit(Max.X,Max.Y);
end;

END.