static void HandleStartEvent(NavCBRecPtr callBackParms, CustomData *data)
{
   data->context = callBackParms->context;

   CreateUserPaneControl(callBackParms->window, &data->bounds, kControlSupportsEmbedding, &data->userpane);

   InstallControlEventHandler(data->userpane,
                              GetHandlePaneEventsUPP(),
                              GetEventTypeCount(namedAttrsList),
                              namedAttrsList,
                              data,
                              NULL);

   EmbedControl(data->choice, data->userpane);
   EmbedControl(data->button, data->userpane);
   
   NavCustomControl(callBackParms->context, kNavCtlAddControl, data->userpane);
   
   HandleAdjustRect(callBackParms, data);
   
   if (data && !(data->defaultLocation).IsEmpty())
   {
      // Set default location for the modern Navigation APIs
      // Apple Technical Q&A 1151
      FSSpec theFSSpec;
      wxMacFilename2FSSpec(data->defaultLocation, &theFSSpec);
      AEDesc theLocation = {typeNull, NULL};
      if (noErr == ::AECreateDesc(typeFSS, &theFSSpec, sizeof(FSSpec), &theLocation))
         ::NavCustomControl(callBackParms->context, kNavCtlSetLocation, (void *) &theLocation);
   }
}
OSStatus CreateYASTControl(WindowRef theWindow, Rect *bounds, ControlRef *theControl) {
	UInt32 featurSet;
	ControlRef theNewControl;
	OSStatus err;
	
		/* feature flags for our control. */
	featurSet = kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
			| kControlWantsActivate | kControlHasSpecialBackground
			| kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
			
		/* create the control */
	err = CreateUserPaneControl( theWindow,  bounds, featurSet, &theNewControl);
	if (err == noErr) {
			/* set up the txn features */
		err = YASTControlAttachToExistingControl(theNewControl);
		if (err == noErr) {
			*theControl = theNewControl;
		} else {
			DisposeControl(theNewControl);
		}
	}
		/* all done.... */
	return err;
}
IGraphicsCarbon::IGraphicsCarbon(IGraphicsMac* pGraphicsMac,
                                 WindowRef pWindow,
                                 ControlRef pParentControl,
                                 short leftOffset,
                                 short topOffset)
  : mGraphicsMac(pGraphicsMac)
  , mWindow(pWindow)
  , mView(0)
  , mTimer(0)
  , mControlHandler(0)
  , mWindowHandler(0)
  , mCGC(0)
  , mTextEntryView(0)
  , mTextEntryHandler(0)
  , mEdControl(0)
  , mEdParam(0)
  , mPrevX(0)
  , mPrevY(0)
//, mRgn(NewRgn())
  , mLeftOffset(leftOffset)
  , mTopOffset(topOffset)
{
  TRACE;

  Rect r;   // Client.
  r.left = r.top = 0;
  r.right = pGraphicsMac->Width();
  r.bottom = pGraphicsMac->Height();

  WindowAttributes winAttrs = 0;
  GetWindowAttributes(pWindow, &winAttrs);
  mIsComposited = (winAttrs & kWindowCompositingAttribute);

  UInt32 features =  kControlSupportsFocus | kControlHandlesTracking | kControlSupportsEmbedding;

  if (mIsComposited)
  {
    features |= kHIViewIsOpaque | kHIViewFeatureDoesNotUseSpecialParts;
  }

  CreateUserPaneControl(pWindow, &r, features, &mView);

  const EventTypeSpec controlEvents[] =
  {
    { kEventClassControl, kEventControlDraw },
  };

  InstallControlEventHandler(mView, MainEventHandler, GetEventTypeCount(controlEvents), controlEvents, this, &mControlHandler);

  const EventTypeSpec windowEvents[] =
  {
    { kEventClassMouse, kEventMouseDown },
    { kEventClassMouse, kEventMouseUp },
    { kEventClassMouse, kEventMouseMoved },
    { kEventClassMouse, kEventMouseDragged },
    { kEventClassMouse, kEventMouseWheelMoved },

    { kEventClassKeyboard, kEventRawKeyDown },

    { kEventClassWindow, kEventWindowDeactivated }
  };

  InstallWindowEventHandler(mWindow, MainEventHandler, GetEventTypeCount(windowEvents), windowEvents, this, &mWindowHandler);

  double t = kEventDurationSecond / (double) pGraphicsMac->FPS();

  OSStatus s = InstallEventLoopTimer(GetMainEventLoop(), 0., t, TimerHandler, this, &mTimer);

  if (mIsComposited)
  {
    if (!pParentControl)
    {
      HIViewRef hvRoot = HIViewGetRoot(pWindow);
      s = HIViewFindByID(hvRoot, kHIViewWindowContentID, &pParentControl);
    }

    s = HIViewAddSubview(pParentControl, mView);
  }
  else
  {
    if (!pParentControl)
    {
      if (GetRootControl(pWindow, &pParentControl) != noErr)
      {
        CreateRootControl(pWindow, &pParentControl);
      }
    }
    s = EmbedControl(mView, pParentControl);
  }

  if (s == noErr)
  {
    SizeControl(mView, r.right, r.bottom);  // offset?
  }
}
/*****************************************************
*
* Handle_WindowCommandProcess(inHandlerCallRef, inEvent, inUserData) 
*
* Purpose:  called to handle of the events generated by the various controls of the HICustomView_Tester window
*
* Inputs:   inHandlerCallRef    - reference to the current handler call chain
*           inEvent             - the event
*           inUserData          - app-specified data you passed in the call to InstallEventHandler
*
* Returns:  OSStatus            - noErr indicates the event was handled
*                                 eventNotHandledErr indicates the event was not handled and the Toolbox should take over
*/
static pascal OSStatus Handle_WindowCommandProcess(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
	{
	OSStatus status;
	HIViewRef customView = (HIViewRef)inUserData;
	
	// getting the command
	HICommandExtended aCommand;
	status = GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof(aCommand), NULL, &aCommand);
	require_noerr(status, ExitCommandProcess);
	status = eventNotHandledErr;

	// cheking that the command came from a control
	if ( ! (aCommand.attributes & kHICommandFromControl) ) goto ExitCommandProcess;

	switch (aCommand.commandID)
		{
		//
		// Asking for a refresh of the custom view
		//
		case 'SNDt':
			HIViewSetNeedsDisplay(customView, true);
			status = noErr;
			break;

		//
		// Setting the control value of the custom view
		//
		case 'SV00':
			SetControl32BitValue(customView, 0);
			status = noErr;
			break;
		case 'SV01':
			SetControl32BitValue(customView, 1);
			status = noErr;
			break;
		case 'SV17':
			SetControl32BitValue(customView, 17);
			status = noErr;
			break;
		case 'SVTH':
			SetControl32BitValue(customView, 1000);
			status = noErr;
			break;
		case 'SVet':
			{
			HIViewRef editText;
			HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kSetValueEditID, &editText);
			char buffer[11];
			Size actualSize;
			GetControlData(editText, kControlEntireControl, kControlEditTextTextTag, 10, buffer, &actualSize);
			if (actualSize > 10) actualSize = 10;
			buffer[actualSize] = 0;
			SetControl32BitValue(customView, atoi(buffer));
			}
			status = noErr;
			break;

		//
		// Setting the state of the custom view
		//
		case 'CHlt':
			// setting the hilite to non-0 also stomps the previous hilite state if any
			// and we don't want that in our testing
			if (GetControl32BitValue(aCommand.source.control) == 1)
				HiliteControl(customView, 1);
			else
				HiliteControl(customView, 0);
			status = noErr;
			break;
		case 'CEnb':
			{
			HIViewRef hiliteControl;
			HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kCheckHiliteID, &hiliteControl);
			if (GetControl32BitValue(aCommand.source.control) == 1)
				EnableControl(customView);
			else
				DisableControl(customView);
			UInt16 prevHilite = GetControlHilite(customView);
			if ((prevHilite == kControlInactivePart) || (prevHilite == kControlDisabledPart))
				DisableControl(hiliteControl);
			else
				EnableControl(hiliteControl);
			HIViewSetNeedsDisplay(customView, true);
			}
			status = noErr;
			break;
		case 'CAct':
			{
			HIViewRef hiliteControl;
			HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kCheckHiliteID, &hiliteControl);
			if (GetControl32BitValue(aCommand.source.control) == 1)
				ActivateControl(customView);
			else
				DeactivateControl(customView);
			UInt16 prevHilite = GetControlHilite(customView);
			if ((prevHilite == kControlInactivePart) || (prevHilite == kControlDisabledPart))
				DisableControl(hiliteControl);
			else
				EnableControl(hiliteControl);
			HIViewSetNeedsDisplay(customView, true);
			}
			status = noErr;
			break;

		//
		// Testing the custom view in or as a scroller in a HIScrollView
		//
		case 'CTiS':
		case 'CTaS':
			if (GetControl32BitValue(aCommand.source.control) == 1)
				{
				// create a HIScrollView and install it where and as the custom view was
				HIViewRef scrollView;
				status = HIScrollViewCreate(kHIScrollViewValidOptions, &scrollView);
				require_noerr(status, ExitCommandProcess);
				
				HIRect frame;
				status = HIViewGetFrame(customView, &frame);
				require_noerr(status, ExitCommandProcess);
				status = HIViewSetFrame(scrollView, &frame);
				require_noerr(status, ExitCommandProcess);

				HIViewSetLayoutInfo(scrollView, &kBindToParentLayout);
				HIViewSetLayoutInfo(customView, &kNoBindLayout);
				
				status = HIViewAddSubview(HIViewGetSuperview(customView), scrollView);
				require_noerr(status, ExitCommandProcess);
				
				if (aCommand.commandID == 'CTiS')
					{
					// if we are testing the custom view in a scroller, we embed it in a scrolling User Pane
					// that we embed in the HIScrollView
					Rect boundsRect = {0, 0, 1000, 1000};
					HIViewRef userPane;
					status = CreateUserPaneControl(NULL, &boundsRect, kControlSupportsEmbedding, &userPane);
					require_noerr(status, ExitCommandProcess);
					
					EventTypeSpec userPaneEvents[] =
						{
							{kEventClassScrollable, kEventScrollableGetInfo},
							{kEventClassScrollable, kEventScrollableScrollTo}
						};
					InstallControlEventHandler(userPane, UserPaneHandler, 2, userPaneEvents, userPane, NULL);

					status = HIViewAddSubview(scrollView, userPane);
					require_noerr(status, ExitCommandProcess);
					status = HIViewAddSubview(userPane, customView);
					require_noerr(status, ExitCommandProcess);

					HIViewSetVisible(userPane, true);
					}
				else
					{
					// else we just embed the custom view directly in the HIScrollView
					status = HIViewAddSubview(scrollView, customView);
					require_noerr(status, ExitCommandProcess);
					}
				
				HIViewSetVisible(scrollView, true);
				
				// the 2 modes are not compatible so we disable the other check box
				HIViewRef otherCheckToDisable;
				if (aCommand.commandID == 'CTiS')
					HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kCheckTestAsScrollID, &otherCheckToDisable);
				else
					HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kCheckTestInScrollID, &otherCheckToDisable);
				require_noerr(status, ExitCommandProcess);
				DisableControl(otherCheckToDisable);
				// if we reach here, status is already set to noErr so we don't set it again
				}
			else
				{
				// we remove the HIScrollView and set the custom view back to where and as it was
				HIViewRef scrollView;
				if (aCommand.commandID == 'CTiS')
					scrollView = HIViewGetSuperview(HIViewGetSuperview(customView));
				else
					scrollView = HIViewGetSuperview(customView);

				status = HIViewAddSubview(HIViewGetSuperview(scrollView), customView);
				require_noerr(status, ExitCommandProcess);

				HIRect frame;
				status = HIViewGetFrame(scrollView, &frame);
				require_noerr(status, ExitCommandProcess);
				status = HIViewSetFrame(customView, &frame);
				require_noerr(status, ExitCommandProcess);

				HIViewSetLayoutInfo(customView, &kBindToParentLayout);

				// by releasing the HIScrollView, we also release the scrolling User Pane if any
				// which was embedded inside
				CFRelease(scrollView);
				
				// we renable the other check box
				HIViewRef otherCheckToEnable;
				if (aCommand.commandID == 'CTiS')
					HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kCheckTestAsScrollID, &otherCheckToEnable);
				else
					HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kCheckTestInScrollID, &otherCheckToEnable);
				require_noerr(status, ExitCommandProcess);
				EnableControl(otherCheckToEnable);
				// if we reach here, status is already set to noErr so we don't set it again
				}
			break;
		}

ExitCommandProcess:

	return status;
	}   // Handle_WindowCommandProcess