home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-11 | 15.3 KB | 490 lines | [TEXT/KAHL] |
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // //
- // //
- // Copyright PennyWise Software, 1994. //
- // //
- // Part of the PennyWise Software Application Framework //
- // //
- // //
- // PictWindow.c Written by Peter Kaplan //
- // //
- // //
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- //
- // This is a template for a PennyWise Software Application Framework window
- //
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- #include "PWFramework.h"
- #include "PWWindowList.h"
- #include "WindowID.h"
- #include "PictWindow.h"
- #include <PWWindowUtils.h>
- #include "HandleMenus.h"
- #include <PWMenuUtils.h>
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- PicHandle GetOurPictHandle(FSSpecPtr theSpec);
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // These procedures are static. They will only be called by name from here
- // outside refrences will use our window proc list.
- static void ThisWindowCreate (EventRecord* theEvent, WindowPtr theWindow);
- static short ThisWindowDispose (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowZoomOut (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowResize (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowUpdate (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowActivate (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowIdle (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowCursor (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowPreMenu (EventRecord* theEvent, WindowPtr theWindow);
- static void ThisWindowPostMenu (EventRecord* theEvent, WindowPtr theWindow);
- static short ThisWindowDoMenu (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID);
- static void ThisWindowGrowRect (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect);
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // This record holds all the information about this window
- // You can add fields to this record.
- // NOTE:ALL WINDOW RECORDS MUST START WITH THIS
- // HEADER OR ELSE THE FRAMEWORK WILL NOT
- // FUNCTION PROPERLY. YOU'VE BEEN WARNED!
- typedef struct OurWinRecord {
- WindowParamHeader theHeader;
- short isDirty;
- PicHandle thePic;
- }OurWinRecord, *OurWinPtr, **OurWinHandle;
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // Macros for accessing the header data
- // you can add your own as you add fields
- // to the OurWinRecord.
- // NOTE:THESE MACROS ASSUME theData HOLDS
- // A VALID COPY OF OurWinHandle.
- #define THE_ID (*theData)->theHeader.theID
- #define IS_DIRTY (*theData)->isDirty
- #define THE_PIC (*theData)->thePic
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- #define PictWindow_RES_ID 128
- //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- void InitPictWindowHandlers()
- {
- PWInstallWindowType (kWINDOW_ID_PictWindow, kWINDOW_TYPE_APPLICATION);
- PWInstallCreate (kWINDOW_ID_PictWindow, ThisWindowCreate);
- PWInstallDispose (kWINDOW_ID_PictWindow, ThisWindowDispose);
- PWInstallZoomIn (kWINDOW_ID_PictWindow, ThisWindowIdle);
- PWInstallZoomOut (kWINDOW_ID_PictWindow, ThisWindowZoomOut);
- PWInstallResize (kWINDOW_ID_PictWindow, ThisWindowResize);
- PWInstallClick (kWINDOW_ID_PictWindow, ThisWindowIdle);
- PWInstallUpdate (kWINDOW_ID_PictWindow, ThisWindowUpdate);
- PWInstallActivate (kWINDOW_ID_PictWindow, ThisWindowActivate);
- PWInstallDeactivate (kWINDOW_ID_PictWindow, ThisWindowDeactivate);
- PWInstallIdle (kWINDOW_ID_PictWindow, ThisWindowIdle);
- PWInstallCursor (kWINDOW_ID_PictWindow, ThisWindowCursor);
- PWInstallKeyDown (kWINDOW_ID_PictWindow, ThisWindowIdle);
- PWInstallDrag (kWINDOW_ID_PictWindow, ThisWindowIdle);
- PWInstallPreMenu (kWINDOW_ID_PictWindow, ThisWindowPreMenu);
- PWInstallMenu (kWINDOW_ID_PictWindow, ThisWindowDoMenu);
- PWInstallPostMenu (kWINDOW_ID_PictWindow, ThisWindowPostMenu);
- PWInstallGrowRect (kWINDOW_ID_PictWindow, ThisWindowGrowRect);
- PWInstallBackground (kWINDOW_ID_PictWindow, ThisWindowIdle);
- PWInstallScrap2Appl (kWINDOW_ID_PictWindow, ThisWindowIdle);
- PWInstallAppl2Scrap (kWINDOW_ID_PictWindow, ThisWindowIdle);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowCreate (EventRecord* theEvent, WindowPtr theWindow)
- {
- // This routine will very rarely contain anything worthwhile
- // You will make custom routines for most windows that will be exported
- // via the include file
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // Since we will be exporting this we don't have to conform to any particular input format
- void PictWindowOpen(FSSpecPtr theSpec)
- {
- WindowPtr theWindow;
- OurWinHandle theData;
- PicHandle thePic;
- Rect pictRect;
-
- //SetRect(&windowBounds, 40, 40, 200,200);
- theWindow = GetNewWindow(PictWindow_RES_ID, NULL, (WindowPtr)-1);
- if (theWindow) {
- // We sucessfully got the window
-
- // Now we have to allocate our storage
- theData = (OurWinHandle) NewHandle(sizeof(OurWinRecord));
- if (theData) {
- // We sucessfully allocated storage
-
- // So Lets set the port to our new window
- SetPort(theWindow);
-
- // Here we would do any screen/size manipulations
- // before we show the window
- thePic = GetOurPictHandle( theSpec);
- if (thePic) {
- pictRect = (*thePic)->picFrame;
-
- // We will not get update events now
- SetWTitle(theWindow, theSpec->name);
-
- // Here we would allocate any other
- // objects that we need. (Controls, etc.)
-
- // Now lets set the fields
- THE_ID = kWINDOW_ID_PictWindow;
- IS_DIRTY = FALSE;
- THE_PIC = thePic;
-
- // Install our routines
- SetWRefCon(theWindow, (long) theData);
-
- // Show it & select it before we leave
- ShowWindow(theWindow);
- SelectWindow(theWindow);
- }
- else {
- // We could not allocate memory for our window's data
- // So lets get rid of the data
- DisposeWindow(theWindow);
- theWindow = NULL;
- DisposeHandle((Handle)theData);
- }
- }
- else {
- // We could not allocate memory for our window's data
- // So lets get rid of the data
- DisposeWindow(theWindow);
- theWindow = NULL;
- }
- }
-
- if (!theWindow) {
- SysBeep(1);
- // We did not create the window
- // You will probably want to put up a dialog here to explain why
- }
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static short ThisWindowDispose (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- short theResults;
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- // Want to save it
- theResults = TRUE;
-
- if (IS_DIRTY) {
- // Data has been changed we would probably want to put up
- // a “Save Changes to x” dialog here
- // But I'm just going to reset IS_DIRTY
- IS_DIRTY = FALSE;
- }
-
- // Do whatever saving you need to do here
-
- // Now lets break it down
- HideWindow(theWindow);
- DisposeHandle((Handle)THE_PIC);
- DisposeHandle((Handle)theData);
- DisposeWindow(theWindow);
-
- return theResults; // True if we closed it, false if we did not [ex. pressed cancel in save dialog]
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowZoomOut (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- Rect theRect;
- WStateData *wsdp;
-
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- // This makes the window only zoom out to the "Page Setup" size
- // This will change the size depending on the Print, Etc.
-
- // De-ref the data--make sure we don't move memory
- wsdp = (WStateData*) *(((WindowPeek)theWindow)->dataHandle);
-
- theRect = (*THE_PIC)->picFrame;
- wsdp->stdState.right = wsdp->stdState.left + (theRect.right- theRect.left);
- wsdp->stdState.bottom = wsdp->stdState.top + (theRect.bottom- theRect.top);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowResize (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- EraseRect(&theWindow->portRect);
-
- // Do any control moving here
- //•••••••••••••••••••••••••••
-
- InvalRect(&theWindow->portRect);
-
- SetPort(oldPort);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowUpdate (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- GrafPtr oldPort;
- Rect theRect;
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- // Draw the window here
- //•••••••••••••••••••••••••••
- DrawGrowIcon(theWindow);
- theRect = theWindow->portRect;
- theRect.right-=16;
- theRect.bottom-=16;
- DrawPicture(THE_PIC, &theRect);
-
- SetPort(oldPort);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowActivate (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- GrafPtr oldPort;
-
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- // Activate items here
- //•••••••••••••••••••••••••••
- DrawGrowIcon(theWindow);
-
- SetPort(oldPort);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- // Deactivate items here
- //•••••••••••••••••••••••••••
- DrawGrowIcon(theWindow);
-
- SetPort(oldPort);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowIdle (EventRecord* theEvent, WindowPtr theWindow)
- {
-
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowCursor (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- GrafPtr oldPort;
- CursHandle hCurs;
- Rect controlRect;
- Point localPoint;
- RgnHandle theRgn;
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- theRgn = NewRgn();
- if (theRgn) {
- // Adjust the cursor here
- if (PtInRgn( theEvent->where, ((WindowPeek)theWindow)->contRgn)) {
-
- // OK the mouse is in the window lets find out where
-
- localPoint = theEvent->where;
- GlobalToLocal(&localPoint);
-
- // Make a box the size of viewRect
- controlRect = theWindow->portRect;
- controlRect.right-=16;
- controlRect.bottom-=16;
-
- if (PtInRect(localPoint,&controlRect)) {
- // OK its in the viewRect area make the cursor an iBeam
- LocalToGlobalRect(&controlRect);
-
- // Set the mouse moved to be the box we are in
- RectRgn(gMouseMovedRgn,&controlRect);
- hCurs = GetCursor(crossCursor);
- HLock((Handle)hCurs);
- SetCursor(*hCurs);
- }
- else {
- controlRect = theWindow->portRect;
- controlRect.top = controlRect.bottom -16;
-
- LocalToGlobalRect(&controlRect);
- RectRgn(theRgn,&controlRect);
-
- controlRect = theWindow->portRect;
- controlRect.top = controlRect.right -16;
- LocalToGlobalRect(&controlRect);
- RectRgn(gMouseMovedRgn,&controlRect);
-
- UnionRgn(theRgn,gMouseMovedRgn,gMouseMovedRgn);
- SetCursor(&arrow);
- }
- }
- else {
- SetRectRgn(gMouseMovedRgn,-32768,-32768,32767,32767);
- DiffRgn(gMouseMovedRgn,((WindowPeek)theWindow)->contRgn,gMouseMovedRgn);
- SetCursor(&arrow);
- }
-
- DisposeRgn(theRgn);
- }
-
-
- SetPort(oldPort);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowPreMenu (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- GrafPtr oldPort;
- Str63 theString;
- Boolean quotes;
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- // Enable and disable items
- // change item names etc.
-
- // When we inable undo, we can add this
- DisableItem( gMenuEdit, kEDIT_UNDO);
- DisableItem( gMenuEdit, kEDIT_CUT);
- DisableItem( gMenuEdit, kEDIT_COPY);
- DisableItem( gMenuEdit, kEDIT_PASTE);
- DisableItem( gMenuEdit, kEDIT_CLEAR);
- DisableItem( gMenuEdit, kEDIT_SEL_ALL);
-
- // Lets handle the file name stuff
- DisableItem( gMenuFile, kFILE_SAVE);
- DisableItem( gMenuFile, kFILE_SAVEAS);
- DisableItem( gMenuFile, kFILE_PRINT);
- DisableItem( gMenuFile, kFILE_PONE);
-
- GetWTitle(theWindow, theString);
- AddNameToMenu( gMenuFile, kFILE_CLOSE, theString, FALSE, TRUE);
-
- SetPort(oldPort);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowPostMenu (EventRecord* theEvent, WindowPtr theWindow)
- {
- OurWinHandle theData;
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- theData = (OurWinHandle) GetWRefCon(theWindow);
-
- // Enable and disable items
- // change item names etc.
- EnableItem( gMenuEdit, kEDIT_UNDO);
- EnableItem( gMenuEdit, kEDIT_CUT);
- EnableItem( gMenuEdit, kEDIT_COPY);
- EnableItem( gMenuEdit, kEDIT_PASTE);
- EnableItem( gMenuEdit, kEDIT_CLEAR);
- EnableItem( gMenuEdit, kEDIT_SEL_ALL);
-
- // Lets handle the file name stuff
- EnableItem( gMenuFile, kFILE_SAVE);
- EnableItem( gMenuFile, kFILE_SAVEAS);
- EnableItem( gMenuFile, kFILE_PRINT);
- EnableItem( gMenuFile, kFILE_PONE);
-
- SetMenuItemToIndString(kMENU_ID_FILE, kFILE_CLOSE, gMenuFile);
-
- SetPort(oldPort);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static short ThisWindowDoMenu (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID)
- {
- return FALSE;
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- static void ThisWindowGrowRect (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect)
- {
- // This simply is the size of the rect the window can grow to
- SetRect(theRect, 64, 64, 32767, 32767);
- }
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- PicHandle GetOurPictHandle(FSSpecPtr theSpec)
- {
- Handle thePic;
- OSErr theErr;
- short fRefNum;
- long curEOF;
-
- thePic = NULL;
-
- theErr = FSpOpenDF(theSpec,fsRdPerm,&fRefNum);
- if (theErr == noErr) {
- // The file opened ok
-
- // Get the EOF
- theErr = GetEOF(fRefNum,&curEOF);
- if (theErr == noErr) {
- // got the eof ok
-
- // Set the file pos
- theErr = SetFPos(fRefNum,fsFromStart,512);
- if (theErr == noErr) {
- // Set the file pos OK
-
- // advance past the header
- curEOF-=512;
- if (curEOF>0) {
- // We still have pict :)
-
-
- thePic = NewHandle(curEOF);
- if (thePic) {
- HLock(thePic);
-
- theErr = FSRead(fRefNum,&curEOF,*thePic);
- if (theErr == noErr) {
- // All is OK
- }
- else {
- DisposeHandle(thePic);
- thePic = NULL;
- }
- HUnlock(thePic);
- }
- }
- }
- }
- }
- return (PicHandle)thePic;
- }