/*
 *  pimainview.Quartz.pi.c
 *  PeekIt
 *
 *  Created by C.K. Haun on 1/14/05.
 *  Copyright 2005 Ravenware Software. All rights reserved.
 *
 */
#define QDxToCGx(portBounds, qdX)  ( (qdX) )
#define QDyToCGy(portBounds, qdY)  ( ((portBounds).bottom - (portBounds).top ) - (qdY) )

#include "datadraw.Quartz.pi.c"

void HilitePISelectionQuartz(UInt32 theID,PIMainWDataP nowCD,CGContextRef cgRef)
{
    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;
 

                {CGRect doIt;
                    CGRGBColor theCol = {0,0,0,.4};
                    doIt.origin.x=  inn.left;
                    doIt.origin.y = inn.top*-1;
                    doIt.size.width = inn.right-inn.left;
                    doIt.size.height = (inn.bottom-inn.top)-3;
                    // ••• TEST TEMP 
                    // well, that worked.
                    doIt.origin.y -=doIt.size.height;
                    PaintCGRectWithCGRGBColorPtr(cgRef,doIt,&theCol);
                    
                }
            }
            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);
                
                {CGRect doIt;
                    CGRGBColor theCol = {0,0,0,.4};
                    doIt.origin.x= inn.left;
                    doIt.origin.y = inn.top*-1;
                    doIt.size.width = inn.right-inn.left;
                    doIt.size.height = inn.bottom-inn.top;
                    // ••• TEST TEMP 
                    // well, that worked.
                    doIt.origin.y -=doIt.size.height;
                    
                    // use alpha
                    
                    PaintCGRectWithCGRGBColorPtr(cgRef,doIt,&theCol);
                    
                }
                
                
            }
            break;
    }
    // in either event, notify people.  Notify the window.  Notify your mom
    if( (*tempWC)->generalMe)
        ((*tempWC)->generalMe)(theWindow,kwmgSelectionChanged,0);
    
    
}





OSStatus PIPaneViewDrawQuartz(
                        EventRef			inEvent,
                        PIPaneViewData*	inData )
{
    OSStatus			err;
    HIRect				bounds;
    ControlID theID;
    Rect tempRect;
    CGrafPtr thePort;
    CGContextRef cgContext;
    WCH tempWC = (WCH)GetWRefCon( inData->theWindow);
    PIMainWDataP  nowCD = (PIMainWDataP)(*tempWC)->dataStore;
    err = HIViewGetBounds( inData->view, &bounds );


    err = GetEventParameter( inEvent, kEventParamCGContextRef, typeCGContextRef,
                             NULL, sizeof( CGContextRef ), NULL, &cgContext );
     CGContextSaveGState (
                              cgContext
                              );
    

         
         CGContextSelectFont(cgContext, "Lucida Grande", nowCD->fontSize, kCGEncodingMacRoman);

         
         
         CGContextSetTextDrawingMode (cgContext,  kCGTextFill);          
         CGContextSetRGBFillColor (cgContext, 0, 0, 0, 1);          
         CGContextSetRGBStrokeColor (cgContext, 0, 0, 0,1 ); 
   
//    myTextTransform =  CGAffineTransformMakeRotation  (radians (45)); 
    
//     CGContextSetTextMatrix (myContext, myTextTransform); 
 //  CGContextShowTextAtPoint (cgContext, 80, -22, "Quartz 2D", 9); 


    GetControlID(
                 (ControlRef)inData->view,
                 &theID);
    switch(theID.id){
        Rect tempRect;
        HIRect theHIRect;
        case 9023:

            
         
         ColDrawRectQuartz(cgContext,kForRawData,nowCD);
                CGContextSetRGBFillColor (cgContext, 0, 0, 0, 1); 
            HIViewGetFrame(
                           nowCD->rawPane,
                           &theHIRect);
// frame it
            {OSStatus ik=0;
                theHIRect.origin.x=0;
                theHIRect.origin.y=0;
                CGContextBeginPath(cgContext);
                CGContextAddRect(cgContext, theHIRect);		// current path now represents cgRect
                CGContextDrawPath(cgContext, kCGPathStroke);
            }
                  CGContextScaleCTM (cgContext, 1,-1);// -1.0);
            HilitePISelectionQuartz(theID.id,nowCD,cgContext);

            CGContextSetLineWidth(cgContext, 1);
//            CGContextSetRGBFillColor (cgContext, 0, 0, 0, 1); 

            DrawDataQuartz( cgContext, inData->theWindow,kDrawRawData);
//                  CGContextScaleCTM (cgContext, 1,1);// -1.0);            
            break;
        case 9024:
            ColDrawRectQuartz(cgContext,kForTransData,nowCD);
            CGContextSetRGBFillColor (cgContext, 0, 0, 0, 1); 

            HIViewGetFrame(
                           nowCD->transPane,
                           &theHIRect);

            
            
    // frame it
            {OSStatus ik=0;
                theHIRect.origin.x=0;
                theHIRect.origin.y=0;
                CGContextBeginPath(cgContext);
                CGContextAddRect(cgContext, theHIRect);		// current path now represents cgRect
                CGContextDrawPath(cgContext, kCGPathStroke);
            }
                CGContextScaleCTM (cgContext, 1,-1);// -1.0);
            CGContextSetLineWidth(cgContext, 0);
            
            DrawDataQuartz( cgContext, inData->theWindow,kDrawTransData);
            

            HilitePISelectionQuartz(theID.id,nowCD,cgContext);
            break;
            


    }
    
     CGContextRestoreGState (
                                 cgContext
                                 );
    
    return err;
}    
/*
 *  datadraw.Quartz.pi.c
 *  PeekIt
 *
 *  Created by C.K. Haun on 1/14/05.
 *  Copyright 2005 Ravenware Software. All rights reserved.
 *
 */



/*
 *  datadraw.pi.c
 *  PeekIt
 *
 *  Created by C.K. Haun on Tue Feb 03 2004.
 *  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
 *
 */
// include by window code

// a little flourish
void SwashOutQuartz(short top,short right,short bottom,Boolean small)
{
    
    
}



void DrawDataQuartz(   CGContextRef cgContext, WindowRef theWindow,UInt16 which){
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PIMainWDataP nowCD = (PIMainWDataP)(*tempWC)->dataStore;
    Rect lilRect,lilRect2,winRect;
    Byte hexen;
    UInt16 howManyLilBytes = 0;
    Ptr realPtr = nowCD->displaying.fileBuffer;

    Str31 theS;
    Boolean                             noReallyBreak = false;
    ULong tempVar1, tempVar2;
    CGContextRef context;
    float incer = nowCD->columnWidth;
    float incer2 = nowCD->columnWidth*2;
    float lilFast1,lilFast2;
    Ptr moddedPtr = nowCD->modBuffer;
    Byte lilByte,modCheckByte;
    float hi = nowCD->cellHi;
    float numCols=nowCD->numColumns;
    LoopVar qq,ii;
    float numRows=    nowCD->numRows ;

    //    RGBColor reddish =  gPrefs.theColors[kModdedTextColor]; //  {0xFFFF,0x1000,0x1000};
    
    ULong lilCounter = nowCD->displaying.offsetInBuffer;
    static        char raw[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    HIRect theHIRect;
    lilFast2 = nowCD->fontSize+2;  // ??? 
    lilFast1 = 0;
    mCGSetColor(cgContext,gTheColors[kColTextColor]);

    switch(which){
        short newID;
        case kDrawRawData:
            tempVar1=nowCD->displaying.bufStartInFile;
            
            if(nowCD->forkShowing==0){
                tempVar2 =nowCD->theFT->dataForkLen; 
            } else {
                tempVar2 =nowCD->theFT->resForkLen; 
                
            }
                
                nowCD->nextNoteIsBM = bmNoteAtOrAfter(nowCD,tempVar1+lilCounter);
            if(nowCD->nextNoteIsBM){
                nowCD->nextNoteIsAtByte = nowCD->nextNoteIsBM->position;
            } else {
                nowCD->nextNoteIsAtByte=0xFFFFFFFF;}
                
                
                for(ii=0;ii<numRows;ii++){
        
        for(qq=0;qq<numCols;qq++){
            modCheckByte = *(moddedPtr + lilCounter);
            // OK buddy, see if the mirror is on.
            //  here will be added the rectangle surrounding noted byes
            extern NotesPrefs gNotesPrefs;
            if(nowCD->nextNoteIsAtByte != 0xFFFFFFFF && nowCD->nextNoteIsAtByte ==  tempVar1+lilCounter){
                BookMarkPtr theBM;
                Boolean swoopIt = false;
                // now, we don't want to show the noted byte if it is from the otehr fork, but I do want to keep track of it
                // to keep nextnoteatbyte working
                if(nowCD->forkShowing == 0 && ((nowCD->nextNoteIsBM->flags & kResForkBookMark) == 0))
                    swoopIt = true;
                if(nowCD->forkShowing == 1 && nowCD->nextNoteIsBM->flags & kResForkBookMark)
                    swoopIt = true;
                if(swoopIt){
                    CGRGBColor swoopColor;
                    mRGBtoCGRGB(gNotesPrefs.boxColor,swoopColor);
                    swoopColor.alpha = .4;
                    Rect theR;
                     HIRect theIt;
                    theR.left = lilFast1-2;
                    theR.top= lilFast2-nowCD->fontSize;
                    theR.right = theR.left +nowCD->charWide;
                    theR.bottom = lilFast2;
   
                    theIt.origin.x= theR.left;
                    theIt.origin.y = (theR.top)*-1;
                    theIt.size.width = theR.right-theR.left;
                    theIt.size.height = theR.bottom-theR.top;

                    theIt.origin.y -=theIt.size.height;

                    PaintCGRectWithCGRGBColorPtr(cgContext,theIt,&swoopColor);
                    
 
                    
                    // no swash in the small window SwashOut(theR.top,theR.right,theR.bottom,true);
                    mCGSetColor(cgContext,gTheColors[kColTextColor]);

                }                  
                
                // see if there is another one
                theBM =                       nowCD->nextNoteIsBM->next;
                if(theBM){
                    nowCD->nextNoteIsAtByte = theBM->position;
                } else {
                    nowCD->nextNoteIsAtByte=0xFFFFFFFF;
                }
                nowCD->nextNoteIsBM = theBM;
            }
            if(modCheckByte){

                                   mCGSetColor(cgContext,gTheColors[kModdedTextColor]);
            }

//            char it = * (realPtr+lilCounter);
            
            CGContextShowTextAtPoint (cgContext,lilFast1 , lilFast2 * -1 ,(realPtr+lilCounter), 1); 

            howManyLilBytes++;
            if(modCheckByte){
                mCGSetColor(cgContext,gTheColors[kColTextColor]);                
            }
            
            lilCounter++;
            if((lilCounter+tempVar1) >  tempVar2){
                noReallyBreak = true;
                break;
            }
            lilFast1+=incer;
            
        }
        lilFast1=2; // bigRect.left+2; // plus 2, dammit
        lilFast2+=hi;
        if(noReallyBreak == true)break;
        
    }
    // reset the notes
    {BookMarkPtr theBM;
        theBM = nowCD->theFileNotes;
        if(theBM){
            nowCD->nextNoteIsAtByte =  theBM-> position; // optimizationssdfasF
            nowCD->nextNoteIsBM = theBM;   }
    }
    nowCD->displaying.bytesShowing = howManyLilBytes;
    noReallyBreak = false;
    
    break;
            /***************/
            
        case kDrawTransData:
            tempVar1=nowCD->displaying.bufStartInFile;
            if(nowCD->forkShowing==0){
                tempVar2 =nowCD->theFT->dataForkLen; 
            } else {
                tempVar2 =nowCD->theFT->resForkLen; 
                
            }         
                
            if(nowCD->showingHex){
                lilCounter=nowCD->displaying.offsetInBuffer;
                
                // reset nextnoteisat based on lilpointer
                nowCD->nextNoteIsBM = bmNoteAtOrAfter(nowCD,tempVar1+lilCounter);
                if(nowCD->nextNoteIsBM){
                    nowCD->nextNoteIsAtByte = nowCD->nextNoteIsBM->position;
                } else {
                    nowCD->nextNoteIsAtByte=0xFFFFFFFF;}
                
                
                
                for(ii=0;ii<numRows;ii++){
                    
                    for(qq=0;qq<numCols;qq++){
                        // now the hex of it
                        modCheckByte = *(moddedPtr + lilCounter);
                        
                        lilByte = *(realPtr+lilCounter);
                        theS[1]   = raw[(lilByte>>4)&0xf];
                        theS[2] = raw[lilByte&0xf];
                        //  here will be added the rectangle surrounding noted byes
                        if(nowCD->nextNoteIsAtByte != 0xFFFFFFFF && nowCD->nextNoteIsAtByte ==  tempVar1+lilCounter){
                            BookMarkPtr theBM;
                            Boolean swoopIt=false;
                            // now, we don't want to show the noted byte if it is from the otehr fork, but I do want to keep track of it
                            // to keep nextnoteatbyte working
                            if(nowCD->forkShowing == 0 && ((nowCD->nextNoteIsBM->flags & kResForkBookMark) == 0))
                                swoopIt = true;
                            if(nowCD->forkShowing == 1 && nowCD->nextNoteIsBM->flags & kResForkBookMark)
                                swoopIt = true;
                            if(swoopIt){
/*                                
                                RGBForeColor(&gNotesPrefs.boxColor);
                                Rect theR;
                                theR.left = lilFast1-2;
                                theR.top= lilFast2-nowCD->fontSize;
                                theR.right = theR.left +nowCD->charWide;
                                theR.bottom = lilFast2;
                                PaintRect(&theR);
                                SwashOut(theR.top,theR.right,theR.bottom,false);
                                
                                RGBForeColor(& gPrefs.theColors[kColTextColor]);
   */
                                CGRGBColor swoopColor;
                                //                    RGBForeColor(&gNotesPrefs.boxColor);
                                mRGBtoCGRGB(gNotesPrefs.boxColor,swoopColor);
                                swoopColor.alpha = .3;
                                //                    mCGSetColor(cgContext,swoopColor);
                                
                                
                                
                                
                                Rect theR;
                                HIRect theIt;
                                theR.left = lilFast1-2;
                                theR.top= lilFast2-nowCD->fontSize;
                                theR.right = theR.left +nowCD->charWide;
                                theR.bottom = lilFast2;
                                
                                theIt.origin.x= theR.left;
                                theIt.origin.y = (theR.top)*-1;
                                theIt.size.width = (theR.right-theR.left)*2;
                                theIt.size.height = theR.bottom-theR.top;
                                
                                 theIt.origin.y -=theIt.size.height;
                                
                                
                                PaintCGRectWithCGRGBColorPtr(cgContext,theIt,&swoopColor);
//                                SwashOutQuartz(theR.top,theR.right,theR.bottom,false);                                
                                
                                // no swash in the small window SwashOut(theR.top,theR.right,theR.bottom,true);
                                //                    RGBForeColor(& gPrefs.theColors[kColTextColor]);
                                mCGSetColor(cgContext,gTheColors[kColTextColor]);
                                
                                
                            
                            
                            }
                            // see if there is another one
                            theBM =                       nowCD->nextNoteIsBM->next;
                            if(theBM){
                                nowCD->nextNoteIsAtByte = theBM->position;
                            } else {
                                nowCD->nextNoteIsAtByte=0xFFFFFFFF;
                            }
                            nowCD->nextNoteIsBM = theBM;
                        }
                        
                        
                        
                        
                        if(modCheckByte){
                            mCGSetColor(cgContext,gTheColors[kModdedTextColor]);
                         
                            
                        }
                        
                        
                        CGContextShowTextAtPoint (cgContext,lilFast1 ,  lilFast2*-1/* ook*/,&theS[1], 2); 
                        
                        
                        if(modCheckByte){
                            mCGSetColor(cgContext,gTheColors[kColTextColor]);
                         
                        }
                        
                        lilCounter++;
                        
                        if((lilCounter+ tempVar1 /* nowCD->displaying.bufStartInFile*/ )  >tempVar2 /* nowCD->theFT->dataForkLen*/ ){
                            noReallyBreak = true;
                            break;
                        }
                        
                        lilFast1 +=incer2;
                        
                    }
                    
                    
                     lilFast1=2;
                    lilFast2+=hi;
                    if(noReallyBreak == true)break;
                    
                }
            }
                // reset the notes
                {BookMarkPtr theBM;
                    theBM = nowCD->theFileNotes;
                    if(theBM){
                        nowCD->nextNoteIsAtByte =  theBM-> position; // optimizationssdfasF
                        nowCD->nextNoteIsBM = theBM;   }
                }
                
                
                
                break;
            
            
            
            /****************/
    
    }
}
