diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index eead7f7683ed..8be83044bcd6 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -23,7 +23,7 @@ from matplotlib.widgets import SubplotTool try: - from matplotlib._windowing import GetForegroundWindow, SetForegroundWindow + from ._tkagg import Win32_GetForegroundWindow, Win32_SetForegroundWindow except ImportError: @contextmanager def _restore_foreground_window_at_end(): @@ -31,12 +31,12 @@ def _restore_foreground_window_at_end(): else: @contextmanager def _restore_foreground_window_at_end(): - foreground = GetForegroundWindow() + foreground = Win32_GetForegroundWindow() try: yield finally: if rcParams['tk.window_focus']: - SetForegroundWindow(foreground) + Win32_SetForegroundWindow(foreground) _log = logging.getLogger(__name__) diff --git a/lib/matplotlib/backends/windowing.py b/lib/matplotlib/backends/windowing.py index 54bfcec4d569..8e93858d6749 100644 --- a/lib/matplotlib/backends/windowing.py +++ b/lib/matplotlib/backends/windowing.py @@ -16,7 +16,9 @@ try: if not rcParams['tk.window_focus']: raise ImportError - from matplotlib._windowing import GetForegroundWindow, SetForegroundWindow + from matplotlib.backends._tkagg import ( + Win32_GetForegroundWindow as GetForegroundWindow, + Win32_SetForegroundWindow as SetForegroundWindow) except ImportError: diff --git a/setup.cfg.template b/setup.cfg.template index 23c9433d3e27..3b17cb94761d 100644 --- a/setup.cfg.template +++ b/setup.cfg.template @@ -41,8 +41,6 @@ # # - Tk support requires Tk development headers and Tkinter. # - Mac OSX backend requires the Cocoa headers included with XCode. -# - Windowing is MS-Windows specific, and requires the "windows.h" -# header. # # The other GUI toolkits do not require any extension code, and can be # used as long as the libraries are installed on your system -- @@ -61,7 +59,6 @@ #agg = auto #macosx = auto #tkagg = auto -#windowing = auto [rc_options] # User-configurable options diff --git a/setup.py b/setup.py index f339807cc56b..6dec6bb59af0 100644 --- a/setup.py +++ b/setup.py @@ -69,7 +69,6 @@ setupext.BackendAgg(), setupext.BackendTkAgg(), setupext.BackendMacOSX(), - setupext.Windowing(), 'Optional package data', setupext.Dlls(), ] diff --git a/setupext.py b/setupext.py index 9ef6c101af48..4a878ab0a487 100644 --- a/setupext.py +++ b/setupext.py @@ -1162,8 +1162,10 @@ def get_extension(self): def add_flags(self, ext): ext.include_dirs.insert(0, 'src') if sys.platform == 'win32': - # PSAPI library needed for finding Tcl/Tk at run time - ext.libraries.extend(['psapi']) + # psapi library needed for finding Tcl/Tk at run time. + # user32 library needed for window manipulation functions. + ext.libraries.extend(['psapi', 'user32']) + ext.extra_link_args.extend(["-mwindows"]) elif sys.platform == 'linux': ext.libraries.extend(['dl']) @@ -1189,32 +1191,6 @@ def get_extension(self): return ext -class Windowing(OptionalBackendPackage): - """ - Builds the windowing extension. - """ - name = "windowing" - - def check_requirements(self): - if sys.platform != 'win32': - raise CheckFailed("Microsoft Windows only") - config = self.get_config() - if config is False: - raise CheckFailed("skipping due to configuration") - return "" - - def get_extension(self): - sources = [ - "src/_windowing.cpp" - ] - ext = make_extension('matplotlib._windowing', sources) - ext.include_dirs.extend(['C:/include']) - ext.libraries.extend(['user32']) - ext.library_dirs.extend(['C:/lib']) - ext.extra_link_args.append("-mwindows") - return ext - - class OptionalPackageData(OptionalPackage): config_category = "package_data" diff --git a/src/_tkagg.cpp b/src/_tkagg.cpp index 431465ca3348..89f1d6f696b5 100644 --- a/src/_tkagg.cpp +++ b/src/_tkagg.cpp @@ -15,6 +15,10 @@ #include // agg:int8u +#ifdef _WIN32 +#include +#endif + // Include our own excerpts from the Tcl / Tk headers #include "_tkmini.h" @@ -245,15 +249,45 @@ static PyObject *mpl_tk_blit(PyObject *self, PyObject *args) } } +#ifdef _WIN32 +static PyObject * +Win32_GetForegroundWindow(PyObject *module, PyObject *args) +{ + HWND handle = GetForegroundWindow(); + if (!PyArg_ParseTuple(args, ":GetForegroundWindow")) { + return NULL; + } + return PyLong_FromSize_t((size_t)handle); +} + +static PyObject * +Win32_SetForegroundWindow(PyObject *module, PyObject *args) +{ + HWND handle; + if (!PyArg_ParseTuple(args, "n:SetForegroundWindow", &handle)) { + return NULL; + } + if (!SetForegroundWindow(handle)) { + return PyErr_Format(PyExc_RuntimeError, "Error setting window"); + } + Py_INCREF(Py_None); + return Py_None; +} +#endif + static PyMethodDef functions[] = { /* Tkinter interface stuff */ - { "tkinit", (PyCFunction)_tkinit, 1 }, - { "blit", (PyCFunction)mpl_tk_blit, 1 }, + { "tkinit", (PyCFunction)_tkinit, METH_VARARGS }, + { "blit", (PyCFunction)mpl_tk_blit, METH_VARARGS }, +#ifdef _WIN32 + { "Win32_GetForegroundWindow", (PyCFunction)Win32_GetForegroundWindow, METH_VARARGS }, + { "Win32_SetForegroundWindow", (PyCFunction)Win32_SetForegroundWindow, METH_VARARGS }, +#endif { NULL, NULL } /* sentinel */ }; // Functions to fill global TCL / Tk function pointers by dynamic loading -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) +#ifdef _WIN32 /* * On Windows, we can't load the tkinter module to get the TCL or Tk symbols, @@ -262,7 +296,6 @@ static PyMethodDef functions[] = { * Python, we scan all modules in the running process for the TCL and Tk * function names. */ -#include #define PSAPI_VERSION 1 #include // Must be linked with 'psapi' library diff --git a/src/_windowing.cpp b/src/_windowing.cpp deleted file mode 100644 index 3f5fc86eb62f..000000000000 --- a/src/_windowing.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "Python.h" -#include - -static PyObject * -_GetForegroundWindow(PyObject *module, PyObject *args) -{ - HWND handle = GetForegroundWindow(); - if (!PyArg_ParseTuple(args, ":GetForegroundWindow")) - { - return NULL; - } - return PyLong_FromSize_t((size_t)handle); -} - -static PyObject * -_SetForegroundWindow(PyObject *module, PyObject *args) -{ - HWND handle; - if (!PyArg_ParseTuple(args, "n:SetForegroundWindow", &handle)) - { - return NULL; - } - if (!SetForegroundWindow(handle)) - { - return PyErr_Format(PyExc_RuntimeError, - "Error setting window"); - } - Py_INCREF(Py_None); - return Py_None; -} - -static PyMethodDef _windowing_methods[] = -{ - {"GetForegroundWindow", _GetForegroundWindow, METH_VARARGS}, - {"SetForegroundWindow", _SetForegroundWindow, METH_VARARGS}, - {NULL, NULL} -}; - -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_windowing", - "", - -1, - _windowing_methods, - NULL, - NULL, - NULL, - NULL -}; - -PyMODINIT_FUNC PyInit__windowing(void) -{ - PyObject *module = PyModule_Create(&moduledef); - return module; -}