1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2003-2008 - TortoiseSVN
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
20 #include "HistoryCombo.h"
\r
21 #include "../registry.h"
\r
23 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
\r
24 #include "SysImageList.h"
\r
27 #define MAX_HISTORY_ITEMS 25
\r
29 CHistoryCombo::CHistoryCombo(BOOL bAllowSortStyle /*=FALSE*/ )
\r
31 m_nMaxHistoryItems = MAX_HISTORY_ITEMS;
\r
32 m_bAllowSortStyle = bAllowSortStyle;
\r
33 m_bURLHistory = FALSE;
\r
34 m_bPathHistory = FALSE;
\r
35 m_hWndToolTip = NULL;
\r
40 CHistoryCombo::~CHistoryCombo()
\r
44 BOOL CHistoryCombo::PreCreateWindow(CREATESTRUCT& cs)
\r
46 if (!m_bAllowSortStyle) //turn off CBS_SORT style
\r
47 cs.style &= ~CBS_SORT;
\r
48 cs.style |= CBS_AUTOHSCROLL;
\r
50 return CComboBoxEx::PreCreateWindow(cs);
\r
53 BOOL CHistoryCombo::PreTranslateMessage(MSG* pMsg)
\r
55 if (pMsg->message == WM_KEYDOWN)
\r
57 bool bShift = !!(GetKeyState(VK_SHIFT) & 0x8000);
\r
58 int nVirtKey = (int) pMsg->wParam;
\r
60 if (nVirtKey == VK_RETURN)
\r
61 return OnReturnKeyPressed();
\r
62 else if (nVirtKey == VK_DELETE && bShift && GetDroppedState() )
\r
64 RemoveSelectedItem();
\r
68 if (pMsg->message == WM_MOUSEMOVE)
\r
70 if ((pMsg->wParam & MK_LBUTTON) == 0)
\r
73 pt.x = LOWORD(pMsg->lParam);
\r
74 pt.y = HIWORD(pMsg->lParam);
\r
75 OnMouseMove(pMsg->wParam, pt);
\r
79 return CComboBoxEx::PreTranslateMessage(pMsg);
\r
82 BEGIN_MESSAGE_MAP(CHistoryCombo, CComboBoxEx)
\r
88 int CHistoryCombo::AddString(CString str, INT_PTR pos)
\r
93 COMBOBOXEXITEM cbei;
\r
94 SecureZeroMemory(&cbei, sizeof cbei);
\r
95 cbei.mask = CBEIF_TEXT;
\r
98 cbei.iItem = GetCount();
\r
103 CString combostring = str;
\r
104 combostring.Replace('\r', ' ');
\r
105 combostring.Replace('\n', ' ');
\r
106 cbei.pszText = const_cast<LPTSTR>(combostring.GetString());
\r
108 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
\r
111 cbei.iImage = SYS_IMAGE_LIST().GetFileIconIndex(str);
\r
112 if (cbei.iImage == SYS_IMAGE_LIST().GetDefaultIconIndex())
\r
114 if (str.Left(5) == _T("http:"))
\r
115 cbei.iImage = SYS_IMAGE_LIST().GetFileIconIndex(_T(".html"));
\r
116 else if (str.Left(6) == _T("https:"))
\r
117 cbei.iImage = SYS_IMAGE_LIST().GetFileIconIndex(_T(".shtml"));
\r
118 else if (str.Left(5) == _T("file:"))
\r
119 cbei.iImage = SYS_IMAGE_LIST().GetDirIconIndex();
\r
120 else if (str.Left(4) == _T("svn:"))
\r
121 cbei.iImage = SYS_IMAGE_LIST().GetDirIconIndex();
\r
122 else if (str.Left(8) == _T("svn+ssh:"))
\r
123 cbei.iImage = SYS_IMAGE_LIST().GetDirIconIndex();
\r
125 cbei.iSelectedImage = cbei.iImage;
\r
126 cbei.mask |= CBEIF_IMAGE | CBEIF_SELECTEDIMAGE;
\r
128 if (m_bPathHistory)
\r
130 cbei.iImage = SYS_IMAGE_LIST().GetFileIconIndex(str);
\r
131 if (cbei.iImage == SYS_IMAGE_LIST().GetDefaultIconIndex())
\r
133 cbei.iImage = SYS_IMAGE_LIST().GetDirIconIndex();
\r
135 cbei.iSelectedImage = cbei.iImage;
\r
136 cbei.mask |= CBEIF_IMAGE | CBEIF_SELECTEDIMAGE;
\r
139 int nRet = InsertItem(&cbei);
\r
141 m_arEntries.InsertAt(nRet, str);
\r
143 //search the Combo for another string like this
\r
144 //and delete it if one is found
\r
146 int nIndex = FindStringExact(0, combostring);
\r
147 if (nIndex != -1 && nIndex != nRet)
\r
149 DeleteItem(nIndex);
\r
150 m_arEntries.RemoveAt(nIndex);
\r
153 //truncate list to m_nMaxHistoryItems
\r
154 int nNumItems = GetCount();
\r
155 for (int n = m_nMaxHistoryItems; n < nNumItems; n++)
\r
157 DeleteItem(m_nMaxHistoryItems);
\r
158 m_arEntries.RemoveAt(m_nMaxHistoryItems);
\r
165 CString CHistoryCombo::LoadHistory(LPCTSTR lpszSection, LPCTSTR lpszKeyPrefix)
\r
167 if (lpszSection == NULL || lpszKeyPrefix == NULL || *lpszSection == '\0')
\r
170 m_sSection = lpszSection;
\r
171 m_sKeyPrefix = lpszKeyPrefix;
\r
177 //keys are of form <lpszKeyPrefix><entrynumber>
\r
179 sKey.Format(_T("%s\\%s%d"), (LPCTSTR)m_sSection, (LPCTSTR)m_sKeyPrefix, n++);
\r
180 sText = CRegString(sKey);
\r
181 if (!sText.IsEmpty())
\r
183 } while (!sText.IsEmpty() && n < m_nMaxHistoryItems);
\r
187 ModifyStyleEx(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE, 0);
\r
189 // need to resize the control for correct display
\r
191 GetWindowRect(rect);
\r
192 GetParent()->ScreenToClient(rect);
\r
193 MoveWindow(rect.left, rect.top, rect.Width(), 100);
\r
198 void CHistoryCombo::SaveHistory()
\r
200 if (m_sSection.IsEmpty())
\r
203 //add the current item to the history
\r
205 GetWindowText(sCurItem);
\r
207 if (!sCurItem.IsEmpty())
\r
208 AddString(sCurItem, 0);
\r
209 //save history to registry/inifile
\r
210 int nMax = min(GetCount(), m_nMaxHistoryItems + 1);
\r
211 for (int n = 0; n < nMax; n++)
\r
214 sKey.Format(_T("%s\\%s%d"), (LPCTSTR)m_sSection, (LPCTSTR)m_sKeyPrefix, n);
\r
215 CRegString regkey = CRegString(sKey);
\r
216 regkey = m_arEntries.GetAt(n);
\r
218 //remove items exceeding the max number of history items
\r
219 for (int n = nMax; ; n++)
\r
222 sKey.Format(_T("%s\\%s%d"), (LPCTSTR)m_sSection, (LPCTSTR)m_sKeyPrefix, n);
\r
223 CRegString regkey = CRegString(sKey);
\r
224 CString sText = regkey;
\r
225 if (sText.IsEmpty())
\r
227 regkey.removeValue(); // remove entry
\r
231 void CHistoryCombo::ClearHistory(BOOL bDeleteRegistryEntries/*=TRUE*/)
\r
234 if (! m_sSection.IsEmpty() && bDeleteRegistryEntries)
\r
236 //remove profile entries
\r
238 for (int n = 0; ; n++)
\r
240 sKey.Format(_T("%s\\%s%d"), (LPCTSTR)m_sSection, (LPCTSTR)m_sKeyPrefix, n);
\r
241 CRegString regkey = CRegString(sKey);
\r
242 CString sText = regkey;
\r
243 if (sText.IsEmpty())
\r
245 regkey.removeValue(); // remove entry
\r
250 void CHistoryCombo::SetURLHistory(BOOL bURLHistory)
\r
252 m_bURLHistory = bURLHistory;
\r
258 hwndEdit = (HWND)::SendMessage(this->m_hWnd, CBEM_GETEDITCONTROL, 0, 0);
\r
259 if (NULL == hwndEdit)
\r
261 // Try the unofficial way of getting the edit control CWnd*
\r
262 CWnd* pWnd = this->GetDlgItem(1001);
\r
265 hwndEdit = pWnd->GetSafeHwnd();
\r
269 SHAutoComplete(hwndEdit, SHACF_URLALL);
\r
272 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
\r
273 SetImageList(&SYS_IMAGE_LIST());
\r
277 void CHistoryCombo::SetPathHistory(BOOL bPathHistory)
\r
279 m_bPathHistory = bPathHistory;
\r
281 if (m_bPathHistory)
\r
285 hwndEdit = (HWND)::SendMessage(this->m_hWnd, CBEM_GETEDITCONTROL, 0, 0);
\r
286 if (NULL == hwndEdit)
\r
288 //if not, try the old standby
\r
291 CWnd* pWnd = this->GetDlgItem(1001);
\r
294 hwndEdit = pWnd->GetSafeHwnd();
\r
299 SHAutoComplete(hwndEdit, SHACF_FILESYSTEM);
\r
302 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
\r
303 SetImageList(&SYS_IMAGE_LIST());
\r
307 void CHistoryCombo::SetMaxHistoryItems(int nMaxItems)
\r
309 m_nMaxHistoryItems = nMaxItems;
\r
311 //truncate list to nMaxItems
\r
312 int nNumItems = GetCount();
\r
313 for (int n = m_nMaxHistoryItems; n < nNumItems; n++)
\r
314 DeleteString(m_nMaxHistoryItems);
\r
317 CString CHistoryCombo::GetString() const
\r
324 GetWindowText(str);
\r
327 if ((m_bURLHistory)||(m_bPathHistory))
\r
329 //URL and path history combo boxes are editable, so get
\r
330 //the string directly from the combobox
\r
331 GetLBText(sel, str.GetBuffer(GetLBTextLen(sel)));
\r
332 str.ReleaseBuffer();
\r
335 return m_arEntries.GetAt(sel);
\r
338 BOOL CHistoryCombo::RemoveSelectedItem()
\r
340 int nIndex = GetCurSel();
\r
341 if (nIndex == CB_ERR)
\r
346 DeleteItem(nIndex);
\r
347 m_arEntries.RemoveAt(nIndex);
\r
349 if ( nIndex < GetCount() )
\r
351 // index stays the same to select the
\r
352 // next item after the item which has
\r
353 // just been deleted
\r
357 // the end of the list has been reached
\r
358 // so we select the previous item
\r
362 if ( nIndex == -1 )
\r
364 // The one and only item has just been
\r
365 // deleted -> reset window text since
\r
366 // there is no item to select
\r
367 SetWindowText(_T(""));
\r
374 // Since the dialog might be canceled we
\r
375 // should now save the history. Before that
\r
376 // set the selection to the first item so that
\r
377 // the items will not be reordered and restore
\r
378 // the selection after saving.
\r
381 if ( nIndex != -1 )
\r
389 void CHistoryCombo::PreSubclassWindow()
\r
391 CComboBoxEx::PreSubclassWindow();
\r
397 void CHistoryCombo::OnMouseMove(UINT nFlags, CPoint point)
\r
400 GetClientRect(&rectClient);
\r
401 int nComboButtonWidth = ::GetSystemMetrics(SM_CXHTHUMB) + 2;
\r
402 rectClient.right = rectClient.right - nComboButtonWidth;
\r
404 if (rectClient.PtInRect(point))
\r
406 ClientToScreen(&rectClient);
\r
408 CString strText = GetString();
\r
409 m_ToolInfo.lpszText = (LPTSTR)(LPCTSTR)strText;
\r
411 HDC hDC = ::GetDC(m_hWnd);
\r
413 CFont *pFont = GetFont();
\r
414 HFONT hOldFont = (HFONT) ::SelectObject(hDC, (HFONT) *pFont);
\r
417 ::GetTextExtentPoint32(hDC, strText, strText.GetLength(), &size);
\r
418 ::SelectObject(hDC, hOldFont);
\r
419 ::ReleaseDC(m_hWnd, hDC);
\r
421 if (size.cx > (rectClient.Width() - 6))
\r
423 rectClient.left += 1;
\r
424 rectClient.top += 3;
\r
426 COLORREF rgbText = ::GetSysColor(COLOR_WINDOWTEXT);
\r
427 COLORREF rgbBackground = ::GetSysColor(COLOR_WINDOW);
\r
429 CWnd *pWnd = GetFocus();
\r
432 if (pWnd->m_hWnd == m_hWnd)
\r
434 rgbText = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
\r
435 rgbBackground = ::GetSysColor(COLOR_HIGHLIGHT);
\r
441 ::SendMessage(m_hWndToolTip, TTM_SETTIPBKCOLOR, rgbBackground, 0);
\r
442 ::SendMessage(m_hWndToolTip, TTM_SETTIPTEXTCOLOR, rgbText, 0);
\r
443 ::SendMessage(m_hWndToolTip, TTM_UPDATETIPTEXT, 0, (LPARAM) &m_ToolInfo);
\r
444 ::SendMessage(m_hWndToolTip, TTM_TRACKPOSITION, 0, (LPARAM)MAKELONG(rectClient.left, rectClient.top));
\r
445 ::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)(LPTOOLINFO) &m_ToolInfo);
\r
446 SetTimer(1, 80, NULL);
\r
447 SetTimer(2, 2000, NULL);
\r
453 ::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_ToolInfo);
\r
459 ::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_ToolInfo);
\r
463 CComboBoxEx::OnMouseMove(nFlags, point);
\r
466 void CHistoryCombo::OnTimer(UINT_PTR nIDEvent)
\r
469 DWORD ptW = GetMessagePos();
\r
470 point.x = GET_X_LPARAM(ptW);
\r
471 point.y = GET_Y_LPARAM(ptW);
\r
472 ScreenToClient(&point);
\r
475 GetClientRect(&rectClient);
\r
476 int nComboButtonWidth = ::GetSystemMetrics(SM_CXHTHUMB) + 2;
\r
478 rectClient.right = rectClient.right - nComboButtonWidth;
\r
480 if (!rectClient.PtInRect(point))
\r
482 KillTimer(nIDEvent);
\r
483 ::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_ToolInfo);
\r
488 // tooltip timeout, just deactivate it
\r
489 ::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_ToolInfo);
\r
490 // don't set m_ttShown to FALSE, because we don't want the tooltip to show up again
\r
491 // without the mouse pointer first leaving the control and entering it again
\r
494 CComboBoxEx::OnTimer(nIDEvent);
\r
497 void CHistoryCombo::CreateToolTip()
\r
500 m_hWndToolTip = ::CreateWindowEx(WS_EX_TOPMOST,
\r
503 TTS_NOPREFIX | TTS_ALWAYSTIP,
\r
513 // initialize tool info struct
\r
514 memset(&m_ToolInfo, 0, sizeof(m_ToolInfo));
\r
515 m_ToolInfo.cbSize = sizeof(m_ToolInfo);
\r
516 m_ToolInfo.uFlags = TTF_TRANSPARENT;
\r
517 m_ToolInfo.hwnd = m_hWnd;
\r
519 ::SendMessage(m_hWndToolTip, TTM_SETMAXTIPWIDTH, 0, SHRT_MAX);
\r
520 ::SendMessage(m_hWndToolTip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_ToolInfo);
\r
521 ::SendMessage(m_hWndToolTip, TTM_SETTIPBKCOLOR, ::GetSysColor(COLOR_HIGHLIGHT), 0);
\r
522 ::SendMessage(m_hWndToolTip, TTM_SETTIPTEXTCOLOR, ::GetSysColor(COLOR_HIGHLIGHTTEXT), 0);
\r
524 CRect rectMargins(0,-1,0,-1);
\r
525 ::SendMessage(m_hWndToolTip, TTM_SETMARGIN, 0, (LPARAM)&rectMargins);
\r
527 CFont *pFont = GetFont();
\r
528 ::SendMessage(m_hWndToolTip, WM_SETFONT, (WPARAM)(HFONT)*pFont, FALSE);
\r
531 int CHistoryCombo::OnCreate(LPCREATESTRUCT lpCreateStruct)
\r
533 if (CComboBoxEx::OnCreate(lpCreateStruct) == -1)
\r