OptionTreeItemComboBox.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 6k
Category:

Graph program

Development Platform:

Visual C++

  1. // COptionTree
  2. //
  3. // License
  4. // -------
  5. // This code is provided "as is" with no expressed or implied warranty.
  6. // 
  7. // You may use this code in a commercial product with or without acknowledgement.
  8. // However you may not sell this code or any modification of this code, this includes 
  9. // commercial libraries and anything else for profit.
  10. //
  11. // I would appreciate a notification of any bugs or bug fixes to help the control grow.
  12. //
  13. // History:
  14. // --------
  15. // See License.txt for full history information.
  16. //
  17. //
  18. // Copyright (c) 1999-2002 
  19. // ComputerSmarts.net 
  20. // mattrmiller@computersmarts.net
  21. #include "stdafx.h"
  22. #include "OptionTreeItemComboBox.h"
  23. // Added Headers
  24. #include "OptionTree.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // COptionTreeItemComboBox
  32. COptionTreeItemComboBox::COptionTreeItemComboBox()
  33. {
  34. // Initialize variables
  35. m_bFocus = FALSE;
  36. m_lDropDownHeight = OT_COMBO_DROPDOWNHEIGHT;
  37. // Set item type
  38. SetItemType(OT_ITEM_COMBOBOX);
  39. }
  40. COptionTreeItemComboBox::~COptionTreeItemComboBox()
  41. {
  42. }
  43. BEGIN_MESSAGE_MAP(COptionTreeItemComboBox, CComboBox)
  44. //{{AFX_MSG_MAP(COptionTreeItemComboBox)
  45. ON_WM_SETFOCUS()
  46. ON_WM_KILLFOCUS()
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // COptionTreeItemComboBox message handlers
  51. void COptionTreeItemComboBox::DrawAttribute(CDC *pDC, const RECT &rcRect)
  52. {
  53. // If we don't have focus, text is drawn.
  54. if (m_bFocus == TRUE)
  55. {
  56. return;
  57. }
  58. // Make sure options aren't NULL
  59. if (m_otOption == NULL)
  60. {
  61. return;
  62. }
  63. // Make sure there is a window
  64. if (!IsWindow(GetSafeHwnd()))
  65. {
  66. return;
  67. }
  68. // Set window position
  69. if (IsWindow(GetSafeHwnd()))
  70. {
  71. MoveWindow(m_rcAttribute.left, m_rcAttribute.top, m_rcAttribute.Width(), m_rcAttribute.Height());
  72. }
  73. // Declare variables
  74. HGDIOBJ hOld;
  75. COLORREF crOld;
  76. int nOldBack;
  77. CRect rcText;
  78. CString strWindowText;
  79. COLORREF crOldBack;
  80. // Get window text
  81. GetWindowText(strWindowText);
  82. // Select font
  83. hOld = pDC->SelectObject(m_otOption->GetNormalFont());
  84. // Set text color
  85. if (IsReadOnly() == TRUE || m_otOption->IsWindowEnabled() == FALSE)
  86. {
  87. crOld = pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
  88. }
  89. else
  90. {
  91. crOld = pDC->SetTextColor(GetTextColor());
  92. }
  93. // Set background mode
  94. nOldBack = pDC->SetBkMode(TRANSPARENT);
  95. // Set background color
  96. crOldBack = pDC->SetBkColor(GetBackgroundColor());
  97. // Get rectangle
  98. rcText = rcRect;
  99. // Draw text
  100. pDC->DrawText(strWindowText, rcText, DT_SINGLELINE | DT_VCENTER);
  101. // Restore GDI ojects
  102. pDC->SelectObject(hOld);
  103. pDC->SetTextColor(crOld);
  104. pDC->SetBkMode(nOldBack);
  105. pDC->SetBkColor(crOldBack);
  106. }
  107. void COptionTreeItemComboBox::OnCommit()
  108. {
  109. // Hide edit control
  110. if (IsWindow(GetSafeHwnd()))
  111. {
  112. // -- Show window
  113. ShowWindow(SW_HIDE);
  114. }
  115. }
  116. void COptionTreeItemComboBox::OnRefresh()
  117. {
  118. // Set the window text
  119. if (IsWindow(GetSafeHwnd()))
  120. {
  121. MoveWindow(m_rcAttribute.left, m_rcAttribute.top, m_rcAttribute.Width(), m_rcAttribute.Height());
  122. }
  123. }
  124. void COptionTreeItemComboBox::OnMove()
  125. {
  126. // Set window position
  127. if (IsWindow(GetSafeHwnd()))
  128. {
  129. MoveWindow(m_rcAttribute.left, m_rcAttribute.top, m_rcAttribute.Width(), m_rcAttribute.Height());
  130. }
  131. // Hide window
  132. if (m_bFocus == FALSE && IsWindow(GetSafeHwnd()))
  133. {
  134. // -- Show window
  135. ShowWindow(SW_HIDE);
  136. }
  137. }
  138. void COptionTreeItemComboBox::OnActivate()
  139. {
  140. // Make sure window is valid
  141. if (IsWindow(GetSafeHwnd()))
  142. {
  143. // -- Show window
  144. ShowWindow(SW_SHOW);
  145. // -- Set window position
  146. MoveWindow(m_rcAttribute.left, m_rcAttribute.top, m_rcAttribute.Width(), m_rcAttribute.Height() + m_lDropDownHeight);
  147. // -- Set focus
  148. SetFocus();
  149. }
  150. }
  151. void COptionTreeItemComboBox::OnSetFocus(CWnd* pOldWnd) 
  152. {
  153. // Mark focus
  154. m_bFocus = TRUE;
  155. CComboBox::OnSetFocus(pOldWnd);
  156. }
  157. void COptionTreeItemComboBox::OnKillFocus(CWnd* pNewWnd) 
  158. {
  159. // Validate
  160. if (m_otOption == NULL)
  161. {
  162. CComboBox::OnKillFocus(pNewWnd);
  163. return;
  164. }
  165. // See if new window is tree of list
  166. if (m_otOption->IsChild(pNewWnd) == TRUE)
  167. {
  168. // -- Mark focus
  169. m_bFocus = FALSE;
  170. // -- Commit changes
  171. CommitChanges();
  172. }
  173. CComboBox::OnKillFocus(pNewWnd);
  174. }
  175. BOOL COptionTreeItemComboBox::CreateComboItem(DWORD dwAddStyle)
  176. {
  177. // Declare variables
  178. DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL;
  179. BOOL bRet = FALSE;
  180. // Make sure options is not NULL
  181. if (m_otOption == NULL)
  182. {
  183. return FALSE;
  184. }
  185. // Create edit control
  186. if (!IsWindow(GetSafeHwnd()))
  187. {
  188. // -- Add style
  189. if (dwAddStyle != 0)
  190. {
  191. dwStyle |= dwAddStyle;
  192. }
  193. // -- Create the combo box
  194. bRet = Create(dwStyle, m_rcAttribute, m_otOption->GetCtrlParent(), GetCtrlID());
  195. // -- Setup combo
  196. if (bRet == TRUE)
  197. {
  198. // -- -- Set font
  199. SetFont(m_otOption->GetNormalFont(), TRUE);
  200. // -- -- Set window position
  201. MoveWindow(m_rcAttribute.left, m_rcAttribute.top, m_rcAttribute.Width(), m_rcAttribute.Height());
  202. // -- -- Hide window
  203. ShowWindow(SW_HIDE);
  204. }
  205. }
  206. return bRet;
  207. }
  208. void COptionTreeItemComboBox::SetDropDownHeight(long lHeight)
  209. {
  210. // Save variable
  211. m_lDropDownHeight = lHeight;
  212. }
  213. long COptionTreeItemComboBox::GetDropDownHeight()
  214. {
  215. // Return variable
  216. return m_lDropDownHeight;
  217. }
  218. void COptionTreeItemComboBox::CleanDestroyWindow()
  219. {
  220. // Destroy window
  221. if (IsWindow(GetSafeHwnd()))
  222. {
  223. // -- Destroy window
  224. DestroyWindow();
  225. }
  226. }
  227. void COptionTreeItemComboBox::OnDeSelect()
  228. {
  229. // Hide window
  230. if (IsWindow(GetSafeHwnd()))
  231. {
  232. // -- Show window
  233. ShowWindow(SW_HIDE);
  234. }
  235. }
  236. void COptionTreeItemComboBox::OnSelect()
  237. {
  238. // Do nothing here
  239. }