home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / Libraries / Intuition / boopsi / demo1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-06  |  7.2 KB  |  305 lines

  1. /* demo1.c -- demonstration of         :ts=8
  2.  * Basic Object Oriented Programming System for Intuition
  3.  *
  4.  * This demo shows gadgets and images created using pre-defined
  5.  * boopsi class, but not connected together or bound into a group.
  6.  */
  7.  
  8. /*
  9. Copyright (c) 198-19989 Amiga, Inc.
  10.  
  11. Executables based on this information may be used in software
  12. for Amiga computers. All other rights reserved.
  13. This information is provided "as is"; no warranties are made.
  14. All use is at your own risk, and no liability or responsibility
  15. is assumed.
  16. */
  17.  
  18. #include "sysall.h"
  19.  
  20. #define D(x)    ;
  21.  
  22. struct  IntuitionBase   *IntuitionBase;
  23. struct  GfxBase         *GfxBase;
  24. struct  Library         *UtilityBase;
  25.  
  26. ULONG    myiflags = GADGETDOWN | GADGETUP | CLOSEWINDOW;
  27.  
  28. struct Gadget    *propg;
  29. struct Gadget    *stringg;
  30. struct Gadget    *uparrowg;
  31. struct Gadget    *downarrowg;
  32.  
  33. /* pictures for arrows    */
  34. struct Image    *upimage;
  35. struct Image    *downimage;
  36.  
  37. /* some static layout and setup constants, for now    */
  38. #define GTOP        (44)
  39. #define ARROWLEFT    (180)
  40. #define PWIDTH        (120)    /* width of horizontal propslider    */
  41. #define SWIDTH        (50)
  42. #define PROPRANGE    (20)
  43. #define INITVAL        (0)    /* initial value of string and slider    */
  44.  
  45. enum gadgetids {
  46.     gUp = 1,
  47.     gDown,
  48.     gSlider,
  49.     gString,
  50. };
  51.  
  52. struct TagItem    proptags[] = {
  53.     {GA_TOP,        GTOP},
  54.     {GA_WIDTH,        PWIDTH},    /* height to be specified    */
  55.     {GA_IMMEDIATE,    TRUE},
  56.     {GA_RELVERIFY,    TRUE},
  57.     {GA_ID,        gSlider},
  58.     {PGA_FREEDOM,    FREEHORIZ},
  59.     {PGA_TOP,        INITVAL},    /* "top" in the scroller sense    */
  60.     {PGA_VISIBLE,    1},        /* want an integer value slider    */
  61.     {PGA_TOTAL,        PROPRANGE},
  62.     {TAG_END ,}
  63. };
  64.  
  65. struct TagItem    stringtags[] = {
  66.     {GA_ID,        gString},
  67.     {GA_TOP,        GTOP},
  68.     {GA_WIDTH,        SWIDTH},
  69.     {GA_IMMEDIATE,    TRUE},
  70.     {GA_RELVERIFY,    TRUE},
  71.     {GA_HEIGHT,        12},        /* fix this    */
  72.     {STRINGA_MaxChars,    10},
  73.     {STRINGA_LongVal,    INITVAL},    /* make it an integer gadget */
  74.     {STRINGA_Justification,
  75.                 STRINGRIGHT},
  76.     {TAG_END, }
  77. };
  78.  
  79. struct TagItem    arrowtags[] = {
  80.     {GA_TOP    ,    GTOP},
  81.     {GA_IMMEDIATE,    TRUE},
  82.     {GA_RELVERIFY,    TRUE},
  83.     {TAG_END, }
  84. };
  85.  
  86. struct TagItem    arrows2me[] = {
  87.     {GA_ID, GA_ID},
  88.     {TAG_END, }
  89. };
  90.  
  91. #define MYWINDOWTITLE    "boopsi Demo 1"
  92.  
  93. main()
  94. {
  95.     struct DrawInfo    *GetScreenDrawInfo();
  96.     struct Window     *OpenWindowTags();    /* in varargs.c for now */
  97.  
  98.     struct Gadget    *mygadgets = NULL;
  99.     struct Gadget    *tmpgad;
  100.     struct Window    *w;
  101.     struct DrawInfo    *drinfo;
  102.  
  103.     if (!(UtilityBase=(struct Library *)OpenLibrary("utility.library",36L)))
  104.     { cleanup("no V36 utility library\n"); }
  105.  
  106.     if (!(IntuitionBase =
  107.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  108.     { cleanup("no V36 intuition library\n"); }
  109.  
  110.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  111.     { cleanup("no V36 graphics library\n"); }
  112.  
  113.     D( printf("about to openwindow\n") );
  114.     w = OpenWindowTags( NULL,
  115.         WA_Title,    MYWINDOWTITLE,
  116.         WA_SimpleRefresh, TRUE,
  117.         WA_NoCareRefresh, TRUE,
  118.         WA_DepthGadget,    TRUE,
  119.         WA_DragBar,    TRUE,
  120.         WA_Left,    300,
  121.         WA_Top,        50,
  122.         WA_Width,    280,
  123.         WA_Height,    120,
  124.         WA_IDCMP,    myiflags,
  125.         WA_CloseGadget,    TRUE,
  126.             TAG_END );
  127.     D( printf("window at %lx\n ", w ) );
  128.  
  129.     if ( w == NULL) cleanup( "couldn't open my window.\n");
  130.     drinfo = GetScreenDrawInfo( w->WScreen );
  131.  
  132.     /* get images for the up and down arrows, sensitive
  133.      * to depth and pen specs for current screen (we'll be
  134.      * adding resolution/size selection later).
  135.      */
  136.     upimage = (struct Image *) NewObject( NULL, "sysiclass",
  137.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  138.     SYSIA_Which,    UPIMAGE,
  139.     SYSIA_DrawInfo,    drinfo,
  140.         TAG_END );
  141.  
  142.     downimage = (struct Image *) NewObject( NULL, "sysiclass",
  143.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  144.     SYSIA_Which,    DOWNIMAGE,
  145.     SYSIA_DrawInfo,    drinfo,
  146.         TAG_END );
  147.  
  148.     /* make gadgets, link into list (easier starting with Beta 4) */
  149.     tmpgad = (struct Gadget *) &mygadgets;
  150.  
  151.     downarrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  152.     GA_IMAGE,     downimage,
  153.     GA_LEFT,    ARROWLEFT,
  154.     GA_ID,        gDown,
  155.     GA_PREVIOUS,    tmpgad,
  156.     TAG_MORE,    arrowtags,
  157.  
  158.     TAG_END );
  159.     D( printf("downgadget at %lx\n", downarrowg ));
  160.  
  161.     /* get up/down arrow button gadgets    */
  162.     uparrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  163.     GA_IMAGE,     upimage,
  164.     GA_LEFT,    ARROWLEFT + (downarrowg? downarrowg->Width: 0),
  165.     GA_ID,        gUp,
  166.     GA_PREVIOUS,    tmpgad,
  167.     TAG_MORE,    arrowtags,
  168.     TAG_END );
  169.     D( printf("upgadget at %lx\n", uparrowg ));
  170.  
  171.  
  172.     propg = (struct Gadget *) NewObject( NULL, "propgclass",
  173.     GA_LEFT,    ARROWLEFT - PWIDTH,
  174.     GA_HEIGHT,    downarrowg? downarrowg->Height: 20,
  175.     GA_PREVIOUS,    tmpgad,
  176.     TAG_MORE,    proptags,
  177.     TAG_END );
  178.     D( printf( "prop gadget returned: %lx\n", propg ) );
  179.  
  180.     stringg  = (struct Gadget *) NewObject( NULL, "strgclass",
  181.         GA_LEFT,    propg? (propg->LeftEdge - SWIDTH): 20,
  182.     GA_PREVIOUS,    tmpgad,
  183.     TAG_MORE,    stringtags,
  184.     TAG_END );
  185.     D( printf( "string at %lx\n", stringg ) );
  186.  
  187.     D(printf("objects initialized\n"));
  188.  
  189.     AddGList( w, mygadgets, -1, -1, NULL );
  190.     RefreshGList( mygadgets, w, NULL, -1 );
  191.  
  192.     D( printf("gadgets added and refreshed \n") );
  193.  
  194.     goHandleWindow( w );
  195.  
  196.     RemoveGList( w, mygadgets, -1 );
  197.     FreeScreenDrawInfo( w->WScreen, drinfo );
  198.     CloseWindow( w );
  199.  
  200.     D( printf("dispose\n") );
  201.     while ( mygadgets )
  202.     {
  203.     tmpgad = mygadgets->NextGadget;
  204.     DisposeObject( mygadgets );
  205.     mygadgets = tmpgad;
  206.     }
  207.  
  208.     DisposeObject( upimage);
  209.     DisposeObject( downimage);
  210.     D( printf("have disposed.\n") );
  211.  
  212.     cleanup( "all done" );
  213. }
  214.  
  215. cleanup( str )
  216. char    *str;
  217. {
  218.     if (str) printf("%s\n", str);
  219.  
  220.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  221.     if (GfxBase) CloseLibrary(GfxBase);
  222.     if (UtilityBase) CloseLibrary(UtilityBase);
  223.  
  224.     exit (0);
  225. }
  226.  
  227. /* this variable holds the integer "current value" of the
  228.  * whole little system of gadgets
  229.  */
  230. LONG        currval = 0;
  231.  
  232. goHandleWindow( w )
  233. struct Window    *w;
  234. {
  235.     struct IntuiMessage *imsg;
  236.     struct Gadget    *g;
  237.  
  238.     for(;;)
  239.     {
  240.     WaitPort( w->UserPort );
  241.     while ( imsg = (struct IntuiMessage *) GetMsg( w->UserPort ) )
  242.     {
  243.         switch ( imsg->Class )
  244.         {
  245.         case CLOSEWINDOW:
  246.             ReplyMsg( (struct Message *) imsg );
  247.         return;
  248.  
  249.         case GADGETDOWN:
  250.         D( printf("gadget down, %lx\n", imsg->IAddress ) );
  251.         break;
  252.  
  253.         case GADGETUP:
  254.         g = (struct Gadget *) imsg->IAddress;
  255.         D( printf("gadget up, %lx, id %lx\n",
  256.             imsg->IAddress, g->GadgetID ) );
  257.  
  258.         /* manual updating of integer value.
  259.          * this is made more automatic in subsequent demos
  260.          */
  261.         switch ( g->GadgetID )
  262.         {
  263.         case gUp:
  264.             currval++;
  265.             manualUpdate( w );
  266.             break;
  267.         case gDown:
  268.             currval--;
  269.             manualUpdate( w );
  270.             break;
  271.         case gSlider:
  272.             GetAttr( PGA_TOP, g, &currval );
  273.             manualUpdate( w );
  274.             break;
  275.         case gString:
  276.             GetAttr( STRINGA_LongVal, g, &currval );
  277.             manualUpdate( w );
  278.             break;
  279.         default:
  280.             D( printf("unknown gadget id\n"));
  281.         }
  282.  
  283.         break;
  284.         }
  285.         ReplyMsg( (struct Message *) imsg );
  286.     }
  287.     }
  288. }
  289.  
  290. manualUpdate( w )
  291. struct Window    *w;
  292. {
  293.     /* put currval in legal bounds    */
  294.     currval = MAX( 0, MIN( PROPRANGE-1, currval ) );
  295.  
  296.     /* tell interested parties        */
  297.    SetGadgetAttrs( propg, w, NULL,
  298.                PGA_TOP, currval,
  299.             TAG_END );
  300.  
  301.    SetGadgetAttrs( stringg, w, NULL,
  302.                STRINGA_LongVal, currval,
  303.             TAG_END );
  304. }
  305.