OSDN Git Service

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