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, CString & sFilepath);
41 BEGIN_MESSAGE_MAP(CFilepathEdit, CEdit)
43 ON_WM_CTLCOLOR_REFLECT()
49 * @brief Format the path for display in header control.
51 * Formats path so it fits to given length, tries to end lines after
54 * @param [in] pDC Pointer to draw context.
55 * @param [in] maxWidth Maximum width of the string in the GUI.
56 * @param [in,out] sFilepath:
57 * - in: string to format
58 * - out: formatted string
59 * @return Number of lines path is splitted to.
61 int FormatFilePathForDisplayWidth(CDC * pDC, int maxWidth, String & sFilepath)
70 // find the next truncation point
72 size_t iEndMax = sFilepath.length() - iBegin + 1;
75 size_t iEnd = (iEndMin + iEndMax) / 2;
78 line = sFilepath.substr(iBegin, iEnd);
79 int width = (pDC->GetTextExtent(line.c_str())).cx;
85 ASSERT(iEndMax == iEndMin+1);
87 // here iEndMin is the last character displayed in maxWidth
89 // exit the loop if we can display the remaining characters with no truncation
90 if (iBegin + iEndMin == sFilepath.length())
93 // truncate the text to the previous "\" if possible
94 line = sFilepath.substr(iBegin, iEndMin);
95 size_t lastSlash = line.rfind('\\');
96 if (lastSlash != String::npos)
97 iEndMin = lastSlash + 1;
99 sFilepath.insert(iBegin + iEndMin, _T("\n"));
100 iBegin += iEndMin + 2;
108 * @brief Constructor.
109 * Set text color to black and background white by default.
111 CFilepathEdit::CFilepathEdit()
112 : m_crBackGnd(RGB(255, 255, 255))
113 , m_crText(RGB(0,0,0))
119 * @brief Subclass the control.
120 * @param [in] nID ID of the control to subclass.
121 * @param [in] pParent Parent control of the control to subclass.
122 * @return TRUE if succeeded, FALSE otherwise.
124 BOOL CFilepathEdit::SubClassEdit(UINT nID, CWnd* pParent)
127 return SubclassDlgItem(nID, pParent);
131 * @brief Return the control's original text.
132 * @return Control's original text.
134 void CFilepathEdit::GetOriginalText(String& rString) const
136 rString = m_sOriginalText;
140 * @brief Set the text to show in the control.
141 * This function sets the text (original text) to show in the control.
142 * The control may modify the text for displaying in the GUI.
144 void CFilepathEdit::SetOriginalText(const String& sString)
146 if (m_sOriginalText.compare(sString) == 0)
149 m_sOriginalText = sString;
151 RefreshDisplayText();
155 * @brief Re-format the displayed text and update GUI.
156 * This method formats the visible text from original text.
158 void CFilepathEdit::RefreshDisplayText()
160 String line = m_sOriginalText;
162 // we want to keep the first and the last path component, and in between,
163 // as much characters as possible from the right
164 // PathCompactPath keeps, in between, as much characters as possible from the left
165 // so we reverse everything between the first and the last component before calling PathCompactPath
166 size_t iBeginLast = line.rfind('\\');
167 size_t iEndIntro = line.find('\\');
168 if (iBeginLast != String::npos && iEndIntro != iBeginLast)
170 String textToReverse = line.substr(iEndIntro + 1, iBeginLast -
172 std::reverse(textToReverse.begin(), textToReverse.end());
173 line = line.substr(0, iEndIntro + 1) + textToReverse + line.substr(iBeginLast);
176 // get a device context object
178 // and use the correct font
179 CFont *pFontOld = lDC.SelectObject(GetFont());
184 // take GetBuffer (lenght +3) to count for ellipsis
185 std::vector<TCHAR> tmp(line.length() + 4);
186 std::copy(line.begin(), line.end(), tmp.begin());
187 PathCompactPath(lDC.GetSafeHdc(), &tmp[0], rect.Width());
191 lDC.SelectObject(pFontOld);
193 // we reverse back everything between the first and the last component
194 // it works OK as "..." reversed = "..." again
195 iBeginLast = line.rfind('\\');
196 iEndIntro = line.find('\\');
197 if (iBeginLast != String::npos && iEndIntro != iBeginLast)
199 String textToReverse = line.substr(iEndIntro + 1, iBeginLast -
201 std::reverse(textToReverse.begin(), textToReverse.end());
202 line = line.substr(0, iEndIntro + 1) + textToReverse + line.substr(iBeginLast);
205 SetWindowText(line.c_str());
209 * @brief Updates and returns the tooltip for this edit box
211 const String& CFilepathEdit::GetUpdatedTipText(CDC * pDC, int maxWidth)
213 GetOriginalText(m_sToolTipString);
214 FormatFilePathForDisplayWidth(pDC, maxWidth, m_sToolTipString);
215 return m_sToolTipString;
219 * @brief retrieve text from the OriginalText
221 * @note The standard Copy function works with the (compacted) windowText
223 void CFilepathEdit::CustomCopy(size_t iBegin, size_t iEnd /*=-1*/)
225 if (iEnd == String::npos)
226 iEnd = m_sOriginalText.length();
228 PutToClipboard(m_sOriginalText.substr(iBegin, iEnd - iBegin), m_hWnd);
232 * @brief Format the context menu.
234 void CFilepathEdit::OnContextMenu(CWnd*, CPoint point)
237 if (point.x == -1 && point.y == -1){
238 //keystroke invocation
241 ClientToScreen(rect);
243 point = rect.TopLeft();
248 VERIFY(menu.LoadMenu(IDR_POPUP_EDITOR_HEADERBAR));
249 theApp.TranslateMenu(menu.m_hMenu);
251 BCMenu* pPopup = static_cast<BCMenu *>(menu.GetSubMenu(0));
252 ASSERT(pPopup != NULL);
254 DWORD sel = GetSel();
255 if (HIWORD(sel) == LOWORD(sel))
256 pPopup->EnableMenuItem(ID_EDITOR_COPY, MF_GRAYED);
257 if (paths::EndsWithSlash(m_sOriginalText))
258 // no filename, we have to disable the unwanted menu entry
259 pPopup->EnableMenuItem(ID_EDITOR_COPY_FILENAME, MF_GRAYED);
261 // invoke context menu
262 // we don't want to use the main application handlers, so we
263 // use flags TPM_NONOTIFY | TPM_RETURNCMD
264 // and handle the command after TrackPopupMenu
265 int command = pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON |
266 TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, AfxGetMainWnd());
268 // compute the beginning of the text to copy (in OriginalText)
275 case ID_EDITOR_COPY_FILENAME:
277 size_t lastSlash = m_sOriginalText.rfind('\\');
278 if (lastSlash == String::npos)
279 lastSlash = m_sOriginalText.rfind('/');
280 if (lastSlash != String::npos)
281 iBegin = lastSlash+1;
286 case ID_EDITOR_COPY_PATH:
287 // pass the heading "*" for modified files
288 if (m_sOriginalText.at(0) == '*')
301 static COLORREF GetDarkenColor(COLORREF a, double r)
303 const int R = static_cast<int>(GetRValue(a) * r);
304 const int G = static_cast<int>(GetGValue(a) * r);
305 const int B = static_cast<int>(GetBValue(a) * r);
309 void CFilepathEdit::OnNcPaint()
313 const int margin = 4;
315 rect.OffsetRect(-rect.TopLeft());
316 dc.FillSolidRect(CRect(rect.left, rect.top, rect.left + margin, rect.bottom), GetDarkenColor(m_crBackGnd, 0.98));
317 dc.FillSolidRect(CRect(rect.left, rect.top, rect.left + 1, rect.bottom), GetDarkenColor(m_crBackGnd, 0.90));
318 dc.FillSolidRect(CRect(rect.right - margin, rect.top, rect.right, rect.bottom), m_crBackGnd);
319 dc.FillSolidRect(CRect(rect.left + 1, rect.top, rect.right, rect.top + margin), GetDarkenColor(m_crBackGnd, 0.98));
320 dc.FillSolidRect(CRect(rect.left, rect.top, rect.right, rect.top + 1), GetDarkenColor(m_crBackGnd, 0.90));
321 dc.FillSolidRect(CRect(rect.left + margin, rect.bottom - margin, rect.right, rect.bottom), m_crBackGnd);
325 * @brief Set the control to look active/inactive.
326 * This function sets control to look like an active control. We don't
327 * have real focus on this control, but editor pane below it. However
328 * for user this active look informs which editor pane is active.
329 * @param [in] bActive If TRUE set control look like active control.
331 void CFilepathEdit::SetActive(bool bActive)
339 GetWindowRect(&rcWnd);
343 SetTextColor(::GetSysColor(COLOR_CAPTIONTEXT));
344 SetBackColor(::GetSysColor(COLOR_ACTIVECAPTION));
348 SetTextColor(::GetSysColor(COLOR_INACTIVECAPTIONTEXT));
349 SetBackColor(::GetSysColor(COLOR_INACTIVECAPTION));
351 RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
355 * @brief Set control's colors.
356 * @param [in] pDC pointer to device context.
357 * @param [in] nCtlColor Control color to set.
358 * @note Parameter @p nCtlColor is not used but must be present as this method
359 * is called by framework.
360 * @return Brush for background.
362 HBRUSH CFilepathEdit::CtlColor(CDC* pDC, UINT nCtlColor)
364 UNUSED_ALWAYS(nCtlColor);
365 // Return a non-NULL brush if the parent's
366 //handler should not be called
369 pDC->SetTextColor(m_crText);
371 //set the text's background color
372 pDC->SetBkColor(m_crBackGnd);
374 //return the brush used for background this sets control background
379 * @brief Set control's bacground color.
380 * @param [in] rgb Color to set as background color.
382 void CFilepathEdit::SetBackColor(COLORREF rgb)
384 //set background color ref (used for text's background)
388 if (m_brBackGnd.GetSafeHandle())
389 m_brBackGnd.DeleteObject();
390 //set brush to new color
391 m_brBackGnd.CreateSolidBrush(rgb);
398 * @brief Set control's text color.
399 * @param [in] Color to set as text color.
401 void CFilepathEdit::SetTextColor(COLORREF rgb)