home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / MUI / MCC_VLab / Developer / c / examples / vlabwinclass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  12.3 KB  |  405 lines

  1. /*
  2. ** vlabwinclass.c - a module in VLab-Demo
  3. ** © Steve Quartly 1999
  4. **
  5. ** This is a MUI Custom Class.
  6. ** The class is a sub class of MUIC_Window.
  7. **
  8. ** This is the main window in this demo program.
  9. **
  10. ** For the monitor to work, or grabbed images to be displayed,
  11. ** either CyberGraphx or guigfx.library must be installed.
  12. **
  13. ** You do not need these libraries to grab.
  14. **
  15. */
  16.  
  17. #include <clib/alib_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/graphics_protos.h>
  20. #include <clib/utility_protos.h>
  21. #include <clib/muimaster_protos.h>
  22. #include <pragmas/muimaster_pragmas.h>
  23. #include <libraries/mui.h>
  24. #include <libraries/iffparse.h>
  25. #include <stdio.h>
  26.  
  27. #include <mui/vlab_mcc.h>
  28.  
  29. #include "vlabwinclass.h"
  30.  
  31. /* Prototypes.*/
  32. ULONG __saveds __asm VLabWinDispatcher( register __a0 struct IClass *cl, register __a2 Object *obj, register __a1 Msg msg );
  33. ULONG __saveds VLabWinNew( struct IClass *cl, Object *obj, struct opSet *msg );
  34. ULONG __saveds VLabWinOwnMonitorOn( struct IClass *cl, Object *obj, Msg msg );
  35. ULONG __saveds VLabWinOwnMonitorOff( struct IClass *cl, Object *obj, Msg msg );
  36. ULONG __saveds VLabWinOwnMonitorRun( struct IClass *cl, Object *obj, Msg msg );
  37. ULONG __saveds VLabWinOwnMonitorStop( struct IClass *cl, Object *obj, Msg msg );
  38. ULONG __saveds VLabWinGrab( struct IClass *cl, Object *obj, Msg msg );
  39. ULONG __saveds VLabWinDispose( struct IClass *cl, Object *obj, Msg msg );
  40.  
  41. extern struct Library *MUIMasterBase;
  42.  
  43. extern struct MUI_CustomClass *mccVLabWin;
  44.  
  45. ULONG __stdargs DoSuperNew( struct IClass *cl,Object *obj,ULONG tag1,... )
  46. {
  47.     return( DoSuperMethod( cl, obj, OM_NEW, &tag1, NULL ) );
  48. }
  49.  
  50. /*
  51. **
  52. ** The dispatcher for our custom class, VLabWinClass.
  53. **
  54. */
  55. ULONG __saveds __asm VLabWinDispatcher( register __a0 struct IClass *cl, register __a2 Object *obj, register __a1 Msg msg )
  56. {
  57.     switch ( msg->MethodID )
  58.     {
  59.         case OM_NEW: return( VLabWinNew( cl, obj, ( APTR )msg ) );
  60.         case OM_DISPOSE: return( VLabWinDispose( cl, obj, ( APTR )msg ) );
  61.         case MUIM_VLabWin_OwnMonitorOn: return( VLabWinOwnMonitorOn( cl, obj, ( APTR )msg ) );
  62.         case MUIM_VLabWin_OwnMonitorOff: return( VLabWinOwnMonitorOff( cl, obj, ( APTR )msg ) );
  63.         case MUIM_VLabWin_OwnMonitorRun: return( VLabWinOwnMonitorRun( cl, obj, ( APTR )msg ) );
  64.         case MUIM_VLabWin_OwnMonitorStop: return( VLabWinOwnMonitorStop( cl, obj, ( APTR )msg ) );
  65.         case MUIM_VLabWin_Grab: return( VLabWinGrab( cl, obj, ( APTR )msg ) );
  66.     }
  67.  
  68.     return( DoSuperMethodA( cl, obj, ( Msg )msg ) );
  69. }
  70.  
  71. /*
  72. **
  73. ** This is called by OM_NEW.
  74. **
  75. */
  76.  
  77. ULONG __saveds VLabWinNew( struct IClass *cl, Object *obj, struct opSet *msg )
  78. {
  79.     struct VLabWinData *data;
  80.  
  81.     Object *vlabon, *vlaboff, *vlabrun, *vlabstop;
  82.     Object *ownon, *ownoff, *ownrun, *ownstop, *grab;
  83.     Object *mainGroup, *buttonGroup, *vlabObj;
  84.  
  85.     /* Get the object pointer to the VLab.mcc object.*/
  86.     vlabObj = ( Object * )GetTagData( VLAB_Object, NULL, msg->ops_AttrList );
  87.  
  88.     /* Build the main window.*/
  89.     if ( obj = ( Object * ) DoSuperNew( cl, obj,
  90.             MUIA_Window_Title, "VLab-Demo",
  91.             MUIA_Window_ID, MAKE_ID('V','L','A','B'),
  92.             WindowContents, mainGroup = HGroup,
  93.  
  94.                 /* The group with all the buttons.*/
  95.                 Child, buttonGroup = VGroup,
  96.                     Child, VGroup, GroupFrameT( "Monitor in VLab Window" ),
  97.                         Child, vlabon = KeyButton( "On", 'o'),
  98.                         Child, vlaboff = KeyButton( "Off", 'f'),
  99.                         Child, vlabrun = KeyButton( "Run", 'r'),
  100.                         Child, vlabstop = KeyButton( "Stop", 'p'),
  101.                     End,
  102.  
  103.                     Child, VGroup, GroupFrameT( "Monitor in Own Window" ),
  104.                         Child, ownon = KeyButton( "On", 'n'),
  105.                         Child, ownoff = KeyButton( "Off", 'h'),
  106.                         Child, ownrun = KeyButton( "Run", 'u'),
  107.                         Child, ownstop = KeyButton( "Stop", 't'),
  108.                     End,
  109.  
  110.                     Child, VGroup, GroupFrameT( "Grab Image" ),
  111.                         Child, grab = KeyButton("Grab", 'g'),
  112.                     End,
  113.  
  114.                 End,
  115.     End,
  116.  
  117.         TAG_MORE, ( ULONG ) msg->ops_AttrList ) )
  118.     {
  119.         data = ( struct VLabWinData * )INST_DATA( cl, obj );
  120.  
  121.         /* Store the pointers to certain object that we will require later
  122.              in our instance data.*/
  123.         data->vlabObj = vlabObj;
  124.         data->buttonGroup = buttonGroup;
  125.         data->mainGroup = mainGroup;
  126.  
  127.         /* Set up the notification on our buttons.*/
  128.         /* To get VLab.mcc to provide the window for the monitor, simply do this...*/
  129.         DoMethod( vlabon, MUIM_Notify, MUIA_Pressed, FALSE, data->vlabObj, 1, MUIM_VLab_MonitorOn );
  130.  
  131.         /* Do this to close the window...*/
  132.         DoMethod( vlaboff, MUIM_Notify, MUIA_Pressed, FALSE, data->vlabObj, 1, MUIM_VLab_MonitorOff );
  133.  
  134.         /* Do this to run and stop the monitor...*/
  135.         DoMethod( vlabrun, MUIM_Notify, MUIA_Pressed, FALSE, data->vlabObj, 3, MUIM_Set, MUIA_VLab_Monitor, MUIV_VLab_MonitorRun );
  136.         DoMethod( vlabstop, MUIM_Notify, MUIA_Pressed, FALSE, data->vlabObj, 3, MUIM_Set, MUIA_VLab_Monitor, MUIV_VLab_MonitorStop );
  137.  
  138.         /* To get a monitor object and display it in our own window, I invoke these methods
  139.              on ourself.*/
  140.         DoMethod( ownon, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_VLabWin_OwnMonitorOn );
  141.         DoMethod( ownoff, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_VLabWin_OwnMonitorOff );
  142.         DoMethod( ownrun, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_VLabWin_OwnMonitorRun );
  143.         DoMethod( ownstop, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_VLabWin_OwnMonitorStop );
  144.  
  145.         /* Same with grab, invoke this method on ourself.*/
  146.         DoMethod( grab, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_VLabWin_Grab );
  147.  
  148.         /* OM_NEW was successful..... return.*/
  149.         return ( ULONG )obj;
  150.     }
  151.  
  152.     /* OM_NEW failed..... invoke a dispose to tidy up and return.
  153.          In our case there will be nothing to do, but we might add something later
  154.          so it is better to leave it here.*/
  155.     CoerceMethod( cl, obj, OM_DISPOSE );
  156.  
  157.     return NULL;
  158. }
  159.  
  160. /*
  161. **
  162. ** This is called by OM_DISPOSE.
  163. **
  164. */
  165.  
  166. ULONG __saveds VLabWinDispose( struct IClass *cl, Object *obj, Msg msg )
  167. {
  168. //    struct VLabWinData *data = INST_DATA( cl, obj );
  169.  
  170.     /* Nothing to do in our dispose.*/
  171.  
  172.     return( DoSuperMethodA( cl, obj, ( Msg ) msg ) );
  173. }
  174.  
  175. /*
  176. **
  177. ** This turns on a monitor in our own window.
  178. **
  179. */
  180.  
  181. ULONG __saveds VLabWinOwnMonitorOn( struct IClass *cl, Object *obj, Msg msg )
  182. {
  183.     struct VLabWinData *data = INST_DATA( cl, obj );
  184.  
  185.     /* Only turn it on if it doesn't already exist.*/
  186.     /* Of course you can open as many monitor's as you want!! (Pretty pointless really).*/
  187.     if ( !data->ownMonitor )
  188.     {
  189.         /* Ask VLab.mcc for a monitor object pointer.
  190.              It is now OUR responsibility to dispose of the monitor object (data->ownMonitor)
  191.              when we have finished with it.*/
  192.         get( data->vlabObj, MUIA_VLab_MonitorObject, &data->ownMonitor );
  193.  
  194.         /* Now build a group to add to our main window.*/
  195.         data->monitorGroup = VGroup, GroupFrameT( "VLab Monitor" ),
  196.  
  197.             /* For spacing only.*/
  198.             Child, RectangleObject, End,
  199.  
  200.             /* Add our monitor.*/
  201.             Child, data->ownMonitor,
  202.  
  203.             /* Somewhere to display text.*/
  204.             Child, data->monitorText = TextObject,
  205.                 TextFrame,
  206.                 MUIA_Background, MUII_TextBack,
  207.                 MUIA_Text_Contents, "\33cRunning...", 
  208.                 MUIA_Text_SetMax, FALSE,
  209.             End,
  210.  
  211.             /* Spacing again.*/
  212.             Child, RectangleObject, End,
  213.  
  214.         End;
  215.  
  216.         /* If it created successfully.*/
  217.         if ( data->monitorGroup )
  218.         {
  219.             /* Put the main group into an InitChange so we can dynamically add objects to it.*/
  220.             if ( DoMethod( data->mainGroup, MUIM_Group_InitChange ) )
  221.             {
  222.                 /* Add our newly created monitor group.*/
  223.                 DoMethod( data->mainGroup, OM_ADDMEMBER, data->monitorGroup );
  224.  
  225.                 /* Sort the group so the buttons are on the left and the monitor on the right.*/
  226.                 DoMethod( data->mainGroup, MUIM_Group_Sort, data->buttonGroup, data->monitorGroup, NULL );
  227.  
  228.                 /* Put the main group into ExitChange, we have finished adding our object(s).*/
  229.                 DoMethod( data->mainGroup, MUIM_Group_ExitChange );
  230.             }
  231.  
  232.             /* The monitor is now being displayed in the main window.*/
  233.  
  234.         }
  235.  
  236.         /* Ooops, the monitor group failed for some reason...*/
  237.         else
  238.         {
  239.             /*... but the monitor may still have been created, so we have to dispose of it.*/
  240.             if ( data->ownMonitor ) MUI_DisposeObject( data->ownMonitor );
  241.  
  242.             /* We should return some error to the user here.*/
  243.         }
  244.     }
  245.  
  246.     return NULL;
  247. }
  248.  
  249. /*
  250. **
  251. ** This turns off the monitor in our own window.
  252. **
  253. */
  254.  
  255. ULONG __saveds VLabWinOwnMonitorOff( struct IClass *cl, Object *obj, Msg msg )
  256. {
  257.     struct VLabWinData *data = INST_DATA( cl, obj );
  258.  
  259.     /* Only turn it off if it is on!*/
  260.     if ( data->ownMonitor )
  261.     {
  262.         /* Put the main group into an InitChange so we can dynamically remove object from it.*/ 
  263.         if ( DoMethod( data->mainGroup, MUIM_Group_InitChange ) )
  264.         {
  265.             /* Remove our monitor group.*/
  266.             DoMethod( data->mainGroup, OM_REMMEMBER, data->monitorGroup );
  267.  
  268.             /* Put the main group into ExitChange, we have finished removing our object(s).*/
  269.             DoMethod( data->mainGroup, MUIM_Group_ExitChange );
  270.         }
  271.  
  272.         /* Dispose of the monitor group. We don't have to dispose of our monitor object
  273.              (data->ownMonitor) seperately as it is a child of this group and will be
  274.              automatically disposed with this group.*/
  275.         MUI_DisposeObject( data->monitorGroup );
  276.  
  277.         data->ownMonitor = NULL;
  278.     }
  279.  
  280.     return TRUE;
  281. }
  282.  
  283. /*
  284. **
  285. ** This set the monitor in our own window to 'run' mode.
  286. **
  287. */
  288.  
  289. ULONG __saveds VLabWinOwnMonitorRun( struct IClass *cl, Object *obj, Msg msg )
  290. {
  291.     struct VLabWinData *data = INST_DATA( cl, obj );
  292.  
  293.     /* Set the monitor to run..... easy!*/
  294.     set( data->ownMonitor, MUIA_VLab_Monitor, MUIV_VLab_MonitorRun );
  295.  
  296.     /* Modify the text below the monitor accordingly.*/
  297.     set( data->monitorText, MUIA_Text_Contents, "\33cRunning..." );
  298.  
  299.     return NULL;
  300. }
  301.  
  302. /*
  303. **
  304. ** This set the monitor in our own window to 'stop' mode.
  305. **
  306. */
  307.  
  308. ULONG __saveds VLabWinOwnMonitorStop( struct IClass *cl, Object *obj, Msg msg )
  309. {
  310.     struct VLabWinData *data = INST_DATA( cl, obj );
  311.  
  312.     /* Set the monitor to stop..... easy!*/
  313.     set( data->ownMonitor, MUIA_VLab_Monitor, MUIV_VLab_MonitorStop );
  314.  
  315.     /* Modify the text below the monitor accordingly.*/
  316.     set( data->monitorText, MUIA_Text_Contents, "\33cStopped..." );
  317.  
  318.     return NULL;
  319. }
  320.  
  321. /*
  322. **
  323. ** Grab an image from the VLab card.
  324. **
  325. */
  326.  
  327. ULONG __saveds VLabWinGrab( struct IClass *cl, Object *obj, Msg msg )
  328. {
  329.     struct VLabWinData *data = INST_DATA( cl, obj );
  330.  
  331.     ULONG result;
  332.  
  333.     /* Firstly dispose of any old images.
  334.          This demo can only grab one at a time, of course you can have as many as you want.*/
  335.     if ( data->vlabImage )
  336.     {
  337.         /* Close the image window if it is open.*/
  338.         set( data->vlabImageWin, MUIA_Window_Open, FALSE );
  339.  
  340.         /* Remove it from the application.*/
  341.         DoMethod( _app( obj ), OM_REMMEMBER, data->vlabImageWin );
  342.  
  343.         /* Dispose of the window.... this will also dispose of the image (data->vlabImage)
  344.              as it is a child of the window and will be automatically disposed with this window.*/
  345.         MUI_DisposeObject( data->vlabImageWin );
  346.  
  347.         data->vlabImageWin = data->vlabImage = NULL;
  348.     }
  349.  
  350.     /* Set our main window to sleep so the user can't do anything while we are grabbing.*/
  351.     set( obj, MUIA_Window_Sleep, TRUE );
  352.  
  353.     /* Tell VLab.mcc to grab and image and store it in data->vlabImage.*/
  354.     result = DoMethod( data->vlabObj, MUIM_VLab_Grab, &data->vlabImage );
  355.  
  356.     /* If all went well, we now have a image. We can do whatever we want with it now.
  357.          In this demo I simply attach it to a window and display it, of course you can
  358.          request an RGB pointer to it (DoMethod( data->vlabImage, MUIM_VLabImage_ObtainRGBPointer))
  359.          and do what you want, eg save it.
  360.          *NOTE* If you want to change the buffer, ie rotate it, scale it etc.. you **MUST** copy
  361.          the buffer first.
  362.          DO NOT MODIFY THE SOURCE RGB BUFFER PASSED TO YOU, COPY IT FIRST.*/
  363.     if ( result == VLABERR_OK )
  364.     {
  365.         /* We now have an image!.
  366.              Let's build the window and display it....*/
  367.         data->vlabImageWin = WindowObject,
  368.             MUIA_Window_Title, "VLab Image",
  369.             MUIA_Window_Width, MUIV_Window_Width_Default,
  370.             MUIA_Window_Height, MUIV_Window_Height_Default,
  371.             WindowContents, VGroup,
  372.  
  373.                 /* The group with the scroll bars.*/
  374.                 Child, ScrollgroupObject,
  375.                     /* The virtual group containing the image.*/
  376.                     MUIA_Scrollgroup_Contents, VirtgroupObject,
  377.                         VirtualFrame,
  378.                         /* The image.*/
  379.                         Child, data->vlabImage,
  380.                     End,
  381.                 End,
  382.             End,
  383.  
  384.          TAG_DONE );
  385.  
  386.         if ( data->vlabImageWin )
  387.       {
  388.             /* Add the window to the application.*/
  389.             DoMethod( _app( obj ), OM_ADDMEMBER, data->vlabImageWin );
  390.  
  391.             /* Open it.*/
  392.             set( data->vlabImageWin, MUIA_Window_Open, TRUE );
  393.  
  394.             /* Set notification on the close gaddet.*/
  395.             DoMethod( data->vlabImageWin, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, data->vlabImageWin, 3, MUIM_Set, MUIA_Window_Open, FALSE );
  396.         }
  397.     }
  398.  
  399.     /* Wake up the main window.*/
  400.     set( obj, MUIA_Window_Sleep, FALSE );
  401.  
  402.     return NULL;
  403. }
  404.  
  405.