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