/*
 *  paneHIView.pi.c
 *  PeekIt
 *
 *  Created by C.K. Haun on Sat Feb 22 2003.
 *  Copyright (c) 2003 Ravenware Software. All rights reserved.
 *
 */

#ifndef PIPaneView_H_
#define PIPaneView_H_

// #include "PeekIt.h"

/*
    File:		PIPaneView.c
 */


// #include <Carbon/Carbon.h>

// -----------------------------------------------------------------------------
// PIPaneView API
// -----------------------------------------------------------------------------
//
OSStatus PIPanesViewCreate(                            WindowRef				inWindow);





#endif // PIPaneView_H_
// -----------------------------------------------------------------------------
//	constants
// -----------------------------------------------------------------------------
//

#define kPIPaneViewClassID	CFSTR( "com.Ravenware.PIPaneView" )

const ControlPartCode	kControlOpaqueRegionMetaPart = -3;

// -----------------------------------------------------------------------------
//	types
// -----------------------------------------------------------------------------
//
typedef struct
{
	HIViewRef			view;

	// Geometry
        WindowRef theWindow;
        
} PIPaneViewData;

// -----------------------------------------------------------------------------
//	prototypes
// -----------------------------------------------------------------------------
//

OSStatus PIPaneViewRegister(void);
OSStatus PIPaneViewHandler(
	EventHandlerCallRef		inCallRef,
	EventRef				inEvent,
	void*					inUserData );
OSStatus PIPaneViewConstruct(
	EventRef				inEvent );
OSStatus PIPaneViewInitialize(
	EventHandlerCallRef		inCallRef,
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewDestruct(
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewDraw(
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewHitTest(
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewTrack(
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewChanged(
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewGetData(
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewSetData(
	EventRef				inEvent,
	PIPaneViewData*		inData );
OSStatus PIPaneViewGetRegion(
	EventRef				inEvent,
	PIPaneViewData*		inData );
ControlPartCode FindPart(
	const HIRect*			inBounds,
	const HIPoint*			inWhere,
	PIPaneViewData*		inData );
void DefaultDrawPart(
	ControlPartCode			inPart,
	const HIRect*			inPartRect
	);
void DefaultDrawPartLabel(
	ControlPartCode			inPart,
	const HIRect*			inPartRect
	);
void SetUpDateData(
	PIPaneViewData*		inData );
OSStatus WhichCellAndByte(UInt16 theView, piCell * theCell,PIMainWDataH nowCD,Point *thePoint);
void ZeroCell(piCell * one);
OSStatus UpdateSelRect( piCell * theCell,PIMainWDataH nowCD);
void HilitePISelection(UInt32 theID,PIMainWDataH nowCD);

// -----------------------------------------------------------------------------
//	globals
// -----------------------------------------------------------------------------
//

// -----------------------------------------------------------------------------
//	PIPaneViewCreate
// -----------------------------------------------------------------------------
//
OSStatus PIPanesViewCreate(	WindowRef			theWindow )
{
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PIMainWDataH nowCD = (PIMainWDataH)(*tempWC)->dataStore;

    
	OSStatus			err=noErr;
	ControlRef			root;
	EventRef			event;
	
	// Make sure this type of view is registered
	PIPaneViewRegister();

	// Make the initialization event
	err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize,
			GetCurrentEventTime(), 0, &event );
	
	// Set the bounds into the event
	err = SetEventParameter( event, 'Boun', typeQDRectangle,
			sizeof( Rect ), &(*nowCD)->rawTargetRect );
        err = SetEventParameter(event,'MYWR',typeVoidPtr,sizeof(WindowRef),&theWindow);
	err = HIObjectCreate( kPIPaneViewClassID, event, (HIObjectRef*) &(*nowCD)->rawPane );

	// Get the content root
	err = GetRootControl( theWindow, &root );
	
	// And stick this view into it
	err = HIViewAddSubview( root, (*nowCD)->rawPane );
	
	ReleaseEvent( event );

        // do it again
        err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize,
                           GetCurrentEventTime(), 0, &event );
        
	// Set the bounds into the event
        err = SetEventParameter( event, 'Boun', typeQDRectangle,
                                 sizeof( Rect ), &(*nowCD)->transTargetRect );
        err = SetEventParameter(event,'MYWR',typeVoidPtr,sizeof(WindowRef),&theWindow);

        err = HIObjectCreate( kPIPaneViewClassID, event, (HIObjectRef*) &(*nowCD)->transPane );
        err = HIViewAddSubview( root, (*nowCD)->transPane );
        ReleaseEvent( event );
        

	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewRegister
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewRegister(void)
{
	OSStatus				err = noErr;
	static HIObjectClassRef	sPIPaneViewClassRef = NULL;

	if ( sPIPaneViewClassRef == NULL )
	{
		EventTypeSpec		eventList[] = {
			{ kEventClassHIObject, kEventHIObjectConstruct },
			{ kEventClassHIObject, kEventHIObjectInitialize },
			{ kEventClassHIObject, kEventHIObjectDestruct },
			{ kEventClassControl, kEventControlInitialize },
			{ kEventClassControl, kEventControlDraw },
			{ kEventClassControl, kEventControlHitTest },
			{ kEventClassControl, kEventControlTrack },
			{ kEventClassControl, kEventControlValueFieldChanged },
			{ kEventClassControl, kEventControlHiliteChanged },
			{ kEventClassControl, kEventControlGetData },
			{ kEventClassControl, kEventControlSetData },
			{ kEventClassControl, kEventControlGetPartRegion },
                {                    kEventClassScrollable,kEventScrollableInfoChanged}

                };

   
		err = HIObjectRegisterSubclass(
			kPIPaneViewClassID,		// class ID
			kHIViewClassID,				// base class ID
			NULL,						// option bits
			PIPaneViewHandler,		// construct proc
			GetEventTypeCount( eventList ),
			eventList,
			NULL,						// construct data,
			&sPIPaneViewClassRef );
	}
	
	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewHandler
// -----------------------------------------------------------------------------
//	This is the bottleneck for incoming events
//
OSStatus PIPaneViewHandler(
	EventHandlerCallRef		inCallRef,
	EventRef				inEvent,
	void*					inUserData )
{
	OSStatus				err = eventNotHandledErr;
	UInt32					eventClass = GetEventClass( inEvent );
	UInt32					eventKind = GetEventKind( inEvent );
	PIPaneViewData*		data = (PIPaneViewData*) inUserData;

	switch ( eventClass )
	{
		case kEventClassHIObject:
		{
			switch ( eventKind )
			{
				case kEventHIObjectConstruct:
					err = PIPaneViewConstruct( inEvent );
					break;

				case kEventHIObjectInitialize:
					err = PIPaneViewInitialize( inCallRef, inEvent, data );
					break;

				case kEventHIObjectDestruct:
					// don't CallNextEventHandler!
					err = PIPaneViewDestruct( inEvent, data );
					break;
			}
		}
		break;
		
		case kEventClassControl:
		{
			switch ( eventKind )
			{
				case kEventControlInitialize:
					err = noErr;
					break;

				case kEventControlDraw:
					err = PIPaneViewDraw( inEvent, data );
					break;
				
				case kEventControlHitTest:
					err = PIPaneViewHitTest( inEvent, data );
					break;
				
				case kEventControlTrack:
					err = PIPaneViewTrack( inEvent, data );
					break;
				
				case kEventControlValueFieldChanged:
				case kEventControlHiliteChanged:
					err = PIPaneViewChanged( inEvent, data );
					break;

				case kEventControlGetData:
					err = PIPaneViewGetData( inEvent, data );
					break;

				case kEventControlSetData:
					err = PIPaneViewSetData( inEvent, data );
					break;
				
				case kEventControlGetPartRegion:
					err = PIPaneViewGetRegion( inEvent, data );
					break;
			}
		}
		break;
                case kEventClassScrollable:
                    // kEventScrollableInfoChanged
                    DoAbout();
                    break;
        }
	
	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewConstruct
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewConstruct(
	EventRef			inEvent )
{
    OSStatus			err=noErr;
	PIPaneViewData*	data;
        
	// don't CallNextEventHandler!
	data = (PIPaneViewData*) malloc( sizeof( PIPaneViewData ) );
	// Set up the default drawing callbacks

	
	// Keep a copy of the created HIViewRef
	err = GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef,
			NULL, sizeof( HIObjectRef ), NULL, (HIObjectRef*) &data->view );
  
        
	// Set the userData that will be used with all subsequent eventHandler calls
	err = SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr,
			sizeof( PIPaneViewData* ), &data ); 

	if ( err != noErr )
		free( data );

	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewDestruct
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewDestruct(
	EventRef			inEvent,
	PIPaneViewData*	inData )
{
#pragma unused( inEvent )
	// Clean up any allocated data
	free( inData );

	return noErr;
}

// -----------------------------------------------------------------------------
//	PIPaneViewInitialize
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewInitialize(
	EventHandlerCallRef	inCallRef,
	EventRef			inEvent,
	PIPaneViewData*	inData )
{
	OSStatus			err;
	Rect				bounds;
	UInt32				features = kControlSupportsDataAccess+kControlHasSpecialBackground+kControlSupportsDragAndDrop;
	// Let the base class initialization occur
	err = CallNextEventHandler( inCallRef, inEvent );
	require_noerr( err, TroubleInSuperClass );

	// Extract the initial view bounds from the event
	err = GetEventParameter( inEvent, 'Boun', typeQDRectangle,
			NULL, sizeof( Rect ), NULL, &bounds );
	require_noerr( err, ParameterMissing );
	       err = GetEventParameter(inEvent,'MYWR',typeVoidPtr,0,sizeof(WindowRef),NULL,&inData->theWindow);

	// Set up this view's feature bits
	err = SetEventParameter( inEvent, kEventParamControlFeatures, typeUInt32,
			sizeof( UInt32 ), &features );

	SetControlBounds( inData->view, &bounds );
        {
            WindowRef theWindow=inData->theWindow; // GetControlOwner(inData->view);
            WCH tempWC = (WCH)GetWRefCon(theWindow);
            PIMainWDataH nowCD = (PIMainWDataH)(*tempWC)->dataStore;
// set a flag
            (*nowCD)->startSel->hPos=64000;
            

        }
        
ParameterMissing:
TroubleInSuperClass:
	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewDraw
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewDraw(
	EventRef			inEvent,
	PIPaneViewData*	inData )
{
	OSStatus			err;
	HIRect				bounds;
        ControlID theID;
        CGrafPtr thePort;
        err = HIViewGetBounds( inData->view, &bounds );
#if 0
	// Get ready to do the CG drawing boogaloo!

	err = GetEventParameter( inEvent, kEventParamCGContextRef, typeCGContextRef,
			NULL, sizeof( CGContextRef ), NULL, &c );
        CGContextSelectFont(c, "courier", 25, kCGEncodingMacRoman);

        err = HIViewGetBounds( inData->view, &bounds );
	
	drawData.hilitePart = GetControlHilite( inData->view );

	// Figure out how tall a row should be
	( inData->drawProc)( kControlStructureMetaPart, &drawRect, &drawData );
#endif
        WCH tempWC = (WCH)GetWRefCon( inData->theWindow);
        PIMainWDataH  nowCD = (PIMainWDataH)(*tempWC)->dataStore;
        thePort= GetWindowPort( inData->theWindow);
        GetControlID(
                     (ControlRef)inData->view,
                     &theID);
                      switch(theID.id){
                          Rect tempRect;
                          HIRect theHIRect;
                          case 9023:
                                             
                              ColDrawRect(nil,kForRawData,inData->theWindow);
// and frame it now
                              RGBForeColor(&gBlack);
                              HIViewGetFrame(
                                             (*nowCD)->rawPane,
                                             &theHIRect);
                              tempRect.top = 0; //theHIRect.origin.y;
                              tempRect.left = 0; //theHIRect.origin.x;

                              tempRect.bottom = tempRect.top+theHIRect.size.height;
                              tempRect.right = tempRect.top+theHIRect.size.width;
//                              InsetRect(&tempRect,-1,-1);
                              FrameRect(&tempRect);
                              RGBForeColor(& gPrefs.theColors[kColTextColor]);
/*                              short 
                                  GetPortTextSize(CGrafPtr port) 
                              extern short 
                              GetPortTextFont(CGrafPtr port)  
   */                           

                              DrawData( inData->theWindow,kDrawRawData);
                              RGBForeColor(&gBlack);

                              HilitePISelection(theID.id,nowCD);
                              break;
                          case 9024:
                              ColDrawRect(nil,kForTransData, inData->theWindow);
                              RGBForeColor(&gBlack);
                              HIViewGetFrame(
                                             (*nowCD)->transPane,
                                             &theHIRect);
                              tempRect.top = 0; //theHIRect.origin.y;
                              tempRect.left = 0; //theHIRect.origin.x;

                              tempRect.bottom = tempRect.top+theHIRect.size.height;
                              tempRect.right = tempRect.top+theHIRect.size.width;
   //                           InsetRect(&tempRect,-1,-1);
                              FrameRect(&tempRect);
                              RGBForeColor(& gPrefs.theColors[kColTextColor]);

                              DrawData(  inData->theWindow,kDrawTransData);
                              RGBForeColor(&gBlack);
                              HilitePISelection(theID.id,nowCD);

                              break;
                      }
        

	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewHitTest
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewHitTest(
	EventRef			inEvent,
	PIPaneViewData*	inData )
{
	OSStatus			err;
	HIRect				bounds;
	HIPoint				where;
	ControlPartCode		part;

	err = GetEventParameter( inEvent, kEventParamMouseLocation, typeHIPoint,
			NULL, sizeof( HIPoint ), NULL, &where );
	require_noerr( err, ParameterMissing );

	err = HIViewGetBounds( inData->view, &bounds );

	part = FindPart( &bounds, &where, inData );

	err = SetEventParameter( inEvent, kEventParamControlPart, typeControlPartCode,
			sizeof( ControlPartCode ), &part ); 

ParameterMissing:
	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewTrack
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewTrack(
	EventRef			inEvent,
	PIPaneViewData*	inData )
{	ControlID theID;
    RgnHandle r1;
	OSStatus			err;
	ControlPartCode		part;
	Point				qdPt;
	MouseTrackingResult	mouseResult;
        CGrafPtr thePorter;
        WCH tempWC;
        PIMainWDataH nowCD;
        WindowRef theWindow = inData->theWindow;
        piCell theCell,tempCell;
        piCell *origCell;
        Rect tempRect;
        tempWC = (WCH)GetWRefCon(theWindow);
        nowCD = (PIMainWDataH)(*tempWC)->dataStore;


theWindow=GetControlOwner(inData->view);
thePorter = GetWindowPort(theWindow);
tempWC = (WCH)GetWRefCon(theWindow);
nowCD = (PIMainWDataH)(*tempWC)->dataStore;

GetControlID(
             (ControlRef)inData->view,
             &theID);
ZeroCell(&theCell);
ZeroCell(&tempCell);
origCell = (*nowCD)->startSel;

do	{

            
		err = TrackMouseLocation(thePorter, &qdPt, &mouseResult );
//    ZeroCell(&theCell);
  WhichCellAndByte(theID.id,&theCell,nowCD,&qdPt);
// kMouseTrackingMouseMoved later
  switch(mouseResult){
      case kMouseTrackingMouseDragged:

          break;        
  

  }
}while( mouseResult != kMouseTrackingMouseUp);

if(!IsEqualCell(origCell,&theCell))
{
    r1 = NewRgn();
// zap the old
    tempRect = (*nowCD)->startSel->rawRect;
    NormalizeRect(&(*nowCD)->rawTargetRect,&tempRect);
    RectRgn(r1,&tempRect);
    HIViewSetNeedsDisplayInRegion( (*nowCD)->rawPane,   r1,                                  true);
    tempRect = (*nowCD)->startSel->realRect;
    NormalizeRect(&(*nowCD)->transTargetRect,&tempRect);
    RectRgn(r1,&tempRect);
    HIViewSetNeedsDisplayInRegion((*nowCD)->transPane, r1, true);

// put the new in the right place
    
(*nowCD)->startSel->hPos=theCell.hPos;
(*nowCD)->startSel->vPos=theCell.vPos;
(*nowCD)->startSel->totalPos=theCell.totalPos;
(*nowCD)->startSel->byteHere=theCell.byteHere;
(*nowCD)->startSel->rawRect.top=theCell.rawRect.top;
(*nowCD)->startSel->rawRect.left=theCell.rawRect.left;
(*nowCD)->startSel->rawRect.bottom=theCell.rawRect.bottom;
(*nowCD)->startSel->rawRect.right=theCell.rawRect.right;

(*nowCD)->startSel->realRect.top=theCell.realRect.top;
(*nowCD)->startSel->realRect.left=theCell.realRect.left;
(*nowCD)->startSel->realRect.bottom=theCell.realRect.bottom;
(*nowCD)->startSel->realRect.right=theCell.realRect.right;


tempRect = (*nowCD)->startSel->rawRect;
NormalizeRect(&(*nowCD)->rawTargetRect,&tempRect);

RectRgn(r1,&tempRect);
HIViewSetNeedsDisplayInRegion(
                              (*nowCD)->rawPane,
                              r1,
                              true);

tempRect = (*nowCD)->startSel->realRect;
NormalizeRect(&(*nowCD)->transTargetRect,&tempRect);

RectRgn(r1,&tempRect);
    HIViewSetNeedsDisplayInRegion(
                                  (*nowCD)->transPane,
                                  r1,
                                  true);
    DisposeRgn(r1);
}
    err = SetEventParameter( inEvent, kEventParamControlPart, typeControlPartCode,
			sizeof( ControlPartCode ), &part ); 

	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewChanged
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewChanged(
	EventRef			inEvent,
	PIPaneViewData*	inData )
{
#pragma unused( inEvent )
	OSStatus			err = noErr;

	HIViewSetNeedsDisplay( inData->view, true );

	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewGetData
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewGetData(
	EventRef				inEvent,
	PIPaneViewData*		inData )
{
	OSStatus				err;
	ControlPartCode			part;
	OSType					tag;
	Ptr						ptr;
	Size					size;
	Size					outSize;
	
	err = GetEventParameter( inEvent, kEventParamControlPart, typeControlPartCode,
			NULL, sizeof( ControlPartCode ), NULL, &part );
	require_noerr( err, ParameterMissing );

	err = GetEventParameter( inEvent, kEventParamControlDataTag, typeEnumeration,
			NULL, sizeof( OSType ), NULL, &tag );
	require_noerr( err, ParameterMissing );

	err = GetEventParameter( inEvent, kEventParamControlDataBuffer, typePtr,
			NULL, sizeof( Ptr ), NULL, &ptr );
	require_noerr( err, ParameterMissing );

	err = GetEventParameter( inEvent, kEventParamControlDataBufferSize, typeLongInteger,
			NULL, sizeof( Size ), NULL, &size );
	require_noerr( err, ParameterMissing );


	if ( err == noErr )
		err = SetEventParameter( inEvent, kEventParamControlDataBufferSize, typeLongInteger,
				sizeof( Size ), &outSize );

ParameterMissing:
	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewSetData
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewSetData(
	EventRef				inEvent,
	PIPaneViewData*		inData )
{
	OSStatus				err;
	ControlPartCode			part;
	OSType					tag;
	Ptr						ptr;
	Size					size;
	
	err = GetEventParameter( inEvent, kEventParamControlPart, typeControlPartCode,
			NULL, sizeof( ControlPartCode ), NULL, &part );
	require_noerr( err, ParameterMissing );

	err = GetEventParameter( inEvent, kEventParamControlDataTag, typeEnumeration,
			NULL, sizeof( OSType ), NULL, &tag );
	require_noerr( err, ParameterMissing );

	err = GetEventParameter( inEvent, kEventParamControlDataBuffer, typePtr,
			NULL, sizeof( Ptr ), NULL, &ptr );
	require_noerr( err, ParameterMissing );

	err = GetEventParameter( inEvent, kEventParamControlDataBufferSize, typeLongInteger,
			NULL, sizeof( Size ), NULL, &size );
	require_noerr( err, ParameterMissing );


ParameterMissing:
	return err;
}

// -----------------------------------------------------------------------------
//	PIPaneViewGetRegion
// -----------------------------------------------------------------------------
//
OSStatus PIPaneViewGetRegion(
	EventRef				inEvent,
	PIPaneViewData*		inData )
{
	OSStatus				err;
	ControlPartCode			part;
	RgnHandle				outRegion;
	HIRect					bounds;
	Rect					qdBounds;
	
	err = GetEventParameter( inEvent, kEventParamControlPart, typeControlPartCode,
			NULL, sizeof( ControlPartCode ), NULL, &part );
	require_noerr( err, ParameterMissing );

	err = GetEventParameter( inEvent, kEventParamControlRegion, typeQDRgnHandle,
			NULL, sizeof( RgnHandle ), NULL, &outRegion );

	if ( part == kControlContentMetaPart
			|| part == kControlStructureMetaPart
			 || part == kControlOpaqueRegionMetaPart  )
	{
		HIViewGetBounds( inData->view, &bounds );
		qdBounds.top = (SInt16) CGRectGetMinY( bounds );
		qdBounds.left = (SInt16) CGRectGetMinX( bounds );
		qdBounds.bottom = (SInt16) CGRectGetMaxY( bounds );
		qdBounds.right = (SInt16) CGRectGetMaxX( bounds );
	
		RectRgn( outRegion, &qdBounds );
	}
	
ParameterMissing:
	return err;
}

// -----------------------------------------------------------------------------
//	FindPart
// -----------------------------------------------------------------------------
//
ControlPartCode FindPart(
	const HIRect*		inBounds,
	const HIPoint*		inWhere,
	PIPaneViewData*	inData )
{
	ControlPartCode		part;
// don't really have parts.
        // so just say 1
	part = 1;

	return part;
}

// -----------------------------------------------------------------------------
//	DefaultDrawPart
// -----------------------------------------------------------------------------
//
void DefaultDrawPart(
	ControlPartCode			inPart,
	const HIRect*			inPartRect
	 )
{
	switch ( inPart )
	{
	}
}

// -----------------------------------------------------------------------------
//	DefaultDrawPartLabel
// -----------------------------------------------------------------------------
//
void DefaultDrawPartLabel(
	ControlPartCode			inPart,
	const HIRect*			inPartRect
	 )
{
	
}

// -----------------------------------------------------------------------------
//	SetUpDateData
// -----------------------------------------------------------------------------
//
void SetUpDateData(
	PIPaneViewData*	inData )
{
}


void SizePIPanesViews(WindowRef theWindow,PIMainWDataH nowCD)
{
    Rect thisRect;
    HIRect   inRect;
    /*     CGPoint origin;
    CGSize size */
    if((*nowCD)->rawPane){
        thisRect = (*nowCD)->rawTargetRect;

        inRect.origin.x =thisRect.left;
        inRect.origin.y =thisRect.top ;
        inRect.size.width =thisRect.right - thisRect.left;
            inRect.size.height = thisRect.bottom- thisRect.top;
        HIViewSetFrame(
                       (*nowCD)->rawPane,
                       &inRect);
    }
    if((*nowCD)->transPane){
        thisRect = (*nowCD)->transTargetRect;
        inRect.origin.x =thisRect.left;
        inRect.origin.y =thisRect.top ;
        inRect.size.width =thisRect.right - thisRect.left;
        inRect.size.height = thisRect.bottom- thisRect.top;
        
        HIViewSetFrame(
                       (*nowCD)->transPane,
                       &inRect);

    }


}

void ZeroCell(piCell * one)
{
    one->hPos=0;
    one->vPos=0;
    one->totalPos=0;
    one->byteHere=0;
    one->realRect.top=0;
    one->realRect.left=0;
    one->realRect.bottom=0;
    one->realRect.right=0;
    one->next = 0;
}


Boolean IsEqualCell(piCell * one,piCell * two)
{Boolean retVal = false;
   if( (one->hPos == two->hPos)&& (one->vPos == two->vPos)&& (one->totalPos == two->totalPos))
       retVal = true;
    return(retVal);
}

OSStatus WhichCellAndByte(UInt16 theView, piCell * theCell,PIMainWDataH nowCD,Point *thePoint)
{UInt16 row,col;

    OSStatus myErr = noErr;
    Rect dingusRect;
// OK, I'm going to make this less streamlined
// well not really.  Just add some bassakwardness later
    
    switch(theView){
        case 9023:
            dingusRect = (*nowCD)->rawTargetRect;
            break;
        case 9024:
            dingusRect = (*nowCD)->transTargetRect;
            break;
    }
// normalize the point
    thePoint->h = thePoint->h - dingusRect.left;
    thePoint->v = thePoint->v - dingusRect.top;
// calculate what row (this is the same for both
    row = thePoint->v / (*nowCD)->cellHi;
    // col is by fish
    switch(theView){
        case 9023:
            col = thePoint->h / (*nowCD)->columnWidth;
            break;
        case 9024:
            col = thePoint->h / ((*nowCD)->columnWidth*2);
            break;
    }
    theCell->hPos = col;
    theCell->vPos = row;
    // make it a real react again
    
theCell->realRect.top =    (theCell->vPos *  (*nowCD)->cellHi)+dingusRect.top;
// again the thing
switch(theView){
    case 9023:
        theCell->realRect.left = (theCell->hPos * (*nowCD)->columnWidth)+dingusRect.left;
        break;
    case 9024:
        theCell->realRect.left = (theCell->hPos *( (*nowCD)->columnWidth)*2)+dingusRect.left;
        break;
}

theCell->realRect.bottom = theCell->realRect.top + (*nowCD)->cellHi;

switch(theView){
    case 9023:
        theCell->realRect.right = theCell->realRect.left + (*nowCD)->columnWidth;
        break;
    case 9024:
        theCell->realRect.right = theCell->realRect.left + ((*nowCD)->columnWidth*2);
        break;
}


// now figure out what the true byte in the file that this position is looking at
{UInt32 ocurrentVal =0;
    short nRow = row;
    do{
        if(nRow == 0){
            ocurrentVal +=col;
        } else {
            ocurrentVal +=  (*nowCD)->numColumns;
        }
    }while(nRow -- >0);
    theCell->totalPos =ocurrentVal+ (*nowCD)->displaying.offsetInBuffer+(*nowCD)->displaying.bufStartInFile;
}

// now wait.   If I have totalPos I can go to  UpdateCellPoses. Cool
UpdateCellPoses(theCell, nowCD);
// and it worked


return(myErr);
}

OSStatus UpdateSelRect( piCell * theCell,PIMainWDataH nowCD)
{OSStatus myErr= noErr;
    Rect dingusRect,dingusRect2;
            dingusRect = (*nowCD)->rawTargetRect;
            dingusRect2 = (*nowCD)->transTargetRect;
            theCell->rawRect.top =    (theCell->vPos *  (*nowCD)->cellHi)+dingusRect.top;

            theCell->realRect.top =    (theCell->vPos *  (*nowCD)->cellHi)+dingusRect2.top;

            theCell->rawRect.left = (theCell->hPos * (*nowCD)->columnWidth)+dingusRect.left;
            theCell->realRect.left = (theCell->hPos *( (*nowCD)->columnWidth)*2)+dingusRect2.left;

            theCell->rawRect.bottom = theCell->realRect.top + (*nowCD)->cellHi;
            theCell->realRect.bottom = theCell->realRect.top + (*nowCD)->cellHi;

            theCell->rawRect.right = theCell->rawRect.left + (*nowCD)->columnWidth;
            theCell->realRect.right = theCell->realRect.left + ((*nowCD)->columnWidth*2);
    



    return(myErr);    
}

// enters with truePos correct, updates hPos & vPos and the rect
OSStatus UpdateCellPoses(piCell *theCell, PIMainWDataH nowCD)
{
    UInt32 theVal =(GetControl32BitValue((*nowCD)->rawScroll) * (*nowCD)->numColumns);
    UInt32 leftOver = theCell->totalPos-theVal;
    UInt16 row,col;
    // DrawDevStringAndNum("\ptheVal ",theVal);
//    DrawDevStringAndNum("\pleft ",leftOver);
    row = leftOver/(*nowCD)->numColumns;
    col = leftOver -(row* (*nowCD)->numColumns);
    theCell->hPos=col;
    theCell->vPos=row;
// update the rect
    UpdateSelRect( theCell,nowCD);
    return(0);
}

void HilitePISelection(UInt32 theID,PIMainWDataH nowCD)
{
    short oHi,oW;
    Rect inn;
    WindowRef theWindow = (*nowCD)->us;
      WCH tempWC = (WCH)GetWRefCon(theWindow);
    // do not draw a rect unless we're actullay modifying
    // actually, that doesn't sit with me
//    if((*nowCD)->modifying == false)
//        return;
    
    switch(theID){
        case 9023:


if((*nowCD)->startSel->hPos != 64000 ){


    UpdateSelRect( (*nowCD)->startSel,nowCD);
    inn=(*nowCD)->startSel->rawRect;

    oHi = (*nowCD)->startSel->rawRect.bottom - (*nowCD)->startSel->rawRect.top;
    oW= (*nowCD)->startSel->rawRect.right - (*nowCD)->startSel->rawRect.left;
    inn.top = inn.top - (*nowCD)->rawTargetRect.top;
    inn.left = inn.left- (*nowCD)->rawTargetRect.left;
    inn.bottom = inn.top + oHi;
    inn.right = inn.left +oW;
    InvertRect(&inn);}
            break;
        case 9024:

            if((*nowCD)->startSel->hPos != 64000 ){


                UpdateSelRect( (*nowCD)->startSel,nowCD);

                inn=(*nowCD)->startSel->realRect;

    
// set inn to neutral
                oHi = (*nowCD)->startSel->realRect.bottom - (*nowCD)->startSel->realRect.top;
                oW= (*nowCD)->startSel->realRect.right - (*nowCD)->startSel->realRect.left;
                inn.top = inn.top - (*nowCD)->transTargetRect.top;
                inn.left = inn.left- (*nowCD)->transTargetRect.left;
                inn.bottom = inn.top + oHi;
                inn.right = inn.left +oW;
                InvertRect(&inn);
            }
            break;
}
// in either event, notify people.  Notify the window.  Notify your mom
if( (*tempWC)->generalMe)
    ((*tempWC)->generalMe)(theWindow,kwmgSelectionChanged,0);


}

#if 0
err = CreateEvent( NULL, kEventClassScrollable, kEventScrollableInfoChanged,
                   GetCurrentEventTime(), 0, &event );

#endif