home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_02 / clune.exe / MENU_DEM.C < prev    next >
Text File  |  1990-07-20  |  8KB  |  311 lines

  1.  
  2. /* menu_dem.c is a demonstration of the menu.c functions and how to use */
  3. /* them.  To compile, use Microsoft C LARGE MEMORY model.  To link, use: */
  4. /* LINK MENU_DEM,,,ERI_MENU MOUSE  If you don't have MOUSE.LIB, you may: */
  5. /* LINK MENU_DEM,,,ERI_MENU MOUS_SYS */
  6. /*  Written 8/89 by T. Clune.  No rights reserved. Modified 9/89 to use */
  7. /* mgets() and mgetch() in MENU instead of function pointers. */
  8. /* Comments spruced up 7/90 by T Clune. */
  9.  
  10. #include <stdio.h>
  11. #include <math.h>   /* atoi() and atof() declarations */
  12.  
  13.     /* my headers needed for this program */
  14. #include "ansi.h"     /* my ansi.sys macros */
  15. #include "mouselib.h" /* my mouse.lib front-end functions */
  16. #include "menu.h"     /* the menu.c declarations and definitions */
  17. #include "keys.h"     /* my defined constants for key presses */
  18.  
  19.     /* the functions in this file */
  20. static void getfile_demo(), menu_options_demo();
  21. static void mouse_sensitivity(), input_options();
  22. static int single_file(), binary_choice();
  23.  
  24.  
  25. void main()
  26. {
  27.     /* the main menu choices */
  28.     static char *options[] =
  29.     {
  30.     "Quit",
  31.     "get_file() demo",
  32.     "Menu adjustments options"
  33.     };
  34.  
  35.     /* ENTRIES is number of items in options[] above, CHOICE will be */
  36.     /* the subscript of options[] selected from the menu. */
  37.     int entries=3, choice;
  38.  
  39.     /* check for mouse and driver presence. */
  40.     if(!m_install().status)
  41.     {
  42.     printf("Mouse or mouse.sys not found.  Program aborting.\n");
  43.     exit(-1);
  44.     }
  45.  
  46.     /* set the menu for selection by mouse or keyboard. */
  47.     mouse_flag_toggle(MOUSE_PLUS_KEYBOARD);
  48.  
  49.     reset_menu(0);  /* menu highlight starts out on first menu item */
  50.  
  51.     for(;;)
  52.     {
  53.  
  54.     CLS; /* clear screen.  ANSI.SYS macro defined in ANSI.H */
  55.  
  56.         /* print the title of the menu, centered and underlined */
  57.     print_headline("DEMONSTRATION PROGRAM MAIN MENU", 20, 1);
  58.  
  59.         /* the heirarchical menu structure 'YOU ARE HERE:' aid */
  60.     pathprint("ROOT");
  61.  
  62.         /* get a menu selection.  single_file() is a */
  63.         /* boilerplate function defined in this file. */
  64.     choice=single_file(options, entries);
  65.  
  66.     switch(choice)  /* see the OPTION strings for meaning of choice */
  67.     {
  68.         case 0:
  69.             CLS;
  70.             exit(0);
  71.         case 1:
  72.             CLS;
  73.             getfile_demo();
  74.             reset_menu(choice);  /* reset the highlight to show */
  75.                        /* the last active selection from this menu */
  76.             break;
  77.         case 2:
  78.             CLS;
  79.             menu_options_demo();
  80.             reset_menu(choice);
  81.             break;
  82.         default:
  83.             break;
  84.  
  85.     }
  86.     }
  87. }
  88.  
  89.  
  90.  
  91.  
  92. /* binary_choice() is a generic input for y/n-type responses, in which */
  93. /* PRIMARY is the default value if <CR> or (assuming the mouse is active) */
  94. /* right mouse button is pressed, and ALTERNATE is the other valid input. */
  95. /* The prompt that is to be printed to explain the binary_choice() options */
  96. /* is separate from this function. */
  97.  
  98. static int binary_choice(primary, alternate)
  99. int primary, alternate;
  100. {
  101.     int c;
  102.     primary=toupper(primary);
  103.     alternate=toupper(alternate);
  104.  
  105.  
  106.     while((c=(toupper(mgetch()))) != primary && c != alternate && c != CARRIAGE_RETURN)
  107.     error_msg();
  108.  
  109.     if(c==CARRIAGE_RETURN)
  110.     c=primary;
  111.  
  112.     return c;
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119. /* getfile_demo() demonstrates get_file().  It displays a directory, from */
  120. /* which you select a file, whose name is then printed on the screen as your */
  121. /* selection. */
  122.  
  123. static void getfile_demo()
  124. {
  125.     string_struc f; /* the get_file() struct, defined in menu.h */
  126.     int c;
  127.  
  128.     CLS;
  129.     do
  130.     {
  131.     f=get_file();
  132.     if(f.error_flag != 0)
  133.     {
  134.         printf("Do you want to abort this function and return to the main menu (y/N)?\n");
  135.         c=binary_choice('N','Y');
  136.         if(c=='Y')
  137.         return;
  138.     }
  139.     }while(f.error_flag != 0);
  140.  
  141.     printf("You selected %s\n", f.string);
  142.     printf("Press any key to return...");
  143.     mgetch();
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /* input_options() is a menu for selecting what input device(s) will */
  150. /* activate the menu selection */
  151.  
  152. static void input_options()
  153. {
  154.     static char *options[] =
  155.     {
  156.     "Keyboard ONLY",
  157.     "Mouse ONLY",
  158.     "Mouse OR keyboard"
  159.     };
  160.  
  161.     int choice, entries=3;
  162.  
  163.     reset_menu(0);
  164.  
  165.     CLS;
  166.     print_headline("INPUT OPTIONS MENU", 20, 1);
  167.     pathprint("INPUT DEVICE");
  168.  
  169.     choice=single_file(options, entries);
  170.  
  171.     switch(choice)
  172.     {
  173.         /* set the selected option by calling mouse_flag_toggle(). */
  174.         /* the definitions of the defined constants are in menu.h */
  175.     case 0:
  176.         mouse_flag_toggle(KEYBOARD_ONLY);
  177.         break;
  178.     case 1:
  179.         mouse_flag_toggle(MOUSE_ONLY);
  180.         break;
  181.     case 2:
  182.         mouse_flag_toggle(MOUSE_PLUS_KEYBOARD);
  183.         break;
  184.     default:
  185.         break;
  186.     }
  187. }
  188.  
  189.  
  190.  
  191. /* menu_options_demo() is the menu for controlling whether the mouse will */
  192. /* be active, and if so, how sensitivie it will be. */
  193.  
  194. static void menu_options_demo()
  195. {
  196.     static char *options[] =
  197.     {
  198.     "Return to main menu",
  199.     "Select menu input option",
  200.     "Modify mouse sensitivity"
  201.     };
  202.  
  203.     int choice, entries=3;
  204.  
  205.     reset_menu(0);
  206.  
  207.     for(;;)
  208.     {
  209.     CLS;
  210.     print_headline("MENU-CONTROL OPTIONS MENU", 20, 1);
  211.     pathprint("MENU OPTIONS");
  212.  
  213.         /* get a menu selection */
  214.     choice=single_file(options, entries);
  215.  
  216.     switch(choice)
  217.     {
  218.         case 0:
  219.             CLS;
  220.             return;
  221.         case 1:
  222.             CLS;
  223.             input_options();
  224.             reset_menu(choice);
  225.             break;
  226.         case 2:
  227.             CLS;
  228.             mouse_sensitivity();
  229.             reset_menu(choice);
  230.             break;
  231.         default:
  232.             break;
  233.     }
  234.     }
  235. }
  236.  
  237.  
  238.  
  239. /* mouse_sensitivity() sets the three parameters of menu mouse */
  240. /* responsiveness: SENSITIVITY is the number of mickeys that the mouse must */
  241. /* move in a reading interval in order to be accepted as movement.  In */
  242. /* general, this is not an important number.  3 will keep random vibrations */
  243. /* of the desktop from causing a false positive, but you would have to make */
  244. /* the number quite large (say, 50 or more) to make an actual mouse motion */
  245. /* go unnoticed.  ON_TIME and OFF_TIME are the more critical parameters.  */
  246. /* together they define a mouse gated duty cycle. ON_TIME is the length */
  247. /* of time in seconds that the mouse is "active", i.e., how long you wait */
  248. /* before seeing whether the mouse has moved more than SENSITIVITY mickeys. */
  249. /* OFF_TIME is the amount of time between ON_TIME reads of the mouse.  The */
  250. /* resolution of the timer is 1/18.2 sec, or about 0.05 sec.  So a value of */
  251. /* 0.04 or 0.06 will be the same value.  It is important to have a reasonable */
  252. /* amount of OFF_TIME, so the user gets a "positive feel" when selecting a */
  253. /* menu item.  If OFF_TIME is too small, the user tends to overshoot his */
  254. /* menu item. */
  255.  
  256. static void mouse_sensitivity()
  257. {
  258.     int sensitivity;
  259.     double on_time, off_time;
  260.     char string[80];
  261.  
  262.     CLS;
  263.     printf("Enter the movement threshold in mickeys (default = 3)\n");
  264.     if(strlen(mgets(string)))
  265.     sensitivity=atoi(string);
  266.     else    /* carriage return keeps defaults */
  267.     sensitivity=3;
  268.  
  269.     printf("\nEnter the active part of the mouse duty cycle (default = 0.05 sec)\n");
  270.     if(strlen(mgets(string)))
  271.     on_time=atof(string);
  272.     else
  273.     on_time=0.05;
  274.  
  275.     printf("\nEnter the inactive part of the mouse duty cycle (default = 0.15 sec)\n");
  276.     if(strlen(mgets(string)))
  277.     off_time=atof(string);
  278.     else
  279.     off_time=0.15;
  280.  
  281.     /* set the new values */
  282.     menu_mouse_config(sensitivity, on_time, off_time);
  283. }
  284.  
  285.  
  286.  
  287. /* single_file() is a front-end for menu() calls when I want to have */
  288. /* a single row of menu selection items using alphabetical selection */
  289. /* and double-space between entries */
  290.  
  291. static int single_file(options, entries)
  292. char *options[];
  293. int entries;
  294. {
  295.     int top, left, tab, columns, spacing, auto_label;
  296.     int choice; /* menu selection variable */
  297.  
  298.     /* initialize menu variables */
  299.     top=5;      /* begin menu fields at line 5 */
  300.     left=20;    /* space in 20 characters to center the menu */
  301.     tab=0;      /* TAB is meaningless with one column. */
  302.     columns=1;
  303.     spacing=2;  /* double space between the menu entries */
  304.     auto_label= -2;  /* automatically generate alpha labels for entries, */
  305.             /* and let the user select an entry by that letter. */
  306.  
  307.     choice=menu(top,left,tab,columns,spacing,entries,auto_label,options);
  308.  
  309.     return choice;
  310. }
  311.