[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() {
}
void RenderViewContextMenuMac::LookUpInDictionary() {
// TODO(morrita): On Safari, A dictionary panel could be shown
// based on a preference setting of Dictionary.app. We currently
// don't support it: http://crbug.com/17951
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);
content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
if (view)
view->ShowDefinitionForSelection();
}
void RenderViewContextMenuMac::StartSpeaking() {
......
......@@ -209,6 +209,7 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase {
virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE;
virtual void SetWindowVisibility(bool visible) OVERRIDE;
virtual void WindowFrameChanged() OVERRIDE;
virtual void ShowDefinitionForSelection() OVERRIDE;
virtual bool SupportsSpeech() const OVERRIDE;
virtual void SpeakSelection() OVERRIDE;
virtual bool IsSpeaking() const OVERRIDE;
......
......@@ -134,6 +134,8 @@ static float ScaleFactor(NSView* view) {
- (void)windowChangedScreen:(NSNotification*)notification;
- (void)checkForPluginImeCancellation;
- (void)updateTabBackingStoreScaleFactor;
- (NSRect)firstViewRectForCharacterRange:(NSRange)theRange
actualRange:(NSRangePointer)actualRange;
@end
// NSEvent subtype for scroll gestures events.
......@@ -765,7 +767,7 @@ void RenderWidgetHostViewMac::SelectionChanged(const string16& text,
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();
......@@ -779,7 +781,7 @@ void RenderWidgetHostViewMac::SelectionChanged(const string16& text,
}
[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]) {
......@@ -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) {
RenderWidgetHostViewBase::SetBackground(background);
if (render_widget_host_)
......@@ -2797,8 +2818,8 @@ extern NSString *NSTextInputReplacementRangeAttributeName;
return index;
}
- (NSRect)firstRectForCharacterRange:(NSRange)theRange
actualRange:(NSRangePointer)actualRange {
- (NSRect)firstViewRectForCharacterRange:(NSRange)theRange
actualRange:(NSRangePointer)actualRange {
NSRect rect;
if (!renderWidgetHostView_->GetCachedFirstRectForCharacterRange(
theRange,
......@@ -2813,10 +2834,18 @@ extern NSString *NSTextInputReplacementRangeAttributeName;
}
// 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;
......
......@@ -69,6 +69,7 @@ class TestRenderWidgetHostView : public RenderWidgetHostViewBase {
virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE {}
virtual void SetWindowVisibility(bool visible) OVERRIDE {}
virtual void WindowFrameChanged() OVERRIDE {}
virtual void ShowDefinitionForSelection() OVERRIDE {}
virtual bool SupportsSpeech() const OVERRIDE;
virtual void SpeakSelection() OVERRIDE;
virtual bool IsSpeaking() const OVERRIDE;
......
......@@ -125,6 +125,9 @@ class CONTENT_EXPORT RenderWidgetHostView {
// Informs the view that its containing window's frame changed.
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.
virtual bool SupportsSpeech() const = 0;
// 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