home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
-
- Buggy MiniEdit.c
-
- The sample application from Inside Macintosh (RoadMap p.15-17)
- beefed up a bit by Stephen Z. Stein, Symantec Corp.
- Use this file with the “MiniEdit” chapter of your manual.
-
- The resources used in this program are in the file MiniEdit.π.rsrc.
- The file was created with ResEdit, so there is no RMaker source
- for it.
-
- In order for THINK C to find the resource file for this
- project, be sure you’ve named the project MiniEdit.π
-
- *********************************************************************/
-
- /*
-
- #include <QuickDraw.h>
- #include <MacTypes.h>
- #include <FontMgr.h>
- #include <WindowMgr.h>
- #include <MenuMgr.h>
- #include <TextEdit.h>
- #include <DialogMgr.h>
- #include <EventMgr.h>
- #include <DeskMgr.h>
- #include <FileMgr.h>
- #include <ToolboxUtil.h>
- #include <ControlMgr.h>
-
- */
-
- #include "MiniEdit.h"
-
- #include "logo.h"
- #include "globals.h"
-
- WindowRecord wRecord;
- WindowPtr myWindow;
- TEHandle TEH;
- int linesInFolder;
- Rect dragRect = { 0, 0, 1024, 1024 };
- ControlHandle vScroll;
- Cursor editCursor;
- Cursor waitCursor;
- BOOLEAN load_p;
-
- BOOLEAN mac_edit()
- {
- CursHandle hCurs;
- MenuHandle menu_handle;
- WindowPtr cWindow;
- int count;
- FILE *holdstrm;
-
- holdstrm = fopen("temp.txt", "a"); /* need to guarantee file exists on mac */
- if (holdstrm != NULL) fclose(holdstrm);
- else { err_logo(FILE_ERROR,
- make_static_strnode("Could not create editor file"));
- return(0); }
-
- cWindow = FrontWindow();
- HideWindow(cWindow);
-
- menu_handle = GetMHandle(2);
- DisableItem(menu_handle, 1);
- menu_handle = GetMHandle(3);
- for (count = 8; count <= 14; count++)
- EnableItem(menu_handle, count);
- DisableItem(menu_handle, 10);
- DisableItem(menu_handle, 13);
-
- hCurs = GetCursor(1);
- editCursor = **hCurs;
- hCurs = GetCursor(watchCursor);
- waitCursor = **hCurs;
-
- SetUpFile();
- load_p = TRUE;
- while (MainEvent()) ;
- for (count = 8; count <= 14; count++)
- DisableItem(menu_handle, count);
- menu_handle = GetMHandle(2);
- EnableItem(menu_handle, 1);
- CloseMyWindow();
- ShowWindow(cWindow);
- return(load_p);
- }
-
- int MainEvent()
- {
- EventRecord myEvent;
- WindowPtr whichWindow;
- Rect r;
-
- MaintainCursor();
- SystemTask();
- TEIdle(TEH);
- if (GetNextEvent(everyEvent, &myEvent)) {
- switch (myEvent.what) {
- case mouseDown:
- switch (FindWindow( myEvent.where, &whichWindow )) {
- case inDesk:
- /* SysBeep(10); */
- break;
- case inGoAway:
- if (ours(whichWindow))
- if (TrackGoAway( myWindow, myEvent.where) )
- switch (Alert(SaveChangesP, 0L)) {
- case choseYes: SaveFile();
- return(0);
- break;
- case choseNo: if (Alert(AreYouSureP, 0L) == 2) { /* 2 = sure */
- load_p = 0;
- return(0);
- }
- else
- return(1);
- }
- break;
- case inMenuBar:
- return( DoCommand( MenuSelect(myEvent.where) ) );
- case inSysWindow:
- SystemClick( &myEvent, whichWindow );
- break;
- case inDrag:
- if (ours(whichWindow))
- DragWindow( whichWindow, myEvent.where, &dragRect );
- break;
- case inGrow:
- if (ours(whichWindow))
- MyGrowWindow( whichWindow, myEvent.where );
- break;
- case inContent:
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else
- if (ours(whichWindow))
- DoContent(whichWindow, &myEvent);
- break;
- default: ;
- } /* end switch FindWindow */
- break;
- case keyDown:
- case autoKey:
- {
- register char theChar;
-
- theChar = myEvent.message & charCodeMask;
- if ((myEvent.modifiers & cmdKey) != 0)
- return( DoCommand( MenuKey( theChar ) ));
- else {
- if ( (theChar != DELETE_KEY && (**TEH).teLength > 30000 ) ||
- ((unsigned char)theChar < 0x1c && theChar != DELETE_KEY && theChar != TAB_KEY && theChar != RETURN_KEY) ||
- ((unsigned char)theChar > 0xd8))
- SysBeep(1);
- else
- TEKey( theChar, TEH );
- ShowSelect();
- }
- }
- break;
- case activateEvt:
- if (ours((WindowPtr)myEvent.message)) {
- r=(*myWindow).portRect;
- r.top = r.bottom - (SBarWidth+1);
- r.left = r.left - (SBarWidth+1);
- InvalRect(&r);
- if ( myEvent.modifiers & activeFlag ) {
- TEActivate( TEH );
- ShowControl( vScroll );
- DisableItem( GetMHandle(3), undoCommand );
- TEFromScrap();
- }
- else {
- TEDeactivate(TEH);
- HideControl( vScroll );
- ZeroScrap();
- TEToScrap();
- }
- }
- break;
- case updateEvt:
- if (ours((WindowPtr)myEvent.message)) UpdateWindow(myWindow);
- if ((WindowPtr)myEvent.message == graphics_window) {
- SetPort(graphics_window);
- BeginUpdate(graphics_window);
- redraw_graphics();
- EndUpdate(graphics_window);
- }
- break;
- default: ;
- } /* end of case myEvent.what */
- }
- return(1);
- }
-
- int DoCommand( mResult )
- long mResult;
- {
- int theItem, temp;
- Str255 name;
- WindowPeek wPtr;
- DialogPtr myDialog;
-
- theItem = LoWord( mResult );
- switch (HiWord(mResult)) {
- case appleM:
- if (theItem == 1) {
- myDialog = GetNewDialog(100,NULL,(WindowPtr)(-1L));
- ModalDialog(NULL,&theItem);
- DisposDialog(myDialog);
- }
- else {
- GetItem(GetMHandle(1), theItem, &name);
- OpenDeskAcc( &name );
- SetPort( myWindow );
- }
- break;
- case fileM:
- Alert(CantQuit, 0L);
- break;
- case editM:
- if (SystemEdit(theItem-1)==0) {
- wPtr = (WindowPeek) FrontWindow();
- switch (theItem) {
- case cutCommand:
- TECut( TEH );
- ZeroScrap();
- TEToScrap();
- break;
- case copyCommand:
- TECopy( TEH );
- ZeroScrap();
- TEToScrap();
- break;
- case pasteCommand:
- if ((**TEH).teLength > 30000)
- SysBeep(1);
- else {
- TEFromScrap();
- TEPaste( TEH );
- }
- break;
- case clearCommand:
- TEDelete( TEH );
- break;
- case acceptCommand:
- SaveFile();
- HiliteMenu(0);
- return(0);
- case cancelCommand:
- HiliteMenu(0);
- if (Alert(AreYouSureP, 0L) == 2) { /* 2 = sure */
- load_p = 0;
- return(0);
- }
- else
- return(1);
- case psuCommand:
- DoPageSetUp();
- break;
- case printCommand:
- PrintText((**TEH).hText, (long)(**TEH).teLength, (GrafPtr)myWindow,
- StringWidth("\pmmmm"), 0L);
- break;
- }
- ShowSelect();
- }
- break;
- }
- HiliteMenu(0);
- return(1);
- }
-
- MaintainCursor()
- {
- Point pt;
- WindowPeek wPtr;
- GrafPtr savePort;
-
- if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
- GetPort( &savePort );
- SetPort( (GrafPtr)wPtr );
- GetMouse(&pt);
- if ( PtInRect(pt, &(**TEH).viewRect ) )
- SetCursor( &editCursor);
- else InitCursor(); /* SetCursor( &arrow ); */
- SetPort( savePort );
- }
- }
-
- ours(w)
- WindowPtr w;
- {
- return( (myWindow!=NULL) && (w==myWindow) );
- }
-