@@ -250,19 +250,30 @@ static void gil_call_method(PyObject* obj, const char* name)
250
250
PyGILState_Release (gstate);
251
251
}
252
252
253
- #define PROCESS_EVENT (cls_name, fmt, ...) \
254
- { \
255
- PyGILState_STATE gstate = PyGILState_Ensure (); \
256
- PyObject* module = NULL , * event = NULL , * result = NULL ; \
257
- if (!(module = PyImport_ImportModule (" matplotlib.backend_bases" )) \
258
- || !(event = PyObject_CallMethod (module, cls_name, fmt, __VA_ARGS__)) \
259
- || !(result = PyObject_CallMethod (event, " _process" , " " ))) { \
260
- PyErr_Print (); \
261
- } \
262
- Py_XDECREF (module); \
263
- Py_XDECREF (event); \
264
- Py_XDECREF (result); \
265
- PyGILState_Release (gstate); \
253
+ void process_event (char const * cls_name, char const * fmt, ...)
254
+ {
255
+ PyGILState_STATE gstate = PyGILState_Ensure ();
256
+ PyObject* module = NULL , * cls = NULL ,
257
+ * args = NULL , * kwargs = NULL ,
258
+ * event = NULL , * result = NULL ;
259
+ va_list argp;
260
+ va_start (argp, fmt);
261
+ if (!(module = PyImport_ImportModule (" matplotlib.backend_bases" ))
262
+ || !(cls = PyObject_GetAttrString (module, cls_name))
263
+ || !(args = PyTuple_New (0 ))
264
+ || !(kwargs = Py_VaBuildValue (fmt, argp))
265
+ || !(event = PyObject_Call (cls, args, kwargs))
266
+ || !(result = PyObject_CallMethod (event, " _process" , " " ))) {
267
+ PyErr_Print ();
268
+ }
269
+ va_end (argp);
270
+ Py_XDECREF (module);
271
+ Py_XDECREF (cls);
272
+ Py_XDECREF (args);
273
+ Py_XDECREF (kwargs);
274
+ Py_XDECREF (event);
275
+ Py_XDECREF (result);
276
+ PyGILState_Release (gstate);
266
277
}
267
278
268
279
static bool backend_inited = false ;
@@ -1363,7 +1374,9 @@ - (void)updateDevicePixelRatio:(double)scale
1363
1374
}
1364
1375
if (PyObject_IsTrue (change)) {
1365
1376
// Notify that there was a resize_event that took place
1366
- PROCESS_EVENT (" ResizeEvent" , " sO" , " resize_event" , canvas);
1377
+ process_event (
1378
+ " ResizeEvent" , " {s:s, s:O}" ,
1379
+ " name" , " resize_event" , " canvas" , canvas);
1367
1380
gil_call_method (canvas, " draw_idle" );
1368
1381
[self setNeedsDisplay: YES ];
1369
1382
}
@@ -1405,7 +1418,9 @@ - (void)windowDidResize: (NSNotification*)notification
1405
1418
1406
1419
- (void )windowWillClose : (NSNotification *)notification
1407
1420
{
1408
- PROCESS_EVENT (" CloseEvent" , " sO" , " close_event" , canvas);
1421
+ process_event (
1422
+ " CloseEvent" , " {s:s, s:O}" ,
1423
+ " name" , " close_event" , " canvas" , canvas);
1409
1424
}
1410
1425
1411
1426
- (BOOL )windowShouldClose : (NSNotification *)notification
@@ -1436,7 +1451,9 @@ - (void)mouseEntered:(NSEvent *)event
1436
1451
location = [self convertPoint: location fromView: nil ];
1437
1452
x = location.x * device_scale;
1438
1453
y = location.y * device_scale;
1439
- PROCESS_EVENT (" LocationEvent" , " sOii" , " figure_enter_event" , canvas, x, y);
1454
+ process_event (
1455
+ " LocationEvent" , " {s:s, s:O, s:i, s:i}" ,
1456
+ " name" , " figure_enter_event" , " canvas" , canvas, " x" , x, " y" , y);
1440
1457
}
1441
1458
1442
1459
- (void )mouseExited : (NSEvent *)event
@@ -1446,13 +1463,15 @@ - (void)mouseExited:(NSEvent *)event
1446
1463
location = [self convertPoint: location fromView: nil ];
1447
1464
x = location.x * device_scale;
1448
1465
y = location.y * device_scale;
1449
- PROCESS_EVENT (" LocationEvent" , " sOii" , " figure_leave_event" , canvas, x, y);
1466
+ process_event (
1467
+ " LocationEvent" , " {s:s, s:O, s:i, s:i}" ,
1468
+ " name" , " figure_leave_event" , " canvas" , canvas, " x" , x, " y" , y);
1450
1469
}
1451
1470
1452
1471
- (void )mouseDown : (NSEvent *)event
1453
1472
{
1454
1473
int x, y;
1455
- int num ;
1474
+ int button ;
1456
1475
int dblclick = 0 ;
1457
1476
NSPoint location = [event locationInWindow ];
1458
1477
location = [self convertPoint: location fromView: nil ];
@@ -1463,49 +1482,53 @@ - (void)mouseDown:(NSEvent *)event
1463
1482
{ unsigned int modifier = [event modifierFlags ];
1464
1483
if (modifier & NSEventModifierFlagControl)
1465
1484
/* emulate a right-button click */
1466
- num = 3 ;
1485
+ button = 3 ;
1467
1486
else if (modifier & NSEventModifierFlagOption)
1468
1487
/* emulate a middle-button click */
1469
- num = 2 ;
1488
+ button = 2 ;
1470
1489
else
1471
1490
{
1472
- num = 1 ;
1491
+ button = 1 ;
1473
1492
if ([NSCursor currentCursor ]==[NSCursor openHandCursor ])
1474
1493
[[NSCursor closedHandCursor ] set ];
1475
1494
}
1476
1495
break ;
1477
1496
}
1478
- case NSEventTypeOtherMouseDown: num = 2 ; break ;
1479
- case NSEventTypeRightMouseDown: num = 3 ; break ;
1497
+ case NSEventTypeOtherMouseDown: button = 2 ; break ;
1498
+ case NSEventTypeRightMouseDown: button = 3 ; break ;
1480
1499
default : return ; /* Unknown mouse event */
1481
1500
}
1482
1501
if ([event clickCount ] == 2 ) {
1483
1502
dblclick = 1 ;
1484
1503
}
1485
- PROCESS_EVENT (" MouseEvent" , " sOiiiOii" , " button_press_event" , canvas,
1486
- x, y, num, Py_None /* key */ , 0 /* step */ , dblclick);
1504
+ process_event (
1505
+ " MouseEvent" , " {s:s, s:O, s:i, s:i, s:i, s:i}" ,
1506
+ " name" , " button_press_event" , " canvas" , canvas, " x" , x, " y" , y,
1507
+ " button" , button, " dblclick" , dblclick);
1487
1508
}
1488
1509
1489
1510
- (void )mouseUp : (NSEvent *)event
1490
1511
{
1491
- int num ;
1512
+ int button ;
1492
1513
int x, y;
1493
1514
NSPoint location = [event locationInWindow ];
1494
1515
location = [self convertPoint: location fromView: nil ];
1495
1516
x = location.x * device_scale;
1496
1517
y = location.y * device_scale;
1497
1518
switch ([event type ])
1498
1519
{ case NSEventTypeLeftMouseUp:
1499
- num = 1 ;
1520
+ button = 1 ;
1500
1521
if ([NSCursor currentCursor ]==[NSCursor closedHandCursor ])
1501
1522
[[NSCursor openHandCursor ] set ];
1502
1523
break ;
1503
- case NSEventTypeOtherMouseUp: num = 2 ; break ;
1504
- case NSEventTypeRightMouseUp: num = 3 ; break ;
1524
+ case NSEventTypeOtherMouseUp: button = 2 ; break ;
1525
+ case NSEventTypeRightMouseUp: button = 3 ; break ;
1505
1526
default : return ; /* Unknown mouse event */
1506
1527
}
1507
- PROCESS_EVENT (" MouseEvent" , " sOiii" , " button_release_event" , canvas,
1508
- x, y, num);
1528
+ process_event (
1529
+ " MouseEvent" , " {s:s, s:O, s:i, s:i, s:i}" ,
1530
+ " name" , " button_release_event" , " canvas" , canvas, " x" , x, " y" , y,
1531
+ " button" , button);
1509
1532
}
1510
1533
1511
1534
- (void )mouseMoved : (NSEvent *)event
@@ -1515,7 +1538,9 @@ - (void)mouseMoved:(NSEvent *)event
1515
1538
location = [self convertPoint: location fromView: nil ];
1516
1539
x = location.x * device_scale;
1517
1540
y = location.y * device_scale;
1518
- PROCESS_EVENT (" MouseEvent" , " sOii" , " motion_notify_event" , canvas, x, y);
1541
+ process_event (
1542
+ " MouseEvent" , " {s:s, s:O, s:i, s:i}" ,
1543
+ " name" , " motion_notify_event" , " canvas" , canvas, " x" , x, " y" , y);
1519
1544
}
1520
1545
1521
1546
- (void )mouseDragged : (NSEvent *)event
@@ -1525,7 +1550,9 @@ - (void)mouseDragged:(NSEvent *)event
1525
1550
location = [self convertPoint: location fromView: nil ];
1526
1551
x = location.x * device_scale;
1527
1552
y = location.y * device_scale;
1528
- PROCESS_EVENT (" MouseEvent" , " sOii" , " motion_notify_event" , canvas, x, y);
1553
+ process_event (
1554
+ " MouseEvent" , " {s:s, s:O, s:i, s:i}" ,
1555
+ " name" , " motion_notify_event" , " canvas" , canvas, " x" , x, " y" , y);
1529
1556
}
1530
1557
1531
1558
- (void )rightMouseDown : (NSEvent *)event { [self mouseDown: event]; }
@@ -1644,9 +1671,13 @@ - (void)keyDown:(NSEvent*)event
1644
1671
int x = location.x * device_scale,
1645
1672
y = location.y * device_scale;
1646
1673
if (s) {
1647
- PROCESS_EVENT (" KeyEvent" , " sOsii" , " key_press_event" , canvas, s, x, y);
1674
+ process_event (
1675
+ " KeyEvent" , " {s:s, s:O, s:s, s:i, s:i}" ,
1676
+ " name" , " key_press_event" , " canvas" , canvas, " key" , s, " x" , x, " y" , y);
1648
1677
} else {
1649
- PROCESS_EVENT (" KeyEvent" , " sOOii" , " key_press_event" , canvas, Py_None, x, y);
1678
+ process_event (
1679
+ " KeyEvent" , " {s:s, s:O, s:O, s:i, s:i}" ,
1680
+ " name" , " key_press_event" , " canvas" , canvas, " key" , Py_None, " x" , x, " y" , y);
1650
1681
}
1651
1682
}
1652
1683
@@ -1658,9 +1689,13 @@ - (void)keyUp:(NSEvent*)event
1658
1689
int x = location.x * device_scale,
1659
1690
y = location.y * device_scale;
1660
1691
if (s) {
1661
- PROCESS_EVENT (" KeyEvent" , " sOsii" , " key_release_event" , canvas, s, x, y);
1692
+ process_event (
1693
+ " KeyEvent" , " {s:s, s:O, s:s, s:i, s:i}" ,
1694
+ " name" , " key_release_event" , " canvas" , canvas, " key" , s, " x" , x, " y" , y);
1662
1695
} else {
1663
- PROCESS_EVENT (" KeyEvent" , " sOOii" , " key_release_event" , canvas, Py_None, x, y);
1696
+ process_event (
1697
+ " KeyEvent" , " {s:s, s:O, s:O, s:i, s:i}" ,
1698
+ " name" , " key_release_event" , " canvas" , canvas, " key" , Py_None, " x" , x, " y" , y);
1664
1699
}
1665
1700
}
1666
1701
@@ -1675,8 +1710,10 @@ - (void)scrollWheel:(NSEvent*)event
1675
1710
NSPoint point = [self convertPoint: location fromView: nil ];
1676
1711
int x = (int )round (point.x * device_scale);
1677
1712
int y = (int )round (point.y * device_scale - 1 );
1678
- PROCESS_EVENT (" MouseEvent" , " sOiiOOi" , " scroll_event" , canvas,
1679
- x, y, Py_None /* button */ , Py_None /* key */ , step);
1713
+ process_event (
1714
+ " MouseEvent" , " {s:s, s:O, s:i, s:i, s:i}" ,
1715
+ " name" , " scroll_event" , " canvas" , canvas,
1716
+ " x" , x, " y" , y, " step" , step);
1680
1717
}
1681
1718
1682
1719
- (BOOL )acceptsFirstResponder
0 commit comments