home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / mouspp30.zip / MICEDEMO.CPP < prev    next >
C/C++ Source or Header  |  1992-02-23  |  12KB  |  430 lines

  1. /* -------------------------------------------------------------------- */
  2. /* Mouse++ Version 3.0           micedemo.cpp          Revised 02/21/92 */
  3. /*                                                                      */
  4. /* Demonstration of a general mouse class for Turbo C++/Borland C++.    */
  5. /* Copyright 1991, 1992 by Carl W. Moreland                             */
  6. /* This source code may be freely distributed as long as the copyright  */
  7. /* notice remains intact.                                               */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #include <dos.h>
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <graphics.h>
  14. #include "mouse.h"
  15. #include "cursor.h"
  16.  
  17. void textdemo(void);
  18. int  textscreen(void);
  19. void nexttdemo(TextCursor&, char *);
  20. void graphicdemo(void);
  21. int  graphicscreen(void);
  22. void nextgdemo (GraphicsCursor&, char *);
  23.  
  24. int maxx, maxy;
  25. int text_width, text_height;
  26. struct text_info t_info;
  27. int done = 0;
  28.  
  29. main()
  30. {
  31.   if(mouse.Exists())                // check for mouse
  32.   {
  33.     mouse.InstallHandler(0xff);        // install event handler
  34.     mouse.Enable();            // enable mouse
  35.     textdemo();                // run text mode demo
  36.     graphicdemo();            // run graphics mode demo
  37.     return(0);                // normal termination
  38.   }
  39.   return(1);                // abnormal termination
  40. }
  41.  
  42. /* ----- text mode demo ----------------------------------------------- */
  43.  
  44. void textdemo()                // main routine for text mode demo
  45. {
  46.   if(textscreen() == 0)            // draw the screen
  47.   {
  48.     textcolor(13);
  49.     gotoxy(1,3);
  50.  
  51.     if(mouse.Info.type == 1) cprintf(" Bus mouse found\n");
  52.     if(mouse.Info.type == 2) cprintf(" Serial mouse found\n");
  53.     if(mouse.Info.type == 3) cprintf(" InPort mouse found\n");
  54.     if(mouse.Info.type == 4) cprintf(" PS/2 mouse found\n");
  55.     if(mouse.Info.type == 5) cprintf(" HP mouse found\n");
  56.     gotoxy(1,4);
  57.     cprintf(" Software version is %d.%d\n", (int)mouse.Info.majorvers,
  58.               (int)mouse.Info.minorvers);
  59.  
  60.     mouse.yLimit(0, 199);        // set y limit
  61.     mouse.Show();            // initial turn-on
  62.  
  63.     if(!done) nexttdemo(tdef, "     Default Cursor      ");
  64.     if(!done) nexttdemo(txt1, " 0000h 0E0Fh Soft Cursor ");
  65.     if(!done) nexttdemo(txt2, " 7000h 0E0Fh Soft Cursor ");
  66.     if(!done) nexttdemo(txt3, " 7F00h FF0Fh Soft Cursor ");
  67.     mouse.xLimit(16, 623);
  68.     if(!done) nexttdemo(txt4, " 00FFh 0FD4h Soft Cursor ");
  69.  
  70.     textmode(C4350);            // 43/50 line mode
  71.     _setcursortype(_NOCURSOR);        // turn the hardware cursor off
  72.     mouse.xLimit(0, 639);        // set x limit
  73.     gettextinfo(&t_info);
  74.     if(t_info.screenheight == 43)
  75.       mouse.yLimit(0, 349);        // set y limit to EGA 43 line
  76.     else
  77.       mouse.yLimit(0, 399);        // set y limit to VGA 50 line
  78.     if(!done) nexttdemo(tdef, "     Default Cursor      ");
  79.   }
  80. }
  81.  
  82. int textscreen()            // draws screen for demo
  83. {
  84.   int i, j;
  85.   char far *video;
  86.   int video_seg = 0xB800,        // pointer to video memory
  87.       video_off = 0x0000;
  88.  
  89.   textmode(C80);            // 80x25 color text mode
  90.   video = (char far *)MK_FP(video_seg, video_off);
  91.  
  92.   clrscr();                // clear screen
  93.   _setcursortype(_NOCURSOR);        // turn the hardware cursor off
  94.  
  95.   for(i=0; i<50; i++)            // draw the text screen background
  96.   {
  97.     for(j=0; j<80; j++)
  98.     {
  99.       *video++ = 0xDB+2*(j/10);        // write the character to video memory
  100.       *video++ = 16*(j/10)+j/10+1;    // write the color
  101.     }
  102.   }
  103.  
  104.   gotoxy(1,1);
  105.   textattr(RED);
  106.   cprintf(" Mouse Enabled... ");
  107.   gotoxy(27,1);
  108.   textattr(4);
  109.   cprintf(" Text Mode Demonstration ");
  110.  
  111.   textattr(LIGHTGRAY);
  112.   for(i=5; i<13; i++)
  113.   {
  114.     gotoxy(5, i);
  115.     cprintf("██████████");
  116.   }
  117.   textattr(DARKGRAY);
  118.   for(i=6; i<8; i++)
  119.   {
  120.     gotoxy(6, i);
  121.     cprintf("██");
  122.     gotoxy(9, i);
  123.     cprintf("██");
  124.     gotoxy(12, i);
  125.     cprintf("██");
  126.   }
  127.   textattr(WHITE);
  128.   gotoxy(6,9);
  129.   cprintf("Position");
  130.  
  131.   textattr(LIGHTBLUE);
  132.   gotoxy(37,22); cprintf("╔══════╗");
  133.   gotoxy(37,23); cprintf("║ Next ║");
  134.   gotoxy(37,24); cprintf("╚══════╝");
  135.  
  136.   return(0);
  137. }
  138.  
  139. void nexttdemo(TextCursor& cursor, char *str)
  140. {
  141.   mouse.SetTextCursor(cursor);        // set text cursor
  142.   textattr(4);
  143.   gotoxy(27,2);
  144.   cprintf(str);                // print title
  145.   textattr(13);
  146.   for(;;)
  147.   {
  148.     mouse.GetEvent();
  149.     textattr(WHITE);
  150.     gotoxy(6,10);
  151.     cprintf(" %2d  %2d ", mouse.xPos() >> 3, mouse.yPos() >> 3);
  152.  
  153.     (mouse.LB_Dn()) ? textattr(RED) :
  154.                       textattr(DARKGRAY);
  155.     gotoxy(6,6);
  156.     cprintf("██");
  157.     gotoxy(6,7);
  158.     cprintf("██");
  159.     (mouse.CB_Dn()) ? textattr(RED) :
  160.                       textattr(DARKGRAY);
  161.     gotoxy(9,6);
  162.     cprintf("██");
  163.     gotoxy(9,7);
  164.     cprintf("██");
  165.  
  166.     (mouse.RB_Dn()) ? textattr(RED) :
  167.                       textattr(DARKGRAY);
  168.     gotoxy(12,6);
  169.     cprintf("██");
  170.     gotoxy(12,7);
  171.     cprintf("██");
  172.  
  173.     if(mouse.Released(LEFTBUTTON))    // check for LB release
  174.       if(mouse.InBox(36*8, 21*8, 43*8, 23*8))
  175.         return;
  176.  
  177.     if(mouse.DoubleClick(LEFTBUTTON))
  178.     {
  179.       mouse.ClearClick(LEFTBUTTON);
  180.       return;
  181.     }
  182.  
  183.     if(mouse.DoubleClick(RIGHTBUTTON))
  184.     {
  185.       mouse.Disable();
  186.       gotoxy(1,1);
  187.       textattr(RED + BLINK);
  188.       cprintf(" Hit any key...   ");
  189.  
  190.       while(!getch());
  191.  
  192.       gotoxy(1,1);
  193.       textattr(RED);
  194.       cprintf(" Mouse Enabled... ");
  195.       mouse.Enable();
  196.       mouse.Show();
  197.     }
  198.  
  199.     if(mouse.Pressed(LEFTBUTTON) && (mouse.Button() & CTRL_PRESSED))
  200.     {
  201.       done = 1;
  202.       return;
  203.     }
  204.   }
  205. }
  206.  
  207. /* ----- graphics mode demo ------------------------------------------- */
  208.  
  209. void graphicdemo()            // main routine for graphics demo
  210. {
  211.   if(graphicscreen() == 0)        // draw the screen
  212.   {
  213.     done = 0;
  214.     mouse.yLimit(0, maxy);        // set y limit (EGA=350, VGA=480)
  215.     mouse.Show();            // initial turn on
  216.  
  217.     mouse.SetSpeedThreshold(32);
  218.     mouse.MickToPix(8, 8);
  219.     if(!done) nextgdemo(gdef,     " Default ");
  220.     if(!done) nextgdemo(cross,    "  Cross  ");
  221.     if(!done) nextgdemo(check,    "  Check  ");
  222.     if(!done) nextgdemo(plus,     "  Plus   ");
  223.     if(!done) nextgdemo(gun,      "   Gun   ");
  224.     if(!done) nextgdemo(hand,     "  Hand   ");
  225.     if(!done) nextgdemo(hourglass,"Hourglass");
  226.     if(!done) nextgdemo(bullseye, " Bullseye");
  227.     mouse.MickToPix(4, 4);
  228.     if(!done) nextgdemo(ibeam,    "  IBeam  ");
  229.     mouse.MickToPix(2, 2);
  230.     if(!done) nextgdemo(jet,      "   Jet   ");
  231.     mouse.MickToPix(16, 16);
  232.     mouse.Move(maxx/2, maxy/2);
  233.     if(!done) nextgdemo(face,     "  Face   ");
  234.  
  235.     closegraph();
  236.   }
  237. }
  238.  
  239. int graphicscreen()
  240. {
  241.   int x, y;
  242.   int  driver = EGA, mode = EGAHI;
  243.  
  244.   initgraph(&driver, &mode, "");
  245.   if(graphresult() == grOk)
  246.   {
  247.     maxx = getmaxx();
  248.     maxy = getmaxy();
  249.     text_width  = textwidth("O");
  250.     text_height = textheight("O");
  251.  
  252.     setcolor(RED);            // paint background
  253.     rectangle(0, 0, maxx/3, maxy);
  254.     setfillstyle(SOLID_FILL, RED);
  255.     floodfill(maxx/6, maxy/2, RED);
  256.     setcolor(GREEN);
  257.     rectangle(maxx/3, 0, 2*maxx/3, maxy);
  258.     setfillstyle(SOLID_FILL, GREEN);
  259.     floodfill(maxx/2, maxy/2, GREEN);
  260.     setcolor(BLUE);
  261.     rectangle(2*maxx/3, 0, maxx, maxy);
  262.     setfillstyle(SOLID_FILL, BLUE);
  263.     floodfill(5*maxx/6, maxy/2, BLUE);
  264.  
  265.     x = (maxx/2)-75;            // center white block
  266.     y = (maxy/2)-75;
  267.     setcolor(WHITE);
  268.     rectangle(x, y, x+150, y+150);
  269.     setfillstyle(SOLID_FILL, WHITE);
  270.     floodfill(x+10, y+10, WHITE);
  271.  
  272.     x = (maxx/2)-40;                    // center black box
  273.     y = (maxy/2)-40;
  274.     setcolor(BLACK);
  275.     rectangle(x, y, x+80, y+80);
  276.     setfillstyle(SOLID_FILL, BLACK);
  277.     floodfill(maxx/2, maxy/2, BLACK);
  278.  
  279.     setcolor(BLACK);            // "Next" box
  280.     moveto(37*text_width, 40*text_height);
  281.     outtext("████████");
  282.     moveto(37*text_width, 41*text_height);
  283.     outtext("████████");
  284.     moveto(37*text_width, 42*text_height);
  285.     outtext("████████");
  286.     setcolor(WHITE);
  287.     moveto(37*text_width, 40*text_height);
  288.     outtext("╔══════╗");
  289.     moveto(37*text_width, 41*text_height);
  290.     outtext("║ Next ║");
  291.     moveto(37*text_width, 42*text_height);
  292.     outtext("╚══════╝");
  293.  
  294.     setfillstyle(SOLID_FILL, LIGHTGRAY);// draw mouse and buttons
  295.     setcolor(LIGHTGRAY);
  296.     rectangle(40, 40, 140, 140);
  297.     floodfill(90, 90, LIGHTGRAY);
  298.     setfillstyle(SOLID_FILL, DARKGRAY);
  299.     setcolor(DARKGRAY);
  300.     rectangle(50, 60, 70, 80);
  301.     floodfill(60, 70, DARKGRAY);
  302.     rectangle(80, 60, 100, 80);
  303.     floodfill(90, 70, DARKGRAY);
  304.     rectangle(110, 60, 130, 80);
  305.     floodfill(120, 70, DARKGRAY);
  306.     setcolor(BLACK);
  307.     moveto(60, 100);
  308.     outtext("Position");
  309.  
  310.     return(0);
  311.   }
  312.   return(1);
  313. }
  314.  
  315. void nextgdemo(GraphicsCursor& cursor, char *str)
  316. {
  317.   int LBDN = 0, CBDN = 0, RBDN = 0;
  318.   char str2[10];
  319.   int color;
  320.  
  321.   mouse.SetGraphicsCursor(cursor);    // set graphics cursor
  322.   setcolor(BLACK);            // print label
  323.   moveto((maxx/2-5*text_width),text_height);
  324.   outtext("█████████");
  325.   setcolor(WHITE);
  326.   moveto((maxx/2-5*text_width),text_height);
  327.   outtext(str);
  328.  
  329.   mouse.ClearBuffer();
  330.   mouse.ClearEvent();
  331.  
  332.   for(;;)
  333.   {
  334.     mouse.GetEvent();
  335.                     // set exclusion area
  336.     mouse.Exclude(40, 40, 140, 140);
  337.                                   // see if mouse has moved
  338.     if(mouse.xCount() || mouse.yCount())
  339.     {
  340.       sprintf(str2, "%3d %3d", mouse.xPos(), mouse.yPos());
  341.       setcolor(LIGHTGRAY);
  342.       moveto(60, 108);
  343.       outtext("███████");
  344.       setcolor(BLACK);
  345.       moveto(60, 108);
  346.       outtext(str2);            // print cursor position
  347.     }
  348.                     // check all button status for change
  349.     if((mouse.LB_Dn() && !LBDN) || (!mouse.LB_Dn() && LBDN))
  350.     {
  351.       color = LBDN ? DARKGRAY : RED;
  352.       if(mouse.MultiClick(LEFTBUTTON) == 2) color = BLUE;
  353.       if(mouse.MultiClick(LEFTBUTTON) == 3) color = GREEN;
  354.       if(mouse.MultiClick(LEFTBUTTON) == 4) color = YELLOW;
  355.  
  356.       setcolor(color);
  357.       setfillstyle(SOLID_FILL, color);
  358.       rectangle(50, 60, 70, 80);
  359.       floodfill(60, 70, color);
  360.       LBDN = !LBDN;
  361.     }
  362.     if((mouse.CB_Dn() && !CBDN) || (!mouse.CB_Dn() && CBDN))
  363.     {
  364.       color = CBDN ? DARKGRAY : RED;
  365.       if(mouse.MultiClick(CENTERBUTTON) == 2)
  366.       {
  367.         if(mouse.CB_Dn())
  368.           color = BLUE;
  369.       }
  370.       if(mouse.MultiClick(CENTERBUTTON) == 3)
  371.       {
  372.         if(mouse.CB_Dn())
  373.           color = GREEN;
  374.       }
  375.       if(mouse.MultiClick(CENTERBUTTON) == 4)
  376.       {
  377.         color = YELLOW;
  378.         mouse.ClearClick(CENTERBUTTON);
  379.       }
  380.  
  381.       setcolor(color);
  382.       setfillstyle(SOLID_FILL, color);
  383.       rectangle(80, 60, 100, 80);
  384.       floodfill(90, 70, color);
  385.       CBDN = !CBDN;
  386.     }
  387.     if((mouse.RB_Dn() && !RBDN) || (!mouse.RB_Dn() && RBDN))
  388.     {
  389.       color = RBDN ? DARKGRAY : RED;
  390.       if(mouse.MultiClick(RIGHTBUTTON) == 2) color = BLUE;
  391.       if(mouse.MultiClick(RIGHTBUTTON) == 3) color = GREEN;
  392.       if(mouse.MultiClick(RIGHTBUTTON) == 4) color = YELLOW;
  393.  
  394.       setcolor(color);
  395.       setfillstyle(SOLID_FILL, color);
  396.       rectangle(110, 60, 130, 80);
  397.       floodfill(120, 70, color);
  398.       RBDN = !RBDN;
  399.     }
  400.  
  401.     if(mouse.Released(LEFTBUTTON))    // check for LB release
  402.     {
  403.       if(mouse.InBox(37*text_width, 40*text_height,
  404.                    45*text_width, 43*text_height))
  405.       {
  406.         if(LBDN)            // check for LBDN status
  407.         {
  408.           setcolor(DARKGRAY);       // dehilite mouse button
  409.           setfillstyle(SOLID_FILL, DARKGRAY);
  410.           rectangle(50, 60, 70, 80);
  411.           floodfill(60, 70, DARKGRAY);
  412.         }
  413.         return;
  414.       }
  415.     }
  416.  
  417.     if(mouse.MultiClick(LEFTBUTTON) == 3)
  418.     {
  419.       mouse.ClearClick(LEFTBUTTON);
  420.       return;
  421.     }
  422.  
  423.     if(mouse.Pressed(LEFTBUTTON) && (mouse.Button() & CTRL_PRESSED))
  424.     {
  425.       done = 1;
  426.       return;
  427.     }
  428.   }
  429. }
  430.