home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickTime / Programming Stuff / Documentation / develop articles / develop Issue 13 / Inside QuickTime and Components / Capture Component / Capture Component Code / NuMathComponent.c < prev    next >
Encoding:
Text File  |  1997-02-26  |  8.7 KB  |  328 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        NuMathComponent.c
  3.  
  4.     Contains:    NuMath component routines.
  5.  
  6.     Written by:    Gary Woodcock
  7.  
  8.     Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12. */
  13.  
  14. //-----------------------------------------------------------------------
  15. // Includes
  16.  
  17. #include "MathComponent.h"
  18. #include "MathComponentPrivate.h"
  19. #include "NuMathComponentPrivate.h"
  20. #include <FixMath.h>
  21.  
  22. //-----------------------------------------------------------------------
  23.  
  24. #ifdef DEBUG_IT
  25.  
  26. // Use this declaration when we're running linked (for debugging)
  27. pascal    ComponentResult    NuMathDispatcher    (ComponentParameters    *params,
  28.                                              Handle                    storage)
  29.                                              
  30. #else
  31.  
  32. // Use this declaration when we're a standalone component
  33. pascal    ComponentResult    main    (ComponentParameters    *params,
  34.                                  Handle                    storage)
  35.  
  36. #endif DEBUG_IT
  37.  
  38. {
  39.     // This routine is the main dispatcher for the NuMath component
  40.     
  41.     ComponentResult    result = noErr;
  42.     PrivateGlobals**    globals=(PrivateGlobals**)storage; 
  43.  
  44.     // Did we get a Component Manager request code (< 0)?
  45.     if (params->what < 0)
  46.     {
  47.         switch (params->what)
  48.         {
  49.             case kComponentOpenSelect:        // Open request
  50.             {
  51.                 result = CallComponentFunctionWithStorage (storage, params,
  52.                     (ComponentFunction) _NuMathOpen);
  53.                 break;
  54.             }
  55.             case kComponentCloseSelect:        // Close request
  56.             {
  57.                 result = CallComponentFunctionWithStorage (storage, params,
  58.                     (ComponentFunction) _NuMathClose);
  59.                 break;
  60.             }
  61.             case kComponentCanDoSelect:        // Can Do request
  62.             {
  63.                 result = CallComponentFunction (params, 
  64.                     (ComponentFunction) _NuMathCanDo);
  65.                 break;
  66.             }
  67.             case kComponentVersionSelect:    // Version request
  68.             {
  69.                 result = CallComponentFunction (params,
  70.                     (ComponentFunction) _NuMathVersion);
  71.                 break;
  72.             }
  73.             case kComponentRegisterSelect:    // Register request
  74.             {
  75.                 result = CallComponentFunction (params,
  76.                     (ComponentFunction) _NuMathRegister);
  77.                 break;
  78.             }
  79.             case kComponentTargetSelect:    // Target request not supported
  80.             default:                        // Unknown request
  81.             {
  82.                 result = paramErr;
  83.                 break;
  84.             }
  85.         }
  86.     }
  87.     else    // Was it one of our request codes?
  88.     {
  89.         if (globals)
  90.             DelegateComponentCall(params,(**globals).delegateComponentInstance);
  91.         else
  92.             result = paramErr;
  93.     }
  94.     return (result);
  95. }
  96.                                              
  97. //-----------------------------------------------------------------------
  98.  
  99. pascal    ComponentResult    _NuMathOpen    (Handle                storage,
  100.                                      ComponentInstance    self)
  101. {
  102.     ComponentResult        result = noErr;
  103.     PrivateGlobals**    globals; 
  104.  
  105.     // Can we open another instance?
  106.     if (CountComponentInstances ((Component) self) <= kMaxNuMathInstances)
  107.     {
  108.         // Did we get our storage?
  109.         globals = (PrivateGlobals**) NewHandleClear (sizeof (PrivateGlobals));
  110.         if (globals != nil)
  111.         {
  112.             ComponentDescription    delegateDesc;
  113.             Component                delegateComponent = nil;
  114.             ComponentInstance        delegateComponentInstance;
  115.             Component                selfComponent;
  116.             ComponentDescription    tempDesc;
  117.             
  118.             // Keep a reference to self
  119.             (*globals)->self = (Component) self;
  120.  
  121.             // Describe the component we want to capture
  122.             delegateDesc.componentType = 'mhlr';
  123.             delegateDesc.componentSubType = 'vide';
  124.             delegateDesc.componentManufacturer = 'appl';
  125.             delegateDesc.componentFlags = 0L;
  126.             delegateDesc.componentFlagsMask = 0L;
  127.             
  128.             // Keep track of which component we are by querying
  129.             // the component info; the componentFlagsMask will
  130.             // contain the component ID for the component we
  131.             // are requesting info for
  132.             result = GetComponentInfo ((Component) self, &tempDesc, nil, nil, nil);
  133.             selfComponent = (Component)(tempDesc.componentFlagsMask);
  134.             
  135.             // Find the component we want to capture
  136.             do
  137.             {
  138.                 delegateComponent = FindNextComponent (delegateComponent, &delegateDesc);
  139.             }
  140.             while (delegateComponent == selfComponent);
  141.             
  142.             // Did we find one?
  143.             if (delegateComponent != 0L)
  144.             {
  145.                 // Can this component be captured (does it support the
  146.                 // target request code)?
  147.                 if (ComponentFunctionImplemented ((ComponentInstance) delegateComponent,
  148.                     kComponentTargetSelect))
  149.                 {
  150.                     // Capture it
  151.                     delegateComponent = CaptureComponent (delegateComponent, 
  152.                         (Component) self);
  153.                         
  154.                     // Keep references to the component we captured
  155.                     (*globals)->delegateComponent = delegateComponent;
  156.                     delegateComponentInstance = OpenComponent (delegateComponent);
  157.                     (*globals)->delegateComponentInstance = delegateComponentInstance;
  158.                     
  159.                     // Did we get an instance of the component we captured?
  160.                     if (delegateComponentInstance != 0L)
  161.                     {
  162.                         // Inform the component it has been captured
  163.                         result = ComponentSetTarget (delegateComponentInstance, self);
  164.                         SetComponentInstanceStorage (self, (Handle) globals);
  165.                     }
  166.                     else    // Couldn't get an instance of the delegate component
  167.                     {
  168.                         DisposHandle ((Handle) globals);
  169.                         result = kGenericError;
  170.                     }
  171.                 }
  172.                 else    // The component we need can't be captured
  173.                 {
  174.                     DisposHandle ((Handle) globals);
  175.                     result = kGenericError;
  176.                 }
  177.  
  178.             }
  179.             else    // Couldn't find the delegate component
  180.             {
  181.                 DisposHandle ((Handle) globals);
  182.                 result = kGenericError;
  183.             }
  184.         }
  185.         else    // NewHandleClear failed
  186.         {
  187.             result = MemError();
  188.         }
  189.     }
  190.     else    // No more instances can be opened
  191.     {
  192.         result = kGenericError;
  193.     }
  194.     return (result);
  195. }
  196.  
  197. //-----------------------------------------------------------------------
  198.  
  199. pascal    ComponentResult    _NuMathClose    (Handle                storage,
  200.                                          ComponentInstance    self)
  201. {
  202.     ComponentResult        result = noErr;
  203.     PrivateGlobals**    globals = (PrivateGlobals**) storage;
  204.     
  205.     // Do we have any clean up to do?
  206.     if (globals != nil)
  207.     {
  208.         // Any instances to close?
  209.         if ((*globals)->delegateComponentInstance != 0L)
  210.         {
  211.             // Close the captured component instance
  212.             result = CloseComponent ((*globals)->delegateComponentInstance);
  213.             (*globals)->delegateComponentInstance = 0L;
  214.             
  215.             // Uncapture the captured component (make it visible again)
  216.             result = UncaptureComponent ((*globals)->delegateComponent);
  217.             (*globals)->delegateComponent = 0L;
  218.         }
  219.         
  220.         // Dispose of globals
  221.         DisposHandle ((Handle) globals);
  222.     }
  223.     return (result);
  224. }
  225.  
  226. //-----------------------------------------------------------------------
  227.  
  228. pascal    ComponentResult    _NuMathCanDo    (short    selector)
  229. {
  230.     switch (selector)
  231.     {
  232.         // Component Manager request codes
  233.         case kComponentOpenSelect:
  234.         case kComponentCloseSelect:
  235.         case kComponentCanDoSelect:
  236.         case kComponentVersionSelect:
  237.         case kComponentRegisterSelect:
  238.         
  239.         // Math component request codes
  240.         case kDoDivideSelect:
  241.         case kDoMultiplySelect:
  242.         {
  243.             return (true);
  244.         }
  245.         
  246.         // Unsupported request codes
  247.         case kComponentTargetSelect:
  248.         default:
  249.         {
  250.             return (false);
  251.         }
  252.     }
  253. }
  254.  
  255. //-----------------------------------------------------------------------
  256.  
  257. pascal    ComponentResult    _NuMathVersion    (void)
  258. {
  259.     // Return the version info
  260.     return (nuMathInterfaceRevision);
  261. }
  262.  
  263. //-----------------------------------------------------------------------
  264.  
  265. pascal    ComponentResult    _NuMathRegister    (void)
  266. {
  267.     // See if a Math component is registered - if not, don't
  268.     // register this component, since it can't work without
  269.     // the Math component.  We return zero to register, one
  270.     // to not register.
  271.     ComponentDescription    mathDesc;
  272.     
  273.     mathDesc.componentType = 'mhlr';
  274.     mathDesc.componentSubType = 'vide';
  275.     mathDesc.componentManufacturer = 'appl';
  276.     mathDesc.componentFlags = 0L;
  277.     mathDesc.componentFlagsMask = 0L;
  278.     
  279.     return ((FindNextComponent (nil, &mathDesc) != 0L) ? 0L : 1L);
  280. }
  281.  
  282. //-----------------------------------------------------------------------
  283.  
  284. pascal    ComponentResult    _NuMathDoDivide    (short    numerator,
  285.                                          short    denominator,
  286.                                          short    *quotient)
  287. {
  288.     ComponentResult    result = noErr;
  289.     
  290.     // Check for zero denominator
  291.     if (denominator != 0)
  292.     {
  293.         *quotient = (short) Fix2Long 
  294.             (FixDiv (Long2Fix ((long) numerator), Long2Fix ((long) denominator)));
  295.     }
  296.     else    // Divide by zero not allowed
  297.     {
  298.         *quotient = 0;
  299.         result = kGenericError;
  300.     }
  301.     return (result);
  302. }
  303.  
  304. //-----------------------------------------------------------------------
  305.  
  306. pascal    ComponentResult    _NuMathDoMultiply    (Handle    storage,
  307.                                               short    firstNum,
  308.                                              short    secondNum,
  309.                                              short    *multiplicationResult)
  310. {
  311.     PrivateGlobals**    globals = (PrivateGlobals**) storage;
  312.         
  313.     // Note that we need access to the component globals because
  314.     // we are delegating the multiply function to the captured
  315.     // Math component, and the component instance of the captured
  316.     // Math component is stored in the NuMath component globals.  In
  317.     // the _MathDoMultiply function in the original Math component,
  318.     // we didn't require access to the component globals, and the
  319.     // interface for this function reflected this (there was no
  320.     // storage parameter).
  321.     return (DoMultiply ((*globals)->delegateComponentInstance, firstNum, 
  322.         secondNum, multiplicationResult));
  323. }
  324.  
  325. //-----------------------------------------------------------------------
  326.  
  327.  
  328.