PageRenderTime 64ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/textarea/FirstLine.java

#
Java | 347 lines | 259 code | 49 blank | 39 comment | 55 complexity | 4a384dc4296fca4275ff0187f657002f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * FirstLine.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2005 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.textarea;
  23. import org.gjt.sp.jedit.Debug;
  24. import org.gjt.sp.util.Log;
  25. class FirstLine extends Anchor
  26. {
  27. int skew;
  28. //{{{ FirstLine constructor
  29. FirstLine(DisplayManager displayManager,
  30. JEditTextArea textArea)
  31. {
  32. super(displayManager,textArea);
  33. } //}}}
  34. //{{{ changed() method
  35. public void changed()
  36. {
  37. //{{{ Debug code
  38. if(Debug.SCROLL_DEBUG)
  39. {
  40. Log.log(Log.DEBUG,this,"changed() before: "
  41. + physicalLine + ":" + scrollLine
  42. + ":" + skew);
  43. } //}}}
  44. ensurePhysicalLineIsVisible();
  45. int screenLines = displayManager
  46. .getScreenLineCount(physicalLine);
  47. if(skew >= screenLines)
  48. skew = screenLines - 1;
  49. //{{{ Debug code
  50. if(Debug.SCROLL_VERIFY)
  51. {
  52. System.err.println("SCROLL_VERIFY");
  53. int verifyScrollLine = 0;
  54. for(int i = 0; i < displayManager.getBuffer()
  55. .getLineCount(); i++)
  56. {
  57. if(!displayManager.isLineVisible(i))
  58. continue;
  59. if(i >= physicalLine)
  60. break;
  61. verifyScrollLine += displayManager
  62. .getScreenLineCount(i);
  63. }
  64. if(verifyScrollLine != scrollLine)
  65. {
  66. Exception ex = new Exception(scrollLine + ":" + verifyScrollLine);
  67. Log.log(Log.ERROR,this,ex);
  68. }
  69. }
  70. if(Debug.SCROLL_DEBUG)
  71. {
  72. Log.log(Log.DEBUG,this,"changed() after: "
  73. + physicalLine + ":" + scrollLine
  74. + ":" + skew);
  75. } //}}}
  76. } //}}}
  77. //{{{ reset() method
  78. public void reset()
  79. {
  80. if(Debug.SCROLL_DEBUG)
  81. Log.log(Log.DEBUG,this,"reset()");
  82. int oldPhysicalLine = physicalLine;
  83. physicalLine = 0;
  84. scrollLine = 0;
  85. int i = displayManager.getFirstVisibleLine();
  86. for(;;)
  87. {
  88. if(i >= oldPhysicalLine)
  89. break;
  90. int before = scrollLine;
  91. displayManager.updateScreenLineCount(i);
  92. if(before != scrollLine)
  93. throw new RuntimeException(this + " nudged");
  94. scrollLine += displayManager.getScreenLineCount(i);
  95. int nextLine = displayManager.getNextVisibleLine(i);
  96. if(nextLine == -1)
  97. break;
  98. else
  99. i = nextLine;
  100. }
  101. physicalLine = i;
  102. displayManager.updateScreenLineCount(i);
  103. int screenLines = displayManager.getScreenLineCount(physicalLine);
  104. if(skew >= screenLines)
  105. skew = screenLines - 1;
  106. textArea.updateScrollBar();
  107. } //}}}
  108. //{{{ physDown() method
  109. // scroll down by physical line amount
  110. void physDown(int amount, int screenAmount)
  111. {
  112. if(Debug.SCROLL_DEBUG)
  113. {
  114. Log.log(Log.DEBUG,this,"physDown() start: "
  115. + physicalLine + ":" + scrollLine);
  116. }
  117. skew = 0;
  118. if(!displayManager.isLineVisible(physicalLine))
  119. {
  120. int lastVisibleLine = displayManager.getLastVisibleLine();
  121. if(physicalLine > lastVisibleLine)
  122. physicalLine = lastVisibleLine;
  123. else
  124. {
  125. int nextPhysicalLine = displayManager.getNextVisibleLine(physicalLine);
  126. amount -= (nextPhysicalLine - physicalLine);
  127. scrollLine += displayManager.getScreenLineCount(physicalLine);
  128. physicalLine = nextPhysicalLine;
  129. }
  130. }
  131. for(;;)
  132. {
  133. int nextPhysicalLine = displayManager.getNextVisibleLine(
  134. physicalLine);
  135. if(nextPhysicalLine == -1)
  136. break;
  137. else if(nextPhysicalLine > physicalLine + amount)
  138. break;
  139. else
  140. {
  141. scrollLine += displayManager.getScreenLineCount(physicalLine);
  142. amount -= (nextPhysicalLine - physicalLine);
  143. physicalLine = nextPhysicalLine;
  144. }
  145. }
  146. if(Debug.SCROLL_DEBUG)
  147. {
  148. Log.log(Log.DEBUG,this,"physDown() end: "
  149. + physicalLine + ":" + scrollLine);
  150. }
  151. callChanged = true;
  152. // JEditTextArea.scrollTo() needs this to simplify
  153. // its code
  154. if(screenAmount < 0)
  155. scrollUp(-screenAmount);
  156. else if(screenAmount > 0)
  157. scrollDown(screenAmount);
  158. } //}}}
  159. //{{{ physUp() method
  160. // scroll up by physical line amount
  161. void physUp(int amount, int screenAmount)
  162. {
  163. if(Debug.SCROLL_DEBUG)
  164. {
  165. Log.log(Log.DEBUG,this,"physUp() start: "
  166. + physicalLine + ":" + scrollLine);
  167. }
  168. skew = 0;
  169. if(!displayManager.isLineVisible(physicalLine))
  170. {
  171. int firstVisibleLine = displayManager.getFirstVisibleLine();
  172. if(physicalLine < firstVisibleLine)
  173. physicalLine = firstVisibleLine;
  174. else
  175. {
  176. int prevPhysicalLine = displayManager.getPrevVisibleLine(physicalLine);
  177. amount -= (physicalLine - prevPhysicalLine);
  178. }
  179. }
  180. for(;;)
  181. {
  182. int prevPhysicalLine = displayManager.getPrevVisibleLine(
  183. physicalLine);
  184. if(prevPhysicalLine == -1)
  185. break;
  186. else if(prevPhysicalLine < physicalLine - amount)
  187. break;
  188. else
  189. {
  190. amount -= (physicalLine - prevPhysicalLine);
  191. physicalLine = prevPhysicalLine;
  192. scrollLine -= displayManager.getScreenLineCount(
  193. prevPhysicalLine);
  194. }
  195. }
  196. if(Debug.SCROLL_DEBUG)
  197. {
  198. Log.log(Log.DEBUG,this,"physUp() end: "
  199. + physicalLine + ":" + scrollLine);
  200. }
  201. callChanged = true;
  202. // JEditTextArea.scrollTo() needs this to simplify
  203. // its code
  204. if(screenAmount < 0)
  205. scrollUp(-screenAmount);
  206. else if(screenAmount > 0)
  207. scrollDown(screenAmount);
  208. } //}}}
  209. //{{{ scrollDown() method
  210. // scroll down by screen line amount
  211. void scrollDown(int amount)
  212. {
  213. if(Debug.SCROLL_DEBUG)
  214. Log.log(Log.DEBUG,this,"scrollDown()");
  215. ensurePhysicalLineIsVisible();
  216. amount += skew;
  217. skew = 0;
  218. while(amount > 0)
  219. {
  220. int screenLines = displayManager.getScreenLineCount(physicalLine);
  221. if(amount < screenLines)
  222. {
  223. skew = amount;
  224. break;
  225. }
  226. else
  227. {
  228. int nextLine = displayManager.getNextVisibleLine(physicalLine);
  229. if(nextLine == -1)
  230. break;
  231. boolean visible = displayManager.isLineVisible(physicalLine);
  232. physicalLine = nextLine;
  233. if(visible)
  234. {
  235. amount -= screenLines;
  236. scrollLine += screenLines;
  237. }
  238. }
  239. }
  240. callChanged = true;
  241. } //}}}
  242. //{{{ scrollUp() method
  243. // scroll up by screen line amount
  244. void scrollUp(int amount)
  245. {
  246. if(Debug.SCROLL_DEBUG)
  247. Log.log(Log.DEBUG,this,"scrollUp()");
  248. ensurePhysicalLineIsVisible();
  249. if(amount <= skew)
  250. {
  251. skew -= amount;
  252. }
  253. else
  254. {
  255. amount -= skew;
  256. skew = 0;
  257. while(amount > 0)
  258. {
  259. int prevLine = displayManager.getPrevVisibleLine(physicalLine);
  260. if(prevLine == -1)
  261. break;
  262. physicalLine = prevLine;
  263. int screenLines = displayManager.getScreenLineCount(physicalLine);
  264. scrollLine -= screenLines;
  265. if(amount < screenLines)
  266. {
  267. skew = screenLines - amount;
  268. break;
  269. }
  270. else
  271. amount -= screenLines;
  272. }
  273. }
  274. callChanged = true;
  275. } //}}}
  276. //{{{ ensurePhysicalLineIsVisible() method
  277. void ensurePhysicalLineIsVisible()
  278. {
  279. if(!displayManager.isLineVisible(physicalLine))
  280. {
  281. if(physicalLine > displayManager.getLastVisibleLine())
  282. {
  283. physicalLine = displayManager.getLastVisibleLine();
  284. scrollLine = displayManager.getScrollLineCount() - 1;
  285. }
  286. else if(physicalLine < displayManager.getFirstVisibleLine())
  287. {
  288. physicalLine = displayManager.getFirstVisibleLine();
  289. scrollLine = 0;
  290. }
  291. else
  292. {
  293. physicalLine = displayManager.getNextVisibleLine(physicalLine);
  294. scrollLine += displayManager.getScreenLineCount(physicalLine);
  295. }
  296. }
  297. } //}}}
  298. }