ResizableComboBox.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // ResizableComboBox.cpp : implementation file
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // This file is part of ResizableLib
  6. // https://github.com/ppescher/resizablelib
  7. //
  8. // Copyright (C) 2000-2015 by Paolo Messina
  9. // mailto:ppescher@hotmail.com
  10. //
  11. // The contents of this file are subject to the Artistic License 2.0
  12. // http://opensource.org/licenses/Artistic-2.0
  13. //
  14. // If you find this code useful, credits would be nice!
  15. //
  16. /////////////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "ResizableComboBox.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CResizableComboBox
  26. CResizableComboBox::CResizableComboBox()
  27. {
  28. m_bClipMaxHeight = TRUE;
  29. m_bIntegralHeight = TRUE;
  30. }
  31. CResizableComboBox::~CResizableComboBox()
  32. {
  33. if (m_ctrlListBox.GetSafeHwnd() != NULL)
  34. m_ctrlListBox.UnsubclassWindow();
  35. }
  36. BEGIN_MESSAGE_MAP(CResizableComboBox, CComboBox)
  37. //{{AFX_MSG_MAP(CResizableComboBox)
  38. ON_WM_CTLCOLOR()
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CResizableComboBox message handlers
  43. HBRUSH CResizableComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  44. {
  45. HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
  46. if (nCtlColor == CTLCOLOR_LISTBOX)
  47. {
  48. if (!(GetStyle() & CBS_SIMPLE)
  49. && (m_ctrlListBox.m_hWnd == NULL))
  50. {
  51. TRACE("ComboLBox: 0x%08X\n", pWnd->m_hWnd);
  52. // attach to the owned listbox
  53. m_ctrlListBox.m_pOwnerCombo = this;
  54. m_ctrlListBox.SubclassWindow(pWnd->m_hWnd);
  55. }
  56. }
  57. return hbr;
  58. }
  59. LRESULT CResizableComboBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  60. {
  61. switch (message)
  62. {
  63. case CB_GETDROPPEDCONTROLRECT:
  64. *(LPRECT)lParam = m_rectDropDown;
  65. MapWindowPoints(NULL, (LPRECT)lParam);
  66. return TRUE;
  67. }
  68. LRESULT lResult = CComboBox::WindowProc(message, wParam, lParam);
  69. // if listbox is attached, update horizontal extent
  70. switch (message)
  71. {
  72. case CB_INSERTSTRING:
  73. case CB_ADDSTRING:
  74. if (lResult != CB_ERR && lResult != CB_ERRSPACE)
  75. UpdateHorizontalExtent((LPCTSTR)lParam);
  76. break;
  77. case CB_DIR:
  78. if (lResult != CB_ERR && lResult != CB_ERRSPACE)
  79. InitHorizontalExtent();
  80. break;
  81. case CB_RESETCONTENT:
  82. InitHorizontalExtent();
  83. break;
  84. }
  85. return lResult;
  86. }
  87. void CResizableComboBox::InitHorizontalExtent()
  88. {
  89. CClientDC dc(this);
  90. CFont* pOldFont = dc.SelectObject(GetFont());
  91. CString str;
  92. m_iExtent = 0;
  93. int n = GetCount();
  94. for (int i=0; i<n; i++)
  95. {
  96. GetLBText(i, str);
  97. int cx = dc.GetTextExtent(str).cx;
  98. if (cx > m_iExtent)
  99. m_iExtent = cx;
  100. }
  101. SetHorizontalExtent(m_iExtent
  102. + LOWORD(GetDialogBaseUnits()));
  103. dc.SelectObject(pOldFont);
  104. }
  105. void CResizableComboBox::UpdateHorizontalExtent(LPCTSTR szText)
  106. {
  107. CClientDC dc(this);
  108. CFont* pOldFont = dc.SelectObject(GetFont());
  109. int cx = dc.GetTextExtent(szText, lstrlen(szText)).cx;
  110. if (cx > m_iExtent)
  111. {
  112. m_iExtent = cx;
  113. SetHorizontalExtent(m_iExtent
  114. + LOWORD(GetDialogBaseUnits()));
  115. }
  116. dc.SelectObject(pOldFont);
  117. }
  118. void CResizableComboBox::PreSubclassWindow()
  119. {
  120. ASSERT(GetStyle() & CBS_NOINTEGRALHEIGHT);
  121. InitHorizontalExtent();
  122. GetDroppedControlRect(&m_rectDropDown);
  123. ::MapWindowPoints(NULL, GetSafeHwnd(),
  124. (LPPOINT)&m_rectDropDown, 2);
  125. CComboBox::PreSubclassWindow();
  126. }
  127. int CResizableComboBox::MakeIntegralHeight(const int height)
  128. {
  129. int inth = height; // integral height (result)
  130. int availh = height; // available height
  131. int n = GetCount();
  132. DWORD dwStyle = GetStyle();
  133. if (!m_bIntegralHeight || n == 0)
  134. return inth;
  135. if (dwStyle & CBS_OWNERDRAWVARIABLE)
  136. {
  137. inth = 0; // try to reach availh by integral steps
  138. int i;
  139. // use items below the first visible
  140. for (i=GetTopIndex(); availh>0 && i<n; i++)
  141. {
  142. int h = GetItemHeight(i);
  143. if (h == CB_ERR)
  144. break;
  145. inth += h;
  146. availh -= h;
  147. }
  148. // to fill the remaining height, use items above
  149. for (i=GetTopIndex()-1; availh>0 && i>=0; i--)
  150. {
  151. int h = GetItemHeight(i);
  152. if (h == CB_ERR)
  153. break;
  154. inth += h;
  155. availh -= h;
  156. }
  157. // scroll into view
  158. SetTopIndex(i);
  159. if (!m_bClipMaxHeight) // it can be higher than all the items
  160. {
  161. // to fill the remaining height, use last item
  162. int h = GetItemHeight(n-1);
  163. if (h != CB_ERR)
  164. {
  165. inth += availh - availh % h;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. // every item has the same height (take the first)
  172. int h = GetItemHeight(0);
  173. if (h != CB_ERR && n != CB_ERR)
  174. {
  175. int rows = availh / h;
  176. // can't be higher than all the items
  177. if (m_bClipMaxHeight && rows > n)
  178. rows = n;
  179. inth = rows * h;
  180. // scroll into view
  181. if (n - rows < GetTopIndex())
  182. SetTopIndex(n-rows);
  183. }
  184. }
  185. return inth;
  186. }