OSDN Git Service

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