1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dnd.cpp
3 // Purpose: wxDropTarget class
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if wxUSE_DRAG_AND_DROP
22 #include "wx/window.h"
23 #include "wx/gdicmn.h"
26 #include "wx/scopeguard.h"
30 //----------------------------------------------------------------------------
32 //----------------------------------------------------------------------------
34 extern bool g_blockEventsOnDrag
;
36 // the flags used for the last DoDragDrop()
37 static long gs_flagsForDrag
= 0;
39 // the trace mask we use with wxLogTrace() - call
40 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
41 // (there are quite a few of them, so don't enable this by default)
42 #define TRACE_DND "dnd"
44 // global variables because GTK+ DnD want to have the
45 // mouse event that caused it
46 extern GdkEvent
*g_lastMouseEvent
;
47 extern int g_lastButtonNumber
;
49 //----------------------------------------------------------------------------
51 //----------------------------------------------------------------------------
53 /* Copyright (c) Julian Smart */
54 static const char * page_xpm
[] = {
55 /* columns rows colors chars-per-pixel */
104 " 56477<<<<8<<9&:X ",
105 " 59642%&*=-;<09&:5 ",
106 " 5q9642%&*=-<<<<<# ",
107 " 5qqw777<<<<<88:>+ ",
108 " erqq9642%&*=t;::+ ",
109 " eyrqq9642%&*=t;:O ",
110 " eyywwww777<<<<t;O ",
111 " e0yyrqq9642%&*=to ",
112 " e00yyrqq9642%&*=o ",
113 " eu0wwwwwww777<&*X ",
114 " euu00yyrqq9642%&X ",
115 " eiuu00yyrqq9642%X ",
116 " eiiwwwwwwwwww742$ ",
117 " eiiiuu00yyrqq964$ ",
118 " eiiiiuu00yyrqq96$ ",
119 " eiiiiiuu00yyrqq95 ",
120 " eiiiiiiuu00yyrqq5 ",
121 " eeeeeeeeeeeeee55e ",
130 // ============================================================================
132 // ============================================================================
134 // ----------------------------------------------------------------------------
135 // convert between GTK+ and wxWidgets DND constants
136 // ----------------------------------------------------------------------------
138 static wxDragResult
ConvertFromGTK(long action
)
142 case GDK_ACTION_COPY
:
145 case GDK_ACTION_LINK
:
148 case GDK_ACTION_MOVE
:
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
160 static void target_drag_leave( GtkWidget
*WXUNUSED(widget
),
161 GdkDragContext
*context
,
162 guint
WXUNUSED(time
),
163 wxDropTarget
*drop_target
)
165 /* inform the wxDropTarget about the current GdkDragContext.
166 this is only valid for the duration of this call */
167 drop_target
->GtkSetDragContext( context
);
169 /* we don't need return values. this event is just for
171 drop_target
->OnLeave();
173 /* this has to be done because GDK has no "drag_enter" event */
174 drop_target
->m_firstMotion
= true;
176 /* after this, invalidate the drop_target's GdkDragContext */
177 drop_target
->GtkSetDragContext( NULL
);
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
186 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
187 GdkDragContext
*context
,
191 wxDropTarget
*drop_target
)
193 /* Owen Taylor: "if the coordinates not in a drop zone,
194 return FALSE, otherwise call gtk_drag_status() and
198 wxPrintf( "motion\n" );
200 for (tmp_list
= context
->targets
; tmp_list
; tmp_list
= tmp_list
->next
)
202 wxString atom
= wxString::FromAscii( gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list
->data
)) );
203 wxPrintf( "Atom: %s\n", atom
);
207 /* inform the wxDropTarget about the current GdkDragContext.
208 this is only valid for the duration of this call */
209 drop_target
->GtkSetDragContext( context
);
211 // GTK+ always supposes that we want to copy the data by default while we
212 // might want to move it, so examine not only suggested_action - which is
213 // only good if we don't have our own preferences - but also the actions
216 if (drop_target
->GetDefaultAction() == wxDragNone
)
218 // use default action set by wxDropSource::DoDragDrop()
219 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
220 (context
->actions
& GDK_ACTION_MOVE
) )
222 // move is requested by the program and allowed by GTK+ - do it, even
223 // though suggested_action may be currently wxDragCopy
226 else // use whatever GTK+ says we should
228 result
= ConvertFromGTK(context
->suggested_action
);
230 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
232 // we're requested to move but we can't
237 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
238 (context
->actions
& GDK_ACTION_MOVE
))
245 if (context
->actions
& GDK_ACTION_COPY
)
247 else if (context
->actions
& GDK_ACTION_MOVE
)
253 if (drop_target
->m_firstMotion
)
255 /* the first "drag_motion" event substitutes a "drag_enter" event */
256 result
= drop_target
->OnEnter( x
, y
, result
);
260 /* give program a chance to react (i.e. to say no by returning FALSE) */
261 result
= drop_target
->OnDragOver( x
, y
, result
);
264 bool ret
= wxIsDragResultOk( result
);
267 GdkDragAction action
;
268 if (result
== wxDragCopy
)
269 action
= GDK_ACTION_COPY
;
270 else if (result
== wxDragLink
)
271 action
= GDK_ACTION_LINK
;
273 action
= GDK_ACTION_MOVE
;
275 gdk_drag_status( context
, action
, time
);
278 /* after this, invalidate the drop_target's GdkDragContext */
279 drop_target
->GtkSetDragContext( NULL
);
281 /* this has to be done because GDK has no "drag_enter" event */
282 drop_target
->m_firstMotion
= false;
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
293 static gboolean
target_drag_drop( GtkWidget
*widget
,
294 GdkDragContext
*context
,
298 wxDropTarget
*drop_target
)
300 /* Owen Taylor: "if the drop is not in a drop zone,
301 return FALSE, otherwise, if you aren't accepting
302 the drop, call gtk_drag_finish() with success == FALSE
303 otherwise call gtk_drag_data_get()" */
305 /* this seems to make a difference between not accepting
306 due to wrong target area and due to wrong format. let
307 us hope that this is not required.. */
309 /* inform the wxDropTarget about the current GdkDragContext.
310 this is only valid for the duration of this call */
311 drop_target
->GtkSetDragContext( context
);
313 /* inform the wxDropTarget about the current drag widget.
314 this is only valid for the duration of this call */
315 drop_target
->GtkSetDragWidget( widget
);
317 /* inform the wxDropTarget about the current drag time.
318 this is only valid for the duration of this call */
319 drop_target
->GtkSetDragTime( time
);
322 wxDragResult result = wxDragMove;
323 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
326 /* reset the block here as someone might very well
327 show a dialog as a reaction to a drop and this
328 wouldn't work without events */
329 g_blockEventsOnDrag
= false;
331 bool ret
= drop_target
->OnDrop( x
, y
);
335 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
337 /* cancel the whole thing */
338 gtk_drag_finish( context
,
339 FALSE
, /* no success */
340 FALSE
, /* don't delete data on dropping side */
345 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
348 /* disable GUI threads */
351 GdkAtom format
= drop_target
->GtkGetMatchingPair();
353 // this does happen somehow, see bug 555111
354 wxCHECK_MSG( format
, FALSE
, wxT("no matching GdkAtom for format?") );
357 GdkDragAction action = GDK_ACTION_MOVE;
358 if (result == wxDragCopy) action == GDK_ACTION_COPY;
359 context->action = action;
361 /* this should trigger an "drag_data_received" event */
362 gtk_drag_get_data( widget
,
368 /* re-enable GUI threads */
372 /* after this, invalidate the drop_target's GdkDragContext */
373 drop_target
->GtkSetDragContext( NULL
);
375 /* after this, invalidate the drop_target's drag widget */
376 drop_target
->GtkSetDragWidget( NULL
);
378 /* this has to be done because GDK has no "drag_enter" event */
379 drop_target
->m_firstMotion
= true;
385 // ----------------------------------------------------------------------------
386 // "drag_data_received"
387 // ----------------------------------------------------------------------------
390 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
391 GdkDragContext
*context
,
394 GtkSelectionData
*data
,
395 guint
WXUNUSED(info
),
397 wxDropTarget
*drop_target
)
399 /* Owen Taylor: "call gtk_drag_finish() with
402 if ((data
->length
<= 0) || (data
->format
!= 8))
404 /* negative data length and non 8-bit data format
405 qualifies for junk */
406 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
411 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
413 /* inform the wxDropTarget about the current GtkSelectionData.
414 this is only valid for the duration of this call */
415 drop_target
->GtkSetDragData( data
);
417 wxDragResult result
= ConvertFromGTK(context
->action
);
419 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
421 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
423 /* tell GTK that data transfer was successful */
424 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
428 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
430 /* tell GTK that data transfer was not successful */
431 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
434 /* after this, invalidate the drop_target's drag data */
435 drop_target
->GtkSetDragData( NULL
);
439 //----------------------------------------------------------------------------
441 //----------------------------------------------------------------------------
443 wxDropTarget::wxDropTarget( wxDataObject
*data
)
444 : wxDropTargetBase( data
)
446 m_firstMotion
= true;
447 m_dragContext
= NULL
;
453 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
457 // GetMatchingPair() checks for m_dataObject too, no need to do it here
459 // disable the trace message from GetMatchingPair() by passing true to it
460 // (there are just too many of them otherwise)
461 return (GtkGetMatchingPair(true) != (GdkAtom
) 0) ? def
: wxDragNone
;
464 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
469 return (GtkGetMatchingPair() != (GdkAtom
) 0);
472 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
478 if (GtkGetMatchingPair() == (GdkAtom
) 0)
481 return GetData() ? def
: wxDragNone
;
485 wxDataFormat
wxDropTarget::GetMatchingPair()
487 return wxDataFormat( GtkGetMatchingPair() );
490 GdkAtom
wxDropTarget::GtkGetMatchingPair(bool quiet
)
498 GList
*child
= m_dragContext
->targets
;
501 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
502 wxDataFormat
format( formatAtom
);
506 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
507 format
.GetId().c_str());
510 if (m_dataObject
->IsSupportedFormat( format
))
519 bool wxDropTarget::GetData()
527 wxDataFormat
dragFormat( m_dragData
->target
);
529 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
532 m_dataObject
->SetData( dragFormat
, (size_t)m_dragData
->length
, (const void*)m_dragData
->data
);
537 void wxDropTarget::GtkUnregisterWidget( GtkWidget
*widget
)
539 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
541 gtk_drag_dest_unset( widget
);
543 g_signal_handlers_disconnect_by_func (widget
,
544 (gpointer
) target_drag_leave
, this);
545 g_signal_handlers_disconnect_by_func (widget
,
546 (gpointer
) target_drag_motion
, this);
547 g_signal_handlers_disconnect_by_func (widget
,
548 (gpointer
) target_drag_drop
, this);
549 g_signal_handlers_disconnect_by_func (widget
,
550 (gpointer
) target_drag_data_received
, this);
553 void wxDropTarget::GtkRegisterWidget( GtkWidget
*widget
)
555 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
557 /* gtk_drag_dest_set() determines what default behaviour we'd like
558 GTK to supply. we don't want to specify out targets (=formats)
559 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
560 not GTK_DEST_DEFAULT_DROP). instead we react individually to
561 "drag_motion" and "drag_drop" events. this makes it possible
562 to allow dropping on only a small area. we should set
563 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
564 highlighting if dragging over standard controls, but this
565 seems to be broken without the other two. */
567 gtk_drag_dest_set( widget
,
568 (GtkDestDefaults
) 0, /* no default behaviour */
569 NULL
, /* we don't supply any formats here */
570 0, /* number of targets = 0 */
571 (GdkDragAction
) 0 ); /* we don't supply any actions here */
573 g_signal_connect (widget
, "drag_leave",
574 G_CALLBACK (target_drag_leave
), this);
576 g_signal_connect (widget
, "drag_motion",
577 G_CALLBACK (target_drag_motion
), this);
579 g_signal_connect (widget
, "drag_drop",
580 G_CALLBACK (target_drag_drop
), this);
582 g_signal_connect (widget
, "drag_data_received",
583 G_CALLBACK (target_drag_data_received
), this);
586 //----------------------------------------------------------------------------
588 //----------------------------------------------------------------------------
592 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
593 GdkDragContext
*WXUNUSED(context
),
594 GtkSelectionData
*selection_data
,
595 guint
WXUNUSED(info
),
596 guint
WXUNUSED(time
),
597 wxDropSource
*drop_source
)
599 wxDataFormat
format( selection_data
->target
);
601 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
602 format
.GetId().c_str());
604 drop_source
->m_retValue
= wxDragCancel
;
606 wxDataObject
*data
= drop_source
->GetDataObject();
610 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
614 if (!data
->IsSupportedFormat(format
))
616 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
620 if (data
->GetDataSize(format
) == 0)
622 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
626 size_t size
= data
->GetDataSize(format
);
628 // printf( "data size: %d.\n", (int)data_size );
630 guchar
*d
= new guchar
[size
];
632 if (!data
->GetDataHere( format
, (void*)d
))
639 /* disable GUI threads */
642 gtk_selection_data_set( selection_data
,
643 selection_data
->target
,
649 /* enable GUI threads */
656 //----------------------------------------------------------------------------
658 //----------------------------------------------------------------------------
661 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
662 GdkDragContext
*WXUNUSED(context
),
663 wxDropSource
*drop_source
)
665 // printf( "Drag source: drag_end.\n" );
667 drop_source
->m_waiting
= false;
671 //-----------------------------------------------------------------------------
672 // "configure_event" from m_iconWindow
673 //-----------------------------------------------------------------------------
677 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
679 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
685 //---------------------------------------------------------------------------
687 //---------------------------------------------------------------------------
689 wxDropSource::wxDropSource(wxWindow
*win
,
690 const wxIcon
&iconCopy
,
691 const wxIcon
&iconMove
,
692 const wxIcon
&iconNone
)
699 m_widget
= win
->m_widget
;
700 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
702 m_retValue
= wxDragCancel
;
704 SetIcons(iconCopy
, iconMove
, iconNone
);
707 wxDropSource::wxDropSource(wxDataObject
& data
,
709 const wxIcon
&iconCopy
,
710 const wxIcon
&iconMove
,
711 const wxIcon
&iconNone
)
720 m_widget
= win
->m_widget
;
721 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
723 m_retValue
= wxDragCancel
;
725 SetIcons(iconCopy
, iconMove
, iconNone
);
728 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
729 const wxIcon
&iconMove
,
730 const wxIcon
&iconNone
)
732 m_iconCopy
= iconCopy
;
733 m_iconMove
= iconMove
;
734 m_iconNone
= iconNone
;
736 if ( !m_iconCopy
.Ok() )
737 m_iconCopy
= wxIcon(page_xpm
);
738 if ( !m_iconMove
.Ok() )
739 m_iconMove
= m_iconCopy
;
740 if ( !m_iconNone
.Ok() )
741 m_iconNone
= m_iconCopy
;
744 wxDropSource::~wxDropSource()
748 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
750 // get the right icon to display
752 if ( action
& GDK_ACTION_MOVE
)
754 else if ( action
& GDK_ACTION_COPY
)
760 if ( icon
->GetMask() )
761 mask
= icon
->GetMask()->GetBitmap();
765 GdkPixmap
*pixmap
= icon
->GetPixmap();
768 gdk_drawable_get_size (pixmap
, &width
, &height
);
770 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
771 gtk_widget_push_colormap (colormap
);
773 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
774 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
775 gtk_widget_set_app_paintable (m_iconWindow
, TRUE
);
777 gtk_widget_pop_colormap ();
779 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
780 gtk_widget_realize (m_iconWindow
);
782 g_signal_connect (m_iconWindow
, "configure_event",
783 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
785 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
788 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
790 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
793 wxDragResult
wxDropSource::DoDragDrop(int flags
)
795 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
796 wxT("Drop source: no data") );
799 if (g_blockEventsOnDrag
)
802 // don't start dragging if no button is down
803 if (g_lastButtonNumber
== 0)
806 // we can only start a drag after a mouse event
807 if (g_lastMouseEvent
== NULL
)
810 GTKConnectDragSignals();
811 wxON_BLOCK_EXIT_OBJ0(*this, wxDropSource::GTKDisconnectDragSignals
);
815 GtkTargetList
*target_list
= gtk_target_list_new( NULL
, 0 );
817 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
818 m_data
->GetAllFormats( array
);
819 size_t count
= m_data
->GetFormatCount();
820 for (size_t i
= 0; i
< count
; i
++)
822 GdkAtom atom
= array
[i
];
823 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"),
824 gdk_atom_name( atom
));
825 gtk_target_list_add( target_list
, atom
, 0, 0 );
829 int action
= GDK_ACTION_COPY
;
830 if ( flags
& wxDrag_AllowMove
)
831 action
|= GDK_ACTION_MOVE
;
833 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
834 // to use a global to pass the flags to the drop target but I'd
835 // surely prefer a better way to do it
836 gs_flagsForDrag
= flags
;
838 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
840 (GdkDragAction
)action
,
841 g_lastButtonNumber
, // number of mouse button which started drag
842 (GdkEvent
*) g_lastMouseEvent
);
846 // this can happen e.g. if gdk_pointer_grab() failed
850 m_dragContext
= context
;
852 PrepareIcon( action
, context
);
855 gtk_main_iteration();
857 m_retValue
= ConvertFromGTK(context
->action
);
858 if ( m_retValue
== wxDragNone
)
859 m_retValue
= wxDragCancel
;
864 void wxDropSource::GTKConnectDragSignals()
869 g_blockEventsOnDrag
= true;
871 g_signal_connect (m_widget
, "drag_data_get",
872 G_CALLBACK (source_drag_data_get
), this);
873 g_signal_connect (m_widget
, "drag_end",
874 G_CALLBACK (source_drag_end
), this);
878 void wxDropSource::GTKDisconnectDragSignals()
883 g_blockEventsOnDrag
= false;
885 g_signal_handlers_disconnect_by_func (m_widget
,
886 (gpointer
) source_drag_data_get
,
888 g_signal_handlers_disconnect_by_func (m_widget
,
889 (gpointer
) source_drag_end
,
894 // wxUSE_DRAG_AND_DROP