/*
 *  PISource.c
 *  PeekIt
 *
 *  Created by C.K. Haun on Sat Apr 19 2003.
 *  Copyright (c) 2003 Ravenware Software. All rights reserved.
 *
 */

#include "PeekIt.h"
extern     IBNibRef 		gNibRef;
// 'PIIT' 7878
#define Defines
enum{kPrefsTextColor=0,kPrefsBackColor,kPrefsHiColor};
extern RGBColor pDefColor[3];

#define Structs

struct PISourceWindData {

    Boolean fileDirty;
    WindowRef us;
    TXNObject theText;
    TXNFrameID theTextFrameID;
    FileTrackerPtr theFT;
};
typedef struct PISourceWindData PISourceWindData;
typedef PISourceWindData *PISourceWDataP, **PISourceWDataH;
/* protos */

// stuff needed for panbes
// sample event handler
// static  OSStatus PISourceEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );
#define Protos
void HitPISourceWIND(WindowRef theWindow, Point *thePoint);
static pascal OSStatus MyPISourceTabEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );
static  OSStatus MyPISourceGenContEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );
static  OSStatus MyPISourceColorContEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );
static  OSStatus MyPISourceDrawSTextColorHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );
static void OpenThisSourceFile(short menuItemIndex,PISourceWDataH nowCD);


static void ReArrangePISourceWControls(WindowRef theWindow, PISourceWDataP nowCD);
void SetUpPISourceWindowMenus(PISourceWDataH nowCD,UInt32 theID);

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

    myStatus = CallNextEventHandler(
                                    inHandlerCallRef,
                                    inEvent);

    return(myStatus);

}

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

myStatus = CallNextEventHandler(
                                inHandlerCallRef,
                                inEvent);

return(myStatus);

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

    return(myStatus);

}


void SizePISourceWIND(WindowRef theWindow, PISourceWDataP nowCD ,EventRecord *theEvent, short how)
{
#pragma unused (theEvent )

    mPushPort(theWindow)
 
    mPullPort
}

OSStatus PISourceWindowSizeCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
    UInt32 attributes =0;
    Rect winFrame;

    WindowRef theWindow = (WindowRef) inUserData;
    WCH  tempWC = (WCH)GetWRefCon(theWindow);
    PISourceWDataH nowCD = (PISourceWDataH)(*tempWC)->dataStore;
    
    myStatus = GetEventParameter (inEvent, kEventParamAttributes, typeUInt32,
                                  NULL, sizeof(UInt32), NULL, &attributes);
    if (attributes & kWindowBoundsChangeOriginChanged)
    {

        if((attributes & kWindowBoundsChangeZoom) == false){
// Window is being moved, do you care?
            CallNextEventHandler(
                                 inHandlerCallRef,
                                 inEvent);
            return(myStatus);
        }
    }

//    SizePISourceWIND( (WindowRef) inUserData, 0,0);

    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);
    
        GetWindowPortBounds(theWindow,&winFrame);
    winFrame.bottom = winFrame.bottom - winFrame.top;
    winFrame.right = winFrame.right - winFrame.left;

     TXNSetFrameBounds (
                            (*nowCD)->theText,
                            90,
                            0,
                            winFrame.bottom,
                            winFrame.right,
                            (*nowCD)->theTextFrameID
                            );
    
    
    return(myStatus);

    
}
OSStatus PISourceWindowCloseCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
    WindowRef theW = FindWindowByKind(kPISourceWINDWindow);
    WCH  tempWC = (WCH)GetWRefCon(theW);
    PISourceWDataH nowCD = (PISourceWDataH)(*tempWC)->dataStore;
// just hide the window
//    DisposeHandle( (*tempWC)->undoAction);
    HideWindow((WindowRef)inUserData);

//  myStatus=  CallNextEventHandler(
//                         inHandlerCallRef,
//                         inEvent);

    return(myStatus);

}

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

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

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

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

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



    myErr = InstallStandardEventHandler(GetWindowEventTarget (theWindow));



    return(myErr);

}














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

}

void IdlePISourceWIND(WindowRef theWindow, Boolean front)
{

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

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

}

void KeyPISourceWIND(WindowRef theWindow, EventRecord *theEvent)
{

}

Boolean EditPISourceWINDWindow(WindowRef theWindow, EventRecord *theEvent, short editAction)
{
#pragma unused (theEvent )
    WCH  tempWC = (WCH)GetWRefCon(theWindow);
    PISourceWDataH nowCD = (PISourceWDataH)(*tempWC)->dataStore;
    Boolean retVal = false;
#if 0
    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]);
            break;

    }
#endif
    return(retVal);
}

void SendPISourceWINDBoard(void)
{

}

void invalPISourceWIND(void)
{
    WindowRef theW = FindWindowByKind(kPISourceWINDWindow);
    mPushPort(theW)
        ZapPort(theW);
    mPullPort
}

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

    return(myErr);
}
void  CommandPISourceWIND(WindowRef theWindow, EventRef inEvent,void *inUserData,HICommand *commandStruct)
{
    WCH  tempWC = (WCH)GetWRefCon(theWindow);
    PISourceWDataH nowCD = (PISourceWDataH)(*tempWC)->dataStore;
// switch on the command
    switch((commandStruct->commandID)){
        case 'kHICommandOpen':
        //    DoSourceWindowTextFile(nowCD);
            break;
        case 'PISM':
        case 'PISC':

            OpenThisSourceFile(commandStruct->menu.menuItemIndex,nowCD);
            break;
        case 'EXPT':
        {  SInt16 outItemHit;
            StandardAlert(
                                   kAlertStopAlert,
                                   "\pJust Kidding!!!",
                                   "\pCan't export the source, write yer own app, yah bum.",
                                   0,                          &outItemHit)   ;
            
        }
            break;
        default:
#ifndef __FINAL__
{char it[5];
    DebugStr("\p command in PISource window defaulted");
    BlockMoveData(   (Ptr)&commandStruct->commandID ,(Ptr)&it[1],4);
    it[0]=4;
    DebugStr(it);
}   
#endif
        break;
    }

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

            
                // stuff the current stuff in the stuff
                //now stuff the other stuff
            break;
    }
}

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

}

WindowRef CreateOrShowPISourceWIND(FileTrackerPtr theFile, Boolean vis,Boolean newWindow)
{
#pragma unused (theFile )

    LoopVar qq;
    WCH tempWC;
    PISourceWDataH nowCD;

    OSErr myErr = 0;
    AliasHandle theA;
    short newID;
    WindowRef PISourceWINDWindow = nil;
    
    // see if it exists yet
    if(newWindow == false){
        PISourceWINDWindow= FindWindowByKind(kPISourceWINDWindow);
        if(PISourceWINDWindow){ShowWindow(PISourceWINDWindow);return(PISourceWINDWindow);}
    }

    myErr = CreateWindowFromNib(gNibRef, CFSTR("Source"), &PISourceWINDWindow);
    
// NEW
    
    tempWC = (WCH)NewHandleClear(sizeof(windowControl));
//    tempWC = (WCPtr)malloc(sizeof(windowControl));
    SetWindowKind(PISourceWINDWindow, kPISourceWINDWindow);
    // nowCD = (PISourceWDataP)malloc(sizeof(PISourceWindData));
     nowCD = (PISourceWDataH)NewHandleClear(sizeof(PISourceWindData));

    (*tempWC)->undoAction = (UndoHand)NewHandleClear(sizeof(UndoControl));
    (*tempWC)->dataStore = (Handle)nowCD;
    (*tempWC)->dataStore2 = 0;
    InstallPISourceWindowHandlers(PISourceWINDWindow,0,0);


    (*tempWC)->helpHint = nil;
    (*tempWC)->lastID = 0;
    (*tempWC)->selectionRect.top = 0;
    (*tempWC)->selectionRect.left = 0;
    (*tempWC)->selectionRect.bottom = 0;
    (*tempWC)->selectionRect.right = 0;
    (*tempWC)->currentAction = 0;
    (*tempWC)->windowSizeBounds.top=-1;
    (*tempWC)->windowSizeBounds.left= -1;
    (*tempWC)->windowSizeBounds.bottom= -1;
    (*tempWC)->windowSizeBounds.right= -1;
    (*tempWC)->contentInset.top= -1;
    (*tempWC)->contentInset.left= -1;
    (*tempWC)->contentInset.bottom= -1;
    (*tempWC)->contentInset.right= -1;
    (*tempWC)->toolBarWide=0;                                      /* don't know if this is perm or not */
    (*tempWC)->arrowAmount=0;
    (*tempWC)->pageAmount=0;
    (*tempWC)->maxContentHi=0;
    (*tempWC)->maxContentWide=0;
    (*tempWC)->auxControls=0;
    (*tempWC)->hasSelection=false;                                   /* is there a selection? */
    (*tempWC)->windowDirty=false;
    (*tempWC)->windAlias=0;
    (*tempWC)->refNum=0;
    (*tempWC)->windowName[0]=0;

    SetWRefCon(PISourceWINDWindow, (long)tempWC);




    AddWindowID(PISourceWINDWindow);

    /* set these goddam things */
    (*tempWC)->drawMe = 0; // DrawPISourceWindow;
    (*tempWC)->clickMe =0; //  HitPISourceWIND;
    (*tempWC)->saveMe = SavePISourceWIND;
    (*tempWC)->closeMe = ClosePISourceWIND;
    (*tempWC)->keyMe = KeyPISourceWIND;
    (*tempWC)->editMe = EditPISourceWINDWindow;
    (*tempWC)->sizeMe = SizePISourceWIND;
    (*tempWC)->idleMe = IdlePISourceWIND;
    (*tempWC)->appleEventMe = AEPISourceWIND;
    (*tempWC)->generalMe = GenPISourceWIND;
    (*tempWC)->commandMe = CommandPISourceWIND;
    (*nowCD)->theFT=0;
    (*nowCD)->us = PISourceWINDWindow;



    {    void PISourceFillUpFileMenu(PISourceWDataH nowCD);
        PISourceFillUpFileMenu(nowCD);
    }
// create and attach the text thingie
    {     OSStatus MyCreateTextObject (const FSSpec *fileSpecPtr,
                                       WindowRef  theWindow);
        MyCreateTextObject (0,
                            PISourceWINDWindow);

    }
    if (vis) {
        ShowWindow(PISourceWINDWindow);
        OpenThisSourceFile(1, nowCD);
        TXNDraw(
                (*nowCD)->theText,0);
    }
// should fill in the menu with source files


    
    return(PISourceWINDWindow);
}





void ReArrangePISourceWControls(WindowRef theWindow, PISourceWDataP nowCD)
{

}

void SetUpPISourceWindowMenus(PISourceWDataH nowCD,UInt32 theID)
{
    static MenuCommand fileenablers[]={  kHICommandQuit ,

        kHICommandPreferences,  kHICommandAbout,
        kHICommandOpen ,  kHICommandClose ,kExportCommand,
        kHICommandSaveAs,
        kHICommandPrint ,
        kHICommandPageSetup,
        kHICommandAppHelp

    };
    static MenuCommand editenablers[]={
        kHICommandCopy
    };

    LoopVar qq;
    switch(theID){
        case kFileMenu:
            for(qq=0;qq<sizeof(fileenablers)/sizeof(MenuCommand);qq++){
                EnableMenuCommand(0,fileenablers[qq]);
            }
            break;
        case kEditMenu:
#if 0
            if((*nowCD)->startSel){
// enable editors
                for(qq=0;qq<sizeof(editenablers)/sizeof(MenuCommand);qq++){
                    EnableMenuCommand(0,editenablers[qq]);
                }
// see if there is a dingus
                if((*nowCD)->changes){
                    EnableMenuCommand(0,kHICommandUndo);
                }
        // always disable paste.  Hate paste, it tastes all yuuky
                DisableMenuCommand(0,kHICommandPaste);


            }
//
#endif            
            break;


#if 0
            others
                kHICommandUndo ,kHICommandRedo,kHICommandCut ,kHICommandCopy , kHICommandPaste ,kHICommandClear, kHICommandSelectAll           = 'sall',

                kHICommandRevert              = 'rvrt',
                kHICommandSave                = 'save',
                kHICommandNew                 = 'new ',

#endif
    }
}








void DoPISourceWindMenu(short which)
{

    WindowRef theW = FindWindowByKind(kPISourceWINDWindow);
    WCH  tempWC = (WCH)GetWRefCon(theW);
    PISourceWDataH nowCD = (PISourceWDataH)(*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
}





static  OSStatus MyPISourceGenContEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
#if 0
    ControlRef theCont;
    ControlID theID;
    GetEventParameter (inEvent, kEventParamDirectObject, typeControlRef,
                       NULL, sizeof(ControlRef), NULL, &theCont);
    GetControlID(
                 theCont,
                 &theID);
    switch(theID.id){
        case 1000:   // ok
                     // harvest the controls and close
            HarvestPrefs((WindowRef)inUserData);
            DisposeWindow((WindowRef)inUserData);

            break;

        case 1001:  // cancel
                    // go away
            DisposeWindow((WindowRef)inUserData);
            break;

        case 1002:  // revert
            SetPrefsToStored((WindowRef)inUserData);

            break;

    }
#endif
    return( eventNotHandledErr );
}


void PISourceWindowOpenFile(void)
{
    FileTrackerPtr theFT = NewClearFileTracker();
    OSStatus myErr = SimpleGetFile( theFT);
    if(myErr == noErr){
        CreateOrShowPISourceWIND( theFT,true,true);
    }




}
OSStatus MyCreateTextObject (const FSSpec *fileSpecPtr,
                             WindowRef  theWindow)
{   WCH  tempWC = (WCH)GetWRefCon(theWindow);
    PISourceWDataH nowCD = (PISourceWDataH)(*tempWC)->dataStore;


    OSStatus status;
    TXNObject   textObject = NULL; // text object
    TXNFrameID  frameID    = 0; // ID for text frame
    TXNFrameOptions frameOptions;
    Rect winFrame;
    frameOptions = /* kTXNDrawGrowIconMask |  kTXNWantHScrollBarMask | */ kTXNWantVScrollBarMask  | kTXNReadOnlyMask | kTXNNoSelectionMask  |  kTXNDoNotInstallDragProcsMask | kTXNDisableDragAndDropMask ;

    GetWindowPortBounds(theWindow,&winFrame);
    InsetRect(&winFrame,1,1);
    winFrame.top+=89;

status=     TXNNewObject (
                         0 , //  const FSSpec * iFileSpec,
                           theWindow,
                           &winFrame,
                           frameOptions,
                           kTXNTextEditStyleFrameType,
0,//                            TXNFileType iFileType,
                           0, // TXNPermanentTextEncodingType iPermanentEncoding,
                           &textObject,
                           &frameID,
                          theWindow
                           );
(*nowCD)->theText = textObject;
(*nowCD)->theTextFrameID = frameID;
status=    SetWindowProperty (theWindow, 'POOP','LOOP',
                       sizeof (TXNObject), &textObject);
status=    SetWindowProperty (theWindow, 'POOP','LOOP',
                       sizeof (TXNFrameID),&frameID); 
    // Activate the text object.

status = TXNActivate (textObject, frameID, kScrollBarsAlwaysActive);
    TXNFocus(textObject,true);
    

}

OSStatus GetAppResourceFileFSSpec(Str255 name, FSSpecPtr theSpec, FSRef *theRef)
{
    OSStatus myErr = noErr;
    CFURLRef    seagullURL;
//    FSRef theRef;
    CFStringRef theS = CFStringCreateWithPascalString(0, name, 0);
    Boolean isok;
    // Look for a resource in the main bundle by name and type.
    seagullURL = CFBundleCopyResourceURL( CFBundleGetMainBundle(),
                                          theS,
                                         NULL,
                                          NULL );
    CFRelease(theS);

     isok = CFURLGetFSRef(seagullURL, theRef);
    CFRelease(seagullURL);
    if (!ok) return -50;


    return FSGetCatalogInfo(theRef, kFSCatInfoNone,
                            NULL, NULL, theSpec, NULL);
    
    

}
OSErr GetApplicationResourceFSSpecFromBundle(FSSpecPtr theFSSpecPtr)
{
    OSErr err = fnfErr;
    CFBundleRef myAppsBundle = CFBundleGetMainBundle();
    if (myAppsBundle == NULL) return err;
    CFURLRef myBundleURL = CFBundleCopyResourcesDirectoryURL(myAppsBundle);
    if (myBundleURL == NULL) return err;


    FSRef myBundleRef;
    Boolean ok = CFURLGetFSRef(myBundleURL, &myBundleRef);
    CFRelease(myBundleURL);
    if (!ok) return err;


    return FSGetCatalogInfo(&myBundleRef, kFSCatInfoNone,
                            NULL, NULL, theFSSpecPtr, NULL);
}


void OpenThisSourceFile(short menuItemIndex,PISourceWDataH nowCD)
{OSStatus myErr = 0;
    FSRef theRef;
    Str255 theFiles[]={"\pInitPeekIt",
        "\pfmenuhandler",
        "\pSupport.peekit",
        "\pPrefs",
        "\ppaneHIView",
        "\pAboutPeekIt",
        "\pAppleEvents",
        "\pmain",
        "\pPISource",
        "\pdebug.peekit",
        "\pFileManagement",
        "\pPeekItMainWind"
        
    };
    
    FileTrackerPtr theFT=NewClearFileTracker();
    FSSpec theSpecToMake;
    // first see if a file is already open
    if((*nowCD)->theFT){
// close and clear
        FSClose((*nowCD)->theFT->dataForkRef);
        DisposeFileTracker((*nowCD)->theFT);
        (*nowCD)->theFT = 0;
    }
    
     GetAppResourceFileFSSpec(theFiles[menuItemIndex-1], theFT->fsSpec,theFT->fsRef);
    theFT->fsSpecIsValid = true;
    theFT->fsRefIsValid = true;

    theFT->openDataFork = true;
    myErr = OpenFile(theFT);
    (*nowCD)->theFT = theFT;
    myErr = TXNSetDataFromFile(
                               (*nowCD)->theText,
                               theFT->dataForkRef,
                               'TEXT',
                               kTXNEndOffset,
                               0,
                               kTXNEndOffset);
     TXNShowSelection (
                           (*nowCD)->theText,
                           kTXNStartOffset
                           );
    
    TXNDraw(
            (*nowCD)->theText,0);

}

void PISourceFillUpFileMenu(PISourceWDataH nowCD)
{
#if 0
    OSErr FSGetCatalogInfoBulk (
                                FSIterator iterator,
                                ItemCount maximumObjects,
                                ItemCount * actualObjects,
                                Boolean * containerChanged,
                                FSCatalogInfoBitmap whichInfo,
                                FSCatalogInfo * catalogInfos,
                                FSRef * refs,
                                FSSpec * specs,
                                HFSUniStr255 * names
                                );
#endif
}


// void PISourceSwitchFile(nowCD


#undef __PISourceWINDOW__



#if 0
// generic handler

// enbraced for quick installes
{
    EventTypeSpec	CCCCControlEvents[] =
    {
    {kEventClassControl,    kEventControlDraw}
    };

    static  OSStatus PICCCCEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );
    InstallControlEventHandler(  (*nowCD)->rawScroll,  PICCCCEventHandlerProc , GetEventTypeCount(scrollControlEvents), CCCCControlEvents, PIMainWINDWindow, NULL );
}

static  OSStatus PICCCCEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    OSStatus myErr = noErr;
    WindowRef theWindow=(WindowRef)inUserData;
    WCH tempWC = (WCH)GetWRefCon((WindowRef)inUserData);
    PISourceWDataH nowCD = (PISourceWDataH)(*tempWC)->dataStore;
//    UInt32 theVal;
//    UInt32 newVal;
    ControlRef theCont;
    ControlPartCode thePart;

    GetEventParameter (inEvent, kEventParamDirectObject, typeControlRef,
                       NULL, sizeof(ControlRef), NULL, &theCont);
    GetEventParameter (inEvent, kEventParamControlPart, typeControlPartCode,
                       NULL, sizeof(ControlPartCode), NULL, &thePart);
    
//    theVal=GetControl32BitValue(theCont);


    return(myErr);

}

#endif

