home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- Project: POV-Ray
-
- Version: 3
-
- File: TextEditor.c
-
- Description:
- This file contains Macintosh-specific routines for the
- source code text editor.
- ------------------------------------------------------------------------------
- Author:
- Symantec, mods by Jim Nitchals, more mods & Think 6 updates by Eduard [esp] Schwan
- ------------------------------------------------------------------------------
- from Persistence of Vision(tm) Ray Tracer
- Copyright 1996 Persistence of Vision Team
- ------------------------------------------------------------------------------
- NOTICE: This source code file is provided so that users may experiment
- with enhancements to POV-Ray and to port the software to platforms other
- than those supported by the POV-Ray Team. There are strict rules under
- which you are permitted to use this file. The rules are in the file
- named POVLEGAL.DOC which should be distributed with this file. If
- POVLEGAL.DOC is not available or for more info please contact the POV-Ray
- Team Coordinator by leaving a message in CompuServe's Graphics Developer's
- Forum. The latest version of POV-Ray may be found there as well.
-
- This program is based on the popular DKB raytracer version 2.12.
- DKBTrace was originally written by David K. Buck.
- DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
- ------------------------------------------------------------------------------
- Change History:
- 920815 [jln] version 1.0 Mac released.
- 920908 [esp] version 1.1 alpha Mac
- 920912 [esp] changed window id to 1000 & moved #define here from POVMac.h
- 921110 [esp] Added TextEditor.h file & pulled in defs to there
- 921207 [esp] Added code to handle zoom box
- 921211 [esp] Fixed bug in Open: if open failed, left New & Open items dimmed
- 930710 [esp] fixed "cancelling of save-as dialog" logic
- 930817 [esp] Added FlushVol calls in SaveFile and SaveAs to force disk update
- 931001 [esp] version 2.0 finished (Released on 10/4/93)
- 931119 [djh] 2.0.1 conditionally compiles for PPC machines, keyword __powerc
- 940416 [PFS] 2.2.1 greatly reworked to clean up PPC support and provide CodeWarrior projects
- ==============================================================================*/
-
-
- #include "TextEditor.h"
-
- /*==== Standard C headers ====*/
- #include <stdio.h>
-
- /*==== Mac toolbox headers ====*/
-
- #include <types.h> // basic types
- #include <desk.h> // SystemEdit, etc.
- #include <errors.h> // noErr, etc.
- #include <files.h> // FSOpen, etc.
- #include <fonts.h> // monaco, etc.
- #include <memory.h> // NewHandle, etc.
- #include <menus.h> // MenuHandle, etc.
- #include <packages.h> // SFGetFile, etc.
- #include <scrap.h> // ZeroScrap, etc.
- #include <toolutils.h> // watchCursor, etc.
- #include <windows.h> // SizeWindow, etc.
- #include <processes.h> // ExitToShell
-
-
- /*==== POV Mac Library routines =====*/
-
- #include "PovMac.h" // fmn_open
- #include "UtilLib.h"
- #include "ImageWindow.h" // CloseImageWindow(), gImageWindIsValid
- #include "FilePrefs.h"
-
- /*==== globals (external scope) ====*/
-
- WindowPtr gSrcWind_Window;
- TEHandle gSrcWind_TEH;
- Boolean gSrcWind_dirty;
- Boolean gSrcWind_visible;
- Str255 gSrcWind_FileName;
- short gSrcWind_VRefNum;
-
-
- /*==== globals (local scope) ====*/
-
- static WindowRecord gWindowRecord;
- static int gLineCount;
- static Cursor gEditCursor;
- static ControlActionUPP vScrollUPP = NULL;
- static ControlHandle gSrcWind_VScroll;
-
-
- static pascal void ScrollProc(ControlHandle theControl, short theCode);
-
- // ==============================================
- #define SrcWind_WindID 1000
- #define ErrorAlert 256
- #define AdviseAlert 257
-
- #define fmNew 1
- #define fmRevert 7
- #define fmPageSetUp 9
- #define fmPrint 10
- #define fmQuit 13
-
- #define aaSave 1
- #define aaDiscard 2
- #define aaCancel 3
-
- #define SBarWidth 15
-
- #define ours(w) ((gSrcWind_Window != NULL) && (w == gSrcWind_Window))
-
- #define kUntitledFName "\pUntitled.POV"
-
-
- // ==============================================
- int SetUpFiles(void)
- {
- pStrCopy(kUntitledFName, gSrcWind_FileName);
- gSrcWind_VRefNum = 0;
- gSrcWind_dirty = 0;
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- int DoFile(int item)
- {
- short vRef, refNum;
- Str255 fn;
-
- switch (item) {
-
- case fmn_open:
- HideWindow(gSrcWind_Window); /* gives a sense of closing the old file. */
- if (OldFile(fn, &vRef))
- {
- gSrcWind_VRefNum = 0;
- gSrcWind_dirty = 0;
- if (FSOpen(fn, vRef, &refNum)==noErr) {
- if (ReadFile(refNum, gSrcWind_TEH)==noErr) {
- pStrCopy(fn, gSrcWind_FileName);
- gSrcWind_VRefNum = vRef;
- SetWTitle(gSrcWind_Window, gSrcWind_FileName);
- }
- if (FSClose(refNum)==noErr) ;
- ShowWindow(gSrcWind_Window);
- gSrcWind_visible = TRUE;
- TESetSelect(0, 0, gSrcWind_TEH);
- ShowSelect();
- }
- else
- FileError("\pError opening ", fn);
- }
- break;
-
- case fmn_close:
- if (gSrcWind_dirty)
- {
- int notClosed=0;
- ParamText("\pSave changes to ā",
- gSrcWind_FileName,
- "\pā?", "\p");
- switch (Alert(AdviseAlert, 0L))
- {
- case aaSave:
- if (gSrcWind_VRefNum == 0)
- { // create new file
- pStrCopy(gSrcWind_FileName, fn);
- if (!SaveAs(fn, &vRef))
- notClosed=1;
- }
- else // save existing file
- if (!SaveFile(gSrcWind_FileName, gSrcWind_VRefNum))
- notClosed=1;
- break;
- case aaCancel:
- notClosed=1;
- case aaDiscard:
- gSrcWind_dirty = 0;
- }
- if (notClosed)
- return 0; // error, just return
- } // if dirty
-
- CloseMyWindow();
- break;
-
- case fmn_save:
- // if file already open
- if (gSrcWind_VRefNum != 0)
- {
- if (!SaveFile(gSrcWind_FileName, gSrcWind_VRefNum))
- return(0);
- break;
- }
- // else drop through to here..
-
- case fmn_saveas:
- pStrCopy(gSrcWind_FileName, fn);
- if (SaveAs(fn, &vRef))
- {
- pStrCopy(fn, gSrcWind_FileName);
- gSrcWind_VRefNum = vRef;
- SetWTitle(gSrcWind_Window, gSrcWind_FileName);
- }
- else
- return(0);
- break;
-
- case fmRevert:
- ParamText("\pRevert to last saved version of ā",
- gSrcWind_FileName, "\pā?", "\p");
- switch (Alert(AdviseAlert, 0L)) {
- case aaSave:
- HidePen();
- TESetSelect(0, (**gSrcWind_TEH).teLength, gSrcWind_TEH);
- ShowPen();
- TEDelete(gSrcWind_TEH);
- if ((gSrcWind_VRefNum != 0) &&
- (FSOpen(gSrcWind_FileName, gSrcWind_VRefNum, &refNum)==noErr))
- {
- gSrcWind_dirty = (ReadFile(refNum, gSrcWind_TEH)==noErr);
- if (FSClose(refNum)==noErr) ;
- }
- ShowWindow(gSrcWind_Window);
- gSrcWind_visible = TRUE;
- UpdateWindow(gSrcWind_Window);
- case aaCancel:
- case aaDiscard:
- return(0);
- }
-
- break;
-
- #if defined(_DO_PRINTING_SOMEDAY_)
- case fmPageSetUp:
- DoPageSetUp();
- break;
- case fmPrint:
- PrintText( (**gSrcWind_TEH).hText, (long)(**gSrcWind_TEH).teLength, (GrafPtr)gSrcWind_Window,
- StringWidth("\pmmmm"));
- break;
- #endif // DO_PRINTING_SOMEDAY
- case fmQuit:
- if (DoFile(fmn_close))
- ExitToShell();
- }
- return(1);
- }
-
-
-
- // ==============================================
- static Point SFwhere;
- static SFReply reply;
-
-
- int SaveAs(Str255 fn, short *vRef)
- {
- short refNum;
-
- if (NewFile(fn, vRef))
- if (!CreateFile(fn, vRef, &refNum))
- {
- FileError("\pError creating file ", fn);
- return (0);
- }
- else
- {
- char hstate;
-
- hstate = HGetState((**gSrcWind_TEH).hText);
- HLock((**gSrcWind_TEH).hText);
- if (WriteFile(refNum, (*(**gSrcWind_TEH).hText), (long)(**gSrcWind_TEH).teLength))
- {
- FileError("\pError writing file ", fn);
- return (0);
- }
- HSetState((**gSrcWind_TEH).hText, hstate);
- FSClose(refNum);
- gSrcWind_dirty = 0;
- // flush any buffers to disk
- FlushVol(NULL, *vRef);
- return(1);
- }
- return (0);
- }
-
-
-
- // ==============================================
- int SaveFile(Str255 fn, short vRef)
- {
- short refNum;
- short anError;
-
- anError = FSOpen(fn, vRef, &refNum);
- if (anError != noErr)
- {
- FileError("\pError opening file ", fn);
- return (1);
- }
- else
- {
- char hstate;
-
- hstate = HGetState((**gSrcWind_TEH).hText);
- HLock((**gSrcWind_TEH).hText);
- anError = WriteFile(refNum, (*(**gSrcWind_TEH).hText), (long)(**gSrcWind_TEH).teLength);
- if (anError)
- FileError("\pError writing file ", fn);
- HSetState((**gSrcWind_TEH).hText, hstate);
- gSrcWind_dirty = 0;
- FSClose(refNum);
- // save prefs out too
- FilePrefs_Write(vRef, fn);
- // flush any buffers to disk
- FlushVol(NULL, vRef);
- return(1);
- }
- }
-
-
-
- // ==============================================
- int NewFile(Str255 fn, short *vRef)
- {
- GetBestDialogPos(&SFwhere, (WindowPtr)&gp2wWindow);
- SFPutFile(SFwhere, "\pSave POV-Ray text file as", fn, 0L, &reply);
- if (!reply.good)
- return (0);
- else {
- pStrCopy(reply.fName, fn);
- *vRef = reply.vRefNum;
- return(1);
- }
- }
-
-
-
- // ==============================================
- int OldFile(Str255 fn, short *vRef)
- {
- SFTypeList myTypes;
-
- myTypes[0]='TEXT';
-
- GetBestDialogPos(&SFwhere, (WindowPtr)&gp2wWindow);
- SFGetFile(SFwhere, "\p", 0L, 1, myTypes, 0L, &reply );
-
- if (!reply.good)
- return (0);
- else {
- pStrCopy(reply.fName, fn);
- *vRef = reply.vRefNum;
- return(1);
- }
- }
-
-
-
- // ==============================================
- int CreateFile(Str255 fn, short *vRef, short *theRef)
- {
- OSErr io;
-
- io=Create(fn, *vRef, kAppSignature, 'TEXT');
- if ((io == noErr) || (io == dupFNErr))
- io = FSOpen(fn, *vRef, theRef );
-
- return ((io == noErr) || (io == dupFNErr));
- }
-
-
-
- // ==============================================
- int WriteFile(short refNum, char *p, long num)
- {
- OSErr io;
- /* gee, somebody should check the return code for errors */
- io=FSWrite(refNum, &num, p);
- if (io)
- return(io);
- io=SetEOF(refNum, num);
- return(io);
- }
-
-
-
- // ==============================================
- #define READ_BUF_SIZE 1024L
- int ReadFile(short refNum, TEHandle textH)
- {
- OSErr io = noErr;
- long totalBytes, count;
- Ptr buffer;
-
- // allocate temp buffer to read into
- buffer = NewPtr(READ_BUF_SIZE);
- if (buffer == NULL)
- return -108; // out of mem
-
- // get rid of any previous TE stuff
- (**textH).selStart = 0;
- (**textH).selEnd = (**textH).teLength;
- TEDelete(textH);
- // how many total bytes in file?
- GetEOF(refNum, &totalBytes);
- if (totalBytes > 32767L)
- return (999);
- // read the file, "READ_BUF_SIZE" bytes at a time
- do {
- // how much to read this pass?
- count = (totalBytes >= READ_BUF_SIZE) ? READ_BUF_SIZE : totalBytes;
- // read it
- if (count > 0L)
- io = FSRead(refNum, &count, buffer);
- // stick chars into TE buffer
- if (count > 0L)
- TEInsert(buffer, count, textH);
- // calculate how much is left to read
- totalBytes -= count;
- } while ((io==noErr) && (totalBytes > 0L));
- // done with temp buffer
- DisposePtr(buffer);
-
- return (io);
- }
-
-
-
- // ==============================================
- /* copies a pascal string from p1 to p2 */
- int pStrCopy(StringPtr p1, StringPtr p2)
- {
- register int len = *p1; // added asignment
-
- *(p2++) = *(p1++); // added parens
- while (--len>=0) *(p2++)=*(p1++); // added parens
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- int FileError(Str255 s, Str255 f)
- {
- ParamText(s, f,"\p", "\p");
- Alert(ErrorAlert, 0L);
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- void PreInitWindows(void)
- {
- if (gSrcWind_Window == 0)
- {
- SetUpWindows();
- HideWindow(gSrcWind_Window); /* just in case it's already visible */
- }
- }
-
-
-
- // ==============================================
- int SetUpWindows(void)
- {
- Rect viewRect;
- Rect vScrollRect;
-
- gSrcWind_Window = GetNewWindow(SrcWind_WindID, &gWindowRecord, (WindowPtr)NULL);
- if (!gSrcWind_Window)
- return 1;
- SetPort(gSrcWind_Window);
- TextFont(monaco);
- TextSize(9);
- vScrollRect = (*gSrcWind_Window).portRect;
- vScrollRect.left = vScrollRect.right-SBarWidth;
- vScrollRect.right += 1;
- vScrollRect.bottom -= SBarWidth-1;
- vScrollRect.top -= 1;
- gSrcWind_VScroll = NewControl( gSrcWind_Window, &vScrollRect, "\pSrcVScroll", 1, 0, 0, 0,
- scrollBarProc, 0L);
-
- viewRect = qd.thePort->portRect;
- viewRect.right -= SBarWidth;
- viewRect.bottom -= SBarWidth;
- InsetRect(&viewRect, 4, 4);
- gSrcWind_TEH = TENew(&viewRect, &viewRect);
- if (gSrcWind_TEH)
- {
- // turn on auto-scrolling and outline hilighting
- TEDeactivate(gSrcWind_TEH);
- /*
- turn on autoscrolling if/when we add a clickloop proc...
- (void)TEFeatureFlag(teFAutoScr, teBitSet, gSrcWind_TEH);
- */
- (void)TEFeatureFlag(teFOutlineHilite, teBitSet, gSrcWind_TEH);
- }
- SetView((WindowPtr) qd.thePort);
-
- if (gSrcWind_VScroll)
- HideControl(gSrcWind_VScroll);
-
- gSrcWind_dirty = 0;
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- int AdjustText(void)
- {
- int oldScroll, newScroll, delta;
-
- oldScroll = (**gSrcWind_TEH).viewRect.top - (**gSrcWind_TEH).destRect.top;
- newScroll = GetControlValue(gSrcWind_VScroll) * (**gSrcWind_TEH).lineHeight;
- delta = oldScroll - newScroll;
- if (delta != 0)
- TEScroll(0, delta, gSrcWind_TEH);
- SetVScroll();
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- int SetVScroll(void)
- {
- register int n;
-
- n = (**gSrcWind_TEH).nLines-gLineCount;
-
- if ((**gSrcWind_TEH).teLength > 0 && (*((**gSrcWind_TEH).hText))[(**gSrcWind_TEH).teLength-1]=='\r')
- n++;
-
- SetControlMaximum(gSrcWind_VScroll, n > 0 ? n : 0);
- ShowControl(gSrcWind_VScroll);
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- int ShowSelect(void)
- {
- register int topLine, bottomLine, theLine;
-
- SetVScroll();
- AdjustText();
-
- topLine = GetControlValue(gSrcWind_VScroll);
- bottomLine = topLine + gLineCount;
-
- // count up the lines to current
- if ((**gSrcWind_TEH).selStart < (**gSrcWind_TEH).lineStarts[topLine] ||
- (**gSrcWind_TEH).selStart >= (**gSrcWind_TEH).lineStarts[bottomLine]) {
- for ( theLine = 0;
- (**gSrcWind_TEH).selStart >= (**gSrcWind_TEH).lineStarts[theLine];
- theLine++)
- ;
- SetControlValue(gSrcWind_VScroll, theLine - gLineCount / 2);
- AdjustText();
- }
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- void SelectAllText(void)
- {
- TESetSelect(0, -1, gSrcWind_TEH);
- SetVScroll();
- AdjustText();
- }
-
-
-
- // ==============================================
- int SetView(WindowPtr w)
- {
- (**gSrcWind_TEH).viewRect = w->portRect;
- (**gSrcWind_TEH).viewRect.right -= SBarWidth;
- (**gSrcWind_TEH).viewRect.bottom -= SBarWidth;
- InsetRect(&(**gSrcWind_TEH).viewRect, 4, 4);
-
- gLineCount = ((**gSrcWind_TEH).viewRect.bottom-(**gSrcWind_TEH).viewRect.top)/(**gSrcWind_TEH).lineHeight;
- (**gSrcWind_TEH).viewRect.bottom = (**gSrcWind_TEH).viewRect.top + (**gSrcWind_TEH).lineHeight*gLineCount;
- (**gSrcWind_TEH).destRect.right = (**gSrcWind_TEH).viewRect.right;
- TECalText(gSrcWind_TEH);
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- int UpdateWindow(WindowPtr theWindow)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- DrawControls(theWindow);
- DrawGrowIcon(theWindow);
- TEUpdate(&theWindow->portRect, gSrcWind_TEH);
- EndUpdate(theWindow);
-
- SetPort(savePort);
- return 0; // should be void fn!?
- }
-
-
- // ==============================================
- // So outside can call scrolling without accessing locals
- void ScrollEditHome(void)
- {
- SetControlValue(gSrcWind_VScroll, 0);
- AdjustText();
- }
-
- // ==============================================
- // So outside can call scrolling without accessing locals
- void ScrollEditEnd(void)
- {
- SetControlValue(gSrcWind_VScroll, (**gSrcWind_TEH).teLength);
- AdjustText();
- }
-
- // ==============================================
- // So outside can call scrolling without accessing locals
- void ScrollEditPage(short thePartCode)
- {
- ScrollProc(gSrcWind_VScroll, thePartCode);
- }
-
-
- // ==============================================
- static pascal void ScrollProc(ControlHandle theControl, short theCode)
- {
- int pageSize;
- int scrollAmt;
- int oldCtl;
-
- if (theCode == 0)
- return ;
-
- pageSize = ((**gSrcWind_TEH).viewRect.bottom-(**gSrcWind_TEH).viewRect.top) /
- (**gSrcWind_TEH).lineHeight - 1;
-
- switch (theCode) {
- case kControlUpButtonPart:
- scrollAmt = -1;
- break;
- case kControlDownButtonPart:
- scrollAmt = 1;
- break;
- case kControlPageUpPart:
- scrollAmt = -pageSize;
- break;
- case kControlPageDownPart:
- scrollAmt = pageSize;
- break;
- }
-
- oldCtl = GetControlValue(theControl);
- SetControlValue(theControl, oldCtl+scrollAmt);
-
- AdjustText();
- }
-
-
-
- // ==============================================
- int DoContent(WindowPtr theWindow, EventRecord *theEvent)
- {
- short cntlCode;
- ControlHandle theControl;
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- GlobalToLocal(&theEvent->where);
- if ((cntlCode = FindControl(theEvent->where, theWindow, &theControl)) == 0) {
- if (PtInRect(theEvent->where, &(**gSrcWind_TEH).viewRect))
- TEClick(theEvent->where, (theEvent->modifiers & shiftKey)!=0, gSrcWind_TEH);
- } else if (cntlCode == kControlIndicatorPart) {
- TrackControl(theControl, theEvent->where, 0L);
- AdjustText();
- } else
- TrackControl(theControl, theEvent->where, vScrollUPP);
-
- SetPort(savePort);
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- void MyResizeWindow(WindowPtr w, short h, short v)
- {
- Rect oldHorizBar;
- Rect r;
-
- SetPort(w);
-
- oldHorizBar = w->portRect;
- oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
-
- SizeWindow(w, h, v, false);
-
- // remember this new size in prefs
- GetGlobalWindowRect(w, &(**gFilePrefs_h).srcWind_pos);
-
- InvalRect(&w->portRect);
-
- SetView(w);
-
- EraseRect(&oldHorizBar);
-
- MoveControl(gSrcWind_VScroll, w->portRect.right - SBarWidth, w->portRect.top-1);
- SizeControl(gSrcWind_VScroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
- r = (**gSrcWind_VScroll).contrlRect;
- ValidRect(&r);
-
- SetVScroll();
- AdjustText();
-
- } // MyResizeWindow
-
-
-
- // ==============================================
- static void MyGrowWindow(WindowPtr w, Point p)
- {
- GrafPtr savePort;
- long theResult;
- Rect r;
-
- GetPort(&savePort);
- SetPort(w);
-
- GetMaxGrowRect(w, &r);
- theResult = GrowWindow(w, p, &r);
- if (theResult != 0)
- MyResizeWindow(w, LoWord(theResult), HiWord(theResult));
-
- SetPort(savePort);
- } // MyGrowWindow
-
-
-
- // ==============================================
- int CloseMyWindow(void)
- {
-
- if (gSrcWind_VRefNum != 0)
- {
- FilePrefs_Write(gSrcWind_VRefNum, gSrcWind_FileName); // Save window info
- // OK, now we're done with this file, release the pesky Working Directory
- // CloseWD(gSrcWind_VRefNum);
- }
-
- // if there's a window (always) then hide it
- if (gSrcWind_Window)
- HideWindow(gSrcWind_Window);
- gSrcWind_visible = false;
-
- // Image window also no longer valid!
- CloseImageWindow();
- gImageWindIsValid = false;
-
- // get rid of TE text (select all & delete)
- if (gSrcWind_TEH)
- {
- TESetSelect(0, (**gSrcWind_TEH).teLength, gSrcWind_TEH);
- TEDelete(gSrcWind_TEH);
- }
-
- SetVScroll();
-
- SetUpFiles();
-
- return 0; // should be void fn!?
- }
-
-
- // ==============================================
- int DoActivateEditor(Boolean becomingActive)
- {
- if (becomingActive)
- {
- TEActivate(gSrcWind_TEH);
- ShowControl(gSrcWind_VScroll);
- DrawGrowIcon(gSrcWind_Window);
- }
- else
- {
- TEDeactivate(gSrcWind_TEH);
- HideControl(gSrcWind_VScroll);
- /* the growbox should be changed immediately here */
- DrawGrowIcon(gSrcWind_Window);
- }
- return 0; // should be void fn!?
- }
-
- // ==============================================
- int main_init()
- {
- CursHandle hCurs;
-
- vScrollUPP = NewControlActionProc(ScrollProc);
- // Note: iBeamCursor & watchCursor are defined in ToolUtils.h
- hCurs = GetCursor(iBeamCursor);
- gEditCursor = **hCurs;
- gSrcWind_Window = NULL;
- SetUpFiles();
- PreInitWindows();
- return 0;
- }
-
-
- // ==============================================
- int DoEditMouseDown(int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
- {
- switch (windowPart) {
- case inGoAway:
- if (ours(whichWindow) && (gDoingRender == 0))
- if (TrackGoAway(gSrcWind_Window, myEvent->where))
- DoFile(fmn_close);
- break;
-
- case inDrag:
- if (ours(whichWindow))
- {
- SelectWindow(whichWindow);
- DragWindow(whichWindow, myEvent->where, &gDragBounds);
- GetGlobalWindowRect(whichWindow, &(**gFilePrefs_h).srcWind_pos);
- }
- break;
-
- case inGrow:
- if (ours(whichWindow))
- MyGrowWindow(whichWindow, myEvent->where);
- break;
-
- case inZoomIn:
- case inZoomOut:
- SelectWindow(whichWindow);
- if (TrackBox(whichWindow, myEvent->where, windowPart))
- {
- EraseRect(&((WindowPtr)whichWindow)->portRect);
- ZoomWindow((WindowPtr)whichWindow, windowPart, (WindowPtr)whichWindow==FrontWindow());
- MyResizeWindow(whichWindow,
- whichWindow->portRect.right - whichWindow->portRect.left,
- whichWindow->portRect.bottom - whichWindow->portRect.top);
- }
- break;
-
- case inContent:
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else if (ours(whichWindow))
- DoContent(whichWindow, myEvent);
- break;
- }
- return 0; // should be void fn!?
- }
-
-
-
- // ==============================================
- int MaintainCursor(void)
- {
- Point pt;
- WindowPeek wPtr;
- GrafPtr savePort;
-
- if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
- GetPort(&savePort);
- SetPort((GrafPtr)wPtr);
- GetMouse(&pt);
- if (PtInRect(pt, &(**gSrcWind_TEH).viewRect ) )
- SetCursor( &gEditCursor);
- else
- SetCursor(&qd.arrow);
- SetPort(savePort);
- }
- return 0; // should be void fn!?
- }
-
-
- // ==============================================
- #if defined(_DO_PRINTING_SOMEDAY_)
-
- #define topMargin 20
- #define leftMargin 20
- #define bottomMargin 20
- #define tabChar ((char)'\t')
-
-
- static THPrint hPrint = NULL;
- static int tabWidth;
-
-
- /**
- ** Prototypes for private functions.
- ** (They really should be static.)
- **
- **/
-
- int CheckPrintHandle(void);
- int MyDrawText(char *p, int count);
- int PrDoc(char **hText, long count, THPrint hPrint, int font, int size);
- int HowMany(void);
-
-
- // ==============================================
- PleaseWait()
- {
- ShowWatchCursor();
- }
-
-
- // ==============================================
- CheckPrintHandle()
- {
- if (hPrint==NULL)
- PrintDefault(hPrint = (TPrint **) NewHandle(sizeof( TPrint)));
- }
-
-
- // ==============================================
- DoPageSetUp()
- {
- PrOpen();
- CheckPrintHandle();
- if (PrStlDialog(hPrint)) ;
- PrClose();
- }
-
-
- // ==============================================
- MyDrawText(char *p, int count)
- {
- register char *p1, *p2;
- int len;
- Point pt;
-
- p1 = p;
- p2 = p+count;
- while (p<p2) {
- while ((p1<p2) && (*p1 !=tabChar)) *p1++;
- if ((len=p1-p)>0) DrawText(p, 0, p1-p);
- if (*p1==tabChar) {
- GetPen(&pt);
- Move((tabWidth-(pt.h-leftMargin)%tabWidth), 0);
- *p1++;
- }
- p = p1;
- }
- }
-
-
- // ==============================================
- PrDoc(char **hText, long count, THPrint hPrint, int font, int size)
- {
- register int line = 0;
- register int lastLineOnPage = 0;
- int length;
- Rect printRect;
- int linesPerPage;
- int lineBase;
- int lineHeight;
- register char *ptr, *p1;
- FontInfo info;
- TPPrPort printPort;
-
- printPort = PrOpenDoc(hPrint, 0L, 0L);
- SetPort((GrafPtr)printPort);
- TextFont(font);
- TextSize(size);
- printRect = (**hPrint).prInfo.rPage;
- GetFontInfo(&info);
- lineHeight = info.leading+info.ascent+info.descent;
- linesPerPage =
- (printRect.bottom-printRect.top-topMargin-bottomMargin)/lineHeight;
- HLock(hText);
- ptr = p1 = (*hText);
- do {
- PrOpenPage(printPort, 0L);
- lastLineOnPage += linesPerPage;
- MoveTo( printRect.left+leftMargin,
- (lineBase = printRect.top+lineHeight) );
- do {
- /* PrintLine: */
- while ((ptr<=(*hText)+count) && (*ptr++ != (char)'\r')) ;
- if ((length=(int)(ptr-p1)-1)>0)
- MyDrawText(p1, length);
- MoveTo( printRect.left+leftMargin, (lineBase += lineHeight));
- p1 = ptr;
- } while ((++line != lastLineOnPage) && (ptr<(*hText)+count));
- PrClosePage(printPort);
- } while (ptr<(*hText)+count);
- HUnlock(hText);
- PrCloseDoc(printPort);
- }
-
-
- // ==============================================
- PrintText(char **hText, long length, GrafPtr gp, int tabPixels)
- {
- GrafPtr savePort;
- TPrStatus prStatus;
- int copies;
-
- PrOpen();
- CheckPrintHandle();
- tabWidth = tabPixels;
- SetCursor(&qd.arrow);
- if (PrJobDialog(hPrint) != 0) {
- PleaseWait();
- GetPort(&savePort);
- for (copies=HowMany(); copies>0; copies--) {
- PrDoc (hText, length, hPrint, (*gp).txFont, (*gp).txSize);
- PrPicFile(hPrint, 0L, 0L, 0L, &prStatus);
- }
- SetPort(savePort);
- }
- PrClose();
- }
-
-
- // ==============================================
- int HowMany(void)
- {
- return( ((**hPrint).prJob.bJDocLoop==bDraftLoop) ?
- (**hPrint).prJob.iCopies : 1 );
- }
-
- #endif // DO_PRINTING_SOMEDAY
-