Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ def destroy(*args):
self.window.protocol("WM_DELETE_WINDOW", destroy)
self.window.deiconify()
self.canvas._tkcanvas.focus_set()
if self._window_dpi.get() != 96:
Copy link
Member

Choose a reason for hiding this comment

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

why hard-code 96?

self._update_window_dpi()
else:
self.canvas.draw_idle()
if mpl.rcParams['figure.raise_window']:
Expand Down
41 changes: 26 additions & 15 deletions src/_tkagg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ mpl_tk_blit(py::object interp_obj, const char *photo_name,
}

#ifdef WIN32_DLL
void update_tk_window_dpi(HWND hwnd, Tcl_Interp *interp, std::string dpi) {
// This variable naming must match the name used in
// lib/matplotlib/backends/_backend_tk.py:FigureManagerTk.
std::string var_name("window_dpi");
var_name += std::to_string((unsigned long long)hwnd);
if (TCL_SETVAR) {
TCL_SETVAR(interp, var_name.c_str(), dpi.c_str(), 0);
} else if (TCL_SETVAR2) {
TCL_SETVAR2(interp, var_name.c_str(), NULL, dpi.c_str(), 0);
} else {
// This should be prevented at import time, and therefore unreachable.
// But defensively throw just in case.
throw std::runtime_error("Unable to call Tcl_SetVar or Tcl_SetVar2");
}
}

LRESULT CALLBACK
DpiSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
Expand All @@ -165,24 +181,10 @@ DpiSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
// call Python code, we must not also call *any* Tk code from Python.
// So stay with Tcl calls in C only.
{
// This variable naming must match the name used in
// lib/matplotlib/backends/_backend_tk.py:FigureManagerTk.
std::string var_name("window_dpi");
var_name += std::to_string((unsigned long long)hwnd);

// X is high word, Y is low word, but they are always equal.
std::string dpi = std::to_string(LOWORD(wParam));

Tcl_Interp* interp = (Tcl_Interp*)dwRefData;
if (TCL_SETVAR) {
TCL_SETVAR(interp, var_name.c_str(), dpi.c_str(), 0);
} else if (TCL_SETVAR2) {
TCL_SETVAR2(interp, var_name.c_str(), NULL, dpi.c_str(), 0);
} else {
// This should be prevented at import time, and therefore unreachable.
// But defensively throw just in case.
throw std::runtime_error("Unable to call Tcl_SetVar or Tcl_SetVar2");
}
update_tk_window_dpi(hwnd, interp, dpi);
}
return 0;
case WM_NCDESTROY:
Expand Down Expand Up @@ -235,6 +237,15 @@ mpl_tk_enable_dpi_awareness(py::object UNUSED_ON_NON_WINDOWS(frame_handle_obj),
// Per monitor aware means we need to handle WM_DPICHANGED by wrapping
// the Window Procedure, and the Python side needs to trace the Tk
// window_dpi variable stored on interp.
typedef UINT (WINAPI *GetDpiForWindow_t)(HWND);
GetDpiForWindow_t GetDpiForWindowPtr =
(GetDpiForWindow_t)GetProcAddress(user32, "GetDpiForWindow");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use the shorter style for casting as in #30615.

if (GetDpiForWindowPtr == NULL) {
FreeLibrary(user32);
return py::cast(false);
Copy link
Member

Choose a reason for hiding this comment

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

Is completely returning here correct? Is there a case where we may not be able to get the methods for managing the dpi but still need to run the setWindowSubclass method?

}
UINT dpi = GetDpiForWindowPtr(frame_handle);
update_tk_window_dpi(frame_handle, interp, std::to_string(dpi));
SetWindowSubclass(frame_handle, DpiSubclassProc, 0, (DWORD_PTR)interp);
}
FreeLibrary(user32);
Expand Down
Loading