home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c003 / 1.ddi / DEMOS / DEM_MENU.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-31  |  10.7 KB  |  247 lines

  1. /* dem_menu.c -- Demo of menu capabilities of Windows for C
  2.  
  3.     ********** Copyright 1985 by Vermont Creative Software **********
  4.  
  5.     USAGE
  6.  
  7.      To display a menu on a screen, allow menu selection, and then
  8.      restore the original screen content.
  9.  
  10.     FUNCTION
  11.  
  12.      A pointer to a structure containing the menu information is passed
  13.      as a calling parameter.  Additional parameters specify the number
  14.      of character spaces per item and the number of items in the menu.
  15.  
  16.      The menu window is made a pop-up window.
  17.  
  18.      The menu-window is placed on the screen and the menu text moved to
  19.      the window.
  20.  
  21.      The first item is highlighted.
  22.  
  23.           Highlighting is accomplished by using a sub-window that is
  24.           one-menu item in size.  The sub-window is placed over the
  25.           menu item, and v_natt() is called to change the text
  26.           attribute within the window to LCHOICEA.
  27.  
  28.      Cursor pad keys are used to move the highlight area to other menu
  29.      items.
  30.  
  31.      Pressing <Enter> "selects" the menu item highlighted, which causes
  32.      the following sequence:
  33.  
  34.           The original screen is restored over the menu window.
  35.  
  36.           The code number of the menu item is returned on exit.
  37.  
  38.  
  39.     COMMENT
  40.  
  41.      This program illustrates how the capabilities of Windows for C can
  42.      be used to program a high-quality menu selection display.
  43.  
  44.      Pop-up windows are implemented just by designating the window to
  45.      be a pop-up as opposed to an overwrite window.
  46.  
  47.      The capability to change the attribute of a section of a window is
  48.      provided by v_natt().
  49.  
  50.           v_natt() is used in menu() to highlight the selected menu
  51.           items.
  52.  
  53.           Highlighting is accomplished by placing a small window just
  54.           the size of one menu-item space over the menu item and then
  55.           using v_natt() to change the attribute of the window to
  56.           REVERSE or LCHOICEA.
  57.  
  58.           When a menu item is de-selected, v_natt() is used to restore
  59.           the original attribute.
  60.  
  61.      The status line that displays the number of the selected menu item
  62.      provides another illustration of the ability of Windows for C to
  63.      simplify screen management.
  64.  
  65. */
  66.  
  67. /* #define WN_DEBUG          Commented out when debugging completed          */
  68. #include <wfc.h>
  69. #include <wfc_glob.h>
  70.  
  71. #define QITEMS 18            /*number of items in menu          */
  72. #define ITEMLEN 10            /*length of each item in menu          */
  73. #define ITEM_PER_ROW 7            /*number of items per row          */
  74. #define DEF_ITEM 1            /*default item when menu is displayed */
  75.  
  76. #define MAXCOL 80            /*maximum number of columns in file   */
  77. #define WINDOW_Q 3            /*Number of windows              */
  78. #define FILE_Q 3            /*Number of file records read          */
  79.  
  80. #define KEYMAX 5            /*max number of keystrokes to process */
  81.                     /*in one call to k_vcom           */
  82.  
  83. /*----------------------------------------------------------------------------*/
  84. /*  Declare structures externally to permit initialization              */
  85. /*----------------------------------------------------------------------------*/
  86. KEYR keyrec = {0, 0, KEYMAX};        /*see wfc_stru.h for definition       */
  87.  
  88. /*----------------------------------------------------------------------------*/
  89. /*   character string contains message put to window on exit              */
  90. /*----------------------------------------------------------------------------*/
  91.  
  92. char *sign_off[] =
  93. {
  94.     "\n                          WINDOWS FOR C\n\n",
  95.     "  For more information contact Vermont Creative Software, (802) 848-7738."
  96. };
  97.  
  98.  
  99. main()
  100. {
  101.     MFILEPTR mfp[3];            /*array of pointers to memory files   */
  102.     WINDOW main_wn;            /*main viewing window              */
  103.     WINDOW stat_wn;            /*status window               */
  104.     WINDOW menu_wn;            /*pop-up menu window              */
  105.     register int i;
  106.     int key;
  107.     int cur_file, next_file;
  108.     int itemnumber;            /* number or selected menu item       */
  109.  
  110.  
  111. /*----------------------------------------------------------------------------*/
  112. /*  Initialize the Windows for C System                       */
  113. /*----------------------------------------------------------------------------*/
  114.     init_wfc();
  115.  
  116. /*----------------------------------------------------------------------------*/
  117. /*  Initialize MFILE structures                           */
  118. /*----------------------------------------------------------------------------*/
  119.     mfp[0] = mf_def("menu.doc", 100, MAXCOL);      /*documentation file       */
  120.     mfp[1] = mf_def("menu_hlp.doc", 75, MAXCOL);   /*help file                */
  121.     mfp[2] = mf_def("menu.dem", 10, MAXCOL);       /*menu item list           */
  122.  
  123. /*----------------------------------------------------------------------------*/
  124. /*  Initialize the WINDOW structures                          */
  125. /*----------------------------------------------------------------------------*/
  126.     def_wn(&main_wn, 0, 22, 0, 79, 1, 1, &bdr_dln);    /*main viewing window */
  127.     sw_mf(mfp[1], &main_wn);
  128.     sw_name("Main Window", &main_wn);
  129.  
  130.     def_wn(&stat_wn, 23, 23, 0, 79, 2, 2, &bdr_0);    /*status line window  */
  131.     sw_att(LREVERSE, &stat_wn);
  132.  
  133.     def_wn(&menu_wn, 19, 22, 0, 79, 0, 0, &bdr_dln);    /*popup-menu window   */
  134.     sw_mf(mfp[2], &menu_wn);
  135.  
  136.  
  137. /*----------------------------------------------------------------------------*/
  138. /*  Time to start the show                              */
  139. /*----------------------------------------------------------------------------*/
  140.     cls();                /*clear screen                  */
  141.  
  142. /*----------------------------------------------------------------------------*/
  143. /* Read in files for viewing                              */
  144. /*----------------------------------------------------------------------------*/
  145.     for(i = 0; i < FILE_Q; i++)
  146.     if(mf_rd(mfp[i]) == 0)
  147.         errout("error in reading file ", mfp[i]->fn);
  148.  
  149. /*----------------------------------------------------------------------------*/
  150. /*  display message file (mfp[1]) in main window                  */
  151. /*----------------------------------------------------------------------------*/
  152.     if(set_wn(&main_wn) == 0)
  153.     errout("\ndefinitions inconsistent for window 1","");
  154.     cur_file = 1;
  155.     sw_mf(mfp[cur_file], &main_wn);    /*set main window to display help file*/
  156.     v_mf(&main_wn);            /*display file                  */
  157.  
  158. /*----------------------------------------------------------------------------*/
  159. /* establish status line and print menu number message                  */
  160. /*----------------------------------------------------------------------------*/
  161.     if(set_wn(&stat_wn) == 0)
  162.     errout("\ndefinitions inconsistent for status window","");
  163.     v_plst(0, 9, "menu item number: ", &stat_wn);
  164.  
  165. /*----------------------------------------------------------------------------*/
  166. /*  Now that we have established the background text on the status line, we   */
  167. /*  will inhibit cursor advance so we can repeatedly update the item number   */
  168. /*  without having to worry about the cursor moving.                  */
  169. /*----------------------------------------------------------------------------*/
  170.     sw_csadv(OFF, &stat_wn);        /*set no cursor advance on status line*/
  171.  
  172. /*----------------------------------------------------------------------------*/
  173. /*    Get keystrokes and implement commands: F1 - F2 change active files,   */
  174. /*    cursor pad keys move window over text. F10 ends program.          */
  175. /*                                          */
  176. /*    ki_cum() captures multiple keystroke accumulated in the buffer so     */
  177. /*    that they can be processed by the scrolling subroutine as a single    */
  178. /*    unit.  This speeds up scrolling and prevents accumulated keystrokes   */
  179. /*    that cause scrolling to continue after a depressed cursor is          */
  180. /*    released.                                  */
  181. /*                                          */
  182. /*    The number of keystrokes collected is in keyrec.kq; this value can    */
  183. /*    be used in k_vcom, which can process multiple keystrokes at once.     */
  184. /*                                          */
  185. /*    In this program we have chosen to call ki_cum() to collect keystrokes */
  186. /*    but to use a value of only 1 keystroke in k_vcom always.  This          */
  187. /*    slows down the scrolling slightly, but makes it evener and          */
  188. /*    pleasanter.  You can experiment using keyrec.kq to see which you      */
  189. /*    prefer.                                   */
  190. /*----------------------------------------------------------------------------*/
  191.     while((key = ki_cum(&keyrec)) != -K_F10)   /*loop until <F_10> is pressed */
  192.     {
  193. /*----------------------------------------------------------------------------*/
  194. /*  Check if key code is for a F1 or F2.  If so transfer file 1 or two to     */
  195. /*  the display window, main_wn.                          */
  196. /*----------------------------------------------------------------------------*/
  197.     if(key == -K_F1 || key == -K_F2)
  198.     {
  199.         next_file = -key - K_F1;
  200.         if(next_file != cur_file)
  201.         {
  202.         cur_file = next_file;
  203.         sw_mf(mfp[cur_file], &main_wn);
  204.         v_mf(&main_wn);                   /* display file */
  205.         }
  206.     }
  207. /*----------------------------------------------------------------------------*/
  208. /*  Check if key code is F9.  If so call menu() routine.  Upon exit from      */
  209. /*  this routine, place screen cursor back in its original position          */
  210. /*----------------------------------------------------------------------------*/
  211.     else if(key == -K_F9)
  212.     {
  213.         itemnumber =  menu2(&menu_wn, QITEMS, ITEMLEN, ITEM_PER_ROW,
  214.                 DEF_ITEM);
  215.         v_printf(&stat_wn, "%d", itemnumber);      /*print in stat_wn     */
  216.     }
  217. /*----------------------------------------------------------------------------*/
  218. /* If not function key, check for cursor pad key and implement commands.      */
  219. /* If command moves window origin in file, redraw window contents.          */
  220. /*----------------------------------------------------------------------------*/
  221.     else
  222.         if(k_vcom(-key, 1, &main_wn) == 1)         /*if window origin moved */
  223.         v_mf(&main_wn);              /*redraw window          */
  224.     }
  225.  
  226. /*----------------------------------------------------------------------------*/
  227. /*  The user has pressed the exit key.    Time to clear the screen and say      */
  228. /*  goodbye.                                      */
  229. /*----------------------------------------------------------------------------*/
  230.     cls();                /*clear screen                  */
  231.     unset_wn(&stat_wn);         /*unset before changing border          */
  232.     mod_wn(9, 0, 7, 80, &stat_wn);    /*change size and location of window  */
  233.     sw_att(LREVERSE, &stat_wn);     /*change window attribute          */
  234.     sw_bdratt(LNORMAL, &stat_wn);    /*change border attribute          */
  235.     sw_border(BDR_LNP, &stat_wn);    /*change border               */
  236.     sw_csadv(ON, &stat_wn);        /*turn on cursor advance          */
  237.                     /*otherwise sign off message          */
  238.                     /*gets garbled.               */
  239.     set_wn(&stat_wn);            /*set window on screen              */
  240.     v_st(sign_off[0],&stat_wn);     /*Windows for C         */
  241.     sw_att(LHIGHLITE, &stat_wn);
  242.     v_st(sign_off[1],&stat_wn);     /*For more information ...  */
  243.     mv_csr(v_rwq - 2, 0, &wn0);     /*Place cursor at bottom of screen    */
  244.     exit_wfc();
  245.     return(0);
  246. }
  247.