Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
MyComponent.c
/* |
File: MyComponent.c |
Contains: simple component sample. |
Written by: John Wang |
Copyright: Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved. |
You may incorporate this Apple sample source code into your program(s) without |
restriction. This Apple sample source code has been provided "AS IS" and the |
responsibility for its operation is yours. You are not permitted to redistribute |
this Apple sample source code as "Apple sample source code" after having made |
changes. If you're going to re-distribute the source, we require that you make |
it clear in the source that the code was descended from Apple sample source |
code, but that you've made changes. |
Change History (most recent first): |
8/18/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1 |
03/14/94 JW Re-Created for Universal Headers. |
*/ |
#ifdef THINK_C |
#define applec |
#endif |
#include <Memory.h> |
#include <Errors.h> |
#include <Components.h> |
#include "MyComponent.h" |
#include "MyComponentRoutines.h" |
/* ------------------------------------------------------------------------- */ |
// Component entry point. |
#ifdef powerc |
ProcInfoType __procinfo=kPascalStackBased |
| RESULT_SIZE(SIZE_CODE(sizeof(ComponentResult))) |
| STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ComponentParameters*))) |
| STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char**))); |
#endif |
pascal ComponentResult main(ComponentParameters *params, char **storage) |
{ |
PrivateGlobals **myPrivateGlobals = (PrivateGlobals **) storage; |
long ret; |
if ( kDEBUGME ) |
DebugStr("\pIn main()"); |
if ( params->what < 0 ) { |
switch ( params->what ) { |
case kComponentOpenSelect: |
return ( CallComponentFunction(params, (ComponentFunctionUPP) MyOpen) ); |
case kComponentCloseSelect: |
return ( CallComponentFunctionWithStorage(storage, params, |
(ComponentFunctionUPP) MyClose) ); |
case kComponentCanDoSelect: |
ret = CallComponentFunction(params, (ComponentFunctionUPP) MyCanDo); |
if ( ret == false ) { |
DebugStr("\pIn kComponentCanDoSelect"); |
if ( (**myPrivateGlobals).delegate != 0 ) { |
ret = DelegateComponentCall(params, (**myPrivateGlobals).delegate); |
} |
} |
return ( ret ); |
case kComponentVersionSelect: |
return ( CallComponentFunction(params, (ComponentFunctionUPP) MyVersion) ); |
case kComponentRegisterSelect: |
return ( CallComponentFunction(params, (ComponentFunctionUPP) MyRegister) ); |
case kComponentTargetSelect: |
return ( CallComponentFunctionWithStorage(storage, params, |
(ComponentFunctionUPP) MyTarget) ); |
default: |
return ( paramErr ); |
} |
} else { |
switch ( params->what ) { |
case kMyProcedureSelect: |
return ( CallComponentFunctionWithStorage(storage, params, |
(ComponentFunctionUPP) MyProcedure) ); |
default: |
return( paramErr ); |
} |
} |
} |
/* ------------------------------------------------------------------------- */ |
// Required component calls. |
pascal ComponentResult MyOpen(ComponentInstance self) |
{ |
SharedGlobals *mySharedGlobals; |
PrivateGlobals **myPrivateGlobals; |
if ( kDEBUGME ) |
DebugStr("\pIn MyOpen()"); |
mySharedGlobals = nil; |
myPrivateGlobals = nil; |
// Create private variables. |
myPrivateGlobals = (PrivateGlobals **) NewHandleClear(sizeof(PrivateGlobals)); |
if ( myPrivateGlobals == nil ) |
goto bail; |
// Create shared variables if refcon == nil. |
mySharedGlobals=(SharedGlobals *)GetComponentRefcon((Component) self); |
if ( mySharedGlobals == nil ) { |
mySharedGlobals=(SharedGlobals *) NewPtrSysClear(sizeof(SharedGlobals)); |
if ( mySharedGlobals == nil ) |
goto bail; |
SetComponentRefcon((Component) self, (long) mySharedGlobals); |
// Initialize shared variables. |
mySharedGlobals->shared = 0; |
} |
// Initialize private variables. |
(**myPrivateGlobals).delegate = 0; |
(**myPrivateGlobals).self = self; |
// Since we've gotten here, everyt hings ok and we can set up the connection. |
SetComponentInstanceStorage(self, (Handle) myPrivateGlobals); |
return ( noErr ); |
bail: |
if ( myPrivateGlobals ) |
DisposeHandle((Handle) myPrivateGlobals); |
if ( mySharedGlobals ) |
DisposePtr((Ptr) mySharedGlobals); |
return ( memFullErr ); |
} |
pascal ComponentResult MyClose(Handle storage, ComponentInstance self) |
{ |
PrivateGlobals **myPrivateGlobals = (PrivateGlobals **) storage; |
SharedGlobals *mySharedGlobals; |
if ( kDEBUGME ) |
DebugStr("\pIn MyClose()"); |
// Dispose of private variables. |
if ( myPrivateGlobals ) |
DisposeHandle((Handle) myPrivateGlobals); |
// Dispose of globals variables if last instance. |
if ( CountComponentInstances((Component) self) == 1 ) { |
mySharedGlobals = (SharedGlobals *) GetComponentRefcon((Component) self); |
if ( mySharedGlobals != nil ) |
DisposePtr((Ptr) mySharedGlobals); |
} |
return ( noErr ); |
} |
pascal ComponentResult MyCanDo(short selector) |
{ |
if ( kDEBUGME ) |
DebugStr("\pIn MyCanDo()"); |
switch ( selector ) { |
// Required component calls. |
case kComponentOpenSelect: |
case kComponentCloseSelect: |
case kComponentCanDoSelect: |
case kComponentVersionSelect: |
case kComponentRegisterSelect: |
case kComponentTargetSelect: |
// MyComponent specific calls. |
case kMyProcedureSelect: |
return ( true ); |
// Not handled. |
default: |
return ( false ); |
} |
} |
pascal ComponentResult MyVersion() |
{ |
if ( kDEBUGME ) |
DebugStr("\pIn MyVersion()"); |
return ( (kMyComponentSpec<<16) | (kMyComponentVersion) ); |
} |
pascal ComponentResult MyRegister() |
{ |
if ( kDEBUGME ) |
DebugStr("\pIn MyRegister()"); |
return ( false ); |
} |
pascal ComponentResult MyTarget(Handle storage, ComponentInstance self) |
{ |
PrivateGlobals **myPrivateGlobals = (PrivateGlobals **) storage; |
if ( kDEBUGME ) |
DebugStr("\pIn MyTarget()"); |
// From now on, self will be the component instance that targeted us. |
(**myPrivateGlobals).self = self; |
return ( noErr ); |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14