Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
source/MainLoops.c
/* |
File: MainLoops.c |
Description:Code to do the general main loops that a Macintosh application needs to do |
to get necessary events and respond to user input. |
Author: MC |
Copyright: © Copyright 1999-2000 Apple Computer, Inc. All rights reserved. |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Change History (most recent first): |
*/ |
#define TARGET_API_MAC_CARBON 1 |
#include <LowMem.h> |
#include <Drag.h> |
#include <Menus.h> |
#include <Devices.h> |
#include <Events.h> |
#include <FixMath.h> |
#include <GXMath.h> |
#include <QuickTimeComponents.h> |
#include <Movies.h> |
#include <TextServices.h> |
#include <Balloons.h> |
#include "Preferences.h" |
#include "AppleEventCode.h" |
#include "ToolboxInit.h" |
#include "DragCode.h" |
#include "GraphicImportDrawCode.h" |
#include "DisplayErrorAlert.h" |
#include "LinkedList.h" |
#include "MovieCode.h" |
#include "MyKeys.h" |
#include "MyEvents.h" |
#include "MyMenus.h" |
#include "MyCarbonPrinting.h" |
#include "WindowCode.h" |
#include "Structs.h" |
#include "Defines.h" |
extern Boolean gDone, |
gStop, |
gPrefsChanged, |
gInForeground; |
extern myListPtr gFilesToOpen, |
gFilesToPrint; |
extern unsigned long gSleepTime; |
extern OSErr gErr; |
extern OSType * gAcceptedFlavorTypes; |
static void IdleWindows () { |
WindowPtr window; |
WindowInfoHandle winInfo; |
window = FrontWindow (); |
while (window != nil) { |
winInfo = (WindowInfoHandle)GetWRefCon ((WindowPtr)window); |
if (winInfo != nil && (**winInfo).sig == kMySig) { |
if ((**winInfo).winMovieController != nil) { |
MCIdle ((**winInfo).winMovieController); |
} |
} |
window = GetNextWindow (window); |
} |
} |
static OSErr DoIdle (void) { |
FSSpecPtr itemToOpen; |
OSErr err; |
FInfo fndrInfo; |
MoviesTask (DoTheRightThing, DoTheRightThing); |
IdleWindows (); |
if (gStop == false) { |
itemToOpen = (FSSpecPtr)GetItemNumFromList (0, gFilesToOpen); |
if (itemToOpen != nil) { |
if (gInForeground == true) { |
gSleepTime = kForegroundBusySleepTime; // Open the files with minimum time give to the system |
} else { |
gSleepTime = kBackgroundBusySleepTime; // Open the files with minimum time give to the system |
} |
err = FSpGetFInfo (itemToOpen, &fndrInfo); |
if (err == noErr && fndrInfo.fdType != MovieFileType) { |
err = NewWindowWithPict (itemToOpen, (WindowPtr)-1L); |
} else if (err == noErr) { |
err = NewWindowWithMovie (itemToOpen, (WindowPtr)-1L); |
} |
if (err != noErr) { |
DisplayErrorAlertWithString (err, itemToOpen->name); |
if (err == memFullErr) { |
gStop = true; |
} |
} |
DeleteHeadFromList (&gFilesToOpen); |
} |
} |
if (gStop == true) { |
gStop = false; |
do { |
itemToOpen = (FSSpecPtr)GetItemNumFromList (0, gFilesToOpen); |
DeleteHeadFromList (&gFilesToOpen); |
} while (itemToOpen != nil); |
} |
return err; |
} |
static void MainEventLoop (void) { |
EventRecord event; |
OSErr err = noErr; |
Boolean gotEvent = false; |
while (!gDone) { |
gotEvent = WaitNextEvent (everyEvent, &event, gSleepTime, nil); |
if (gotEvent) { |
// We are about to be busy, so up our time needs |
if (gInForeground == true) { |
gSleepTime = kForegroundBusySleepTime; |
} else { |
gSleepTime = kBackgroundBusySleepTime; |
} |
if (!MovieEvent (&event)) { |
if (IsDialogEvent (&event)) { |
err = DispatchDialogEvent (&event); |
} else { |
err = DispatchEvent (&event); |
} |
} |
} else { |
if (gInForeground == true) { |
gSleepTime = kForegroundIdleSleepTime; |
} else { |
gSleepTime = kBackgroundIdleSleepTime; |
} |
err = DoIdle (); |
} |
if (gErr != noErr) { |
DisplayErrorAlert (gErr); |
gErr = noErr; |
} |
} |
} |
int main (void) { |
OSErr err; |
err = noErr; |
ToolboxInit (); |
if (err == noErr) { |
err = EnterMovies (); |
} |
if (err == noErr) { |
err = FindAllGIComponents (); |
} |
if (err == noErr) { |
err = MenuBarInit (); |
} |
if (err == noErr) { |
err = InstallRequiredAppleEvents (); |
} |
if (err == noErr) { |
err = InitDragManager (); |
} |
if (err == noErr) { |
err = ReadPrefs (); |
} |
if (err != noErr) { |
// No prefs file, so run with defaults. |
err = InitPrefs (); |
} |
if (err == noErr) { |
MainEventLoop (); |
} |
if (gPrefsChanged == true) { |
err = WritePrefs (); |
if (err != noErr) { |
err = CreatePrefs (); |
err = WritePrefs (); |
} |
} |
DisposePtr ((Ptr)gAcceptedFlavorTypes); |
return 0; |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14