home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-10-16 | 9.0 KB | 345 lines | [TEXT/KAHL] |
- /*
- File: NuMathComponent.c
-
- Contains: NuMath component routines.
-
- Written by: Gary Woodcock
-
- Copyright: © 1992 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- */
-
- //-----------------------------------------------------------------------
- // Includes
-
- #include "MathComponent.h"
- #include "MathComponentPrivate.h"
- #include "NuMathComponentPrivate.h"
- #include <FixMath.h>
-
- //-----------------------------------------------------------------------
-
- #ifdef DEBUG_IT
-
- // Use this declaration when we're running linked (for debugging)
- pascal ComponentResult NuMathDispatcher (ComponentParameters *params,
- Handle storage)
-
- #else
-
- // Use this declaration when we're a standalone component
- pascal ComponentResult main (ComponentParameters *params,
- Handle storage)
-
- #endif DEBUG_IT
-
- {
- // This routine is the main dispatcher for the NuMath component
-
- ComponentResult result = noErr;
-
- // Did we get a Component Manager request code (< 0)?
- if (params->what < 0)
- {
- switch (params->what)
- {
- case kComponentOpenSelect: // Open request
- {
- result = CallComponentFunctionWithStorage (storage, params,
- (ComponentFunction) _NuMathOpen);
- break;
- }
- case kComponentCloseSelect: // Close request
- {
- result = CallComponentFunctionWithStorage (storage, params,
- (ComponentFunction) _NuMathClose);
- break;
- }
- case kComponentCanDoSelect: // Can Do request
- {
- result = CallComponentFunction (params,
- (ComponentFunction) _NuMathCanDo);
- break;
- }
- case kComponentVersionSelect: // Version request
- {
- result = CallComponentFunction (params,
- (ComponentFunction) _NuMathVersion);
- break;
- }
- case kComponentRegisterSelect: // Register request
- {
- result = CallComponentFunction (params,
- (ComponentFunction) _NuMathRegister);
- break;
- }
- case kComponentTargetSelect: // Target request not supported
- default: // Unknown request
- {
- result = paramErr;
- break;
- }
- }
- }
- else // Was it one of our request codes?
- {
- switch (params->what)
- {
- case kDoDivideSelect: // Divide request
- {
- result = CallComponentFunction (params,
- (ComponentFunction) _NuMathDoDivide);
- break;
- }
-
- case kDoMultiplySelect: // Multiply request
- {
- result = CallComponentFunctionWithStorage (storage, params,
- (ComponentFunction) _NuMathDoMultiply);
- break;
- }
-
- default: // Unknown request
- {
- result = paramErr;
- break;
- }
- }
- }
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult _NuMathOpen (Handle storage,
- ComponentInstance self)
- {
- ComponentResult result = noErr;
- PrivateGlobals** globals;
-
- // Can we open another instance?
- if (CountComponentInstances ((Component) self) <= kMaxNuMathInstances)
- {
- // Did we get our storage?
- globals = (PrivateGlobals**) NewHandleClear (sizeof (PrivateGlobals));
- if (globals != nil)
- {
- ComponentDescription delegateDesc;
- Component delegateComponent = nil;
- ComponentInstance delegateComponentInstance;
- Component selfComponent;
- ComponentDescription tempDesc;
-
- // Keep a reference to self
- (*globals)->self = (Component) self;
-
- // Describe the component we want to capture
- delegateDesc.componentType = mathComponentType;
- delegateDesc.componentSubType = 0L;
- delegateDesc.componentManufacturer = 'appl';
- delegateDesc.componentFlags = 0L;
- delegateDesc.componentFlagsMask = 0L;
-
- // Keep track of which component we are by querying
- // the component info; the componentFlagsMask will
- // contain the component ID for the component we
- // are requesting info for
- result = GetComponentInfo ((Component) self, &tempDesc, nil, nil, nil);
- selfComponent = (Component)(tempDesc.componentFlagsMask);
-
- // Find the component we want to capture
- do
- {
- delegateComponent = FindNextComponent (delegateComponent, &delegateDesc);
- }
- while (delegateComponent == selfComponent);
-
- // Did we find one?
- if (delegateComponent != 0L)
- {
- // Can this component be captured (does it support the
- // target request code)?
- if (ComponentFunctionImplemented ((ComponentInstance) delegateComponent,
- kComponentTargetSelect))
- {
- // Capture it
- delegateComponent = CaptureComponent (delegateComponent,
- (Component) self);
-
- // Keep references to the component we captured
- (*globals)->delegateComponent = delegateComponent;
- delegateComponentInstance = OpenComponent (delegateComponent);
- (*globals)->delegateComponentInstance = delegateComponentInstance;
-
- // Did we get an instance of the component we captured?
- if (delegateComponentInstance != 0L)
- {
- // Inform the component it has been captured
- result = ComponentSetTarget (delegateComponentInstance, self);
- SetComponentInstanceStorage (self, (Handle) globals);
- }
- else // Couldn't get an instance of the delegate component
- {
- DisposHandle ((Handle) globals);
- result = kGenericError;
- }
- }
- else // The component we need can't be captured
- {
- DisposHandle ((Handle) globals);
- result = kGenericError;
- }
-
- }
- else // Couldn't find the delegate component
- {
- DisposHandle ((Handle) globals);
- result = kGenericError;
- }
- }
- else // NewHandleClear failed
- {
- result = MemError();
- }
- }
- else // No more instances can be opened
- {
- result = kGenericError;
- }
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult _NuMathClose (Handle storage,
- ComponentInstance self)
- {
- ComponentResult result = noErr;
- PrivateGlobals** globals = (PrivateGlobals**) storage;
-
- // Do we have any clean up to do?
- if (globals != nil)
- {
- // Any instances to close?
- if ((*globals)->delegateComponentInstance != 0L)
- {
- // Close the captured component instance
- result = CloseComponent ((*globals)->delegateComponentInstance);
- (*globals)->delegateComponentInstance = 0L;
-
- // Uncapture the captured component (make it visible again)
- result = UncaptureComponent ((*globals)->delegateComponent);
- (*globals)->delegateComponent = 0L;
- }
-
- // Dispose of globals
- DisposHandle ((Handle) globals);
- }
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult _NuMathCanDo (short selector)
- {
- switch (selector)
- {
- // Component Manager request codes
- case kComponentOpenSelect:
- case kComponentCloseSelect:
- case kComponentCanDoSelect:
- case kComponentVersionSelect:
- case kComponentRegisterSelect:
-
- // Math component request codes
- case kDoDivideSelect:
- case kDoMultiplySelect:
- {
- return (true);
- }
-
- // Unsupported request codes
- case kComponentTargetSelect:
- default:
- {
- return (false);
- }
- }
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult _NuMathVersion (void)
- {
- // Return the version info
- return (nuMathInterfaceRevision);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult _NuMathRegister (void)
- {
- // See if a Math component is registered - if not, don't
- // register this component, since it can't work without
- // the Math component. We return zero to register, one
- // to not register.
- ComponentDescription mathDesc;
-
- mathDesc.componentType = mathComponentType;
- mathDesc.componentSubType = 0L;
- mathDesc.componentManufacturer = 'appl';
- mathDesc.componentFlags = 0L;
- mathDesc.componentFlagsMask = 0L;
-
- return ((FindNextComponent (nil, &mathDesc) != 0L) ? 0L : 1L);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult _NuMathDoDivide (short numerator,
- short denominator,
- short *quotient)
- {
- ComponentResult result = noErr;
-
- // Check for zero denominator
- if (denominator != 0)
- {
- *quotient = (short) Fix2Long
- (FixDiv (Long2Fix ((long) numerator), Long2Fix ((long) denominator)));
- }
- else // Divide by zero not allowed
- {
- *quotient = 0;
- result = kGenericError;
- }
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult _NuMathDoMultiply (Handle storage,
- short firstNum,
- short secondNum,
- short *multiplicationResult)
- {
- PrivateGlobals** globals = (PrivateGlobals**) storage;
-
- // Note that we need access to the component globals because
- // we are delegating the multiply function to the captured
- // Math component, and the component instance of the captured
- // Math component is stored in the NuMath component globals. In
- // the _MathDoMultiply function in the original Math component,
- // we didn't require access to the component globals, and the
- // interface for this function reflected this (there was no
- // storage parameter).
- return (DoMultiply ((*globals)->delegateComponentInstance, firstNum,
- secondNum, multiplicationResult));
- }
-
- //-----------------------------------------------------------------------
-
-
-