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

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