reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

filedialog.cpp (2651B)


      1 /* ReaPack: Package manager for REAPER
      2  * Copyright (C) 2015-2025  Christian Fillion
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU Lesser General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU Lesser General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Lesser General Public License
     15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #include "filedialog.hpp"
     19 
     20 #include "path.hpp"
     21 #include "win32.hpp"
     22 
     23 #ifndef _WIN32
     24 #  include <swell/swell.h>
     25 #endif
     26 
     27 #ifdef _WIN32
     28 static std::string getFileName(BOOL(__stdcall *func)(LPOPENFILENAME),
     29   HWND parent, HINSTANCE instance, const char *title, const Path &initialDir,
     30   const Win32::char_type *filters, const Win32::char_type *defaultExt)
     31 {
     32   const auto &&wideTitle = Win32::widen(title);
     33   const auto &&wideInitialDir = Win32::widen(initialDir.join());
     34 
     35   wchar_t pathBuffer[4096] = {};
     36 
     37   OPENFILENAME of{sizeof(of), parent, instance};
     38   of.lpstrFilter = filters;
     39   of.lpstrFile = pathBuffer;
     40   of.nMaxFile = static_cast<DWORD>(std::size(pathBuffer));
     41   of.lpstrInitialDir = wideInitialDir.c_str();
     42   of.lpstrTitle = wideTitle.c_str();
     43   of.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;
     44   of.lpstrDefExt = defaultExt;
     45 
     46   return func(&of) ? Win32::narrow(pathBuffer) : std::string{};
     47 }
     48 #endif
     49 
     50 std::string FileDialog::getOpenFileName(HWND parent, HINSTANCE instance,
     51   const char *title, const Path &initialDir,
     52   const Win32::char_type *filters, const Win32::char_type *defaultExt)
     53 {
     54 #ifdef _WIN32
     55   return getFileName(&GetOpenFileName, parent, instance,
     56     title, initialDir, filters, defaultExt);
     57 #else
     58   const char *path = BrowseForFiles(title, initialDir.join().c_str(),
     59     nullptr, false, filters);
     60   return path ? path : std::string{};
     61 #endif
     62 }
     63 
     64 std::string FileDialog::getSaveFileName(HWND parent, HINSTANCE instance,
     65   const char *title, const Path &initialDir,
     66   const Win32::char_type *filters, const Win32::char_type *defaultExt)
     67 {
     68 #ifdef _WIN32
     69   return getFileName(&GetSaveFileName, parent, instance,
     70     title, initialDir, filters, defaultExt);
     71 #else
     72   char path[4096]{};
     73 
     74   if(BrowseForSaveFile(title, initialDir.join().c_str(),
     75       nullptr, filters, path, sizeof(path)))
     76     return path;
     77   else
     78     return {};
     79 #endif
     80 }