scroll.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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,Commands,Drivers,Views,App;
  16. const
  17. CScrollBoxBackground = #6;
  18. type
  19. PScrollBoxBackground = ^TScrollBoxBackground;
  20. TScrollBoxBackground = object(TBackground)
  21. function GetPalette: PPalette; virtual;
  22. end;
  23. PScrollBox = ^TScrollBox;
  24. TScrollBox = object(TGroup)
  25. Delta,Limit: TPoint;
  26. HScrollBar,VScrollBar: PScrollBar;
  27. Background: PScrollBoxBackground;
  28. constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
  29. procedure InitBackground; virtual;
  30. procedure HandleEvent(var Event: TEvent); virtual;
  31. procedure ChangeBounds(var Bounds: TRect); virtual;
  32. procedure ScrollDraw; virtual;
  33. procedure ScrollTo(X, Y: Sw_Integer);
  34. procedure SetLimit(X, Y: Sw_Integer);
  35. procedure SetState(AState: Word; Enable: Boolean); virtual;
  36. procedure TrackCursor;
  37. procedure Draw; virtual;
  38. function ClipChilds: boolean; virtual;
  39. procedure BeforeInsert(P: PView); virtual;
  40. procedure AfterInsert(P: PView); virtual;
  41. procedure AfterDelete(P: PView); virtual;
  42. private
  43. DrawLock: Byte;
  44. DrawFlag: Boolean;
  45. procedure CheckDraw;
  46. procedure UpdateLimits;
  47. procedure ShiftViews(DX,DY: sw_integer);
  48. end;
  49. implementation
  50. function TScrollBoxBackground.GetPalette: PPalette;
  51. const P: string[length(CScrollBoxBackground)] = CScrollBoxBackground;
  52. begin
  53. GetPalette:=@P;
  54. end;
  55. constructor TScrollBox.Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
  56. begin
  57. inherited Init(Bounds);
  58. EventMask:=EventMask or evBroadcast;
  59. HScrollBar:=AHScrollBar; VScrollBar:=AVScrollBar;
  60. InitBackground;
  61. if Assigned(Background) then Insert(Background);
  62. ReDraw;
  63. end;
  64. procedure TScrollBox.InitBackground;
  65. var R: TRect;
  66. begin
  67. GetExtent(R);
  68. New(Background, Init(R,' '));
  69. end;
  70. procedure TScrollBox.HandleEvent(var Event: TEvent);
  71. begin
  72. if (Event.What=evBroadcast) and (Event.Command=cmCursorChanged) then
  73. TrackCursor;
  74. inherited HandleEvent(Event);
  75. end;
  76. procedure TScrollBox.ChangeBounds(var Bounds: TRect);
  77. begin
  78. SetBounds(Bounds);
  79. Inc(DrawLock);
  80. SetLimit(Limit.X, Limit.Y);
  81. Dec(DrawLock);
  82. DrawFlag := False;
  83. DrawView;
  84. end;
  85. procedure TScrollBox.CheckDraw;
  86. begin
  87. if (DrawLock = 0) and DrawFlag then
  88. begin
  89. DrawFlag := False;
  90. ReDraw; DrawView;
  91. end;
  92. end;
  93. procedure TScrollBox.ScrollDraw;
  94. var
  95. D: TPoint;
  96. begin
  97. if HScrollBar <> nil then
  98. D.X := HScrollBar^.Value
  99. else
  100. D.X := 0;
  101. if VScrollBar <> nil then
  102. D.Y := VScrollBar^.Value
  103. else
  104. D.Y := 0;
  105. if (D.X <> Delta.X) or (D.Y <> Delta.Y) then
  106. begin
  107. SetCursor(Cursor.X + Delta.X - D.X, Cursor.Y + Delta.Y - D.Y);
  108. Delta := D;
  109. if DrawLock <> 0 then
  110. DrawFlag := True
  111. else
  112. DrawView;
  113. end;
  114. end;
  115. procedure TScrollBox.ScrollTo(X, Y: Sw_Integer);
  116. var DX,DY: sw_integer;
  117. begin
  118. Inc(DrawLock);
  119. DX:=Delta.X-X; DY:=Delta.Y-Y;
  120. if HScrollBar <> nil then
  121. HScrollBar^.SetValue(X);
  122. if VScrollBar <> nil then
  123. VScrollBar^.SetValue(Y);
  124. ShiftViews(DX,DY);
  125. Dec(DrawLock);
  126. CheckDraw;
  127. end;
  128. procedure TScrollBox.ShiftViews(DX,DY: sw_integer);
  129. procedure DoShift(P: PView); {$ifndef FPC}far;{$endif}
  130. begin
  131. P^.MoveTo(P^.Origin.X+DX,P^.Origin.Y+DY);
  132. end;
  133. begin
  134. ForEach(@DoShift);
  135. end;
  136. procedure TScrollBox.SetLimit(X, Y: Sw_Integer);
  137. begin
  138. Limit.X := X;
  139. Limit.Y := Y;
  140. Inc(DrawLock);
  141. if HScrollBar <> nil then
  142. HScrollBar^.SetParams(HScrollBar^.Value, 0, X - Size.X, Size.X - 1, HScrollBar^.ArStep);
  143. if VScrollBar <> nil then
  144. VScrollBar^.SetParams(VScrollBar^.Value, 0, Y - Size.Y, Size.Y - 1, VScrollBar^.ArStep);
  145. Dec(DrawLock);
  146. CheckDraw;
  147. end;
  148. procedure TScrollBox.SetState(AState: Word; Enable: Boolean);
  149. procedure ShowSBar(SBar: PScrollBar);
  150. begin
  151. if (SBar <> nil) then
  152. if GetState(sfActive + sfSelected) then
  153. SBar^.Show
  154. else
  155. SBar^.Hide;
  156. end;
  157. var OState: word;
  158. begin
  159. OState:=State;
  160. inherited SetState(AState, Enable);
  161. if AState and (sfActive + sfSelected) <> 0 then
  162. begin
  163. ShowSBar(HScrollBar);
  164. ShowSBar(VScrollBar);
  165. end;
  166. if ((OState xor State) and (sfFocused))<>0 then
  167. TrackCursor;
  168. end;
  169. procedure TScrollBox.TrackCursor;
  170. var V: PView;
  171. P,ND: TPoint;
  172. begin
  173. V:=Current;
  174. if (not Assigned(V)) then Exit;
  175. P.X:=V^.Origin.X+V^.Cursor.X; P.Y:=V^.Origin.Y+V^.Cursor.Y;
  176. ND:=Delta;
  177. if (P.X<0) then Dec(ND.X,-P.X) else
  178. if (P.X>=Size.X) then Inc(ND.X,P.X-(Size.X-1));
  179. if (P.Y<0) then Dec(ND.Y,-P.Y) else
  180. if (P.Y>=Size.Y) then Inc(ND.Y,P.Y-(Size.Y-1));
  181. if (ND.X<>Delta.X) or (ND.Y<>Delta.Y) then
  182. ScrollTo(ND.X,ND.Y);
  183. end;
  184. function TScrollBox.ClipChilds: boolean;
  185. begin
  186. ClipChilds:=false;
  187. end;
  188. procedure TScrollBox.BeforeInsert(P: PView);
  189. begin
  190. if Assigned(P) then
  191. P^.MoveTo(P^.Origin.X-Delta.X,P^.Origin.Y-Delta.Y);
  192. end;
  193. procedure TScrollBox.AfterInsert(P: PView);
  194. begin
  195. UpdateLimits;
  196. end;
  197. procedure TScrollBox.AfterDelete(P: PView);
  198. begin
  199. UpdateLimits;
  200. end;
  201. procedure TScrollBox.Draw;
  202. begin
  203. inherited Draw;
  204. end;
  205. procedure TScrollBox.UpdateLimits;
  206. var Max: TPoint;
  207. procedure Check(P: PView); {$ifndef FPC}far;{$endif}
  208. var O: TPoint;
  209. begin
  210. O.X:=P^.Origin.X+P^.Size.X+Delta.X; O.Y:=P^.Origin.Y+P^.Size.Y+Delta.Y;
  211. if O.X>Max.X then Max.X:=O.X;
  212. if O.Y>Max.Y then Max.Y:=O.Y;
  213. end;
  214. begin
  215. Max.X:=0; Max.Y:=0;
  216. ForEach(@Check);
  217. if (Max.X<>Limit.X) or (Max.Y<>Limit.Y) then
  218. SetLimit(Max.X,Max.Y);
  219. end;
  220. END.
  221. {
  222. $Log$
  223. Revision 1.2 2000-09-22 23:13:37 pierre
  224. * add emulation for go32v2 and display currently extraced file
  225. and changes by Gabor for scrolling support (merged)
  226. Revision 1.1.2.1 2000/09/21 10:51:33 pierre
  227. new file from Gabor
  228. }