[Mac]: Make dictionary context menu item use system settings for whether to...

[Mac]: Make dictionary context menu item use system settings for whether to launch Dictionary.app or show a panel.

BUG=65537, 121917
TEST=Open Dictionary.app's preferences and change "Contextual Menu:" to "Open Dictionary Panel".
Select a word in the web content area, right click and choose "Look Up in Dictionary". The
dictionary panel should appear with the overlay text positioned correctly over the word.
Change the setting back in Dictionary.app and observe that choosing "Look Up in Dictionary"
brings up Dictionary.app in that case.

Review URL: https://chromiumcodereview.appspot.com/10830176

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150388 0039d316-1c4b-4281-b951-d872f2087c98
parent cb25aa1b
...@@ -192,18 +192,9 @@ void RenderViewContextMenuMac::InitPlatformMenu() { ...@@ -192,18 +192,9 @@ void RenderViewContextMenuMac::InitPlatformMenu() {
} }
void RenderViewContextMenuMac::LookUpInDictionary() { void RenderViewContextMenuMac::LookUpInDictionary() {
// TODO(morrita): On Safari, A dictionary panel could be shown content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
// based on a preference setting of Dictionary.app. We currently if (view)
// don't support it: http://crbug.com/17951 view->ShowDefinitionForSelection();
NSString* text = base::SysUTF16ToNSString(params_.selection_text);
NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName];
// 10.5 and earlier require declareTypes before setData.
// See the documentation on [NSPasteboard declareTypes].
NSArray* toDeclare = [NSArray arrayWithObject:NSStringPboardType];
[pboard declareTypes:toDeclare owner:nil];
BOOL ok = [pboard setString:text forType:NSStringPboardType];
if (ok)
NSPerformService(@"Look Up in Dictionary", pboard);
} }
void RenderViewContextMenuMac::StartSpeaking() { void RenderViewContextMenuMac::StartSpeaking() {
......
...@@ -209,6 +209,7 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase { ...@@ -209,6 +209,7 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase {
virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE; virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE;
virtual void SetWindowVisibility(bool visible) OVERRIDE; virtual void SetWindowVisibility(bool visible) OVERRIDE;
virtual void WindowFrameChanged() OVERRIDE; virtual void WindowFrameChanged() OVERRIDE;
virtual void ShowDefinitionForSelection() OVERRIDE;
virtual bool SupportsSpeech() const OVERRIDE; virtual bool SupportsSpeech() const OVERRIDE;
virtual void SpeakSelection() OVERRIDE; virtual void SpeakSelection() OVERRIDE;
virtual bool IsSpeaking() const OVERRIDE; virtual bool IsSpeaking() const OVERRIDE;
......
...@@ -134,6 +134,8 @@ static float ScaleFactor(NSView* view) { ...@@ -134,6 +134,8 @@ static float ScaleFactor(NSView* view) {
- (void)windowChangedScreen:(NSNotification*)notification; - (void)windowChangedScreen:(NSNotification*)notification;
- (void)checkForPluginImeCancellation; - (void)checkForPluginImeCancellation;
- (void)updateTabBackingStoreScaleFactor; - (void)updateTabBackingStoreScaleFactor;
- (NSRect)firstViewRectForCharacterRange:(NSRange)theRange
actualRange:(NSRangePointer)actualRange;
@end @end
// NSEvent subtype for scroll gestures events. // NSEvent subtype for scroll gestures events.
...@@ -765,7 +767,7 @@ void RenderWidgetHostViewMac::SelectionChanged(const string16& text, ...@@ -765,7 +767,7 @@ void RenderWidgetHostViewMac::SelectionChanged(const string16& text,
size_t offset, size_t offset,
const ui::Range& range) { const ui::Range& range) {
if (range.is_empty() || text.empty()) { if (range.is_empty() || text.empty()) {
selected_text_.clear(); selected_text_.clear();
} else { } else {
size_t pos = range.GetMin() - offset; size_t pos = range.GetMin() - offset;
size_t n = range.length(); size_t n = range.length();
...@@ -779,7 +781,7 @@ void RenderWidgetHostViewMac::SelectionChanged(const string16& text, ...@@ -779,7 +781,7 @@ void RenderWidgetHostViewMac::SelectionChanged(const string16& text,
} }
[cocoa_view_ setSelectedRange:range.ToNSRange()]; [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 // markedRange immediately after calling setMarkdText: returns the current
// caret position. // caret position.
if (![cocoa_view_ hasMarkedText]) { if (![cocoa_view_ hasMarkedText]) {
...@@ -1399,6 +1401,25 @@ void RenderWidgetHostViewMac::WindowFrameChanged() { ...@@ -1399,6 +1401,25 @@ void RenderWidgetHostViewMac::WindowFrameChanged() {
} }
} }
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,
// since -baselineDeltaForCharacterAtIndex: is currently not implemented.
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) { void RenderWidgetHostViewMac::SetBackground(const SkBitmap& background) {
RenderWidgetHostViewBase::SetBackground(background); RenderWidgetHostViewBase::SetBackground(background);
if (render_widget_host_) if (render_widget_host_)
...@@ -2797,8 +2818,8 @@ extern NSString *NSTextInputReplacementRangeAttributeName; ...@@ -2797,8 +2818,8 @@ extern NSString *NSTextInputReplacementRangeAttributeName;
return index; return index;
} }
- (NSRect)firstRectForCharacterRange:(NSRange)theRange - (NSRect)firstViewRectForCharacterRange:(NSRange)theRange
actualRange:(NSRangePointer)actualRange { actualRange:(NSRangePointer)actualRange {
NSRect rect; NSRect rect;
if (!renderWidgetHostView_->GetCachedFirstRectForCharacterRange( if (!renderWidgetHostView_->GetCachedFirstRectForCharacterRange(
theRange, theRange,
...@@ -2813,10 +2834,18 @@ extern NSString *NSTextInputReplacementRangeAttributeName; ...@@ -2813,10 +2834,18 @@ extern NSString *NSTextInputReplacementRangeAttributeName;
} }
// The returned rectangle is in WebKit coordinates (upper left origin), so // The returned rectangle is in WebKit coordinates (upper left origin), so
// flip the coordinate system and then convert it into screen coordinates for // flip the coordinate system.
// return.
NSRect viewFrame = [self frame]; NSRect viewFrame = [self frame];
rect.origin.y = NSHeight(viewFrame) - NSMaxY(rect); 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 = [self convertRect:rect toView:nil];
rect.origin = [[self window] convertBaseToScreen:rect.origin]; rect.origin = [[self window] convertBaseToScreen:rect.origin];
return rect; return rect;
......
...@@ -69,6 +69,7 @@ class TestRenderWidgetHostView : public RenderWidgetHostViewBase { ...@@ -69,6 +69,7 @@ class TestRenderWidgetHostView : public RenderWidgetHostViewBase {
virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE {} virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE {}
virtual void SetWindowVisibility(bool visible) OVERRIDE {} virtual void SetWindowVisibility(bool visible) OVERRIDE {}
virtual void WindowFrameChanged() OVERRIDE {} virtual void WindowFrameChanged() OVERRIDE {}
virtual void ShowDefinitionForSelection() OVERRIDE {}
virtual bool SupportsSpeech() const OVERRIDE; virtual bool SupportsSpeech() const OVERRIDE;
virtual void SpeakSelection() OVERRIDE; virtual void SpeakSelection() OVERRIDE;
virtual bool IsSpeaking() const OVERRIDE; virtual bool IsSpeaking() const OVERRIDE;
......
...@@ -125,6 +125,9 @@ class CONTENT_EXPORT RenderWidgetHostView { ...@@ -125,6 +125,9 @@ class CONTENT_EXPORT RenderWidgetHostView {
// Informs the view that its containing window's frame changed. // Informs the view that its containing window's frame changed.
virtual void WindowFrameChanged() = 0; virtual void WindowFrameChanged() = 0;
// Brings up the dictionary showing a definition for the selected text.
virtual void ShowDefinitionForSelection() = 0;
// Returns |true| if Mac OS X text to speech is supported. // Returns |true| if Mac OS X text to speech is supported.
virtual bool SupportsSpeech() const = 0; virtual bool SupportsSpeech() const = 0;
// Tells the view to speak the currently selected text. // Tells the view to speak the currently selected text.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment