scroll.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2000 by B‚rczi, G bor
  5. member of the Free Pascal development team
  6. Support objects for the install program
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit Scroll;
  14. interface
  15. uses Objects,
  16. {$ifdef FVISION}
  17. FVConsts,
  18. {$else}
  19. Commands,
  20. {$endif}
  21. Drivers,Views,App;
  22. const
  23. CScrollBoxBackground = #6;
  24. type
  25. PScrollBoxBackground = ^TScrollBoxBackground;
  26. TScrollBoxBackground = object(TBackground)
  27. function GetPalette: PPalette; virtual;
  28. end;
  29. PScrollBox = ^TScrollBox;
  30. TScrollBox = object(TGroup)
  31. Delta,Limit: TPoint;
  32. HScrollBar,VScrollBar: PScrollBar;
  33. Background: PScrollBoxBackground;
  34. constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
  35. procedure InitBackground; virtual;
  36. procedure HandleEvent(var Event: TEvent); virtual;
  37. procedure ChangeBounds(var Bounds: TRect); virtual;
  38. procedure ScrollDraw; virtual;
  39. procedure ScrollTo(X, Y: Sw_Integer);
  40. procedure SetLimit(X, Y: Sw_Integer);
  41. procedure SetState(AState: Word; Enable: Boolean); virtual;
  42. procedure TrackCursor;
  43. procedure Draw; virtual;
  44. function ClipChilds: boolean; virtual;
  45. procedure BeforeInsert(P: PView); virtual;
  46. procedure AfterInsert(P: PView); virtual;
  47. procedure AfterDelete(P: PView); virtual;
  48. private
  49. DrawLock: Byte;
  50. DrawFlag: Boolean;
  51. procedure CheckDraw;
  52. procedure UpdateLimits;
  53. procedure ShiftViews(DX,DY: sw_integer);
  54. end;
  55. implementation
  56. function TScrollBoxBackground.GetPalette: PPalette;
  57. const P: string[length(CScrollBoxBackground)] = CScrollBoxBackground;
  58. begin
  59. GetPalette:=@P;
  60. end;
  61. constructor TScrollBox.Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
  62. begin
  63. inherited Init(Bounds);
  64. EventMask:=EventMask or evBroadcast;
  65. HScrollBar:=AHScrollBar; VScrollBar:=AVScrollBar;
  66. InitBackground;
  67. if Assigned(Background) then Insert(Background);
  68. ReDraw;
  69. end;
  70. procedure TScrollBox.InitBackground;
  71. var R: TRect;
  72. begin
  73. GetExtent(R);
  74. New(Background, Init(R,' '));
  75. end;
  76. procedure TScrollBox.HandleEvent(var Event: TEvent);
  77. begin
  78. if (Event.What=evBroadcast) and (Event.Command=cmCursorChanged) then
  79. TrackCursor;
  80. inherited HandleEvent(Event);
  81. end;
  82. procedure TScrollBox.ChangeBounds(var Bounds: TRect);
  83. begin
  84. SetBounds(Bounds);
  85. Inc(DrawLock);
  86. SetLimit(Limit.X, Limit.Y);
  87. Dec(DrawLock);
  88. DrawFlag := False;
  89. DrawView;
  90. end;
  91. procedure TScrollBox.CheckDraw;
  92. begin
  93. if (DrawLock = 0) and DrawFlag then
  94. begin
  95. DrawFlag := False;
  96. ReDraw; DrawView;
  97. end;
  98. end;
  99. procedure TScrollBox.ScrollDraw;
  100. var
  101. D: TPoint;
  102. begin
  103. if HScrollBar <> nil then
  104. D.X := HScrollBar^.Value
  105. else
  106. D.X := 0;
  107. if VScrollBar <> nil then
  108. D.Y := VScrollBar^.Value
  109. else
  110. D.Y := 0;
  111. if (D.X <> Delta.X) or (D.Y <> Delta.Y) then
  112. begin
  113. SetCursor(Cursor.X + Delta.X - D.X, Cursor.Y + Delta.Y - D.Y);
  114. Delta := D;
  115. if DrawLock <> 0 then
  116. DrawFlag := True
  117. else
  118. DrawView;
  119. end;
  120. end;
  121. procedure TScrollBox.ScrollTo(X, Y: Sw_Integer);
  122. var DX,DY: sw_integer;
  123. begin
  124. Inc(DrawLock);
  125. DX:=Delta.X-X; DY:=Delta.Y-Y;
  126. if HScrollBar <> nil then
  127. HScrollBar^.SetValue(X);
  128. if VScrollBar <> nil then
  129. VScrollBar^.SetValue(Y);
  130. ShiftViews(DX,DY);
  131. Dec(DrawLock);
  132. CheckDraw;
  133. end;
  134. procedure TScrollBox.ShiftViews(DX,DY: sw_integer);
  135. procedure DoShift(P: PView); {$ifndef FPC}far;{$endif}
  136. begin
  137. P^.MoveTo(P^.Origin.X+DX,P^.Origin.Y+DY);
  138. end;
  139. begin
  140. ForEach(@DoShift);
  141. end;
  142. procedure TScrollBox.SetLimit(X, Y: Sw_Integer);
  143. begin
  144. Limit.X := X;
  145. Limit.Y := Y;
  146. Inc(DrawLock);
  147. if HScrollBar <> nil then
  148. HScrollBar^.SetParams(HScrollBar^.Value, 0, X - Size.X, Size.X - 1, HScrollBar^.ArStep);
  149. if VScrollBar <> nil then
  150. VScrollBar^.SetParams(VScrollBar^.Value, 0, Y - Size.Y, Size.Y - 1, VScrollBar^.ArStep);
  151. Dec(DrawLock);
  152. CheckDraw;
  153. end;
  154. procedure TScrollBox.SetState(AState: Word; Enable: Boolean);
  155. procedure ShowSBar(SBar: PScrollBar);
  156. begin
  157. if (SBar <> nil) then
  158. if GetState(sfActive + sfSelected) then
  159. SBar^.Show
  160. else
  161. SBar^.Hide;
  162. end;
  163. var OState: word;
  164. begin
  165. OState:=State;
  166. inherited SetState(AState, Enable);
  167. if AState and (sfActive + sfSelected) <> 0 then
  168. begin
  169. ShowSBar(HScrollBar);
  170. ShowSBar(VScrollBar);
  171. end;
  172. if ((OState xor State) and (sfFocused))<>0 then
  173. TrackCursor;
  174. end;
  175. procedure TScrollBox.TrackCursor;
  176. var V: PView;
  177. P,ND: TPoint;
  178. begin
  179. V:=Current;
  180. if (not Assigned(V)) then Exit;
  181. P.X:=V^.Origin.X+V^.Cursor.X; P.Y:=V^.Origin.Y+V^.Cursor.Y;
  182. ND:=Delta;
  183. if (P.X<0) then Dec(ND.X,-P.X) else
  184. if (P.X>=Size.X) then Inc(ND.X,P.X-(Size.X-1));
  185. if (P.Y<0) then Dec(ND.Y,-P.Y) else
  186. if (P.Y>=Size.Y) then Inc(ND.Y,P.Y-(Size.Y-1));
  187. if (ND.X<>Delta.X) or (ND.Y<>Delta.Y) then
  188. ScrollTo(ND.X,ND.Y);
  189. end;
  190. function TScrollBox.ClipChilds: boolean;
  191. begin
  192. ClipChilds:=false;
  193. end;
  194. procedure TScrollBox.BeforeInsert(P: PView);
  195. begin
  196. if Assigned(P) then
  197. P^.MoveTo(P^.Origin.X-Delta.X,P^.Origin.Y-Delta.Y);
  198. end;
  199. procedure TScrollBox.AfterInsert(P: PView);
  200. begin
  201. UpdateLimits;
  202. end;
  203. procedure TScrollBox.AfterDelete(P: PView);
  204. begin
  205. { UpdateLimits;
  206. removed because it creates GPF PM }
  207. end;
  208. procedure TScrollBox.Draw;
  209. begin
  210. inherited Draw;
  211. end;
  212. procedure TScrollBox.UpdateLimits;
  213. var Max: TPoint;
  214. procedure Check(P: PView); {$ifndef FPC}far;{$endif}
  215. var O: TPoint;
  216. begin
  217. O.X:=P^.Origin.X+P^.Size.X+Delta.X; O.Y:=P^.Origin.Y+P^.Size.Y+Delta.Y;
  218. if O.X>Max.X then Max.X:=O.X;
  219. if O.Y>Max.Y then Max.Y:=O.Y;
  220. end;
  221. begin
  222. Max.X:=0; Max.Y:=0;
  223. ForEach(@Check);
  224. if (Max.X<>Limit.X) or (Max.Y<>Limit.Y) then
  225. SetLimit(Max.X,Max.Y);
  226. end;
  227. END.
  228. {
  229. $Log$
  230. Revision 1.3 2002-09-07 15:40:59 peter
  231. * old logs removed and tabs fixed
  232. Revision 1.2 2002/01/29 22:01:17 peter
  233. * support fvision
  234. Revision 1.1 2002/01/29 17:59:15 peter
  235. * moved installer
  236. }