OpenDlg.cpp

00001 /* 
00002  *      Copyright (C) 2003-2005 Gabest
00003  *      http://www.gabest.org
00004  *
00005  *  This Program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2, or (at your option)
00008  *  any later version.
00009  *   
00010  *  This Program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  *  GNU General Public License for more details.
00014  *   
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with GNU Make; see the file COPYING.  If not, write to
00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
00018  *  http://www.gnu.org/copyleft/gpl.html
00019  *
00020  */
00021 
00022 // OpenDlg.cpp : implementation file
00023 //
00024 
00025 #include "stdafx.h"
00026 #include "mplayerc.h"
00027 #include "OpenDlg.h"
00028 #include <atlbase.h>
00029 #include <shlobj.h>
00030 #include "..\..\DSUtil\DSUtil.h"
00031 
00032 // COpenDlg dialog
00033 
00034 //IMPLEMENT_DYNAMIC(COpenDlg, CResizableDialog)
00035 COpenDlg::COpenDlg(CWnd* pParent /*=NULL*/)
00036         : CResizableDialog(COpenDlg::IDD, pParent)
00037         , m_path(_T(""))
00038         , m_path2(_T(""))
00039         , m_fMultipleFiles(false)
00040 {
00041 }
00042 
00043 COpenDlg::~COpenDlg()
00044 {
00045 }
00046 
00047 void COpenDlg::DoDataExchange(CDataExchange* pDX)
00048 {
00049         __super::DoDataExchange(pDX);
00050         DDX_Control(pDX, IDC_COMBO1, m_mrucombo);
00051         DDX_CBString(pDX, IDC_COMBO1, m_path);
00052         DDX_Control(pDX, IDC_COMBO2, m_mrucombo2);
00053         DDX_CBString(pDX, IDC_COMBO2, m_path2);
00054         DDX_Control(pDX, IDC_STATIC1, m_label2);
00055         DDX_Control(pDX, IDC_BROWSEBUTTON2, m_openbtn2);
00056 }
00057 
00058 
00059 BEGIN_MESSAGE_MAP(COpenDlg, CResizableDialog)
00060         ON_BN_CLICKED(IDC_BROWSEBUTTON, OnBnClickedBrowsebutton)
00061         ON_BN_CLICKED(IDC_BROWSEBUTTON2, OnBnClickedBrowsebutton2)
00062         ON_BN_CLICKED(IDOK, OnBnClickedOk)
00063         ON_UPDATE_COMMAND_UI(IDC_STATIC1, OnUpdateDub)
00064         ON_UPDATE_COMMAND_UI(IDC_COMBO2, OnUpdateDub)
00065         ON_UPDATE_COMMAND_UI(IDC_BROWSEBUTTON2, OnUpdateDub)
00066 END_MESSAGE_MAP()
00067 
00068 
00069 // COpenDlg message handlers
00070 
00071 BOOL COpenDlg::OnInitDialog()
00072 {
00073         __super::OnInitDialog();
00074 
00075         CRecentFileList& MRU = AfxGetAppSettings().MRU;
00076         MRU.ReadList();
00077         m_mrucombo.ResetContent();
00078         for(int i = 0; i < MRU.GetSize(); i++)
00079                 if(!MRU[i].IsEmpty())
00080                         m_mrucombo.AddString(MRU[i]);
00081         CorrectComboListWidth(m_mrucombo, GetFont());
00082 
00083         CRecentFileList& MRUDub = AfxGetAppSettings().MRUDub;
00084         MRUDub.ReadList();
00085         m_mrucombo2.ResetContent();
00086         for(int i = 0; i < MRUDub.GetSize(); i++)
00087                 if(!MRUDub[i].IsEmpty())
00088                         m_mrucombo2.AddString(MRUDub[i]);
00089         CorrectComboListWidth(m_mrucombo2, GetFont());
00090 
00091         if(m_mrucombo.GetCount() > 0) m_mrucombo.SetCurSel(0);
00092 
00093         AddAnchor(m_mrucombo, TOP_LEFT, TOP_RIGHT);
00094         AddAnchor(m_mrucombo2, TOP_LEFT, TOP_RIGHT);
00095         AddAnchor(IDC_BROWSEBUTTON, TOP_RIGHT);
00096         AddAnchor(IDC_BROWSEBUTTON2, TOP_RIGHT);
00097         AddAnchor(IDOK, TOP_CENTER);
00098         AddAnchor(IDCANCEL, TOP_CENTER);
00099         AddAnchor(IDC_STATIC1, TOP_LEFT, TOP_RIGHT);
00100 
00101         CRect r;
00102         GetWindowRect(r);
00103         CSize s = r.Size();
00104         SetMinTrackSize(s);
00105         s.cx = 1000;
00106         SetMaxTrackSize(s);
00107 
00108         return TRUE;  // return TRUE unless you set the focus to a control
00109         // EXCEPTION: OCX Property Pages should return FALSE
00110 }
00111 
00112 static CString GetFileName(CString str)
00113 {
00114         CPath p = str;
00115         p.StripPath();
00116         return (LPCTSTR)p;
00117 }
00118 
00119 void COpenDlg::OnBnClickedBrowsebutton()
00120 {
00121         UpdateData();
00122 
00123         CMediaFormats& mf = AfxGetAppSettings().Formats;
00124 
00125         CString filter;
00126         CStringArray mask;
00127 
00128         filter += _T("Media files (all types)|__dummy|");
00129         mask.Add(_T(""));
00130 
00131         for(int i = 0; i < mf.GetCount(); i++) 
00132                 mask[0] += mf[i].GetFilter() + _T(";");
00133         mask[0].TrimRight(_T(";"));
00134 
00135         for(int i = 0; i < mf.GetCount(); i++)
00136         {
00137                 CMediaFormatCategory& mfc = mf[i];
00138                 filter += mfc.GetLabel() + _T("|__dummy|");
00139                 mask.Add(mfc.GetFilter());
00140         }
00141 
00142         filter += _T("All files (*.*)|__dummy|");
00143         mask.Add(_T("*.*"));
00144 
00145         filter += _T("|");
00146 
00147         COpenFileDialog fd(mask, true, NULL, m_path, 
00148                 OFN_EXPLORER|OFN_ENABLESIZING|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_ENABLEINCLUDENOTIFY, 
00149                 filter, this);
00150 
00151         CAutoVectorPtr<TCHAR> buff;
00152         buff.Allocate(10000);
00153         buff[0] = 0;
00154         fd.m_pOFN->lpstrFile = buff;
00155         fd.m_pOFN->nMaxFile = 10000;
00156 
00157         if(fd.DoModal() != IDOK) return;
00158 
00159         m_fns.RemoveAll();
00160 
00161         POSITION pos = fd.GetStartPosition();
00162         while(pos)
00163         {
00164 /*
00165                 CString str = fd.GetNextPathName(pos);
00166                 POSITION insertpos = m_fns.GetTailPosition();
00167                 while(insertpos && GetFileName(str).CompareNoCase(GetFileName(m_fns.GetAt(insertpos))) <= 0)
00168                         m_fns.GetPrev(insertpos);
00169                 if(!insertpos) m_fns.AddHead(str);
00170                 else m_fns.InsertAfter(insertpos, str);
00171 */
00172                 m_fns.AddTail(fd.GetNextPathName(pos));
00173         }
00174 
00175         if(m_fns.GetCount() > 1 
00176         || m_fns.GetCount() == 1 
00177                 && (m_fns.GetHead()[m_fns.GetHead().GetLength()-1] == '\\'
00178                 || m_fns.GetHead()[m_fns.GetHead().GetLength()-1] == '*'))
00179         {
00180                 m_fMultipleFiles = true;
00181                 EndDialog(IDOK);
00182                 return;
00183         }
00184 
00185         m_mrucombo.SetWindowText(fd.GetPathName());
00186 }
00187 
00188 void COpenDlg::OnBnClickedBrowsebutton2()
00189 {
00190         UpdateData();
00191 
00192         CMediaFormats& mf = AfxGetAppSettings().Formats;
00193 
00194         CString filter;
00195         CStringArray mask;
00196 
00197         filter += _T("Audio files (all types)|__dummy|");
00198         mask.Add(_T(""));
00199 
00200         for(int i = 0; i < mf.GetCount(); i++)
00201         {
00202                 CMediaFormatCategory& mfc = mf[i];
00203                 if(!mfc.IsAudioOnly() || mfc.GetEngineType() != DirectShow) continue;
00204                 mask[0] += mf[i].GetFilter() + _T(";");
00205         }
00206         mask[0].TrimRight(_T(";"));
00207 
00208         for(int i = 0; i < mf.GetCount(); i++)
00209         {
00210                 CMediaFormatCategory& mfc = mf[i];
00211                 if(!mfc.IsAudioOnly() || mfc.GetEngineType() != DirectShow) continue;
00212                 filter += mfc.GetLabel() + _T("|__dummy|");
00213                 mask.Add(mfc.GetFilter());
00214         }
00215 
00216         filter += _T("All files (*.*)|__dummy|");
00217         mask.Add(_T("*.*"));
00218 
00219         filter += _T("|");
00220 
00221         COpenFileDialog fd(mask, false, NULL, m_path2, 
00222                 OFN_EXPLORER|OFN_ENABLESIZING|OFN_HIDEREADONLY|OFN_ENABLEINCLUDENOTIFY, 
00223                 filter, this);
00224 
00225         if(fd.DoModal() != IDOK) return;
00226 
00227         m_mrucombo2.SetWindowText(fd.GetPathName());
00228 }
00229 
00230 void COpenDlg::OnBnClickedOk()
00231 {
00232         UpdateData();
00233 
00234         m_fns.RemoveAll();
00235         m_fns.AddTail(m_path);
00236         if(m_mrucombo2.IsWindowEnabled())
00237                 m_fns.AddTail(m_path2);
00238 
00239         m_fMultipleFiles = false;
00240 
00241         OnOK();
00242 }
00243 
00244 void COpenDlg::OnUpdateDub(CCmdUI* pCmdUI)
00245 {
00246         m_mrucombo.GetWindowText(m_path);
00247         pCmdUI->Enable(AfxGetAppSettings().Formats.GetEngine(m_path) == DirectShow);
00248 }
00249 
00250 // OpenDlg.cpp : implementation file
00251 //
00252 
00253 #include "OpenDlg.h"
00254 #include <dlgs.h>
00255 
00256 #define __DUMMY__ _T("*.*")
00257 
00258 bool COpenFileDialog::m_fAllowDirSelection = false;
00259 WNDPROC COpenFileDialog::m_wndProc = NULL;
00260 
00261 // COpenFileDialog
00262 
00263 IMPLEMENT_DYNAMIC(COpenFileDialog, CFileDialog)
00264 COpenFileDialog::COpenFileDialog(CStringArray& mask, bool fAllowDirSelection, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
00265                 DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd)
00266         : CFileDialog(TRUE, lpszDefExt, lpszFileName, dwFlags|OFN_NOVALIDATE, lpszFilter, pParentWnd, 0)
00267         , m_mask(mask)
00268 {
00269         m_fAllowDirSelection = fAllowDirSelection;
00270         m_pOFN->lpstrInitialDir = lpszFileName;
00271 }
00272 
00273 COpenFileDialog::~COpenFileDialog()
00274 {
00275 }
00276 
00277 BEGIN_MESSAGE_MAP(COpenFileDialog, CFileDialog)
00278         ON_WM_DESTROY()
00279 END_MESSAGE_MAP()
00280 
00281 
00282 // COpenFileDialog message handlers
00283 
00284 LRESULT CALLBACK COpenFileDialog::WindowProcNew(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00285 {
00286         if(message ==  WM_COMMAND && HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDOK
00287         && m_fAllowDirSelection)
00288         {
00289                 CAutoVectorPtr<TCHAR> path;
00290                 path.Allocate(MAX_PATH+1); // MAX_PATH should be bigger for multiple selection, but we are only interested if it's zero length
00291                 // note: allocating MAX_PATH only will cause a buffer overrun for too long strings, and will result in a silent app disappearing crash, 100% reproducable
00292                 if(::GetDlgItemText(hwnd, cmb13, (TCHAR*)path, MAX_PATH) == 0)
00293                         ::SendMessage(hwnd, CDM_SETCONTROLTEXT, edt1, (LPARAM)__DUMMY__);
00294         }
00295 
00296         return CallWindowProc(COpenFileDialog::m_wndProc, hwnd, message, wParam, lParam);
00297 }
00298 
00299 BOOL COpenFileDialog::OnInitDialog()
00300 {
00301         CFileDialog::OnInitDialog();
00302 
00303         m_wndProc = (WNDPROC)SetWindowLong(GetParent()->m_hWnd, GWL_WNDPROC, (LONG)WindowProcNew);
00304 
00305         return TRUE;  // return TRUE unless you set the focus to a control
00306         // EXCEPTION: OCX Property Pages should return FALSE
00307 }
00308 
00309 void COpenFileDialog::OnDestroy()
00310 {
00311         int i = GetPathName().Find(__DUMMY__);
00312         if(i >= 0) m_pOFN->lpstrFile[i] = m_pOFN->lpstrFile[i+1] = 0;
00313 
00314         CFileDialog::OnDestroy();
00315 }
00316 
00317 BOOL COpenFileDialog::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
00318 {
00319         ASSERT(pResult != NULL);
00320 
00321         OFNOTIFY* pNotify = (OFNOTIFY*)lParam;
00322         // allow message map to override
00323         if (__super::OnNotify(wParam, lParam, pResult))
00324         {
00325                 ASSERT(pNotify->hdr.code != CDN_INCLUDEITEM);
00326                 return TRUE;
00327         }
00328 
00329         switch(pNotify->hdr.code)
00330         {
00331         case CDN_INCLUDEITEM:
00332                 if(OnIncludeItem((OFNOTIFYEX*)lParam, pResult))
00333                         return TRUE;
00334                 break;
00335         }
00336 
00337         return FALSE;   // not handled
00338 }
00339 
00340 BOOL COpenFileDialog::OnIncludeItem(OFNOTIFYEX* pOFNEx, LRESULT* pResult)
00341 {
00342         TCHAR buff[MAX_PATH];
00343         if(!SHGetPathFromIDList((LPCITEMIDLIST)pOFNEx->pidl, buff)) 
00344         {
00345                 STRRET s;
00346                 HRESULT hr = ((IShellFolder*)pOFNEx->psf)->GetDisplayNameOf((LPCITEMIDLIST)pOFNEx->pidl, SHGDN_NORMAL|SHGDN_FORPARSING, &s);
00347                 if(S_OK != hr) return FALSE;
00348                 switch(s.uType)
00349                 {
00350                 case STRRET_CSTR: _tcscpy(buff, CString(s.cStr)); break;
00351                 case STRRET_WSTR: _tcscpy(buff, CString(s.pOleStr)); CoTaskMemFree(s.pOleStr); break;
00352                 default: return FALSE;
00353                 }
00354         }
00355 
00356         CString fn(buff);
00357 /*
00358         WIN32_FILE_ATTRIBUTE_DATA fad;
00359         if(GetFileAttributesEx(fn, GetFileExInfoStandard, &fad)
00360         && (fad.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
00361                 return FALSE;
00362 */
00363         int i = fn.ReverseFind('.'), j = fn.ReverseFind('\\');
00364         if(i < 0 || i < j) 
00365                 return FALSE;
00366 
00367         CString mask = m_mask[pOFNEx->lpOFN->nFilterIndex-1] + _T(";");
00368         CString ext = fn.Mid(i).MakeLower() + _T(";");
00369 
00370         *pResult = mask.Find(ext) >= 0 || mask.Find(_T("*.*")) >= 0;
00371 
00372         return TRUE;
00373 }

Generated on Tue Dec 13 14:46:57 2005 for guliverkli by  doxygen 1.4.5