home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 2.ddi / TOOLS.2 / EXAMPLES / WINKEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  8.7 KB  |  300 lines

  1. /**
  2. *    WINKEY.C    Show an application with overlapping
  3. *            windows controlled by the keyboard.
  4. *
  5. *  The purpose of WINKEY is to show a moderately complex window
  6. *  application, in the hopes that the user can gain insight into
  7. *  better ways to use the window function set.
  8. *
  9. *  This program displays instructions and a prompt on the screen
  10. *  (in a window), asking for the number of windows to make
  11. *  accessible.    If the user types ENTER, he is returned to DOS.
  12. *  If the user types a valid number of windows (one to ten), then
  13. *  he is put into a mode wherein the numeric keys will create/
  14. *  promote windows.
  15. *
  16. *  If the user types the number of a window which is not displayed
  17. *  yet, the window is displayed "on top" of any other window whose
  18. *  location coincides with it.    If the window is already displayed,
  19. *  it is "promoted" on top of any covering windows.
  20. *
  21. *  The command line format is as follows:
  22. *
  23. *    winkey
  24. *
  25. *  Version    6.00 (C)Copyright Blaise Computing Inc. 1987-1989
  26. *
  27. **/
  28.  
  29.  
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <time.h>
  34.  
  35. #include <bkeybrd.h>
  36. #include <bvideo.h>
  37. #include <bwindow.h>
  38.  
  39. #define NUL   '\0'
  40. #define ESC   27
  41.  
  42. #define TRUE  1
  43. #define FALSE 0
  44.  
  45. #define exitBad()                             \
  46.     {                                     \
  47.     printf("error %d in line %d\n", b_wnerr, __LINE__);         \
  48.     exit(b_wnerr);                            \
  49.     }
  50.  
  51. static char *descrip[] =
  52. {
  53.     "      WINKEY.C        Show an application with overlapping\n"
  54.     "                      windows controlled by the keyboard.\n\n",
  55.     "This program displays instructions and a prompt on the screen\n",
  56.     "(in a window), asking for the number of windows to make\n",
  57.     "accessible.  If the user types ENTER, he is returned to DOS.\n",
  58.     "If the user types a valid number of windows (one to ten), then\n",
  59.     "he is put into a mode wherein the numeric keys will create/\n",
  60.     "promote windows.  The 'i' will promote this instruction window.\n\n",
  61.     "If the user types the number of a window which is not displayed\n",
  62.     "yet, the window is displayed \"on top\" of any other window whose\n",
  63.     "location coincides with it.  If the window is already displayed,\n",
  64.     "it is \"promoted\" on top of any covering windows.\n\n", NIL
  65. };
  66.  
  67. void windows(int, BWINDOW *, WHERE *, BORDER *);
  68.  
  69.  
  70. void main(void)
  71. {
  72.     char s [200];
  73.     char *pdescrip;
  74.     int  act_page, columns, numwins, mode, junk, i;
  75.     int  cursor_was_off, row, col, high, low;
  76.     long now;
  77.     BWINDOW *pinstwin;
  78.     BORDER   border;
  79.     WHERE    where;
  80.  
  81.         /* Save cursor state.                    */
  82.     cursor_was_off = sccurst(&row, &col, &high, &low);
  83.  
  84.         /* Create instruction and query window.         */
  85.     if ((pinstwin = wncreate(17, 65,
  86.                  utnybbyt(SC_MAGENTA,
  87.                       NORMAL | INTENSITY))) == NIL)
  88.     exitBad()
  89.  
  90.     wnselect(pinstwin);
  91.  
  92.     where.dev         = scmode(&mode, &columns, &act_page);
  93.     where.page         = act_page;
  94.     where.corner.row = 2;
  95.     where.corner.col = 5;
  96.  
  97.     border.type     = BBRD_SDDD | BBRD_TCT;
  98.     border.attr     = utnybbyt(SC_BLACK, NORMAL);
  99.     border.ch        = NUL;
  100.     border.pttitle  = "Instructions";
  101.     border.ttattr   = utnybbyt(SC_BLUE, NORMAL | INTENSITY);
  102.  
  103.     for (pdescrip = descrip[0], i=0;
  104.      pdescrip != NIL;
  105.      i++, pdescrip = descrip[i])
  106.     wnprintf(pdescrip);
  107.  
  108.     if (NIL == wndsplay(pinstwin, &where, &border))
  109.     exitBad()
  110.  
  111.         /* Get the number of windows desired from user.     */
  112.     wnprintf("\nHow many windows (or ENTER to quit)? ");
  113.     wnquery(s, 3, &junk);
  114.  
  115.         /* Blank out the line the response was entered on.  */
  116.     wnscrblk(pinstwin, pinstwin->cur_loc.row, 0,
  117.           pinstwin->cur_loc.row, 64, -1, -1, 0, 0, WN_UPDATE);
  118.     wncurmov(pinstwin->cur_loc.row, 0);
  119.  
  120.         /* If the user did not type just ENTER, proceed.    */
  121.     if (s[0] != NUL)
  122.     {
  123.         /* Make sure number of windows requested is        */
  124.         /* reasonable.                        */
  125.     sscanf(s, "%d", &numwins);
  126.     if (numwins < 1 || numwins > 10)
  127.     {
  128.         wnprintf("Number of windows must be [1..10] -- {hit any key}");
  129.         kbgetkey(&junk);
  130.         wnremove(pinstwin);
  131.     }
  132.     else
  133.     {
  134.         /* Seed the random number generator.            */
  135.         srand((int) (time(&now) % 37L));
  136.  
  137.         /* Turn the cursor off in the "instructions" window.*/
  138.         wnsetopt(pinstwin, WN_CUR_OFF, 1);
  139.         wncursor(pinstwin);
  140.  
  141.         /* Change the default text color so we can WNPRINTF */
  142.         /* an instruction bar in a different color.  Then   */
  143.         /* actually write out the instruction bar.        */
  144.         wnsetopt(pinstwin, WN_ATTR,
  145.              utnybbyt(SC_RED, NORMAL | INTENSITY));
  146.         wnscrblk(pinstwin, pinstwin->cur_loc.row, 0,
  147.              pinstwin->cur_loc.row, 64, -1, -1, 0, 0,
  148.              WN_UPDATE);
  149.         wnprintf(
  150.       "Hit 0-%d to pop up window #n, 'I' for this window, ESC to quit",
  151.               numwins - 1);
  152.  
  153.         /* Go do windows manipulations.             */
  154.         windows(numwins, pinstwin, &where, &border);
  155.     }
  156.    }
  157.  
  158.         /* Undisplay instruction window.            */
  159.     wnremove(pinstwin);
  160.  
  161.         /* Destroy instructions window, put cursor back     */
  162.         /* where it was when we started the program.        */
  163.     wndstroy(pinstwin);
  164.  
  165.     sccurset(row, col);
  166.     scpgcur(cursor_was_off, high, low, CUR_NO_ADJUST);
  167. }
  168.  
  169.  
  170.  
  171. void windows(numwins, pinstwin, pwhere, pborder)
  172. int numwins;
  173. BWINDOW *pinstwin;
  174. WHERE    *pwhere;
  175. BORDER    *pborder;
  176. {
  177.     char     title[10][20];
  178.     int      done = FALSE, i;
  179.     int      mode, columns, act_page;
  180.     int      ch, scan;
  181.     int      back, fore, rnum, wnum;
  182.     BORDER   border;
  183.     BWINDOW *pwins [10];
  184.     WHERE    where;
  185.  
  186.         /* Create all windows, making sure for each one     */
  187.         /* that its "random" default attribute is not 0.    */
  188.     for (i = 0;  i < numwins;  i++)
  189.     {
  190.     while ((fore = rand()) == 0)
  191.         ;
  192.     fore = (fore % 15) + 1;
  193.  
  194.     while (((back = rand()) != 0) &&
  195.            ((back = ((back % 7) + 1)) == fore))
  196.         ;
  197.  
  198.     if ((pwins[i] = wncreate(7, 20, utnybbyt(back, fore))) == NIL)
  199.     {
  200.         printf("error %d in wncreate\n", b_wnerr);
  201.         exit(b_wnerr);
  202.     }
  203.     }
  204.  
  205.         /* Save device and page information.            */
  206.     where.dev  = scmode(&mode, &columns, &act_page);
  207.     where.page = act_page;
  208.  
  209.         /* Get keys and do window operations until the user */
  210.         /* types ESC.                        */
  211.     do
  212.     {
  213.     ch   = kbgetkey(&scan);
  214.     wnum = ch - '0';
  215.     done = (ch == ESC);
  216.  
  217.         /* Beep if user typed an invalid key.            */
  218.     if ((wnum < 0 || wnum >= numwins) && !done)
  219.     {
  220.         if ((ch != 'i') && (ch != 'I'))
  221.         scttywrt('\a', 0);
  222.  
  223.         else    /* Promote the "instructions" window.       */
  224.         if (pinstwin->internals.any_data_covered)
  225.         {
  226.             wnremove(pinstwin);
  227.             wndsplay(pinstwin, pwhere, pborder);
  228.         }
  229.     }
  230.     else    /* Display / promote numbered window.            */
  231.     {
  232.         /* If the window is displayed, but not "on top",    */
  233.         /* remove it from display, and save its coordinates */
  234.         /* so we can put it back in the same place when we  */
  235.         /* promote it.                        */
  236.  
  237.         /* If the window is displayed, and "on top", we can */
  238.         /* ignore the display/promotion aspects, and go     */
  239.         /* directly to the WNPRINTF call.            */
  240.  
  241.         if ((pwins[wnum]->where_shown.dev == SC_COLOR) ||
  242.         (pwins[wnum]->where_shown.dev == SC_MONO))
  243.         {
  244.         if (pwins[wnum]->internals.any_data_covered)
  245.         {
  246.             where.corner.col = pwins[wnum]->where_shown.corner.col;
  247.             where.corner.row = pwins[wnum]->where_shown.corner.row;
  248.             wnremove(pwins[wnum]);
  249.         }
  250.         }
  251.         else
  252.         {
  253.         /* If the window is not displayed, come up with a   */
  254.         /* random location for it.                */
  255.  
  256.         where.corner.col = (((rnum = rand()) == 0)
  257.                    ? 0 : (rnum % 69)) + 1;
  258.         where.corner.row = (((rnum = rand()) == 0)
  259.                    ? 0 : (rnum % 17)) + 1;
  260.         }
  261.  
  262.         /* Twiddle with the window's attribute bits to      */
  263.         /* create border and title attributes that are        */
  264.         /* different from each other and from the default   */
  265.         /* window attribute.                    */
  266.  
  267.         border.attr   = ((pwins[wnum]->attr ^ 0x77) & 0x7e);
  268.         border.ttattr = ((pwins[wnum]->attr ^ 0xee) & 0x7f);
  269.  
  270.         /* Construct the border with a top centered title,  */
  271.         /* and with single ruled line sides.            */
  272.  
  273.         border.type   = BBRD_SSSS | BBRD_TCT;
  274.  
  275.         /* Create the title.                    */
  276.  
  277.         sprintf(title[wnum], " window #%d ", wnum);
  278.         border.pttitle = title[wnum];
  279.  
  280.         /* Display/promote the window, and WNPRINTF some    */
  281.         /* text into it.                    */
  282.  
  283.         wndsplay(pwins[wnum], &where, &border);
  284.         wnselect(pwins[wnum]);
  285.         wncursor(pwins[wnum]);
  286.         wnprintf("This string will show up in win %d; ", wnum);
  287.     }
  288.     } while (!done);
  289.  
  290.         /* Destroy all windows.                 */
  291.     for (i = 0;  i < numwins;  i++)
  292.     {
  293.     if ((pwins[i]->where_shown.dev == SC_COLOR) ||
  294.         (pwins[i]->where_shown.dev == SC_MONO))
  295.         wnremove(pwins[i]);
  296.  
  297.     wndstroy(pwins[i]);
  298.     }
  299. }
  300.