home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / demomous.c < prev    next >
C/C++ Source or Header  |  1991-03-31  |  8KB  |  366 lines

  1. /* demomous.c
  2.  *         demo file for illustrating mouse programming
  3.  */
  4.  
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <dos.h>
  8. #include <time.h>
  9.  
  10.  
  11. #ifdef __TURBOC__
  12.     #include <graphics.h>
  13. #else
  14.     #include "msc.h"
  15.     #include <graph.h>
  16.     #include <time.h>
  17.  
  18.  
  19. /* Microsoft doesn't provide a way to delay program execution
  20.  * This is a limitted version of the TurboC routine.
  21.  */
  22. void delay (long);
  23. void delay (long millisec)
  24.     {
  25.     time_t  start, quit;
  26.     
  27.     start = clock ();
  28.     quit  = start + (time_t) ((millisec *1000L)/CLK_TCK);
  29.     
  30.     while ( clock() < quit )
  31.         /* empty  loop - wait */
  32.         ;
  33.             
  34.     return;        /* Microsoft C version of delay */
  35.     } 
  36.  
  37. #endif        /* microsoft */
  38.  
  39. #include "wtwg.h"
  40.  
  41.  
  42. /* define arrays of 'buttons' which are mouse-selectable boxes onscreen.
  43.  * each button translates mouse selection into a keystroke,
  44.  * so it's transparent whether user clicked the button or pressed equivalent
  45.  * key.
  46.  */
  47. #define NBUTTONS 4
  48. #define BUTTON_LEN  9
  49. static char  *buttons[] =        { "F1 cow","F2 horse","F3 pig","F4 goat"};
  50. static char  button_toggles[NBUTTONS] = {0};
  51.  
  52.  
  53. static char *flabels[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
  54.                             "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN" };
  55.  
  56.  
  57.  
  58.  
  59.  
  60. main ()
  61.     {
  62.     int key, n;
  63.  
  64.  
  65.     #ifdef __TURBOC__
  66.         WOBJECT_D_ART *art_object;
  67.     #endif /* Turbo C */
  68.  
  69.     #define SCROLL_KEY  -1
  70.     #define RANGE         50
  71.     WBUTTON *Bptr;
  72.     
  73.     
  74.     int color= 0, linestyle = 0, thickness =0;
  75.     
  76.     
  77.     
  78.     winit ('T');
  79.     wclear ();
  80.     
  81.     key = wpromptc ("MOUSE DEMO", "   Run first part of demo\n"
  82.                                   "   in Text or Graphics modes ?\n",
  83.                                   "Text", "Graphics", NULL );
  84.                                   
  85.     /* Only possible returned values from above prompt are 'T', 'G'  & ESCAPE
  86.      */
  87.     if ( key == 'G' )
  88.         {
  89.         winit ('G');
  90.         }
  91.     else 
  92.     if ( key == ESCAPE )
  93.         {
  94.         exit (0);
  95.         }
  96.     
  97.         
  98.     /* DEMO part I - simple use of mouse relative to a window.
  99.      */
  100.     wopen ( 5,5, 50,10, (RED<<4)+YELLOW, SINGLE_BORDER,(RED<<4)+YELLOW, 0);
  101.     wputs ( "Move the mouse with a button down, or press a key\n"
  102.             "ESCAPE quits (RIGHT MOUSE BUTTON = ESCAPE)");
  103.             
  104.     while ( ESCAPE != (key = wgetc()) )
  105.         {
  106.         wclear();    
  107.         if ( key == MOUSE )
  108.             {
  109.             wprintf ( "Mouse used. %s\nX (window) =%3.3i, Y (window) =%3.3i\n"
  110.                                       "X (screen) =%3.3i, Y (screen) =%3.3i\n",
  111.                                       (wmouse.wms_inwindow)? "in window" : 
  112.                                                                "outside window",
  113.                                        wmouse.wms_x,   wmouse.wms_y,
  114.                                         wmouse.wms_xabs,wmouse.wms_yabs );
  115.             if ( wmode == 'G' )
  116.                 {
  117.                 wprintf (             "X pixels   =%3.3i, Y pixels  =%3.3i\n",
  118.                                         wmouse.wms_px, wmouse.wms_py );
  119.                 }                                    
  120.             if ( wmouse.wms_used & WMS_LEFT_RLS )
  121.                 {
  122.                 wputs ("LEFT BUTTON RELEASED");
  123.                 }
  124.             else
  125.             if ( wmouse.wms_used & WMS_LEFT_PRS )
  126.                 {
  127.                 wputs ("LEFT BUTTON JUST PRESSED - WAIT...");
  128.                 delay (1000);
  129.                 }
  130.             else
  131.             if ( wmouse.wms_used & WMS_CENTER_PRS )
  132.                 {
  133.                 wputs ("CENTER BUTTON JUST PRESSED - WAIT...");
  134.                 delay (1000);
  135.                 }
  136.             else
  137.             if ( wmouse.wms_used & WMS_CENTER_RLS )
  138.                 {
  139.                 wputs ("CENTER BUTTON RELEASED");
  140.                 }
  141.              /* The right button is automatically translated into ESCAPE 
  142.              */
  143.             
  144.             }    /* end (key==MOUSE) */
  145.         else
  146.             {
  147.             if ( isascii (key) && isprint(key) )
  148.                 {
  149.                 wprintf ("Key pressed,\nvalue = %c",key);
  150.                 }
  151.             else
  152.                 {
  153.                 wprintf ("Non-print key pressed,\nvalue =%i, scancode=%i",
  154.                             key, key-128);
  155.                 }
  156.             }
  157.         wgoto ( 2,9 );
  158.         wputs ( "ESCAPE or RIGHT BUTTON to quit" );
  159.         }    /* end while() */
  160.     
  161.     wclose ();
  162.      
  163.     /* DEMO PART II.  demo buttons.
  164.      */
  165.      
  166.     wopen ( 3,3,  25,20, GREEN, DOUBLE_BORDER, GREEN<<4, 0 );
  167.     wputs ( "Pick animals with mouse" );
  168.     for ( n= 0; n< NBUTTONS; ++n )
  169.             {
  170.                         /* text,     x, y,    len,          key,   style */
  171.             wbutton_add (buttons[n], 2, 2+3*n,BUTTON_LEN, FKEY(1+n),WBTN_BOX );
  172.             wgoto ( 4, 2+3*n );
  173.             }
  174.             
  175.             /* buttons for ESCAPE and ENTER to be mouse-selectable.
  176.              * Notes: x & y specify co-rds of text for button,
  177.              *            not the co-ords for the surrounding box.
  178.              *          length of the button includes terminal NULL of string.
  179.              *   
  180.              */
  181.     wbutton_add ( "ESCAPE", 2, 18, 7, ESCAPE, WBTN_BOX );
  182.     wbutton_add ( "ENTER",  11,18, 6, ENTER, WBTN_BOX );
  183.     
  184.     
  185.     /* allow user to select some buttons
  186.      */    
  187.     while ( !  (  (ESCAPE == (key = wgetc())) || ( ENTER == key ) )  )
  188.         {
  189.         if ( key >= FKEY(1)  && key <= FKEY(NBUTTONS) )
  190.             {
  191.             n = key - FKEY(1);        /* 0 thru NBUTTONS-1 */
  192.             button_toggles[n] = button_toggles[n] ? 0: 1;    /* toggle */
  193.             
  194.             if ( button_toggles[n] )
  195.                 {
  196.                 wbutton_mark (key, '>');        /* mark as ON */
  197.                 }
  198.             else
  199.                 {
  200.                 wbutton_mark (key, ' ');        /* remove mark */
  201.                 }
  202.             
  203.             }
  204.         
  205.         
  206.         }    /* end while */
  207.     /* show what choices were made
  208.      */    
  209.     wclear ();
  210.     wputs ("You...");
  211.     for ( n=0; n< NBUTTONS; ++n )
  212.         {
  213.         wgoto ( 4, 2+3*n );
  214.         if ( button_toggles[n] )
  215.             {
  216.             wputs ("\nSelected ");
  217.             }
  218.         else
  219.             {
  220.             wputs ("\nIgnored ");
  221.             }
  222.         wputs ( buttons[n] +3);        /* pts to animal name */
  223.         }
  224.     wgoto ( 0,18 );
  225.     wprintf ( "Final key was %s", (key==ENTER)? "ENTER" : "ESCAPE" );
  226.     
  227.     key=wpromptc (NULL,"This prompt also creates buttons...\n"
  228.                        "Is this easy? (Y=yes, N=no)", 
  229.                        /* NULL-terminated list of buttons for answers 
  230.                         */
  231.                           "YES", "NO", NULL );
  232.     wgoto ( 0,14 );
  233.     if ( key== 'Y' )
  234.         {
  235.         wputs ("Yes, it's easy");
  236.         }
  237.     else
  238.     if ( key== 'N' )
  239.         {
  240.         wputs ("It is so easy");
  241.         }
  242.     else
  243.         {
  244.         /* the only values returned after wpromptc(),
  245.          * are ESCAPE or the first letter of the buttons on the list
  246.          * so only choice left is ESCAPE
  247.          */
  248.         wputs ("ESCAPE hit");
  249.         }
  250.  
  251.  
  252.     wclose();
  253.  
  254.     /* DEMO PART III - scroll bar.
  255.      */
  256.     
  257.     n= RANGE/2;    
  258.     
  259.     wopen (5,2, 12,20, 0x07, SINGLE_BORDER, 0x70, 0);
  260.     wtitle ("SCROLL BAR");
  261.         
  262.     /* create a scrollbar at x=5, onscreen from y=2 to 18,
  263.      * covering virtual values from 0 to 50 (RANGE = 50)
  264.      * initially at midpoint. 
  265.      */
  266.     Bptr =wscrollbar_add ( SCROLL_KEY, 5, 2, 18, RANGE, n );
  267.     wgoto (0,0);
  268.     wprintf ("value =%2.2u", n);
  269.  
  270.     /* label the limits of the scrollbar
  271.      */
  272.     wgoto ( 3,  2 );
  273.     wputs ( "00" ); 
  274.     wgoto ( 3, 18 );
  275.     wprintf ("%2.2u", RANGE);
  276.     
  277.     
  278.     while ( ESCAPE != (key=wgetc()) )
  279.         {
  280.  
  281.         if ( key == SCROLL_KEY )
  282.             {
  283.             n = wscrollbar_scroll ( Bptr );
  284.             }
  285.         else
  286.         if ( key == DN_ARROW  &&  n < RANGE )
  287.             {
  288.             ++n;
  289.             wscrollbar_reset (Bptr, n);
  290.             }
  291.         else
  292.         if ( key == UP_ARROW  &&  n >0 )
  293.             {
  294.             --n;
  295.             wscrollbar_reset (Bptr, n);
  296.             }
  297.  
  298.         wgoto (0,0);
  299.         wprintf ("value =%2.2u", n);
  300.             
  301.             
  302.         }
  303.  
  304.  
  305.     /* DEMO part IV. mouse center button.
  306.      */
  307.     wpopfkey ( flabels );
  308.     
  309.     key = wpromptc ( NULL, "PRESS MIDDLE MOUSE BUTTON TO GET FKEY MENU", NULL );
  310.     if (  ( key >= FKEY(1) && key <= FKEY(10) )
  311.         {
  312.         wpromptc ( NULL, flabels[key - FKEY(1)], NULL );
  313.         }
  314.     else 
  315.     if ( key == FKEY_11 )
  316.         {
  317.         wpromptc ( NULL, flabels[11 -1], NULL );
  318.         }
  319.  
  320.  
  321.  
  322.     
  323.     #ifdef __TURBOC__    
  324.     /* DEMO part V. drawing.
  325.      */
  326.     winit ('G'); 
  327.      
  328.     wsetattr (0x70);
  329.     wclear ();
  330.     
  331.     wopen ( 2,2, 75, 15, LIGHTGRAY, SINGLE_BORDER, LIGHTGRAY, 0); 
  332.     wtitle ("CANVAS");
  333.     while ( ESCAPE !=     
  334.                 (key = wpromptc ("DRAWING DEMO", 
  335.                         "Pick a shape to draw.\n"
  336.                         "Click left button to start, right to erase\n"
  337.                         "H=horizontal line, V=vertical line, X=cross-hair,\n"
  338.                         "R=rectangle,       S=squiggle,      L=line",
  339.                         "H", "V", "X", "R", "L", "S", NULL ) ) )
  340.         {
  341.         wclear();
  342.         key = tolower (key);    /* shape indicators are 'r', 'v' etc... */
  343.         if ( wmonitor == 'H' )
  344.             {
  345.             color = 0x07;
  346.             }
  347.         else
  348.             {
  349.             color = 1+ (color++)%7;        /* rotate colors 1-7 */
  350.             }
  351.         thickness = (thickness==NORM_WIDTH) ? THICK_WIDTH : NORM_WIDTH;
  352.         linestyle = (linestyle++)%4;    /* rotate linestyles 0-3 */
  353.         
  354.         art_object =wdraw ( key, color +BRIGHT, linestyle, thickness );
  355.         /* the structure members art_object-> wdr_x1, _x2, _y1, _y2
  356.          * give the window-relative pixel co-ords for the object 
  357.          */
  358.         free ( art_object );
  359.         }
  360.     
  361.     #endif /* __TURBOC__ */
  362.     
  363.     
  364.     return (0);        /* main */
  365.     }
  366.