home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a012 / 1.ddi / CHAP13.EXE / CHP1336.PRG < prev    next >
Encoding:
Text File  |  1991-06-12  |  3.7 KB  |  132 lines

  1. /*
  2.    Listing 13.36 YN(), a user interface building block function. Calls
  3.                  should be made through the YESNO user-defined command
  4.                  found in YESNO.CH.
  5.    NOTE: makes external calls to:
  6.  
  7.          ColorInit() and ColorSet(), which are contained in the
  8.          file CHP1313.PRG
  9.  
  10.          SaveEnv() and RestEnv(), which are contained in the file
  11.          CHP1201.PRG (chapter 12)
  12.  
  13.    Author: Craig Yellick
  14.    Excerpted from "Clipper 5: A Developer's Guide"
  15.    Copyright (c) 1991 M&T Books
  16.                       501 Galveston Drive
  17.                       Redwood City, CA 94063-4728
  18.                       (415) 366-3600
  19. */
  20.  
  21. //───── NOTE: must compile with the /N option!
  22.  
  23. //  Required for using the YESNO command.
  24. #include "yesno.ch"
  25.  
  26. //  Required for using the ColorSet() function.
  27. #include "colors.ch"
  28.  
  29. //  Other handy programming things.
  30. #include "inkey.ch"
  31. #include "box.ch"
  32. #command DEFAULT <a> to <b> => <a> := if(<a> = nil, <b>, <a>)
  33.  
  34.  
  35. /*
  36.    This #define will cause the testing/demo routine to be included in
  37.    the compile. Delete the line and recompile to get rid of the test
  38.    code and produce a smaller OBJ file.  However-- leave the source
  39.    code here so you can more easily test changes and additions in the
  40.    future.
  41. */
  42. #define TESTING
  43. #ifdef TESTING
  44.   function YNTest()
  45.   local answer
  46.  
  47.     //  Must be called prior to using ColorSet().
  48.     ColorInit()
  49.  
  50.     //  Clean up screen so this demo looks nice.
  51.     setcursor(0)
  52.     cls
  53.  
  54.     yesno "Is this a simple YESNO example?" into answer
  55.  
  56.     yesno "You said " +if(answer, "YES", "NO") +"?" ;
  57.            at 8,20 into answer
  58.  
  59.     yesno "Do you like this color?" ;
  60.            color C_WARNING into answer at 16,40
  61.  
  62.     setcursor(1)
  63.     cls
  64.   return nil
  65.  
  66. #endif       // end stub test program
  67.  
  68.  
  69.  
  70. /*------------------------------------------------------------------*/
  71.  
  72. function YesNoMenu(r, c, msg, clr)
  73. /*
  74.    General-purpose Yes/No prompt function.
  75.  
  76.    Parameter  Description
  77.    ---------  -----------
  78.    r          Starting row, default is center of screen.
  79.    c          Starting column, default is center of screen.
  80.    msg        Message to display as a prompt.
  81.    clr        Color number to use, default is current color.
  82.  
  83.    Note: Color selection for this function is handled by
  84.    the ColorSet() function.
  85. */
  86.  
  87. local response                 //  User's response
  88. local r2, c2, offset           //  Help with box coordinates
  89. local oldScr, oldCur, oldClr   //  Original screen, cursor, color.
  90. local oldR, oldC               //  Original row and column
  91. local pYes, pNo                //  Prompts for "Yes" and "No"
  92.  
  93.   //  Make sure all parameters have reasonable default values.
  94.   default r to (maxrow() / 2) -2
  95.   default c to (maxcol() - len(msg)) / 2
  96.   default clr to C_NORMAL
  97.  
  98.   //  Prompts to display for choices.
  99.   pYes := "YES"
  100.   pNo  := "NO"
  101.  
  102.   /*
  103.      Calculate ending box coordinates.  Box width must be able to
  104.      accommodate the longest of either the prompt message or the
  105.      YES and NO selections.
  106.   */
  107.   r2 := r +3
  108.   c2 := c +len(msg) +1
  109.   c2 := max(c2, c +len(pYes) +2 +len(pNo))
  110.  
  111.   //  Calculate how far to indent Yes/No prompts within the box.
  112.   offset := ((c2 -c) -(len(pYes) +2 +len(pNo))) /2
  113.  
  114.   //  Save existing screen, color etc.
  115.   SaveEnv()
  116.  
  117.   //  Clear the area and draw a box
  118.   dispbox(r, c, r2, c2, B_SINGLE + ' ')
  119.  
  120.   //  Display message and Yes/No prompts, wait for response.
  121.   @ r +1, c +1 say msg
  122.   @ r +2, c +1 +offset prompt pYes
  123.   @ r +2, col() +2  prompt pNo
  124.   menu to response
  125.  
  126.   //  Restore original screen, color etc before returning.
  127.   RestEnv()
  128.  
  129. return (response == 1)
  130.  
  131. // end of file CHP1336.PRG
  132.