home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / code_examples / cmanual_456 / idcmp / example2.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  6KB  |  183 lines

  1. /* Example2                                                   */
  2. /* This program explains how to use the IDCMP flag MOUSEMOVE. */
  3.  
  4.  
  5.  
  6. #include <intuition/intuition.h>
  7.  
  8.  
  9.  
  10. struct IntuitionBase *IntuitionBase;
  11.  
  12.  
  13.  
  14. /* Declare a pointer to a Window structure: */ 
  15. struct Window *my_window;
  16.  
  17. /* Declare and initialize your NewWindow structure: */
  18. struct NewWindow my_new_window=
  19. {
  20.   50,            /* LeftEdge    x position of the window. */
  21.   25,            /* TopEdge     y positio of the window. */
  22.   320,           /* Width       320 pixels wide. */
  23.   100,           /* Height      100 lines high. */
  24.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  25.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  26.   CLOSEWINDOW|   /* IDCMPFlags  We will recieve a message when the user:  */
  27.                  /*             selects the Close window gad, or when the */
  28.   MOUSEMOVE,     /*             user moves the mouse.                     */
  29.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  30.   WINDOWCLOSE|   /*             Close Gadget. */
  31.   WINDOWDRAG|    /*             Drag gadget. */
  32.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  33.   WINDOWSIZING|  /*             Sizing Gadget. */
  34.   ACTIVATE|      /*             The window should be Active when opened. */
  35.   
  36.   REPORTMOUSE,   /*             Create MOUSEMOVE messages whenever this */
  37.                  /*             window is active and the mouse is moved. */
  38.  
  39.   NULL,          /* FirstGadget No gadgets connected to this window. */
  40.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  41.   "MOVE THE MOUSE", /* Title    Title of the window. */
  42.   NULL,          /* Screen      Connected to the Workbench Screen. */
  43.   NULL,          /* BitMap      No Custom BitMap. */
  44.   100,           /* MinWidth    We will not allow the window to become */
  45.   50,            /* MinHeight   smaller than 100 x 50, and not bigger */
  46.   400,           /* MaxWidth    than 400 x 200. */
  47.   200,           /* MaxHeight */
  48.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  49. };
  50.  
  51.  
  52.  
  53. /***********************************************************************/
  54. /* Extra information:                                                  */
  55. /* If we set the IDCMP flag MOUSEMOVE then we tell Intuition that we   */
  56. /* are interested in MOUSEMOVE events. However, we must tell Intuition */
  57. /* how and when these messages should be created. There exist two      */
  58. /* ways to do it:                                                      */
  59. /* 1. Set the flag FOLLOWMOUSE in the Activation field in the Gadget   */
  60. /*    structure. We will then recieve messages whenever the gadget is  */
  61. /*    selected and the mouse is moved.                                 */
  62. /* 2. Set the flag REPORTMOUSE in the Flag field in the NewWindow      */
  63. /*    structure. We will then recieve messages whenever the window is  */
  64. /*    active and the mouse is moved. (Showed in this example.)         */
  65. /***********************************************************************/
  66.  
  67.  
  68.  
  69. main()
  70. {
  71.   /* Boolean variable used for the while loop: */
  72.   BOOL close_me;
  73.  
  74.   ULONG class; /* IDCMP flag. */
  75.  
  76.   SHORT x, y;  /* Position of the mouse (x,y). */
  77.  
  78.   BOOL mouse_moved;
  79.  
  80.   /* Pointer to an IntuiMessage structure: */
  81.   struct IntuiMessage *my_message;
  82.  
  83.  
  84.  
  85.   /* Before we can use Intuition we need to open the Intuition Library: */
  86.   IntuitionBase = (struct IntuitionBase *)
  87.     OpenLibrary( "intuition.library", 0 );
  88.   
  89.   if( IntuitionBase == NULL )
  90.     exit(); /* Could NOT open the Intuition Library! */
  91.  
  92.  
  93.  
  94.   /* We will now try to open the window: */
  95.   my_window = (struct Window *) OpenWindow( &my_new_window );
  96.   
  97.   /* Have we opened the window succesfully? */
  98.   if(my_window == NULL)
  99.   {
  100.     /* Could NOT open the Window! */
  101.     
  102.     /* Close the Intuition Library since we have opened it: */
  103.     CloseLibrary( IntuitionBase );
  104.  
  105.     exit();  
  106.   }
  107.  
  108.  
  109.  
  110.   /* We have opened the window, and everything seems to be OK. */
  111.  
  112.   printf("Move the mouse!\n");
  113.  
  114.  
  115.  
  116.   close_me = FALSE;
  117.  
  118.   /* Stay in the while loop until the user has selected the Close window */
  119.   /* gadget: */
  120.   while( close_me == FALSE )
  121.   {
  122.     mouse_moved = FALSE;
  123.  
  124.  
  125.     /* Wait until we have recieved a message: */
  126.     Wait( 1 << my_window->UserPort->mp_SigBit );
  127.  
  128.     /* As long as we can collect messages successfully we stay in the */
  129.     /* while-loop: */
  130.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  131.     {
  132.       /* After we have successfully collected the message we can read */
  133.       /* it, and save any important values which we maybe want to check */
  134.       /* later: */
  135.       class = my_message->Class;  /* IDCMP flag. */
  136.       x     = my_message->MouseX; /* X position of the mouse. */
  137.       y     = my_message->MouseY; /* Y position of the mouse. */
  138.  
  139.  
  140.       /* After we have read it we reply as fast as possible: */
  141.       /* REMEMBER! Do never try to read a message after you have replied! */
  142.       /* (Some other process has maybe changed it.) */
  143.       ReplyMsg( my_message );
  144.  
  145.  
  146.       /* Check which IDCMP flag was sent: */
  147.       switch( class )
  148.       {
  149.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  150.                close_me=TRUE;
  151.                break;
  152.         
  153.         case MOUSEMOVE:    /* The user moved the mouse. */
  154.                mouse_moved = TRUE;
  155.                break;
  156.       }
  157.     }
  158.     
  159.     if( mouse_moved )
  160.     {
  161.       /* Since we recieve so many messages when the mouse is moved, we */
  162.       /* respond first when the mouse has halted. Print out the mouse  */
  163.       /* position relative to the top left corner of the window.       */
  164.       printf("New position: (%d, %d)\n", x, y);
  165.     }
  166.   }
  167.  
  168.  
  169.  
  170.   /* Close the window: */
  171.   CloseWindow( my_window );
  172.  
  173.  
  174.  
  175.   /* Close the Intuition Library: */
  176.   CloseLibrary( IntuitionBase );
  177.   
  178.   /* THE END */
  179. }
  180.  
  181.  
  182.  
  183.