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.
26 #include "wx/wxprec.h"
32 #include "DeprecatedString.h"
35 #include "EventHandler.h"
37 #include "FrameLoader.h"
38 #include "FrameView.h"
39 #include "GraphicsContext.h"
40 #include "HTMLFrameOwnerElement.h"
43 #include "PlatformKeyboardEvent.h"
44 #include "PlatformMouseEvent.h"
45 #include "PlatformString.h"
46 #include "PlatformWheelEvent.h"
47 #include "RenderObject.h"
50 #include "ChromeClientWx.h"
51 #include "ContextMenuClientWx.h"
52 #include "DragClientWx.h"
53 #include "EditorClientWx.h"
54 #include "FrameLoaderClientWx.h"
55 #include "InspectorClientWx.h"
57 #include "kjs_proxy.h"
58 #include "kjs_binding.h"
59 #include <kjs/value.h>
60 #include <kjs/ustring.h>
63 #include "WebViewPrivate.h"
66 #include <wx/dcbuffer.h>
68 // Match Safari's min/max zoom sizes by default
69 #define MinimumTextSizeMultiplier 0.5f
70 #define MaximumTextSizeMultiplier 3.0f
71 #define TextSizeMultiplierRatio 1.2f
77 return (int)(val < 0 ? val - 0.5 : val + 0.5);
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 IMPLEMENT_DYNAMIC_CLASS(wxWebViewStateChangedEvent, wxCommandEvent)
87 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_STATE_CHANGED)
89 wxWebViewStateChangedEvent::wxWebViewStateChangedEvent(wxWindow* win)
91 SetEventType( wxEVT_WEBVIEW_STATE_CHANGED);
92 SetEventObject( win );
96 IMPLEMENT_DYNAMIC_CLASS(wxWebViewBeforeLoadEvent, wxCommandEvent)
98 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_BEFORE_LOAD)
100 wxWebViewBeforeLoadEvent::wxWebViewBeforeLoadEvent(wxWindow* win)
103 SetEventType(wxEVT_WEBVIEW_BEFORE_LOAD);
108 IMPLEMENT_DYNAMIC_CLASS(wxWebViewNewWindowEvent, wxCommandEvent)
110 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_NEW_WINDOW)
112 wxWebViewNewWindowEvent::wxWebViewNewWindowEvent(wxWindow* win)
114 SetEventType(wxEVT_WEBVIEW_NEW_WINDOW);
119 IMPLEMENT_DYNAMIC_CLASS(wxWebViewRightClickEvent, wxCommandEvent)
121 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_RIGHT_CLICK)
123 wxWebViewRightClickEvent::wxWebViewRightClickEvent(wxWindow* win)
125 SetEventType(wxEVT_WEBVIEW_RIGHT_CLICK);
130 //---------------------------------------------------------
131 // DOM Element info data type
132 //---------------------------------------------------------
134 wxWebViewDOMElementInfo::wxWebViewDOMElementInfo() :
137 m_text(wxEmptyString),
138 m_imageSrc(wxEmptyString),
139 m_link(wxEmptyString)
143 BEGIN_EVENT_TABLE(wxWebView, wxScrolledWindow)
144 EVT_PAINT(wxWebView::OnPaint)
145 EVT_SIZE(wxWebView::OnSize)
146 EVT_MOUSE_EVENTS(wxWebView::OnMouseEvents)
147 EVT_KEY_DOWN(wxWebView::OnKeyEvents)
148 EVT_KEY_UP(wxWebView::OnKeyEvents)
149 EVT_CHAR(wxWebView::OnKeyEvents)
150 EVT_SET_FOCUS(wxWebView::OnSetFocus)
151 EVT_KILL_FOCUS(wxWebView::OnKillFocus)
152 EVT_ACTIVATE(wxWebView::OnActivate)
155 wxWebView::wxWebView(wxWindow* parent, int id, const wxPoint& position,
156 const wxSize& size, WebViewFrameData* data) :
157 m_textMagnifier(1.0),
159 m_isInitialized(false),
160 m_beingDestroyed(false),
161 m_title(wxEmptyString)
163 if (!wxScrolledWindow::Create(parent, id, position, size))
166 // this helps reduce flicker on platforms like MSW
167 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
169 m_impl = new WebViewPrivate();
171 WebCore::InitializeLoggingChannelsIfNecessary();
172 WebCore::HTMLFrameOwnerElement* parentFrame = 0;
173 WebCore::Page* page = 0;
175 // FIXME: This cast is obviously not as safe as a dynamic
176 // cast, but this allows us to get around requiring RTTI
177 // support for the moment. This is only used for subframes
178 // in any case, which aren't currently supported.
179 wxWebView* parentWebView = static_cast<wxWebView*>(parent);
182 parentFrame = data->ownerElement;
183 page = parentWebView->m_impl->frame->page();
186 WebCore::EditorClientWx* editorClient = new WebCore::EditorClientWx();
187 page = new WebCore::Page(new WebCore::ChromeClientWx(), new WebCore::ContextMenuClientWx(), editorClient, new WebCore::DragClientWx(), new WebCore::InspectorClientWx());
188 editorClient->setPage(page);
191 WebCore::FrameLoaderClientWx* loaderClient = new WebCore::FrameLoaderClientWx();
193 m_impl->frame = new WebCore::Frame(page, parentFrame, loaderClient);
194 m_impl->frame->deref();
195 m_impl->frameView = new WebCore::FrameView(m_impl->frame.get());
196 m_impl->frameView->deref();
198 m_impl->frame->setView(m_impl->frameView.get());
199 m_impl->frame->init();
201 m_impl->frameView->setNativeWindow(this);
202 loaderClient->setFrame(m_impl->frame.get());
204 // Default settings - we should have wxWebViewSettings class for this
206 WebCore::Settings* settings = page->settings();
207 settings->setLoadsImagesAutomatically(true);
208 settings->setDefaultFixedFontSize(13);
209 settings->setDefaultFontSize(16);
210 settings->setSerifFontFamily("Times New Roman");
211 settings->setFixedFontFamily("Courier New");
212 settings->setSansSerifFontFamily("Arial");
213 settings->setStandardFontFamily("Times New Roman");
214 settings->setJavaScriptEnabled(true);
216 m_isInitialized = true;
219 wxWebView::~wxWebView()
221 m_beingDestroyed = true;
223 m_impl->frame->loader()->detachFromParent();
225 // This test determines whether or not the frame is a subframe
226 // or the main (top level) frame. If it's the main frame, then
227 // delete its page to keep leaks from occurring
228 if (!m_impl->frame->ownerElement()) {
229 delete m_impl->frame->page();
231 m_impl->frameView = 0;
237 void wxWebView::Stop()
239 if (m_impl->frame && m_impl->frame->loader())
240 m_impl->frame->loader()->stop();
243 void wxWebView::Reload()
245 if (m_impl->frame && m_impl->frame->loader())
246 m_impl->frame->loader()->reload();
249 wxString wxWebView::GetPageSource()
252 WebCore::Document* doc = m_impl->frame->document();
255 wxString source = doc->toString();
259 return wxEmptyString;
262 void wxWebView::SetPageSource(const wxString& source, const wxString& baseUrl)
264 if (m_impl->frame && m_impl->frame->loader()) {
265 WebCore::FrameLoader* loader = m_impl->frame->loader();
266 loader->begin(WebCore::KURL(static_cast<const char*>(baseUrl.mb_str(wxConvUTF8))));
267 loader->write(source);
272 wxString wxWebView::RunScript(const wxString& javascript)
274 wxString returnValue = wxEmptyString;
276 KJS::JSValue* result = m_impl->frame->loader()->executeScript(javascript, true);
278 returnValue = wxString(result->toString(m_impl->frame->scriptProxy()->interpreter()->globalExec()).UTF8String().c_str(), wxConvUTF8);
283 void wxWebView::LoadURL(wxString url)
285 if (m_impl->frame && m_impl->frame->loader()) {
286 WebCore::KURL kurl = WebCore::KURL(static_cast<const char*>(url.mb_str(wxConvUTF8)));
287 // NB: This is an ugly fix, but CURL won't load sub-resources if the
288 // protocol is omitted; sadly, it will not emit an error, either, so
289 // there's no way for us to catch this problem the correct way yet.
290 if (kurl.protocol().isEmpty()) {
291 // is it a file on disk?
292 if (wxFileExists(url)) {
293 kurl.setProtocol("file");
294 kurl.setPath("//" + kurl.path());
297 kurl.setProtocol("http");
298 kurl.setPath("//" + kurl.path());
301 m_impl->frame->loader()->load(kurl);
305 bool wxWebView::GoBack()
307 if (m_impl->frame && m_impl->frame->page()) {
308 return m_impl->frame->page()->goBack();
312 bool wxWebView::GoForward()
314 if (m_impl->frame && m_impl->frame->page())
315 return m_impl->frame->page()->goForward();
318 bool wxWebView::CanIncreaseTextSize() const
321 if (m_textMagnifier*TextSizeMultiplierRatio <= MaximumTextSizeMultiplier)
327 void wxWebView::IncreaseTextSize()
329 if (CanIncreaseTextSize()) {
330 m_textMagnifier = m_textMagnifier*TextSizeMultiplierRatio;
331 m_impl->frame->setZoomFactor((int)rint(m_textMagnifier*100));
335 bool wxWebView::CanDecreaseTextSize() const
338 if (m_textMagnifier/TextSizeMultiplierRatio >= MinimumTextSizeMultiplier)
344 void wxWebView::DecreaseTextSize()
346 if (CanDecreaseTextSize()) {
347 m_textMagnifier = m_textMagnifier/TextSizeMultiplierRatio;
348 m_impl->frame->setZoomFactor( (int)rint(m_textMagnifier*100));
352 void wxWebView::MakeEditable(bool enable)
354 m_isEditable = enable;
359 * Event forwarding functions to send events down to WebCore.
362 void wxWebView::OnPaint(wxPaintEvent& event)
364 if (m_beingDestroyed || !m_impl->frameView || !m_impl->frame)
367 wxAutoBufferedPaintDC dc(this);
370 if (IsShown() && m_impl->frame && m_impl->frame->document()) {
376 wxRect paintRect = GetUpdateRegion().GetBox();
379 GetViewStart(&x, &y);
382 GetScrollPixelsPerUnit(&unitX, &unitY);
383 paintRect.Offset(x * unitX, y * unitY);
386 WebCore::GraphicsContext* gc = new WebCore::GraphicsContext(&gcdc);
388 WebCore::GraphicsContext* gc = new WebCore::GraphicsContext((wxWindowDC*)&dc);
390 if (gc && m_impl->frame->renderer()) {
391 // FIXME: Replace this with layoutIfNeededRecursive
392 if (m_impl->frameView->needsLayout())
393 m_impl->frameView->layout();
395 m_impl->frame->paint(gc, paintRect);
401 void wxWebView::OnSize(wxSizeEvent& event)
403 // NOTE: this call can be expensive on heavy pages, particularly on Mac,
404 // so we probably should set a timer not put x ms between layouts.
406 if (m_isInitialized && m_impl->frame && m_impl->frameView) {
407 m_impl->frameView->layout();
414 void wxWebView::OnMouseEvents(wxMouseEvent& event)
418 if (!m_impl->frame && m_impl->frameView)
421 wxPoint globalPoint = ClientToScreen(event.GetPosition());
423 wxEventType type = event.GetEventType();
425 if (type == wxEVT_MOUSEWHEEL) {
426 WebCore::PlatformWheelEvent wkEvent(event, globalPoint);
427 m_impl->frame->eventHandler()->handleWheelEvent(wkEvent);
431 WebCore::PlatformMouseEvent wkEvent(event, globalPoint);
433 if (type == wxEVT_LEFT_DOWN || type == wxEVT_MIDDLE_DOWN || type == wxEVT_RIGHT_DOWN)
434 m_impl->frame->eventHandler()->handleMousePressEvent(wkEvent);
436 else if (type == wxEVT_LEFT_UP || type == wxEVT_MIDDLE_UP || type == wxEVT_RIGHT_UP ||
437 type == wxEVT_LEFT_DCLICK || type == wxEVT_MIDDLE_DCLICK || type == wxEVT_RIGHT_DCLICK)
438 m_impl->frame->eventHandler()->handleMouseReleaseEvent(wkEvent);
440 else if (type == wxEVT_MOTION)
441 m_impl->frame->eventHandler()->handleMouseMoveEvent(wkEvent);
444 bool wxWebView::CanCopy()
446 if (m_impl->frame && m_impl->frameView) {
447 return (m_impl->frame->editor()->canCopy() || m_impl->frame->editor()->canDHTMLCopy());
452 void wxWebView::Copy()
455 m_impl->frame->editor()->copy();
459 bool wxWebView::CanCut()
461 if (m_impl->frame && m_impl->frameView) {
462 return (m_impl->frame->editor()->canCut() || m_impl->frame->editor()->canDHTMLCut());
467 void wxWebView::Cut()
470 m_impl->frame->editor()->cut();
474 bool wxWebView::CanPaste()
476 if (m_impl->frame && m_impl->frameView) {
477 return (m_impl->frame->editor()->canPaste() || m_impl->frame->editor()->canDHTMLPaste());
482 void wxWebView::Paste()
485 m_impl->frame->editor()->paste();
489 void wxWebView::OnKeyEvents(wxKeyEvent& event)
491 if (m_impl->frame && m_impl->frameView) {
492 // WebCore doesn't handle these events itself, so we need to do
493 // it and not send the event down or else CTRL+C will erase the text
494 // and replace it with c.
495 if (event.CmdDown() && event.GetKeyCode() == static_cast<int>('C')) {
498 else if (event.CmdDown() && event.GetKeyCode() == static_cast<int>('X')) {
501 else if (event.CmdDown() && event.GetKeyCode() == static_cast<int>('V')) {
505 WebCore::PlatformKeyboardEvent wkEvent(event);
506 m_impl->frame->eventHandler()->keyEvent(wkEvent);
510 // make sure we get the character event.
511 if (event.GetEventType() != wxEVT_CHAR)
515 void wxWebView::OnSetFocus(wxFocusEvent& event)
518 m_impl->frame->setWindowHasFocus(true);
523 void wxWebView::OnKillFocus(wxFocusEvent& event)
526 m_impl->frame->setWindowHasFocus(false);
531 void wxWebView::OnActivate(wxActivateEvent& event)
534 m_impl->frame->setIsActive(event.GetActive());