Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a79b95

Browse files
committed
Merge pull request #741 from jdh2358/mdhoon-macosx
macosx fixes
2 parents cb396f2 + 7bf6a11 commit 1a79b95

File tree

1 file changed

+69
-33
lines changed

1 file changed

+69
-33
lines changed

src/_macosx.m

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
#include "numpy/arrayobject.h"
66
#include "path_cleanup.h"
77

8+
#if PY_MAJOR_VERSION >= 3
9+
#define PY3K 1
10+
#else
11+
#define PY3K 0
12+
#endif
13+
814
/* Must define Py_TYPE for Python 2.5 or older */
915
#ifndef Py_TYPE
1016
# define Py_TYPE(o) ((o)->ob_type)
@@ -506,7 +512,7 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
506512
static PyObject*
507513
GraphicsContext_repr(GraphicsContext* self)
508514
{
509-
#if PY_MAJOR_VERSION >= 3
515+
#if PY3K
510516
return PyUnicode_FromFormat("GraphicsContext object %p wrapping the Quartz 2D graphics context %p", (void*)self, (void*)(self->cr));
511517
#else
512518
return PyString_FromFormat("GraphicsContext object %p wrapping the Quartz 2D graphics context %p", (void*)self, (void*)(self->cr));
@@ -713,8 +719,12 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
713719

714720
if (offset!=Py_None)
715721
{
716-
if (PyFloat_Check(offset)) phase = PyFloat_AsDouble(offset);
717-
else if (PyInt_Check(offset)) phase = PyInt_AsLong(offset);
722+
if (PyFloat_Check(offset)) phase = PyFloat_AS_DOUBLE(offset);
723+
#if PY3K
724+
else if (PyLong_Check(offset)) phase = PyLong_AS_LONG(offset);
725+
#else
726+
else if (PyInt_Check(offset)) phase = PyInt_AS_LONG(offset);
727+
#endif
718728
else
719729
{
720730
PyErr_SetString(PyExc_TypeError,
@@ -747,8 +757,13 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
747757
PyObject* value = PyTuple_GET_ITEM(dashes, i);
748758
if (PyFloat_Check(value))
749759
lengths[i] = (CGFloat) PyFloat_AS_DOUBLE(value);
760+
#if PY3K
761+
else if (PyLong_Check(value))
762+
lengths[i] = (CGFloat) PyLong_AS_LONG(value);
763+
#else
750764
else if (PyInt_Check(value))
751765
lengths[i] = (CGFloat) PyInt_AS_LONG(value);
766+
#endif
752767
else break;
753768
}
754769
Py_DECREF(dashes);
@@ -2252,7 +2267,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
22522267
#else
22532268
ATSFontRef font = 0;
22542269
#endif
2255-
#if PY_MAJOR_VERSION >= 3
2270+
#if PY3K
22562271
PyObject* ascii = NULL;
22572272
#endif
22582273

@@ -2435,7 +2450,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
24352450
for (i = 0; i < n; i++)
24362451
{
24372452
PyObject* item = PyList_GET_ITEM(family, i);
2438-
#if PY_MAJOR_VERSION >= 3
2453+
#if PY3K
24392454
ascii = PyUnicode_AsASCIIString(item);
24402455
if(!ascii) return 0;
24412456
temp = PyBytes_AS_STRING(ascii);
@@ -2469,7 +2484,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
24692484
name = temp;
24702485
break;
24712486
}
2472-
#if PY_MAJOR_VERSION >= 3
2487+
#if PY3K
24732488
Py_DECREF(ascii);
24742489
ascii = NULL;
24752490
#endif
@@ -2488,7 +2503,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
24882503
#ifndef COMPILING_FOR_10_5
24892504
CGContextSelectFont(cr, name, size, kCGEncodingMacRoman);
24902505
#endif
2491-
#if PY_MAJOR_VERSION >= 3
2506+
#if PY3K
24922507
Py_XDECREF(ascii);
24932508
#endif
24942509
return font;
@@ -2989,19 +3004,15 @@ static void _data_provider_release(void* info, const void* data, size_t size)
29893004
CGDataProviderRef provider;
29903005
double rect[4] = {0.0, 0.0, self->size.width, self->size.height};
29913006

2992-
#if PY_MAJOR_VERSION >= 3
29933007
if (!PyBytes_Check(image))
29943008
{
2995-
PyErr_SetString(PyExc_RuntimeError, "image is not a byte array");
2996-
return NULL;
2997-
}
3009+
#if PY3K
3010+
PyErr_SetString(PyExc_RuntimeError, "image is not a bytes object");
29983011
#else
2999-
if (!PyString_Check(image))
3000-
{
3001-
PyErr_SetString(PyExc_RuntimeError, "image is not a string");
3012+
PyErr_SetString(PyExc_RuntimeError, "image is not a str object");
3013+
#endif
30023014
return NULL;
30033015
}
3004-
#endif
30053016

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

30193030
Py_INCREF(image);
3020-
#if PY_MAJOR_VERSION >= 3
3021-
n = PyByteArray_GET_SIZE(image);
3022-
data = PyByteArray_AS_STRING(image);
3031+
#ifdef PY3K
3032+
n = PyBytes_GET_SIZE(image);
3033+
data = PyBytes_AS_STRING(image);
30233034
#else
30243035
n = PyString_GET_SIZE(image);
3025-
data = PyString_AsString(image);
3036+
data = PyString_AS_STRING(image);
30263037
#endif
30273038

30283039
provider = CGDataProviderCreateWithData(image,
@@ -3039,7 +3050,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
30393050
provider,
30403051
NULL,
30413052
false,
3042-
kCGRenderingIntentDefault);
3053+
kCGRenderingIntentDefault);
30433054
CGColorSpaceRelease(colorspace);
30443055
CGDataProviderRelease(provider);
30453056

@@ -3296,7 +3307,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
32963307
static PyObject*
32973308
FigureCanvas_repr(FigureCanvas* self)
32983309
{
3299-
#if PY_MAJOR_VERSION >= 3
3310+
#if PY3K
33003311
return PyUnicode_FromFormat("FigureCanvas object %p wrapping NSView %p",
33013312
(void*)self, (void*)(self->view));
33023313
#else
@@ -3765,7 +3776,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
37653776
static PyObject*
37663777
FigureManager_repr(FigureManager* self)
37673778
{
3768-
#if PY_MAJOR_VERSION >= 3
3779+
#if PY3K
37693780
return PyUnicode_FromFormat("FigureManager object %p wrapping NSWindow %p",
37703781
(void*) self, (void*)(self->window));
37713782
#else
@@ -4175,7 +4186,7 @@ -(void)save_figure:(id)sender
41754186
static PyObject*
41764187
NavigationToolbar_repr(NavigationToolbar* self)
41774188
{
4178-
#if PY_MAJOR_VERSION >= 3
4189+
#if PY3K
41794190
return PyUnicode_FromFormat("NavigationToolbar object %p", (void*)self);
41804191
#else
41814192
return PyString_FromFormat("NavigationToolbar object %p", (void*)self);
@@ -4286,7 +4297,7 @@ -(void)save_figure:(id)sender
42864297
{
42874298
if(states[i]==1)
42884299
{
4289-
#if PY_MAJOR_VERSION >= 3
4300+
#if PY3K
42904301
PyList_SET_ITEM(list, j, PyLong_FromLong(i));
42914302
#else
42924303
PyList_SET_ITEM(list, j, PyInt_FromLong(i));
@@ -4706,7 +4717,7 @@ -(void)save_figure:(id)sender
47064717
static PyObject*
47074718
NavigationToolbar2_repr(NavigationToolbar2* self)
47084719
{
4709-
#if PY_MAJOR_VERSION >= 3
4720+
#if PY3K
47104721
return PyUnicode_FromFormat("NavigationToolbar2 object %p", (void*)self);
47114722
#else
47124723
return PyString_FromFormat("NavigationToolbar2 object %p", (void*)self);
@@ -4721,7 +4732,12 @@ -(void)save_figure:(id)sender
47214732
{
47224733
const char* message;
47234734

4735+
#if PY3K
4736+
if(!PyArg_ParseTuple(args, "y", &message)) return NULL;
4737+
#else
47244738
if(!PyArg_ParseTuple(args, "s", &message)) return NULL;
4739+
#endif
4740+
47254741
NSText* messagebox = self->messagebox;
47264742

47274743
if (messagebox)
@@ -5100,6 +5116,7 @@ - (void)mouseDown:(NSEvent *)event
51005116
{
51015117
int x, y;
51025118
int num;
5119+
int dblclick = 0;
51035120
PyObject* result;
51045121
PyGILState_STATE gstate;
51055122
NSPoint location = [event locationInWindow];
@@ -5127,8 +5144,11 @@ - (void)mouseDown:(NSEvent *)event
51275144
case NSRightMouseDown: num = 3; break;
51285145
default: return; /* Unknown mouse event */
51295146
}
5147+
if ([event clickCount] == 2) {
5148+
dblclick = 1;
5149+
}
51305150
gstate = PyGILState_Ensure();
5131-
result = PyObject_CallMethod(canvas, "button_press_event", "iii", x, y, num);
5151+
result = PyObject_CallMethod(canvas, "button_press_event", "iiii", x, y, num, dblclick);
51325152
if(result)
51335153
Py_DECREF(result);
51345154
else
@@ -5205,14 +5225,18 @@ - (void)rightMouseDown:(NSEvent *)event
52055225
{
52065226
int x, y;
52075227
int num = 3;
5228+
int dblclick = 0;
52085229
PyObject* result;
52095230
PyGILState_STATE gstate;
52105231
NSPoint location = [event locationInWindow];
52115232
location = [self convertPoint: location fromView: nil];
52125233
x = location.x;
52135234
y = location.y;
52145235
gstate = PyGILState_Ensure();
5215-
result = PyObject_CallMethod(canvas, "button_press_event", "iii", x, y, num);
5236+
if ([event clickCount] == 2) {
5237+
dblclick = 1;
5238+
}
5239+
result = PyObject_CallMethod(canvas, "button_press_event", "iiii", x, y, num, dblclick);
52165240
if(result)
52175241
Py_DECREF(result);
52185242
else
@@ -5262,14 +5286,18 @@ - (void)otherMouseDown:(NSEvent *)event
52625286
{
52635287
int x, y;
52645288
int num = 2;
5289+
int dblclick = 0;
52655290
PyObject* result;
52665291
PyGILState_STATE gstate;
52675292
NSPoint location = [event locationInWindow];
52685293
location = [self convertPoint: location fromView: nil];
52695294
x = location.x;
52705295
y = location.y;
52715296
gstate = PyGILState_Ensure();
5272-
result = PyObject_CallMethod(canvas, "button_press_event", "iii", x, y, num);
5297+
if ([event clickCount] == 2) {
5298+
dblclick = 1;
5299+
}
5300+
result = PyObject_CallMethod(canvas, "button_press_event", "iiii", x, y, num, dblclick);
52735301
if(result)
52745302
Py_DECREF(result);
52755303
else
@@ -5578,7 +5606,11 @@ - (int)index
55785606
static PyObject*
55795607
show(PyObject* self)
55805608
{
5581-
if(nwin > 0) [NSApp run];
5609+
if(nwin > 0)
5610+
{
5611+
[NSApp activateIgnoringOtherApps: YES];
5612+
[NSApp run];
5613+
}
55825614
Py_INCREF(Py_None);
55835615
return Py_None;
55845616
}
@@ -5631,7 +5663,7 @@ - (int)index
56315663
static PyObject*
56325664
Timer_repr(Timer* self)
56335665
{
5634-
#if PY_MAJOR_VERSION >= 3
5666+
#if PY3K
56355667
return PyUnicode_FromFormat("Timer object %p wrapping CFRunLoopTimerRef %p",
56365668
(void*) self, (void*)(self->timer));
56375669
#else
@@ -5812,7 +5844,7 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
58125844
{NULL, NULL, 0, NULL}/* sentinel */
58135845
};
58145846

5815-
#if PY_MAJOR_VERSION >= 3
5847+
#if PY3K
58165848

58175849
static struct PyModuleDef moduledef = {
58185850
PyModuleDef_HEAD_INIT,
@@ -5842,13 +5874,13 @@ void init_macosx(void)
58425874
|| PyType_Ready(&NavigationToolbarType) < 0
58435875
|| PyType_Ready(&NavigationToolbar2Type) < 0
58445876
|| PyType_Ready(&TimerType) < 0)
5845-
#if PY_MAJOR_VERSION >= 3
5877+
#if PY3K
58465878
return NULL;
58475879
#else
58485880
return;
58495881
#endif
58505882

5851-
#if PY_MAJOR_VERSION >= 3
5883+
#if PY3K
58525884
module = PyModule_Create(&moduledef);
58535885
if (module==NULL) return NULL;
58545886
#else
@@ -5873,4 +5905,8 @@ void init_macosx(void)
58735905
PyModule_AddObject(module, "Timer", (PyObject*) &TimerType);
58745906

58755907
PyOS_InputHook = wait_for_stdin;
5908+
5909+
#if PY3K
5910+
return module;
5911+
#endif
58765912
}

0 commit comments

Comments
 (0)