home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / MAXON_C3.DMS / in.adf / Demos / ANSI-C / Intuition / pointerdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  9.4 KB  |  387 lines

  1. /*
  2.  * pointerdemo.c - shows off new pointer features
  3.  *
  4.  * (c) Copyright 1992 Commodore-Amiga, Inc.  All Rights Reserved.
  5.  *
  6.  * This example shows off some of the V39 pointer features available
  7.  * through the boopsi pointerclass and through the new SetWindowPointer()
  8.  * call.  These features include AA-specific pointer features, as well
  9.  * as the standard busy pointer and the pointer-delay feature.  This
  10.  * feature is useful if the application may be busy for an indeterminate
  11.  * but possibly short period of time.  If the application restores the
  12.  * original pointer before a short time has elapsed, no busy pointer
  13.  * will appear.  This removes annoying short flashes of the busy pointer.
  14.  */
  15.  
  16. /*------------------------------------------------------------------------*/
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <intuition/intuition.h>
  21. #include <intuition/pointerclass.h>
  22. #include <intuition/gadgetclass.h>
  23. #include <libraries/gadtools.h>
  24.  
  25. #include <pragma/intuition_lib.h>
  26. #include <pragma/gadtools_lib.h>
  27. #include <pragma/graphics_lib.h>
  28. #include <pragma/exec_lib.h>
  29.  
  30. /*------------------------------------------------------------------------*/
  31.  
  32. void doPointerStuff( struct Window *win, ULONG code );
  33. void bail_out(long code);
  34. struct Gadget *createAllGadgets( struct Gadget **glistptr, void *vi,
  35.     struct Screen *sc );
  36. BOOL createPointers();
  37.  
  38. /*------------------------------------------------------------------------*/
  39.  
  40. struct NewWindow newwin =
  41. {
  42.     0,0,        /*  LeftEdge, TopEdge */
  43.     320,200,            /*  Width, Height */
  44.     -1, -1,             /*  DetailPen, BlockPen */
  45.     VANILLAKEY | CLOSEWINDOW | REFRESHWINDOW | BUTTONIDCMP,    /*  IDCMPFlags */
  46.     WINDOWDRAG | WINDOWSIZING | WINDOWDEPTH | WINDOWCLOSE |
  47.     WFLG_ACTIVATE|SIMPLE_REFRESH,    /* Flags */
  48.     NULL,        /*  FirstGadget */
  49.     NULL,        /*  CheckMark */
  50.     (UBYTE *) "New Pointer Demo",    /*  Title */
  51.     NULL,        /*  Screen */
  52.     NULL,        /*  BitMap */
  53.     100,50,        /*  MinWidth, MinHeight */
  54.     640,200,        /*  MaxWidth, MaxHeight */
  55.     WBENCHSCREEN,    /*  Type */
  56. };
  57.  
  58. /*------------------------------------------------------------------------*/
  59.  
  60. struct GfxBase *GfxBase = NULL;
  61. struct IntuitionBase *IntuitionBase = NULL;
  62. struct Library *GadToolsBase = NULL;
  63.  
  64. struct Screen *mysc = NULL;
  65. struct VisualInfo *vi = NULL;
  66. struct Gadget *glist = NULL;
  67.  
  68. struct Window *win = NULL;
  69. struct BitMap *bm = NULL;
  70. struct BitMap simplebm;
  71.  
  72. VOID *simple_pointer = NULL;
  73. VOID *wide_pointer = NULL;
  74.  
  75. #pragma chip
  76. UWORD simplePointer0[] =
  77. {
  78.     0x0180,    /* .......##....... */
  79.     0x0180,    /* .......##....... */
  80.     0x0180,    /* .......##....... */
  81.     0x0ff0,    /* ....########.... */
  82.     0x3ffc,    /* ..############.. */
  83.     0x7ffe,    /* .##############. */
  84.     0xffff,    /* ################ */
  85.     0xffff,    /* ################ */
  86.     0xffff,    /* ################ */
  87.     0xffff,    /* ################ */
  88.     0xffff,    /* ################ */
  89.     0xffff,    /* ################ */
  90.     0x7ffe,    /* .##############. */
  91.     0x7ffe,    /* .##############. */
  92.     0x3ffc,    /* ..############.. */
  93.     0x0ff0     /* ....########.... */
  94. };
  95.  
  96. UWORD simplePointer1[] =
  97. {
  98.     0x0000,    /* ................ */
  99.     0x0000,    /* ................ */
  100.     0x0000,    /* ................ */
  101.     0x0000,    /* ................ */
  102.     0x0000,    /* ................ */
  103.     0x0660,    /* .....##..##..... */
  104.     0x0440,    /* .....#...#...... */
  105.     0x0000,    /* ................ */
  106.     0x0000,    /* ................ */
  107.     0x0000,    /* ................ */
  108.     0x0000,    /* ................ */
  109.     0x0ef0,    /* ....###.####.... */
  110.     0x07e0,    /* .....######..... */
  111.     0x0340,    /* ......##.#...... */
  112.     0x0000,    /* ................ */
  113.     0x0000     /* ................ */
  114. };
  115.  
  116. #pragma fast
  117.  
  118. struct TextAttr topaz80 =
  119. {
  120.     "topaz.font",
  121.     8,
  122.     0,
  123.     0,
  124. };
  125.  
  126. /*------------------------------------------------------------------------*/
  127.  
  128. void main( void )
  129. {
  130.     BOOL terminated;
  131.     struct IntuiMessage *imsg;
  132.     struct Gadget *gad;
  133.  
  134.     terminated = FALSE;
  135.     win = NULL;
  136.  
  137.     if (!(GfxBase = (struct GfxBase *)
  138.     OpenLibrary("graphics.library",39L)))
  139.     bail_out(20);
  140.  
  141.     if (!(IntuitionBase = (struct IntuitionBase *)
  142.     OpenLibrary("intuition.library",39L)))
  143.     bail_out(20);
  144.  
  145.     if (!(GadToolsBase = OpenLibrary("gadtools.library",39L)))
  146.     bail_out(20);
  147.  
  148.     if ( !( mysc = LockPubScreen( NULL ) ) )
  149.     bail_out(20);
  150.  
  151.     if ( !( vi = GetVisualInfo( mysc, NULL ) ) )
  152.     bail_out(20);
  153.  
  154.     if ( ! createPointers() )
  155.     bail_out(20);
  156.  
  157.     if ( !( gad = createAllGadgets( &glist, vi, mysc ) ) )
  158.     bail_out(20);
  159.  
  160.     if (!(win = OpenWindowTags(&newwin,
  161.     WA_Pointer, simple_pointer,
  162.     WA_PubScreen, mysc,
  163.     WA_Gadgets, glist,
  164.     TAG_DONE)))
  165.     bail_out(20);
  166.  
  167.     while (!terminated)
  168.     {
  169.     Wait (1 << win->UserPort->mp_SigBit);
  170.     while (imsg = GT_GetIMsg(win->UserPort))
  171.     {
  172.         if (imsg->Class == CLOSEWINDOW)
  173.         terminated = TRUE;
  174.         else if (imsg->Class == REFRESHWINDOW)
  175.         {
  176.         GT_BeginRefresh(win);
  177.         GT_EndRefresh(win,TRUE);
  178.         }
  179.         else if (imsg->Class == GADGETUP)
  180.         {
  181.         doPointerStuff( win, ((struct Gadget *)imsg->IAddress)->GadgetID );
  182.         }
  183.         else if (imsg->Class == VANILLAKEY)
  184.         {
  185.         doPointerStuff( win, imsg->Code );
  186.         }
  187.         GT_ReplyIMsg(imsg);
  188.     }
  189.     }
  190.     bail_out(0);
  191. }
  192.  
  193.  
  194. /*------------------------------------------------------------------------*/
  195.  
  196. void doPointerStuff( struct Window *win, ULONG code )
  197. {
  198.     switch ( toupper(code) )
  199.     {
  200.     case 'C':    /* Clear pointer */
  201.     SetWindowPointer( win,
  202.         TAG_DONE );
  203.     break;
  204.  
  205.     case 'S':    /* Set custom */
  206.     SetWindowPointer( win,
  207.         WA_Pointer, simple_pointer,
  208.         TAG_DONE );
  209.     break;
  210.  
  211.     case 'W':    /* Set wide custom */
  212.     SetWindowPointer( win,
  213.         WA_Pointer, wide_pointer,
  214.         TAG_DONE );
  215.     break;
  216.  
  217.     case 'B':    /* Set Busy */
  218.     SetWindowPointer( win,
  219.         WA_BusyPointer, TRUE,
  220.         TAG_DONE );
  221.     break;
  222.  
  223.     case 'D':    /* Busy w/ delay */
  224.     SetWindowPointer( win,
  225.         WA_BusyPointer, TRUE,
  226.         WA_PointerDelay, TRUE,
  227.         TAG_DONE );
  228.     break;
  229.     }
  230. }
  231.  
  232.  
  233. /*------------------------------------------------------------------------*/
  234.  
  235. struct Gadget *createAllGadgets( struct Gadget **glistptr, void *vi,
  236.     struct Screen *sc )
  237. {
  238.     struct NewGadget ng;
  239.     struct Gadget *gad;
  240.  
  241.     gad = CreateContext( glistptr );
  242.  
  243.     ng.ng_LeftEdge = 20;
  244.     ng.ng_TopEdge = sc->WBorTop + sc->Font->ta_YSize + 11;
  245.     ng.ng_Width = 120;
  246.     ng.ng_Height = 14;
  247.     ng.ng_Flags = 0;
  248.     ng.ng_VisualInfo = vi;
  249.     ng.ng_TextAttr = &topaz80;
  250.     ng.ng_GadgetText = "_Clear Pointer";
  251.     ng.ng_GadgetID = 'C';
  252.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  253.     GT_Underscore, '_',
  254.     TAG_DONE );
  255.  
  256.     ng.ng_LeftEdge += 130;
  257.     ng.ng_GadgetText = "_Simple Pointer";
  258.     ng.ng_GadgetID = 'S';
  259.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  260.     GT_Underscore, '_',
  261.     TAG_DONE );
  262.  
  263.     ng.ng_TopEdge += 20;
  264.     ng.ng_LeftEdge = 20;
  265.     ng.ng_GadgetText = "_Wide Pointer";
  266.     ng.ng_GadgetID = 'W';
  267.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  268.     GT_Underscore, '_',
  269.     TAG_DONE );
  270.  
  271.     ng.ng_TopEdge += 20;
  272.     ng.ng_LeftEdge = 20;
  273.     ng.ng_GadgetText = "_Busy Pointer";
  274.     ng.ng_GadgetID = 'B';
  275.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  276.     GT_Underscore, '_',
  277.     TAG_DONE );
  278.  
  279.     ng.ng_LeftEdge += 130;
  280.     ng.ng_GadgetText = "_Delayed Busy";
  281.     ng.ng_GadgetID = 'D';
  282.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  283.     GT_Underscore, '_',
  284.     TAG_DONE );
  285.  
  286.     return( gad );
  287. }
  288.  
  289. /*------------------------------------------------------------------------*/
  290.  
  291. void bail_out(long code)
  292. {
  293.     if (win)
  294.     CloseWindow(win);
  295.  
  296.     if ( glist )
  297.     FreeGadgets( glist );
  298.  
  299.     if ( vi )
  300.     FreeVisualInfo( vi );
  301.  
  302.     if ( mysc )
  303.         UnlockPubScreen( NULL, mysc );
  304.  
  305.     if (wide_pointer)
  306.     DisposeObject(wide_pointer);
  307.  
  308.     if (simple_pointer)
  309.     DisposeObject(simple_pointer);
  310.  
  311.     if (bm)
  312.     FreeBitMap(bm);
  313.  
  314.     if (GadToolsBase)
  315.     CloseLibrary(( struct Library *)GadToolsBase);
  316.  
  317.     if (IntuitionBase)
  318.     CloseLibrary(( struct Library *)IntuitionBase);
  319.  
  320.     if (GfxBase)
  321.     CloseLibrary(( struct Library *)GfxBase);
  322.  
  323.     exit(code);
  324. }
  325.  
  326.  
  327. /*------------------------------------------------------------------------*/
  328.  
  329. #define PWORDWIDTH    4
  330. #define PHEIGHT        64
  331.  
  332. BOOL createPointers()
  333. {
  334.     struct RastPort rport;
  335.     int i;
  336.  
  337.     /* First, let's make a traditional bitmap, then make a
  338.      * pointer out of it:
  339.      */
  340.     InitBitMap( &simplebm, 2, 16, 16 );
  341.     simplebm.Planes[0] = (PLANEPTR)simplePointer0;
  342.     simplebm.Planes[1] = (PLANEPTR)simplePointer1;
  343.     simple_pointer = NewObject( NULL, "pointerclass",
  344.     POINTERA_BitMap, &simplebm,
  345.     POINTERA_XOffset, -6,
  346.     POINTERA_WordWidth, 1,
  347.     POINTERA_XResolution, POINTERXRESN_HIRES,
  348.     POINTERA_YResolution, POINTERYRESN_HIGH,
  349.     TAG_DONE );
  350.  
  351.     /* Now, we'll make a V39-style bitmap, and render into
  352.      * a RastPort we throw around it:
  353.      */
  354.  
  355.     if ( bm = AllocBitMap( 16*PWORDWIDTH, PHEIGHT, 2, BMF_CLEAR, NULL ) )
  356.     {
  357.     InitRastPort( &rport );
  358.     rport.BitMap = bm;
  359.  
  360.     SetAPen( &rport, 1 );
  361.     for ( i = 0; i < PHEIGHT; i++ )
  362.     {
  363.         Move( &rport, 0, i );
  364.         Draw( &rport, (i*(PWORDWIDTH*16))/PHEIGHT, i );
  365.     }
  366.  
  367.     SetAPen( &rport, 3 );
  368.     Move( &rport, 0 ,0 );
  369.     Draw( &rport, PWORDWIDTH*16-1, PHEIGHT-1 );
  370.     SetAPen( &rport, 2 );
  371.     Move( &rport, 0 ,0 );
  372.     Draw( &rport, 0, PHEIGHT-1 );
  373.  
  374.     wide_pointer = NewObject( NULL, "pointerclass",
  375.         POINTERA_BitMap, bm,
  376.         POINTERA_WordWidth, PWORDWIDTH,
  377.         POINTERA_XResolution, POINTERXRESN_HIRES,
  378.         POINTERA_YResolution, POINTERYRESN_HIGHASPECT,
  379.         TAG_DONE );
  380.     }
  381.  
  382.     return( simple_pointer && wide_pointer );
  383. }
  384.  
  385. /*------------------------------------------------------------------------*/
  386.  
  387.