OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #import "ui/views/cocoa/bridged_content_view.h" |
| 6 |
| 7 #include "ui/gfx/mac/point_utils.h" |
| 8 #include "ui/accessibility/ax_view_state.h" |
| 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/canvas_paint_mac.h" |
| 11 #include "ui/views/view.h" |
| 12 #include "ui/views/widget/widget.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 template <class OSTREAM> |
| 17 OSTREAM& operator<<(OSTREAM& out, const NSRect& rect) { |
| 18 CGFloat x = rect.origin.x; |
| 19 CGFloat y = rect.origin.y; |
| 20 out << '[' << rect.size.width << 'x' << rect.size.height |
| 21 << (x < 0 ? '-' : '+') << x << (y < 0 ? '-' : '+') << y << ']'; |
| 22 return out; |
| 23 } |
| 24 |
| 25 template <class OSTREAM> |
| 26 OSTREAM& operator<<(OSTREAM& out, const NSPoint& point) { |
| 27 out << '(' << point.x << ',' << point.y << ')'; |
| 28 return out; |
| 29 } |
| 30 } |
| 31 |
| 32 @implementation BridgedContentView |
| 33 |
| 34 - (id)initWithView:(views::View*)viewToHost { |
| 35 DCHECK(viewToHost); |
| 36 NSRect initialFrame = ScreenRectToNSRect(viewToHost->bounds()); |
| 37 if ((self = [super initWithFrame:initialFrame])) { |
| 38 hostedView_ = viewToHost; |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 - (views::View*)view { |
| 44 return hostedView_; |
| 45 } |
| 46 |
| 47 - (void)clearView { |
| 48 hostedView_ = NULL; |
| 49 } |
| 50 |
| 51 // BridgedContentView private implementation. |
| 52 |
| 53 - (void)handleMouseEvent:(NSEvent*)theEvent { |
| 54 if (!hostedView_) |
| 55 return; |
| 56 |
| 57 ui::MouseEvent event(theEvent); |
| 58 |
| 59 NSPoint windowLocation = [theEvent locationInWindow]; |
| 60 NSPoint viewLocation = [self convertPoint:windowLocation fromView:nil]; |
| 61 event.set_location(event.location() + |
| 62 gfx::Vector2dF(viewLocation.x - windowLocation.x, |
| 63 viewLocation.y - windowLocation.y)); |
| 64 |
| 65 { |
| 66 gfx::Point uiViewLocation = event.location(); |
| 67 views::View::ConvertPointFromWidget(hostedView_, &uiViewLocation); |
| 68 |
| 69 DLOG(INFO) << "Forwarding mouse event in " << [self frame] << " " |
| 70 << hostedView_->GetClassName() << ":" |
| 71 << " window" << windowLocation << " view" << viewLocation |
| 72 << " ui.Location: " << event.location_f().ToString() |
| 73 << " ui.ViewLocation: " << uiViewLocation.ToString(); |
| 74 } |
| 75 |
| 76 hostedView_->GetWidget()->OnMouseEvent(&event); |
| 77 } |
| 78 |
| 79 // NSView implementation. |
| 80 |
| 81 - (void)setFrame:(NSRect)newFrame { |
| 82 [super setFrame:newFrame]; |
| 83 hostedView_->SetSize(gfx::Size(NSWidth(newFrame), NSHeight(newFrame))); |
| 84 } |
| 85 |
| 86 - (void)drawRect:(NSRect)dirtyRect { |
| 87 if (!hostedView_) |
| 88 return; |
| 89 |
| 90 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); |
| 91 hostedView_->Paint(&canvas, views::CullSet()); |
| 92 } |
| 93 |
| 94 - (void)mouseDown:(NSEvent*)theEvent { |
| 95 [self handleMouseEvent:theEvent]; |
| 96 } |
| 97 |
| 98 - (void)mouseDragged:(NSEvent*)theEvent { |
| 99 [self handleMouseEvent:theEvent]; |
| 100 } |
| 101 |
| 102 - (void)mouseUp:(NSEvent*)theEvent { |
| 103 [self handleMouseEvent:theEvent]; |
| 104 } |
| 105 |
| 106 // NSAccessibility informal protocol implementation. |
| 107 |
| 108 - (NSArray*)accessibilityAttributeNames { |
| 109 NSMutableArray* attributes = |
| 110 [[super accessibilityAttributeNames] mutableCopy]; |
| 111 if (!hostedView_) |
| 112 return [attributes autorelease]; |
| 113 |
| 114 [attributes addObject:NSAccessibilityValueAttribute]; |
| 115 [attributes addObject:NSAccessibilityDescriptionAttribute]; |
| 116 return [attributes autorelease]; |
| 117 } |
| 118 |
| 119 - (id)accessibilityAttributeValue:(NSString*)attribute { |
| 120 if (!hostedView_) |
| 121 return [super accessibilityAttributeValue:attribute]; |
| 122 |
| 123 ui::AXViewState state; |
| 124 hostedView_->GetAccessibleState(&state); |
| 125 switch (state.role) { |
| 126 default: |
| 127 NOTREACHED(); |
| 128 } |
| 129 #if 0 |
| 130 if ([attribute isEqual:NSAccessibilityRoleAttribute]) |
| 131 return NSAccessibilityTabGroupRole; |
| 132 if ([attribute isEqual:NSAccessibilityTabsAttribute]) { |
| 133 NSMutableArray* tabs = [[[NSMutableArray alloc] init] autorelease]; |
| 134 NSArray* children = |
| 135 [self accessibilityAttributeValue:NSAccessibilityChildrenAttribute]; |
| 136 for (id child in children) { |
| 137 if ([[child accessibilityAttributeValue:NSAccessibilityRoleAttribute] |
| 138 isEqual:NSAccessibilityRadioButtonRole]) { |
| 139 [tabs addObject:child]; |
| 140 } |
| 141 } |
| 142 return tabs; |
| 143 } |
| 144 if ([attribute isEqual:NSAccessibilityContentsAttribute]) |
| 145 return [self accessibilityAttributeValue:NSAccessibilityChildrenAttribute]; |
| 146 if ([attribute isEqual:NSAccessibilityValueAttribute]) |
| 147 return [controller_ activeTabView]; |
| 148 #endif |
| 149 |
| 150 return [super accessibilityAttributeValue:attribute]; |
| 151 } |
| 152 |
| 153 @end |
OLD | NEW |