Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/WebKit/Source/WebKit/chromium/src/mac/WebTextHelper.mm

Issue 6289009: [Mac] Implement the system dictionary popup by implementing NSTextInput methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "WebFrameImpl.h"
33 #include "WebTextHelper.h"
34
35 #include "ColorMac.h"
36 #include "Document.h"
37 #include "Element.h"
38 #include "FocusController.h"
39 #include "Frame.h"
40 #include "FrameView.h"
41 #include "HitTestResult.h"
42 #include "HTMLElement.h"
43 #include "Node.h"
44 #include "Range.h"
45 #include "TextIterator.h"
46 #include "WebRect.h"
47 #include "WebString.h"
48
49 #import <Cocoa/Cocoa.h>
50
51 using namespace WebCore;
52
53 namespace WebKit {
54
55 // This is the internal implementation of WebTextHelper that can return WebCore
56 // types.
57 class WebTextHelperImpl {
58 public:
59 explicit WebTextHelperImpl(WebFrame* frame) : m_frame(frame) {}
60
61 IntRect firstRectForWkRange(Range* range);
62
63 Range* characterRangeAtPoint(const IntPoint point);
64
65 void getLocationAndLengthFromRange(Range* range, NSUInteger& location, NSUIn teger& length);
66
67 Element* rangeScope();
68
69 WebFrameImpl* webframeimpl();
70
71 protected:
72 WebFrame* m_frame;
73 };
74
75 IntRect WebTextHelperImpl::firstRectForWkRange(Range* range)
76 {
77 return webframeimpl()->frame()->editor()->firstRectForRange(range);
78 }
79
80 // This function is copied from /WebKit2/WebPage/mac/WebPageMac.mm.
81 Range* WebTextHelperImpl::characterRangeAtPoint(const IntPoint point)
82 {
83 Frame* frame(webframeimpl()->frame());
84
85 VisiblePosition position = frame->visiblePositionForPoint(point);
86 if (position.isNull())
87 return NULL;
88
89 VisiblePosition previous = position.previous();
90 if (previous.isNotNull()) {
91 Range* previousCharacterRange = makeRange(previous, position).get();
92 IntRect rect = firstRectForWkRange(previousCharacterRange);
93 if (rect.contains(point))
94 return previousCharacterRange;
95 }
96
97 VisiblePosition next = position.next();
98 if (next.isNotNull()) {
99 Range* nextCharacterRange = makeRange(position, next).get();
100 IntRect rect = firstRectForWkRange(nextCharacterRange);
101 if (rect.contains(point))
102 return nextCharacterRange;
103 }
104
105 return NULL;
106 }
107
108 // This function is copied from /WebKit2/WebPage/WebPage.cpp.
109 void WebTextHelperImpl::getLocationAndLengthFromRange(Range* range, NSUInteger& location, NSUInteger& length)
110 {
111 location = notFound;
112 length = 0;
113
114 if (!range || !range->startContainer())
115 return;
116
117 Element* selectionRoot = range->ownerDocument()->frame()->selection()->rootE ditableElement();
118 Element* scope = selectionRoot ? selectionRoot : range->ownerDocument()->doc umentElement();
119
120 // Mouse events may cause TSM to attempt to create an NSRange for a portion of the view
121 // that is not inside the current editable region. These checks ensure we d on't produce
122 // potentially invalid data when responding to such requests.
123 if (range->startContainer() != scope && !range->startContainer()->isDescenda ntOf(scope))
124 return;
125 if (range->endContainer() != scope && !range->endContainer()->isDescendantOf (scope))
126 return;
127
128 RefPtr<Range> testRange = Range::create(scope->document(), scope, 0, range-> startContainer(), range->startOffset());
129 ASSERT(testRange->startContainer() == scope);
130 location = TextIterator::rangeLength(testRange.get());
131
132 ExceptionCode ec;
133 testRange->setEnd(range->endContainer(), range->endOffset(), ec);
134 ASSERT(testRange->startContainer() == scope);
135 length = TextIterator::rangeLength(testRange.get()) - location;
136 }
137
138 Element* WebTextHelperImpl::rangeScope()
139 {
140 Frame* frame(webframeimpl()->frame());
141 Element* selectionRoot(frame->selection()->rootEditableElement());
142 return selectionRoot ? selectionRoot : frame->document()->documentElement();
143 }
144
145 WebFrameImpl* WebTextHelperImpl::webframeimpl()
146 {
147 return static_cast<WebFrameImpl*>(m_frame);
148 }
149
150 ////////////////////////////////////////////////////////////////////////////////
151
152 WebTextHelper::WebTextHelper(WebFrame* frame)
153 : m_private(new WebTextHelperImpl(frame))
154 {
155 }
156
157 WebTextHelper::~WebTextHelper()
158 {
159 m_private.reset(0);
160 }
161
162 uint WebTextHelper::characterIndexForPoint(int x, int y)
163 {
164 Frame* frame(m_private->webframeimpl()->frame());
165 if (!frame)
166 return NSNotFound;
167
168 IntPoint point(frame->view()->windowToContents(IntPoint(x, y)));
169 HitTestResult result = frame->eventHandler()->hitTestResultAtPoint(point, fa lse);
170 frame = result.innerNonSharedNode() ? result.innerNonSharedNode()->document( )->frame() : frame->page()->focusController()->focusedOrMainFrame();
171
172 Range* range(m_private->characterRangeAtPoint(result.point()));
173 if (!range)
174 return NSNotFound;
175
176 NSRange nsRange = { 0 };
177 m_private->getLocationAndLengthFromRange(range, nsRange.location, nsRange.le ngth);
178
179 return nsRange.location;
180 }
181
182 WebKit::WebRect WebTextHelper::firstRectForRange(uint location, uint length)
183 {
184 WebFrameImpl* frameImpl(m_private->webframeimpl());
185 Frame* frame(frameImpl->frame());
186 WebKit::WebRect rect;
187 if (!frameImpl->firstRectForCharacterRange(location, length, rect))
188 return rect;
189 // When inside an text control, don't adjust the range.
190 if (!frame->selection()->rootEditableElement())
191 rect = frame->view()->contentsToWindow(rect);
192 return rect;
193 }
194
195 // This function is copied from /WebKit/mac/Misc/WebNSAttributedStringExtras.mm.
196 WebKit::WebString WebTextHelper::substringInRange(uint location, uint length)
197 {
198 Frame* frame(m_private->webframeimpl()->frame());
199 if (frame->view()->needsLayout())
200 frame->view()->layout();
201
202 RefPtr<Range> range(TextIterator::rangeFromLocationAndLength(m_private->rang eScope(), location, length));
203 if (!range)
204 return WebKit::WebString();
205
206 NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init] ;
207 NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
208
209 unsigned position = 0;
210 for (TextIterator it(range.get()); !it.atEnd() && [string length] < length; it.advance()) {
211 unsigned numCharacters = it.length();
212 if (!numCharacters)
213 continue;
214
215 ExceptionCode exception = 0;
216 Node* container = it.range()->startContainer(exception);
217 RenderObject* renderer = container->renderer();
218 ASSERT(renderer);
219 if (!renderer)
220 continue;
221
222 RenderStyle* style = renderer->style();
223 NSFont* font = style->font().primaryFont()->getNSFont();
224 // If the platform font can't be loaded, it's likely that the site is
225 // using a web font. For now, just use the default font instead.
226 // TODO(rsesek): Change the font activation flags to allow other process es
227 // to use the font.
228 if (!font)
229 font = [NSFont systemFontOfSize:style->font().size()];
230 [attrs setObject:font forKey:NSFontAttributeName];
231
232 if (style->visitedDependentColor(CSSPropertyColor).alpha())
233 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyCol or)) forKey:NSForegroundColorAttributeName];
234 else
235 [attrs removeObjectForKey:NSForegroundColorAttributeName];
236 if (style->visitedDependentColor(CSSPropertyBackgroundColor).alpha())
237 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyBac kgroundColor)) forKey:NSBackgroundColorAttributeName];
238 else
239 [attrs removeObjectForKey:NSBackgroundColorAttributeName];
240
241 NSString* substring =
242 [[[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(it.ch aracters())
243 length:it.length()
244 freeWhenDone:NO] autorelease];
245 [string replaceCharactersInRange:NSMakeRange(position, 0)
246 withString:substring];
247 [string setAttributes:attrs range:NSMakeRange(position, numCharacters)];
248 position += numCharacters;
249 }
250 NSData* archivedData([NSArchiver archivedDataWithRootObject:string]);
jeremy 2011/02/21 09:19:47 If archiveData is nil , is this codepath safe?
Robert Sesek 2011/02/24 23:26:52 Added a nil check.
251 return WebKit::WebString(static_cast<const WebUChar*>([archivedData bytes]),
252 [archivedData length]);
253 }
254
255 } // namespace WebKit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698