home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / AppleScript / Development Tools / Sample Code / 7Edit 3.1 / Sources / SVEditMain.c < prev    next >
Encoding:
Text File  |  1995-11-20  |  15.2 KB  |  650 lines  |  [TEXT/CWIE]

  1. // SVEditMain.c
  2. //
  3. // 7Edit 3.1d1. Original version by Jon Lansdell and Nigel Humphreys.
  4. // 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1995, all rights reserved.
  6.  
  7. /*    
  8.     Changes for 3.1 :
  9.  
  10.     12-Oct-95 : CW : Added call to InitDragHandlers in DoSVEdit
  11.     12-Oct-95 : CW : Changed MaintainCursor to make cursor arrow if over selected text
  12.     1-Nov-95    : DS : Made Changes for GX Printing.
  13.     
  14. */
  15.  
  16. #include <Memory.h>
  17. #include <Quickdraw.h>
  18. #include <Types.h>
  19. #include <Menus.h>
  20. #include <Windows.h>
  21. #include <Dialogs.h>
  22. #include <Traps.h>
  23. #include <Packages.h>
  24. #include <PPCToolBox.h>
  25. #include <Editions.h>
  26. #include <Printing.h>
  27. #include <ToolUtils.h>
  28. #include <Desk.h>
  29. #include <Scrap.h>
  30. #include <OSEvents.h>
  31. #include <AppleEvents.h>
  32. #include <AEObjects.h>
  33. #include <Errors.h>
  34.  
  35. #include "SVEditMain.h"
  36.  
  37. #include "SVEditGlobals.h"
  38. #include "SVEditUtils.h"
  39. #include "SVAERecording.h"
  40. #include "SVAppleEvents.h"
  41. #include "SVEditWindow.h"
  42. #include "SVEditFile.h"
  43. #include "SVDrag.h"
  44. #include "SVEditGXPrinting.h"
  45.  
  46.  
  47. /*-----------------------------------------------------------------------*/
  48. /**----------                         Standard Main routines                  --------------**/
  49. /*-----------------------------------------------------------------------*/
  50.  
  51. #pragma segment Main
  52.  
  53.     pascal void MaintainCursor(void)
  54.         {
  55.             Point     pt;
  56.             WindowPtr wPtr;
  57.             GrafPtr   savePort;
  58.             DPtr      theDoc;
  59.  
  60.             wPtr = FrontWindow();
  61.             if (Ours(wPtr))
  62.                 {
  63.                     theDoc = DPtrFromWindowPtr(wPtr);
  64.                     GetPort(&savePort);
  65.                     SetPort(wPtr);
  66.                     GetMouse(&pt);
  67.                     if (theDoc->theText)
  68.                         if ( PtInRect ( pt, &(**(theDoc->theText)).viewRect ) &&
  69.                                                         !PointInWindowSelection ( pt, wPtr ) )
  70.                             SetCursor(&editCursor);
  71.                         else
  72.                             SetCursor(&qd.arrow);
  73.                     else
  74.                         SetCursor(&qd.arrow);
  75.  
  76.                     if (theDoc->theText)
  77.                         TEIdle(theDoc->theText);
  78.  
  79.                     SetPort(savePort);
  80.                 }
  81.         }
  82.  
  83. #pragma segment Main
  84.  
  85. pascal void MaintainMenus(void)
  86.     {
  87.         DPtr       theDoc;
  88.         WindowPtr  firstWindow;
  89.  
  90.         firstWindow = FrontWindow();
  91.         if (!Ours(firstWindow))
  92.             {
  93.                 EnableItem(myMenus[fileM], fmNew);
  94.                 EnableItem(myMenus[fileM], fmOpen);
  95.                 DisableItem(myMenus[fileM], fmClose);
  96.                 DisableItem(myMenus[fileM], fmSave);
  97.                 DisableItem(myMenus[fileM], fmSaveAs);
  98.                 DisableItem(myMenus[fileM], fmRevert);
  99.                 DisableItem(myMenus[fileM], fmPageSetUp);
  100.  
  101.                 if (gGXIsPresent)
  102.                 {
  103.                     DisableItem(myMenus[fileM], fmPrint);
  104.                     DisableItem(myMenus[fileM], fmPrintOne );
  105.                 }
  106.                 else
  107.                     DisableItem(myMenus[fileM], fmNoGXPrint);
  108.                                 
  109.                 if (firstWindow)
  110.                     {
  111.                         EnableItem(myMenus[editM], undoCommand);
  112.                         EnableItem(myMenus[editM], cutCommand);
  113.                         EnableItem(myMenus[editM], copyCommand);
  114.                         EnableItem(myMenus[editM], pasteCommand);
  115.                         EnableItem(myMenus[editM], clearCommand);
  116.                     }
  117.             }
  118.         else
  119.             {
  120.                 theDoc = DPtrFromWindowPtr(firstWindow);
  121.                 EnableItem(myMenus[editM], pasteCommand);
  122.                 EnableItem(myMenus[fileM], fmClose);
  123.                 EnableItem(myMenus[fileM], fmSaveAs);
  124.                 EnableItem(myMenus[fileM], fmPageSetUp);
  125.  
  126.                 if (gGXIsPresent)
  127.                 {
  128.                     EnableItem(myMenus[fileM], fmPrint);
  129.                     EnableItem(myMenus[fileM], fmPrintOne );
  130.                 }
  131.                 else
  132.                     EnableItem(myMenus[fileM], fmNoGXPrint);                
  133.  
  134.                 if (theDoc->dirty)
  135.                     EnableItem(myMenus[fileM], fmRevert);
  136.                 else
  137.                     DisableItem(myMenus[fileM], fmRevert);
  138.                     
  139.                 if ((theDoc->dirty) && (theDoc->everSaved))
  140.                     EnableItem(myMenus[fileM], fmSave);
  141.                 else
  142.                     DisableItem(myMenus[fileM], fmSave);
  143.  
  144.                 DisableItem(myMenus[editM], undoCommand);
  145.                 
  146.                 if (((**(theDoc->theText)).selEnd - (**(theDoc->theText)).selStart) < 0)
  147.                     {
  148.                         DisableItem(myMenus[editM], cutCommand);
  149.                         DisableItem(myMenus[editM], copyCommand);
  150.                         DisableItem(myMenus[editM], clearCommand);
  151.                     }
  152.                 else
  153.                     {
  154.                         EnableItem(myMenus[editM], cutCommand);
  155.                         EnableItem(myMenus[editM], copyCommand);
  156.                         EnableItem(myMenus[editM], clearCommand);
  157.                     }
  158.     }
  159.     
  160. }
  161.  
  162. #pragma segment Main
  163.  
  164. pascal void SetUpCursors(void)
  165.  
  166.     {
  167.         CursHandle  hCurs;
  168.  
  169.         hCurs = GetCursor(1);
  170.         editCursor = **hCurs;
  171.         hCurs = GetCursor(watchCursor);
  172.         waitCursor = **hCurs;
  173.     }
  174.  
  175. #pragma segment Main
  176.  
  177. pascal void SetUpMenus(void)
  178.     {
  179.         short i;
  180.         
  181.         myMenus[appleM] = GetMenu(appleID);
  182.         AddResMenu(myMenus[appleM], 'DRVR');
  183.         myMenus[fileM] = GetMenu(fileID);
  184.         myMenus[editM] = GetMenu(editID);
  185.         myMenus[fontM] = GetMenu(mfontID);
  186.         AddResMenu(myMenus[fontM], 'FONT');
  187.         myMenus[sizeM]  = GetMenu(sizeID);
  188.         myMenus[styleM] = GetMenu(styleID);
  189.  
  190.         for (i = appleM; i <= kLastMenu; i++)
  191.             InsertMenu(myMenus[i], 0);
  192.  
  193.         SetItemStyle(myMenus[styleM], cPlain, 0);
  194.         SetItemStyle(myMenus[styleM], cBold, bold);
  195.         SetItemStyle(myMenus[styleM], cItalic, italic);
  196.         SetItemStyle(myMenus[styleM], cUnderline, underline);
  197.         SetItemStyle(myMenus[styleM], cOutline, outline);
  198.         SetItemStyle(myMenus[styleM], cShadow, shadow);
  199.         SetItemStyle(myMenus[styleM], cCondense, condense);
  200.         SetItemStyle(myMenus[styleM], cExtend, extend);
  201.  
  202.         SetShortMenus(); /* Does a DrawMenuBar() */
  203.     }
  204.  
  205. pascal void DoFile(short theItem)
  206.     {        
  207.         short   alertResult;
  208.         DPtr    theDoc = nil;
  209.         FSSpec  theFSSpec;
  210.         OSErr   fileErr;
  211.         TPrint  thePSetup;
  212.         
  213.         theItem = ConvertMenuActualToGXMenu ( theItem );
  214.  
  215.         switch (theItem)
  216.         {
  217.             case fmNew : IssueAENewWindow();
  218.                    break;
  219.             case fmOpen: if (GetFile(&theFSSpec)==noErr)
  220.                                          fileErr = IssueAEOpenDoc(theFSSpec);
  221.                    break;
  222.             case fmClose:IssueCloseCommand(FrontWindow());
  223.                    break;
  224.             case fmSave:
  225.             case fmSaveAs:
  226.                                         theDoc = DPtrFromWindowPtr(FrontWindow());
  227.                     
  228.                                         if (theDoc->everSaved == false || theItem == fmSaveAs)
  229.                                         {
  230.                                             fileErr = GetFileNameToSaveAs(theDoc);
  231.                                             if (fileErr == userCanceledErr)
  232.                                                 break;
  233.                                             else if (fileErr != noErr)
  234.                                                 FileError((unsigned char *)"\perror saving ", theDoc->theFileName);
  235.                                             else
  236.                                                 fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
  237.                 
  238.                                             if (fileErr == noErr)
  239.                                             {
  240.                                                 SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
  241.                                                 theDoc->everSaved = true;
  242.                                             }
  243.                                         }
  244.                                         else
  245.                                             fileErr = IssueSaveCommand(theDoc->theWindow, nil);
  246.                                         break;
  247.  
  248.             case fmRevert:
  249.                                         alertResult = DoFileDialog ( kRevertDialog, FrontWindow ( ) );
  250.                                         if ( alertResult == kStdOkItemIndex )
  251.                                         {
  252.                                             if (IssueRevertCommand(theDoc->theWindow))
  253.                                                 FileError((unsigned char *)"\perror reverting ", theDoc->theFileName);
  254.                                         }
  255.                                         break;
  256.  
  257.             case fmPageSetUp:
  258.                         theDoc = DPtrFromWindowPtr(FrontWindow());
  259.                                         if (gGXIsPresent)
  260.                                             {
  261.                                                 if (DoGXPageSetup(theDoc))
  262.                                                     IssueGXPageSetupWindow(theDoc->theWindow, theDoc->documentJob);
  263.                                             }
  264.                                         else
  265.                                             if (DoPageSetup(theDoc))
  266.                                                 {
  267.                                                     thePSetup = **(theDoc->thePrintSetup);
  268.                                                      IssuePageSetupWindow(theDoc->theWindow, thePSetup);
  269.                                                  }
  270.                                         break;
  271.  
  272.             case fmPrint: IssuePrintWindow(FrontWindow(),kUsePrintDialog);
  273.                                       break;
  274.             case fmPrintOne: IssuePrintWindow(FrontWindow(),kNoPrintDialog);
  275.                                       break;
  276.  
  277.             case fmQuit : IssueQuitCommand();
  278.                     break;
  279.         }                 /*of switch*/
  280.     }
  281.  
  282. #pragma segment Main
  283.  
  284. pascal void DoCommand(long mResult)
  285.     {
  286.         short   theItem;
  287.         short   err;
  288.         long    result;
  289.         Str255  name;
  290.         DPtr    theDocument;
  291.  
  292.         theDocument = DPtrFromWindowPtr(FrontWindow());
  293.         
  294.         theItem = LoWord(mResult);
  295.  
  296.         switch (HiWord(mResult)){
  297.  
  298.             case appleID:
  299.                 if (theItem == aboutItem)
  300.                     {
  301.                         SetCursor(&qd.arrow);
  302.                         result = Alert(258, nil);
  303.                     }
  304.                 else
  305.                     {
  306.                         GetItem(myMenus[appleM], theItem, name);
  307.                         err = OpenDeskAcc(name);
  308.                         SetPort(FrontWindow());
  309.                     }
  310.         break;
  311.                 
  312.             case fileID: DoFile(theItem);
  313.                          break;
  314.  
  315.             case editID:
  316.                     if (SystemEdit(theItem - 1) == false)
  317.                     {
  318.                         switch (theItem){
  319.     
  320.                             case cutCommand   : IssueCutCommand(theDocument);
  321.                                break;
  322.                                                          
  323.                             case copyCommand  : IssueCopyCommand(theDocument);
  324.                                break;
  325.                                                          
  326.                             case pasteCommand : IssuePasteCommand(theDocument);
  327.                                break;
  328.                                                          
  329.                             case clearCommand : IssueClearCommand(theDocument);
  330.                                break;
  331.                                                          
  332.                             case selectAllCommand :
  333.                                                          if (theDocument)
  334.                                                              TESetSelect(0,
  335.                                                                                      (**(theDocument->theText)).teLength,
  336.                                                                                      theDocument->theText);
  337.                                break;
  338.                         }     /*of switch*/
  339.                         ShowSelect(theDocument);
  340.                     }
  341.           break;
  342.  
  343.             case mfontID: IssueFontCommand(theDocument, theItem);
  344.                             break;
  345.  
  346.             case sizeID: IssueSizeCommand(theDocument, theItem);
  347.                             break;
  348.  
  349.             case styleID: IssueStyleCommand(theDocument, theItem);
  350.                              break;
  351.  
  352.         }                 /*of switch*/
  353.  
  354.         HiliteMenu(0);
  355.  
  356.     }
  357.  
  358. #pragma segment Main
  359.  
  360.  
  361.     pascal void DoMouseDown(const EventRecord *myEvent)
  362.         {
  363.             WindowPtr whichWindow;
  364.             Point     p;
  365.             Rect      dragRect;
  366.             DPtr      theDoc;
  367.  
  368.             p = myEvent->where;
  369.             switch (FindWindow(p, &whichWindow)){
  370.  
  371.                 case inDesk: SysBeep(10);
  372.                              break;
  373.  
  374.                 case inGoAway:if (Ours(whichWindow))
  375.                                                 if (TrackGoAway(whichWindow, p))
  376.                                                     IssueCloseCommand(whichWindow);
  377.                                             break;
  378.                 case inMenuBar:
  379.                                             SetCursor(&qd.arrow);
  380.                                             theDoc = DPtrFromWindowPtr(FrontWindow());
  381.                                             if (theDoc)
  382.                                                 {
  383.                                                     SetFontMenu(theDoc);
  384.                                                 }
  385.                     
  386.                                             DoCommand(MenuSelect(p));
  387.                                             HiliteMenu(0);
  388.                                             break;
  389.  
  390.                 case inSysWindow: SystemClick(myEvent, whichWindow);
  391.                                   break;
  392.  
  393.                 case inDrag:
  394.                     
  395.                         dragRect = qd.screenBits.bounds;
  396.                         
  397.                         if (Ours(whichWindow))
  398.                             {
  399.                                 DragWindow(whichWindow, p, &dragRect);
  400.                                 /*
  401.                                     As rgnBBox may be passed by address
  402.                                 */
  403.                                 dragRect = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
  404.                                 /*
  405.                                     The windows already there, but still tell
  406.                                     the our AppleEvents core about the move in case
  407.                                     they want to do anything
  408.                                 */
  409.                                 IssueMoveWindow(whichWindow, dragRect);
  410.                             }
  411.                       break;
  412.  
  413.                 case inGrow:SetCursor(&qd.arrow);
  414.                                         if (Ours(whichWindow))
  415.                                             MyGrowWindow(whichWindow, p);
  416.                                         break;
  417.                                     
  418.                 case inZoomIn : DoZoom(whichWindow, inZoomIn, p);
  419.                                               break;
  420.  
  421.                 case inZoomOut: DoZoom(whichWindow, inZoomOut, p);
  422.                         break;
  423.                 case inContent: if (whichWindow != FrontWindow())
  424.                                                     SelectWindow(whichWindow);
  425.                                                 else
  426.                                                     if (Ours(whichWindow))
  427.                                                         DoContent(whichWindow, *myEvent);
  428.                         break;
  429.             }                 /*of switch*/
  430.         }
  431.  
  432. #pragma segment Main
  433.  
  434. pascal long GetSleep(void)
  435.     {
  436.         long      sleep;
  437.         WindowPtr theWindow;
  438.         DPtr      theDoc;
  439.  
  440.         sleep = 0x7fffffff;
  441.         if (!gInBackground)
  442.             {
  443.                 theWindow = FrontWindow();
  444.                 if (theWindow)
  445.                     if (Ours(theWindow))
  446.                         {
  447.                             theDoc = DPtrFromWindowPtr(theWindow);
  448.                             if ((**(theDoc->theText)).selStart == (**(theDoc->theText)).selEnd)
  449.                                 sleep = GetCaretTime();
  450.                         }
  451.             }
  452.         return(sleep);
  453.     }                     /*GetSleep*/
  454.  
  455. #pragma segment Main
  456.  
  457. void HandleOneEvent( EventRecord *myEvent )
  458.     {
  459.         DPtr        theDoc;
  460.         char        theChar;
  461.         WindowPtr   theWindow;
  462.         Boolean     activate;
  463.         
  464.         switch (myEvent->what) {
  465.             case mouseDown: FlushAndRecordTypingBuffer();
  466.                                             DoMouseDown(myEvent);
  467.                             break;
  468.             case keyDown:
  469.             case autoKey:
  470.                         theDoc = DPtrFromWindowPtr(FrontWindow());
  471.  
  472.                         theChar = myEvent->message & charCodeMask;
  473.  
  474.                         if ((myEvent->modifiers & cmdKey) == cmdKey)
  475.                             {
  476.                                 DoCommand(MenuKey(theChar));
  477.                                 HiliteMenu(0);
  478.                             }
  479.                         else
  480.                             if (theDoc->theText)
  481.                                 {
  482.                                     AddKeyToTypingBuffer(theDoc, theChar);
  483.                                     
  484.                                     TEKey(theChar, theDoc->theText);
  485.                                     AdjustScrollbars(theDoc, false);
  486.                     
  487.                                     ShowSelect(theDoc);
  488.                                     
  489.                                     theDoc->dirty = true;
  490.                                         }
  491.                       break;
  492.                 
  493.             case activateEvt:
  494.                     activate = ((myEvent->modifiers & activeFlag) != 0);
  495.                     theWindow = (WindowPtr)myEvent->message;
  496.                     DoActivate(theWindow, activate);
  497.                   break;
  498.  
  499.             case updateEvt:
  500.                     DoUpdate ( (WindowPtr) myEvent->message );
  501.                   break;
  502.  
  503.             case kHighLevelEvent: FlushAndRecordTypingBuffer();
  504.                                                         DoAppleEvent(*myEvent);
  505.                             break;
  506.             case kOSEvent:
  507.                 
  508.                 switch (myEvent->message & osEvtMessageMask) { /*high byte of message*/
  509.                     case 0x01000000:
  510.                         {
  511.                             gInBackground = ((myEvent->message & resumeFlag) == 0);
  512.                             DoActivate(FrontWindow(), !gInBackground);
  513.                         }
  514.                 }
  515.             } /*of switch*/
  516.     }
  517.     
  518. #pragma segment Main
  519.  
  520. pascal void MainEvent(void)
  521.     {
  522.         EventRecord myEvent;
  523.  
  524.         MaintainCursor(); /* TEIdle in here for now */
  525.         MaintainMenus();
  526.  
  527.         if (WaitNextEvent(everyEvent, &myEvent, GetSleep(), nil))
  528.             HandleOneEvent( &myEvent );
  529.         }
  530.  
  531. #pragma segment Main
  532.  
  533. OSErr GXPrintingEventOverride(EventRecord *anEvent, Boolean filterEvent)
  534. {
  535.     /* Handle events in whatever way is appropriate. MyDoEvent is
  536.         our generic event handler. We don't pass it events that it
  537.         shouldn't handle while printing dialogs are displayed. */
  538.  
  539.     if (!filterEvent)
  540.         switch (anEvent->what) {
  541.             case mouseDown:
  542.             case keyDown:
  543.             case autoKey:
  544.                 break;
  545.             default:
  546.                 HandleOneEvent( anEvent );
  547.         }
  548.     return noErr;
  549. }
  550.  
  551. #pragma segment Main
  552.  
  553. pascal void DoSVEdit(void)
  554.     {
  555.         OSErr  err;
  556.         short  result;
  557.  
  558.         InitGraf(&qd.thePort);
  559.         InitFonts();
  560.         FlushEvents(everyEvent, 0);
  561.         InitWindows();
  562.         InitMenus();
  563.         TEInit();
  564.         InitDialogs(nil);
  565.         InitCursor();
  566.  
  567.         MaxApplZone();
  568.         SetUpCursors();
  569.  
  570.         SetUpMenus();
  571.  
  572.         gWCount    = 0;
  573.         gNewDocCount = 0;
  574.         gQuitting  = false;
  575.         gFontMItem = 0;
  576.  
  577.         gGestaltAvailable          = false;
  578.         gAppleEventsImplemented    = false;
  579.         gAliasManagerImplemented   = false;
  580.         gEditionManagerImplemented = false;
  581.         gOutlineFontsImplemented   = false;
  582.         gGXIsPresent               = false;
  583.  
  584.         /*check environment checks to see if we are running 7.0*/
  585.         if (!CheckEnvironment())
  586.             {
  587.                 SetCursor(&qd.arrow);
  588.                 /*pose the only 7.0 alert*/
  589.                 result = Alert(302, nil);
  590.                 return;
  591.             }
  592.  
  593. /* Init GX Printing */
  594.         InitGXIfPresent();
  595.  
  596.         err = InitEditionPack();
  597.  
  598.         if (err)
  599.             {
  600.                 ShowError((unsigned char *)"\pInitEditionPack", err);
  601.                 gQuitting = true;
  602.             }
  603.  
  604.         err = AEObjectInit();
  605.         if (err)
  606.             {
  607.                 ShowError((unsigned char *)"\pAEObjectInit", err);
  608.                 gQuitting = true;
  609.             }
  610.  
  611.         InitAppleEvents();
  612.  
  613.         err = PPCInit();
  614.         if (err)
  615.             {
  616.                 ShowError((unsigned char *)"\pPPCInit", err);
  617.                 gQuitting = true;
  618.             }
  619.             
  620.             
  621.         err = InitDragHandlers ( );
  622.         if (err)
  623.             {
  624.                 ShowError((unsigned char *)"\pInitDragHandlers", err);
  625.                 gQuitting = true;
  626.             }
  627.         
  628.             
  629.         /* create the UPPs for the control tracking routines */
  630.         /* globals, since we don't want to have to create and dispose of the routine */
  631.         /* descriptors each time we call DoContent */
  632.         gHScrollActionUPP = NewControlActionProc ( HActionProc );
  633.         gVScrollActionUPP = NewControlActionProc ( VActionProc );
  634.         
  635.         /* create a UPP for drawing a dialog's default button. Again, make it a global */
  636.         /* for convenience */
  637.         gDefaultButtonUPP = NewUserItemProc(DrawDefaultOutline);
  638.  
  639.  
  640.         while (!gQuitting)
  641.             MainEvent();
  642.         
  643.         CleanUpGXIfPresent ();
  644.     }
  645.     
  646. void main ()
  647.     {
  648.         /*the main routine starts here*/
  649.         DoSVEdit();
  650.     }