Skip to content

Commit 94fbf2d

Browse files
authored
Merge pull request #13074 from anntzer/windowing-tkagg
Move _windowing extension into _tkagg.
2 parents 9a5e6ed + 824c2ef commit 94fbf2d

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
@@ -1162,8 +1162,10 @@ def get_extension(self):
11621162
def add_flags(self, ext):
11631163
ext.include_dirs.insert(0, 'src')
11641164
if sys.platform == 'win32':
1165-
# PSAPI library needed for finding Tcl/Tk at run time
1166-
ext.libraries.extend(['psapi'])
1165+
# psapi library needed for finding Tcl/Tk at run time.
1166+
# user32 library needed for window manipulation functions.
1167+
ext.libraries.extend(['psapi', 'user32'])
1168+
ext.extra_link_args.extend(["-mwindows"])
11671169
elif sys.platform == 'linux':
11681170
ext.libraries.extend(['dl'])
11691171

@@ -1189,32 +1191,6 @@ def get_extension(self):
11891191
return ext
11901192

11911193

1192-
class Windowing(OptionalBackendPackage):
1193-
"""
1194-
Builds the windowing extension.
1195-
"""
1196-
name = "windowing"
1197-
1198-
def check_requirements(self):
1199-
if sys.platform != 'win32':
1200-
raise CheckFailed("Microsoft Windows only")
1201-
config = self.get_config()
1202-
if config is False:
1203-
raise CheckFailed("skipping due to configuration")
1204-
return ""
1205-
1206-
def get_extension(self):
1207-
sources = [
1208-
"src/_windowing.cpp"
1209-
]
1210-
ext = make_extension('matplotlib._windowing', sources)
1211-
ext.include_dirs.extend(['C:/include'])
1212-
ext.libraries.extend(['user32'])
1213-
ext.library_dirs.extend(['C:/lib'])
1214-
ext.extra_link_args.append("-mwindows")
1215-
return ext
1216-
1217-
12181194
class OptionalPackageData(OptionalPackage):
12191195
config_category = "package_data"
12201196

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)