home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-15 | 8.8 KB | 431 lines | [TEXT/CWIE] |
- /*
-
- File: SampleWindow.cp
- Project: Sample code for Sprocket Framework 1.1 (DR2), released 6/15/96
- Contains: A drag manager-aware window designed to hold PICTs
- Designed to show you how easy it is to subclass TWindow
- To Do: ?
-
-
- Sprocket Major Contributors:
- ----------------------------
- Dave Falkenburg, producer of Sprocket 1.0
- Bill Hayden, producer of Sprocket 1.1
- Steve Sisak, producer of the upcoming Sprocket 2.0
-
- Pete Alexander Steve Falkenburg Randy Thelen
- Eric Berdahl Nitin Ganatra Chris K. Thomas
- Marshall Clow Dave Hershey Leonard Rosenthal
- Tim Craycroft Dave Mark Dean Yu
- David denBoer Gary Powell
- Cameron Esfahani Jon Summers Apple Computer, Inc.
-
- Comments, Additions, or Corrections:
- ------------------------------------
- Bill Hayden, Nikol Software <nikol@codewell.com>
-
- */
-
-
-
- const short kPictureWindowTemplateID = 2001;
- const short kDefaultPICTResID = 1025;
-
-
- #include "SampleWindow.h"
- #include "SprocketMacros.h"
- #include <ToolUtils.h>
-
-
- MenuRef TSampleWindow::fgMenu;
- unsigned long TSampleWindow::fgWindowTitleCount = 0;
-
- extern TMenuBar* gMenuBar;
-
- // methods
-
- TSampleWindow::TSampleWindow()
- {
- fDraggedPicHandle = nil;
- fCenter = true;
-
- SetUpStaticMenu(); // CreateWindow will call Activate(), so we must do this first
-
- TSampleWindow::fgWindowTitleCount++; // Starts out as zero but we want “Picture Window 1”
- this->CreateWindow(); // kNormalWindow is default. Method inherited from TWindow
- // TWindow::CreateWindow() calls our MakeNewWindow().
-
- ShowWindow(fWindow);
- }
-
-
- TSampleWindow::~TSampleWindow()
- {
- // No particular cleanup in this version, since our real purpose was
- // to demonstrate the Drag Manager, not to build a picture handling app.
- }
-
-
- WindowRef TSampleWindow::MakeNewWindow( WindowRef behindWindow )
- {
- WindowRef aWindow;
- Str255 titleString;
- GrafPtr savedPort;
-
- GetPort(&savedPort);
-
- aWindow = GetNewColorOrBlackAndWhiteWindow( kPictureWindowTemplateID, nil, behindWindow );
-
- if (aWindow)
- {
- GetWTitle(aWindow,titleString);
- if (StrLength(titleString) != 0)
- {
- Str255 numberString;
-
- NumToString( fgWindowTitleCount, numberString );
- BlockMoveData(&numberString[1], &titleString[titleString[0]+1], numberString[0]);
- titleString[0] += numberString[0];
- }
- SetWTitle(aWindow,titleString);
-
- SetPortWindowPort(aWindow);
-
- ShowWindow(aWindow);
- }
- SetPort(savedPort);
-
- return aWindow;
- }
-
-
-
-
- void TSampleWindow::Draw(void)
- {
- PicHandle pic;
- Rect r;
-
- SetPortWindowPort(fWindow);
-
- r = GetWindowPort(fWindow)->portRect;
- EraseRect( &r );
-
- if ( fDraggedPicHandle == nil )
- pic = this->LoadDefaultPicture();
- else
- pic = fDraggedPicHandle;
-
- if (fCenter)
- this->CenterPict( pic, &r );
- else
- SetRect(&r, 0, 0, (*pic)->picFrame.right, (*pic)->picFrame.bottom);
-
- DrawPicture( pic, &r );
- }
-
-
-
-
- void TSampleWindow::Activate( Boolean activating )
- {
- if ( activating )
- InsertMenu( fgMenu, 0 );
- else
- DeleteMenu( mPicture );
-
- gMenuBar->Invalidate();
- }
-
-
-
-
- void TSampleWindow::ClickAndDrag( EventRecord *eventPtr )
- {
- OSErr err;
- DragReference dragRef;
- RgnHandle dragRegion, tempRgn;
- Rect itemBounds;
- Handle flavorDataHandle;
-
-
- SetPortWindowPort(fWindow);
-
- err = NewDrag( &dragRef );
- if ( err != noErr )
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
-
- if ( fDraggedPicHandle == nil )
- flavorDataHandle = (Handle)this->LoadDefaultPicture();
- else
- flavorDataHandle = (Handle)fDraggedPicHandle;
-
- HLock( flavorDataHandle );
-
- err = AddDragItemFlavor( dragRef,
- (ItemReference)fWindow,// no particular reason, could use any number here
- (FlavorType) 'PICT',
- (Ptr)*flavorDataHandle,
- GetHandleSize( (Handle)flavorDataHandle ),
- (FlavorFlags)0 );
-
- HUnlock( flavorDataHandle );
-
- if ( err != noErr )
- {
- DisposeDrag( dragRef );
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
- }
-
- itemBounds = this->GetContentsBounds();
-
- GlobalToLocal((Point*) &itemBounds.top);
- GlobalToLocal((Point*) &itemBounds.bottom);
-
- err = SetDragItemBounds( dragRef, (ItemReference)fWindow, &itemBounds );
- if ( err != noErr )
- {
- DisposeDrag( dragRef );
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
- }
-
- dragRegion = NewRgn();
- RectRgn( dragRegion, &itemBounds );
- tempRgn = NewRgn();
- CopyRgn( dragRegion, tempRgn );
- InsetRgn( tempRgn, 1, 1 );
- DiffRgn( dragRegion, tempRgn, dragRegion );
- DisposeRgn( tempRgn );
-
- err = TrackDrag( dragRef, eventPtr, dragRegion );
- DisposeRgn( dragRegion );
- DisposeDrag( dragRef );
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
- }
-
-
-
-
- OSErr TSampleWindow::DragEnterWindow( DragReference dragRef )
- {
- fCanAcceptDrag = IsPictFlavorAvailable( dragRef );
- fIsWindowHighlighted = false;
-
- if ( fCanAcceptDrag )
- return noErr;
- else
- return dragNotAcceptedErr;
- }
-
-
-
-
- OSErr TSampleWindow::DragInWindow( DragReference dragRef )
- {
- DragAttributes attributes;
- RgnHandle tempRgn;
-
-
- SetPortWindowPort(fWindow);
-
- GetDragAttributes( dragRef, &attributes );
-
- if ( (! fCanAcceptDrag) || (! (attributes & dragHasLeftSenderWindow))
- || (attributes & dragInsideSenderWindow) )
- return dragNotAcceptedErr;
-
- if ( this->IsMouseInContentRgn( dragRef ) )
- {
- if ( ! fIsWindowHighlighted )
- {
- tempRgn = NewRgn();
- RectRgn( tempRgn, &GetWindowPort(fWindow)->portRect );
-
- if ( ShowDragHilite( dragRef, tempRgn, true ) == noErr )
- fIsWindowHighlighted = true;
-
- DisposeRgn(tempRgn);
- }
- }
-
- return noErr;
- }
-
-
-
-
- OSErr TSampleWindow::DragLeaveWindow( DragReference dragRef )
- {
- if ( fIsWindowHighlighted )
- HideDragHilite( dragRef );
-
- fIsWindowHighlighted = false;
- fCanAcceptDrag = false;
-
- return noErr;
- }
-
-
-
-
- OSErr TSampleWindow::HandleDrop( DragReference dragRef )
- {
- OSErr err;
- Size dataSize;
- ItemReference item;
- FlavorFlags flags;
- DragAttributes attributes;
-
- GetDragAttributes( dragRef, &attributes );
-
- if ( attributes & dragInsideSenderWindow )
- return dragNotAcceptedErr;
-
- err = GetDragItemReferenceNumber( dragRef, 1, &item );
- if ( err == noErr )
- err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
-
- if ( err == noErr )
- {
- err = GetFlavorDataSize( dragRef, item, 'PICT', &dataSize);
- if (err == noErr )
- {
- fDraggedPicHandle = (PicHandle)TempNewHandle( dataSize, &err );
-
- if ( fDraggedPicHandle == nil )
- fDraggedPicHandle = (PicHandle)NewHandle( dataSize );
-
- if ( fDraggedPicHandle == nil )
- err = dragNotAcceptedErr;
- else
- {
- HLock( (Handle)fDraggedPicHandle );
- err = GetFlavorData( dragRef, item, 'PICT',
- *fDraggedPicHandle, &dataSize, 0L );
- HUnlock( (Handle)fDraggedPicHandle );
-
- if ( err != noErr)
- {
- err = dragNotAcceptedErr;
- DisposeHandle( (Handle)fDraggedPicHandle );
- fDraggedPicHandle = nil;
- }
- else
- {
- SetPortWindowPort( fWindow );
- InvalRect( &(GetWindowPort(fWindow)->portRect) );
- }
- }
- }
- }
-
- return( err );
- }
-
-
-
-
- PicHandle TSampleWindow::LoadDefaultPicture()
- {
- PicHandle pic;
-
- pic = GetPicture( kDefaultPICTResID );
-
- if ( pic == nil )
- {
- DebugMessage( "\pCould not load PICT resource!" );
- return (PicHandle)nil;
- }
- else
- return( pic );
- }
-
-
-
-
- void TSampleWindow::CenterPict( PicHandle picture, Rect *destRectPtr )
- {
- Rect windRect, pictRect;
-
- windRect = *destRectPtr;
- pictRect = (**( picture )).picFrame;
- OffsetRect( &pictRect, windRect.left - pictRect.left,
- windRect.top - pictRect.top);
- OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
- (windRect.bottom - pictRect.bottom)/2);
- *destRectPtr = pictRect;
- }
-
-
-
-
- Boolean TSampleWindow::IsPictFlavorAvailable( DragReference dragRef )
- {
- unsigned short numItems;
- FlavorFlags flags;
- OSErr err;
- ItemReference item;
-
- CountDragItems( dragRef, &numItems );
-
- if ( numItems < 1 )
- return( false );
-
- err = GetDragItemReferenceNumber( dragRef, 1, &item );
- if ( err == noErr )
- err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
-
- return( err == noErr );
- }
-
-
- void TSampleWindow::SetUpStaticMenu( void )
- {
- TSampleWindow::fgMenu = gMenuBar->GetMenuFromCMNU( mPicture );
- }
-
-
-
-
- /**********************************************************************************/
-
-
-
-
- void TSampleWindow::AdjustMenusBeforeMenuSelection(void)
- {
- gMenuBar->EnableAndCheckCommand( cCentered, true, fCenter );
- gMenuBar->EnableAndCheckCommand( cUpperLeft, true, !fCenter );
- }
-
-
-
-
- /**********************************************************************************/
-
-
-
-
- Boolean TSampleWindow::DoCommand(CommandID theCommand)
- {
- switch(theCommand)
- {
- case cCentered:
- fCenter = true;
- break;
-
- case cUpperLeft:
- fCenter = false;
- break;
-
- default:
- return TWindow::DoCommand(theCommand);
- break;
- }
-
- this->Draw();
-
- return true;
- }
-
-