home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************\
- * Console driver based on
- * the sample application from Inside Macintosh (RoadMap p.15-17)
- *
- \*******************************************************************/
-
- #include <Desk.h>
- #include <Scrap.h>
- #include <Fonts.h>
- #include <Memory.h>
- #include <OSEvents.h>
- #include <ToolUtils.h>
- #include <SegLoad.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <AppleEvents.h>
-
- #define appleID 128
- #define fileID 129
- #define editID 130
-
- /* Edit menu command indices */
- #define undoCommand 1
- #define cutCommand 3
- #define copyCommand 4
- #define pasteCommand 5
- #define clearCommand 6
- #define selallCommand 7
-
- /* Menu indices */
- #define appleM 0
- #define fileM 1
- #define editM 2
-
- #define SBarWidth 15
-
- int DoCommand(long mResult);
-
- #include "crtlocal.h"
-
- typedef struct
- {
- long readptr,writeptr,complete_line;
- char buffer[8192];
- } tty_struct;
-
- static struct crt_win {
- WindowRecord crt_wRecord;
- int linesInFolder;
- ControlHandle vScroll;
- TEHandle crt_TEH;
- WindowPtr crt_myWindow;
- tty_struct *console;
- } crt_windows[10],*w;
-
- void __exit(int status)
- {
- closeall();
- sock_close_all();
- if (w && w->crt_myWindow)
- {
- TESetSelect(0, 32767, w->crt_TEH);
- ZeroScrap();
- TECopy(w->crt_TEH);
- }
- ExitToShell();
- }
-
- int ours(WindowPtr win)
- {
- if (win != NULL)
- {
- struct crt_win *tmp = crt_windows;
- while (tmp < &crt_windows[sizeof(crt_windows)/sizeof(*crt_windows)])
- {
- if (tmp->crt_myWindow == win)
- {
- w = tmp;
- return 1;
- }
- ++tmp;
- }
- }
- return 0;
- }
-
- int SetUpWindows(Str255 name, int winidx)
- {
- Rect viewRect;
- FontInfo myInfo;
- GrafPort myPort;
-
- w = &crt_windows[winidx];
- OpenPort(&myPort);
- SetPort(&myPort);
-
- TextFont(monaco);
- TextSize(9);
- GetFontInfo(&myInfo);
- ClosePort(&myPort);
-
- viewRect.left = 0;
- viewRect.top = 32;
- viewRect.right = viewRect.left+82*myInfo.widMax+SBarWidth;
- viewRect.bottom = viewRect.top+26*(myInfo.ascent+myInfo.descent+myInfo.leading)+SBarWidth;
- OffsetRect(&viewRect,
- (qd.screenBits.bounds.right-viewRect.right)/2+(winidx<<5),
- (qd.screenBits.bounds.bottom-viewRect.bottom)/2+(winidx<<5));
- w->crt_myWindow = NewWindow(&(w->crt_wRecord),&viewRect,name,1,0,(WindowPtr) -1L,1,0);
- SetPort(w->crt_myWindow);
- TextFont(monaco);
- TextSize(9);
-
- viewRect = qd.thePort->portRect;
- viewRect.left = viewRect.right-15;
- viewRect.right += 1;
- viewRect.bottom -= 14;
- viewRect.top -= 1;
- w->vScroll = NewControl( w->crt_myWindow, &viewRect, (unsigned char *)"\000", 1, 0, 0, 0, scrollBarProc, 0L);
-
- viewRect = qd.thePort->portRect;
- viewRect.right -= SBarWidth;
- viewRect.bottom -= SBarWidth;
- InsetRect(&viewRect, 4, 4);
-
- w->crt_TEH = TEStylNew(&viewRect, &viewRect);
- (**w->crt_TEH).viewRect = qd.thePort->portRect;
- (**w->crt_TEH).viewRect.right -= SBarWidth;
- (**w->crt_TEH).viewRect.bottom -= SBarWidth;
- InsetRect(&(**w->crt_TEH).viewRect, 4, 4);
-
- w->linesInFolder = ((**w->crt_TEH).viewRect.bottom-(**w->crt_TEH).viewRect.top)/(**w->crt_TEH).lineHeight;
- (**w->crt_TEH).viewRect.bottom = (**w->crt_TEH).viewRect.top + (**w->crt_TEH).lineHeight*w->linesInFolder;
- (**w->crt_TEH).destRect.right = (**w->crt_TEH).viewRect.right;
- TECalText(w->crt_TEH);
- }
-
- int AdjustText (void)
-
- {
- int oldScroll, newScroll, delta;
-
- oldScroll = (**w->crt_TEH).viewRect.top - (**w->crt_TEH).destRect.top;
- newScroll = GetCtlValue(w->vScroll) * (**w->crt_TEH).lineHeight;
- delta = oldScroll - newScroll;
- if (delta != 0)
- TEScroll(0, delta, w->crt_TEH);
- }
-
- #ifdef SCROLLPROC
-
- pascal void ScrollProc (ControlHandle theControl, int theCode)
- {
- int pageSize;
- int scrollAmt;
- int oldCtl;
-
- if (theCode == 0)
- return ;
-
- pageSize = ((**w->crt_TEH).viewRect.bottom-(**w->crt_TEH).viewRect.top) /
- (**w->crt_TEH).lineHeight - 1;
-
- switch (theCode) {
- case inUpButton:
- scrollAmt = -1;
- break;
- case inDownButton:
- scrollAmt = 1;
- break;
- case inPageUp:
- scrollAmt = -pageSize;
- break;
- case inPageDown:
- scrollAmt = pageSize;
- break;
- }
-
- oldCtl = GetCtlValue(theControl);
- SetCtlValue(theControl, oldCtl+scrollAmt);
-
- AdjustText();
-
- }
-
- #endif
-
- int DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
-
- {
- switch (windowPart) {
- case inGoAway:
- if (ours(whichWindow))
- if (TrackGoAway(whichWindow, myEvent->where))
- {
- HideWindow(whichWindow);
- TESetSelect(0, (**w->crt_TEH).teLength, w->crt_TEH);
- TEDelete(w->crt_TEH);
- {
- register int n;
-
- n = (**w->crt_TEH).nLines-w->linesInFolder;
-
- if ((**w->crt_TEH).teLength > 0 && (*((**w->crt_TEH).hText))[(**w->crt_TEH).teLength-1]=='\r')
- n++;
-
- SetCtlMax(w->vScroll, n > 0 ? n : 0);
- }
- }
- break;
-
- case inMenuBar:
- return(HandleMenu(MenuSelect(myEvent->where)));
-
- case inSysWindow:
- SystemClick(myEvent, whichWindow);
- break;
-
- case inDrag:
- if (ours(whichWindow))
- {
- Rect dragRect = { 0, 0, 1024, 1024 };
- DragWindow(whichWindow, myEvent->where, &dragRect);
- }
- break;
-
- case inGrow:
- if (ours(whichWindow))
- {
- Point p = myEvent->where;
- GrafPtr savePort;
- long theResult;
- Rect oldHorizBar;
- Rect r;
-
- GetPort(&savePort);
- SetPort(whichWindow);
-
- oldHorizBar = whichWindow->portRect;
- oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
-
- SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
- theResult = GrowWindow(whichWindow, p, &r);
- if (theResult == 0)
- return;
- SizeWindow( whichWindow, LoWord(theResult), HiWord(theResult), false);
-
- InvalRect(&whichWindow->portRect);
-
- (**w->crt_TEH).viewRect = whichWindow->portRect;
- (**w->crt_TEH).viewRect.right -= SBarWidth;
- (**w->crt_TEH).viewRect.bottom -= SBarWidth;
- InsetRect(&(**w->crt_TEH).viewRect, 4, 4);
-
- w->linesInFolder = ((**w->crt_TEH).viewRect.bottom-(**w->crt_TEH).viewRect.top)/(**w->crt_TEH).lineHeight;
- (**w->crt_TEH).viewRect.bottom = (**w->crt_TEH).viewRect.top + (**w->crt_TEH).lineHeight*w->linesInFolder;
- (**w->crt_TEH).destRect.right = (**w->crt_TEH).viewRect.right;
- TECalText(w->crt_TEH);
-
- EraseRect(&oldHorizBar);
-
- MoveControl(w->vScroll, whichWindow->portRect.right - SBarWidth, whichWindow->portRect.top-1);
- SizeControl(w->vScroll, SBarWidth+1, whichWindow->portRect.bottom - whichWindow->portRect.top-(SBarWidth-2));
- r = (**w->vScroll).contrlRect;
- ValidRect(&r);
-
-
- {
- register int n;
-
- n = (**w->crt_TEH).nLines-w->linesInFolder;
-
- if ((**w->crt_TEH).teLength > 0 && (*((**w->crt_TEH).hText))[(**w->crt_TEH).teLength-1]=='\r')
- n++;
-
- SetCtlMax(w->vScroll, n > 0 ? n : 0);
- }
-
- AdjustText();
-
- SetPort(savePort);
- }
- break;
-
- case inContent:
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else if (ours(whichWindow))
- {
- int cntlCode;
- ControlHandle theControl;
- int pageSize;
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(whichWindow);
-
- GlobalToLocal(&myEvent->where);
- if ((cntlCode = FindControl(myEvent->where, whichWindow, &theControl)) == 0) {
- if (PtInRect(myEvent->where, &(**w->crt_TEH).viewRect))
- TEClick(myEvent->where, (myEvent->modifiers & shiftKey)!=0, w->crt_TEH);
- } else if (cntlCode == inThumb) {
- TrackControl(theControl, myEvent->where, 0L);
- AdjustText();
- } else
- TrackControl(theControl, myEvent->where,
- #ifdef SCROLLPROC
- &ScrollProc);
- #else
- 0);
- #endif
- SetPort(savePort);
- }
- break;
- }
- }
-
-
- int ShowSelect (void)
-
- {
- register int topLine, bottomLine, theLine;
-
- register int n;
-
- n = (**w->crt_TEH).nLines-w->linesInFolder;
-
- if ((**w->crt_TEH).teLength > 0 && (*((**w->crt_TEH).hText))[(**w->crt_TEH).teLength-1]=='\r')
- n++;
-
- SetCtlMax(w->vScroll, n > 0 ? n : 0);
- AdjustText();
-
- topLine = GetCtlValue(w->vScroll);
- bottomLine = topLine + w->linesInFolder;
-
- if ((**w->crt_TEH).selStart < (**w->crt_TEH).lineStarts[topLine] ||
- (**w->crt_TEH).selStart >= (**w->crt_TEH).lineStarts[bottomLine]) {
- for (theLine = 0; (**w->crt_TEH).selStart >= (**w->crt_TEH).lineStarts[theLine]; theLine++)
- ;
- SetCtlValue(w->vScroll, theLine - w->linesInFolder / 2);
- AdjustText();
- }
- }
-
- 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, &(**w->crt_TEH).viewRect ) )
- SetCursor( *GetCursor(1));
- else SetCursor(&qd.arrow);
- SetPort(savePort);
- }
- }
-
- void test_inited(int minor)
- {
- #if 0
- if (!qd.thePort)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitCursor();
- SetUpMenus();
- }
- #endif
- w = &crt_windows[minor];
- if (!w->crt_myWindow)
- {
- Str255 name;
- if (minor)
- {
- ksprintf((char *)name+1, "Window %d", minor);
- *name = strlen(name+1);
- }
- else strcpy(name, "\pMessages");
- SetUpWindows(name, minor);
- }
- }
-
- int cwrite(int fd, const void *buf, int size)
- {
- char *ptr;
- int lastcr,tot,cnt = 0;
- Ptr newtext = NewPtr(size);
- BlockMove(buf, newtext, size);
- tot = size;
- while (tot--)
- {
- if (newtext[tot]=='\n') newtext[tot]='\r';
- }
- if (fd!=-1) test_inited(fd);
- TESetSelect(32767,32767,w->crt_TEH);
- TEInsert(newtext, size, w->crt_TEH);
- DisposPtr(newtext);
- tot = (*w->crt_TEH)->teLength;
- ptr = *((*w->crt_TEH)->hText);
- lastcr = tot;
- while (tot-- && (cnt < 24))
- {
- if (ptr[tot]=='\r')
- {
- cnt += (lastcr-tot)/80;
- lastcr = tot;
- cnt++;
- }
- }
- if (tot > 0)
- {
- TESetSelect(0, tot, w->crt_TEH);
- TEDelete(w->crt_TEH);
- }
- tot = (*w->crt_TEH)->teLength;
- ptr = *((*w->crt_TEH)->hText);
- lastcr = 0;
- for (cnt = 0; cnt < tot; cnt++)
- {
- if (ptr[cnt]=='\r') lastcr = cnt;
- if (ptr[cnt]=='\t')
- {
- int pos = (cnt-lastcr-1)&7;
- int ins = 8-pos;
- TESetSelect(cnt, cnt+1, w->crt_TEH);
- TEDelete(w->crt_TEH);
- TEInsert(" ", ins, w->crt_TEH);
- ptr = *((*w->crt_TEH)->hText);
- tot += ins-1;
- }
- }
- TESetSelect(32767,32767,w->crt_TEH);
- return size;
- }
-
- void checkevents(void)
- {
- EventRecord myEvent;
- WindowPtr whichWindow;
- short windowPart;
- Rect r;
- MaintainCursor();
- SystemTask();
- if (w) TEIdle(w->crt_TEH);
- if (GetNextEvent(everyEvent, &myEvent))
- {
- switch (myEvent.what)
- {
- case mouseDown:
- windowPart = FindWindow(myEvent.where, &whichWindow);
- DoMouseDown(windowPart, whichWindow, &myEvent);
- break;
- case keyDown:
- case autoKey:
- {
- unsigned char theChar;
- theChar = myEvent.message & charCodeMask;
- if ((myEvent.modifiers & cmdKey) != 0)
- HandleMenu(MenuKey(theChar));
- else if (theChar < ' ')
- {
- TEKey(theChar, w->crt_TEH);
- if ((theChar == 3) || (theChar == 28)) interrupt = theChar;
- if (theChar == 4) theChar = -1; /* EOF */
- if (theChar == '\r') theChar = '\n';
- if (theChar == '\b') theChar = 127;
- if ((theChar == -1) || (theChar == '\n') || (theChar == 127))
- {
- write_console_stream(theChar);
- }
- }
- else
- {
- write_console_stream(theChar);
- if (w) cwrite(-1, &theChar, 1);
- }
- break;
- }
- case activateEvt:
- if (ours((WindowPtr)myEvent.message))
- {
- if (myEvent.modifiers & activeFlag)
- {
- TEActivate(w->crt_TEH);
- ShowControl(w->vScroll);
- }
- else
- {
- TEDeactivate(w->crt_TEH);
- HideControl(w->vScroll);
- }
- }
- break;
-
- case updateEvt:
- {
- WindowPtr myWindow = (WindowPtr) myEvent.message;
- if (ours(myWindow))
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(myWindow);
-
- BeginUpdate(myWindow);
- EraseRect(&myWindow->portRect);
- DrawControls(myWindow);
- DrawGrowIcon(myWindow);
- TEUpdate(&myWindow->portRect, w->crt_TEH);
- EndUpdate(myWindow);
-
- SetPort(savePort);
- }
- break;
- }
- case kHighLevelEvent:
- AEProcessAppleEvent (&myEvent);
- break;
- } /* end of case myEvent.what */
- } /* if */
- }
-
- long read_console_stream(int fd, char *readbuffer, long size)
- {
- long actual = 0;
- tty_struct *buf;
- if (!crt_windows[fd].console)
- crt_windows[fd].console = (tty_struct *)xmalloc(sizeof(tty_struct));
- buf = crt_windows[fd].console;
- if (buf->complete_line)
- {
- checkevents();
- while ((actual < size) && (buf->writeptr != buf->readptr))
- {
- int ch = buf->buffer[buf->readptr];
- buf->readptr = (buf->readptr+1)&8191;
- switch(ch)
- {
- case EOF: return EOF; break;
- case '\177':
- if (actual >0) actual--; break;
- default:
- readbuffer[actual++] = ch;
- break;
- }
- }
- buf->complete_line = 0;
- return actual;
- }
- return 0;
- }
-
- int write_console_stream(int writechar)
- {
- long actual = 0;
- tty_struct *buf;
- if (!w->console) w->console = (tty_struct *)xmalloc(sizeof(tty_struct));
- buf = w->console;
- if (buf->writeptr != (buf->readptr-1)&8191)
- {
- buf->buffer[buf->writeptr] = writechar;
- buf->writeptr = (buf->writeptr+1)&8191;
- if (writechar == '\n') buf->complete_line = 1;
- ++actual;
- }
- return actual;
- }
-