/* * $Id$ * Copyright © 2007-2020 gingko - https://www.gingko.ovh/ * * This file is part of Pouchin TV Mod, a free DVB-T viewer. * See https://www.pouchintv.fr/ for updates. * * Pouchin TV Mod is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Pouchin TV Mod is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "stdafx.h" #include "filedlg.h" #include // Pour "edt1" // ==================================================================================== // Probablement dajà défini dans un en-tête de l'application, mais le redéfinir ici // simplifie l'éventuelle réutilisation de ce module dans un projet dérivé. extern HINSTANCE hAppInstance; //!< Handle d'instance de l'application /// Retourner le nom de fichier spécifié avec une nouvelle extension tstring ChangeExtension(LPCTSTR pszPathName, LPCTSTR pszExtension) { TCHAR szResult[MAX_PATH] = TEXT(""); TCHAR szDir[MAX_PATH] = TEXT(""); TCHAR szName[MAX_PATH] = TEXT(""); TCHAR szExt[32] = TEXT(""); if (pszPathName && *pszPathName) { _tsplitpath_s(pszPathName, szResult, szDir, szName, szExt); if (pszExtension && *pszExtension) strcpy_T(szExt, pszExtension); strcat_T(szResult, szDir); strcat_T(szResult, szName); strcat_T(szResult, szExt); } return szResult; } // ==================================================================================== CFileNameDialogBase::CFileNameDialogBase() : hDlg(NULL), sOfn(OPENFILENAME()) { szFile[0] = 0; szFileTitle[0] = 0; sOfn.lStructSize = sizeof(sOfn); sOfn.hInstance = hAppInstance; sOfn.nFilterIndex = 1; sOfn.lpstrFile = szFile; sOfn.nMaxFile = _countof(szFile); sOfn.lpstrFileTitle = szFileTitle; sOfn.nMaxFileTitle = _countof(szFileTitle); sOfn.Flags = OFN_ENABLEHOOK|OFN_EXPLORER|OFN_ENABLESIZING; sOfn.lCustData = LPARAM(this); sOfn.lpfnHook = __OFNHookProc; } /// Fonction "OFNHookProc" statique /// Voir http://msdn.microsoft.com/en-us/library/ms646960%28VS.85%29.aspx UINT_PTR CALLBACK CFileNameDialogBase::__OFNHookProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { static LPCTSTR pszPropName = TEXT("HookData"); CFileNameDialogBase * pCl = reinterpret_cast(GetProp(hDlg, pszPropName)); if (!pCl && uMsg == WM_INITDIALOG) { pCl = reinterpret_cast(reinterpret_cast(lParam)->lCustData); if (pCl) { pCl->hDlg = GetParent(hDlg); SetProp(hDlg, pszPropName, pCl); } } if (pCl) return pCl->OFNHookProc(uMsg, wParam, lParam); return 0; } /// Fonction "OFNHookProc" non statique /// Voir http://msdn.microsoft.com/en-us/library/ms646960%28VS.85%29.aspx UINT_PTR CFileNameDialogBase::OFNHookProc(UINT uMsg, WPARAM wParam, LPARAM lParam) { //#ifdef _DEBUG // myprintf(tstr_printf(TEXT("%s") EOL, FormatMsg(hDlg, uMsg, wParam, lParam)).c_str()); //#endif return 0; } // ==================================================================================== tstring COpenFileNameDialog::Do(HWND hWndOwner, LPCTSTR pszTitle, LPCTSTR pszFilter, LPCTSTR pszInitial, vtstring * pMulti) { vtstring vStrRes; sOfn.hwndOwner = hWndOwner; sOfn.lpstrTitle = pszTitle; sOfn.lpstrFilter = pszFilter; if (pMulti) sOfn.Flags |= OFN_ALLOWMULTISELECT; if (pszInitial) strcpy_T(szFile, pszInitial); if (! GetOpenFileName(&sOfn)) return TEXT(""); LPCTSTR pszFile = szFile; INT nInx = 0; do { switch (nInx) { case 0: vStrRes.push_back(pszFile); break; case 1: vStrRes.front() += _T("\\") + tstring(pszFile); break; default: vStrRes.push_back(tstring(szFile) + _T("\\") + pszFile); } if ((sOfn.Flags & OFN_ALLOWMULTISELECT) == 0) break; pszFile += strlen_T(pszFile)+1; ++nInx; } while (*pszFile); if (pMulti) *pMulti = vStrRes; return vStrRes.front(); } // ==================================================================================== /// Obtenir l'extension du Nème filtre LPCTSTR CSaveFileNameDialog::GetNumFilter(int nNum) { LPCTSTR pszStr = sOfn.lpstrFilter; nNum *= 2; // 2 zéros terminateurs par filtre // Trouver la chaîne contenant l'extension for (int nInx = 1; nInx < nNum && *pszStr; ++ nInx) pszStr += strlen_T(pszStr)+1; if (*pszStr == TCHAR('*')) ++pszStr; // Sauter le "*" éventuel strcpy_T(szSelExtension, pszStr); _tcslwr_s(szSelExtension); return szSelExtension; } void CSaveFileNameDialog::ChangeSaveExtension(LPCTSTR pszExtension) { TCHAR szWrk[MAX_PATH] = TEXT(""); CommDlg_OpenSave_GetSpec(hDlg, szWrk, _countof(szWrk)); CommDlg_OpenSave_SetControlText(hDlg, edt1, ChangeExtension(szWrk, pszExtension).c_str()); if (*pszExtension == TCHAR('.')) ++pszExtension; // Sauter le "." éventuel CommDlg_OpenSave_SetDefExt(hDlg, pszExtension); } /// Fonction "OFNHookProc" non statique /// Voir http://msdn.microsoft.com/en-us/library/ms646960%28VS.85%29.aspx UINT_PTR CSaveFileNameDialog::OFNHookProc(UINT uMsg, WPARAM wParam, LPARAM lParam) { //#ifdef _DEBUG // myprintf(tstr_printf(TEXT("%s") EOL, FormatMsg(hDlg, uMsg, wParam, lParam)).c_str()); //#endif switch (uMsg) { case WM_NOTIFY: { const OFNOTIFY & sOfNtfy = * reinterpret_cast(lParam); switch (sOfNtfy.hdr.code) { case CDN_SELCHANGE: case CDN_TYPECHANGE: { ChangeSaveExtension(GetNumFilter(sOfNtfy.lpOFN->nFilterIndex)); break; } } break; } } return 0; } tstring CSaveFileNameDialog::Do(HWND hWndOwner, LPCTSTR pszTitle, LPCTSTR pszFilter, LPCTSTR pszInitial) { sOfn.hwndOwner = hWndOwner; sOfn.lpstrTitle = pszTitle; sOfn.lpstrFilter = pszFilter; sOfn.Flags |= OFN_OVERWRITEPROMPT; if (pszInitial) strcpy_T(szFile, pszInitial); if (GetSaveFileName(&sOfn)) return szFile; return TEXT(""); }