home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / capturing / digitizershell / macframework.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  8.7 KB  |  387 lines

  1. /*
  2.     File:        MacFramework.c
  3.  
  4.     Contains:    Basic Macintosh Functions for window, menu handling and similar things for the 
  5.                 SG/vdig environment.
  6.  
  7.     Written by:     
  8.  
  9.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/28/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24.  
  25.  
  26. // INCLUDES
  27. #include <DiskInit.h>
  28. #include <SegLoad.h>
  29. #include <ToolUtils.h>
  30. #include <Devices.h>
  31. #include <Fonts.h>
  32.  
  33. #include "DTSQTUtilities.h"
  34. #include "AppConfiguration.h"
  35. #include "MacFramework.h"
  36.  
  37.  
  38. // GLOBALS
  39. Boolean gQuitFlag = false;                                        // Flag that keeps track of termination state.
  40. unsigned long gWNEsleep = kWNEDefaultSleep;        // WaitNextEvent sleep time.
  41.  
  42.  
  43. // PURE MAC TOOLBOX FUNCTIONS
  44.  
  45. // ______________________________________________________________________
  46. void InitMacEnvironment(long nMasters)
  47. {
  48.     long i;
  49.     MaxApplZone();
  50.     
  51.     for(i = 0; i <nMasters; i++)
  52.         MoreMasters();
  53.     
  54.     InitGraf(&qd.thePort);
  55.     InitFonts();
  56.     InitWindows();
  57.     InitMenus();
  58.     FlushEvents(everyEvent, 0);
  59.     TEInit();
  60.     InitCursor();
  61.     InitDialogs(NULL);
  62. }
  63.  
  64.  
  65. // ______________________________________________________________________
  66. void InitStack(long extraStackSpace)
  67. {
  68.     Ptr size = GetApplLimit();
  69.     SetApplLimit(size - extraStackSpace);    // make room on the stack
  70. }
  71.  
  72.  
  73. // ______________________________________________________________________
  74. Boolean InitMenubar(void)
  75. {
  76.     Handle aMenuHandle = NULL;
  77.     
  78.     aMenuHandle = GetNewMBar(mMenubar); DebugAssert(aMenuHandle != NULL);
  79.     if(aMenuHandle == NULL)
  80.     {
  81.         ShowWarning("\pCould not find the Menubar resource!", 0);
  82.         return false;
  83.     }
  84.     
  85.     SetMenuBar(aMenuHandle);
  86.     DisposeHandle(aMenuHandle);  DebugAssert(MemError() == noErr);
  87.     
  88.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
  89.  
  90.     DrawMenuBar();
  91.     return true;
  92. }
  93.  
  94.  
  95. // ______________________________________________________________________
  96. void HandleMenuCommand(long theMenuResult)
  97. {
  98.     short             aMenuID, aMenuItem;
  99.     Str255            daName;
  100.     WindowRef    whichWindow;
  101.     
  102.     aMenuID = HiWord(theMenuResult);
  103.     aMenuItem = LoWord(theMenuResult);
  104.     
  105.     switch(aMenuID)
  106.     {
  107.         // APPLE MENU
  108.         case mApple:
  109.             switch(aMenuItem)
  110.             {
  111.                 case iAbout:    // about box
  112.                     ShowAboutDialogBox();     
  113.                     break;
  114.                 
  115.                 default:     // Apple menu handling
  116.                     GetMenuItemText(GetMenuHandle(mApple), aMenuItem, daName);
  117.                     (void)OpenDeskAcc(daName);
  118.                     break;
  119.             } // end switch(aMenuItem)
  120.             break;
  121.  
  122.         // FILE MENU            
  123.         case mFile:
  124.             switch(aMenuItem)
  125.             {
  126.                 case iNew:
  127.                     {
  128.                         CreateSGEnviroment();
  129.                     }
  130.                     break;
  131.                 
  132.                 case iClose:
  133.                     {
  134.                         if( (whichWindow = FrontWindow() ) != NULL)
  135.                         {    
  136.                             if(IsAppWindow(whichWindow))
  137.                                 DoDestroyMovieWindow(whichWindow);
  138.                         }
  139.                     }
  140.                     break;
  141.  
  142.                 case iQuit:
  143.                     {
  144.                         gQuitFlag = true;
  145.                         break;
  146.                     }
  147.  
  148.             } // end switch(aMenuItem), mFile
  149.             break;
  150.     
  151.  
  152.     default:
  153.         HandleApplicationMenu(aMenuID, aMenuItem);
  154.         break;
  155.     } // end switch(aMenuID)
  156.     
  157.     HiliteMenu(0);
  158. }
  159.  
  160.  
  161. // ______________________________________________________________________
  162. void AdjustMenus(void)
  163. {
  164.     WindowRef             aWindow;
  165.     
  166.     aWindow = FrontWindow();
  167.  
  168.     if(aWindow != NULL)
  169.     {
  170.         // Enable the close entry of we have windows = movies.
  171.         EnableItem( GetMenuHandle(mFile), iClose);
  172.  
  173.     } // end if(aWindow != NULL)
  174.     else 
  175.     {
  176.         DisableItem(GetMenuHandle(mFile), iClose);
  177.         
  178.     }
  179.     
  180.     AdjustApplicationMenus();                    // fix any specific app menus as well.
  181. }
  182.  
  183.  
  184. // ______________________________________________________________________
  185. void MainEventLoop(void)
  186. {
  187.     EventRecord                         anEvent;
  188.     WindowRef                        whichWindow;
  189.     Boolean                                aMovieEvent;
  190.     short                                aWindowPart;
  191.     Rect                                    aRefreshArea;
  192.     Point                                    aPoint  = {100, 100};
  193.     
  194.     while(!gQuitFlag)
  195.     {
  196.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  197.         
  198. #ifdef USESIOUX
  199.         SIOUXHandleOneEvent(&anEvent);
  200. #endif USESIOUX
  201.  
  202.         AdjustMenus();
  203.         aMovieEvent = false;
  204.         
  205.         if( (whichWindow = FrontWindow() ) != NULL)
  206.             DoIdle(whichWindow);
  207.  
  208.         switch(anEvent.what)
  209.         {
  210.             case mouseDown:
  211.                 aWindowPart = FindWindow(anEvent.where, &whichWindow);
  212.  
  213.                 // Window related events:            
  214.                 switch(aWindowPart)
  215.                 {
  216.                     case inMenuBar:
  217.                         HandleMenuCommand(MenuSelect(anEvent.where));
  218.                         break;
  219.     
  220.                     case inContent:
  221.                         SelectWindow(whichWindow);
  222.                         HandleContentClick(whichWindow, &anEvent);
  223.                         break;
  224.                     
  225.                     case inDrag:
  226.                         DoDragWindow(whichWindow, &anEvent);
  227.                         break;
  228.                     
  229.                     case inGoAway:
  230.                         // if the window is closed, dispose the movie, the controller and the window
  231.                         if( TrackGoAway(whichWindow, anEvent.where) )
  232.                             DoDestroyMovieWindow(whichWindow);
  233.                         break;
  234.                 } // end switch(aWindowPart):
  235.                 break;
  236.  
  237.                 // System level events:
  238.                 case updateEvt:
  239.                     whichWindow = (WindowRef)anEvent.message;
  240.                     aRefreshArea = ((**(whichWindow->visRgn)).rgnBBox);
  241.                     DoUpdateWindow(whichWindow, &aRefreshArea);
  242.                     break;
  243.                     
  244.                 case keyDown:
  245.                 case autoKey:
  246.                     HandleKeyPress(&anEvent);
  247.                     break;
  248.                 
  249.                 case diskEvt:
  250.                     if(HiWord(anEvent.message) != noErr)
  251.                         (void)DIBadMount(aPoint, anEvent.message);
  252.                     break;
  253.                 
  254.                 case activateEvt:
  255.                     whichWindow = (WindowRef)anEvent.message;
  256.                     
  257.                      if ( IsAppWindow(whichWindow) )
  258.                     {
  259.                         DoActivateWindow(whichWindow, ((anEvent.modifiers & activeFlag) != 0 ));
  260.                     }
  261.                     break;
  262.                     
  263.                 case osEvt:
  264.                     switch(( anEvent.message > 24) & 0x00FF )        // get high byte of word
  265.                     {
  266.                         case suspendResumeMessage:
  267.                             if( FrontWindow() )
  268.                             {
  269.                                 DoActivateWindow(FrontWindow(), !((anEvent.message & resumeFlag) == 0));
  270.                             }
  271.                             break;
  272.                         
  273.                         case mouseMovedMessage:
  274.                             break;
  275.                     } // end switch(anEvent.message > 24) & 0x00FF)    
  276.                     break;
  277.                 
  278.                 case nullEvent:
  279.                     if(( whichWindow = FrontWindow() ) != NULL)
  280.                         DoIdle(whichWindow);
  281.                     break;
  282.         } // end switch(anEvent.what)
  283.     } // end while(!gQuitFlag)
  284. }
  285.  
  286.  
  287. // ______________________________________________________________________
  288. Boolean IsAppWindow(WindowRef theWindow)
  289. {
  290.     short aWindowKind;
  291.     
  292.     if (theWindow == NULL)
  293.         return false;
  294.     else
  295.     {
  296.         aWindowKind = ((WindowPeek)theWindow)->windowKind;
  297.         return ( (aWindowKind >= userKind) || (aWindowKind == dialogKind) );
  298.     }
  299. }
  300.  
  301.  
  302. // ______________________________________________________________________
  303. void HandleKeyPress(EventRecord *theEvent)
  304. {
  305.     char aKey;
  306.     
  307.     aKey = theEvent->message & charCodeMask;
  308.     
  309.     if(theEvent->modifiers & cmdKey)        // command key down?
  310.         HandleMenuCommand(MenuKey(aKey));
  311. }
  312.  
  313.  
  314. // ______________________________________________________________________
  315. void ShowAboutDialogBox(void)
  316. {
  317.     DialogPtr aDialog;
  318.     short         itemHit;
  319.     FontInfo    aFontInfo;
  320.     GrafPtr        aSavedPort;
  321.     
  322.     GetPort(&aSavedPort);
  323.     aDialog = GetNewDialog(kAboutBox, NULL, (WindowPtr) - 1L); DebugAssert(aDialog != NULL);
  324.     SetPort(aDialog);
  325.  
  326.     // Change font to Geneva, 9pt, bold, just for the sake of it...
  327.     TextFont(applFont); TextSize(9); TextFace(bold);
  328.     GetFontInfo(&aFontInfo);
  329.     
  330.     (*((DialogPeek)aDialog)->textH)->txFont = applFont;
  331.     (*((DialogPeek)aDialog)->textH)->txSize = 9;
  332.     (*((DialogPeek)aDialog)->textH)->lineHeight = aFontInfo.ascent + aFontInfo.descent + aFontInfo.leading;
  333.     (*((DialogPeek)aDialog)->textH)->fontAscent = aFontInfo.ascent;
  334.  
  335.     SetDialogDefaultItem(aDialog, 1);
  336.         
  337.     do
  338.     {
  339.         ModalDialog(NULL, &itemHit);
  340.     } while(itemHit != ok);
  341.     
  342.     SetPort(aSavedPort);
  343.     DisposeDialog(aDialog);  DebugAssert(MemError() == noErr);
  344. }
  345.  
  346.  
  347. // ______________________________________________________________________
  348. void ShowWarning(Str255 theMessage, OSErr theErr)
  349. {
  350.     Str255 errString;
  351.     
  352.     NumToString(theErr, errString);
  353.     ParamText("\pWarning!", theMessage, theErr ? errString:  NULL, NULL);
  354.     Alert(kAlertError, NULL);
  355. }
  356.  
  357.  
  358. // ______________________________________________________________________
  359. void DoDestroyMovieWindow(WindowRef theWindow)
  360. {
  361.     DoCloseWindow(theWindow);
  362.  
  363.     DisposeWindow(theWindow); DebugAssert(MemError() == noErr);
  364.     
  365.     CompactMem(0xFFFFFFFF);        //We might as well compact the mem here for getting better performance later.
  366. }
  367.  
  368.  
  369. // ______________________________________________________________________
  370. void DoActivateWindow(WindowRef theWindow, Boolean becomingActive)
  371. {
  372.     #pragma unused(becomingActive)
  373.     WindowObject         aWindowObject = NULL;
  374.     MovieController    mc = NULL;
  375.     GrafPtr                    aSavedPort = NULL;
  376.     
  377.     GetPort(&aSavedPort);
  378.     SetPort((GrafPtr)theWindow);
  379.     
  380.     // @@@ Do something related to activation of movie here.
  381.     
  382.     SetPort(aSavedPort);
  383. }
  384.  
  385.  
  386.  
  387.