]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: dnd.cpp | |
3 | // Purpose: wxDropTarget class | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
f03fc89f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifdef __GNUG__ | |
510fc784 | 11 | #pragma implementation "dnd.h" |
c801d85f KB |
12 | #endif |
13 | ||
14 | #include "wx/dnd.h" | |
ac57418f | 15 | |
06cfab17 | 16 | #if wxUSE_DRAG_AND_DROP |
ac57418f | 17 | |
c801d85f KB |
18 | #include "wx/window.h" |
19 | #include "wx/app.h" | |
20 | #include "wx/gdicmn.h" | |
b527aac5 RR |
21 | #include "wx/intl.h" |
22 | #include "wx/utils.h" | |
c801d85f | 23 | |
071a2d78 RR |
24 | #include <gdk/gdk.h> |
25 | #include <gtk/gtk.h> | |
26 | #include <gdk/gdkprivate.h> | |
c801d85f | 27 | |
071a2d78 RR |
28 | #include <gtk/gtkdnd.h> |
29 | #include <gtk/gtkselection.h> | |
c801d85f | 30 | |
5549fa65 RR |
31 | //----------------------------------------------------------------------------- |
32 | // idle system | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | extern void wxapp_install_idle_handler(); | |
36 | extern bool g_isIdle; | |
37 | ||
b453e1b2 RR |
38 | //----------------------------------------------------------------------------- |
39 | // thread system | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | #if wxUSE_THREADS | |
43 | extern void wxapp_install_thread_wakeup(); | |
44 | extern void wxapp_uninstall_thread_wakeup(); | |
45 | #endif | |
46 | ||
22d5903e RR |
47 | //---------------------------------------------------------------------------- |
48 | // global data | |
49 | //---------------------------------------------------------------------------- | |
c801d85f KB |
50 | |
51 | extern bool g_blockEventsOnDrag; | |
52 | ||
22d5903e RR |
53 | //---------------------------------------------------------------------------- |
54 | // standard icons | |
55 | //---------------------------------------------------------------------------- | |
56 | ||
22d5903e RR |
57 | /* XPM */ |
58 | static char * page_xpm[] = { | |
59 | /* width height ncolors chars_per_pixel */ | |
60 | "32 32 5 1", | |
61 | /* colors */ | |
f03fc89f VZ |
62 | " s None c None", |
63 | ". c black", | |
64 | "X c wheat", | |
65 | "o c tan", | |
66 | "O c #6699FF", | |
22d5903e RR |
67 | /* pixels */ |
68 | " ................... ", | |
69 | " .XXXXXXXXXXXXXXXXX.. ", | |
70 | " .XXXXXXXXXXXXXXXXX.o. ", | |
71 | " .XXXXXXXXXXXXXXXXX.oo. ", | |
72 | " .XXXXXXXXXXXXXXXXX.ooo. ", | |
73 | " .XXXXXXXXXXXXXXXXX.oooo. ", | |
74 | " .XXXXXXXXXXXXXXXXX....... ", | |
75 | " .XXXXXOOOOOOOOOOXXXooooo. ", | |
76 | " .XXXXXXXXXXXXXXXXXXooooo. ", | |
77 | " .XXXXXOOOOOOOOOOXXXXXXXX. ", | |
78 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
79 | " .XXXXXXXOOOOOOOOOXXXXXXX. ", | |
80 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
81 | " .XXXXXXOOOOOOOOOOXXXXXXX. ", | |
82 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
83 | " .XXXXXOOOOOOOOOOXXXXXXXX. ", | |
84 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
85 | " .XXXXXXXOOOOOOOOOXXXXXXX. ", | |
86 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
87 | " .XXXXXXOOOOOOOOOOXXXXXXX. ", | |
88 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
89 | " .XXXXXOOOOOOOOOOXXXXXXXX. ", | |
90 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
91 | " .XXXXXXOOOOOOOOOOXXXXXXX. ", | |
92 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
93 | " .XXXXXOOOOOOOXXXXXXXXXXX. ", | |
94 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
95 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
96 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
97 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
98 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
99 | " ......................... "}; | |
f03fc89f VZ |
100 | |
101 | ||
4ba47b40 | 102 | |
33a5bc52 RR |
103 | // ---------------------------------------------------------------------------- |
104 | // "drag_leave" | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | static void target_drag_leave( GtkWidget *WXUNUSED(widget), | |
f03fc89f VZ |
108 | GdkDragContext *context, |
109 | guint WXUNUSED(time), | |
110 | wxDropTarget *drop_target ) | |
33a5bc52 | 111 | { |
5549fa65 RR |
112 | if (g_isIdle) wxapp_install_idle_handler(); |
113 | ||
829e3e8d RR |
114 | /* inform the wxDropTarget about the current GdkDragContext. |
115 | this is only valid for the duration of this call */ | |
116 | drop_target->SetDragContext( context ); | |
f03fc89f | 117 | |
829e3e8d RR |
118 | /* we don't need return values. this event is just for |
119 | information */ | |
120 | drop_target->OnLeave(); | |
f03fc89f | 121 | |
829e3e8d RR |
122 | /* this has to be done because GDK has no "drag_enter" event */ |
123 | drop_target->m_firstMotion = TRUE; | |
f03fc89f | 124 | |
829e3e8d RR |
125 | /* after this, invalidate the drop_target's GdkDragContext */ |
126 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
33a5bc52 RR |
127 | } |
128 | ||
129 | // ---------------------------------------------------------------------------- | |
130 | // "drag_motion" | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
133 | static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget), | |
f03fc89f VZ |
134 | GdkDragContext *context, |
135 | gint x, | |
136 | gint y, | |
137 | guint time, | |
138 | wxDropTarget *drop_target ) | |
33a5bc52 | 139 | { |
5549fa65 RR |
140 | if (g_isIdle) wxapp_install_idle_handler(); |
141 | ||
829e3e8d RR |
142 | /* Owen Taylor: "if the coordinates not in a drop zone, |
143 | return FALSE, otherwise call gtk_drag_status() and | |
144 | return TRUE" */ | |
f03fc89f | 145 | |
829e3e8d RR |
146 | /* inform the wxDropTarget about the current GdkDragContext. |
147 | this is only valid for the duration of this call */ | |
148 | drop_target->SetDragContext( context ); | |
f03fc89f | 149 | |
8ee9d618 VZ |
150 | wxDragResult result; |
151 | if ( context->suggested_action == GDK_ACTION_COPY ) | |
152 | result = wxDragCopy; | |
153 | else | |
154 | result = wxDragMove; | |
8e00741d | 155 | |
829e3e8d | 156 | if (drop_target->m_firstMotion) |
d6086ea6 | 157 | { |
829e3e8d | 158 | /* the first "drag_motion" event substitutes a "drag_enter" event */ |
c9057ae1 | 159 | result = drop_target->OnEnter( x, y, result ); |
8e00741d RR |
160 | } |
161 | else | |
162 | { | |
163 | /* give program a chance to react (i.e. to say no by returning FALSE) */ | |
c9057ae1 | 164 | result = drop_target->OnDragOver( x, y, result ); |
d6086ea6 | 165 | } |
f03fc89f | 166 | |
8ee9d618 | 167 | bool ret = wxIsDragResultOk( result ); |
829e3e8d | 168 | if (ret) |
bd77da97 | 169 | { |
8ee9d618 VZ |
170 | GdkDragAction action; |
171 | if (result == wxDragCopy) | |
172 | action = GDK_ACTION_COPY; | |
173 | else | |
174 | action = GDK_ACTION_MOVE; | |
175 | ||
7b5d5699 | 176 | gdk_drag_status( context, action, time ); |
bd77da97 | 177 | } |
f03fc89f | 178 | |
829e3e8d RR |
179 | /* after this, invalidate the drop_target's GdkDragContext */ |
180 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
f03fc89f | 181 | |
829e3e8d RR |
182 | /* this has to be done because GDK has no "drag_enter" event */ |
183 | drop_target->m_firstMotion = FALSE; | |
f03fc89f | 184 | |
829e3e8d | 185 | return ret; |
33a5bc52 RR |
186 | } |
187 | ||
188 | // ---------------------------------------------------------------------------- | |
189 | // "drag_drop" | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | static gboolean target_drag_drop( GtkWidget *widget, | |
f03fc89f VZ |
193 | GdkDragContext *context, |
194 | gint x, | |
195 | gint y, | |
196 | guint time, | |
197 | wxDropTarget *drop_target ) | |
33a5bc52 | 198 | { |
5549fa65 RR |
199 | if (g_isIdle) wxapp_install_idle_handler(); |
200 | ||
829e3e8d RR |
201 | /* Owen Taylor: "if the drop is not in a drop zone, |
202 | return FALSE, otherwise, if you aren't accepting | |
203 | the drop, call gtk_drag_finish() with success == FALSE | |
204 | otherwise call gtk_drag_data_get()" */ | |
4ba47b40 RR |
205 | |
206 | // printf( "drop.\n" ); | |
f03fc89f | 207 | |
829e3e8d RR |
208 | /* this seems to make a difference between not accepting |
209 | due to wrong target area and due to wrong format. let | |
210 | us hope that this is not required.. */ | |
f03fc89f | 211 | |
829e3e8d RR |
212 | /* inform the wxDropTarget about the current GdkDragContext. |
213 | this is only valid for the duration of this call */ | |
214 | drop_target->SetDragContext( context ); | |
f03fc89f | 215 | |
829e3e8d RR |
216 | /* inform the wxDropTarget about the current drag widget. |
217 | this is only valid for the duration of this call */ | |
218 | drop_target->SetDragWidget( widget ); | |
f03fc89f | 219 | |
829e3e8d RR |
220 | /* inform the wxDropTarget about the current drag time. |
221 | this is only valid for the duration of this call */ | |
222 | drop_target->SetDragTime( time ); | |
f03fc89f | 223 | |
bd77da97 RR |
224 | /* |
225 | wxDragResult result = wxDragMove; | |
226 | if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy; | |
227 | */ | |
228 | ||
513abb88 RR |
229 | /* reset the block here as someone might very well |
230 | show a dialog as a reaction to a drop and this | |
231 | wouldn't work without events */ | |
232 | g_blockEventsOnDrag = FALSE; | |
233 | ||
829e3e8d | 234 | bool ret = drop_target->OnDrop( x, y ); |
f03fc89f | 235 | |
5af019af | 236 | if (!ret) |
829e3e8d | 237 | { |
2e5c594e | 238 | wxLogDebug( wxT( "Drop target: OnDrop returned FALSE") ); |
8ee9d618 | 239 | |
829e3e8d RR |
240 | /* cancel the whole thing */ |
241 | gtk_drag_finish( context, | |
f03fc89f VZ |
242 | FALSE, /* no success */ |
243 | FALSE, /* don't delete data on dropping side */ | |
244 | time ); | |
829e3e8d | 245 | } |
8e00741d RR |
246 | else |
247 | { | |
97c79de2 | 248 | wxLogDebug( wxT( "Drop target: OnDrop returned TRUE") ); |
8ee9d618 | 249 | |
8e00741d RR |
250 | #if wxUSE_THREADS |
251 | /* disable GUI threads */ | |
252 | wxapp_uninstall_thread_wakeup(); | |
253 | #endif | |
254 | ||
255 | GdkAtom format = drop_target->GetMatchingPair(); | |
2edc8f5b | 256 | wxASSERT( format ); |
8ee9d618 | 257 | |
bd77da97 RR |
258 | /* |
259 | GdkDragAction action = GDK_ACTION_MOVE; | |
8ee9d618 VZ |
260 | if (result == wxDragCopy) action == GDK_ACTION_COPY; |
261 | context->action = action; | |
bd77da97 | 262 | */ |
8e00741d RR |
263 | /* this should trigger an "drag_data_received" event */ |
264 | gtk_drag_get_data( widget, | |
265 | context, | |
266 | format, | |
267 | time ); | |
268 | ||
269 | #if wxUSE_THREADS | |
270 | /* re-enable GUI threads */ | |
271 | wxapp_install_thread_wakeup(); | |
272 | #endif | |
273 | } | |
f03fc89f | 274 | |
829e3e8d RR |
275 | /* after this, invalidate the drop_target's GdkDragContext */ |
276 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
f03fc89f | 277 | |
829e3e8d RR |
278 | /* after this, invalidate the drop_target's drag widget */ |
279 | drop_target->SetDragWidget( (GtkWidget*) NULL ); | |
f03fc89f | 280 | |
829e3e8d RR |
281 | /* this has to be done because GDK has no "drag_enter" event */ |
282 | drop_target->m_firstMotion = TRUE; | |
f03fc89f | 283 | |
829e3e8d | 284 | return ret; |
33a5bc52 RR |
285 | } |
286 | ||
287 | // ---------------------------------------------------------------------------- | |
288 | // "drag_data_received" | |
289 | // ---------------------------------------------------------------------------- | |
290 | ||
291 | static void target_drag_data_received( GtkWidget *WXUNUSED(widget), | |
f03fc89f VZ |
292 | GdkDragContext *context, |
293 | gint x, | |
294 | gint y, | |
295 | GtkSelectionData *data, | |
296 | guint WXUNUSED(info), | |
297 | guint time, | |
298 | wxDropTarget *drop_target ) | |
33a5bc52 | 299 | { |
5549fa65 RR |
300 | if (g_isIdle) wxapp_install_idle_handler(); |
301 | ||
829e3e8d RR |
302 | /* Owen Taylor: "call gtk_drag_finish() with |
303 | success == TRUE" */ | |
304 | ||
829e3e8d | 305 | if ((data->length <= 0) || (data->format != 8)) |
33a5bc52 | 306 | { |
829e3e8d RR |
307 | /* negative data length and non 8-bit data format |
308 | qualifies for junk */ | |
309 | gtk_drag_finish (context, FALSE, FALSE, time); | |
f03fc89f | 310 | |
f03fc89f | 311 | return; |
829e3e8d | 312 | } |
f03fc89f | 313 | |
97c79de2 | 314 | wxLogDebug( wxT( "Drop target: data received event") ); |
8ee9d618 | 315 | |
5af019af RR |
316 | /* inform the wxDropTarget about the current GtkSelectionData. |
317 | this is only valid for the duration of this call */ | |
318 | drop_target->SetDragData( data ); | |
f03fc89f | 319 | |
8ee9d618 VZ |
320 | wxDragResult result; |
321 | if ( context->suggested_action == GDK_ACTION_COPY ) | |
322 | result = wxDragCopy; | |
323 | else | |
324 | result = wxDragMove; | |
325 | ||
326 | if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) ) | |
829e3e8d | 327 | { |
97c79de2 | 328 | wxLogDebug( wxT( "Drop target: OnData returned TRUE") ); |
8ee9d618 | 329 | |
f03fc89f | 330 | /* tell GTK that data transfer was successfull */ |
ab8884ac | 331 | gtk_drag_finish( context, TRUE, FALSE, time ); |
33a5bc52 | 332 | } |
5af019af RR |
333 | else |
334 | { | |
97c79de2 | 335 | wxLogDebug( wxT( "Drop target: OnData returned FALSE") ); |
8ee9d618 | 336 | |
f03fc89f | 337 | /* tell GTK that data transfer was not successfull */ |
5af019af RR |
338 | gtk_drag_finish( context, FALSE, FALSE, time ); |
339 | } | |
f03fc89f | 340 | |
5af019af RR |
341 | /* after this, invalidate the drop_target's drag data */ |
342 | drop_target->SetDragData( (GtkSelectionData*) NULL ); | |
33a5bc52 RR |
343 | } |
344 | ||
4ba47b40 | 345 | //---------------------------------------------------------------------------- |
33a5bc52 | 346 | // wxDropTarget |
4ba47b40 | 347 | //---------------------------------------------------------------------------- |
33a5bc52 | 348 | |
8ee9d618 VZ |
349 | wxDropTarget::wxDropTarget( wxDataObject *data ) |
350 | : wxDropTargetBase( data ) | |
f5368809 | 351 | { |
829e3e8d RR |
352 | m_firstMotion = TRUE; |
353 | m_dragContext = (GdkDragContext*) NULL; | |
354 | m_dragWidget = (GtkWidget*) NULL; | |
5af019af | 355 | m_dragData = (GtkSelectionData*) NULL; |
829e3e8d | 356 | m_dragTime = 0; |
f5368809 RR |
357 | } |
358 | ||
c9057ae1 VZ |
359 | wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x), |
360 | wxCoord WXUNUSED(y), | |
361 | wxDragResult def ) | |
d6086ea6 | 362 | { |
8ee9d618 | 363 | // GetMatchingPair() checks for m_dataObject too, no need to do it here |
2edc8f5b VZ |
364 | |
365 | // disable the debug message from GetMatchingPair() - there are too many | |
366 | // of them otherwise | |
c4fda16b | 367 | #ifdef __WXDEBUG__ |
2edc8f5b VZ |
368 | wxLogNull noLog; |
369 | #endif // Debug | |
370 | ||
c9057ae1 | 371 | return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone; |
d6086ea6 RR |
372 | } |
373 | ||
c9057ae1 | 374 | bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) ) |
d6086ea6 | 375 | { |
b068c4e8 | 376 | if (!m_dataObject) |
a3e7d24d | 377 | return FALSE; |
8ee9d618 | 378 | |
8e00741d | 379 | return (GetMatchingPair() != (GdkAtom) 0); |
d6086ea6 RR |
380 | } |
381 | ||
8ee9d618 VZ |
382 | wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), |
383 | wxDragResult def ) | |
5af019af | 384 | { |
b068c4e8 | 385 | if (!m_dataObject) |
11e1c70d | 386 | return wxDragNone; |
8ee9d618 | 387 | |
8e00741d | 388 | if (GetMatchingPair() == (GdkAtom) 0) |
11e1c70d | 389 | return wxDragNone; |
8ee9d618 VZ |
390 | |
391 | return GetData() ? def : wxDragNone; | |
5af019af RR |
392 | } |
393 | ||
8e00741d | 394 | GdkAtom wxDropTarget::GetMatchingPair() |
5af019af | 395 | { |
8ee9d618 | 396 | if (!m_dataObject) |
8e00741d | 397 | return (GdkAtom) 0; |
5af019af | 398 | |
8ee9d618 | 399 | if (!m_dragContext) |
8e00741d | 400 | return (GdkAtom) 0; |
f03fc89f | 401 | |
22d5903e RR |
402 | GList *child = m_dragContext->targets; |
403 | while (child) | |
404 | { | |
405 | GdkAtom formatAtom = (GdkAtom) GPOINTER_TO_INT(child->data); | |
2edc8f5b | 406 | wxDataFormat format( formatAtom ); |
f03fc89f | 407 | |
a3e7d24d | 408 | #ifdef __WXDEBUG__ |
7dd62924 | 409 | wxLogDebug( wxT("Drop target: drag has format: %s"), format.GetId().c_str() ); |
2edc8f5b VZ |
410 | #endif // Debug |
411 | ||
b068c4e8 | 412 | if (m_dataObject->IsSupportedFormat( format )) |
2edc8f5b | 413 | return formatAtom; |
f03fc89f | 414 | |
22d5903e RR |
415 | child = child->next; |
416 | } | |
829e3e8d | 417 | |
8e00741d | 418 | return (GdkAtom) 0; |
d6086ea6 | 419 | } |
f03fc89f | 420 | |
8e00741d | 421 | bool wxDropTarget::GetData() |
d6086ea6 | 422 | { |
8ee9d618 | 423 | if (!m_dragData) |
8e00741d | 424 | return FALSE; |
f03fc89f | 425 | |
8ee9d618 | 426 | if (!m_dataObject) |
8e00741d | 427 | return FALSE; |
f03fc89f | 428 | |
8e00741d | 429 | wxDataFormat dragFormat( m_dragData->target ); |
8ee9d618 | 430 | |
b068c4e8 | 431 | if (!m_dataObject->IsSupportedFormat( dragFormat )) |
8e00741d | 432 | return FALSE; |
f03fc89f | 433 | |
97c79de2 RR |
434 | m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data ); |
435 | ||
436 | return TRUE; | |
d6086ea6 | 437 | } |
f03fc89f | 438 | |
f5368809 RR |
439 | void wxDropTarget::UnregisterWidget( GtkWidget *widget ) |
440 | { | |
223d09f6 | 441 | wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") ); |
f03fc89f | 442 | |
829e3e8d | 443 | gtk_drag_dest_unset( widget ); |
f03fc89f | 444 | |
d6086ea6 | 445 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), |
f03fc89f | 446 | GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this ); |
33a5bc52 | 447 | |
d6086ea6 | 448 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), |
f03fc89f | 449 | GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this ); |
33a5bc52 | 450 | |
d6086ea6 | 451 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), |
f03fc89f | 452 | GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this ); |
33a5bc52 | 453 | |
d6086ea6 | 454 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), |
f03fc89f | 455 | GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this ); |
f5368809 RR |
456 | } |
457 | ||
458 | void wxDropTarget::RegisterWidget( GtkWidget *widget ) | |
459 | { | |
223d09f6 | 460 | wxCHECK_RET( widget != NULL, wxT("register widget is NULL") ); |
f03fc89f | 461 | |
829e3e8d RR |
462 | /* gtk_drag_dest_set() determines what default behaviour we'd like |
463 | GTK to supply. we don't want to specify out targets (=formats) | |
464 | or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and | |
465 | not GTK_DEST_DEFAULT_DROP). instead we react individually to | |
466 | "drag_motion" and "drag_drop" events. this makes it possible | |
f03fc89f | 467 | to allow dropping on only a small area. we should set |
829e3e8d RR |
468 | GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice |
469 | highlighting if dragging over standard controls, but this | |
470 | seems to be broken without the other two. */ | |
f03fc89f | 471 | |
d6086ea6 | 472 | gtk_drag_dest_set( widget, |
f03fc89f VZ |
473 | (GtkDestDefaults) 0, /* no default behaviour */ |
474 | (GtkTargetEntry*) NULL, /* we don't supply any formats here */ | |
475 | 0, /* number of targets = 0 */ | |
476 | (GdkDragAction) 0 ); /* we don't supply any actions here */ | |
477 | ||
d6086ea6 | 478 | gtk_signal_connect( GTK_OBJECT(widget), "drag_leave", |
f03fc89f | 479 | GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this ); |
33a5bc52 | 480 | |
d6086ea6 | 481 | gtk_signal_connect( GTK_OBJECT(widget), "drag_motion", |
f03fc89f | 482 | GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this ); |
33a5bc52 | 483 | |
d6086ea6 | 484 | gtk_signal_connect( GTK_OBJECT(widget), "drag_drop", |
f03fc89f | 485 | GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this ); |
33a5bc52 | 486 | |
d6086ea6 | 487 | gtk_signal_connect( GTK_OBJECT(widget), "drag_data_received", |
f03fc89f | 488 | GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this ); |
f5368809 RR |
489 | } |
490 | ||
4ba47b40 RR |
491 | //---------------------------------------------------------------------------- |
492 | // "drag_data_get" | |
493 | //---------------------------------------------------------------------------- | |
494 | ||
f03fc89f | 495 | static void |
4ba47b40 | 496 | source_drag_data_get (GtkWidget *WXUNUSED(widget), |
b02da6b1 | 497 | GdkDragContext *WXUNUSED(context), |
f03fc89f VZ |
498 | GtkSelectionData *selection_data, |
499 | guint WXUNUSED(info), | |
500 | guint WXUNUSED(time), | |
501 | wxDropSource *drop_source ) | |
4ba47b40 | 502 | { |
5549fa65 RR |
503 | if (g_isIdle) wxapp_install_idle_handler(); |
504 | ||
97c79de2 | 505 | wxDataFormat format( selection_data->target ); |
8ee9d618 | 506 | |
97c79de2 | 507 | wxLogDebug( wxT("Drop source: format requested: %s"), format.GetId().c_str() ); |
f6bcfd97 | 508 | |
1dd989e1 | 509 | drop_source->m_retValue = wxDragCancel; |
8ee9d618 | 510 | |
97c79de2 | 511 | wxDataObject *data = drop_source->GetDataObject(); |
f6bcfd97 | 512 | |
1dd989e1 | 513 | if (!data) |
97c79de2 RR |
514 | { |
515 | wxLogDebug( wxT("Drop source: no data object") ); | |
2edc8f5b | 516 | return; |
97c79de2 | 517 | } |
1dd989e1 | 518 | |
97c79de2 RR |
519 | if (!data->IsSupportedFormat(format)) |
520 | { | |
521 | wxLogDebug( wxT("Drop source: unsupported format") ); | |
2edc8f5b | 522 | return; |
97c79de2 | 523 | } |
f03fc89f | 524 | |
97c79de2 RR |
525 | if (data->GetDataSize(format) == 0) |
526 | { | |
527 | wxLogDebug( wxT("Drop source: empty data") ); | |
2edc8f5b | 528 | return; |
97c79de2 | 529 | } |
8ee9d618 | 530 | |
97c79de2 | 531 | size_t size = data->GetDataSize(format); |
f03fc89f | 532 | |
1dd989e1 | 533 | // printf( "data size: %d.\n", (int)data_size ); |
f03fc89f | 534 | |
1dd989e1 | 535 | guchar *d = new guchar[size]; |
8ee9d618 | 536 | |
97c79de2 | 537 | if (!data->GetDataHere( format, (void*)d )) |
1dd989e1 | 538 | { |
97c79de2 | 539 | delete[] d; |
2edc8f5b | 540 | return; |
1dd989e1 | 541 | } |
f03fc89f | 542 | |
b453e1b2 | 543 | #if wxUSE_THREADS |
1dd989e1 RR |
544 | /* disable GUI threads */ |
545 | wxapp_uninstall_thread_wakeup(); | |
b453e1b2 | 546 | #endif |
8e193f38 | 547 | |
4ba47b40 | 548 | gtk_selection_data_set( selection_data, |
f03fc89f VZ |
549 | selection_data->target, |
550 | 8, // 8-bit | |
1dd989e1 RR |
551 | d, |
552 | size ); | |
f03fc89f | 553 | |
b453e1b2 | 554 | #if wxUSE_THREADS |
1dd989e1 RR |
555 | /* enable GUI threads */ |
556 | wxapp_install_thread_wakeup(); | |
b453e1b2 | 557 | #endif |
f03fc89f | 558 | |
97c79de2 | 559 | delete[] d; |
4ba47b40 | 560 | } |
f03fc89f | 561 | |
4ba47b40 RR |
562 | //---------------------------------------------------------------------------- |
563 | // "drag_data_delete" | |
564 | //---------------------------------------------------------------------------- | |
565 | ||
566 | static void source_drag_data_delete( GtkWidget *WXUNUSED(widget), | |
f03fc89f | 567 | GdkDragContext *WXUNUSED(context), |
13fb7b7a | 568 | wxDropSource *WXUNUSED(drop_source) ) |
4ba47b40 | 569 | { |
f6bcfd97 | 570 | if (g_isIdle) |
13fb7b7a | 571 | wxapp_install_idle_handler(); |
33a5bc52 | 572 | |
13fb7b7a | 573 | // printf( "Drag source: drag_data_delete\n" ); |
4ba47b40 | 574 | } |
f03fc89f | 575 | |
4ba47b40 RR |
576 | //---------------------------------------------------------------------------- |
577 | // "drag_begin" | |
578 | //---------------------------------------------------------------------------- | |
579 | ||
580 | static void source_drag_begin( GtkWidget *WXUNUSED(widget), | |
f03fc89f VZ |
581 | GdkDragContext *WXUNUSED(context), |
582 | wxDropSource *WXUNUSED(drop_source) ) | |
4ba47b40 | 583 | { |
f6bcfd97 | 584 | if (g_isIdle) |
13fb7b7a | 585 | wxapp_install_idle_handler(); |
5549fa65 | 586 | |
13fb7b7a | 587 | // printf( "Drag source: drag_begin.\n" ); |
4ba47b40 | 588 | } |
f03fc89f | 589 | |
4ba47b40 RR |
590 | //---------------------------------------------------------------------------- |
591 | // "drag_end" | |
592 | //---------------------------------------------------------------------------- | |
593 | ||
594 | static void source_drag_end( GtkWidget *WXUNUSED(widget), | |
f03fc89f VZ |
595 | GdkDragContext *WXUNUSED(context), |
596 | wxDropSource *drop_source ) | |
4ba47b40 | 597 | { |
5549fa65 RR |
598 | if (g_isIdle) wxapp_install_idle_handler(); |
599 | ||
13fb7b7a | 600 | // printf( "Drag source: drag_end.\n" ); |
4ba47b40 RR |
601 | |
602 | drop_source->m_waiting = FALSE; | |
603 | } | |
f03fc89f | 604 | |
7b5d5699 RR |
605 | //----------------------------------------------------------------------------- |
606 | // "configure_event" from m_iconWindow | |
607 | //----------------------------------------------------------------------------- | |
608 | ||
8ee9d618 | 609 | static gint |
7b5d5699 RR |
610 | gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source ) |
611 | { | |
8ee9d618 | 612 | if (g_isIdle) |
7b5d5699 RR |
613 | wxapp_install_idle_handler(); |
614 | ||
615 | wxDragResult action = wxDragNone; | |
616 | if (source->m_dragContext->action == GDK_ACTION_COPY) action = wxDragCopy; | |
617 | if (source->m_dragContext->action == GDK_ACTION_MOVE) action = wxDragMove; | |
618 | ||
974e8d94 | 619 | source->GiveFeedback( action ); |
8ee9d618 | 620 | |
7b5d5699 RR |
621 | return 0; |
622 | } | |
623 | ||
4ba47b40 RR |
624 | //--------------------------------------------------------------------------- |
625 | // wxDropSource | |
626 | //--------------------------------------------------------------------------- | |
22d5903e | 627 | |
f6bcfd97 BP |
628 | wxDropSource::wxDropSource(wxWindow *win, |
629 | const wxIcon &iconCopy, | |
630 | const wxIcon &iconMove, | |
631 | const wxIcon &iconNone) | |
22d5903e | 632 | { |
4ba47b40 | 633 | m_waiting = TRUE; |
f03fc89f | 634 | |
7b5d5699 | 635 | m_iconWindow = (GtkWidget*) NULL; |
8ee9d618 | 636 | |
22d5903e | 637 | m_window = win; |
a2053b27 RR |
638 | m_widget = win->m_widget; |
639 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
f03fc89f | 640 | |
22d5903e RR |
641 | m_retValue = wxDragCancel; |
642 | ||
f6bcfd97 | 643 | SetIcons(iconCopy, iconMove, iconNone); |
22d5903e RR |
644 | } |
645 | ||
f6bcfd97 BP |
646 | wxDropSource::wxDropSource(wxDataObject& data, |
647 | wxWindow *win, | |
648 | const wxIcon &iconCopy, | |
649 | const wxIcon &iconMove, | |
650 | const wxIcon &iconNone) | |
22d5903e | 651 | { |
4ba47b40 | 652 | m_waiting = TRUE; |
8ee9d618 | 653 | |
b068c4e8 | 654 | SetData( data ); |
f03fc89f | 655 | |
7b5d5699 | 656 | m_iconWindow = (GtkWidget*) NULL; |
8ee9d618 | 657 | |
22d5903e | 658 | m_window = win; |
a2053b27 RR |
659 | m_widget = win->m_widget; |
660 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
8ee9d618 | 661 | |
22d5903e | 662 | m_retValue = wxDragCancel; |
f03fc89f | 663 | |
f6bcfd97 BP |
664 | SetIcons(iconCopy, iconMove, iconNone); |
665 | } | |
666 | ||
667 | void wxDropSource::SetIcons(const wxIcon &iconCopy, | |
668 | const wxIcon &iconMove, | |
669 | const wxIcon &iconNone) | |
670 | { | |
671 | m_iconCopy = iconCopy; | |
672 | m_iconMove = iconMove; | |
673 | m_iconNone = iconNone; | |
674 | ||
675 | if ( !m_iconCopy.Ok() ) | |
676 | m_iconCopy = wxIcon(page_xpm); | |
677 | if ( !m_iconMove.Ok() ) | |
678 | m_iconMove = m_iconCopy; | |
679 | if ( !m_iconNone.Ok() ) | |
680 | m_iconNone = m_iconCopy; | |
22d5903e RR |
681 | } |
682 | ||
8e193f38 | 683 | wxDropSource::~wxDropSource() |
22d5903e | 684 | { |
22d5903e | 685 | } |
f03fc89f | 686 | |
f6bcfd97 | 687 | void wxDropSource::PrepareIcon( int action, GdkDragContext *context ) |
7b5d5699 | 688 | { |
f6bcfd97 BP |
689 | // get the right icon to display |
690 | wxIcon *icon = NULL; | |
691 | if ( action & GDK_ACTION_MOVE ) | |
692 | icon = &m_iconMove; | |
693 | else if ( action & GDK_ACTION_COPY ) | |
694 | icon = &m_iconCopy; | |
695 | else | |
696 | icon = &m_iconNone; | |
697 | ||
698 | GdkBitmap *mask; | |
699 | if ( icon->GetMask() ) | |
700 | mask = icon->GetMask()->GetBitmap(); | |
701 | else | |
702 | mask = (GdkBitmap *)NULL; | |
703 | ||
704 | GdkPixmap *pixmap = icon->GetPixmap(); | |
7b5d5699 RR |
705 | |
706 | gint width,height; | |
707 | gdk_window_get_size (pixmap, &width, &height); | |
708 | ||
709 | GdkColormap *colormap = gtk_widget_get_colormap( m_widget ); | |
9d084560 | 710 | #ifndef __WXGTK20__ |
7b5d5699 | 711 | gtk_widget_push_visual (gdk_colormap_get_visual (colormap)); |
9d084560 | 712 | #endif |
7b5d5699 RR |
713 | gtk_widget_push_colormap (colormap); |
714 | ||
715 | m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP); | |
716 | gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); | |
717 | gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE); | |
718 | ||
9d084560 | 719 | #ifndef __WXGTK20__ |
7b5d5699 | 720 | gtk_widget_pop_visual (); |
9d084560 | 721 | #endif |
7b5d5699 RR |
722 | gtk_widget_pop_colormap (); |
723 | ||
724 | gtk_widget_set_usize (m_iconWindow, width, height); | |
725 | gtk_widget_realize (m_iconWindow); | |
726 | ||
727 | gtk_signal_connect( GTK_OBJECT(m_iconWindow), "configure_event", | |
728 | GTK_SIGNAL_FUNC(gtk_dnd_window_configure_callback), (gpointer)this ); | |
8ee9d618 | 729 | |
7b5d5699 | 730 | gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE); |
8ee9d618 | 731 | |
7b5d5699 RR |
732 | if (mask) |
733 | gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0); | |
734 | ||
f6bcfd97 | 735 | gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 ); |
7b5d5699 RR |
736 | } |
737 | ||
fc9c7c09 | 738 | wxDragResult wxDropSource::DoDragDrop( bool allowMove ) |
22d5903e | 739 | { |
2d68e1b4 | 740 | wxASSERT_MSG( m_data, wxT("Drop source: no data") ); |
f03fc89f | 741 | |
8ee9d618 | 742 | if (!m_data) |
a3e7d24d | 743 | return (wxDragResult) wxDragNone; |
f03fc89f | 744 | |
8ee9d618 | 745 | if (m_data->GetFormatCount() == 0) |
a3e7d24d | 746 | return (wxDragResult) wxDragNone; |
8ee9d618 | 747 | |
513abb88 RR |
748 | // still in drag |
749 | if (g_blockEventsOnDrag) | |
750 | return (wxDragResult) wxDragNone; | |
751 | ||
f6bcfd97 | 752 | // disabled for now |
513abb88 | 753 | g_blockEventsOnDrag = TRUE; |
f03fc89f | 754 | |
4ba47b40 | 755 | RegisterWindow(); |
f03fc89f | 756 | |
4ba47b40 | 757 | m_waiting = TRUE; |
22d5903e | 758 | |
4ba47b40 | 759 | GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 ); |
8ee9d618 | 760 | |
a3e7d24d | 761 | wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; |
b068c4e8 | 762 | m_data->GetAllFormats( array ); |
a3e7d24d RR |
763 | for (size_t i = 0; i < m_data->GetFormatCount(); i++) |
764 | { | |
765 | GdkAtom atom = array[i]; | |
2d68e1b4 | 766 | wxLogDebug( wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ) ); |
a3e7d24d RR |
767 | gtk_target_list_add( target_list, atom, 0, 0 ); |
768 | } | |
769 | delete[] array; | |
f03fc89f | 770 | |
4ba47b40 RR |
771 | GdkEventMotion event; |
772 | event.window = m_widget->window; | |
773 | int x = 0; | |
774 | int y = 0; | |
775 | GdkModifierType state; | |
776 | gdk_window_get_pointer( event.window, &x, &y, &state ); | |
777 | event.x = x; | |
778 | event.y = y; | |
779 | event.state = state; | |
b02da6b1 | 780 | event.time = (guint32)GDK_CURRENT_TIME; |
f03fc89f | 781 | |
4ba47b40 RR |
782 | /* GTK wants to know which button was pressed which caused the dragging */ |
783 | int button_number = 0; | |
784 | if (event.state & GDK_BUTTON1_MASK) button_number = 1; | |
785 | else if (event.state & GDK_BUTTON2_MASK) button_number = 2; | |
786 | else if (event.state & GDK_BUTTON3_MASK) button_number = 3; | |
787 | ||
b453e1b2 RR |
788 | #if wxUSE_THREADS |
789 | /* disable GUI threads */ | |
790 | wxapp_uninstall_thread_wakeup(); | |
791 | #endif | |
8e193f38 | 792 | |
4ba47b40 RR |
793 | /* don't start dragging if no button is down */ |
794 | if (button_number) | |
22d5903e | 795 | { |
fc9c7c09 | 796 | GdkDragAction action = GDK_ACTION_COPY; |
f6bcfd97 | 797 | if (allowMove) action = (GdkDragAction)(GDK_ACTION_MOVE|GDK_ACTION_COPY); |
4ba47b40 | 798 | GdkDragContext *context = gtk_drag_begin( m_widget, |
f6bcfd97 BP |
799 | target_list, |
800 | action, | |
801 | button_number, /* number of mouse button which started drag */ | |
802 | (GdkEvent*) &event ); | |
8ee9d618 VZ |
803 | |
804 | m_dragContext = context; | |
805 | ||
f6bcfd97 | 806 | PrepareIcon( action, context ); |
f03fc89f | 807 | |
13fb7b7a | 808 | while (m_waiting) gtk_main_iteration(); |
f6bcfd97 BP |
809 | |
810 | if (context->action == GDK_ACTION_COPY) | |
13fb7b7a | 811 | m_retValue = wxDragCopy; |
f6bcfd97 | 812 | if (context->action == GDK_ACTION_MOVE) |
13fb7b7a | 813 | m_retValue = wxDragMove; |
4ba47b40 | 814 | } |
22d5903e | 815 | |
b453e1b2 RR |
816 | #if wxUSE_THREADS |
817 | /* re-enable GUI threads */ | |
818 | wxapp_install_thread_wakeup(); | |
819 | #endif | |
8e193f38 | 820 | |
4ba47b40 | 821 | g_blockEventsOnDrag = FALSE; |
f03fc89f | 822 | |
4ba47b40 RR |
823 | UnregisterWindow(); |
824 | ||
825 | return m_retValue; | |
22d5903e RR |
826 | } |
827 | ||
4ba47b40 | 828 | void wxDropSource::RegisterWindow() |
22d5903e | 829 | { |
4ba47b40 | 830 | if (!m_widget) return; |
f6bcfd97 | 831 | |
4ba47b40 | 832 | gtk_signal_connect( GTK_OBJECT(m_widget), "drag_data_get", |
f03fc89f | 833 | GTK_SIGNAL_FUNC (source_drag_data_get), (gpointer) this); |
4ba47b40 | 834 | gtk_signal_connect (GTK_OBJECT(m_widget), "drag_data_delete", |
f03fc89f | 835 | GTK_SIGNAL_FUNC (source_drag_data_delete), (gpointer) this ); |
4ba47b40 | 836 | gtk_signal_connect (GTK_OBJECT(m_widget), "drag_begin", |
f03fc89f | 837 | GTK_SIGNAL_FUNC (source_drag_begin), (gpointer) this ); |
4ba47b40 | 838 | gtk_signal_connect (GTK_OBJECT(m_widget), "drag_end", |
f03fc89f | 839 | GTK_SIGNAL_FUNC (source_drag_end), (gpointer) this ); |
4ba47b40 | 840 | |
22d5903e RR |
841 | } |
842 | ||
4ba47b40 | 843 | void wxDropSource::UnregisterWindow() |
22d5903e RR |
844 | { |
845 | if (!m_widget) return; | |
f03fc89f | 846 | |
4ba47b40 | 847 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
f03fc89f | 848 | GTK_SIGNAL_FUNC(source_drag_data_get), (gpointer) this ); |
4ba47b40 | 849 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
f03fc89f | 850 | GTK_SIGNAL_FUNC(source_drag_data_delete), (gpointer) this ); |
4ba47b40 | 851 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
f03fc89f | 852 | GTK_SIGNAL_FUNC(source_drag_begin), (gpointer) this ); |
4ba47b40 | 853 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
f03fc89f | 854 | GTK_SIGNAL_FUNC(source_drag_end), (gpointer) this ); |
22d5903e RR |
855 | } |
856 | ||
ac57418f | 857 | #endif |
93c5dd39 | 858 | // wxUSE_DRAG_AND_DROP |