home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Ports / mac / caret.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-07  |  2.8 KB  |  115 lines  |  [TEXT/????]

  1. /* MAC STDWIN -- TEXT CARET HANDLING. */
  2.  
  3. /* The 'caret' is a blinking vertical bar indicating the text insertion point.
  4.    Each window has its own caret position (hcaret, vcaret) telling where the
  5.    insertion point is; if either is negative no caret is shown.
  6.    Only the caret of the active window is actually visible.
  7.    The blinking is done by repeatedly calling (in the event loop when idling)
  8.    blinkcaret(), which flips the caret's state if it's been stable long enough.
  9.    The file-static variable lastflip records when the caret has last changed
  10.    state.
  11.    A struct member caret_on indicates per window whether the caret is
  12.    currently drawn or not.  (A file-static variable would really suffice.)
  13.  
  14.    NB: all routines except w{set,no}caret assume win->w is the current GrafPort.    
  15. */
  16.  
  17. #include "macwin.h"
  18.  
  19. #ifdef MPW
  20. #include <Events.h>
  21. #endif
  22.  
  23. /* Function prototypes */
  24. STATIC void invertcaret _ARGS((WINDOW *win));
  25. STATIC void flipcaret _ARGS((WINDOW *win));
  26.  
  27. static long lastflip = 0;    /* Time when the caret last blinked */
  28.  
  29. /* Invert the caret's pixels. */
  30.  
  31. static void
  32. invertcaret(win)
  33.     WINDOW *win;
  34. {
  35.     Rect r;
  36.     FontInfo finfo;
  37.     
  38.     if (win->hcaret < 0 || win->vcaret < 0)
  39.         return;
  40.     GetFontInfo(&finfo);
  41.     makerect(win, &r,
  42.         win->hcaret-1,
  43.         win->vcaret,
  44.         win->hcaret,
  45.         win->vcaret + finfo.leading + finfo.ascent + finfo.descent);
  46.     InvertRect(&r);
  47. }
  48.  
  49. /* Flip the caret state, if the window has a caret.
  50.    Also set lastflip, so blinkcaret() below knows when to flip it again. */
  51.  
  52. static void
  53. flipcaret(win)
  54.     WINDOW *win;
  55. {
  56.     if (win->hcaret < 0 || win->vcaret < 0)
  57.         return;
  58.     invertcaret(win);
  59.     win->caret_on = !win->caret_on;
  60.     lastflip= TickCount();
  61. }
  62.  
  63. /* Make sure the caret is invisible.
  64.    This is necessary before operations like handling mouse clicks,
  65.    bringing up dialog boxes, or when the window goes inactive. */
  66.  
  67. void
  68. rmcaret(win)
  69.     WINDOW *win;
  70. {
  71.     if (win->caret_on)
  72.         flipcaret(win);
  73. }
  74.  
  75. /* Blink the caret, if there is one.
  76.    This should be called sufficiently often from the main event loop.
  77.    We only blink if enough ticks have passed since the last flip,
  78.    whether caused by us or by an explicit call to rmcaret(). */
  79.  
  80. void
  81. blinkcaret(win)
  82.     WINDOW *win;
  83. {
  84.     if (win->hcaret < 0 || win->vcaret < 0)
  85.         return;
  86.     if (TickCount() >= lastflip + GetCaretTime())
  87.         flipcaret(win);
  88. }
  89.  
  90. /* User-callable routine to specify the caret position.
  91.    The caret is not drawn now, but when the next blink is due. */
  92.  
  93. void
  94. wsetcaret(win, h, v)
  95.     WINDOW *win;
  96.     int h, v;
  97. {
  98.     SetPort(win->w);
  99.     rmcaret(win);
  100.     win->hcaret = h;
  101.     win->vcaret = v;
  102. }
  103.  
  104. /* User-callable routine to specify that the window has no caret.
  105.    This is indicated by setting (hcaret, vcaret) to (-1, -1). */
  106.  
  107. void
  108. wnocaret(win)
  109.     WINDOW *win;
  110. {
  111.     SetPort(win->w);
  112.     rmcaret(win);    /* See remark in wsetcaret() */
  113.     win->hcaret = win->vcaret= -1;
  114. }
  115.