home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 298.lha / AskTask / at_input.c < prev    next >
C/C++ Source or Header  |  1980-11-30  |  7KB  |  191 lines

  1. /* at_input.c */
  2. /*************************************************************************
  3.  ***                        AskTask Input Module                       ***
  4.  *** Date begun: 1/4/89.                                               ***
  5.  *** Last modified: 1/4/89.                                            ***
  6.  *************************************************************************/
  7. /*** Final version now usable with other programs (with trans.c).      ***
  8.  *************************************************************************/
  9.  
  10. #include <exec/types.h>
  11. #include <intuition/intuition.h>
  12. #include <proto/graphics.h>
  13. #include <proto/intuition.h>
  14. #include <string.h>
  15.  
  16. extern char trans(int);
  17.  
  18. static struct RastPort  *rport;
  19. static char inbuf[81],*vset;
  20. static int  cursor,length,start,slen,top;
  21. static struct IntuiText inputline = {1,2,JAM2,0,0,NULL,&inbuf[0],NULL};
  22. /* Note: track strlen(inbuf) with dead reckoning - slen */
  23.  
  24. static void clear(int);
  25. static void drawcursor();
  26. static void forward();
  27. static void backward();
  28. static void delprev();
  29. static void delnext();
  30. static void insert(char);
  31. static void reset();
  32. static void delEOL();
  33. static void delBOL();
  34. void    input(struct Window *,char *,char *,char *,int,int,int,int,int);
  35.  
  36. static void reset() /*===================================================*/
  37. {                   /* Clear input line.                                 */
  38.     slen = 0;   cursor = 0;
  39.     inbuf[0] = 0;
  40. }
  41.  
  42. static void delEOL() /*==================================================*/
  43. {                    /* Delete to end of line.                           */
  44.     inbuf[cursor] = 0;
  45.     slen = strlen(inbuf);   /* Recalibrate */
  46. }
  47.  
  48. static void delBOL() /*==================================================*/
  49. {                    /* Delete to beginning of line.                     */
  50. int     i,j;
  51.     i = 0;
  52.     for (j = cursor; j <= slen; j++) inbuf[i++] = inbuf[j];
  53.     cursor = 0;
  54.     slen = strlen(inbuf);   /* Recalibrate */
  55. }
  56.  
  57. static void clear(b) /*==================================================*/
  58. int     b;           /* Blank from EOL to end of input rect + 8 pixels.  */
  59. {
  60. int     x,mx;
  61.     mx = start + 7 + (length * 8);
  62.     x = start + (slen * 8);
  63.     SetAPen(rport,b);
  64.     RectFill(rport,x,top,mx,top+7);
  65. }
  66.  
  67. static void drawcursor() /*==============================================*/
  68. {                        /* XOR the cursor rectangle.                    */
  69. int     x;
  70.     x = start + cursor * 8;
  71.     SetDrMd(rport,COMPLEMENT);
  72.     RectFill(rport,x,top,x+7,top+7);
  73.     SetDrMd(rport,JAM1);
  74. }
  75.  
  76. static void forward() /*=================================================*/
  77. {                     /* Advance a character.                            */
  78.     if (cursor < slen) cursor++;
  79. }
  80.  
  81. static void backward() /*================================================*/
  82. {                      /* Reverse a character.                           */
  83.     if (cursor > 0) cursor--;
  84. }
  85.  
  86. static void delprev() /*=================================================*/
  87. {                     /* Delete previous character.                      */
  88. int     e,j;
  89.     if (cursor == 0) return;
  90.     e = slen;
  91.     for (j = cursor; j <= e; j++) inbuf[j-1] = inbuf[j];
  92.     cursor--;
  93.     slen--;
  94. }
  95.  
  96. static void delnext() /*=================================================*/
  97. {                     /* Delete next character (at cursor pos).          */
  98. int     e,j;
  99.     if (!inbuf[cursor]) return;
  100.     e = slen;
  101.     for (j = cursor; j < e; j++) inbuf[j] = inbuf[j+1];
  102.     slen--;
  103. }
  104.  
  105. static int  valid(ch) /*=================================================*/
  106. char    ch;           /* Check if ch is an elt of vset.                  */
  107. {
  108. char    *cp;
  109.     if (!vset) return(1);            /* A valid set of NULL is all valid */
  110.     cp = vset;
  111.     while (*cp) if (ch == *cp++) return(1);
  112.     return(0);
  113. }
  114.  
  115. static void insert(ch) /*================================================*/
  116. char    ch;            /* Insert ch into the input line.                 */
  117. {
  118. int     e,j;
  119.     if (!valid(ch)) return;
  120.     e = slen;
  121.     if (length <= e) return;
  122.     for (j = e; j >= cursor; j--) inbuf[j+1] = inbuf[j];
  123.     inbuf[cursor] = ch;
  124.     cursor++; slen++;
  125. }
  126.  
  127. /************** INPUT() **************************************************
  128.  * Funtion: input(char *,char *,char *,int,int,int,int)                  *
  129.  * Purpose: this function takes input from the window, and puts it into  *
  130.  *      a buffer area passed by the caller. The caller specifies the     *
  131.  *      input length, initial data, colors, startx position, and a valid *
  132.  *      character set.                                                   *
  133.  * Parameters: w    - window to read from.                               *
  134.  *             init - initialization string.                             *
  135.  *             data - final storage buffer (ie: input goes here).        *
  136.  *             cset - valid char set string (NULL = no restrictions).    *
  137.  *             x    - start x position.                                  *
  138.  *             y    - y position (top of character).                     *
  139.  *             l    - maximum length (in characters) of input.           *
  140.  *             f,b  - foreground,background colors.                      *
  141.  *************************************************************************/
  142. void    input(w,init,data,cset,x,y,l,f,b) /*=============================*/
  143. struct Window   *w;
  144. char    *init,*data,*cset;
  145. int     x,y,l,f,b;
  146. {
  147. struct IntuiMessage *msg;
  148. int     j,loop;
  149. char    ch;
  150.  
  151.     cursor = 0; start = x; length = l; vset = cset; slen = strlen(init);
  152.     top = y;    rport = w->RPort;
  153.     for (j = 0; j < l; j++) inbuf[j] = 0;
  154.     inputline.FrontPen = f;     inputline.BackPen = b;
  155.     strcpy(inbuf,init);
  156.     loop = 1;
  157.     while (loop) {
  158.         PrintIText(rport,&inputline,x,y);
  159.         clear(b);
  160.         drawcursor();
  161.         Wait(1<<w->UserPort->mp_SigBit);
  162.         while (msg = (struct IntuiMessage *)GetMsg(w->UserPort)) {
  163.             if (msg->Class != RAWKEY) {
  164.                 ReplyMsg((struct Message *)msg);
  165.                 continue;
  166.             }
  167.             if (msg->Qualifier & 0x08) {
  168.                 switch (msg->Code) {
  169.                     case (0x32): reset();   break;      /* CTRL X */
  170.                     case (0x27):                        /* CTRL K */
  171.                     case (0x15): delEOL();  break;      /* CTRL Y */
  172.                     case (0x16): delBOL();  break;      /* CTRL U */
  173.                 }
  174.             }
  175.             else {
  176.                 switch (msg->Code) {
  177.                     case (0x4e): forward();     break;      /* CSR R */
  178.                     case (0x4f): backward();    break;      /* CSR L */
  179.                     case (0x41): delprev();     break;      /*  <--  */
  180.                     case (0x46): delnext();     break;      /* DEL   */
  181.                     case (0x43):                            /* ENTER */
  182.                     case (0x44): loop = 0;      break;      /* RETURN */
  183.                     default: if (ch = trans(msg->Code)) insert(ch);
  184.                 }
  185.             }
  186.             ReplyMsg((struct Message *)msg);
  187.         }
  188.     }
  189.     strcpy(data,inbuf);
  190. }
  191.