reapack

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

win32.cpp (3535B)


      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 "win32.hpp"
     19 
     20 #ifdef _WIN32
     21 #  define widen_cstr(cstr) widen(cstr).c_str()
     22 #else
     23 #  include <swell/swell.h>
     24 #  define widen_cstr(cstr) cstr
     25 #endif
     26 
     27 #include <vector>
     28 
     29 #ifdef _WIN32
     30 std::wstring Win32::widen(const char *input, const UINT codepage)
     31 {
     32   const int size = MultiByteToWideChar(codepage, 0, input, -1, nullptr, 0) - 1;
     33 
     34   std::wstring output(size, 0);
     35   MultiByteToWideChar(codepage, 0, input, -1, &output[0], size);
     36 
     37   return output;
     38 }
     39 
     40 std::string Win32::narrow(const wchar_t *input)
     41 {
     42   const int size = WideCharToMultiByte(CP_UTF8, 0,
     43     input, -1, nullptr, 0, nullptr, nullptr) - 1;
     44 
     45   std::string output(size, 0);
     46   WideCharToMultiByte(CP_UTF8, 0, input, -1, &output[0], size, nullptr, nullptr);
     47 
     48   return output;
     49 }
     50 #endif
     51 
     52 int Win32::messageBox(const HWND handle, const char *text,
     53   const char *title, const unsigned int buttons)
     54 {
     55   return MessageBox(handle, widen_cstr(text), widen_cstr(title), buttons);
     56 }
     57 
     58 std::string Win32::getWindowText(const HWND handle)
     59 {
     60   int buffer_len = GetWindowTextLength(handle);
     61   if(buffer_len)
     62     ++buffer_len; // null terminator
     63   else // REAPER <6.09 on non-Windows, before SWELL a34caf91
     64     buffer_len = 8192;
     65 
     66   std::vector<char_type> buffer(buffer_len, 0);
     67   GetWindowText(handle, buffer.data(), buffer_len);
     68 
     69   return narrow(buffer.data());
     70 }
     71 
     72 void Win32::setWindowText(const HWND handle, const char *text)
     73 {
     74   SetWindowText(handle, widen_cstr(text));
     75 }
     76 
     77 void Win32::shellExecute(const char *what, const char *arg)
     78 {
     79   ShellExecute(nullptr, L("open"), widen_cstr(what),
     80     arg ? widen_cstr(arg) : nullptr, nullptr, SW_SHOW);
     81 }
     82 
     83 HANDLE Win32::globalCopy(const std::string &text)
     84 {
     85   // calculate the size in bytes including the null terminator
     86   const size_t size = (text.size() + 1) * sizeof(char_type);
     87 
     88   HANDLE mem = GlobalAlloc(GMEM_MOVEABLE, size);
     89   memcpy(GlobalLock(mem), widen_cstr(text.c_str()), size);
     90   GlobalUnlock(mem);
     91 
     92   return mem;
     93 }
     94 
     95 bool Win32::writePrivateProfileString(const char *group, const char *key,
     96   const char *value, const char *path)
     97 {
     98   // value can be null for deleting a key
     99   return WritePrivateProfileString(widen_cstr(group), widen_cstr(key),
    100     value ? widen_cstr(value) : nullptr, widen_cstr(path));
    101 }
    102 
    103 std::string Win32::getPrivateProfileString(const char *group, const char *key,
    104   const char *fallback, const char *path)
    105 {
    106   char_type buffer[4096];
    107   GetPrivateProfileString(widen_cstr(group), widen_cstr(key), widen_cstr(fallback),
    108     buffer, static_cast<DWORD>(std::size(buffer)), widen_cstr(path));
    109 
    110   return narrow(buffer);
    111 }
    112 
    113 unsigned int Win32::getPrivateProfileInt(const char *group, const char *key,
    114   const unsigned int fallback, const char *path)
    115 {
    116   return GetPrivateProfileInt(widen_cstr(group), widen_cstr(key),
    117     fallback, widen_cstr(path));
    118 }