1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (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
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /////////////////////////////////////////////////////////////////////////////
22 * @file FilePathEdit.cpp
24 * @brief Implementation of the CFilepathEdit class.
28 #include "FilepathEdit.h"
31 #include "ClipBoard.h"
39 static int FormatFilePathForDisplayWidth(CDC * pDC, int maxWidth, String & sFilepath);
41 BEGIN_MESSAGE_MAP(CFilepathEdit, CEdit)
43 ON_WM_CTLCOLOR_REFLECT()
45 ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
50 * @brief Format the path for display in header control.
52 * Formats path so it fits to given length, tries to end lines after
55 * @param [in] pDC Pointer to draw context.
56 * @param [in] maxWidth Maximum width of the string in the GUI.
57 * @param [in,out] sFilepath:
58 * - in: string to format
59 * - out: formatted string
60 * @return Number of lines path is splitted to.
62 static int FormatFilePathForDisplayWidth(CDC * pDC, int maxWidth, String & sFilepath)
71 // find the next truncation point
73 size_t iEndMax = sFilepath.length() - iBegin + 1;
76 size_t iEnd = (iEndMin + iEndMax) / 2;
79 line = sFilepath.substr(iBegin, iEnd);
80 int width = (pDC->GetTextExtent(line.c_str())).cx;
86 ASSERT(iEndMax == iEndMin+1);
88 // here iEndMin is the last character displayed in maxWidth
90 // exit the loop if we can display the remaining characters with no truncation
91 if (iBegin + iEndMin == sFilepath.length())
94 // truncate the text to the previous "\" if possible
95 line = sFilepath.substr(iBegin, iEndMin);
96 size_t lastSlash = line.rfind('\\');
97 if (lastSlash != String::npos)
98 iEndMin = lastSlash + 1;
100 sFilepath.insert(iBegin + iEndMin, _T("\n"));
101 iBegin += iEndMin + 2;
109 * @brief Constructor.
110 * Set text color to black and background white by default.
112 CFilepathEdit::CFilepathEdit()
113 : m_crBackGnd(RGB(255, 255, 255))
114 , m_crText(RGB(0,0,0))
120 * @brief Subclass the control.
121 * @param [in] nID ID of the control to subclass.
122 * @param [in] pParent Parent control of the control to subclass.
123 * @return `true` if succeeded, `false` otherwise.
125 bool CFilepathEdit::SubClassEdit(UINT nID, CWnd* pParent)
128 return SubclassDlgItem(nID, pParent);
132 * @brief Set the text to show in the control.
133 * This function sets the text (original text) to show in the control.
134 * The control may modify the text for displaying in the GUI.
136 void CFilepathEdit::SetOriginalText(const String& sString)
138 if (m_sOriginalText.compare(sString) == 0)
141 m_sOriginalText = sString;
143 RefreshDisplayText();
147 * @brief Re-format the displayed text and update GUI.
148 * This method formats the visible text from original text.
150 void CFilepathEdit::RefreshDisplayText()
152 String line = m_sOriginalText;
154 // we want to keep the first and the last path component, and in between,
155 // as much characters as possible from the right
156 // PathCompactPath keeps, in between, as much characters as possible from the left
157 // so we reverse everything between the first and the last component before calling PathCompactPath
158 size_t iBeginLast = line.rfind('\\');
159 size_t iEndIntro = line.find('\\');
160 if (iBeginLast != String::npos && iEndIntro != iBeginLast)
162 String textToReverse = line.substr(iEndIntro + 1, iBeginLast -
164 std::reverse(textToReverse.begin(), textToReverse.end());
165 line = line.substr(0, iEndIntro + 1) + textToReverse + line.substr(iBeginLast);
168 // get a device context object
170 // and use the correct font
171 CFont *pFontOld = lDC.SelectObject(GetFont());
176 // take GetBuffer (lenght +3) to count for ellipsis
177 std::vector<TCHAR> tmp(line.length() + 4);
178 std::copy(line.begin(), line.end(), tmp.begin());
179 PathCompactPath(lDC.GetSafeHdc(), &tmp[0], rect.Width());
183 lDC.SelectObject(pFontOld);
185 // we reverse back everything between the first and the last component
186 // it works OK as "..." reversed = "..." again
187 iBeginLast = line.rfind('\\');
188 iEndIntro = line.find('\\');
189 if (iBeginLast != String::npos && iEndIntro != iBeginLast)
191 String textToReverse = line.substr(iEndIntro + 1, iBeginLast -
193 std::reverse(textToReverse.begin(), textToReverse.end());
194 line = line.substr(0, iEndIntro + 1) + textToReverse + line.substr(iBeginLast);
197 SetWindowText(line.c_str());
201 * @brief Updates and returns the tooltip for this edit box
203 const String& CFilepathEdit::GetUpdatedTipText(CDC * pDC, int maxWidth)
205 GetOriginalText(m_sToolTipString);
206 FormatFilePathForDisplayWidth(pDC, maxWidth, m_sToolTipString);
207 return m_sToolTipString;
211 * @brief retrieve text from the OriginalText
213 * @note The standard Copy function works with the (compacted) windowText
215 void CFilepathEdit::CustomCopy(size_t iBegin, size_t iEnd /*=-1*/)
217 if (iEnd == String::npos)
218 iEnd = m_sOriginalText.length();
220 PutToClipboard(m_sOriginalText.substr(iBegin, iEnd - iBegin), m_hWnd);
224 * @brief Format the context menu.
226 void CFilepathEdit::OnContextMenu(CWnd*, CPoint point)
229 if (point.x == -1 && point.y == -1){
230 //keystroke invocation
233 ClientToScreen(rect);
235 point = rect.TopLeft();
240 VERIFY(menu.LoadMenu(IDR_POPUP_EDITOR_HEADERBAR));
241 theApp.TranslateMenu(menu.m_hMenu);
243 BCMenu* pPopup = static_cast<BCMenu *>(menu.GetSubMenu(0));
244 ASSERT(pPopup != nullptr);
246 DWORD sel = GetSel();
247 if (HIWORD(sel) == LOWORD(sel))
248 pPopup->EnableMenuItem(ID_EDITOR_COPY, MF_GRAYED);
249 if (paths::EndsWithSlash(m_sOriginalText))
250 // no filename, we have to disable the unwanted menu entry
251 pPopup->EnableMenuItem(ID_EDITOR_COPY_FILENAME, MF_GRAYED);
253 // invoke context menu
254 // we don't want to use the main application handlers, so we
255 // use flags TPM_NONOTIFY | TPM_RETURNCMD
256 // and handle the command after TrackPopupMenu
257 int command = pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON |
258 TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, AfxGetMainWnd());
260 // compute the beginning of the text to copy (in OriginalText)
267 case ID_EDITOR_COPY_FILENAME:
269 size_t lastSlash = m_sOriginalText.rfind('\\');
270 if (lastSlash == String::npos)
271 lastSlash = m_sOriginalText.rfind('/');
272 if (lastSlash != String::npos)
273 iBegin = lastSlash+1;
278 case ID_EDITOR_COPY_PATH:
279 // pass the heading "*" for modified files
280 if (m_sOriginalText.at(0) == '*')
293 static COLORREF GetDarkenColor(COLORREF a, double r)
295 const int R = static_cast<int>(GetRValue(a) * r);
296 const int G = static_cast<int>(GetGValue(a) * r);
297 const int B = static_cast<int>(GetBValue(a) * r);
301 void CFilepathEdit::OnNcPaint()
305 const int margin = 4;
307 rect.OffsetRect(-rect.TopLeft());
308 dc.FillSolidRect(CRect(rect.left, rect.top, rect.left + margin, rect.bottom), GetDarkenColor(m_crBackGnd, 0.98));
309 dc.FillSolidRect(CRect(rect.left, rect.top, rect.left + 1, rect.bottom), GetDarkenColor(m_crBackGnd, 0.90));
310 dc.FillSolidRect(CRect(rect.right - margin, rect.top, rect.right, rect.bottom), m_crBackGnd);
311 dc.FillSolidRect(CRect(rect.left + 1, rect.top, rect.right, rect.top + margin), GetDarkenColor(m_crBackGnd, 0.98));
312 dc.FillSolidRect(CRect(rect.left, rect.top, rect.right, rect.top + 1), GetDarkenColor(m_crBackGnd, 0.90));
313 dc.FillSolidRect(CRect(rect.left + margin, rect.bottom - margin, rect.right, rect.bottom), m_crBackGnd);
316 void CFilepathEdit::OnEditCopy()
318 int nStartChar, nEndChar;
319 GetSel(nStartChar, nEndChar);
320 if (nStartChar == nEndChar)
323 if (nStartChar == nEndChar)
324 SetSel(nStartChar, nEndChar);
327 BOOL CFilepathEdit::PreTranslateMessage(MSG *pMsg)
329 if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST)
331 if (::TranslateAccelerator (m_hWnd, static_cast<CFrameWnd *>(AfxGetMainWnd())->GetDefaultAccelerator(), pMsg))
334 return CEdit::PreTranslateMessage(pMsg);
338 * @brief Set the control to look active/inactive.
339 * This function sets control to look like an active control. We don't
340 * have real focus on this control, but editor pane below it. However
341 * for user this active look informs which editor pane is active.
342 * @param [in] bActive If `true` set control look like active control.
344 void CFilepathEdit::SetActive(bool bActive)
348 if (m_hWnd == nullptr)
352 GetWindowRect(&rcWnd);
356 SetTextColor(::GetSysColor(COLOR_CAPTIONTEXT));
357 SetBackColor(::GetSysColor(COLOR_ACTIVECAPTION));
361 SetTextColor(::GetSysColor(COLOR_INACTIVECAPTIONTEXT));
362 SetBackColor(::GetSysColor(COLOR_INACTIVECAPTION));
364 RedrawWindow(nullptr, nullptr, RDW_FRAME | RDW_INVALIDATE);
368 * @brief Set control's colors.
369 * @param [in] pDC pointer to device context.
370 * @param [in] nCtlColor Control color to set.
371 * @note Parameter @p nCtlColor is not used but must be present as this method
372 * is called by framework.
373 * @return Brush for background.
375 HBRUSH CFilepathEdit::CtlColor(CDC* pDC, UINT nCtlColor)
377 UNUSED_ALWAYS(nCtlColor);
378 // Return a non-`nullptr` brush if the parent's
379 //handler should not be called
382 pDC->SetTextColor(m_crText);
384 //set the text's background color
385 pDC->SetBkColor(m_crBackGnd);
387 //return the brush used for background this sets control background
392 * @brief Set control's bacground color.
393 * @param [in] rgb Color to set as background color.
395 void CFilepathEdit::SetBackColor(COLORREF rgb)
397 //set background color ref (used for text's background)
401 if (m_brBackGnd.GetSafeHandle())
402 m_brBackGnd.DeleteObject();
403 //set brush to new color
404 m_brBackGnd.CreateSolidBrush(rgb);
411 * @brief Set control's text color.
412 * @param [in] Color to set as text color.
414 void CFilepathEdit::SetTextColor(COLORREF rgb)