home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / RKM_Companion_v2.04 / Intuition / Mouse_Keyboard / mousetest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  8.9 KB  |  275 lines

  1. ;/* mousetest.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 mousetest.c
  3. Blink FROM LIB:c.o,mousetest.o TO mousetest LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992-1999 Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /*
  34. ** mousetest.c - Read position and button events from the mouse.
  35. */
  36. #define INTUI_V36_NAMES_ONLY
  37.  
  38. #include <exec/types.h>
  39. #include <intuition/intuition.h>
  40. #include <graphics/gfxbase.h>
  41. #include <devices/inputevent.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/graphics_protos.h>
  45. #include <clib/intuition_protos.h>
  46. #include <clib/macros.h>
  47.  
  48. #include <stdio.h>
  49.  
  50. #ifdef LATTICE
  51. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  52. int chkabort(void) { return(0); }  /* really */
  53. #endif
  54.  
  55. #define BUFSIZE 16
  56.  
  57. /* something to use to track the time between messages
  58. ** to test for double-clicks.
  59. */
  60. typedef struct myTimeVal
  61.     {
  62.     ULONG LeftSeconds;
  63.     ULONG LeftMicros;
  64.     ULONG RightSeconds;
  65.     ULONG RightMicros;
  66.     } MYTIMEVAL;
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. /* our function prototypes */
  76. VOID doButtons(struct IntuiMessage *msg, MYTIMEVAL *tv);
  77. VOID process_window(struct Window *win);
  78.  
  79. struct Library *IntuitionBase;
  80. struct GfxBase       *GfxBase;  /* we need GfxBase->DefaultFont */
  81.  
  82.  
  83. /*
  84. ** main() -- set-up everything.
  85. */
  86. VOID main(int argc, char **argv)
  87. {
  88. struct Window *win;
  89. struct Screen *scr;
  90. struct DrawInfo *dr_info;
  91. ULONG width;
  92.  
  93. /* Open the libraries we will use.  Requires Release 2 (KS V2.04, V37) */
  94. if (IntuitionBase = OpenLibrary("intuition.library",37))
  95.     {
  96.     if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37))
  97.         {
  98.         /* Lock the default public screen in order to read its DrawInfo data */
  99.         if (scr = LockPubScreen(NULL))
  100.             {
  101.             if (dr_info = GetScreenDrawInfo(scr))
  102.                 {
  103.                 /* use wider of space needed for output (18 chars and spaces)
  104.                  * or titlebar text plus room for titlebar gads (approx 18 each)
  105.                  */
  106.                 width = MAX((GfxBase->DefaultFont->tf_XSize * 18),
  107.                             (18 * 2) + TextLength(&scr->RastPort,"MouseTest",9));
  108.  
  109.                 if (win = OpenWindowTags(NULL,
  110.                             WA_Top,    20,
  111.                             WA_Left,   100,
  112.                             WA_InnerWidth,  width,
  113.                             WA_Height, (2 * GfxBase->DefaultFont->tf_YSize) +
  114.                                        scr->WBorTop + scr->Font->ta_YSize + 1 +
  115.                                        scr->WBorBottom,
  116.                             WA_Flags, WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  117.                                       WFLG_ACTIVATE    | WFLG_REPORTMOUSE |
  118.                                       WFLG_RMBTRAP     | WFLG_DRAGBAR,
  119.                             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_RAWKEY |
  120.                                       IDCMP_MOUSEMOVE   | IDCMP_MOUSEBUTTONS,
  121.                             WA_Title, "MouseTest",
  122.                             WA_PubScreen, scr,
  123.                             TAG_END))
  124.                     {
  125.                     printf("Monitors the Mouse:\n");
  126.                     printf("    Move Mouse, Click and DoubleClick in Window\n");
  127.  
  128.                     SetAPen(win->RPort,dr_info->dri_Pens[TEXTPEN]);
  129.                     SetBPen(win->RPort,dr_info->dri_Pens[BACKGROUNDPEN]);
  130.                     SetDrMd(win->RPort,JAM2);
  131.  
  132.                     process_window(win);
  133.  
  134.                     CloseWindow(win);
  135.                     }
  136.                 FreeScreenDrawInfo(scr, dr_info);
  137.                 }
  138.             UnlockPubScreen(NULL,scr);
  139.             }
  140.         CloseLibrary((struct Library *)GfxBase);
  141.         }
  142.     CloseLibrary(IntuitionBase);
  143.     }
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. /*
  152. ** process_window() - simple message loop for processing IntuiMessages
  153. */
  154. VOID process_window(struct Window *win)
  155. {
  156. USHORT done;
  157. struct IntuiMessage *msg;
  158. MYTIMEVAL tv;
  159. UBYTE prt_buff[14];
  160. LONG xText, yText;  /* places to position text in window. */
  161.  
  162. done = FALSE;
  163. tv.LeftSeconds = 0; /* initial values for testing double-click */
  164. tv.LeftMicros  = 0;
  165. tv.RightSeconds = 0;
  166. tv.RightMicros  = 0;
  167. xText = win->BorderLeft + (win->IFont->tf_XSize * 2);
  168. yText = win->BorderTop + 3 + win->IFont->tf_Baseline;
  169.  
  170. while (!done)
  171.     {
  172.     Wait((1L<<win->UserPort->mp_SigBit));
  173.  
  174.     while ((!done) &&
  175.            (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  176.         {
  177.         switch (msg->Class)
  178.             {
  179.             case IDCMP_CLOSEWINDOW:
  180.                 done = TRUE;
  181.                 break;
  182.             /* NOTE NOTE NOTE:  If the mouse queue backs up a lot, Intuition
  183.             ** will start dropping MOUSEMOVE messages off the end until the
  184.             ** queue is serviced.  This may cause the program to lose some
  185.             ** of the MOUSEMOVE events at the end of the stream.
  186.             **
  187.             ** Look in the window structure if you need the true position
  188.             ** of the mouse pointer at any given time.  Look in the
  189.             ** MOUSEBUTTONS message if you need position when it clicked.
  190.             ** An alternate to this processing would be to set a flag that
  191.             ** a mousemove event arrived, then print the position of the
  192.             ** mouse outside of the "while (GetMsg())" loop.  This allows
  193.             ** a single processing call for many mouse events, which speeds
  194.             ** up processing A LOT!  Something like:
  195.             **
  196.             ** while (GetMsg())
  197.             **    {
  198.             **    if (class == IDCMP_MOUSEMOVE)
  199.             **        mouse_flag = TRUE;
  200.             **    ReplyMsg();   NOTE: copy out all needed fields first !
  201.             **    }
  202.             ** if (mouse_flag)
  203.             **    {
  204.             **    process_mouse_event();
  205.             **    mouse_flag = FALSE;
  206.             **    }
  207.             **
  208.             ** You can also use IDCMP_INTUITICKS for slower paced messages
  209.             ** (all messages have mouse coordinates.)
  210.             */
  211.             case IDCMP_MOUSEMOVE:
  212.                 /* Show the current position of the mouse relative to the
  213.                 ** upper left hand corner of our window
  214.                 */
  215.                 Move(win->RPort,xText,yText);
  216.                 sprintf(prt_buff, "X%5d Y%5d", msg->MouseX, msg->MouseY);
  217.                 Text(win->RPort,prt_buff,13);
  218.                 break;
  219.             case IDCMP_MOUSEBUTTONS:
  220.                 doButtons(msg,&tv);
  221.                 break;
  222.             }
  223.         ReplyMsg((struct Message *)msg);
  224.         }
  225.     }
  226. }
  227.  
  228. /*
  229. ** Show what mouse buttons where pushed
  230. */
  231. VOID doButtons(struct IntuiMessage *msg, MYTIMEVAL *tv)
  232. {
  233. /* Yes, qualifiers can apply to the mouse also.  That is how
  234. ** we get the shift select on the Workbench.  This shows how
  235. ** to see if a specific bit is set within the qualifier
  236. */
  237. if (msg->Qualifier & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  238.     printf("Shift ");
  239.  
  240. switch (msg->Code)
  241.     {
  242.     case SELECTDOWN:
  243.         printf("Left Button Down at X%ld Y%ld", msg->MouseX, msg->MouseY);
  244.         if(DoubleClick(tv->LeftSeconds, tv->LeftMicros, msg->Seconds, msg->Micros))
  245.             printf(" DoubleClick!");
  246.         else
  247.             {
  248.             tv->LeftSeconds = msg->Seconds;
  249.             tv->LeftMicros  = msg->Micros;
  250.             tv->RightSeconds = 0;
  251.             tv->RightMicros  = 0;
  252.             }
  253.         break;
  254.     case SELECTUP:
  255.         printf("Left Button Up   at X%ld Y%ld", msg->MouseX, msg->MouseY);
  256.         break;
  257.     case MENUDOWN:
  258.         printf("Right Button down at X%ld Y%ld", msg->MouseX, msg->MouseY);
  259.         if(DoubleClick(tv->RightSeconds, tv->RightMicros, msg->Seconds, msg->Micros))
  260.             printf(" DoubleClick!");
  261.         else
  262.             {
  263.             tv->LeftSeconds = 0;
  264.             tv->LeftMicros  = 0;
  265.             tv->RightSeconds = msg->Seconds;
  266.             tv->RightMicros  = msg->Micros;
  267.             }
  268.         break;
  269.     case MENUUP:
  270.         printf("Right Button Up   at X%ld Y%ld", msg->MouseX, msg->MouseY);
  271.         break;
  272.     }
  273. printf("\n");
  274. }
  275.