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 / demo2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  7.9 KB  |  333 lines

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