home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Documentation / develop / develop Issue 12 / develop 12 code / Components / NuMathComponent.c < prev    next >
Encoding:
Text File  |  1992-10-16  |  9.0 KB  |  345 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.     
  43.     // Did we get a Component Manager request code (< 0)?
  44.     if (params->what < 0)
  45.     {
  46.         switch (params->what)
  47.         {
  48.             case kComponentOpenSelect:        // Open request
  49.             {
  50.                 result = CallComponentFunctionWithStorage (storage, params,
  51.                     (ComponentFunction) _NuMathOpen);
  52.                 break;
  53.             }
  54.             case kComponentCloseSelect:        // Close request
  55.             {
  56.                 result = CallComponentFunctionWithStorage (storage, params,
  57.                     (ComponentFunction) _NuMathClose);
  58.                 break;
  59.             }
  60.             case kComponentCanDoSelect:        // Can Do request
  61.             {
  62.                 result = CallComponentFunction (params, 
  63.                     (ComponentFunction) _NuMathCanDo);
  64.                 break;
  65.             }
  66.             case kComponentVersionSelect:    // Version request
  67.             {
  68.                 result = CallComponentFunction (params,
  69.                     (ComponentFunction) _NuMathVersion);
  70.                 break;
  71.             }
  72.             case kComponentRegisterSelect:    // Register request
  73.             {
  74.                 result = CallComponentFunction (params,
  75.                     (ComponentFunction) _NuMathRegister);
  76.                 break;
  77.             }
  78.             case kComponentTargetSelect:    // Target request not supported
  79.             default:                        // Unknown request
  80.             {
  81.                 result = paramErr;
  82.                 break;
  83.             }
  84.         }
  85.     }
  86.     else    // Was it one of our request codes?
  87.     {
  88.         switch (params->what)
  89.         {
  90.             case kDoDivideSelect:            // Divide request
  91.             {
  92.                 result = CallComponentFunction (params, 
  93.                     (ComponentFunction) _NuMathDoDivide);
  94.                 break;
  95.             }
  96.             
  97.             case kDoMultiplySelect:            // Multiply request
  98.             {    
  99.                 result = CallComponentFunctionWithStorage (storage, params,
  100.                     (ComponentFunction) _NuMathDoMultiply);
  101.                 break;
  102.             }
  103.  
  104.             default:                        // Unknown request
  105.             {
  106.                 result = paramErr;
  107.                 break;
  108.             }
  109.         }
  110.     }
  111.     return (result);
  112. }
  113.                                              
  114. //-----------------------------------------------------------------------
  115.  
  116. pascal    ComponentResult    _NuMathOpen    (Handle                storage,
  117.                                      ComponentInstance    self)
  118. {
  119.     ComponentResult        result = noErr;
  120.     PrivateGlobals**    globals; 
  121.  
  122.     // Can we open another instance?
  123.     if (CountComponentInstances ((Component) self) <= kMaxNuMathInstances)
  124.     {
  125.         // Did we get our storage?
  126.         globals = (PrivateGlobals**) NewHandleClear (sizeof (PrivateGlobals));
  127.         if (globals != nil)
  128.         {
  129.             ComponentDescription    delegateDesc;
  130.             Component                delegateComponent = nil;
  131.             ComponentInstance        delegateComponentInstance;
  132.             Component                selfComponent;
  133.             ComponentDescription    tempDesc;
  134.             
  135.             // Keep a reference to self
  136.             (*globals)->self = (Component) self;
  137.  
  138.             // Describe the component we want to capture
  139.             delegateDesc.componentType = mathComponentType;
  140.             delegateDesc.componentSubType = 0L;
  141.             delegateDesc.componentManufacturer = 'appl';
  142.             delegateDesc.componentFlags = 0L;
  143.             delegateDesc.componentFlagsMask = 0L;
  144.             
  145.             // Keep track of which component we are by querying
  146.             // the component info; the componentFlagsMask will
  147.             // contain the component ID for the component we
  148.             // are requesting info for
  149.             result = GetComponentInfo ((Component) self, &tempDesc, nil, nil, nil);
  150.             selfComponent = (Component)(tempDesc.componentFlagsMask);
  151.             
  152.             // Find the component we want to capture
  153.             do
  154.             {
  155.                 delegateComponent = FindNextComponent (delegateComponent, &delegateDesc);
  156.             }
  157.             while (delegateComponent == selfComponent);
  158.             
  159.             // Did we find one?
  160.             if (delegateComponent != 0L)
  161.             {
  162.                 // Can this component be captured (does it support the
  163.                 // target request code)?
  164.                 if (ComponentFunctionImplemented ((ComponentInstance) delegateComponent,
  165.                     kComponentTargetSelect))
  166.                 {
  167.                     // Capture it
  168.                     delegateComponent = CaptureComponent (delegateComponent, 
  169.                         (Component) self);
  170.                         
  171.                     // Keep references to the component we captured
  172.                     (*globals)->delegateComponent = delegateComponent;
  173.                     delegateComponentInstance = OpenComponent (delegateComponent);
  174.                     (*globals)->delegateComponentInstance = delegateComponentInstance;
  175.                     
  176.                     // Did we get an instance of the component we captured?
  177.                     if (delegateComponentInstance != 0L)
  178.                     {
  179.                         // Inform the component it has been captured
  180.                         result = ComponentSetTarget (delegateComponentInstance, self);
  181.                         SetComponentInstanceStorage (self, (Handle) globals);
  182.                     }
  183.                     else    // Couldn't get an instance of the delegate component
  184.                     {
  185.                         DisposHandle ((Handle) globals);
  186.                         result = kGenericError;
  187.                     }
  188.                 }
  189.                 else    // The component we need can't be captured
  190.                 {
  191.                     DisposHandle ((Handle) globals);
  192.                     result = kGenericError;
  193.                 }
  194.  
  195.             }
  196.             else    // Couldn't find the delegate component
  197.             {
  198.                 DisposHandle ((Handle) globals);
  199.                 result = kGenericError;
  200.             }
  201.         }
  202.         else    // NewHandleClear failed
  203.         {
  204.             result = MemError();
  205.         }
  206.     }
  207.     else    // No more instances can be opened
  208.     {
  209.         result = kGenericError;
  210.     }
  211.     return (result);
  212. }
  213.  
  214. //-----------------------------------------------------------------------
  215.  
  216. pascal    ComponentResult    _NuMathClose    (Handle                storage,
  217.                                          ComponentInstance    self)
  218. {
  219.     ComponentResult        result = noErr;
  220.     PrivateGlobals**    globals = (PrivateGlobals**) storage;
  221.     
  222.     // Do we have any clean up to do?
  223.     if (globals != nil)
  224.     {
  225.         // Any instances to close?
  226.         if ((*globals)->delegateComponentInstance != 0L)
  227.         {
  228.             // Close the captured component instance
  229.             result = CloseComponent ((*globals)->delegateComponentInstance);
  230.             (*globals)->delegateComponentInstance = 0L;
  231.             
  232.             // Uncapture the captured component (make it visible again)
  233.             result = UncaptureComponent ((*globals)->delegateComponent);
  234.             (*globals)->delegateComponent = 0L;
  235.         }
  236.         
  237.         // Dispose of globals
  238.         DisposHandle ((Handle) globals);
  239.     }
  240.     return (result);
  241. }
  242.  
  243. //-----------------------------------------------------------------------
  244.  
  245. pascal    ComponentResult    _NuMathCanDo    (short    selector)
  246. {
  247.     switch (selector)
  248.     {
  249.         // Component Manager request codes
  250.         case kComponentOpenSelect:
  251.         case kComponentCloseSelect:
  252.         case kComponentCanDoSelect:
  253.         case kComponentVersionSelect:
  254.         case kComponentRegisterSelect:
  255.         
  256.         // Math component request codes
  257.         case kDoDivideSelect:
  258.         case kDoMultiplySelect:
  259.         {
  260.             return (true);
  261.         }
  262.         
  263.         // Unsupported request codes
  264.         case kComponentTargetSelect:
  265.         default:
  266.         {
  267.             return (false);
  268.         }
  269.     }
  270. }
  271.  
  272. //-----------------------------------------------------------------------
  273.  
  274. pascal    ComponentResult    _NuMathVersion    (void)
  275. {
  276.     // Return the version info
  277.     return (nuMathInterfaceRevision);
  278. }
  279.  
  280. //-----------------------------------------------------------------------
  281.  
  282. pascal    ComponentResult    _NuMathRegister    (void)
  283. {
  284.     // See if a Math component is registered - if not, don't
  285.     // register this component, since it can't work without
  286.     // the Math component.  We return zero to register, one
  287.     // to not register.
  288.     ComponentDescription    mathDesc;
  289.     
  290.     mathDesc.componentType = mathComponentType;
  291.     mathDesc.componentSubType = 0L;
  292.     mathDesc.componentManufacturer = 'appl';
  293.     mathDesc.componentFlags = 0L;
  294.     mathDesc.componentFlagsMask = 0L;
  295.     
  296.     return ((FindNextComponent (nil, &mathDesc) != 0L) ? 0L : 1L);
  297. }
  298.  
  299. //-----------------------------------------------------------------------
  300.  
  301. pascal    ComponentResult    _NuMathDoDivide    (short    numerator,
  302.                                          short    denominator,
  303.                                          short    *quotient)
  304. {
  305.     ComponentResult    result = noErr;
  306.     
  307.     // Check for zero denominator
  308.     if (denominator != 0)
  309.     {
  310.         *quotient = (short) Fix2Long 
  311.             (FixDiv (Long2Fix ((long) numerator), Long2Fix ((long) denominator)));
  312.     }
  313.     else    // Divide by zero not allowed
  314.     {
  315.         *quotient = 0;
  316.         result = kGenericError;
  317.     }
  318.     return (result);
  319. }
  320.  
  321. //-----------------------------------------------------------------------
  322.  
  323. pascal    ComponentResult    _NuMathDoMultiply    (Handle    storage,
  324.                                               short    firstNum,
  325.                                              short    secondNum,
  326.                                              short    *multiplicationResult)
  327. {
  328.     PrivateGlobals**    globals = (PrivateGlobals**) storage;
  329.         
  330.     // Note that we need access to the component globals because
  331.     // we are delegating the multiply function to the captured
  332.     // Math component, and the component instance of the captured
  333.     // Math component is stored in the NuMath component globals.  In
  334.     // the _MathDoMultiply function in the original Math component,
  335.     // we didn't require access to the component globals, and the
  336.     // interface for this function reflected this (there was no
  337.     // storage parameter).
  338.     return (DoMultiply ((*globals)->delegateComponentInstance, firstNum, 
  339.         secondNum, multiplicationResult));
  340. }
  341.  
  342. //-----------------------------------------------------------------------
  343.  
  344.  
  345.