1
0
Fork 0

Codechange: Switch to explicit wide strings

pull/8732/head
Niels Martin Hansen 2021-02-21 20:48:21 +01:00
parent beeb9e0a1b
commit b427ddce88
13 changed files with 134 additions and 145 deletions

View File

@ -134,7 +134,7 @@ static void debug_print(const char *dbg, const char *buf)
char buffer[512]; char buffer[512];
seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf); seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
#if defined(_WIN32) #if defined(_WIN32)
TCHAR system_buf[512]; wchar_t system_buf[512];
convert_to_fs(buffer, system_buf, lengthof(system_buf), true); convert_to_fs(buffer, system_buf, lengthof(system_buf), true);
_fputts(system_buf, stderr); _fputts(system_buf, stderr);
#else #else

View File

@ -107,14 +107,14 @@ DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
struct DIR; struct DIR;
struct dirent { // XXX - only d_name implemented struct dirent { // XXX - only d_name implemented
TCHAR *d_name; // name of found file wchar_t *d_name; // name of found file
/* little hack which will point to parent DIR struct which will /* little hack which will point to parent DIR struct which will
* save us a call to GetFileAttributes if we want information * save us a call to GetFileAttributes if we want information
* about the file (for example in function fio_bla) */ * about the file (for example in function fio_bla) */
DIR *dir; DIR *dir;
}; };
DIR *opendir(const TCHAR *path); DIR *opendir(const wchar_t *path);
struct dirent *readdir(DIR *d); struct dirent *readdir(DIR *d);
int closedir(DIR *d); int closedir(DIR *d);
#else #else

View File

@ -92,17 +92,15 @@ bool IniFile::SaveToDisk(const std::string &filename)
#endif #endif
#if defined(_WIN32) #if defined(_WIN32)
/* _tcsncpy = strcpy is TCHAR is char, but isn't when TCHAR is wchar. */
# undef strncpy
/* Allocate space for one more \0 character. */ /* Allocate space for one more \0 character. */
TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1]; wchar_t tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
_tcsncpy(tfilename, OTTD2FS(filename.c_str()), MAX_PATH); wcsncpy(tfilename, OTTD2FS(filename.c_str()), MAX_PATH);
_tcsncpy(tfile_new, OTTD2FS(file_new.c_str()), MAX_PATH); wcsncpy(tfile_new, OTTD2FS(file_new.c_str()), MAX_PATH);
/* SHFileOperation wants a double '\0' terminated string. */ /* SHFileOperation wants a double '\0' terminated string. */
tfilename[MAX_PATH - 1] = '\0'; tfilename[MAX_PATH - 1] = '\0';
tfile_new[MAX_PATH - 1] = '\0'; tfile_new[MAX_PATH - 1] = '\0';
tfilename[_tcslen(tfilename) + 1] = '\0'; tfilename[wcslen(tfilename) + 1] = '\0';
tfile_new[_tcslen(tfile_new) + 1] = '\0'; tfile_new[wcslen(tfile_new) + 1] = '\0';
/* Rename file without any user confirmation. */ /* Rename file without any user confirmation. */
SHFILEOPSTRUCT shfopt; SHFILEOPSTRUCT shfopt;

View File

@ -81,7 +81,7 @@ struct DLSFile {
std::vector<DLSWave> waves; std::vector<DLSWave> waves;
/** Try loading a DLS file into memory. */ /** Try loading a DLS file into memory. */
bool LoadFile(const TCHAR *file); bool LoadFile(const wchar_t *file);
private: private:
/** Load an articulation structure from a DLS file. */ /** Load an articulation structure from a DLS file. */
@ -428,11 +428,11 @@ bool DLSFile::ReadDLSWaveList(FILE *f, DWORD list_length)
return true; return true;
} }
bool DLSFile::LoadFile(const TCHAR *file) bool DLSFile::LoadFile(const wchar_t *file)
{ {
DEBUG(driver, 2, "DMusic: Try to load DLS file %s", FS2OTTD(file)); DEBUG(driver, 2, "DMusic: Try to load DLS file %s", FS2OTTD(file));
FILE *f = _tfopen(file, _T("rb")); FILE *f = _wfopen(file, L"rb");
if (f == nullptr) return false; if (f == nullptr) return false;
FileCloser f_scope(f); FileCloser f_scope(f);
@ -861,11 +861,11 @@ static const char *LoadDefaultDLSFile(const char *user_dls)
if (user_dls == nullptr) { if (user_dls == nullptr) {
/* Try loading the default GM DLS file stored in the registry. */ /* Try loading the default GM DLS file stored in the registry. */
HKEY hkDM; HKEY hkDM;
if (SUCCEEDED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\DirectMusic"), 0, KEY_READ, &hkDM))) { if (SUCCEEDED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\DirectMusic", 0, KEY_READ, &hkDM))) {
TCHAR dls_path[MAX_PATH]; wchar_t dls_path[MAX_PATH];
DWORD buf_size = sizeof(dls_path); // Buffer size as to be given in bytes! DWORD buf_size = sizeof(dls_path); // Buffer size as to be given in bytes!
if (SUCCEEDED(RegQueryValueEx(hkDM, _T("GMFilePath"), nullptr, nullptr, (LPBYTE)dls_path, &buf_size))) { if (SUCCEEDED(RegQueryValueEx(hkDM, L"GMFilePath", nullptr, nullptr, (LPBYTE)dls_path, &buf_size))) {
TCHAR expand_path[MAX_PATH * 2]; wchar_t expand_path[MAX_PATH * 2];
ExpandEnvironmentStrings(dls_path, expand_path, lengthof(expand_path)); ExpandEnvironmentStrings(dls_path, expand_path, lengthof(expand_path));
if (!dls_file.LoadFile(expand_path)) DEBUG(driver, 1, "Failed to load default GM DLS file from registry"); if (!dls_file.LoadFile(expand_path)) DEBUG(driver, 1, "Failed to load default GM DLS file from registry");
} }
@ -874,8 +874,8 @@ static const char *LoadDefaultDLSFile(const char *user_dls)
/* If we couldn't load the file from the registry, try again at the default install path of the GM DLS file. */ /* If we couldn't load the file from the registry, try again at the default install path of the GM DLS file. */
if (dls_file.instruments.size() == 0) { if (dls_file.instruments.size() == 0) {
static const TCHAR *DLS_GM_FILE = _T("%windir%\\System32\\drivers\\gm.dls"); static const wchar_t *DLS_GM_FILE = L"%windir%\\System32\\drivers\\gm.dls";
TCHAR path[MAX_PATH]; wchar_t path[MAX_PATH];
ExpandEnvironmentStrings(DLS_GM_FILE, path, lengthof(path)); ExpandEnvironmentStrings(DLS_GM_FILE, path, lengthof(path));
if (!dls_file.LoadFile(path)) return "Can't load GM DLS collection"; if (!dls_file.LoadFile(path)) return "Can't load GM DLS collection";

View File

@ -150,7 +150,7 @@ static uint32 CalcCRC(byte *data, uint size, uint32 crc)
return crc; return crc;
} }
static void GetFileInfo(DebugFileInfo *dfi, const TCHAR *filename) static void GetFileInfo(DebugFileInfo *dfi, const wchar_t *filename)
{ {
HANDLE file; HANDLE file;
memset(dfi, 0, sizeof(*dfi)); memset(dfi, 0, sizeof(*dfi));
@ -183,7 +183,7 @@ static void GetFileInfo(DebugFileInfo *dfi, const TCHAR *filename)
static char *PrintModuleInfo(char *output, const char *last, HMODULE mod) static char *PrintModuleInfo(char *output, const char *last, HMODULE mod)
{ {
TCHAR buffer[MAX_PATH]; wchar_t buffer[MAX_PATH];
DebugFileInfo dfi; DebugFileInfo dfi;
GetModuleFileName(mod, buffer, MAX_PATH); GetModuleFileName(mod, buffer, MAX_PATH);
@ -491,7 +491,7 @@ char *CrashLogWindows::AppendDecodedStacktrace(char *buffer, const char *last) c
/* virtual */ int CrashLogWindows::WriteCrashDump(char *filename, const char *filename_last) const /* virtual */ int CrashLogWindows::WriteCrashDump(char *filename, const char *filename_last) const
{ {
int ret = 0; int ret = 0;
HMODULE dbghelp = LoadLibrary(_T("dbghelp.dll")); HMODULE dbghelp = LoadLibrary(L"dbghelp.dll");
if (dbghelp != nullptr) { if (dbghelp != nullptr) {
typedef BOOL (WINAPI *MiniDumpWriteDump_t)(HANDLE, DWORD, HANDLE, typedef BOOL (WINAPI *MiniDumpWriteDump_t)(HANDLE, DWORD, HANDLE,
MINIDUMP_TYPE, MINIDUMP_TYPE,
@ -553,19 +553,19 @@ static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep)
} }
if (GamelogTestEmergency()) { if (GamelogTestEmergency()) {
static const TCHAR _emergency_crash[] = static const wchar_t _emergency_crash[] =
_T("A serious fault condition occurred in the game. The game will shut down.\n") L"A serious fault condition occurred in the game. The game will shut down.\n"
_T("As you loaded an emergency savegame no crash information will be generated.\n"); L"As you loaded an emergency savegame no crash information will be generated.\n";
MessageBox(nullptr, _emergency_crash, _T("Fatal Application Failure"), MB_ICONERROR); MessageBox(nullptr, _emergency_crash, L"Fatal Application Failure", MB_ICONERROR);
ExitProcess(3); ExitProcess(3);
} }
if (SaveloadCrashWithMissingNewGRFs()) { if (SaveloadCrashWithMissingNewGRFs()) {
static const TCHAR _saveload_crash[] = static const wchar_t _saveload_crash[] =
_T("A serious fault condition occurred in the game. The game will shut down.\n") L"A serious fault condition occurred in the game. The game will shut down.\n"
_T("As you loaded an savegame for which you do not have the required NewGRFs\n") L"As you loaded an savegame for which you do not have the required NewGRFs\n"
_T("no crash information will be generated.\n"); L"no crash information will be generated.\n";
MessageBox(nullptr, _saveload_crash, _T("Fatal Application Failure"), MB_ICONERROR); MessageBox(nullptr, _saveload_crash, L"Fatal Application Failure", MB_ICONERROR);
ExitProcess(3); ExitProcess(3);
} }
@ -641,20 +641,20 @@ static void CDECL CustomAbort(int signal)
static bool _expanded; static bool _expanded;
static const TCHAR _crash_desc[] = static const wchar_t _crash_desc[] =
_T("A serious fault condition occurred in the game. The game will shut down.\n") L"A serious fault condition occurred in the game. The game will shut down.\n"
_T("Please send the crash information and the crash.dmp file (if any) to the developers.\n") L"Please send the crash information and the crash.dmp file (if any) to the developers.\n"
_T("This will greatly help debugging. The correct place to do this is https://github.com/OpenTTD/OpenTTD/issues. ") L"This will greatly help debugging. The correct place to do this is https://github.com/OpenTTD/OpenTTD/issues. "
_T("The information contained in the report is displayed below.\n") L"The information contained in the report is displayed below.\n"
_T("Press \"Emergency save\" to attempt saving the game. Generated file(s):\n") L"Press \"Emergency save\" to attempt saving the game. Generated file(s):\n"
_T("%s"); L"%s";
static const TCHAR _save_succeeded[] = static const wchar_t _save_succeeded[] =
_T("Emergency save succeeded.\nIts location is '%s'.\n") L"Emergency save succeeded.\nIts location is '%s'.\n"
_T("Be aware that critical parts of the internal game state may have become ") L"Be aware that critical parts of the internal game state may have become "
_T("corrupted. The saved game is not guaranteed to work."); L"corrupted. The saved game is not guaranteed to work.";
static const TCHAR * const _expand_texts[] = {_T("S&how report >>"), _T("&Hide report <<") }; static const wchar_t * const _expand_texts[] = {L"S&how report >>", L"&Hide report <<" };
static void SetWndSize(HWND wnd, int mode) static void SetWndSize(HWND wnd, int mode)
{ {
@ -677,17 +677,13 @@ static void SetWndSize(HWND wnd, int mode)
} }
} }
/* When TCHAR is char, then _sntprintf becomes snprintf. When TCHAR is wchar it doesn't. Likewise for strcat. */
#undef snprintf
#undef strcat
static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
switch (msg) { switch (msg) {
case WM_INITDIALOG: { case WM_INITDIALOG: {
/* We need to put the crash-log in a separate buffer because the default /* We need to put the crash-log in a separate buffer because the default
* buffer in MB_TO_WIDE is not large enough (512 chars) */ * buffer in OTTD2FS is not large enough (512 chars) */
TCHAR crash_msgW[lengthof(CrashLogWindows::current->crashlog)]; wchar_t crash_msgW[lengthof(CrashLogWindows::current->crashlog)];
/* Convert unix -> dos newlines because the edit box only supports that properly :( */ /* Convert unix -> dos newlines because the edit box only supports that properly :( */
const char *unix_nl = CrashLogWindows::current->crashlog; const char *unix_nl = CrashLogWindows::current->crashlog;
char dos_nl[lengthof(CrashLogWindows::current->crashlog)]; char dos_nl[lengthof(CrashLogWindows::current->crashlog)];
@ -700,20 +696,20 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARA
*p = '\0'; *p = '\0';
/* Add path to crash.log and crash.dmp (if any) to the crash window text */ /* Add path to crash.log and crash.dmp (if any) to the crash window text */
size_t len = _tcslen(_crash_desc) + 2; size_t len = wcslen(_crash_desc) + 2;
len += _tcslen(OTTD2FS(CrashLogWindows::current->crashlog_filename)) + 2; len += wcslen(OTTD2FS(CrashLogWindows::current->crashlog_filename)) + 2;
len += _tcslen(OTTD2FS(CrashLogWindows::current->crashdump_filename)) + 2; len += wcslen(OTTD2FS(CrashLogWindows::current->crashdump_filename)) + 2;
len += _tcslen(OTTD2FS(CrashLogWindows::current->screenshot_filename)) + 1; len += wcslen(OTTD2FS(CrashLogWindows::current->screenshot_filename)) + 1;
TCHAR *text = AllocaM(TCHAR, len); wchar_t *text = AllocaM(wchar_t, len);
_sntprintf(text, len, _crash_desc, OTTD2FS(CrashLogWindows::current->crashlog_filename)); _snwprintf(text, len, _crash_desc, OTTD2FS(CrashLogWindows::current->crashlog_filename));
if (OTTD2FS(CrashLogWindows::current->crashdump_filename)[0] != _T('\0')) { if (OTTD2FS(CrashLogWindows::current->crashdump_filename)[0] != L'\0') {
_tcscat(text, _T("\n")); wcscat(text, L"\n");
_tcscat(text, OTTD2FS(CrashLogWindows::current->crashdump_filename)); wcscat(text, OTTD2FS(CrashLogWindows::current->crashdump_filename));
} }
if (OTTD2FS(CrashLogWindows::current->screenshot_filename)[0] != _T('\0')) { if (OTTD2FS(CrashLogWindows::current->screenshot_filename)[0] != L'\0') {
_tcscat(text, _T("\n")); wcscat(text, L"\n");
_tcscat(text, OTTD2FS(CrashLogWindows::current->screenshot_filename)); wcscat(text, OTTD2FS(CrashLogWindows::current->screenshot_filename));
} }
SetDlgItemText(wnd, 10, text); SetDlgItemText(wnd, 10, text);
@ -729,12 +725,12 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARA
case 13: // Emergency save case 13: // Emergency save
char filename[MAX_PATH]; char filename[MAX_PATH];
if (CrashLogWindows::current->WriteSavegame(filename, lastof(filename))) { if (CrashLogWindows::current->WriteSavegame(filename, lastof(filename))) {
size_t len = _tcslen(_save_succeeded) + _tcslen(OTTD2FS(filename)) + 1; size_t len = wcslen(_save_succeeded) + wcslen(OTTD2FS(filename)) + 1;
TCHAR *text = AllocaM(TCHAR, len); wchar_t *text = AllocaM(wchar_t, len);
_sntprintf(text, len, _save_succeeded, OTTD2FS(filename)); _snwprintf(text, len, _save_succeeded, OTTD2FS(filename));
MessageBox(wnd, text, _T("Save successful"), MB_ICONINFORMATION); MessageBox(wnd, text, L"Save successful", MB_ICONINFORMATION);
} else { } else {
MessageBox(wnd, _T("Save failed"), _T("Save failed"), MB_ICONINFORMATION); MessageBox(wnd, L"Save failed", L"Save failed", MB_ICONINFORMATION);
} }
break; break;
case 15: // Expand window to show crash-message case 15: // Expand window to show crash-message

View File

@ -46,10 +46,10 @@ extern FT_Library _library;
* @param long_path the path in system encoding. * @param long_path the path in system encoding.
* @return the short path in ANSI (ASCII). * @return the short path in ANSI (ASCII).
*/ */
static const char *GetShortPath(const TCHAR *long_path) static const char *GetShortPath(const wchar_t *long_path)
{ {
static char short_path[MAX_PATH]; static char short_path[MAX_PATH];
WCHAR short_path_w[MAX_PATH]; wchar_t short_path_w[MAX_PATH];
GetShortPathName(long_path, short_path_w, lengthof(short_path_w)); GetShortPathName(long_path, short_path_w, lengthof(short_path_w));
WideCharToMultiByte(CP_ACP, 0, short_path_w, -1, short_path, lengthof(short_path), nullptr, nullptr); WideCharToMultiByte(CP_ACP, 0, short_path_w, -1, short_path, lengthof(short_path), nullptr, nullptr);
return short_path; return short_path;
@ -69,8 +69,8 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
FT_Error err = FT_Err_Cannot_Open_Resource; FT_Error err = FT_Err_Cannot_Open_Resource;
HKEY hKey; HKEY hKey;
LONG ret; LONG ret;
TCHAR vbuffer[MAX_PATH], dbuffer[256]; wchar_t vbuffer[MAX_PATH], dbuffer[256];
TCHAR *pathbuf; wchar_t *pathbuf;
const char *font_path; const char *font_path;
uint index; uint index;
size_t path_len; size_t path_len;
@ -83,10 +83,10 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
} }
/* Convert font name to file system encoding. */ /* Convert font name to file system encoding. */
TCHAR *font_namep = _tcsdup(OTTD2FS(font_name)); wchar_t *font_namep = wcsdup(OTTD2FS(font_name));
for (index = 0;; index++) { for (index = 0;; index++) {
TCHAR *s; wchar_t *s;
DWORD vbuflen = lengthof(vbuffer); DWORD vbuflen = lengthof(vbuffer);
DWORD dbuflen = lengthof(dbuffer); DWORD dbuflen = lengthof(dbuffer);
@ -102,13 +102,13 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
* TTC files, font files which contain more than one font are separated * TTC files, font files which contain more than one font are separated
* by '&'. Our best bet will be to do substr match for the fontname * by '&'. Our best bet will be to do substr match for the fontname
* and then let FreeType figure out which index to load */ * and then let FreeType figure out which index to load */
s = _tcschr(vbuffer, _T('(')); s = wcschr(vbuffer, L'(');
if (s != nullptr) s[-1] = '\0'; if (s != nullptr) s[-1] = '\0';
if (_tcschr(vbuffer, _T('&')) == nullptr) { if (wcschr(vbuffer, L'&') == nullptr) {
if (_tcsicmp(vbuffer, font_namep) == 0) break; if (wcsicmp(vbuffer, font_namep) == 0) break;
} else { } else {
if (_tcsstr(vbuffer, font_namep) != nullptr) break; if (wcsstr(vbuffer, font_namep) != nullptr) break;
} }
} }
@ -121,9 +121,9 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
* contain multiple fonts inside this single file. GetFontData however * contain multiple fonts inside this single file. GetFontData however
* returns the whole file, so we need to check each font inside to get the * returns the whole file, so we need to check each font inside to get the
* proper font. */ * proper font. */
path_len = _tcslen(vbuffer) + _tcslen(dbuffer) + 2; // '\' and terminating nul. path_len = wcslen(vbuffer) + wcslen(dbuffer) + 2; // '\' and terminating nul.
pathbuf = AllocaM(TCHAR, path_len); pathbuf = AllocaM(wchar_t, path_len);
_sntprintf(pathbuf, path_len, _T("%s\\%s"), vbuffer, dbuffer); _snwprintf(pathbuf, path_len, L"%s\\%s", vbuffer, dbuffer);
/* Convert the path into something that FreeType understands. */ /* Convert the path into something that FreeType understands. */
font_path = GetShortPath(pathbuf); font_path = GetShortPath(pathbuf);
@ -228,13 +228,13 @@ err2:
ReleaseDC(nullptr, dc); ReleaseDC(nullptr, dc);
DeleteObject(font); DeleteObject(font);
err1: err1:
return ret_font_name == nullptr ? WIDE_TO_MB((const TCHAR *)logfont->elfFullName) : ret_font_name; return ret_font_name == nullptr ? FS2OTTD((const wchar_t *)logfont->elfFullName) : ret_font_name;
} }
#endif /* WITH_FREETYPE */ #endif /* WITH_FREETYPE */
class FontList { class FontList {
protected: protected:
TCHAR **fonts; wchar_t **fonts;
uint items; uint items;
uint capacity; uint capacity;
@ -251,9 +251,9 @@ public:
free(this->fonts); free(this->fonts);
} }
bool Add(const TCHAR *font) { bool Add(const wchar_t *font) {
for (uint i = 0; i < this->items; i++) { for (uint i = 0; i < this->items; i++) {
if (_tcscmp(this->fonts[i], font) == 0) return false; if (wcscmp(this->fonts[i], font) == 0) return false;
} }
if (this->items == this->capacity) { if (this->items == this->capacity) {
@ -261,7 +261,7 @@ public:
this->fonts = ReallocT(this->fonts, this->capacity); this->fonts = ReallocT(this->fonts, this->capacity);
} }
this->fonts[this->items++] = _tcsdup(font); this->fonts[this->items++] = wcsdup(font);
return true; return true;
} }
@ -279,7 +279,7 @@ static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXT
EFCParam *info = (EFCParam *)lParam; EFCParam *info = (EFCParam *)lParam;
/* Skip duplicates */ /* Skip duplicates */
if (!info->fonts.Add((const TCHAR *)logfont->elfFullName)) return 1; if (!info->fonts.Add((const wchar_t *)logfont->elfFullName)) return 1;
/* Only use TrueType fonts */ /* Only use TrueType fonts */
if (!(type & TRUETYPE_FONTTYPE)) return 1; if (!(type & TRUETYPE_FONTTYPE)) return 1;
/* Don't use SYMBOL fonts */ /* Don't use SYMBOL fonts */
@ -305,7 +305,7 @@ static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXT
} }
char font_name[MAX_PATH]; char font_name[MAX_PATH];
convert_from_fs((const TCHAR *)logfont->elfFullName, font_name, lengthof(font_name)); convert_from_fs((const wchar_t *)logfont->elfFullName, font_name, lengthof(font_name));
#ifdef WITH_FREETYPE #ifdef WITH_FREETYPE
/* Add english name after font name */ /* Add english name after font name */
@ -342,7 +342,7 @@ bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, i
{ {
DEBUG(freetype, 1, "Trying fallback fonts"); DEBUG(freetype, 1, "Trying fallback fonts");
EFCParam langInfo; EFCParam langInfo;
if (GetLocaleInfo(MAKELCID(winlangid, SORT_DEFAULT), LOCALE_FONTSIGNATURE, (LPTSTR)&langInfo.locale, sizeof(langInfo.locale) / sizeof(TCHAR)) == 0) { if (GetLocaleInfo(MAKELCID(winlangid, SORT_DEFAULT), LOCALE_FONTSIGNATURE, (LPTSTR)&langInfo.locale, sizeof(langInfo.locale) / sizeof(wchar_t)) == 0) {
/* Invalid langid or some other mysterious error, can't determine fallback font. */ /* Invalid langid or some other mysterious error, can't determine fallback font. */
DEBUG(freetype, 1, "Can't get locale info for fallback font (langid=0x%x)", winlangid); DEBUG(freetype, 1, "Can't get locale info for fallback font (langid=0x%x)", winlangid);
return false; return false;
@ -601,7 +601,7 @@ void LoadWin32Font(FontSize fs)
} else if (strchr(settings->font, '.') != nullptr) { } else if (strchr(settings->font, '.') != nullptr) {
/* Might be a font file name, try load it. */ /* Might be a font file name, try load it. */
TCHAR fontPath[MAX_PATH] = {}; wchar_t fontPath[MAX_PATH] = {};
/* See if this is an absolute path. */ /* See if this is an absolute path. */
if (FileExists(settings->font)) { if (FileExists(settings->font)) {
@ -619,7 +619,7 @@ void LoadWin32Font(FontSize fs)
/* Try a nice little undocumented function first for getting the internal font name. /* Try a nice little undocumented function first for getting the internal font name.
* Some documentation is found at: http://www.undocprint.org/winspool/getfontresourceinfo */ * Some documentation is found at: http://www.undocprint.org/winspool/getfontresourceinfo */
typedef BOOL(WINAPI *PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD); typedef BOOL(WINAPI *PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD);
static PFNGETFONTRESOURCEINFO GetFontResourceInfo = (PFNGETFONTRESOURCEINFO)GetProcAddress(GetModuleHandle(_T("Gdi32")), "GetFontResourceInfoW"); static PFNGETFONTRESOURCEINFO GetFontResourceInfo = (PFNGETFONTRESOURCEINFO)GetProcAddress(GetModuleHandle(L"Gdi32"), "GetFontResourceInfoW");
if (GetFontResourceInfo != nullptr) { if (GetFontResourceInfo != nullptr) {
/* Try to query an array of LOGFONTs that describe the file. */ /* Try to query an array of LOGFONTs that describe the file. */
@ -634,10 +634,10 @@ void LoadWin32Font(FontSize fs)
/* No dice yet. Use the file name as the font face name, hoping it matches. */ /* No dice yet. Use the file name as the font face name, hoping it matches. */
if (logfont.lfFaceName[0] == 0) { if (logfont.lfFaceName[0] == 0) {
TCHAR fname[_MAX_FNAME]; wchar_t fname[_MAX_FNAME];
_tsplitpath(fontPath, nullptr, nullptr, fname, nullptr); _wsplitpath(fontPath, nullptr, nullptr, fname, nullptr);
_tcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE); wcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE);
logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr || strcasestr(settings->font, "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr || strcasestr(settings->font, "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
} }
} else { } else {

View File

@ -33,7 +33,7 @@ public:
~Win32FontCache(); ~Win32FontCache();
void ClearFontCache() override; void ClearFontCache() override;
GlyphID MapCharToGlyph(WChar key) override; GlyphID MapCharToGlyph(WChar key) override;
const char *GetFontName() override { return WIDE_TO_MB(this->logfont.lfFaceName); } const char *GetFontName() override { return FS2OTTD(this->logfont.lfFaceName); }
const void *GetOSHandle() override { return &this->logfont; } const void *GetOSHandle() override { return &this->logfont; }
}; };

View File

@ -57,7 +57,7 @@ bool LoadLibraryList(Function proc[], const char *dll)
{ {
while (*dll != '\0') { while (*dll != '\0') {
HMODULE lib; HMODULE lib;
lib = LoadLibrary(MB_TO_WIDE(dll)); lib = LoadLibrary(OTTD2FS(dll));
if (lib == nullptr) return false; if (lib == nullptr) return false;
for (;;) { for (;;) {
@ -77,12 +77,12 @@ bool LoadLibraryList(Function proc[], const char *dll)
void ShowOSErrorBox(const char *buf, bool system) void ShowOSErrorBox(const char *buf, bool system)
{ {
MyShowCursor(true); MyShowCursor(true);
MessageBox(GetActiveWindow(), OTTD2FS(buf), _T("Error!"), MB_ICONSTOP | MB_TASKMODAL); MessageBox(GetActiveWindow(), OTTD2FS(buf), L"Error!", MB_ICONSTOP | MB_TASKMODAL);
} }
void OSOpenBrowser(const char *url) void OSOpenBrowser(const char *url)
{ {
ShellExecute(GetActiveWindow(), _T("open"), OTTD2FS(url), nullptr, nullptr, SW_SHOWNORMAL); ShellExecute(GetActiveWindow(), L"open", OTTD2FS(url), nullptr, nullptr, SW_SHOWNORMAL);
} }
/* Code below for windows version of opendir/readdir/closedir copied and /* Code below for windows version of opendir/readdir/closedir copied and
@ -132,7 +132,7 @@ static inline void dir_free(DIR *d)
} }
} }
DIR *opendir(const TCHAR *path) DIR *opendir(const wchar_t *path)
{ {
DIR *d; DIR *d;
UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
@ -141,12 +141,12 @@ DIR *opendir(const TCHAR *path)
if ((fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY)) { if ((fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY)) {
d = dir_calloc(); d = dir_calloc();
if (d != nullptr) { if (d != nullptr) {
TCHAR search_path[MAX_PATH]; wchar_t search_path[MAX_PATH];
bool slash = path[_tcslen(path) - 1] == '\\'; bool slash = path[wcslen(path) - 1] == '\\';
/* build search path for FindFirstFile, try not to append additional slashes /* build search path for FindFirstFile, try not to append additional slashes
* as it throws Win9x off its groove for root directories */ * as it throws Win9x off its groove for root directories */
_sntprintf(search_path, lengthof(search_path), _T("%s%s*"), path, slash ? _T("") : _T("\\")); _snwprintf(search_path, lengthof(search_path), L"%s%s*", path, slash ? L"" : L"\\");
*lastof(search_path) = '\0'; *lastof(search_path) = '\0';
d->hFind = FindFirstFile(search_path, &d->fd); d->hFind = FindFirstFile(search_path, &d->fd);
@ -204,8 +204,8 @@ bool FiosIsRoot(const char *file)
void FiosGetDrives(FileList &file_list) void FiosGetDrives(FileList &file_list)
{ {
TCHAR drives[256]; wchar_t drives[256];
const TCHAR *s; const wchar_t *s;
GetLogicalDriveStrings(lengthof(drives), drives); GetLogicalDriveStrings(lengthof(drives), drives);
for (s = drives; *s != '\0';) { for (s = drives; *s != '\0';) {
@ -245,10 +245,10 @@ bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
{ {
UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
bool retval = false; bool retval = false;
TCHAR root[4]; wchar_t root[4];
DWORD spc, bps, nfc, tnc; DWORD spc, bps, nfc, tnc;
_sntprintf(root, lengthof(root), _T("%c:") _T(PATHSEP), path[0]); _snwprintf(root, lengthof(root), L"%c:" PATHSEP, path[0]);
if (tot != nullptr && GetDiskFreeSpace(root, &spc, &bps, &nfc, &tnc)) { if (tot != nullptr && GetDiskFreeSpace(root, &spc, &bps, &nfc, &tnc)) {
*tot = ((spc * bps) * (uint64)nfc); *tot = ((spc * bps) * (uint64)nfc);
retval = true; retval = true;
@ -366,7 +366,7 @@ static INT_PTR CALLBACK HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM
*q = '\0'; *q = '\0';
/* We need to put the text in a separate buffer because the default /* We need to put the text in a separate buffer because the default
* buffer in OTTD2FS might not be large enough (512 chars). */ * buffer in OTTD2FS might not be large enough (512 chars). */
TCHAR help_msg_buf[8192]; wchar_t help_msg_buf[8192];
SetDlgItemText(wnd, 11, convert_to_fs(help_msg, help_msg_buf, lengthof(help_msg_buf))); SetDlgItemText(wnd, 11, convert_to_fs(help_msg, help_msg_buf, lengthof(help_msg_buf)));
SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE); SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
} return TRUE; } return TRUE;
@ -400,8 +400,8 @@ void ShowInfo(const char *str)
} else { } else {
/* We need to put the text in a separate buffer because the default /* We need to put the text in a separate buffer because the default
* buffer in OTTD2FS might not be large enough (512 chars). */ * buffer in OTTD2FS might not be large enough (512 chars). */
TCHAR help_msg_buf[8192]; wchar_t help_msg_buf[8192];
MessageBox(GetActiveWindow(), convert_to_fs(str, help_msg_buf, lengthof(help_msg_buf)), _T("OpenTTD"), MB_ICONINFORMATION | MB_OK); MessageBox(GetActiveWindow(), convert_to_fs(str, help_msg_buf, lengthof(help_msg_buf)), L"OpenTTD", MB_ICONINFORMATION | MB_OK);
} }
MyShowCursor(old); MyShowCursor(old);
} }
@ -447,7 +447,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
char *getcwd(char *buf, size_t size) char *getcwd(char *buf, size_t size)
{ {
TCHAR path[MAX_PATH]; wchar_t path[MAX_PATH];
GetCurrentDirectory(MAX_PATH - 1, path); GetCurrentDirectory(MAX_PATH - 1, path);
convert_from_fs(path, buf, size); convert_from_fs(path, buf, size);
return buf; return buf;
@ -459,7 +459,7 @@ void DetermineBasePaths(const char *exe)
{ {
extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths; extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
TCHAR path[MAX_PATH]; wchar_t path[MAX_PATH];
#ifdef WITH_PERSONAL_DIR #ifdef WITH_PERSONAL_DIR
if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path))) { if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path))) {
std::string tmp(FS2OTTD(path)); std::string tmp(FS2OTTD(path));
@ -497,8 +497,8 @@ void DetermineBasePaths(const char *exe)
_searchpaths[SP_WORKING_DIR] = cwd_s; _searchpaths[SP_WORKING_DIR] = cwd_s;
} else { } else {
/* Use the folder of the config file as working directory. */ /* Use the folder of the config file as working directory. */
TCHAR config_dir[MAX_PATH]; wchar_t config_dir[MAX_PATH];
_tcsncpy(path, convert_to_fs(_config_file.c_str(), path, lengthof(path)), lengthof(path)); wcsncpy(path, convert_to_fs(_config_file.c_str(), path, lengthof(path)), lengthof(path));
if (!GetFullPathName(path, lengthof(config_dir), config_dir, nullptr)) { if (!GetFullPathName(path, lengthof(config_dir), config_dir, nullptr)) {
DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError()); DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError());
_searchpaths[SP_WORKING_DIR].clear(); _searchpaths[SP_WORKING_DIR].clear();
@ -515,8 +515,8 @@ void DetermineBasePaths(const char *exe)
DEBUG(misc, 0, "GetModuleFileName failed (%lu)\n", GetLastError()); DEBUG(misc, 0, "GetModuleFileName failed (%lu)\n", GetLastError());
_searchpaths[SP_BINARY_DIR].clear(); _searchpaths[SP_BINARY_DIR].clear();
} else { } else {
TCHAR exec_dir[MAX_PATH]; wchar_t exec_dir[MAX_PATH];
_tcsncpy(path, convert_to_fs(exe, path, lengthof(path)), lengthof(path)); wcsncpy(path, convert_to_fs(exe, path, lengthof(path)), lengthof(path));
if (!GetFullPathName(path, lengthof(exec_dir), exec_dir, nullptr)) { if (!GetFullPathName(path, lengthof(exec_dir), exec_dir, nullptr)) {
DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError()); DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError());
_searchpaths[SP_BINARY_DIR].clear(); _searchpaths[SP_BINARY_DIR].clear();
@ -567,7 +567,7 @@ bool GetClipboardContents(char *buffer, const char *last)
* @see the current code-page comes from video\win32_v.cpp, event-notification * @see the current code-page comes from video\win32_v.cpp, event-notification
* WM_INPUTLANGCHANGE * WM_INPUTLANGCHANGE
*/ */
const char *FS2OTTD(const TCHAR *name) const char *FS2OTTD(const wchar_t *name)
{ {
static char utf8_buf[512]; static char utf8_buf[512];
return convert_from_fs(name, utf8_buf, lengthof(utf8_buf)); return convert_from_fs(name, utf8_buf, lengthof(utf8_buf));
@ -582,9 +582,9 @@ const char *FS2OTTD(const TCHAR *name)
* @param console_cp convert to the console encoding instead of the normal system encoding. * @param console_cp convert to the console encoding instead of the normal system encoding.
* @return pointer to the converted string; if failed string is of zero-length * @return pointer to the converted string; if failed string is of zero-length
*/ */
const TCHAR *OTTD2FS(const char *name, bool console_cp) const wchar_t *OTTD2FS(const char *name, bool console_cp)
{ {
static TCHAR system_buf[512]; static wchar_t system_buf[512];
return convert_to_fs(name, system_buf, lengthof(system_buf), console_cp); return convert_to_fs(name, system_buf, lengthof(system_buf), console_cp);
} }
@ -597,9 +597,9 @@ const TCHAR *OTTD2FS(const char *name, bool console_cp)
* @param buflen length in characters of the receiving buffer * @param buflen length in characters of the receiving buffer
* @return pointer to utf8_buf. If conversion fails the string is of zero-length * @return pointer to utf8_buf. If conversion fails the string is of zero-length
*/ */
char *convert_from_fs(const TCHAR *name, char *utf8_buf, size_t buflen) char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen)
{ {
const WCHAR *wide_buf = name; const wchar_t *wide_buf = name;
/* Convert UTF-16 string to UTF-8. */ /* Convert UTF-16 string to UTF-8. */
int len = WideCharToMultiByte(CP_UTF8, 0, wide_buf, -1, utf8_buf, (int)buflen, nullptr, nullptr); int len = WideCharToMultiByte(CP_UTF8, 0, wide_buf, -1, utf8_buf, (int)buflen, nullptr, nullptr);
@ -619,7 +619,7 @@ char *convert_from_fs(const TCHAR *name, char *utf8_buf, size_t buflen)
* @param console_cp convert to the console encoding instead of the normal system encoding. * @param console_cp convert to the console encoding instead of the normal system encoding.
* @return pointer to system_buf. If conversion fails the string is of zero-length * @return pointer to system_buf. If conversion fails the string is of zero-length
*/ */
TCHAR *convert_to_fs(const char *name, TCHAR *system_buf, size_t buflen, bool console_cp) wchar_t *convert_to_fs(const char *name, wchar_t *system_buf, size_t buflen, bool console_cp)
{ {
int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, system_buf, (int)buflen); int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, system_buf, (int)buflen);
if (len == 0) system_buf[0] = '\0'; if (len == 0) system_buf[0] = '\0';
@ -677,7 +677,7 @@ int OTTDStringCompare(const char *s1, const char *s2)
#endif #endif
if (first_time) { if (first_time) {
_CompareStringEx = (PFNCOMPARESTRINGEX)GetProcAddress(GetModuleHandle(_T("Kernel32")), "CompareStringEx"); _CompareStringEx = (PFNCOMPARESTRINGEX)GetProcAddress(GetModuleHandle(L"Kernel32"), "CompareStringEx");
first_time = false; first_time = false;
} }
@ -698,7 +698,7 @@ int OTTDStringCompare(const char *s1, const char *s2)
} }
} }
TCHAR s1_buf[512], s2_buf[512]; wchar_t s1_buf[512], s2_buf[512];
convert_to_fs(s1, s1_buf, lengthof(s1_buf)); convert_to_fs(s1, s1_buf, lengthof(s1_buf));
convert_to_fs(s2, s2_buf, lengthof(s2_buf)); convert_to_fs(s2, s2_buf, lengthof(s2_buf));

View File

@ -16,13 +16,8 @@ bool MyShowCursor(bool show, bool toggle = false);
typedef void (*Function)(int); typedef void (*Function)(int);
bool LoadLibraryList(Function proc[], const char *dll); bool LoadLibraryList(Function proc[], const char *dll);
char *convert_from_fs(const TCHAR *name, char *utf8_buf, size_t buflen); char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen);
TCHAR *convert_to_fs(const char *name, TCHAR *utf16_buf, size_t buflen, bool console_cp = false); wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen, bool console_cp = false);
/* Function shortcuts for UTF-8 <> UNICODE conversion. These functions use an
* internal buffer of max 512 characters. */
# define MB_TO_WIDE(str) OTTD2FS(str)
# define WIDE_TO_MB(str) FS2OTTD(str)
#if defined(__MINGW32__) && !defined(__MINGW64__) #if defined(__MINGW32__) && !defined(__MINGW64__)
#define SHGFP_TYPE_CURRENT 0 #define SHGFP_TYPE_CURRENT 0

View File

@ -48,7 +48,7 @@ static DWORD WINAPI SoundThread(LPVOID arg)
if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue; if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue;
MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4); MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) { if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
MessageBox(nullptr, _T("Sounds are disabled until restart."), _T("waveOutWrite failed"), MB_ICONINFORMATION); MessageBox(nullptr, L"Sounds are disabled until restart.", L"waveOutWrite failed", MB_ICONINFORMATION);
return 0; return 0;
} }
} }

View File

@ -260,12 +260,12 @@
# include <tchar.h> # include <tchar.h>
# include <io.h> # include <io.h>
namespace std { using ::_tfopen; } namespace std { using ::_wfopen; }
# define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode)) # define fopen(file, mode) _wfopen(OTTD2FS(file), _T(mode))
# define unlink(file) _tunlink(OTTD2FS(file)) # define unlink(file) _wunlink(OTTD2FS(file))
const char *FS2OTTD(const TCHAR *name); const char *FS2OTTD(const wchar_t *name);
const TCHAR *OTTD2FS(const char *name, bool console_cp = false); const wchar_t *OTTD2FS(const char *name, bool console_cp = false);
# else # else
# define fopen(file, mode) fopen(OTTD2FS(file), mode) # define fopen(file, mode) fopen(OTTD2FS(file), mode)
const char *FS2OTTD(const char *name); const char *FS2OTTD(const char *name);

View File

@ -151,7 +151,7 @@ const char *VideoDriver_Dedicated::Start(const StringList &parm)
/* For win32 we need to allocate a console (debug mode does the same) */ /* For win32 we need to allocate a console (debug mode does the same) */
CreateConsole(); CreateConsole();
CreateWindowsConsoleThread(); CreateWindowsConsoleThread();
SetConsoleTitle(_T("OpenTTD Dedicated Server")); SetConsoleTitle(L"OpenTTD Dedicated Server");
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER

View File

@ -214,7 +214,7 @@ bool VideoDriver_Win32Base::MakeWindow(bool full_screen)
char window_title[64]; char window_title[64];
seprintf(window_title, lastof(window_title), "OpenTTD %s", _openttd_revision); seprintf(window_title, lastof(window_title), "OpenTTD %s", _openttd_revision);
this->main_wnd = CreateWindow(_T("OTTD"), MB_TO_WIDE(window_title), style, x, y, w, h, 0, 0, GetModuleHandle(nullptr), this); this->main_wnd = CreateWindow(L"OTTD", OTTD2FS(window_title), style, x, y, w, h, 0, 0, GetModuleHandle(nullptr), this);
if (this->main_wnd == nullptr) usererror("CreateWindow failed"); if (this->main_wnd == nullptr) usererror("CreateWindow failed");
ShowWindow(this->main_wnd, showstyle); ShowWindow(this->main_wnd, showstyle);
} }
@ -329,9 +329,9 @@ static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
if (lParam & GCS_RESULTSTR) { if (lParam & GCS_RESULTSTR) {
/* Read result string from the IME. */ /* Read result string from the IME. */
LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build. LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR)); wchar_t *str = (wchar_t *)_alloca(len + sizeof(wchar_t));
len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str, len); len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str, len);
str[len / sizeof(TCHAR)] = '\0'; str[len / sizeof(wchar_t)] = '\0';
/* Transmit text to windowing system. */ /* Transmit text to windowing system. */
if (len > 0) { if (len > 0) {
@ -347,9 +347,9 @@ static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
if ((lParam & GCS_COMPSTR) && DrawIMECompositionString()) { if ((lParam & GCS_COMPSTR) && DrawIMECompositionString()) {
/* Read composition string from the IME. */ /* Read composition string from the IME. */
LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build. LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR)); wchar_t *str = (wchar_t *)_alloca(len + sizeof(wchar_t));
len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str, len); len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str, len);
str[len / sizeof(TCHAR)] = '\0'; str[len / sizeof(wchar_t)] = '\0';
if (len > 0) { if (len > 0) {
static char utf8_buf[1024]; static char utf8_buf[1024];
@ -358,7 +358,7 @@ static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
/* Convert caret position from bytes in the input string to a position in the UTF-8 encoded string. */ /* Convert caret position from bytes in the input string to a position in the UTF-8 encoded string. */
LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS, nullptr, 0); LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS, nullptr, 0);
const char *caret = utf8_buf; const char *caret = utf8_buf;
for (const TCHAR *c = str; *c != '\0' && *caret != '\0' && caret_bytes > 0; c++, caret_bytes--) { for (const wchar_t *c = str; *c != '\0' && *caret != '\0' && caret_bytes > 0; c++, caret_bytes--) {
/* Skip DBCS lead bytes or leading surrogates. */ /* Skip DBCS lead bytes or leading surrogates. */
if (Utf16IsLeadSurrogate(*c)) { if (Utf16IsLeadSurrogate(*c)) {
c++; c++;
@ -748,7 +748,7 @@ static void RegisterWndClass()
LoadCursor(nullptr, IDC_ARROW), LoadCursor(nullptr, IDC_ARROW),
0, 0,
0, 0,
_T("OTTD") L"OTTD"
}; };
registered = true; registered = true;
@ -1009,9 +1009,9 @@ float VideoDriver_Win32Base::GetDPIScale()
if (!init_done) { if (!init_done) {
init_done = true; init_done = true;
_GetDpiForWindow = (PFNGETDPIFORWINDOW)GetProcAddress(GetModuleHandle(_T("User32")), "GetDpiForWindow"); _GetDpiForWindow = (PFNGETDPIFORWINDOW)GetProcAddress(GetModuleHandle(L"User32"), "GetDpiForWindow");
_GetDpiForSystem = (PFNGETDPIFORSYSTEM)GetProcAddress(GetModuleHandle(_T("User32")), "GetDpiForSystem"); _GetDpiForSystem = (PFNGETDPIFORSYSTEM)GetProcAddress(GetModuleHandle(L"User32"), "GetDpiForSystem");
_GetDpiForMonitor = (PFNGETDPIFORMONITOR)GetProcAddress(LoadLibrary(_T("Shcore.dll")), "GetDpiForMonitor"); _GetDpiForMonitor = (PFNGETDPIFORMONITOR)GetProcAddress(LoadLibrary(L"Shcore.dll"), "GetDpiForMonitor");
} }
UINT cur_dpi = 0; UINT cur_dpi = 0;