2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 // OpenFileDlg.cpp : implementation file
28 #include "OpenFileDlg.h"
30 #define __DUMMY__ _T("*.*")
32 bool COpenFileDlg::m_fAllowDirSelection
= false;
33 WNDPROC
COpenFileDlg::m_wndProc
= NULL
;
37 IMPLEMENT_DYNAMIC(COpenFileDlg
, CFileDialog
)
38 COpenFileDlg::COpenFileDlg(CAtlArray
<CString
>& mask
, bool fAllowDirSelection
, LPCTSTR lpszDefExt
, LPCTSTR lpszFileName
,
39 DWORD dwFlags
, LPCTSTR lpszFilter
, CWnd
* pParentWnd
)
40 : CFileDialog(TRUE
, lpszDefExt
, lpszFileName
, dwFlags
|OFN_NOVALIDATE
, lpszFilter
, pParentWnd
, 0)
43 m_fAllowDirSelection
= fAllowDirSelection
;
44 m_pOFN
->lpstrInitialDir
= lpszFileName
;
46 m_buff
= new TCHAR
[10000];
48 m_pOFN
->lpstrFile
= m_buff
;
49 m_pOFN
->nMaxFile
= 10000;
52 COpenFileDlg::~COpenFileDlg()
57 BEGIN_MESSAGE_MAP(COpenFileDlg
, CFileDialog
)
62 // COpenFileDlg message handlers
64 LRESULT CALLBACK
COpenFileDlg::WindowProcNew(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
66 if(message
== WM_COMMAND
&& HIWORD(wParam
) == BN_CLICKED
&& LOWORD(wParam
) == IDOK
67 && m_fAllowDirSelection
)
69 CAutoVectorPtr
<TCHAR
> path
;
70 path
.Allocate(MAX_PATH
+1); // MAX_PATH should be bigger for multiple selection, but we are only interested if it's zero length
71 // 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
72 if(::GetDlgItemText(hwnd
, cmb13
, (TCHAR
*)path
, MAX_PATH
) == 0)
73 ::SendMessage(hwnd
, CDM_SETCONTROLTEXT
, edt1
, (LPARAM
)__DUMMY__
);
76 return CallWindowProc(COpenFileDlg::m_wndProc
, hwnd
, message
, wParam
, lParam
);
79 BOOL
COpenFileDlg::OnInitDialog()
81 CFileDialog::OnInitDialog();
83 m_wndProc
= (WNDPROC
)SetWindowLong(GetParent()->m_hWnd
, GWL_WNDPROC
, (LONG
)WindowProcNew
);
85 return TRUE
; // return TRUE unless you set the focus to a control
86 // EXCEPTION: OCX Property Pages should return FALSE
89 void COpenFileDlg::OnDestroy()
91 int i
= GetPathName().Find(__DUMMY__
);
92 if(i
>= 0) m_pOFN
->lpstrFile
[i
] = m_pOFN
->lpstrFile
[i
+1] = 0;
94 CFileDialog::OnDestroy();
97 BOOL
COpenFileDlg::OnNotify(WPARAM wParam
, LPARAM lParam
, LRESULT
* pResult
)
99 ASSERT(pResult
!= NULL
);
101 OFNOTIFY
* pNotify
= (OFNOTIFY
*)lParam
;
102 // allow message map to override
103 if (__super::OnNotify(wParam
, lParam
, pResult
))
105 ASSERT(pNotify
->hdr
.code
!= CDN_INCLUDEITEM
);
109 switch(pNotify
->hdr
.code
)
111 case CDN_INCLUDEITEM
:
112 if(OnIncludeItem((OFNOTIFYEX
*)lParam
, pResult
))
117 return FALSE
; // not handled
120 BOOL
COpenFileDlg::OnIncludeItem(OFNOTIFYEX
* pOFNEx
, LRESULT
* pResult
)
122 TCHAR buff
[MAX_PATH
];
123 if(!SHGetPathFromIDList((LPCITEMIDLIST
)pOFNEx
->pidl
, buff
))
126 HRESULT hr
= ((IShellFolder
*)pOFNEx
->psf
)->GetDisplayNameOf((LPCITEMIDLIST
)pOFNEx
->pidl
, SHGDN_NORMAL
|SHGDN_FORPARSING
, &s
);
127 if(S_OK
!= hr
) return FALSE
;
130 case STRRET_CSTR
: _tcscpy(buff
, CString(s
.cStr
)); break;
131 case STRRET_WSTR
: _tcscpy(buff
, CString(s
.pOleStr
)); CoTaskMemFree(s
.pOleStr
); break;
132 default: return FALSE
;
138 WIN32_FILE_ATTRIBUTE_DATA fad;
139 if(GetFileAttributesEx(fn, GetFileExInfoStandard, &fad)
140 && (fad.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
143 int i
= fn
.ReverseFind('.'), j
= fn
.ReverseFind('\\');
147 CString mask
= m_mask
[pOFNEx
->lpOFN
->nFilterIndex
-1] + _T(";");
148 CString ext
= fn
.Mid(i
).MakeLower() + _T(";");
150 *pResult
= mask
.Find(ext
) >= 0 || mask
.Find(_T("*.*")) >= 0;