home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / CBMDevKit3.dms / CBMDevKit3.adf / intuition / pointerdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  9.4 KB  |  386 lines

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