home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DSIIC2.ZIP / L_PRINT.C < prev    next >
C/C++ Source or Header  |  1991-07-15  |  7KB  |  232 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   L_PRINT.C  ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <dos.h>
  7.  
  8. #if defined TURBOC
  9. #include <mem.h>
  10. #endif
  11.  
  12. #include <string.h>
  13.  
  14.  
  15. /*****************************************************************
  16.  
  17.  Usage: void print(int x, int y, char *string);
  18.  
  19.  int x,y       = column, row  at which text is printed.
  20.  char * string = string to print.
  21.  
  22.  Prints the string at the location indicated for the active window.
  23.  The string is printed with the text attribute stored in scr.current.
  24.  If the external variable scr.bold_caps is set true, uppercase
  25.  letters are printed with the attribute intensity set high.
  26.  Text is wrapped to fit within the window, and the window is scrolled
  27.  up when the bottom of the screen is reached.  The cursor position is
  28.  updated.
  29.  
  30.  Calls dma_write() to do actual work.
  31.  
  32. *****************************************************************/
  33.  
  34. void print(int x,int y,char *string)
  35. {
  36.   int x2,y2;
  37.   x2=x;y2=y;                /* make copies of x,y, we don't want
  38.                                original values changed */
  39.   dma_print(&x2,&y2,string);  /* call dma_print to write the text */
  40.   goxy(x2,y2);              /* move the cursor */
  41. }
  42.  
  43.  
  44. /*****************************************************************
  45.  
  46.  Usage: void print_here(char *string);
  47.  
  48.  char * string = string to print.
  49.  
  50.  Just like print() except that it prints the text at the current
  51.  cursor location.
  52.  
  53. *****************************************************************/
  54.  
  55. void print_here(char *string)
  56.  
  57. {
  58.  int  x,y;
  59.  wherexy(&x,&y);    /* find out where the cursor is */
  60.  print(x,y,string); /* print the string */
  61. }
  62.  
  63.  
  64. /*****************************************************************
  65.  
  66.  Usage: void dma_print(int *x, int *y, char *string);
  67.  
  68.  int *x,*y       = column, row  at which text is printed.
  69.  char * string = string to print.
  70.  
  71.  Works like print() except the cursor is not moved.  x and y are
  72.  set to the location that the cursor should occupy. This function
  73.  is useful if you don't actually want the cursor moved.
  74.  
  75. *****************************************************************/
  76.  
  77.  
  78. void dma_print(int *x, int *y,char *string)
  79. {
  80. extern struct  screen_structure scr;
  81. extern struct window_structure w[];
  82.  
  83.  int i,j,str_index,write_width;
  84.  int abs_x,abs_y;
  85.  int orig_y;
  86.  int width,height;
  87.  unsigned  offset;
  88.  char far *video_buffer;
  89.  char string2[MAX_STRING*2];
  90.  
  91.  orig_y=*y;  /* save the original y */
  92.  width=scr.right-scr.left+1;    /* figure the length of the window */
  93.  height=scr.bottom-scr.top +1;  /* figure the height of the window */
  94.  
  95.  if(*y> height){  /* if y is beyond last line of window*/
  96.   *y= height;     /* set y = last line of window */
  97.   scroll_up(1);   /* scroll_up window to make space */
  98.  }
  99.   if(*x> width)*x=width; /* if x greater than last column, */
  100.                          /* set equal to last column */
  101.  
  102.    abs_x=(*x-1)+scr.left;  /* adjust to screen coordinates */
  103.    abs_y=(*y-1)+scr.top;
  104.  
  105. /* do final adjustment of coordinates so they fit screen */
  106.    if (abs_x<scr.left) abs_x=scr.left;
  107.    if (abs_x>scr.right) abs_x=scr.right;
  108.    if (abs_y<scr.top) abs_y=scr.top;
  109.    if (abs_y>scr.bottom) abs_y=scr.bottom;
  110.  
  111. /* the following code creates a new string, padded with attributes */
  112.    i=0;
  113.    while(*string){
  114.       string2[i++]=*string; /*copy char to string2 */
  115.  
  116.        /* is it an uppercase char ?*/
  117.        if (*string >=65 && *string <=90 && scr.bold_caps==TRUE)
  118.          string2[i++]= set_intense(scr.current); else
  119.          string2[i++]=scr.current;
  120.           string++;
  121.    }
  122.  
  123.       string2[i]='\0';  /* terminate string */
  124.  
  125. /* keep index pointer for location within string2, copy sections of
  126.    the string into the screen buffer, staying within the screen
  127.    margins */
  128.  
  129.        str_index=0;
  130.        j=0;
  131.  
  132.        for(;;){  /* do for each row of window */
  133.         /* calculate offset */
  134.         offset=(((abs_y-1)*scr.columns)+(abs_x-1))*2; 
  135.  
  136.         /* figure width of sting to write */
  137.         write_width=(scr.right-abs_x+1)*2;  
  138.  
  139.         /* trying to write beyond end of string ? */
  140.         if (str_index+write_width>strlen(string2))
  141.          write_width=strlen(string2)-str_index; /* adjust length */
  142.          video_buffer=scr.buffer+offset;      /* set video pointer */
  143.  
  144.         /* move the string segment */
  145.         move_scr_mem((char far *)&string2[str_index],
  146.                       video_buffer,write_width);
  147.  
  148.         abs_x=scr.left;
  149.         abs_y++; j++;   /* update index to new location */
  150.         str_index=str_index+write_width; /* increment string index */
  151.  
  152.         if(str_index>=strlen(string2)) break;  /* end of string */
  153.         if(abs_y>scr.bottom){   /* past bottom of window */
  154.           scroll_up(1);
  155.           abs_y--;
  156.          }
  157.       }
  158.  
  159.      /* update cursor location */
  160.  
  161.       *y=*y+j-1;
  162.       if(*y>orig_y)
  163.        *x=(write_width/2)+1;
  164.       else
  165.       *x=*x+(write_width/2);
  166.      /* if last char printed on border */
  167.      if((*x>width) && *y< height){
  168.       (*y)++; *x=1;                   /* move cursor down */
  169.      }
  170. }
  171.  
  172.  
  173. /*****************************************************************
  174.  
  175.  Usage: void move_scr_mem(char far *string,char far *video,
  176.                           int number)
  177.  
  178.  char far *string = string to print.
  179.  char far *video  = address within screen buffer to which text is
  180.                     written.
  181.  int number       = the number of bytes to write.
  182.  
  183.  Copies the string to the video buffer.  The pointers are far,
  184.  so the string could be copied any where in conventional memory.
  185.  Can also copy from screen to another location.  Avoids screen
  186.  snow on CGA display adapters when the external variable scr.snow
  187.  is set to TRUE.
  188.  
  189. *****************************************************************/
  190.  
  191. void move_scr_mem (char far *string,char far *video,int number)
  192. {
  193. extern struct  screen_structure scr;
  194. extern struct window_structure w[];
  195.  
  196. unsigned int sseg,soff,dseg,doff;
  197. int regval;
  198. int origval;
  199. int vert;
  200. char far *ptr;
  201.  
  202. sseg=FP_SEG(string);soff=FP_OFF(string);
  203. dseg=FP_SEG(video);doff=FP_OFF(video);
  204.  
  205.  if (scr.snow==TRUE){
  206.  
  207.    /* save the mode setting */
  208.    ptr=MAKE_FP(0 ,0x465);  /* point to BIOS location for mode */
  209.    regval=*ptr;            /* read the mode */
  210.    origval=regval;         /* save the original value */
  211.    regval=regval&0x37;     /* mask bit 3 off */
  212.  
  213.  
  214.    /* wait for the CGA status register (bit 3) to go from 0 to 1 */
  215.    for(;;){
  216.     vert =inp(0x3da);        /* read the CGA status register */
  217.     if ((vert&8)==0) break;  /* is bit three equal 0? if so, break */
  218.    }
  219.  
  220.    for(;;){
  221.     vert=inp(0x3da);        /* read the status register again */
  222.     if((vert&8)==8) break;  /* exit when bit three equals 1   */
  223.    }
  224.    outp (0x3d8,regval);   /* send new mode to CGA controller */
  225.  }
  226.  
  227.  movedata(sseg,soff,dseg,doff,number);  /* move the data */
  228.  
  229.  if (scr.snow==TRUE)
  230.       outp(0x3d8,origval); /* set controller to original value */
  231. }
  232.