home *** CD-ROM | disk | FTP | other *** search
- /*
- file PackageTool.c
-
- Description:
- This file contains the main event displatching code used in the PackageTool
- application.
-
- PackageTool is an application illustrating how to create application
- packages in Mac OS 9. It provides a simple interface for converting
- correctly formatted folders into packages and vice versa.
-
- by John Montbriand, 1999.
-
- Copyright: © 1999 by Apple Computer, Inc.
- all rights reserved.
-
- Disclaimer:
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "DSC Sample Code"
- after having made changes. If you're going to re-distribute the source,
- we require that you make it clear in the source that the code was
- descended from Apple Sample Code, but that you've made changes.
-
- Change History (most recent first):
- 10/19/99 created by John Montbriand
- */
-
- #include "PackageTool.h"
- #include "Utilities.h"
-
- #include <Fonts.h>
- #include <Dialogs.h>
- #include <PLStringFuncs.h>
- #include <TextUtils.h>
- #include <Gestalt.h>
- #include <StdIO.h>
- #include <String.h>
- #include <Devices.h>
- #include <Appearance.h>
- #include <Resources.h>
-
- #include "SimplePrefs.h"
- #include "PackageUtils.h"
- #include "PackageWindow.h"
-
-
- #ifndef __MWERKS__
- QDGlobals qd; /* QuickDraw globals */
- #endif
-
- /* application's globals */
- Boolean gRunning = true; /* true while the application is running, set to false to quit */
- Collection gPreferences = NULL; /* main preferences collection, saved in the prefs file. */
-
-
- Collection GetCollectedPreferences(void) {
- return gPreferences;
- }
-
-
-
-
- /* ResetMenus is called to reset the menus immediately before
- either MenuSelect or MenuKey is called. Here, we disable the
- quit command during file copies. */
- static void ResetMenus(void) {
- MenuHandle fileMenu;
- /* get the file menu from the current menu list */
- fileMenu = GetMenuHandle(mFile);
- /* disable quit if we're in the middle of copying a file */
- }
-
-
- /* DoMenuCommand is called after either MenuSelect of MenuKey. The
- parameter rawMenuSelectResult is the result from one of these two routines.
- DoMenuCommand parses this result into its two parts, and dispatches
- the menu command as appropriate. */
- static void DoMenuCommand(long rawMenuSelectResult) {
- short menu, item;
- /* decode the MenuSelect result */
- menu = (rawMenuSelectResult >> 16);
- if (menu == 0) return;
- item = (rawMenuSelectResult & 0x0000FFFF);
- /* dispatch on result */
- switch (menu) {
- case mApple:
- if (item == iAbout) {
- /* show the about box. */
- ParamAlert(kAboutBoxAlert, NULL, NULL);
- } else if (item >= iFirstAppleItem) {
- Str255 deskAccName;
- /* open an apple menu item. */
- GetMenuItemText(GetMenuHandle(mApple), item, deskAccName);
- OpenDeskAcc(deskAccName);
- }
- break;
- case mFile:
- if (item == iQuit) gRunning = false; /* file.quit == quit */
- break;
- case mEdit:
- if (item == iClear) SetNewDisplay(NULL); /* edit.clear == clear the display */
- break;
- }
- /* unhilite the menu once we're done the command */
- HiliteMenu(0);
- }
-
-
-
-
-
-
-
- /* OpenApplication is an apple event handler called for 'open application' apple events. */
- static pascal OSErr OpenApplication(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
- return noErr;
- }
-
- /* CloseApplication is an apple event handler called for 'close application' apple events. */
- static pascal OSErr CloseApplication(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
- gRunning = false;
- return noErr;
- }
-
-
- static pascal OSErr OpenDocument(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
- OSErr err;
- AEDescList documents;
- long n;
- FSSpec fileSpec, packageSpec;
- AEKeyword keyWd;
- DescType typeCd;
- Size actSz;
-
- /* initial state */
- AECreateDesc(typeNull, NULL, 0, &documents);
-
- /* get the open parameter */
- err = AEGetParamDesc(appleEvt, keyDirectObject, typeAEList, &documents);
- if (err != noErr) goto bail;
- err = AECountItems(&documents, &n);
- if (err != noErr) goto bail;
- if (n == 0) { err = paramErr; goto bail; }
- err = AEGetNthPtr(&documents, 1, typeFSS, &keyWd, &typeCd,
- (Ptr) &fileSpec, sizeof(fileSpec), (actSz = sizeof(fileSpec), &actSz));
- if (err != noErr) goto bail;
-
- if (IdentifyPackage(&fileSpec, &packageSpec))
- SetNewDisplay(&fileSpec);
- else if (FSSpecIsAFolder(&fileSpec))
- SetNewDisplay(&fileSpec);
- else SetNewDisplay(NULL);
-
- bail:
- AEDisposeDesc(&documents);
- return err;
- }
-
-
- /* EVENT HANDLING ------------------------------------------------ */
-
-
- /* HandleNextEvent handles the event in the event record *ev dispatching
- the event to appropriate routines. */
- static void HandleNextEvent(EventRecord *ev) {
- DialogPtr theDialog;
- WindowPtr theWindow;
- short itemNo;
-
- /* dialog pre-processing */
- if (((ev->what == keyDown) || (ev->what == autoKey)) && ((ev->modifiers & cmdKey) != 0)) {
- ResetMenus();
- DoMenuCommand(MenuKey((char) (ev->message & charCodeMask)));
- } else if (ev->what == osEvt) {
- WindowPtr target;
- Boolean activate;
- if ( (((ev->message >> 24) & 0x0FF) == suspendResumeMessage) && ((ev->message & resumeFlag) != 0)) {
- activate = true;/* switching in */
- } else activate = false;
- target = FrontWindow();
- if (IsPackageWindow(target))
- ActivatePackageWindow(target, activate);
- } else if (ev->what == activateEvt) {
- WindowPtr target;
- target = (WindowPtr) ev->message;
- if (IsPackageWindow(target))
- ActivatePackageWindow(target, ((ev->modifiers&1) != 0));
- }
-
- /* handle clicks in the dialog window */
- if (IsDialogEvent(ev))
- if (DialogSelect(ev, &theDialog, &itemNo)) {
- if (IsPackageWindow(theDialog))
- HitPackageWindow(theDialog, ev, itemNo);
- }
-
- /* clicks and apple events... */
- if (ev->what == kHighLevelEvent) {
- AEProcessAppleEvent(ev);
- } else if (ev->what == mouseDown)
- switch (FindWindow(ev->where, &theWindow)) {
-
- /* menu bar clicks */
- case inMenuBar:
- ResetMenus();
- DoMenuCommand(MenuSelect(ev->where));
- break;
-
- /* clicks in the close box, close the app */
- case inGoAway:
- if (TrackGoAway(theWindow, ev->where)) {
- gRunning = false;
- }
- break;
-
- /* allow window drags */
- case inDrag:
- if (theWindow == FrontWindow()) {
- Rect boundsRect = { -32000, -32000, 32000, 32000};
- DragWindow(theWindow, ev->where, &boundsRect);
- }
- break;
-
- /* desktop clicks, etc... */
- case inSysWindow:
- SystemClick(ev, theWindow);
- break;
- }
- }
-
-
-
-
- /* MyIdleProc is the idle procedure called by AEInteractWithUser while we are waiting
- for the application to be pulled into the forground. It simply passes the event along
- to HandleNextEvent */
- static pascal Boolean MyIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) {
- HandleNextEvent(theEvent);
- return false;
- }
-
- /* ParamAlert is a general alert handling routine. If Apple events exist, then it
- calls AEInteractWithUser to ensure the application is in the forground, and then
- it displays an alert after passing the s1 and s2 parameters to ParamText. */
- OSStatus ParamAlert(short alertID, StringPtr s1, StringPtr s2) {
- AEIdleUPP theIdleUPP;
- OSStatus err;
- theIdleUPP = NewAEIdleProc(MyIdleProc);
- if (theIdleUPP == NULL) { err = memFullErr; goto bail; }
- err = AEInteractWithUser(kNoTimeOut, NULL, theIdleUPP);
- if (err != noErr) goto bail;
- ParamText(s1, s2, NULL, NULL);
- err = Alert(alertID, NULL);
- DisposeAEIdleUPP(theIdleUPP);
- return err;
- bail:
- if (theIdleUPP != NULL) DisposeAEIdleUPP(theIdleUPP);
- return err;
- }
-
-
-
-
- /* main program */
-
- int main(void) {
- OSErr err;
- long response;
- AEEventHandlerUPP aehandler;
-
- /* set up the managers */
- SetApplLimit(GetApplLimit());
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- TEInit();
- InitMenus();
- InitDialogs(0);
- FlushEvents(everyEvent, 0);
- InitCursor();
-
- if (Gestalt('sysv', &response) != noErr) response = 0;
- if (response < 0x0900) {
- ParamAlert(138, NULL, NULL);
- err = userCanceledErr;
- goto bail;
- }
-
- err = RegisterAppearanceClient();
- if (err != noErr) goto bail;
-
- /* Apple event handlers */
- aehandler = NewAEEventHandlerProc(OpenApplication);
- if (aehandler == NULL) { err = memFullErr; goto bail; }
- err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, aehandler, 0, false);
- if (err != noErr) goto bail;
- aehandler = NewAEEventHandlerProc(CloseApplication);
- if (aehandler == NULL) { err = memFullErr; goto bail; }
- err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, aehandler, 0, false);
- if (err != noErr) goto bail;
- aehandler = NewAEEventHandlerProc(OpenDocument);
- if (aehandler == NULL) { err = memFullErr; goto bail; }
- err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, aehandler, 0, false);
- if (err != noErr) goto bail;
-
- /* get our preferences */
- gPreferences = NewCollection();
- if (gPreferences == NULL) { err = memFullErr; goto bail; }
- GetPreferences(kAppPrefsType, kAppCreatorType, gPreferences);
-
- /* ***** set up the menu bar ***** */
- SetMenuBar(GetNewMBar(kMenuBarResource));
- DrawMenuBar();
- AppendResMenu(GetMenuHandle(mApple), 'DRVR');
-
- /* open the window */
- err = CreatePackageWindow();
- if (err != noErr) {
- Str255 errStr;
- NumToString(err, errStr);
- ParamAlert(kOpenAppFailedAlert, errStr, NULL);
- }
-
- /* run the main loop */
- while (gRunning) {
- EventRecord ev;
- /* get the next event */
- if ( ! WaitNextEvent(everyEvent, &ev, GetCaretTime(), NULL) )
- ev.what = nullEvent;
- HandleNextEvent(&ev);
- }
-
- /* all done */
- ClosePackageWindow();
- UnregisterAppearanceClient();
- SavePreferences(kAppPrefsType, kAppCreatorType, "\pPackageTool Preferences", gPreferences);
- DisposeCollection(gPreferences);
- ExitToShell();
- return 0;
- bail:
- if (err != userCanceledErr) {
- Str255 errStr;
- NumToString(err, errStr);
- ParamAlert(kMainFailedAlert, errStr, NULL);
- }
- ExitToShell();
- return 1;
- }
-
-
-