home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Biomorph 0.77 / Biomorph src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-29  |  10.9 KB  |  497 lines  |  [TEXT/ALFA]

  1. #include <MacHeaders>
  2. #include "constants.h"
  3. #include "globals.h"
  4. #include "morphutils.h"
  5. #include "redraw.h"
  6.  
  7.  
  8. // ---------------  Function Protos  (alphabetized)
  9. static void AdjustCursor(Point where, RgnHandle cursorRgn);
  10.        void Cleanup(void);
  11. static void DoAbout(void);
  12. static void DoActivate(EventRecord *theEvent);
  13. static void DoCommand(long mResult);
  14. static void DoDialogEvent(EventRecord *theEvent);
  15. static void DoEvent(EventRecord *theEvent);
  16. static void DoKeyDown(EventRecord *theEvent);
  17. static void DoIdle(EventRecord *theEvent, RgnHandle cursorRgn);
  18. static void DoInfo(void);
  19. static void DoMouseDown(EventRecord *theEvent);
  20. static void DoUpdate(EventRecord *theEvent);
  21. static void Init(void);
  22. static void SetupMenus(void);
  23. static void SetupWindows(void);
  24.        void main(void);
  25.  
  26. // ---------------  Functions
  27.  
  28. static void AdjustCursor(Point where, RgnHandle cursorRgn)
  29. {
  30.     // if in image window, change cursor to + cursor.
  31.     // else return to normal pointer.
  32.     GrafPtr savedPort;
  33.     Point pt;
  34.  
  35. #ifdef DEBUG
  36.     DebugStr("\pAdjustCursor");
  37. #endif
  38.     GetPort(&savedPort);
  39.     SetPort(gMainWindow);
  40.     pt = where;
  41.     GlobalToLocal(&pt);  // convert to main window coords
  42.     if ( (gCurrentWindow == gMainWindow) &&
  43.           PtInRect( pt, &gMainWindow->portRect))
  44.     {    if (gCrossCursor != NULL)
  45.             SetCursor( *gCrossCursor);  // dereference handle to cursor *
  46.                                         // OK to do, doesn't move heap
  47.     }
  48.     else
  49.         InitCursor();    // restore to arrow
  50.     
  51.     SetPort(savedPort);
  52.     
  53. } // AdjustCursor()
  54.  
  55.  
  56. void Cleanup(void)
  57. {
  58. //  Call any cleanup routines we need to to free any storage we alloc'd.
  59. //
  60.     CleanupOffscreen();        // get rid of offscreen bitmap -- offscreen.c
  61. } // Cleanup()
  62.  
  63.  
  64. static void DoAbout(void)
  65. {
  66.     short itemHit = -9;
  67.     DialogPtr aboutDlog;
  68.     
  69.     if ((aboutDlog = GetNewDialog(kAboutDLOG, NULL, (void*)(-1))) != NULL)
  70.     {
  71.         ShowWindow((WindowPtr)aboutDlog);
  72.         SelectWindow((WindowPtr)aboutDlog);
  73.         UserItem( aboutDlog, kAboutOKOutline, OutlineButton);
  74.         while (itemHit != kAboutOKButt)
  75.         {
  76.             ModalDialog( NULL, &itemHit);
  77.             if (itemHit == kAboutInfoButt)
  78.                 DoInfo();    // display the more info dialog...
  79.         }
  80.         DisposDialog(aboutDlog);
  81.     } else
  82.         SysBeep(5);       /* beep if we can't get dialog box */
  83. } // DoAbout() 
  84.  
  85.  
  86. static void DoActivate(EventRecord *theEvent)
  87. {
  88. #ifdef DEBUG
  89.     DebugStr("\pDoActivate");
  90. #endif
  91.     if (gControlDialog == (WindowPtr)theEvent->message)
  92.         DoDialogEvent(theEvent);
  93. } // DoActivate()
  94.  
  95.  
  96. static void DoCommand(long mResult)
  97. {
  98.     int theItem = LoWord(mResult);
  99.     int theMenu = HiWord(mResult);
  100.     Str255 name;
  101.     int temp;
  102.     
  103. #ifdef DEBUG
  104.     DebugStr("\pDoCommand");
  105. #endif
  106.     switch (theMenu) {
  107.         case kAppleM:
  108.             if (theItem == kAppleAboutItem) {
  109.                 DoAbout();
  110.             } else {
  111.                 GetItem( gAppleM, theItem, name);
  112.                 temp = OpenDeskAcc(name);
  113.             }
  114.             break;
  115.             
  116.         case kFileM:
  117.             switch (theItem) {
  118.                 case kFileNewItem:
  119.                     break;
  120.                 case kFileOpenItem:
  121.                     break;
  122.                 case kFileSaveAsItem:
  123.                     SavePict();        // savepict.c
  124.                     break;
  125.                 case kFileQuitItem:
  126.                     Cleanup();
  127.                     break;
  128.             } /* switch theItem */
  129.             break;
  130.             
  131.         case kEditM:
  132.             break;
  133.         
  134.         case kTypeM:
  135.             switch(theItem)
  136.             {
  137.             case kTypeAddItem:
  138.                 MorphAddRes();
  139.                 break;
  140.             case kTypeDeleteItem:
  141.                 MorphDelRes();
  142.                 break;
  143.             
  144.             case kTypeDefaultItem:
  145.                 MorphClearProc();
  146.                 gMorphProcH = gDefaultProcH;     // use the builtin
  147.                 CheckItem( gTypeM, gTypeChkItem, FALSE);  // uncheck prev
  148.                 CheckItem( gTypeM, kTypeDefaultItem, TRUE);
  149.                 gTypeChkItem = kTypeDefaultItem;
  150.                 break;
  151.  
  152.             default:  // whichever is left...
  153.                 MorphClearProc();
  154.                 CheckItem( gTypeM, gTypeChkItem, FALSE); // uncheck it
  155.                 
  156.                 GetItem( gTypeM, theItem, name);
  157.                 gMorphProcH =
  158.                         (MorphProcH)GetNamedResource( kMorphECR, name);
  159.                 
  160.                 if (gMorphProcH != NULL)   // got it
  161.                 {
  162.                     HLock((Handle)gMorphProcH);
  163.                     gTypeChkItem = theItem;
  164.                 }
  165.                 else   // didn't get it so use the default.
  166.                 {
  167.                     Error( kMorphCodeErr, 0, 0, 0, noteIcon);
  168.                     gMorphProcH = gDefaultProcH;
  169.                     gTypeChkItem = kTypeDefaultItem;
  170.                 }
  171.                 CheckItem( gTypeM, gTypeChkItem, TRUE);
  172.                 break;
  173.             } // kTypeM switch
  174.             break;
  175.             
  176.     } /* switch theMenu */
  177.     HiliteMenu(0);        /* unhilite the selected menu */
  178. } // DoCommand()
  179.  
  180.  
  181. static void DoDialogEvent(EventRecord *theEvent)
  182. {
  183.     DialogPtr whichDialog;
  184.     Boolean btemp;
  185.     int itemHit;
  186.     
  187. #ifdef DEBUG
  188.     DebugStr("\pDoDialogEvent");
  189. #endif
  190.     if (IsDialogEvent(theEvent))
  191.     {
  192.         if (DialogSelect(theEvent, &whichDialog, &itemHit))
  193.             switch(itemHit) {
  194.             case kZoomInB:
  195.                 ZoomIn();    // redraw.c
  196.                 break;
  197.                 
  198.             case kZoomOutB:
  199.                 ZoomOut();    // redraw.c
  200.                 break;
  201.                 
  202.             case kRedrawB:
  203.                 Redraw();    // redraw.c
  204.                 break;
  205.                 
  206.             case kSelectB:
  207.                 // blow up the selection
  208.                 break;
  209.             } // switch
  210.     } // if
  211.  
  212. } // DoDialogEvent()
  213.  
  214.  
  215. static void DoEvent(EventRecord *theEvent)
  216. {
  217.     switch( theEvent->what)
  218.     {
  219.         case nullEvent:
  220.             DoDialogEvent(theEvent);
  221.             break;
  222.         case mouseDown:
  223.             DoMouseDown(theEvent);
  224.             break;
  225.         case keyDown:
  226.         case autoKey:
  227.             DoKeyDown(theEvent);
  228.             break;
  229.         case activateEvt:
  230.             DoActivate(theEvent);
  231.             break;
  232.         case updateEvt:
  233.             DoUpdate(theEvent);
  234.             break;
  235.         case kHighLevelEvent:
  236.             break;
  237.         case osEvt:        /* old app4Evt -- suspend/resume event */
  238.                         /* modifiers contains data */
  239.             {
  240.                 Str255 s;
  241.                 Print("osEvt, mods=");
  242.                 NumToString( (long)theEvent->modifiers, s);
  243.                 PtoCstr( s);
  244.                 Print( (char*)s);
  245.             }
  246.             break;
  247.     }
  248.         
  249. } // DoEvent()
  250.  
  251.  
  252. static void DoIdle(EventRecord *theEvent, RgnHandle cursorRgn)
  253. {
  254. #ifdef DEBUG
  255.     DebugStr("\pDoIdle, before AdjustCursor");
  256. #endif
  257.     AdjustCursor(theEvent->where, cursorRgn);
  258. #ifdef DEBUG
  259.     DebugStr("\pDoIdle, before DoDialogEvent");
  260. #endif
  261.     DoDialogEvent(theEvent);
  262. } // DoIdle()
  263.  
  264.  
  265. static void DoInfo(void)
  266. {
  267.     short itemHit = -9;
  268.     DialogPtr infoDlog;
  269.     
  270.     if ((infoDlog = GetNewDialog(kInfoDLOG, NULL, (void*)(-1))) != NULL)
  271.     {
  272.         ShowWindow((WindowPtr)infoDlog);
  273.         SelectWindow((WindowPtr)infoDlog);
  274.         UserItem( infoDlog, kInfoOKOutline, OutlineButton);
  275.         while (itemHit != kInfoOKButt)
  276.             ModalDialog( NULL, &itemHit);
  277.         DisposDialog(infoDlog);
  278.     } else
  279.         SysBeep(5);       /* beep if we can't get dialog box */
  280. } // DoInfo() 
  281.  
  282.  
  283. static void DoKeyDown(EventRecord *theEvent)
  284. {
  285.     char theChar = (char)(theEvent->message & charCodeMask);
  286.     if (theEvent->modifiers & cmdKey)
  287.         DoCommand( MenuKey(theChar));
  288.     else
  289.         if (gCurrentWindow == gControlDialog)
  290.             DoDialogEvent(theEvent);
  291. } // DoKeyDown()
  292.  
  293.  
  294. static void DoMouseDown(EventRecord *theEvent)
  295. {
  296.     WindowPtr whichWindow;
  297.     Rect dragRect;
  298.     int  part;
  299.     
  300. #ifdef DEBUG
  301.     DebugStr("\pDoMouseDown");
  302. #endif
  303.     dragRect = qd.screenBits.bounds;
  304.     InsetRect( &dragRect, -4, -4);
  305.     
  306.     part = FindWindow(theEvent->where, &whichWindow);
  307.     
  308.     switch(part) {
  309.         case inMenuBar:
  310.             DoCommand( MenuSelect(theEvent->where) );
  311.             break;
  312.         case inContent:
  313.             if (whichWindow != gCurrentWindow)
  314.             {    SelectWindow(whichWindow);        // bring clicked window up
  315.                 gCurrentWindow = whichWindow;
  316.             }
  317.             else if (gCurrentWindow == gControlDialog)  // dialog event
  318.                 DoDialogEvent(theEvent);
  319.             else if (gCurrentWindow == gMainWindow)    // track mouse coords
  320.                 TrackCoords(theEvent->where);
  321.             break;
  322.         case inDrag:
  323.             if (whichWindow != gCurrentWindow)
  324.             {
  325.                 if ( theEvent->modifiers & cmdKey)
  326.                 {
  327.                     // then drag whichWindow around
  328.                     DragWindow( whichWindow, theEvent->where, &dragRect);
  329.                 }
  330.                 else  // no command key, so bring whichWindow to the front
  331.                 {
  332.                     SelectWindow(whichWindow);
  333.                     gCurrentWindow = whichWindow;
  334.                 }
  335.             }
  336.             else // a click in the current window's drag region
  337.             {
  338.                 // then drag whichWindow around
  339.                 DragWindow( whichWindow, theEvent->where, &dragRect);
  340.             }
  341.             break;
  342.         case inGrow:
  343.             break;
  344.         case inGoAway:
  345.             break;
  346.     } // switch
  347. } // DoMouseDown()
  348.  
  349.  
  350. static void DoUpdate(EventRecord *theEvent)
  351. {
  352.     GrafPtr savedPort;
  353.     
  354. #ifdef DEBUG
  355.     DebugStr("\pDoUpdate");
  356. #endif
  357.     if ( gControlDialog == (WindowPtr)theEvent->message)
  358.     {
  359.         Print("Dialog update");
  360.         DoDialogEvent(theEvent);
  361.     }
  362.     else if (gMainWindow == (WindowPtr)theEvent->message)
  363.     {
  364.         Print("Main update");
  365.         GetPort(&savedPort);
  366.         SetPort(gMainWindow);
  367.         BeginUpdate(gMainWindow);
  368.         // Restore the entire bitmap image...
  369.         CopyBits( &gOffGP.portBits,  &gMainWindow->portBits,
  370.                     &gOffGP.portRect,  &gMainWindow->portRect,
  371.                     srcCopy,           NULL);
  372.         EndUpdate(gMainWindow);
  373.         SetPort(savedPort);
  374.     }
  375.     else // must be the select window
  376.     {
  377.         Print("select update");
  378.         BeginUpdate(gSelectWindow);
  379.         EndUpdate(gSelectWindow);
  380.     }
  381. } // DoUpdate()
  382.  
  383.  
  384. static void Init(void)
  385. {
  386.     register int i;
  387.     InitGraf(&qd.thePort);
  388.     InitFonts();
  389.     InitWindows();
  390.     InitMenus();
  391.     TEInit();
  392.     InitDialogs( (ProcPtr)0L);
  393.     InitCursor();
  394.     for (i=0; i<7; i++) (void)MoreMasters();    
  395.     FlushEvents(everyEvent, 0);
  396.     SetEventMask(everyEvent);
  397. } // Init()
  398.  
  399.  
  400. static void SetupMenus(void)
  401. {
  402.     Handle mbarH;
  403.     
  404.     mbarH = (Handle)GetNewMBar(kMenuBar);
  405.     if (mbarH == NULL) {
  406.         Error( kFetchMenuErr, 0, 0, 0, stopIcon);
  407.         ExitToShell();
  408.     }
  409.     SetMenuBar( mbarH);
  410.     
  411.     gAppleM= GetMenu(kAppleM);
  412.     gFileM = GetMenu(kFileM);
  413.     gEditM = GetMenu(kEditM);
  414.     gTypeM = GetMenu(kTypeM);
  415.     
  416.     AddResMenu( gAppleM, 'DRVR');
  417.     AddResMenu( gTypeM, kMorphECR); // Add external code resources
  418.     DrawMenuBar();
  419. } // SetupMenus()
  420.  
  421.  
  422. static void SetupWindows(void)
  423. {
  424.     gControlDialog = GetNewDialog(kControlDLOG, NULL, (DialogPtr)-1L);
  425.     gMainWindow    = GetNewWindow(kMainWIND, NULL, (WindowPtr)-1L);
  426.     gSelectWindow  = GetNewWindow(kSelectWIND, NULL, (WindowPtr)-1L);
  427.  
  428.     if ( (gControlDialog == NULL) ||
  429.          (gMainWindow == NULL) ||
  430.          (gSelectWindow == NULL) )
  431.     {
  432.         // Alert the user we couldn't get a window...
  433.         Error( kFetchWINDErr, 0, 0, 0, stopIcon);
  434.         ExitToShell();
  435.     }
  436.  
  437.     WriteCoords();
  438.     UserItem(gControlDialog, kZoomGroup, BoxItem);
  439.     UserItem(gControlDialog, kRangeGroup, BoxItem);
  440.     
  441.     ShowWindow(gMainWindow);
  442.     ShowWindow(gSelectWindow);   // used for status messages, too.
  443.     SetPort(gSelectWindow);
  444.     TextFont(monaco);
  445.     TextSize(9);
  446.     { FontInfo finfo;
  447.         GetFontInfo(&finfo);
  448.         gPrintY = finfo.ascent + finfo.descent +
  449.                   finfo.leading;
  450.     }
  451.     ShowWindow(gControlDialog);
  452.     DrawDialog(gControlDialog);  // show the dialog items now.
  453.     SelectWindow(gControlDialog);
  454.     gCurrentWindow = gControlDialog; // which one is selected now.
  455. } // SetupWindows()
  456.  
  457.  
  458. void main(void)
  459. {
  460.     RgnHandle cursorRgn;
  461.     Boolean gotEvent;
  462.     EventRecord theEvent;
  463.     
  464.     Init(); // init mac toolbox
  465.     SetupMenus();        // get menubar/menus going, install ECRs
  466.     SetupWindows();        // load the windows, install user items, etc.
  467.     SetupOffscreen();    // get offscreen buffer set up
  468.     
  469.     SetPort(gControlDialog);
  470.     
  471. #ifdef DEBUG
  472.     DebugStr("\pmain, before GetCursor()");
  473. #endif
  474.     gCrossCursor = NULL;
  475.     gCrossCursor = GetCursor( crossCursor);  // load "plus" cursor
  476.     
  477.     gDefaultProcP = Default;        // Built-in function
  478.     gDefaultProcH = &gDefaultProcP;    // simulate a handle.  be careful.
  479.     gMorphProcH = gDefaultProcH;    // set the global to use
  480.     CheckItem( gTypeM, kTypeDefaultItem, TRUE);
  481.     gTypeChkItem = kTypeDefaultItem;
  482.     
  483.     cursorRgn = NewRgn();            // create an empty region
  484.     do
  485.     {
  486.         gotEvent = WaitNextEvent( everyEvent, &theEvent,
  487.                                   gSleepTime, cursorRgn);
  488.                     
  489.         if (gotEvent)
  490.             DoEvent(&theEvent);
  491.         else
  492.             DoIdle(&theEvent, cursorRgn);  // allow dlog text areas to blink
  493.     } while (1);
  494. } // main()
  495.  
  496.  
  497.