Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim: set ts=4 et sw=4 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef nsDragService_h__
8 : #define nsDragService_h__
9 :
10 : #include "mozilla/RefPtr.h"
11 : #include "nsBaseDragService.h"
12 : #include "nsIObserver.h"
13 : #include "nsAutoRef.h"
14 : #include <gtk/gtk.h>
15 :
16 : class nsWindow;
17 :
18 : namespace mozilla {
19 : namespace gfx {
20 : class SourceSurface;
21 : }
22 : }
23 :
24 : #ifndef HAVE_NSGOBJECTREFTRAITS
25 : #define HAVE_NSGOBJECTREFTRAITS
26 : template <class T>
27 4 : class nsGObjectRefTraits : public nsPointerRefTraits<T> {
28 : public:
29 0 : static void Release(T *aPtr) { g_object_unref(aPtr); }
30 0 : static void AddRef(T *aPtr) { g_object_ref(aPtr); }
31 : };
32 : #endif
33 :
34 : #ifndef HAVE_NSAUTOREFTRAITS_GTKWIDGET
35 : #define HAVE_NSAUTOREFTRAITS_GTKWIDGET
36 : template <>
37 1 : class nsAutoRefTraits<GtkWidget> : public nsGObjectRefTraits<GtkWidget> { };
38 : #endif
39 :
40 : #ifndef HAVE_NSAUTOREFTRAITS_GDKDRAGCONTEXT
41 : #define HAVE_NSAUTOREFTRAITS_GDKDRAGCONTEXT
42 : template <>
43 3 : class nsAutoRefTraits<GdkDragContext> :
44 : public nsGObjectRefTraits<GdkDragContext> { };
45 : #endif
46 :
47 : /**
48 : * Native GTK DragService wrapper
49 : */
50 :
51 : class nsDragService final : public nsBaseDragService,
52 : public nsIObserver
53 : {
54 : public:
55 : nsDragService();
56 :
57 : NS_DECL_ISUPPORTS_INHERITED
58 :
59 : NS_DECL_NSIOBSERVER
60 :
61 : // nsBaseDragService
62 : virtual nsresult InvokeDragSessionImpl(nsIArray* anArrayTransferables,
63 : nsIScriptableRegion* aRegion,
64 : uint32_t aActionType) override;
65 : // nsIDragService
66 : NS_IMETHOD InvokeDragSession (nsIDOMNode *aDOMNode,
67 : nsIArray * anArrayTransferables,
68 : nsIScriptableRegion * aRegion,
69 : uint32_t aActionType,
70 : nsContentPolicyType aContentPolicyType) override;
71 : NS_IMETHOD StartDragSession() override;
72 : NS_IMETHOD EndDragSession(bool aDoneDrag, uint32_t aKeyModifiers) override;
73 :
74 : // nsIDragSession
75 : NS_IMETHOD SetCanDrop (bool aCanDrop) override;
76 : NS_IMETHOD GetCanDrop (bool *aCanDrop) override;
77 : NS_IMETHOD GetNumDropItems (uint32_t * aNumItems) override;
78 : NS_IMETHOD GetData (nsITransferable * aTransferable,
79 : uint32_t aItemIndex) override;
80 : NS_IMETHOD IsDataFlavorSupported (const char *aDataFlavor,
81 : bool *_retval) override;
82 :
83 : NS_IMETHOD UpdateDragEffect() override;
84 :
85 : // Methods called from nsWindow to handle responding to GTK drag
86 : // destination signals
87 :
88 : static already_AddRefed<nsDragService> GetInstance();
89 :
90 : void TargetDataReceived (GtkWidget *aWidget,
91 : GdkDragContext *aContext,
92 : gint aX,
93 : gint aY,
94 : GtkSelectionData *aSelection_data,
95 : guint aInfo,
96 : guint32 aTime);
97 :
98 : gboolean ScheduleMotionEvent(nsWindow *aWindow,
99 : GdkDragContext *aDragContext,
100 : mozilla::LayoutDeviceIntPoint aWindowPoint,
101 : guint aTime);
102 : void ScheduleLeaveEvent();
103 : gboolean ScheduleDropEvent(nsWindow *aWindow,
104 : GdkDragContext *aDragContext,
105 : mozilla::LayoutDeviceIntPoint aWindowPoint,
106 : guint aTime);
107 :
108 0 : nsWindow* GetMostRecentDestWindow()
109 : {
110 0 : return mScheduledTask == eDragTaskNone ? mTargetWindow
111 0 : : mPendingWindow;
112 : }
113 :
114 : // END PUBLIC API
115 :
116 : // These methods are public only so that they can be called from functions
117 : // with C calling conventions. They are called for drags started with the
118 : // invisible widget.
119 : void SourceEndDragSession(GdkDragContext *aContext,
120 : gint aResult);
121 : void SourceDataGet(GtkWidget *widget,
122 : GdkDragContext *context,
123 : GtkSelectionData *selection_data,
124 : guint32 aTime);
125 :
126 : // set the drag icon during drag-begin
127 : void SetDragIcon(GdkDragContext* aContext);
128 :
129 : protected:
130 : virtual ~nsDragService();
131 :
132 : private:
133 :
134 : // mScheduledTask indicates what signal has been received from GTK and
135 : // so what needs to be dispatched when the scheduled task is run. It is
136 : // eDragTaskNone when there is no task scheduled (but the
137 : // previous task may still not have finished running).
138 : enum DragTask {
139 : eDragTaskNone,
140 : eDragTaskMotion,
141 : eDragTaskLeave,
142 : eDragTaskDrop,
143 : eDragTaskSourceEnd
144 : };
145 : DragTask mScheduledTask;
146 : // mTaskSource is the GSource id for the task that is either scheduled
147 : // or currently running. It is 0 if no task is scheduled or running.
148 : guint mTaskSource;
149 :
150 : // target/destination side vars
151 : // These variables keep track of the state of the current drag.
152 :
153 : // mPendingWindow, mPendingWindowPoint, mPendingDragContext, and
154 : // mPendingTime, carry information from the GTK signal that will be used
155 : // when the scheduled task is run. mPendingWindow and mPendingDragContext
156 : // will be nullptr if the scheduled task is eDragTaskLeave.
157 : RefPtr<nsWindow> mPendingWindow;
158 : mozilla::LayoutDeviceIntPoint mPendingWindowPoint;
159 : nsCountedRef<GdkDragContext> mPendingDragContext;
160 : guint mPendingTime;
161 :
162 : // mTargetWindow and mTargetWindowPoint record the position of the last
163 : // eDragTaskMotion or eDragTaskDrop task that was run or is still running.
164 : // mTargetWindow is cleared once the drag has completed or left.
165 : RefPtr<nsWindow> mTargetWindow;
166 : mozilla::LayoutDeviceIntPoint mTargetWindowPoint;
167 : // mTargetWidget and mTargetDragContext are set only while dispatching
168 : // motion or drop events. mTime records the corresponding timestamp.
169 : nsCountedRef<GtkWidget> mTargetWidget;
170 : nsCountedRef<GdkDragContext> mTargetDragContext;
171 : // mTargetDragContextForRemote is set while waiting for a reply from
172 : // a child process.
173 : nsCountedRef<GdkDragContext> mTargetDragContextForRemote;
174 : guint mTargetTime;
175 :
176 : // is it OK to drop on us?
177 : bool mCanDrop;
178 :
179 : // have we received our drag data?
180 : bool mTargetDragDataReceived;
181 : // last data received and its length
182 : void *mTargetDragData;
183 : uint32_t mTargetDragDataLen;
184 : // is the current target drag context contain a list?
185 : bool IsTargetContextList(void);
186 : // this will get the native data from the last target given a
187 : // specific flavor
188 : void GetTargetDragData(GdkAtom aFlavor);
189 : // this will reset all of the target vars
190 : void TargetResetData(void);
191 :
192 : // source side vars
193 :
194 : // the source of our drags
195 : GtkWidget *mHiddenWidget;
196 : // our source data items
197 : nsCOMPtr<nsIArray> mSourceDataItems;
198 :
199 : nsCOMPtr<nsIScriptableRegion> mSourceRegion;
200 :
201 : // get a list of the sources in gtk's format
202 : GtkTargetList *GetSourceList(void);
203 :
204 : // attempts to create a semi-transparent drag image. Returns TRUE if
205 : // successful, FALSE if not
206 : bool SetAlphaPixmap(SourceSurface *aPixbuf,
207 : GdkDragContext *aContext,
208 : int32_t aXOffset,
209 : int32_t aYOffset,
210 : const mozilla::LayoutDeviceIntRect &dragRect);
211 :
212 : gboolean Schedule(DragTask aTask, nsWindow *aWindow,
213 : GdkDragContext *aDragContext,
214 : mozilla::LayoutDeviceIntPoint aWindowPoint, guint aTime);
215 :
216 : // Callback for g_idle_add_full() to run mScheduledTask.
217 : static gboolean TaskDispatchCallback(gpointer data);
218 : gboolean RunScheduledTask();
219 : void UpdateDragAction();
220 : void DispatchMotionEvents();
221 : void ReplyToDragMotion(GdkDragContext* aDragContext);
222 : gboolean DispatchDropEvent();
223 : static uint32_t GetCurrentModifiers();
224 : };
225 :
226 : #endif // nsDragService_h__
227 :
|