Roll src/third_party/WebKit 4526c29:30f3df0 (svn 190187:190189)
[chromium-blink-merge.git] / ui / base / cocoa / nsview_additions.mm
blobf111b140248450af2e0d2d239ad6655264199f61
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/mac/sdk_forward_declarations.h"
6 #import "ui/base/cocoa/nsview_additions.h"
7 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
9 #include "base/logging.h"
11 @implementation NSView (ChromeAdditions)
13 - (CGFloat)cr_lineWidth {
14   // All shipping retina macs run at least 10.7.
15   if (![self respondsToSelector:@selector(convertSizeFromBacking:)])
16     return 1;
17   return [self convertSizeFromBacking:NSMakeSize(1, 1)].width;
20 - (BOOL)cr_isMouseInView {
21   NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
22   mouseLoc = [[self superview] convertPoint:mouseLoc fromView:nil];
23   return [self hitTest:mouseLoc] == self;
26 - (BOOL)cr_isBelowView:(NSView*)otherView {
27   NSArray* subviews = [[self superview] subviews];
29   NSUInteger selfIndex = [subviews indexOfObject:self];
30   DCHECK_NE(NSNotFound, selfIndex);
32   NSUInteger otherIndex = [subviews indexOfObject:otherView];
33   DCHECK_NE(NSNotFound, otherIndex);
35   return selfIndex < otherIndex;
38 - (BOOL)cr_isAboveView:(NSView*)otherView {
39   return ![self cr_isBelowView:otherView];
42 - (void)cr_ensureSubview:(NSView*)subview
43             isPositioned:(NSWindowOrderingMode)place
44               relativeTo:(NSView *)otherView {
45   DCHECK(place == NSWindowAbove || place == NSWindowBelow);
46   BOOL isAbove = place == NSWindowAbove;
47   if ([[subview superview] isEqual:self] &&
48       [subview cr_isAboveView:otherView] == isAbove) {
49     return;
50   }
52   [subview removeFromSuperview];
53   [self addSubview:subview
54         positioned:place
55         relativeTo:otherView];
58 - (NSColor*)cr_keyboardFocusIndicatorColor {
59   return [[NSColor keyboardFocusIndicatorColor]
60       colorWithAlphaComponent:0.5 / [self cr_lineWidth]];
63 - (void)cr_recursivelyInvokeBlock:(void (^)(id view))block {
64   block(self);
65   for (NSView* subview in [self subviews])
66     [subview cr_recursivelyInvokeBlock:block];
69 - (void)cr_recursivelySetNeedsDisplay:(BOOL)flag {
70   [self setNeedsDisplay:YES];
71   for (NSView* child in [self subviews])
72     [child cr_recursivelySetNeedsDisplay:flag];
75 static NSView* g_ancestorBeingDrawnFrom = nil;
76 static NSView* g_childBeingDrawnTo = nil;
78 - (void)cr_drawUsingAncestor:(NSView*)ancestorView inRect:(NSRect)rect {
79   gfx::ScopedNSGraphicsContextSaveGState scopedGSState;
80   NSRect frame = [self convertRect:[self bounds] toView:ancestorView];
81   NSAffineTransform* transform = [NSAffineTransform transform];
82   if ([self isFlipped] == [ancestorView isFlipped]) {
83     [transform translateXBy:-NSMinX(frame) yBy:-NSMinY(frame)];
84   } else {
85     [transform translateXBy:-NSMinX(frame) yBy:NSMaxY(frame)];
86     [transform scaleXBy:1.0 yBy:-1.0];
87   }
88   [transform concat];
90   // This can be made robust to recursive calls, but is as of yet unneeded.
91   DCHECK(!g_ancestorBeingDrawnFrom && !g_childBeingDrawnTo);
92   g_ancestorBeingDrawnFrom = ancestorView;
93   g_childBeingDrawnTo = self;
94   [ancestorView drawRect:[ancestorView bounds]];
95   g_childBeingDrawnTo = nil;
96   g_ancestorBeingDrawnFrom = nil;
99 - (NSView*)cr_viewBeingDrawnTo {
100   if (!g_ancestorBeingDrawnFrom)
101     return self;
102   DCHECK(g_ancestorBeingDrawnFrom == self);
103   return g_childBeingDrawnTo;
106 @end