2 * Copyright (C) 2007 Kevin Ollivier All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #include "EventHandler.h"
33 #include "FrameLoader.h"
34 #include "FrameView.h"
35 #include "HitTestResult.h"
36 #include "HTMLFrameOwnerElement.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"
46 #include "JSDOMBinding.h"
47 #include <runtime/JSValue.h>
48 #include <runtime/UString.h>
50 #include "EditorClientWx.h"
51 #include "FrameLoaderClientWx.h"
53 #include "wx/wxprec.h"
60 #include "WebFramePrivate.h"
61 #include "WebViewPrivate.h"
64 #include <wx/dcbuffer.h>
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
71 wxWebFrame::wxWebFrame(wxWebView* container, wxWebFrame* parent, WebViewFrameData* data) :
74 m_isInitialized(false),
75 m_beingDestroyed(false),
76 m_title(wxEmptyString)
79 m_impl = new WebFramePrivate();
81 WebCore::HTMLFrameOwnerElement* parentFrame = 0;
84 parentFrame = data->ownerElement;
87 WebCore::FrameLoaderClientWx* loaderClient = new WebCore::FrameLoaderClientWx();
89 m_impl->frame = WebCore::Frame::create(container->m_impl->page, parentFrame, loaderClient);
90 m_impl->frame->deref();
92 loaderClient->setFrame(m_impl->frame.get());
93 loaderClient->setWebView(container);
95 m_impl->frame->init();
97 m_isInitialized = true;
100 wxWebFrame::~wxWebFrame()
102 m_impl->frame->loader()->detachFromParent();
105 WebCore::Frame* wxWebFrame::GetFrame()
108 return m_impl->frame.get();
113 void wxWebFrame::Stop()
115 if (m_impl->frame && m_impl->frame->loader())
116 m_impl->frame->loader()->stop();
119 void wxWebFrame::Reload()
121 if (m_impl->frame && m_impl->frame->loader())
122 m_impl->frame->loader()->reload();
125 wxString wxWebFrame::GetPageSource()
128 if (m_impl->frame->view() && m_impl->frame->view()->layoutPending())
129 m_impl->frame->view()->layout();
131 WebCore::Document* doc = m_impl->frame->document();
134 wxString source = createMarkup(doc);
138 return wxEmptyString;
141 void wxWebFrame::SetPageSource(const wxString& source, const wxString& baseUrl)
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));
151 wxString wxWebFrame::GetInnerText()
153 if (m_impl->frame->view() && m_impl->frame->view()->layoutPending())
154 m_impl->frame->view()->layout();
156 WebCore::Element *documentElement = m_impl->frame->document()->documentElement();
157 return documentElement->innerText();
160 wxString wxWebFrame::GetAsMarkup()
162 if (!m_impl->frame || !m_impl->frame->document())
163 return wxEmptyString;
165 return createMarkup(m_impl->frame->document());
168 wxString wxWebFrame::GetExternalRepresentation()
170 if (m_impl->frame->view() && m_impl->frame->view()->layoutPending())
171 m_impl->frame->view()->layout();
173 return externalRepresentation(m_impl->frame->contentRenderer());
176 wxString wxWebFrame::RunScript(const wxString& javascript)
178 wxString returnValue = wxEmptyString;
180 JSC::JSValuePtr result = m_impl->frame->loader()->executeScript(javascript, true).jsValue();
182 returnValue = wxString(result.toString(m_impl->frame->script()->globalObject()->globalExec()).UTF8String().c_str(), wxConvUTF8);
187 bool wxWebFrame::FindString(const wxString& string, bool forward, bool caseSensitive, bool wrapSelection, bool startInSelection)
190 return m_impl->frame->findString(string, forward, caseSensitive, wrapSelection, startInSelection);
195 void wxWebFrame::LoadURL(const wxString& url)
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());
209 kurl.setProtocol("http");
210 kurl.setPath("//" + kurl.path());
213 m_impl->frame->loader()->load(kurl, false);
217 bool wxWebFrame::GoBack()
219 if (m_impl->frame && m_impl->frame->page())
220 return m_impl->frame->page()->goBack();
225 bool wxWebFrame::GoForward()
227 if (m_impl->frame && m_impl->frame->page())
228 return m_impl->frame->page()->goForward();
233 bool wxWebFrame::CanGoBack()
235 if (m_impl->frame && m_impl->frame->page() && m_impl->frame->page()->backForwardList())
236 return m_impl->frame->page()->backForwardList()->backItem() != NULL;
241 bool wxWebFrame::CanGoForward()
243 if (m_impl->frame && m_impl->frame->page() && m_impl->frame->page()->backForwardList())
244 return m_impl->frame->page()->backForwardList()->forwardItem() != NULL;
249 void wxWebFrame::Undo()
251 if (m_impl->frame && m_impl->frame->editor() && CanUndo())
252 return m_impl->frame->editor()->undo();
255 void wxWebFrame::Redo()
257 if (m_impl->frame && m_impl->frame->editor() && CanRedo())
258 return m_impl->frame->editor()->redo();
261 bool wxWebFrame::CanUndo()
263 if (m_impl->frame && m_impl->frame->editor())
264 return m_impl->frame->editor()->canUndo();
269 bool wxWebFrame::CanRedo()
271 if (m_impl->frame && m_impl->frame->editor())
272 return m_impl->frame->editor()->canRedo();
277 bool wxWebFrame::CanIncreaseTextSize() const
280 if (m_textMagnifier*TextSizeMultiplierRatio <= MaximumTextSizeMultiplier)
286 void wxWebFrame::IncreaseTextSize()
288 if (CanIncreaseTextSize()) {
289 m_textMagnifier = m_textMagnifier*TextSizeMultiplierRatio;
290 m_impl->frame->setZoomFactor(m_textMagnifier, true);
294 bool wxWebFrame::CanDecreaseTextSize() const
297 if (m_textMagnifier/TextSizeMultiplierRatio >= MinimumTextSizeMultiplier)
303 void wxWebFrame::DecreaseTextSize()
305 if (CanDecreaseTextSize()) {
306 m_textMagnifier = m_textMagnifier/TextSizeMultiplierRatio;
307 m_impl->frame->setZoomFactor(m_textMagnifier, true);
311 void wxWebFrame::MakeEditable(bool enable)
313 m_isEditable = enable;
318 bool wxWebFrame::CanCopy()
320 if (m_impl->frame && m_impl->frame->view())
321 return (m_impl->frame->editor()->canCopy() || m_impl->frame->editor()->canDHTMLCopy());
326 void wxWebFrame::Copy()
329 m_impl->frame->editor()->copy();
332 bool wxWebFrame::CanCut()
334 if (m_impl->frame && m_impl->frame->view())
335 return (m_impl->frame->editor()->canCut() || m_impl->frame->editor()->canDHTMLCut());
340 void wxWebFrame::Cut()
343 m_impl->frame->editor()->cut();
346 bool wxWebFrame::CanPaste()
348 if (m_impl->frame && m_impl->frame->view())
349 return (m_impl->frame->editor()->canPaste() || m_impl->frame->editor()->canDHTMLPaste());
354 void wxWebFrame::Paste()
357 m_impl->frame->editor()->paste();
361 wxWebViewDOMElementInfo wxWebFrame::HitTest(const wxPoint& pos) const
363 wxWebViewDOMElementInfo domInfo;
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());