tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / page / mac / FrameMac.mm
1 /*
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)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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. 
26  */
27
28 #import "config.h"
29 #import "Frame.h"
30
31 #import "BlockExceptions.h"
32 #import "ColorMac.h"
33 #import "Cursor.h"
34 #import "DOMInternal.h"
35 #import "Event.h"
36 #import "FrameLoaderClient.h"
37 #import "FrameView.h"
38 #import "GraphicsContext.h"
39 #import "HTMLNames.h"
40 #import "HTMLTableCellElement.h"
41 #import "HitTestRequest.h"
42 #import "HitTestResult.h"
43 #import "KeyboardEvent.h"
44 #import "Logging.h"
45 #import "MouseEventWithHitTestResults.h"
46 #import "Page.h"
47 #import "PlatformKeyboardEvent.h"
48 #import "PlatformWheelEvent.h"
49 #import "RegularExpression.h"
50 #import "RenderTableCell.h"
51 #import "RenderView.h"
52 #import "Scrollbar.h"
53 #import "SimpleFontData.h"
54 #import "visible_units.h"
55 #import <wtf/StdLibExtras.h>
56
57 @interface NSView (WebCoreHTMLDocumentView)
58 - (void)drawSingleRect:(NSRect)rect;
59 @end
60  
61 using namespace std;
62
63 namespace WebCore {
64
65 using namespace HTMLNames;
66
67 NSImage* Frame::imageFromRect(NSRect rect) const
68 {
69     PaintBehavior oldBehavior = m_view->paintBehavior();
70     m_view->setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
71     
72     BEGIN_BLOCK_OBJC_EXCEPTIONS;
73     
74     NSImage* resultImage = [[[NSImage alloc] initWithSize:rect.size] autorelease];
75     
76     if (rect.size.width != 0 && rect.size.height != 0) {
77         [resultImage setFlipped:YES];
78         [resultImage lockFocus];
79
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();
85
86         [resultImage unlockFocus];
87         [resultImage setFlipped:NO];
88     }
89     
90     m_view->setPaintBehavior(oldBehavior);
91     return resultImage;
92     
93     END_BLOCK_OBJC_EXCEPTIONS;
94     
95     m_view->setPaintBehavior(oldBehavior);
96     return nil;
97 }
98
99 NSImage* Frame::selectionImage(bool forceBlackText) const
100 {
101     m_view->setPaintBehavior(PaintBehaviorSelectionOnly | (forceBlackText ? PaintBehaviorForceBlackText : 0));
102     m_doc->updateLayout();
103     NSImage* result = imageFromRect(selection()->bounds());
104     m_view->setPaintBehavior(PaintBehaviorNormal);
105     return result;
106 }
107
108 NSImage *Frame::rangeImage(Range* range, bool forceBlackText) const
109 {
110     m_view->setPaintBehavior(PaintBehaviorSelectionOnly | (forceBlackText ? PaintBehaviorForceBlackText : 0));
111     m_doc->updateLayout();
112     RenderView* view = contentRenderer();
113     if (!view)
114         return nil;
115
116     VisibleSelection visibleSelection(range);
117
118     if (!visibleSelection.isRange())
119         return nil;
120
121     Position start = visibleSelection.start();
122     Position candidate = start.downstream();
123     if (candidate.isCandidate())
124         start = candidate;
125     Position end = visibleSelection.end();
126     candidate = end.upstream();
127     if (candidate.isCandidate())
128         end = candidate;
129
130     if (start.isNull() || end.isNull() || visibleSelection.visibleStart() == visibleSelection.visibleEnd())
131         return nil;
132
133     RenderObject* savedStartRenderer;
134     int savedStartOffset;
135     RenderObject* savedEndRenderer;
136     int savedEndOffset;
137     view->getSelection(savedStartRenderer, savedStartOffset, savedEndRenderer, savedEndOffset);
138
139     RenderObject* startRenderer = start.deprecatedNode()->renderer();
140     RenderObject* endRenderer = end.deprecatedNode()->renderer();
141
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);
145
146     m_view->setPaintBehavior(PaintBehaviorNormal);
147     return result;
148 }
149
150 NSImage* Frame::snapshotDragImage(Node* node, NSRect* imageRect, NSRect* elementRect) const
151 {
152     RenderObject* renderer = node->renderer();
153     if (!renderer)
154         return nil;
155     
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);
161
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);
167
168     if (elementRect)
169         *elementRect = topLevelRect;
170     if (imageRect)
171         *imageRect = paintingRect;
172     return result;
173 }
174
175 DragImageRef Frame::nodeImage(Node* node)
176 {
177     RenderObject* renderer = node->renderer();
178     if (!renderer)
179         return nil;
180
181     m_doc->updateLayout(); // forces style recalc
182
183     LayoutRect topLevelRect;
184     NSRect paintingRect = renderer->paintingRootRect(topLevelRect);
185
186     m_view->setNodeToDraw(node); // invoke special sub-tree drawing mode
187     NSImage* result = imageFromRect(paintingRect);
188     m_view->setNodeToDraw(0);
189
190     return result;
191 }
192
193 DragImageRef Frame::dragImageForSelection()
194 {
195     if (!selection()->isRange())
196         return nil;
197     return selectionImage();
198 }
199
200 } // namespace WebCore