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

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   L_GETFLD.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. /*****************************************************************
  10.  
  11.  Usage: char getfield( char *string, int inlength, int start,
  12.                       char attribute);
  13.  
  14.   char *string=   string to edit (if empty "" then accepts new text.
  15.   int inlength=   maximum string length allowed.
  16.   int start=      position within string to start editing.
  17.   char attribute= text attribute to use to highlight field.
  18.  
  19.   Allows input of text.  If *string contains text, then *string
  20.   may be edited.
  21.  
  22.   Editing keys (home,end etc) allowed.
  23.  
  24. *****************************************************************/
  25.  
  26. char getfield(char *string,int inlength,int start, char attribute)
  27. {
  28. extern struct  screen_structure scr;
  29. extern struct window_structure w[];
  30.  
  31. int width;
  32. char left[255], right[255], chip[255];
  33. char ch=' ',ext2=' ';
  34. int i,x,y,orig_x,orig_y,temp_x,temp_y;
  35. int insert=FALSE;
  36. int letter=0,len_left=0, len_right=0;
  37. int return_code;
  38. int old_attribute=scr.current;
  39.  
  40. scr.current=attribute;     /* use specified attribute */
  41. wherexy(&orig_x,&orig_y);  /* save original cursor position */
  42. x=orig_x;y=orig_y;         /* set x,y to current cursor location */
  43.  
  44. hilight_field(x,y,inlength,attribute);  /* highlight input area */
  45.  
  46. width=scr.right-scr.left +1;  /* get the width of the window */
  47.  
  48. chip[0]= '\0'; left[0]= '\0';right[0]= '\0';
  49.  
  50.   strcpy(right,string);
  51.   i=strlen(right);
  52.   if(start>i)       /* break up string into left and right
  53.                        components, depending on the requested
  54.                        start position */
  55.      chip_left(left,right,i);
  56.    else
  57.     chip_left(left,right,start);
  58.  
  59.  print(x,y,left);     /* print left string */
  60.  wherexy(&x,&y);      /* store cursor edit position */
  61.  print_here(right);   /* print right string */
  62.  
  63.  goxy(x,y);         /* move cursor back to edit location */
  64.  
  65. for (;;){    /* begin input cycle */
  66.   ext2= 0; ch= 0;letter=0;
  67.  
  68.   len_left=strlen(left);     /* get string lengths */
  69.   len_right = strlen(right); 
  70.  
  71.   get_key(&ch,&ext2);
  72.  
  73.   if (ch >=32) letter =TRUE;
  74.  
  75.   if (ext2 == INSERT){     /* toggle inset/overtype mode */
  76.     insert = !insert;
  77.     if(insert) cursor(2);  /* make big cursor to show insert */
  78.     else cursor(1);
  79.   }
  80.  
  81.    if(len_right>0) trim_right(right); /* Trim trailing blanks
  82.                                          so we avoid problems
  83.                                          with the insert
  84.                                          feature. */
  85.   len_left=strlen(left); len_right = strlen(right);
  86.  
  87.   if (!letter){         /* not regular letter */
  88.  
  89.    if (ext2 == LEFT && len_left >0){  /* if it is a left arrow  */
  90.  
  91.     chip_right(chip,left,1);  /* remove end of left and  */
  92.     strcat(chip,right);       /* add to right            */
  93.     strcpy(right,chip);
  94.     x--; len_left--;
  95.    }
  96.  
  97.    if (ext2 == HOME && len_left >0){ /* home */
  98.     strcat(left,right);     /* concat stings */
  99.     strcpy(right,left);     /* copy combined strings to right */
  100.     len_left = 0;           /* set left length variable to zero */
  101.     len_right = len_left+len_right;  /* set new right length */
  102.     left[0] ='\0';                   /* set left string to "" */
  103.     x=orig_x; y=orig_y;   /* move cursor to beginning of field */
  104.    }
  105.  
  106.    if (ext2 == END){          /* end */
  107.     print (x,y,right);        /* print right part of string */
  108.     wherexy(&x,&y);           /* get cursor location */
  109.     strcat(left,right);       /* combine left and right in left */
  110.      len_right = 0;           /* new length to be zero */
  111.      len_left = len_left+len_right;   /* new right length */
  112.     right[0] ='\0';                   /* right ="" */
  113.    }
  114.  
  115.    if (ext2 == RIGHT && len_right> 0){  /* if it is right arrow */
  116.     chip_left(chip,right,1);      /* chip left end off right sting */
  117.     strcat (left,chip);           /* add it to left string */
  118.     x++;                 /* update cursor location */
  119.     len_right++;         /* adjust length */
  120.     goxy(x,y);        /* move cursor */
  121.    }
  122.    if (ext2 == DELETE && len_right >0){  /* delete */
  123.     chip_left(chip,right,1); /* chip left end of right string */
  124.     len_right--;            /* update length */
  125.    }
  126.  
  127.   } /* end  if not letter */
  128.  
  129.  if (ch == BACKSPACE && len_left >0) {   /* if it is backspace */
  130.   chip_right(chip,left,1);    /* chip right end of left string */
  131.   x--;                        /* up-date cursor */
  132.   len_left--;                 /* up-date length */
  133.  }
  134.  
  135. /* enter here if it is letter   */
  136.  
  137.   if (letter){
  138.  
  139.    /* insert mode on and no room for char */
  140.    if ((insert && (len_left +len_right >= inlength))){  
  141.     putchar(BELL);  letter = 0;  /* no room for char */
  142.    }
  143.     else{
  144.      if(insert){        /* insert mode on */
  145.       x++;              /* update cursor location */
  146.       /* add char to string */
  147.       left[len_left++] = ch; left[len_left]= '\0'; 
  148.      }
  149.       if(!insert){      /* in overtype mode */
  150.          if( len_left ==inlength){    /* no room for character */
  151.           putchar(BELL);              /* "beep" the user */
  152.           letter =0;
  153.          }
  154.          else
  155.         {
  156.          /* add new character, overtype old character */
  157.          chip_left(chip,right,1);   /* chip off left end of
  158.                                        right string */
  159.          left[len_left++] = ch ;/* add new character */
  160.          left[len_left]= '\0'; 
  161.          len_right--;      /* record new right length */
  162.          x++;
  163.         }
  164.       }  /* not insert */
  165.  
  166.     }
  167.   }            /* end if (letter) */
  168.  
  169.         /* check for boundry conditions */
  170.  
  171.       if (x<1 && y == orig_y) x=1; else
  172.  
  173.       if (x<1){ y--; x=width;}
  174.       if (x>width){   /* have we reached the end of the window ? */
  175.         y++;x=1;
  176.       }
  177.  
  178.       if (ext2 == LEFT || ext2 == RIGHT|| ext2 == HOME) goxy(x,y);
  179.  
  180.       /* if backspace or delete then redraw the line */
  181.       if( ch == BACKSPACE || ext2 == DELETE){   
  182.          goxy(x,y);
  183.          temp_x=x; temp_y=y;
  184.           /* print right string (don't move cursor) */
  185.           dma_print(&temp_x,&temp_y,right); 
  186.           /* print  blanks to cover old trailing text */
  187.           dma_print(&temp_x,&temp_y," ");   
  188.       }
  189.           if (letter ){
  190.             chip[0]=ch;chip[1]='\0'; /* turn character into string */
  191.             print_here(chip);  /* print string at current location */
  192.             wherexy(&x,&y);    /* get new location */
  193.  
  194.             if (insert){
  195.                temp_x=x; temp_y=y;
  196.                dma_print(&temp_x,&temp_y,right);
  197.              }
  198.           }
  199.  
  200.    if (ch==ESCAPE || ext2 == UP || ext2 == DOWN) break;
  201.    if (ext2 == PGUP || ext2 == PGDN) break;
  202.    if (ch==RETURN /*|| ext2 == UP || ext2 == DOWN*/) break;
  203.  
  204. }  /* end of for loop */
  205.  
  206.  switch(ext2){
  207.   case UP :return_code=UP;break;
  208.   case DOWN:return_code=DOWN;break;
  209.   case PGUP:return_code=PGUP;break;
  210.   case PGDN:return_code=PGDN;break;
  211.  }
  212.  
  213. if (ch==RETURN) return_code = RETURN;
  214. if (ch==ESCAPE) return_code = ESCAPE;
  215.  strcat(left,right);
  216.  strcpy(string,left);
  217. cursor(NORMAL_CURSOR); /* set normal cursor */
  218. scr.current=old_attribute;
  219. return(return_code);
  220.  
  221. }
  222.  
  223.  
  224. /*****************************************************************
  225.  
  226.  Usage: static hilight_field (int x, int y, int length,
  227.                               char attribute);
  228.  
  229.  
  230.   int x,y =        column,row to place highlighted field.
  231.   int length=      length of highlighted area.
  232.   char *attribute= text attribute to use for highlighted area.
  233.  
  234.  
  235.   Creates a highlighted area on the screen.
  236.  
  237. *****************************************************************/
  238.  
  239. void hilight_field (int x, int y, int length, char attribute)
  240. {
  241. extern struct  screen_structure scr;
  242. extern struct window_structure w[];
  243.  
  244. char hilight[MAX_STRING];
  245. int i;
  246. char old_attribute=scr.current;  /* save current attribute */
  247. scr.current=attribute;  /* set current attribute */
  248.  
  249.     for(i=0;i<length;i++){  /* build a string of blanks */
  250.       hilight[i]=' ';
  251.     }
  252.     hilight[i]='\0'; /* terminate string */
  253.  
  254.     dma_print(&x,&y,hilight); /* use dma_print which does
  255.                                  not move the cursor */
  256.  
  257.    scr.current=old_attribute; /* restore current attribute */
  258. }
  259.