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

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: Plumb selection rannge with ViewHostMsg_SelectionChanged Created 9 years, 11 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 /*
jam 2011/01/21 18:51:07 nit: usually we have the interface in the public d
Robert Sesek 2011/01/21 23:40:07 Ok. I won't do that now because it's easier for th
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 scrollPosition(frame->view()->scrollPosition());
169 IntPoint point(x + scrollPosition.x(), y + scrollPosition.y());
170 HitTestResult result = frame->eventHandler()->hitTestResultAtPoint(point, fa lse);
171 frame = result.innerNonSharedNode() ? result.innerNonSharedNode()->document( )->frame() : frame->page()->focusController()->focusedOrMainFrame();
172
173 Range* range(m_private->characterRangeAtPoint(result.point()));
174 if (!range)
175 return NSNotFound;
176
177 NSRange nsRange = { 0 };
178 m_private->getLocationAndLengthFromRange(range, nsRange.location, nsRange.le ngth);
179
180 return nsRange.location;
181 }
182
183 WebKit::WebRect WebTextHelper::firstRectForRange(uint location, uint length)
184 {
185 Frame* frame(m_private->webframeimpl()->frame());
186 if (frame->view()->needsLayout())
187 frame->view()->layout();
188
189 RefPtr<Range> range(TextIterator::rangeFromLocationAndLength(m_private->rang eScope(), location, length));
James Su 2011/01/21 00:02:47 range could be NULL if the given location is out o
Robert Sesek 2011/01/21 23:40:07 Taken care of the NULL dereference. But I don't th
190
191 WebKit::WebRect rect(m_private->firstRectForWkRange(range.get()));
192 IntPoint scrollPosition(frame->view()->scrollPosition());
193 rect.x += scrollPosition.x();
194 rect.y -= scrollPosition.y();
195 return rect;
196 }
197
198 // This function is copied from /WebKit/mac/Misc/WebNSAttributedStringExtras.mm.
199 WebKit::WebString WebTextHelper::substringInRange(uint location, uint length)
200 {
201 Frame* frame(m_private->webframeimpl()->frame());
202 if (frame->view()->needsLayout())
203 frame->view()->layout();
204
205 RefPtr<Range> range(TextIterator::rangeFromLocationAndLength(m_private->rang eScope(), location, length));
James Su 2011/01/21 00:02:47 range could be NULL.
Robert Sesek 2011/01/21 23:40:07 Done.
206
207 NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init] ;
208 NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
209
210 unsigned position = 0;
211 for (TextIterator it(range.get(), TextIteratorEntersTextControls);
212 !it.atEnd() && [string length] < length; it.advance()) {
213 unsigned numCharacters = it.length();
214 if (!numCharacters)
215 continue;
216
217 ExceptionCode exception = 0;
218 Node* container = it.range()->startContainer(exception);
219 RenderObject* renderer = container->renderer();
220 ASSERT(renderer);
221 if (!renderer)
222 continue;
223
224 RenderStyle* style = renderer->style();
225 NSFont* font = style->font().primaryFont()->getNSFont();
226 // If the platform font can't be loaded, it's likely that the site is
227 // using a web font. For now, just use the default font instead.
228 // TODO(rsesek): Change the font activation flags to allow other process es
229 // to use the font.
230 if (!font)
231 font = [NSFont systemFontOfSize:style->font().size()];
232 [attrs setObject:font forKey:NSFontAttributeName];
233
234 if (style->visitedDependentColor(CSSPropertyColor).alpha())
235 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyCol or)) forKey:NSForegroundColorAttributeName];
236 else
237 [attrs removeObjectForKey:NSForegroundColorAttributeName];
238 if (style->visitedDependentColor(CSSPropertyBackgroundColor).alpha())
239 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyBac kgroundColor)) forKey:NSBackgroundColorAttributeName];
240 else
241 [attrs removeObjectForKey:NSBackgroundColorAttributeName];
242
243 NSString* substring =
244 [[[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(it.ch aracters())
245 length:it.length()
246 freeWhenDone:NO] autorelease];
247 [string replaceCharactersInRange:NSMakeRange(position, 0)
248 withString:substring];
249 [string setAttributes:attrs range:NSMakeRange(position, numCharacters)];
250 position += numCharacters;
251 }
252 NSData* archivedData([NSArchiver archivedDataWithRootObject:string]);
253 return WebKit::WebString(static_cast<const WebUChar*>([archivedData bytes]),
254 [archivedData length]);
255 }
256
257 } // namespace WebKit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698