Skip to content

Commit 0d33556

Browse files
committed
Move _windowing extension into _tkagg.
The main point is to remove one not particularly useful customization point from setup.cfg, and the associated logic in setupext.py. Minor additional changes in _tkagg.cpp: use METH_VARARGS instead of the opaque "1" (one can check in Python.h that the values indeed match), and just use `#ifdef _WIN32` to check for Windows-ness (`src/file_compat.h` also uses that check so it works).
1 parent d1060a8 commit 0d33556

File tree

7 files changed

+47
-95
lines changed

7 files changed

+47
-95
lines changed

lib/matplotlib/backends/_backend_tk.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
from matplotlib.widgets import SubplotTool
2424

2525
try:
26-
from matplotlib._windowing import GetForegroundWindow, SetForegroundWindow
26+
from ._tkagg import Win32_GetForegroundWindow, Win32_SetForegroundWindow
2727
except ImportError:
2828
@contextmanager
2929
def _restore_foreground_window_at_end():
3030
yield
3131
else:
3232
@contextmanager
3333
def _restore_foreground_window_at_end():
34-
foreground = GetForegroundWindow()
34+
foreground = Win32_GetForegroundWindow()
3535
try:
3636
yield
3737
finally:
3838
if rcParams['tk.window_focus']:
39-
SetForegroundWindow(foreground)
39+
Win32_SetForegroundWindow(foreground)
4040

4141

4242
_log = logging.getLogger(__name__)

lib/matplotlib/backends/windowing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
try:
1717
if not rcParams['tk.window_focus']:
1818
raise ImportError
19-
from matplotlib._windowing import GetForegroundWindow, SetForegroundWindow
19+
from matplotlib.backends._tkagg import (
20+
Win32_GetForeGroundWindow as GetForegroundWindow,
21+
Win32_SetForegroundWindow as SetForegroundWindow)
2022

2123
except ImportError:
2224

setup.cfg.template

-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
#
4242
# - Tk support requires Tk development headers and Tkinter.
4343
# - Mac OSX backend requires the Cocoa headers included with XCode.
44-
# - Windowing is MS-Windows specific, and requires the "windows.h"
45-
# header.
4644
#
4745
# The other GUI toolkits do not require any extension code, and can be
4846
# used as long as the libraries are installed on your system --
@@ -61,7 +59,6 @@
6159
#agg = auto
6260
#macosx = auto
6361
#tkagg = auto
64-
#windowing = auto
6562

6663
[rc_options]
6764
# User-configurable options

setup.py

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
setupext.BackendAgg(),
7070
setupext.BackendTkAgg(),
7171
setupext.BackendMacOSX(),
72-
setupext.Windowing(),
7372
'Optional package data',
7473
setupext.Dlls(),
7574
]

setupext.py

+4-28
Original file line numberDiff line numberDiff line change
@@ -1185,8 +1185,10 @@ def get_extension(self):
11851185
def add_flags(self, ext):
11861186
ext.include_dirs.insert(0, 'src')
11871187
if sys.platform == 'win32':
1188-
# PSAPI library needed for finding Tcl/Tk at run time
1189-
ext.libraries.extend(['psapi'])
1188+
# psapi library needed for finding Tcl/Tk at run time.
1189+
# user32 library needed for window manipulation functions.
1190+
ext.libraries.extend(['psapi', 'user32'])
1191+
ext.extra_link_args.extend(["-mwindows"])
11901192
elif sys.platform == 'linux':
11911193
ext.libraries.extend(['dl'])
11921194

@@ -1212,32 +1214,6 @@ def get_extension(self):
12121214
return ext
12131215

12141216

1215-
class Windowing(OptionalBackendPackage):
1216-
"""
1217-
Builds the windowing extension.
1218-
"""
1219-
name = "windowing"
1220-
1221-
def check_requirements(self):
1222-
if sys.platform != 'win32':
1223-
raise CheckFailed("Microsoft Windows only")
1224-
config = self.get_config()
1225-
if config is False:
1226-
raise CheckFailed("skipping due to configuration")
1227-
return ""
1228-
1229-
def get_extension(self):
1230-
sources = [
1231-
"src/_windowing.cpp"
1232-
]
1233-
ext = make_extension('matplotlib._windowing', sources)
1234-
ext.include_dirs.extend(['C:/include'])
1235-
ext.libraries.extend(['user32'])
1236-
ext.library_dirs.extend(['C:/lib'])
1237-
ext.extra_link_args.append("-mwindows")
1238-
return ext
1239-
1240-
12411217
class OptionalPackageData(OptionalPackage):
12421218
config_category = "package_data"
12431219

src/_tkagg.cpp

+37-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include <agg_basics.h> // agg:int8u
1717

18+
#ifdef _WIN32
19+
#include <windows.h>
20+
#endif
21+
1822
// Include our own excerpts from the Tcl / Tk headers
1923
#include "_tkmini.h"
2024

@@ -245,15 +249,45 @@ static PyObject *mpl_tk_blit(PyObject *self, PyObject *args)
245249
}
246250
}
247251

252+
#ifdef _WIN32
253+
static PyObject *
254+
Win32_GetForegroundWindow(PyObject *module, PyObject *args)
255+
{
256+
HWND handle = GetForegroundWindow();
257+
if (!PyArg_ParseTuple(args, ":GetForegroundWindow")) {
258+
return NULL;
259+
}
260+
return PyLong_FromSize_t((size_t)handle);
261+
}
262+
263+
static PyObject *
264+
Win32_SetForegroundWindow(PyObject *module, PyObject *args)
265+
{
266+
HWND handle;
267+
if (!PyArg_ParseTuple(args, "n:SetForegroundWindow", &handle)) {
268+
return NULL;
269+
}
270+
if (!SetForegroundWindow(handle)) {
271+
return PyErr_Format(PyExc_RuntimeError, "Error setting window");
272+
}
273+
Py_INCREF(Py_None);
274+
return Py_None;
275+
}
276+
#endif
277+
248278
static PyMethodDef functions[] = {
249279
/* Tkinter interface stuff */
250-
{ "tkinit", (PyCFunction)_tkinit, 1 },
251-
{ "blit", (PyCFunction)mpl_tk_blit, 1 },
280+
{ "tkinit", (PyCFunction)_tkinit, METH_VARARGS },
281+
{ "blit", (PyCFunction)mpl_tk_blit, METH_VARARGS },
282+
#ifdef _WIN32
283+
{ "Win32_GetForegroundWindow", (PyCFunction)Win32_GetForegroundWindow, METH_VARARGS },
284+
{ "Win32_SetForegroundWindow", (PyCFunction)Win32_SetForegroundWindow, METH_VARARGS },
285+
#endif
252286
{ NULL, NULL } /* sentinel */
253287
};
254288

255289
// Functions to fill global TCL / Tk function pointers by dynamic loading
256-
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
290+
#ifdef _WIN32
257291

258292
/*
259293
* On Windows, we can't load the tkinter module to get the TCL or Tk symbols,
@@ -262,7 +296,6 @@ static PyMethodDef functions[] = {
262296
* Python, we scan all modules in the running process for the TCL and Tk
263297
* function names.
264298
*/
265-
#include <windows.h>
266299
#define PSAPI_VERSION 1
267300
#include <psapi.h>
268301
// Must be linked with 'psapi' library

src/_windowing.cpp

-55
This file was deleted.

0 commit comments

Comments
 (0)