Index: content/browser/renderer_host/render_widget_host_view_mac.mm |
=================================================================== |
--- content/browser/renderer_host/render_widget_host_view_mac.mm (revision 149502) |
+++ content/browser/renderer_host/render_widget_host_view_mac.mm (working copy) |
@@ -140,6 +140,8 @@ |
- (void)windowChangedScreen:(NSNotification*)notification; |
- (void)checkForPluginImeCancellation; |
- (void)updateTabBackingStoreScaleFactor; |
+- (NSRect)firstViewRectForCharacterRange:(NSRange)theRange |
+ actualRange:(NSRangePointer)actualRange; |
@end |
// NSEvent subtype for scroll gestures events. |
@@ -754,7 +756,7 @@ |
size_t offset, |
const ui::Range& range) { |
if (range.is_empty() || text.empty()) { |
- selected_text_.clear(); |
+ selected_text_.clear(); |
} else { |
size_t pos = range.GetMin() - offset; |
size_t n = range.length(); |
@@ -768,7 +770,7 @@ |
} |
[cocoa_view_ setSelectedRange:range.ToNSRange()]; |
- // Updaes markedRange when there is no marked text so that retrieving |
+ // Updates markedRange when there is no marked text so that retrieving |
// markedRange immediately after calling setMarkdText: returns the current |
// caret position. |
if (![cocoa_view_ hasMarkedText]) { |
@@ -1400,6 +1402,24 @@ |
} |
} |
+void RenderWidgetHostViewMac::ShowDefinitionForSelection() { |
+ // Brings up either Dictionary.app or a light-weight dictionary panel, |
+ // depending on system settings. |
+ NSRange selection_range = [cocoa_view_ selectedRange]; |
+ NSAttributedString* attr_string = |
+ [cocoa_view_ attributedSubstringForProposedRange:selection_range |
+ actualRange:nil]; |
+ NSRect rect = [cocoa_view_ firstViewRectForCharacterRange:selection_range |
+ actualRange:nil]; |
+ |
+ // Set |rect.origin| to the text baseline based on |attr_string|'s font. |
Avi (use Gerrit)
2012/08/07 14:18:30
Nice cheat! Can you acknowledge in the comment tha
|
+ NSDictionary* attrs = [attr_string attributesAtIndex:0 effectiveRange:nil]; |
+ NSFont* font = [attrs objectForKey:NSFontAttributeName]; |
+ rect.origin.y += NSHeight(rect) - [font ascender]; |
+ [cocoa_view_ showDefinitionForAttributedString:attr_string |
+ atPoint:rect.origin]; |
+} |
+ |
void RenderWidgetHostViewMac::SetBackground(const SkBitmap& background) { |
RenderWidgetHostViewBase::SetBackground(background); |
if (render_widget_host_) |
@@ -2797,8 +2817,8 @@ |
return index; |
} |
-- (NSRect)firstRectForCharacterRange:(NSRange)theRange |
- actualRange:(NSRangePointer)actualRange { |
+- (NSRect)firstViewRectForCharacterRange:(NSRange)theRange |
+ actualRange:(NSRangePointer)actualRange { |
NSRect rect; |
if (!renderWidgetHostView_->GetCachedFirstRectForCharacterRange( |
theRange, |
@@ -2813,10 +2833,18 @@ |
} |
// The returned rectangle is in WebKit coordinates (upper left origin), so |
- // flip the coordinate system and then convert it into screen coordinates for |
- // return. |
+ // flip the coordinate system. |
NSRect viewFrame = [self frame]; |
rect.origin.y = NSHeight(viewFrame) - NSMaxY(rect); |
+ return rect; |
+} |
+ |
+- (NSRect)firstRectForCharacterRange:(NSRange)theRange |
+ actualRange:(NSRangePointer)actualRange { |
+ NSRect rect = [self firstViewRectForCharacterRange:theRange |
+ actualRange:actualRange]; |
+ |
+ // Convert into screen coordinates for return. |
rect = [self convertRect:rect toView:nil]; |
rect.origin = [[self window] convertBaseToScreen:rect.origin]; |
return rect; |