home *** CD-ROM | disk | FTP | other *** search
- /* dem_grph.c -- a demonstration of graphing capabilities
-
- ************* Copyright 1985 by Vermont Creative Software **************
-
- FUNCTION
-
- Calls graphing functions to draw vertical and horizontal bar
- graphs.
-
- COMMENT
-
- The bar graphing routines included in Windows for C can be used
- for simple graphing with the monochrome adapter, which does not
- permit true graphics.
-
- The graphing routines are interesting as an example of the
- versatility that windows can provide in screen display. The
- axes and bars that make up the graph are all individual windows.
- Because the structure facility of C makes it easy to change
- window size, location, and attributes, relatively little code is
- needed to specify a graph.
-
- */
-
- /* #define WN_DEBUG Commented out when debugging completed */
- #include <wfc.h>
- #include <wfc_glob.h>
-
- #define MAXLINES 500 /*maximum number of lines in the file */
- #define MAXCOL 80 /*maximum number of columns in file */
-
- #define WINDOW_Q 3 /*Number of windows */
-
- #define KEYMAX 5 /*max number of keystrokes to process */
- /*in one call to k_vcom */
-
- /*----------------------------------------------------------------------------*/
- /* Declare structures externally to permit initialization */
- /*----------------------------------------------------------------------------*/
- KEYR keyrec = {0, 0, KEYMAX}; /*see wfc_stru.h for definition */
-
- /*----------------------------------------------------------------------------*/
- /* character string contains message put to window on exit */
- /*----------------------------------------------------------------------------*/
- char *sign_off[] =
- {
- "\n WINDOWS FOR C\n\n",
- " For more information contact Vermont Creative Software, (802) 848-7738."
- };
-
- char *title[] =
- {
- "VERTICAL BAR GRAPH",
- "HORIZONTAL BAR GRAPH"
- };
-
- main()
- {
- WINDOW wn[WINDOW_Q];
- MFILEPTR mfp;
- int cur_wn;
- int j, key;
-
-
- char r_origin = 14; /*declarations for graph demo */
- char c_origin = 6;
- char height = 14;
- char width = 70;
- char row_size;
- char col_size = 5;
- char col_space = 3;
- char row_space = 1;
- char r_begin = r_origin - 1;
- char c_begin;
- char ch = ' ';
- char attrib = LREVERSE; /*end of declarations for graph demo */
-
- /*----------------------------------------------------------------------------*/
- /* Initialize the Windows for C System */
- /*----------------------------------------------------------------------------*/
- init_wfc();
-
- /*----------------------------------------------------------------------------*/
- /* Initialize FREC's */
- /*----------------------------------------------------------------------------*/
- mfp = mf_def("grph_hlp.doc", MAXLINES, MAXCOL); /*explanation file */
-
- /*----------------------------------------------------------------------------*/
- /* Initialize windows */
- /*----------------------------------------------------------------------------*/
- def_wn(&wn[0], 0, 23, 0, 79, 1, 1, BDR_DLNP);
- sw_mf(mfp, &wn[0]);
- def_wn(&wn[1], 19, 23, 0, 79, 1, 1, BDR_DLNP);
- sw_mf(mfp, &wn[1]);
- def_wn(&wn[2], 0, 18, 0, 79, 0, 0, BDR_LNP);
-
- /*----------------------------------------------------------------------------*/
- /* Start the show */
- /*----------------------------------------------------------------------------*/
- cls(); /*clear screen */
-
- /*----------------------------------------------------------------------------*/
- /* Read in file for viewing */
- /*----------------------------------------------------------------------------*/
- if(mf_rd(mfp) == 0)
- errout("error in reading file ", mfp->fn);
-
- /*----------------------------------------------------------------------------*/
- /* display message file (fr[0]) in window 0 */
- /*----------------------------------------------------------------------------*/
- cur_wn = 0;
- if(set_wn(&wn[cur_wn]) == 0)
- errout("\ndefinitions inconsistent for window 1","");
-
- v_mf(&wn[cur_wn]); /*display file */
-
- /*----------------------------------------------------------------------------*/
- /* Get keystrokes and implement commands: F1 places message file in */
- /* wn[0]; F2 and F3 move the message file to the small window at */
- /* bottom and draw horizontal or vertical bar graphs in a window */
- /* above. */
- /* */
- /* ki_cum() captures multiple keystroke accumulated in the buffer so */
- /* that they can be processed by the scrolling subroutine as a single */
- /* unit. This speeds up scrolling and prevents accumulated keystrokes */
- /* that cause scrolling to continue after a depressed cursor is */
- /* released. */
- /* */
- /* The number of keystrokes collected is in keyrec.kq; this value can */
- /* be used in k_vcom, which can process multiple keystrokes at once. */
- /* */
- /* In this program we have chosen to call ki_cum() to collect keystrokes */
- /* but to use a value of only 1 keystroke in k_vcom always. This */
- /* slows down the scrolling slightly, but makes it evener and */
- /* pleasanter. You can experiment using keyrec.kq to see which you */
- /* prefer. */
- /*----------------------------------------------------------------------------*/
- while((key = ki_cum(&keyrec)) != -K_F10) /*loop until <F_10> is pressed */
- {
- /*----------------------------------------------------------------------------*/
- /* Check if key code is for F1. If so display file wn[0] */
- /*----------------------------------------------------------------------------*/
- if(key == -K_F1)
- {
- cur_wn = 0; /*display will be in wn[0] */
- set_wn(&wn[cur_wn]);
- v_mf(&wn[cur_wn]); /*display file */
- }
- /*----------------------------------------------------------------------------*/
- /* Check if key code is F2 or F3. If so display file in wn[1] */
- /* and then display appropriate graphing routine in wn[2]. */
- /*----------------------------------------------------------------------------*/
- else if(key == -K_F2 || key == -K_F3)
- {
- cur_wn = 1; /*display will be in wn[1] */
- set_wn(&wn[cur_wn]);
- v_mf(&wn[cur_wn]); /* display file */
- set_wn(&wn[2]); /*window for graphing display */
-
- if(key == -K_F2) /*draw vertical graph */
- {
- col_size = 5;
- r_begin = r_origin - 1;
- if(v_axes(r_origin, c_origin, height, width, &wn[2]) == 0)
- errout("error in axes dimensions","");
- for(j = 0; j < 8; j++)
- {
- c_begin = c_origin + col_space + (col_space + col_size)*j;
- row_size = 1 + j;
- if(v_bar(row_size, col_size, r_begin, c_begin,
- ch, attrib, &wn[2], BDR_0P) == 0)
- errout("error in bar dimensions","");
- }
- v_plst(r_origin + 2, CENTER_TEXT, title[0], &wn[2]);
- }
- else if(key == -K_F3) /*draw horizontal graph */
- /*power() function is appended */
- {
- v_axes(r_origin, c_origin, height, width, &wn[2]);
- c_begin = c_origin +1;
- row_space = 1;
- row_size = 1;
- for(j = 0; j < 6; j++)
- {
- r_begin = (r_origin -1) - (row_space) * (1 + j)
- - row_size * j;
- col_size = 2 * power(2, j);
- if(v_bar(row_size, col_size, r_begin, c_begin,
- ch, attrib, &wn[2], BDR_0P) == 0)
- errout("error in bar dimensions","");
- }
- v_plst(r_origin + 2, CENTER_TEXT, title[1],&wn[2]);
- }
- }
- /*----------------------------------------------------------------------------*/
- /* If not function key, check for cursor pad key and implement commands. */
- /* If command moves window origin in file, redraw window contents. */
- /*----------------------------------------------------------------------------*/
- else
- if(k_vcom(-key, keyrec.kq, &wn[cur_wn]) == 1)
- v_mf(&wn[cur_wn]);
- }
- /*----------------------------------------------------------------------------*/
- /* User has requested to exit. Time to say goodbye. */
- /*----------------------------------------------------------------------------*/
-
- cls(); /*clear screen */
- unset_wn(&wn[0]); /*unset before changing border */
- mod_wn(9, 0, 7, 80, &wn[0]); /*change size and location of window 1*/
- sw_border(BDR_DLNP, &wn[0]); /*changed border */
- sw_att(LREVERSE, &wn[0]); /*change window attribute */
- sw_bdratt(LNORMAL, &wn[0]); /*change border attribute */
- set_wn(&wn[0]); /*place window on screen */
- v_st(sign_off[0], &wn[0]); /*Windows for C */
- sw_att(LHIGHLITE, &wn[0]);
- v_st(sign_off[1], &wn[0]); /*For more informations ... */
- mv_csr(v_rwq - 2, 0, &wn0); /*move cursor to last line */
- exit_wfc();
- return(0);
- }
-
- /*power.c -- raise integers to integer powers */
-
-
- int power(a, n)
- int a;
- int n;
- {
- int i,p;
-
- if(a == 0)
- return(0);
-
- p = 1;
- for(i = 1; n > 0 && i <= n; i++)
- p = a * p;
- for(i = -1; n < 0 && i >= n; i--)
- p = p/a;
- return(p);
- }