home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / mousetest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  7.7 KB  |  235 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. ** mousetest.c - Read position and button events from the mouse.
  7. */
  8. #define INTUI_V36_NAMES_ONLY
  9.  
  10. #include <exec/types.h>
  11. #include <intuition/intuition.h>
  12. #include <graphics/gfxbase.h>
  13. #include <devices/inputevent.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/intuition_protos.h>
  18.  
  19. #include <stdio.h>
  20.  
  21. #ifdef LATTICE
  22. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  23. int chkabort(void) { return(0); }  /* really */
  24. #endif
  25.  
  26. #define BUFSIZE 16
  27.  
  28. /* something to use to track the time between messages
  29. ** to test for double-clicks.
  30. */
  31. typedef struct myTimeVal
  32.     {
  33.     ULONG LeftSeconds;
  34.     ULONG LeftMicros;
  35.     ULONG RightSeconds;
  36.     ULONG RightMicros;
  37.     } MYTIMEVAL;
  38.  
  39.  
  40. /* our function prototypes */
  41. VOID doButtons(struct IntuiMessage *msg, MYTIMEVAL *tv);
  42. VOID process_window(struct Window *win);
  43.  
  44. struct Library *IntuitionBase;
  45. struct GfxBase       *GfxBase;    /* we need GfxBase->DefaultFont */
  46.  
  47.  
  48. /*
  49. ** main() -- set-up everything.
  50. */
  51. VOID main(int argc, char **argv)
  52. {
  53. struct Window *win;
  54. struct Screen *scr;
  55. struct DrawInfo *dr_info;
  56. ULONG width;
  57.  
  58. /* Open the libraries we will use.  Requires Release 2 (KS V2.04, V37) */
  59. if (IntuitionBase = OpenLibrary("intuition.library",37))
  60.     {
  61.     if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37))
  62.         {
  63.         /* Lock the default public screen in order to read its DrawInfo data */
  64.         if (scr = LockPubScreen(NULL))
  65.             {
  66.             if (dr_info = GetScreenDrawInfo(scr))
  67.                 {
  68.                 /* use wider of space needed for output (18 chars and spaces)
  69.                  * or titlebar text plus room for titlebar gads (approx 18 each)
  70.                  */
  71.                 width = max((GfxBase->DefaultFont->tf_XSize * 18),
  72.                             (18 * 2) + TextLength(&scr->RastPort,"MouseTest",9));
  73.  
  74.                 if (win = OpenWindowTags(NULL,
  75.                             WA_Top,    20,
  76.                             WA_Left,   100,
  77.                             WA_InnerWidth,  width,
  78.                             WA_Height, (2 * GfxBase->DefaultFont->tf_YSize) +
  79.                                        scr->WBorTop + scr->Font->ta_YSize + 1 +
  80.                                        scr->WBorBottom,
  81.                             WA_Flags, WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  82.                                       WFLG_ACTIVATE    | WFLG_REPORTMOUSE |
  83.                                       WFLG_RMBTRAP     | WFLG_DRAGBAR,
  84.                             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_RAWKEY |
  85.                                       IDCMP_MOUSEMOVE   | IDCMP_MOUSEBUTTONS,
  86.                             WA_Title, "MouseTest",
  87.                             WA_PubScreen, scr,
  88.                             TAG_END))
  89.                     {
  90.                     printf("Monitors the Mouse:\n");
  91.                     printf("    Move Mouse, Click and DoubleClick in Window\n");
  92.  
  93.                     SetAPen(win->RPort,dr_info->dri_Pens[TEXTPEN]);
  94.                     SetBPen(win->RPort,dr_info->dri_Pens[BACKGROUNDPEN]);
  95.                     SetDrMd(win->RPort,JAM2);
  96.  
  97.                     process_window(win);
  98.  
  99.                     CloseWindow(win);
  100.                     }
  101.                 FreeScreenDrawInfo(scr, dr_info);
  102.                 }
  103.             UnlockPubScreen(NULL,scr);
  104.             }
  105.         CloseLibrary((struct Library *)GfxBase);
  106.         }
  107.     CloseLibrary(IntuitionBase);
  108.     }
  109. }
  110.  
  111. /*
  112. ** process_window() - simple message loop for processing IntuiMessages
  113. */
  114. VOID process_window(struct Window *win)
  115. {
  116. USHORT done;
  117. struct IntuiMessage *msg;
  118. MYTIMEVAL tv;
  119. UBYTE prt_buff[14];
  120. LONG xText, yText;  /* places to position text in window. */
  121.  
  122. done = FALSE;
  123. tv.LeftSeconds = 0; /* initial values for testing double-click */
  124. tv.LeftMicros  = 0;
  125. tv.RightSeconds = 0;
  126. tv.RightMicros  = 0;
  127. xText = win->BorderLeft + (win->IFont->tf_XSize * 2);
  128. yText = win->BorderTop + 3 + win->IFont->tf_Baseline;
  129.  
  130. while (!done)
  131.     {
  132.     Wait((1L<<win->UserPort->mp_SigBit));
  133.  
  134.     while ((!done) &&
  135.            (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  136.         {
  137.         switch (msg->Class)
  138.             {
  139.             case IDCMP_CLOSEWINDOW:
  140.                 done = TRUE;
  141.                 break;
  142.             /* NOTE NOTE NOTE:  If the mouse queue backs up a lot, Intuition
  143.             ** will start dropping MOUSEMOVE messages off the end until the
  144.             ** queue is serviced.  This may cause the program to lose some
  145.             ** of the MOUSEMOVE events at the end of the stream.
  146.             **
  147.             ** Look in the window structure if you need the true position
  148.             ** of the mouse pointer at any given time.  Look in the
  149.             ** MOUSEBUTTONS message if you need position when it clicked.
  150.             ** An alternate to this processing would be to set a flag that
  151.             ** a mousemove event arrived, then print the position of the
  152.             ** mouse outside of the "while (GetMsg())" loop.  This allows
  153.             ** a single processing call for many mouse events, which speeds
  154.             ** up processing A LOT!  Something like:
  155.             **
  156.             ** while (GetMsg())
  157.             **    {
  158.             **    if (class == IDCMP_MOUSEMOVE)
  159.             **        mouse_flag = TRUE;
  160.             **    ReplyMsg();   NOTE: copy out all needed fields first !
  161.             **    }
  162.             ** if (mouse_flag)
  163.             **    {
  164.             **    process_mouse_event();
  165.             **    mouse_flag = FALSE;
  166.             **    }
  167.             **
  168.             ** You can also use IDCMP_INTUITICKS for slower paced messages
  169.             ** (all messages have mouse coordinates.)
  170.             */
  171.             case IDCMP_MOUSEMOVE:
  172.                 /* Show the current position of the mouse relative to the
  173.                 ** upper left hand corner of our window
  174.                 */
  175.                 Move(win->RPort,xText,yText);
  176.                 sprintf(prt_buff, "X%5d Y%5d", msg->MouseX, msg->MouseY);
  177.                 Text(win->RPort,prt_buff,13);
  178.                 break;
  179.             case IDCMP_MOUSEBUTTONS:
  180.                 doButtons(msg,&tv);
  181.                 break;
  182.             }
  183.         ReplyMsg((struct Message *)msg);
  184.         }
  185.     }
  186. }
  187.  
  188. /*
  189. ** Show what mouse buttons where pushed
  190. */
  191. VOID doButtons(struct IntuiMessage *msg, MYTIMEVAL *tv)
  192. {
  193. /* Yes, qualifiers can apply to the mouse also.  That is how
  194. ** we get the shift select on the Workbench.  This shows how
  195. ** to see if a specific bit is set within the qualifier
  196. */
  197. if (msg->Qualifier & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  198.     printf("Shift ");
  199.  
  200. switch (msg->Code)
  201.     {
  202.     case SELECTDOWN:
  203.         printf("Left Button Down at X%ld Y%ld", msg->MouseX, msg->MouseY);
  204.         if(DoubleClick(tv->LeftSeconds, tv->LeftMicros, msg->Seconds, msg->Micros))
  205.             printf(" DoubleClick!");
  206.         else
  207.             {
  208.             tv->LeftSeconds = msg->Seconds;
  209.             tv->LeftMicros  = msg->Micros;
  210.             tv->RightSeconds = 0;
  211.             tv->RightMicros  = 0;
  212.             }
  213.         break;
  214.     case SELECTUP:
  215.         printf("Left Button Up   at X%ld Y%ld", msg->MouseX, msg->MouseY);
  216.         break;
  217.     case MENUDOWN:
  218.         printf("Right Button down at X%ld Y%ld", msg->MouseX, msg->MouseY);
  219.         if(DoubleClick(tv->RightSeconds, tv->RightMicros, msg->Seconds, msg->Micros))
  220.             printf(" DoubleClick!");
  221.         else
  222.             {
  223.             tv->LeftSeconds = 0;
  224.             tv->LeftMicros  = 0;
  225.             tv->RightSeconds = msg->Seconds;
  226.             tv->RightMicros  = msg->Micros;
  227.             }
  228.         break;
  229.     case MENUUP:
  230.         printf("Right Button Up   at X%ld Y%ld", msg->MouseX, msg->MouseY);
  231.         break;
  232.     }
  233. printf("\n");
  234. }
  235.