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"
32 #include "FocusController.h"
34 #include "FrameLoader.h"
35 #include "FrameView.h"
36 #include "GraphicsContext.h"
40 #include "ContextMenu.h"
41 #include "ContextMenuItem.h"
42 #include "ContextMenuController.h"
43 #include "PlatformKeyboardEvent.h"
44 #include "PlatformMouseEvent.h"
45 #include "PlatformString.h"
46 #include "PlatformWheelEvent.h"
47 #include "RenderObject.h"
48 #include "RenderView.h"
49 #include "SelectionController.h"
51 #include "SubstituteData.h"
53 #include "ChromeClientWx.h"
54 #include "ContextMenuClientWx.h"
55 #include "DragClientWx.h"
56 #include "EditorClientWx.h"
57 #include "FrameLoaderClientWx.h"
58 #include "InspectorClientWx.h"
60 #include "ScriptController.h"
61 #include "JSDOMBinding.h"
62 #include <runtime/JSValue.h>
63 #include <runtime/UString.h>
65 #include "wx/wxprec.h"
72 #include "WebViewPrivate.h"
75 #include <wx/dcbuffer.h>
76 #include <wx/dcgraph.h>
81 return (int)(val < 0 ? val - 0.5 : val + 0.5);
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 IMPLEMENT_DYNAMIC_CLASS(wxWebViewLoadEvent, wxCommandEvent)
91 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_LOAD)
93 wxWebViewLoadEvent::wxWebViewLoadEvent(wxWindow* win)
95 SetEventType( wxEVT_WEBVIEW_LOAD);
96 SetEventObject( win );
101 IMPLEMENT_DYNAMIC_CLASS(wxWebViewBeforeLoadEvent, wxCommandEvent)
103 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_BEFORE_LOAD)
105 wxWebViewBeforeLoadEvent::wxWebViewBeforeLoadEvent(wxWindow* win)
108 SetEventType(wxEVT_WEBVIEW_BEFORE_LOAD);
114 IMPLEMENT_DYNAMIC_CLASS(wxWebViewNewWindowEvent, wxCommandEvent)
116 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_NEW_WINDOW)
118 wxWebViewNewWindowEvent::wxWebViewNewWindowEvent(wxWindow* win)
120 SetEventType(wxEVT_WEBVIEW_NEW_WINDOW);
126 IMPLEMENT_DYNAMIC_CLASS(wxWebViewRightClickEvent, wxCommandEvent)
128 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_RIGHT_CLICK)
130 wxWebViewRightClickEvent::wxWebViewRightClickEvent(wxWindow* win)
132 SetEventType(wxEVT_WEBVIEW_RIGHT_CLICK);
138 IMPLEMENT_DYNAMIC_CLASS(wxWebViewConsoleMessageEvent, wxCommandEvent)
140 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_CONSOLE_MESSAGE)
142 wxWebViewConsoleMessageEvent::wxWebViewConsoleMessageEvent(wxWindow* win)
144 SetEventType(wxEVT_WEBVIEW_CONSOLE_MESSAGE);
150 IMPLEMENT_DYNAMIC_CLASS(wxWebViewReceivedTitleEvent, wxCommandEvent)
152 DEFINE_EVENT_TYPE(wxEVT_WEBVIEW_RECEIVED_TITLE)
154 wxWebViewReceivedTitleEvent::wxWebViewReceivedTitleEvent(wxWindow* win)
156 SetEventType(wxEVT_WEBVIEW_RECEIVED_TITLE);
162 //---------------------------------------------------------
163 // DOM Element info data type
164 //---------------------------------------------------------
166 wxWebViewDOMElementInfo::wxWebViewDOMElementInfo() :
169 m_text(wxEmptyString),
170 m_imageSrc(wxEmptyString),
171 m_link(wxEmptyString)
175 BEGIN_EVENT_TABLE(wxWebView, wxWindow)
176 EVT_PAINT(wxWebView::OnPaint)
177 EVT_SIZE(wxWebView::OnSize)
178 EVT_MOUSE_EVENTS(wxWebView::OnMouseEvents)
179 EVT_CONTEXT_MENU(wxWebView::OnContextMenuEvents)
180 EVT_MENU(wxID_ANY, wxWebView::OnMenuSelectEvents)
181 EVT_KEY_DOWN(wxWebView::OnKeyEvents)
182 EVT_KEY_UP(wxWebView::OnKeyEvents)
183 EVT_CHAR(wxWebView::OnKeyEvents)
184 EVT_SET_FOCUS(wxWebView::OnSetFocus)
185 EVT_KILL_FOCUS(wxWebView::OnKillFocus)
186 EVT_ACTIVATE(wxWebView::OnActivate)
189 IMPLEMENT_DYNAMIC_CLASS(wxWebView, wxWindow)
191 const wxChar* wxWebViewNameStr = wxT("webView");
193 wxWebView::wxWebView() :
194 m_textMagnifier(1.0),
196 m_isInitialized(false),
197 m_beingDestroyed(false),
198 m_title(wxEmptyString)
202 wxWebView::wxWebView(wxWindow* parent, int id, const wxPoint& position,
203 const wxSize& size, long style, const wxString& name) :
204 m_textMagnifier(1.0),
206 m_isInitialized(false),
207 m_beingDestroyed(false),
208 m_title(wxEmptyString)
210 Create(parent, id, position, size, style, name);
213 bool wxWebView::Create(wxWindow* parent, int id, const wxPoint& position,
214 const wxSize& size, long style, const wxString& name)
216 if ( (style & wxBORDER_MASK) == 0)
217 style |= wxBORDER_NONE;
218 style |= wxHSCROLL | wxVSCROLL;
220 if (!wxWindow::Create(parent, id, position, size, style, name))
223 // This is necessary because we are using SharedTimerWin.cpp on Windows,
224 // due to a problem with exceptions getting eaten when using the callback
225 // approach to timers (which wx itself uses).
227 WebCore::Page::setInstanceHandle(wxGetInstance());
230 // this helps reduce flicker on platforms like MSW
231 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
233 m_impl = new WebViewPrivate();
235 WebCore::InitializeLoggingChannelsIfNecessary();
236 WebCore::HTMLFrameOwnerElement* parentFrame = 0;
238 WebCore::EditorClientWx* editorClient = new WebCore::EditorClientWx();
239 m_impl->page = new WebCore::Page(new WebCore::ChromeClientWx(this), new WebCore::ContextMenuClientWx(), editorClient, new WebCore::DragClientWx(), new WebCore::InspectorClientWx());
240 editorClient->setPage(m_impl->page);
242 m_mainFrame = new wxWebFrame(this);
244 // Default settings - we should have wxWebViewSettings class for this
246 WebCore::Settings* settings = m_impl->page->settings();
247 settings->setLoadsImagesAutomatically(true);
248 settings->setDefaultFixedFontSize(13);
249 settings->setDefaultFontSize(16);
250 settings->setSerifFontFamily("Times New Roman");
251 settings->setFixedFontFamily("Courier New");
252 settings->setSansSerifFontFamily("Arial");
253 settings->setStandardFontFamily("Times New Roman");
254 settings->setJavaScriptEnabled(true);
256 m_isInitialized = true;
261 wxWebView::~wxWebView()
263 m_beingDestroyed = true;
271 void wxWebView::Stop()
277 void wxWebView::Reload()
280 m_mainFrame->Reload();
283 wxString wxWebView::GetPageSource()
286 return m_mainFrame->GetPageSource();
288 return wxEmptyString;
291 void wxWebView::SetPageSource(const wxString& source, const wxString& baseUrl)
294 m_mainFrame->SetPageSource(source, baseUrl);
297 wxString wxWebView::GetInnerText()
300 return m_mainFrame->GetInnerText();
302 return wxEmptyString;
305 wxString wxWebView::GetAsMarkup()
308 return m_mainFrame->GetAsMarkup();
310 return wxEmptyString;
313 wxString wxWebView::GetExternalRepresentation()
316 return m_mainFrame->GetExternalRepresentation();
318 return wxEmptyString;
321 void wxWebView::SetTransparent(bool transparent)
323 WebCore::Frame* frame = 0;
325 frame = m_mainFrame->GetFrame();
327 if (!frame || !frame->view())
330 frame->view()->setTransparent(transparent);
333 bool wxWebView::IsTransparent() const
335 WebCore::Frame* frame = 0;
337 frame = m_mainFrame->GetFrame();
339 if (!frame || !frame->view())
342 return frame->view()->isTransparent();
345 wxString wxWebView::RunScript(const wxString& javascript)
348 return m_mainFrame->RunScript(javascript);
350 return wxEmptyString;
353 void wxWebView::LoadURL(const wxString& url)
356 m_mainFrame->LoadURL(url);
359 bool wxWebView::GoBack()
362 return m_mainFrame->GoBack();
367 bool wxWebView::GoForward()
370 return m_mainFrame->GoForward();
375 bool wxWebView::CanGoBack()
378 return m_mainFrame->CanGoBack();
383 bool wxWebView::CanGoForward()
386 return m_mainFrame->CanGoForward();
391 bool wxWebView::CanIncreaseTextSize() const
394 return m_mainFrame->CanIncreaseTextSize();
399 void wxWebView::IncreaseTextSize()
402 m_mainFrame->IncreaseTextSize();
405 bool wxWebView::CanDecreaseTextSize() const
408 m_mainFrame->CanDecreaseTextSize();
413 void wxWebView::DecreaseTextSize()
416 m_mainFrame->DecreaseTextSize();
419 void wxWebView::MakeEditable(bool enable)
421 m_isEditable = enable;
426 * Event forwarding functions to send events down to WebCore.
429 void wxWebView::OnPaint(wxPaintEvent& event)
432 if (m_beingDestroyed || !m_mainFrame)
435 WebCore::Frame* frame = m_mainFrame->GetFrame();
436 if (!frame || !frame->view())
439 wxAutoBufferedPaintDC dc(this);
441 if (IsShown() && frame->document()) {
447 wxRect paintRect = GetUpdateRegion().GetBox();
449 WebCore::IntSize offset = frame->view()->scrollOffset();
451 gcdc.SetDeviceOrigin(-offset.width(), -offset.height());
453 dc.SetDeviceOrigin(-offset.width(), -offset.height());
454 paintRect.Offset(offset.width(), offset.height());
457 WebCore::GraphicsContext* gc = new WebCore::GraphicsContext(&gcdc);
459 WebCore::GraphicsContext* gc = new WebCore::GraphicsContext((wxWindowDC*)&dc);
461 if (gc && frame->contentRenderer()) {
462 if (frame->view()->needsLayout())
463 frame->view()->layout();
465 frame->view()->paintContents(gc, paintRect);
472 bool wxWebView::FindString(const wxString& string, bool forward, bool caseSensitive, bool wrapSelection, bool startInSelection)
475 return m_mainFrame->FindString(string, forward, caseSensitive, wrapSelection, startInSelection);
480 void wxWebView::OnSize(wxSizeEvent& event)
482 if (m_isInitialized && m_mainFrame) {
483 WebCore::Frame* frame = m_mainFrame->GetFrame();
484 frame->eventHandler()->sendResizeEvent();
485 frame->view()->layout();
486 frame->view()->adjustScrollbars();
492 void wxWebView::OnMouseEvents(wxMouseEvent& event)
499 WebCore::Frame* frame = m_mainFrame->GetFrame();
500 if (!frame || !frame->view())
503 wxPoint globalPoint = ClientToScreen(event.GetPosition());
505 wxEventType type = event.GetEventType();
507 if (type == wxEVT_MOUSEWHEEL) {
508 WebCore::PlatformWheelEvent wkEvent(event, globalPoint);
509 frame->eventHandler()->handleWheelEvent(wkEvent);
513 WebCore::PlatformMouseEvent wkEvent(event, globalPoint);
515 if (type == wxEVT_LEFT_DOWN || type == wxEVT_MIDDLE_DOWN || type == wxEVT_RIGHT_DOWN ||
516 type == wxEVT_LEFT_DCLICK || type == wxEVT_MIDDLE_DCLICK || type == wxEVT_RIGHT_DCLICK)
517 frame->eventHandler()->handleMousePressEvent(wkEvent);
519 else if (type == wxEVT_LEFT_UP || type == wxEVT_MIDDLE_UP || type == wxEVT_RIGHT_UP)
520 frame->eventHandler()->handleMouseReleaseEvent(wkEvent);
522 else if (type == wxEVT_MOTION)
523 frame->eventHandler()->mouseMoved(wkEvent);
526 void wxWebView::OnContextMenuEvents(wxContextMenuEvent& event)
528 m_impl->page->contextMenuController()->clearContextMenu();
529 wxPoint localEventPoint = ScreenToClient(event.GetPosition());
534 WebCore::Frame* focusedFrame = m_mainFrame->GetFrame();
535 if (!focusedFrame->view())
538 //Create WebCore mouse event from the wxContextMenuEvent
539 wxMouseEvent mouseEvent(wxEVT_RIGHT_DOWN);
540 mouseEvent.m_x = localEventPoint.x;
541 mouseEvent.m_y = localEventPoint.y;
542 WebCore::PlatformMouseEvent wkEvent(mouseEvent, event.GetPosition());
544 bool handledEvent = focusedFrame->eventHandler()->sendContextMenuEvent(wkEvent);
548 WebCore::ContextMenu* coreMenu = m_impl->page->contextMenuController()->contextMenu();
552 WebCore::PlatformMenuDescription menuWx = coreMenu->platformDescription();
556 PopupMenu(menuWx, localEventPoint);
559 void wxWebView::OnMenuSelectEvents(wxCommandEvent& event)
561 WebCore::ContextMenuItem* item = WebCore::ContextMenu::itemWithId (event.GetId());
565 m_impl->page->contextMenuController()->contextMenuItemSelected(item);
569 bool wxWebView::CanCopy()
572 return m_mainFrame->CanCopy();
577 void wxWebView::Copy()
583 bool wxWebView::CanCut()
586 m_mainFrame->CanCut();
591 void wxWebView::Cut()
597 bool wxWebView::CanPaste()
600 m_mainFrame->CanPaste();
605 void wxWebView::Paste()
608 m_mainFrame->Paste();
612 void wxWebView::OnKeyEvents(wxKeyEvent& event)
614 WebCore::Frame* frame = 0;
616 frame = m_mainFrame->GetFrame();
618 if (frame && frame->view()) {
619 // WebCore doesn't handle these events itself, so we need to do
620 // it and not send the event down or else CTRL+C will erase the text
621 // and replace it with c.
622 if (event.CmdDown() && event.GetEventType() == wxEVT_KEY_UP) {
623 if (event.GetKeyCode() == static_cast<int>('C'))
625 else if (event.GetKeyCode() == static_cast<int>('X'))
627 else if (event.GetKeyCode() == static_cast<int>('V'))
629 else if (event.GetKeyCode() == static_cast<int>('Z')) {
630 if (event.ShiftDown()) {
631 if (m_mainFrame->CanRedo())
635 if (m_mainFrame->CanUndo())
640 WebCore::PlatformKeyboardEvent wkEvent(event);
641 if (wkEvent.type() == WebCore::PlatformKeyboardEvent::Char && wkEvent.altKey())
642 frame->eventHandler()->handleAccessKey(wkEvent);
644 frame->eventHandler()->keyEvent(wkEvent);
648 // make sure we get the character event.
649 if (event.GetEventType() != wxEVT_CHAR)
653 void wxWebView::OnSetFocus(wxFocusEvent& event)
655 WebCore::Frame* frame = 0;
657 frame = m_mainFrame->GetFrame();
660 m_impl->page->focusController()->setActive(true);
661 frame->selection()->setFocused(true);
667 void wxWebView::OnKillFocus(wxFocusEvent& event)
669 WebCore::Frame* frame = 0;
671 frame = m_mainFrame->GetFrame();
674 m_impl->page->focusController()->setActive(false);
675 frame->selection()->setFocused(false);
680 void wxWebView::OnActivate(wxActivateEvent& event)
683 m_impl->page->focusController()->setActive(event.GetActive());
688 wxWebViewDOMElementInfo wxWebView::HitTest(const wxPoint& pos) const
691 return m_mainFrame->HitTest(pos);
693 return wxWebViewDOMElementInfo();