root/trunk/GUIComponents/ComboBoxSuper.cpp

Revision 5, 10.2 kB (checked in by qbert, 3 years ago)

Initial ( and last :( ) commit

Line 
1 // ComboBoxBold.cpp : implementation file
2 //
3
4 #pragma warning ( disable : 4786 )
5
6 #include "stdafx.h"
7 #include "..\elephant.h"
8 #include "ComboBoxSuper.h"
9
10 #ifdef _DEBUG
11 #define new DEBUG_NEW
12 #undef THIS_FILE
13 static char THIS_FILE[] = __FILE__;
14 #endif
15
16 const CComboBoxSuper::DEFAULT_COLUMN_COUNT = 100;
17 const CComboBoxSuper::DEFAULT_COLUMN_WIDTH = 50;
18
19 CComboBoxSuper::CComboBoxSuper()
20 : m_pImageList(NULL)
21 , m_bUseImage(TRUE)
22 {
23     m_vecColumnWidth.resize(DEFAULT_COLUMN_COUNT);
24     for (int i=0; i<DEFAULT_COLUMN_COUNT;i++)
25     {
26         m_vecColumnWidth[i] = DEFAULT_COLUMN_WIDTH;
27     }
28 }
29
30 CComboBoxSuper::~CComboBoxSuper()
31 {
32 }
33
34
35 BEGIN_MESSAGE_MAP(CComboBoxSuper, CComboBox)
36     //{{AFX_MSG_MAP(CComboBoxSuper)
37     ON_WM_DELETEITEM()
38     //}}AFX_MSG_MAP
39 END_MESSAGE_MAP()
40
41 /*********************************************************************************************
42  *
43  * @ChangesHistory
44  ********************************************************************************************/
45 void CComboBoxSuper::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
46 {
47     if (GetCount() == 0 || lpDrawItemStruct->itemID > GetCount()) return;
48
49     ItemData* pItemData = NULL;
50
51     if (lpDrawItemStruct->itemData!=NULL)
52     {
53         pItemData = (ItemData*)lpDrawItemStruct->itemData;
54     }
55
56     CString str;
57     GetLBText(lpDrawItemStruct->itemID, str);
58     CDC dc;
59     BOOL bSelected = FALSE;
60
61     dc.Attach(lpDrawItemStruct->hDC);
62
63     // Save these value to restore them when done drawing.
64     COLORREF crOldTextColor = dc.GetTextColor();
65     COLORREF crOldBkColor = dc.GetBkColor();
66
67     // If this item is selected, set the background color and the text color to appropriate
68     // values. Erase the rect by filling it with the background color.
69     if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
70         (lpDrawItemStruct->itemState & ODS_SELECTED))
71     {
72         dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
73         dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
74         dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT));
75         bSelected = TRUE;
76     }
77     else
78     {
79         dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);
80     }
81
82     CRect rect(lpDrawItemStruct->rcItem);
83     rect.DeflateRect(1,0);
84
85     // If we use images, and there is data, and the index is valid:
86     if (m_pImageList && m_bUseImage && pItemData && pItemData->nImageIndex!=-1)
87     {
88         DrawIconEx(dc.GetSafeHdc(),rect.left,rect.top,
89             m_pImageList->ExtractIcon(pItemData->nImageIndex),0, 0, 0, NULL, DI_NORMAL);
90     }
91
92     // If we use images - move text to the right:
93     if (m_bUseImage && m_pImageList)
94     {
95         IMAGEINFO sImageInfo;
96         m_pImageList->GetImageInfo(0, &sImageInfo);
97         rect.left += sImageInfo.rcImage.right;
98     }
99
100     CFont* pOldFont;
101     CFont boldFont;
102     if (pItemData && pItemData->bBold)
103     {
104         CFont* curFont = dc.GetCurrentFont();
105         LOGFONT lf;
106         curFont->GetLogFont(&lf);
107         lf.lfWeight = FW_BOLD;
108         boldFont.CreateFontIndirect(&lf);
109         pOldFont = dc.SelectObject(&boldFont);
110     }
111
112     // If the item has its own color, replace text color (exception - color is black, and
113     // the item is selected - then we leave the highlight text color)
114     if (pItemData && (!bSelected || (bSelected && pItemData->crTextColor != RGB(0,0,0))))
115     {
116         dc.SetTextColor(pItemData->crTextColor);
117     }
118
119     // If we need to display columns - a bit more complicated...
120     if (m_vecColumnWidth.size()>1)
121     {
122         CPen linePen(PS_SOLID, 0, RGB(192,192,192));
123         CPen* pOldPen = dc.SelectObject(&linePen);
124         int nCurX=0;
125         for (int i=0; i<m_vecColumnWidth.size(); i++)
126         {
127             if (i!=m_vecColumnWidth.size()-1)
128             {
129                 dc.MoveTo(rect.left+nCurX+m_vecColumnWidth[i],rect.top);
130                 dc.LineTo(rect.left+nCurX+m_vecColumnWidth[i],rect.bottom);
131             }
132             CRect rc(rect);
133             rc.left=rect.left+nCurX+1;
134             if (i!=m_vecColumnWidth.size()-1)
135             {
136                 rc.right=rect.left+nCurX+m_vecColumnWidth[i];
137             }
138             else
139             {
140                 rc.right = rect.right;
141             }
142             nCurX += m_vecColumnWidth[i];
143
144             if (i==0)
145             {
146                 dc.DrawText(str, -1, &rc, DT_LEFT|DT_SINGLELINE|DT_VCENTER);
147             }
148             else if (pItemData)
149             {
150                 if (pItemData->mapStrings.find(i)!=pItemData->mapStrings.end())
151                 {
152                     dc.DrawText(pItemData->mapStrings[i], -1, &rc, DT_LEFT|DT_SINGLELINE|DT_VCENTER);
153                 }
154             }
155         }
156
157         dc.SelectObject(pOldPen);
158     }
159     else
160     {
161         // Normal one column text display:
162         dc.DrawText(str, -1, &rect, DT_LEFT|DT_SINGLELINE|DT_VCENTER);
163     }
164
165     if (pItemData && pItemData->bBold)
166     {
167         dc.SelectObject(pOldFont);
168         boldFont.DeleteObject();
169     }
170
171     // Reset the background color and the text color back to their original values.
172     dc.SetTextColor(crOldTextColor);
173     dc.SetBkColor(crOldBkColor);
174
175     dc.Detach();
176 }
177
178 /*********************************************************************************************
179  *
180  * @ChangesHistory
181  ********************************************************************************************/
182 void CComboBoxSuper::SetItemBold(int nItemIndex,  BOOL bBold)
183 {
184     ItemData* pItemData = GetOrCreateItemData(nItemIndex);
185     if (pItemData)
186     {
187         pItemData->bBold = bBold;
188         Invalidate();
189     }
190 }
191
192 /*********************************************************************************************
193  *
194  * @ChangesHistory
195  ********************************************************************************************/
196 CString CComboBoxSuper::GetItemText(int nItemIndex,int nColumn)
197 {
198     if (0==nColumn)
199     {
200         CString str;
201         GetLBText(nItemIndex, str);
202         return str;
203     }
204
205     ItemData* pItemData = GetOrCreateItemData(nItemIndex);
206     if (pItemData)
207     {
208         return pItemData->mapStrings[nColumn];
209     }
210     else
211     {
212         return "";
213     }
214 }
215
216 /*********************************************************************************************
217  *
218  * @ChangesHistory
219  ********************************************************************************************/
220 void CComboBoxSuper::SetItemImage(int nItemIndex, int nImageIndex)
221 {
222     ItemData* pItemData = GetOrCreateItemData(nItemIndex);
223     if (pItemData)
224     {
225         pItemData->nImageIndex = nImageIndex;
226         Invalidate();
227     }
228 }
229
230 /*********************************************************************************************
231  *
232  * @ChangesHistory
233  ********************************************************************************************/
234 void CComboBoxSuper::SetItemColor(int nItemIndex, COLORREF rcTextColor)
235 {
236     ItemData* pItemData = GetOrCreateItemData(nItemIndex);
237     if (pItemData)
238     {
239         pItemData->crTextColor = rcTextColor;
240         Invalidate();
241     }
242 }
243
244 /*********************************************************************************************
245  *
246  * @ChangesHistory
247  ********************************************************************************************/
248 void CComboBoxSuper::SetItemText(int nItemIndex, int nColumn, CString str)
249 {
250     ItemData* pItemData = GetOrCreateItemData(nItemIndex);
251     if (pItemData)
252     {
253         pItemData->mapStrings[nColumn] = str;
254     }
255 }
256
257 /*********************************************************************************************
258  *
259  * @ChangesHistory
260  ********************************************************************************************/
261 void CComboBoxSuper::SetColumnWidth(int nColumnIndex, int nWidth)
262 {
263     if (nColumnIndex<0 || nColumnIndex>=m_vecColumnWidth.size()) return;
264
265     m_vecColumnWidth[nColumnIndex] = nWidth;
266
267     Invalidate();
268 }
269
270 /*********************************************************************************************
271  *
272  * @ChangesHistory
273  ********************************************************************************************/
274 void CComboBoxSuper::OnDeleteItem(int nIDCtl, LPDELETEITEMSTRUCT lpDeleteItemStruct)
275 {
276     if (lpDeleteItemStruct->itemData)
277     {
278         ItemData* pItemData = (ItemData*)lpDeleteItemStruct->itemData;
279         delete pItemData;
280         lpDeleteItemStruct->itemData = NULL;
281     }
282
283     CComboBox::OnDeleteItem(nIDCtl, lpDeleteItemStruct);
284 }
285
286 /*********************************************************************************************
287  *
288  * @ChangesHistory
289  ********************************************************************************************/
290 CComboBoxSuper::ItemData* CComboBoxSuper::GetOrCreateItemData(int nItemIndex)
291 {
292     if (nItemIndex<0 || nItemIndex>=GetCount()) return NULL;
293
294     ItemData* pItemData = (ItemData*)(CComboBox::GetItemData(nItemIndex));
295
296     if (NULL==pItemData)
297     {
298         pItemData = new ItemData;
299         CComboBox::SetItemData(nItemIndex,(DWORD)(pItemData));
300     }
301    
302     return pItemData;
303 }
304
305 /*********************************************************************************************
306  *
307  * @ChangesHistory
308  ********************************************************************************************/
309 void CComboBoxSuper::SetItemData(int nItemIndex, DWORD dwData)
310 {
311     ItemData* pItemData = GetOrCreateItemData(nItemIndex);
312     if (pItemData)
313     {
314         pItemData->dwItemData = dwData;
315     }
316 }
317
318 /*********************************************************************************************
319  *
320  * @ChangesHistory
321  ********************************************************************************************/
322 DWORD CComboBoxSuper::GetItemData(int nItemIndex)
323 {
324     ItemData* pItemData = GetOrCreateItemData(nItemIndex);
325     if (pItemData)
326     {
327         return pItemData->dwItemData;
328     }
329
330     return 0;
331 }
332
333 /*********************************************************************************************
334  *
335  * @ChangesHistory
336  ********************************************************************************************/
337 void CComboBoxSuper::SetColumnCount(int nColumnCount )
338 {
339     int nPrevColumnCount = m_vecColumnWidth.size();
340     m_vecColumnWidth.resize(nColumnCount);
341
342     for (int i=nPrevColumnCount; i<m_vecColumnWidth.size(); i++)
343     {
344         m_vecColumnWidth[i] = DEFAULT_COLUMN_WIDTH;
345     }
346
347     Invalidate();
348 }
349
350
351
352 void CComboBoxSuper::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
353 {
354     //AfxMessageBox("HERE");
355     if ( nChar == VK_ESCAPE )
356     {
357
358         CMainFrame* m = (CMainFrame*)Globals::theApp.m_pMainWnd;
359         if ( m ){
360        
361         CElephantView* v = m->GetActiveView();
362         if ( v )
363         {
364             CScintillaCtrl& rCtrl = v->GetCtrl();
365             rCtrl.SetFocus();
366         }
367
368         return;
369         }
370        
371
372
373     }   
374     //CComboBoxSuper::OnKeyUp(nChar, nRepCnt, nFlags);
375 }
376
377
378 BOOL CComboBoxSuper::PreTranslateMessage(MSG* pMsg)
379 {
380     // TODO: Add your specialized code here and/or call the base class
381    
382 return Globals::theApp.m_pMainWnd->PreTranslateMessage(pMsg);
383 }
Note: See TracBrowser for help on using the browser.