From 9a6e6b2fc24cce5bed2831d892fbaf91f7926b53 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 17 Apr 2013 21:22:22 +0200 Subject: cocoa_common: keep aspect ratio when clipping window With commit 33ffc283 resizing to double size would cause the window to lose aspect ratio. This commit fixes this bug. --- video/out/cocoa_common.m | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/video/out/cocoa_common.m b/video/out/cocoa_common.m index 836373e051..ee451ed1dc 100644 --- a/video/out/cocoa_common.m +++ b/video/out/cocoa_common.m @@ -92,6 +92,8 @@ static bool RightAltPressed(NSEvent *event) - (void)fullscreen; - (void)mouseEvent:(NSEvent *)theEvent; - (void)mulSize:(float)multiplier; +- (int)titleHeight; +- (NSRect)clipFrame:(NSRect)frame withContentAspect:(NSSize) aspect; - (void)setContentSize:(NSSize)newSize keepCentered:(BOOL)keepCentered; @end @@ -903,21 +905,47 @@ void create_menu() } } +- (int)titleHeight +{ + NSRect of = [self frame]; + NSRect cb = [[self contentView] bounds]; + return of.size.height - cb.size.height; +} + +- (NSRect)clipFrame:(NSRect)frame withContentAspect:(NSSize) aspect +{ + NSRect vf = [[self screen] visibleFrame]; + double ratio = (double)aspect.width / (double)aspect.height; + + // clip frame to screens visibile frame + frame = CGRectIntersection(frame, vf); + + NSSize s = frame.size; + s.height -= [self titleHeight]; + + if (s.width > s.height) { + s.width = ((double)s.height * ratio + 0.5); + } else { + s.height = ((double)s.width * 1/ratio + 0.5); + } + + s.height += [self titleHeight]; + frame.size = s; + + return frame; +} + - (void)setCenteredContentSize:(NSSize)ns { #define get_center(x) NSMakePoint(CGRectGetMidX((x)), CGRectGetMidY((x))) NSRect of = [self frame]; NSRect vf = [[self screen] visibleFrame]; - NSRect cb = [[self contentView] bounds]; - int title_h = of.size.height - cb.size.height; - NSPoint old_center = get_center(of); - NSRect nf = - NSMakeRect(vf.origin.x, vf.origin.y, ns.width, ns.height + title_h); + NSRect nf = NSMakeRect(vf.origin.x, vf.origin.y, + ns.width, ns.height + [self titleHeight]); - // clip frame to screens visibile frame - nf = CGRectIntersection(nf, vf); + nf = [self clipFrame:nf withContentAspect:ns]; NSPoint new_center = get_center(nf); -- cgit v1.2.3