3131#include " FileDialog.h"
3232#include " Parameters.h"
3333
34+ #include < algorithm>
3435
3536FileDialog *FileDialog::staticThis = NULL ;
3637// int FileDialog::_dialogFileBoxId = (NppParameters::getInstance())->getWinVersion() < WV_W2K?edt1:cmb13;
@@ -171,6 +172,12 @@ TCHAR* FileDialog::doOpenSingleFileDlg()
171172
172173 _ofn.Flags |= OFN_FILEMUSTEXIST;
173174
175+ if (!params->useNewStyleSaveDlg ())
176+ {
177+ _ofn.Flags |= OFN_ENABLEHOOK | OFN_NOVALIDATE;
178+ _ofn.lpfnHook = OFNHookProc;
179+ }
180+
174181 TCHAR *fn = NULL ;
175182 try {
176183 fn = ::GetOpenFileName (&_ofn)?_fileName:NULL ;
@@ -200,7 +207,13 @@ stringVector * FileDialog::doOpenMultiFilesDlg()
200207 NppParameters * params = NppParameters::getInstance ();
201208 _ofn.lpstrInitialDir = params->getWorkingDir ();
202209
203- _ofn.Flags |= OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT;
210+ _ofn.Flags |= OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
211+
212+ if (!params->useNewStyleSaveDlg ())
213+ {
214+ _ofn.Flags |= OFN_ENABLEHOOK | OFN_NOVALIDATE;
215+ _ofn.lpfnHook = OFNHookProc;
216+ }
204217
205218 BOOL res = ::GetOpenFileName (&_ofn);
206219 if (params->getNppGUI ()._openSaveDir == dir_last)
@@ -256,7 +269,7 @@ TCHAR * FileDialog::doSaveDlg()
256269
257270 if (!params->useNewStyleSaveDlg ())
258271 {
259- _ofn.Flags |= OFN_ENABLEHOOK;
272+ _ofn.Flags |= OFN_ENABLEHOOK | OFN_NOVALIDATE ;
260273 _ofn.lpfnHook = OFNHookProc;
261274 }
262275
@@ -355,7 +368,6 @@ static generic_string addExt(HWND textCtrl, HWND typeCtrl) {
355368 return returnExt;
356369};
357370
358-
359371UINT_PTR CALLBACK FileDialog::OFNHookProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
360372{
361373 switch (uMsg)
@@ -374,7 +386,6 @@ UINT_PTR CALLBACK FileDialog::OFNHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, L
374386 HWND typeControl = ::GetDlgItem (hFileDlg, cmb1);
375387 ::SendMessage (typeControl, CB_SETCURSEL, index, 0 );
376388 }
377-
378389 // Don't touch the following 3 lines, they are cursed !!!
379390 oldProc = reinterpret_cast <WNDPROC>(::GetWindowLongPtr (hFileDlg, GWLP_WNDPROC));
380391 if (oldProc)
@@ -431,6 +442,34 @@ BOOL APIENTRY FileDialog::run(HWND hWnd, UINT uMsg, WPARAM, LPARAM lParam)
431442 int index = static_cast <int32_t >(::SendMessage (typeControl, CB_GETCURSEL, 0 , 0 ));
432443 NppParameters *pNppParam = NppParameters::getInstance ();
433444 pNppParam->setFileSaveDlgFilterIndex (index);
445+
446+ // change forward-slash to back-slash directory paths so dialog can interpret
447+ OPENFILENAME* ofn = reinterpret_cast <LPOFNOTIFY>(lParam)->lpOFN ;
448+ TCHAR* fileName = ofn->lpstrFile ;
449+
450+ // note: this check is essential, because otherwise we could return True
451+ // with a OFN_NOVALIDATE dialog, which leads to opening every file
452+ // in the specified directory. Multi-select terminator is \0\0.
453+ if ((ofn->Flags & OFN_ALLOWMULTISELECT) &&
454+ (*(fileName + lstrlen (fileName) + 1 ) != ' \0 ' ))
455+ return FALSE ;
456+
457+ if (::PathIsDirectory (fileName))
458+ {
459+ // change to backslash, and insert trailing '\' to indicate directory
460+ hFileDlg = ::GetParent (hWnd);
461+ std::wstring _fnStr (fileName);
462+ std::replace (_fnStr.begin (), _fnStr.end (), ' /' , ' \\ ' );
463+
464+ if (_fnStr.back () != ' \\ ' )
465+ _fnStr.insert (_fnStr.end (), ' \\ ' );
466+
467+ // change the dialog directory selection
468+ ::SendMessage (hFileDlg, CDM_SETCONTROLTEXT, edt1,
469+ reinterpret_cast <LPARAM>(_fnStr.c_str()));
470+ ::PostMessage (hFileDlg, WM_COMMAND, IDOK, 0 );
471+ ::SetWindowLongPtr (hWnd, 0 /* DWL_MSGRESULT*/ , 1 );
472+ }
434473 return TRUE ;
435474 }
436475
0 commit comments