/*
 *  FormatterPluginMgmt.pi.c
 *  PeekIt
 *
 *  Created by C.K. Haun on 7/19/06.
 *  Copyright 2006 Ravenware Software. All rights reserved.
 *
 */

#include "peekit.h"
#define kCurrentPluginVersion 0x01000000
 void  piFiler( CInfoPBRec *cpbPtr, Boolean *quitFlag, void *yourDataPtr)
{OSStatus myErr=0;
     // look for our plugins
     FSSpec theSpec;
     short fullPathLength;
     Handle fullPath;
     CFURLRef bundleURLRef;
     CFBundleRef myBundle = 0;
     CFStringRef string;
     Ptr fullP =0;
     myErr =  FSMakeFSSpec(cpbPtr->dirInfo.ioVRefNum, cpbPtr->dirInfo.ioDrParID,0/* cpbPtr->dirInfo.ioNamePtr*/, &theSpec);

fullP =           FSpGetFullPath(
                          &theSpec,
                          &fullPathLength,
                          &fullPath);
if(fullP != 0){
string=                         CFStringCreateWithBytes (
                                                                  0,
                                                                  fullP,
                                                                  fullPathLength/*-1*/,
                                                                  0,
                                                                  false
                                                                  );



 bundleURLRef= CFURLCreateWithFileSystemPath(0, string, kCFURLHFSPathStyle,true);
 
 CFArrayRef ook= CFBundleCreateBundlesFromDirectory (
                                              0  ,
                                               bundleURLRef,0
                                                                                                );
 
 
 
//    bundleURLRef=CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *) fullP, fullPathLength, false);
myBundle = CFBundleCreate( NULL, bundleURLRef );
CFRelease(string);


}
}





// 


void LoadFormatterPlugIns(void)
{FSRef putItHere;
     OSStatus    myErr=0;
FSSpec    theSpec;
FSRef dirRef;
PeekitDataFormatter * newOne;
PIDataFormatterPtr startHere=0;

SInt32 ooop;
Ptr theP = 0;
FormPlugPtr theThing ;
WindowRef plugLoadingWindow=0;
CFURLRef bundleURLRef;
CFArrayRef theBundles;
CFIndex idx=0;
CFBundleRef theCurrentBundle;
    GetRavenwarePlugInsFolder(&putItHere);

     bundleURLRef=  CFURLCreateFromFSRef(0, &putItHere);

     theBundles= CFBundleCreateBundlesFromDirectory (
                                                         0  ,
                                                         bundleURLRef,CFSTR("PIFormPlug")
                                                         );
    
ooop=     CFArrayGetCount(theBundles);
extern WindowRef CreateOrShowPILoadWWIND(FSSpecPtr thexFile, Boolean visi);
// plugLoadingWindow =  CreateOrShowPILoadWWIND(0,true);

if(ooop > 4){
// put up a window
}
while(ooop--){
    theCurrentBundle = (CFBundleRef)    CFArrayGetValueAtIndex(theBundles,  idx);
    theThing = calloc(1,sizeof(FormatterPluginControl));
    idx++;
//    NumVersion
    *((UInt32 *)(&theThing->version)) = CFBundleGetVersionNumber( theCurrentBundle );
    theThing->authorName = (CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIAuthorNameKey);
    theThing->authorEmail = (CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIAuthorEmailKey);
    theThing->authorURL = (CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIPeekItAuthorWebsiteKey);
    theThing->formatterName = (CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIPeekItFormatterName);
    theThing->formatterDesc = (CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIFormatterDesc);
    theThing->authorCompany=(CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIAuthorOrganizationKey);
 CFTypeRef number=  CFBundleGetValueForInfoDictionaryKey(theCurrentBundle,CFSTR("PeekItFormatterBytesConsumed"));
 if(number){
 CFNumberGetValue (number, kCFNumberShortType, & theThing->bytesConsumed);
 } else {
 theThing->bytesConsumed = -1;
 }
  number=  CFBundleGetValueForInfoDictionaryKey(theCurrentBundle,CFSTR("PeekItFormatterLinesNeeded"));
 if(number){
     CFNumberGetValue (number, kCFNumberShortType, & theThing->linesNeeded);
 } else {
     theThing->linesNeeded = 1;
 }
 
 
    if(theThing->version.majorRev == 1){

          theThing->formatFuncName=(CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIFormatFuncNameKey);
          theThing->drawFuncName=(CFStringRef) CFBundleGetValueForInfoDictionaryKey(theCurrentBundle, kPIFormandDrawFuncNameKey);
          theThing->formatFunc= (void*)CFBundleGetFunctionPointerForName( theCurrentBundle, theThing->formatFuncName);
          theThing->drawFunc= (void*)CFBundleGetFunctionPointerForName( theCurrentBundle, theThing->drawFuncName);

    }

    // see if this is duplicated
 PIDataFormatterPtr testDupFormatter =0;
 testDupFormatter= GetPIPlugInDataFormatterByName(theThing->formatterName);
 if(testDupFormatter){
        // whine
        
    }
    
    
    // add it to the end of the global ones
    newOne = calloc(1,sizeof( PeekitDataFormatter));
    newOne->version = 1;
    newOne->uniqueID = 1;
    newOne->title=    theThing->formatterName ;
        newOne->description= theThing->formatterDesc ;
        newOne->dataType = kFormatterValPlugIn;
            newOne->embeddedFormatterUniqueID=2;
            newOne->embeddedFormatter = (PeekitStructFormatter *)theThing;
            newOne->length=theThing->bytesConsumed;
             startHere =     gPlugInDataFormatters;

            if(startHere){
        while(startHere->next)startHere = startHere->next;
        startHere->next = newOne;
        newOne->previous = startHere;
    } else {
        gPlugInDataFormatters = newOne;
    }
    
}
if(plugLoadingWindow){
// HideWindow(plugLoadingWindow);
}

}

/*
 *  FormaterInfoDrawer.pi.c
 *  PeekIt
 *
 *  Created by C.K. Haun on 8/2/06.
 *  Copyright 2006 Ravenware Software. All rights reserved.
 *
 */

/*
 *  FormatterInfo.pi.c
 *  PeekIt
 *
 *  Created by C.K. Haun on 1/8/05.
 *  Copyright 2005 Ravenware Software. All rights reserved.
 *
 */
#define kColorDataVisPixSize 4
extern PeekitStructFormatterPtr gFormatters;



#define kFormatterInfoPopUp 5561
#define kFormatterInfoPane 9000
void HitFormatterInfoIND(WindowRef theWindow, Point *thePoint);

struct FormatterInfoindData {
    WindowRef mommy;
    UInt32 largest;
    HIRect FormatterInfoViewRect;
    Rect FormatterInfoViewRectQD;
    FormatWWDataP    parentNowCD;
    ControlRef  FormatInfoView;
    PeekitStructFormatterPtr theFormatter; // current formatter
    ControlRef formatterPopUp;
    UInt16 viewShowing;
    UInt16 FormatterInfoFViewLine;
    Boolean FormatterInfoFViewLineChanged;
    short buddy;
};

typedef struct FormatterInfoindData FormatterInfoindData;
typedef FormatterInfoindData *FormatterInfoDataP;
/* protos */

void innerFormatInfoSizer(WindowRef theWindow,FormatterInfoDataP nowCD);
#include "FormaterInfoDrawerView.pi.c"

// #include "FormatterInfoview.pi.c"

OSStatus FormatterInfoDrawingCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

}
OSStatus FormatterInfoClickCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
// extract the mouse
    Point theMouse;
    mPushPort((WindowRef)inUserData)

        GetEventParameter (inEvent, kEventParamMouseLocation, typeQDPoint,
                           NULL, sizeof(Point), NULL, &theMouse);
    
// now hand off
// TEST TEMP get the mouse
// GetMouse(&theMouse);  // I should not have to get the mouse again
    HitFormatterInfoIND((WindowRef)inUserData,&theMouse);
    mPullPort

        CallNextEventHandler(
                             inHandlerCallRef,
                             inEvent);

    return(myStatus);

}
OSStatus FormatterInfoStateChangeCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
    UInt32 theKind;
    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    
    
    theKind = GetEventKind(inEvent);

//    ZapPort((WindowRef)inUserData);
    switch(theKind){

        case        kEventWindowActivated:
        {                WCH parentTempWC = (WCH)GetWRefCon(nowCD->mommy);
            FormatWWDataP parentNowCD= (FormatWWDataP)(*parentTempWC)->dataStore;

            if(           nowCD->theFormatter &&  nowCD->theFormatter->flags & kPIStructChanged){
                HIViewSetNeedsDisplay(      
                                            nowCD->FormatInfoView,
                                            true);
                
            }
        }
            break;
       case kEventWindowDeactivated:

           break;
           }
    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

}
#pragma mark Sizing

OSStatus FormatterInfoSizeCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
    WindowRef theWindow = (WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;

    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

}
OSStatus FormatterInfoCloseCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
// just hide the window


    HideWindow((WindowRef)inUserData);

    return(myStatus);

}

OSStatus FormatterInfoSpecialCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;

    HICommand commandStruct;
    GetEventParameter (inEvent, kEventParamDirectObject,
                       kEventClassWindow, NULL, sizeof(HICommand),
                       NULL, &commandStruct);

    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

}





OSStatus InstallFormatterInfoHandlers(
                                     WindowRef theWindow,
                                     void * refCon,
                                     ULong flags)

{
    OSStatus myErr = noErr;
    long realRefCon = 0;
    EventHandlerRef windowOutRef;
    EventTypeSpec  windowDrawEventList[1]={{kEventClassWindow,kEventWindowDrawContent}};

    EventTypeSpec  windowStateEventList[2]={{kEventClassWindow,kEventWindowActivated},{kEventClassWindow,kEventWindowDeactivated}};

    EventTypeSpec  windowSizeEventList[1]={{kEventClassWindow,kEventWindowBoundsChanged}};

    EventTypeSpec  windowCloseEventList[1]={{kEventClassWindow,kEventWindowClose}};
    
//    EventTypeSpec  windowSpecialEventList[1]={{kEventClassWindow,kEventWindowContextualMenuSelect}};

//    EventTypeSpec  windowClickEventList[1]={   {kEventClassWindow,kEventWindowHandleContentClick}};

    if(refCon == NULL)realRefCon = (long)theWindow;
// we should collect all these event refs in the window data struct
    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(FormatterInfoDrawingCode),     sizeof(windowDrawEventList)/sizeof(EventTypeSpec),  (EventTypeSpec *)  &windowDrawEventList,     (void *)theWindow,   &windowOutRef);

//    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(FormatterInfoClickCode),     sizeof(windowClickEventList)/sizeof(EventTypeSpec),    (EventTypeSpec *)&windowClickEventList, (void *)theWindow,      &windowOutRef);

    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(FormatterInfoStateChangeCode),     sizeof(windowStateEventList)/sizeof(EventTypeSpec),    (EventTypeSpec *)&windowStateEventList, (void *)theWindow,      &windowOutRef);

    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(FormatterInfoSizeCode),     sizeof(windowSizeEventList)/sizeof(EventTypeSpec),   (EventTypeSpec *) &windowSizeEventList, (void *)theWindow,      &windowOutRef);

    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(FormatterInfoCloseCode),     sizeof(windowCloseEventList)/sizeof(EventTypeSpec),    (EventTypeSpec *)&windowCloseEventList, (void *)theWindow,      &windowOutRef);
    
// taking the special out for a miunutes
// myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(FormatterInfoSpecialCode),     sizeof(windowSpecialEventList)/sizeof(EventTypeSpec),
// &windowSizeEventList, (void *)theWindow,      &windowOutRef);
    myErr = InstallStandardEventHandler(GetWindowEventTarget (theWindow));



    return(myErr);

}





// ****************************
// pascal void Contdevloop(short depth, short deviceFlags, GDHandle targetDevice, long userData)
// going back through and removing all the offscreen stuff in this.


void DrawFormatterInfoIND(WindowRef theWindow, EventRecord *theEvent)
{
}



void HitFormatterInfoIND(WindowRef theWindow, Point *thePoint)
{

    WCH tempWC = (WCH)GetWRefCon(theWindow);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    Point theP;
    GetMouse(&theP);
}


void PrepEditFormatterInfoIND(WindowRef theWindow, Boolean *theEdits)
{
#if 0
#pragma unused (theEdits )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    /* file */
    // EnableItem(GetMHandle(kFileMenu),kSave);
    EnableItem(GetMenuHandle(kFileMenu), kSaveAs);
    EnableItem(GetMenuHandle(kFileMenu), kPrint);
    /* edit */

    if ((*(*tempWC)->undoAction)->undoWhat)
        EnableItem(GetMenuHandle(kEditMenu), kUndo);
//    if (objSelected(theWindow)) {
// cut/copy the screen text on demand
    EnableItem(GetMenuHandle(kEditMenu), kCut);
    EnableItem(GetMenuHandle(kEditMenu), kCopy);
//        EnableItem(GetMenuHandle(kEditMenu), kClear);
//    }
    EnableItem(GetMenuHandle(kEditMenu), kSelectAll);
    EnableItem(GetMenuHandle(kEditMenu), kClipBoard);
    EnableItem(GetMenuHandle(kCompanionMenu),1);

#endif
}

void SizeFormatterInfoIND(WindowRef theWindow, EventRecord *theEvent, short how)
{
#pragma unused (theEvent )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;

}

//  save the current screen snapshot
void SaveFormatterInfoIND(WindowRef theWindow, Boolean as)
{

}

void IdleFormatterInfoIND(WindowRef theWindow, Boolean front)
{

#pragma unused (front )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    LoopVar qq;

    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;

}

void KeyFormatterInfoIND(WindowRef theWindow, EventRecord *theEvent)
{

}
#if 0
Boolean EdittheWindow(WindowRef theWindow, EventRecord *theEvent, short editAction)
{
#pragma unused (theEvent )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    Boolean retVal = false;

    switch (editAction) {
        case kCut:
        case kCopy:
	// spit the screen text to the clipboard
 //	PutScrap(320,'TEXT',&nowCD->alltext[0]);
 // PutScrapFlavor(  0,  'TEXT',  0,  320, &nowCD->alltext[0]);


            return(retVal);
    }
    return(retVal);
}
#endif
void SendFormatterInfoINDBoard(void)
{

}


OSErr AEFormatterInfoIND(WindowRef theWindow, AppleEvent *Event)
{
#pragma unused (theWindow,Event )
    OSErr  myErr = noErr;

    return(myErr);
}
void  CommandFormatterInfoIND(WindowRef theWindow, EventRef inEvent,void *inUserData,HICommand *commandStruct)
{
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
// switch on the command
    switch((commandStruct->commandID)){
deafult:
#ifndef __FINAL__
        DebugStr("\p command in AboutW window defaulted");
#endif
        break;
    }

}
void GenFormatterInfoIND(WindowRef theWindow, UShort selector, void *theData)
{
#pragma unused (theWindow,theData )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    WindowPrefStructHdl thePrefs;
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    LoopVar qq;
    switch (selector) {
        case kMenuGeneral:
            break;
        case kwmgActContHit:
		// coming forward mouse hit, so use it as well
            SetPort(theWindow);
            HitFormatterInfoIND( theWindow,nil);
            break;
        case kwmgUpdatePrefs: 
	// no prefs
            break;
    }
}

void CloseFormatterInfoIND(WindowRef theWindow, Boolean kill)
{
#pragma unused (kill )
    CloseDrawer(theWindow,true);

}

#pragma mark infopane
static  OSStatus FormatterInfoPaneEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    CFStringRef standardNames[] = {
        CFSTR("Byte "),
        CFSTR("Word"),
        CFSTR("Long"),
        CFSTR("Byte string"),
        CFSTR("C String"),
        CFSTR("Pascal String"),
        CFSTR("Padding bytes")
    };
    CFStringRef standardDescs[]={
        CFSTR("A 8 bit Byte"),
        CFSTR("A 16 bit Word"),
        CFSTR("A 32 bit Long"),
        CFSTR("A specified number of 8 bit bytes"),
        CFSTR("Processes n number of bytes until a 0 byte is found. Maximum 1024 bytes."),
        CFSTR("A string with a leading count byte"),
        CFSTR("Skips ahead n number of bytes"),

        
    };
//    theThing->formatterDesc = standardDescs[itemChecked-1];

    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    FormatWWDataP parentNowCD;
    OSStatus myErr=0;
    Rect theRect;
    CGContextRef cgContext;
    ControlRef theCont;
    PIDataFormatterPtr theFormatter;
    FormPlugPtr theThing = calloc(1,sizeof(FormatterPluginControl));
    FormPlugPtr realOne=0;
    HIRect theHIRect;
    UInt16 theKind=0;
 // get th epopup
    UInt32 typeItem=0;

    UInt16 itemChecked = GetControlValue(SnatchCRef(theWindow,'FORI',kFormatterInfoPopUp));
    MenuRef theMenu = GetControlPopupMenuHandle(SnatchCRef(theWindow,'FORI',kFormatterInfoPopUp));

    PeekitStructFormatterPtr startHere = gFormatters;
    GetMenuItemProperty(theMenu,itemChecked, 'PEEK', 'DFTY',sizeof(UInt32),0,&typeItem) ;
    switch(typeItem){
        case 0:
            theThing->authorName=CFSTR("Dejah Tallin");
            theThing->authorCompany = CFSTR("Ravenware Industries, LLC");
            theThing->authorEmail =CFSTR("peekit@ravenware.com");
            theThing->authorURL=CFSTR("Ravenware.com");        
            theThing->formatterName = standardNames[itemChecked-1];
            theThing->formatterDesc = standardDescs[itemChecked-1];

            //            theThing->formatterName= realOne->formatterName;
//            theThing->formatterDesc=realOne->formatterDesc;
            
            break;
        case 1:
            // get the thing
            GetMenuItemProperty(theMenu,itemChecked, 'PEEK', 'ITSE',sizeof(PIDataFormatterPtr),0,&theFormatter) ;
            realOne = (FormPlugPtr)theFormatter->embeddedFormatter;
            theThing->authorName=realOne->authorName;
            theThing->authorEmail =realOne->authorEmail;
            theThing->authorURL=realOne->authorURL;        
            theThing->authorCompany = realOne->authorCompany;
            theThing->formatterName= realOne->formatterName;
            theThing->formatterDesc=realOne->formatterDesc;
            break;
    }
    


    GetEventParameter( inEvent, kEventParamCGContextRef, typeCGContextRef,
                           NULL, sizeof( CGContextRef ), NULL, &cgContext );
        
        
        GetEventParameter (inEvent, kEventParamDirectObject, typeControlRef,
                           NULL, sizeof(ControlRef), NULL, &theCont);
        theKind = GetEventKind(inEvent);
        HIViewGetFrame(
                       theCont,
                       &theHIRect);
        theHIRect.origin.x=theHIRect.origin.y=0;
        // works	 PaintCGRectWithRGBColorPtr(cgContext,theHIRect,&gBlack);
        

        GetControlBounds(theCont,&theRect);
        
        switch(theKind){
            case kEventControlHit:
            case kEventControlClick:
                
                break;
            case kEventControlDraw:
            {                float vPos;
//                CGNumInBoxRightJust(cgContext,024,&theHIRect);   
RGBColor bleu = gDrawerValColor;                
#define mInfopaneresetfont   CGContextSelectFont(cgContext, "Lucida Grande", gDefaultFontSize-2, kCGEncodingMacRoman);
#define mElseNotProvided  else {CGContextSetRGBFillColor (cgContext, 1,0,0, .7);     CGDrawCFStringAt(cgContext,CFSTR("Not provided"),theHIRect.origin.x,vPos);    CGContextSetRGBFillColor (cgContext, 0,0,0, 1); }
                
                mInfopaneresetfont;
                CGContextSetTextDrawingMode (cgContext,  kCGTextFill);          
                CGContextScaleCTM (cgContext, 1,-1);// -1.0);
                vPos = theHIRect.origin.y -( gDefaultFontSize+4);
                
                CGDrawStringAt(cgContext,"\pName",theHIRect.origin.x,vPos);
                vPos -=( gDefaultFontSize+4);
                //                CGDrawCFString
                if(theThing->formatterName){
                    CGContextSetRGBFillColor (cgContext, bleu.red, bleu.green, bleu.blue, 1); 
                    CGDrawCFStringAt(cgContext,theThing->formatterName,theHIRect.origin.x,vPos);
                    CGContextSetRGBFillColor (cgContext, 0,0,0, 1); 
                } mElseNotProvided

                vPos -=( gDefaultFontSize+4);
                CGDrawStringAt(cgContext,"\pAuthor",theHIRect.origin.x,vPos);
                vPos -=( gDefaultFontSize+4);

                if(theThing->authorName){
                    CGContextSetRGBFillColor (cgContext, bleu.red, bleu.green, bleu.blue, 1); 
                    CGDrawCFStringAt(cgContext,theThing->authorName,theHIRect.origin.x,vPos);
                    CGContextSetRGBFillColor (cgContext, 0,0,0, 1); 
                } mElseNotProvided
                

                vPos -=( gDefaultFontSize+4);
                CGDrawStringAt(cgContext,"\pOrganization",theHIRect.origin.x,vPos);
                vPos -=( gDefaultFontSize+4);
                
                if(theThing->authorCompany ){
                    CGContextSetRGBFillColor (cgContext, bleu.red, bleu.green, bleu.blue, 1); 
                    CGDrawCFStringAt(cgContext,theThing->authorCompany,theHIRect.origin.x,vPos);
                    CGContextSetRGBFillColor (cgContext, 0,0,0, 1); 
                } mElseNotProvided
                
                

                
                vPos -=( gDefaultFontSize+4);
                CGDrawStringAt(cgContext,"\pemail",theHIRect.origin.x,vPos);
                vPos -=( gDefaultFontSize+4);
                if(theThing->authorEmail){
                    CGContextSetRGBFillColor (cgContext, bleu.red, bleu.green, bleu.blue, 1); 
                    CGDrawCFStringAt(cgContext,theThing->authorEmail,theHIRect.origin.x,vPos);
                    CGContextSetRGBFillColor (cgContext, 0,0,0, 1);                 
                } mElseNotProvided
                

                vPos -=( gDefaultFontSize+4);
                CGDrawStringAt(cgContext,"\pweb site",theHIRect.origin.x,vPos);
                vPos -=( gDefaultFontSize+4);
                if(theThing->authorURL){
                    CGContextSetRGBFillColor (cgContext, bleu.red, bleu.green, bleu.blue, 1); 
                    CGDrawCFStringAt(cgContext,theThing->authorURL,theHIRect.origin.x,vPos);
                    CGContextSetRGBFillColor (cgContext, 0,0,0, 1);                 
                } mElseNotProvided
                
                
                vPos -=( gDefaultFontSize+4);
                
                CGDrawStringAt(cgContext,"\pDescription",theHIRect.origin.x,vPos);
//                vPos -=( gDefaultFontSize+4);
                if(theThing->formatterDesc){
                    HIThemeTextInfo textInfo = {0, kThemeStateActive, kThemeSystemFont, kHIThemeTextHorizontalFlushLeft, kHIThemeTextVerticalFlushTop, kHIThemeTextBoxOptionStronglyVertical, kHIThemeTextTruncationNone, 0, false};

                    HIRect newOne = theHIRect;
                    newOne.origin.y = vPos*-1;
                    newOne.size.height +=vPos;
                    CGContextSetRGBFillColor (cgContext, bleu.red, bleu.green, bleu.blue, 1); 
  //                 CGDrawCFStringAt(cgContext,theThing->formatterDesc,theHIRect.origin.x,vPos);
//                                    CGContextScaleCTM (cgContext, 0,0);// -1.0);
/*
                   HIThemeDrawTextBox(
                                       theThing->formatterDesc,
                                      &newOne,
                                       &textInfo,
                                       cgContext,
                                       kHIThemeOrientationInverted);
 */  

                   Rect bboundingBox;
                   bboundingBox.top = vPos*-1;
                   bboundingBox.left = 0;
                   bboundingBox.bottom = 500;
                   bboundingBox.right = newOne.size.width;
                   DrawThemeTextBox(  theThing->formatterDesc,  0, 0, true, &bboundingBox, 0, cgContext)  ;
                       
                   
                   //                              CGContextScaleCTM (cgContext, 1,-1);// -1.0);
//                    CGContextSetRGBFillColor (cgContext, 0,0,0, 1); 
                } mElseNotProvided                         
                
                
//                    CGNumInBoxRightJust(cgContext,024,&theHIRect);   
//                    	 PaintCGRectWithRGBColorPtr(cgContext,theHIRect,&gBlack);

}
                    
                         break;
}
                    
				


              


return(myErr);


    
    
}    

static  OSStatus FormatterInfoOpenFolderEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    FormatWWDataP parentNowCD;
    UInt32 formatterUniqueID=0;
    FSRef putItHere;
FSSpec    theSpec;
    // now find this one and add it to the thingie
    GetRavenwarePlugInsFolder(&putItHere);
    FSRefMakeFSSpec(&putItHere,&theSpec);
FinderLaunch(&theSpec,2);
    
    
    return(0);
}



static  OSStatus FormatterInfoPopupEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FormatterInfoDataP nowCD = (FormatterInfoDataP)(*tempWC)->dataStore;
    FormatWWDataP parentNowCD;
    UInt32 formatterUniqueID=0;
// now find this one and add it to the thingie


    HIViewSetNeedsDisplay(      
                            SnatchCRef(theWindow,'FORI',kFormatterInfoPane ),
                           true);
    
    
    return(0);
}








// this doesn't do much.  Just displays the drawer contents, no clicking or typing  No controls
#pragma mark creation
WindowRef AddPIFormatterInfoDrawer(WindowRef inWindow)
{
    
    LoopVar qq;
    EventTypeSpec	aboutlControlEvents[] =
    {
    {kEventClassControl,    kEventControlHit}
    };
    EventTypeSpec	posControlEvents[] =    {    {kEventClassControl,    kEventControlDraw}    };
    FormatWWDataP parentNowCD;
    WCH tempWC;
    FormatterInfoDataP nowCD;
    OSErr myErr = 0;
    AliasHandle theA;
    WindowRef theWindow = nil;
    
    EventTypeSpec	vscControlEvents[] ={    {kEventClassControl,    kEventControlHit}    };

    WCH parentTempWC = (WCH)GetWRefCon(inWindow);
    parentNowCD = (FormatWWDataP)(*parentTempWC)->dataStore;

    
    // see if it exists yet
    
    
    myErr = CreateWindowFromNib(gNibRef, CFSTR("FormInfo"), & theWindow);
    
    // NEW
    InstallFormatterInfoHandlers(
                             theWindow,
                             0,
                             0);
    
    tempWC = (WCH)NewHandleClear(sizeof(windowControl));
    
    nowCD = (FormatterInfoDataP)calloc(1,sizeof(FormatterInfoindData));
    mPushPort( theWindow)
        TextSize(9);
    
    mPullPort
        (*tempWC)->undoAction = (UndoHand)NewHandleClear(sizeof(UndoControl));
    (*tempWC)->dataStore = (Handle)nowCD;
    SetWRefCon( theWindow, (long)tempWC);
    AddWindowID( theWindow);
    
    /* set these goddam things */
    (*tempWC)->drawMe = DrawFormatterInfoIND;
    (*tempWC)->clickMe = HitFormatterInfoIND;
    (*tempWC)->saveMe = SaveFormatterInfoIND;
    (*tempWC)->closeMe = CloseFormatterInfoIND;
    (*tempWC)->keyMe = KeyFormatterInfoIND;
    //    (*tempWC)->prepEdit = PrepEditFormatterInfoIND;
    //    (*tempWC)->activateMe = ActivateFormatterInfoIND;
    //    (*tempWC)->deactMe = DeActivateFormatterInfoIND;
    (*tempWC)->sizeMe = SizeFormatterInfoIND;
    (*tempWC)->idleMe = IdleFormatterInfoIND;
    (*tempWC)->appleEventMe = AEFormatterInfoIND;
    (*tempWC)->generalMe = GenFormatterInfoIND;
    (*tempWC)->commandMe = CommandFormatterInfoIND;
    // new way Jose
    
    
    nowCD->mommy = inWindow;
    nowCD->parentNowCD = parentNowCD;
    
    {Rect wRect;
    GetWindowPortBounds(theWindow,&wRect );
    nowCD->FormatterInfoViewRectQD = wRect;
    nowCD->FormatterInfoViewRectQD.top +=30;
    nowCD-> FormatterInfoViewRect.origin.x = wRect.left;
    nowCD-> FormatterInfoViewRect.origin.y = wRect.top;
    nowCD-> FormatterInfoViewRect.size.width = wRect.right-wRect.left;
    nowCD-> FormatterInfoViewRect.size.height = (wRect.bottom-wRect.top);
    }
   
    // see if there is a divider line position
    
//    FormatInfoViewCreate(                              theWindow );
//      HIViewSetVisible((HIViewRef)nowCD->FormatterInfoView,true);
    //    HIViewSetVisible((HIViewRef)nowCD->transScroll,true);
    // add control IDs.  Why?  Dunno.  Then don't do it.  well, nevermind

//          InstallControlEventHandler(    SnatchCRef(theWindow,'FORI',2323),  FormatterInfoScrollEventHandlerProc , GetEventTypeCount(vscControlEvents),vscControlEvents, theWindow, NULL );
//      InstallControlEventHandler(    SnatchCRef(theWindow,'FORI',2324),  FormatterInfoScrollEventHandlerProc , GetEventTypeCount(vscControlEvents),vscControlEvents, theWindow, NULL );

InstallControlEventHandler(    SnatchCRef(theWindow,'FORI',kFormatterInfoPopUp),  FormatterInfoPopupEventHandlerProc , GetEventTypeCount(vscControlEvents),vscControlEvents, theWindow, NULL );
    InstallControlEventHandler(    SnatchCRef(theWindow,'FORI',kFormatterInfoPane ),  FormatterInfoPaneEventHandlerProc , GetEventTypeCount(posControlEvents),posControlEvents, theWindow, NULL );
    InstallControlEventHandler(    SnatchCRef(theWindow,'FORI',8018),  FormatterInfoOpenFolderEventHandlerProc , GetEventTypeCount(vscControlEvents),vscControlEvents, theWindow, NULL );

    
    
        


      SetDrawerParent (
                     theWindow,
                     inWindow
                     );
    SetDrawerPreferredEdge(
                           theWindow,
                           kWindowEdgeRight);
  
    // and the formatter popup
extern    UInt16 BuildADataFormatterMenu(MenuRef theMenu, UInt32 flags);
{MenuRef theMenu=  GetControlPopupMenuHandle( SnatchCRef(theWindow,'FORI',kFormatterInfoPopUp));
    
    // we have the standards, then the formatters
    BuildADataFormatterMenu( theMenu,0);
    SetControlMaximum(  theMenu,                  CountMenuItems(theMenu));
    SetControlValue(theMenu,1);

}



return( theWindow);
    
}
void    UpdateFormatterInfo(WindowRef theWindow)
{
    TellValWindowAboutChange(theWindow);

}
void xxUpdateFormatterInfoPopupMenu(void)
{
    /*
MenuRef theMenu = GetControlPopupMenuHandle (
                                            SnatchCRef(theWindow,'FORI',kFormatterInfoPopUp)
                                             );

 AddFormattersToMenu( theMenu);
     */
}

