1 // Copyright (c) 2011 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 "ui/base/cocoa/base_view.h"
7 #include "base/mac/mac_util.h"
9 NSString* kViewDidBecomeFirstResponder =
10 @"Chromium.kViewDidBecomeFirstResponder";
11 NSString* kSelectionDirection = @"Chromium.kSelectionDirection";
13 @implementation BaseView
15 - (instancetype)initWithFrame:(NSRect)frame {
16 if ((self = [super initWithFrame:frame])) {
17 [self enableTracking];
22 - (instancetype)initWithCoder:(NSCoder*)decoder {
23 if ((self = [super initWithCoder:decoder])) {
24 [self enableTracking];
30 [self disableTracking];
34 - (void)enableTracking {
35 if (trackingArea_.get())
38 NSTrackingAreaOptions trackingOptions = NSTrackingMouseEnteredAndExited |
39 NSTrackingMouseMoved |
40 NSTrackingActiveAlways |
41 NSTrackingInVisibleRect;
42 trackingArea_.reset([[CrTrackingArea alloc] initWithRect:NSZeroRect
43 options:trackingOptions
46 [self addTrackingArea:trackingArea_.get()];
49 - (void)disableTracking {
50 if (trackingArea_.get()) {
51 [self removeTrackingArea:trackingArea_.get()];
52 trackingArea_.reset();
56 - (void)updateTrackingAreas {
57 [super updateTrackingAreas];
59 // NSTrackingInVisibleRect doesn't work correctly with Lion's window resizing,
60 // http://crbug.com/176725 / http://openradar.appspot.com/radar?id=2773401 .
61 // Work around it by reinstalling the tracking area after window resize.
62 // This AppKit bug is fixed on Yosemite, so we only apply this workaround on
64 if (base::mac::IsOSMavericksOrEarlier() && base::mac::IsOSLionOrLater()) {
65 [self disableTracking];
66 [self enableTracking];
70 - (void)mouseEvent:(NSEvent*)theEvent {
71 // This method left intentionally blank.
74 - (EventHandled)keyEvent:(NSEvent*)theEvent {
75 // The default implementation of this method does not handle any key events.
76 // Derived classes should return kEventHandled if they handled an event,
77 // otherwise it will be forwarded on to |super|.
78 return kEventNotHandled;
81 - (void)mouseDown:(NSEvent*)theEvent {
83 [self mouseEvent:theEvent];
86 - (void)rightMouseDown:(NSEvent*)theEvent {
87 [self mouseEvent:theEvent];
90 - (void)otherMouseDown:(NSEvent*)theEvent {
91 [self mouseEvent:theEvent];
94 - (void)mouseUp:(NSEvent*)theEvent {
95 [self mouseEvent:theEvent];
98 if (pendingExitEvent_.get()) {
100 [NSEvent enterExitEventWithType:NSMouseExited
101 location:[theEvent locationInWindow]
102 modifierFlags:[theEvent modifierFlags]
103 timestamp:[theEvent timestamp]
104 windowNumber:[theEvent windowNumber]
105 context:[theEvent context]
106 eventNumber:[pendingExitEvent_.get() eventNumber]
107 trackingNumber:[pendingExitEvent_.get() trackingNumber]
108 userData:[pendingExitEvent_.get() userData]];
109 [self mouseEvent:exitEvent];
110 pendingExitEvent_.reset();
114 - (void)rightMouseUp:(NSEvent*)theEvent {
115 [self mouseEvent:theEvent];
118 - (void)otherMouseUp:(NSEvent*)theEvent {
119 [self mouseEvent:theEvent];
122 - (void)mouseMoved:(NSEvent*)theEvent {
123 [self mouseEvent:theEvent];
126 - (void)mouseDragged:(NSEvent*)theEvent {
127 [self mouseEvent:theEvent];
130 - (void)rightMouseDragged:(NSEvent*)theEvent {
131 [self mouseEvent:theEvent];
134 - (void)otherMouseDragged:(NSEvent*)theEvent {
135 [self mouseEvent:theEvent];
138 - (void)mouseEntered:(NSEvent*)theEvent {
139 if (pendingExitEvent_.get()) {
140 pendingExitEvent_.reset();
144 [self mouseEvent:theEvent];
147 - (void)mouseExited:(NSEvent*)theEvent {
148 // The tracking area will send an exit event even during a drag, which isn't
149 // how the event flow for drags should work. This stores the exit event, and
150 // sends it when the drag completes instead.
152 pendingExitEvent_.reset([theEvent retain]);
156 [self mouseEvent:theEvent];
159 - (void)keyDown:(NSEvent*)theEvent {
160 if ([self keyEvent:theEvent] != kEventHandled)
161 [super keyDown:theEvent];
164 - (void)keyUp:(NSEvent*)theEvent {
165 if ([self keyEvent:theEvent] != kEventHandled)
166 [super keyUp:theEvent];
169 - (void)flagsChanged:(NSEvent*)theEvent {
170 if ([self keyEvent:theEvent] != kEventHandled)
171 [super flagsChanged:theEvent];
174 - (gfx::Rect)flipNSRectToRect:(NSRect)rect {
175 gfx::Rect new_rect(NSRectToCGRect(rect));
176 new_rect.set_y(NSHeight([self bounds]) - new_rect.bottom());
180 - (NSRect)flipRectToNSRect:(gfx::Rect)rect {
181 NSRect new_rect(NSRectFromCGRect(rect.ToCGRect()));
182 new_rect.origin.y = NSHeight([self bounds]) - NSMaxY(new_rect);