Files
XCEngine/new_editor/app/Bootstrap/ApplicationWindowClass.cpp

115 lines
3.6 KiB
C++

#include "Bootstrap/Application.h"
#include "Bootstrap/ApplicationBootstrapSupport.h"
#include "Platform/Win32/EditorWindow.h"
#include "Platform/Win32/EditorWindowManager.h"
#include <Platform/Win32/WindowMessageDispatcher.h>
namespace XCEngine::UI::Editor {
using namespace BootstrapSupport;
namespace {
void TryEnableNonClientDpiScaling(HWND hwnd) {
if (hwnd == nullptr) {
return;
}
const HMODULE user32 = GetModuleHandleW(L"user32.dll");
if (user32 == nullptr) {
return;
}
using EnableNonClientDpiScalingFn = BOOL(WINAPI*)(HWND);
const auto enableNonClientDpiScaling =
reinterpret_cast<EnableNonClientDpiScalingFn>(
GetProcAddress(user32, "EnableNonClientDpiScaling"));
if (enableNonClientDpiScaling != nullptr) {
enableNonClientDpiScaling(hwnd);
}
}
Application* GetApplicationFromWindowUserData(HWND hwnd) {
return reinterpret_cast<Application*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
}
} // namespace
bool Application::RegisterWindowClass() {
WNDCLASSEXW windowClass = {};
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
windowClass.lpfnWndProc = &Application::WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = m_hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.hbrBackground = nullptr;
windowClass.lpszMenuName = nullptr;
windowClass.hIcon = static_cast<HICON>(
LoadImageW(
m_hInstance,
MAKEINTRESOURCEW(IDI_APP_ICON),
IMAGE_ICON,
0,
0,
LR_DEFAULTSIZE));
windowClass.hIconSm = static_cast<HICON>(
LoadImageW(
m_hInstance,
MAKEINTRESOURCEW(IDI_APP_ICON_SMALL),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR));
windowClass.lpszClassName = kWindowClassName;
m_windowClassAtom = RegisterClassExW(&windowClass);
if (m_windowClassAtom == 0) {
AppendUIEditorRuntimeTrace("app", "window class registration failed");
return false;
}
return true;
}
LRESULT CALLBACK Application::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_NCCREATE) {
TryEnableNonClientDpiScaling(hwnd);
const auto* createStruct = reinterpret_cast<const CREATESTRUCTW*>(lParam);
Application* application =
createStruct != nullptr
? reinterpret_cast<Application*>(createStruct->lpCreateParams)
: nullptr;
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(application));
if (application != nullptr && application->m_windowManager != nullptr) {
application->m_windowManager->HandlePendingNativeWindowCreated(hwnd);
}
return TRUE;
}
Application* application = GetApplicationFromWindowUserData(hwnd);
App::EditorWindow* window =
application != nullptr && application->m_windowManager != nullptr
? application->m_windowManager->FindWindow(hwnd)
: nullptr;
LRESULT dispatcherResult = 0;
if (application != nullptr &&
window != nullptr &&
Host::WindowMessageDispatcher::TryDispatch(
hwnd,
*application,
*window,
message,
wParam,
lParam,
dispatcherResult)) {
return dispatcherResult;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
} // namespace XCEngine::UI::Editor