home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / mxterm.zip / mxterm / cursor.c < prev    next >
C/C++ Source or Header  |  1992-10-17  |  6KB  |  245 lines

  1. /*
  2.  *    $XConsortium: cursor.c,v 1.13 91/05/10 16:57:14 gildea Exp $
  3.  */
  4.  
  5. /*
  6.  * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  7.  *
  8.  *                         All Rights Reserved
  9.  *
  10.  * Permission to use, copy, modify, and distribute this software and its
  11.  * documentation for any purpose and without fee is hereby granted,
  12.  * provided that the above copyright notice appear in all copies and that
  13.  * both that copyright notice and this permission notice appear in
  14.  * supporting documentation, and that the name of Digital Equipment
  15.  * Corporation not be used in advertising or publicity pertaining to
  16.  * distribution of the software without specific, written prior permission.
  17.  *
  18.  *
  19.  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  20.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  21.  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  23.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  24.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  25.  * SOFTWARE.
  26.  */
  27.  
  28. /* cursor.c */
  29.  
  30. #include "ptyx.h"        /* also gets Xlib.h */
  31.  
  32. static void _CheckSelection(screen)
  33. register TScreen *screen;
  34. {
  35.     extern XtermWidget term;    /* %%% gross */
  36.  
  37.     if (screen->cur_row > screen->endHRow ||
  38.     (screen->cur_row == screen->endHRow &&
  39.      screen->cur_col >= screen->endHCol)) {}
  40.     else
  41.     DisownSelection(term);
  42. }
  43.  
  44.  
  45.  
  46. /*
  47.  * Moves the cursor to the specified position, checking for bounds.
  48.  * (this includes scrolling regions)
  49.  * The origin is considered to be 0, 0 for this procedure.
  50.  */
  51. CursorSet(screen, row, col, flags)
  52. register TScreen    *screen;
  53. register int    row, col;
  54. unsigned    flags;
  55. {
  56.     register int maxr;
  57.  
  58.     col = (col < 0 ? 0 : col);
  59.     screen->cur_col = (col <= screen->max_col ? col : screen->max_col);
  60.     maxr = screen->max_row;
  61.     if (flags & ORIGIN) {
  62.         row += screen->top_marg;
  63.         maxr = screen->bot_marg;
  64.     }
  65.     row = (row < 0 ? 0 : row);
  66.     screen->cur_row = (row <= maxr ? row : maxr);
  67.     screen->do_wrap = 0;
  68.     _CheckSelection(screen);
  69. }
  70.  
  71. /*
  72.  * moves the cursor left n, no wrap around
  73.  */
  74. CursorBack(screen, n)
  75. register TScreen    *screen;
  76. int        n;
  77. {
  78.     register int i, j, k, rev;
  79.     extern XtermWidget term;
  80.  
  81.     if((rev = (term->flags & (REVERSEWRAP | WRAPAROUND)) ==
  82.      (REVERSEWRAP | WRAPAROUND)) && screen->do_wrap)
  83.         n--;
  84.     if ((screen->cur_col -= n) < 0) {
  85.         if(rev) {
  86.             if((i = (j = screen->max_col + 1) * screen->cur_row +
  87.              screen->cur_col) < 0) {
  88.                 k = j * (screen->max_row + 1);
  89.                 i += ((-i) / k + 1) * k;
  90.             }
  91.             screen->cur_row = i / j;
  92.             screen->cur_col = i % j;
  93.         } else
  94.             screen->cur_col = 0;
  95.     }
  96.     screen->do_wrap = 0;
  97.     _CheckSelection(screen);
  98. }
  99.  
  100. /*
  101.  * moves the cursor forward n, no wraparound
  102.  */
  103. CursorForward(screen, n)
  104. register TScreen    *screen;
  105. int        n;
  106. {
  107.     screen->cur_col += n;
  108.     if (screen->cur_col > screen->max_col)
  109.         screen->cur_col = screen->max_col;
  110.     screen->do_wrap = 0;
  111.     _CheckSelection(screen);
  112. }
  113.  
  114. /* 
  115.  * moves the cursor down n, no scrolling.
  116.  * Won't pass bottom margin or bottom of screen.
  117.  */
  118. CursorDown(screen, n)
  119. register TScreen    *screen;
  120. int        n;
  121. {
  122.     register int max;
  123.  
  124.     max = (screen->cur_row > screen->bot_marg ?
  125.         screen->max_row : screen->bot_marg);
  126.  
  127.     screen->cur_row += n;
  128.     if (screen->cur_row > max)
  129.         screen->cur_row = max;
  130.     screen->do_wrap = 0;
  131.     _CheckSelection(screen);
  132. }
  133.  
  134. /* 
  135.  * moves the cursor up n, no linestarving.
  136.  * Won't pass top margin or top of screen.
  137.  */
  138. CursorUp(screen, n)
  139. register TScreen    *screen;
  140. int        n;
  141. {
  142.     register int min;
  143.  
  144.     min = (screen->cur_row < screen->top_marg ?
  145.         0 : screen->top_marg);
  146.  
  147.     screen->cur_row -= n;
  148.     if (screen->cur_row < min)
  149.         screen->cur_row = min;
  150.     screen->do_wrap = 0;
  151.     _CheckSelection(screen);
  152. }
  153.  
  154. /* 
  155.  * Moves cursor down amount lines, scrolls if necessary.
  156.  * Won't leave scrolling region. No carriage return.
  157.  */
  158. Index(screen, amount)
  159. register TScreen    *screen;
  160. register int    amount;
  161. {
  162.     register int j;
  163.  
  164.     /* 
  165.      * indexing when below scrolling region is cursor down.
  166.      * if cursor high enough, no scrolling necessary.
  167.      */
  168.     if (screen->cur_row > screen->bot_marg
  169.      || screen->cur_row + amount <= screen->bot_marg) {
  170.         CursorDown(screen, amount);
  171.         return;
  172.     }
  173.  
  174.     CursorDown(screen, j = screen->bot_marg - screen->cur_row);
  175.     Scroll(screen, amount - j);
  176. }
  177.  
  178. /*
  179.  * Moves cursor up amount lines, reverse scrolls if necessary.
  180.  * Won't leave scrolling region. No carriage return.
  181.  */
  182. RevIndex(screen, amount)
  183. register TScreen    *screen;
  184. register int    amount;
  185. {
  186.     /*
  187.      * reverse indexing when above scrolling region is cursor up.
  188.      * if cursor low enough, no reverse indexing needed
  189.      */
  190.     if (screen->cur_row < screen->top_marg
  191.      || screen->cur_row-amount >= screen->top_marg) {
  192.         CursorUp(screen, amount);
  193.         return;
  194.     }
  195.  
  196.     RevScroll(screen, amount - (screen->cur_row - screen->top_marg));
  197.     CursorUp(screen, screen->cur_row - screen->top_marg);
  198. }
  199.  
  200. /*
  201.  * Moves Cursor To First Column In Line
  202.  */
  203. CarriageReturn(screen)
  204. register TScreen *screen;
  205. {
  206.     screen->cur_col = 0;
  207.     screen->do_wrap = 0;
  208.     _CheckSelection(screen);
  209. }
  210.  
  211. /*
  212.  * Save Cursor and Attributes
  213.  */
  214. CursorSave(term, sc)
  215. register XtermWidget term;
  216. register SavedCursor *sc;
  217. {
  218.     register TScreen *screen = &term->screen;
  219.  
  220.     sc->row = screen->cur_row;
  221.     sc->col = screen->cur_col;
  222.     sc->flags = term->flags;
  223.     sc->curgl = screen->curgl;
  224.     sc->curgr = screen->curgr;
  225.     bcopy(screen->gsets, sc->gsets, sizeof(screen->gsets));
  226. }
  227.  
  228. /*
  229.  * Restore Cursor and Attributes
  230.  */
  231. CursorRestore(term, sc)
  232. register XtermWidget term;
  233. register SavedCursor *sc;
  234. {
  235.     register TScreen *screen = &term->screen;
  236.  
  237.     bcopy(sc->gsets, screen->gsets, sizeof(screen->gsets));
  238.     screen->curgl = sc->curgl;
  239.     screen->curgr = sc->curgr;
  240.     term->flags &= ~(BOLD|INVERSE|UNDERLINE|ORIGIN);
  241.     term->flags |= sc->flags & (BOLD|INVERSE|UNDERLINE|ORIGIN);
  242.     CursorSet (screen, (term->flags & ORIGIN) ? sc->row - screen->top_marg
  243.                : sc->row, sc->col, term->flags);
  244. }
  245.