OSDN Git Service

Use __super
[winmerge-jp/winmerge-jp.git] / Src / EditorFilepathBar.cpp
index ddc3f43..7700e63 100644 (file)
@@ -2,24 +2,10 @@
 //    WinMerge:  an interactive diff/merge utility
 //    Copyright (C) 1997-2000  Thingamahoochie Software
 //    Author: Dean Grimm
-//
-//    This program is free software; you can redistribute it and/or modify
-//    it under the terms of the GNU General Public License as published by
-//    the Free Software Foundation; either version 2 of the License, or
-//    (at your option) any later version.
-//
-//    This program is distributed in the hope that it will be useful,
-//    but WITHOUT ANY WARRANTY; without even the implied warranty of
-//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//    GNU General Public License for more details.
-//
-//    You should have received a copy of the GNU General Public License
-//    along with this program; if not, write to the Free Software
-//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-//
+//    SPDX-License-Identifier: GPL-2.0-or-later
 /////////////////////////////////////////////////////////////////////////////
 /** 
- * @file  DiffList.cpp
+ * @file  EditorFilepathBar.cpp
  *
  * @brief Implementation file for CEditorFilepathBar class
  */
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
-#undef THIS_FILE
-static char THIS_FILE[] = __FILE__;
 #endif
 
 
 BEGIN_MESSAGE_MAP(CEditorFilePathBar, CDialogBar)
        ON_NOTIFY_EX (TTN_NEEDTEXT, 0, OnToolTipNotify)
+       ON_CONTROL_RANGE (EN_SETFOCUS, IDC_STATIC_TITLE_PANE0, IDC_STATIC_TITLE_PANE2, OnSetFocusEdit)
+       ON_CONTROL_RANGE (EN_USER_CAPTION_CHANGED, IDC_STATIC_TITLE_PANE0, IDC_STATIC_TITLE_PANE2, OnChangeEdit)
+       ON_CONTROL_RANGE (EN_USER_FILE_SELECTED, IDC_STATIC_TITLE_PANE0, IDC_STATIC_TITLE_PANE2, OnSelectEdit)
 END_MESSAGE_MAP()
 
 
@@ -43,7 +30,7 @@ END_MESSAGE_MAP()
  * @brief Constructor.
  */
 CEditorFilePathBar::CEditorFilePathBar()
-: m_pFont(nullptr), m_nPanes(2)
+: m_nPanes(2)
 {
 }
 
@@ -62,63 +49,34 @@ CEditorFilePathBar::~CEditorFilePathBar()
  */
 BOOL CEditorFilePathBar::Create(CWnd* pParentWnd)
 {
-       if (! CDialogBar::Create(pParentWnd, CEditorFilePathBar::IDD, 
-                       CBRS_TOP | CBRS_TOOLTIPS | CBRS_FLYBY, CEditorFilePathBar::IDD))
+       if (! __super::Create(pParentWnd, CEditorFilePathBar::IDD, 
+                       CBRS_ALIGN_TOP | CBRS_TOOLTIPS | CBRS_FLYBY, CEditorFilePathBar::IDD))
                return FALSE;
 
+       NONCLIENTMETRICS ncm = { sizeof NONCLIENTMETRICS };
+       if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof NONCLIENTMETRICS, &ncm, 0))
+               m_font.CreateFontIndirect(&ncm.lfStatusFont);
+
        // subclass the two custom edit boxes
-       for (int pane = 0; pane < countof(m_Edit); pane++)
+       const int nLogPixelsY = CClientDC(this).GetDeviceCaps(LOGPIXELSY);
+       int cx = -MulDiv(ncm.lfStatusFont.lfHeight, nLogPixelsY, 72);
+       for (int pane = 0; pane < static_cast<int>(std::size(m_Edit)); pane++)
+       {
                m_Edit[pane].SubClassEdit(IDC_STATIC_TITLE_PANE0 + pane, this);
-
+               m_Edit[pane].SetFont(&m_font);
+               m_Edit[pane].SetMargins(4, 4 + cx);
+       }
        return TRUE;
 };
 
-void CEditorFilePathBar::SetPaneCount(int nPanes)
+CSize CEditorFilePathBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
 {
-       m_nPanes = nPanes;
-}
-
-/**
- * @brief Set look of headerbars similar to other window.
- *
- * @param [in] pWnd Pointer to window we want to imitate
- * @return TRUE if parent must recompute layout
- */
-BOOL CEditorFilePathBar::LookLikeThisWnd(const CWnd * pWnd)
-{
-       // Update font. Note that we must delete previous font
-       // before creating a new one.
-       CFont * pFont = pWnd->GetFont();
-       if (pFont)
-       {
-               m_pFont.reset(new CFont);
-               if (m_pFont != NULL)
-               {
-                       LOGFONT lfFont = {0};
-                       if (pFont->GetLogFont(&lfFont))
-                       {
-                               m_pFont->CreateFontIndirect(&lfFont);
-                               for (int pane = 0; pane < m_nPanes; pane++)
-                               {
-                                       m_Edit[pane].SetFont(m_pFont.get());
-                                       m_Edit[pane].SetMargins(4, 4);
-                               }
-                       }
-               }
-       }
-
-       // Set same dimensions (than window we imitate)
-       CRect rectNew;
-       pWnd->GetWindowRect(rectNew);
-       CRect rectCurrent;
-       GetWindowRect(rectCurrent);
-       if (rectNew != rectCurrent)
-       {
-               SetWindowPos(NULL,0,0,rectNew.Width(), rectNew.Height(),
-                       SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE);     
-               return TRUE;
-       }
-       return FALSE;
+       TEXTMETRIC tm;
+       CClientDC dc(this);
+       CFont *pOldFont = dc.SelectObject(&m_font);
+       dc.GetTextMetrics(&tm);
+       dc.SelectObject(pOldFont);
+       return CSize(SHRT_MAX, tm.tmHeight + 6);
 }
 
 /** 
@@ -126,17 +84,18 @@ BOOL CEditorFilePathBar::LookLikeThisWnd(const CWnd * pWnd)
  */
 void CEditorFilePathBar::Resize()
 {
-       if (m_hWnd == NULL)
+       if (m_hWnd == nullptr)
                return;
 
-       WINDOWPLACEMENT infoBar;
+       WINDOWPLACEMENT infoBar = {};
        GetWindowPlacement(&infoBar);
 
-       int widths[3];
+       int widths[3] = {};
        for (int pane = 0; pane < m_nPanes; pane++)
-               widths[pane] = (infoBar.rcNormalPosition.right / m_nPanes) - 6;
+               widths[pane] = (infoBar.rcNormalPosition.right / m_nPanes) - ((pane == 0) ? 7 : 5);
        Resize(widths);
 }
+
 /** 
  * @brief Set widths.
  * This function resizes both controls to given size. The width is usually
@@ -146,7 +105,7 @@ void CEditorFilePathBar::Resize()
  */
 void CEditorFilePathBar::Resize(int widths[])
 {
-       if (m_hWnd == NULL)
+       if (m_hWnd == nullptr)
                return;
 
        // resize left filename
@@ -158,8 +117,8 @@ void CEditorFilePathBar::Resize(int widths[])
                CRect rcOld;
                m_Edit[pane].GetClientRect(&rcOld);
                rc.left = x;
-               rc.right = x + widths[pane] + 4;
-               x += widths[pane] + 7;
+               rc.right = x + widths[pane] + (pane == 0 ? 5 : 7);
+               x = rc.right;
                if (rcOld.Width() != rc.Width())
                {
                        m_Edit[pane].MoveWindow(&rc);
@@ -174,7 +133,7 @@ void CEditorFilePathBar::Resize(int widths[])
  */
 BOOL CEditorFilePathBar::OnToolTipNotify(UINT id, NMHDR * pTTTStruct, LRESULT * pResult)
 {
-       if (m_hWnd == NULL)
+       if (m_hWnd == nullptr)
                return FALSE;
 
        TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;
@@ -203,17 +162,62 @@ BOOL CEditorFilePathBar::OnToolTipNotify(UINT id, NMHDR * pTTTStruct, LRESULT *
                        pTTT->lpszText = const_cast<TCHAR *>(pItem->GetUpdatedTipText(&tempDC, maxWidth).c_str());
 
                        // set old font back
-                       if (hOldFont)
+                       if (hOldFont != nullptr)
                                ::SelectObject(tempDC.GetSafeHdc(),hOldFont);
 
                        // we must set TTM_SETMAXTIPWIDTH to use \n in tooltips
                        // just to do the first time, but how to access the tooltip during init ?
                        ::SendMessage(pTTTStruct->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 5000);
 
-                       return(TRUE);
+                       return TRUE;
                }
        }
-       return(FALSE);
+       return FALSE;
+}
+
+void CEditorFilePathBar::OnSetFocusEdit(UINT id)
+{
+       if (m_setFocusCallbackfunc)
+               m_setFocusCallbackfunc(id - IDC_STATIC_TITLE_PANE0);
+}
+
+void CEditorFilePathBar::OnChangeEdit(UINT id)
+{
+       const int pane = id - IDC_STATIC_TITLE_PANE0;
+       if (m_captionChangedCallbackfunc)
+       {
+               CString text;
+               m_Edit[pane].GetWindowText(text);
+               m_captionChangedCallbackfunc(pane, (LPCTSTR)text);
+       }
+}
+
+void CEditorFilePathBar::OnSelectEdit(UINT id)
+{
+       const int pane = id - IDC_STATIC_TITLE_PANE0;
+       if (m_fileSelectedCallbackfunc)
+       {
+               String filename = m_Edit[pane].GetSelectedFile();
+               m_fileSelectedCallbackfunc(pane, filename);
+       }
+}
+
+/** 
+ * @brief Get the path for one side
+ *
+ * @param [in] pane Index (0-based) of pane to update.
+ */
+String CEditorFilePathBar::GetText(int pane) const
+{
+       ASSERT (pane >= 0 && pane < static_cast<int>(std::size(m_Edit)));
+
+       // Check for `nullptr` since window may be closing..
+       if (m_hWnd == nullptr)
+               return _T("");
+
+       CString str;
+       m_Edit[pane].GetWindowText(str);
+       return String(str);
 }
 
 /** 
@@ -224,10 +228,10 @@ BOOL CEditorFilePathBar::OnToolTipNotify(UINT id, NMHDR * pTTTStruct, LRESULT *
  */
 void CEditorFilePathBar::SetText(int pane, const String& sString)
 {
-       ASSERT (pane >= 0 && pane < countof(m_Edit));
+       ASSERT (pane >= 0 && pane < static_cast<int>(std::size(m_Edit)));
 
-       // Check for NULL since window may be closing..
-       if (m_hWnd == NULL)
+       // Check for `nullptr` since window may be closing..
+       if (m_hWnd == nullptr)
                return;
 
        m_Edit[pane].SetOriginalText(sString);
@@ -237,14 +241,14 @@ void CEditorFilePathBar::SetText(int pane, const String& sString)
  * @brief Set the active status for one status (change the appearance)
  *
  * @param [in] pane Index (0-based) of pane to update.
- * @param [in] bActive If TRUE activates pane, FALSE deactivates.
+ * @param [in] bActive If `true` activates pane, `false` deactivates.
  */
 void CEditorFilePathBar::SetActive(int pane, bool bActive)
 {
-       ASSERT (pane >= 0 && pane < countof(m_Edit));
+       ASSERT (pane >= 0 && pane < static_cast<int>(std::size(m_Edit)));
 
-       // Check for NULL since window may be closing..
-       if (m_hWnd == NULL)
+       // Check for `nullptr` since window may be closing..
+       if (m_hWnd == nullptr)
                return;
 
        m_Edit[pane].SetActive(bActive);