Skip to content

Commit

Permalink
Respect mouse pointer size for reverse arrow cursor, issue #653.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Apr 14, 2024
1 parent c8fa714 commit 244c226
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
12 changes: 8 additions & 4 deletions scintilla/win32/PlatWin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2887,15 +2887,19 @@ void FlipBitmap(HBITMAP bitmap, int width, int height) noexcept {

}

HCURSOR LoadReverseArrowCursor(UINT dpi) noexcept {
HCURSOR LoadReverseArrowCursor(UINT dpi, int cursorBaseSize) noexcept {
HCURSOR reverseArrowCursor {};

bool created = false;
HCURSOR cursor = ::LoadCursor({}, IDC_ARROW);

if (dpi != g_uSystemDPI) {
const int width = SystemMetricsForDpi(SM_CXCURSOR, dpi);
const int height = SystemMetricsForDpi(SM_CYCURSOR, dpi);
if (dpi != g_uSystemDPI || cursorBaseSize > 32) {
int width = SystemMetricsForDpi(SM_CXCURSOR, dpi);
int height = SystemMetricsForDpi(SM_CYCURSOR, dpi);
if (cursorBaseSize > width || cursorBaseSize > height) {
width = MulDiv(cursorBaseSize, dpi, g_uSystemDPI);
height = width;
}
HCURSOR copy = static_cast<HCURSOR>(::CopyImage(cursor, IMAGE_CURSOR, width, height, LR_COPYFROMRESOURCE | LR_COPYRETURNORG));
if (copy) {
created = copy != cursor;
Expand Down
2 changes: 1 addition & 1 deletion scintilla/win32/PlatWin.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ inline UINT DpiForWindow(WindowID wid) noexcept {
return GetWindowDPI(HwndFromWindowID(wid));
}

HCURSOR LoadReverseArrowCursor(UINT dpi) noexcept;
HCURSOR LoadReverseArrowCursor(UINT dpi, int cursorBaseSize) noexcept;

class MouseWheelDelta {
int wheelDelta = 0;
Expand Down
34 changes: 29 additions & 5 deletions scintilla/win32/ScintillaWin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class GlobalMemory;
class ReverseArrowCursor {
HCURSOR cursor {};
UINT dpi = USER_DEFAULT_SCREEN_DPI;
UINT cursorBaseSize = 32;

public:
ReverseArrowCursor() noexcept = default;
Expand All @@ -372,16 +373,17 @@ class ReverseArrowCursor {
}
}

HCURSOR Load(UINT dpi_) noexcept {
if (cursor) {
if (dpi == dpi_) {
HCURSOR Load(UINT dpi_, UINT cursorBaseSize_) noexcept {
if (cursor) {
if (dpi == dpi_ && cursorBaseSize == cursorBaseSize_) {
return cursor;
}
::DestroyCursor(cursor);
}

dpi = dpi_;
cursor = LoadReverseArrowCursor(dpi_);
cursorBaseSize = cursorBaseSize_;
cursor = LoadReverseArrowCursor(dpi_, cursorBaseSize_);
return cursor ? cursor : ::LoadCursor({}, IDC_ARROW);
}
};
Expand Down Expand Up @@ -419,6 +421,7 @@ class ScintillaWin final :
MouseWheelDelta horizontalWheelDelta;

UINT dpi = USER_DEFAULT_SCREEN_DPI;
UINT cursorBaseSize = 32;
ReverseArrowCursor reverseArrowCursor;

PRectangle rectangleClient;
Expand Down Expand Up @@ -846,7 +849,7 @@ void ScintillaWin::DisplayCursor(Window::Cursor c) noexcept {
c = static_cast<Window::Cursor>(cursorMode);
}
if (c == Window::Cursor::reverseArrow) {
::SetCursor(reverseArrowCursor.Load(dpi));
::SetCursor(reverseArrowCursor.Load(dpi, cursorBaseSize));
} else {
wMain.SetCursor(c);
}
Expand Down Expand Up @@ -3469,6 +3472,27 @@ void ScintillaWin::GetMouseParameters() noexcept {
// no horizontal scrolling configuration on Windows XP
charsPerScroll = (linesPerScroll == WHEEL_PAGESCROLL) ? 3 : linesPerScroll;
}

// https://learn.microsoft.com/en-us/answers/questions/815036/windows-cursor-size
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
DWORD type = REG_DWORD;
DWORD size = sizeof(DWORD);
[[maybe_unused]] const LSTATUS status = RegGetValue(HKEY_CURRENT_USER, L"Control Panel\\Cursors", L"CursorBaseSize", RRF_RT_REG_DWORD, &type, &cursorBaseSize, &size);

#else
HKEY hKey;
LSTATUS status = RegOpenKeyEx(HKEY_CURRENT_USER, L"Control Panel\\Cursors", 0, KEY_READ, &hKey);
if (status == ERROR_SUCCESS) {
DWORD baseSize = 0;
DWORD type = REG_DWORD;
DWORD size = sizeof(DWORD);
status = RegQueryValueEx(hKey, L"CursorBaseSize", nullptr, &type, (LPBYTE)(&baseSize), &size);
if (status == ERROR_SUCCESS && type == REG_DWORD) {
cursorBaseSize = baseSize;
}
RegCloseKey(hKey);
}
#endif
}

void ScintillaWin::CopyToGlobal(GlobalMemory &gmUnicode, const SelectionText &selectedText, CopyEncoding encoding) const {
Expand Down

2 comments on commit 244c226

@bluenlive
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a small mistyping on title.
This is about #635 not #653. :)

@zufuliu
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but can't be changed now due to branch protection.
image

Please sign in to comment.