ComboGridCtrl.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // ComboGridCtrl.cpp: implementation of the CComboGridCtrl class.
  2. //
  3. // This is a demo of how to override CGridCtrl in order to offer
  4. // other editing capabilities such as a drop down list
  5. //
  6. //////////////////////////////////////////////////////////////////////
  7. #include "stdafx.h"
  8. #include "ComboGridCtrl.h"
  9. #include "InPlaceList.h"
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char THIS_FILE[]=__FILE__;
  13. #define new DEBUG_NEW
  14. #endif
  15. BEGIN_MESSAGE_MAP(CComboGridCtrl, CGridCtrl)
  16. //{{AFX_MSG_MAP(CComboGridCtrl)
  17. //}}AFX_MSG_MAP
  18. END_MESSAGE_MAP()
  19. //////////////////////////////////////////////////////////////////////
  20. // Construction/Destruction
  21. //////////////////////////////////////////////////////////////////////
  22. CComboGridCtrl::CComboGridCtrl()
  23. {
  24. m_comboBoxStyle=CBS_DROPDOWN;
  25. }
  26. CComboGridCtrl::~CComboGridCtrl()
  27. {
  28. for(int i=0;i<m_comStrArray.GetSize();i++)
  29. delete m_comStrArray.GetAt(i).comStr;
  30. }
  31. //////////////////////////////////////////////////////////////////////
  32. // Overrides modified by huangjianxiong at 2003-5-17
  33. //////////////////////////////////////////////////////////////////////
  34. void CComboGridCtrl::CreateInPlaceEditControl(CRect& rect, DWORD dwStyle, UINT nID,
  35. int nRow, int nCol,
  36. LPCTSTR szText, int nChar)
  37. {
  38. BOOL bComboCol=FALSE;
  39. COMBOCOL combocol;
  40. for(int i=0;i<m_comStrArray.GetSize();i++)
  41. {
  42. combocol=m_comStrArray.GetAt(i);
  43. if(combocol.iCol==nCol)
  44. {
  45. bComboCol=TRUE;
  46. break;
  47. }
  48. }
  49. // InPlaceList and auto-deletes itself
  50. if(bComboCol)
  51. new CInPlaceList(this, rect,
  52. //CBS_DROPDOWNLIST, // Uncomment for dropdown list style
  53. m_comboBoxStyle, // Uncomment for dropdown style
  54. //CBS_SIMPLE, // Uncomment for simple style
  55. nID, // ID of control being created
  56. nRow, nCol,
  57. *combocol.comStr, szText, nChar);
  58. else
  59. CGridCtrl::CreateInPlaceEditControl(rect,dwStyle,nID,nRow,nCol,szText,nChar);
  60. }
  61. //****************************************************************
  62. // function:make a specified column can be edit by combo select
  63. // param: 1 int iCol[in]: specify the column
  64. // 2 CArrayString & comStr[in]:string list
  65. // return: BOOL bSuccess
  66. // remark: huangjianxiong modify at 2003-5-17
  67. //*****************************************************************
  68. BOOL CComboGridCtrl::AddComboColumn(int iCol, CStringArray &comStr)
  69. {
  70. if(iCol<GetFixedColumnCount()) return FALSE;
  71. COMBOCOL col;
  72. col.iCol=iCol;
  73. col.comStr=new CStringArray;
  74. col.comStr->Copy(comStr);
  75. m_comStrArray.Add(col);
  76. return TRUE;
  77. }
  78. BOOL CComboGridCtrl::SetItemText(int nRow, int nCol, LPCTSTR str)
  79. {
  80. int comIndex=IsColumnCombo(nCol);
  81. if(comIndex!=-1&&nRow>=GetFixedRowCount())
  82. {
  83. BOOL bRet;
  84. COMBOCOL combocol=m_comStrArray.GetAt(comIndex);
  85. for(int i=0;i<combocol.comStr->GetSize();i++)
  86. if(combocol.comStr->GetAt(i)==str)
  87. {
  88. bRet=CGridCtrl::SetItemText(nRow,nCol,str);
  89. break;
  90. }
  91. return bRet;
  92. }else
  93. return CGridCtrl::SetItemText(nRow,nCol,str);
  94. }
  95. int CComboGridCtrl::IsColumnCombo(int nCol)
  96. {
  97. int comIndex=-1;
  98. COMBOCOL combocol;
  99. for(int i=0;i<m_comStrArray.GetSize();i++)
  100. {
  101. combocol=m_comStrArray.GetAt(i);
  102. if(combocol.iCol==nCol)
  103. {
  104. comIndex=i;
  105. break;
  106. }
  107. }
  108. return comIndex;
  109. }
  110. BOOL CComboGridCtrl::SetItem(GV_ITEM *pItem)
  111. {
  112. if (!pItem)
  113. return FALSE;
  114. CGridCell* pCell = GetCell(pItem->row, pItem->col);
  115. if (!pCell)
  116. return FALSE;
  117. if (pItem->mask & GVIF_TEXT)
  118. {
  119. int comIndex=IsColumnCombo(pItem->col);
  120. BOOL bFind=FALSE;
  121. if(comIndex!=-1)
  122. {
  123. if(pItem->row<GetFixedRowCount())
  124. bFind=TRUE;
  125. else
  126. {
  127. COMBOCOL combocol=m_comStrArray.GetAt(comIndex);
  128. for(int i=0;i<combocol.comStr->GetSize();i++)
  129. if(combocol.comStr->GetAt(i)==pItem->szText)
  130. {
  131. bFind=TRUE;
  132. }
  133. }
  134. }
  135. if(!bFind&&comIndex!=-1)
  136. return FALSE;
  137. else
  138. pCell->szText=pItem->szText;
  139. }
  140. if (pItem->mask & GVIF_PARAM) pCell->lParam = pItem->lParam;
  141. if (pItem->mask & GVIF_IMAGE) pCell->iImage = pItem->iImage;
  142. if (pItem->mask & GVIF_STATE) pCell->state = pItem->state;
  143. if (pItem->mask & GVIF_FORMAT) pCell->nFormat = pItem->nFormat;
  144. if (pItem->mask & GVIF_BKCLR) pCell->crBkClr = pItem->crBkClr;
  145. if (pItem->mask & GVIF_FGCLR) pCell->crFgClr = pItem->crFgClr;
  146. if (pItem->mask & GVIF_FONT) memcpy(&(pCell->lfFont), &(pItem->lfFont), sizeof(LOGFONT));
  147. return TRUE;
  148. }
  149. //***************************************************
  150. // set item text by combobox index
  151. //**************************************************
  152. BOOL CComboGridCtrl::SetComboItemText(int nRow, int nCol, int index)
  153. {
  154. int comIndex=IsColumnCombo(nCol);
  155. if(comIndex==-1) return FALSE;
  156. COMBOCOL combocol=m_comStrArray.GetAt(comIndex);
  157. if(index>=combocol.comStr->GetSize()) return FALSE;
  158. return CGridCtrl::SetItemText(nRow,nCol,combocol.comStr->GetAt(index));
  159. }
  160. //********************************************************
  161. // set combobox style
  162. //********************************************************
  163. void CComboGridCtrl::SetComboBoxStyle(DWORD dwStyle)
  164. {
  165. m_comboBoxStyle=dwStyle;
  166. }