home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 28 / develop issue 28 code / sketch / source / main / sketch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-23  |  10.3 KB  |  581 lines

  1. /****************************************************************************
  2.  * 
  3.  * Sketch.c
  4.  * 
  5.  ****************************************************************************/
  6.  
  7. #include "Assertion.h"
  8.  
  9. #include "Structs.h"
  10. #include "Sketch.h"
  11.  
  12. #include "AppleEvent.h"
  13. #include "DocumentADT.h"
  14. #include "OSASupport.h"
  15.  
  16. #include "Windows.h"
  17. #include "Imaging.h"
  18.  
  19. // ---------------------------------------------------------------
  20. // constants
  21.  
  22. #define    kDiskInitTop            0x0050                // disk init dialog coordinates
  23. #define    kDiskInitLeft            0x0070
  24.  
  25. #define    kNILL_POINTER            0L
  26. #define    kREMOVE_ALL_EVENTS    0
  27.  
  28. // ---------------------------------------------------------------
  29. // Prototypes
  30.  
  31. void                     main                    (void);
  32.  
  33. static void         Initialize            (void);
  34. static void         CleanUp                (void);
  35.  
  36. // Event processing
  37.  
  38. static void         EventLoop            (void);
  39. static void         ProcessEvent        (EventRecord *event);
  40. static void         ProcessIdleEvent    (EventRecord *event);
  41.  
  42. // event handler
  43.  
  44. static void        HandleMouseDown    (EventRecord *event);
  45. static void            HandleOSEvent        (EventRecord *event);
  46. static void            DoActivate            (WindowPtr window, Boolean becomingActive);
  47. static void         DoMenuCommand        (long menuResult);
  48. static void         DoUpdate                (WindowPtr window);
  49.  
  50. // menus
  51.  
  52. static void        HandleAppleChoice    (short item);
  53. static void        HandleFileChoice    (short item);
  54. static void         AdjustMenus            (void);
  55.  
  56. // windows
  57.  
  58. static Boolean        DoCloseWindow        (WindowPtr window);
  59.  
  60. // utilities
  61.  
  62. static Boolean     HasSystem7            (void);
  63.  
  64. // content
  65.  
  66. static void         DrawWindowContents    (WindowPtr window);
  67. static void         DoContentClick            (WindowPtr window, Point where);
  68.  
  69. // ---------------------------------------------------------------
  70.  
  71. static Boolean     gQuitting;
  72. static Boolean     gInBackground;
  73. static RgnHandle    gCursorRgn;
  74.  
  75. // ---------------------------------------------------------------
  76.  
  77. void main(void)
  78. {
  79.     Initialize();
  80.     EventLoop();
  81.     ZeroScrap();
  82.     TEToScrap();
  83. }
  84.  
  85. // ---------------------------------------------------------------
  86.  
  87. #define NUM_MASTER_POINTER_BLOCKS    8
  88.  
  89. static void
  90. Initialize(void)
  91. {
  92.     short     i;
  93.     Handle    menuBar;
  94.     
  95.     MaxApplZone();
  96.     for (i=1; i<= NUM_MASTER_POINTER_BLOCKS; i++) {
  97.         MoreMasters();
  98.     }
  99.     
  100.     FlushEvents(everyEvent, kREMOVE_ALL_EVENTS);
  101.  
  102.     InitGraf(&qd.thePort);
  103.     InitFonts();
  104.     InitWindows();
  105.     InitMenus();
  106.     TEInit();
  107.     InitDialogs(kNILL_POINTER);
  108.     InitCursor();
  109.     
  110.     gInBackground     = false;
  111.     gQuitting        = false;
  112.     gCursorRgn        =    NewRgn();
  113.     
  114.     TEFromScrap();
  115.     
  116.     menuBar = GetNewMBar(rMenuBar);
  117.     
  118.     if (menuBar) 
  119.     {
  120.         SetMenuBar(menuBar);
  121.         DisposHandle(menuBar);
  122.         AddResMenu(GetMHandle(mApple), 'DRVR');
  123.         DrawMenuBar();
  124.     }
  125.     else 
  126.     {
  127.         gQuitting         = true;
  128.         return;
  129.     }
  130.     
  131.     if (HasSystem7() == false) 
  132.     {
  133.         Alert(rNotSystem7, kNILL_POINTER);
  134.         gQuitting = true;
  135.         return;
  136.     }
  137.     
  138.     InitEditionPack();
  139.     InstallAEHandlers();
  140.     
  141.     // Initialize the ADTs
  142.     
  143.     CreateDocumentList();
  144.     
  145.     // Intialize OSA, so we can load and run compiled scripts
  146.     
  147.     InitializeOSASupport();
  148.         
  149. }
  150.  
  151. // ---------------------------------------------------------------
  152.  
  153. static void
  154. EventLoop(void)
  155. {
  156.     Boolean            gotEvent = false;
  157.     EventRecord        event;
  158.     long                sleepTime;
  159.  
  160.     while (gQuitting == false)
  161.     {
  162.         if (gInBackground) 
  163.         {
  164.             sleepTime = -1L;
  165.         }
  166.         else 
  167.         {
  168.             sleepTime = GetDblTime();
  169.         }
  170.         
  171.         gotEvent = WaitNextEvent(everyEvent, &event, sleepTime, gCursorRgn);
  172.         
  173.         if (gotEvent) 
  174.         {
  175.             ProcessEvent(&event);
  176.         }
  177.         else 
  178.         {
  179.             ProcessIdleEvent(&event);
  180.         }        
  181.     }
  182. }
  183.  
  184. // ---------------------------------------------------------------
  185.  
  186. static void
  187. ProcessEvent(EventRecord *event)
  188. {
  189.     short            error;
  190.     char            key;
  191.     Point            mountPoint;
  192.  
  193.     switch (event->what) 
  194.     {    
  195.         case activateEvt:
  196.             DoActivate((WindowPtr)event->message, (event->modifiers & activeFlag) != 0);
  197.             InitCursor();
  198.             break;
  199.         
  200.         case diskEvt:
  201.             InitCursor();
  202.             if ((event->message >> 16) != noErr) 
  203.             {
  204.                 mountPoint.h = kDiskInitLeft;
  205.                 mountPoint.v = kDiskInitTop;
  206.                 error = DIBadMount(mountPoint, event->message);
  207.             }
  208.             break;
  209.  
  210.         case keyDown:
  211.         case autoKey:
  212.              key = event->message & charCodeMask;
  213.              if (( event->modifiers & cmdKey ) != 0) 
  214.              {
  215.                   AdjustMenus();
  216.                   DoMenuCommand(MenuKey(key));
  217.              }
  218.              break;
  219.       
  220.         case mouseDown:
  221.              HandleMouseDown(event);
  222.              break;
  223.              
  224.         case osEvt:
  225.             HandleOSEvent(event);
  226.             break;
  227.  
  228.         case updateEvt:
  229.             DoUpdate((WindowPtr)event->message);
  230.             break;
  231.             
  232.         case kHighLevelEvent:
  233.             AEProcessAppleEvent(event);
  234.             break;
  235.             
  236.         default:
  237.             ProcessIdleEvent(event);
  238.             break;
  239.     
  240.     }
  241.     
  242. }
  243.  
  244. // ---------------------------------------------------------------
  245.  
  246. static void
  247. ProcessIdleEvent(EventRecord *event)
  248. {
  249. #pragma unused (event)
  250.  
  251.     // do idle event activities here
  252. }
  253.  
  254. // ---------------------------------------------------------------
  255.  
  256. static void
  257. HandleMouseDown(EventRecord *event)
  258. {
  259.     WindowPtr    window;
  260.     short         windowPart;
  261.     
  262.     windowPart = FindWindow(event->where, &window);
  263.     
  264.     switch(windowPart) 
  265.     {
  266.         case inMenuBar:
  267.             AdjustMenus();
  268.             DoMenuCommand(MenuSelect(event->where));
  269.             break;
  270.  
  271. #if 0                     
  272.         case inSysWindow:
  273.              SystemClick(&gTheEvent, whichWindow);
  274.              break;
  275. #endif
  276.                      
  277.         case inDrag:
  278.              DragWindow(window, event->where, &qd.screenBits.bounds);
  279.              break;        
  280.  
  281.         case inGrow:
  282.              break;        
  283.  
  284.         case inContent:
  285.             if (window != FrontWindow()) 
  286.             {
  287.                 SelectWindow(window);
  288.             }
  289.             else {
  290.                 DoContentClick(window, event->where);
  291.             }
  292.             break;
  293.             
  294.         case inGoAway:
  295.             if (TrackGoAway(window, event->where)) 
  296.             {
  297.                 DoCloseWindow(window);
  298.             }
  299.             break;
  300.  
  301.         case inZoomIn:
  302.         case inZoomOut:
  303.             if (TrackBox(window, event->where, windowPart)) 
  304.             {
  305.               SetPort(window);
  306.               EraseRect(&window->portRect);
  307.               ZoomWindow(window, windowPart, true);
  308.               InvalRect(&window->portRect);
  309.             }
  310.             break;
  311.     }
  312. }
  313.  
  314. // ---------------------------------------------------------------
  315.  
  316. static void
  317. HandleOSEvent(EventRecord *event)
  318. {
  319.     switch ((event->message >> 24) & 0xFF) 
  320.     {
  321.         case suspendResumeMessage:
  322.             if ((event->message & resumeFlag) == 0) 
  323.             {  
  324.                 gInBackground = true;                                                // suspend event
  325.                 ZeroScrap();
  326.                 TEToScrap();
  327.                 DoActivate(FrontWindow(), false);                                // deactive our front window
  328.             }
  329.             else 
  330.             {                                                                        
  331.                 gInBackground = false;                                                // resume event
  332.                 if (event->message & convertClipboardFlag) 
  333.                 {
  334.                     TEFromScrap();
  335.                 }
  336.                 DoActivate(FrontWindow(), true);                                    // active our front window
  337.             }
  338.             break;
  339.             
  340.         case mouseMovedMessage:
  341.             DisposeRgn(gCursorRgn);
  342.             gCursorRgn = NewRgn();
  343.             SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
  344.             break;
  345.             
  346.         case kHighLevelEvent:
  347.             AEProcessAppleEvent(event);
  348.             break;
  349.             
  350.     }
  351. }
  352.  
  353. // ---------------------------------------------------------------
  354.  
  355. static void
  356. DoMenuCommand(long menuChoice)
  357. {
  358.     short                            menuID;
  359.     short                            menuItem;
  360.     
  361.     menuID   = HiWord(menuChoice);
  362.     menuItem = LoWord(menuChoice);
  363.     
  364.     switch(menuID) 
  365.     {
  366.         case mApple:
  367.             HandleAppleChoice(menuItem);
  368.             break;
  369.         
  370.         case mFile:
  371.             HandleFileChoice(menuItem);
  372.             break;
  373.     }
  374.         
  375.     HiliteMenu(0);
  376. }
  377.  
  378. // ---------------------------------------------------------------
  379.  
  380. static void
  381. HandleAppleChoice(short item)
  382. {
  383.     Str255        accName;
  384.     short        accNumber;
  385.     
  386.     switch (item)
  387.     {
  388.         case iAbout:
  389.             Alert(rAboutBox, kNILL_POINTER);
  390.             break;
  391.         
  392.         default:
  393.             GetItem(GetMHandle(mApple), item, accName);
  394.             accNumber = OpenDeskAcc(accName);
  395.             break;
  396.     }
  397. }
  398.  
  399. // ---------------------------------------------------------------
  400.  
  401. static void
  402. HandleFileChoice(short item)
  403. {
  404.     switch (item) 
  405.     {    
  406.         case iQuit:
  407.             DoQuit((DescType)0L);
  408.             break;
  409.             
  410.         default:
  411.             break;
  412.     }
  413. }
  414.  
  415. // ---------------------------------------------------------------
  416. // Also called from the RequiredSuite
  417.  
  418. OSErr
  419. DoQuit(DescType saveOptions)
  420. {
  421. #pragma unused (saveOptions)
  422.  
  423.     OSErr error = noErr;
  424.     
  425.     CleanUp();
  426.     gQuitting = true;
  427.     
  428.     return error;
  429. }
  430.  
  431. #pragma mark -
  432.  
  433. // ---------------------------------------------------------------
  434.  
  435. static Boolean
  436. DoCloseWindow(WindowPtr window) 
  437. {
  438.     DocumentReference document;
  439.     
  440.     if (window != NULL) 
  441.     {
  442.         SetPort(window);
  443.         document = (DocumentReference)GetWRefCon(window);
  444.         
  445.         if (document != nil) 
  446.             DestroyDocument(document);
  447.     }
  448.     return true;
  449. }
  450.  
  451. // ---------------------------------------------------------------
  452.  
  453. static void
  454. CleanUp(void)
  455. {
  456.     WindowPtr window;
  457.     Boolean   closed = true;
  458.     
  459.     do 
  460.     {
  461.         window = FrontWindow();
  462.         if (window) 
  463.             closed = DoCloseWindow(window);
  464.     } while (closed && window);
  465.     
  466.     gQuitting = closed;
  467. }
  468.  
  469. // ---------------------------------------------------------------
  470.  
  471. static void
  472. DoActivate(WindowPtr window, Boolean becomingActive)
  473. {
  474. #pragma unused (window)
  475.  
  476.     if (becomingActive) 
  477.     {
  478.         // do activate stuff
  479.         // •••
  480.     }
  481.     else 
  482.     {
  483.         // do deactivate stuff
  484.         // •••
  485.     }
  486. }
  487.  
  488. // ---------------------------------------------------------------
  489.  
  490. static void
  491. DoUpdate(WindowPtr window)
  492. {
  493.     SetPort(window);
  494.     BeginUpdate(window);
  495.     
  496.     if (!EmptyRgn(window->visRgn)) 
  497.     {
  498.         DrawWindowContents(window);
  499.     }
  500.     
  501.     EndUpdate(window);
  502. }
  503.  
  504. // ---------------------------------------------------------------
  505.  
  506. static void
  507. DrawWindowContents(WindowPtr window)
  508. {
  509.     DocumentReference document;
  510.     
  511.     document = (DocumentReference)GetWRefCon(window);
  512.     
  513.     IMDraw(document);
  514. }
  515.  
  516. // ---------------------------------------------------------------
  517.  
  518. static void
  519. AdjustMenus (void)
  520. {
  521.     WindowPtr     window;
  522.     MenuHandle    fileMenu;
  523.     
  524.     window     = FrontWindow();    
  525.     fileMenu = GetMHandle(mFile);
  526.     
  527.     EnableItem(fileMenu, iQuit);
  528. }
  529.  
  530. // ---------------------------------------------------------------
  531.  
  532. static void
  533. DoContentClick(WindowPtr window, Point where)
  534. {
  535. #pragma unused (window, where)
  536.  
  537.     InitCursor();
  538.     SysBeep(2);
  539. }
  540.  
  541. // ---------------------------------------------------------------
  542.  
  543. static Boolean HasSystem7(void)
  544. {
  545.     long        response;
  546.     OSErr        error;
  547.     
  548.     error = Gestalt(gestaltSystemVersion, &response);
  549.     if (error == noErr)
  550.         if ((response & 0x0000FFFF) < 0x0700)
  551.             error = -1;
  552.         
  553.     return (error == noErr);
  554. }
  555.  
  556. // ---------------------------------------------------------------
  557. // Determine if the current process is in the background.
  558. // ---------------------------------------------------------------
  559.  
  560. Boolean IsCurrentProcessInBackground(void)
  561. {
  562.     Boolean                    inBackground = true;
  563.  
  564.     ProcessSerialNumber    currentPSN;
  565.     ProcessSerialNumber    frontPSN;
  566.     Boolean                    isSameProcess;
  567.  
  568.     if (GetFrontProcess(&frontPSN) == noErr)
  569.         if (GetCurrentProcess(¤tPSN) == noErr)
  570.             if (SameProcess(&frontPSN, ¤tPSN, &isSameProcess) == noErr)
  571.                 if (isSameProcess)
  572.                     inBackground = false;
  573.  
  574.     return inBackground;
  575. }
  576.  
  577. // ---------------------------------------------------------------
  578.  
  579.  
  580.  
  581.