/*
 *  Find.pi.c
 *  PeekIt
 * *  Created by C.K. Haun on Wed Aug 27 2003.
 *  Copyright (c) 2003 Ravenware Software. All rights reserved.
 *
 */

#include "peekit.h"
extern PIFindPrefs gFindPrefs;
extern             void PIMWFindEnded(WindowRef theWindow);

// Drawer out
#define kFindWindSig 'FIND'
enum{kFindTextBox=4501,
    kFindHexASCIIRadio,
    kFindCaseSensitive,
    kFindWrapAround,
    kFindNow,
    kFindBackwards,
    kFindCancel,
    kFindNOTButt=4510,
    kFindFromTop,
	kMultifileFIndButt
};
#define __AboutPeekWINDOW__


void HitFindWIND(WindowRef theWindow, Point *thePoint);
void GoToRware(void);

struct FindWindData {
    WindowRef mommy;
    UInt16 editingAs;
    short buddy;
    FileTrackerPtr theFT;
    Ptr lastFind;

};
typedef struct FindWindData FindWindData;
typedef FindWindData *FindWDataP /* ,**FindWDataP */ ;

extern UInt64 PIReturnFileFIndFlags(WindowRef theWindow);

enum {k,kEditingAsAscii=1,kEditingAsHex};
/* protos */

OSStatus FIndKeyHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData);

static void ReArrangeFindWControls(WindowRef theWindow, FindWDataP nowCD);
static void SizeFindWINDControls(WindowRef theWindow);
static Boolean ValidateHexBox(WindowRef theWindow,FindWDataP nowCD);

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

    return(myStatus);

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

        CallNextEventHandler(
                             inHandlerCallRef,
                             inEvent);

    return(myStatus);

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

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

        case        kEventWindowActivated:
            SetKeyboardFocus(
                             theWindow,
                             SnatchCRef(theWindow,kFindWindSig,kFindTextBox),
                             kControlFocusNoPart);
            SetKeyboardFocus(
                             theWindow,
                             SnatchCRef(theWindow,kFindWindSig,kFindTextBox),
                             kControlFocusNextPart);
							 
							 if(CountNumberOfWindowsOfThisKind(kPIMainWINDWindow) >1 ){
	EnableControl(SnatchCRef(theWindow,kFindWindSig, kMultifileFIndButt));
}
							 
            break;
       case kEventWindowDeactivated:
//           ClearKeyboardFocus(theWindow);

           break;
           }
myStatus=    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);
    return(myStatus);

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

    SizeFindWINDControls((WindowRef) inUserData);

    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

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


    HideWindow((WindowRef)inUserData);

    return(myStatus);

}

static  OSStatus FindHexAsciiEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{OSStatus myErr = noErr;
    ControlRef theCont;
    UInt16 theVal;
    Boolean isHex = false;
    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FindWDataP nowCD = (FindWDataP)(*tempWC)->dataStore;
    
    GetEventParameter (inEvent, kEventParamDirectObject, typeControlRef,
                       NULL, sizeof(ControlRef), NULL, &theCont);
    theVal = GetControlValue(theCont);
    if(theVal !=  nowCD->editingAs){
    switch(theVal){
        case kEditingAsAscii:
            // turn on the case sense thingie
            EnableControl( SnatchCRef(theWindow,kFindWindSig,kFindCaseSensitive));
// ok, if they switch to ASCII with non-translatable charater
            break;
        case kEditingAsHex:
            // validate that this is really hex
            isHex = ValidateHexBox(theWindow,nowCD);
            if(isHex){
            DisableControl( SnatchCRef(theWindow,kFindWindSig,kFindCaseSensitive));
            } else {DialogRef outAlert;UInt16 item;
                CreateStandardAlert(
                                    kAlertStopAlert,
                                    CFSTR("Bad Hex values"),
                                    CFSTR("Your switching to Hex with non-Hex values in Find box.Please remove the non-Hex characters and try again."),       /* can be NULL */
                                         0,             /* can be NULL */
                                          &outAlert); 
                                          RunStandardAlert(outAlert,0,&item);
                                    SetControlValue(theCont,kEditingAsAscii);
                                    theVal = kEditingAsAscii;
                                    
            }

            break;
    } nowCD->editingAs = theVal;
}
       return(myErr);
}
static  OSStatus FindCancelEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{OSStatus myErr = noErr;
    // go away bitch
    WindowRef theWindow=(WindowRef)inUserData;
    HideSheetWindow(theWindow);

    return(myErr);   
}

void innerStartFind()
{


}
static  OSStatus FindFindEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{OSStatus myErr = noErr;
// findit, bitch
    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FindWDataP nowCD = (FindWDataP)(*tempWC)->dataStore;
    Size theS=0;
    UInt32 where;
    LoopVar qq;
    char theChar;
    SLibSearchParamBlockPtr theSearchBlock = calloc(1,sizeof(SLibSearchParamBlock));
     
    Ptr theSel;
    UInt16 editingAs,caseSense,wrap,bassAkwards,findNot,findFromTop,multiFileFind;
    ControlRef theControl = SnatchCRef(theWindow,kFindWindSig,kFindTextBox);
// coolet the rosebuds    
    GetControlDataSize(
                       theControl ,
                       5,
                       kControlEditTextTextTag,
                       &theS 
                       );
    if(theS){
        theSel = malloc(theS);
        GetControlData(
                       theControl ,
                       5,
                       kControlEditTextTextTag,
                       theS,
                       theSel,
                       0);
        
        // decode based on hex vs ascii
        editingAs= GetControlValue(SnatchCRef(theWindow,kFindWindSig,kFindHexASCIIRadio));
            caseSense= GetControlValue(SnatchCRef(theWindow,kFindWindSig,kFindCaseSensitive));
            wrap= GetControlValue(SnatchCRef(theWindow,kFindWindSig,kFindWrapAround));
    bassAkwards=GetControlValue(SnatchCRef(theWindow,kFindWindSig,kFindBackwards));
    findNot = GetControlValue(SnatchCRef(theWindow,kFindWindSig,kFindNOTButt));
    findFromTop = GetControlValue(SnatchCRef(theWindow,kFindWindSig,kFindFromTop));
multiFileFind =GetControlValue(SnatchCRef(theWindow,kFindWindSig, kMultifileFIndButt));
// if they are finding hex then that is de-facto a case-sensitive search. 7.14.06
if(editingAs == kEditingAsHex)
    caseSense = true;
if((multiFileFind == false && gMultiFileFindBlock != 0) || (multiFileFind == true)){
// if we're starting a new multifile, or if we're NOT doing multifile AND there was an old one hanging aroud...
CleanUpMultiFileFind();
} 
    // gathered, hide the sheet
    HideSheetWindow(theWindow);
    
    
            // upper case the string
    if(caseSense == false && editingAs != kEditingAsHex){
        for(qq=0;qq<theS;qq++){
            theChar = *(theSel+qq);
            if(theChar > 0x60 &&theChar < 0x7b)*(theSel+qq) = theChar - 0x20;            
        }}// uppercase loop
    // if this is hex collapse the string into bytes, and divide by 2
    if(editingAs == kEditingAsHex){
    // first, see if it's even.  Later.  Well, now, cuz somebody just reported this as a crashing bug.  Huh, whaddaya know. 6.25.05
        char holder = 0;
        static        char raw[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        static char ookies[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        Ptr reducedString = malloc(theS/2);
        LoopVar qq,ii;
        LoopVar redCounter = 0;
        LoopVar countString = theS;
        LoopVar imAnIdiot=0;
        if((theS%2)){
            DialogRef outAlert;UInt16 item;
            CreateStandardAlert(
                                kAlertStopAlert,
                                CFSTR("Bad Hex values"),
                                CFSTR("The hex string must have an even number of characters, \ncan't find partial bytes."),       /* can be NULL */
                                      0,             /* can be NULL */
                                      &outAlert); 
                                RunStandardAlert(outAlert,0,&item);
                                return; // •••• Exit point
                                
        }
       do{
            // do each at a time
            for(qq=0;qq<16;qq++){
                if(*(theSel+imAnIdiot) == raw[qq] || *(theSel+imAnIdiot) ==ookies[qq]){
                    holder = qq << 4;
                    break;
                }
            } 
           imAnIdiot++;
            for(qq=0;qq<16;qq++){
                if(*(theSel+imAnIdiot) == raw[qq] || *(theSel+imAnIdiot) ==ookies[qq]){
                    holder |= qq;
                    break;
                }
            }
           imAnIdiot++; 
            *(reducedString + redCounter) = holder;
            redCounter++;
            countString -=2;
       }while(countString);
    // now move it back where the thing expects it
       BlockMoveData(reducedString,theSel,theS/2);
       free(reducedString);
       // make theS short
       theS/=2;
    }
        // hand it off to the find
    theSearchBlock->searchFlags=0;
    if(caseSense)
        theSearchBlock->searchFlags=kFileSearchCaseSensitiveSearch;  // I've already made the adjustment here

// just ask    
    if(findNot)
        theSearchBlock->searchFlags |=        kFileSearchFindNOT;
    
theSearchBlock->searchFlags |= PIReturnFileFIndFlags(nowCD->mommy);

theSearchBlock->searchString=theSel;  // was a CF string, but that doesn't make sense for raw file searching
        theSearchBlock->searchLength=theS;
if(findFromTop){
        theSearchBlock->startoffset=0;
}    else {
        theSearchBlock->startoffset=PIReturnSelectionRealPosition(nowCD->mommy);
    theSearchBlock->startoffset++;
    if(theSearchBlock->startoffset >   PIReturnCurrentForkLen(nowCD->mommy)   )
       theSearchBlock->startoffset--;

}
        theSearchBlock->resultFlags=0;
        theSearchBlock->result=0;
        theSearchBlock-> theFile=nowCD->theFT;
        theSearchBlock-> progRoutine=0; 
        
    if(multiFileFind)BeginMultifileFind(theSearchBlock,nowCD->mommy);   // log the search block in, then come back here

PIExecuteSearch(nowCD->mommy, theSearchBlock);
#if 0
// moved into ExecuteFind
        myErr = SLibFindInFile(theSearchBlock);
        if(myErr == noErr){
            extern void MoveToThisByte(WindowRef theWindow, UInt32 theByte);
//            extern            void             pimwStoreLastFind(WindowRef theWindow,SLibSearchParamBlockPtr theSearchBlock);

            where = theSearchBlock->result->foundLocation;
            pimwMoveToThisByte(nowCD->mommy,where);
// so a successful find, store the Find block for re-finding
            // see if there is one already
            // actually, hand it back so we don't know too much
            pimwStoreLastFind(nowCD->mommy,theSearchBlock);
        } else {
            PIMWFindEnded(nowCD->mommy);
  // these two need to be cleaned up somehow ••• to do          free(theSearchBlock->searchString);
//            free(theSearchBlock);
            // does a failed search clear searahc again?  Don't think so
        }
    
#endif 


    } else {
    // zero size, still have to retract
        HideSheetWindow(theWindow);

    }
    
    return(myErr);
}
static  OSStatus FindFindBassAkwardsEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{OSStatus myErr = noErr;

    return(myErr);
}



void InstallFindKeyHandlers(WindowRef theWindow)

{OSErr myErr = 0;
    EventTypeSpec keyEventList[3]={
    {kEventClassKeyboard,kEventRawKeyDown},
    {kEventClassKeyboard,kEventRawKeyRepeat},
    {kEventClassKeyboard,kEventRawKeyUp}};
    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(FIndKeyHandler),     sizeof(keyEventList)/sizeof(EventTypeSpec),    &keyEventList,     (void *)theWindow,   nil);
    
}



OSStatus InstallFindWindowHandlers(
                                     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},{kEventClassWindow,kEventAppActiveWindowChanged}};

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

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

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

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

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

    InstallFindKeyHandlers( theWindow);
    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 DrawFindWIND(WindowRef theWindow, EventRecord *theEvent)
{
}



void HitFindWIND(WindowRef theWindow, Point *thePoint)
{

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


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

    mPushPort(theWindow)
        SizeFindWINDControls(theWindow);
    mPullPort
}

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

}

void IdleFindWIND(WindowRef theWindow, Boolean front)
{

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

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

}

void KeyFindWIND(WindowRef theWindow, EventRecord *theEvent)
{

}

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

void SendFindWINDBoard(void)
{

}

void invalFindWIND(void)
{
    WindowRef theW = FindWindowByKind(kFindWINDWindow);
    mPushPort(theW)
        ZapPort(theW);
    mPullPort
}

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

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

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

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

}

WindowRef AddFindWIND(WindowRef theWindow,FileTrackerPtr theFile)
{
#pragma unused (theFile )
    LoopVar qq;
    Boolean setIt=false;
    WindowPrefStructHdl thePrefs=nil;
    EventTypeSpec	aboutlControlEvents[] =
    {
    {kEventClassControl,    kEventControlHit}
    };
#if 0    
    EventTypeSpec	fwControlEvents[] =
    {
    {kEventClassControl,    kEventControlHit},
    {kEventClassControl,    kEventControlDraw},
    {kEventClassControl,    kEventControlClick}


    };
#endif
    
    WCH tempWC;
    FindWDataP nowCD;
    OSErr myErr = 0;
    AliasHandle theA;
    WindowRef FindWINDWindow = nil;
    
    // see if it exists yet


    myErr = CreateWindowFromNib(gNibRef, CFSTR("FindDraw"), &FindWINDWindow);
    
// NEW
    InstallFindWindowHandlers(
                                FindWINDWindow,
                                0,
                                0);

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

    SetWindowKind(FindWINDWindow, kSheetWindowClass);
    nowCD = (FindWDataP)malloc(sizeof(FindWindData));
    mPushPort(FindWINDWindow)
        TextSize(9);

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

    /* set these goddam things */
    (*tempWC)->drawMe = DrawFindWIND;
    (*tempWC)->clickMe = HitFindWIND;
    (*tempWC)->saveMe = SaveFindWIND;
    (*tempWC)->closeMe = CloseFindWIND;
    (*tempWC)->keyMe = KeyFindWIND;
//    (*tempWC)->prepEdit = PrepEditFindWIND;
    (*tempWC)->editMe = EditFindWINDWindow;
//    (*tempWC)->activateMe = ActivateFindWIND;
//    (*tempWC)->deactMe = DeActivateFindWIND;
    (*tempWC)->sizeMe = SizeFindWIND;
    (*tempWC)->idleMe = IdleFindWIND;
    (*tempWC)->appleEventMe = AEFindWIND;
    (*tempWC)->generalMe = GenFindWIND;
    (*tempWC)->commandMe = CommandFindWIND;
    // new way Jose
    
    // set up the initial rects, either defaulted or saved
    SizeFindWINDControls(FindWINDWindow);
   InstallControlEventHandler(          SnatchCRef(FindWINDWindow,kFindWindSig,kFindHexASCIIRadio),  FindHexAsciiEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, FindWINDWindow, NULL );
   InstallControlEventHandler(          SnatchCRef(FindWINDWindow,kFindWindSig,kFindNow),  FindFindEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, FindWINDWindow, NULL );
//   InstallControlEventHandler(          SnatchCRef(FindWINDWindow,kFindWindSig,kFindBackwards),  FindFindBassAkwardsEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, FindWINDWindow, NULL );
   InstallControlEventHandler(          SnatchCRef(FindWINDWindow,kFindWindSig,kFindCancel),  FindFindEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, FindWINDWindow, NULL );
// I'm going to default to case sensitive searching
//   SetControlValue( SnatchCRef(FindWINDWindow,kFindWindSig,kFindCaseSensitive),true);
   setIt = gFindPrefs.caseInsense ? false:true;
 SetControlValue( SnatchCRef(FindWINDWindow,kFindWindSig,kFindCaseSensitive),setIt);

 setIt =  gFindPrefs.multiFile ? true:false;
 SetControlValue( SnatchCRef(FindWINDWindow,kFindWindSig,kMultifileFIndButt),setIt);

 SetControlValue( SnatchCRef(FindWINDWindow,kFindWindSig,kFindHexASCIIRadio),gFindPrefs.findHex+1);
 nowCD->editingAs = kEditingAsAscii;

 if(gFindPrefs.findHex+1==kEditingAsHex ) {nowCD->editingAs =kEditingAsHex ;}

if(gFindPrefs.findHex){
// have to set the other stuff
            DisableControl( SnatchCRef(FindWINDWindow,kFindWindSig,kFindCaseSensitive));

}
if(CountNumberOfWindowsOfThisKind(kPIMainWINDWindow) <= 1){
DisableControl(SnatchCRef(FindWINDWindow,kFindWindSig, kMultifileFIndButt));
}
// ABOU 9000
//    InstallControlEventHandler(          SnatchCRef(FindWINDWindow,'ABOU',9000),  FredWareEventHandlerProc , GetEventTypeCount(fwControlEvents), fwControlEvents, FindWINDWindow, NULL );




//    SetDrawerParent(FindWINDWindow, theWindow);
//    SetDrawerPreferredEdge(FindWINDWindow,kWindowEdgeBottom);
// SetDrawerOffsets (FindWINDWindow, 10.0, 10.0);

 nowCD->mommy = theWindow;

 nowCD->  theFT=theFile;

    return(FindWINDWindow);
}


void CleanFindWindowMemory(WindowRef theWindow)
{
    return; // don't know why this is crashing, can't reproduce.  Just leak.
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    FindWDataP nowCD = (FindWDataP)(*tempWC)->dataStore;

DisposeHandle((Handle)(*tempWC)->undoAction);
free((Ptr)nowCD);
}

// size scroll bar and text area
static void SizeFindWINDControls(WindowRef theWindow)
{
#pragma unused (theWindow )
// see if we can change the menu position by mussing with the rect
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    FindWDataP nowCD = (FindWDataP)(*tempWC)->dataStore;
}






















void ReArrangeFindWControls(WindowRef theWindow, FindWDataP nowCD)
{
#if 0
    // make sure they're drawn
    Point thePoint;
    LoopVar qq;
    mPushPort(theWindow)
        PenPat(&gGrayPat);
    PenSize(2, 2);

    for (qq = 0; qq < kNumCWPicts; qq++) {


        FrameRect(&nowCD->compRects[qq]);


    }
    QDFlushPortBuffer(
                      GetWindowPort((WindowRef)theWindow),
                      0);

    PenSize(1, 1);
    PenPat(&gBlackPat);

    GetMouse(&thePoint);
    // see if we're in any. FIFo
    for (qq = 0; qq < kNumCWPicts; qq++) {
        if (PtInRect(thePoint, &nowCD->compRects[qq])) {
            
	        // drag this one
            Rect tempRect = nowCD->compRects[qq];
            Rect old = tempRect;
            DragABM(theWindow, &tempRect);
            
            // ask 'em about it
            nowCD->compRects[qq] = tempRect;
            // save this to the prefs
            gPrefs.userCompRects[qq] = nowCD->compRects[qq];
            // resize here
            ZapRect(theWindow, &old);
            ZapRect(theWindow, &nowCD->compRects[qq]);
			// make sure the border gets redrawn
            nowCD->drewOutline=false;
        }
    }
    mPullPort

        ResetCWRectOffsets(theWindow, nowCD);
#endif
}








void DoFindWindMenu(short which)
{

    WindowRef theW = FindWindowByKind(kFindWINDWindow);
    WCH tempWC = (WCH)GetWRefCon(theW);
    FindWDataP nowCD = (FindWDataP)(*tempWC)->dataStore;
#if 0
    switch(which){
        case kResetContPositions:
            ResetControlPostions();

            break;

        case 2:
            ExternRequestScreenUpdate(0x15);


            break;

        case 3:
        {
            if(nowCD->refreshOFF)
                nowCD->refreshOFF = false;
            else
                nowCD->refreshOFF = true;
// •••• FIX		CheckItem(GetMenuHandle(5),3, (nowCD->refreshOFF==true));

        }
            break;
        case 4:
// say parameter
            SayBuffer(&nowCD->paramName[1],nowCD->paramName[0]);
            SayBuffer(&nowCD->paramVal[1],nowCD->paramVal[0]);

            break;
        case 5:
// say whole screen
            SayBuffer((Ptr)&nowCD->alltext,320);

            break;
    }

#endif
}


OSStatus FIndKeyHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{OSErr myErr=noErr;
    Boolean tellEm = false;
//    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    FindWDataP nowCD = (FindWDataP)(*tempWC)->dataStore;
    UInt32 modKeys;
    UInt32	eventKind = GetEventKind( inEvent );
    char theChar = 0;
// get the key
// kEventParamKeyCode

    myErr = GetEventParameter( inEvent, kEventParamKeyMacCharCodes, typeChar,
                               NULL, sizeof( typeChar ), NULL,&theChar );

    myErr = GetEventParameter( inEvent, kEventParamKeyModifiers, typeUInt32,
                               NULL, sizeof( UInt32 ), NULL,&modKeys );
    if(theChar == 0xd || theChar == kEnterKey || theChar == kEscKey){
        tellEm = true;

    } else {
    
    
    // now what are they doing?
        switch(nowCD->editingAs){
            case kEditingAsHex:
                tellEm = true;
                switch(eventKind){
                    case kEventRawKeyDown:
                    case kEventRawKeyRepeat:
    // zamine the key
                    {
// is it an edit key? Or command key
                        if(!IsEditKey(theChar,modKeys)){
// nope
// see if we're at 2 chars already, or whatever.  I.e. can we accept this key?
//    FitInEditText(SnatchCRef((WindowRef)inUserData,'PEEK',kPeekHexEditBox),theChar,2)
                            if(
                               ((theChar >='0' && theChar <='9') || (theChar >='a' && theChar <='f') || (theChar >='A' && theChar <='F')))
                            {
                                if( (theChar >='a' && theChar <='f')){UInt32 theKeyCode=0;
                                    EventRef newEvent;

                                }



                            } else {
                                tellEm = false;
                            }
                        } else {
                            tellEm = true; // pass on an edit key
                        }
                    }
                        break;
                    case kEventRawKeyUp:
                        tellEm = false;

                        if( (theChar >='a' && theChar <='f')){
                            tellEm = false;
                        }
                            break;

                }
                    break;
            case kEditingAsAscii:
    // so really we're just gonna take the raw keycode and slam it home.  Easy as Pie
    // only care about keydonws, please
                    tellEm = true;

                break;

        } // eidit as switch
    }
    if(tellEm){
        CallNextEventHandler(
                             inHandlerCallRef,
                             inEvent);
    }


    return(myErr);

}

static Boolean ValidateHexBox(WindowRef theWindow,FindWDataP nowCD)
{
    Boolean isHex = true;
    Size theS=0;
    Ptr theSel = 0;
    LoopVar qq;
    ControlRef theControl = SnatchCRef(theWindow,kFindWindSig,kFindTextBox);

    GetControlDataSize(
                       theControl ,
                       5,
                       kControlEditTextTextTag,
                       &theS
                       );
    if(theS){
        char theChar;
        theSel = malloc(theS);
        GetControlData(
                       theControl ,
                       5,
                       kControlEditTextTextTag,
                       theS,
                       theSel,
                       0);
        for(qq=0;qq<theS;qq++){
            theChar =  *(theSel + qq);
            if( (theChar >='0' && theChar <='9') || (theChar >='a' && theChar <='f') || (theChar >='A' && theChar <='F')){
} else {
    isHex = false;
    break;
}}}

        
    return(isHex);
    
}

/*
 *  findmultifile.pi.c
 *  PeekIt
 *
 *  Created by ckhaun on 6/18/06.
 *  Copyright 2006 Ravenware Software. All rights reserved.
 *
 */
#include "Peekit.h"
WindowRef CreateOrShowMFindProgWWIND(FSSpecPtr thexFile, Boolean visi);

// starts if the checkbox is checked
void BeginMultifileFind( SLibSearchParamBlockPtr theSearchBlock, WindowRef thisWindow )
{MultiFileFindParmPtr newBlock = calloc(1,sizeof(MultiFileFindParm));
// UInt16 numWindows = CountNumberOfWindowsOfThisKind();
UInt16 winCount=0;
// WindowRef * theWindows 
newBlock->nowWindow = thisWindow;
newBlock->windowOrder = ReturnAllWindowsOfThisKind(kPIMainWINDWindow,true,&newBlock->winCount);
if(gMultiFileFindBlock){
// soemthing wierdie is going on, clear this
} 
gMultiFileFindBlock  = newBlock;
newBlock->slibBlock = slibDuplicateSearchBlock(theSearchBlock);
// CreateOrShowMFindProgWWIND(0,true);
}
// called if the global is set
// this is called when the window containing the first find comes to the end
WindowRef ContinueMultifileFind(void)
{
    
    MultiFileFindParmPtr newBlock =gMultiFileFindBlock;
    WindowRef nextWindow = 0;
    if(gMultiFileFindBlock){
    WindowRef *theRefs = newBlock->windowOrder;
    newBlock->position ++;
    theRefs = theRefs + newBlock->position;
    nextWindow = *theRefs;
    if(nextWindow){
        newBlock->nowWindow = nextWindow;
        SelectWindow(nextWindow);
    // now
        
        SLibSearchParamBlockPtr theSearchBlock = malloc(sizeof(SLibSearchParamBlock));
        SLibSearchParamBlockPtr xtheSearchBlock =gMultiFileFindBlock->slibBlock;
        theSearchBlock->searchFlags= xtheSearchBlock->searchFlags;  
        theSearchBlock->searchLength=        xtheSearchBlock->searchLength;
        theSearchBlock->startoffset=        xtheSearchBlock->startoffset;
        theSearchBlock->resultFlags=        xtheSearchBlock->resultFlags;
        theSearchBlock-> theFile=xtheSearchBlock->theFile; 
        theSearchBlock-> progRoutine=xtheSearchBlock-> progRoutine;
        theSearchBlock->searchString = malloc(theSearchBlock->searchLength);
        BlockMoveData(  xtheSearchBlock->searchString,  theSearchBlock->searchString,theSearchBlock->searchLength);
        // change the offset
        theSearchBlock->startoffset=0;  // xtheSearchBlock->result->foundLocation +1;
        theSearchBlock->lastSearchStarted = 0;
        PIExecuteSearch(nextWindow,theSearchBlock);
    } else {
    // done
        CleanUpMultiFileFind();
    }
    }
    return(nextWindow);
}

void NextFileInMultifileFind(void)
{

}

// purges the global when a find is done without the checkbox being set.
void CleanUpMultiFileFind(void)
{
    MultiFileFindParmPtr newBlock =gMultiFileFindBlock;
    if(newBlock){
    free((Ptr)newBlock->windowOrder);
        free(newBlock);
        gMultiFileFindBlock = 0;
    }
}


void CancelMultiFind(void)
{

}
/*
 *  MFindProgWindow.c
 *  PeeKit
 *
 *  Created by C.K. Haun on Tue Feb 04 2003.
 *  Copyright (c) 2003 Ravenware Software. All rights reserved.
 *
 */
/*
 GenericWindow

 */

enum{kMFindProgOKButt=1000,kMFindProgGoToRWare = 4689,kMFindProgEmailUs = 5500,
kMFindProgFredWare = 9000,kFredIconWell = 9125
};
#define __MFindProgPeekWINDOW__

// #include <HIServices/Icons.h>

void HitMFindProgWWIND(WindowRef theWindow, Point *thePoint);
static  OSStatus MFindProgOKiEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );

void GoToRware(void);

struct MFindProgWWindData {
    IconFamilyHandle fredFace;
    IconSuiteRef fredFaceIcons;
    short buddy;

};
typedef struct MFindProgWWindData MFindProgWWindData;
typedef MFindProgWWindData *MFindProgWWDataP/* , **MFindProgWWDataH */;
/* protos */


static void ReArrangeMFindProgWWControls(WindowRef theWindow, MFindProgWWDataP nowCD);
static void SizeMFindProgWWINDControls(WindowRef theWindow);

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

    return(myStatus);

}
OSStatus MFindProgWWindowClickCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData);

OSStatus MFindProgWWindowClickCode(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
    HitMFindProgWWIND((WindowRef)inUserData,&theMouse);
    mPullPort

        CallNextEventHandler(
                             inHandlerCallRef,
                             inEvent);

    return(myStatus);

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

    return(myStatus);

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

    SizeMFindProgWWINDControls((WindowRef) inUserData);

    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

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


    HideWindow((WindowRef)inUserData);
CancelMultiFind();
    return(myStatus);

}

OSStatus MFindProgWWindowSpecialCode(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 MFindProgOKiEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
HideWindow((WindowRef)inUserData);

    return(0);
}


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

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

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

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

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



void HitMFindProgWWIND(WindowRef theWindow, Point *thePoint)
{

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


void PrepEditMFindProgWWIND(WindowRef theWindow, Boolean *theEdits)
{
}

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

    mPushPort(theWindow)
        SizeMFindProgWWINDControls(theWindow);
    mPullPort
}

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

}

void IdleMFindProgWWIND(WindowRef theWindow, Boolean front)
{

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

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

}

void KeyMFindProgWWIND(WindowRef theWindow, EventRecord *theEvent)
{

}

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

void SendMFindProgWWINDBoard(void)
{

}

void invalMFindProgWWIND(void)
{
    WindowRef theW = FindWindowByKind(kMFindProgWWINDWindow);
    mPushPort(theW)
        ZapPort(theW);
    mPullPort
}

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

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

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

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

}

WindowRef CreateOrShowMFindProgWWIND(FSSpecPtr thexFile, Boolean visi)
{
#pragma unused (theFile )
    LoopVar qq;
    FSRef theFsRef;
    FSSpec theSpec;
    OSErr    theStatus =0;
    WindowPrefStructHdl thePrefs=nil;
    EventTypeSpec	MFindProglControlEvents[] =
    {
    {kEventClassControl,    kEventControlHit}
    };
    EventTypeSpec	fwControlEvents[] =
    {
    {kEventClassControl,    kEventControlHit},
    {kEventClassControl,    kEventControlDraw},
      {kEventClassControl,    kEventControlClick}   


    };
    
    WCH tempWC;
    MFindProgWWDataP nowCD;
    OSErr myErr = 0;
    AliasHandle theA;
    WindowRef MFindProgWWINDWindow = nil;
    
    // see if it exists yet
    MFindProgWWINDWindow= FindWindowByKind(kMFindProgWWINDWindow);
    if(MFindProgWWINDWindow){ShowWindow(MFindProgWWINDWindow);return(MFindProgWWINDWindow);}


    myErr = CreateWindowFromNib(gNibRef, CFSTR("MFindProg"), &MFindProgWWINDWindow);
    
// NEW
    InstallMFindProgWWindowHandlers(
                                MFindProgWWINDWindow,
                                0,
                                0);

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

    SetWindowKind(MFindProgWWINDWindow, kMFindProgWWINDWindow);
//    nowCD = (MFindProgWWDataH)NewHandleClear(sizeof(MFindProgWWindData));
    nowCD=(MFindProgWWDataP) malloc(sizeof(MFindProgWWindData));
    ZeroPtr((Ptr)nowCD,sizeof(MFindProgWWindData));

    mPushPort(MFindProgWWINDWindow)
        TextSize(9);

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

    /* set these goddam things */
    (*tempWC)->drawMe = DrawMFindProgWWIND;
    (*tempWC)->clickMe = HitMFindProgWWIND;
    (*tempWC)->saveMe = SaveMFindProgWWIND;
    (*tempWC)->closeMe = CloseMFindProgWWIND;
    (*tempWC)->keyMe = KeyMFindProgWWIND;
//    (*tempWC)->prepEdit = PrepEditMFindProgWWIND;
    (*tempWC)->editMe = EditMFindProgWWINDWindow;
//    (*tempWC)->activateMe = ActivateMFindProgWWIND;
//    (*tempWC)->deactMe = DeActivateMFindProgWWIND;
    (*tempWC)->sizeMe = SizeMFindProgWWIND;
    (*tempWC)->idleMe = IdleMFindProgWWIND;
    (*tempWC)->appleEventMe = AEMFindProgWWIND;
    (*tempWC)->generalMe = GenMFindProgWWIND;
    (*tempWC)->commandMe = CommandMFindProgWWIND;
    // new way Jose
    
    // set up the initial rects, either defaulted or saved
    InstallControlEventHandler(          SnatchCRef(MFindProgWWINDWindow,'MFID',1000),  MFindProgOKiEventHandlerProc , GetEventTypeCount(MFindProglControlEvents), MFindProglControlEvents, MFindProgWWINDWindow, NULL );

    
    if (visi) {

        ShowWindow(MFindProgWWINDWindow);

    }
    
    
    return(MFindProgWWINDWindow);
}



// size scroll bar and text area
static void SizeMFindProgWWINDControls(WindowRef theWindow)
{
#pragma unused (theWindow )
// see if we can change the menu position by mussing with the rect
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    MFindProgWWDataP nowCD = (MFindProgWWDataP)(*tempWC)->dataStore;
}






















void ReArrangeMFindProgWWControls(WindowRef theWindow, MFindProgWWDataP nowCD)
{


}








void DoMFindProgWWindMenu(short which)
{

    WindowRef theW = FindWindowByKind(kMFindProgWWINDWindow);
    WCH tempWC = (WCH)GetWRefCon(theW);
    MFindProgWWDataP nowCD = (MFindProgWWDataP)(*tempWC)->dataStore;


}



void DoMFindProg(void)
{

    CreateOrShowMFindProgWWIND(0,true);

}
#undef __MFindProgPeekWINDOW__



#undef __AboutPeekWINDOW__


