www.pudn.com > LECTEUR-MP3.zip > FileView.cpp


// FileView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "MusGest.h" 
#include "FileView.h" 
#include "MainFrm.h" 
#include "MusGestDoc.h" 
#include "ControlView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileView 
extern CMusGestApp theApp; 
 
IMPLEMENT_DYNCREATE(CFileView, CListView) 
 
CFileView::CFileView() 
{ 
	m_pImageList   = NULL; 
	m_pFileInPlay  = NULL; 
	m_pThread	   = NULL; 
} 
 
CFileView::~CFileView() 
{ 
	if (m_pImageList) 
		delete m_pImageList; 
 
	if (m_pThread) 
	{ 
		m_pThread->StopThread(); 
		delete m_pThread; 
		m_pThread = NULL; 
	} 
} 
 
 
BEGIN_MESSAGE_MAP(CFileView, CListView) 
	//{{AFX_MSG_MAP(CFileView) 
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick) 
	ON_MESSAGE(WM_MAJ_TIME, OnMajTime) 
	ON_MESSAGE(WM_STOP_THREAD, OnStopThread) 
	ON_MESSAGE(WM_CHANGE_SOUND_POS, OnChangeSoundPos) 
	ON_MESSAGE(WM_PAUSE_SOUND, OnPauseFileMessage) 
 
	ON_NOTIFY_REFLECT(HDN_ITEMDBLCLICK, OnItemdblclick) 
	ON_WM_SIZE() 
	//}}AFX_MSG_MAP 
	ON_COMMAND(ID_FILE_PLAY, OnPlayFile) 
	ON_COMMAND(ID_FILE_PAUSE, OnPauseFile) 
	ON_COMMAND(ID_FILE_STOP, OnStopFile) 
	ON_COMMAND(ID_FILE_VOLUME_PLUS, OnUpVolume) 
	ON_COMMAND(ID_FILE_VOLUME_MOIN, OnDownVolume) 
	ON_COMMAND(ID_FILE_PLAYLIST_ADD, OnAddPlayList) 
 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileView drawing 
 
void CFileView::OnDraw(CDC* pDC) 
{ 
	CDocument* pDoc = GetDocument(); 
	// TODO: add draw code here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileView diagnostics 
 
#ifdef _DEBUG 
void CFileView::AssertValid() const 
{ 
	CListView::AssertValid(); 
} 
 
void CFileView::Dump(CDumpContext& dc) const 
{ 
	CListView::Dump(dc); 
} 
#endif //_DEBUG 
 
void CFileView::OnSize(UINT nType, int cx, int cy)  
{ 
	CListView::OnSize(nType, cx, cy); 
} 
 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnInitialUpdate() 
{ 
	CListView::OnInitialUpdate(); 
 
	CListCtrl &List = GetListCtrl(); 
	List.SetExtendedStyle(LVS_EX_HEADERDRAGDROP | LVS_EX_INFOTIP); 
	List.SetExtendedStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT); 
 
	//Met le style report au liste view, ajoute les collones 
	List.ModifyStyle(LVS_LIST, LVS_REPORT | LVS_SHOWSELALWAYS | LVS_SINGLESEL); 
	List.InsertColumn(0,"Titre",LVCFMT_RIGHT,250); 
	List.InsertColumn(1,"Type",LVCFMT_LEFT,50); 
	List.InsertColumn(2,"Artiste",LVCFMT_LEFT,150); 
	List.InsertColumn(3,"Album",LVCFMT_LEFT,150); 
	List.InsertColumn(4,"Durée",LVCFMT_LEFT,100); 
 
	 
	CMusGestDoc * pDoc = (CMusGestDoc*)GetDocument(); 
 
	//Init du document 
	if (pDoc) 
	{ 
		pDoc->SetParentView(this); 
	} 
		 
	m_pImageList = new CImageList; 
	m_pImageList->Create(14, 14, ILC_COLOR8, 0, 4); 
 
	HICON hIcon = (HICON)LoadImage(theApp.m_hInstance,MAKEINTRESOURCE(IDI_AUDIO_FILE),IMAGE_ICON,14, 14,LR_DEFAULTCOLOR|LR_SHARED); 
	if (hIcon) m_pImageList->Add(hIcon); 
 
	hIcon = (HICON)LoadImage(theApp.m_hInstance,MAKEINTRESOURCE(IDI_AUDIO_FILE_PLAY),IMAGE_ICON,14, 14,LR_DEFAULTCOLOR|LR_SHARED); 
	if (hIcon) m_pImageList->Add(hIcon); 
 
	hIcon = (HICON)LoadImage(theApp.m_hInstance,MAKEINTRESOURCE(IDI_AUDIO_FILE_PAUSE),IMAGE_ICON,14, 14,LR_DEFAULTCOLOR|LR_SHARED); 
	if (hIcon) m_pImageList->Add(hIcon); 
 
	List.SetImageList(m_pImageList, LVSIL_SMALL); 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::ReInit() 
{ 
	CListCtrl &List = GetListCtrl(); 
 
	//Stoppe le thread si c'est pas deja fait 
	if (m_pThread) 
	{ 
		m_pThread->StopThread(); 
		delete m_pThread; 
		m_pThread = NULL; 
	} 
	 
	//Remet l'iconne std sur l'ancien elem en play si il y en a un 
	for (int i = 0; i< List.GetItemCount();i++) 
	{ 
		List.SetItem(i, 0, LVIF_IMAGE, 0, 0, 0,0,0); 
	} 
 
	SetStatusBarText("Pas de lecture en cour",0); 
	m_pFileInPlay = NULL; 
} 
 
 
//------------------------------------------------------------------------------------- 
void CFileView::InsertItem(int nItem, CRepFile * pRepFile) 
{ 
	int iImage = -1; 
	CListCtrl &List = GetListCtrl(); 
	CAudioFile * pFile = (CAudioFile*)pRepFile; 
 
	if (pFile) 
	{ 
		if(m_pFileInPlay == pFile) iImage = 1; 
		else iImage = 0; 
 
		int iItem = List.InsertItem(nItem,pFile->GetTitle(),iImage); 
 
		if (iItem != -1) 
		{ 
			List.SetItemText(iItem,1,pFile->GetFileExt()); 
			List.SetItemText(iItem,2,pFile->GetArtist()); 
			List.SetItemText(iItem,3,pFile->GetAlbum()); 
			List.SetItemText(iItem,4,pFile->GetStringLenght()); 
			List.SetItemData(iItem, (LPARAM)pFile); 
		} 
	} 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	CListCtrl &List = GetListCtrl(); 
    CPoint point; 
    CMenu menu,*pPopup; 
 	 
	if (GetSelectedItem() > -1) 
	{ 
		GetCursorPos(&point); 
		menu.LoadMenu(IDR_PLAY_MENU); 
		ASSERT (menu.m_hMenu != NULL); 
		pPopup = menu.GetSubMenu(0); 
		ASSERT (pPopup != NULL); 
 
		// insert a separator item at the top 
	    pPopup->InsertMenu(0, MF_BYPOSITION | MF_SEPARATOR, 0, "Options de lectures"); 
 
		// insert title item 
		// note: item is not selectable (disabled) but not grayed 
		pPopup->InsertMenu(0, MF_BYPOSITION | MF_STRING | MF_DISABLED, 0, "Options de lectures"); 
		SetMenuDefaultItem(pPopup->m_hMenu, 0, TRUE); 
 
		int nResult = pPopup->TrackPopupMenu(TPM_LEFTALIGN |  TPM_RIGHTBUTTON, point.x, point.y, this);   
   		menu.DestroyMenu(); 
	} 
 
	*pResult = 0; 
} 
 
//--------------------------------------------------------------------------------- 
void CFileView::OnItemdblclick(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR; 
	// TODO: Add your control notification handler code here 
	OnPlayFile(); 
	 
	*pResult = 0; 
} 
 
//------------------------------------------------------------------------------------- 
int CFileView::GetSelectedItem() 
{ 
	CListCtrl &List = GetListCtrl(); 
	int iItem = -1; 
	POSITION pos = NULL; 
	pos = List.GetFirstSelectedItemPosition(); 
 
	if (pos) 
		iItem = List.GetNextSelectedItem(pos); 
 
	return iItem; 
} 
 
 
//------------------------------------------------------------------------------------- 
void CFileView::RemoveAllItem() 
{ 
	CListCtrl &List = GetListCtrl(); 
	List.DeleteAllItems(); 
} 
 
 
//------------------------------------------------------------------------------------- 
void CFileView::SetStatusBarText(CString strText, int iPane) 
{ 
	CMainFrame * pFrame = (CMainFrame*)GetParentFrame(); 
 
	if (pFrame) 
	{ 
		CStatusBar * pBar = pFrame->GetStatusBar(); 
 
		if (pBar) 
		{ 
			pBar->SetPaneText(iPane,strText); 
		} 
	} 
} 
 
//------------------------------------------------------------------------------------- 
CRepFile * CFileView::GetSelectedRepFile() 
{ 
	int iItem = -1; 
	POSITION pos = NULL; 
	CListCtrl &List = GetListCtrl(); 
	pos = List.GetFirstSelectedItemPosition(); 
 
	if (pos) 
		iItem = List.GetNextSelectedItem(pos); 
	 
	if (iItem > -1) 
	{ 
		CRepFile * pFile = (CRepFile*)List.GetItemData(iItem); 
		return pFile; 
	} 
		 
	return NULL; 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnPlayFile() 
{ 
	CListCtrl &List = GetListCtrl(); 
	CMusGestDoc * pDoc = (CMusGestDoc*)GetDocument(); 
	CRepFile * pFile = GetSelectedRepFile(); 
	CMainFrame * pFrame = (CMainFrame*)GetParentFrame(); 
	CPlayListView * pView = NULL; 
 
	if (pFrame) 
		pView = (CPlayListView*)pFrame->GetRightView(1,0); 
 
	if (pView) 
	{ 
		if (pView->IsPlaying()) 
			if (AfxMessageBox("Arreter la lecture de la playlist ?", MB_YESNO) == IDYES) 
				pView->OnStopFile(); 
			else 
				return; 
	} 
 
	if (pDoc && pFile) 
	{ 
		int iRsl = pDoc->PlayFile(pFile); 
 
		if (iRsl == SOUND_IN_PLAY || iRsl == SOUND_IN_PLAY_AFTER_PAUSE) 
		{ 
			//Afiche le msg ds la status bar 
			if (pFile) 
			{ 
				CString strText; 
				strText.Format("Lecture en cour du fichier : %s",pFile->GetFileName()); 
				SetStatusBarText(strText,0); 
			} 
			 
 
			//Remet l'iconne std sur l'ancien elem en play si il y en a un 
			for (int i = 0; i< List.GetItemCount();i++) 
			{ 
				CAudioFile * pFile = (CAudioFile*)List.GetItemData(i); 
				if (pFile == m_pFileInPlay) 
				{ 
					List.SetItem(i, 0, LVIF_IMAGE, 0, 0, 0,0,0); 
				} 
			} 
 
			//Met l'image play 
			List.SetItem(GetSelectedItem(), 0, LVIF_IMAGE, 0, 1, 0,0,0); 
			m_pFileInPlay = (CAudioFile*)pFile; 
 
			//Met a jour la durré 
			List.SetItem(GetSelectedItem(), 4, LVIF_TEXT , m_pFileInPlay->GetStringLenght(), NULL, 0,0,0); 
 
			CAudioFile * pAudFile = (CAudioFile*)pFile; 
 
			CTimeSpan Time = pAudFile->GetFileLenght(); 
			int iWait = (Time.GetSeconds() + (Time.GetMinutes() * 60)) * 1000; 
 
			if (pFrame) 
			{ 
				CWnd * pWnd = pFrame->GetLeftView(); 
 
				if (pWnd) 
					pWnd->PostMessage(WM_FOLDER_IN_PLAY,0); 
 
			} 
			 
			if (iRsl == SOUND_IN_PLAY) 
			{ 
				//Stoppe le thread si c'est pas deja fait 
				if (m_pThread) 
				{ 
					m_pThread->StopThread(); 
					delete m_pThread; 
					m_pThread = NULL; 
				} 
				 
				//Recupère le ptr sur la playbar 
				CControlView * pView = NULL; 
				 
				if (pFrame) 
				{ 
					pView = (CControlView*)pFrame->GetRightView(2,0); 
					if (pView) 
						pView->SetFileTime(iWait); 
				} 
 
				//Créé le nouveau thread 
				m_pThread = new CWorkerThread(this,pView,&m_strTime); 
				 
				//Envoi la duréé du fichier 
				m_pThread->SetWaitTime(iWait); 
 
				//Demarre le thread 
				m_pThread->InitThread(); 
			} 
			else 
			{ 
				if (m_pThread) 
					m_pThread->PauseThread(false); 
			} 
		} 
	} 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnPauseFile() 
{ 
	CListCtrl &List = GetListCtrl(); 
	CMusGestDoc * pDoc = (CMusGestDoc*)GetDocument(); 
	CRepFile * pFile = GetSelectedRepFile(); 
 
	if (pDoc && pFile) 
	{ 
		int iRet = pDoc->PauseFile(pFile); 
 
		if (iRet == SOUND_IN_PLAY) 
		{ 
			CString strText; 
			strText.Format("Lecture en cour du fichier : %s",pFile->GetFileName()); 
			SetStatusBarText(strText,0); 
			List.SetItem(GetSelectedItem(), 0, LVIF_IMAGE, 0, 1, 0,0,0); 
 
			if (m_pThread) 
				m_pThread->PauseThread(false); 
		} 
		else if (iRet == SOUND_IN_PAUSE) 
		{ 
			CString strText; 
			strText.Format("Lecture en pause du fichier : %s",pFile->GetFileName()); 
			SetStatusBarText(strText,0); 
			List.SetItem(GetSelectedItem(), 0, LVIF_IMAGE, 0, 2, 0,0,0); 
			 
			if (m_pThread) 
				m_pThread->PauseThread(true); 
		} 
 
		m_pFileInPlay = (CAudioFile*)pFile; 
	} 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnStopFile() 
{ 
	CListCtrl &List = GetListCtrl(); 
	CMusGestDoc * pDoc = (CMusGestDoc*)GetDocument(); 
	CRepFile * pFile = GetSelectedRepFile(); 
	CMainFrame * pFrame = (CMainFrame*)GetParentFrame(); 
 
	if (pDoc && pFile) 
	{ 
		if (pDoc->StopFile(pFile) == SOUND_STOPPED) 
		{ 
			SetStatusBarText("  Pas de lecture en cour",0); 
			m_pFileInPlay = NULL; 
			List.SetItem(GetSelectedItem(), 0, LVIF_IMAGE, 0, 0, 0,0,0); 
 
			//Stoppe le thread si c'est pas deja fait 
			if (m_pThread) 
			{ 
				m_pThread->StopThread(); 
				delete m_pThread; 
				m_pThread = NULL; 
			} 
 
			if (pFrame) 
			{ 
				CWnd * pWnd = pFrame->GetLeftView(); 
 
				if (pWnd) 
					pWnd->PostMessage(WM_FOLDER_IN_PLAY,1); 
 
			} 
		} 
	} 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnDownVolume() 
{ 
	CMusGestDoc * pDoc = (CMusGestDoc*)GetDocument(); 
	CMainFrame * pFrame = (CMainFrame*)GetParentFrame(); 
 
	if (pDoc) 
	{ 
		int iVol = pDoc->DownVolume(); 
 
		if (pFrame) 
			pFrame->ShowVolume(iVol); 
	} 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnUpVolume() 
{ 
	CMusGestDoc * pDoc = (CMusGestDoc*)GetDocument(); 
	CMainFrame * pFrame = (CMainFrame*)GetParentFrame(); 
 
	if (pDoc) 
	{ 
		int iVol = pDoc->UpVolume(); 
	 
		if (pFrame) 
			pFrame->ShowVolume(iVol); 
	} 
} 
 
//------------------------------------------------------------------------------------- 
void CFileView::ChangeVolume(float fVol) 
{ 
	CMusGestDoc * pDoc = (CMusGestDoc*)GetDocument(); 
	CMainFrame * pFrame = (CMainFrame*)GetParentFrame(); 
 
	if (pDoc) 
	{ 
		int iVol = pDoc->ChangeVolume(fVol); 
		 
		if (pFrame) 
			pFrame->ShowVolume(iVol); 
	} 
} 
 
 
//------------------------------------------------------------------------------------- 
void CFileView::OnAddPlayList() 
{ 
	CRepFile * pFile = GetSelectedRepFile(); 
	CMainFrame * pFrame = (CMainFrame*)GetParentFrame(); 
 
	if (pFile) 
	{ 
		if (pFrame) 
		{ 
			CPlayListView * pPlayList = (CPlayListView*)pFrame->GetRightView(1,0); 
			if (pPlayList) 
				pPlayList->InsertItem(pFile); 
		} 
	} 
} 
 
 
//--------------------------------------------------------------------------------- 
LRESULT CFileView::OnMajTime(WPARAM w, LPARAM l) 
{ 
	CString strTime; 
 
	if (m_pFileInPlay) 
	{ 
		strTime.Format("Lecture en cour du Titre : %s \t Temp écoulé : %s",m_pFileInPlay->GetTitle(), m_strTime); 
		SetStatusBarText(strTime,0); 
	} 
 
	return TRUE; 
} 
 
 
//--------------------------------------------------------------------------------- 
LRESULT CFileView::OnStopThread(WPARAM w, LPARAM l) 
{ 
	ReInit(); 
	return TRUE; 
} 
 
//--------------------------------------------------------------------------------- 
LRESULT CFileView::OnChangeSoundPos(WPARAM w, LPARAM l) 
{ 
	if (m_pThread) 
	{ 
		if (!m_pThread->IsStopped()) 
			m_pThread->ChangePos((int)w); 
	} 
 
	return TRUE; 
} 
 
//--------------------------------------------------------------------------------- 
LRESULT CFileView:: OnPauseFileMessage(WPARAM w, LPARAM l) 
{ 
	OnPauseFile(); 
	return TRUE; 
}