NSFlexConstraints.cpp

Go to the documentation of this file.
00001 #ifndef NSFLEXCONSTRAINTS_IMPLEMENTATION_FILE
00002 #define NSFLEXCONSTRAINTS_IMPLEMENTATION_FILE
00003 
00004 // This code is a part of the NanoSoft NSViews C++ Library.
00005 // Copyright (C) 1996 NanoSoft Corporation. All rights reserved.
00006 
00007 #include "stdafx.h"
00008 #include "NSFlexConstraints.h"
00009 #include "NSFlexConstraintList.h"
00010 
00011 #include <basis/convert_utf.h>
00012 
00013 const CNSFlexHorizontalConstraint NSFlexHorizontallyFixed (0.0f, 0.0f);
00014 const CNSFlexHorizontalConstraint NSFlexShiftRight        (1.0f, 1.0f);
00015 const CNSFlexHorizontalConstraint NSFlexExpandRight       (0.0f, 1.0f);
00016 
00017 CNSFlexHorizontalConstraint::CNSFlexHorizontalConstraint(
00018     float fLeft, float fRight) :
00019         m_fLeft(fLeft), m_fRight(fRight)
00020 {
00021     ASSERT(m_fLeft >= 0.0f && m_fLeft <= 1.0f);
00022     ASSERT(m_fRight >= 0.0f && m_fRight <= 1.0f);
00023     ASSERT(m_fRight >= m_fLeft);
00024         // or else the control shrinks as the dialog grows.
00025 }
00026 
00027 CNSFlexHorizontalConstraint::CNSFlexHorizontalConstraint(
00028     const CNSFlexHorizontalConstraint& ref)
00029 {
00030     m_fLeft = ref.m_fLeft;
00031     m_fRight = ref.m_fRight;
00032 }
00033 
00034 const CNSFlexHorizontalConstraint& CNSFlexHorizontalConstraint::operator=(
00035     CNSFlexHorizontalConstraint& ref)
00036 {
00037     m_fLeft = ref.m_fLeft;
00038     m_fRight = ref.m_fRight;
00039     return(*this);
00040 }
00041 
00042 CNSFlexHorizontalConstraint* CNSFlexHorizontalConstraint::CopyMe() const
00043 {
00044     return new CNSFlexHorizontalConstraint(*this);
00045 }
00046 
00047 CNSFlexHorizontalConstraint::~CNSFlexHorizontalConstraint()
00048 {
00049 }
00050 
00051 void CNSFlexHorizontalConstraint::Init(
00052     const CWnd* pContainerWnd, int nWidth)
00053 {
00054 }
00055 
00056 void CNSFlexHorizontalConstraint::SetLeft(float fLeft)
00057 {
00058     m_fLeft = fLeft;
00059     ASSERT(m_fLeft >= 0.0f && m_fLeft <= 1.0f);
00060 }
00061 
00062 void CNSFlexHorizontalConstraint::SetRight(float fRight)
00063 {
00064     m_fRight = fRight;
00065     ASSERT(m_fRight >= 0.0f && m_fRight <= 1.0f);
00066 }
00067 
00068     
00069 
00070 const CNSFlexVerticalConstraint NSFlexVerticallyFixed (0.0f, 0.0f);
00071 const CNSFlexVerticalConstraint NSFlexShiftDown       (1.0f, 1.0f);
00072 const CNSFlexVerticalConstraint NSFlexExpandDown      (0.0f, 1.0f);
00073 
00074 CNSFlexVerticalConstraint::CNSFlexVerticalConstraint(
00075     float fTop, float fBottom) :
00076         m_fTop(fTop), m_fBottom(fBottom)
00077 {
00078     ASSERT(m_fTop >= 0.0f && m_fTop <= 1.0f);
00079     ASSERT(m_fBottom >= 0.0f && m_fBottom <= 1.0f);
00080     ASSERT(m_fBottom >= m_fTop);
00081         // or else the control shrinks as the dialog grows.
00082 }
00083 
00084 CNSFlexVerticalConstraint::CNSFlexVerticalConstraint(
00085     const CNSFlexVerticalConstraint& ref)
00086 {
00087     m_fTop = ref.m_fTop;
00088     m_fBottom = ref.m_fBottom;
00089 }
00090 
00091 const CNSFlexVerticalConstraint& CNSFlexVerticalConstraint::operator=(
00092     CNSFlexVerticalConstraint& ref)
00093 {
00094     m_fTop = ref.m_fTop;
00095     m_fBottom = ref.m_fBottom;
00096     return(*this);
00097 }
00098 
00099 CNSFlexVerticalConstraint* CNSFlexVerticalConstraint::CopyMe() const
00100 {
00101     return new CNSFlexVerticalConstraint(*this);
00102 }
00103 
00104 CNSFlexVerticalConstraint::~CNSFlexVerticalConstraint()
00105 {
00106 }
00107 
00108 void CNSFlexVerticalConstraint::Init(
00109     const CWnd* pContainerWnd, int nHeight)
00110 {
00111 }
00112     
00113 void CNSFlexVerticalConstraint::SetTop(float fTop)
00114 {
00115     m_fTop = fTop;
00116     ASSERT(m_fTop >= 0.0f && m_fTop <= 1.0f);
00117 }
00118 
00119 void CNSFlexVerticalConstraint::SetBottom(float fBottom)
00120 {
00121     m_fBottom = fBottom;
00122     ASSERT(m_fBottom >= 0.0f && m_fBottom <= 1.0f);
00123 }
00124 
00125 
00126 CNSFlexConstraint::CNSFlexConstraint(UINT nControlID,
00127     const CNSFlexHorizontalConstraint& HorizontalConstraint,
00128     const CNSFlexVerticalConstraint& VerticalConstraint) :
00129         m_nControlID(nControlID)
00130 {
00131     // Using the CopyMe functions allows the Constraints to be
00132     // passed in by const ref as the base type and then get a pointer
00133     // we can use to a copy of the exact type.
00134     m_pHorizontalConstraint = HorizontalConstraint.CopyMe();
00135     m_pVerticalConstraint = VerticalConstraint.CopyMe();
00136 
00137     ASSERT(m_pHorizontalConstraint);
00138     ASSERT(m_pVerticalConstraint);
00139 }
00140 
00141 CNSFlexConstraint::~CNSFlexConstraint()
00142 {
00143     delete m_pHorizontalConstraint;
00144     delete m_pVerticalConstraint;
00145 }
00146 
00147 void CNSFlexConstraint::SetBasePosition(int nBaseLeft, int nBaseTop, 
00148     int nBaseRight, int nBaseBottom)
00149 {
00150     m_nBaseLeft = nBaseLeft;
00151     m_nBaseTop = nBaseTop;
00152     m_nBaseRight = nBaseRight;
00153     m_nBaseBottom = nBaseBottom;
00154 }
00155 
00156 BOOL CNSFlexConstraint::Init(const CWnd* pContainerWnd,
00157     int nClientWidth, int nClientHeight)
00158 {
00159     ASSERT_VALID(pContainerWnd);
00160 
00161     CWnd* pFlexControlWnd = pContainerWnd->GetDlgItem(GetControlID());
00162     if(!pFlexControlWnd)
00163         return FALSE;
00164 
00165     CRect rectFlexControlWnd;
00166     pFlexControlWnd->GetWindowRect(rectFlexControlWnd);
00167 
00168     // Special ComboBox handling is needed on Windows NT before version 4.0
00169     // and under Win32s
00170     DWORD dwVersion = GetVersion(); 
00171     if (LOBYTE(LOWORD(dwVersion)) < 4)
00172     {
00173         char szClassName[10];
00174         GetClassName(pFlexControlWnd->GetSafeHwnd(), to_unicode_temp(szClassName), 10);
00175         if (lstrcmp(to_unicode_temp(szClassName), to_unicode_temp("ComboBox")) == 0)  // It is a combo box.
00176         {
00177             CRect rectDrop;
00178             ((CComboBox *)pFlexControlWnd)->
00179                 GetDroppedControlRect(&rectDrop);
00180             rectFlexControlWnd.BottomRight().y =
00181                 rectFlexControlWnd.TopLeft().y + rectDrop.Height();
00182         }
00183     }
00184 
00185     pContainerWnd->ScreenToClient(rectFlexControlWnd);
00186 
00187     SetBasePosition(rectFlexControlWnd.left,
00188         rectFlexControlWnd.top,rectFlexControlWnd.right, 
00189         rectFlexControlWnd.bottom);
00190 
00191     m_pHorizontalConstraint->Init(pContainerWnd, nClientWidth);
00192     m_pVerticalConstraint->Init(pContainerWnd, nClientHeight);
00193     
00194     return TRUE;
00195 }
00196 
00197 CNSFlexConstraintList::CNSFlexConstraintList(CWnd* pContainerWnd) :
00198     m_pContainerWnd(pContainerWnd), m_bConstraintsInitialized(FALSE)
00199 {
00200 
00201 }
00202 
00203 CNSFlexConstraintList::~CNSFlexConstraintList()
00204 {
00205     for (POSITION pos = m_FlexConstraints.GetHeadPosition(); pos; )
00206     {
00207         CNSFlexConstraint* pFlexConstraint = 
00208             (CNSFlexConstraint*)m_FlexConstraints.GetNext(pos);
00209         delete pFlexConstraint;
00210     }
00211 }
00212 
00213 void CNSFlexConstraintList::AddConstraint(UINT nControlID,
00214     const CNSFlexHorizontalConstraint& HorizontalConstraint,
00215     const CNSFlexVerticalConstraint& VerticalConstraint)
00216 {
00217     CNSFlexConstraint* pFlexConstraint = new CNSFlexConstraint(nControlID,
00218         HorizontalConstraint, VerticalConstraint);
00219     ASSERT(pFlexConstraint);
00220     m_FlexConstraints.AddTail(pFlexConstraint);
00221 }
00222 
00223 #ifdef _DEBUG
00224 
00225 BOOL CNSFlexConstraintList::AssertAllFlexControlsExist()
00226 {
00227     for (POSITION pos = m_FlexConstraints.GetHeadPosition(); pos; )
00228     {
00229         CNSFlexConstraint* pFlexConstraint = 
00230             (CNSFlexConstraint*)m_FlexConstraints.GetNext(pos);
00231         
00232         UINT nControlID = pFlexConstraint->GetControlID();
00233 
00234         CWnd* pFlexControlWnd = 
00235             m_pContainerWnd->GetDlgItem(nControlID);
00236 
00237         if (!pFlexControlWnd)
00238         {
00239             ASSERT(FALSE);
00240             return FALSE;
00241         }
00242     }
00243 
00244     return TRUE;
00245 }
00246 
00247 #endif
00248 
00249 void CNSFlexConstraintList::PositionControls(int nClientWidth,
00250     int nClientHeight)
00251 {
00252     if (m_FlexConstraints.GetCount() == 0)
00253         return;
00254 
00255     if (!m_bConstraintsInitialized)
00256         InitConstraints();
00257 
00258     if (!m_bConstraintsInitialized)
00259         return;
00260 
00261     int nDeltaClientWidth = max(nClientWidth - m_nClientBaseWidth,0);
00262     int nDeltaClientHeight = max(nClientHeight - m_nClientBaseHeight,0);
00263 
00264   // REVISION 4/10/97 TO WORK WITH ACTIVEX CONTROLS (Ed Smetak)
00265   // BeginDeferWindowPos and DeferWindowPos do not work with
00266   // ActiveX controls. MoveWindow works OK, so do that instead.
00267     //  HDWP  hDeferWndPosStruct =
00268     //    BeginDeferWindowPos(m_FlexConstraints.GetCount());  
00269   // END REVISION (Ver 1.04)
00270 
00271     for (POSITION pos = m_FlexConstraints.GetHeadPosition(); pos; )
00272     {
00273         CNSFlexConstraint* pFlexConstraint = 
00274             (CNSFlexConstraint*)m_FlexConstraints.GetNext(pos);
00275         
00276         CWnd* pFlexControlWnd = 
00277             m_pContainerWnd->GetDlgItem(pFlexConstraint->GetControlID());
00278         ASSERT(pFlexControlWnd);
00279 
00280         int nNewLeft = pFlexConstraint->GetBaseLeft() + 
00281             (int)(pFlexConstraint->GetLeftLocator()*nDeltaClientWidth);
00282         int nNewTop = pFlexConstraint->GetBaseTop() + 
00283             (int)(pFlexConstraint->GetTopLocator()*nDeltaClientHeight);
00284         int nNewRight = pFlexConstraint->GetBaseRight() + 
00285             (int)(pFlexConstraint->GetRightLocator()*nDeltaClientWidth);
00286         int nNewBottom = pFlexConstraint->GetBaseBottom() + 
00287             (int)(pFlexConstraint->GetBottomLocator()*nDeltaClientHeight);
00288 
00289   // REVISION 4/10/97 TO WORK WITH ACTIVEX CONTROLS (Ed Smetak)
00290   //  hDeferWndPosStruct = DeferWindowPos(hDeferWndPosStruct,
00291   //    pFlexControlWnd->GetSafeHwnd(), NULL,
00292   //    nNewLeft,nNewTop,nNewRight-nNewLeft,nNewBottom-nNewTop,
00293   //    SWP_NOACTIVATE|SWP_NOZORDER);
00294 
00295     pFlexControlWnd->MoveWindow(nNewLeft,nNewTop,nNewRight-nNewLeft,
00296       nNewBottom-nNewTop,FALSE);
00297     pFlexControlWnd->Invalidate();
00298     for (CWnd* pWnd = pFlexControlWnd->GetTopWindow(); pWnd;
00299       pWnd = pWnd->GetNextWindow())
00300         if (pWnd)
00301           pWnd->Invalidate();
00302   // END REVISION (Ver 1.04)
00303 
00304     }
00305 
00306   // REVISION 4/10/97 TO WORK WITH ACTIVEX CONTROLS (Ed Smetak)
00307   //  EndDeferWindowPos(hDeferWndPosStruct);
00308   // END REVISION (Ver 1.04)
00309 }
00310 
00311 void CNSFlexConstraintList::SetClientBaseSize(int nClientBaseWidth, 
00312     int nClientBaseHeight)
00313 {
00314     m_nClientBaseWidth = nClientBaseWidth;
00315     m_nClientBaseHeight = nClientBaseHeight;
00316 }
00317 
00318 void CNSFlexConstraintList::InitConstraints()
00319 {
00320     for (POSITION pos = m_FlexConstraints.GetHeadPosition(); pos; )
00321     {
00322         CNSFlexConstraint* pFlexConstraint = 
00323             (CNSFlexConstraint*)m_FlexConstraints.GetNext(pos);
00324         
00325         if (!pFlexConstraint->Init(m_pContainerWnd,
00326             m_nClientBaseWidth, m_nClientBaseHeight))
00327                 return;
00328     }
00329 
00330     m_bConstraintsInitialized = TRUE;
00331 }
00332 
00333 #endif //NSFLEXCONSTRAINTS_IMPLEMENTATION_FILE
00334 

Generated on Thu Nov 20 04:28:49 2008 for HOOPLE Libraries by  doxygen 1.5.1