test: parentinfo.cpp Source File

Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

# parentinfo.cpp

Go to the documentation of this file.

    00001 // ParentInfo.cpp: implementation of the CParentInfo class.
    00002 //
    00004
    00005 #include "pch.h"
    00006
    00007 #include "ParentInfo.h"
    00008 #include "debug.h"
    00009 #include "persctl.h"
    00010
    00012 // Construction/Destruction
    00014
    00015 CParentInfo::CParentInfo()
    00016 {
    00017         SetRightBorder(0);
    00018         SetLeftBorder(0);
    00019         SetTopBorder(0);
    00020         SetBottomBorder(0);
    00021         m_pTopEdge=NULL;
    00022         m_pBottomEdge=NULL;
    00023         m_pLeftEdge=NULL;
    00024         m_pRightEdge=NULL;
    00025 }
    00026
    00029 void CParentInfo::Init(HWND h)
    00030 {
    00031         m_hWnd=h;
    00032         DeterminSize();
    00033         SetRightBorder( GetWidth() );
    00034         SetLeftBorder( GetWidth() );
    00035         SetTopBorder( GetHeight() );
    00036         SetBottomBorder( GetHeight() );
    00037 }
    00038
    00041 CParentInfo::~CParentInfo()
    00042 {
    00043
    00044 }
    00045
    00047 //
    00048 //
    00049 // This is the size of the client area of the window
    00050 // we don't have to take into account the frame and title bar when positioning
    00051 // indside it.
    00052 //
    00054 void CParentInfo::DeterminSize()
    00055 {
    00056         GetClientRect( GetWindow(), &m_Size);
    00057         RECT r;
    00058         GetWindowRect( GetWindow(), &r);
    00059         DIMENSION d;
    00060         d.Width= r.right - r.left;
    00061         d.Height = r.bottom - r.top;
    00062         SetMinimumSize(d);
    00063         TRACE(L"ParentDimensions : %d by %d \n",
    00064                         GetWidth(),
    00065                         GetHeight());
    00066 }
    00067
    00068
    00070 //
    00071 // Walks all the edges, finding one that is closest and thus re-usable.
    00072 //
    00074 #define ABS(x) (x<0?-x:x)
    00075 CEdge * CParentInfo::AddEdge(int Position, int Axis, BOOL Flexible, int Slop)
    00076 {
    00077         int i=0;
    00078         CEdge * pEdge;
    00079         CGuide * pClosest=NULL;
    00080         int closest=0xffff;
    00081
    00082         //
    00083         // First see if the edge is what we wanted
    00084         //
    00085         while( pEdge=m_Edges.GetEdge(i++) )
    00086         {
    00087                 if(pEdge->GetPosition() == Position )
    00088                 {
    00089                         if( pEdge->GetAxis() == Axis )
    00090                         {
    00091                                 TRACE(L"Guide REUSE:: %03d, %02d\n",Position, Axis);
    00092                                 return pEdge;
    00093                         }
    00094                 }
    00095         }
    00096
    00097         i=0;
    00098         while ( pEdge=m_Edges.GetEdge(i++))
    00099         {
    00100                 CGuide * pGuide=pEdge->GetGuide();
    00101
    00102                 if(pGuide->GetAxis() == Axis )
    00103                 {
    00104                         // if( pEdge->GetFlexible() == Flexible )
    00105                         {
    00106                                 int distance=Position -pGuide->GetPosition() ;
    00107                                 if( ABS(distance) <= Slop )
    00108                                 {
    00109                                         if( Slop == 0 )
    00110                                                 return pEdge;
    00111
    00112                                         //
    00113                                         // Within the range, but is it the best?
    00114                                         //
    00115                                         if(closest==0xffff)
    00116                                         {
    00117                                                 closest=distance;
    00118                                                 pClosest=pGuide;
    00119                                         }
    00120
    00121                                         if( ABS(distance)<=ABS(closest) )
    00122                                         {
    00123                                                 pClosest=pGuide;
    00124                                                 closest=distance;
    00125                                         }
    00126                                 }
    00127                         }
    00128                 }
    00129         }
    00130
    00131         if(pClosest)
    00132         {
    00133                 //
    00134                 // Create a new edge with the same guide, different offset.
    00135                 //
    00136                 closest = Position - pClosest->GetPosition();
    00137                 TRACE(L"Guide CLOSE:: %03d, %02d, closest %d, actual pos %03d\n",pClosest->GetPosition(), pClosest->GetAxis(), closest, Position);
    00138                 return m_Edges.Create( pClosest, closest);
    00139         }
    00140
    00141         TRACE(L"Guide NEW  :: %03d, %02d\n",Position, Axis);
    00142         return m_Edges.Create(Position, Axis, Flexible, 0);
    00143 }
    00144
    00146 //
    00147 //
    00149 CEdge & CParentInfo::FindCloseEdge(CEdge & Fixed, int Offset)
    00150 {
    00151         int i=0;
    00152         CEdge * pEdge;
    00153         while ( pEdge=m_Edges.GetEdge(i++) )
    00154         {
    00155                 if(pEdge->GetAxis() == Fixed.GetAxis() )
    00156                 {
    00157                         int distance=Fixed.GetPosition() -pEdge->GetPosition() ;
    00158                         if( ABS(distance) <= Offset )
    00159                         {
    00160                                 return *pEdge;
    00161                         }
    00162                 }
    00163         }
    00164         return Fixed;
    00165 }
    00166
    00168 //
    00169 //
    00171 CEdge * CParentInfo::AddEdge(CGuide * pGuide, int Offset)
    00172 {
    00173         // TRACE(L"Associating another edge %03d from 0x%08x\n", Offset , Edge);
    00174         return m_Edges.Create( pGuide, Offset);
    00175 }
    00176
    00178 //
    00179 // BORDERS FOR THE DIALOG - (not for the components).
    00180 // Buids some default guides for the borders.
    00181 // the borders can move
    00182 //
    00184 void CParentInfo::ConstructBorders()
    00185 {
    00186         m_pLeftEdge=AddEdge( GetLeftBorder(), LEFT_AT, FALSE );
    00187         m_pRightEdge=AddEdge( GetWidth() - GetRightBorder(), RIGHT_AT, TRUE );
    00188
    00189         m_pTopEdge=AddEdge(  GetTopBorder(), TOP_AT, FALSE );
    00190         m_pBottomEdge=AddEdge( GetHeight() - GetBottomBorder(), BOTTOM_AT, TRUE );
    00191 }
    00192
    00194 //
    00195 // We know that the left and bottom edges move to follow the dialog
    00196 //
    00198 void CParentInfo::Resize(int width, int height)
    00199 {
    00200         m_pRightEdge->SetPosition( width - GetRightBorder() );
    00201         m_pBottomEdge->SetPosition( height - GetBottomBorder() );
    00202 }
    00203
    00204
    00206 //
    00207 // Walks all the edges building an array of the veritcal ones - not sorted
    00208 //
    00210 CEdge ** CParentInfo::GetVerticalEdges()
    00211 {
    00212         int iCount=m_Edges.GetNumVert();
    00213         CEdge ** ppEdges= (CEdge**)new (CEdge*[iCount]);
    00214         CEdge * pEdge;
    00215         int i=0,iDest=0;
    00216         while( pEdge = m_Edges.GetEdge(i++) )
    00217         {
    00218                 if(pEdge->IsVertical() )
    00219                         ppEdges[iDest++] = pEdge;
    00220 #ifdef _DEBUG
    00221                 if( iDest > iCount )
    00222                         TRACE(L"Vertical edge count is off %d vs %d\n", iDest, iCount);
    00223 #endif
    00224         }
    00225         return ppEdges;
    00226 }
    00227
    00229 //
    00230 // Walks all the edges building an array of the horizontal ones - not sorted
    00231 //
    00232 //
    00234 CEdge ** CParentInfo::GetHorizontalEdges()
    00235 {
    00236         int iCount=m_Edges.GetNumHoriz();
    00237         CEdge ** ppEdges= new CEdge *[iCount];
    00238         CEdge * pEdge;
    00239         int i=0,iDest=0;
    00240         while( pEdge = m_Edges.GetEdge(i++) )
    00241         {
    00242                 if(pEdge->IsHorizontal())
    00243                         ppEdges[iDest++] = pEdge;
    00244 #ifdef _DEBUG
    00245                 if( iDest > iCount )
    00246                         TRACE(L"Horiz edge count is off %d vs %d\n", iDest, iCount);
    00247 #endif
    00248         }
    00249         return ppEdges;
    00250 }
    00251
    00252 void CParentInfo::Annotate(HDC hdc)
    00253 {
    00254         int i=0;
    00255         CEdge * pEdge;
    00256         int iWidth=GetWidth();
    00257         int iHeight=GetHeight();
    00258         HPEN hpen = CreatePen( PS_SOLID, 1, RGB( 0xff,0x00,0x00) );
    00259         HGDIOBJ holdPen= SelectObject( hdc, hpen);
    00260         while( pEdge=m_Edges.GetEdge(i++) )
    00261         {
    00262                 if( pEdge->IsHorizontal() )
    00263                 {
    00264                         MoveToEx( hdc, 0, pEdge->GetPosition(), NULL );
    00265                         LineTo( hdc, iWidth , pEdge->GetPosition() );
    00266                 }
    00267                 else
    00268                 {
    00269                         MoveToEx( hdc, pEdge->GetPosition(), 0, NULL );
    00270                         LineTo( hdc, pEdge->GetPosition(), iHeight );
    00271                 }
    00272         }
    00273         SelectObject(hdc, holdPen);
    00274         DeleteObject(hpen);
    00275 }
    00276
    00278 //
    00279 // Walks all the controls, finds where the borders of the dialog are
    00280 //
    00282 void CParentInfo::DeterminBorders(CControlList * pControlList)
    00283 {
    00284         CResizeControl * pC;
    00285         int i=0;
    00286
    00287         //
    00288         // Now walk and see if we have any borders - this is a lame way to do it.
    00289         //
    00290 #define BORDER(Edge)                    if( pC->Get##Edge##Gap() < Get##Edge##Border() ) \
    00291                         Set##Edge##Border(pC->Get##Edge##Gap());
    00292
    00293         i=0;
    00294         while( pC=pControlList->GetControl(i++) )
    00295         {
    00296                 if( pC->GetRightGap() < GetRightBorder() )
    00297                         SetRightBorder(pC->GetRightGap());
    00298         }
    00299         TRACE(L"Right border is %d\n", GetRightBorder() );
    00300
    00301         i=0;
    00302         while( pC=pControlList->GetControl(i++) )
    00303         {
    00304                 BORDER(Left);
    00305         }
    00306         TRACE(L"Left border is %d\n", GetLeftBorder() );
    00307
    00308         i=0;
    00309         while( pC=pControlList->GetControl(i++) )
    00310         {
    00311                 BORDER(Top);
    00312         }
    00313         TRACE(L"Top border is %d\n", GetTopBorder() );
    00314
    00315         i=0;
    00316         while( pC=pControlList->GetControl(i++) )
    00317         {
    00318                 BORDER(Bottom);
    00319         }
    00320         TRACE(L"Bottom border is %d\n", GetBottomBorder() );
    00321
    00322         //
    00323         // Now add these edges to the parent info.
    00324         // or should the be add these guides to the parent info?
    00325         //
    00326         ConstructBorders();
    00327 }