1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2012 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "HistoryCombo.h"
21 #include "../registry.h"
23 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
24 #include "../SysImageList.h"
27 #define MAX_HISTORY_ITEMS 25
29 CHistoryCombo::CHistoryCombo(BOOL bAllowSortStyle
/*=FALSE*/ )
31 m_nMaxHistoryItems
= MAX_HISTORY_ITEMS
;
32 m_bAllowSortStyle
= bAllowSortStyle
;
33 m_bURLHistory
= FALSE
;
34 m_bPathHistory
= FALSE
;
38 m_bWantReturn
= FALSE
;
40 SecureZeroMemory(&m_ToolInfo
, sizeof(m_ToolInfo
));
43 CHistoryCombo::~CHistoryCombo()
47 BOOL
CHistoryCombo::PreCreateWindow(CREATESTRUCT
& cs
)
49 if (!m_bAllowSortStyle
) //turn off CBS_SORT style
50 cs
.style
&= ~CBS_SORT
;
51 cs
.style
|= CBS_AUTOHSCROLL
;
53 return CComboBoxEx::PreCreateWindow(cs
);
56 BOOL
CHistoryCombo::PreTranslateMessage(MSG
* pMsg
)
58 if (pMsg
->message
== WM_KEYDOWN
)
60 bool bShift
= !!(GetKeyState(VK_SHIFT
) & 0x8000);
61 int nVirtKey
= (int) pMsg
->wParam
;
63 if (nVirtKey
== VK_RETURN
)
64 return OnReturnKeyPressed();
65 else if (nVirtKey
== VK_DELETE
&& bShift
&& GetDroppedState() )
71 if (nVirtKey
== 'A' && (GetKeyState(VK_CONTROL
) & 0x8000 ) )
73 CEdit
*edit
= GetEditCtrl();
79 else if (pMsg
->message
== WM_MOUSEMOVE
&& this->m_bDyn
)
81 if ((pMsg
->wParam
& MK_LBUTTON
) == 0)
84 pt
.x
= LOWORD(pMsg
->lParam
);
85 pt
.y
= HIWORD(pMsg
->lParam
);
86 OnMouseMove((UINT
)pMsg
->wParam
, pt
);
90 else if ((pMsg
->message
== WM_MOUSEWHEEL
|| pMsg
->message
== WM_MOUSEHWHEEL
) && !GetDroppedState())
95 return CComboBoxEx::PreTranslateMessage(pMsg
);
98 BEGIN_MESSAGE_MAP(CHistoryCombo
, CComboBoxEx
)
104 int CHistoryCombo::AddString(CString str
, INT_PTR pos
,BOOL isSel
)
110 SecureZeroMemory(&cbei
, sizeof cbei
);
111 cbei
.mask
= CBEIF_TEXT
;
114 cbei
.iItem
= GetCount();
120 CString combostring
= str
;
121 combostring
.Replace('\r', ' ');
122 combostring
.Replace('\n', ' ');
125 cbei
.pszText
= const_cast<LPTSTR
>(combostring
.GetString());
127 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
130 cbei
.iImage
= SYS_IMAGE_LIST().GetFileIconIndex(str
);
131 if (cbei
.iImage
== SYS_IMAGE_LIST().GetDefaultIconIndex())
133 if (str
.Left(5) == _T("http:"))
134 cbei
.iImage
= SYS_IMAGE_LIST().GetFileIconIndex(_T(".html"));
135 else if (str
.Left(6) == _T("https:"))
136 cbei
.iImage
= SYS_IMAGE_LIST().GetFileIconIndex(_T(".shtml"));
137 else if (str
.Left(5) == _T("file:"))
138 cbei
.iImage
= SYS_IMAGE_LIST().GetDirIconIndex();
139 else if (str
.Left(4) == _T("git:"))
140 cbei
.iImage
= SYS_IMAGE_LIST().GetDirIconIndex();
141 else if (str
.Left(4) == _T("ssh:"))
142 cbei
.iImage
= SYS_IMAGE_LIST().GetDirIconIndex();
144 cbei
.iImage
= SYS_IMAGE_LIST().GetDirIconIndex();
146 cbei
.iSelectedImage
= cbei
.iImage
;
147 cbei
.mask
|= CBEIF_IMAGE
| CBEIF_SELECTEDIMAGE
;
151 cbei
.iImage
= SYS_IMAGE_LIST().GetFileIconIndex(str
);
152 if (cbei
.iImage
== SYS_IMAGE_LIST().GetDefaultIconIndex())
154 cbei
.iImage
= SYS_IMAGE_LIST().GetDirIconIndex();
156 cbei
.iSelectedImage
= cbei
.iImage
;
157 cbei
.mask
|= CBEIF_IMAGE
| CBEIF_SELECTEDIMAGE
;
161 //search the Combo for another string like this
162 //and do not insert if found
163 int nIndex
= FindStringExact(-1, combostring
);
166 if (nIndex
> cbei
.iItem
)
169 m_arEntries
.RemoveAt(nIndex
);
179 int nRet
= InsertItem(&cbei
);
181 m_arEntries
.InsertAt(nRet
, str
);
183 //truncate list to m_nMaxHistoryItems
184 int nNumItems
= GetCount();
185 for (int n
= m_nMaxHistoryItems
; n
< nNumItems
; n
++)
187 DeleteItem(m_nMaxHistoryItems
);
188 m_arEntries
.RemoveAt(m_nMaxHistoryItems
);
196 CString
CHistoryCombo::LoadHistory(LPCTSTR lpszSection
, LPCTSTR lpszKeyPrefix
)
198 if (lpszSection
== NULL
|| lpszKeyPrefix
== NULL
|| *lpszSection
== '\0')
201 m_sSection
= lpszSection
;
202 m_sKeyPrefix
= lpszKeyPrefix
;
208 //keys are of form <lpszKeyPrefix><entrynumber>
210 sKey
.Format(_T("%s\\%s%d"), (LPCTSTR
)m_sSection
, (LPCTSTR
)m_sKeyPrefix
, n
++);
211 sText
= CRegString(sKey
);
212 if (!sText
.IsEmpty())
214 } while (!sText
.IsEmpty() && n
< m_nMaxHistoryItems
);
218 ModifyStyleEx(WS_EX_DLGMODALFRAME
| WS_EX_WINDOWEDGE
, 0);
220 // need to resize the control for correct display
223 GetParent()->ScreenToClient(rect
);
224 MoveWindow(rect
.left
, rect
.top
, rect
.Width(),100);
229 void CHistoryCombo::SaveHistory()
231 if (m_sSection
.IsEmpty())
234 //add the current item to the history
236 GetWindowText(sCurItem
);
239 if (!sCurItem
.IsEmpty())
240 AddString(sCurItem
, 0);
241 //save history to registry/inifile
242 int nMax
= min(GetCount(), m_nMaxHistoryItems
+ 1);
243 for (int n
= 0; n
< nMax
; n
++)
246 sKey
.Format(_T("%s\\%s%d"), (LPCTSTR
)m_sSection
, (LPCTSTR
)m_sKeyPrefix
, n
);
247 CRegString regkey
= CRegString(sKey
);
248 regkey
= m_arEntries
.GetAt(n
);
250 //remove items exceeding the max number of history items
251 for (int n
= nMax
; ; n
++)
254 sKey
.Format(_T("%s\\%s%d"), (LPCTSTR
)m_sSection
, (LPCTSTR
)m_sKeyPrefix
, n
);
255 CRegString regkey
= CRegString(sKey
);
256 CString sText
= regkey
;
259 regkey
.removeValue(); // remove entry
263 void CHistoryCombo::ClearHistory(BOOL bDeleteRegistryEntries
/*=TRUE*/)
266 if (! m_sSection
.IsEmpty() && bDeleteRegistryEntries
)
268 //remove profile entries
270 for (int n
= 0; ; n
++)
272 sKey
.Format(_T("%s\\%s%d"), (LPCTSTR
)m_sSection
, (LPCTSTR
)m_sKeyPrefix
, n
);
273 CRegString regkey
= CRegString(sKey
);
274 CString sText
= regkey
;
277 regkey
.removeValue(); // remove entry
282 void CHistoryCombo::SetURLHistory(BOOL bURLHistory
)
284 m_bURLHistory
= bURLHistory
;
290 hwndEdit
= (HWND
)::SendMessage(this->m_hWnd
, CBEM_GETEDITCONTROL
, 0, 0);
291 if (NULL
== hwndEdit
)
293 // Try the unofficial way of getting the edit control CWnd*
294 CWnd
* pWnd
= this->GetDlgItem(1001);
297 hwndEdit
= pWnd
->GetSafeHwnd();
301 SHAutoComplete(hwndEdit
, SHACF_URLALL
);
304 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
305 SetImageList(&SYS_IMAGE_LIST());
309 void CHistoryCombo::SetPathHistory(BOOL bPathHistory
)
311 m_bPathHistory
= bPathHistory
;
317 hwndEdit
= (HWND
)::SendMessage(this->m_hWnd
, CBEM_GETEDITCONTROL
, 0, 0);
318 if (NULL
== hwndEdit
)
320 //if not, try the old standby
323 CWnd
* pWnd
= this->GetDlgItem(1001);
326 hwndEdit
= pWnd
->GetSafeHwnd();
331 SHAutoComplete(hwndEdit
, SHACF_FILESYSTEM
);
334 #ifdef HISTORYCOMBO_WITH_SYSIMAGELIST
335 SetImageList(&SYS_IMAGE_LIST());
339 void CHistoryCombo::SetMaxHistoryItems(int nMaxItems
)
341 m_nMaxHistoryItems
= nMaxItems
;
343 //truncate list to nMaxItems
344 int nNumItems
= GetCount();
345 for (int n
= m_nMaxHistoryItems
; n
< nNumItems
; n
++)
346 DeleteString(m_nMaxHistoryItems
);
348 void CHistoryCombo::AddString(STRING_VECTOR
&list
,BOOL isSel
)
350 for(unsigned int i
=0;i
<list
.size();i
++)
352 AddString(list
[i
], -1, isSel
);
355 CString
CHistoryCombo::GetString() const
360 DWORD style
=GetStyle();
362 if (sel
== CB_ERR
||(m_bURLHistory
)||(m_bPathHistory
) || (!(style
&CBS_SIMPLE
)))
368 return m_arEntries
.GetAt(sel
);
371 BOOL
CHistoryCombo::RemoveSelectedItem()
373 int nIndex
= GetCurSel();
374 if (nIndex
== CB_ERR
)
380 m_arEntries
.RemoveAt(nIndex
);
382 if ( nIndex
< GetCount() )
384 // index stays the same to select the
385 // next item after the item which has
390 // the end of the list has been reached
391 // so we select the previous item
397 // The one and only item has just been
398 // deleted -> reset window text since
399 // there is no item to select
400 SetWindowText(_T(""));
407 // Since the dialog might be canceled we
408 // should now save the history. Before that
409 // set the selection to the first item so that
410 // the items will not be reordered and restore
411 // the selection after saving.
422 void CHistoryCombo::PreSubclassWindow()
424 CComboBoxEx::PreSubclassWindow();
430 void CHistoryCombo::OnMouseMove(UINT nFlags
, CPoint point
)
433 GetClientRect(&rectClient
);
434 int nComboButtonWidth
= ::GetSystemMetrics(SM_CXHTHUMB
) + 2;
435 rectClient
.right
= rectClient
.right
- nComboButtonWidth
;
437 if (rectClient
.PtInRect(point
))
439 ClientToScreen(&rectClient
);
441 CString strText
= GetString();
442 m_ToolInfo
.lpszText
= (LPTSTR
)(LPCTSTR
)strText
;
444 HDC hDC
= ::GetDC(m_hWnd
);
446 CFont
*pFont
= GetFont();
447 HFONT hOldFont
= (HFONT
) ::SelectObject(hDC
, (HFONT
) *pFont
);
450 ::GetTextExtentPoint32(hDC
, strText
, strText
.GetLength(), &size
);
451 ::SelectObject(hDC
, hOldFont
);
452 ::ReleaseDC(m_hWnd
, hDC
);
454 if (size
.cx
> (rectClient
.Width() - 6))
456 rectClient
.left
+= 1;
459 COLORREF rgbText
= ::GetSysColor(COLOR_WINDOWTEXT
);
460 COLORREF rgbBackground
= ::GetSysColor(COLOR_WINDOW
);
462 CWnd
*pWnd
= GetFocus();
465 if (pWnd
->m_hWnd
== m_hWnd
)
467 rgbText
= ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
468 rgbBackground
= ::GetSysColor(COLOR_HIGHLIGHT
);
474 ::SendMessage(m_hWndToolTip
, TTM_SETTIPBKCOLOR
, rgbBackground
, 0);
475 ::SendMessage(m_hWndToolTip
, TTM_SETTIPTEXTCOLOR
, rgbText
, 0);
476 ::SendMessage(m_hWndToolTip
, TTM_UPDATETIPTEXT
, 0, (LPARAM
) &m_ToolInfo
);
477 ::SendMessage(m_hWndToolTip
, TTM_TRACKPOSITION
, 0, (LPARAM
)MAKELONG(rectClient
.left
, rectClient
.top
));
478 ::SendMessage(m_hWndToolTip
, TTM_TRACKACTIVATE
, TRUE
, (LPARAM
)(LPTOOLINFO
) &m_ToolInfo
);
479 SetTimer(1, 80, NULL
);
480 SetTimer(2, 2000, NULL
);
486 ::SendMessage(m_hWndToolTip
, TTM_TRACKACTIVATE
, FALSE
, (LPARAM
)(LPTOOLINFO
) &m_ToolInfo
);
492 ::SendMessage(m_hWndToolTip
, TTM_TRACKACTIVATE
, FALSE
, (LPARAM
)(LPTOOLINFO
) &m_ToolInfo
);
496 CComboBoxEx::OnMouseMove(nFlags
, point
);
499 void CHistoryCombo::OnTimer(UINT_PTR nIDEvent
)
502 DWORD ptW
= GetMessagePos();
503 point
.x
= GET_X_LPARAM(ptW
);
504 point
.y
= GET_Y_LPARAM(ptW
);
505 ScreenToClient(&point
);
508 GetClientRect(&rectClient
);
509 int nComboButtonWidth
= ::GetSystemMetrics(SM_CXHTHUMB
) + 2;
511 rectClient
.right
= rectClient
.right
- nComboButtonWidth
;
513 if (!rectClient
.PtInRect(point
))
516 ::SendMessage(m_hWndToolTip
, TTM_TRACKACTIVATE
, FALSE
, (LPARAM
)(LPTOOLINFO
) &m_ToolInfo
);
521 // tooltip timeout, just deactivate it
522 ::SendMessage(m_hWndToolTip
, TTM_TRACKACTIVATE
, FALSE
, (LPARAM
)(LPTOOLINFO
) &m_ToolInfo
);
523 // don't set m_ttShown to FALSE, because we don't want the tooltip to show up again
524 // without the mouse pointer first leaving the control and entering it again
527 CComboBoxEx::OnTimer(nIDEvent
);
530 void CHistoryCombo::CreateToolTip()
533 m_hWndToolTip
= ::CreateWindowEx(WS_EX_TOPMOST
,
536 TTS_NOPREFIX
| TTS_ALWAYSTIP
,
546 // initialize tool info struct
547 memset(&m_ToolInfo
, 0, sizeof(m_ToolInfo
));
548 m_ToolInfo
.cbSize
= sizeof(m_ToolInfo
);
549 m_ToolInfo
.uFlags
= TTF_TRANSPARENT
;
550 m_ToolInfo
.hwnd
= m_hWnd
;
552 ::SendMessage(m_hWndToolTip
, TTM_SETMAXTIPWIDTH
, 0, SHRT_MAX
);
553 ::SendMessage(m_hWndToolTip
, TTM_ADDTOOL
, 0, (LPARAM
) (LPTOOLINFO
) &m_ToolInfo
);
554 ::SendMessage(m_hWndToolTip
, TTM_SETTIPBKCOLOR
, ::GetSysColor(COLOR_HIGHLIGHT
), 0);
555 ::SendMessage(m_hWndToolTip
, TTM_SETTIPTEXTCOLOR
, ::GetSysColor(COLOR_HIGHLIGHTTEXT
), 0);
557 CRect
rectMargins(0,-1,0,-1);
558 ::SendMessage(m_hWndToolTip
, TTM_SETMARGIN
, 0, (LPARAM
)&rectMargins
);
560 CFont
*pFont
= GetFont();
561 ::SendMessage(m_hWndToolTip
, WM_SETFONT
, (WPARAM
)(HFONT
)*pFont
, FALSE
);
564 int CHistoryCombo::OnCreate(LPCREATESTRUCT lpCreateStruct
)
566 if (CComboBoxEx::OnCreate(lpCreateStruct
) == -1)