OSDN Git Service

Remove THIS_FILE
[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 END_MESSAGE_MAP()
38
39
40 /**
41  * @brief Constructor.
42  */
43 CEditorFilePathBar::CEditorFilePathBar()
44 : m_pFont(nullptr), m_nPanes(2)
45 {
46 }
47
48 /**
49  * @brief Destructor.
50  */
51 CEditorFilePathBar::~CEditorFilePathBar()
52 {
53 }
54
55 /**
56  * @brief Create the window.
57  * This function subclasses the edit controls.
58  * @param [in] pParentWnd Parent window for edit controls.
59  * @return TRUE if succeeded, FALSE otherwise.
60  */
61 BOOL CEditorFilePathBar::Create(CWnd* pParentWnd)
62 {
63         if (! CDialogBar::Create(pParentWnd, CEditorFilePathBar::IDD, 
64                         CBRS_TOP | CBRS_TOOLTIPS | CBRS_FLYBY, CEditorFilePathBar::IDD))
65                 return FALSE;
66
67         // subclass the two custom edit boxes
68         for (int pane = 0; pane < countof(m_Edit); pane++)
69                 m_Edit[pane].SubClassEdit(IDC_STATIC_TITLE_PANE0 + pane, this);
70
71         return TRUE;
72 };
73
74 CSize CEditorFilePathBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
75 {
76         TEXTMETRIC tm;
77         CDC *pdc = GetDC();
78         CFont *pOldFont = pdc->SelectObject(m_pFont.get());
79         pdc->GetTextMetrics(&tm);
80         pdc->SelectObject(pOldFont);
81         ReleaseDC(pdc);
82         return CSize(SHRT_MAX, tm.tmHeight + 6);
83 }
84
85 void CEditorFilePathBar::SetPaneCount(int nPanes)
86 {
87         m_nPanes = nPanes;
88 }
89
90 /**
91  * @brief Set look of headerbars similar to other window.
92  *
93  * @param [in] pWnd Pointer to window we want to imitate
94  * @return TRUE if parent must recompute layout
95  */
96 BOOL CEditorFilePathBar::LookLikeThisWnd(const CWnd * pWnd)
97 {
98         // Update font. Note that we must delete previous font
99         // before creating a new one.
100         CFont * pFont = pWnd->GetFont();
101         if (pFont)
102         {
103                 m_pFont.reset(new CFont);
104                 if (m_pFont != NULL)
105                 {
106                         LOGFONT lfFont = {0};
107                         if (pFont->GetLogFont(&lfFont))
108                         {
109                                 m_pFont->CreateFontIndirect(&lfFont);
110                                 for (int pane = 0; pane < m_nPanes; pane++)
111                                 {
112                                         m_Edit[pane].SetFont(m_pFont.get());
113                                         m_Edit[pane].SetMargins(4, 4);
114                                 }
115                         }
116                 }
117         }
118
119         // Set same dimensions (than window we imitate)
120         CRect rectNew;
121         pWnd->GetWindowRect(rectNew);
122         CRect rectCurrent;
123         GetWindowRect(rectCurrent);
124         if (rectNew != rectCurrent)
125         {
126                 SetWindowPos(NULL,0,0,rectNew.Width(), rectNew.Height(),
127                         SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE);     
128                 return TRUE;
129         }
130         return FALSE;
131 }
132
133 /** 
134  * @brief Resize both controls to an equal size.
135  */
136 void CEditorFilePathBar::Resize()
137 {
138         if (m_hWnd == NULL)
139                 return;
140
141         WINDOWPLACEMENT infoBar;
142         GetWindowPlacement(&infoBar);
143
144         int widths[3];
145         for (int pane = 0; pane < m_nPanes; pane++)
146                 widths[pane] = (infoBar.rcNormalPosition.right / m_nPanes) - 6;
147         Resize(widths);
148 }
149 /** 
150  * @brief Set widths.
151  * This function resizes both controls to given size. The width is usually
152  * same as the splitter view width.
153  * @param [in] leftWidth Left-side control width.
154  * @param [in] rightWidth Right-side control width.
155  */
156 void CEditorFilePathBar::Resize(int widths[])
157 {
158         if (m_hWnd == NULL)
159                 return;
160
161         // resize left filename
162         CRect rc;
163         int x = 0;
164         GetClientRect(&rc);
165         for (int pane = 0; pane < m_nPanes; pane++)
166         {
167                 CRect rcOld;
168                 m_Edit[pane].GetClientRect(&rcOld);
169                 rc.left = x;
170                 rc.right = x + widths[pane] + 4;
171                 x += widths[pane] + 7;
172                 if (rcOld.Width() != rc.Width())
173                 {
174                         m_Edit[pane].MoveWindow(&rc);
175                         m_Edit[pane].RefreshDisplayText();
176                 }
177         }
178 }
179
180 /**
181  * @brief Called when tooltip is about to be shown.
182  * In this function we set the tooltip text shown.
183  */
184 BOOL CEditorFilePathBar::OnToolTipNotify(UINT id, NMHDR * pTTTStruct, LRESULT * pResult)
185 {
186         if (m_hWnd == NULL)
187                 return FALSE;
188
189         TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;
190         if (pTTT->uFlags & TTF_IDISHWND)
191         {
192                 // idFrom is actually the HWND of the CEdit 
193                 int nID = ::GetDlgCtrlID((HWND)pTTTStruct->idFrom);
194                 if(nID == IDC_STATIC_TITLE_PANE0 || nID == IDC_STATIC_TITLE_PANE1 || nID == IDC_STATIC_TITLE_PANE2)
195                 {
196                         // compute max width : 97% of application width or 80% or full screen width
197                         CRect rect;
198                         GetWindowRect(rect);
199                         int maxWidth = (int)(rect.Width() * .97);
200                         CRect rectScreen; 
201                         SystemParametersInfo(SPI_GETWORKAREA, 0, rectScreen, 0);
202                         if (rectScreen.Width() * .8 > maxWidth)
203                                 maxWidth = (int)(rectScreen.Width() * .8);
204
205                         // use the tooltip font
206                         HANDLE hFont = (HANDLE) ::SendMessage(pTTTStruct->hwndFrom, WM_GETFONT, 0, 0);
207                         CClientDC tempDC(this);
208                         HANDLE hOldFont = ::SelectObject(tempDC.GetSafeHdc(),hFont);
209
210                         // fill in the returned structure
211                         CFilepathEdit * pItem = static_cast<CFilepathEdit*>(GetDlgItem(nID));
212                         pTTT->lpszText = const_cast<TCHAR *>(pItem->GetUpdatedTipText(&tempDC, maxWidth).c_str());
213
214                         // set old font back
215                         if (hOldFont)
216                                 ::SelectObject(tempDC.GetSafeHdc(),hOldFont);
217
218                         // we must set TTM_SETMAXTIPWIDTH to use \n in tooltips
219                         // just to do the first time, but how to access the tooltip during init ?
220                         ::SendMessage(pTTTStruct->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 5000);
221
222                         return(TRUE);
223                 }
224         }
225         return(FALSE);
226 }
227
228 /** 
229  * @brief Set the path for one side
230  *
231  * @param [in] pane Index (0-based) of pane to update.
232  * @param [in] lpszString New text for pane.
233  */
234 void CEditorFilePathBar::SetText(int pane, const String& sString)
235 {
236         ASSERT (pane >= 0 && pane < countof(m_Edit));
237
238         // Check for NULL since window may be closing..
239         if (m_hWnd == NULL)
240                 return;
241
242         m_Edit[pane].SetOriginalText(sString);
243 }
244
245 /** 
246  * @brief Set the active status for one status (change the appearance)
247  *
248  * @param [in] pane Index (0-based) of pane to update.
249  * @param [in] bActive If TRUE activates pane, FALSE deactivates.
250  */
251 void CEditorFilePathBar::SetActive(int pane, bool bActive)
252 {
253         ASSERT (pane >= 0 && pane < countof(m_Edit));
254
255         // Check for NULL since window may be closing..
256         if (m_hWnd == NULL)
257                 return;
258
259         m_Edit[pane].SetActive(bActive);
260 }