1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2012, 2014 - TortoiseGit
4 // Copyright (C) 2007, 2012-2013 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "FilterEdit.h"
23 IMPLEMENT_DYNAMIC(CFilterEdit
, CEdit
)
25 CFilterEdit::CFilterEdit() : m_hIconCancelNormal(NULL
)
26 , m_hIconCancelPressed(NULL
)
29 , m_bShowCancelButtonAlways(FALSE
)
30 , m_iButtonClickedMessageId(WM_FILTEREDIT_INFOCLICKED
)
31 , m_iCancelClickedMessageId(WM_FILTEREDIT_CANCELCLICKED
)
33 , m_backColor(GetSysColor(COLOR_WINDOW
))
36 m_rcEditArea
.SetRect(0, 0, 0, 0);
37 m_rcButtonArea
.SetRect(0, 0, 0, 0);
38 m_rcInfoArea
.SetRect(0, 0, 0, 0);
39 m_sizeInfoIcon
.SetSize(0, 0);
40 m_sizeCancelIcon
.SetSize(0, 0);
43 CFilterEdit::~CFilterEdit()
45 if (m_hIconCancelNormal
)
46 DestroyIcon(m_hIconCancelNormal
);
47 if (m_hIconCancelPressed
)
48 DestroyIcon(m_hIconCancelPressed
);
50 DestroyIcon(m_hIconInfo
);
52 DeleteObject(m_brBack
);
55 BEGIN_MESSAGE_MAP(CFilterEdit
, CEdit
)
57 ON_MESSAGE(WM_SETFONT
, OnSetFont
)
65 ON_CONTROL_REFLECT_EX(EN_CHANGE
, &CFilterEdit::OnEnChange
)
66 ON_WM_CTLCOLOR_REFLECT()
68 ON_CONTROL_REFLECT(EN_KILLFOCUS
, &CFilterEdit::OnEnKillfocus
)
69 ON_CONTROL_REFLECT(EN_SETFOCUS
, &CFilterEdit::OnEnSetfocus
)
70 ON_MESSAGE(WM_PASTE
, &CFilterEdit::OnPaste
)
75 // CFilterEdit message handlers
77 void CFilterEdit::PreSubclassWindow( )
79 // We must have a multi line edit
80 // to be able to set the edit rect
81 ASSERT( GetStyle() & ES_MULTILINE
);
86 BOOL
CFilterEdit::PreTranslateMessage( MSG
* pMsg
)
88 return CEdit::PreTranslateMessage(pMsg
);
91 BOOL
CFilterEdit::SetCancelBitmaps(UINT uCancelNormal
, UINT uCancelPressed
, BOOL bShowAlways
)
93 m_bShowCancelButtonAlways
= bShowAlways
;
95 if (m_hIconCancelNormal
)
96 DestroyIcon(m_hIconCancelNormal
);
97 if (m_hIconCancelPressed
)
98 DestroyIcon(m_hIconCancelPressed
);
100 m_hIconCancelNormal
= (HICON
)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(uCancelNormal
), IMAGE_ICON
, 0, 0, LR_DEFAULTCOLOR
);
101 m_hIconCancelPressed
= (HICON
)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(uCancelPressed
), IMAGE_ICON
, 0, 0, LR_DEFAULTCOLOR
);
103 if ((m_hIconCancelNormal
== 0) || (m_hIconCancelPressed
== 0))
106 m_sizeCancelIcon
= GetIconSize(m_hIconCancelNormal
);
112 BOOL
CFilterEdit::SetInfoIcon(UINT uInfo
)
115 DestroyIcon(m_hIconInfo
);
117 m_hIconInfo
= (HICON
)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(uInfo
), IMAGE_ICON
, 0, 0, LR_DEFAULTCOLOR
);
119 if (m_hIconInfo
== 0)
122 m_sizeInfoIcon
= GetIconSize(m_hIconInfo
);
128 BOOL
CFilterEdit::SetCueBanner(LPCWSTR lpcwText
)
132 size_t len
= _tcslen(lpcwText
);
133 m_pCueBanner
.reset(new TCHAR
[len
+ 1]);
134 _tcscpy_s(m_pCueBanner
.get(), len
+ 1, lpcwText
);
135 InvalidateRect(NULL
, TRUE
);
141 void CFilterEdit::ResizeWindow()
143 if (!::IsWindow(m_hWnd
))
149 editrc
.left
= rc
.left
+ 4;
150 editrc
.top
= rc
.top
+ 1;
151 editrc
.right
= rc
.right
- 4;
152 editrc
.bottom
= rc
.bottom
- 4;
154 m_rcEditArea
.left
= editrc
.left
+ m_sizeInfoIcon
.cx
;
155 m_rcEditArea
.right
= editrc
.right
- m_sizeCancelIcon
.cx
- 5;
156 m_rcEditArea
.top
= editrc
.top
;
157 m_rcEditArea
.bottom
= editrc
.bottom
;
159 m_rcButtonArea
.left
= m_rcEditArea
.right
+ 5;
160 m_rcButtonArea
.right
= rc
.right
;
161 m_rcButtonArea
.top
= (((rc
.bottom
)-m_sizeCancelIcon
.cy
)/2);
162 m_rcButtonArea
.bottom
= m_rcButtonArea
.top
+ m_sizeCancelIcon
.cy
;
164 m_rcInfoArea
.left
= 0;
165 m_rcInfoArea
.right
= m_rcEditArea
.left
;
166 m_rcInfoArea
.top
= (((rc
.bottom
)-m_sizeInfoIcon
.cy
)/2);
167 m_rcInfoArea
.bottom
= m_rcInfoArea
.top
+ m_sizeInfoIcon
.cy
;
169 SetRect(&m_rcEditArea
);
172 void CFilterEdit::SetButtonClickedMessageId(UINT iButtonClickedMessageId
, UINT iCancelClickedMessageId
)
174 m_iButtonClickedMessageId
= iButtonClickedMessageId
;
175 m_iCancelClickedMessageId
= iCancelClickedMessageId
;
178 CSize
CFilterEdit::GetIconSize(HICON hIcon
)
182 if (GetIconInfo(hIcon
, &iconinfo
))
185 if (GetObject(iconinfo
.hbmColor
, sizeof(BITMAP
), &bmp
))
187 size
.cx
= bmp
.bmWidth
;
188 size
.cy
= bmp
.bmHeight
;
194 BOOL
CFilterEdit::OnEraseBkgnd(CDC
* pDC
)
198 pDC
->FillSolidRect(&rc
, m_backColor
);
200 if (GetWindowTextLength() || m_bShowCancelButtonAlways
)
204 DrawIconEx(pDC
->GetSafeHdc(), m_rcButtonArea
.left
, m_rcButtonArea
.top
, m_hIconCancelNormal
,
205 m_sizeCancelIcon
.cx
, m_sizeCancelIcon
.cy
, 0, NULL
, DI_NORMAL
);
209 DrawIconEx(pDC
->GetSafeHdc(), m_rcButtonArea
.left
, m_rcButtonArea
.top
, m_hIconCancelPressed
,
210 m_sizeCancelIcon
.cx
, m_sizeCancelIcon
.cy
, 0, NULL
, DI_NORMAL
);
215 DrawIconEx(pDC
->GetSafeHdc(), m_rcInfoArea
.left
, m_rcInfoArea
.top
, m_hIconInfo
,
216 m_sizeInfoIcon
.cx
, m_sizeInfoIcon
.cy
, 0, NULL
, DI_NORMAL
);
222 void CFilterEdit::OnLButtonUp(UINT nFlags
, CPoint point
)
225 InvalidateRect(NULL
);
226 if (m_rcButtonArea
.PtInRect(point
))
228 SetWindowText(_T(""));
229 CWnd
*pOwner
= GetOwner();
232 pOwner
->SendMessage(m_iCancelClickedMessageId
, (WPARAM
)GetSafeHwnd(), 0);
236 if (m_rcInfoArea
.PtInRect(point
))
238 CWnd
*pOwner
= GetOwner();
241 RECT rc
= m_rcInfoArea
;
243 pOwner
->SendMessage(m_iButtonClickedMessageId
, (WPARAM
)GetSafeHwnd(), (LPARAM
)(LPRECT
)&rc
);
247 CEdit::OnLButtonUp(nFlags
, point
);
250 void CFilterEdit::OnLButtonDown(UINT nFlags
, CPoint point
)
252 m_bPressed
= m_rcButtonArea
.PtInRect(point
);
253 //InvalidateRect(NULL);
254 CEdit::OnLButtonDown(nFlags
, point
);
257 int CFilterEdit::OnCreate(LPCREATESTRUCT lpCreateStruct
)
259 if (CEdit::OnCreate(lpCreateStruct
) == -1)
267 LRESULT
CFilterEdit::OnSetFont( WPARAM wParam
, LPARAM lParam
)
269 DefWindowProc( WM_SETFONT
, wParam
, lParam
);
276 void CFilterEdit::OnSize( UINT nType
, int cx
, int cy
)
278 CEdit::OnSize( nType
, cx
, cy
);
282 BOOL
CFilterEdit::OnSetCursor(CWnd
* pWnd
, UINT nHitTest
, UINT message
)
285 GetCursorPos(&pntCursor
);
286 ScreenToClient(&pntCursor
);
287 // if the cursor is not in the edit area, show the normal arrow cursor
288 if (!m_rcEditArea
.PtInRect(pntCursor
))
290 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW
));
294 return CEdit::OnSetCursor(pWnd
, nHitTest
, message
);
297 BOOL
CFilterEdit::OnEnChange()
299 // check whether the entered text is valid
301 InvalidateRect(NULL
);
305 HBRUSH
CFilterEdit::CtlColor(CDC
* pDC
, UINT
/*nCtlColor*/)
307 if (m_backColor
!= GetSysColor(COLOR_WINDOW
))
309 pDC
->SetBkColor(m_backColor
);
315 void CFilterEdit::Validate()
319 int len
= GetWindowTextLength();
320 TCHAR
* pBuf
= new TCHAR
[len
+1];
321 GetWindowText(pBuf
, len
+1);
322 m_backColor
= GetSysColor(COLOR_WINDOW
);
323 if (!m_pValidator
->Validate(pBuf
))
325 // Use a background color slightly shifted to red.
326 // We do this by increasing red component and decreasing green and blue.
327 const int SHIFT_PRECENTAGE
= 10;
328 int r
= GetRValue(m_backColor
);
329 int g
= GetGValue(m_backColor
);
330 int b
= GetBValue(m_backColor
);
332 r
= min(r
* (100 + SHIFT_PRECENTAGE
) / 100, 255);
333 // Ensure that there is at least some redness.
334 r
= max(r
, 255 * SHIFT_PRECENTAGE
/ 100);
335 g
= g
* (100 - SHIFT_PRECENTAGE
) / 100;
336 b
= b
* (100 - SHIFT_PRECENTAGE
) / 100;
337 m_backColor
= RGB(r
, g
, b
);
339 DeleteObject(m_brBack
);
340 m_brBack
= CreateSolidBrush(m_backColor
);
346 void CFilterEdit::OnPaint()
348 LRESULT defres
= Default();
353 // the Default() call did not process the WM_PAINT message!
354 // Validate the update region ourselves to avoid
355 // an endless loop repainting
357 GetUpdateRect(&rc
, FALSE
);
358 if (!rc
.IsRectEmpty())
365 void CFilterEdit::DrawDimText()
367 if (m_pCueBanner
.get() == NULL
)
369 if (GetWindowTextLength())
371 if (m_pCueBanner
.get()[0] == 0)
373 if (GetFocus() == this)
376 CClientDC
dcDraw(this);
378 int iState
= dcDraw
.SaveDC();
380 GetClientRect(&rRect
);
381 rRect
.OffsetRect(1, 1);
383 dcDraw
.SelectObject((*GetFont()));
384 dcDraw
.SetTextColor(GetSysColor(COLOR_GRAYTEXT
));
385 dcDraw
.SetBkColor(GetSysColor(COLOR_WINDOW
));
386 dcDraw
.DrawText(m_pCueBanner
.get(), (int)_tcslen(m_pCueBanner
.get()), &rRect
, DT_CENTER
| DT_VCENTER
);
387 dcDraw
.RestoreDC(iState
);
391 void CFilterEdit::OnEnKillfocus()
393 InvalidateRect(NULL
);
396 void CFilterEdit::OnEnSetfocus()
398 InvalidateRect(NULL
);
401 LRESULT
CFilterEdit::OnPaste(WPARAM
, LPARAM
)
405 HANDLE hData
= GetClipboardData (CF_TEXT
);
406 CString
toInsert((const char*)GlobalLock(hData
));
410 // elimate control chars, especially newlines
411 toInsert
.Replace(_T('\t'), _T(' '));
413 // only insert first line
414 toInsert
.Replace(_T('\r'), _T('\n'));
416 toInsert
= toInsert
.Tokenize(_T("\n"), pos
);
419 // get the current text
423 // construct the new text
426 text
.Delete(from
, to
- from
);
427 text
.Insert(from
, toInsert
);
428 from
+= toInsert
.GetLength();
430 // update & notify controls
432 SetSel(from
, from
, FALSE
);
435 GetParent()->SendMessage(WM_COMMAND
, MAKEWPARAM(GetDlgCtrlID(), EN_CHANGE
), (LPARAM
)GetSafeHwnd());