home *** CD-ROM | disk | FTP | other *** search
- 18-Jun-88 15:13:21-MDT,18034;000000000000
- Return-Path: <u-lchoqu%sunset@cs.utah.edu>
- Received: from cs.utah.edu by SIMTEL20.ARPA with TCP; Sat, 18 Jun 88 15:12:51 MDT
- Received: by cs.utah.edu (5.54/utah-2.0-cs)
- id AA22096; Sat, 18 Jun 88 14:25:13 MDT
- Received: by sunset.utah.edu (5.54/utah-2.0-leaf)
- id AA24506; Sat, 18 Jun 88 14:25:10 MDT
- Date: Sat, 18 Jun 88 14:25:10 MDT
- From: u-lchoqu%sunset@cs.utah.edu (Lee Choquette)
- Message-Id: <8806182025.AA24506@sunset.utah.edu>
- To: rthum@simtel20.arpa
- Subject: BMUG5.c
-
- #include "MacCDefs.h"
- #include "Window.h"
- #include "Events.h"
- #include "TextEdit.h"
- #include "Font.h"
- #include "Menu.h"
-
-
- #define FALSE 0
- #define NIL 0
- #define TRUE 1
- #define Desk_ID 200
- #define Edit_ID 201
- #define Window_ID 202
- #define TE_ID 203
- #define Font_ID 204
- #define Style_ID 205
- #define iBeamCursor 1
-
- #define teNormal 0x0000
- #define teBold 0x0100
- #define teItalic 0x0200
- #define teUnderline 0x0400
- #define teOutline 0x0800
- #define teShadow 0x1000
- #define teCondense 0x2000
- #define teExtend 0x4000
-
- // Global Variables
-
- WindowPtr theWindow,windPtrA,windPtrB;
- WindowRecord windRecordA,windRecordB;
- TEHandle theText;
- Rect screenRect = {0, 0, 384, 512};
- Rect windowRectA,windowRectB;
- MenuHandle deskMenu,editMenu,windowMenu,teMenu,fontMenu,styleMenu;
- char *titleA = "\10Window A";
- char *titleB = "\10Window B";
- short doUpdates;
- CursHandle iBeam;
-
- // External or Non-Integer functions
-
- WindowPtr openWindow();
- TEHandle openTE();
- extern struct P_Str *CtoPstr();
-
- int strlen(str) char *str;
- {int i=0; while (str[i++]); return i-1;}
-
- reStartProc()
- {ExitToShell();}
-
-
- // Initialization Routine
-
- Init() /* Init() */
- {
- InitDialogs(reStartProc);
- TEInit();
- putUpMenus();
- iBeam = GetCursor(iBeamCursor);
-
- SetRect(&windowRectA ,5, 50, 500, 290);
- SetRect(&windowRectB ,5, 60, 500, 300);
-
- theWindow = NIL;
- theText = NIL;
- doUpdates = TRUE;
-
- windPtrB = openWindow(&windRecordB,&windowRectB,titleB);
- windPtrA = openWindow(&windRecordA,&windowRectA,titleA);
- }
-
-
- // Main
-
- main() /* main() */
- {
- char c;
- short windowcode;
-
- EventRecord theEvent;
- WindowPtr mouseWindow;
- WindowPtr tempWindow;
-
-
- Init();
-
- InitCursor();
- FlushEvents(-1);
- SelectWindow(windPtrA); /* Generate an activate event for window A */
- SetPort(windPtrA);
-
- while (TRUE) {
-
- SystemTask();
- if(theText) {
- TEIdle(theText);
- ChangeMouse();
- }
-
-
- if (GetNextEvent(everyEvent, &theEvent)) {
- switch ( theEvent.what ) {
- case autoKey:
- case keyDown:
- c = theEvent.message & charCodeMask;
- if ((theEvent.modifiers & cmdKey)) {
- doMenuItem(MenuKey(c));
- } else {
- if(theText) TEKey(c, theText);
- }
- break;
-
-
- case mouseDown:
- windowcode = FindWindow(&theEvent.where, &mouseWindow);
-
- /* mouseDown occurs in active window */
- if ((FrontWindow() == mouseWindow) && (mouseWindow != NIL)) {
- activeWindowEvt(&theEvent,mouseWindow,windowcode);
- break;
- }
-
- /* mouseDown occurs in inactive window */
- if ((FrontWindow() != mouseWindow) && (mouseWindow != NIL)) {
- /* Activate window if not Front window -UNLESS cmdKey & inDrag */
- if( !((theEvent.modifiers & cmdKey) && (windowcode == inDrag)) ) {
- SelectWindow(mouseWindow);
- break;
- }
- inactiveWindowEvt(&theEvent,mouseWindow,windowcode);
- break;
- }
-
- /* mouseDown does not occur in a window */
- if (mouseWindow == NIL) {
- notaWindowEvt(&theEvent,mouseWindow,windowcode);
- break;
- }
-
- case updateEvt:
- if(doUpdates) doUpdateEvt(&theEvent);
- break;
-
- case activateEvt:
- doActivateEvt(&theEvent);
- break;
-
- }
- }
- }
- }
-
-
- // mouseDown event handler #1 - Active window /* activeWindowEvt() */
-
- activeWindowEvt(theEvent,mouseWindow,windowcode)
- EventRecord *theEvent;
- WindowPtr mouseWindow;
- short windowcode;
- {
- long growResult;
-
- SetPort(mouseWindow);
- switch ( windowcode ) {
- case inContent:
- GlobalToLocal(&theEvent->where);
- TEClick(&theEvent->where,(theEvent->modifiers & shiftKey)? TRUE :FALSE, theText);
- break;
-
- case inDrag:
- DragWindow(mouseWindow, &theEvent->where, &screenRect);
- break;
-
- case inGrow:
- growResult = GrowWindow(mouseWindow, &theEvent->where, &screenRect);
- SizeWindow(mouseWindow, LoWord(growResult), HiWord(growResult), TRUE);
- EraseRect(&mouseWindow->portRect);
- InvalRect(&mouseWindow->portRect);
- SizeTE(mouseWindow);
- DrawGrowIcon(mouseWindow);
- break;
-
- case inGoAway:
- if (TrackGoAway(theWindow, &theEvent->where)) {
- TEDispose(theText);
- theText = NIL;
- if(theWindow == windPtrA) windPtrA = NIL;
- if(theWindow == windPtrB) windPtrB = NIL;
- DisposeWindow(theWindow);
- theWindow = NIL;
- SetMenus(theText,FALSE);
- }
- break;
-
- case inSysWindow:
- SystemClick(theEvent,mouseWindow);
- break;
- }
- }
-
-
- // mouseDown event handler #2 - inactive window /* inactiveWindowEvt() */
-
- inactiveWindowEvt(theEvent,mouseWindow,windowcode)
- EventRecord *theEvent;
- WindowPtr mouseWindow;
- short windowcode;
- {
- switch ( windowcode ) {
- case inDrag:
- DragWindow(mouseWindow, &theEvent->where, &screenRect);
- break;
- case inSysWindow:
- SystemClick(theEvent,mouseWindow);
- break;
- }
- }
-
-
- // mouseDown event handler #3 - no window /* notaWindowEvt() */
-
- notaWindowEvt(theEvent,mouseWindow,windowcode)
- EventRecord *theEvent;
- WindowPtr mouseWindow;
- short windowcode;
- {
- switch ( windowcode ) {
- case inMenuBar:
- doMenuItem(MenuSelect(&theEvent->where));
- break;
- case inDesk:
- SysBeep(1);
- break;
- }
- }
-
-
- // Update event handler /* doUpdateEvt() */
-
- doUpdateEvt(theEvent)
- EventRecord *theEvent;
- {
- WindowPtr tempWindow,oldPort;
- TEHandle tempText;
-
- GetPort(&oldPort);
- SetPort(tempWindow = (WindowPtr)theEvent->message);
- BeginUpdate(tempWindow);
- tempText = (TEHandle)GetWRefCon(tempWindow);
- TEUpdate(&tempWindow->portRect, tempText);
- DrawGrowIcon(tempWindow);
- EndUpdate(tempWindow);
- SetPort(oldPort);
- return;
- }
-
-
- // Activate event handler /* doActivateEvt() */
-
- doActivateEvt(theEvent)
- EventRecord *theEvent;
- {
- WindowPtr tempWindow;
- TEHandle tempText;
-
- SetPort(tempWindow = (WindowPtr)theEvent->message);
- tempText = (TEHandle)GetWRefCon(tempWindow);
- if ((theEvent->modifiers & activeFlag)) {
- theWindow = tempWindow;
- TEActivate(theText = tempText);
- SetMenus(theText,TRUE);
- } else {
- TEDeactivate(tempText);
- theText = NIL;
- SetMenus(theText,FALSE);
- }
- DrawGrowIcon(tempWindow);
- return;
- }
-
-
- // Put up a new Window
-
- WindowPtr openWindow(wRec,rect,title) /* openWindow() */
- WindowRecord *wRec;
- Rect *rect;
- char *title;
- {
- WindowPtr tempWindow;
- TEHandle tempText;
-
- tempWindow = NewWindow(wRec,rect, title, TRUE, 0, -1, TRUE, 0);
- SetPort(tempWindow);
- TextFont(newYork);
- TextFace(boldStyle);
- TextSize(12);
- tempText = openTE(tempWindow);
- SetWRefCon(tempWindow, tempText);
- TEUpdate(&(*tempText)->viewRect, tempText);
- (*tempText)->crOnly = 1;
- return tempWindow;
- }
-
-
- // Get a New a TEHandle
-
- TEHandle openTE(tempWindow)
- WindowPtr tempWindow;
- {
- Rect destRect, viewRect;
-
- DupPortRect(tempWindow, &viewRect);
- viewRect.right -= 16; /* Make room for scroll bar */
- viewRect.bottom -= 16; /* Make room for scroll bar */
- DupPortRect(tempWindow, &destRect);
- destRect.right -= 16; /* Make room for scroll bar */
- destRect.bottom -= 16; /* Make room for scroll bar */
- destRect.left += 4; /* indent a bit */
- return TENew(&destRect, &viewRect);
- }
-
-
-
- // ReSize Suffix Rects
-
- sizeTE(tempWindow) /* sizeTE() */
- WindowPtr tempWindow;
- {
- Rect rect;
- TEHandle tempText;
-
- tempText = (TEHandle)GetWRefCon(tempWindow);
- DupPortRect(tempWindow, &rect);
- rect.right -= 16; /* Make room for scroll bar */
- rect.bottom -= 16; /* Make room for scroll bar */
- BlockMove(&rect, &(*tempText)->viewRect, sizeof(Rect));
- rect.left += 4; /* indent a bit */
- BlockMove(&rect, &(*tempText)->destRect, sizeof(Rect));
- TECalText(tempText);
- }
-
-
- // Make a new Rect based on size of tempWindow's portRect
-
- DupPortRect(tempWindow, rect) /* DupPortRect() */
- WindowPtr tempWindow;
- Rect *rect;
- {
- BlockMove(&tempWindow->portRect, rect, sizeof(Rect));
- }
-
-
- // Puts up Menus
-
- putUpMenus()
- {
- InitMenus();
-
- /* Desk Accessory menu */
- deskMenu = NewMenu(Desk_ID,CtoPstr("\024"));
- AppendMenu(deskMenu,CtoPstr("About BMUG5...;(-"));
- AddResMenu(deskMenu, 'DRVR');
- InsertMenu(deskMenu, 0);
-
- /* Edit menu */
- editMenu = NewMenu(Edit_ID, CtoPstr("Edit"));
- AppendMenu(editMenu,CtoPstr("(Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear/Z;(-;Select All"));
- InsertMenu(editMenu, 0);
-
- /* "Windows" menu */
- windowMenu = NewMenu(Window_ID, CtoPstr("Windows"));
- AppendMenu(windowMenu,CtoPstr("Window A/A;Window B/B;Do Updates;(-;Item 5/5;Quit/."));
- doUpdates = TRUE;
- CheckItem(windowMenu,3,doUpdates);
- InsertMenu(windowMenu, 0);
-
- /* "TE" menu */
- teMenu = NewMenu(TE_ID, CtoPstr("TEStuff"));
- AppendMenu(teMenu,CtoPstr("Left/L;Center/M;Right/R;(-;WordWrap"));
- InsertMenu(teMenu, 0);
-
- /* "Font" menu */
- fontMenu = NewMenu(Font_ID, CtoPstr("Font"));
- AppendMenu(fontMenu,CtoPstr("Chicago;New York;Geneva;Monaco"));
- AppendMenu(fontMenu,CtoPstr("(-;9 point;10 point;12 point;14 point"));
- InsertMenu(fontMenu, 0);
-
- /* "Style" menu */
- styleMenu = NewMenu(Style_ID, CtoPstr("Style"));
- AppendMenu(styleMenu,CtoPstr("Normal;Bold;Italic;Underline;Outlined;Shadow;Condense;Extend"));
- InsertMenu(styleMenu, 0);
-
- DrawMenuBar();
- }
-
-
- // Do what the menu says...
-
- doMenuItem(menuresult)
- long menuresult;
- {
- struct P_Str accessoryName;
- short menuID, itemNumber;
-
- menuID = HiWord(menuresult);
- itemNumber = menuresult;
-
- switch ( menuID ) {
- case Window_ID:
- switch ( itemNumber ) {
- case 1: /* item 1 is Window A*/
- if(windPtrA == NIL) {
- windPtrA = openWindow(&windRecordA,&windowRectA,titleA);
- }
- SelectWindow(windPtrA);
- break;
-
- case 2: /* item 2 is Window B*/
- if(windPtrB == NIL) {
- windPtrB = openWindow(&windRecordB,&windowRectB,titleB);
- }
- SelectWindow(windPtrB);
- break;
-
- case 3: /* item 3 is do updates */
- doUpdates = ((doUpdates) ? FALSE : TRUE);
- CheckItem(windowMenu,itemNumber,doUpdates);
- break;
-
- case 5: /* item 5 */
- break;
-
- case 6: /* item 6 is quit */
- ExitToShell();
- break;
- }
- break;
-
- case Desk_ID:
- if(itemNumber == 1) { /* item 1 is about window */
- AboutWindow();
- } else {
- GetItem(deskMenu, itemNumber, &accessoryName);
- OpenDeskAcc(&accessoryName);
- }
- break;
-
- case TE_ID:
- if(!theText) break;
- switch(itemNumber) {
- case 1: /* item 1 is Left Justification */
- TESetJust(teJustLeft,theText);
- break;
- case 2: /* item 2 is Center Justification */
- TESetJust(teJustCenter,theText);
- break;
- case 3: /* item 3 is Right Justification */
- TESetJust(teJustRight,theText);
- break;
- case 5: /* item 5 is WordWrap */
- (*theText)->crOnly *= -1;
- EraseRect(&(*theText)->viewRect);
- break;
- }
- TECalText(theText);
- TEUpdate(&(*theText)->viewRect, theText);
- SetMenus(theText,TRUE);
- break;
-
- case Edit_ID:
- if( SystemEdit(itemNumber-1) ) break;
- if(!theText) break;
- switch(itemNumber) {
- case 3: /* item 3 is Cut */
- TECut(theText);
- break;
- case 4: /* item 4 is Copy */
- TECopy(theText);
- break;
- case 5: /* item 5 is Paste */
- TEPaste(theText);
- break;
- case 6: /* item 6 is Clear */
- TEDelete(theText);
- break;
- case 8: /* item 8 is Select All */
- TESetSelect(0,65000,theText);
- break;
- }
- break;
-
- case Font_ID:
- if(!theText) break;
- switch(itemNumber) {
- case 1: /* case 1 is Chicago */
- (*theText)->txFont = sysFont;
- break;
- case 2: /* case 2 is New York */
- (*theText)->txFont = newYork;
- break;
- case 3: /* case 3 is Geneva */
- (*theText)->txFont = geneva;
- break;
- case 4: /* case 4 is Monaco */
- (*theText)->txFont = monaco;
- break;
- case 6: /* case 6 is 9 point */
- (*theText)->txSize = 9;
- break;
- case 7: /* case 7 is 10 point */
- (*theText)->txSize = 10;
- break;
- case 8: /* case 8 is 12 point */
- (*theText)->txSize = 12;
- break;
- case 9: /* case 9 is 14 point */
- (*theText)->txSize = 14;
- break;
- }
- EraseRect(&(*theText)->viewRect);
- TECalText(theText);
- TEUpdate(&(*theText)->viewRect, theText);
- SetMenus(theText,TRUE);
- break;
-
- case Style_ID:
- if(!theText) break;
- switch(itemNumber) {
- case 1: /* case 1 is Normal */
- (*theText)->txFace = teNormal;
- break;
- case 2: /* case 2 is Bold */
- (*theText)->txFace ^= teBold;
- break;
- case 3: /* case 3 is Italic */
- (*theText)->txFace ^= teItalic;
- break;
- case 4: /* case 4 is Underline */
- (*theText)->txFace ^= teUnderline;
- break;
- case 5: /* case 5 is Outline */
- (*theText)->txFace ^= teOutline;
- break;
- case 6: /* case 6 is Shadow */
- (*theText)->txFace ^= teShadow;
- break;
- case 7: /* case 7 is Condense */
- (*theText)->txFace ^= teCondense;
- break;
- case 8: /* case 8 is Extend */
- (*theText)->txFace ^= teExtend;
- break;
- }
- EraseRect(&(*theText)->viewRect);
- TECalText(theText);
- TEUpdate(&(*theText)->viewRect, theText);
- SetMenus(theText,TRUE);
- break;
-
- }
- HiliteMenu(0);
- }
-
-
- // Set up Menus for current TextWindow
-
- SetMenus(tempText,flag) /* SetMenus() */
- TEHandle tempText;
- short flag;
- {
- int i;
-
- for(i=1;i<6;i++) {
- CheckItem(teMenu,i,FALSE);
- }
- for(i=1;i<10;i++) {
- CheckItem(fontMenu,i,FALSE);
- }
- for(i=1;i<9;i++) {
- CheckItem(styleMenu,i,FALSE);
- }
- if(!flag) return;
-
- switch((*theText)->just) {
- case teJustLeft:
- CheckItem(teMenu,1,TRUE);
- break;
- case teJustCenter:
- CheckItem(teMenu,2,TRUE);
- break;
- case teJustRight:
- CheckItem(teMenu,3,TRUE);
- break;
- }
- if( (*theText)->crOnly > -1 ) CheckItem(teMenu,5,TRUE);
- switch((*theText)->txFont) {
- case sysFont:
- CheckItem(fontMenu,1,TRUE);
- break;
- case newYork:
- CheckItem(fontMenu,2,TRUE);
- break;
- case geneva:
- CheckItem(fontMenu,3,TRUE);
- break;
- case monaco:
- CheckItem(fontMenu,4,TRUE);
- break;
- }
- switch((*theText)->txSize) {
- case 9:
- CheckItem(fontMenu,6,TRUE);
- break;
- case 10:
- CheckItem(fontMenu,7,TRUE);
- break;
- case 12:
- CheckItem(fontMenu,8,TRUE);
- break;
- case 14:
- CheckItem(fontMenu,9,TRUE);
- break;
- }
- if( (*theText)->txFace == teNormal ) CheckItem(styleMenu,1,TRUE);
- if( (*theText)->txFace & teBold) CheckItem(styleMenu,2,TRUE);
- if( (*theText)->txFace & teItalic) CheckItem(styleMenu,3,TRUE);
- if( (*theText)->txFace & teUnderline) CheckItem(styleMenu,4,TRUE);
- if( (*theText)->txFace & teOutline) CheckItem(styleMenu,5,TRUE);
- if( (*theText)->txFace & teShadow) CheckItem(styleMenu,6,TRUE);
- if( (*theText)->txFace & teCondense) CheckItem(styleMenu,7,TRUE);
- if( (*theText)->txFace & teExtend) CheckItem(styleMenu,8,TRUE);
- }
-
-
- // Put up About Window
-
- AboutWindow()
- {
- Rect creditR;
- Rect lineR;
- GrafPtr port;
- WindowPtr creditW;
- EventRecord anEvent;
- long dummy;
- char *line1 = "BMUG Developers Group - April 17,1985";
- char *line2 = "Sample Program #5";
- char *line3 = "written by Dave Burnard";
- char *line4 = "developed using Consulair Mac C(tm)";
-
- GetPort(&port); // must preserve port
- SetRect(&lineR,5,5,345,20);
- SetRect(&creditR,75,120,425,220);
- creditW = NewWindow((WindowPeek) NIL,&creditR,"\1x",TRUE,dBoxProc,(WindowPtr) -1,TRUE,0);
- SetPort(creditW);
-
- TextSize(12);
- TextFont(0);
- TextBox(line1,strlen(line1),&lineR,teJustCenter);
- OffsetRect(&lineR,0,20);
- TextBox(line2,strlen(line2),&lineR,teJustCenter);
- OffsetRect(&lineR,0,20);
- TextBox(line3,strlen(line3),&lineR,teJustCenter);
- OffsetRect(&lineR,0,20);
- TextBox(line4,strlen(line4),&lineR,teJustCenter);
-
- do {
- GetNextEvent(everyEvent, &anEvent);
- } while (anEvent.what != mouseDown);
-
- DisposeWindow(creditW);
-
- SetPort(port);
-
- return;
- }
-
-
- // Change Mouse if over active Text region
-
- ChangeMouse()
- {
- Point mousePt;
- if( ((WindowPeek) FrontWindow() )->windowKind == userKind) {
- GetMouse(&mousePt);
- if( PtInRect(&mousePt,&(*theText)->viewRect) ) {
- SetCursor(*iBeam);
- } else {
- SetCursor(&QD->arrow);
- }
- } else {
- SetCursor(&QD->arrow);
- }
-
- }
-