home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / boopsi / dragclass / source / rkmbut.class < prev   
Text File  |  1977-12-31  |  10KB  |  320 lines

  1. //////////////////////////////////////////////////
  2. //
  3. //  DEMO for the ClassParser
  4. //
  5. //  This is a modified copy of
  6. //  RKMLibs/intuition/Boopsi/RKMButton.c
  7. //  while that file was about 16k
  8. //  this one is only about 9k and it still
  9. //  has almost full functionality;
  10. //
  11. //  that way the programmer can give away
  12. //  some automatics to the parser and
  13. //  concentrate to the problem
  14. //
  15. //  I must admit that there are still
  16. //  many problems...
  17. //  the worst thing is, You must know
  18. //  what is defined in Ya "class.tplt"
  19. //
  20. //////////////////////////////////////////////////
  21.  
  22.  
  23. CLASS        rkmbutton
  24. SUPERCLASS  "gadgetclass",NULL
  25. VERSION     TestBut 37.1
  26. CLASS_INIT
  27. CLASS_EXIT
  28.  
  29. INCLUDES
  30.     <intuition/gadgetclass.h>
  31.     <intuition/icclass.h>
  32.  
  33. LIBRARIES
  34.     graphics
  35.  
  36. PRIVATE_INCLUDES
  37.     <graphics/gfxmacros.h>
  38.  
  39.  
  40. DEFINES
  41.     RKMBUT_Pulse (TAG_USER + 1)
  42.     ERASE_ONLY     0x00000001    // Tells rendering routine to
  43.                 // only erase the gadget, not
  44.                 // rerender a new one.    This
  45.                 // lets the gadget erase it-
  46.                 // self before it rescales.
  47.  
  48. PRIVATE_DEFINES
  49.     TEST 1
  50.  
  51. CONTENTS
  52.     LONG midX, midY;        // Coordinates of middle of gadget
  53.  
  54. OBJ_INIT
  55.     inst->midX     = ((struct Gadget *)o)->LeftEdge + ( (((struct Gadget *)o)->Width)  / 2);
  56.     inst->midY     = ((struct Gadget *)o)->TopEdge  + ( (((struct Gadget *)o)->Height) / 2);
  57.  
  58.  
  59.  
  60. METHOD GM_HITTEST struct gpHitTest*
  61.        // Since this is a rectangular gadget this
  62.        // method always returns GMR_GADGETHIT.
  63.     return GMR_GADGETHIT;
  64.  
  65.  
  66. METHOD GM_GOACTIVE struct gpInput*
  67.            // Only become active if the GM_GOACTIVE
  68.            // was triggered by direct user input.
  69.     if (msg->gpi_IEvent)
  70.     {
  71.            // This gadget is now active, change
  72.            // visual state to selected and render.
  73.     ((struct Gadget *)o)->Flags |= GFLG_SELECTED;
  74.             RenderRKMBut(cl, (struct Gadget *)o, (struct gpRender *)msg);
  75.     return GMR_MEACTIVE;
  76.     }
  77.     else        // The GM_GOACTIVE was not
  78.             // triggered by direct user input.
  79.     return GMR_NOREUSE;
  80.  
  81. METHOD GM_RENDER struct gpRender*
  82.     return RenderRKMBut(cl, (struct Gadget *)o, msg);
  83.  
  84.  
  85. METHOD GM_HANDLEINPUT struct gpInput*
  86.            // While it is active, this gadget sends its superclass an
  87.            // OM_NOTIFY pulse for every IECLASS_TIMER event that goes by
  88.            // (about one every 10th of a second).  Any object that is
  89.            // connected to this gadget will get A LOT of OM_UPDATE messages.
  90.  
  91.     struct Gadget *g = (struct Gadget *)o;
  92.     struct InputEvent *ie = msg->gpi_IEvent;
  93.  
  94.     if (ie->ie_Class == IECLASS_RAWMOUSE)
  95.     {
  96.     switch (ie->ie_Code)
  97.     {
  98.         case SELECTUP: // The user let go of the gadget so return GMR_NOREUSE
  99.                // to deactivate and to tell Intuition not to reuse
  100.                // this Input Event as we have already processed it.
  101.                // If the user let go of the gadget while the mouse was
  102.                // over it, mask GMR_VERIFY into the return value so
  103.                // Intuition will send a Release Verify (GADGETUP).
  104.                // Since the gadget is going inactive, send a final
  105.                // notification to the ICA_TARGET.
  106.         NotifyPulse(cl , o, 0L, inst->midX, msg);
  107.         if ( (msg->gpi_Mouse.X < g->LeftEdge) ||
  108.              (msg->gpi_Mouse.X > g->LeftEdge + g->Width) ||
  109.              (msg->gpi_Mouse.Y < g->TopEdge) ||
  110.              (msg->gpi_Mouse.Y > g->TopEdge + g->Height) )
  111.             return GMR_NOREUSE | GMR_VERIFY;
  112.         else
  113.             return GMR_NOREUSE;
  114.  
  115.         case MENUDOWN: // The user hit the menu button. Go inactive and let
  116.                // Intuition reuse the menu button event so Intuition can
  117.                // pop up the menu bar.
  118.                // Since the gadget is going inactive, send a final
  119.                // notification to the ICA_TARGET.
  120.         NotifyPulse(cl , o, 0L, inst->midX, msg);
  121.         return GMR_REUSE;
  122.  
  123.         default:
  124.         return GMR_MEACTIVE;
  125.     }
  126.  
  127.     }
  128.     else if (ie->ie_Class == IECLASS_TIMER)
  129.         // If the gadget gets a timer event, it sends an
  130.         // interim OM_NOTIFY to its superclass.
  131.     NotifyPulse(cl, o, OPUF_INTERIM, inst->midX, msg);
  132.  
  133.     return GMR_MEACTIVE;
  134.  
  135. METHOD GM_GOINACTIVE struct gpGoInactive*
  136.                   // Intuition said to go inactive.  Clear the GFLG_SELECTED
  137.                   // bit and render using unselected imagery.
  138.     ((struct Gadget *)o)->Flags &= ~GFLG_SELECTED;
  139.     RenderRKMBut(cl, (struct Gadget *)o, (struct gpRender *)msg);
  140.     return FALSE;
  141.  
  142.  
  143. METHOD OM_SET struct opSet*
  144.         // Although this class doesn't have settable attributes, this gadget class   */
  145.         // does have scaleable imagery, so it needs to find out when its size and/or */
  146.         // position has changed so it can erase itself, THEN scale, and rerender.     */
  147.     if ( FindTagItem(GA_Width,  msg->ops_AttrList) ||
  148.      FindTagItem(GA_Height, msg->ops_AttrList) ||
  149.      FindTagItem(GA_Top,    msg->ops_AttrList) ||
  150.      FindTagItem(GA_Left,   msg->ops_AttrList) )
  151.     {
  152.     struct RastPort *rp;
  153.     struct Gadget *g = (struct Gadget *)o;
  154.     ULONG retval;
  155.  
  156.     WORD x,y,w,h;
  157.  
  158.     x = g->LeftEdge;
  159.     y = g->TopEdge;
  160.     w = g->Width;
  161.     h = g->Height;
  162.  
  163.     retval = SUPER;
  164.                           // Get pointer to RastPort for gadget.
  165.     if (rp = ObtainGIRPort( msg->ops_GInfo) )
  166.     {
  167.         UWORD *pens = msg->ops_GInfo->gi_DrInfo->dri_Pens;
  168.  
  169.         SetAPen(rp, pens[BACKGROUNDPEN]);
  170.         SetDrMd(rp, JAM1);                            // Erase the old gadget.
  171.         RectFill(rp, x, y, x+w, y+h);
  172.  
  173.         inst->midX = g->LeftEdge + ( (g->Width) / 2); // Recalculate where the
  174.         inst->midY = g->TopEdge + ( (g->Height) / 2); // center of the gadget is.
  175.  
  176.                               // Rerender the gadget.
  177.         DoMethod(o, GM_RENDER, msg->ops_GInfo, rp, GREDRAW_REDRAW);
  178.         ReleaseGIRPort(rp);
  179.     }
  180.     return retval;
  181.     }
  182.     else
  183.     return SUPER;
  184.  
  185.  
  186. SUPPORT
  187. //*************************************************************************************************
  188. //************** Build an OM_NOTIFY message for RKMBUT_Pulse and send it to the superclass. *******
  189. //*************************************************************************************************
  190. void NotifyPulse(Class *cl, Object *o, ULONG flags, LONG mid, struct gpInput *gpi)
  191. {
  192.     struct TagItem tt[3];
  193.  
  194.     tt[0].ti_Tag = RKMBUT_Pulse;
  195.     tt[0].ti_Data = mid - ((gpi->gpi_Mouse).X + ((struct Gadget *)o)->LeftEdge);
  196.  
  197.     tt[1].ti_Tag = GA_ID;
  198.     tt[1].ti_Data = ((struct Gadget *)o)->GadgetID;
  199.  
  200.     tt[2].ti_Tag = TAG_DONE;
  201.  
  202.     DoSuperMethod(cl, o, OM_NOTIFY, tt, gpi->gpi_GInfo, flags);
  203. }
  204.  
  205. //*************************************************************************************************
  206. //*******************************   Erase and rerender the gadget.   ******************************
  207. //*************************************************************************************************
  208.  ULONG RenderRKMBut(Class *cl, struct Gadget *g, struct gpRender *msg)
  209. {
  210.     struct _CLASS *inst = INST_DATA(cl, (Object *)g);
  211.     struct RastPort *rp;
  212.     ULONG retval = TRUE;
  213.     UWORD *pens = msg->gpr_GInfo->gi_DrInfo->dri_Pens;
  214.  
  215.     if (msg->MethodID == GM_RENDER)   // If msg is truly a GM_RENDER message (not a gpInput that
  216.                       // looks like a gpRender), use the rastport within it...
  217.     rp = msg->gpr_RPort;
  218.     else                  // ...Otherwise, get a rastport using ObtainGIRPort().
  219.     rp = ObtainGIRPort(msg->gpr_GInfo);
  220.  
  221.     if (rp)
  222.     {
  223.     UWORD back, shine, shadow, w, h, x, y;
  224.  
  225.     if (g->Flags & GFLG_SELECTED) // If the gadget is selected, reverse the meanings of the
  226.     {                  // pens.
  227.         back   = pens[FILLPEN];
  228.         shine  = pens[SHADOWPEN];
  229.         shadow = pens[SHINEPEN];
  230.     }
  231.     else
  232.     {
  233.         back   = pens[BACKGROUNDPEN];
  234.         shine  = pens[SHINEPEN];
  235.         shadow = pens[SHADOWPEN];
  236.     }
  237.     SetDrMd(rp, JAM1);
  238.  
  239.     SetAPen(rp, back);                      // Erase the old gadget.
  240.     RectFill(rp, g->LeftEdge,
  241.              g->TopEdge,
  242.              g->LeftEdge + g->Width,
  243.              g->TopEdge + g->Height);
  244.  
  245.     SetAPen(rp, shadow);                    // Draw shadow edge.
  246.     Move(rp, g->LeftEdge + 1, g->TopEdge + g->Height);
  247.     Draw(rp, g->LeftEdge + g->Width, g->TopEdge + g->Height);
  248.     Draw(rp, g->LeftEdge + g->Width, g->TopEdge + 1);
  249.  
  250.     w = g->Width / 4;            // Draw Arrows - Sorry, no frills imagery
  251.     h = g->Height / 2;
  252.     x = g->LeftEdge + (w/2);
  253.     y = g->TopEdge + (h/2);
  254.  
  255.     Move(rp, x, inst->midY);
  256.     Draw(rp, x + w, y);
  257.     Draw(rp, x + w, y + (g->Height) - h);
  258.     Draw(rp, x, inst->midY);
  259.  
  260.     x = g->LeftEdge + (w/2) + g->Width / 2;
  261.  
  262.     Move(rp, x + w, inst->midY);
  263.     Draw(rp, x, y);
  264.     Draw(rp, x, y  + (g->Height) - h);
  265.     Draw(rp, x + w, inst->midY);
  266.  
  267.     SetAPen(rp, shine);                     // Draw shine edge.
  268.     Move(rp, g->LeftEdge, g->TopEdge + g->Height - 1);
  269.     Draw(rp, g->LeftEdge, g->TopEdge);
  270.     Draw(rp, g->LeftEdge + g->Width - 1, g->TopEdge);
  271.  
  272.     if (msg->MethodID != GM_RENDER)         // If we allocated a rastport, give it back.
  273.         ReleaseGIRPort(rp);
  274.     }
  275.     else retval = FALSE;
  276.     return(retval);
  277. }
  278.  
  279.  
  280.  
  281. TEST_SUPPORT
  282. struct TagItem pulse2int[] = {
  283.     {RKMBUT_Pulse, STRINGA_LongVal},
  284.     {TAG_END,}
  285. };
  286.  
  287. #   define INTWIDTH  40
  288. #   define INTHEIGHT 20
  289.  
  290. TEST_WINDOW
  291.     WA_Flags,    WFLG_DEPTHGADGET | WFLG_DRAGBAR | WFLG_CLOSEGADGET | WFLG_SIZEGADGET,
  292.     WA_IDCMP,    IDCMP_CLOSEWINDOW,
  293.     WA_Width,        640,
  294.     WA_Height,        200,
  295.  
  296.  
  297. TEST_OBJECT integer NULL,"strgclass",
  298.        GA_ID,         1L,
  299.        GA_Top,         (my_win->BorderTop) + 5L,
  300.        GA_Left,      (my_win->BorderLeft) + 5L,
  301.        GA_Width,     INTWIDTH,
  302.        GA_Height,     INTHEIGHT,
  303.        STRINGA_LongVal,  0L,
  304.        STRINGA_MaxChars, 5L,
  305.  
  306.  
  307. TEST_OBJECT but // myclass,NULL,
  308.        GA_ID,        2L,
  309.        GA_Top,        (my_win->BorderTop) + 5L,
  310.        GA_Left,     ((struct Gadget*)integer)->LeftEdge + ((struct Gadget*)integer)->Width + 5L,
  311.        GA_Width,    40L,
  312.        GA_Height,   INTHEIGHT,
  313.        GA_Previous, integer,
  314.        ICA_MAP,     pulse2int,
  315.        ICA_TARGET,  integer,
  316.  
  317.  
  318.  
  319.  
  320.