OSDN Git Service

WindowsManagerDialog.cpp: Fix warning C6001: Using uninitialized memory 'this'.
[winmerge-jp/winmerge-jp.git] / Src / WindowsManagerDialog.cpp
1 // WindowsManagerDialog.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "Merge.h"
6 #include "WindowsManagerDialog.h"
7
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
13
14 /////////////////////////////////////////////////////////////////////////////
15 // CWindowsManagerDialog dialog
16
17 CWindowsManagerDialog::CWindowsManagerDialog(CWnd* pParent/* = NULL*/)
18         : CDialog(CWindowsManagerDialog::IDD, pParent)
19         ,m_bAutoCleanup(FALSE)
20         ,m_pFrame(NULL)
21         ,m_pIL(NULL)
22 {
23         //{{AFX_DATA_INIT(CWindowsManagerDialog)
24                 // NOTE: the ClassWizard will add member initialization here
25         //}}AFX_DATA_INIT
26 }
27
28 CWindowsManagerDialog::~CWindowsManagerDialog()
29 {
30         if (NULL != m_pIL->GetSafeHandle())
31                 m_pIL->DeleteImageList();
32         if (NULL != m_pIL)
33                 delete m_pIL;
34 }
35
36 void CWindowsManagerDialog::DoDataExchange(CDataExchange* pDX)
37 {
38         CDialog::DoDataExchange(pDX);
39         //{{AFX_DATA_MAP(CWindowsManagerDialog)
40         // NOTE: the ClassWizard will add DDX and DDV calls here
41         //}}AFX_DATA_MAP
42         DDX_Control(pDX, IDC_LIST_FILE, m_List);
43 }
44
45 BEGIN_MESSAGE_MAP(CWindowsManagerDialog, CDialog)
46         //{{AFX_MSG_MAP(CWindowsManagerDialog)
47         ON_WM_SIZE()
48         ON_WM_CLOSE()
49         ON_WM_ACTIVATE()
50         ON_WM_DESTROY()
51         ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_FILE, &CWindowsManagerDialog::OnNMCustomdrawListFile)
52         ON_NOTIFY(NM_DBLCLK, IDC_LIST_FILE, &CWindowsManagerDialog::OnNMDblclkListFile)
53         //}}AFX_MSG_MAP
54         ON_MESSAGE(WMU_ISOPEN, &CWindowsManagerDialog::OnIsOpen)
55         ON_MESSAGE(WMU_SELECTNEXT, &CWindowsManagerDialog::OnSelectNext)
56 END_MESSAGE_MAP()
57
58 /////////////////////////////////////////////////////////////////////////////
59 // CWindowsManagerDialog message handlers
60
61 BOOL CWindowsManagerDialog::PreTranslateMessage(MSG* pMsg)
62 {
63         // TODO: Add your specialized code here and/or call the base class
64
65         if ((WM_KEYUP == pMsg->message && VK_CONTROL == pMsg->wParam) || 
66                 (WM_KEYDOWN == pMsg->message && VK_ESCAPE == pMsg->wParam))
67         {
68                 PostMessage(WM_CLOSE);
69         }
70
71         if (WM_KEYDOWN == pMsg->message && VK_TAB == pMsg->wParam)
72         {
73                 PostMessage(WMU_SELECTNEXT, (GetAsyncKeyState(VK_SHIFT) < 0) ? 1 : 0, 0);
74         }
75
76         return CDialog::PreTranslateMessage(pMsg);
77 }
78
79 BOOL CWindowsManagerDialog::OnInitDialog()
80 {
81         CDialog::OnInitDialog();
82
83         // TODO: Add extra initialization here
84
85         m_List.SetExtendedStyle(LVS_EX_INFOTIP | LVS_EX_FULLROWSELECT);
86
87         if (NULL == m_pIL)
88                 m_pIL = new CImageList();
89
90         m_pIL->DeleteImageList();
91         m_pIL->Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 1);
92         m_List.SetImageList(m_pIL, LVSIL_SMALL);
93         m_List.SetBkColor(WMD_LISTCOLOR_BKG);
94         m_List.SetTextBkColor(WMD_LISTCOLOR_BKG);
95
96         PopulateList();
97
98         if(m_List.GetItemCount() > 0)
99                 m_List.SetItemState((1 == m_List.GetItemCount()) ? 0 : 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
100
101         AdjustSize();
102         CenterWindow(m_pFrame); // please initialize the parent window before creating dialog
103
104         return TRUE;  // return TRUE unless you set the focus to a control
105                                   // EXCEPTION: OCX Property Pages should return FALSE
106 }
107
108 void CWindowsManagerDialog::PopulateList()
109 {
110         if (m_List.GetItemCount() <= 0)
111         {
112                 CRect rect;
113                 m_List.GetWindowRect(rect);
114                 m_List.InsertColumn(0, _T("Files"), LVCFMT_LEFT, rect.Width());
115         }
116
117         m_List.DeleteAllItems();
118
119         CString sText;
120         const CTypedPtrArray<CPtrArray, CMDIChildWnd*>* pArrChild = m_pFrame->GetChildArray();
121         for (int i = 0; i < pArrChild->GetSize(); ++i)
122         {
123                 HICON hIcon = pArrChild->GetAt(i)->GetIcon(TRUE);
124                 if (NULL == hIcon)
125                         hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
126                 m_pIL->Add(hIcon);
127                 sText = pArrChild->GetAt(i)->GetActiveDocument()->GetPathName();
128                 if(sText.IsEmpty())
129                         pArrChild->GetAt(i)->GetWindowText(sText);
130                 m_List.InsertItem(i, sText, m_pIL->GetImageCount() - 1);
131                 m_List.SetItemData(i, (DWORD_PTR)pArrChild->GetAt(i));
132         }
133 }
134 // adjust size to listctrl column and dialog
135 void CWindowsManagerDialog::AdjustSize()
136 {
137         CRect rect;
138         m_List.GetItemRect(0, rect, LVIR_ICON);
139
140         const int nImgWidth = rect.right - rect.left;
141         const int nSpaceWidth = m_List.GetStringWidth(_T(" "));
142         const int nLeftMargin = ::GetSystemMetrics(SM_CXFRAME) * 2 + nSpaceWidth * 4;
143
144         int nMaxWidth = -1;
145
146         CString sText;
147         CRect _rc(0, 0, 0, 0);
148         for (int i = 0; i < m_List.GetItemCount(); ++i)
149         {
150                 sText = m_List.GetItemText(i, 0);
151                 int nWidth = m_List.GetStringWidth(sText);
152                 if (nWidth > nMaxWidth)
153                         nMaxWidth = nWidth;
154                 _rc.bottom += rect.bottom - rect.top;
155         }
156
157         _rc.right = nMaxWidth + nImgWidth + nLeftMargin + 2 * ::GetSystemMetrics(SM_CYVSCROLL);
158         m_List.SetColumnWidth(0, _rc.right - ::GetSystemMetrics(SM_CYFRAME));
159
160         //if the tasklist exceeds the height of the display, leave some space at the bottom
161         if (_rc.bottom > ::GetSystemMetrics(SM_CYSCREEN) - 50)
162         {
163                 _rc.bottom = ::GetSystemMetrics(SM_CYSCREEN) - 50;
164                 m_List.SetColumnWidth(0, _rc.right - ::GetSystemMetrics(SM_CYFRAME) - ::GetSystemMetrics(SM_CYVSCROLL));
165         }
166         // Task List's border is 1px smaller than ::GetSystemMetrics(SM_CYFRAME) returns
167         _rc.bottom += ::GetSystemMetrics(SM_CYFRAME);
168         MoveWindow(_rc, FALSE);
169 }
170
171 void CWindowsManagerDialog::SetParentWnd(CWnd* pWnd)
172 {
173         ASSERT(NULL != pWnd);
174         m_pFrame = DYNAMIC_DOWNCAST(CMainFrame, pWnd);
175 }
176
177 void CWindowsManagerDialog::OnSize(UINT nType, int cx, int cy)
178 {
179         CDialog::OnSize(nType, cx, cy);
180
181         // TODO: Add your message handler code here
182
183         if(NULL != m_List.GetSafeHwnd() && cx > 0 && cy > 0)
184                 m_List.MoveWindow(0, 0, cx, cy);
185 }
186
187 void CWindowsManagerDialog::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
188 {
189         CDialog::OnActivate(nState, pWndOther, bMinimized);
190
191         if (WA_INACTIVE == nState)
192                 PostMessage(WM_CLOSE);
193 }
194
195 void CWindowsManagerDialog::OnClose()
196 {
197         // TODO: Add your message handler code here and/or call default
198
199         if (m_bAutoCleanup)
200                 DestroyWindow();
201         else
202                 CDialog::OnClose();
203 }
204
205 void CWindowsManagerDialog::OnOK()
206 {
207         // TODO: Add extra validation here
208
209         if (m_bAutoCleanup)
210                 DestroyWindow();
211         else
212                 CDialog::OnOK();
213 }
214
215 void CWindowsManagerDialog::OnCancel()
216 {
217         // TODO: Add extra cleanup here
218
219         if (m_bAutoCleanup)
220                 DestroyWindow();
221         else
222                 CDialog::OnCancel();
223 }
224
225 void CWindowsManagerDialog::PostNcDestroy()
226 {
227         // TODO: Add your specialized code here and/or call the base class
228         CDialog::PostNcDestroy();
229
230         if (m_bAutoCleanup)
231                 delete this;
232 }
233
234 void CWindowsManagerDialog::OnDestroy()
235 {
236         CDialog::OnDestroy();
237
238         // TODO: Add your message handler code here
239
240         const int nIndex = m_List.GetNextItem(-1, LVNI_SELECTED);
241         if (nIndex >= 0 && nIndex < m_List.GetItemCount())
242                 ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), WMU_CHILDFRAMEACTIVATE, 0, (LPARAM)m_List.GetItemData(nIndex));
243 }
244
245 LRESULT CWindowsManagerDialog::OnIsOpen(WPARAM wParam, LPARAM lParam)
246 {
247         return 1;
248 }
249
250 LRESULT CWindowsManagerDialog::OnSelectNext(WPARAM wParam, LPARAM lParam)
251 {
252         int nIndex = m_List.GetNextItem(-1, LVNI_SELECTED);
253         if (wParam)             // reverse order
254         {
255                 if (0 == nIndex)
256                         nIndex = m_List.GetItemCount() - 1;
257                 else
258                         nIndex--;
259         }
260         else
261         {
262                 if (nIndex == m_List.GetItemCount() - 1)
263                         nIndex = 0;
264                 else
265                         nIndex++;
266         }
267
268         m_List.SetItemState(nIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
269         m_List.EnsureVisible(nIndex, FALSE);
270
271         return 1;
272 }
273
274 void CWindowsManagerDialog::OnNMCustomdrawListFile(NMHDR* pNMHDR, LRESULT* pResult)
275 {
276 //      LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
277         NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
278         // TODO: Add your control notification handler code here
279
280         switch (pLVCD->nmcd.dwDrawStage)
281         {
282         case CDDS_PREPAINT:
283                 *pResult = CDRF_NOTIFYITEMDRAW;
284                 break;
285         case CDDS_ITEMPREPAINT:
286                 if (CDIS_FOCUS == (pLVCD->nmcd.uItemState & CDIS_FOCUS))
287                 {
288                         pLVCD->nmcd.uItemState = CDIS_DEFAULT;
289                         pLVCD->clrTextBk = WMD_LISTCOLOR_BKGSEL;
290                 }
291                 *pResult = CDRF_DODEFAULT;
292                 break;
293         default:
294                 *pResult = CDRF_DODEFAULT;
295                 break;
296         }
297 }
298
299 void CWindowsManagerDialog::OnNMDblclkListFile(NMHDR* pNMHDR, LRESULT* pResult)
300 {
301         LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
302         // TODO: Add your control notification handler code here
303         *pResult = 0;
304
305         PostMessage(WM_CLOSE);
306 }