1 // Copyright 2013 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 #import "content/browser/renderer_host/render_widget_host_view_mac_dictionary_helper.h"
7 #include "base/strings/sys_string_conversions.h"
8 #import "content/browser/renderer_host/render_widget_host_view_mac.h"
12 RenderWidgetHostViewMacDictionaryHelper::
13 RenderWidgetHostViewMacDictionaryHelper(RenderWidgetHostView* view)
14 : view_(static_cast<RenderWidgetHostViewMac*>(view)),
15 target_view_(static_cast<RenderWidgetHostViewMac*>(view)) {
18 void RenderWidgetHostViewMacDictionaryHelper::SetTargetView(
19 RenderWidgetHostView* target_view) {
20 target_view_ = static_cast<RenderWidgetHostViewMac*>(target_view);
23 void RenderWidgetHostViewMacDictionaryHelper::ShowDefinitionForSelection() {
24 NSRange selection_range = [view_->cocoa_view() selectedRange];
25 NSAttributedString* attr_string =
26 [view_->cocoa_view() attributedSubstringForProposedRange:selection_range
29 if (view_->selected_text().empty())
31 // The PDF plugin does not support getting the attributed string. Until it
32 // does, use NSPerformService(), which opens Dictionary.app.
33 // http://crbug.com/152438
34 // TODO(asvitkine): This should be removed after the above support is added.
35 NSString* text = base::SysUTF8ToNSString(view_->selected_text());
36 NSPasteboard* pasteboard = [NSPasteboard pasteboardWithUniqueName];
37 NSArray* types = [NSArray arrayWithObject:NSStringPboardType];
38 [pasteboard declareTypes:types owner:nil];
39 if ([pasteboard setString:text forType:NSStringPboardType])
40 NSPerformService(@"Look Up in Dictionary", pasteboard);
45 [view_->cocoa_view() firstViewRectForCharacterRange:selection_range
48 NSDictionary* attrs = [attr_string attributesAtIndex:0 effectiveRange:nil];
49 NSFont* font = [attrs objectForKey:NSFontAttributeName];
50 rect.origin.y += NSHeight(rect) - [font ascender];
52 rect.origin.x += offset_.x();
53 rect.origin.y += offset_.y();
55 [target_view_->cocoa_view() showDefinitionForAttributedString:attr_string
59 } // namespace content