OSDN Git Service

am fbb3cf06: (-s ours) Do not merge.
[android-x86/external-webkit.git] / WebKit / wx / WebFrame.cpp
1 /*
2  * Copyright (C) 2007 Kevin Ollivier  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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "CString.h"
28 #include "Document.h"
29 #include "Editor.h"
30 #include "Element.h"
31 #include "EventHandler.h"
32 #include "Frame.h"
33 #include "FrameLoader.h"
34 #include "FrameView.h"
35 #include "HitTestResult.h"
36 #include "HTMLFrameOwnerElement.h"
37 #include "markup.h"
38 #include "Page.h"
39 #include "RenderTreeAsText.h"
40 #include "RenderObject.h"
41 #include "RenderView.h"
42 #include "ScriptController.h"
43 #include "ScriptValue.h"
44 #include "TextEncoding.h"
45
46 #include "JSDOMBinding.h"
47 #include <runtime/JSValue.h>
48 #include <runtime/UString.h>
49
50 #include "EditorClientWx.h"
51 #include "FrameLoaderClientWx.h"
52
53 #include "wx/wxprec.h"
54 #ifndef WX_PRECOMP
55     #include "wx/wx.h"
56 #endif
57
58 #include "WebFrame.h"
59 #include "WebView.h"
60 #include "WebFramePrivate.h"
61 #include "WebViewPrivate.h"
62
63 #include <wx/defs.h>
64 #include <wx/dcbuffer.h>
65
66 // Match Safari's min/max zoom sizes by default
67 #define MinimumTextSizeMultiplier       0.5f
68 #define MaximumTextSizeMultiplier       3.0f
69 #define TextSizeMultiplierRatio         1.2f
70
71 wxWebFrame::wxWebFrame(wxWebView* container, wxWebFrame* parent, WebViewFrameData* data) :
72     m_textMagnifier(1.0),
73     m_isEditable(false),
74     m_isInitialized(false),
75     m_beingDestroyed(false),
76     m_title(wxEmptyString)
77 {
78
79     m_impl = new WebFramePrivate();
80  
81     WebCore::HTMLFrameOwnerElement* parentFrame = 0;
82     
83     if (data) {
84         parentFrame = data->ownerElement;
85     }
86     
87     WebCore::FrameLoaderClientWx* loaderClient = new WebCore::FrameLoaderClientWx();
88     
89     m_impl->frame = WebCore::Frame::create(container->m_impl->page, parentFrame, loaderClient);
90     m_impl->frame->deref();
91
92     loaderClient->setFrame(m_impl->frame.get());
93     loaderClient->setWebView(container);
94     
95     m_impl->frame->init();
96         
97     m_isInitialized = true;
98 }
99
100 wxWebFrame::~wxWebFrame()
101 {
102     m_impl->frame->loader()->detachFromParent();
103 }
104
105 WebCore::Frame* wxWebFrame::GetFrame()
106 {
107     if (m_impl)
108         return m_impl->frame.get();
109         
110     return 0;
111 }
112
113 void wxWebFrame::Stop()
114 {
115     if (m_impl->frame && m_impl->frame->loader())
116         m_impl->frame->loader()->stop();
117 }
118
119 void wxWebFrame::Reload()
120 {
121     if (m_impl->frame && m_impl->frame->loader())
122         m_impl->frame->loader()->reload();
123 }
124
125 wxString wxWebFrame::GetPageSource()
126 {
127     if (m_impl->frame) {
128         if (m_impl->frame->view() && m_impl->frame->view()->layoutPending())
129             m_impl->frame->view()->layout();
130     
131         WebCore::Document* doc = m_impl->frame->document();
132         
133         if (doc) {
134             wxString source = createMarkup(doc);
135             return source;
136         }
137     }
138     return wxEmptyString;
139 }
140
141 void wxWebFrame::SetPageSource(const wxString& source, const wxString& baseUrl)
142 {
143     if (m_impl->frame && m_impl->frame->loader()) {
144         WebCore::FrameLoader* loader = m_impl->frame->loader();
145         loader->begin(WebCore::KURL(WebCore::KURL(), static_cast<const char*>(baseUrl.mb_str(wxConvUTF8)), WebCore::UTF8Encoding()));
146         loader->write(static_cast<const WebCore::String>(source));
147         loader->end();
148     }
149 }
150
151 wxString wxWebFrame::GetInnerText()
152 {
153     if (m_impl->frame->view() && m_impl->frame->view()->layoutPending())
154         m_impl->frame->view()->layout();
155         
156     WebCore::Element *documentElement = m_impl->frame->document()->documentElement();
157     return documentElement->innerText();
158 }
159
160 wxString wxWebFrame::GetAsMarkup()
161 {
162     if (!m_impl->frame || !m_impl->frame->document())
163         return wxEmptyString;
164
165     return createMarkup(m_impl->frame->document());
166 }
167
168 wxString wxWebFrame::GetExternalRepresentation()
169 {
170     if (m_impl->frame->view() && m_impl->frame->view()->layoutPending())
171         m_impl->frame->view()->layout();
172
173     return externalRepresentation(m_impl->frame->contentRenderer());
174 }
175
176 wxString wxWebFrame::RunScript(const wxString& javascript)
177 {
178     wxString returnValue = wxEmptyString;
179     if (m_impl->frame) {
180         JSC::JSValuePtr result = m_impl->frame->loader()->executeScript(javascript, true).jsValue();
181         if (result)
182             returnValue = wxString(result.toString(m_impl->frame->script()->globalObject()->globalExec()).UTF8String().c_str(), wxConvUTF8);        
183     }
184     return returnValue;
185 }
186
187 bool wxWebFrame::FindString(const wxString& string, bool forward, bool caseSensitive, bool wrapSelection, bool startInSelection)
188 {
189     if (m_impl->frame)
190         return m_impl->frame->findString(string, forward, caseSensitive, wrapSelection, startInSelection);
191
192     return false;
193 }
194
195 void wxWebFrame::LoadURL(const wxString& url)
196 {
197     if (m_impl->frame && m_impl->frame->loader()) {
198         WebCore::KURL kurl = WebCore::KURL(WebCore::KURL(), static_cast<const char*>(url.mb_str(wxConvUTF8)), WebCore::UTF8Encoding());
199         // NB: This is an ugly fix, but CURL won't load sub-resources if the
200         // protocol is omitted; sadly, it will not emit an error, either, so
201         // there's no way for us to catch this problem the correct way yet.
202         if (kurl.protocol().isEmpty()) {
203             // is it a file on disk?
204             if (wxFileExists(url)) {
205                 kurl.setProtocol("file");
206                 kurl.setPath("//" + kurl.path());
207             }
208             else {
209                 kurl.setProtocol("http");
210                 kurl.setPath("//" + kurl.path());
211             }
212         }
213         m_impl->frame->loader()->load(kurl, false);
214     }
215 }
216
217 bool wxWebFrame::GoBack()
218 {
219     if (m_impl->frame && m_impl->frame->page())
220         return m_impl->frame->page()->goBack();
221
222     return false;
223 }
224
225 bool wxWebFrame::GoForward()
226 {
227     if (m_impl->frame && m_impl->frame->page())
228         return m_impl->frame->page()->goForward();
229
230     return false;
231 }
232
233 bool wxWebFrame::CanGoBack()
234 {
235     if (m_impl->frame && m_impl->frame->page() && m_impl->frame->page()->backForwardList())
236         return m_impl->frame->page()->backForwardList()->backItem() != NULL;
237
238     return false;
239 }
240
241 bool wxWebFrame::CanGoForward()
242 {
243     if (m_impl->frame && m_impl->frame->page() && m_impl->frame->page()->backForwardList())
244         return m_impl->frame->page()->backForwardList()->forwardItem() != NULL;
245
246     return false;
247 }
248
249 void wxWebFrame::Undo()
250 {
251     if (m_impl->frame && m_impl->frame->editor() && CanUndo())
252         return m_impl->frame->editor()->undo();
253 }
254
255 void wxWebFrame::Redo()
256 {
257     if (m_impl->frame && m_impl->frame->editor() && CanRedo())
258         return m_impl->frame->editor()->redo();
259 }
260
261 bool wxWebFrame::CanUndo()
262 {
263     if (m_impl->frame && m_impl->frame->editor())
264         return m_impl->frame->editor()->canUndo();
265
266     return false;
267 }
268
269 bool wxWebFrame::CanRedo()
270 {
271     if (m_impl->frame && m_impl->frame->editor())
272         return m_impl->frame->editor()->canRedo();
273
274     return false;
275 }
276
277 bool wxWebFrame::CanIncreaseTextSize() const
278 {
279     if (m_impl->frame) {
280         if (m_textMagnifier*TextSizeMultiplierRatio <= MaximumTextSizeMultiplier)
281             return true;
282     }
283     return false;
284 }
285
286 void wxWebFrame::IncreaseTextSize()
287 {
288     if (CanIncreaseTextSize()) {
289         m_textMagnifier = m_textMagnifier*TextSizeMultiplierRatio;
290         m_impl->frame->setZoomFactor(m_textMagnifier, true);
291     }
292 }
293
294 bool wxWebFrame::CanDecreaseTextSize() const
295 {
296     if (m_impl->frame) {
297         if (m_textMagnifier/TextSizeMultiplierRatio >= MinimumTextSizeMultiplier)
298             return true;
299     }
300     return false;
301 }
302
303 void wxWebFrame::DecreaseTextSize()
304 {        
305     if (CanDecreaseTextSize()) {
306         m_textMagnifier = m_textMagnifier/TextSizeMultiplierRatio;
307         m_impl->frame->setZoomFactor(m_textMagnifier, true);
308     }
309 }
310
311 void wxWebFrame::MakeEditable(bool enable)
312 {
313     m_isEditable = enable;
314 }
315
316
317
318 bool wxWebFrame::CanCopy()
319 {
320     if (m_impl->frame && m_impl->frame->view())
321         return (m_impl->frame->editor()->canCopy() || m_impl->frame->editor()->canDHTMLCopy());
322
323     return false;
324 }
325
326 void wxWebFrame::Copy()
327 {
328     if (CanCopy())
329         m_impl->frame->editor()->copy();
330 }
331
332 bool wxWebFrame::CanCut()
333 {
334     if (m_impl->frame && m_impl->frame->view())
335         return (m_impl->frame->editor()->canCut() || m_impl->frame->editor()->canDHTMLCut());
336
337     return false;
338 }
339
340 void wxWebFrame::Cut()
341 {
342     if (CanCut())
343         m_impl->frame->editor()->cut();
344 }
345
346 bool wxWebFrame::CanPaste()
347 {
348     if (m_impl->frame && m_impl->frame->view())
349         return (m_impl->frame->editor()->canPaste() || m_impl->frame->editor()->canDHTMLPaste());
350
351     return false;
352 }
353
354 void wxWebFrame::Paste()
355 {
356     if (CanPaste())
357         m_impl->frame->editor()->paste();
358
359 }
360
361 wxWebViewDOMElementInfo wxWebFrame::HitTest(const wxPoint& pos) const
362 {
363     wxWebViewDOMElementInfo domInfo;
364
365     if (m_impl->frame->view()) {
366         WebCore::HitTestResult result = m_impl->frame->eventHandler()->hitTestResultAtPoint(m_impl->frame->view()->windowToContents(pos), false);
367         if (result.innerNode()) {
368             domInfo.SetLink(result.absoluteLinkURL().string());
369             domInfo.SetText(result.textContent());
370             domInfo.SetImageSrc(result.absoluteImageURL().string());
371             domInfo.SetSelected(result.isSelected());
372         }
373     }
374
375     return domInfo;
376 }
377