home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************************
- *
- *
- * ObjectMacZapp -- a standard Mac OOP application template
- *
- *
- *
- * ZTextWindow.cpp -- a window that displays text files (uses TextEdit)
- *
- *
- *
- *
- *
- * © 1996, Graham Cox
- *
- *
- *
- *
- *************************************************************************************************/
-
-
-
- #include "ZTextWindow.h"
- #include "ZErrors.h"
- #include "ZDefines.h"
-
-
- ZTextWindow::ZTextWindow(ZCommander* aBoss, short windID, Boolean allowEditing)
- : ZScroller(aBoss, windID, TRUE, TRUE)
- {
- itsText = NULL;
- isEditable = allowEditing;
- }
-
- ZTextWindow::~ZTextWindow()
- {
- if (itsText)
- TEDispose(itsText);
- }
-
-
- void ZTextWindow::InitZWindow()
- {
- inherited::InitZWindow();
-
- MakeTextEdit();
- }
-
- void ZTextWindow::DrawContent()
- {
- Rect r;
-
- GetContentRect(&r);
- TEUpdate(&r, itsText);
- }
-
- void ZTextWindow::ClickContent(Point mouse, short modifiers)
- {
- if (isEditable)
- TEClick(mouse, (modifiers & shiftKey), itsText);
- }
-
- void ZTextWindow::Activate()
- {
- inherited::Activate();
- TEActivate(itsText);
- }
-
- void ZTextWindow::Deactivate()
- {
- TEDeactivate(itsText);
- inherited::Deactivate();
- }
-
-
- void ZTextWindow::Idle()
- {
- Rect content;
- Point mouse;
- CursHandle aCursor;
-
- if (isEditable)
- {
- TEIdle( itsText);
-
- GetContentRect(&content);
- Focus();
- GetMouse(&mouse);
-
- if (PtInRect(mouse, &content))
- {
- aCursor = GetCursor(iBeamCursor);
- SetCursor(*aCursor);
- }
- else
- SetCursor(&qd.arrow);
- }
- }
-
-
- void ZTextWindow::SetSize(short width, short height)
- {
- inherited::SetSize(width,height);
- RecalText();
- Draw();
- }
-
-
- void ZTextWindow::Zoom(short partCode)
- {
- inherited::Zoom(partCode);
- RecalText();
- Draw();
- }
-
-
-
- void ZTextWindow::Scroll(short dH, short dV)
- {
- TEPinScroll(dH, dV, itsText);
- }
-
-
-
- void ZTextWindow::Type(char theChar)
- {
- if (isEditable)
- {
- TEKey(theChar, itsText);
-
- dirty = TRUE;
- }
- }
-
-
- void ZTextWindow::MakeTextEdit()
- {
- Rect destRect, viewRect;
-
- // the dest rect is the scrollbounds, but that hasn't been set yet.
- // the view rect is the content area
-
- Focus();
-
- TextFont(monaco);
- TextSize(9);
-
- emSpace = CharWidth('m');
- GetContentRect(&viewRect);
-
- destRect = viewRect;
- destRect.right = destRect.left + (emSpace * 255);
-
- FailNIL(itsText = TEStylNew(&destRect, &viewRect));
-
- SetBounds(&destRect);
- RecalText();
- }
-
-
-
- void ZTextWindow::HandleCommand(short menuID, short itemID)
- {
- if (menuID == kEditMenu && isEditable)
- {
- switch (itemID)
- {
- case kCutCmd:
- TECut( itsText );
- dirty = TRUE;
- break;
- case kCopyCmd:
- TECopy( itsText );
- break;
- case kPasteCmd:
- TEPaste( itsText );
- dirty = TRUE;
- break;
- case kClearCmd:
- TEDelete( itsText );
- dirty = TRUE;
- break;
- }
- }
-
- inherited::HandleCommand(menuID, itemID);
- }
-
-
-
- void ZTextWindow::UpdateMenus()
- {
- if (isEditable)
- {
- MenuHandle editM = GetMHandle(kEditMenu);
-
- if (editM)
- {
- EnableItem(editM, kCutCmd);
- EnableItem(editM, kCopyCmd);
- EnableItem(editM, kClearCmd);
-
- EnableItem(editM, kPasteCmd);
- }
- }
-
- inherited::UpdateMenus();
- }
-
-
- void ZTextWindow::GetContentRect(Rect* contents)
- {
- inherited::GetContentRect(contents);
-
- contents->top += 2;
- contents->left += 2;
- }
-
-
- void ZTextWindow::OpenFile(OSType fType)
- {
- FInfo fi;
- short refNum;
- long pSize;
- Handle temp = NULL;
-
- if (macFile.vRefNum != kNoFile)
- {
- FailOSErr(FSpGetFInfo(&macFile, &fi));
-
- if (fi.fdType != 'TEXT')
- FailOSErr(paramErr);
-
- FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
-
- try
- {
- FailOSErr(GetEOF(refNum, &pSize));
- if (pSize > kMaxTextSize)
- FailOSErr(kTextFileTooBigErr);
-
- FailNIL(temp = NewHandle(pSize));
-
- HLock(temp);
- FailOSErr(FSRead(refNum, &pSize, *temp));
-
- // set the text in text edit
-
- TESetText(*temp, pSize, itsText);
- HUnlock(temp);
-
- DisposeHandle(temp);
- FSClose(refNum);
- }
- catch(OSErr err)
- {
- FSClose(refNum);
-
- if (temp)
- {
- HUnlock(temp);
- DisposeHandle(temp);
- }
- throw err;
- }
- }
- inherited::OpenFile( fType );
-
- // set up the bounds, etc
-
- RecalText();
- Draw();
- }
-
-
-
- void ZTextWindow::SaveFile()
- {
- short refNum;
- long pSize;
- OSErr theErr;
- Handle text = NULL;
- char tState;
-
- if (macFile.vRefNum != kNoFile)
- {
- theErr = FSpOpenDF(&macFile, fsCurPerm, &refNum);
-
- if (theErr == fnfErr)
- {
- FailOSErr(FSpCreate(&macFile, gMacZoopSignature, 'TEXT', 0));
- FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum));
- }
- else
- FailOSErr(theErr);
-
- try
- {
- FailNIL(text = (*itsText)->hText);
-
- pSize = GetHandleSize(text);
-
- tState = HGetState(text);
- HLock(text);
- FailOSErr(FSWrite(refNum, &pSize, *text));
- HSetState(text, tState);
-
- FailOSErr(SetEOF(refNum, pSize));
- FSClose(refNum);
- }
- catch(OSErr err)
- {
- FSClose(refNum);
- if (text)
- HSetState(text, tState);
- throw err;
- }
- inherited::SaveFile();
- }
- }
-
-
- void ZTextWindow::RecalText()
- {
- Rect content,tBounds;
- short textHeight;
- short lineHeight;
-
- GetContentRect(&content);
- (*itsText)->viewRect = content;
-
- TECalText(itsText);
-
- textHeight = TEGetHeight(0, 32767 , itsText);
-
- GetBounds(&tBounds);
- tBounds.bottom = textHeight;
- SetBounds(&tBounds);
-
- (*itsText)->destRect = tBounds;
- lineHeight = TEGetHeight(1, 1, itsText);
- SetScrollAmount( emSpace, lineHeight);
- }
-