Skip to content

Commit 46f2d53

Browse files
committed
Using Cocoa when available, using the older Carbon API otherwise.
1 parent 5607c24 commit 46f2d53

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

lib/matplotlib/backends/backend_macosx.py

-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@
2323
import matplotlib
2424
from matplotlib.backends import _macosx
2525

26-
if not _macosx.verify_main_display():
27-
import warnings
28-
warnings.warn("Python is not installed as a framework. The MacOSX "
29-
"backend may not work correctly if Python is not "
30-
"installed as a framework. Please see the Python "
31-
"documentation for more information on installing "
32-
"Python as a framework on Mac OS X.")
33-
3426

3527
class Show(ShowBase):
3628
def mainloop(self):

src/_macosx.m

+33-41
Original file line numberDiff line numberDiff line change
@@ -3950,7 +3950,6 @@ static void _data_provider_release(void* info, const void* data, size_t size)
39503950
rect.size.height = height;
39513951
rect.size.width = width;
39523952

3953-
NSApp = [NSApplication sharedApplication];
39543953
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
39553954
self->window = [self->window initWithContentRect: rect
39563955
styleMask: NSTitledWindowMask
@@ -6208,32 +6207,34 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
62086207
Timer_new, /* tp_new */
62096208
};
62106209

6211-
6212-
static PyObject*
6213-
verify_main_display(PyObject* self)
6214-
/* Python2 contains a WMAvailable function in the MacOS module to check
6215-
* if python can interact with the display. This function calls
6216-
* CGMainDisplayID, which will return 0 if no window manager is available,
6217-
* or if we don't have permission to talk to it; this will happen if Python
6218-
* is not installed as a framework.
6219-
* The MacOS module was removed from Python3, so we need to provide a way
6220-
* to call CGMainDisplayID ourselves.
6221-
*/
6210+
static bool verify_framework(void)
62226211
{
6223-
PyObject* result;
6224-
CGDirectDisplayID display = CGMainDisplayID();
6225-
if (display == 0) result = Py_False;
6226-
else result = Py_True;
6227-
Py_INCREF(result);
6228-
return result;
6212+
#ifdef COMPILING_FOR_10_6
6213+
NSRunningApplication* app = [NSRunningApplication currentApplication];
6214+
NSApplicationActivationPolicy activationPolicy = [app activationPolicy];
6215+
switch (activationPolicy) {
6216+
case NSApplicationActivationPolicyRegular:
6217+
case NSApplicationActivationPolicyAccessory:
6218+
return true;
6219+
case NSApplicationActivationPolicyProhibited:
6220+
break;
6221+
}
6222+
#else
6223+
ProcessSerialNumber psn;
6224+
if (CGMainDisplayID()!=0
6225+
&& GetCurrentProcess(&psn)==noErr
6226+
&& SetFrontProcess(&psn)==noErr) return true;
6227+
#endif
6228+
PyErr_SetString(PyExc_RuntimeError,
6229+
"Python is not installed as a framework. The Mac OS X backend will "
6230+
"not be able to function correctly if Python is not installed as a "
6231+
"framework. See the Python documentation for more information on "
6232+
"installing Python as a framework on Mac OS X. Please either reinstall "
6233+
"Python as a framework, or try one of the other backends.");
6234+
return false;
62296235
}
62306236

62316237
static struct PyMethodDef methods[] = {
6232-
{"verify_main_display",
6233-
(PyCFunction)verify_main_display,
6234-
METH_NOARGS,
6235-
"Verifies if the main display can be found. This function returns False if Python is not built as a framework."
6236-
},
62376238
{"show",
62386239
(PyCFunction)show,
62396240
METH_NOARGS,
@@ -6273,7 +6274,6 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
62736274
void init_macosx(void)
62746275
#endif
62756276
{
6276-
#ifdef WITH_NEXT_FRAMEWORK
62776277
PyObject *module;
62786278
import_array();
62796279

@@ -6289,6 +6289,15 @@ void init_macosx(void)
62896289
return;
62906290
#endif
62916291

6292+
NSApp = [NSApplication sharedApplication];
6293+
6294+
if (!verify_framework())
6295+
#if PY3K
6296+
return NULL;
6297+
#else
6298+
return;
6299+
#endif
6300+
62926301
#if PY3K
62936302
module = PyModule_Create(&moduledef);
62946303
if (module==NULL) return NULL;
@@ -6325,21 +6334,4 @@ void init_macosx(void)
63256334
#if PY3K
63266335
return module;
63276336
#endif
6328-
#else
6329-
/* WITH_NEXT_FRAMEWORK is not defined. This means that Python is not
6330-
* installed as a framework, and therefore the Mac OS X backend will
6331-
* not interact properly with the window manager.
6332-
*/
6333-
PyErr_SetString(PyExc_RuntimeError,
6334-
"Python is not installed as a framework. The Mac OS X backend will "
6335-
"not be able to function correctly if Python is not installed as a "
6336-
"framework. See the Python documentation for more information on "
6337-
"installing Python as a framework on Mac OS X. Please either reinstall "
6338-
"Python as a framework, or try one of the other backends.");
6339-
#if PY3K
6340-
return NULL;
6341-
#else
6342-
return;
6343-
#endif
6344-
#endif
63456337
}

0 commit comments

Comments
 (0)