ResizableLayout.h

00001 // ResizableLayout.h: interface for the CResizableLayout class.
00002 //
00004 //
00005 // Copyright (C) 2000-2002 by Paolo Messina
00006 // (http://www.geocities.com/ppescher - [email protected])
00007 //
00008 // The contents of this file are subject to the Artistic License (the "License").
00009 // You may not use this file except in compliance with the License. 
00010 // You may obtain a copy of the License at:
00011 // http://www.opensource.org/licenses/artistic-license.html
00012 //
00013 // If you find this code useful, credits would be nice!
00014 //
00016 
00017 #if !defined(AFX_RESIZABLELAYOUT_H__INCLUDED_)
00018 #define AFX_RESIZABLELAYOUT_H__INCLUDED_
00019 
00020 #include <afxtempl.h>
00021 #include "ResizableMsgSupport.h"
00022 
00023 #if _MSC_VER > 1000
00024 #pragma once
00025 #endif // _MSC_VER > 1000
00026 
00027 // useful compatibility constants (the only one required is NOANCHOR)
00028 
00029 const CSize NOANCHOR(-1,-1),
00030         TOP_LEFT(0,0), TOP_CENTER(50,0), TOP_RIGHT(100,0),
00031         MIDDLE_LEFT(0,50), MIDDLE_CENTER(50,50), MIDDLE_RIGHT(100,50),
00032         BOTTOM_LEFT(0,100), BOTTOM_CENTER(50,100), BOTTOM_RIGHT(100,100);
00033 
00034 
00035 class CResizableLayout
00036 {
00037 protected:
00038         class LayoutInfo
00039         {
00040         public:
00041                 HWND hWnd;
00042                 UINT nCallbackID;
00043 
00044                 CString sWndClass;
00045 
00046                 // upper-left corner
00047                 SIZE sizeTypeTL;
00048                 SIZE sizeMarginTL;
00049                 
00050                 // bottom-right corner
00051                 SIZE sizeTypeBR;
00052                 SIZE sizeMarginBR;
00053 
00054                 // custom window support
00055                 BOOL bMsgSupport;
00056                 RESIZEPROPERTIES properties;
00057         
00058         public:
00059                 LayoutInfo() : hWnd(NULL), nCallbackID(0), bMsgSupport(FALSE)
00060                 { 
00061                         sizeTypeTL.cx = 0;
00062                         sizeTypeTL.cy = 0;
00063                         sizeMarginTL.cx = 0;
00064                         sizeMarginTL.cy = 0;
00065                         sizeTypeBR.cx = 0;
00066                         sizeTypeBR.cy = 0;
00067                         sizeMarginBR.cx = 0;
00068                         sizeMarginBR.cy = 0;
00069                         memset(&properties, 0, sizeof properties);
00070                 }
00071 
00072                 LayoutInfo(HWND hwnd, SIZE tl_t, SIZE tl_m, 
00073                         SIZE br_t, SIZE br_m, CString classname)
00074                         : hWnd(hwnd), nCallbackID(0),
00075                         sWndClass(classname), bMsgSupport(FALSE),
00076                         sizeTypeTL(tl_t), sizeMarginTL(tl_m),
00077                         sizeTypeBR(br_t), sizeMarginBR(br_m)
00078                 { 
00079                         memset(&properties, 0, sizeof properties);
00080                 }
00081         };
00082 
00083 private:
00084         // list of repositionable controls
00085         CMap<HWND, HWND, POSITION, POSITION> m_mapLayout;
00086         CList<LayoutInfo, LayoutInfo&> m_listLayout;
00087         CList<LayoutInfo, LayoutInfo&> m_listLayoutCB;
00088 
00089         void ClipChildWindow(const CResizableLayout::LayoutInfo &layout, CRgn* pRegion);
00090 
00091         void CalcNewChildPosition(const CResizableLayout::LayoutInfo &layout,
00092                 const CRect &rectParent, CRect &rectChild, UINT& uFlags);
00093 
00094 protected:
00095         // override to initialize resize properties (clipping, refresh)
00096         virtual void InitResizeProperties(CResizableLayout::LayoutInfo& layout);
00097 
00098         // override to specify clipping for unsupported windows
00099         virtual BOOL LikesClipping(const CResizableLayout::LayoutInfo &layout);
00100 
00101         // override to specify refresh for unsupported windows
00102         virtual BOOL NeedsRefresh(const CResizableLayout::LayoutInfo &layout,
00103                 const CRect &rectOld, const CRect &rectNew);
00104 
00105         // paint the background on the given DC (for XP theme's compatibility)
00106         void EraseBackground(CDC* pDC);
00107 
00108         // clip out child windows from the given DC (support old code)
00109         void ClipChildren(CDC* pDC);
00110 
00111         // get the clipping region (without clipped child windows)
00112         void GetClippingRegion(CRgn* pRegion);
00113         
00114         // override for scrollable or expanding parent windows
00115         virtual void GetTotalClientRect(LPRECT lpRect);
00116 
00117         // add anchors to a control, given its HWND
00118         void AddAnchor(HWND hWnd, CSize sizeTypeTL, CSize sizeTypeBR = NOANCHOR);
00119 
00120         // add anchors to a control, given its ID
00121         void AddAnchor(UINT nID, CSize sizeTypeTL, CSize sizeTypeBR = NOANCHOR)
00122         {
00123                 AddAnchor(::GetDlgItem(GetResizableWnd()->GetSafeHwnd(), nID),
00124                         sizeTypeTL, sizeTypeBR);
00125         }
00126 
00127         // add a callback (control ID or HWND is unknown or may change)
00128         void AddAnchorCallback(UINT nCallbackID);
00129 
00130         // get rect of an anchored window, given the parent's client area
00131         BOOL GetAnchorPosition(HWND hWnd, const CRect &rectParent,
00132                 CRect &rectChild, UINT* lpFlags = NULL)
00133         {
00134                 POSITION pos;
00135                 if (!m_mapLayout.Lookup(hWnd, pos))
00136                         return FALSE;
00137 
00138                 UINT uTmpFlags;
00139                 CalcNewChildPosition(m_listLayout.GetAt(pos), rectParent, rectChild,
00140                         (lpFlags != NULL) ? (*lpFlags) : uTmpFlags);
00141                 return TRUE;
00142         }
00143 
00144         // get rect of an anchored window, given the parent's client area
00145         BOOL GetAnchorPosition(UINT nID, const CRect &rectParent,
00146                 CRect &rectChild, UINT* lpFlags = NULL)
00147         {
00148                 return GetAnchorPosition(::GetDlgItem(GetResizableWnd()->GetSafeHwnd(), nID),
00149                         rectParent, rectChild, lpFlags);
00150         }
00151 
00152         // remove an anchored control from the layout, given its HWND
00153         BOOL RemoveAnchor(HWND hWnd)
00154         {
00155                 POSITION pos;
00156                 if (!m_mapLayout.Lookup(hWnd, pos))
00157                         return FALSE;
00158 
00159                 m_listLayout.RemoveAt(pos);
00160                 return m_mapLayout.RemoveKey(hWnd);
00161         }
00162 
00163         // remove an anchored control from the layout, given its HWND
00164         BOOL RemoveAnchor(UINT nID)
00165         {
00166                 return RemoveAnchor(::GetDlgItem(GetResizableWnd()->GetSafeHwnd(), nID));
00167         }
00168 
00169         // reset layout content
00170         void RemoveAllAnchors()
00171         {
00172                 m_mapLayout.RemoveAll();
00173                 m_listLayout.RemoveAll();
00174                 m_listLayoutCB.RemoveAll();
00175         }
00176 
00177         // adjust children's layout, when parent's size changes
00178         void ArrangeLayout();
00179 
00180         // override to provide dynamic control's layout info
00181         virtual BOOL ArrangeLayoutCallback(CResizableLayout::LayoutInfo& layout);
00182 
00183         // override to provide the parent window
00184         virtual CWnd* GetResizableWnd() = 0;
00185 
00186 public:
00187         CResizableLayout() { }
00188 
00189         virtual ~CResizableLayout()
00190         {
00191                 // just for safety
00192                 RemoveAllAnchors();
00193         }
00194 };
00195 
00196 #endif // !defined(AFX_RESIZABLELAYOUT_H__INCLUDED_)

Generated on Tue Dec 13 14:47:57 2005 for guliverkli by  doxygen 1.4.5