home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tc20 / mcdisply.c < prev    next >
Text File  |  1988-10-13  |  7KB  |  286 lines

  1. /* Turbo C - (C) Copyright 1987,1988 by Borland International */
  2.  
  3. #include <dos.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <mem.h>
  7. #include <conio.h>
  8. #include "mcalc.h"
  9.  
  10. static unsigned char colortable[256];
  11.  
  12. void setcolor(int color)
  13. /* Sets the current color using the color table */
  14. {
  15.   textattr(colortable[color]);
  16. } /* setcolor */
  17.  
  18. void writef(int col, int row, int color, int width, va_list arg_list, ...)
  19. /* Prints a string in video memory at a selected location in a color */
  20. {
  21.  va_list arg_ptr;
  22.  char *format;
  23.  char output[81];
  24.  int len;
  25.  
  26.  va_start(arg_ptr, arg_list);
  27.  format = arg_list;
  28.  vsprintf(output, format, arg_ptr);
  29.  output[width] = 0;
  30.  if ((len = strlen(output)) < width)
  31.   setmem(&output[len], width - len, ' ');
  32.  setcolor(color);
  33.  gotoxy(col, row);
  34.  cprintf(output);
  35. } /* writef */
  36.  
  37. void scroll(int direction, int lines, int x1, int y1, int x2, int y2,
  38.             int attrib)
  39. /* Scrolls an area of the screen */
  40. {
  41.   if (lines == 0)
  42.     window(x1, y1, x2, y2);
  43.   else switch(direction)
  44.   {
  45.     case UP :
  46.       movetext(x1, y1 + lines, x2, y2, x1, y1);
  47.       window(x1, y2 - lines + 1, x2, y2);
  48.       break;
  49.     case DOWN :
  50.       movetext(x1, y1, x2, y2 - lines, x1, y1 + lines);
  51.       window(x1, y1, x2, y1 + lines - 1);
  52.       break;
  53.     case LEFT :
  54.       movetext(x1 + lines, y1, x2, y2, x1, y1);
  55.       window(x2 - lines + 1, y1, x2, y2);
  56.       break;
  57.     case RIGHT :
  58.       movetext(x1, y1, x2 - lines, y2, x1 + lines, y1);
  59.       window(x1, y1, x1 + lines - 1, y2);
  60.       break;
  61.   } /* switch */
  62.   setcolor(attrib);
  63.   clrscr();
  64.   window(1, 1, 80, 25);
  65. } /* scroll */
  66.  
  67. void setcursor(unsigned int shape)
  68. /* Sets the shape of the cursor */
  69. {
  70.  union REGS reg;
  71.  
  72.  reg.h.ah = 1;
  73.  reg.x.cx = shape;
  74.  int86(0X10, ®, ®);
  75. } /* setcursor */
  76.  
  77. unsigned int getcursor(void)
  78. /* Returns the shape of the current cursor */
  79. {
  80.  union REGS reg;
  81.  
  82.  reg.h.ah = 3;
  83.  reg.h.bh = 0;
  84.  int86(0X10, ®, ®);
  85.  return(reg.x.cx);
  86. } /* getcursor */
  87.  
  88. void changecursor(insmode)
  89. /* Changes the cursor shape based on the current insert mode */
  90. {
  91.  if (insmode)
  92.   setcursor(tallcursor);
  93.  else
  94.   setcursor(shortcursor);
  95. } /* changecursor */
  96.  
  97. void printcol(void)
  98. /* Prints the column headings */
  99. {
  100.  int col;
  101.  char colstr[MAXCOLWIDTH + 1];
  102.  
  103.  scroll(UP, 0, 1, 2, 80, 2, HEADERCOLOR);
  104.  for (col = leftcol; col <= rightcol; col++)
  105.  {
  106.   centercolstring(col, colstr);
  107.   writef(colstart[col - leftcol] + 1, 2, HEADERCOLOR, colwidth[col], colstr);
  108.  }
  109. } /* printcol */
  110.  
  111. void clearlastcol()
  112. /* Clears any data left in the last column */
  113. {
  114.  int col;
  115.  
  116.  if ((col = colstart[rightcol - leftcol] + colwidth[rightcol]) < 80)
  117.   scroll(UP, 0, col + 1, 3, 80, SCREENROWS + 2, WHITE);
  118. } /* clearlastcol */
  119.  
  120. void printrow(void)
  121. /* Prints the row headings */
  122. {
  123.  int row;
  124.  
  125.  for (row = 0; row < SCREENROWS; row++)
  126.   writef(1, row + 3, HEADERCOLOR, LEFTMARGIN, "%-d", row + toprow + 1);
  127. } /* printrow */
  128.  
  129. void displaycell(int col, int row, int highlighting, int updating)
  130. /* Displays the contents of a cell */
  131. {
  132.  int color;
  133.  char *s;
  134.  
  135.  if ((updating) &&
  136.      ((cell[col][row] == NULL) || (cell[col][row]->attrib != FORMULA)))
  137.   return;
  138.  s = cellstring(col, row, &color, FORMAT);
  139.  if (highlighting)
  140.  {
  141.   if (color == ERRORCOLOR)
  142.    color = HIGHLIGHTERRORCOLOR;
  143.   else
  144.    color = HIGHLIGHTCOLOR;
  145.  }
  146.  writef(colstart[col - leftcol] + 1, row - toprow + 3, color, colwidth[col],
  147.         "%s", s);
  148. } /* displaycell */
  149.  
  150. void displaycol(int col, int updating)
  151. /* Displays a column on the screen */
  152. {
  153.  int row;
  154.  
  155.  for (row = toprow; row <= bottomrow; row++)
  156.   displaycell(col, row, NOHIGHLIGHT, updating);
  157. } /* displaycol */
  158.  
  159. void displayrow(int row, int updating)
  160. /* Displays a row on the screen */
  161. {
  162.  int col;
  163.  
  164.  for (col = leftcol; col <= rightcol; col++)
  165.   displaycell(col, row, NOHIGHLIGHT, updating);
  166. } /* displayrow */
  167.  
  168. void displayscreen(int updating)
  169. /* Displays the current screen of the spreadsheet */
  170. {
  171.  int row;
  172.  
  173.  for (row = toprow; row <= bottomrow; row++)
  174.   displayrow(row, updating);
  175.  clearlastcol();
  176. } /* displayscreen */
  177.  
  178. void clearinput(void)
  179. /* Clears the input line */
  180. {
  181.  scroll(UP, 0, 1, 25, 80, 25, WHITE);
  182.  gotoxy(1, 25);
  183. } /* clearinput */
  184.  
  185. void showcelltype(void)
  186. /* Prints the type of cell and what is in it */
  187. {
  188.  char colstr[3], *s;
  189.  int color;
  190.  
  191.  formdisplay = !formdisplay;
  192.  s = cellstring(curcol, currow, &color, NOFORMAT);
  193.  colstring(curcol, colstr);
  194.  if (curcell == NULL)
  195.   writef(1, 23, CELLTYPECOLOR, 80, "%s%d %s", colstr, currow + 1, MSGEMPTY);
  196.  else switch(curcell->attrib)
  197.  {
  198.   case TEXT :
  199.    writef(1, 23, CELLTYPECOLOR, 80, "%s%d %s", colstr, currow + 1,
  200.           MSGTEXT);
  201.    break;
  202.   case VALUE :
  203.    writef(1, 23, CELLTYPECOLOR, 80, "%s%d %s", colstr, currow + 1,
  204.           MSGVALUE);
  205.    break;
  206.   case FORMULA :
  207.    writef(1, 23, CELLTYPECOLOR, 80, "%s%d %s", colstr, currow + 1,
  208.           MSGFORMULA);
  209.    break;
  210.  } /* switch */
  211.  writef(1, 24, CELLCONTENTSCOLOR, 80, "%s", s);
  212.  formdisplay = !formdisplay;
  213. } /* showcelltype */
  214.  
  215. void redrawscreen(void)
  216. /* Displays the entire screen */
  217. {
  218.  setrightcol();
  219.  setbottomrow();
  220.  writef(1, 1, MSGMEMORYCOLOR, strlen(MSGMEMORY), MSGMEMORY);
  221.  writef(29, 1, PROMPTCOLOR, strlen(MSGCOMMAND), MSGCOMMAND);
  222.  changeautocalc(autocalc);
  223.  changeformdisplay(formdisplay);
  224.  printfreemem();
  225.  displayscreen(NOUPDATE);
  226. } /* redrawscreen */
  227.  
  228. void initcursor(void)
  229. /* Initializes the different cursor types */
  230. {
  231.  struct text_info ti;
  232.  
  233.  gettextinfo(&ti);
  234.  oldcursor = getcursor();
  235.  if (ti.currmode == MONO)
  236.  {
  237.   shortcursor = 0x0A0C;
  238.   tallcursor = 0x090C;
  239.  }
  240.  else
  241.  {
  242.   shortcursor = 0x0607;
  243.   tallcursor = 0x0507;
  244.  }
  245. } /* initcursor */
  246.  
  247. void initcolortable(void)
  248. /* Sets up the color table */
  249. {
  250.   int color, fg, bg, fcolor, bcolor;
  251.   struct text_info ti;
  252.  
  253.   gettextinfo(&ti);
  254.   if (ti.currmode == C80)
  255.   {
  256.     for (color = 0; color <= 255; color++)
  257.       colortable[color] = color;
  258.   }
  259.   else
  260.   {
  261.     for (fg = BLACK; fg <= WHITE; fg++)
  262.     {
  263.       if (fg == BLACK)
  264.         fcolor = BLACK;
  265.       else if (fg <= LIGHTGRAY)
  266.         fcolor = LIGHTGRAY;
  267.       else
  268.         fcolor = WHITE;
  269.       for (bg = BLACK; bg <= LIGHTGRAY; bg++)
  270.       {
  271.         if (bg == BLACK)
  272.           bcolor = BLACK;
  273.         else
  274.   {
  275.           if (fcolor == WHITE)
  276.             fcolor = BLACK;
  277.           bcolor = LIGHTGRAY;
  278.         }
  279.         colortable[fg + (bg << 4)] = fcolor + (bcolor << 4);
  280.       }
  281.     }
  282.     for (fg = 128; fg <= 255; fg++)
  283.       colortable[fg] = colortable[fg - 128] | 0x80;
  284.   }
  285. } /* initcolortable */
  286.