Skip to content

Use Agg for rendering in the Mac OSX backend #6178

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 6 commits into from
Mar 28, 2016
Merged
Changes from 1 commit
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
Prev Previous commit
Fix scaling of mouse events
  • Loading branch information
mdboom committed Mar 21, 2016
commit 79be27ddeebd639ff99aa7c05fe104d10907e07f
64 changes: 38 additions & 26 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ @interface View : NSView
NSRect rubberband;
BOOL inside;
NSTrackingRectTag tracking;
@public double device_scale;
}
- (void)dealloc;
- (void)drawRect:(NSRect)rect;
Expand Down Expand Up @@ -407,6 +408,12 @@ static CGFloat _get_device_scale(CGContextRef cr)
return NULL;
}
if(!PyArg_ParseTuple(args, "iiii", &x0, &y0, &x1, &y1)) return NULL;

x0 /= view->device_scale;
x1 /= view->device_scale;
y0 /= view->device_scale;
y1 /= view->device_scale;

if (x1 > x0)
{
rubberband.origin.x = x0;
Expand All @@ -427,6 +434,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
rubberband.origin.y = y1;
rubberband.size.height = y0 - y1;
}

[view setRubberband: rubberband];
Py_INCREF(Py_None);
return Py_None;
Expand Down Expand Up @@ -2085,6 +2093,7 @@ - (View*)initWithFrame:(NSRect)rect
rubberband = NSZeroRect;
inside = false;
tracking = 0;
device_scale = 1;
return self;
}

Expand Down Expand Up @@ -2181,11 +2190,14 @@ -(void)drawRect:(NSRect)rect

CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];

double device_scale = _get_device_scale(cr);
double new_device_scale = _get_device_scale(cr);

if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
PyErr_Print();
goto exit;
if (device_scale != new_device_scale) {
device_scale = new_device_scale;
if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
PyErr_Print();
goto exit;
}
}

renderer = PyObject_CallMethod(canvas, "_draw", "", NULL);
Expand Down Expand Up @@ -2330,8 +2342,8 @@ - (void)mouseDown:(NSEvent *)event
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
switch ([event type])
{ case NSLeftMouseDown:
{ unsigned int modifier = [event modifierFlags];
Expand Down Expand Up @@ -2374,8 +2386,8 @@ - (void)mouseUp:(NSEvent *)event
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
switch ([event type])
{ case NSLeftMouseUp:
num = 1;
Expand All @@ -2401,8 +2413,8 @@ - (void)mouseMoved:(NSEvent *)event
int x, y;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
PyGILState_STATE gstate = PyGILState_Ensure();
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
if(result)
Expand All @@ -2418,8 +2430,8 @@ - (void)mouseDragged:(NSEvent *)event
int x, y;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
PyGILState_STATE gstate = PyGILState_Ensure();
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
if(result)
Expand All @@ -2439,8 +2451,8 @@ - (void)rightMouseDown:(NSEvent *)event
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
gstate = PyGILState_Ensure();
if ([event clickCount] == 2) {
dblclick = 1;
Expand All @@ -2462,8 +2474,8 @@ - (void)rightMouseUp:(NSEvent *)event
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
gstate = PyGILState_Ensure();
result = PyObject_CallMethod(canvas, "button_release_event", "iii", x, y, num);
if(result)
Expand All @@ -2479,8 +2491,8 @@ - (void)rightMouseDragged:(NSEvent *)event
int x, y;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
PyGILState_STATE gstate = PyGILState_Ensure();
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
if(result)
Expand All @@ -2500,8 +2512,8 @@ - (void)otherMouseDown:(NSEvent *)event
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
gstate = PyGILState_Ensure();
if ([event clickCount] == 2) {
dblclick = 1;
Expand All @@ -2523,8 +2535,8 @@ - (void)otherMouseUp:(NSEvent *)event
PyGILState_STATE gstate;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
gstate = PyGILState_Ensure();
result = PyObject_CallMethod(canvas, "button_release_event", "iii", x, y, num);
if(result)
Expand All @@ -2540,8 +2552,8 @@ - (void)otherMouseDragged:(NSEvent *)event
int x, y;
NSPoint location = [event locationInWindow];
location = [self convertPoint: location fromView: nil];
x = location.x;
y = location.y;
x = location.x * device_scale;
y = location.y * device_scale;
PyGILState_STATE gstate = PyGILState_Ensure();
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
if(result)
Expand Down Expand Up @@ -2680,8 +2692,8 @@ - (void)scrollWheel:(NSEvent*)event
else return;
NSPoint location = [event locationInWindow];
NSPoint point = [self convertPoint: location fromView: nil];
int x = (int)round(point.x);
int y = (int)round(point.y - 1);
int x = (int)round(point.x * device_scale);
int y = (int)round(point.y * device_scale - 1);

PyObject* result;
PyGILState_STATE gstate = PyGILState_Ensure();
Expand Down