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

 */

enum{kABoutOKButt=1000,kAboutGoToRWare = 4689,
kAboutFredWare = 9000
};
#define __AboutPeekWINDOW__

#include "PeekIt.h"
void HitAboutWWIND(WindowRef theWindow, Point *thePoint);
static  OSStatus AboutOKiEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData );

void GoToRware(void);

struct AboutWWindData {

    short buddy;

};
typedef struct AboutWWindData AboutWWindData;
typedef AboutWWindData *AboutWWDataP, **AboutWWDataH;
/* protos */


static void ReArrangeAboutWWControls(WindowRef theWindow, AboutWWDataH nowCD);
void RealAboutDrawLoop(RgnHandle realUpdate,WindowRef theWindow);
static void SizeAboutWWINDControls(WindowRef theWindow);

OSStatus AboutWWindowDrawingCode(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
    OSStatus myStatus = noErr;
    RealAboutDrawLoop(0,(WindowRef)inUserData);
    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

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

        CallNextEventHandler(
                             inHandlerCallRef,
                             inEvent);

    return(myStatus);

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

    return(myStatus);

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

    SizeAboutWWINDControls((WindowRef) inUserData);

    CallNextEventHandler(
                         inHandlerCallRef,
                         inEvent);

    return(myStatus);

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


    HideWindow((WindowRef)inUserData);

    return(myStatus);

}

OSStatus AboutWWindowSpecialCode(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 FredWareEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    ControlRef theCont;
    Rect theRect;
    ControlID theID;
    UInt32 theKind;
    SInt16 outItemHit;
    OSStatus myErr = noErr;
    mPushPort((WindowRef)inUserData);
    GetEventParameter (inEvent, kEventParamDirectObject, typeControlRef,
                       NULL, sizeof(ControlRef), NULL, &theCont);
    GetControlID(
                 theCont,
                 &theID);
    theKind = GetEventKind(inEvent);
    GetControlBounds(theCont,&theRect);

    switch(theKind){
        case kEventControlHit:
case kEventControlClick:
            StandardAlert(
                          kAlertDefaultOKText,
                          "\pThis is FredWare!",
                          "\pIf you don't like it, write a better one yourself!",
                          0,                          &outItemHit)   ;
            
            break;
        case kEventControlDraw:

        {SInt16 old=GetPortTextSize((CGrafPtr)inUserData);
            TextSize(26);
            CtrDraw2("\pFredWare",&theRect);
            TextSize(old);

                
        }
            break;
        
    }


    mPullPort;
    return(myErr);

}



static  OSStatus AboutGTRWEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
    GoToRware();

    return(0);
}

static  OSStatus AboutOKiEventHandlerProc( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
{
HideWindow((WindowRef)inUserData);
    return(0);
}


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

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

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

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

    myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(AboutWWindowCloseCode),     sizeof(windowCloseEventList)/sizeof(EventTypeSpec),    &windowCloseEventList, (void *)theWindow,      &windowOutRef);
    
// taking the special out for a miunutes
// myErr = InstallEventHandler (GetWindowEventTarget (theWindow), NewEventHandlerUPP(AboutWWindowSpecialCode),     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 RealAboutDrawLoop(RgnHandle realUpdate,WindowRef theWindow)

{
    Rect winRect;
    WCH tempWC = (WCH)GetWRefCon(theWindow);
    AboutWWDataH nowCD = (AboutWWDataH)(*tempWC)->dataStore;

    LockHandle(nowCD);
    GetWindowPortBounds((theWindow),&winRect);

    FreeHandle(nowCD);

}

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



void HitAboutWWIND(WindowRef theWindow, Point *thePoint)
{

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


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

    mPushPort(theWindow)
        SizeAboutWWINDControls(theWindow);
    mPullPort
}

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

}

void IdleAboutWWIND(WindowRef theWindow, Boolean front)
{

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

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

}

void KeyAboutWWIND(WindowRef theWindow, EventRecord *theEvent)
{

}

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

}

void invalAboutWWIND(void)
{
    WindowRef theW = FindWindowByKind(kAboutWWINDWindow);
    mPushPort(theW)
        ZapPort(theW);
    mPullPort
}

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

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

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

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

}

WindowRef CreateOrShowAboutWWIND(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;
    AboutWWDataH nowCD;
    OSErr myErr = 0;
    AliasHandle theA;
    WindowRef AboutWWINDWindow = nil;
    
    // see if it exists yet
    AboutWWINDWindow= FindWindowByKind(kAboutWWINDWindow);
    if(AboutWWINDWindow){ShowWindow(AboutWWINDWindow);return(AboutWWINDWindow);}


    myErr = CreateWindowFromNib(gNibRef, CFSTR("About"), &AboutWWINDWindow);
    
// NEW
    InstallAboutWWindowHandlers(
                                AboutWWINDWindow,
                                0,
                                0);

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

    SetWindowKind(AboutWWINDWindow, kAboutWWINDWindow);
    nowCD = (AboutWWDataH)NewHandleClear(sizeof(AboutWWindData));
    mPushPort(AboutWWINDWindow)
        TextSize(9);

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

    /* set these goddam things */
    (*tempWC)->drawMe = DrawAboutWWIND;
    (*tempWC)->clickMe = HitAboutWWIND;
    (*tempWC)->saveMe = SaveAboutWWIND;
    (*tempWC)->closeMe = CloseAboutWWIND;
    (*tempWC)->keyMe = KeyAboutWWIND;
//    (*tempWC)->prepEdit = PrepEditAboutWWIND;
    (*tempWC)->editMe = EditAboutWWINDWindow;
//    (*tempWC)->activateMe = ActivateAboutWWIND;
//    (*tempWC)->deactMe = DeActivateAboutWWIND;
    (*tempWC)->sizeMe = SizeAboutWWIND;
    (*tempWC)->idleMe = IdleAboutWWIND;
    (*tempWC)->appleEventMe = AEAboutWWIND;
    (*tempWC)->generalMe = GenAboutWWIND;
    (*tempWC)->commandMe = CommandAboutWWIND;
    // new way Jose
    
    // set up the initial rects, either defaulted or saved
    SizeAboutWWINDControls(AboutWWINDWindow);
    InstallControlEventHandler(          SnatchCRef(AboutWWINDWindow,'ABOU',1000),  AboutOKiEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, AboutWWINDWindow, NULL );
// ABOU 9000
    InstallControlEventHandler(          SnatchCRef(AboutWWINDWindow,'ABOU',9000),  FredWareEventHandlerProc , GetEventTypeCount(fwControlEvents), fwControlEvents, AboutWWINDWindow, NULL );

    
        
        InstallControlEventHandler(          SnatchCRef(AboutWWINDWindow,'ABOU',kAboutGoToRWare),  AboutGTRWEventHandlerProc , GetEventTypeCount(aboutlControlEvents), aboutlControlEvents, AboutWWINDWindow, NULL );

    
// add our ports

    if (visi) {
        ShowWindow(AboutWWINDWindow);
     // ••• FIX   AddToWindowMenu(AboutWWINDWindow, nil, kWindowMenu);

    }


    return(AboutWWINDWindow);
}



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






















void ReArrangeAboutWWControls(WindowRef theWindow, AboutWWDataH 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 DoAboutWWindMenu(short which)
{

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



void DoAbout(void)
{

    CreateOrShowAboutWWIND(0,true);

}
void GoToRware(void)
{
    LSOpenCFURLRef(
                   CFURLCreateWithString (
                                                   0,
                                          CFSTR("http://www.ravenware.com"),
                                                   0
                                                   )
                   ,
                   0) ;
}
#undef __AboutPeekWINDOW__


