123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- #include "stdafx.h"
- #include "InPlaceList.h"
- #include "GridCtrl.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CComboEdit::CComboEdit()
- {
- }
- CComboEdit::~CComboEdit()
- {
- }
- BOOL CComboEdit::PreTranslateMessage(MSG* pMsg)
- {
-
- if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_KEYUP)
- {
- ::TranslateMessage(pMsg);
- ::DispatchMessage(pMsg);
- return TRUE;
- }
-
- if (pMsg->message == WM_SYSCHAR)
- return TRUE;
- return CEdit::PreTranslateMessage(pMsg);
- }
- BEGIN_MESSAGE_MAP(CComboEdit, CEdit)
-
- ON_WM_KILLFOCUS()
- ON_WM_KEYDOWN()
- ON_WM_KEYUP()
-
- END_MESSAGE_MAP()
- void CComboEdit::OnKillFocus(CWnd* pNewWnd)
- {
- CEdit::OnKillFocus(pNewWnd);
-
- CInPlaceList* pOwner = (CInPlaceList*) GetOwner();
- if (pOwner)
- pOwner->EndEdit();
- }
- void CComboEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if ((nChar == VK_PRIOR || nChar == VK_NEXT ||
- nChar == VK_DOWN || nChar == VK_UP ||
- nChar == VK_RIGHT || nChar == VK_LEFT) &&
- (GetKeyState(VK_CONTROL) < 0 && GetDlgCtrlID() == IDC_COMBOEDIT))
- {
- CWnd* pOwner = GetOwner();
- if (pOwner)
- pOwner->SendMessage(WM_KEYDOWN, nChar, nRepCnt+ (((DWORD)nFlags)<<16));
- return;
- }
- CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CComboEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if (nChar == VK_ESCAPE)
- {
- CWnd* pOwner = GetOwner();
- if (pOwner)
- pOwner->SendMessage(WM_KEYUP, nChar, nRepCnt + (((DWORD)nFlags)<<16));
- return;
- }
- if (nChar == VK_TAB || nChar == VK_RETURN || nChar == VK_ESCAPE)
- {
- CWnd* pOwner = GetOwner();
- if (pOwner)
- pOwner->SendMessage(WM_KEYUP, nChar, nRepCnt + (((DWORD)nFlags)<<16));
- return;
- }
- CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
- }
- CInPlaceList::CInPlaceList(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID,
- int nRow, int nColumn,
- CStringArray& Items, CString sInitText,
- UINT nFirstChar)
- {
- m_nNumLines = 4;
- m_sInitText = sInitText;
- m_nRow = nRow;
- m_nCol = nColumn;
- m_nLastChar = 0;
- m_bExitOnArrows = FALSE;
-
- DWORD dwComboStyle = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL|
- CBS_AUTOHSCROLL | dwStyle;
- int nHeight = rect.Height();
- rect.bottom = rect.bottom + m_nNumLines*nHeight + ::GetSystemMetrics(SM_CYHSCROLL);
- if (!Create(dwComboStyle, rect, pParent, nID)) return;
-
- for (int i = 0; i < Items.GetSize(); i++)
- AddString(Items[i]);
-
- int nMaxLength = 0;
- CClientDC dc(GetParent());
- CFont* pOldFont = dc.SelectObject(pParent->GetFont());
- for (int i = 0; i < Items.GetSize(); i++)
- nMaxLength = max(nMaxLength, dc.GetTextExtent(Items[i]).cx);
- nMaxLength += (::GetSystemMetrics(SM_CXVSCROLL) + dc.GetTextExtent(_T(" ")).cx*2);
- dc.SelectObject(pOldFont);
- if (nMaxLength > rect.Width())
- rect.right = rect.left + nMaxLength;
-
- MoveWindow(rect);
- SetFont(pParent->GetFont());
- SetItemHeight(-1, nHeight);
- SetDroppedWidth(nMaxLength);
- SetHorizontalExtent(0);
-
- if (SelectString(-1, m_sInitText) == CB_ERR)
- SetWindowText(m_sInitText);
-
- if ((dwStyle & CBS_DROPDOWNLIST) != CBS_DROPDOWNLIST)
- {
- m_edit.SubclassDlgItem(IDC_COMBOEDIT, this);
- SetFocus();
- switch (nFirstChar)
- {
- case VK_LBUTTON:
- case VK_RETURN: m_edit.SetSel((int)_tcslen(m_sInitText), -1); return;
- case VK_BACK: m_edit.SetSel((int)_tcslen(m_sInitText), -1); break;
- case VK_DOWN:
- case VK_UP:
- case VK_RIGHT:
- case VK_LEFT:
- case VK_NEXT:
- case VK_PRIOR:
- case VK_HOME:
- case VK_END: m_edit.SetSel(0,-1); return;
- default: m_edit.SetSel(0,-1);
- }
- SendMessage(WM_CHAR, nFirstChar);
- }
- else
- SetFocus();
- }
- CInPlaceList::~CInPlaceList()
- {
- }
- void CInPlaceList::EndEdit()
- {
- CString str;
- GetWindowText(str);
-
-
- GV_DISPINFO dispinfo;
- dispinfo.hdr.hwndFrom = GetSafeHwnd();
- dispinfo.hdr.idFrom = GetDlgCtrlID();
- dispinfo.hdr.code = GVN_ENDLABELEDIT;
-
- dispinfo.item.mask = LVIF_TEXT|LVIF_PARAM;
- dispinfo.item.row = m_nRow;
- dispinfo.item.col = m_nCol;
- dispinfo.item.szText = str;
- dispinfo.item.lParam = (LPARAM) m_nLastChar;
-
- CWnd* pOwner = GetOwner();
- if (IsWindow(pOwner->GetSafeHwnd()))
- pOwner->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo );
-
-
- PostMessage(WM_CLOSE, 0, 0);
- }
- void CInPlaceList::PostNcDestroy()
- {
- CComboBox::PostNcDestroy();
- delete this;
- }
- BEGIN_MESSAGE_MAP(CInPlaceList, CComboBox)
-
- ON_WM_KILLFOCUS()
- ON_WM_KEYDOWN()
- ON_WM_KEYUP()
-
- END_MESSAGE_MAP()
- void CInPlaceList::OnKillFocus(CWnd* pNewWnd)
- {
- CComboBox::OnKillFocus(pNewWnd);
- if (GetSafeHwnd() == pNewWnd->GetSafeHwnd())
- return;
-
- if ((GetStyle() & CBS_DROPDOWNLIST) == CBS_DROPDOWNLIST)
- EndEdit();
- }
- void CInPlaceList::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if ((nChar == VK_PRIOR || nChar == VK_NEXT ||
- nChar == VK_DOWN || nChar == VK_UP ||
- nChar == VK_RIGHT || nChar == VK_LEFT) &&
- (m_bExitOnArrows || GetKeyState(VK_CONTROL) < 0))
- {
- m_nLastChar = nChar;
- GetParent()->SetFocus();
- return;
- }
- CComboBox::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CInPlaceList::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if (nChar == VK_ESCAPE)
- SetWindowText(m_sInitText);
- if (nChar == VK_TAB || nChar == VK_RETURN || nChar == VK_ESCAPE)
- {
- m_nLastChar = nChar;
- GetParent()->SetFocus();
- return;
- }
- CComboBox::OnKeyUp(nChar, nRepCnt, nFlags);
- }
|