Skip to content

macosx fixes #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2012
Merged
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
102 changes: 69 additions & 33 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include "numpy/arrayobject.h"
#include "path_cleanup.h"

#if PY_MAJOR_VERSION >= 3
#define PY3K 1
#else
#define PY3K 0
#endif

/* Must define Py_TYPE for Python 2.5 or older */
#ifndef Py_TYPE
# define Py_TYPE(o) ((o)->ob_type)
Expand Down Expand Up @@ -506,7 +512,7 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
static PyObject*
GraphicsContext_repr(GraphicsContext* self)
{
#if PY_MAJOR_VERSION >= 3
#if PY3K
return PyUnicode_FromFormat("GraphicsContext object %p wrapping the Quartz 2D graphics context %p", (void*)self, (void*)(self->cr));
#else
return PyString_FromFormat("GraphicsContext object %p wrapping the Quartz 2D graphics context %p", (void*)self, (void*)(self->cr));
Expand Down Expand Up @@ -713,8 +719,12 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)

if (offset!=Py_None)
{
if (PyFloat_Check(offset)) phase = PyFloat_AsDouble(offset);
else if (PyInt_Check(offset)) phase = PyInt_AsLong(offset);
if (PyFloat_Check(offset)) phase = PyFloat_AS_DOUBLE(offset);
#if PY3K
else if (PyLong_Check(offset)) phase = PyLong_AS_LONG(offset);
#else
else if (PyInt_Check(offset)) phase = PyInt_AS_LONG(offset);
#endif
else
{
PyErr_SetString(PyExc_TypeError,
Expand Down Expand Up @@ -747,8 +757,13 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
PyObject* value = PyTuple_GET_ITEM(dashes, i);
if (PyFloat_Check(value))
lengths[i] = (CGFloat) PyFloat_AS_DOUBLE(value);
#if PY3K
else if (PyLong_Check(value))
lengths[i] = (CGFloat) PyLong_AS_LONG(value);
#else
else if (PyInt_Check(value))
lengths[i] = (CGFloat) PyInt_AS_LONG(value);
#endif
else break;
}
Py_DECREF(dashes);
Expand Down Expand Up @@ -2252,7 +2267,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
#else
ATSFontRef font = 0;
#endif
#if PY_MAJOR_VERSION >= 3
#if PY3K
PyObject* ascii = NULL;
#endif

Expand Down Expand Up @@ -2435,7 +2450,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
for (i = 0; i < n; i++)
{
PyObject* item = PyList_GET_ITEM(family, i);
#if PY_MAJOR_VERSION >= 3
#if PY3K
ascii = PyUnicode_AsASCIIString(item);
if(!ascii) return 0;
temp = PyBytes_AS_STRING(ascii);
Expand Down Expand Up @@ -2469,7 +2484,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
name = temp;
break;
}
#if PY_MAJOR_VERSION >= 3
#if PY3K
Py_DECREF(ascii);
ascii = NULL;
#endif
Expand All @@ -2488,7 +2503,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
#ifndef COMPILING_FOR_10_5
CGContextSelectFont(cr, name, size, kCGEncodingMacRoman);
#endif
#if PY_MAJOR_VERSION >= 3
#if PY3K
Py_XDECREF(ascii);
#endif
return font;
Expand Down Expand Up @@ -2989,19 +3004,15 @@ static void _data_provider_release(void* info, const void* data, size_t size)
CGDataProviderRef provider;
double rect[4] = {0.0, 0.0, self->size.width, self->size.height};

#if PY_MAJOR_VERSION >= 3
if (!PyBytes_Check(image))
{
PyErr_SetString(PyExc_RuntimeError, "image is not a byte array");
return NULL;
}
#if PY3K
PyErr_SetString(PyExc_RuntimeError, "image is not a bytes object");
#else
if (!PyString_Check(image))
{
PyErr_SetString(PyExc_RuntimeError, "image is not a string");
PyErr_SetString(PyExc_RuntimeError, "image is not a str object");
#endif
return NULL;
}
#endif

const size_t bytesPerComponent = 1;
const size_t bitsPerComponent = 8 * bytesPerComponent;
Expand All @@ -3017,12 +3028,12 @@ static void _data_provider_release(void* info, const void* data, size_t size)
}

Py_INCREF(image);
#if PY_MAJOR_VERSION >= 3
n = PyByteArray_GET_SIZE(image);
data = PyByteArray_AS_STRING(image);
#ifdef PY3K
n = PyBytes_GET_SIZE(image);
data = PyBytes_AS_STRING(image);
#else
n = PyString_GET_SIZE(image);
data = PyString_AsString(image);
data = PyString_AS_STRING(image);
#endif

provider = CGDataProviderCreateWithData(image,
Expand All @@ -3039,7 +3050,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
provider,
NULL,
false,
kCGRenderingIntentDefault);
kCGRenderingIntentDefault);
CGColorSpaceRelease(colorspace);
CGDataProviderRelease(provider);

Expand Down Expand Up @@ -3296,7 +3307,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
static PyObject*
FigureCanvas_repr(FigureCanvas* self)
{
#if PY_MAJOR_VERSION >= 3
#if PY3K
return PyUnicode_FromFormat("FigureCanvas object %p wrapping NSView %p",
(void*)self, (void*)(self->view));
#else
Expand Down Expand Up @@ -3765,7 +3776,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
static PyObject*
FigureManager_repr(FigureManager* self)
{
#if PY_MAJOR_VERSION >= 3
#if PY3K
return PyUnicode_FromFormat("FigureManager object %p wrapping NSWindow %p",
(void*) self, (void*)(self->window));
#else
Expand Down Expand Up @@ -4175,7 +4186,7 @@ -(void)save_figure:(id)sender
static PyObject*
NavigationToolbar_repr(NavigationToolbar* self)
{
#if PY_MAJOR_VERSION >= 3
#if PY3K
return PyUnicode_FromFormat("NavigationToolbar object %p", (void*)self);
#else
return PyString_FromFormat("NavigationToolbar object %p", (void*)self);
Expand Down Expand Up @@ -4286,7 +4297,7 @@ -(void)save_figure:(id)sender
{
if(states[i]==1)
{
#if PY_MAJOR_VERSION >= 3
#if PY3K
PyList_SET_ITEM(list, j, PyLong_FromLong(i));
#else
PyList_SET_ITEM(list, j, PyInt_FromLong(i));
Expand Down Expand Up @@ -4706,7 +4717,7 @@ -(void)save_figure:(id)sender
static PyObject*
NavigationToolbar2_repr(NavigationToolbar2* self)
{
#if PY_MAJOR_VERSION >= 3
#if PY3K
return PyUnicode_FromFormat("NavigationToolbar2 object %p", (void*)self);
#else
return PyString_FromFormat("NavigationToolbar2 object %p", (void*)self);
Expand All @@ -4721,7 +4732,12 @@ -(void)save_figure:(id)sender
{
const char* message;

#if PY3K
if(!PyArg_ParseTuple(args, "y", &message)) return NULL;
#else
if(!PyArg_ParseTuple(args, "s", &message)) return NULL;
#endif

NSText* messagebox = self->messagebox;

if (messagebox)
Expand Down Expand Up @@ -5100,6 +5116,7 @@ - (void)mouseDown:(NSEvent *)event
{
int x, y;
int num;
int dblclick = 0;
PyObject* result;
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
Expand Down Expand Up @@ -5127,8 +5144,11 @@ - (void)mouseDown:(NSEvent *)event
case NSRightMouseDown: num = 3; break;
default: return; /* Unknown mouse event */
}
if ([event clickCount] == 2) {
dblclick = 1;
}
gstate = PyGILState_Ensure();
result = PyObject_CallMethod(canvas, "button_press_event", "iii", x, y, num);
result = PyObject_CallMethod(canvas, "button_press_event", "iiii", x, y, num, dblclick);
if(result)
Py_DECREF(result);
else
Expand Down Expand Up @@ -5205,14 +5225,18 @@ - (void)rightMouseDown:(NSEvent *)event
{
int x, y;
int num = 3;
int dblclick = 0;
PyObject* result;
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
gstate = PyGILState_Ensure();
result = PyObject_CallMethod(canvas, "button_press_event", "iii", x, y, num);
if ([event clickCount] == 2) {
dblclick = 1;
}
result = PyObject_CallMethod(canvas, "button_press_event", "iiii", x, y, num, dblclick);
if(result)
Py_DECREF(result);
else
Expand Down Expand Up @@ -5262,14 +5286,18 @@ - (void)otherMouseDown:(NSEvent *)event
{
int x, y;
int num = 2;
int dblclick = 0;
PyObject* result;
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
gstate = PyGILState_Ensure();
result = PyObject_CallMethod(canvas, "button_press_event", "iii", x, y, num);
if ([event clickCount] == 2) {
dblclick = 1;
}
result = PyObject_CallMethod(canvas, "button_press_event", "iiii", x, y, num, dblclick);
if(result)
Py_DECREF(result);
else
Expand Down Expand Up @@ -5578,7 +5606,11 @@ - (int)index
static PyObject*
show(PyObject* self)
{
if(nwin > 0) [NSApp run];
if(nwin > 0)
{
[NSApp activateIgnoringOtherApps: YES];
[NSApp run];
}
Py_INCREF(Py_None);
return Py_None;
}
Expand Down Expand Up @@ -5631,7 +5663,7 @@ - (int)index
static PyObject*
Timer_repr(Timer* self)
{
#if PY_MAJOR_VERSION >= 3
#if PY3K
return PyUnicode_FromFormat("Timer object %p wrapping CFRunLoopTimerRef %p",
(void*) self, (void*)(self->timer));
#else
Expand Down Expand Up @@ -5812,7 +5844,7 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
{NULL, NULL, 0, NULL}/* sentinel */
};

#if PY_MAJOR_VERSION >= 3
#if PY3K

static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
Expand Down Expand Up @@ -5842,13 +5874,13 @@ void init_macosx(void)
|| PyType_Ready(&NavigationToolbarType) < 0
|| PyType_Ready(&NavigationToolbar2Type) < 0
|| PyType_Ready(&TimerType) < 0)
#if PY_MAJOR_VERSION >= 3
#if PY3K
return NULL;
#else
return;
#endif

#if PY_MAJOR_VERSION >= 3
#if PY3K
module = PyModule_Create(&moduledef);
if (module==NULL) return NULL;
#else
Expand All @@ -5873,4 +5905,8 @@ void init_macosx(void)
PyModule_AddObject(module, "Timer", (PyObject*) &TimerType);

PyOS_InputHook = wait_for_stdin;

#if PY3K
return module;
#endif
}