2021-08-18 22:36:46 +02:00
|
|
|
#include "window.hpp"
|
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2021-08-18 22:36:46 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
2021-08-18 22:36:46 +02:00
|
|
|
#include <imgui.h>
|
2021-08-25 19:32:47 +02:00
|
|
|
#include <imgui_internal.h>
|
|
|
|
#include <codicons_font.h>
|
2021-08-18 22:36:46 +02:00
|
|
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#define GLFW_EXPOSE_NATIVE_WIN32
|
|
|
|
#include <GLFW/glfw3native.h>
|
2021-08-25 19:32:47 +02:00
|
|
|
#undef GLFW_EXPOSE_NATIVE_WIN32
|
|
|
|
|
2021-08-18 22:36:46 +02:00
|
|
|
#include <winuser.h>
|
|
|
|
#include <dwmapi.h>
|
|
|
|
#include <windowsx.h>
|
|
|
|
|
|
|
|
namespace hex {
|
2021-08-25 19:32:47 +02:00
|
|
|
|
2021-08-18 22:36:46 +02:00
|
|
|
static LONG_PTR oldWndProc;
|
|
|
|
static float titleBarHeight;
|
|
|
|
static ImGuiMouseCursor mouseCursorIcon;
|
2021-08-21 13:55:21 +02:00
|
|
|
static float borderScaling;
|
2021-08-18 22:36:46 +02:00
|
|
|
|
2021-08-25 19:32:47 +02:00
|
|
|
static bool isTaskbarAutoHideEnabled(UINT edge, RECT monitor) {
|
|
|
|
APPBARDATA data = { .cbSize = sizeof(APPBARDATA), .uEdge = edge, .rc = monitor };
|
|
|
|
return ::SHAppBarMessage(ABM_GETAUTOHIDEBAR, &data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LRESULT windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
2021-08-18 22:36:46 +02:00
|
|
|
switch (uMsg) {
|
|
|
|
case WM_NCCALCSIZE: {
|
2021-08-25 19:32:47 +02:00
|
|
|
RECT &rect = *reinterpret_cast<RECT*>(lParam);
|
|
|
|
RECT client = rect;
|
|
|
|
|
|
|
|
DefWindowProcW(hwnd, WM_NCCALCSIZE, wParam, lParam);
|
|
|
|
|
|
|
|
if (IsMaximized(hwnd)) {
|
|
|
|
WINDOWINFO windowInfo = { .cbSize = sizeof(WINDOWINFO) };
|
|
|
|
GetWindowInfo(hwnd, &windowInfo);
|
|
|
|
rect = RECT {
|
|
|
|
.left = static_cast<LONG>(client.left + windowInfo.cyWindowBorders),
|
|
|
|
.top = static_cast<LONG>(client.top + windowInfo.cyWindowBorders),
|
|
|
|
.right = static_cast<LONG>(client.right - windowInfo.cyWindowBorders),
|
|
|
|
.bottom = static_cast<LONG>(client.bottom - windowInfo.cyWindowBorders)
|
|
|
|
};
|
|
|
|
|
|
|
|
HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
|
|
|
|
MONITORINFO monitorInfo = { .cbSize = sizeof(MONITORINFO) };
|
|
|
|
GetMonitorInfoW(hMonitor, &monitorInfo);
|
|
|
|
|
|
|
|
if (EqualRect(&rect, &monitorInfo.rcMonitor)) {
|
|
|
|
if (isTaskbarAutoHideEnabled(ABE_BOTTOM, monitorInfo.rcMonitor))
|
|
|
|
rect.bottom--;
|
|
|
|
else if (isTaskbarAutoHideEnabled(ABE_LEFT, monitorInfo.rcMonitor))
|
|
|
|
rect.left++;
|
|
|
|
else if (isTaskbarAutoHideEnabled(ABE_TOP, monitorInfo.rcMonitor))
|
|
|
|
rect.top++;
|
|
|
|
else if (isTaskbarAutoHideEnabled(ABE_RIGHT, monitorInfo.rcMonitor))
|
|
|
|
rect.right--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rect = client;
|
2021-08-18 22:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case WM_SETCURSOR: {
|
|
|
|
auto cursorPos = LOWORD(lParam);
|
|
|
|
|
|
|
|
switch (cursorPos) {
|
|
|
|
case HTRIGHT:
|
|
|
|
case HTLEFT:
|
|
|
|
mouseCursorIcon = ImGuiMouseCursor_ResizeEW;
|
|
|
|
return TRUE;
|
|
|
|
case HTTOP:
|
|
|
|
case HTBOTTOM:
|
|
|
|
mouseCursorIcon = ImGuiMouseCursor_ResizeNS;
|
|
|
|
return TRUE;
|
|
|
|
case HTTOPLEFT:
|
|
|
|
case HTBOTTOMRIGHT:
|
|
|
|
mouseCursorIcon = ImGuiMouseCursor_ResizeNWSE;
|
|
|
|
return TRUE;
|
|
|
|
case HTTOPRIGHT:
|
|
|
|
case HTBOTTOMLEFT:
|
|
|
|
mouseCursorIcon = ImGuiMouseCursor_ResizeNESW;
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
mouseCursorIcon = ImGuiMouseCursor_None;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case WM_NCHITTEST: {
|
|
|
|
POINT cursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
|
|
|
|
|
|
|
|
const POINT border{
|
2021-08-21 13:55:21 +02:00
|
|
|
static_cast<LONG>((::GetSystemMetrics(SM_CXFRAME) + ::GetSystemMetrics(SM_CXPADDEDBORDER)) * borderScaling / 2.0F),
|
|
|
|
static_cast<LONG>((::GetSystemMetrics(SM_CYFRAME) + ::GetSystemMetrics(SM_CXPADDEDBORDER)) * borderScaling / 2.0F)
|
2021-08-18 22:36:46 +02:00
|
|
|
};
|
2021-08-21 13:55:21 +02:00
|
|
|
|
2021-08-18 22:36:46 +02:00
|
|
|
RECT window;
|
|
|
|
if (!::GetWindowRect(hwnd, &window)) {
|
|
|
|
return HTNOWHERE;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto RegionClient = 0b0000;
|
|
|
|
constexpr auto RegionLeft = 0b0001;
|
|
|
|
constexpr auto RegionRight = 0b0010;
|
|
|
|
constexpr auto RegionTop = 0b0100;
|
|
|
|
constexpr auto RegionBottom = 0b1000;
|
|
|
|
|
|
|
|
const auto result =
|
|
|
|
RegionLeft * (cursor.x < (window.left + border.x)) |
|
|
|
|
RegionRight * (cursor.x >= (window.right - border.x)) |
|
|
|
|
RegionTop * (cursor.y < (window.top + border.y)) |
|
|
|
|
RegionBottom * (cursor.y >= (window.bottom - border.y));
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case RegionLeft:
|
|
|
|
return HTLEFT;
|
|
|
|
case RegionRight:
|
|
|
|
return HTRIGHT;
|
|
|
|
case RegionTop:
|
|
|
|
return HTTOP;
|
|
|
|
case RegionBottom:
|
|
|
|
return HTBOTTOM;
|
|
|
|
case RegionTop | RegionLeft:
|
|
|
|
return HTTOPLEFT;
|
|
|
|
case RegionTop | RegionRight:
|
|
|
|
return HTTOPRIGHT;
|
|
|
|
case RegionBottom | RegionLeft:
|
|
|
|
return HTBOTTOMLEFT;
|
|
|
|
case RegionBottom | RegionRight:
|
|
|
|
return HTBOTTOMRIGHT;
|
|
|
|
case RegionClient:
|
2021-08-25 19:32:47 +02:00
|
|
|
default:
|
2021-08-21 00:52:11 +02:00
|
|
|
if ((cursor.y < (window.top + titleBarHeight * 2)) && !ImGui::IsAnyItemHovered())
|
2021-08-18 22:36:46 +02:00
|
|
|
return HTCAPTION;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:32:47 +02:00
|
|
|
default: break;
|
2021-08-18 22:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return CallWindowProc((WNDPROC)oldWndProc, hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2021-08-22 20:24:42 +02:00
|
|
|
|
|
|
|
void Window::initNative() {
|
|
|
|
auto hConsoleWindow = ::GetConsoleWindow();
|
|
|
|
::ShowWindow(hConsoleWindow, FALSE);
|
|
|
|
|
|
|
|
auto hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (hConsole != INVALID_HANDLE_VALUE) {
|
|
|
|
DWORD mode = 0;
|
|
|
|
if (::GetConsoleMode(hConsole, &mode)) {
|
|
|
|
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
::SetConsoleMode(hConsole, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 22:36:46 +02:00
|
|
|
void Window::setupNativeWindow() {
|
|
|
|
auto hwnd = glfwGetWin32Window(this->m_window);
|
2021-08-21 13:55:21 +02:00
|
|
|
|
2021-08-25 19:32:47 +02:00
|
|
|
oldWndProc = ::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)windowProc);
|
2021-08-21 13:55:21 +02:00
|
|
|
|
2021-08-18 22:36:46 +02:00
|
|
|
MARGINS borderless = {1,1,1,1};
|
2021-08-22 20:24:42 +02:00
|
|
|
::DwmExtendFrameIntoClientArea(hwnd, &borderless);
|
2021-08-21 13:55:21 +02:00
|
|
|
|
2021-08-22 20:24:42 +02:00
|
|
|
::SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_ASYNCWINDOWPOS | SWP_NOSIZE | SWP_NOMOVE);
|
|
|
|
::SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_POPUP | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU);
|
2021-08-18 22:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::updateNativeWindow() {
|
|
|
|
titleBarHeight = ImGui::GetCurrentWindow()->MenuBarHeight();
|
2021-08-21 13:55:21 +02:00
|
|
|
borderScaling = this->m_globalScale;
|
2021-08-18 22:36:46 +02:00
|
|
|
|
|
|
|
if (mouseCursorIcon != ImGuiMouseCursor_None)
|
|
|
|
ImGui::SetMouseCursor(mouseCursorIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::drawTitleBar() {
|
2021-08-21 00:52:11 +02:00
|
|
|
auto buttonSize = ImVec2(titleBarHeight * 1.5F, titleBarHeight - 1);
|
2021-08-18 22:36:46 +02:00
|
|
|
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_MenuBarBg));
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabActive));
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabHovered));
|
|
|
|
|
|
|
|
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * 6);
|
|
|
|
#if defined(DEBUG)
|
2021-08-21 00:52:11 +02:00
|
|
|
if (ImGui::TitleBarButton(ICON_VS_DEBUG, buttonSize))
|
2021-08-18 22:36:46 +02:00
|
|
|
hex::openWebpage("https://imhex.werwolv.net/debug");
|
2021-08-18 23:12:27 +02:00
|
|
|
ImGui::InfoTooltip("hex.menu.debug_build"_lang);
|
2021-08-18 22:36:46 +02:00
|
|
|
#endif
|
2021-08-21 00:52:11 +02:00
|
|
|
if (ImGui::TitleBarButton(ICON_VS_SMILEY, buttonSize))
|
2021-08-18 22:36:46 +02:00
|
|
|
hex::openWebpage("mailto://hey@werwolv.net");
|
2021-08-18 23:12:27 +02:00
|
|
|
ImGui::InfoTooltip("hex.menu.feedback"_lang);
|
2021-08-18 22:36:46 +02:00
|
|
|
|
|
|
|
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * 3);
|
2021-08-21 00:52:11 +02:00
|
|
|
if (ImGui::TitleBarButton(ICON_VS_CHROME_MINIMIZE, buttonSize))
|
2021-08-18 22:36:46 +02:00
|
|
|
glfwIconifyWindow(this->m_window);
|
|
|
|
if (glfwGetWindowAttrib(this->m_window, GLFW_MAXIMIZED)) {
|
2021-08-21 00:52:11 +02:00
|
|
|
if (ImGui::TitleBarButton(ICON_VS_CHROME_RESTORE, buttonSize))
|
2021-08-18 22:36:46 +02:00
|
|
|
glfwRestoreWindow(this->m_window);
|
|
|
|
} else {
|
2021-08-21 00:52:11 +02:00
|
|
|
if (ImGui::TitleBarButton(ICON_VS_CHROME_MAXIMIZE, buttonSize))
|
2021-08-18 22:36:46 +02:00
|
|
|
glfwMaximizeWindow(this->m_window);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonActive, 0xFF7A70F1);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, 0xFF2311E8);
|
|
|
|
|
|
|
|
|
2021-08-21 00:52:11 +02:00
|
|
|
if (ImGui::TitleBarButton(ICON_VS_CHROME_CLOSE, buttonSize)) {
|
2021-08-21 13:55:21 +02:00
|
|
|
ImHexApi::Common::closeImHex();
|
2021-08-18 22:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PopStyleColor(5);
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
|
|
|
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - ImGui::CalcTextSize(this->m_windowTitle.c_str()).x) / 2);
|
|
|
|
ImGui::TextUnformatted(this->m_windowTitle.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|