home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------------------------
- *
- * Simple Sample PowerTalk Application Framework
- *
- * ©1991-1993 Apple Computer
- *
- -------------------------------------------------------------------------------------*/
- /*
- * commands.c -- called in response to menu commands or appleevents
- *
- * change history:
- *
- * SJF 08/23/93 1.0f1 update to final headers, fix comments
- * SJF 04/21/93 1.0b2 update to b2
- * SJF 03/01/93 1.0b1 added digital signatures
- * SJF 02/09/93 1.0b1 update to b1
- * SJF 10/13/92 1.0d4 update to a11
- * SJF 09/09/92 1.0d3 update to a9
- * SJF 05/07/92 1.0d2 update to a6
- * SJF 11/06/91 1.0d1 initial coding
- *
- */
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __CONTROLS__
- #include <Controls.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
-
- #ifndef __FILES__
- #include <Files.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #ifndef __OCEMAIL__
- #include <OCEMail.h>
- #endif
-
- #ifndef __OCESTANDARDMAIL__
- #include <OCEStandardMail.h>
- #endif
-
- #include "const.h"
- #include "strconst.h"
- #include "mymenus.h"
- #include "mytypes.h"
- #include "globals.h"
- #include "utils.h"
- #include "windowstuff.h"
- #include "mystandardmail.h"
-
- #include "commands.h"
-
- /* closes a window in response to the File:Close menu item */
-
- void CommCloseWindow(WindowPtr window)
- {
- WInfoHndl infoHndl;
- Boolean shouldClose;
-
- if (!IsAppWindow(window))
- return;
-
- infoHndl = GetWindowInfo(window);
-
- if (!CanCloseLetter(window)) // see if we're allowed to close the letter
- shouldClose = false;
- else {
- if ((**infoHndl).changed)
- shouldClose = WarnOnWindowClose(window); // see if user really wants to close letter
- else
- shouldClose = true;
- }
-
- if (shouldClose) {
- SendWindowMessage(window,kDeactivateMessage,nil); // send window a deactivate message
- SendWindowMessage(window,kDestroyMessage,nil); // sent window a destroy message
-
- DisposHandleChk(infoHndl);
- DisposeWindow(window);
- }
- else gDone = false; // we might be quitting, so we'd want to abort that
- }
-
-
- /* processes edit menu commands */
-
- void CommEdit(WindowPtr window,short command)
- {
- short msg;
-
- if (!IsAppWindow(window))
- return;
-
- switch (command) {
- case kUndoItem:
- msg = kUndoMessage;
- break;
- case kCutItem:
- msg = kCutMessage;
- break;
- case kCopyItem:
- msg = kCopyMessage;
- break;
- case kPasteItem:
- msg = kPasteMessage;
- break;
- case kClearItem:
- msg = kClearMessage;
- break;
- case kSelectAllItem:
- msg = kSelectAllMessage;
- break;
- default:
- return;
- }
- SendWindowMessage(window,msg,nil); // send the edit message to the window
- }
-
-
- /* called in response to the user selecting the "Preferences..." menu item */
- /* displays the preferences modal dialog and saves the changes in the */
- /* gPreferences global */
-
- void CommEditPreferences(void)
- {
- DialogPtr theDialog;
- short iType;
- Handle iHndl;
- Rect iRect;
- short item;
- Boolean *prefSwitchPtr;
- MyPreferences newPreferences;
-
- BlockMove(&gPreferences,&newPreferences,sizeof(MyPreferences));
-
- theDialog = GetNewDialog(kPrefsDialogID,nil,(WindowPtr)-1L);
- MyDrawDefaultButtonOutline(theDialog,ok);
-
- // set up our dialog with the initial values
-
- GetDItem(theDialog,kCloseAfterSave,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.closeOnSend);
- GetDItem(theDialog,kShowCloseOptions,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.closeOptionsDialog);
- GetDItem(theDialog,kExpandMailerCreate,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.expandOnCreate);
- GetDItem(theDialog,kExpandMailerOpen,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.expandOnOpen);
-
- // let the user play with the dialog
-
- do {
- ModalDialog(nil,&item);
- if (item>=kCloseAfterSave && item<=kExpandMailerOpen) {
- switch (item) {
- case kCloseAfterSave:
- prefSwitchPtr = &newPreferences.closeOnSend;
- break;
- case kShowCloseOptions:
- prefSwitchPtr = &newPreferences.closeOptionsDialog;
- break;
- case kExpandMailerCreate:
- prefSwitchPtr = &newPreferences.expandOnCreate;
- break;
- case kExpandMailerOpen:
- prefSwitchPtr = &newPreferences.expandOnOpen;
- break;
- }
- *prefSwitchPtr = !(*prefSwitchPtr);
- GetDItem(theDialog,item,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,*prefSwitchPtr);
- }
-
- } while (item!=ok && item!=cancel);
-
- // store the new preferences in the gPreferences global
-
- if (item==ok)
- BlockMove(&newPreferences,&gPreferences,sizeof(MyPreferences));
-
- DisposeDialog(theDialog);
- }
-
-
- /* processes print menu item */
-
- void CommPrint(WindowPtr window)
- {
- if (IsAppWindow(window))
- SendWindowMessage(window,kPrintMessage,nil);
- }
-
-
- /* processes page setup menu item */
-
- void CommPageSetup(WindowPtr window)
- {
- WInfoPtr infoPtr;
- char hState;
- Boolean changed;
-
- SetCursor(&qd.arrow);
-
- if (!IsAppWindow(window))
- return;
-
- infoPtr = BeginWindowAccess(window,&hState);
-
- PrOpen();
- changed = PrStlDialog(infoPtr->printRecord); // display page setup dialog
- if (PrError()!=noErr)
- DoError(PrError());
- PrClose();
-
- if (changed)
- SendWindowMessage(window,kPageSetupMessage,nil); // tell window that we changed things
-
- EndWindowAccess(window,hState);
- }
-
-
- /* show about box */
-
- void CommAbout(void)
- {
- DialogPtr theDlg;
- short item;
-
- theDlg = GetNewDialog(kAboutBoxID,nil,(WindowPtr)-1L);
- ModalDialog(nil,&item);
- DisposeDialog(theDlg);
- }
-
-
- /* new command- make a new drawing window */
-
- void CommNew(void)
- {
- MakeWindow(kDrawWindow,nil,nil,true);
- }
-
-
- /* open command- open a drawing or a letter */
-
- void CommOpen(void)
- {
- StandardFileReply sfReply;
- SFTypeList typeList;
- OSErr err;
- WindowPtr window;
-
- typeList[0] = kDrawingType;
- typeList[1] = kCDLtrMsgType;
-
- StandardGetFile(nil,2,typeList,&sfReply);
- if (sfReply.sfGood) {
- err = LoOpen(true, &sfReply.sfFile, nil,true,&window); // call through to low-level call
- if (err!=noErr)
- DoError(err);
- }
- }
-
-
- /* low-level open (can be called by aevt or CommOpen above) */
- /* opens either a letter (from disk or in tray) or a drawing */
-
- OSErr LoOpen(Boolean diskForm, FSSpec *fSpec, LetterSpec *lSpec,Boolean showWindow,
- WindowPtr *openedWindow)
- {
- WindowPtr window;
- WInfoHndl info;
- short wType;
- OSErr err;
- FInfo fInfo;
- LetterDescriptor letterDesc;
- void *message;
- RString name;
-
- name.charSet = smRoman;
- name.dataLength = 0;
-
- *openedWindow = nil;
-
- // if file is on disk, put an fsspec in the load message
-
- if (diskForm) {
- err = FSpGetFInfo(fSpec,&fInfo);
- if (err!=noErr)
- return err;
-
- switch (fInfo.fdType) {
- case kDrawingType:
- wType = kDrawWindow;
- message = fSpec;
- break;
- case kCDLtrMsgType:
- wType = kDrawMailerWindow;
- letterDesc.onDisk = true;
- BlockMove(fSpec,&letterDesc.u.fileSpec,sizeof(FSSpec));
- message = &letterDesc;
- break;
- default:
- return kInternalError;
- }
- }
- else {
- wType = kDrawMailerWindow; // put a letter spec into the message for in tray letters
- letterDesc.onDisk = false;
- BlockMove(lSpec,&letterDesc.u.mailboxSpec,sizeof(LetterSpec));
- message = &letterDesc;
- }
-
- window = MakeWindow(wType,nil,nil,false); // create the window
-
- if (window) {
- info = GetWindowInfo(window);
- SendWindowMessage(window,kLoadMessage,message); // load the window data (letter or drawing)
- if (diskForm)
- BlockMove(fSpec->name, name.body - 1, fSpec->name[0] + 1);
- else {
- (**info).saved = false;
- err = SMPGetComponentInfo(window, 1, kSMPRegarding, &name); // set the filename to
- BlockMove(name.body - 1,fSpec->name,name.dataLength+1); // the letter's subject
- if (err!=noErr)
- return err;
- }
- SetWTitle(window, name.body - 1);
- BlockMove(fSpec,&(*info)->fileSpec,sizeof(FSSpec)); // copy the fsspec into the window info
- if (showWindow)
- ShowWindow(window);
- }
- *openedWindow = window;
- return noErr;
- }
-
-
- /* save command- returns false if user cancelled save */
-
- Boolean CommSaveFile(WindowPtr window)
- {
- WInfoHndl info;
-
- if (!IsAppWindow(window))
- return true;
-
- info = GetWindowInfo(window);
- if (!(**info).changed) // don't do anything if we haven't changed the document
- return true;
-
- if (!(**info).saved)
- return CommSaveAsFile(window); // if we haven't saved before, do a save as
- else {
- LoSaveFile(window,kSMPSave); // call the low-level save
- return true;
- }
- }
-
-
- /* save as command (display standard file to get filename and then call LoSaveFile) */
-
- Boolean CommSaveAsFile(WindowPtr window)
- {
- WInfoHndl info;
- StandardFileReply sfReply;
- Str255 promptString;
- Str255 defaultFilename;
- StringPtr fileName;
- WindowKind kind;
-
- info = GetWindowInfo(window);
-
- // get the default filename
-
- fileName = (**info).fileSpec.name;
- if (fileName[0]==0) {
- GetResString(defaultFilename,kDefaultFilenameID,kDefaultFilename);
- fileName = defaultFilename;
- }
-
- if (!IsAppWindow(window))
- return true;
-
- // get the document type string for "save letter/drawing as:"
-
- kind = GetWindowKind(window);
- switch (kind) {
- case kDrawWindow:
- GetResString(promptString,kPromptStringID,kPromptString);
- break;
- case kDrawMailerWindow:
- GetResString(promptString,kSaveLetterAsID,kSaveLetterAs);
- break;
- }
-
- StandardPutFile(promptString,fileName,&sfReply);
- if (sfReply.sfGood) {
- BlockMove(&sfReply.sfFile,&((**info).fileSpec),sizeof(FSSpec));
- LoSaveFile(window,kSMPSaveAs);
- return true;
- }
- else
- return false;
- }
-
-
- /* low-level save file */
-
- void LoSaveFile(WindowPtr window,SMPSaveType how)
- {
- SendWindowMessage(window,kSaveMessage,(void *)how); // just send the window the save message
- }
-
-
- /* warn the user that we're going to close the window via a "Do you want to save changes" dialog */
-
- Boolean WarnOnWindowClose(WindowPtr window)
- {
- short item;
- WInfoPtr infoPtr;
- char hState;
- Boolean returnValue;
- WindowKind kind;
- Str63 documentKind,quitReason;
-
- // get document type for "Are you sure you want to close the drawing/letter xxx"
-
- kind = GetWindowKind(window);
- switch (kind) {
- case kDrawWindow:
- GetResString(documentKind,kDrawingID,kDrawing);
- break;
- case kDrawMailerWindow:
- GetResString(documentKind,kLetterID,kLetter);
- break;
- }
-
- // get either the "before quitting" or "before closing" string, depending on which we're doing
-
- if (gDone)
- GetResString(quitReason,kQuittingID,kQuitting);
- else
- GetResString(quitReason,kClosingID,kClosing);
-
- infoPtr = BeginWindowAccess(window,&hState);
- ParamText(documentKind,infoPtr->fileSpec.name,quitReason,nil);
- EndWindowAccess(window,hState);
-
- SetCursor(&qd.arrow);
- item = StopAlert(kWarnCloseID,nil);
-
- switch (item) {
- case cancel:
- returnValue = false; // if they cancel, return false to cancel the close
- break;
- case kDontSave:
- returnValue = true; // if they click don't save, return true to continue
- break;
- case ok:
- returnValue = CommSaveFile(window); // if they click yes, return true if the save works
- break;
- }
- return returnValue;
- }
-