OSDN Git Service

Fix the issue where the Apache Tika plugin becomes enabled again when reopening the...
[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                 sText.Empty();
119                 HICON hIcon = pArrChild->GetAt(i)->GetIcon(FALSE);
120                 if (NULL == hIcon)
121                 {
122                         hIcon = pArrChild->GetAt(i)->GetIcon(TRUE);
123                         if (NULL == hIcon)
124                                 hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
125                 }
126                 m_pIL->Add(hIcon);
127                 const CDocument* pDoc = pArrChild->GetAt(i)->GetActiveDocument();
128                 if (nullptr != pDoc)
129                         sText = pDoc->GetPathName();
130                 if (sText.IsEmpty())
131                         pArrChild->GetAt(i)->GetWindowText(sText);
132                 m_List.InsertItem(i, sText, m_pIL->GetImageCount() - 1);
133                 m_List.SetItemData(i, reinterpret_cast<DWORD_PTR>(pArrChild->GetAt(i)));
134         }
135 }
136 // adjust size to listctrl column and dialog
137 void CWindowsManagerDialog::AdjustSize()
138 {
139         CRect rect;
140         m_List.GetItemRect(0, rect, LVIR_ICON);
141
142         const int nImgWidth = rect.right - rect.left;
143         const int nSpaceWidth = m_List.GetStringWidth(_T(" "));
144         const int nLeftMargin = ::GetSystemMetrics(SM_CXFRAME) * 2 + nSpaceWidth * 4;
145
146         int nMaxWidth = -1;
147
148         CString sText;
149         CRect _rc(0, 0, 0, 0);
150         for (int i = 0; i < m_List.GetItemCount(); ++i)
151         {
152                 sText = m_List.GetItemText(i, 0);
153                 int nWidth = m_List.GetStringWidth(sText);
154                 if (nWidth > nMaxWidth)
155                         nMaxWidth = nWidth;
156                 _rc.bottom += rect.bottom - rect.top;
157         }
158
159         _rc.right = nMaxWidth + nImgWidth + nLeftMargin + 2 * ::GetSystemMetrics(SM_CYVSCROLL);
160         m_List.SetColumnWidth(0, _rc.right - ::GetSystemMetrics(SM_CYFRAME));
161
162         //if the tasklist exceeds the height of the display, leave some space at the bottom
163         if (_rc.bottom > ::GetSystemMetrics(SM_CYSCREEN) - 50)
164         {
165                 _rc.bottom = ::GetSystemMetrics(SM_CYSCREEN) - 50;
166                 m_List.SetColumnWidth(0, _rc.right - ::GetSystemMetrics(SM_CYFRAME) - ::GetSystemMetrics(SM_CYVSCROLL));
167         }
168         // Task List's border is 1px smaller than ::GetSystemMetrics(SM_CYFRAME) returns
169         _rc.bottom += ::GetSystemMetrics(SM_CYFRAME);
170         MoveWindow(_rc, FALSE);
171 }
172
173 void CWindowsManagerDialog::SetParentWnd(CWnd* pWnd)
174 {
175         ASSERT(NULL != pWnd);
176         m_pFrame = DYNAMIC_DOWNCAST(CMainFrame, pWnd);
177 }
178
179 void CWindowsManagerDialog::OnSize(UINT nType, int cx, int cy)
180 {
181         CDialog::OnSize(nType, cx, cy);
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         if (m_bAutoCleanup)
198                 DestroyWindow();
199         else
200                 CDialog::OnClose();
201 }
202
203 void CWindowsManagerDialog::OnOK()
204 {
205         if (m_bAutoCleanup)
206                 DestroyWindow();
207         else
208                 CDialog::OnOK();
209 }
210
211 void CWindowsManagerDialog::OnCancel()
212 {
213         if (m_bAutoCleanup)
214                 DestroyWindow();
215         else
216                 CDialog::OnCancel();
217 }
218
219 void CWindowsManagerDialog::PostNcDestroy()
220 {
221         CDialog::PostNcDestroy();
222
223         if (m_bAutoCleanup)
224                 delete this;
225 }
226
227 void CWindowsManagerDialog::OnDestroy()
228 {
229         CDialog::OnDestroy();
230
231         const int nIndex = m_List.GetNextItem(-1, LVNI_SELECTED);
232         if (nIndex >= 0 && nIndex < m_List.GetItemCount())
233                 ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), WMU_CHILDFRAMEACTIVATE, 0, static_cast<LPARAM>(m_List.GetItemData(nIndex)));
234 }
235
236 LRESULT CWindowsManagerDialog::OnIsOpen(WPARAM wParam, LPARAM lParam)
237 {
238         return 1;
239 }
240
241 LRESULT CWindowsManagerDialog::OnSelectNext(WPARAM wParam, LPARAM lParam)
242 {
243         int nIndex = m_List.GetNextItem(-1, LVNI_SELECTED);
244         if (wParam)             // reverse order
245         {
246                 if (0 == nIndex)
247                         nIndex = m_List.GetItemCount() - 1;
248                 else
249                         nIndex--;
250         }
251         else
252         {
253                 if (nIndex == m_List.GetItemCount() - 1)
254                         nIndex = 0;
255                 else
256                         nIndex++;
257         }
258
259         m_List.SetItemState(nIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
260         m_List.EnsureVisible(nIndex, FALSE);
261
262         return 1;
263 }
264
265 void CWindowsManagerDialog::OnNMCustomdrawListFile(NMHDR* pNMHDR, LRESULT* pResult)
266 {
267         NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
268
269         switch (pLVCD->nmcd.dwDrawStage)
270         {
271         case CDDS_PREPAINT:
272                 *pResult = CDRF_NOTIFYITEMDRAW;
273                 break;
274         case CDDS_ITEMPREPAINT:
275                 if (CDIS_FOCUS == (pLVCD->nmcd.uItemState & CDIS_FOCUS))
276                 {
277                         pLVCD->nmcd.uItemState = CDIS_DEFAULT;
278                         pLVCD->clrTextBk = WMD_LISTCOLOR_BKGSEL;
279                 }
280                 *pResult = CDRF_DODEFAULT;
281                 break;
282         default:
283                 *pResult = CDRF_DODEFAULT;
284                 break;
285         }
286 }
287
288 void CWindowsManagerDialog::OnNMDblclkListFile(NMHDR* pNMHDR, LRESULT* pResult)
289 {
290         *pResult = 0;
291
292         PostMessage(WM_CLOSE);
293 }