1/*
2 * Copyright (C) 2007 Kevin Ollivier <kevino@theolliviers.com>
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "ChromeClientWx.h"
31#include "Console.h"
32#if ENABLE(DATABASE)
33#include "DatabaseTracker.h"
34#endif
35#include "FileChooser.h"
36#include "FloatRect.h"
37#include "Frame.h"
38#include "FrameLoadRequest.h"
39#include "Icon.h"
40#include "NavigationAction.h"
41#include "NotImplemented.h"
42#include "PlatformString.h"
43#include "SecurityOrigin.h"
44#include "PopupMenuWx.h"
45#include "SearchPopupMenuWx.h"
46#include "WindowFeatures.h"
47
48#include <stdio.h>
49
50#include <wx/wxprec.h>
51#ifndef WX_PRECOMP
52    #include <wx/wx.h>
53#endif
54#include <wx/textdlg.h>
55#include <wx/tooltip.h>
56
57#include "WebBrowserShell.h"
58#include "WebView.h"
59#include "WebViewPrivate.h"
60
61namespace WebCore {
62
63wxWebKitWindowFeatures wkFeaturesforWindowFeatures(const WindowFeatures& features)
64{
65    wxWebKitWindowFeatures wkFeatures;
66    wkFeatures.menuBarVisible = features.menuBarVisible;
67    wkFeatures.statusBarVisible = features.statusBarVisible;
68    wkFeatures.toolBarVisible = features.toolBarVisible;
69    wkFeatures.locationBarVisible = features.locationBarVisible;
70    wkFeatures.scrollbarsVisible = features.scrollbarsVisible;
71    wkFeatures.resizable = features.resizable;
72    wkFeatures.fullscreen = features.fullscreen;
73    wkFeatures.dialog = features.dialog;
74
75    return wkFeatures;
76}
77
78ChromeClientWx::ChromeClientWx(wxWebView* webView)
79{
80    m_webView = webView;
81}
82
83ChromeClientWx::~ChromeClientWx()
84{
85}
86
87void ChromeClientWx::chromeDestroyed()
88{
89    notImplemented();
90}
91
92void ChromeClientWx::setWindowRect(const FloatRect&)
93{
94    notImplemented();
95}
96
97FloatRect ChromeClientWx::windowRect()
98{
99    notImplemented();
100    return FloatRect();
101}
102
103FloatRect ChromeClientWx::pageRect()
104{
105    notImplemented();
106    return FloatRect();
107}
108
109float ChromeClientWx::scaleFactor()
110{
111    notImplemented();
112    return 1.0;
113}
114
115void ChromeClientWx::focus()
116{
117    notImplemented();
118}
119
120void ChromeClientWx::unfocus()
121{
122    notImplemented();
123}
124
125bool ChromeClientWx::canTakeFocus(FocusDirection)
126{
127    notImplemented();
128    return false;
129}
130
131void ChromeClientWx::takeFocus(FocusDirection)
132{
133    notImplemented();
134}
135
136void ChromeClientWx::focusedNodeChanged(Node*)
137{
138}
139
140void ChromeClientWx::focusedFrameChanged(Frame*)
141{
142}
143
144Page* ChromeClientWx::createWindow(Frame*, const FrameLoadRequest&, const WindowFeatures& features, const NavigationAction&)
145{
146    Page* myPage = 0;
147    wxWebViewNewWindowEvent wkEvent(m_webView);
148
149    wxWebKitWindowFeatures wkFeatures = wkFeaturesforWindowFeatures(features);
150    wkEvent.SetWindowFeatures(wkFeatures);
151
152    if (m_webView->GetEventHandler()->ProcessEvent(wkEvent)) {
153        if (wxWebView* webView = wkEvent.GetWebView()) {
154            WebViewPrivate* impl = webView->m_impl;
155            if (impl)
156                myPage = impl->page;
157        }
158    }
159
160    return myPage;
161}
162
163Page* ChromeClientWx::createModalDialog(Frame*, const FrameLoadRequest&)
164{
165    notImplemented();
166    return 0;
167}
168
169void ChromeClientWx::show()
170{
171    notImplemented();
172}
173
174bool ChromeClientWx::canRunModal()
175{
176    notImplemented();
177    return false;
178}
179
180void ChromeClientWx::runModal()
181{
182    notImplemented();
183}
184
185void ChromeClientWx::setToolbarsVisible(bool)
186{
187    notImplemented();
188}
189
190bool ChromeClientWx::toolbarsVisible()
191{
192    notImplemented();
193    return false;
194}
195
196void ChromeClientWx::setStatusbarVisible(bool)
197{
198    notImplemented();
199}
200
201bool ChromeClientWx::statusbarVisible()
202{
203    notImplemented();
204    return false;
205}
206
207void ChromeClientWx::setScrollbarsVisible(bool)
208{
209    notImplemented();
210}
211
212bool ChromeClientWx::scrollbarsVisible()
213{
214    notImplemented();
215    return false;
216}
217
218void ChromeClientWx::setMenubarVisible(bool)
219{
220    notImplemented();
221}
222
223bool ChromeClientWx::menubarVisible()
224{
225    notImplemented();
226    return false;
227}
228
229void ChromeClientWx::setResizable(bool)
230{
231    notImplemented();
232}
233
234void ChromeClientWx::addMessageToConsole(MessageSource source,
235                                          MessageType type,
236                                          MessageLevel level,
237                                          const String& message,
238                                          unsigned int lineNumber,
239                                          const String& sourceID)
240{
241    if (m_webView) {
242        wxWebViewConsoleMessageEvent wkEvent(m_webView);
243        wkEvent.SetMessage(message);
244        wkEvent.SetLineNumber(lineNumber);
245        wkEvent.SetSourceID(sourceID);
246        wkEvent.SetLevel(static_cast<wxWebViewConsoleMessageLevel>(level));
247        m_webView->GetEventHandler()->ProcessEvent(wkEvent);
248    }
249}
250
251bool ChromeClientWx::canRunBeforeUnloadConfirmPanel()
252{
253    notImplemented();
254    return true;
255}
256
257bool ChromeClientWx::runBeforeUnloadConfirmPanel(const String& string,
258                                                  Frame* frame)
259{
260    wxMessageDialog dialog(NULL, string, wxT("Confirm Action?"), wxYES_NO);
261    return dialog.ShowModal() == wxYES;
262}
263
264void ChromeClientWx::closeWindowSoon()
265{
266    notImplemented();
267}
268
269/*
270    Sites for testing prompts:
271    Alert - just type in a bad web address or http://www.htmlite.com/JS002.php
272    Prompt - http://www.htmlite.com/JS007.php
273    Confirm - http://www.htmlite.com/JS006.php
274*/
275
276void ChromeClientWx::runJavaScriptAlert(Frame* frame, const String& string)
277{
278    if (m_webView) {
279        wxWebViewAlertEvent wkEvent(m_webView);
280        wkEvent.SetMessage(string);
281        if (!m_webView->GetEventHandler()->ProcessEvent(wkEvent))
282            wxMessageBox(string, wxT("JavaScript Alert"), wxOK);
283    }
284}
285
286bool ChromeClientWx::runJavaScriptConfirm(Frame* frame, const String& string)
287{
288    bool result = false;
289    if (m_webView) {
290        wxWebViewConfirmEvent wkEvent(m_webView);
291        wkEvent.SetMessage(string);
292        if (m_webView->GetEventHandler()->ProcessEvent(wkEvent))
293            result = wkEvent.GetReturnCode() == wxID_YES;
294        else {
295            wxMessageDialog dialog(NULL, string, wxT("JavaScript Confirm"), wxYES_NO);
296            dialog.Centre();
297            result = (dialog.ShowModal() == wxID_YES);
298        }
299    }
300    return result;
301}
302
303bool ChromeClientWx::runJavaScriptPrompt(Frame* frame, const String& message, const String& defaultValue, String& result)
304{
305    if (m_webView) {
306        wxWebViewPromptEvent wkEvent(m_webView);
307        wkEvent.SetMessage(message);
308        wkEvent.SetResponse(defaultValue);
309        if (m_webView->GetEventHandler()->ProcessEvent(wkEvent)) {
310            result = wkEvent.GetResponse();
311            return true;
312        }
313        else {
314            wxTextEntryDialog dialog(NULL, message, wxT("JavaScript Prompt"), wxEmptyString, wxOK | wxCANCEL);
315            dialog.Centre();
316            if (dialog.ShowModal() == wxID_OK) {
317                result = dialog.GetValue();
318                return true;
319            }
320        }
321    }
322    return false;
323}
324
325void ChromeClientWx::setStatusbarText(const String&)
326{
327    notImplemented();
328}
329
330bool ChromeClientWx::shouldInterruptJavaScript()
331{
332    notImplemented();
333    return false;
334}
335
336KeyboardUIMode ChromeClientWx::keyboardUIMode()
337{
338    notImplemented();
339    return KeyboardAccessDefault;
340}
341
342IntRect ChromeClientWx::windowResizerRect() const
343{
344    notImplemented();
345    return IntRect();
346}
347
348void ChromeClientWx::invalidateWindow(const IntRect& rect, bool immediate)
349{
350    if (immediate)
351        m_webView->Update();
352}
353
354void ChromeClientWx::invalidateContentsForSlowScroll(const IntRect& rect, bool immediate)
355{
356    invalidateContentsAndWindow(rect, immediate);
357}
358
359void ChromeClientWx::invalidateContentsAndWindow(const IntRect& rect, bool immediate)
360{
361    if (!m_webView)
362        return;
363
364    m_webView->RefreshRect(rect);
365
366    if (immediate) {
367        m_webView->Update();
368    }
369}
370
371IntRect ChromeClientWx::windowToScreen(const IntRect& rect) const
372{
373    notImplemented();
374    return rect;
375}
376
377IntPoint ChromeClientWx::screenToWindow(const IntPoint& point) const
378{
379    notImplemented();
380    return point;
381}
382
383PlatformPageClient ChromeClientWx::platformPageClient() const
384{
385    return m_webView;
386}
387
388void ChromeClientWx::contentsSizeChanged(Frame*, const IntSize&) const
389{
390    notImplemented();
391}
392
393void ChromeClientWx::scrollBackingStore(int dx, int dy,
394                    const IntRect& scrollViewRect,
395                    const IntRect& clipRect)
396{
397    notImplemented();
398}
399
400void ChromeClientWx::updateBackingStore()
401{
402    notImplemented();
403}
404
405void ChromeClientWx::mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags)
406{
407    notImplemented();
408}
409
410void ChromeClientWx::setToolTip(const String& tip, TextDirection)
411{
412    wxToolTip* tooltip = m_webView->GetToolTip();
413    if (!tooltip || tooltip->GetTip() != wxString(tip))
414        m_webView->SetToolTip(tip);
415}
416
417void ChromeClientWx::print(Frame*)
418{
419    notImplemented();
420}
421
422#if ENABLE(DATABASE)
423void ChromeClientWx::exceededDatabaseQuota(Frame*, const String&)
424{
425    unsigned long long quota = 5 * 1024 * 1024;
426
427    if (wxWebFrame* webFrame = m_webView->GetMainFrame())
428        if (Frame* frame = webFrame->GetFrame())
429            if (Document* document = frame->document())
430                if (!DatabaseTracker::tracker().hasEntryForOrigin(document->securityOrigin()))
431                    DatabaseTracker::tracker().setQuota(document->securityOrigin(), quota);
432}
433#endif
434
435#if ENABLE(OFFLINE_WEB_APPLICATIONS)
436void ChromeClientWx::reachedMaxAppCacheSize(int64_t spaceNeeded)
437{
438    notImplemented();
439}
440
441void ChromeClientWx::reachedApplicationCacheOriginQuota(SecurityOrigin*)
442{
443    notImplemented();
444}
445#endif
446
447void ChromeClientWx::scroll(const IntSize&, const IntRect&, const IntRect&)
448{
449    m_webView->Refresh();
450    notImplemented();
451}
452
453void ChromeClientWx::runOpenPanel(Frame*, PassRefPtr<FileChooser>)
454{
455    notImplemented();
456}
457
458void ChromeClientWx::chooseIconForFiles(const Vector<String>& filenames, FileChooser* chooser)
459{
460    chooser->iconLoaded(Icon::createIconForFiles(filenames));
461}
462
463void ChromeClientWx::setCursor(const Cursor& cursor)
464{
465    if (m_webView && cursor.impl())
466        m_webView->SetCursor(*cursor.impl());
467}
468
469void ChromeClientWx::requestGeolocationPermissionForFrame(Frame*, Geolocation*)
470{
471    // See the comment in WebCore/page/ChromeClient.h
472    notImplemented();
473}
474
475bool ChromeClientWx::selectItemWritingDirectionIsNatural()
476{
477    return false;
478}
479
480bool ChromeClientWx::selectItemAlignmentFollowsMenuWritingDirection()
481{
482    return false;
483}
484
485PassRefPtr<PopupMenu> ChromeClientWx::createPopupMenu(PopupMenuClient* client) const
486{
487    return adoptRef(new PopupMenuWx(client));
488}
489
490PassRefPtr<SearchPopupMenu> ChromeClientWx::createSearchPopupMenu(PopupMenuClient* client) const
491{
492    return adoptRef(new SearchPopupMenuWx(client));
493}
494
495}
496