home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part03 / char.c next >
C/C++ Source or Header  |  1990-07-02  |  4KB  |  161 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1988 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    Febuary 1988.
  6.  *
  7.  *    %W%    %G%
  8. */
  9. #include "fig.h"
  10. #include "resources.h"
  11. #include "font.h"
  12. #include "paintop.h"
  13. #include "object.h"
  14.  
  15. extern int        cur_x, cur_y;
  16. extern int        cur_font, cur_fontsize;
  17. extern int        cur_textjust;
  18.  
  19. #define            BLINK_INTERVAL    700    /* milliseconds blink rate */
  20. #define            BUF_SIZE    400
  21.     
  22. char            prefix[BUF_SIZE],    /* part of string left of mouse click */
  23.             suffix[BUF_SIZE];    /* part to right of click */
  24. int            leng_prefix, leng_suffix;
  25.  
  26. static PIXWIN        pw;
  27. static int        char_ht;
  28. static int        char_received;
  29. static int        cbase_x, cbase_y;
  30. static float        rbase_x, rcur_x;
  31. static int        obase_x;
  32.  
  33. static            (*cr_proc)();
  34.  
  35. static
  36. draw_cursor(x, y)
  37. int    x, y;
  38. {
  39.     pw_vector(pw, x, y, x, y-char_ht, INV_PAINT, 1, SOLID_LINE, 0.0);
  40.     }
  41.  
  42. initialize_char_handler(p, cr, bx, by)
  43. PIXWIN        p;
  44. int        (*cr)();
  45. int        bx, by;
  46. {
  47.     pw = p;
  48.     cr_proc = cr;
  49.     rbase_x = obase_x = cbase_x = bx;    /* keep real base so dont have roundoff */
  50.     rcur_x = cur_x;
  51.     cbase_y = by;
  52.  
  53.     char_ht = char_height(canvas_font);
  54.     char_received = 0;
  55.     turn_on_blinking_cursor(draw_cursor, draw_cursor,
  56.                 cur_x, cur_y, (long)BLINK_INTERVAL);
  57.     }
  58.  
  59. terminate_char_handler()
  60. {
  61.     turn_off_blinking_cursor();
  62.     cr_proc = NULL;
  63.     return(char_received);
  64.     }
  65.  
  66. erase_char_string()
  67. {
  68.     pw_text(pw, cbase_x, cbase_y, INV_PAINT, 
  69.         cur_font, cur_fontsize, prefix);
  70.     if (leng_suffix) 
  71.         pw_text(pw, cur_x, cbase_y, INV_PAINT, 
  72.             cur_font, cur_fontsize, suffix);
  73.     }
  74.  
  75. draw_char_string()
  76. {
  77.     pw_text(pw, cbase_x, cbase_y, INV_PAINT, 
  78.         cur_font, cur_fontsize, prefix);
  79.     if (leng_suffix) 
  80.         pw_text(pw, cur_x, cbase_y, INV_PAINT, 
  81.             cur_font, cur_fontsize, suffix);
  82.     move_blinking_cursor(cur_x, cur_y);
  83.     }
  84.  
  85. char_handler(c)
  86. unsigned char c;
  87. {
  88.     int cwidth; 
  89.  
  90.     if (cr_proc == NULL)
  91.         return;
  92.  
  93.     if (c == CR) {
  94.         erase_char_string();
  95.         cr_proc();
  96.         }
  97.     else if (c == DEL || c == CTRL_H) {
  98.         if (leng_prefix > 0) {
  99.         erase_char_string();
  100.         char_received = 1;
  101.         cwidth = char_advance(canvas_font,prefix[leng_prefix-1]);
  102.         if (cur_textjust == T_LEFT_JUSTIFIED)
  103.             rcur_x -= cwidth;    /* move the suffix to the left */
  104.         else if (cur_textjust == T_CENTER_JUSTIFIED)
  105.             {
  106.             rbase_x += cwidth/2.0;    /* advance right by half cwidth */
  107.             rcur_x -= cwidth/2.0;    /* move suffix left half cwidth */
  108.             }
  109.         if (cur_textjust == T_RIGHT_JUSTIFIED)
  110.             rbase_x += (float) cwidth;    /* move the prefix to the right */
  111.         prefix[--leng_prefix] = '\0';
  112.         cbase_x = rbase_x;        /* fix */
  113.         cur_x = rcur_x;
  114.         draw_char_string();
  115.         }
  116.         }
  117.     else if (c == CTRL_U || c == CTRL_X) {
  118.         if (leng_prefix > 0) {
  119.         erase_char_string();
  120.         char_received = 1;
  121.         if (cur_textjust == T_CENTER_JUSTIFIED)
  122.             {
  123.             while (leng_prefix--) /* subtract half of the character widths */
  124.             rcur_x -= char_advance(canvas_font,prefix[leng_prefix])/2.0;
  125.             cur_x = cbase_x = rbase_x = rcur_x ;
  126.             }
  127.         else if (cur_textjust == T_RIGHT_JUSTIFIED)
  128.             cbase_x = rbase_x = cur_x = rcur_x;
  129.         else
  130.             cur_x = rcur_x = cbase_x = rbase_x;
  131.         leng_prefix = 0;
  132.         *prefix = '\0';
  133.         draw_char_string();
  134.         }
  135.         }
  136.     else if (c < SP) {
  137.         }
  138.     else if (leng_prefix+leng_suffix == BUF_SIZE) {
  139.         put_msg("Text buffer is full, character is ignored");
  140.         }
  141.     else {
  142.         erase_char_string();
  143.         cwidth = char_advance(canvas_font,c);
  144.         if (cur_textjust == T_LEFT_JUSTIFIED)
  145.             rcur_x += cwidth;    /* move the suffix to the right */
  146.         else if (cur_textjust == T_CENTER_JUSTIFIED)
  147.         {
  148.         rbase_x -= cwidth/2.0;        /* advance left by half cwidth */
  149.         rcur_x += cwidth/2.0;        /* move suffix right half cwidth */
  150.         }
  151.         if (cur_textjust == T_RIGHT_JUSTIFIED)
  152.             rbase_x -= cwidth;    /* move the prefix to the left */
  153.         char_received = 1;
  154.         prefix[leng_prefix++] = c;
  155.         prefix[leng_prefix] = '\0';
  156.         cbase_x = rbase_x;            /* fix */
  157.         cur_x = rcur_x;
  158.         draw_char_string();
  159.         }
  160.     }
  161.