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

  1. /* demo3.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, interconnected using boopsi interconnections
  6.  * and a "model".
  7.  *
  8.  * The improvement over demo 2 is that the application only
  9.  * receives messages it is specifically interested in.  All
  10.  * inter-gadget traffic is handled (synchronously) among
  11.  * the objects involved.
  12.  */
  13.  
  14. /*
  15. Copyright (c) 1989-1999 Amiga, Inc.
  16.  
  17. Executables based on this information may be used in software
  18. for Amiga computers. All other rights reserved.
  19. This information is provided "as is"; no warranties are made.
  20. All use is at your own risk, and no liability or responsibility
  21. is assumed.
  22. */
  23.  
  24. #include "sysall.h"
  25. #include <intuition/icclass.h>
  26. #include "mymodel.h"
  27.  
  28. #define D(x)    x
  29.  
  30. struct  IntuitionBase   *IntuitionBase;
  31. struct  GfxBase         *GfxBase;
  32. struct  Library         *UtilityBase;
  33.  
  34. ULONG    myiflags =  CLOSEWINDOW | IDCMPUPDATE;
  35.  
  36. Object        *mymodel = NULL; /* the repository of the "Current Value" */
  37. void    *MyModClass = NULL;
  38. void    *initMyModClass();
  39.  
  40.  
  41. struct Gadget    *propg = NULL;
  42. struct Gadget    *stringg = NULL;
  43. struct Gadget    *uparrowg = NULL;
  44. struct Gadget    *downarrowg = NULL;
  45. struct Gadget    *mygadgets = NULL;        /* linked list    */
  46.  
  47. /* pictures for arrows    */
  48. struct Image    *upimage = NULL;
  49. struct Image    *downimage = NULL;
  50.  
  51. /* some static layout and setup constants, for now    */
  52. #define GTOP        (44)
  53. #define ARROWLEFT    (180)
  54. #define PWIDTH        (120)    /* width of horizontal propslider    */
  55. #define SWIDTH        (50)
  56. #define PROPRANGE    (20)
  57. #define INITVAL        (0)    /* initial value of string and slider    */
  58.  
  59. enum gadgetids {
  60.     gUp = 1,
  61.     gDown,
  62.     gSlider,
  63.     gString,
  64. };
  65.  
  66.  
  67. /****************************************************************/
  68. /*  mapping tag lists                        */
  69. /****************************************************************/
  70.  
  71. /* for IDCMPUPDATE    */
  72. struct TagItem    model2me[] = {
  73.     /* {MYMODA_CURRVAL, MYMODA_CURRVAL }, */
  74.     {MYMODA_CURRVAL, ICSPECIAL_CODE },
  75.     /* put (16 bits of) currval into IntuiMessage.Code    */
  76.     { TAG_END, }
  77. };
  78.  
  79. struct TagItem    slider2model[] = {
  80.     {PGA_TOP, MYMODA_CURRVAL},
  81.     {TAG_END, }
  82. };
  83.  
  84. struct TagItem    model2slider[] = {
  85.     {MYMODA_CURRVAL, PGA_TOP},
  86.     {MYMODA_RANGE, PGA_TOTAL },
  87.     {TAG_END, }
  88. };
  89.  
  90. struct TagItem    string2model[] = {
  91.     {STRINGA_LongVal, MYMODA_CURRVAL},
  92.     {TAG_END, }
  93. };
  94.  
  95. struct TagItem    model2string[] = {
  96.     {MYMODA_CURRVAL, STRINGA_LongVal},
  97.     {TAG_END, }
  98. };
  99.  
  100. struct TagItem    uparrow2model[] = {
  101.     {GA_ID, MYMODA_INCRSTROBE},
  102.     {TAG_END, }
  103. };
  104.  
  105. struct TagItem    downarrow2model[] = {
  106.     {GA_ID, MYMODA_DECRSTROBE},
  107.     {TAG_END, }
  108. };
  109.  
  110. /****************************************************************/
  111. /* tag lists for creating objects                */
  112. /****************************************************************/
  113.  
  114. struct TagItem modeltags[] = {
  115.     { ICA_TARGET,    ICTARGET_IDCMP},    /* talk to me    */
  116.     { ICA_MAP,        (ULONG) &model2me[0]},
  117.     {TAG_END ,}
  118. };
  119.  
  120. struct TagItem    proptags[] = {
  121.     {GA_TOP,        GTOP},
  122.     {GA_WIDTH,        PWIDTH},    /* height to be specified    */
  123.     {GA_ID,        gSlider},
  124.  
  125.     /* {ICA_TARGET,    XXX }, * will be model object    */
  126.     {ICA_MAP,        (ULONG) &slider2model[0]},
  127.  
  128.     {PGA_FREEDOM,    FREEHORIZ},
  129.     {PGA_VISIBLE,    1},        /* want an integer value slider    */
  130.  
  131. #if 0    /* the whole set of gadgets will be initialized together    */
  132.     {PGA_TOP,        INITVAL},    /* "top" in the scroller sense    */
  133.     {PGA_TOTAL,        PROPRANGE},
  134. #endif
  135.  
  136.     {TAG_END ,}
  137. };
  138.  
  139. struct TagItem    stringtags[] = {
  140.     {GA_ID,        gString},
  141.     {GA_TOP,        GTOP},
  142.     {GA_WIDTH,        SWIDTH},
  143.     {GA_HEIGHT,        12},        /* fix this    */
  144.  
  145.     /* {ICA_TARGET,    XXXX },        will be model object    */
  146.     {ICA_MAP,        (ULONG) &string2model[0]},
  147.  
  148.     {STRINGA_MaxChars,    10},
  149.     {STRINGA_LongVal,    INITVAL},    /* make it an integer gadget */
  150.     {STRINGA_Justification,
  151.                 STRINGRIGHT},
  152.     {TAG_END, }
  153. };
  154.  
  155. struct TagItem    uparrowtags[] = {
  156.     {GA_TOP,        GTOP},
  157.     /* {ICA_TARGET,    ICTARGET_IDCMP}, will be model object    */
  158.     {ICA_MAP,        (ULONG) &uparrow2model[0]},
  159.     {TAG_END, }
  160. };
  161.  
  162. struct TagItem    downarrowtags[] = {
  163.     {GA_TOP,        GTOP},
  164.     /* {ICA_TARGET,    ICTARGET_IDCMP}, will be model object    */
  165.     {ICA_MAP,        (ULONG) &downarrow2model[0]},
  166.     {TAG_END, }
  167. };
  168.  
  169. #define MYWINDOWTITLE    "boopsi Demo 3"
  170.  
  171. main()
  172. {
  173.     struct DrawInfo    *GetScreenDrawInfo();
  174.     struct Window     *OpenWindowTags();    /* in varargs.c for now */
  175.  
  176.     struct Gadget    *tmpgad;
  177.     struct Window    *w;
  178.     struct DrawInfo    *drinfo;
  179.  
  180.     Object        *ic;
  181.  
  182.     if (!(UtilityBase=(struct Library *)OpenLibrary("utility.library",36L)))
  183.     { cleanup("no V36 utility library\n"); }
  184.  
  185.     if (!(IntuitionBase =
  186.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  187.     { cleanup("no V36 intuition library\n"); }
  188.  
  189.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  190.     { cleanup("no V36 graphics library\n"); }
  191.  
  192.     w = OpenWindowTags( NULL,
  193.         WA_Title,    MYWINDOWTITLE,
  194.         WA_SimpleRefresh, TRUE,
  195.         WA_NoCareRefresh, TRUE,
  196.         WA_DepthGadget,    TRUE,
  197.         WA_DragBar,    TRUE,
  198.         WA_Left,    300,
  199.         WA_Top,        50,
  200.         WA_Width,    280,
  201.         WA_Height,    120,
  202.         WA_IDCMP,    myiflags,
  203.         WA_CloseGadget,    TRUE,
  204.             TAG_END );
  205.     D( printf("window at %lx\n", w ) );
  206.  
  207.     if ( w == NULL) cleanup( "couldn't open my window.\n");
  208.     drinfo = GetScreenDrawInfo( w->WScreen );
  209.  
  210.     /* get images for the up and down arrows, sensitive
  211.      * to depth and pen specs for current screen (we'll be
  212.      * adding resolution/size selection later).
  213.      */
  214.     upimage = (struct Image *) NewObject( NULL, "sysiclass",
  215.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  216.     SYSIA_DrawInfo, drinfo,
  217.     SYSIA_Which,    UPIMAGE,
  218.         TAG_END );
  219.  
  220.     downimage = (struct Image *) NewObject( NULL, "sysiclass",
  221.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  222.     SYSIA_Which,    DOWNIMAGE,
  223.     SYSIA_DrawInfo, drinfo,
  224.         TAG_END );
  225.  
  226.  
  227.     /* get "model" object which is the repository of our "current
  228.      * value" and is the hub of object interconnection.
  229.      * This thing also is used to free icclass objects,
  230.      * so we'd better make sure it got allocated.
  231.      */
  232.     MyModClass = initMyModClass();    /* private class    */
  233.     D( printf("get model object, class at %lx\n", MyModClass ) );
  234.  
  235.     mymodel = (Object *) NewObjectA( MyModClass, NULL, modeltags );
  236.     D( printf("model at %lx\n", mymodel ) );
  237.  
  238.     if ( ! mymodel ) cleanup( "couldn't get model object" );
  239.  
  240.     /* make gadgets, link into list (easier starting with Beta 4) */
  241.     tmpgad = (struct Gadget *) &mygadgets;
  242.  
  243.     D( printf("get gadgets\n"));
  244.  
  245.     downarrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  246.     GA_IMAGE,     downimage,
  247.     GA_TOP,        GTOP,
  248.     GA_LEFT,    ARROWLEFT,
  249.     GA_ID,        gDown,
  250.     GA_PREVIOUS,    tmpgad,
  251.     /* interconnections ...    */
  252.     ICA_TARGET,    mymodel,
  253.     ICA_MAP,    &downarrow2model[0],
  254.     TAG_END );
  255.     D( printf("downgadget at %lx\n", downarrowg ));
  256.  
  257.     /* get up/down arrow button gadgets    */
  258.     uparrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  259.     GA_IMAGE,     upimage,
  260.     GA_TOP,        GTOP,
  261.     GA_LEFT,    ARROWLEFT + (downarrowg? downarrowg->Width: 0),
  262.     GA_ID,        gUp,
  263.     GA_PREVIOUS,    tmpgad,
  264.     /* interconnections ...    */
  265.     ICA_MAP,    &uparrow2model[0],
  266.     ICA_TARGET,    mymodel,
  267.     TAG_END );
  268.     D( printf("upgadget at %lx\n", uparrowg ));
  269.  
  270.     propg = (struct Gadget *) NewObject( NULL, "propgclass",
  271.     GA_LEFT,    ARROWLEFT - PWIDTH,
  272.     GA_HEIGHT,    downarrowg? downarrowg->Height: 20,
  273.     GA_PREVIOUS,    tmpgad,
  274.  
  275.     ICA_TARGET,    mymodel,
  276.     TAG_MORE,    proptags,
  277.     TAG_END );
  278.     D( printf( "prop gadget returned: %lx\n", propg ) );
  279.  
  280.     stringg  = (struct Gadget *) NewObject( NULL, "strgclass",
  281.         GA_LEFT,    propg? (propg->LeftEdge - SWIDTH): 20,
  282.     GA_PREVIOUS,    tmpgad,
  283.  
  284.     ICA_TARGET,    mymodel,
  285.     TAG_MORE,    stringtags,
  286.     TAG_END );
  287.     D( printf( "string gadget at %lx\n", stringg ) );
  288.  
  289.     /*
  290.      * We now have all the gadgets talking to the model,
  291.      * but we need to create some little IC nodes for
  292.      * the model to update the string and propotional gadgets
  293.      */
  294.     ic = NewObject( NULL, ICCLASS,
  295.             ICA_TARGET,    stringg,
  296.         ICA_MAP,    model2string,
  297.         TAG_END );
  298.     DoMethod( mymodel, OM_ADDMEMBER, ic );
  299.  
  300.     ic = NewObject( NULL, ICCLASS,
  301.             ICA_TARGET,    propg,
  302.         ICA_MAP,    model2slider,
  303.         TAG_END );
  304.     DoMethod( mymodel, OM_ADDMEMBER, ic );
  305.  
  306.     D(printf("objects initialized\n"));
  307.  
  308.     AddGList( w, mygadgets, -1, -1, NULL );
  309.     RefreshGList( mygadgets, w, NULL, -1 );
  310.  
  311.     D( printf("gadgets added and refreshed \n") );
  312.  
  313.     /* although we're changing the attributes of
  314.      * the model, if we want the gadgets attached
  315.      * to it to be able to refresh, we have to
  316.      * use SetGadgetAttr()s rather than SetAttrs()
  317.      */
  318.     SetGadgetAttrs( mymodel, w, NULL,
  319.                 MYMODA_RANGE, PROPRANGE,
  320.             MYMODA_CURRVAL, PROPRANGE/2,
  321.             TAG_END );
  322.  
  323.     D( printf("have set range and initval\n" ) );
  324.  
  325.     goHandleWindow( w );
  326.  
  327.     RemoveGList( w, mygadgets, -1 );
  328.     FreeScreenDrawInfo( w->WScreen, drinfo );
  329.     CloseWindow( w );
  330.     cleanup( "all done" );
  331. }
  332.  
  333. cleanup( str )
  334. char    *str;
  335. {
  336.     struct Gadget    *tmpgad;
  337.  
  338.     if (str) printf("%s\n", str);
  339.  
  340.     D( printf("dispose objects\n") );
  341.     DisposeObject( mymodel );
  342.  
  343.     while ( mygadgets )
  344.     {
  345.     tmpgad = mygadgets->NextGadget;
  346.     DisposeObject( mygadgets );
  347.     mygadgets = tmpgad;
  348.     }
  349.  
  350.     DisposeObject( upimage);
  351.     DisposeObject( downimage);
  352.     D( printf("have disposed.\n") );
  353.  
  354.     if ( MyModClass ) freeMyModClass( MyModClass );
  355.  
  356.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  357.     if (GfxBase) CloseLibrary(GfxBase);
  358.     if (UtilityBase) CloseLibrary(UtilityBase);
  359.  
  360.     exit (0);
  361. }
  362.  
  363. goHandleWindow( w )
  364. struct Window    *w;
  365. {
  366.     struct TagItem    *FindTagItem();
  367.  
  368.     struct IntuiMessage *imsg;
  369.     struct TagItem    *tags;
  370.     ULONG        currval;
  371.     /* struct TagItem    *ti; */
  372.  
  373.     for(;;)
  374.     {
  375.     WaitPort( w->UserPort );
  376.     while ( imsg = (struct IntuiMessage *) GetMsg( w->UserPort ) )
  377.     {
  378.         switch ( imsg->Class )
  379.         {
  380.         case CLOSEWINDOW:
  381.             ReplyMsg( (struct Message *) imsg );
  382.         return;
  383.  
  384.         case IDCMPUPDATE:
  385.         tags = (struct TagItem *) imsg->IAddress;
  386.             D( printf("IDCMPUPDATE, quals %lx code (decimal) %ld\n",
  387.             imsg->Qualifier, imsg->Code ));
  388.  
  389.         GetAttr( MYMODA_CURRVAL, mymodel, &currval );
  390.         D( printf("Current value now %ld\n", currval ) );
  391.  
  392.         break;
  393.         }
  394.         ReplyMsg( (struct Message *) imsg );
  395.     }
  396.     }
  397. }
  398.  
  399.