home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / mui-2.1.lha / MUI-2.1 / Developer / C / Examples / Class3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-04  |  7.5 KB  |  295 lines

  1. #include "demo.h"
  2.  
  3.  
  4. /***************************************************************************/
  5. /* Here is the beginning of our simple new class...                        */
  6. /***************************************************************************/
  7.  
  8. /*
  9. ** This is an example for the simplest possible MUI class. It's just some
  10. ** kind of custom image and supports only two methods:
  11. ** MUIM_AskMinMax and MUIM_Draw.
  12. */
  13.  
  14. /*
  15. ** This is the instance data for our custom class.
  16. ** Since it's a very simple class, it contains just a dummy entry.
  17. */
  18.  
  19. struct Data
  20. {
  21.     int x,y,sx,sy;
  22. };
  23.  
  24.  
  25. /*
  26. ** AskMinMax method will be called before the window is opened
  27. ** and before layout takes place. We need to tell MUI the
  28. ** minimum, maximum and default size of our object.
  29. */
  30.  
  31. SAVEDS ULONG mAskMinMax(struct IClass *cl,Object *obj,struct MUIP_AskMinMax *msg)
  32. {
  33.     /*
  34.     ** let our superclass first fill in what it thinks about sizes.
  35.     ** this will e.g. add the size of frame and inner spacing.
  36.     */
  37.  
  38.     DoSuperMethodA(cl,obj,msg);
  39.  
  40.     /*
  41.     ** now add the values specific to our object. note that we
  42.     ** indeed need to *add* these values, not just set them!
  43.     */
  44.  
  45.     msg->MinMaxInfo->MinWidth  += 100;
  46.     msg->MinMaxInfo->DefWidth  += 120;
  47.     msg->MinMaxInfo->MaxWidth  += 500;
  48.  
  49.     msg->MinMaxInfo->MinHeight += 40;
  50.     msg->MinMaxInfo->DefHeight += 90;
  51.     msg->MinMaxInfo->MaxHeight += 300;
  52.  
  53.     return(0);
  54. }
  55.  
  56.  
  57. /*
  58. ** Draw method is called whenever MUI feels we should render
  59. ** our object. This usually happens after layout is finished
  60. ** or when we need to refresh in a simplerefresh window.
  61. ** Note: You may only render within the rectangle
  62. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  63. */
  64.  
  65. SAVEDS ULONG mDraw(struct IClass *cl,Object *obj,struct MUIP_Draw *msg)
  66. {
  67.     struct Data *data = INST_DATA(cl,obj);
  68.  
  69.     /*
  70.     ** let our superclass draw itself first, area class would
  71.     ** e.g. draw the frame and clear the whole region. What
  72.     ** it does exactly depends on msg->flags.
  73.     */
  74.  
  75.     DoSuperMethodA(cl,obj,msg);
  76.  
  77.     /*
  78.     ** if MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  79.     ** MUI just wanted to update the frame or something like that.
  80.     */
  81.  
  82.     if (msg->flags & MADF_DRAWUPDATE) /* called from our input method */
  83.     {
  84.         if (data->sx || data->sy)
  85.         {
  86.             SetBPen(_rp(obj),_dri(obj)->dri_Pens[SHINEPEN]);
  87.             ScrollRaster(_rp(obj),data->sx,data->sy,_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj));
  88.             SetBPen(_rp(obj),0);
  89.             data->sx = 0;
  90.             data->sy = 0;
  91.         }
  92.         else
  93.         {
  94.             SetAPen(_rp(obj),_dri(obj)->dri_Pens[SHADOWPEN]);
  95.             WritePixel(_rp(obj),data->x,data->y);
  96.         }
  97.     }
  98.     else if (msg->flags & MADF_DRAWOBJECT)
  99.     {
  100.         SetAPen(_rp(obj),_dri(obj)->dri_Pens[SHINEPEN]);
  101.         RectFill(_rp(obj),_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj));
  102.     }
  103.  
  104.     return(0);
  105. }
  106.  
  107.  
  108. SAVEDS ULONG mSetup(struct IClass *cl,Object *obj,struct MUIP_HandleInput *msg)
  109. {
  110.     if (!(DoSuperMethodA(cl,obj,msg)))
  111.         return(FALSE);
  112.  
  113.     MUI_RequestIDCMP(obj,IDCMP_MOUSEBUTTONS|IDCMP_RAWKEY);
  114.  
  115.     return(TRUE);
  116. }
  117.  
  118.  
  119. SAVEDS ULONG mCleanup(struct IClass *cl,Object *obj,struct MUIP_HandleInput *msg)
  120. {
  121.     MUI_RejectIDCMP(obj,IDCMP_MOUSEBUTTONS|IDCMP_RAWKEY);
  122.     return(DoSuperMethodA(cl,obj,msg));
  123. }
  124.  
  125.  
  126. SAVEDS ULONG mHandleInput(struct IClass *cl,Object *obj,struct MUIP_HandleInput *msg)
  127. {
  128.     #define _between(a,x,b) ((x)>=(a) && (x)<=(b))
  129.     #define _isinobject(x,y) (_between(_mleft(obj),(x),_mright(obj)) && _between(_mtop(obj),(y),_mbottom(obj)))
  130.  
  131.     struct Data *data = INST_DATA(cl,obj);
  132.  
  133.     if (msg->muikey)
  134.     {
  135.         switch (msg->muikey)
  136.         {
  137.             case MUIKEY_LEFT : data->sx=-1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  138.             case MUIKEY_RIGHT: data->sx= 1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  139.             case MUIKEY_UP   : data->sy=-1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  140.             case MUIKEY_DOWN : data->sy= 1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  141.         }
  142.     }
  143.  
  144.     if (msg->imsg)
  145.     {
  146.         switch (msg->imsg->Class)
  147.         {
  148.             case IDCMP_MOUSEBUTTONS:
  149.             {
  150.                 if (msg->imsg->Code==SELECTDOWN)
  151.                 {
  152.                     if (_isinobject(msg->imsg->MouseX,msg->imsg->MouseY))
  153.                     {
  154.                         data->x = msg->imsg->MouseX;
  155.                         data->y = msg->imsg->MouseY;
  156.                         MUI_Redraw(obj,MADF_DRAWUPDATE);
  157.                         MUI_RequestIDCMP(obj,IDCMP_MOUSEMOVE);
  158.                     }
  159.                 }
  160.                 else
  161.                     MUI_RejectIDCMP(obj,IDCMP_MOUSEMOVE);
  162.             }
  163.             break;
  164.  
  165.             case IDCMP_MOUSEMOVE:
  166.             {
  167.                 if (_isinobject(msg->imsg->MouseX,msg->imsg->MouseY))
  168.                 {
  169.                     data->x = msg->imsg->MouseX;
  170.                     data->y = msg->imsg->MouseY;
  171.                     MUI_Redraw(obj,MADF_DRAWUPDATE);
  172.                 }
  173.             }
  174.             break;
  175.         }
  176.     }
  177.  
  178.     return(0);
  179. }
  180.  
  181.  
  182. /*
  183. ** Here comes the dispatcher for our custom class. We only need to
  184. ** care about MUIM_AskMinMax and MUIM_Draw in this simple case.
  185. ** Unknown/unused methods are passed to the superclass immediately.
  186. */
  187.  
  188. SAVEDS ASM ULONG MyDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  189. {
  190.     switch (msg->MethodID)
  191.     {
  192.         case MUIM_AskMinMax  : return(mAskMinMax  (cl,obj,(APTR)msg));
  193.         case MUIM_Draw       : return(mDraw       (cl,obj,(APTR)msg));
  194.         case MUIM_HandleInput: return(mHandleInput(cl,obj,(APTR)msg));
  195.         case MUIM_Setup      : return(mSetup      (cl,obj,(APTR)msg));
  196.         case MUIM_Cleanup    : return(mCleanup    (cl,obj,(APTR)msg));
  197.     }
  198.  
  199.     return(DoSuperMethodA(cl,obj,msg));
  200. }
  201.  
  202.  
  203.  
  204. /***************************************************************************/
  205. /* Thats all there is about it. Now lets see how things are used...        */
  206. /***************************************************************************/
  207.  
  208. int main(int argc,char *argv[])
  209. {
  210.     APTR app,window,MyObj;
  211.     struct MUI_CustomClass *mcc;
  212.     ULONG signals;
  213.     BOOL running = TRUE;
  214.  
  215.     init();
  216.  
  217.     /* Create the new custom class with a call to MUI_CreateCustomClass(). */
  218.     /* Caution: This function returns not a struct IClass, but a           */
  219.     /* struct MUI_CustomClass which contains a struct IClass to be         */
  220.     /* used with NewObject() calls.                                        */
  221.     /* Note well: MUI creates the dispatcher hook for you, you may         */
  222.     /* *not* use its h_Data field! If you need custom data, use the        */
  223.     /* cl_UserData of the IClass structure!                                */
  224.  
  225.     if (!(mcc = MUI_CreateCustomClass(NULL,MUIC_Area,NULL,sizeof(struct Data),MyDispatcher)))
  226.         fail(NULL,"Could not create custom class.");
  227.  
  228.     app = ApplicationObject,
  229.         MUIA_Application_Title      , "Class3",
  230.         MUIA_Application_Version    , "$VER: Class3 1.0 (01.12.93)",
  231.         MUIA_Application_Copyright  , "©1993, Stefan Stuntz",
  232.         MUIA_Application_Author     , "Stefan Stuntz",
  233.         MUIA_Application_Description, "Demonstrate the use of custom classes.",
  234.         MUIA_Application_Base       , "CLASS3",
  235.  
  236.         SubWindow, window = WindowObject,
  237.             MUIA_Window_Title, "A rather complex custom class",
  238.             MUIA_Window_ID   , MAKE_ID('C','L','S','3'),
  239.             WindowContents, VGroup,
  240.  
  241.                 Child, TextObject,
  242.                     TextFrame,
  243.                     MUIA_Background, MUII_TextBack,
  244.                     MUIA_Text_Contents, "\33cPaint with mouse,\nscroll with cursor keys.",
  245.                     End,
  246.  
  247.                 Child, MyObj = NewObject(mcc->mcc_Class,NULL,
  248.                     TextFrame,
  249.                     TAG_DONE),
  250.  
  251.                 End,
  252.  
  253.             End,
  254.         End;
  255.  
  256.     if (!app)
  257.         fail(app,"Failed to create Application.");
  258.  
  259.     set(window,MUIA_Window_DefaultObject, MyObj);
  260.  
  261.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  262.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  263.  
  264.  
  265.  
  266. /*
  267. ** Input loop...
  268. */
  269.  
  270.     set(window,MUIA_Window_Open,TRUE);
  271.  
  272.     while (running)
  273.     {
  274.         switch (DoMethod(app,MUIM_Application_Input,&signals))
  275.         {
  276.             case MUIV_Application_ReturnID_Quit:
  277.                 running = FALSE;
  278.                 break;
  279.         }
  280.  
  281.         if (running && signals) Wait(signals);
  282.     }
  283.  
  284.     set(window,MUIA_Window_Open,FALSE);
  285.  
  286.  
  287. /*
  288. ** Shut down...
  289. */
  290.  
  291.     MUI_DisposeObject(app);     /* dispose all objects. */
  292.     MUI_DeleteCustomClass(mcc); /* delete the custom class. */
  293.     fail(NULL,NULL);            /* exit, app is already disposed. */
  294. }
  295.