home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / GRAPHICS / WINDOWPR.ZIP / TUTOR5.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-30  |  13.4 KB  |  500 lines

  1. #include "pro.h"
  2. #include "xglobals.h"
  3. #include "vs.h"
  4. #include "colors.h"
  5. #include "mouse.h"
  6.  
  7. int main()
  8. {
  9.     unsigned int window_handle[3], x, y, rhandle;
  10.     int m1, m2, m3, m4, result;
  11.     unsigned char rthandle;
  12.  
  13.     /*
  14.      * See tutors 1-3 for an explanation of this section.
  15.     */
  16.     active_attr = WHITE + (BLACK << 4);
  17.     inactive_attr = LIGHTGRAY + (BLACK << 4);
  18.     active_tile_attr = LIGHTGREEN + (BLACK << 4);
  19.     inactive_tile_attr = RED + (BLACK << 4);
  20.     scroll_bars_on = TRUE;
  21.     wn_init();
  22.  
  23.  
  24.     /*
  25.      * We'll use this window to make a backdrop for the other windows
  26.     */
  27.     window_handle[0] = wn_createw(25,80,1,1,1,1,23,78,FALSE,NONE,NULL,NULL);
  28.     vs_fillchar(window_handle[0],0,1,1,80,25, '░', WHITE, GREEN);
  29.     wn_openw(window_handle[0]);
  30.     
  31.     window_handle[2] = wn_createw(25,80,7,7,1,1,10,35,FALSE,HEAD_ON,"EXTRA WINDOW",NULL);
  32.     wn_openw(window_handle[2]);
  33.     vs_clrvs(window_handle[2], 0, WHITE, BLACK);
  34.  
  35.     window_handle[1] = wn_createw(25,80,5,5,1,1,15,30,FALSE,HEAD_ON,"MOUSE TUTORIAL",NULL);
  36.     wn_openw(window_handle[1]);
  37.  
  38.     /*
  39.      * Determine if mouse hardware and software exist.
  40.      * kb_ismouse also resets the mouse internal registers.
  41.     */
  42.     sprintf(buf,"kb_ismouse returns = %d.\n\n",kb_ismouse());
  43.     vs_format(window_handle[1],0,WHITE,BLACK,buf);
  44.     if (kb_ismouse()) vs_format(window_handle[1],0,WHITE, BLACK,
  45.         "Great, you've got a mouse\ninstalled.....\n");
  46.     else {
  47.         vs_format(window_handle[1],0,WHITE,BLACK,
  48. "Sorry, unless you've got a\n\
  49. mouse installed we can't\n\
  50. continue.\n");
  51.         exit(0);
  52.     }
  53.  
  54.     vs_format(window_handle[1],0, WHITE, BLACK, "\nPress any key to continue.");
  55.     getch();
  56.  
  57.     /*
  58.      * Lesson 1:  Turning the mouse cursor on and off and
  59.      *          Is the mouse on a window?
  60.     */
  61.  
  62.     /* Standard stuff..from previous tutortials */
  63.     vs_locatecur(window_handle[1],0,1,1);
  64.         vs_clrvs(window_handle[1], 0, WHITE, BLACK);
  65.     vs_format(window_handle[1],0,WHITE,BLACK,
  66. "Click on either window\n\
  67. to activate it.\n\n\
  68. Press any key to go on.\n");
  69.  
  70.     /* turn on the mouse cursor */
  71.     kb_showmouse();
  72.  
  73.     /* We'll use a keypress to terminate this lesson */
  74.     while (!kbhit()) {
  75.  
  76.         /*
  77.          * mouse status will return the status of the buttons
  78.          * in m2 (1 if left pressed, 2 if right pressed, 3 if
  79.          * both pressed.) and the location of the mouse cursor
  80.          * in m3 (x, 0-640) and m4 (y, 0-200).
  81.         */
  82.  
  83.         kb_mousestatus(&m2, &m3, &m4);
  84.         
  85.         /*
  86.          * Have to convert from a 0-640,0-200 to a 1-80,1-25 coord
  87.          * system.
  88.         */
  89.  
  90.         x = (m3/8) + 1;
  91.         y = (m4/8) + 1;
  92.  
  93.         /*
  94.          * Note in our example the first window we created was actually
  95.          * a background for the other two windows.  The first window
  96.          * after calling wn_init always has a handle of 0.
  97.          *
  98.          * wn_isonwdw returns TRUE is the cursor is on a window, otherwise
  99.          * it returns FALSE.  The handle of the window is returned in
  100.          * rhandle.
  101.         */
  102.  
  103.         if ((m2 != 0) && (wn_isonwdw(x, y, &rhandle))) {
  104.  
  105.             /* If it is not the background window then continue */
  106.             if (rhandle > 0) {
  107.  
  108.                 /*
  109.                  * Always turn off the mouse cursor before
  110.                  * updating the screen.  Otherwise the
  111.                  * mouse software will sometimes interrupt
  112.                  * the redraw operations at 'bad' points
  113.                  * leaving 'mouse droppings :-).'
  114.                 */
  115.  
  116.                 kb_hidemouse();
  117.  
  118.                 /*
  119.                  * No need to open it if it is already on top
  120.                 */
  121.                 if (rhandle != last_wdw) wn_openw(rhandle);
  122.  
  123.                 /* And then turn it back on again */
  124.                 kb_showmouse();
  125.             }
  126.         }
  127.     }
  128.     /* clear the previous keypress */
  129.     getch();
  130.  
  131.     /*
  132.      * Lesson 2 : Using wn_isonvs.
  133.      *
  134.      * wn_isonvs, is similar to wn_isonwdw, but only returns true if
  135.      * the mouse cursor is in the viewport of the specific window and
  136.      * tile.  If it is it transforms the
  137.      * coordinates to virtual screen logical coordinates.
  138.      *
  139.      * When calling wn_isonvs you must specify the window handle and
  140.      * tile handle of interest.  You have already seen how to get
  141.      * a window handle via wn_isonwdw.  You can use a similar function
  142.      * wn_isontile to determine the tile it is on, and then call
  143.      * wn_isonvs to determine if it is in the viewport and have the
  144.      * coordinates transformed.
  145.      *
  146.      * Let's say we want a system where we can move the logical cursor
  147.      * to a point on a virtual screen by placing the mouse there and
  148.      * clicking.
  149.      *
  150.     */
  151.  
  152.     /* Standard stuff... */
  153.     wn_openw(window_handle[1]);
  154.     vs_clrvs(window_handle[1],0,WHITE,BLACK);
  155.     vs_locatecur(window_handle[1],0,1,1);
  156.     vs_format(window_handle[1],0, WHITE, BLACK,
  157. "Position mouse inside of\n\
  158. window and click to move\n\
  159. hardware cursor..\n\n\
  160. Press any key to continue.\n");
  161.  
  162.     /* See below for explanation */
  163.     kb_mousereleased(&m1, &m2, &m3, &m4);
  164.  
  165.     while (!kbhit()) {
  166.         
  167.             /*
  168.          * kb_mousepressed reports information on mouse presses.
  169.          * You pass the button(s) of interest in m1 (1 = left, 2=
  170.          * right, 3 = both) and it returns the current status of
  171.          * the buttons in m1, the number of presses on the button
  172.          * of interest since the last call to kb_mousepressed.
  173.          * m3,m4 return the coordinates of the last button press.
  174.          *
  175.          * kb_mouserelease is identical, but returns information on
  176.          * mouse releases.
  177.          *
  178.          * Let's call a 'click' equivalent to a mouse release since
  179.          * you can't release a button until you've pressed.
  180.         */
  181.         
  182.         /* We are interested in the left button */
  183.         m1 = 1;
  184.  
  185.         /* Has the left button been released? */
  186.         kb_mousereleased(&m1, &m2, &m3, &m4);
  187.  
  188.         if (m2 > 0) {
  189.             
  190.             /*
  191.              * Transform from mouse coordinates to physical screen
  192.              * coordinates.
  193.             */
  194.  
  195.             x = (m3/8) + 1;
  196.             y = (m4/8) + 1;
  197.  
  198.             /* Is it on a window? */
  199.             if (wn_isonwdw(x,y,&rhandle)) {
  200.  
  201.                 /*
  202.                  * Is it something other than the background
  203.                  * window ?
  204.                 */
  205.         
  206.                 if (rhandle > 0) {
  207.  
  208.                     /*
  209.                      * bring the window of interest to the top
  210.                      * if it isn't already on top.
  211.                     */
  212.                     if (rhandle != last_wdw) {
  213.  
  214.                                             /* turn off the mouse cursor */
  215.                         kb_hidemouse();
  216.  
  217.                         wn_openw(rhandle);
  218.                 
  219.                         /* turn the mouse cursor back on */
  220.                         kb_showmouse();
  221.                     }
  222.                                                              
  223.                     /* find out what tile the mouse is in */
  224.                     wn_isontile(rhandle, &rthandle, x, y);
  225.  
  226.                     /*
  227.                      * Is it in a viewport?
  228.                      * If yes, relocate the logical cursor.
  229.                      */
  230.  
  231.                     if (wn_isonvs(rhandle, rthandle, &x, &y))  {
  232.                         vs_locatecur(rhandle, rthandle, x, y);
  233.                     }
  234.                 }        
  235.             }
  236.         }
  237.     }
  238.     /* clear the last keypress */
  239.     getch();
  240.  
  241.     /*
  242.      * Lesson 3: Sensing points on the window border and using kb_mouseclicks
  243.      *
  244.      * You can make points on the border of a window have functional
  245.      * significance.  For example, you might say that the upper left
  246.      * corner can be 'grabbed' and used to move a window and the
  247.      * lower right corner can be grabbed and used to resize a window.
  248.      *
  249.      * Perhaps double-clicking on a window zooms it and single clicking
  250.      * positions the hardware cursor.
  251.      *
  252.      * Once you have determined that a point is on a window you can
  253.      * find out if it is in the border with functions like, wn_isonulc
  254.      * (is it on the upper left corner?) and wn_isonlrc (is it on the
  255.      * lower right corner?).  There are several of these -- see the
  256.      * reference manual for a complete list.
  257.     */
  258.  
  259.     /* Standard stuff... */
  260.     wn_openw(window_handle[1]);
  261.     vs_clrvs(window_handle[1],0,WHITE,BLACK);
  262.     vs_locatecur(window_handle[1],0,1,1);
  263.     vs_format(window_handle[1],0, WHITE, BLACK,
  264. "Double click to zoom.\n\
  265. Click to move hardware cursor.\n\
  266. Grab upper left corner to drag.\n\
  267. Grab lower right corner to size.\n\
  268. Press or click scroll bars to\n\
  269.   scroll virtual screen.\n\n\
  270. Press any key or click\n\
  271. on window name to continue.\n");
  272.  
  273.  
  274.     while (!kbhit()) {
  275.     
  276.         /*
  277.          * kb_mouseclicks is a simpler function to use than
  278.          * the other button sensing functions.
  279.          * However, it does not provide as much control.
  280.          * It returns
  281.          * DOUBLECLICK for a double-click,
  282.          * CLICK for a single-click,
  283.          * PRESS for a button-press, and
  284.          * RELEASE for a button-release.
  285.          *
  286.          * BUTTON should be 1 for the left and 2 for the right.
  287.          * TIMEOUT sets the mouse sensitivity.  It is actually the
  288.          * the number of times kb_mouseclicks iterates through its
  289.          * main loop without sensing any mouse activity.  As such
  290.          * it is a processor dependent routine. (Maybe we'll fix that
  291.          * later on.)
  292.         */
  293.  
  294.         #define BUTTON        1
  295.         #define TIMEOUT        300
  296.         
  297.         /* check the mouse */
  298.         result = kb_mouseclicks(BUTTON, TIMEOUT, &x, &y);
  299.  
  300.         /* convert the coordinates */
  301.         x /= 8;
  302.         y /= 8;
  303.         x++;
  304.         y++;
  305.  
  306.         /*
  307.          * If there was a button event and we are on a window and
  308.          * it is not the background window, then we better do something
  309.          * about it.
  310.         */
  311.         
  312.                 if ( (result) && (wn_isonwdw(x, y, &rhandle)) && (rhandle > 0)) {
  313.  
  314.             /*
  315.              * First let's bring that window to the top of
  316.              * the screen if it isn't already there
  317.             */
  318.  
  319.             if (rhandle != last_wdw) {
  320.                 kb_hidemouse();
  321.                 wn_openw(rhandle);
  322.                 kb_showmouse();
  323.             }
  324.             
  325.             /*
  326.              * If this is a PRESS let's investigate further */
  327.             if (result == PRESS) {
  328.                 
  329.                 /*
  330.                  * If it is on the upper left corner then
  331.                  * move the window until we release the button
  332.                 */
  333.                 
  334.                 if (wn_isonulc(rhandle, x, y)) {
  335.                 
  336.                     m2 = 1;
  337.  
  338.                     /* While left button is pressed --- */
  339.                     while (m2 == 1) {
  340.                         /*
  341.                          * we have to get the current location of the cursor
  342.                          * because kb_mouseclicks only gives us the
  343.                          * location of the cursor at the last button event.
  344.                          * and this routine is predicated on their not being
  345.                          * an event.  In addition we use kb_mousestatus to
  346.                          * to look for the release of the key because it
  347.                          * doesn't use the timeout approach (which slows down
  348.                          * the response and makes the window lag behind the mouse
  349.                          * cursor.)
  350.                         */
  351.                         kb_mousestatus(&m2, &m3, &m4);
  352.  
  353.                         /* convert the coordinates */
  354.                         x = (m3/8) + 1;
  355.                         y = (m4/8) + 1;
  356.  
  357.                         /*
  358.                          * If window is in a different position
  359.                          * then the mouse cursor, move the window
  360.                         */                        
  361.                         if ((window[rhandle]->physical_x != x) ||
  362.                             (window[rhandle]->physical_y != y)) {
  363.                             kb_hidemouse();
  364.                             wn_locatew(rhandle, x, y);
  365.                             kb_showmouse();
  366.                         }
  367.                     }
  368.                 }
  369.                 
  370.                 /*
  371.                  * Same as for moving except we size the window
  372.                 */
  373.                 if (wn_isonlrc(rhandle, x, y)) {
  374.                     m2 = 1;
  375.                     while (m2 == 1) {
  376.                         kb_mousestatus(&m2, &m3, &m4);
  377.                         x = (m3/8) + 1;
  378.                         y = (m4/8) + 1;
  379.                         if (((window[rhandle]->physical_x +
  380.                             window[rhandle]->port_columns + 1)
  381.                             != x) ||
  382.                             ((window[rhandle]->physical_y +
  383.                             window[rhandle]->port_rows + 1)
  384.                             != y)) {
  385.                             kb_hidemouse();
  386.                             wn_sizet(rhandle,
  387.                                 window[rhandle]->last_tile,
  388.                                 x - window[rhandle]->physical_x -
  389.                                 window[rhandle]->port_columns - 1, y -
  390.                                 window[rhandle]->physical_y -
  391.                                 window[rhandle]->port_rows - 1);
  392.                             kb_showmouse();
  393.                         }
  394.                     }
  395.                 }
  396.  
  397.                 /* We handle the scroll bars in the same way */
  398.                                 if (wn_isonsbu(rhandle, 0, x, y)) {
  399.                     m2 = 1;
  400.                     while (m2 == 1) {
  401.                         kb_mousestatus(&m2, &m3, &m4);
  402.                         kb_hidemouse();
  403.                         wn_scrollvs(rhandle,0,0,1);
  404.                         kb_showmouse();
  405.                     }
  406.                 }
  407.                                 if (wn_isonsbl(rhandle, 0, x, y)) {
  408.                     m2 = 1;
  409.                     while (m2 == 1) {
  410.                         kb_mousestatus(&m2, &m3, &m4);
  411.                         kb_hidemouse();
  412.                         wn_scrollvs(rhandle,0,1,0);
  413.                         kb_showmouse();
  414.                     }
  415.                 }
  416.                                 if (wn_isonsbr(rhandle, 0, x, y)) {
  417.                     m2 = 1;
  418.                     while (m2 == 1) {
  419.                         kb_mousestatus(&m2, &m3, &m4);
  420.                         kb_hidemouse();
  421.                         wn_scrollvs(rhandle,0,-1,0);
  422.                         kb_showmouse();
  423.                     }
  424.                 }
  425.                                 if (wn_isonsbd(rhandle, 0, x, y)) {
  426.                     m2 = 1;
  427.                     while (m2 == 1) {
  428.                         kb_mousestatus(&m2, &m3, &m4);
  429.                         kb_hidemouse();
  430.                         wn_scrollvs(rhandle,0,0,-1);
  431.                         kb_showmouse();
  432.                     }
  433.                 }
  434.             }
  435.  
  436.             /* If its a double-click zoom in the viewport the zoom it */
  437.             if ((result == DOUBLECLICK) && (wn_isonvs(rhandle, 0, &x, &y))) {
  438.                 kb_hidemouse();
  439.                 wn_zoomw();
  440.                 kb_showmouse();
  441.             }
  442.  
  443.             /* If it is a single-click we've got different choices */
  444.             if (result == CLICK) {
  445.  
  446.                 /*
  447.                  * if it is in a virtual screen move the
  448.                  * screen's cursor.
  449.                 */
  450.                 if (wn_isonvs(rhandle,0,&x,&y))
  451.                     vs_locatecur(rhandle,0,x,y);
  452.  
  453.                 /*
  454.                  * If it is on a window name -- exit
  455.                 */
  456.                 if (wn_isonnamew(rhandle,x,y)) {
  457.  
  458.                     /* restore the background screen */
  459.                         wn_restorescr();
  460.  
  461.                     /* restore the cursor position */
  462.                     v_gotoxy(oldx,oldy);
  463.  
  464.                     /* restore the cursor shape */
  465.                     v_curshape(oldb, olde);
  466.  
  467.                     exit(0);
  468.                 }
  469.  
  470.                 /* We handle single clicks on scroll bars in the same way */
  471.                                 if (wn_isonsbu(rhandle, 0, x, y)) {
  472.                     wn_scrollvs(rhandle,0,0,1);
  473.                 }
  474.                                 if (wn_isonsbl(rhandle, 0, x, y)) {
  475.                     wn_scrollvs(rhandle,0,1,0);
  476.                 }
  477.                                 if (wn_isonsbr(rhandle, 0, x, y)) {
  478.                     wn_scrollvs(rhandle,0,-1,0);
  479.                 }
  480.                                 if (wn_isonsbd(rhandle, 0, x, y)) {
  481.                     wn_scrollvs(rhandle,0,0,-1);
  482.                 }
  483.             }
  484.         }
  485.     }
  486.     getch();
  487.  
  488.     /*
  489.  
  490.     /* restore the background screen */
  491.         wn_restorescr();
  492.  
  493.     /* restore the cursor position */
  494.     v_gotoxy(oldx,oldy);
  495.  
  496.     /* restore the cursor shape */
  497.     v_curshape(oldb, olde);
  498.  
  499. }
  500.