home *** CD-ROM | disk | FTP | other *** search
- /* loop.c -- Tutorial on using keyboard loop functions.
-
- ************* Copyright 1985 by Vermont Creative Software **************
-
- DATE: January 20, 1986
-
- INTRODUCTION
-
- This program shows the steps required to install and use the keyboard loop
- feature provided in the Window library.
-
- To keep the program simple, we will use only 2 windows. In the main
- window we will display some text and any keystrokes typed by the user.
- In the status window we will display a running count of how many times
- the loop function has been executed.
-
- */
-
- #define WN_DEBUG
- #include <wfc.h>
- #include <wfc_glob.h>
-
-
- WINDOW stat_wn; /*declared outside main so that the */
- /*loop function can have access to */
- /*this variable */
-
- char *message[] =
- {
- "This program demonstrates how to install and use keyboard loop functions. ",
- "Pressing any key will cause the character to be echoed to the main window. ",
- "If no keystrokes are waiting in the keyboard buffer, the loop function is executed. ",
- "The loop function simply prints in the status window the number of times it has been ",
- "executed.\n\nTo leave the program, press the ESC key.",
- NULLP
- };
-
- char *title = "Tutorial on Installing and Using Keyboard Loop Functions";
-
- main()
- {
- WINDOW main_wn;
- int ch;
- register int i;
- int count(); /*this is the loop function */
- void prt_ch(); /*this is a primitive editor function */
-
- init_wfc(); /*initialize the WFC system */
-
- def_wn(&main_wn, 0, 20, 0, 79, 1, 1, BDR_LNP);
- def_wn(&stat_wn, 21, 21, 0, 79, 0, 0, BDR_0P);
-
- se_keyloop(count); /*install the keyboard loop function */
-
- set_wn(&main_wn);
- set_wn(&stat_wn);
-
- v_plst(1, CENTER_TEXT, title, &main_wn); /*display title */
-
- mv_cs(3, 0, &main_wn);
- i = 0;
- while(message[i] != NULLP) /*display info messages in main_wn */
- v_st(message[i++], &main_wn);
-
- mv_csr(11, 0, &main_wn);
- while((ch = ki()) != K_ESC) /*read keyboard */
- prt_ch(ch, &main_wn); /*print character in window */
-
- mv_csr(v_rwq - 2, 0, &wn0); /*move cursor to bottom */
- exit_wfc();
- return(0);
- }
-
- /*----------------------------------------------------------------------------*/
- /* This is the keyboard loop function that will be executed whenever there */
- /* are no keystrokes waiting in the keyboard buffer. */
- /*----------------------------------------------------------------------------*/
- int count()
- {
- static unsigned int x;
-
- mv_cs(0, 60, &stat_wn);
- v_printf(&stat_wn, "%d", x++);
- return(0);
- }
-
- /*----------------------------------------------------------------------------*/
- /* This is a very primitive editing function that will display a typed */
- /* character in a window if it is printable; otherwise an editing function */
- /* is performed. */
- /*----------------------------------------------------------------------------*/
- void prt_ch(ch, wnp)
- int ch;
- WINDOWPTR wnp;
- {
- int last_row;
- int last_col;
-
- last_row = row_qty(wnp) - 1;
- last_col = col_qty(wnp) - 1;
-
- switch(ch)
- {
- case K_ENTER: /*user pressed <ENTER> */
- wnp->r++;
- if(wnp->r > last_row)
- wnp->r = last_row;
- else
- wnp->c = 0;
- pl_csr(wnp);
- break;
-
- case K_BACK: /*handle backspace */
- if(wnp->c > 0) /*if not at left boundary */
- {
- wnp->c--; /*go back one position */
- v_ch(' ', wnp); /*print "space" */
- wnp->c--; /*go back one position */
- pl_csr(wnp); /*place screen cursor */
- }
- else /*can't backspace beyond left boundary*/
- bell();
- break;
-
- case -K_LEFT: /*handle left cursor */
- if(wnp->c > 0) /*if not at left boundary */
- {
- wnp->c--; /*move cursor 1 column left */
- pl_csr(wnp); /*place screen cursor */
- }
- else /*can't move cursor left */
- bell();
- break;
-
- case -K_RIGHT: /*handle right cursor */
- if(wnp->c < last_col) /*if not at right boundary */
- {
- wnp->c++; /*move cursor 1 column right */
- pl_csr(wnp); /*place screen cursor */
- }
- else /*can't move cursor right */
- bell();
- break;
-
- case -K_UP: /*handle up cursor */
- if(wnp->r > 0) /*if not at top boundary */
- {
- wnp->r--; /*move cursor up one line */
- pl_csr(wnp);
- }
- else /*can't move cursor up */
- bell();
- break;
-
- case -K_DN: /*handle down cursor */
- if(wnp->r < last_row) /*if not at bottom boundary */
- {
- wnp->r++; /*move cursor down one line */
- pl_csr(wnp);
- }
- else /*can't move cursor down */
- bell();
- break;
-
- default: /*handle all other possibilities */
- if(ch > 0) /*check for printable character */
- {
- v_ch(ch, wnp); /*else write character and continue */
- if(wnp->r > last_row)
- {
- wnp->r = last_row;
- wnp->c = last_col;
- bell();
- }
- pl_csr(wnp);
- }
- else /* extended code */
- bell(); /* let user know it's illegal */
- break;
- }
- return;
- }