home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-19 | 10.0 KB | 470 lines | [TEXT/CWIE] |
- //Copyright (c) 1997 Aidan Cully
- //All rights reserved
-
- #include "CLLayout.h"
- #include "CLMouseHandler.h"
- #include "CLBaseWindow.h"
-
- CursPtr TLayoutLeaf::sPresCurs = 0l;
-
- TLayoutLeaf::TLayoutLeaf( TLayoutBranch *newParent ):
- mParent( newParent ),
- mWindow( 0 )
- {
- }
-
- TLayoutLeaf::~TLayoutLeaf() {
- if( mParent )
- mParent->RemoveChild( this );
- }
-
- TLayoutBranch::TLayoutBranch( TLayoutBranch *newParent ):
- TLayoutLeaf( newParent ),
- mActiveChild( 0l )
- {
- }
-
- TLayoutBranch::~TLayoutBranch() {
- TChildStruct *child;
- UInt32 temp;
-
- if( mChildren.MoveFirst() ) {
- do {
- mChildren.GetData( child );
- child->mChild->SetParent( 0l );
- } while( mChildren.MoveNext() );
- }
- }
-
- void TLayoutLeaf::AttachedToWindow( TBaseWindow *win, Rect contRect )
- {
- mWindow= win;
- mContentRect= contRect;
- }
-
- void TLayoutBranch::AttachedToWindow( TBaseWindow *win, Rect contRect )
- {
- TLayoutLeaf::AttachedToWindow( win, contRect );
- BuildChildren();
- }
-
- Boolean TLayoutLeaf::SetParent( TLayoutBranch *newParent )
- {
- if( mParent )
- mParent->RemoveChild( this );
- mParent = newParent;
- return( true );
- }
-
- Boolean TLayoutBranch::AddChild( TLayoutLeaf *newChild, Rect contRect, Boolean *binding )
- {
- TChildStruct *child;
- bool found= false;
-
- if( mChildren.MoveFirst() ) {
- UINT32 ch;
- do {
- mChildren.GetData( child );
- if( child->mChild==newChild )
- found= true;
- } while( (!found)&&mChildren.MoveNext() );
- }
- if( found ) {
- for( int i= 0; i<4; i++ )
- child->mBinds[i]= binding[i];
- return( true );
- }
- mChildren.MoveLast();
- child= new TChildStruct;
- for( int i= 0; i<4; i++ )
- child->mBinds[i]= binding[i];
- child->mChild= newChild;
- if( !mChildren.AddNext( child ) )
- return( false );
- child->mChild->AttachedToWindow( mWindow, contRect );
- if( ::EmptyRect( &(newChild->GetRect()) ) )
- AcceptNewSubSizeRequest( newChild->GetRect(), newChild );
- return( true );
- }
-
- Boolean TLayoutBranch::RemoveChild( TLayoutLeaf *deadChild )
- {
- TChildStruct *child;
- if( !mChildren.MoveFirst() )
- return( true );
- bool found= false;
- UINT32 ch;
- do {
- mChildren.GetData( child );
- if( child->mChild==deadChild )
- found= true;
- } while( !found&&mChildren.MoveNext() );
- if( !found )
- return( true );
- mChildren.Remove();
- delete child;
- if( mChildren.MoveFirst() )
- TrySize( mContentRect );
- return( true );
- }
-
- Boolean TLayoutBranch::AcceptNewSubSizeRequest( Rect newSubRect, TLayoutLeaf *sub )
- {
- RgnHandle theRgn, subRgn;
-
- TChildStruct *child;
- if( !mChildren.MoveFirst() )
- return( false );
- bool found= false;
- UINT32 ch;
- do {
- mChildren.GetData( child );
- if( child->mChild==sub )
- found= true;
- } while( (!found)&&mChildren.MoveNext() );
- if( !found )
- return( false );
- theRgn = ::NewRgn();
- ::RectRgn( theRgn, &mContentRect );
- subRgn = ::NewRgn();
- ::RectRgn( subRgn, &newSubRect );
-
- UInt32 temp;
- mChildren.MoveFirst();
- do {
- mChildren.GetData( child );
- if( child->mChild != sub ) {
- }
- } while( mChildren.MoveNext() );
- ::DisposeRgn( theRgn );
- ::DisposeRgn( subRgn );
- return( true );
- }
-
- Boolean TLayoutLeaf::Init()
- {
- return( true );
- }
-
- Boolean TLayoutLeaf::Close()
- {
- if( mParent )
- return( mParent->RemoveChild( this ) );
- return( true );
- }
-
- Boolean TLayoutBranch::Close()
- {
- if( !TLayoutLeaf::Close() )
- return( false );
- TChildStruct *child;
- UInt32 temp;
- if( mChildren.MoveFirst() )
- do {
- mChildren.GetData( child );
- child->mChild->Close();
- mChildren.Remove();
- delete child->mChild;
- delete child;
- } while( mChildren.MoveFirst() );
- return( true );
- }
-
- Boolean TLayoutLeaf::TrySize( Rect theSize ) {
- mContentRect = theSize;
- return( true );
- }
-
- Boolean TLayoutBranch::TrySize( Rect theSize )
- {
- TChildStruct *child;
- UInt32 tempInt;
- Rect tempRect, tRect2;
- const Boolean *cBinding;
-
- if( mChildren.MoveFirst() ) {
- do {
- mChildren.GetData( child );
- tempRect = child->mChild->GetRect();
- tRect2 = tempRect;
- cBinding = child->mBinds;
- if( cBinding[right] ) {
- if( !cBinding[left] )
- OffsetRect( &tempRect, theSize.right-mContentRect.right,0 );
- else
- tempRect.right += theSize.right-mContentRect.right;
- }
- if( cBinding[bottom] ) {
- if( !cBinding[top] )
- OffsetRect( &tempRect, 0, theSize.bottom-mContentRect.bottom );
- else
- tempRect.bottom += theSize.bottom-mContentRect.bottom;
- }
- if( !EqualRect( &tempRect, &tRect2 ) )
- child->mChild->TrySize( tempRect );
- } while( mChildren.MoveNext() );
- }
- TLayoutLeaf::TrySize( theSize );
- }
-
- void TLayoutLeaf::HandleMouse( TMouseButtonEvent *ev )
- {
- mWindow->SelectWindow();
- mWindow->GlobalToLocal( &ev->where );
- mWindow->GetDrawFocus();
- HandleMouseSelf( ev );
- mWindow->ReleaseDrawFocus();
- }
-
- Boolean TLayoutLeaf::HandleMouseSelf( TMouseButtonEvent *ev )
- {
- return( false );
- }
-
- void TLayoutBranch::DrawSelf( TDrawSlate *gr )
- {
- TChildStruct *child;
- UInt32 temp;
- RgnHandle outRgn, tempRgn, drawRgn, oClip;
-
- outRgn = ::NewRgn();
- tempRgn = ::NewRgn();
- drawRgn= ::NewRgn();
- oClip= ::NewRgn();
- ::GetClip( oClip );
- if( mParent )
- mParent->CalcClip( this, drawRgn );
- else
- ::RectRgn( drawRgn, &mContentRect );
- if( mChildren.MoveFirst() )
- do {
- mChildren.GetData( child );
- ::RectRgn( tempRgn, &child->mChild->GetRect() );
- ::UnionRgn( outRgn, tempRgn, outRgn );
- ::SectRgn( tempRgn, drawRgn, tempRgn );
- ::SetClip( tempRgn );
- child->mChild->DrawSelf( gr );
- ::DiffRgn( drawRgn, tempRgn, drawRgn );
- } while( mChildren.MoveNext() );
- if( mParent )
- mParent->CalcClip( this, drawRgn );
- else
- ::RectRgn( tempRgn, &mContentRect );
- ::DiffRgn( tempRgn, outRgn, outRgn );
- ::SetClip( tempRgn );
- ::EraseRgn( outRgn );
- ::DisposeRgn( outRgn );
- ::DisposeRgn( tempRgn );
- ::DisposeRgn( drawRgn );
- ::SetClip( oClip );
- ::DisposeRgn( oClip );
- }
-
- void TLayoutLeaf::DrawSelf( TDrawSlate *gr )
- {
- ::EraseRect( &GetLocalRect() );
- }
-
- void TLayoutLeaf::Draw( TDrawSlate *root )
- {
- RgnHandle oldClip, newClip;
-
- // ::SetOrigin( -mContentRect.left, -mContentRect.top );
- newClip= ::NewRgn();
- if( root->GetDrawFocus() ) {
- oldClip = ::NewRgn();
- ::GetClip( oldClip );
- if( mParent )
- mParent->CalcClip( this, newClip );
- else
- ::RectRgn( newClip, &GetLocalRect() );
- ::SetClip( newClip );
- DrawSelf( root );
- ::SetClip( oldClip );
- root->MarkChanged( GetRelativeRect() );
- root->ReleaseDrawFocus();
- ::DisposeRgn( newClip );
- }
- // ::SetOrigin( 0, 0 );
- }
-
- Rect TLayoutBranch::GetLargestSize()
- {
- UInt32 temp;
- TChildStruct *child;
- Rect retRect, tempRect;
-
- ::SetRect( &retRect, 0, 0, 0, 0 );
- if( mChildren.MoveFirst() ) {
- do {
- mChildren.GetData( child );
- if( child->mBinds[left] == true ) {
- tempRect = child->mChild->GetLargestSize();
- if( tempRect.right+child->mChild->GetRect().left > retRect.right )
- retRect.right = tempRect.right+child->mChild->GetRect().left;
- }
- if( child->mBinds[top] == true ) {
- tempRect = child->mChild->GetLargestSize();
- if( tempRect.bottom+child->mChild->GetRect().top > retRect.bottom )
- retRect.bottom = tempRect.bottom+child->mChild->GetRect().top;
- }
- } while( mChildren.MoveNext() );
- }
- return( retRect );
- }
-
- Rect TLayoutLeaf::GetLocalRect()
- {
- Rect retRect = GetRect();
-
- // OffsetRect( &retRect, -retRect.left, -retRect.top );
- return( retRect );
- }
-
- Rect TLayoutLeaf::GetRelativeRect()
- {
- return( GetRect() );
- }
-
- Rect TLayoutLeaf::GetRect()
- {
- return( mContentRect );
- }
-
- Boolean TLayoutLeaf::MakeActive( Boolean active )
- {
- return( true );
- }
-
- Boolean TLayoutBranch::MakeActive( Boolean active )
- {
- if( mActiveChild )
- return( mActiveChild->MakeActive( active ) );
- else
- return( false );
- }
-
- Boolean TLayoutBranch::SetActiveChild( TLayoutLeaf *which )
- {
- UInt32 childNum;
- TChildStruct *child;
-
- if( (!which) && mActiveChild ) {
- which= mActiveChild;
- mActiveChild= 0l;
- return( which->MakeActive( false ) );
- }
- if( !mChildren.MoveFirst() )
- return( false );
- do {
- mChildren.GetData( child );
- } while( mChildren.MoveNext() && (child->mChild != which ) );
- if( child->mChild != which )
- return( false );
- if( mActiveChild && (mActiveChild != which) )
- mActiveChild->MakeActive( false );
- mActiveChild= which;
- return( mActiveChild->MakeActive( true ) );
- }
-
- void TLayoutLeaf::CalcMouseMove( Point pt, RgnHandle rgn )
- {
- Rect localRect= GetRect();
- RgnHandle myRgn= ::NewRgn();
- if( mParent )
- mParent->CalcClip( this, myRgn );
- else
- ::RectRgn( myRgn, &localRect );
- if( ::PtInRgn( pt, myRgn ) ) {
- ::CopyRgn( myRgn, rgn );
- TMouseHandler::SGetMouse()->SetListener( this );
- } else {
- ::DiffRgn( rgn, myRgn, rgn );
- }
- ::DisposeRgn( myRgn );
- }
-
- void TLayoutBranch::CalcMouseMove( Point pt, RgnHandle rgn )
- {
- if( !mChildren.MoveFirst() ) {
- TLayoutLeaf::CalcMouseMove( pt, rgn );
- return;
- }
- UInt32 childInt;
- TChildStruct *child;
- Rect childRect;
- do {
- mChildren.GetData( child );
- childRect= child->mChild->GetRect();
- if( ::PtInRect( pt, &childRect ) ) {
- child->mChild->CalcMouseMove( pt, rgn );
- break;
- }
- } while( mChildren.MoveNext() );
- if( !::PtInRect( pt, &childRect ) ) {
- ::RectRgn( rgn, &mContentRect );
- mChildren.MoveFirst();
- RgnHandle rRgn= ::NewRgn();
- do {
- mChildren.GetData( child );
- childRect= child->mChild->GetRect();
- ::RectRgn( rRgn, &childRect );
- ::DiffRgn( rgn, rRgn, rgn );
- } while( mChildren.MoveNext() );
- ::DisposeRgn( rRgn );
- }
- }
-
- void TLayoutLeaf::HandleMouseMoved( TMouseEvent *ev, bool within )
- {
- if( within )
- HandleMouseEnter();
- else
- HandleMouseExit();
- }
-
- void TLayoutLeaf::GetClip( RgnHandle rgn )
- {
- if( mParent )
- mParent->CalcClip( this, rgn );
- else
- ::RectRgn( rgn, &mContentRect );
- }
-
- void TLayoutBranch::CalcClip( TLayoutLeaf *child, RgnHandle rgn )
- {
- TChildStruct *ch;
- UINT32 temp;
- bool found= false;
- RgnHandle rRgn;
-
- if( !mChildren.MoveFirst() )
- return;
- if( mParent )
- mParent->CalcClip( this, rgn );
- else
- ::RectRgn( rgn, &mContentRect );
- rRgn= ::NewRgn();
- do {
- mChildren.GetData( ch );
- found= (ch->mChild==child);
- if( !found ) {
- ::RectRgn( rRgn, &ch->mChild->GetRect() );
- ::DiffRgn( rgn, rRgn, rgn );
- }
- } while( !found&&mChildren.MoveNext() );
- if( found ) {
- ::RectRgn( rRgn, &child->GetRect() );
- ::SectRgn( rgn, rRgn, rgn );
- }
- ::DisposeRgn( rRgn );
- }
-
- void TLayoutLeaf::HandleMouseEnter()
- {
- ::SetCursor( &qd.arrow );
- }
-
- void TLayoutLeaf::HandleMouseExit()
- {
- }