| 1 | /* |
| 2 | * Copyright (C) 2014 Igalia S.L. |
| 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(DRAG_SUPPORT) |
| 29 | |
| 30 | #include <WebCore/DragActions.h> |
| 31 | #include <WebCore/IntPoint.h> |
| 32 | #include <WebCore/SelectionData.h> |
| 33 | #include <gtk/gtk.h> |
| 34 | #include <wtf/HashMap.h> |
| 35 | #include <wtf/Noncopyable.h> |
| 36 | #include <wtf/glib/GRefPtr.h> |
| 37 | |
| 38 | typedef struct _GdkDragContext GdkDragContext; |
| 39 | typedef struct _GtkSelectionData GtkSelectionData; |
| 40 | |
| 41 | namespace WebCore { |
| 42 | class DragData; |
| 43 | class SelectionData; |
| 44 | } |
| 45 | |
| 46 | namespace WebKit { |
| 47 | |
| 48 | class ShareableBitmap; |
| 49 | class WebPageProxy; |
| 50 | |
| 51 | class DragAndDropHandler { |
| 52 | WTF_MAKE_NONCOPYABLE(DragAndDropHandler); |
| 53 | public: |
| 54 | DragAndDropHandler(WebPageProxy&); |
| 55 | |
| 56 | void startDrag(Ref<WebCore::SelectionData>&&, WebCore::DragOperation, RefPtr<ShareableBitmap>&& dragImage); |
| 57 | void fillDragData(GdkDragContext*, GtkSelectionData*, unsigned info); |
| 58 | void finishDrag(GdkDragContext*); |
| 59 | |
| 60 | void dragEntered(GdkDragContext*, GtkSelectionData*, unsigned info, unsigned time); |
| 61 | void dragMotion(GdkDragContext*, const WebCore::IntPoint& position, unsigned time); |
| 62 | void dragLeave(GdkDragContext*); |
| 63 | bool drop(GdkDragContext*, const WebCore::IntPoint& position, unsigned time); |
| 64 | |
| 65 | private: |
| 66 | struct DroppingContext { |
| 67 | DroppingContext(GdkDragContext*, const WebCore::IntPoint& position); |
| 68 | |
| 69 | GdkDragContext* gdkContext { nullptr }; |
| 70 | WebCore::IntPoint lastMotionPosition; |
| 71 | Ref<WebCore::SelectionData> selectionData; |
| 72 | unsigned pendingDataRequests { 0 }; |
| 73 | bool dropHappened { false }; |
| 74 | }; |
| 75 | |
| 76 | WebCore::SelectionData* dropDataSelection(GdkDragContext*, GtkSelectionData*, unsigned info, WebCore::IntPoint& position); |
| 77 | WebCore::SelectionData* dragDataSelection(GdkDragContext*, const WebCore::IntPoint& position, unsigned time); |
| 78 | |
| 79 | WebPageProxy& m_page; |
| 80 | HashMap<GdkDragContext*, std::unique_ptr<DroppingContext>> m_droppingContexts; |
| 81 | |
| 82 | #if GTK_CHECK_VERSION(3, 16, 0) |
| 83 | GRefPtr<GdkDragContext> m_dragContext; |
| 84 | RefPtr<WebCore::SelectionData> m_draggingSelectionData; |
| 85 | #else |
| 86 | // We don't have gtk_drag_cancel() in GTK+ < 3.16, so we use the old code. |
| 87 | // See https://bugs.webkit.org/show_bug.cgi?id=138468 |
| 88 | HashMap<GdkDragContext*, RefPtr<WebCore::SelectionData>> m_draggingSelectionDataMap; |
| 89 | #endif |
| 90 | }; |
| 91 | |
| 92 | } // namespace WebKit |
| 93 | |
| 94 | #endif // ENABLE(DRAG_SUPPORT) |
| 95 | |