home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / WNQUERY.C < prev    next >
Encoding:
Text File  |  1987-04-13  |  7.7 KB  |  240 lines

  1. /**
  2. *
  3. * Name        wnquery -- Return a string from the operator.
  4. *
  5. * Synopsis    final = wnquery(presponse,resp_size,pscan);
  6. *
  7. *        char final    Character that terminated input, or
  8. *                NUL ('\0') if an extended key
  9. *                sequence terminated the input.
  10. *        char *presponse Pointer to character buffer in which
  11. *                to put operator's input.
  12. *        int  resp_size    Size of input buffer in bytes (including
  13. *                one byte for the trailing NUL ('\0')).
  14. *        int  *pscan    If the terminal character was an ASCII
  15. *                character, this is its value;
  16. *                if it was an extended key
  17. *                sequence, this is its scan code.
  18. *
  19. * Description    This function returns a string of keystrokes from the
  20. *        user, echoing keystrokes and confining all I/O to the
  21. *        current window.
  22. *
  23. *        Previous pending output to the window is completed and
  24. *        all keystrokes are echoed to the window even though the
  25. *        window may be designated "delayed".  If the window was
  26. *        "delayed" before WNQUERY is called, this status is
  27. *        restored on exit.
  28. *
  29. *        The user may type characters as usual, terminating the
  30. *        input by one of the following:    ENTER, CTRL-M, CTRL-J,
  31. *        or a function key or other extended key sequence.  The
  32. *        characters are echoed and the cursor moves across the
  33. *        window.  When the rightmost column of the window is
  34. *        reached, the cursor moves to the next row, except that
  35. *        input stops when the lower right corner of the window is
  36. *        reached.  The input also stops when the buffer is full.
  37. *
  38. *        (The cursor is only visible if the current window is the
  39. *        one (among all the windows on this display page) that is
  40. *        selected to have an active cursor (via WNCURSOR) and if
  41. *        this window's own cursor is on.)
  42. *
  43. *        Once the input has stopped (because the end of the
  44. *        window is reached or the buffer is full), the cursor
  45. *        remains just after the last response character.  Further
  46. *        keystrokes (except for backspace and terminating keys)
  47. *        cause beeps and are discarded.    Backspace works as
  48. *        usual.    The function does not return until ENTER or
  49. *        another terminating key has been pressed (even if the
  50. *        input has stopped).
  51. *
  52. *        The terminating character is not returned as part of the
  53. *        input.    If the terminating character is an ASCII
  54. *        character, its value is returned in *pscan and as the
  55. *        value of the function; if it was an extended key
  56. *        sequence, its scan code is returned in *pscan and the
  57. *        value of the function is zero.
  58. *
  59. *        An error occurs if no window is designated current, if
  60. *        any portion of the current window's data area is covered
  61. *        by another window, or if the current window is not
  62. *        currently displayed.  In event of error, the function
  63. *        returns immediately without displaying the message, 0 is
  64. *        returned in *pscan and as the value of the function, and
  65. *        *presponse is made the null string ("").
  66. *
  67. * Special    The following keystrokes have special effects when
  68. * characters    entered as part of the response:
  69. *
  70. *        Backspace or CTRL-H ('\b') deletes the previous
  71. *        character entered and moves the cursor to that
  72. *        character's location.  A backspace at the beginning of
  73. *        the response causes a beep.
  74. *
  75. *        NULs (ASCII 0) are ignored and discarded.
  76. *
  77. *        CTRL-G causes a beep but is not returned as part of the
  78. *        response.
  79. *
  80. *        ENTER or CTRL-M ('\15') terminates the response.
  81. *
  82. *        CTRL-J ('\12') terminates the response.
  83. *
  84. *        Special non-ASCII IBM keys (such as function keys and
  85. *        some ALT keys) terminate the response.
  86. *
  87. *        All other ASCII characters, including control
  88. *        characters, are echoed.  (Control characters are echoed
  89. *        as themselves, that is, without an uparrow ('^')
  90. *        prefix.)
  91. *
  92. * Returns    final        Character that terminated input, or
  93. *                NUL ('\0') if an extended key
  94. *                sequence terminated the input.
  95. *        *pscan        ASCII code or extended code of
  96. *                keystroke that terminated input.
  97. *        *presponse    User's response as a string.
  98. *        b_wnerr     Possible values:
  99. *                (No change)    Success.
  100. *                WN_BAD_WIN    No window designated
  101. *                        as current.
  102. *                WN_NOT_SHOWN    Window not displayed.
  103. *                WN_COVERED    Data area covered.
  104. *                WN_ILL_DIM    Internal error.
  105. *                WN_BAD_DEV    Internal error.
  106. *                WN_NULL_PTR    Internal error.
  107. *
  108. * Version    3.0  (C)Copyright Blaise Computing Inc. 1986
  109. *
  110. * Version    3.02 March 24, 1987
  111. *        Forced WNUPDATE in every case (previously, we would call
  112. *            WNUPDATE only if the "delayed" flag was set; but
  113. *            that would be inproper if the flag was cleared while
  114. *            the window was dirty).
  115. *
  116. **/
  117.  
  118. #include <string.h>
  119.  
  120. #include <bkeybd.h>
  121. #include <bwindow.h>
  122.  
  123. #define  BEL   ((int) '\7' )          /* Bell,      CTRL-G            */
  124. #define  BS    ((int) '\b' )          /* Backspace, CTRL-H            */
  125. #define  LF    ((int) '\12')          /* Linefeed,  CTRL-J            */
  126. #define  ENTER ((int) '\15')          /* ENTER,     CTRL-M            */
  127.  
  128. char wnquery(presponse,resp_size,pscan)
  129. char *presponse;
  130. int resp_size,*pscan;
  131. {
  132.     int  was_delayed;
  133.     char ch;
  134.     int  is_char;
  135.     int  max_row,max_col,row,col;
  136.     int  num_have;
  137.     static char blank = ' ';
  138.  
  139.     *presponse = '\0';                /* Zeros in case of error.      */
  140.     *pscan     = 0;
  141.  
  142.     if (wnvalwin(b_pcurwin) == NIL)   /* Validate window.          */
  143.     {
  144.     wnerror(WN_BAD_WIN);
  145.     return '\0';
  146.     }
  147.                       /* Make sure it's visible.      */
  148.     if (   b_pcurwin->where_shown.dev != MONO
  149.     && b_pcurwin->where_shown.dev != COLOR)
  150.     {
  151.     wnerror(WN_NOT_SHOWN);
  152.     return '\0';
  153.     }
  154.                       /* Make sure we can write to it.*/
  155.     if (b_pcurwin->internals.frozen)
  156.     {
  157.     wnerror(WN_COVERED);
  158.     return '\0';
  159.     }
  160.  
  161.     was_delayed = b_pcurwin->options.delayed;
  162.  
  163.     if (wnupdate(b_pcurwin) == NIL)        /* Complete pending writes*/
  164.     return '\0';
  165.     b_pcurwin->options.delayed = 0;        /* Allow immediate I/O.   */
  166.  
  167.     max_row  = b_pcurwin->img.dim.h - 1;
  168.     max_col  = b_pcurwin->img.dim.w - 1;
  169.     num_have = 0;
  170.  
  171.     do                      /* Collect the response.          */
  172.     {
  173.                       /* Await a keystroke.          */
  174.     if (is_char = kbin(pscan))
  175.     {                  /* Got an ASCII character.      */
  176.         wncurpos(&row,&col);
  177.  
  178.         switch (ch = (char) *pscan)
  179.         {
  180.         case LF:
  181.         case ENTER:
  182.             break;          /* This is a terminating          */
  183.                       /* character:  handle it at     */
  184.                       /* loop exit.              */
  185.  
  186.         case '\0':            /* Ignore NULs.                 */
  187.             break;
  188.  
  189.         case BEL:          /* BEL:  just beep.          */
  190.             wnwrtty((char) BEL,-1,-1);
  191.             break;
  192.  
  193.         default:
  194.             if (    num_have >= resp_size - 1  /* Buffer full */
  195.             || (   row >= max_row
  196.                 && col >= max_col))      /* End of window */
  197.             {
  198.             wnwrtty((char) BEL,-1,-1);
  199.             }
  200.             else
  201.             {              /* Echo char & advance cursor.  */
  202.             wnwrtty(ch,-1,-1);
  203.                       /* Update response buffer.      */
  204.             num_have++;
  205.             *presponse++ = ch;
  206.             }
  207.             break;
  208.  
  209.         case BS:
  210.             if (num_have)
  211.             {              /* Move cursor to last char     */
  212.                       /* entered.              */
  213.  
  214.             if (col)      /* Back up one column.          */
  215.                 wncurmov(row,--col);
  216.             else          /* Back up to previous row.     */
  217.                 wncurmov(--row,col = max_col);
  218.  
  219.                       /* Erase previous character.    */
  220.             wnwrbuf(row,col,1,&blank,-1,-1,
  221.                 CUR_BEG | CHARS_ONLY | MOVE_CUR);
  222.             num_have--;
  223.             presponse--;
  224.             }
  225.             else
  226.             wnwrtty((char) BEL,-1,-1); /* Can't delete    */
  227.                            /* past beginning. */
  228.             break;
  229.         }
  230.     }
  231.     } while (is_char && (ch != (char) ENTER) && (ch != (char) LF));
  232.  
  233.     *presponse = '\0';                /* Terminate response string.   */
  234.  
  235.                       /* Restore "delayed" state.     */
  236.     b_pcurwin->options.delayed = was_delayed;
  237.  
  238.     return is_char ? ch : (char) '\0';/* Successful exit.             */
  239. }
  240.