2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
4 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #import "BlockExceptions.h"
34 #import "DOMInternal.h"
36 #import "FrameLoaderClient.h"
38 #import "GraphicsContext.h"
40 #import "HTMLTableCellElement.h"
41 #import "HitTestRequest.h"
42 #import "HitTestResult.h"
43 #import "KeyboardEvent.h"
45 #import "MouseEventWithHitTestResults.h"
47 #import "PlatformKeyboardEvent.h"
48 #import "PlatformWheelEvent.h"
49 #import "RegularExpression.h"
50 #import "RenderTableCell.h"
51 #import "RenderView.h"
53 #import "SimpleFontData.h"
54 #import "visible_units.h"
55 #import <wtf/StdLibExtras.h>
57 @interface NSView (WebCoreHTMLDocumentView)
58 - (void)drawSingleRect:(NSRect)rect;
65 using namespace HTMLNames;
67 NSImage* Frame::imageFromRect(NSRect rect) const
69 PaintBehavior oldBehavior = m_view->paintBehavior();
70 m_view->setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
72 BEGIN_BLOCK_OBJC_EXCEPTIONS;
74 NSImage* resultImage = [[[NSImage alloc] initWithSize:rect.size] autorelease];
76 if (rect.size.width != 0 && rect.size.height != 0) {
77 [resultImage setFlipped:YES];
78 [resultImage lockFocus];
80 GraphicsContext graphicsContext((CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]);
81 graphicsContext.save();
82 graphicsContext.translate(-rect.origin.x, -rect.origin.y);
83 m_view->paintContents(&graphicsContext, IntRect(rect));
84 graphicsContext.restore();
86 [resultImage unlockFocus];
87 [resultImage setFlipped:NO];
90 m_view->setPaintBehavior(oldBehavior);
93 END_BLOCK_OBJC_EXCEPTIONS;
95 m_view->setPaintBehavior(oldBehavior);
99 NSImage* Frame::selectionImage(bool forceBlackText) const
101 m_view->setPaintBehavior(PaintBehaviorSelectionOnly | (forceBlackText ? PaintBehaviorForceBlackText : 0));
102 m_doc->updateLayout();
103 NSImage* result = imageFromRect(selection()->bounds());
104 m_view->setPaintBehavior(PaintBehaviorNormal);
108 NSImage *Frame::rangeImage(Range* range, bool forceBlackText) const
110 m_view->setPaintBehavior(PaintBehaviorSelectionOnly | (forceBlackText ? PaintBehaviorForceBlackText : 0));
111 m_doc->updateLayout();
112 RenderView* view = contentRenderer();
116 VisibleSelection visibleSelection(range);
118 if (!visibleSelection.isRange())
121 Position start = visibleSelection.start();
122 Position candidate = start.downstream();
123 if (candidate.isCandidate())
125 Position end = visibleSelection.end();
126 candidate = end.upstream();
127 if (candidate.isCandidate())
130 if (start.isNull() || end.isNull() || visibleSelection.visibleStart() == visibleSelection.visibleEnd())
133 RenderObject* savedStartRenderer;
134 int savedStartOffset;
135 RenderObject* savedEndRenderer;
137 view->getSelection(savedStartRenderer, savedStartOffset, savedEndRenderer, savedEndOffset);
139 RenderObject* startRenderer = start.deprecatedNode()->renderer();
140 RenderObject* endRenderer = end.deprecatedNode()->renderer();
142 view->setSelection(startRenderer, start.deprecatedEditingOffset(), endRenderer, end.deprecatedEditingOffset(), RenderView::RepaintNothing);
143 NSImage* result = imageFromRect(view->selectionBounds());
144 view->setSelection(savedStartRenderer, savedStartOffset, savedEndRenderer, savedEndOffset, RenderView::RepaintNothing);
146 m_view->setPaintBehavior(PaintBehaviorNormal);
150 NSImage* Frame::snapshotDragImage(Node* node, NSRect* imageRect, NSRect* elementRect) const
152 RenderObject* renderer = node->renderer();
156 renderer->updateDragState(true); // mark dragged nodes (so they pick up the right CSS)
157 m_doc->updateLayout(); // forces style recalc - needed since changing the drag state might
158 // imply new styles, plus JS could have changed other things
159 LayoutRect topLevelRect;
160 NSRect paintingRect = renderer->paintingRootRect(topLevelRect);
162 m_view->setNodeToDraw(node); // invoke special sub-tree drawing mode
163 NSImage* result = imageFromRect(paintingRect);
164 renderer->updateDragState(false);
165 m_doc->updateLayout();
166 m_view->setNodeToDraw(0);
169 *elementRect = topLevelRect;
171 *imageRect = paintingRect;
175 DragImageRef Frame::nodeImage(Node* node)
177 RenderObject* renderer = node->renderer();
181 m_doc->updateLayout(); // forces style recalc
183 LayoutRect topLevelRect;
184 NSRect paintingRect = renderer->paintingRootRect(topLevelRect);
186 m_view->setNodeToDraw(node); // invoke special sub-tree drawing mode
187 NSImage* result = imageFromRect(paintingRect);
188 m_view->setNodeToDraw(0);
193 DragImageRef Frame::dragImageForSelection()
195 if (!selection()->isRange())
197 return selectionImage();
200 } // namespace WebCore