Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

CtrlMediaList.cpp

Go to the documentation of this file.
00001 //
00002 // CtrlMediaList.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Library.h"
00026 #include "SharedFile.h"
00027 #include "ShellIcons.h"
00028 #include "LiveList.h"
00029 #include "Skin.h"
00030 #include "Buffer.h"
00031 #include "CtrlMediaList.h"
00032 
00033 #ifdef _DEBUG
00034 #define new DEBUG_NEW
00035 #undef THIS_FILE
00036 static char THIS_FILE[] = __FILE__;
00037 #endif
00038 
00039 IMPLEMENT_DYNAMIC(CMediaListCtrl, CListCtrl)
00040 
00041 BEGIN_MESSAGE_MAP(CMediaListCtrl, CListCtrl)
00042         //{{AFX_MSG_MAP(CMediaListCtrl)
00043         ON_WM_CREATE()
00044         ON_NOTIFY_REFLECT(NM_DBLCLK, OnDoubleClick)
00045         ON_WM_CONTEXTMENU()
00046         ON_COMMAND(ID_MEDIA_ADD, OnMediaAdd)
00047         ON_UPDATE_COMMAND_UI(ID_MEDIA_REMOVE, OnUpdateMediaRemove)
00048         ON_COMMAND(ID_MEDIA_REMOVE, OnMediaRemove)
00049         ON_UPDATE_COMMAND_UI(ID_MEDIA_CLEAR, OnUpdateMediaClear)
00050         ON_COMMAND(ID_MEDIA_CLEAR, OnMediaClear)
00051         ON_UPDATE_COMMAND_UI(ID_MEDIA_SELECT, OnUpdateMediaSelect)
00052         ON_COMMAND(ID_MEDIA_SELECT, OnMediaSelect)
00053         ON_UPDATE_COMMAND_UI(ID_MEDIA_SAVE, OnUpdateMediaSave)
00054         ON_COMMAND(ID_MEDIA_SAVE, OnMediaSave)
00055         ON_COMMAND(ID_MEDIA_OPEN, OnMediaOpen)
00056         ON_UPDATE_COMMAND_UI(ID_MEDIA_PREVIOUS, OnUpdateMediaPrevious)
00057         ON_COMMAND(ID_MEDIA_PREVIOUS, OnMediaPrevious)
00058         ON_UPDATE_COMMAND_UI(ID_MEDIA_NEXT, OnUpdateMediaNext)
00059         ON_COMMAND(ID_MEDIA_NEXT, OnMediaNext)
00060         ON_NOTIFY_REFLECT(LVN_KEYDOWN, OnKeyDown)
00061         ON_WM_SIZE()
00062         ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBeginDrag)
00063         ON_WM_MOUSEMOVE()
00064         ON_WM_LBUTTONUP()
00065         ON_UPDATE_COMMAND_UI(ID_MEDIA_REPEAT, OnUpdateMediaRepeat)
00066         ON_COMMAND(ID_MEDIA_REPEAT, OnMediaRepeat)
00067         ON_UPDATE_COMMAND_UI(ID_MEDIA_RANDOM, OnUpdateMediaRandom)
00068         ON_COMMAND(ID_MEDIA_RANDOM, OnMediaRandom)
00069         ON_WM_LBUTTONDOWN()
00070         ON_WM_RBUTTONDOWN()
00071         ON_COMMAND(ID_MEDIA_ADD_FOLDER, OnMediaAddFolder)
00072         //}}AFX_MSG_MAP
00073         ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
00074 END_MESSAGE_MAP()
00075 
00076 #define STATE_CURRENT   (1<<12)
00077 #define STATE_PLAYED    (1<<13)
00078 
00079 
00081 // CMediaListCtrl construction
00082 
00083 CMediaListCtrl::CMediaListCtrl()
00084 {
00085         m_pDragImage            = NULL;
00086         m_bCreateDragImage      = FALSE;
00087 }
00088 
00089 CMediaListCtrl::~CMediaListCtrl()
00090 {
00091 }
00092 
00094 // CMediaListCtrl operations
00095 
00096 BOOL CMediaListCtrl::Create(CWnd* pParentWnd, UINT nID) 
00097 {
00098         CRect rect;
00099 
00100         return CListCtrl::Create(
00101                 WS_CHILD|WS_VSCROLL|LVS_ICON|LVS_REPORT|LVS_SHAREIMAGELISTS|LVS_NOCOLUMNHEADER,
00102                 rect, pParentWnd, nID );
00103 }
00104 
00105 BOOL CMediaListCtrl::Open(LPCTSTR pszFile)
00106 {
00107         Clear();
00108         
00109         Enqueue( pszFile, FALSE );
00110         
00111         if ( GetItemCount() == 0 ) return FALSE;
00112         
00113         GetNext();
00114         
00115         return TRUE;
00116 }
00117 
00118 BOOL CMediaListCtrl::Enqueue(LPCTSTR pszFile, BOOL bStart)
00119 {
00120         if ( _tcsistr( pszFile, _T(".m3u") ) != NULL ||
00121                  _tcsistr( pszFile, _T(".pls") ) != NULL )
00122         {
00123                 LoadTextList( pszFile );
00124         }
00125         else
00126         {
00127                 Add( pszFile );
00128         }
00129         
00130         if ( bStart && GetItemCount() == 1 ) GetNext();
00131         
00132         return TRUE;
00133 }
00134 
00135 int CMediaListCtrl::RecursiveEnqueue(LPCTSTR pszPath)
00136 {
00137         WIN32_FIND_DATA pFind;
00138         CString strPath;
00139         HANDLE hSearch;
00140         int nCount = 0;
00141         
00142         strPath.Format( _T("%s\\*.*"), pszPath );
00143         
00144         hSearch = FindFirstFile( strPath, &pFind );
00145         
00146         if ( hSearch != INVALID_HANDLE_VALUE )
00147         {
00148                 do
00149                 {
00150                         if ( pFind.cFileName[0] == '.' ) continue;
00151                         if ( _tcsicmp( pFind.cFileName, _T("Metadata") ) == 0 ) continue;
00152                         
00153                         strPath.Format( _T("%s\\%s"), pszPath, pFind.cFileName );
00154                         
00155                         if ( pFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
00156                         {
00157                                 nCount += RecursiveEnqueue( strPath );
00158                         }
00159                         else
00160                         {
00161                                 nCount += Enqueue( strPath, FALSE );
00162                         }
00163                 }
00164                 while ( FindNextFile( hSearch, &pFind ) );
00165                 
00166                 FindClose( hSearch );
00167         }
00168         
00169         return nCount;
00170 }
00171 
00172 void CMediaListCtrl::Remove(LPCTSTR pszFile)
00173 {
00174         for ( int nItem = GetItemCount() - 1 ; nItem >= 0 ; nItem-- )
00175         {
00176                 if ( GetItemText( nItem, 1 ).CompareNoCase( pszFile ) == 0 )
00177                 {
00178                         Remove( nItem );
00179                 }
00180         }
00181 }
00182 
00183 BOOL CMediaListCtrl::LoadTextList(LPCTSTR pszFile)
00184 {
00185         CString strPath, strItem;
00186         CFile pFile;
00187         
00188         strPath = pszFile;
00189         strPath = strPath.Left( strPath.ReverseFind( '\\' ) + 1 );
00190         
00191         if ( ! pFile.Open( pszFile, CFile::modeRead ) ) return FALSE;
00192         
00193         CBuffer pBuffer;
00194         pBuffer.EnsureBuffer( (DWORD)pFile.GetLength() );
00195         pBuffer.m_nLength = (DWORD)pFile.GetLength();
00196         pFile.Read( pBuffer.m_pBuffer, pBuffer.m_nLength );
00197         pFile.Close();
00198         
00199         while ( pBuffer.ReadLine( strItem ) )
00200         {
00201                 strItem.TrimLeft();
00202                 strItem.TrimRight();
00203 
00204                 if ( strItem.GetLength() && strItem.GetAt( 0 ) != '#' )
00205                 {
00206                         if ( strItem.Find( '\\' ) != 0 &&
00207                                  strItem.Find( ':' ) != 1 )
00208                                 strItem = strPath + strItem;
00209 
00210                         if ( GetFileAttributes( strItem ) != 0xFFFFFFFF )
00211                                 Enqueue( strItem, FALSE );
00212                 }
00213         }
00214         
00215         return TRUE;
00216 }
00217 
00218 int CMediaListCtrl::Add(LPCTSTR pszPath, int nItem)
00219 {
00220         LPCTSTR pszFile = _tcsrchr( pszPath, '\\' );
00221         pszFile = pszFile ? pszFile + 1 : pszPath;
00222         
00223         DWORD nFile = 0;
00224         {
00225                 CQuickLock oLock( Library.m_pSection );
00226                 if ( CLibraryFile* pFile = LibraryMaps.LookupFileByPath( pszPath ) )
00227                 {
00228                         nFile = pFile->m_nIndex;
00229                 }
00230         }
00231         
00232         LV_ITEM pItem;
00233         CString strTemp = (LPTSTR)pszFile;
00234         int nDotPos = strTemp.ReverseFind( '.' );
00235         if ( nDotPos != -1 ) strTemp = strTemp.Left( nDotPos );
00236         LPTSTR pszFileTmp = strTemp.GetBuffer( strTemp.GetLength() );
00237         ZeroMemory( &pItem, sizeof(pItem) );
00238         pItem.mask              = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
00239         pItem.iItem             = nItem >= 0 ? nItem : GetItemCount();
00240         pItem.iImage    = ShellIcons.Get( pszFile, 16 );
00241         pItem.lParam    = nFile;
00242         pItem.pszText   = pszFileTmp;
00243         pItem.iItem             = InsertItem( &pItem );
00244         pItem.mask              = LVIF_TEXT;
00245         pItem.iSubItem  = 1;
00246         pItem.pszText   = (LPTSTR)pszPath;
00247         SetItem( &pItem );
00248         strTemp.ReleaseBuffer();
00249         return pItem.iItem;
00250 }
00251 
00252 void CMediaListCtrl::Remove(int nItem)
00253 {
00254         if ( nItem < 0 || nItem >= GetItemCount() ) return;
00255         
00256         BOOL bActive = GetItemState( nItem, STATE_CURRENT );
00257         DeleteItem( nItem );
00258         
00259         if ( bActive ) SetCurrent( nItem );
00260 }
00261 
00262 int CMediaListCtrl::GetCount()
00263 {
00264         return GetItemCount();
00265 }
00266 
00267 void CMediaListCtrl::Clear()
00268 {
00269         DeleteAllItems();
00270         SetCurrent( -1 );
00271 }
00272 
00273 int CMediaListCtrl::GetCurrent()
00274 {
00275         for ( int nItem = GetItemCount() - 1 ; nItem >= 0 ; nItem-- )
00276         {
00277                 if ( GetItemState( nItem, STATE_CURRENT ) ) return nItem;
00278         }
00279 
00280         return -1;
00281 }
00282 
00283 void CMediaListCtrl::SetCurrent(int nCurrent)
00284 {
00285         for ( int nItem = GetItemCount() - 1 ; nItem >= 0 ; nItem-- )
00286         {
00287                 if ( GetItemState( nItem, STATE_CURRENT ) )
00288                 {
00289                         if ( nItem != nCurrent ) SetItemState( nItem, 0, STATE_CURRENT );
00290                 }
00291                 else
00292                 {
00293                         if ( nItem == nCurrent ) SetItemState( nItem, STATE_CURRENT, STATE_CURRENT );
00294                 }
00295         }
00296 
00297         NMHDR pNM = { GetSafeHwnd(), GetDlgCtrlID(), MLN_NEWCURRENT };
00298         CString strPath = GetPath( nCurrent );
00299         GetSafeOwner()->SendMessage( WM_NOTIFY, pNM.idFrom, (LPARAM)&pNM );
00300 }
00301 
00302 int CMediaListCtrl::GetNext(BOOL bSet)
00303 {
00304         int nItem = GetCurrent();
00305 
00306         if ( nItem >= 0 ) SetItemState( nItem, STATE_PLAYED, STATE_PLAYED );
00307 
00308         if ( Settings.MediaPlayer.Random )
00309         {
00310                 int nCount = 0;
00311 
00312                 for ( nItem = GetItemCount() - 1 ; nItem >= 0 ; nItem-- )
00313                 {
00314                         if ( GetItemState( nItem, STATE_PLAYED ) == 0 ) nCount++;
00315                 }
00316 
00317                 if ( nCount > 0 )
00318                 {
00319                         nCount = rand() % nCount;
00320 
00321                         for ( nItem = GetItemCount() - 1 ; nItem >= 0 ; nItem-- )
00322                         {
00323                                 if ( GetItemState( nItem, STATE_PLAYED ) == 0 )
00324                                 {
00325                                         if ( nCount-- == 0 ) break;
00326                                 }
00327                         }
00328                 }
00329         }
00330         else
00331         {
00332                 if ( ++nItem >= GetItemCount() ) nItem = -1;
00333         }
00334 
00335         if ( bSet ) SetCurrent( nItem );
00336 
00337         return nItem;
00338 }
00339 
00340 void CMediaListCtrl::Reset(BOOL bNext)
00341 {
00342         for ( int nItem = GetItemCount() - 1 ; nItem >= 0 ; nItem-- )
00343                 SetItemState( nItem, 0, STATE_PLAYED );
00344         if ( bNext ) SetCurrent( -1 );
00345 }
00346 
00347 CString CMediaListCtrl::GetPath(int nItem)
00348 {
00349         CString str;
00350         if ( nItem < 0 || nItem >= GetItemCount() ) return str;
00351         return GetItemText( nItem, 1 );
00352 }
00353 
00355 // CMediaListCtrl message handlers
00356 
00357 int CMediaListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
00358 {
00359         if ( CListCtrl::OnCreate( lpCreateStruct ) == -1 ) return -1;
00360         
00361         SetBkColor( RGB( 0, 0, 0x30 ) );
00362         SetImageList( ShellIcons.GetObject( 16 ), LVSIL_SMALL );
00363         InsertColumn( 0, _T("Name"), LVCFMT_LEFT, 100, -1 );
00364         InsertColumn( 1, _T("Path"), LVCFMT_LEFT, 0, 0 );
00365 
00366         // SendMessage( LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_LABELTIP, LVS_EX_LABELTIP );
00367         
00368         m_wndTip.Create( this, &Settings.Interface.TipMedia );
00369 
00370         return 0;
00371 }
00372 
00373 void CMediaListCtrl::OnSize(UINT nType, int cx, int cy) 
00374 {
00375         CListCtrl::OnSize( nType, cx, cy );
00376         
00377         CRect rc;
00378         GetWindowRect( &rc );
00379         
00380         LV_COLUMN pColumn;
00381         pColumn.mask    = LVCF_WIDTH;
00382         pColumn.cx              = rc.Width() - GetSystemMetrics( SM_CXVSCROLL ) - 8;
00383         SetColumn( 0, &pColumn );
00384 }
00385 
00386 void CMediaListCtrl::OnCustomDraw(NMLVCUSTOMDRAW* pNotify, LRESULT* pResult)
00387 {
00388         if ( pNotify->nmcd.dwDrawStage == CDDS_PREPAINT )
00389         {
00390                 *pResult = CDRF_NOTIFYITEMDRAW;
00391         }
00392         else if ( pNotify->nmcd.dwDrawStage == CDDS_ITEMPREPAINT )
00393         {
00394                 if (    GetItemState( pNotify->nmcd.dwItemSpec, LVIS_SELECTED ) == 0 &&
00395                                 GetItemState( pNotify->nmcd.dwItemSpec, STATE_CURRENT ) != 0 )
00396                 {
00397                         pNotify->clrText        = RGB( 255, 255, 255 );
00398                         pNotify->clrTextBk      = RGB( 128, 0, 0 );
00399                 }
00400                 else
00401                 {
00402                         pNotify->clrText        = RGB( 255, 255, 255 );
00403                         pNotify->clrTextBk      = RGB( 0, 0, 0x30 );
00404                 }
00405 
00406                 if ( m_bCreateDragImage ) pNotify->clrTextBk = RGB( 0, 255, 0 );
00407 
00408                 *pResult = CDRF_DODEFAULT;
00409         }
00410         else
00411         {
00412                 *pResult = 0;
00413         }
00414 }
00415 
00416 void CMediaListCtrl::OnDoubleClick(NMHDR* pNMHDR, LRESULT* pResult) 
00417 {
00418         OnMediaSelect();
00419         *pResult = 0;
00420 }
00421 
00422 void CMediaListCtrl::OnContextMenu(CWnd* pWnd, CPoint point) 
00423 {
00424         Skin.TrackPopupMenu( _T("CMediaList"), point, ID_MEDIA_SELECT );
00425 }
00426 
00427 void CMediaListCtrl::OnKeyDown(NMHDR* pNMHDR, LRESULT* pResult) 
00428 {
00429         LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
00430         
00431         if ( pLVKeyDow->wVKey == VK_RETURN )
00432                 PostMessage( WM_COMMAND, ID_MEDIA_SELECT );
00433         else if ( pLVKeyDow->wVKey == VK_DELETE )
00434                 PostMessage( WM_COMMAND, ID_MEDIA_REMOVE );
00435         
00436         m_wndTip.Hide();
00437         *pResult = 0;
00438 }
00439 
00440 void CMediaListCtrl::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult) 
00441 {
00442         NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
00443         *pResult = 0;
00444 
00445         m_wndTip.Hide();
00446 
00447         CPoint ptAction( pNMListView->ptAction );
00448 
00449         m_bCreateDragImage = TRUE;
00450         m_pDragImage = CLiveList::CreateDragImage( this, ptAction );
00451         m_bCreateDragImage = FALSE;
00452 
00453         if ( m_pDragImage == NULL ) return;
00454         m_nDragDrop = -1;
00455 
00456         UpdateWindow();
00457 
00458         CRect rcClient;
00459         GetClientRect( &rcClient );
00460         ClientToScreen( &rcClient );
00461         ClipCursor( &rcClient );
00462         SetCapture();
00463 
00464         SetFocus();
00465         UpdateWindow();
00466 
00467         m_pDragImage->DragEnter( this, ptAction );
00468 }
00469 
00470 void CMediaListCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
00471 {
00472         m_wndTip.Hide();
00473         CListCtrl::OnLButtonDown( nFlags, point );
00474 }
00475 
00476 void CMediaListCtrl::OnRButtonDown(UINT nFlags, CPoint point) 
00477 {
00478         m_wndTip.Hide();
00479         CListCtrl::OnRButtonDown( nFlags, point );
00480 }
00481 
00482 void CMediaListCtrl::OnMouseMove(UINT nFlags, CPoint point) 
00483 {
00484         int nHit = HitTest( point );
00485 
00486         if ( m_pDragImage != NULL )
00487         {
00488                 m_pDragImage->DragMove( point );
00489 
00490                 if ( nHit != m_nDragDrop )
00491                 {
00492                         CImageList::DragShowNolock( FALSE );
00493                         if ( m_nDragDrop >= 0 ) SetItemState( m_nDragDrop, 0, LVIS_DROPHILITED );
00494                         m_nDragDrop = nHit;
00495                         if ( m_nDragDrop >= 0 ) SetItemState( m_nDragDrop, LVIS_DROPHILITED, LVIS_DROPHILITED );
00496                         UpdateWindow();
00497                         CImageList::DragShowNolock( TRUE );
00498                 }
00499         }
00500         else
00501         {
00502                 DWORD nFile = nHit >= 0 ? GetItemData( nHit ) : 0;
00503 
00504                 if ( nFile > 0 && ! Library.LookupFile( nFile ) ) nFile = 0;
00505 
00506                 if ( nFile > 0 )
00507                 {
00508                         m_wndTip.Show( (LPVOID)nFile );
00509                 }
00510                 else
00511                 {
00512                         m_wndTip.Hide();
00513                 }
00514         }
00515 
00516         CListCtrl::OnMouseMove( nFlags, point );
00517 }
00518 
00519 void CMediaListCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
00520 {
00521         if ( m_pDragImage == NULL )
00522         {
00523                 CListCtrl::OnLButtonUp( nFlags, point );
00524                 return;
00525         }
00526 
00527         ClipCursor( NULL );
00528         ReleaseCapture();
00529 
00530         m_pDragImage->DragLeave( this );
00531         m_pDragImage->EndDrag();
00532         delete m_pDragImage;
00533         m_pDragImage = NULL;
00534 
00535         if ( m_nDragDrop >= 0 )
00536                 SetItemState( m_nDragDrop, 0, LVIS_DROPHILITED );
00537         else
00538                 m_nDragDrop = GetItemCount();
00539 
00540         CStringArray pPaths;
00541         CPtrArray pStates;
00542 
00543         while ( TRUE )
00544         {
00545                 int nItem = GetNextItem( -1, LVNI_SELECTED );
00546                 if ( nItem < 0 ) break;
00547 
00548                 pPaths.Add( GetItemText( nItem, 1 ) );
00549                 pStates.Add( (LPVOID)GetItemState( nItem, 0xFFFFFFFF ) );
00550 
00551                 DeleteItem( nItem );
00552 
00553                 if ( m_nDragDrop > nItem ) m_nDragDrop --;
00554         }
00555 
00556         for ( int nItem = 0 ; nItem < pPaths.GetSize() ; nItem++ )
00557         {
00558                 int nIndex = Add( pPaths.GetAt( nItem ), m_nDragDrop++ );
00559                 SetItemState( nIndex, (DWORD)pStates.GetAt( nItem ), 0xFFFFFFFF );
00560         }
00561 }
00562 
00564 // CMediaListCtrl command handlers
00565 
00566 void CMediaListCtrl::OnUpdateMediaSelect(CCmdUI* pCmdUI) 
00567 {
00568         pCmdUI->Enable( GetSelectedCount() == 1 );
00569 }
00570 
00571 void CMediaListCtrl::OnMediaSelect() 
00572 {
00573         int nItem = GetNextItem( -1, LVNI_SELECTED );
00574 
00575         if ( nItem >= 0 )
00576         {
00577                 SetCurrent( nItem );
00578                 SetFocus();
00579                 Invalidate();
00580         }
00581 }
00582 
00583 void CMediaListCtrl::OnMediaAdd() 
00584 {
00585         if ( ! AfxGetMainWnd()->IsWindowEnabled() ) return;
00586         
00587         CString strFilter;
00588         Skin.LoadString( strFilter, IDS_MEDIA_FILTER );
00589 
00590         CFileDialog dlg( TRUE, NULL, NULL,
00591                 OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING,
00592                 strFilter, this );
00593 
00594         TCHAR szFiles[81920] = { 0 };
00595         dlg.m_ofn.lpstrFile     = szFiles;
00596         dlg.m_ofn.nMaxFile      = 81920;
00597 
00598         if ( dlg.DoModal() != IDOK ) return;
00599         
00600         CString strFolder       = CString( szFiles );
00601         LPCTSTR pszFile         = szFiles + strFolder.GetLength() + 1;
00602 
00603         BOOL bWasEmpty = ( GetItemCount() == 0 );
00604 
00605         if ( *pszFile )
00606         {
00607                 for ( strFolder += '\\' ; *pszFile ; )
00608                 {
00609                         Enqueue( strFolder + pszFile, FALSE );
00610                         pszFile += _tcslen( pszFile ) + 1;
00611                 }
00612         }
00613         else
00614         {
00615                 Enqueue( strFolder, FALSE );
00616         }
00617 
00618         if ( GetItemCount() > 0 && bWasEmpty ) GetNext();
00619 }
00620 
00621 void CMediaListCtrl::OnMediaAddFolder() 
00622 {
00623         if ( ! AfxGetMainWnd()->IsWindowEnabled() ) return;
00624         
00625         TCHAR szPath[MAX_PATH];
00626         LPITEMIDLIST pPath;
00627         LPMALLOC pMalloc;
00628         BROWSEINFO pBI;
00629                 
00630         ZeroMemory( &pBI, sizeof(pBI) );
00631         pBI.hwndOwner           = AfxGetMainWnd()->GetSafeHwnd();
00632         pBI.pszDisplayName      = szPath;
00633         pBI.lpszTitle           = _T("Select folder to add to playlist:");
00634         pBI.ulFlags                     = BIF_RETURNONLYFSDIRS;
00635         
00636         pPath = SHBrowseForFolder( &pBI );
00637 
00638         if ( pPath == NULL ) return;
00639 
00640         SHGetPathFromIDList( pPath, szPath );
00641         SHGetMalloc( &pMalloc );
00642         pMalloc->Free( pPath );
00643         
00644         BOOL bWasEmpty = ( GetItemCount() == 0 );
00645         
00646         RecursiveEnqueue( szPath );
00647 
00648         if ( GetItemCount() > 0 && bWasEmpty ) GetNext();
00649 }
00650 
00651 void CMediaListCtrl::OnUpdateMediaRemove(CCmdUI* pCmdUI) 
00652 {
00653         pCmdUI->Enable( GetSelectedCount() > 0 );
00654 }
00655 
00656 void CMediaListCtrl::OnMediaRemove() 
00657 {
00658         for ( int nItem = GetItemCount() - 1 ; nItem >= 0 ; nItem-- )
00659         {
00660                 if ( GetItemState( nItem, LVIS_SELECTED ) ) Remove( nItem );
00661         }
00662 }
00663 
00664 void CMediaListCtrl::OnUpdateMediaClear(CCmdUI* pCmdUI) 
00665 {
00666         pCmdUI->Enable( GetItemCount() > 0 );
00667 }
00668 
00669 void CMediaListCtrl::OnMediaClear() 
00670 {
00671         Clear();
00672 }
00673 
00674 void CMediaListCtrl::OnMediaOpen() 
00675 {
00676         CString strFilter;
00677         Skin.LoadString( strFilter, IDS_MEDIA_FILTER );
00678         CFileDialog dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_ENABLESIZING, strFilter, this );
00679 
00680         if ( dlg.DoModal() != IDOK ) return;
00681 
00682         Open( dlg.GetPathName() );
00683 }
00684 
00685 void CMediaListCtrl::OnUpdateMediaSave(CCmdUI* pCmdUI) 
00686 {
00687         pCmdUI->Enable( GetItemCount() > 0 );
00688 }
00689 
00690 void CMediaListCtrl::OnMediaSave() 
00691 {
00692         CFileDialog dlg( FALSE, _T("m3u"), NULL, OFN_HIDEREADONLY|OFN_ENABLESIZING,
00693                 _T("Media Playlists|*.m3u|All Files|*.*||"), this );
00694 
00695         if ( dlg.DoModal() != IDOK ) return;
00696 
00697         CString strFile, strPath;
00698         CFile pFile;
00699 
00700         strPath = dlg.GetPathName();
00701         strPath = strPath.Left( strPath.ReverseFind( '\\' ) + 1 );
00702 
00703         for ( int nItem = 0 ; nItem < GetItemCount() ; nItem++ )
00704         {
00705                 CString strItem = GetItemText( nItem, 1 );
00706                 
00707                 if ( _tcsnicmp( strPath, strItem, strPath.GetLength() ) == 0 )
00708                         strItem = strItem.Mid( strPath.GetLength() );
00709 
00710                 strFile += strItem + _T("\r\n");
00711         }
00712 
00713         if ( ! pFile.Open( dlg.GetPathName(), CFile::modeWrite|CFile::modeCreate ) ) return;
00714 
00715         USES_CONVERSION;
00716         LPCSTR pszFile = T2CA( (LPCTSTR)strFile );
00717         
00718         pFile.Write( pszFile, strlen(pszFile) );
00719         pFile.Close();
00720 }
00721 
00722 void CMediaListCtrl::OnUpdateMediaPrevious(CCmdUI* pCmdUI) 
00723 {
00724         pCmdUI->Enable( GetCurrent() > 0 );
00725 }
00726 
00727 void CMediaListCtrl::OnMediaPrevious() 
00728 {
00729         int nCurrent = GetCurrent();
00730         if ( nCurrent > 0 ) SetCurrent( nCurrent - 1 );
00731 }
00732 
00733 void CMediaListCtrl::OnUpdateMediaNext(CCmdUI* pCmdUI) 
00734 {
00735         pCmdUI->Enable( GetItemCount() > 1 );
00736 }
00737 
00738 void CMediaListCtrl::OnMediaNext() 
00739 {
00740         GetNext();
00741 }
00742 
00743 void CMediaListCtrl::OnUpdateMediaRepeat(CCmdUI* pCmdUI) 
00744 {
00745         pCmdUI->SetCheck( Settings.MediaPlayer.Repeat );
00746 }
00747 
00748 void CMediaListCtrl::OnMediaRepeat() 
00749 {
00750         Settings.MediaPlayer.Repeat = ! Settings.MediaPlayer.Repeat;
00751 }
00752 
00753 void CMediaListCtrl::OnUpdateMediaRandom(CCmdUI* pCmdUI) 
00754 {
00755         pCmdUI->SetCheck( Settings.MediaPlayer.Random );
00756 }
00757 
00758 void CMediaListCtrl::OnMediaRandom() 
00759 {
00760         Settings.MediaPlayer.Random = ! Settings.MediaPlayer.Random;
00761 }

Generated on Thu Dec 15 10:39:37 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2