home *** CD-ROM | disk | FTP | other *** search
- /**
- * WINKEY.C Show an application with overlapping
- * windows controlled by the keyboard.
- *
- * The purpose of WINKEY is to show a moderately complex window
- * application, in the hopes that the user can gain insight into
- * better ways to use the window function set.
- *
- * This program displays instructions and a prompt on the screen
- * (in a window), asking for the number of windows to make
- * accessible. If the user types ENTER, he is returned to DOS.
- * If the user types a valid number of windows (one to ten), then
- * he is put into a mode wherein the numeric keys will create/
- * promote windows.
- *
- * If the user types the number of a window which is not displayed
- * yet, the window is displayed "on top" of any other window whose
- * location coincides with it. If the window is already displayed,
- * it is "promoted" on top of any covering windows.
- *
- * The command line format is as follows:
- *
- * winkey
- *
- * Version 6.00 (C)Copyright Blaise Computing Inc. 1987-1989
- *
- **/
-
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- #include <bkeybrd.h>
- #include <bvideo.h>
- #include <bwindow.h>
-
- #define NUL '\0'
- #define ESC 27
-
- #define TRUE 1
- #define FALSE 0
-
- #define exitBad() \
- { \
- printf("error %d in line %d\n", b_wnerr, __LINE__); \
- exit(b_wnerr); \
- }
-
- static char *descrip[] =
- {
- " WINKEY.C Show an application with overlapping\n"
- " windows controlled by the keyboard.\n\n",
- "This program displays instructions and a prompt on the screen\n",
- "(in a window), asking for the number of windows to make\n",
- "accessible. If the user types ENTER, he is returned to DOS.\n",
- "If the user types a valid number of windows (one to ten), then\n",
- "he is put into a mode wherein the numeric keys will create/\n",
- "promote windows. The 'i' will promote this instruction window.\n\n",
- "If the user types the number of a window which is not displayed\n",
- "yet, the window is displayed \"on top\" of any other window whose\n",
- "location coincides with it. If the window is already displayed,\n",
- "it is \"promoted\" on top of any covering windows.\n\n", NIL
- };
-
- void windows(int, BWINDOW *, WHERE *, BORDER *);
-
-
- void main(void)
- {
- char s [200];
- char *pdescrip;
- int act_page, columns, numwins, mode, junk, i;
- int cursor_was_off, row, col, high, low;
- long now;
- BWINDOW *pinstwin;
- BORDER border;
- WHERE where;
-
- /* Save cursor state. */
- cursor_was_off = sccurst(&row, &col, &high, &low);
-
- /* Create instruction and query window. */
- if ((pinstwin = wncreate(17, 65,
- utnybbyt(SC_MAGENTA,
- NORMAL | INTENSITY))) == NIL)
- exitBad()
-
- wnselect(pinstwin);
-
- where.dev = scmode(&mode, &columns, &act_page);
- where.page = act_page;
- where.corner.row = 2;
- where.corner.col = 5;
-
- border.type = BBRD_SDDD | BBRD_TCT;
- border.attr = utnybbyt(SC_BLACK, NORMAL);
- border.ch = NUL;
- border.pttitle = "Instructions";
- border.ttattr = utnybbyt(SC_BLUE, NORMAL | INTENSITY);
-
- for (pdescrip = descrip[0], i=0;
- pdescrip != NIL;
- i++, pdescrip = descrip[i])
- wnprintf(pdescrip);
-
- if (NIL == wndsplay(pinstwin, &where, &border))
- exitBad()
-
- /* Get the number of windows desired from user. */
- wnprintf("\nHow many windows (or ENTER to quit)? ");
- wnquery(s, 3, &junk);
-
- /* Blank out the line the response was entered on. */
- wnscrblk(pinstwin, pinstwin->cur_loc.row, 0,
- pinstwin->cur_loc.row, 64, -1, -1, 0, 0, WN_UPDATE);
- wncurmov(pinstwin->cur_loc.row, 0);
-
- /* If the user did not type just ENTER, proceed. */
- if (s[0] != NUL)
- {
- /* Make sure number of windows requested is */
- /* reasonable. */
- sscanf(s, "%d", &numwins);
- if (numwins < 1 || numwins > 10)
- {
- wnprintf("Number of windows must be [1..10] -- {hit any key}");
- kbgetkey(&junk);
- wnremove(pinstwin);
- }
- else
- {
- /* Seed the random number generator. */
- srand((int) (time(&now) % 37L));
-
- /* Turn the cursor off in the "instructions" window.*/
- wnsetopt(pinstwin, WN_CUR_OFF, 1);
- wncursor(pinstwin);
-
- /* Change the default text color so we can WNPRINTF */
- /* an instruction bar in a different color. Then */
- /* actually write out the instruction bar. */
- wnsetopt(pinstwin, WN_ATTR,
- utnybbyt(SC_RED, NORMAL | INTENSITY));
- wnscrblk(pinstwin, pinstwin->cur_loc.row, 0,
- pinstwin->cur_loc.row, 64, -1, -1, 0, 0,
- WN_UPDATE);
- wnprintf(
- "Hit 0-%d to pop up window #n, 'I' for this window, ESC to quit",
- numwins - 1);
-
- /* Go do windows manipulations. */
- windows(numwins, pinstwin, &where, &border);
- }
- }
-
- /* Undisplay instruction window. */
- wnremove(pinstwin);
-
- /* Destroy instructions window, put cursor back */
- /* where it was when we started the program. */
- wndstroy(pinstwin);
-
- sccurset(row, col);
- scpgcur(cursor_was_off, high, low, CUR_NO_ADJUST);
- }
-
-
-
- void windows(numwins, pinstwin, pwhere, pborder)
- int numwins;
- BWINDOW *pinstwin;
- WHERE *pwhere;
- BORDER *pborder;
- {
- char title[10][20];
- int done = FALSE, i;
- int mode, columns, act_page;
- int ch, scan;
- int back, fore, rnum, wnum;
- BORDER border;
- BWINDOW *pwins [10];
- WHERE where;
-
- /* Create all windows, making sure for each one */
- /* that its "random" default attribute is not 0. */
- for (i = 0; i < numwins; i++)
- {
- while ((fore = rand()) == 0)
- ;
- fore = (fore % 15) + 1;
-
- while (((back = rand()) != 0) &&
- ((back = ((back % 7) + 1)) == fore))
- ;
-
- if ((pwins[i] = wncreate(7, 20, utnybbyt(back, fore))) == NIL)
- {
- printf("error %d in wncreate\n", b_wnerr);
- exit(b_wnerr);
- }
- }
-
- /* Save device and page information. */
- where.dev = scmode(&mode, &columns, &act_page);
- where.page = act_page;
-
- /* Get keys and do window operations until the user */
- /* types ESC. */
- do
- {
- ch = kbgetkey(&scan);
- wnum = ch - '0';
- done = (ch == ESC);
-
- /* Beep if user typed an invalid key. */
- if ((wnum < 0 || wnum >= numwins) && !done)
- {
- if ((ch != 'i') && (ch != 'I'))
- scttywrt('\a', 0);
-
- else /* Promote the "instructions" window. */
- if (pinstwin->internals.any_data_covered)
- {
- wnremove(pinstwin);
- wndsplay(pinstwin, pwhere, pborder);
- }
- }
- else /* Display / promote numbered window. */
- {
- /* If the window is displayed, but not "on top", */
- /* remove it from display, and save its coordinates */
- /* so we can put it back in the same place when we */
- /* promote it. */
-
- /* If the window is displayed, and "on top", we can */
- /* ignore the display/promotion aspects, and go */
- /* directly to the WNPRINTF call. */
-
- if ((pwins[wnum]->where_shown.dev == SC_COLOR) ||
- (pwins[wnum]->where_shown.dev == SC_MONO))
- {
- if (pwins[wnum]->internals.any_data_covered)
- {
- where.corner.col = pwins[wnum]->where_shown.corner.col;
- where.corner.row = pwins[wnum]->where_shown.corner.row;
- wnremove(pwins[wnum]);
- }
- }
- else
- {
- /* If the window is not displayed, come up with a */
- /* random location for it. */
-
- where.corner.col = (((rnum = rand()) == 0)
- ? 0 : (rnum % 69)) + 1;
- where.corner.row = (((rnum = rand()) == 0)
- ? 0 : (rnum % 17)) + 1;
- }
-
- /* Twiddle with the window's attribute bits to */
- /* create border and title attributes that are */
- /* different from each other and from the default */
- /* window attribute. */
-
- border.attr = ((pwins[wnum]->attr ^ 0x77) & 0x7e);
- border.ttattr = ((pwins[wnum]->attr ^ 0xee) & 0x7f);
-
- /* Construct the border with a top centered title, */
- /* and with single ruled line sides. */
-
- border.type = BBRD_SSSS | BBRD_TCT;
-
- /* Create the title. */
-
- sprintf(title[wnum], " window #%d ", wnum);
- border.pttitle = title[wnum];
-
- /* Display/promote the window, and WNPRINTF some */
- /* text into it. */
-
- wndsplay(pwins[wnum], &where, &border);
- wnselect(pwins[wnum]);
- wncursor(pwins[wnum]);
- wnprintf("This string will show up in win %d; ", wnum);
- }
- } while (!done);
-
- /* Destroy all windows. */
- for (i = 0; i < numwins; i++)
- {
- if ((pwins[i]->where_shown.dev == SC_COLOR) ||
- (pwins[i]->where_shown.dev == SC_MONO))
- wnremove(pwins[i]);
-
- wndstroy(pwins[i]);
- }
- }