OSDN Git Service

Make UI look flatter (3)
[winmerge-jp/winmerge-jp.git] / Src / EditorFilepathBar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //    WinMerge:  an interactive diff/merge utility
3 //    Copyright (C) 1997-2000  Thingamahoochie Software
4 //    Author: Dean Grimm
5 //
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.
10 //
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.
15 //
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.
19 //
20 /////////////////////////////////////////////////////////////////////////////
21 /** 
22  * @file  EditorFilepathBar.cpp
23  *
24  * @brief Implementation file for CEditorFilepathBar class
25  */
26
27 #include "stdafx.h"
28 #include "EditorFilepathBar.h"
29
30 #ifdef _DEBUG
31 #define new DEBUG_NEW
32 #endif
33
34
35 BEGIN_MESSAGE_MAP(CEditorFilePathBar, CDialogBar)
36         ON_NOTIFY_EX (TTN_NEEDTEXT, 0, OnToolTipNotify)
37         ON_CONTROL_RANGE (EN_SETFOCUS, IDC_STATIC_TITLE_PANE0, IDC_STATIC_TITLE_PANE2, OnSetFocusEdit)
38 END_MESSAGE_MAP()
39
40
41 /**
42  * @brief Constructor.
43  */
44 CEditorFilePathBar::CEditorFilePathBar()
45 : m_nPanes(2)
46 {
47 }
48
49 /**
50  * @brief Destructor.
51  */
52 CEditorFilePathBar::~CEditorFilePathBar()
53 {
54 }
55
56 /**
57  * @brief Create the window.
58  * This function subclasses the edit controls.
59  * @param [in] pParentWnd Parent window for edit controls.
60  * @return TRUE if succeeded, FALSE otherwise.
61  */
62 BOOL CEditorFilePathBar::Create(CWnd* pParentWnd)
63 {
64         if (! CDialogBar::Create(pParentWnd, CEditorFilePathBar::IDD, 
65                         CBRS_ALIGN_TOP | CBRS_TOOLTIPS | CBRS_FLYBY, CEditorFilePathBar::IDD))
66                 return FALSE;
67
68         NONCLIENTMETRICS ncm = { sizeof NONCLIENTMETRICS };
69         if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof NONCLIENTMETRICS, &ncm, 0))
70                 m_font.CreateFontIndirect(&ncm.lfStatusFont);
71
72         // subclass the two custom edit boxes
73         for (int pane = 0; pane < static_cast<int>(std::size(m_Edit)); pane++)
74         {
75                 m_Edit[pane].SubClassEdit(IDC_STATIC_TITLE_PANE0 + pane, this);
76                 m_Edit[pane].SetFont(&m_font);
77                 m_Edit[pane].SetMargins(4, 4);
78         }
79         return TRUE;
80 };
81
82 CSize CEditorFilePathBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
83 {
84         TEXTMETRIC tm;
85         CClientDC dc(this);
86         CFont *pOldFont = dc.SelectObject(&m_font);
87         dc.GetTextMetrics(&tm);
88         dc.SelectObject(pOldFont);
89         return CSize(SHRT_MAX, tm.tmHeight + 6);
90 }
91
92 /** 
93  * @brief Resize both controls to an equal size.
94  */
95 void CEditorFilePathBar::Resize()
96 {
97         if (m_hWnd == nullptr)
98                 return;
99
100         WINDOWPLACEMENT infoBar;
101         GetWindowPlacement(&infoBar);
102
103         int widths[3];
104         for (int pane = 0; pane < m_nPanes; pane++)
105                 widths[pane] = (infoBar.rcNormalPosition.right / m_nPanes);
106         Resize(widths);
107 }
108
109 /** 
110  * @brief Set widths.
111  * This function resizes both controls to given size. The width is usually
112  * same as the splitter view width.
113  * @param [in] leftWidth Left-side control width.
114  * @param [in] rightWidth Right-side control width.
115  */
116 void CEditorFilePathBar::Resize(int widths[])
117 {
118         if (m_hWnd == nullptr)
119                 return;
120
121         // resize left filename
122         CRect rc;
123         int x = 0;
124         GetClientRect(&rc);
125         for (int pane = 0; pane < m_nPanes; pane++)
126         {
127                 CRect rcOld;
128                 m_Edit[pane].GetClientRect(&rcOld);
129                 rc.left = x;
130                 rc.right = x + widths[pane] + (pane == 0 ? 5 : 7);
131                 x = rc.right;
132                 if (rcOld.Width() != rc.Width())
133                 {
134                         m_Edit[pane].MoveWindow(&rc);
135                         m_Edit[pane].RefreshDisplayText();
136                 }
137         }
138 }
139
140 /**
141  * @brief Called when tooltip is about to be shown.
142  * In this function we set the tooltip text shown.
143  */
144 BOOL CEditorFilePathBar::OnToolTipNotify(UINT id, NMHDR * pTTTStruct, LRESULT * pResult)
145 {
146         if (m_hWnd == nullptr)
147                 return FALSE;
148
149         TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;
150         if (pTTT->uFlags & TTF_IDISHWND)
151         {
152                 // idFrom is actually the HWND of the CEdit 
153                 int nID = ::GetDlgCtrlID((HWND)pTTTStruct->idFrom);
154                 if(nID == IDC_STATIC_TITLE_PANE0 || nID == IDC_STATIC_TITLE_PANE1 || nID == IDC_STATIC_TITLE_PANE2)
155                 {
156                         // compute max width : 97% of application width or 80% or full screen width
157                         CRect rect;
158                         GetWindowRect(rect);
159                         int maxWidth = (int)(rect.Width() * .97);
160                         CRect rectScreen; 
161                         SystemParametersInfo(SPI_GETWORKAREA, 0, rectScreen, 0);
162                         if (rectScreen.Width() * .8 > maxWidth)
163                                 maxWidth = (int)(rectScreen.Width() * .8);
164
165                         // use the tooltip font
166                         HANDLE hFont = (HANDLE) ::SendMessage(pTTTStruct->hwndFrom, WM_GETFONT, 0, 0);
167                         CClientDC tempDC(this);
168                         HANDLE hOldFont = ::SelectObject(tempDC.GetSafeHdc(),hFont);
169
170                         // fill in the returned structure
171                         CFilepathEdit * pItem = static_cast<CFilepathEdit*>(GetDlgItem(nID));
172                         pTTT->lpszText = const_cast<TCHAR *>(pItem->GetUpdatedTipText(&tempDC, maxWidth).c_str());
173
174                         // set old font back
175                         if (hOldFont != nullptr)
176                                 ::SelectObject(tempDC.GetSafeHdc(),hOldFont);
177
178                         // we must set TTM_SETMAXTIPWIDTH to use \n in tooltips
179                         // just to do the first time, but how to access the tooltip during init ?
180                         ::SendMessage(pTTTStruct->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 5000);
181
182                         return TRUE;
183                 }
184         }
185         return FALSE;
186 }
187
188 void CEditorFilePathBar::OnSetFocusEdit(UINT id)
189 {
190         if (m_callbackfunc)
191                 m_callbackfunc(id - IDC_STATIC_TITLE_PANE0);
192 }
193
194 /** 
195  * @brief Get the path for one side
196  *
197  * @param [in] pane Index (0-based) of pane to update.
198  */
199 String CEditorFilePathBar::GetText(int pane) const
200 {
201         ASSERT (pane >= 0 && pane < static_cast<int>(std::size(m_Edit)));
202
203         // Check for `nullptr` since window may be closing..
204         if (m_hWnd == nullptr)
205                 return _T("");
206
207         CString str;
208         m_Edit[pane].GetWindowText(str);
209         return String(str);
210 }
211
212 /** 
213  * @brief Set the path for one side
214  *
215  * @param [in] pane Index (0-based) of pane to update.
216  * @param [in] lpszString New text for pane.
217  */
218 void CEditorFilePathBar::SetText(int pane, const String& sString)
219 {
220         ASSERT (pane >= 0 && pane < static_cast<int>(std::size(m_Edit)));
221
222         // Check for `nullptr` since window may be closing..
223         if (m_hWnd == nullptr)
224                 return;
225
226         m_Edit[pane].SetOriginalText(sString);
227 }
228
229 /** 
230  * @brief Set the active status for one status (change the appearance)
231  *
232  * @param [in] pane Index (0-based) of pane to update.
233  * @param [in] bActive If `true` activates pane, `false` deactivates.
234  */
235 void CEditorFilePathBar::SetActive(int pane, bool bActive)
236 {
237         ASSERT (pane >= 0 && pane < static_cast<int>(std::size(m_Edit)));
238
239         // Check for `nullptr` since window may be closing..
240         if (m_hWnd == nullptr)
241                 return;
242
243         m_Edit[pane].SetActive(bActive);
244 }