Add ICU message format support
[chromium-blink-merge.git] / content / browser / renderer_host / render_widget_host_view_mac_dictionary_helper.mm
blob92f10bc2688d6d9e84c2a68b9bc772205fc97c83
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"
10 namespace content {
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
27                                                    actualRange:nil];
28   if (!attr_string) {
29     if (view_->selected_text().empty())
30       return;
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);
41     return;
42   }
44   NSRect rect =
45       [view_->cocoa_view() firstViewRectForCharacterRange:selection_range
46                                               actualRange:nil];
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
56                                                         atPoint:rect.origin];
59 }  // namespace content