home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-11 | 5.8 KB | 278 lines | [TEXT/CWIE] |
- // CModalDialog.cp
- // Copyright © 1995 by Michael F. Kamprath, All rights reserved.
- //
- // Contact information:
- // mailto:kamprat@leonardo.net
- // http://www.leonardo.net/kamprath
- //
- // License:
- // This code may be freely used in free and shareware products. Commercial product
- // users must supply me with a free, fully licensed copy of the product this code is
- // used in. In either case, users should notify me of their use of this code.
- //
- // This code may be freely distributed in it's non-modified form. It's original archive
- // should be kept intact.
- //
- // Version History:
- // v1.0.0 - Inititial release.
- //
-
- #include "CModalDialog.h"
-
- pascal Boolean CModalDialogFilter( DialogPtr theDlg,
- EventRecord *theEvent,
- short *itemHit);
-
- CModalDialog::CModalDialog( short resID,
- short defaultID,
- short cancelID )
- : dlogResID( resID ),
- defaultItem( defaultID ),
- cancelItem( cancelID ),
- showHasBeenCalled( false )
- {
- this->dialogFilterUPP = NewModalFilterProc( CModalDialogFilter );
- this->theDialog = ::GetNewDialog(resID, nil, (WindowPtr)-1L );
- SetWRefCon( this->theDialog, (long)this );
- }
-
- CModalDialog::~CModalDialog()
- {
- DisposeRoutineDescriptor((UniversalProcPtr)this->dialogFilterUPP);
- ::DisposeDialog( this->theDialog );
- }
-
- void
- CModalDialog::SetDialogPort( void )
- {
- ::SetPort( this->theDialog );
- }
- void
- CModalDialog::SetDefaultItem( short newVal )
- {
- this->InvalidateDefaultRect();
- this->defaultItem = newVal;
- this->InvalidateDefaultRect();
- }
- void
- CModalDialog::SetCancelItem( short newVal )
- {
- this->cancelItem = newVal;
- }
-
- void
- CModalDialog::GetItemInfo( short item, short* itemType, Handle* itemHandle, Rect* itemRect )
- {
- ::GetDialogItem(this->theDialog, item,itemType, itemHandle,itemRect);
- }
- void
- CModalDialog::SetItemInfo( short item, short itemType, Handle itemHandle, Rect* itemRect )
- {
- ::SetDialogItem(this->theDialog, item,itemType, itemHandle,itemRect);
- }
-
- void
- CModalDialog::FrameDefaultButton(void)
- {
- GrafPtr savePort;
- PenState thePnState;
- Rect defaultRect;
-
- this->GetDefaultRect( defaultRect );
-
- ::GetPort(&savePort);
- ::SetPort(this->theDialog);
- ::GetPenState( &thePnState );
-
- ::PenSize(3,3);
-
- ::FrameRoundRect(&defaultRect, 18, 18);
-
- ::SetPenState( &thePnState );
- ::SetPort(savePort);
- }
-
- void
- CModalDialog::GetDefaultRect( Rect& theRect )
- {
- short itemType;
- Rect itemRect;
- Handle itemHandle;
-
- ::GetDialogItem( this->theDialog, this->defaultItem, &itemType, &itemHandle, &itemRect);
-
- itemRect.top -= 4;
- itemRect.left -= 4;
- itemRect.right += 4;
- itemRect.bottom += 4;
-
- theRect = itemRect;
- }
- void
- CModalDialog::InvalidateDefaultRect(void)
- {
- Rect defaultRect;
- GrafPtr savePort;
-
- this->GetDefaultRect( defaultRect );
- ::GetPort(&savePort);
- ::SetPort(this->theDialog);
- ::InvalRect(&defaultRect);
- ::SetPort(savePort);
- }
-
- void
- CModalDialog::FakeDefaultButtonPress( void )
- {
- short itemType;
- Rect itemRect;
- Handle itemHandle;
- long ticks = ::TickCount();
-
- ::GetDialogItem( this->theDialog, this->defaultItem, &itemType, &itemHandle, &itemRect);
-
- ::HiliteControl( (ControlHandle)itemHandle, 1 );
- while (ticks + 10 > ::TickCount() )
- ;
- ::HiliteControl( (ControlHandle)itemHandle, 0 );
- }
-
- void
- CModalDialog::FakeCancelButtonPress( void )
- {
- short itemType;
- Rect itemRect;
- Handle itemHandle;
- long ticks = ::TickCount();
-
- ::GetDialogItem( this->theDialog, this->cancelItem, &itemType, &itemHandle, &itemRect);
-
- ::HiliteControl( (ControlHandle)itemHandle, 1 );
- while (ticks + 10 > ::TickCount() )
- ;
- ::HiliteControl( (ControlHandle)itemHandle, 0 );
- }
-
- void
- CModalDialog::Show( void )
- {
- ::ShowWindow(this->theDialog);
- ::SelectWindow(this->theDialog);
-
- this->showHasBeenCalled = true;
- }
- void
- CModalDialog::Hide( void )
- {
- ::HideWindow(this->theDialog);
- }
-
- short
- CModalDialog::DoOneModalLoop( void )
- {
- short itemHit;
-
- if (!this->showHasBeenCalled)
- this->Show();
- ::ModalDialog(this->dialogFilterUPP, &itemHit);
-
- return itemHit;
- }
-
- //
- // This is the dialog filter stuff.
- //
-
- Boolean
- CModalDialog::UpdateFilter( DialogPtr theDlg, EventRecord *theEvent, short *itemHit)
- {
- GrafPtr savePort;
- Boolean returnCode = false;
-
- if ( (WindowPtr)(theEvent->message) == theDlg )
- {
- GetPort(&savePort);
- SetPort(theDlg);
- BeginUpdate(theDlg);
-
- this->DrawDialog();
-
- EndUpdate(theDlg);
- SetPort(savePort);
-
- *itemHit = 0;
- returnCode = true;
- }
- return returnCode;
- }
-
- void
- CModalDialog::DrawDialog( void )
- {
- ::UpdateDialog(this->theDialog,this->theDialog->visRgn);
-
- this->FrameDefaultButton();
- }
-
- Boolean
- CModalDialog::KeyDownFilter( DialogPtr /* theDlg */, EventRecord *theEvent, short *itemHit)
- {
- Boolean returnCode = false;
- char key = theEvent->message&charCodeMask;
-
- if (( key == 0x0D )||( key == 0x03))
- // return or enter key
- {
- this->FakeDefaultButtonPress();
- *itemHit = this->defaultItem;
- returnCode = true;
- }
- else if ( ( key == 0x1B )
- || (( key == '.')&&(theEvent->modifiers&cmdKey)) )
- // cancel or command period
- {
- this->FakeCancelButtonPress();
- *itemHit = this->cancelItem;
- returnCode = true;
- }
- return returnCode;
- }
- Boolean
- CModalDialog::MouseDownFilter( DialogPtr, EventRecord*, short* )
- {
- return false;
- }
- Boolean
- CModalDialog::OtherFilter( DialogPtr, EventRecord*, short* )
- {
- return false;
- }
-
- pascal Boolean CModalDialogFilter( DialogPtr theDlg, EventRecord *theEvent, short *itemHit)
- {
- CModalDialog *theThis;
- Boolean returnCode;
-
- theThis = (CModalDialog*)GetWRefCon(theDlg);
-
- if (theDlg != theThis->theDialog)
- return false;
-
- switch ( theEvent->what )
- {
- case updateEvt:
- returnCode = theThis->UpdateFilter( theDlg, theEvent, itemHit );
- break;
- case keyDown:
- returnCode = theThis->KeyDownFilter( theDlg, theEvent, itemHit );
- break;
- case mouseDown:
- returnCode = theThis->MouseDownFilter( theDlg, theEvent, itemHit );
- break;
- default:
- returnCode = theThis->OtherFilter( theDlg, theEvent, itemHit );
- break;
- }
- return (returnCode);
- }
-