Skip to content

Commit

Permalink
Use wide char for window
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Oct 3, 2023
1 parent 2f85ca7 commit 255fcb4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/html_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace momo
return this->frame_;
}

std::optional<LRESULT> html_window::processor(const UINT message, const WPARAM w_param, const LPARAM l_param)
std::optional<LRESULT> html_window::processor(const UINT message, const WPARAM /*w_param*/, const LPARAM l_param)
{
if (message == WM_SIZE)
{
Expand Down
41 changes: 25 additions & 16 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ namespace momo

return get_dpi(window);
}

std::wstring convert_utf8_to_wide(const std::string& str)
{
const auto count = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), nullptr, 0);

std::wstring wstr(count, 0);
MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), wstr.data(), count);

return wstr;
}
}

window::window(const std::string& title, const int width, const int height,
Expand All @@ -37,27 +47,26 @@ namespace momo
{
ZeroMemory(&this->wc_, sizeof(this->wc_));

this->classname_ = "window-base-" + std::to_string(time(nullptr));
this->classname_ = L"window-base-" + std::to_wstring(time(nullptr));

this->wc_.cbSize = sizeof(this->wc_);
this->wc_.style = CS_HREDRAW | CS_VREDRAW;
this->wc_.lpfnWndProc = static_processor;
this->wc_.hInstance = GetModuleHandle(nullptr);
this->wc_.hCursor = LoadCursor(nullptr, IDC_ARROW);
this->wc_.hIcon = LoadIcon(this->wc_.hInstance, MAKEINTRESOURCE(102));
this->wc_.lpfnWndProc = &static_processor;
this->wc_.hInstance = GetModuleHandleA(nullptr);
this->wc_.hCursor = LoadCursorA(nullptr, IDC_ARROW);
this->wc_.hIcon = LoadIconW(this->wc_.hInstance, MAKEINTRESOURCEW(102));
this->wc_.hIconSm = this->wc_.hIcon;
this->wc_.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW);
this->wc_.lpszClassName = this->classname_.data();
RegisterClassEx(&this->wc_);
RegisterClassExW(&this->wc_);

const auto x = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
const auto y = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;

++window_count;

this->handle_ = CreateWindowExA(NULL, this->wc_.lpszClassName, title.data(), flags, x, y, width, height,
nullptr,
nullptr, this->wc_.hInstance, this);
this->handle_ = CreateWindowExW(NULL, this->wc_.lpszClassName, convert_utf8_to_wide(title).data(), flags, x, y,
width, height, nullptr, nullptr, this->wc_.hInstance, this);

constexpr BOOL value = TRUE;
DwmSetWindowAttribute(this->handle_,
Expand All @@ -71,7 +80,7 @@ namespace momo
window::~window()
{
this->close();
UnregisterClass(this->wc_.lpszClassName, this->wc_.hInstance);
UnregisterClassW(this->wc_.lpszClassName, this->wc_.hInstance);
}

void window::close()
Expand All @@ -85,10 +94,10 @@ namespace momo
void window::run()
{
MSG msg{};
while (GetMessage(&msg, nullptr, 0, 0))
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
DispatchMessageW(&msg);
}
}

Expand Down Expand Up @@ -133,7 +142,7 @@ namespace momo
}
}

return DefWindowProc(*this, message, w_param, l_param);
return DefWindowProcW(*this, message, w_param, l_param);
}

LRESULT CALLBACK window::static_processor(const HWND hwnd, const UINT message, const WPARAM w_param,
Expand All @@ -142,15 +151,15 @@ namespace momo
if (message == WM_CREATE)
{
auto* data = reinterpret_cast<LPCREATESTRUCT>(l_param);
SetWindowLongPtrA(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(data->lpCreateParams));
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(data->lpCreateParams));

static_cast<window*>(data->lpCreateParams)->handle_ = hwnd;
}

const auto self = reinterpret_cast<window*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
const auto self = reinterpret_cast<window*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
if (self) return self->processor(message, w_param, l_param);

return DefWindowProc(hwnd, message, w_param, l_param);
return DefWindowProcW(hwnd, message, w_param, l_param);
}

window::operator HWND() const
Expand Down
4 changes: 2 additions & 2 deletions src/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace momo
private:
uint32_t last_dpi_ = 96;

WNDCLASSEX wc_{};
WNDCLASSEXW wc_{};
HWND handle_ = nullptr;
std::string classname_;
std::wstring classname_;
std::function<std::optional<LRESULT>(window*, UINT, WPARAM, LPARAM)> callback_;

static LRESULT CALLBACK static_processor(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param);
Expand Down

0 comments on commit 255fcb4

Please sign in to comment.