/*
 *  Prefs.peekit.c
 *  PeekIt
 *
 *  Created by C.K. Haun on Tue Feb 25 2003.
 *  Copyright (c) 2003 Ravenware Software. All rights reserved.
 *
 */


#define __PrefPeekWINDOW__
// extern NotesPrefs gNotesPrefs;

#include "PeekIt.h"
extern PIFindPrefs gFindPrefs;

extern RGBColor gHiliteBoxColor;
extern RGBColor gSelectionRangeColor;
extern CGRGBColor gCGSelectionRangeColor;
void HitPrefWIND(WindowRef theWindow, Point *thePoint);
static  OSStatus PrefOKiEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );
enum {kPrefsID = 'PKPR',
kPrefTabID = 6887,
    kPrefOKButt = 1000,kPrefCancelButt, kPrefDefaultButt,
    kPrefsFontPU = 1050,
    kPrefsValDGColor,
    kLeftColPane= 2000,                 kRightColPane,
     kSampBack1,
     kSampBack2,
     kSampBack3,
    kModdedByte,
    kSampHiliteColor,
    kValDrawerColor,
    kHiliteBoxColor,
    kSelectionRangeColor,
kMetalPref = 9888,    
    kAddWarnPref = 9881,
    kAlShHex = 8000,
    kAlShNote=8001,
    
kHexPositionsPref = 9890,
        kTurnOffDragging = 9892,
    kHorizontalBars,
	kTurnOffNewColResize,
	
	kPrefFindCaseInsense= 1450,
	kPrefFindHexFind,
	kPrefFindMultiFile
    
};
struct PrefWindData {

    short buddy;
    RGBColor theColors[10];
    short fontSize;

};
typedef struct PrefWindData PrefWindData;
typedef PrefWindData *PrefWDataP, **PrefWDataH;
/* protos */


static void ReArrangePrefWControls(WindowRef theWindow, PrefWDataH nowCD);


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

    return(myStatus);

}
OSStatus PrefWindowClickCode(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
    HitPrefWIND((WindowRef)inUserData,&theMouse);
    mPullPort

        CallNextEventHandler(
                             inHandlerCallRef,
                             inEvent);

    return(myStatus);

}
OSStatus PrefWindowStateChangeCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
    ZapPort((WindowRef)inUserData);
    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

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


    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

}
OSStatus PrefWindowCloseCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
// pull the prefs


    HideWindow((WindowRef)inUserData);

    return(myStatus);

}

OSStatus PrefWindowSpecialCode(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);

}


static  OSStatus PrefColorEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    ControlRef theCont;
    Rect theRect;
    ControlID theID;
    UInt32 theKind;
    SInt16 outItemHit;
    LoopVar qq;
    RGBColor newColor ;
	Rect tabRect;
    OSStatus myErr = noErr;
    Point where =  {
        -1, -1
    };
    
    mPushPort((WindowRef)inUserData);
    GetEventParameter (inEvent, kEventParamDirectObject, typeControlRef,
                       NULL, sizeof(ControlRef), NULL, &theCont);

    GetControlID(
                 theCont,
                 &theID);
    theKind = GetEventKind(inEvent);
    GetControlBounds(theCont,&theRect);
// add the tab frame
// GetControlBounds(SnatchCRef((WindowRef)inUserData,kPrefsID,6887),&tabRect);
OffsetRect(&theRect,23,42);

    switch(theKind){
        case kEventControlHit:
case kEventControlClick    :
    switch(theID.id){
        case kLeftColPane:
            newColor  = gPrefs.theColors[theID.id - kLeftColPane];
            break;
        case kRightColPane:
            newColor  = gPrefs.theColors[theID.id - kLeftColPane];
            break;
        case kSampBack1:
            newColor  = gPrefs.theColors[kColTextColor];
            break;
        case kSampBack2:
            newColor  = gPrefs.theColors[kColTextColor];

            break;
        case kSampBack3:
            newColor  = gPrefs.theColors[kPosTextColor];

            break;

        case kModdedByte:
          newColor  =  gPrefs.theColors[kModdedTextColor];
            
            break;
        case kSampHiliteColor:
            newColor =  gNotesPrefs.boxColor;
            break;
        case kValDrawerColor:
            newColor = gDrawerValColor;
            break;
case            kHiliteBoxColor:
    newColor = gHiliteBoxColor;

    break;
            
    }
    
    gTempString[0]=0;
    if (GetColor(where, gTempString, &newColor, &newColor)) {
        switch(theID.id){
            case kLeftColPane:
                gPrefs.theColors[theID.id - kLeftColPane]=newColor ;
                break;
            case kRightColPane:
               gPrefs.theColors[theID.id - kLeftColPane]= newColor ;
                break;
            case kSampBack1:
                gPrefs.theColors[kColTextColor]=newColor ;
                break;
            case kSampBack2:
                 gPrefs.theColors[kColTextColor]=newColor  ;

                break;
            case kSampBack3:
                 gPrefs.theColors[kPosTextColor]=newColor  ;
                break;
            case                kModdedByte:
                gPrefs.theColors[kModdedTextColor]=newColor ;

                break;
            case kSampHiliteColor:
 gNotesPrefs.boxColor=                newColor  ;

                break;
            case kValDrawerColor:
                 gDrawerValColor=newColor;
                break;
            case kHiliteBoxColor:
                gHiliteBoxColor=newColor;

                break;
            case kSelectionRangeColor:
                gSelectionRangeColor = newColor;
                
                    mRGBtoCGRGB(gSelectionRangeColor,gCGSelectionRangeColor);
                break;
                
        }
        for(qq=0;qq<8;qq++){
            ControlID theID2;
            static SInt32            theiIDs[] = {kLeftColPane,                 kRightColPane,
                kSampBack1,
                kSampBack2,
                kSampBack3,kModdedByte,         kSampHiliteColor,kValDrawerColor,kHiliteBoxColor,kSelectionRangeColor
};
            ControlRef outControl;
            theID2.signature = kPrefsID;
            theID2.id = theiIDs[qq];
            GetControlByID(
                           (WindowRef)inUserData,
                          & theID2,
                           &outControl);
// draw1
HIViewSetNeedsDisplay(outControl,true);
// move the colors into the CG space here

mRGBtoCGRGB(gPrefs.theColors[qq],gTheColors[qq] );
//            GetControlBounds(outControl,&theRect);
//        ZapRect((WindowRef)inUserData,&theRect);
ZapPort((WindowRef)inUserData);
        }
    
    }
        
            break;
        case kEventControlDraw:
            switch(theID.id){
                case kLeftColPane:
                    RGBForeColor(&gPrefs.theColors[theID.id - kLeftColPane]);
                    PaintRect(&theRect);
                    RGBForeColor(&gBlack);
                    FrameRect(&theRect);
                    break;
                case kRightColPane:
                    RGBForeColor(&gPrefs.theColors[theID.id - kLeftColPane]);
                    PaintRect(&theRect);
                    RGBForeColor(&gBlack);
                    FrameRect(&theRect);

                    break;

                case kSampBack1:
                    RGBForeColor(&gPrefs.theColors[kLeftColColor]);

                    PaintRect(&theRect);
                    RGBForeColor(&gBlack);
                    FrameRect(&theRect);

                    RGBForeColor(&gPrefs.theColors[kColTextColor]);
                    TextFace(bold);
                    
                    MoveTo(theRect.left+2,theRect.bottom-4);
                    DrawChar('0');
                    TextFace(normal);
                    break;

                case kSampBack2:
                    RGBForeColor(&gPrefs.theColors[kRightColColor]);

                    PaintRect(&theRect);
                    RGBForeColor(&gBlack);
                    FrameRect(&theRect);

                    RGBForeColor(&gPrefs.theColors[kColTextColor]);
                    TextFace(bold);
                    MoveTo(theRect.left+2,theRect.bottom-4);
                    DrawChar('0');
                    TextFace(normal);
                    break;

                case kSampBack3:
                    PaintRect(&theRect);
                    RGBForeColor(&gBlack);
                    FrameRect(&theRect);

                    break;
                case                kModdedByte:
                    RGBForeColor(&gPrefs.theColors[kModdedTextColor]);

                    PaintRect(&theRect);
                    RGBForeColor(&gBlack);
                    FrameRect(&theRect);
                    
                    break;
                    
                    case                kSampHiliteColor:
                        RGBForeColor(& gNotesPrefs.boxColor);
                        
                        PaintRect(&theRect);
                        RGBForeColor(&gBlack);
                        FrameRect(&theRect);
                        break;
                    case kValDrawerColor :
                        RGBForeColor(&gDrawerValColor);
                        
                        PaintRect(&theRect);
                        RGBForeColor(&gBlack);
                        FrameRect(&theRect);
                        
                        break;
                        
case kHiliteBoxColor :
    RGBForeColor(&gHiliteBoxColor);
    
    PaintRect(&theRect);
    RGBForeColor(&gBlack);
    FrameRect(&theRect);
    
    break;
case kSelectionRangeColor:
    RGBForeColor(&gSelectionRangeColor);
    PaintRect(&theRect);
    RGBForeColor(&gBlack);
    FrameRect(&theRect);
    
    break;
    
    
            }
            

            RGBForeColor(&gBlack);
            // draw sample text
            break;
        
    }


    mPullPort;
    return(myErr);

}


static void PrefsTabEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    // reap the wind
    WindowRef theWindow = (WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PrefWDataH nowCD = (PrefWDataH)(*tempWC)->dataStore;
    static UInt16 lastPaneSelected = 1;	// static, to keep track of it long term (in a more complex application
                                        // you might store this in a data structure in the window refCon)                                            
    short controlValue = 0;
    //  Get the new value of the tab control now that the user has picked it    
    controlValue = GetControlValue( SnatchCRef(theWindow,'PKPR',6887) );
    // same as last ?
    if ( controlValue != lastPaneSelected )
    {
	#define kTabMasterID 8000
	#define kDummyValue 0
        // different from last time.
        // Hide the current pane and make the user selected pane the active one
        // our 3 tab pane IDs.  Put a dummy in so we can index without subtracting 1 (this array is zero based, 
        // control values are 1 based).
        int tabList[] = {kDummyValue, kTabMasterID,kTabMasterID+1,kTabMasterID+2};
                                                                                    
        // hide the current one, and set the new one
        SetControlVisibility( SnatchCRef(  theWindow, 'PRUP',  tabList[lastPaneSelected]), false, true );
        SetControlVisibility( SnatchCRef(  theWindow, 'PRUP',  tabList[controlValue]), true, true );    

        // make sure the new configuration is drawn correctly by redrawing the Tab control itself        
        Draw1Control( SnatchCRef(theWindow,'PKPR',6887) );		
        // and update our tracking
        lastPaneSelected= controlValue;    
    }
    
    return( eventNotHandledErr );

}

static  OSStatus PrefDefaultEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    // reap the wind
    WindowRef theWindow = (WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PrefWDataH nowCD = (PrefWDataH)(*tempWC)->dataStore;

    DefaultPrefs();
    ZapPort(theWindow);
}

static  OSStatus PrefCanxEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    WindowRef theWindow = (WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PrefWDataH nowCD = (PrefWDataH)(*tempWC)->dataStore;
    LoopVar qq;
      for(qq=0;qq<8;qq++)
          gPrefs.theColors[qq]= (*nowCD)->theColors[qq];
      gDefaultFontSize=(*nowCD)->fontSize ;
      HideWindow((WindowRef)inUserData);

}
static  OSStatus PrefOKiEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    // reap the wind
    short theVal = GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kPrefsFontPU)); // theCont);

 Boolean hexOpen = GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kAlShHex));
 Boolean notesOpen = GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kAlShNote));
 Boolean metalPref = GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kMetalPref));
Boolean filePosResizePref = GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kTurnOffNewColResize));
Boolean warnOff = GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kAddWarnPref));
Boolean hexAlways= GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kHexPositionsPref)); 
gAddPrefs.valDGraphicColor = GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kPrefsValDGColor))-1; // theCont);
gAddPrefs.dragExtendingOff =GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kTurnOffDragging));  
gAddPrefs.horizontalBars =GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kHorizontalBars));  

    gFindPrefs.caseInsense=   GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kPrefFindCaseInsense));  
    gFindPrefs.findHex=   GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kPrefFindHexFind));  
    gFindPrefs.multiFile=   GetControlValue(SnatchCRef((WindowRef)inUserData,kPrefsID,kPrefFindMultiFile));  






gDefaultFontSize = theVal + 8;
    if(    filePosResizePref){
        gAddPrefs.windowFlags |= kIgnoreRightSizedPosPane;
    
    } else {
     gAddPrefs.windowFlags &= ~kIgnoreRightSizedPosPane;
        
    }
    if(    metalPref){
        gAddPrefs.windowFlags |= kWindowMetalFlag;
    
    } else {
     gAddPrefs.windowFlags &= ~kWindowMetalFlag;
        
    }
    if(    hexAlways){
        gAddPrefs.windowFlags |= kWindowHexFlag;
        
    } else {
        gAddPrefs.windowFlags &= ~kWindowHexFlag;
        
    }
    gAddPrefs.extraWarningOff = warnOff;
    
    RunWindows(kwmgUpdateViews);
HideWindow((WindowRef)inUserData);
}


OSStatus InstallPrefWindowHandlers(
                                     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(PrefWindowDrawingCode),     sizeof(windowDrawEventList)/sizeof(EventTypeSpec),    &windowDrawEventList,     (void *)theWindow,   &windowOutRef);

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

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

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

    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(PrefWindowCloseCode),     sizeof(windowCloseEventList)/sizeof(EventTypeSpec),    &windowCloseEventList, (void *)theWindow,      &windowOutRef);
    
// taking the special out for a miunutes
// myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(PrefWindowSpecialCode),     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 DrawPrefWIND(WindowRef theWindow, EventRecord *theEvent)
{
}



void HitPrefWIND(WindowRef theWindow, Point *thePoint)
{

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


void PrepEditPrefWIND(WindowRef theWindow, Boolean *theEdits)
{
#if 0
#pragma unused (theEdits )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PrefWDataH nowCD = (PrefWDataH)(*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 SizePrefWIND(WindowRef theWindow, EventRecord *theEvent, short how)
{
#pragma unused (theEvent )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PrefWDataH nowCD = (PrefWDataH)(*tempWC)->dataStore;

}

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

}

void IdlePrefWIND(WindowRef theWindow, Boolean front)
{

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

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

}

void KeyPrefWIND(WindowRef theWindow, EventRecord *theEvent)
{

}

Boolean EditPrefWINDWindow(WindowRef theWindow, EventRecord *theEvent, short editAction)
{
#pragma unused (theEvent )
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    PrefWDataH nowCD = (PrefWDataH)(*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);
    }
}

void SendPrefWINDBoard(void)
{

}

void invalPrefWIND(void)
{
    WindowRef theW = FindWindowByKind(kPrefWINDWindow);
    mPushPort(theW)
        ZapPort(theW);
    mPullPort
}

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

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

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

void ClosePrefWIND(WindowRef theWindow, Boolean kill)
{
#pragma unused (kill )
    HideWindow(theWindow);

}
#define kTabMasterID 8000
// Set up the tab initial state.  In this case we just make pane 1 the visible pane
// in a real situation, you would want to cache the last place a user visited and 
// reset to that when the window is re-opened
static void SetInitialTabState(WindowRef theWindow)
{
    int tabList[] = {kTabMasterID,kTabMasterID+1,kTabMasterID+2}; 
    short qq=0;
    // If we just run without setting the initial state, then the tab control
    // will have both (or all) sets of controls overlapping each other.
    // So we'll fix that by making one pane active right off the bat.
#define     kMaxNumTabs 3
    // First pass, turn every pane invisible

    for(qq=0;qq<kMaxNumTabs;qq++)
    SetControlVisibility( SnatchCRef(  theWindow, 'PRUP',  tabList[qq]), false, true );  
    
    // Set the tab control itself to have a value of 1, the first pane of the tab set
    SetControlValue(SnatchCRef(theWindow,'PRUP',kTabMasterID),1 );

    // This is the important bit, of course.  We're setting the currently selected pane
    // to be visible, which makes the associated controls in the pane visible.
    SetControlVisibility( SnatchCRef(  theWindow, 'PRUP',  tabList[0]), true, true );

}


WindowRef CreateOrShowPrefWIND(FSSpecPtr theFile, Boolean visi)
{
#pragma unused (theFile )
    LoopVar qq;
    WindowPrefStructHdl thePrefs=nil;
    EventTypeSpec	aboutlControlEvents[] =
    {
    {kEventClassControl,    kEventControlHit}
    };
    EventTypeSpec	fwControlEvents[] =
    {
    {kEventClassControl,    kEventControlHit},
    {kEventClassControl,    kEventControlDraw},
      {kEventClassControl,    kEventControlClick}   


    };
    
    WCH tempWC;
    PrefWDataH nowCD;
    OSErr myErr = 0;
    AliasHandle theA;
    WindowRef PrefWINDWindow = nil;
    
    // see if it exists yet
    PrefWINDWindow= FindWindowByKind(kPrefWINDWindow);
    if(PrefWINDWindow){ShowWindow(PrefWINDWindow);SelectWindow(PrefWINDWindow);
        return(PrefWINDWindow);}


    myErr = CreateWindowFromNib(gNibRef, CFSTR("NewPrefs"), &PrefWINDWindow);
    
// NEW
    InstallPrefWindowHandlers(
                                PrefWINDWindow,
                                0,
                                0);

    tempWC = (WCH)NewHandleClear(sizeof(windowControl));

    SetWindowKind(PrefWINDWindow, kPrefWINDWindow);
    nowCD = (PrefWDataH)NewHandleClear(sizeof(PrefWindData));
    mPushPort(PrefWINDWindow)
        TextSize(9);

    mPullPort
        (*tempWC)->undoAction = (UndoHand)NewHandleClear(sizeof(UndoControl));
    (*tempWC)->dataStore = (Handle)nowCD;
    SetWRefCon(PrefWINDWindow, (long)tempWC);
    AddWindowID(PrefWINDWindow);

    /* set these goddam things */
    (*tempWC)->drawMe = DrawPrefWIND;
    (*tempWC)->clickMe = HitPrefWIND;
    (*tempWC)->saveMe = SavePrefWIND;
    (*tempWC)->closeMe = ClosePrefWIND;
    (*tempWC)->keyMe = KeyPrefWIND;
//    (*tempWC)->prepEdit = PrepEditPrefWIND;
    (*tempWC)->editMe = EditPrefWINDWindow; 
//    (*tempWC)->activateMe = ActivatePrefWIND;
//    (*tempWC)->deactMe = DeActivatePrefWIND;
    (*tempWC)->sizeMe = SizePrefWIND;
    (*tempWC)->idleMe = IdlePrefWIND;
    (*tempWC)->appleEventMe = AEPrefWIND;
    (*tempWC)->generalMe = GenPrefWIND;
    (*tempWC)->commandMe = CommandPrefWIND;
    // new way Jose
    
    
    InstallControlEventHandler(          SnatchCRef(PrefWINDWindow,kPrefsID,kPrefOKButt),  PrefOKiEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, PrefWINDWindow, NULL );
    InstallControlEventHandler(          SnatchCRef(PrefWINDWindow,kPrefsID,kPrefCancelButt),  PrefCanxEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, PrefWINDWindow, NULL );
    InstallControlEventHandler(          SnatchCRef(PrefWINDWindow,kPrefsID,kPrefDefaultButt),  PrefDefaultEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, PrefWINDWindow, NULL );
InstallControlEventHandler( SnatchCRef(PrefWINDWindow,kPrefsID,6887),  PrefsTabEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, PrefWINDWindow, NULL );

	
    SetControlValue(SnatchCRef(PrefWINDWindow,kPrefsID,kPrefsFontPU),gDefaultFontSize - 8); 
    SetControlValue(SnatchCRef(PrefWINDWindow,kPrefsID,kPrefsValDGColor),gAddPrefs.valDGraphicColor+1); 
    SetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kTurnOffDragging),    gAddPrefs.dragExtendingOff);  
    SetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kHorizontalBars),    gAddPrefs.horizontalBars);  


    SetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kPrefFindCaseInsense),    gFindPrefs.caseInsense);  
    SetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kPrefFindHexFind),    gFindPrefs.findHex);  
    SetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kPrefFindMultiFile),    gFindPrefs.multiFile);  

    
    
    RGBColor theColors[10];
    short fontSize;

    
    for(qq=0;qq<10;qq++){
    InstallControlEventHandler(          SnatchCRef(PrefWINDWindow,kPrefsID,kLeftColPane+qq),  PrefColorEventHandlerProc , GetEventTypeCount(fwControlEvents), fwControlEvents, PrefWINDWindow, NULL );
        if(qq<5){
        (*nowCD)->theColors[qq] = gPrefs.theColors[qq];
        }
    }
    (*nowCD)->fontSize = gDefaultFontSize;
    // check and set the checkbox thingme for metal
    if(gAddPrefs.windowFlags & kWindowMetalFlag){
        SetControlValue(SnatchCRef(PrefWINDWindow,kPrefsID,kMetalPref),true);
    }
    if(gAddPrefs.windowFlags & kWindowHexFlag){
    
    SetControlValue(SnatchCRef(PrefWINDWindow,kPrefsID,kHexPositionsPref),true); 
    }
    if(gAddPrefs.windowFlags & kIgnoreRightSizedPosPane){
    
    SetControlValue(SnatchCRef(PrefWINDWindow,kPrefsID,kTurnOffNewColResize),true); 
    }


    if(gAddPrefs.extraWarningOff)
        SetControlValue(SnatchCRef(PrefWINDWindow,kPrefsID,kAddWarnPref),true);

 SetInitialTabState(PrefWINDWindow);

// add our ports
    if (visi) {
        ShowWindow(PrefWINDWindow);
     //  FIX   AddToWindowMenu(PrefWINDWindow, nil, kWindowMenu);

    }


    return(PrefWINDWindow);
}







void SavePrefs(void)
{
// default stuff
    CFDataRef refsPrefs,notesPrefs,recentPrefs,addsPrefs,valdPref,moreColPref,findPref;
    CFStringRef prefsKey = CFSTR("BasicPrefs");
    CFStringRef prefsVerKey = CFSTR("BasicPrefsVer");
    CFStringRef fsizeKey = CFSTR("FSizePrefs");
    CFStringRef notesPKey = CFSTR("NotesPrefs");
    CFStringRef recentPKey = CFSTR("RecentPrefs");
    CFStringRef addPKey = CFSTR("AddPrefs");
    CFStringRef valDHiKey = CFSTR("ValDHi");
    CFStringRef moreColorsKey = CFSTR("MoreColor");
  CFStringRef findPrefsKey = CFSTR("FindPrefs");
    MorePrefColors moreColors;

    Size menuDataSize = 0;
    
    
    CFNumberRef    curPrefsVerRef,curFSize;
    UInt32    curPVer=kCurrentPrefsVersionNumber;
    Ptr theMenuData = CollectRecentItems(&menuDataSize);

    valdPref= CFDataCreate (
                             0,
                             (UInt8 *)&gDrawerValColor,
                             sizeof(gDrawerValColor)
                             );
    
    // fill in morecolors
moreColors.moreColors[0]=    gSelectionRangeColor ;
moreColors.moreColors[1]= gDiffRangeColor;
    moreColPref = CFDataCreate (
                                0,
                                (UInt8 *)&moreColors,
                                sizeof(MorePrefColors)
                                );

findPref = CFDataCreate (
                                0,
                                (UInt8 *)&gFindPrefs,
                                sizeof(gFindPrefs)
                                );
    
    refsPrefs= CFDataCreate (
                           0,
                            (UInt8 *)&gPrefs,
                            sizeof(gPrefs)
                            );
    addsPrefs= CFDataCreate (
                             0,
                             (UInt8 *)&gAddPrefs,
                             sizeof(gAddPrefs)
                             );
    
    notesPrefs= CFDataCreate (
                             0,
                              (UInt8 *)&gNotesPrefs,
                              sizeof(gNotesPrefs));
    
    
    recentPrefs = CFDataCreate (
                               0,
                               theMenuData,
                              menuDataSize
                               );
    curPrefsVerRef =  CFNumberCreate (
                              0,
                                kCFNumberSInt32Type,
                                &curPVer
                                );
    curFSize=  CFNumberCreate (
                               0,
                               kCFNumberSInt16Type,
                               &gDefaultFontSize
                               );
    
    CFPreferencesSetAppValue(prefsKey, refsPrefs,
                             kCFPreferencesCurrentApplication);
    
// Release the CFNumber.
    CFRelease(refsPrefs);
    CFPreferencesSetAppValue(addPKey, addsPrefs,
                             kCFPreferencesCurrentApplication);
    
    CFRelease(addsPrefs);
        
        CFPreferencesSetAppValue(recentPKey, recentPrefs,
                                 kCFPreferencesCurrentApplication);
        CFRelease(notesPrefs);

        
        
    CFPreferencesSetAppValue(prefsVerKey, curPrefsVerRef,
                             kCFPreferencesCurrentApplication);
    CFPreferencesSetAppValue(prefsVerKey, curPrefsVerRef,
                             kCFPreferencesCurrentApplication);
    CFRelease(curPrefsVerRef);
    CFPreferencesSetAppValue(valDHiKey, valdPref,
                             kCFPreferencesCurrentApplication);    
    CFRelease(valdPref);


    
    CFPreferencesSetAppValue(moreColorsKey, moreColPref,
                             kCFPreferencesCurrentApplication);    
    CFRelease(moreColPref);
    
 
	   
    CFPreferencesSetAppValue(findPrefsKey, findPref,
                             kCFPreferencesCurrentApplication);    
    CFRelease(findPref);

	   
		     
    CFPreferencesSetAppValue(fsizeKey, curFSize,
                             kCFPreferencesCurrentApplication);
    CFRelease(curFSize);
    
    notesPrefs= CFDataCreate (
                             0,
                             (UInt8 *)&gNotesPrefs,
                             sizeof(gNotesPrefs)
                             );
    CFPreferencesSetAppValue(notesPKey, notesPrefs,
                             kCFPreferencesCurrentApplication);
    CFRelease(notesPrefs);

    
// Write out the preferences.
    CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
    


}



void DefaultPrefs(void){
    LoopVar qq;
    mSetColor(  gPrefs.theColors[kRightColColor],0xe000,0xe000,0xffff);
    mSetColor(  gPrefs.theColors[kLeftColColor],0xffff,0xffff,0xffff);
    mSetColor(  gPrefs.theColors[kColTextColor],0x000,0x000,0);
    mSetColor(  gPrefs.theColors[kPosTextColor],0x000,0x000,0);
    
    mSetColor(  gPrefs.theColors[kModdedTextColor],0xFFFF,0x1000,0x1000);
    mSetColor(gHiliteBoxColor,0,0,0xffff);
    mSetColor(gSelectionRangeColor,0,0,0);
    for(qq=0;qq<6;qq++){
        mRGBtoCGRGB(gPrefs.theColors[qq],gTheColors[qq] );
    }

    mSetColor(gDrawerValColor,0,0,0xfe00);
    gPrefs.showHex = true;
mSetColor(    gNotesPrefs.boxColor,0,0xffff,0);
    // set the metal flag
gAddPrefs.windowFlags = 0;
  gAddPrefs.dragExtendingOff=0;
    gFindPrefs.caseInsense=  0; // GetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kPrefFindCaseInsense));  
    gFindPrefs.findHex=  0; // GetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kPrefFindHexFind));  
    gFindPrefs.multiFile=  0; // GetControlValue(SnatchCRef((WindowRef)PrefWINDWindow,kPrefsID,kPrefFindMultiFile));  


}


void LoadPrefs(void)
{
// default stuff
    LoopVar qq;
    CFDataRef refsPrefs,notesPrefs,recentPrefs,addsPrefs,valdPref,moreColPref,findPref;
    CFStringRef prefsKey = CFSTR("BasicPrefs");
    CFStringRef prefsVerKey = CFSTR("BasicPrefsVer");
    CFStringRef fsizeKey = CFSTR("FSizePrefs");
    CFStringRef notesPKey = CFSTR("NotesPrefs");
    CFStringRef recentPKey = CFSTR("RecentPrefs");
    CFStringRef addPKey = CFSTR("AddPrefs");
  CFStringRef valDHiKey = CFSTR("ValDHi");
  CFStringRef moreColorsKey = CFSTR("MoreColor");
  CFStringRef findPrefsKey = CFSTR("FindPrefs");

    
    CFNumberRef    curPrefsVerRef,curFSize;
    MorePrefColors moreColors;
    UInt32    curPVer;
 //    gPrefs.showHex = true;
    DefaultPrefs();
    refsPrefs = CFPreferencesCopyAppValue(prefsKey,
                                          kCFPreferencesCurrentApplication);
    addsPrefs= CFPreferencesCopyAppValue(addPKey,
                                         kCFPreferencesCurrentApplication);
    
    notesPrefs = CFPreferencesCopyAppValue(notesPKey,
                                          kCFPreferencesCurrentApplication);
    recentPrefs =  CFPreferencesCopyAppValue(recentPKey,
                                             kCFPreferencesCurrentApplication);

    curPrefsVerRef = CFPreferencesCopyAppValue(prefsVerKey,
                                          kCFPreferencesCurrentApplication);

    curFSize = CFPreferencesCopyAppValue(fsizeKey,
                                               kCFPreferencesCurrentApplication);
    valdPref =   CFPreferencesCopyAppValue( valDHiKey,  kCFPreferencesCurrentApplication);
    moreColPref =   CFPreferencesCopyAppValue( moreColorsKey,  kCFPreferencesCurrentApplication);
    findPref =   CFPreferencesCopyAppValue( findPrefsKey,  kCFPreferencesCurrentApplication);

    
    if(curFSize){
        CFNumberGetValue(curFSize, kCFNumberSInt16Type, &gDefaultFontSize);
        CFRelease(curFSize);

    } 

    if(curPrefsVerRef){
        CFNumberGetValue(curPrefsVerRef, kCFNumberSInt32Type, &curPVer);
        CFRelease(curPrefsVerRef);
        // see if current
        if(curPVer >= kCurrentPrefsVersionNumber){
        if(refsPrefs){
// its a stream o bytes.  Make it happen
            UInt8 * dataIn;
            dataIn = CFDataGetBytePtr (
                                           refsPrefs
                                           );
            BlockMoveData(dataIn,(Ptr)&gPrefs,sizeof(gPrefs));
            for(qq=0;qq<6;qq++){
                mRGBtoCGRGB(gPrefs.theColors[qq],gTheColors[qq] );
            }
            
            
        CFRelease(refsPrefs);
        
        if(addsPrefs){
            UInt8 * dataIn;
            dataIn = CFDataGetBytePtr (
                                       addsPrefs
                                       );
            BlockMoveData(dataIn,(Ptr)&gAddPrefs,sizeof(gAddPrefs)); 
                 CFRelease(addsPrefs);   
        }
        
        
            if(valdPref){
                dataIn = CFDataGetBytePtr (
                                           valdPref  );
                BlockMoveData(dataIn,(Ptr)&gDrawerValColor,sizeof(gDrawerValColor));                                
                CFRelease(valdPref);
            }
        if(moreColPref){
            dataIn = CFDataGetBytePtr (
                                       moreColPref  );
            BlockMoveData(dataIn,(Ptr)&moreColors,sizeof(MorePrefColors));                                
            // and put 'em back
            gSelectionRangeColor = moreColors.moreColors[0];
            gDiffRangeColor= moreColors.moreColors[1];
            mRGBtoCGRGB(gSelectionRangeColor,gCGSelectionRangeColor);
            mRGBtoCGRGB(gDiffRangeColor,gCGDiffRangeColor);

            CFRelease(moreColPref);
        }
        
        if(findPref){
            dataIn = CFDataGetBytePtr (
                                       findPref  );
            BlockMoveData(dataIn,(Ptr)&gFindPrefs,sizeof(gFindPrefs));                                
            // and put 'em back

            CFRelease(findPref);
        }
            
            
            if(notesPrefs){
        dataIn = CFDataGetBytePtr (
                                   notesPrefs
                                   );
        BlockMoveData(dataIn,(Ptr)&gNotesPrefs,sizeof(gNotesPrefs));
        
        
        CFRelease(notesPrefs);
        }
        
        
        
        
        if(recentPrefs){
        CFIndex theSize = CFDataGetLength(recentPrefs);
            Ptr thePtr = CFDataGetBytePtr (
                                           recentPrefs
                                           );
            CreateRecentMenu(thePtr,theSize);
            
            CFRelease(recentPrefs);

        } 
        
        }
        
        
        
        } else {
// not the same version muber, deal
        }

    }
}





void DoPrefs(void)
{

    CreateOrShowPrefWIND(0,true);

}
#undef __PrefPeekWINDOW__



