1 // WindowsManagerDialog.cpp : implementation file
6 #include "WindowsManagerDialog.h"
11 static char THIS_FILE[] = __FILE__;
14 /////////////////////////////////////////////////////////////////////////////
15 // CWindowsManagerDialog dialog
17 CWindowsManagerDialog::CWindowsManagerDialog(CWnd* pParent/* = NULL*/)
18 : CDialog(CWindowsManagerDialog::IDD, pParent)
19 ,m_bAutoCleanup(FALSE)
23 //{{AFX_DATA_INIT(CWindowsManagerDialog)
24 // NOTE: the ClassWizard will add member initialization here
28 CWindowsManagerDialog::~CWindowsManagerDialog()
30 if (NULL != m_pIL->GetSafeHandle())
31 m_pIL->DeleteImageList();
35 void CWindowsManagerDialog::DoDataExchange(CDataExchange* pDX)
37 CDialog::DoDataExchange(pDX);
38 //{{AFX_DATA_MAP(CWindowsManagerDialog)
39 // NOTE: the ClassWizard will add DDX and DDV calls here
41 DDX_Control(pDX, IDC_LIST_FILE, m_List);
44 BEGIN_MESSAGE_MAP(CWindowsManagerDialog, CDialog)
45 //{{AFX_MSG_MAP(CWindowsManagerDialog)
50 ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_FILE, &CWindowsManagerDialog::OnNMCustomdrawListFile)
51 ON_NOTIFY(NM_DBLCLK, IDC_LIST_FILE, &CWindowsManagerDialog::OnNMDblclkListFile)
53 ON_MESSAGE(WMU_ISOPEN, &CWindowsManagerDialog::OnIsOpen)
54 ON_MESSAGE(WMU_SELECTNEXT, &CWindowsManagerDialog::OnSelectNext)
57 /////////////////////////////////////////////////////////////////////////////
58 // CWindowsManagerDialog message handlers
60 BOOL CWindowsManagerDialog::PreTranslateMessage(MSG* pMsg)
62 if ((WM_KEYUP == pMsg->message && VK_CONTROL == pMsg->wParam) ||
63 (WM_KEYDOWN == pMsg->message && VK_ESCAPE == pMsg->wParam))
65 PostMessage(WM_CLOSE);
68 if (WM_KEYDOWN == pMsg->message && VK_TAB == pMsg->wParam)
70 PostMessage(WMU_SELECTNEXT, (GetAsyncKeyState(VK_SHIFT) < 0) ? 1 : 0, 0);
73 return CDialog::PreTranslateMessage(pMsg);
76 BOOL CWindowsManagerDialog::OnInitDialog()
78 CDialog::OnInitDialog();
80 m_List.SetExtendedStyle(LVS_EX_INFOTIP | LVS_EX_FULLROWSELECT);
83 m_pIL = new CImageList();
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);
93 if(m_List.GetItemCount() > 0)
94 m_List.SetItemState((1 == m_List.GetItemCount()) ? 0 : 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
97 CenterWindow(m_pFrame); // please initialize the parent window before creating dialog
99 return TRUE; // return TRUE unless you set the focus to a control
100 // EXCEPTION: OCX Property Pages should return FALSE
103 void CWindowsManagerDialog::PopulateList()
105 if (m_List.GetItemCount() <= 0)
108 m_List.GetWindowRect(rect);
109 m_List.InsertColumn(0, _T("Files"), LVCFMT_LEFT, rect.Width());
112 m_List.DeleteAllItems();
115 const CTypedPtrArray<CPtrArray, CMDIChildWnd*>* pArrChild = m_pFrame->GetChildArray();
116 for (int i = 0; i < pArrChild->GetSize(); ++i)
119 HICON hIcon = pArrChild->GetAt(i)->GetIcon(FALSE);
122 hIcon = pArrChild->GetAt(i)->GetIcon(TRUE);
124 hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
127 const CDocument* pDoc = pArrChild->GetAt(i)->GetActiveDocument();
129 sText = pDoc->GetPathName();
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)));
136 // adjust size to listctrl column and dialog
137 void CWindowsManagerDialog::AdjustSize()
140 m_List.GetItemRect(0, rect, LVIR_ICON);
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;
149 CRect _rc(0, 0, 0, 0);
150 for (int i = 0; i < m_List.GetItemCount(); ++i)
152 sText = m_List.GetItemText(i, 0);
153 int nWidth = m_List.GetStringWidth(sText);
154 if (nWidth > nMaxWidth)
156 _rc.bottom += rect.bottom - rect.top;
159 _rc.right = nMaxWidth + nImgWidth + nLeftMargin + 2 * ::GetSystemMetrics(SM_CYVSCROLL);
160 m_List.SetColumnWidth(0, _rc.right - ::GetSystemMetrics(SM_CYFRAME));
162 //if the tasklist exceeds the height of the display, leave some space at the bottom
163 if (_rc.bottom > ::GetSystemMetrics(SM_CYSCREEN) - 50)
165 _rc.bottom = ::GetSystemMetrics(SM_CYSCREEN) - 50;
166 m_List.SetColumnWidth(0, _rc.right - ::GetSystemMetrics(SM_CYFRAME) - ::GetSystemMetrics(SM_CYVSCROLL));
168 // Task List's border is 1px smaller than ::GetSystemMetrics(SM_CYFRAME) returns
169 _rc.bottom += ::GetSystemMetrics(SM_CYFRAME);
170 MoveWindow(_rc, FALSE);
173 void CWindowsManagerDialog::SetParentWnd(CWnd* pWnd)
175 ASSERT(NULL != pWnd);
176 m_pFrame = DYNAMIC_DOWNCAST(CMainFrame, pWnd);
179 void CWindowsManagerDialog::OnSize(UINT nType, int cx, int cy)
181 CDialog::OnSize(nType, cx, cy);
183 if(NULL != m_List.GetSafeHwnd() && cx > 0 && cy > 0)
184 m_List.MoveWindow(0, 0, cx, cy);
187 void CWindowsManagerDialog::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
189 CDialog::OnActivate(nState, pWndOther, bMinimized);
191 if (WA_INACTIVE == nState)
192 PostMessage(WM_CLOSE);
195 void CWindowsManagerDialog::OnClose()
203 void CWindowsManagerDialog::OnOK()
211 void CWindowsManagerDialog::OnCancel()
219 void CWindowsManagerDialog::PostNcDestroy()
221 CDialog::PostNcDestroy();
227 void CWindowsManagerDialog::OnDestroy()
229 CDialog::OnDestroy();
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)));
236 LRESULT CWindowsManagerDialog::OnIsOpen(WPARAM wParam, LPARAM lParam)
241 LRESULT CWindowsManagerDialog::OnSelectNext(WPARAM wParam, LPARAM lParam)
243 int nIndex = m_List.GetNextItem(-1, LVNI_SELECTED);
244 if (wParam) // reverse order
247 nIndex = m_List.GetItemCount() - 1;
253 if (nIndex == m_List.GetItemCount() - 1)
259 m_List.SetItemState(nIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
260 m_List.EnsureVisible(nIndex, FALSE);
265 void CWindowsManagerDialog::OnNMCustomdrawListFile(NMHDR* pNMHDR, LRESULT* pResult)
267 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
269 switch (pLVCD->nmcd.dwDrawStage)
272 *pResult = CDRF_NOTIFYITEMDRAW;
274 case CDDS_ITEMPREPAINT:
275 if (CDIS_FOCUS == (pLVCD->nmcd.uItemState & CDIS_FOCUS))
277 pLVCD->nmcd.uItemState = CDIS_DEFAULT;
278 pLVCD->clrTextBk = WMD_LISTCOLOR_BKGSEL;
280 *pResult = CDRF_DODEFAULT;
283 *pResult = CDRF_DODEFAULT;
288 void CWindowsManagerDialog::OnNMDblclkListFile(NMHDR* pNMHDR, LRESULT* pResult)
292 PostMessage(WM_CLOSE);