5
5
#include " numpy/arrayobject.h"
6
6
#include " path_cleanup.h"
7
7
8
+ #if PY_MAJOR_VERSION >= 3
9
+ #define PY3K 1
10
+ #else
11
+ #define PY3K 0
12
+ #endif
13
+
8
14
/* Must define Py_TYPE for Python 2.5 or older */
9
15
#ifndef Py_TYPE
10
16
# define Py_TYPE (o ) ((o)->ob_type)
@@ -506,7 +512,7 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
506
512
static PyObject*
507
513
GraphicsContext_repr (GraphicsContext* self)
508
514
{
509
- #if PY_MAJOR_VERSION >= 3
515
+ #if PY3K
510
516
return PyUnicode_FromFormat (" GraphicsContext object %p wrapping the Quartz 2D graphics context %p " , (void *)self, (void *)(self->cr ));
511
517
#else
512
518
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)
713
719
714
720
if (offset!=Py_None)
715
721
{
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
718
728
else
719
729
{
720
730
PyErr_SetString (PyExc_TypeError,
@@ -747,8 +757,13 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
747
757
PyObject* value = PyTuple_GET_ITEM (dashes, i);
748
758
if (PyFloat_Check (value))
749
759
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
750
764
else if (PyInt_Check (value))
751
765
lengths[i] = (CGFloat ) PyInt_AS_LONG (value);
766
+ #endif
752
767
else break ;
753
768
}
754
769
Py_DECREF (dashes);
@@ -2252,7 +2267,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2252
2267
#else
2253
2268
ATSFontRef font = 0 ;
2254
2269
#endif
2255
- #if PY_MAJOR_VERSION >= 3
2270
+ #if PY3K
2256
2271
PyObject* ascii = NULL ;
2257
2272
#endif
2258
2273
@@ -2435,7 +2450,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2435
2450
for (i = 0 ; i < n; i++)
2436
2451
{
2437
2452
PyObject* item = PyList_GET_ITEM (family, i);
2438
- #if PY_MAJOR_VERSION >= 3
2453
+ #if PY3K
2439
2454
ascii = PyUnicode_AsASCIIString (item);
2440
2455
if (!ascii) return 0 ;
2441
2456
temp = PyBytes_AS_STRING (ascii);
@@ -2469,7 +2484,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2469
2484
name = temp;
2470
2485
break ;
2471
2486
}
2472
- #if PY_MAJOR_VERSION >= 3
2487
+ #if PY3K
2473
2488
Py_DECREF (ascii);
2474
2489
ascii = NULL ;
2475
2490
#endif
@@ -2488,7 +2503,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2488
2503
#ifndef COMPILING_FOR_10_5
2489
2504
CGContextSelectFont (cr, name, size, kCGEncodingMacRoman );
2490
2505
#endif
2491
- #if PY_MAJOR_VERSION >= 3
2506
+ #if PY3K
2492
2507
Py_XDECREF (ascii);
2493
2508
#endif
2494
2509
return font;
@@ -2989,19 +3004,15 @@ static void _data_provider_release(void* info, const void* data, size_t size)
2989
3004
CGDataProviderRef provider;
2990
3005
double rect[4 ] = {0.0 , 0.0 , self->size .width , self->size .height };
2991
3006
2992
- #if PY_MAJOR_VERSION >= 3
2993
3007
if (!PyBytes_Check (image))
2994
3008
{
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" );
2998
3011
#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
3002
3014
return NULL ;
3003
3015
}
3004
- #endif
3005
3016
3006
3017
const size_t bytesPerComponent = 1 ;
3007
3018
const size_t bitsPerComponent = 8 * bytesPerComponent;
@@ -3017,12 +3028,12 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3017
3028
}
3018
3029
3019
3030
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);
3023
3034
#else
3024
3035
n = PyString_GET_SIZE (image);
3025
- data = PyString_AsString (image);
3036
+ data = PyString_AS_STRING (image);
3026
3037
#endif
3027
3038
3028
3039
provider = CGDataProviderCreateWithData (image,
@@ -3039,7 +3050,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3039
3050
provider,
3040
3051
NULL ,
3041
3052
false ,
3042
- kCGRenderingIntentDefault );
3053
+ kCGRenderingIntentDefault );
3043
3054
CGColorSpaceRelease (colorspace);
3044
3055
CGDataProviderRelease (provider);
3045
3056
@@ -3296,7 +3307,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3296
3307
static PyObject*
3297
3308
FigureCanvas_repr (FigureCanvas* self)
3298
3309
{
3299
- #if PY_MAJOR_VERSION >= 3
3310
+ #if PY3K
3300
3311
return PyUnicode_FromFormat (" FigureCanvas object %p wrapping NSView %p " ,
3301
3312
(void *)self, (void *)(self->view ));
3302
3313
#else
@@ -3765,7 +3776,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3765
3776
static PyObject*
3766
3777
FigureManager_repr (FigureManager* self)
3767
3778
{
3768
- #if PY_MAJOR_VERSION >= 3
3779
+ #if PY3K
3769
3780
return PyUnicode_FromFormat (" FigureManager object %p wrapping NSWindow %p " ,
3770
3781
(void *) self, (void *)(self->window ));
3771
3782
#else
@@ -4175,7 +4186,7 @@ -(void)save_figure:(id)sender
4175
4186
static PyObject*
4176
4187
NavigationToolbar_repr (NavigationToolbar* self)
4177
4188
{
4178
- #if PY_MAJOR_VERSION >= 3
4189
+ #if PY3K
4179
4190
return PyUnicode_FromFormat (" NavigationToolbar object %p " , (void *)self);
4180
4191
#else
4181
4192
return PyString_FromFormat (" NavigationToolbar object %p " , (void *)self);
@@ -4286,7 +4297,7 @@ -(void)save_figure:(id)sender
4286
4297
{
4287
4298
if (states[i]==1 )
4288
4299
{
4289
- #if PY_MAJOR_VERSION >= 3
4300
+ #if PY3K
4290
4301
PyList_SET_ITEM (list, j, PyLong_FromLong (i));
4291
4302
#else
4292
4303
PyList_SET_ITEM (list, j, PyInt_FromLong (i));
@@ -4706,7 +4717,7 @@ -(void)save_figure:(id)sender
4706
4717
static PyObject*
4707
4718
NavigationToolbar2_repr (NavigationToolbar2* self)
4708
4719
{
4709
- #if PY_MAJOR_VERSION >= 3
4720
+ #if PY3K
4710
4721
return PyUnicode_FromFormat (" NavigationToolbar2 object %p " , (void *)self);
4711
4722
#else
4712
4723
return PyString_FromFormat (" NavigationToolbar2 object %p " , (void *)self);
@@ -4721,7 +4732,12 @@ -(void)save_figure:(id)sender
4721
4732
{
4722
4733
const char * message;
4723
4734
4735
+ #if PY3K
4736
+ if (!PyArg_ParseTuple (args, " y" , &message)) return NULL ;
4737
+ #else
4724
4738
if (!PyArg_ParseTuple (args, " s" , &message)) return NULL ;
4739
+ #endif
4740
+
4725
4741
NSText * messagebox = self->messagebox ;
4726
4742
4727
4743
if (messagebox)
@@ -5100,6 +5116,7 @@ - (void)mouseDown:(NSEvent *)event
5100
5116
{
5101
5117
int x, y;
5102
5118
int num;
5119
+ int dblclick = 0 ;
5103
5120
PyObject* result;
5104
5121
PyGILState_STATE gstate;
5105
5122
NSPoint location = [event locationInWindow ];
@@ -5127,8 +5144,11 @@ - (void)mouseDown:(NSEvent *)event
5127
5144
case NSRightMouseDown : num = 3 ; break ;
5128
5145
default : return ; /* Unknown mouse event */
5129
5146
}
5147
+ if ([event clickCount ] == 2 ) {
5148
+ dblclick = 1 ;
5149
+ }
5130
5150
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 );
5132
5152
if (result)
5133
5153
Py_DECREF (result);
5134
5154
else
@@ -5205,14 +5225,18 @@ - (void)rightMouseDown:(NSEvent *)event
5205
5225
{
5206
5226
int x, y;
5207
5227
int num = 3 ;
5228
+ int dblclick = 0 ;
5208
5229
PyObject* result;
5209
5230
PyGILState_STATE gstate;
5210
5231
NSPoint location = [event locationInWindow ];
5211
5232
location = [self convertPoint: location fromView: nil ];
5212
5233
x = location.x ;
5213
5234
y = location.y ;
5214
5235
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);
5216
5240
if (result)
5217
5241
Py_DECREF (result);
5218
5242
else
@@ -5262,14 +5286,18 @@ - (void)otherMouseDown:(NSEvent *)event
5262
5286
{
5263
5287
int x, y;
5264
5288
int num = 2 ;
5289
+ int dblclick = 0 ;
5265
5290
PyObject* result;
5266
5291
PyGILState_STATE gstate;
5267
5292
NSPoint location = [event locationInWindow ];
5268
5293
location = [self convertPoint: location fromView: nil ];
5269
5294
x = location.x ;
5270
5295
y = location.y ;
5271
5296
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);
5273
5301
if (result)
5274
5302
Py_DECREF (result);
5275
5303
else
@@ -5578,7 +5606,11 @@ - (int)index
5578
5606
static PyObject*
5579
5607
show (PyObject* self)
5580
5608
{
5581
- if (nwin > 0 ) [NSApp run ];
5609
+ if (nwin > 0 )
5610
+ {
5611
+ [NSApp activateIgnoringOtherApps: YES ];
5612
+ [NSApp run ];
5613
+ }
5582
5614
Py_INCREF (Py_None);
5583
5615
return Py_None;
5584
5616
}
@@ -5631,7 +5663,7 @@ - (int)index
5631
5663
static PyObject*
5632
5664
Timer_repr (Timer* self)
5633
5665
{
5634
- #if PY_MAJOR_VERSION >= 3
5666
+ #if PY3K
5635
5667
return PyUnicode_FromFormat (" Timer object %p wrapping CFRunLoopTimerRef %p " ,
5636
5668
(void *) self, (void *)(self->timer ));
5637
5669
#else
@@ -5812,7 +5844,7 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
5812
5844
{NULL , NULL , 0 , NULL }/* sentinel */
5813
5845
};
5814
5846
5815
- #if PY_MAJOR_VERSION >= 3
5847
+ #if PY3K
5816
5848
5817
5849
static struct PyModuleDef moduledef = {
5818
5850
PyModuleDef_HEAD_INIT,
@@ -5842,13 +5874,13 @@ void init_macosx(void)
5842
5874
|| PyType_Ready (&NavigationToolbarType) < 0
5843
5875
|| PyType_Ready (&NavigationToolbar2Type) < 0
5844
5876
|| PyType_Ready (&TimerType) < 0 )
5845
- #if PY_MAJOR_VERSION >= 3
5877
+ #if PY3K
5846
5878
return NULL ;
5847
5879
#else
5848
5880
return ;
5849
5881
#endif
5850
5882
5851
- #if PY_MAJOR_VERSION >= 3
5883
+ #if PY3K
5852
5884
module = PyModule_Create (&moduledef);
5853
5885
if (module==NULL ) return NULL ;
5854
5886
#else
@@ -5873,4 +5905,8 @@ void init_macosx(void)
5873
5905
PyModule_AddObject (module, " Timer" , (PyObject*) &TimerType);
5874
5906
5875
5907
PyOS_InputHook = wait_for_stdin;
5908
+
5909
+ #if PY3K
5910
+ return module;
5911
+ #endif
5876
5912
}
0 commit comments