ultimatepp/uppsrc/Core/Win32Util.cpp
Mirek Fidler 34ff691308 sizeof(wchar) is changed to 4 (32 bits) to support non BMP unicode characters
This might bring some incompatibilities in the code that expects wchar to be 16 bit, which
  escpecially involves dealing with Win32 (and to lesser extend MacOS) APIs, so if your application
  is doing that, please check all instances of WCHAR (UniChar on MacOS) or even wchar
  especially type casts.

  To support host APIs, char16 is introduced (but there is no 16-bit String varian).

  Use ToSystemCharsetW, FromSystemCharsetW to convert texts to Win32 API.

- Support of drawing non-BMP characters in GUI
- Vastly improved character font replacement code (when drawing characters missing with requested font, replacement font is used)
- Last instances of Win32 ANSI calls (those ending with A) are removed
- UTF handling routines are refactored and their's naming is unified
- RTF is now being able to handle non-BMP characters (RTF is used as clipboard format for RichText)

Other minor changes:

- fixed TryRealloc issue
- improved MemoryCheck
- Removed MemoryAlloc48/MemoryFree48
- In theide Background parsing should less often cause delays in the main thread
2021-12-02 12:03:19 +01:00

221 lines
5.1 KiB
C++

#include "Core.h"
namespace Upp {
#ifdef PLATFORM_WIN32
static HINSTANCE app_instance;
HINSTANCE AppGetHandle()
{
if(!app_instance)
app_instance = GetModuleHandle(NULL);
return app_instance;
}
void AppSetHandle(HINSTANCE dll_instance) { app_instance = dll_instance; }
#ifndef PLATFORM_WINCE
bool IsWin2K()
{
OSVERSIONINFO of;
of.dwOSVersionInfoSize = sizeof(of);
GetVersionEx(&of);
return of.dwMajorVersion >= 5;
}
bool IsWinXP()
{
OSVERSIONINFO of;
of.dwOSVersionInfoSize = sizeof(of);
GetVersionEx(&of);
return of.dwMajorVersion > 5 || of.dwMajorVersion == 5 && of.dwMinorVersion >= 1;
}
bool IsWinVista()
{
OSVERSIONINFO of;
of.dwOSVersionInfoSize = sizeof(of);
GetVersionEx(&of);
return of.dwMajorVersion >= 6;
}
bool IsWin7()
{
OSVERSIONINFO of;
of.dwOSVersionInfoSize = sizeof(of);
GetVersionEx(&of);
return of.dwMajorVersion >= 6 && of.dwMinorVersion >= 1;
}
#endif
String AsString(const wchar_t *buffer) {
if(!buffer)
return Null;
return AsString(buffer, (int)wcslen(buffer));
}
String AsString(const wchar_t *buffer, int count) { // Convert with code page...
if(!buffer)
return Null;
StringBuffer temp(count);
for(char *p = temp, *e = p + count; p < e;)
*p++ = (char)*buffer++;
return String(temp);
}
String AsString(const wchar_t *buffer, const wchar_t *end) {
if(!buffer)
return Null;
return AsString(buffer, (int)(end - buffer));
}
String GetWinRegString(const char *value, const char *path, HKEY base_key, dword wow) {
HKEY key = 0;
if(RegOpenKeyEx(base_key, path, 0, KEY_READ|wow, &key) != ERROR_SUCCESS)
return String::GetVoid();
dword type, len;
if(RegQueryValueEx(key, value, 0, &type, NULL, &len) != ERROR_SUCCESS)
{
RegCloseKey(key);
return String::GetVoid();
}
StringBuffer raw_len(len);
if(RegQueryValueEx(key, value, 0, 0, (byte *)~raw_len, &len) != ERROR_SUCCESS)
{
RegCloseKey(key);
return String::GetVoid();
}
if(len > 0 && (type == REG_SZ || type == REG_EXPAND_SZ))
len--;
raw_len.SetLength(len);
RegCloseKey(key);
return String(raw_len);
}
int GetWinRegInt(const char *value, const char *path, HKEY base_key, dword wow) {
HKEY key = 0;
if(RegOpenKeyEx(base_key, path, 0, KEY_READ|wow, &key) != ERROR_SUCCESS)
return Null;
int data;
dword type, length = sizeof(data);
if(RegQueryValueEx(key, value, 0, &type, (byte *)&data, &length) != ERROR_SUCCESS)
data = Null;
RegCloseKey(key);
return data;
}
bool SetWinRegString(const String& string, const char *value, const char *path, HKEY base_key, dword wow) {
HKEY key = 0;
if(RegCreateKeyEx(base_key, path, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS|wow, NULL, &key, NULL) != ERROR_SUCCESS)
return false;
bool ok = (RegSetValueEx(key, value, 0, REG_SZ, string, string.GetLength() + 1) == ERROR_SUCCESS);
RegCloseKey(key);
return ok;
}
bool SetWinRegExpandString(const String& string, const char *value, const char *path, HKEY base_key, dword wow) {
HKEY key = 0;
if(RegCreateKeyEx(base_key, path, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS|wow, NULL, &key, NULL) != ERROR_SUCCESS)
return false;
bool ok = (RegSetValueEx(key, value, 0, REG_EXPAND_SZ, string, string.GetLength() + 1) == ERROR_SUCCESS);
RegCloseKey(key);
return ok;
}
bool SetWinRegInt(int data, const char *value, const char *path, HKEY base_key, dword wow)
{
HKEY key = 0;
if(RegCreateKeyEx(base_key, path, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS|wow, NULL, &key, NULL) != ERROR_SUCCESS)
return false;
bool ok = (RegSetValueEx(key, value, 0, REG_DWORD, (const byte *)&data, sizeof(data)) == ERROR_SUCCESS);
RegCloseKey(key);
return ok;
}
void DeleteWinReg(const String& key, HKEY base, dword wow) {
HKEY hkey;
if(RegOpenKeyEx(base, key, 0, KEY_READ|wow, &hkey) != ERROR_SUCCESS)
return;
Vector<String> subkeys;
char temp[_MAX_PATH];
dword len;
for(dword dw = 0; len = sizeof(temp), RegEnumKeyEx(hkey, dw, temp, &len, 0, 0, 0, 0) == ERROR_SUCCESS; dw++)
subkeys.Add(temp);
RegCloseKey(hkey);
while(!subkeys.IsEmpty())
DeleteWinReg(key + '\\' + subkeys.Pop(), base);
static LONG (WINAPI *RegDeleteKeyEx)(HKEY, LPCTSTR, REGSAM, DWORD);
DllFn(RegDeleteKeyEx, "Advapi32.dll", "RegDeleteKeyExA");
if(wow && RegDeleteKeyEx)
RegDeleteKeyEx(base, key, wow, 0);
else
RegDeleteKey(base, key);
}
String GetSystemDirectory() {
WCHAR temp[MAX_PATH];
*temp = 0;
::GetSystemDirectoryW(temp, sizeof(temp));
return FromSystemCharsetW(temp);
}
String GetWindowsDirectory() {
WCHAR temp[MAX_PATH];
*temp = 0;
GetWindowsDirectoryW(temp, sizeof(temp));
return FromSystemCharsetW(temp);
}
void *GetDllFn(const char *dll, const char *fn)
{
if(HMODULE hDLL = LoadLibrary(dll))
return (void *)GetProcAddress(hDLL, fn);
return NULL;
}
String GetModuleFileName(HINSTANCE instance) {
WCHAR h[_MAX_PATH];
GetModuleFileNameW(instance, h, _MAX_PATH);
return FromSystemCharsetW(h);
}
#ifdef DEPRECATED
bool SyncObject::Wait(int ms)
{
return WaitForSingleObject(handle, ms);
}
bool SyncObject::Wait()
{
return Wait(INFINITE);
}
SyncObject::SyncObject()
{
handle = NULL;
}
SyncObject::~SyncObject()
{
if(handle) CloseHandle(handle);
}
Win32Event::Win32Event()
{
handle = CreateEvent(NULL, FALSE, FALSE, NULL);
}
void Win32Event::Set()
{
SetEvent(handle);
}
#endif
#endif
}