PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/nx-3.5.0/nx-X11/programs/xterm/cursor.c

#
C | 345 lines | 194 code | 37 blank | 114 comment | 22 complexity | 377a6e6442ee207db00a34fd8fccff2a MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /* $XTermId: cursor.c,v 1.36 2005/08/05 01:25:39 tom Exp $ */
  2. /*
  3. * $Xorg: cursor.c,v 1.3 2000/08/17 19:55:08 cpqbld Exp $
  4. */
  5. /* $XFree86: xc/programs/xterm/cursor.c,v 3.18 2005/08/05 01:25:39 dickey Exp $ */
  6. /*
  7. * Copyright 2002-2004,2005 by Thomas E. Dickey
  8. *
  9. * All Rights Reserved
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  25. * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
  26. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  27. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  28. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. *
  30. * Except as contained in this notice, the name(s) of the above copyright
  31. * holders shall not be used in advertising or otherwise to promote the
  32. * sale, use or other dealings in this Software without prior written
  33. * authorization.
  34. *
  35. * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  36. *
  37. * All Rights Reserved
  38. *
  39. * Permission to use, copy, modify, and distribute this software and its
  40. * documentation for any purpose and without fee is hereby granted,
  41. * provided that the above copyright notice appear in all copies and that
  42. * both that copyright notice and this permission notice appear in
  43. * supporting documentation, and that the name of Digital Equipment
  44. * Corporation not be used in advertising or publicity pertaining to
  45. * distribution of the software without specific, written prior permission.
  46. *
  47. *
  48. * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  49. * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  50. * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  51. * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  52. * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  53. * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  54. * SOFTWARE.
  55. */
  56. /* cursor.c */
  57. #include <xterm.h>
  58. #include <data.h>
  59. #include <assert.h>
  60. /*
  61. * Moves the cursor to the specified position, checking for bounds.
  62. * (this includes scrolling regions)
  63. * The origin is considered to be 0, 0 for this procedure.
  64. */
  65. void
  66. CursorSet(TScreen * screen, int row, int col, unsigned flags)
  67. {
  68. int use_row = row;
  69. int max_row;
  70. col = (col < 0 ? 0 : col);
  71. set_cur_col(screen, (col <= screen->max_col ? col : screen->max_col));
  72. max_row = screen->max_row;
  73. if (flags & ORIGIN) {
  74. use_row += screen->top_marg;
  75. max_row = screen->bot_marg;
  76. }
  77. use_row = (use_row < 0 ? 0 : use_row);
  78. set_cur_row(screen, (use_row <= max_row ? use_row : max_row));
  79. screen->do_wrap = 0;
  80. TRACE(("CursorSet(%d,%d) margins [%d..%d] -> %d,%d %s\n",
  81. row, col,
  82. screen->top_marg,
  83. screen->bot_marg,
  84. screen->cur_row,
  85. screen->cur_col,
  86. (flags & ORIGIN ? "origin" : "normal")));
  87. }
  88. /*
  89. * moves the cursor left n, no wrap around
  90. */
  91. void
  92. CursorBack(TScreen * screen, int n)
  93. {
  94. int i, j, k, rev;
  95. if ((rev = (term->flags & (REVERSEWRAP | WRAPAROUND)) ==
  96. (REVERSEWRAP | WRAPAROUND)) != 0
  97. && screen->do_wrap)
  98. n--;
  99. if ((screen->cur_col -= n) < 0) {
  100. if (rev) {
  101. if ((i = ((j = MaxCols(screen))
  102. * screen->cur_row) + screen->cur_col) < 0) {
  103. k = j * MaxRows(screen);
  104. i += ((-i) / k + 1) * k;
  105. }
  106. set_cur_row(screen, i / j);
  107. set_cur_col(screen, i % j);
  108. } else
  109. set_cur_col(screen, 0);
  110. }
  111. screen->do_wrap = 0;
  112. }
  113. /*
  114. * moves the cursor forward n, no wraparound
  115. */
  116. void
  117. CursorForward(TScreen * screen, int n)
  118. {
  119. int next = screen->cur_col + n;
  120. int max = CurMaxCol(screen, screen->cur_row);
  121. if (next > max)
  122. next = max;
  123. set_cur_col(screen, next);
  124. screen->do_wrap = 0;
  125. }
  126. /*
  127. * moves the cursor down n, no scrolling.
  128. * Won't pass bottom margin or bottom of screen.
  129. */
  130. void
  131. CursorDown(TScreen * screen, int n)
  132. {
  133. int max;
  134. int next = screen->cur_row + n;
  135. max = (screen->cur_row > screen->bot_marg ?
  136. screen->max_row : screen->bot_marg);
  137. if (next > max)
  138. next = max;
  139. if (next > screen->max_row)
  140. next = screen->max_row;
  141. set_cur_row(screen, next);
  142. screen->do_wrap = 0;
  143. }
  144. /*
  145. * moves the cursor up n, no linestarving.
  146. * Won't pass top margin or top of screen.
  147. */
  148. void
  149. CursorUp(TScreen * screen, int n)
  150. {
  151. int min;
  152. int next = screen->cur_row - n;
  153. min = ((screen->cur_row < screen->top_marg)
  154. ? 0
  155. : screen->top_marg);
  156. if (next < min)
  157. next = min;
  158. if (next < 0)
  159. next = 0;
  160. set_cur_row(screen, next);
  161. screen->do_wrap = 0;
  162. }
  163. /*
  164. * Moves cursor down amount lines, scrolls if necessary.
  165. * Won't leave scrolling region. No carriage return.
  166. */
  167. void
  168. xtermIndex(TScreen * screen, int amount)
  169. {
  170. int j;
  171. /*
  172. * indexing when below scrolling region is cursor down.
  173. * if cursor high enough, no scrolling necessary.
  174. */
  175. if (screen->cur_row > screen->bot_marg
  176. || screen->cur_row + amount <= screen->bot_marg) {
  177. CursorDown(screen, amount);
  178. return;
  179. }
  180. CursorDown(screen, j = screen->bot_marg - screen->cur_row);
  181. xtermScroll(screen, amount - j);
  182. }
  183. /*
  184. * Moves cursor up amount lines, reverse scrolls if necessary.
  185. * Won't leave scrolling region. No carriage return.
  186. */
  187. void
  188. RevIndex(TScreen * screen, int amount)
  189. {
  190. /*
  191. * reverse indexing when above scrolling region is cursor up.
  192. * if cursor low enough, no reverse indexing needed
  193. */
  194. if (screen->cur_row < screen->top_marg
  195. || screen->cur_row - amount >= screen->top_marg) {
  196. CursorUp(screen, amount);
  197. return;
  198. }
  199. RevScroll(screen, amount - (screen->cur_row - screen->top_marg));
  200. CursorUp(screen, screen->cur_row - screen->top_marg);
  201. }
  202. /*
  203. * Moves Cursor To First Column In Line
  204. * (Note: xterm doesn't implement SLH, SLL which would affect use of this)
  205. */
  206. void
  207. CarriageReturn(TScreen * screen)
  208. {
  209. set_cur_col(screen, 0);
  210. screen->do_wrap = 0;
  211. }
  212. /*
  213. * Save Cursor and Attributes
  214. */
  215. void
  216. CursorSave(XtermWidget tw)
  217. {
  218. TScreen *screen = &tw->screen;
  219. SavedCursor *sc = &screen->sc[screen->alternate != False];
  220. sc->saved = True;
  221. sc->row = screen->cur_row;
  222. sc->col = screen->cur_col;
  223. sc->flags = tw->flags;
  224. sc->curgl = screen->curgl;
  225. sc->curgr = screen->curgr;
  226. #if OPT_ISO_COLORS
  227. sc->cur_foreground = tw->cur_foreground;
  228. sc->cur_background = tw->cur_background;
  229. sc->sgr_foreground = tw->sgr_foreground;
  230. #endif
  231. memmove(sc->gsets, screen->gsets, sizeof(screen->gsets));
  232. }
  233. /*
  234. * We save/restore all visible attributes, plus wrapping, origin mode, and the
  235. * selective erase attribute.
  236. */
  237. #define DECSC_FLAGS (ATTRIBUTES|ORIGIN|WRAPAROUND|PROTECTED)
  238. /*
  239. * Restore Cursor and Attributes
  240. */
  241. void
  242. CursorRestore(XtermWidget tw)
  243. {
  244. TScreen *screen = &tw->screen;
  245. SavedCursor *sc = &screen->sc[screen->alternate != False];
  246. /* Restore the character sets, unless we never did a save-cursor op.
  247. * In that case, we'll reset the character sets.
  248. */
  249. if (sc->saved) {
  250. memmove(screen->gsets, sc->gsets, sizeof(screen->gsets));
  251. screen->curgl = sc->curgl;
  252. screen->curgr = sc->curgr;
  253. } else {
  254. resetCharsets(screen);
  255. }
  256. tw->flags &= ~DECSC_FLAGS;
  257. tw->flags |= sc->flags & DECSC_FLAGS;
  258. CursorSet(screen,
  259. ((tw->flags & ORIGIN)
  260. ? sc->row - screen->top_marg
  261. : sc->row),
  262. sc->col, tw->flags);
  263. #if OPT_ISO_COLORS
  264. tw->sgr_foreground = sc->sgr_foreground;
  265. SGR_Foreground(tw->flags & FG_COLOR ? sc->cur_foreground : -1);
  266. SGR_Background(tw->flags & BG_COLOR ? sc->cur_background : -1);
  267. #endif
  268. }
  269. /*
  270. * Move the cursor to the first column of the n-th next line.
  271. */
  272. void
  273. CursorNextLine(TScreen * screen, int count)
  274. {
  275. CursorDown(screen, count < 1 ? 1 : count);
  276. CarriageReturn(screen);
  277. do_xevents();
  278. }
  279. /*
  280. * Move the cursor to the first column of the n-th previous line.
  281. */
  282. void
  283. CursorPrevLine(TScreen * screen, int count)
  284. {
  285. CursorUp(screen, count < 1 ? 1 : count);
  286. CarriageReturn(screen);
  287. do_xevents();
  288. }
  289. #if OPT_TRACE
  290. int
  291. set_cur_row(TScreen * screen, int value)
  292. {
  293. assert(screen != 0);
  294. assert(value >= 0);
  295. assert(value <= screen->max_row);
  296. screen->cur_row = value;
  297. return value;
  298. }
  299. int
  300. set_cur_col(TScreen * screen, int value)
  301. {
  302. assert(screen != 0);
  303. assert(value >= 0);
  304. assert(value <= screen->max_col);
  305. screen->cur_col = value;
  306. return value;
  307. }
  308. #endif /* OPT_TRACE */