home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 18 / amigaformatcd18.iso / mui / mui_developer / c / examples / slidorama.c < prev    next >
C/C++ Source or Header  |  1997-03-10  |  11KB  |  364 lines

  1. #include "demo.h"
  2.  
  3. /*****************************************************************************
  4. ** This is the Mousepower custom class, a sub class of Levelmeter.mui.
  5. ** It is quite simple and does nothing but add some input capabilities
  6. ** to its super class by implementing MUIM_HandleInput.
  7. ** Don't be afraid of writing sub classes!
  8. ******************************************************************************/
  9.  
  10. struct MousepowerData
  11. {
  12.     WORD Decrease;
  13.     WORD MouseX;
  14.     WORD MouseY;
  15.     WORD Direction;
  16. };
  17.  
  18. #define MUIA_Mousepower_Direction ((TAG_USER | ( 1 << 16)) | 0x0001)
  19.  
  20. SAVEDS ASM ULONG MousepowerDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  21. {
  22.     switch (msg->MethodID)
  23.     {
  24.         case OM_NEW:
  25.         {
  26.             if (obj=(Object *)DoSuperMethodA(cl,obj,msg))
  27.             {
  28.                 struct MousepowerData *data = INST_DATA(cl,obj);
  29.                 data->MouseX    = -1;
  30.                 data->Direction = GetTagData(MUIA_Mousepower_Direction,0,((struct opSet *)msg)->ops_AttrList);
  31.                 set(obj,MUIA_Numeric_Max,1000);
  32.             }
  33.             return((ULONG)obj);
  34.         }
  35.         break;
  36.  
  37.         case MUIM_Setup:
  38.         {
  39.             struct MousepowerData *data = INST_DATA(cl,obj);
  40.             if (!DoSuperMethodA(cl,obj,msg))
  41.                 return(FALSE);
  42.             data->MouseX = -1;
  43.             set(obj,MUIA_Numeric_Max,1000);
  44.             MUI_RequestIDCMP(obj,IDCMP_MOUSEMOVE|IDCMP_INTUITICKS|IDCMP_INACTIVEWINDOW);
  45.             return(TRUE);
  46.         }
  47.         break;
  48.  
  49.         case MUIM_Cleanup:
  50.         {
  51.             MUI_RejectIDCMP(obj,IDCMP_MOUSEMOVE|IDCMP_INTUITICKS|IDCMP_INACTIVEWINDOW);
  52.             return(DoSuperMethodA(cl,obj,msg));
  53.         }
  54.         break;
  55.  
  56.         case MUIM_HandleInput:
  57.         {
  58.             struct MUIP_HandleInput *m = (APTR)msg;
  59.             struct MousepowerData *data = INST_DATA(cl,obj);
  60.  
  61.             if (m->imsg)
  62.             {
  63.                 if (m->imsg->Class==IDCMP_MOUSEMOVE)
  64.                 {
  65.                     if (data->MouseX!=-1)
  66.                     {
  67.                         LONG delta;
  68.  
  69.                         switch (data->Direction)
  70.                         {
  71.                             case 1 : delta = abs(data->MouseX - m->imsg->MouseX) * 2; break;
  72.                             case 2 : delta = abs(data->MouseY - m->imsg->MouseY) * 2; break;
  73.                             default: delta = abs(data->MouseX - m->imsg->MouseX) + abs(data->MouseY - m->imsg->MouseY); break;
  74.                         }
  75.                         if (data->Decrease>0) data->Decrease--;
  76.                         DoMethod(obj,MUIM_Numeric_Increase,delta/10);
  77.                     }
  78.                     data->MouseX = m->imsg->MouseX;
  79.                     data->MouseY = m->imsg->MouseY;
  80.                 }
  81.                 else if (m->imsg->Class==IDCMP_INTUITICKS)
  82.                 {
  83.                     DoMethod(obj,MUIM_Numeric_Decrease,data->Decrease*data->Decrease);
  84.                     if (data->Decrease<50) data->Decrease++;
  85.                 }
  86.                 else if (m->imsg->Class==IDCMP_INACTIVEWINDOW)
  87.                 {
  88.                     set(obj,MUIA_Numeric_Value,0);
  89.                 }
  90.             }
  91.  
  92.             return(0);
  93.         }
  94.         break;
  95.     }
  96.  
  97.     return(DoSuperMethodA(cl,obj,msg));
  98. }
  99.  
  100.  
  101.  
  102. /*****************************************************************************
  103. ** This is the Rating custom class, a sub class of Slider.mui.
  104. ** It shows how to override the MUIM_Numeric_Stringify method
  105. ** to implement custom displays in a slider gadget. Nothing
  106. ** easier than that... :-)
  107. ******************************************************************************/
  108.  
  109. struct RatingData
  110. {
  111.     char buf[20];
  112. };
  113.  
  114. SAVEDS ASM ULONG RatingDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  115. {
  116.     if (msg->MethodID==MUIM_Numeric_Stringify)
  117.     {
  118.         struct RatingData *data = INST_DATA(cl,obj);
  119.         struct MUIP_Numeric_Stringify *m = (APTR)msg;
  120.  
  121.         if (m->value==0)
  122.         {
  123.             strcpy(data->buf,"You're kidding!");
  124.         }
  125.         else if (m->value==100)
  126.         {
  127.             strcpy(data->buf,"It's magic!");
  128.         }
  129.         else
  130.         {
  131.             static const STRPTR ratings[] = { ":-((",":-(",":-|",":-)",":-))" };
  132.             LONG r = DoMethod(obj,MUIM_Numeric_ValueToScale,0,sizeof(ratings)/sizeof(STRPTR)-1);
  133.             sprintf(data->buf,"%3ld points. %s",m->value,(char *)ratings[r]);
  134.         }
  135.         return((ULONG)data->buf);
  136.     }
  137.     return(DoSuperMethodA(cl,obj,msg));
  138. }
  139.  
  140.  
  141.  
  142. /*****************************************************************************
  143. ** A time slider custom class. Just like with the Rating class, we override
  144. ** the MUIM_Numeric_Stringify method. Wow... our classes get smaller and 
  145. ** smaller. This one only has about 10 lines of C code. :-)
  146. ** Note that we can use this TimeDispatcher as subclass of any of
  147. ** MUI's numeric classes. In Slidorama, we create a Timebutton class
  148. ** from MUIC_Numericbutton and Timeslider class for MUIC_Slider with
  149. ** the same dispatcher function.
  150. ******************************************************************************/
  151.  
  152. struct TimeData
  153. {
  154.     char buf[16];
  155. };
  156.  
  157. SAVEDS ASM ULONG TimeDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  158. {
  159.     if (msg->MethodID==MUIM_Numeric_Stringify)
  160.     {
  161.         struct TimeData *data = INST_DATA(cl,obj);
  162.         struct MUIP_Numeric_Stringify *m = (APTR)msg;
  163.         sprintf(data->buf,"%02ld:%02ld",m->value/60,m->value%60);
  164.         return((ULONG)data->buf);
  165.     }
  166.     return(DoSuperMethodA(cl,obj,msg));
  167. }
  168.  
  169.  
  170.  
  171. /*****************************************************************************
  172. ** Main Program
  173. ******************************************************************************/
  174.  
  175. static struct MUI_CustomClass *MousepowerClass;
  176. static struct MUI_CustomClass *RatingClass;
  177. static struct MUI_CustomClass *TimebuttonClass;
  178. static struct MUI_CustomClass *TimesliderClass;
  179.  
  180. static VOID CleanupClasses(VOID)
  181. {
  182.     if (MousepowerClass) MUI_DeleteCustomClass(MousepowerClass);
  183.     if (RatingClass    ) MUI_DeleteCustomClass(RatingClass);
  184.     if (TimebuttonClass) MUI_DeleteCustomClass(TimebuttonClass);
  185.     if (TimesliderClass    ) MUI_DeleteCustomClass(TimesliderClass);
  186. }
  187.  
  188. static BOOL SetupClasses(VOID)
  189. {
  190.     MousepowerClass = MUI_CreateCustomClass(NULL,MUIC_Levelmeter,NULL,sizeof(struct MousepowerData),MousepowerDispatcher);
  191.     RatingClass     = MUI_CreateCustomClass(NULL,MUIC_Slider,NULL,sizeof(struct RatingData),RatingDispatcher);
  192.     TimesliderClass = MUI_CreateCustomClass(NULL,MUIC_Slider,NULL,sizeof(struct TimeData),TimeDispatcher);
  193.     TimebuttonClass = MUI_CreateCustomClass(NULL,MUIC_Numericbutton,NULL,sizeof(struct TimeData),TimeDispatcher);
  194.  
  195.     if (MousepowerClass && RatingClass && TimebuttonClass && TimesliderClass)
  196.         return(TRUE);
  197.  
  198.     CleanupClasses();
  199.     return(FALSE);
  200. }
  201.  
  202. int main(int argc,char *argv[])
  203. {
  204.     APTR app,window;
  205.  
  206.     init();
  207.  
  208.     if (!SetupClasses())
  209.         fail(NULL,"Could not create custom classes.");
  210.  
  211.     app = ApplicationObject,
  212.         MUIA_Application_Title      , "Slidorama",
  213.         MUIA_Application_Version    , "$VER: Slidorama 19.5 (12.02.97)",
  214.         MUIA_Application_Copyright  , "©1992-95, Stefan Stuntz",
  215.         MUIA_Application_Author     , "Stefan Stuntz",
  216.         MUIA_Application_Description, "Show different kinds of sliders",
  217.         MUIA_Application_Base       , "SLIDORAMA",
  218.  
  219.         SubWindow, window = WindowObject,
  220.             MUIA_Window_Title, "Slidorama",
  221.             MUIA_Window_ID   , MAKE_ID('S','L','I','D'),
  222.  
  223.             WindowContents, VGroup,
  224.  
  225.                 Child, HGroup,
  226.  
  227.                     Child, VGroup, GroupSpacing(0), GroupFrameT("Knobs"),
  228.                         Child, VSpace(0),
  229.                         Child, ColGroup(6),
  230.                             GroupSpacing(0),
  231.                             Child, VSpace(0),
  232.                             Child, HSpace(4),
  233.                             Child, CLabel("1"),
  234.                             Child, CLabel("2"),
  235.                             Child, CLabel("3"),
  236.                             Child, CLabel("4"),
  237.                             Child, VSpace(2),
  238.                             Child, VSpace(2),
  239.                             Child, VSpace(2),
  240.                             Child, VSpace(2),
  241.                             Child, VSpace(2),
  242.                             Child, VSpace(2),
  243.                             Child, Label("Volume:"),
  244.                             Child, HSpace(4),
  245.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Max, 64, MUIA_Numeric_Default, 64, End,
  246.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Max, 64, MUIA_Numeric_Default, 64, End,
  247.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Max, 64, MUIA_Numeric_Default, 64, End,
  248.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Max, 64, MUIA_Numeric_Default, 64, End,
  249.                             Child, Label("Bass:"),
  250.                             Child, HSpace(4),
  251.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  252.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  253.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  254.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  255.                             Child, Label("Treble:"),
  256.                             Child, HSpace(4),
  257.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  258.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  259.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  260.                             Child, KnobObject, MUIA_CycleChain, 1, MUIA_Numeric_Min, -100, MUIA_Numeric_Max, 100, End,
  261.                             End,
  262.                         Child, VSpace(0),
  263.                         End,
  264.  
  265.                     Child, VGroup,
  266.                         Child, VGroup, GroupFrameT("Levelmeter Displays"),
  267.                             Child, VSpace(0),
  268.                             Child, HGroup,
  269.                                 Child, HSpace(0),
  270.                                 Child, NewObject(MousepowerClass->mcc_Class,0,MUIA_Mousepower_Direction,1,MUIA_Levelmeter_Label,"Horizontal",TAG_DONE),
  271.                                 Child, HSpace(0),
  272.                                 Child, NewObject(MousepowerClass->mcc_Class,0,MUIA_Mousepower_Direction,2,MUIA_Levelmeter_Label,"Vertical"  ,TAG_DONE),
  273.                                 Child, HSpace(0),
  274.                                 Child, NewObject(MousepowerClass->mcc_Class,0,MUIA_Mousepower_Direction,0,MUIA_Levelmeter_Label,"Total"     ,TAG_DONE),
  275.                                 Child, HSpace(0),
  276.                                 End,
  277.                             Child, VSpace(0),
  278.                             End,
  279.                         Child, VGroup, GroupFrameT("Numeric Buttons"),
  280.                             Child, VSpace(0),
  281.                             Child, HGroup, GroupSpacing(0),
  282.                                 Child, HSpace(0),
  283.                                 Child, ColGroup(4), MUIA_Group_VertSpacing, 1,
  284.                                     Child, VSpace(0),
  285.                                     Child, CLabel("Left"),
  286.                                     Child, CLabel("Right"),
  287.                                     Child, CLabel("SPL"),
  288.                                     Child, Label1("Low:"),
  289.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,0,100,"%3ld %%"),
  290.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,0,100,"%3ld %%"),
  291.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,30,99,"%2ld dB"),
  292.                                     Child, Label1("Mid:"),
  293.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,0,100,"%3ld %%"),
  294.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,0,100,"%3ld %%"),
  295.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,30,99,"%2ld dB"),
  296.                                     Child, Label1("High:"),
  297.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,0,100,"%3ld %%"),
  298.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,0,100,"%3ld %%"),
  299.                                     Child, MUI_MakeObject(MUIO_NumericButton,NULL,30,99,"%2ld dB"),
  300.                                     End,
  301.                                 Child, HSpace(0),
  302.                                 End,
  303.                             Child, VSpace(0),
  304.                             End,
  305.                         End,
  306.  
  307.                     End,
  308.  
  309.                 Child, VSpace(4),
  310.  
  311.                 Child, ColGroup(2),
  312.                     Child, Label("Slidorama Rating:"),
  313.                     Child, NewObject(RatingClass->mcc_Class,0,
  314.                         MUIA_Numeric_Value,50,
  315.                         MUIA_CycleChain, 1,
  316.                         TAG_DONE),
  317.                     End,
  318.                 End,
  319.             End,
  320.         End;
  321.  
  322.     if (!app)
  323.         fail(app,"Failed to create Application.");
  324.  
  325.  
  326.  
  327.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  328.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  329.  
  330. /*
  331. ** This is the ideal input loop for an object oriented MUI application.
  332. ** Everything is encapsulated in classes, no return ids need to be used,
  333. ** we just check if the program shall terminate.
  334. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  335. ** from Wait() (or 0). This makes the input loop significantly faster.
  336. */
  337.  
  338.     set(window,MUIA_Window_Open,TRUE);
  339.  
  340.     {
  341.         ULONG sigs = 0;
  342.  
  343.         while (DoMethod(app,MUIM_Application_NewInput,&sigs) != MUIV_Application_ReturnID_Quit)
  344.         {
  345.             if (sigs)
  346.             {
  347.                 sigs = Wait(sigs | SIGBREAKF_CTRL_C);
  348.                 if (sigs & SIGBREAKF_CTRL_C) break;
  349.             }
  350.         }
  351.     }
  352.  
  353.     set(window,MUIA_Window_Open,FALSE);
  354.  
  355.  
  356. /*
  357. ** Shut down...
  358. */
  359.  
  360.     MUI_DisposeObject(app);
  361.     CleanupClasses();
  362.     fail(NULL,NULL);
  363. }
  364.