Rollback from r3608 to r3606
[public/netxms.git] / src / console / nxuilib / InPlaceCombo.cpp
1 /*******************************************************************************
2 Author : Aravindan Premkumar
3 Unregistered Copyright 2003 : Aravindan Premkumar
4 All Rights Reserved
5
6 This piece of code does not have any registered copyright and is free to be
7 used as necessary. The user is free to modify as per the requirements. As a
8 fellow developer, all that I expect and request for is to be given the
9 credit for intially developing this reusable code by not removing my name as
10 the author.
11 *******************************************************************************/
12
13 #include "stdafx.h"
14 #include "nxuilib.h"
15 #include "InPlaceCombo.h"
16
17 #ifdef _DEBUG
18 #define new DEBUG_NEW
19 #undef THIS_FILE
20 static char THIS_FILE[] = __FILE__;
21 #endif
22
23 /////////////////////////////////////////////////////////////////////////////
24 // CInPlaceCombo
25
26 CInPlaceCombo* CInPlaceCombo::m_pInPlaceCombo = NULL;
27
28 CInPlaceCombo::CInPlaceCombo()
29 {
30 m_iRowIndex = -1;
31 m_iColumnIndex = -1;
32 m_bESC = FALSE;
33
34 m_nOffset = GetSystemMetrics(SM_CXHTHUMB);
35 }
36
37 CInPlaceCombo::~CInPlaceCombo()
38 {
39 }
40
41 BEGIN_MESSAGE_MAP(CInPlaceCombo, CComboBox)
42 //{{AFX_MSG_MAP(CInPlaceCombo)
43 ON_WM_CREATE()
44 ON_WM_KILLFOCUS()
45 ON_WM_CHAR()
46 ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
47 ON_WM_PAINT()
48 //}}AFX_MSG_MAP
49 END_MESSAGE_MAP()
50
51 /////////////////////////////////////////////////////////////////////////////
52 // CInPlaceCombo message handlers
53
54 int CInPlaceCombo::OnCreate(LPCREATESTRUCT lpCreateStruct)
55 {
56 if (CComboBox::OnCreate(lpCreateStruct) == -1)
57 {
58 return -1;
59 }
60
61 //SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
62
63 // Set the proper font
64 CFont* pFont = GetParent()->GetFont();
65 SetFont(pFont);
66
67 SetFocus();
68
69 ResetContent();
70 for (POSITION Pos_ = m_DropDownList.GetHeadPosition(); Pos_ != NULL;)
71 {
72 AddString((LPCTSTR) (m_DropDownList.GetNext(Pos_)));
73 }
74
75 return 0;
76 }
77
78 BOOL CInPlaceCombo::PreTranslateMessage(MSG* pMsg)
79 {
80 // If the message if for "Enter" or "Esc"
81 // Do not process
82 if (pMsg->message == WM_KEYDOWN)
83 {
84 if(pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
85 {
86 ::TranslateMessage(pMsg);
87 ::DispatchMessage(pMsg);
88 // DO NOT process further
89 return TRUE;
90 }
91 }
92
93 return CComboBox::PreTranslateMessage(pMsg);
94 }
95
96 void CInPlaceCombo::OnKillFocus(CWnd* pNewWnd)
97 {
98 CComboBox::OnKillFocus(pNewWnd);
99
100 // Get the current selection text
101 CString str;
102 GetWindowText(str);
103
104 // Send Notification to parent of ListView ctrl
105 LV_DISPINFO dispinfo;
106 dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
107 dispinfo.hdr.idFrom = GetDlgCtrlID();
108 dispinfo.hdr.code = LVN_ENDLABELEDIT;
109
110 dispinfo.item.mask = LVIF_TEXT;
111 dispinfo.item.iItem = m_iRowIndex;
112 dispinfo.item.iSubItem = m_iColumnIndex;
113 dispinfo.item.pszText = m_bESC ? LPTSTR((LPCTSTR)m_strWindowText) : LPTSTR((LPCTSTR)str);
114 dispinfo.item.cchTextMax = m_bESC ? m_strWindowText.GetLength() : str.GetLength();
115
116 GetParent()->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM)&dispinfo);
117
118 // Close the control
119 PostMessage(WM_CLOSE);
120 }
121
122 void CInPlaceCombo::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
123 {
124 // If the key is "Esc" set focus back to the list control
125 if (nChar == VK_ESCAPE || nChar == VK_RETURN)
126 {
127 if (nChar == VK_ESCAPE)
128 {
129 m_bESC = TRUE;
130 }
131
132 GetParent()->SetFocus();
133 return;
134 }
135
136 CComboBox::OnChar(nChar, nRepCnt, nFlags);
137 }
138
139 void CInPlaceCombo::OnCloseup()
140 {
141 // Set the focus to the parent list control
142 GetParent()->SetFocus();
143 }
144
145 CInPlaceCombo* CInPlaceCombo::GetInstance()
146 {
147 if(m_pInPlaceCombo == NULL)
148 {
149 m_pInPlaceCombo = new CInPlaceCombo;
150 }
151 return m_pInPlaceCombo;
152 }
153
154 void CInPlaceCombo::DeleteInstance()
155 {
156 delete m_pInPlaceCombo;
157 m_pInPlaceCombo = NULL;
158 }
159
160 BOOL CInPlaceCombo::ShowComboCtrl(DWORD dwStyle, const CRect &rCellRect, CWnd* pParentWnd, UINT uiResourceID,
161 int iRowIndex, int iColumnIndex, CStringList* pDropDownList,
162 CString strCurSelecetion /*= ""*/, int iCurSel /*= -1*/)
163 {
164
165 m_iRowIndex = iRowIndex;
166 m_iColumnIndex = iColumnIndex;
167 m_bESC = FALSE;
168
169 m_DropDownList.RemoveAll();
170 m_DropDownList.AddTail(pDropDownList);
171
172 BOOL bRetVal = TRUE;
173
174 if (-1 != iCurSel)
175 {
176 GetLBText(iCurSel, m_strWindowText);
177 }
178 else if (!strCurSelecetion.IsEmpty())
179 {
180 m_strWindowText = strCurSelecetion;
181 }
182
183 if (NULL == m_pInPlaceCombo->m_hWnd)
184 {
185 bRetVal = m_pInPlaceCombo->Create(dwStyle, rCellRect, pParentWnd, uiResourceID);
186 // m_pInPlaceCombo->SetItemHeight(-1, 8);
187 }
188
189 SetCurSel(iCurSel);
190
191 return bRetVal;
192 }
193
194
195 //
196 // WM_PAINT message handler
197 //
198
199 void CInPlaceCombo::OnPaint()
200 {
201 COLORREF rgbBorder, rgbWindow;
202 CRect rcItem;
203 CDC *pDC;
204
205 ModifyStyleEx (WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE, 0, SWP_FRAMECHANGED);
206 Default();
207
208 rgbBorder = GetSysColor(COLOR_WINDOWFRAME);
209 rgbWindow = GetSysColor(COLOR_WINDOW);
210 GetClientRect(&rcItem);
211 pDC = GetDC();
212
213 // Cover up dark 3D shadow.
214 pDC->Draw3dRect(rcItem, rgbBorder, rgbBorder);
215 rcItem.DeflateRect(1, 1);
216 pDC->Draw3dRect(rcItem, rgbWindow, rgbWindow);
217
218 // Cover up dark 3D shadow on drop arrow.
219 rcItem.DeflateRect(1, 1);
220 rcItem.left = rcItem.right - m_nOffset;
221 pDC->Draw3dRect(rcItem, rgbWindow, rgbWindow);
222
223 // Cover up normal 3D shadow on drop arrow.
224 rcItem.DeflateRect(1, 1);
225 pDC->Draw3dRect(rcItem, rgbBorder, rgbBorder);
226
227 ReleaseDC(pDC);
228 }