Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 49724bf

Browse filesBrowse files
committed
Construct events with kwargs in macosx.
1 parent e0d65b6 commit 49724bf
Copy full SHA for 49724bf

File tree

Expand file treeCollapse file tree

1 file changed

+76
-39
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+76
-39
lines changed

‎src/_macosx.m

Copy file name to clipboardExpand all lines: src/_macosx.m
+76-39Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,30 @@ static void gil_call_method(PyObject* obj, const char* name)
250250
PyGILState_Release(gstate);
251251
}
252252

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);
266277
}
267278

268279
static bool backend_inited = false;
@@ -1363,7 +1374,9 @@ - (void)updateDevicePixelRatio:(double)scale
13631374
}
13641375
if (PyObject_IsTrue(change)) {
13651376
// 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);
13671380
gil_call_method(canvas, "draw_idle");
13681381
[self setNeedsDisplay: YES];
13691382
}
@@ -1405,7 +1418,9 @@ - (void)windowDidResize: (NSNotification*)notification
14051418

14061419
- (void)windowWillClose:(NSNotification*)notification
14071420
{
1408-
PROCESS_EVENT("CloseEvent", "sO", "close_event", canvas);
1421+
process_event(
1422+
"CloseEvent", "{s:s, s:O}",
1423+
"name", "close_event", "canvas", canvas);
14091424
}
14101425

14111426
- (BOOL)windowShouldClose:(NSNotification*)notification
@@ -1436,7 +1451,9 @@ - (void)mouseEntered:(NSEvent *)event
14361451
location = [self convertPoint: location fromView: nil];
14371452
x = location.x * device_scale;
14381453
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);
14401457
}
14411458

14421459
- (void)mouseExited:(NSEvent *)event
@@ -1446,13 +1463,15 @@ - (void)mouseExited:(NSEvent *)event
14461463
location = [self convertPoint: location fromView: nil];
14471464
x = location.x * device_scale;
14481465
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);
14501469
}
14511470

14521471
- (void)mouseDown:(NSEvent *)event
14531472
{
14541473
int x, y;
1455-
int num;
1474+
int button;
14561475
int dblclick = 0;
14571476
NSPoint location = [event locationInWindow];
14581477
location = [self convertPoint: location fromView: nil];
@@ -1463,49 +1482,53 @@ - (void)mouseDown:(NSEvent *)event
14631482
{ unsigned int modifier = [event modifierFlags];
14641483
if (modifier & NSEventModifierFlagControl)
14651484
/* emulate a right-button click */
1466-
num = 3;
1485+
button = 3;
14671486
else if (modifier & NSEventModifierFlagOption)
14681487
/* emulate a middle-button click */
1469-
num = 2;
1488+
button = 2;
14701489
else
14711490
{
1472-
num = 1;
1491+
button = 1;
14731492
if ([NSCursor currentCursor]==[NSCursor openHandCursor])
14741493
[[NSCursor closedHandCursor] set];
14751494
}
14761495
break;
14771496
}
1478-
case NSEventTypeOtherMouseDown: num = 2; break;
1479-
case NSEventTypeRightMouseDown: num = 3; break;
1497+
case NSEventTypeOtherMouseDown: button = 2; break;
1498+
case NSEventTypeRightMouseDown: button = 3; break;
14801499
default: return; /* Unknown mouse event */
14811500
}
14821501
if ([event clickCount] == 2) {
14831502
dblclick = 1;
14841503
}
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);
14871508
}
14881509

14891510
- (void)mouseUp:(NSEvent *)event
14901511
{
1491-
int num;
1512+
int button;
14921513
int x, y;
14931514
NSPoint location = [event locationInWindow];
14941515
location = [self convertPoint: location fromView: nil];
14951516
x = location.x * device_scale;
14961517
y = location.y * device_scale;
14971518
switch ([event type])
14981519
{ case NSEventTypeLeftMouseUp:
1499-
num = 1;
1520+
button = 1;
15001521
if ([NSCursor currentCursor]==[NSCursor closedHandCursor])
15011522
[[NSCursor openHandCursor] set];
15021523
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;
15051526
default: return; /* Unknown mouse event */
15061527
}
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);
15091532
}
15101533

15111534
- (void)mouseMoved:(NSEvent *)event
@@ -1515,7 +1538,9 @@ - (void)mouseMoved:(NSEvent *)event
15151538
location = [self convertPoint: location fromView: nil];
15161539
x = location.x * device_scale;
15171540
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);
15191544
}
15201545

15211546
- (void)mouseDragged:(NSEvent *)event
@@ -1525,7 +1550,9 @@ - (void)mouseDragged:(NSEvent *)event
15251550
location = [self convertPoint: location fromView: nil];
15261551
x = location.x * device_scale;
15271552
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);
15291556
}
15301557

15311558
- (void)rightMouseDown:(NSEvent *)event { [self mouseDown: event]; }
@@ -1644,9 +1671,13 @@ - (void)keyDown:(NSEvent*)event
16441671
int x = location.x * device_scale,
16451672
y = location.y * device_scale;
16461673
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);
16481677
} 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);
16501681
}
16511682
}
16521683

@@ -1658,9 +1689,13 @@ - (void)keyUp:(NSEvent*)event
16581689
int x = location.x * device_scale,
16591690
y = location.y * device_scale;
16601691
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);
16621695
} 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);
16641699
}
16651700
}
16661701

@@ -1675,8 +1710,10 @@ - (void)scrollWheel:(NSEvent*)event
16751710
NSPoint point = [self convertPoint: location fromView: nil];
16761711
int x = (int)round(point.x * device_scale);
16771712
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);
16801717
}
16811718

16821719
- (BOOL)acceptsFirstResponder

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.