1/*
2 * Copyright (C) 2010 Apple Inc. 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WebView_h
27#define WebView_h
28
29#include "APIObject.h"
30#include "PageClient.h"
31#include "WKView.h"
32#include "WebPageProxy.h"
33#include "WebUndoClient.h"
34#include <ShlObj.h>
35#include <WebCore/COMPtr.h>
36#include <WebCore/DragActions.h>
37#include <WebCore/DragData.h>
38#include <WebCore/WindowMessageListener.h>
39#include <wtf/Forward.h>
40#include <wtf/PassRefPtr.h>
41#include <wtf/RefPtr.h>
42
43interface IDropTargetHelper;
44
45namespace WebKit {
46
47class DrawingAreaProxy;
48
49class WebView : public APIObject, public PageClient, WebCore::WindowMessageListener, public IDropTarget {
50public:
51    static PassRefPtr<WebView> create(RECT rect, WebContext* context, WebPageGroup* pageGroup, HWND parentWindow)
52    {
53        RefPtr<WebView> webView = adoptRef(new WebView(rect, context, pageGroup, parentWindow));
54        webView->initialize();
55        return webView;
56    }
57    ~WebView();
58
59    HWND window() const { return m_window; }
60    void setParentWindow(HWND);
61    void windowAncestryDidChange();
62    void setIsInWindow(bool);
63    void setIsVisible(bool);
64    void setOverrideCursor(HCURSOR);
65    void setInitialFocus(bool forward);
66    void setScrollOffsetOnNextResize(const WebCore::IntSize&);
67    void setFindIndicatorCallback(WKViewFindIndicatorCallback, void*);
68    WKViewFindIndicatorCallback getFindIndicatorCallback(void**);
69    void initialize();
70
71    void initializeUndoClient(const WKViewUndoClient*);
72    void reapplyEditCommand(WebEditCommandProxy*);
73    void unapplyEditCommand(WebEditCommandProxy*);
74
75    // IUnknown
76    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
77    virtual ULONG STDMETHODCALLTYPE AddRef(void);
78    virtual ULONG STDMETHODCALLTYPE Release(void);
79
80    // IDropTarget
81    virtual HRESULT STDMETHODCALLTYPE DragEnter(IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect);
82    virtual HRESULT STDMETHODCALLTYPE DragOver(DWORD grfKeyState, POINTL pt, DWORD* pdwEffect);
83    virtual HRESULT STDMETHODCALLTYPE DragLeave();
84    virtual HRESULT STDMETHODCALLTYPE Drop(IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect);
85
86    WebPageProxy* page() const { return m_page.get(); }
87
88private:
89    WebView(RECT, WebContext*, WebPageGroup*, HWND parentWindow);
90
91    virtual Type type() const { return TypeView; }
92
93    static bool registerWebViewWindowClass();
94    static LRESULT CALLBACK WebViewWndProc(HWND, UINT, WPARAM, LPARAM);
95    LRESULT wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
96
97    LRESULT onMouseEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
98    LRESULT onWheelEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
99    LRESULT onHorizontalScroll(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
100    LRESULT onVerticalScroll(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
101    LRESULT onGestureNotify(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
102    LRESULT onGesture(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
103    LRESULT onKeyEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
104    LRESULT onPaintEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
105    LRESULT onPrintClientEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
106    LRESULT onSizeEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
107    LRESULT onWindowPositionChangedEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
108    LRESULT onSetFocusEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
109    LRESULT onKillFocusEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
110    LRESULT onTimerEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
111    LRESULT onShowWindowEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
112    LRESULT onSetCursor(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled);
113
114    void paint(HDC, const WebCore::IntRect& dirtyRect);
115    void setWasActivatedByMouseEvent(bool flag) { m_wasActivatedByMouseEvent = flag; }
116    bool onIMEStartComposition();
117    bool onIMEComposition(LPARAM);
118    bool onIMEEndComposition();
119    LRESULT onIMERequest(WPARAM, LPARAM);
120    bool onIMESelect(WPARAM, LPARAM);
121    bool onIMESetContext(WPARAM, LPARAM);
122    void resetIME();
123    void setInputMethodState(bool);
124    HIMC getIMMContext();
125    void prepareCandidateWindow(HIMC);
126    LRESULT onIMERequestCharPosition(IMECHARPOSITION*);
127    LRESULT onIMERequestReconvertString(RECONVERTSTRING*);
128
129    void updateActiveState();
130    void updateActiveStateSoon();
131
132    void initializeToolTipWindow();
133
134    void startTrackingMouseLeave();
135    void stopTrackingMouseLeave();
136
137    bool shouldInitializeTrackPointHack();
138
139    void close();
140
141    HCURSOR cursorToShow() const;
142    void updateNativeCursor();
143
144    // PageClient
145    virtual PassOwnPtr<DrawingAreaProxy> createDrawingAreaProxy();
146    virtual void setViewNeedsDisplay(const WebCore::IntRect&);
147    virtual void displayView();
148    virtual void scrollView(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollOffset);
149    virtual void flashBackingStoreUpdates(const Vector<WebCore::IntRect>& updateRects);
150    virtual float userSpaceScaleFactor() const { return 1; }
151
152    virtual WebCore::IntSize viewSize();
153    virtual bool isViewWindowActive();
154    virtual bool isViewFocused();
155    virtual bool isViewVisible();
156    virtual bool isViewInWindow();
157    virtual void processDidCrash();
158    virtual void didRelaunchProcess();
159    virtual void pageClosed();
160    virtual void toolTipChanged(const WTF::String&, const WTF::String&);
161    virtual void setCursor(const WebCore::Cursor&);
162    virtual void setViewportArguments(const WebCore::ViewportArguments&);
163    virtual void registerEditCommand(PassRefPtr<WebEditCommandProxy>, WebPageProxy::UndoOrRedo);
164    virtual void clearAllEditCommands();
165    virtual bool canUndoRedo(WebPageProxy::UndoOrRedo);
166    virtual void executeUndoRedo(WebPageProxy::UndoOrRedo);
167    virtual WebCore::FloatRect convertToDeviceSpace(const WebCore::FloatRect&);
168    virtual WebCore::FloatRect convertToUserSpace(const WebCore::FloatRect&);
169    virtual WebCore::IntRect windowToScreen(const WebCore::IntRect&);
170    virtual void doneWithKeyEvent(const NativeWebKeyboardEvent&, bool wasEventHandled);
171    virtual void compositionSelectionChanged(bool);
172    virtual PassRefPtr<WebPopupMenuProxy> createPopupMenuProxy(WebPageProxy*);
173    virtual PassRefPtr<WebContextMenuProxy> createContextMenuProxy(WebPageProxy*);
174    virtual void setFindIndicator(PassRefPtr<FindIndicator>, bool fadeOut);
175
176#if USE(ACCELERATED_COMPOSITING)
177    virtual void enterAcceleratedCompositingMode(const LayerTreeContext&);
178    virtual void exitAcceleratedCompositingMode();
179#endif
180
181    void didCommitLoadForMainFrame(bool useCustomRepresentation);
182    void didFinishLoadingDataForCustomRepresentation(const String& suggestedFilename, const CoreIPC::DataReference&);
183    virtual double customRepresentationZoomFactor();
184    virtual void setCustomRepresentationZoomFactor(double);
185    WebCore::DragOperation keyStateToDragOperation(DWORD grfKeyState) const;
186    virtual void didChangeScrollbarsForMainFrame() const;
187
188    virtual void findStringInCustomRepresentation(const String&, FindOptions, unsigned maxMatchCount);
189    virtual void countStringMatchesInCustomRepresentation(const String&, FindOptions, unsigned maxMatchCount);
190
191    virtual HWND nativeWindow();
192
193    virtual void setGestureReachedScrollingLimit(bool limitReached) { m_gestureReachedScrollingLimit = limitReached; }
194
195    // WebCore::WindowMessageListener
196    virtual void windowReceivedMessage(HWND, UINT message, WPARAM, LPARAM);
197
198    HWND m_window;
199    HWND m_topLevelParentWindow;
200    HWND m_toolTipWindow;
201
202    WebCore::IntSize m_nextResizeScrollOffset;
203
204    HCURSOR m_lastCursorSet;
205    HCURSOR m_webCoreCursor;
206    HCURSOR m_overrideCursor;
207
208    bool m_isInWindow;
209    bool m_isVisible;
210    bool m_wasActivatedByMouseEvent;
211    bool m_trackingMouseLeave;
212    bool m_isBeingDestroyed;
213
214    RefPtr<WebPageProxy> m_page;
215
216    unsigned m_inIMEComposition;
217
218    WebUndoClient m_undoClient;
219
220    WKViewFindIndicatorCallback m_findIndicatorCallback;
221    void* m_findIndicatorCallbackContext;
222
223    COMPtr<IDataObject> m_dragData;
224    COMPtr<IDropTargetHelper> m_dropTargetHelper;
225    // FIXME: This variable is part of a workaround. The drop effect (pdwEffect) passed to Drop is incorrect.
226    // We set this variable in DragEnter and DragOver so that it can be used in Drop to set the correct drop effect.
227    // Thus, on return from DoDragDrop we have the correct pdwEffect for the drag-and-drop operation.
228    // (see https://bugs.webkit.org/show_bug.cgi?id=29264)
229    DWORD m_lastDropEffect;
230
231    int m_lastPanX;
232    int m_lastPanY;
233
234    int m_overPanY;
235
236    bool m_gestureReachedScrollingLimit;
237};
238
239} // namespace WebKit
240
241#endif // WebView_h
242