ResizableLayout.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // ResizableLayout.h: interface for the CResizableLayout class.
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Copyright (C) 2000-2002 by Paolo Messina
  6. // (http://www.geocities.com/ppescher - ppescher@yahoo.com)
  7. //
  8. // The contents of this file are subject to the Artistic License (the "License").
  9. // You may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at:
  11. // http://www.opensource.org/licenses/artistic-license.html
  12. //
  13. // If you find this code useful, credits would be nice!
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. #if !defined(AFX_RESIZABLELAYOUT_H__INCLUDED_)
  17. #define AFX_RESIZABLELAYOUT_H__INCLUDED_
  18. #include <afxtempl.h>
  19. #include "ResizableMsgSupport.h"
  20. #if _MSC_VER > 1000
  21. #pragma once
  22. #endif // _MSC_VER > 1000
  23. // useful compatibility constants (the only one required is NOANCHOR)
  24. const CSize NOANCHOR(-1,-1),
  25. TOP_LEFT(0,0), TOP_CENTER(50,0), TOP_RIGHT(100,0),
  26. MIDDLE_LEFT(0,50), MIDDLE_CENTER(50,50), MIDDLE_RIGHT(100,50),
  27. BOTTOM_LEFT(0,100), BOTTOM_CENTER(50,100), BOTTOM_RIGHT(100,100);
  28. class CResizableLayout
  29. {
  30. protected:
  31. class LayoutInfo
  32. {
  33. public:
  34. HWND hWnd;
  35. UINT nCallbackID;
  36. CString sWndClass;
  37. // upper-left corner
  38. SIZE sizeTypeTL;
  39. SIZE sizeMarginTL;
  40. // bottom-right corner
  41. SIZE sizeTypeBR;
  42. SIZE sizeMarginBR;
  43. // custom window support
  44. BOOL bMsgSupport;
  45. RESIZEPROPERTIES properties;
  46. public:
  47. LayoutInfo() : hWnd(NULL), nCallbackID(0), bMsgSupport(FALSE)
  48. { }
  49. LayoutInfo(HWND hwnd, SIZE tl_t, SIZE tl_m,
  50. SIZE br_t, SIZE br_m, CString classname)
  51. : hWnd(hwnd), nCallbackID(0),
  52. sWndClass(classname), bMsgSupport(FALSE),
  53. sizeTypeTL(tl_t), sizeMarginTL(tl_m),
  54. sizeTypeBR(br_t), sizeMarginBR(br_m)
  55. { }
  56. };
  57. private:
  58. // list of repositionable controls
  59. CMap<HWND, HWND, POSITION, POSITION> m_mapLayout;
  60. CList<LayoutInfo, LayoutInfo&> m_listLayout;
  61. CList<LayoutInfo, LayoutInfo&> m_listLayoutCB;
  62. void ClipChildWindow(const CResizableLayout::LayoutInfo &layout, CRgn* pRegion);
  63. void CalcNewChildPosition(const CResizableLayout::LayoutInfo &layout,
  64. const CRect &rectParent, CRect &rectChild, UINT& uFlags);
  65. protected:
  66. // override to initialize resize properties (clipping, refresh)
  67. virtual void InitResizeProperties(CResizableLayout::LayoutInfo& layout);
  68. // override to specify clipping for unsupported windows
  69. virtual BOOL LikesClipping(const CResizableLayout::LayoutInfo &layout);
  70. // override to specify refresh for unsupported windows
  71. virtual BOOL NeedsRefresh(const CResizableLayout::LayoutInfo &layout,
  72. const CRect &rectOld, const CRect &rectNew);
  73. // paint the background on the given DC (for XP theme's compatibility)
  74. void EraseBackground(CDC* pDC);
  75. // clip out child windows from the given DC (support old code)
  76. void ClipChildren(CDC* pDC);
  77. // get the clipping region (without clipped child windows)
  78. void GetClippingRegion(CRgn* pRegion);
  79. // override for scrollable or expanding parent windows
  80. virtual void GetTotalClientRect(LPRECT lpRect);
  81. // add anchors to a control, given its HWND
  82. void AddAnchor(HWND hWnd, CSize sizeTypeTL, CSize sizeTypeBR = NOANCHOR);
  83. // add anchors to a control, given its ID
  84. void AddAnchor(UINT nID, CSize sizeTypeTL, CSize sizeTypeBR = NOANCHOR)
  85. {
  86. AddAnchor(::GetDlgItem(GetResizableWnd()->GetSafeHwnd(), nID),
  87. sizeTypeTL, sizeTypeBR);
  88. }
  89. // add a callback (control ID or HWND is unknown or may change)
  90. void AddAnchorCallback(UINT nCallbackID);
  91. // get rect of an anchored window, given the parent's client area
  92. BOOL GetAnchorPosition(HWND hWnd, const CRect &rectParent,
  93. CRect &rectChild, UINT* lpFlags = NULL)
  94. {
  95. POSITION pos;
  96. if (!m_mapLayout.Lookup(hWnd, pos))
  97. return FALSE;
  98. UINT uTmpFlags;
  99. CalcNewChildPosition(m_listLayout.GetAt(pos), rectParent, rectChild,
  100. (lpFlags != NULL) ? (*lpFlags) : uTmpFlags);
  101. return TRUE;
  102. }
  103. // get rect of an anchored window, given the parent's client area
  104. BOOL GetAnchorPosition(UINT nID, const CRect &rectParent,
  105. CRect &rectChild, UINT* lpFlags = NULL)
  106. {
  107. return GetAnchorPosition(::GetDlgItem(GetResizableWnd()->GetSafeHwnd(), nID),
  108. rectParent, rectChild, lpFlags);
  109. }
  110. // remove an anchored control from the layout, given its HWND
  111. BOOL RemoveAnchor(HWND hWnd)
  112. {
  113. POSITION pos;
  114. if (!m_mapLayout.Lookup(hWnd, pos))
  115. return FALSE;
  116. m_listLayout.RemoveAt(pos);
  117. return m_mapLayout.RemoveKey(hWnd);
  118. }
  119. // remove an anchored control from the layout, given its HWND
  120. BOOL RemoveAnchor(UINT nID)
  121. {
  122. return RemoveAnchor(::GetDlgItem(GetResizableWnd()->GetSafeHwnd(), nID));
  123. }
  124. // reset layout content
  125. void RemoveAllAnchors()
  126. {
  127. m_mapLayout.RemoveAll();
  128. m_listLayout.RemoveAll();
  129. m_listLayoutCB.RemoveAll();
  130. }
  131. // adjust children's layout, when parent's size changes
  132. void ArrangeLayout();
  133. // override to provide dynamic control's layout info
  134. virtual BOOL ArrangeLayoutCallback(CResizableLayout::LayoutInfo& layout);
  135. // override to provide the parent window
  136. virtual CWnd* GetResizableWnd() = 0;
  137. public:
  138. CResizableLayout() { }
  139. virtual ~CResizableLayout()
  140. {
  141. // just for safety
  142. RemoveAllAnchors();
  143. }
  144. };
  145. #endif // !defined(AFX_RESIZABLELAYOUT_H__INCLUDED_)