home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-09 | 14.9 KB | 623 lines | [TEXT/MPCC] |
- // ----------------------------------------------------------------------------------
- // WindowHandler.c
- // ----------------------------------------------------------------------------------
- // Handles all window commands, including window creation and window file i/o.
- //
- // ----------------------------------------------------------------------------------
- // History:
- // 8/26/94 Clark Goble Original
- //
-
- #include "WindowHandler.h"
- #include "TEHandler.h"
- #include "Globals.h"
-
- void SetWindowBounds(WindowPtr theWindow, Rect newBounds);
- Rect GetWindowContentRect(WindowPtr window);
- Rect GetWindowStructureRect(WindowPtr window);
- void LocalToGlobalRect(Rect *aRect);
- Point GetGlobalTopLeft(WindowPtr window);
- void CreateDocWindow(void);
- void CloseDocWindow(WindowPtr window);
- void UpdateDocWindow(WindowPtr updateWindow);
- void DoIdle(void);
- void DoCloseWindow(register EventRecord *evt, register WindowPtr theWindow);
- void DoClickInContent(register EventRecord *evt, register WindowPtr theWindow);
- void DoDragWindow(register EventRecord *evt, register WindowPtr theWindow);
- void DoGrowWindow(register EventRecord *evt, register WindowPtr theWindow);
- void InvalidateScrollbars(WindowPtr theWindow);
- void DoZoom(register EventRecord *evt, register WindowPtr theWindow, int part);
- void ActivateWindow(register WindowPtr newFrontWindow);
- void DeactivateWindow(register WindowPtr newBehindWindow);
-
- extern void ErrMsgCode(Str255 msg, short code);
-
- // ----------------------------------------------------------------------------------
- WindowPtr gFirstWindow;
- WindowPtr gDocWindow;
- CursHandle gTextCurs;
-
- // ----------------------------------------------------------------------------------
- // CreateDocWindow
- // ----------------------------------------------------------------------------------
- // This creates the physical window, the TE record and the like. It is empty
- // after this routine. It is also in the front (and the global gDocWindow is
- // set to it)
-
- void CreateDocWindow(void)
- {
- WindowPtr newWindow;
- Rect viewRect;
- WinDocRecord **theData;
-
- Rect vScrollRect;
-
- // create the window
- newWindow = GetNewWindow(rWindowID, nil, (WindowPtr) -1);
- SetPort(newWindow);
-
- if (newWindow == nil)
- return;
-
- // Setup the Window Data
-
- SetWTitle(newWindow, "\pUntitled");
- theData = (WinDocRecord**) NewHandle(sizeof(WinDocRecord));
- (*theData)->DocWindow = newWindow;
- (*theData)->DocNumber = 0;
- (*theData)->DocName = "\pUntitled";
- (*theData)->vRefNum = 0;
-
- // Setup the Scrollbar
-
- vScrollRect = newWindow->portRect;
- vScrollRect.left = vScrollRect.right - 15;
- vScrollRect.right += 1;
- vScrollRect.bottom -= 14;
- (*theData)->DocVScroll = NewControl( newWindow, &vScrollRect, "\p", TRUE,
- 0, 0, 0, 16, 0L);
-
- // Setup the TE View
-
- viewRect = newWindow->portRect;
- viewRect.right -= 15;
- InsetRect(&viewRect, 4, 4);
- (*theData)->DocTE = TENew(&viewRect, &viewRect);
- TEAutoView(true, (*theData)->DocTE);
- (**theData).dirty = FALSE;
-
- if ((*theData)->DocTE == NULL)
- return;
-
- // Save our data in the window
-
- SetWRefCon(newWindow, (long) theData);
-
- // This is the current window
- gDocWindow = newWindow;
-
- SetScrollBar();
-
-
- } // CreateDocWindow
-
-
-
- // ----------------------------------------------------------------------------------
- // DoSave
- // ----------------------------------------------------------------------------------
- // This physcially save the text in the data fork and the style in the resource fork.
-
- Boolean
- DoSave(unsigned char *Name, short vRefNum, TEHandle theTE)
- {
- CharsHandle theText;
- short fRefNum;
- long theTextSize;
- OSErr err;
-
- // delete possibly extant file
-
- err = FSDelete(Name, vRefNum);
- if ((err != 0) && (err != -43)) // no error or file not found
- return err;
-
- err = Create(Name, vRefNum, 'DLLL', 'TEXT');
- if (err)
- return err;
-
- err = FSOpen(Name, vRefNum, &fRefNum);
- if (err)
- return err;
- err = SetFPos(fRefNum, fsFromStart, 0L);
- if (err)
- return err;
- err = SetEOF(fRefNum, 0L);
- if (err)
- return err;
-
-
-
- // write the text
- theText = TEGetText(theTE);
- theTextSize = GetHandleSize( theText);
-
- HLock(theText);
- err = FSWrite(fRefNum, &theTextSize, *theText);
- if (err)
- return err;
- HUnlock(theText);
-
- err = FSClose(fRefNum);
-
-
- return 0;
-
- } // DoSave
-
- // ----------------------------------------------------------------------------------
- // SaveDocWindowAs
- // ----------------------------------------------------------------------------------
- // This brings up a SaveAs dialog and then (if selected) saves the window to that
- // file.
-
- void
- SaveDocWindowAs(WindowPtr window)
- {
- WinDocRecord **theData;
- SFReply reply;
- Point where;
-
- short oldResFile;
-
- oldResFile = CurResFile();
-
- theData = (WinDocRecord**) GetWRefCon(window);
-
- where.h = 100;
- where.v = 60;
-
- SFPutFile( where, "\pSave document as:", "\pUntitled", nil, &reply);
-
- if (!reply.good)
- return; // cancelled
-
- DoSave(reply.fName, reply.vRefNum, GetTE(window));
-
- (*theData)->vRefNum = reply.vRefNum;
- (*theData)->DocName = reply.fName;
- SetWTitle(window, reply.fName);
- (**theData).dirty = FALSE;
-
- UseResFile( oldResFile );
-
-
- } // SaveDocWindowAs
-
- // ----------------------------------------------------------------------------------
- // SaveDocWindow
- // ----------------------------------------------------------------------------------
- // If the window has been saved before, this saves it with the data stored in it's
- // data table. Otherwise it calls SaveAs.
-
- void
- SaveDocWindow(WindowPtr window)
- {
- WinDocRecord **theData;
- short oldResFile;
-
- theData = (WinDocRecord**) GetWRefCon(window);
-
- oldResFile = CurResFile();
-
- if ((*theData)->vRefNum == 0)
- { SaveDocWindowAs(window);
- return;
- }
-
- DoSave((*theData)->DocName, (*theData)->vRefNum, GetTE(window));
-
- (**theData).dirty = FALSE;
-
- UseResFile( oldResFile );
-
- }
-
- // ----------------------------------------------------------------------------------
- // DoOpen
- // ----------------------------------------------------------------------------------
- // This physically reads in the data and style. It can be passed the file or, if
- // passed nil, it brings up a standard open dialog box to select a file to open.
-
-
- OSErr DoOpen(FSSpec *inSpec)
- {
- long theTextSize, sizeRead;
-
- TEHandle theTE;
- char* theText;
- WinDocRecord **theData;
- OSErr err;
-
- StandardFileReply reply;
- FSSpec theFile;
- short FileRef;
- SFTypeList theTypes;
-
- theTypes[0] = 'TEXT';
-
-
- if (inSpec)
- { // was passed the file
- theFile = reply.sfFile = *inSpec;
-
- } else
- {
- StandardGetFile( nil, 1, theTypes, &reply);
-
- if (!reply.sfGood)
- return false;
-
- theFile = reply.sfFile;
- }
-
- // put up watch while opening
- SetCursor(*GetCursor(watchCursor));
-
- // create the window
- CreateDocWindow();
- SetWTitle(gDocWindow, theFile.name);
- theData = (WinDocRecord**) GetWRefCon(gDocWindow);
- (*theData)->DocName = theFile.name;
- (*theData)->vRefNum = theFile.vRefNum;
-
- // open up the data fork
- if (err = FSpOpenDF(&reply.sfFile, fsRdPerm, &FileRef))
- { ErrMsgCode("\pOpening File", err);
- return err;
- } // if
-
- // load the data
- err = GetEOF(FileRef, &theTextSize);
-
- theTE = GetTE(gDocWindow);
- theText = NewPtr(theTextSize);
- sizeRead = theTextSize;
-
- err = FSRead(FileRef, &sizeRead, (Ptr) theText);
-
- FSClose(FileRef);
-
- TEInsert(theText, sizeRead, theTE);
- DisposePtr(theText);
-
- ShowSelect(theTE, (**theData).DocVScroll);
- SetScrollBar();
- InitCursor();
-
- return 0;
- } // DoOpen
-
- // ----------------------------------------------------------------------------------
- // SaveAsk
- // ----------------------------------------------------------------------------------
- // This prompts to see if you want to save the file. Ideally this should only be
- // done if the file is dirty. Right now it always does it.
-
-
- short SaveAsk()
- {
-
- return Alert( rSaveAskID, nil);
-
- }
-
-
- // ----------------------------------------------------------------------------------
- // OpenDocWindow
- // ----------------------------------------------------------------------------------
- // Opens a doc window
-
-
- void
- OpenDocWindow()
- {
- // open a new window
- DoOpen(nil);
- }
-
- // ----------------------------------------------------------------------------------
- // CloseDocWindow
- // ----------------------------------------------------------------------------------
- // Closes a doc window.
-
- void
- CloseDocWindow(WindowPtr window)
- {
- short answer;
- WinDocRecord **data;
-
- data = (WinDocRecord**) GetWRefCon(window);
-
- if ( (**data).dirty )
- {
- answer = SaveAsk();
- if (answer == kACancel)
- return;
- if (answer == kASave)
- SaveDocWindow(window);
- }
-
-
- DisposeControl((*data)->DocVScroll);
- TEDispose(GetTE(window));
- DisposeHandle( (Handle) data );
- DisposeWindow(window);
-
- // make the new front window the current window.
- gDocWindow = FrontWindow();
-
- } // CloseDocWindow
-
-
-
- // ----------------------------------------------------------------------------------
- // UpdateDocWindow
- // ----------------------------------------------------------------------------------
- // Handle a document Updata event (Not used right now)
-
- void UpdateDocWindow(WindowPtr updateWindow)
- {
-
- }
-
-
- // ----------------------------------------------------------------------------------
- // DoIdle
- // ----------------------------------------------------------------------------------
- // Things to do at idle time (once through the event loop)
- // (Not used right now)
-
- void
- DoIdle(void)
- {
- }
-
- // ----------------------------------------------------------------------------------
- // DoCloseWindow
- // ----------------------------------------------------------------------------------
- // Close a window.
-
- void DoCloseWindow(register EventRecord *evt, register WindowPtr theWindow)
- {
- if (TrackGoAway(theWindow,evt->where))
- {
-
- if ( theWindow == nil )
- return;
-
- if ( ((WindowPeek) theWindow)->windowKind < 0 )
- { // system window
- CloseDeskAcc( ( (WindowPeek) theWindow )->windowKind );
-
- } else if ( ((WindowPeek) theWindow)->windowKind == dialogKind)
- { // dialog window
- HideWindow(theWindow);
-
- } else if (((WindowPeek) theWindow)->windowKind >= userKind)
- {
- // app window
- CloseDocWindow(theWindow);
- }// if/else/else
- } // if
-
- } // DoCloseWindow
-
- // ----------------------------------------------------------------------------------
- // DoClickInContent
- // ----------------------------------------------------------------------------------
- // Handle a click in a window.
-
- void DoClickInContent(register EventRecord *evt, register WindowPtr theWindow)
- {
- int part;
- ControlHandle theControl;
- Point pt;
- GrafPtr saveport;
- TEHandle theTE;
- Boolean theShiftKey;
-
- if(theWindow!=FrontWindow())
- {
- SelectWindow(theWindow);
- } else
- {
- GetPort(&saveport);
- SetPort(theWindow);
- pt = evt->where;
- GlobalToLocal(&pt);
-
- if( (part = FindControl(pt,theWindow,&theControl)) != 0)
- {
- DoControl(theWindow, theControl, part, pt);
- } else
- { // Clicked in TE
- // update global window for use by scroll function
- gDocWindow = theWindow;
- theTE = GetTE(theWindow);
- theShiftKey = ( (evt->modifiers & shiftKey) > 0);
-
- TEClick(pt, theShiftKey, theTE);
-
- } // if/else
-
- SetPort(saveport);
- } // if/else
- } // DoClickInContent
-
-
-
- // ----------------------------------------------------------------------------------
- // DoDragWindow
- // ----------------------------------------------------------------------------------
- // Drag a window.
-
- void DoDragWindow(register EventRecord *evt, register WindowPtr theWindow)
- {
- DragWindow(theWindow,evt->where,&qd.screenBits.bounds);
-
- } // DoDragWindow
-
- // ----------------------------------------------------------------------------------
- // DoGrowWindow
- // ----------------------------------------------------------------------------------
- // Resize a window.
-
- void DoGrowWindow(register EventRecord *evt, register WindowPtr theWindow)
- {
- long newSize;
- int newHeight,newWidth;
- Rect SizeLimits;
- GrafPtr oldPort;
- TEHandle theTE;
- WinDocRecord **theData;
- ControlHandle theControl;
-
- theData = (WinDocRecord**) GetWRefCon(theWindow);
- theControl = (*theData)->DocVScroll;
-
- SetRect(&SizeLimits, 30,50,32767,32767);
-
- newSize = GrowWindow(theWindow,evt->where,&SizeLimits);
- if (newSize == 0L)
- return;
-
- newHeight = HiWord(newSize);
- newWidth = LoWord(newSize);
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- SizeWindow(theWindow,newWidth,newHeight,true);
-
- // resize the text edit pane
-
- theTE = GetTE(theWindow);
- TEAdjustView(theWindow, theTE);
-
- InvalRect( &(theWindow->portRect) ); // mark everything for an update
-
- // resize the scrollbars
- SizeScroll(theControl, theWindow);
- SetScrollBar();
-
-
- SetPort(oldPort);
-
- } // DoGrowWindow
-
-
- // ----------------------------------------------------------------------------------
- // InvalidateScrollbars
- // ----------------------------------------------------------------------------------
- // Take care of scrollbars during window sizing
-
- void InvalidateScrollbars(WindowPtr theWindow)
- {
- Rect tempRect;
-
- SetPort(theWindow);
-
- tempRect = theWindow->portRect;
- tempRect.left = tempRect.right - 15;
- InvalRect(&tempRect);
- EraseRect(&tempRect);
-
- tempRect = theWindow->portRect;
- tempRect.top = tempRect.bottom - 15;
- InvalRect(&tempRect);
- EraseRect(&tempRect);
-
- } // InvalidateScrollbars
-
-
- // ----------------------------------------------------------------------------------
- // GetTE
- // ----------------------------------------------------------------------------------
- // This gets the TE handle from the window's ref data.
-
- TEHandle GetTE(WindowPtr window)
- {
- WinDocRecord **theData;
-
- theData = (WinDocRecord**) GetWRefCon(window);
- return (*theData)->DocTE;
-
- } // GetTE
-
- // ----------------------------------------------------------------------------------
- // DoZoom
- // ----------------------------------------------------------------------------------
- // Zooms a window between sysem size and user size.
-
- void DoZoom(register EventRecord *evt, register WindowPtr theWindow, int part)
- {
-
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- if(TrackBox(theWindow,evt->where,part)) {
- ZoomWindow(theWindow,part,true);
- }
-
- SetPort(savePort);
- }
-
- // ----------------------------------------------------------------------------------
- // ActivateWindow
- // ----------------------------------------------------------------------------------
- // A window is becoming active.
-
- void ActivateWindow(register WindowPtr newFrontWindow)
- {
-
- TEActivate( GetTE(newFrontWindow) );
- gDocWindow = newFrontWindow;
-
- } // ActivateWindow
-
- // ----------------------------------------------------------------------------------
- // DeactivateWindow
- // ----------------------------------------------------------------------------------
- // A window is being deactivated.
-
- void DeactivateWindow(register WindowPtr newBehindWindow)
- {
- TEDeactivate ( GetTE(newBehindWindow) );
- } // DeactivateWindow
-
- void WindowCursor(WindowPtr theWindow)
- {
- Point theMouse;
- TEHandle theTE;
- Rect theView;
- GrafPtr oldPort;
-
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- GetMouse(&theMouse);
- theTE = GetTE(theWindow);
- theView = (*theTE)->viewRect;
-
- if (PtInRect(theMouse, &theView)) // in text view
- SetCursor(*gTextCurs);
- else
- InitCursor();
-
- SetPort(oldPort);
- } // DeactivateWindow
-