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;
40 // the trace mask we use with wxLogTrace() - call
41 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
42 // (there are quite a few of them, so don't enable this by default)
43 static const wxChar
*TRACE_DND
= _T("dnd");
46 // global variables because GTK+ DnD want to have the
47 // mouse event that caused it
48 extern GdkEvent
*g_lastMouseEvent
;
49 extern int g_lastButtonNumber
;
51 //----------------------------------------------------------------------------
53 //----------------------------------------------------------------------------
55 /* Copyright (c) Julian Smart */
56 static const char * page_xpm
[] = {
57 /* columns rows colors chars-per-pixel */
106 " 56477<<<<8<<9&:X ",
107 " 59642%&*=-;<09&:5 ",
108 " 5q9642%&*=-<<<<<# ",
109 " 5qqw777<<<<<88:>+ ",
110 " erqq9642%&*=t;::+ ",
111 " eyrqq9642%&*=t;:O ",
112 " eyywwww777<<<<t;O ",
113 " e0yyrqq9642%&*=to ",
114 " e00yyrqq9642%&*=o ",
115 " eu0wwwwwww777<&*X ",
116 " euu00yyrqq9642%&X ",
117 " eiuu00yyrqq9642%X ",
118 " eiiwwwwwwwwww742$ ",
119 " eiiiuu00yyrqq964$ ",
120 " eiiiiuu00yyrqq96$ ",
121 " eiiiiiuu00yyrqq95 ",
122 " eiiiiiiuu00yyrqq5 ",
123 " eeeeeeeeeeeeee55e ",
132 // ============================================================================
134 // ============================================================================
136 // ----------------------------------------------------------------------------
137 // convert between GTK+ and wxWidgets DND constants
138 // ----------------------------------------------------------------------------
140 static wxDragResult
ConvertFromGTK(long action
)
144 case GDK_ACTION_COPY
:
147 case GDK_ACTION_LINK
:
150 case GDK_ACTION_MOVE
:
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
162 static void target_drag_leave( GtkWidget
*WXUNUSED(widget
),
163 GdkDragContext
*context
,
164 guint
WXUNUSED(time
),
165 wxDropTarget
*drop_target
)
167 /* inform the wxDropTarget about the current GdkDragContext.
168 this is only valid for the duration of this call */
169 drop_target
->GtkSetDragContext( context
);
171 /* we don't need return values. this event is just for
173 drop_target
->OnLeave();
175 /* this has to be done because GDK has no "drag_enter" event */
176 drop_target
->m_firstMotion
= true;
178 /* after this, invalidate the drop_target's GdkDragContext */
179 drop_target
->GtkSetDragContext( NULL
);
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
188 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
189 GdkDragContext
*context
,
193 wxDropTarget
*drop_target
)
195 /* Owen Taylor: "if the coordinates not in a drop zone,
196 return FALSE, otherwise call gtk_drag_status() and
200 wxPrintf( "motion\n" );
202 for (tmp_list
= context
->targets
; tmp_list
; tmp_list
= tmp_list
->next
)
204 wxString atom
= wxString::FromAscii( gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list
->data
)) );
205 wxPrintf( "Atom: %s\n", atom
);
209 /* inform the wxDropTarget about the current GdkDragContext.
210 this is only valid for the duration of this call */
211 drop_target
->GtkSetDragContext( context
);
213 // GTK+ always supposes that we want to copy the data by default while we
214 // might want to move it, so examine not only suggested_action - which is
215 // only good if we don't have our own preferences - but also the actions
218 if (drop_target
->GetDefaultAction() == wxDragNone
)
220 // use default action set by wxDropSource::DoDragDrop()
221 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
222 (context
->actions
& GDK_ACTION_MOVE
) )
224 // move is requested by the program and allowed by GTK+ - do it, even
225 // though suggested_action may be currently wxDragCopy
228 else // use whatever GTK+ says we should
230 result
= ConvertFromGTK(context
->suggested_action
);
232 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
234 // we're requested to move but we can't
239 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
240 (context
->actions
& GDK_ACTION_MOVE
))
247 if (context
->actions
& GDK_ACTION_COPY
)
249 else if (context
->actions
& GDK_ACTION_MOVE
)
255 if (drop_target
->m_firstMotion
)
257 /* the first "drag_motion" event substitutes a "drag_enter" event */
258 result
= drop_target
->OnEnter( x
, y
, result
);
262 /* give program a chance to react (i.e. to say no by returning FALSE) */
263 result
= drop_target
->OnDragOver( x
, y
, result
);
266 bool ret
= wxIsDragResultOk( result
);
269 GdkDragAction action
;
270 if (result
== wxDragCopy
)
271 action
= GDK_ACTION_COPY
;
272 else if (result
== wxDragLink
)
273 action
= GDK_ACTION_LINK
;
275 action
= GDK_ACTION_MOVE
;
277 gdk_drag_status( context
, action
, time
);
280 /* after this, invalidate the drop_target's GdkDragContext */
281 drop_target
->GtkSetDragContext( NULL
);
283 /* this has to be done because GDK has no "drag_enter" event */
284 drop_target
->m_firstMotion
= false;
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
295 static gboolean
target_drag_drop( GtkWidget
*widget
,
296 GdkDragContext
*context
,
300 wxDropTarget
*drop_target
)
302 /* Owen Taylor: "if the drop is not in a drop zone,
303 return FALSE, otherwise, if you aren't accepting
304 the drop, call gtk_drag_finish() with success == FALSE
305 otherwise call gtk_drag_data_get()" */
307 /* this seems to make a difference between not accepting
308 due to wrong target area and due to wrong format. let
309 us hope that this is not required.. */
311 /* inform the wxDropTarget about the current GdkDragContext.
312 this is only valid for the duration of this call */
313 drop_target
->GtkSetDragContext( context
);
315 /* inform the wxDropTarget about the current drag widget.
316 this is only valid for the duration of this call */
317 drop_target
->GtkSetDragWidget( widget
);
319 /* inform the wxDropTarget about the current drag time.
320 this is only valid for the duration of this call */
321 drop_target
->GtkSetDragTime( time
);
324 wxDragResult result = wxDragMove;
325 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
328 /* reset the block here as someone might very well
329 show a dialog as a reaction to a drop and this
330 wouldn't work without events */
331 g_blockEventsOnDrag
= false;
333 bool ret
= drop_target
->OnDrop( x
, y
);
338 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
341 /* cancel the whole thing */
342 gtk_drag_finish( context
,
343 FALSE
, /* no success */
344 FALSE
, /* don't delete data on dropping side */
350 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
354 /* disable GUI threads */
357 GdkAtom format
= drop_target
->GtkGetMatchingPair();
359 // this does happen somehow, see bug 555111
360 wxCHECK_MSG( format
, FALSE
, _T("no matching GdkAtom for format?") );
363 GdkDragAction action = GDK_ACTION_MOVE;
364 if (result == wxDragCopy) action == GDK_ACTION_COPY;
365 context->action = action;
367 /* this should trigger an "drag_data_received" event */
368 gtk_drag_get_data( widget
,
374 /* re-enable GUI threads */
378 /* after this, invalidate the drop_target's GdkDragContext */
379 drop_target
->GtkSetDragContext( NULL
);
381 /* after this, invalidate the drop_target's drag widget */
382 drop_target
->GtkSetDragWidget( NULL
);
384 /* this has to be done because GDK has no "drag_enter" event */
385 drop_target
->m_firstMotion
= true;
391 // ----------------------------------------------------------------------------
392 // "drag_data_received"
393 // ----------------------------------------------------------------------------
396 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
397 GdkDragContext
*context
,
400 GtkSelectionData
*data
,
401 guint
WXUNUSED(info
),
403 wxDropTarget
*drop_target
)
405 /* Owen Taylor: "call gtk_drag_finish() with
408 if ((data
->length
<= 0) || (data
->format
!= 8))
410 /* negative data length and non 8-bit data format
411 qualifies for junk */
412 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
418 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
421 /* inform the wxDropTarget about the current GtkSelectionData.
422 this is only valid for the duration of this call */
423 drop_target
->GtkSetDragData( data
);
425 wxDragResult result
= ConvertFromGTK(context
->action
);
427 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
430 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
433 /* tell GTK that data transfer was successful */
434 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
439 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
442 /* tell GTK that data transfer was not successful */
443 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
446 /* after this, invalidate the drop_target's drag data */
447 drop_target
->GtkSetDragData( NULL
);
451 //----------------------------------------------------------------------------
453 //----------------------------------------------------------------------------
455 wxDropTarget::wxDropTarget( wxDataObject
*data
)
456 : wxDropTargetBase( data
)
458 m_firstMotion
= true;
459 m_dragContext
= NULL
;
465 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
469 // GetMatchingPair() checks for m_dataObject too, no need to do it here
471 // disable the debug message from GetMatchingPair() - there are too many
477 return (GtkGetMatchingPair() != (GdkAtom
) 0) ? def
: wxDragNone
;
480 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
485 return (GtkGetMatchingPair() != (GdkAtom
) 0);
488 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
494 if (GtkGetMatchingPair() == (GdkAtom
) 0)
497 return GetData() ? def
: wxDragNone
;
501 wxDataFormat
wxDropTarget::GetMatchingPair()
503 return wxDataFormat( GtkGetMatchingPair() );
506 GdkAtom
wxDropTarget::GtkGetMatchingPair()
514 GList
*child
= m_dragContext
->targets
;
517 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
518 wxDataFormat
format( formatAtom
);
521 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
522 format
.GetId().c_str());
525 if (m_dataObject
->IsSupportedFormat( format
))
534 bool wxDropTarget::GetData()
542 wxDataFormat
dragFormat( m_dragData
->target
);
544 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
547 m_dataObject
->SetData( dragFormat
, (size_t)m_dragData
->length
, (const void*)m_dragData
->data
);
552 void wxDropTarget::GtkUnregisterWidget( GtkWidget
*widget
)
554 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
556 gtk_drag_dest_unset( widget
);
558 g_signal_handlers_disconnect_by_func (widget
,
559 (gpointer
) target_drag_leave
, this);
560 g_signal_handlers_disconnect_by_func (widget
,
561 (gpointer
) target_drag_motion
, this);
562 g_signal_handlers_disconnect_by_func (widget
,
563 (gpointer
) target_drag_drop
, this);
564 g_signal_handlers_disconnect_by_func (widget
,
565 (gpointer
) target_drag_data_received
, this);
568 void wxDropTarget::GtkRegisterWidget( GtkWidget
*widget
)
570 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
572 /* gtk_drag_dest_set() determines what default behaviour we'd like
573 GTK to supply. we don't want to specify out targets (=formats)
574 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
575 not GTK_DEST_DEFAULT_DROP). instead we react individually to
576 "drag_motion" and "drag_drop" events. this makes it possible
577 to allow dropping on only a small area. we should set
578 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
579 highlighting if dragging over standard controls, but this
580 seems to be broken without the other two. */
582 gtk_drag_dest_set( widget
,
583 (GtkDestDefaults
) 0, /* no default behaviour */
584 NULL
, /* we don't supply any formats here */
585 0, /* number of targets = 0 */
586 (GdkDragAction
) 0 ); /* we don't supply any actions here */
588 g_signal_connect (widget
, "drag_leave",
589 G_CALLBACK (target_drag_leave
), this);
591 g_signal_connect (widget
, "drag_motion",
592 G_CALLBACK (target_drag_motion
), this);
594 g_signal_connect (widget
, "drag_drop",
595 G_CALLBACK (target_drag_drop
), this);
597 g_signal_connect (widget
, "drag_data_received",
598 G_CALLBACK (target_drag_data_received
), this);
601 //----------------------------------------------------------------------------
603 //----------------------------------------------------------------------------
607 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
608 GdkDragContext
*WXUNUSED(context
),
609 GtkSelectionData
*selection_data
,
610 guint
WXUNUSED(info
),
611 guint
WXUNUSED(time
),
612 wxDropSource
*drop_source
)
614 wxDataFormat
format( selection_data
->target
);
617 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
618 format
.GetId().c_str());
621 drop_source
->m_retValue
= wxDragCancel
;
623 wxDataObject
*data
= drop_source
->GetDataObject();
628 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
633 if (!data
->IsSupportedFormat(format
))
636 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
641 if (data
->GetDataSize(format
) == 0)
644 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
649 size_t size
= data
->GetDataSize(format
);
651 // printf( "data size: %d.\n", (int)data_size );
653 guchar
*d
= new guchar
[size
];
655 if (!data
->GetDataHere( format
, (void*)d
))
662 /* disable GUI threads */
665 gtk_selection_data_set( selection_data
,
666 selection_data
->target
,
672 /* enable GUI threads */
679 //----------------------------------------------------------------------------
681 //----------------------------------------------------------------------------
684 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
685 GdkDragContext
*WXUNUSED(context
),
686 wxDropSource
*drop_source
)
688 // printf( "Drag source: drag_end.\n" );
690 drop_source
->m_waiting
= false;
694 //-----------------------------------------------------------------------------
695 // "configure_event" from m_iconWindow
696 //-----------------------------------------------------------------------------
700 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
702 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
708 //---------------------------------------------------------------------------
710 //---------------------------------------------------------------------------
712 wxDropSource::wxDropSource(wxWindow
*win
,
713 const wxIcon
&iconCopy
,
714 const wxIcon
&iconMove
,
715 const wxIcon
&iconNone
)
722 m_widget
= win
->m_widget
;
723 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
725 m_retValue
= wxDragCancel
;
727 SetIcons(iconCopy
, iconMove
, iconNone
);
730 wxDropSource::wxDropSource(wxDataObject
& data
,
732 const wxIcon
&iconCopy
,
733 const wxIcon
&iconMove
,
734 const wxIcon
&iconNone
)
743 m_widget
= win
->m_widget
;
744 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
746 m_retValue
= wxDragCancel
;
748 SetIcons(iconCopy
, iconMove
, iconNone
);
751 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
752 const wxIcon
&iconMove
,
753 const wxIcon
&iconNone
)
755 m_iconCopy
= iconCopy
;
756 m_iconMove
= iconMove
;
757 m_iconNone
= iconNone
;
759 if ( !m_iconCopy
.Ok() )
760 m_iconCopy
= wxIcon(page_xpm
);
761 if ( !m_iconMove
.Ok() )
762 m_iconMove
= m_iconCopy
;
763 if ( !m_iconNone
.Ok() )
764 m_iconNone
= m_iconCopy
;
767 wxDropSource::~wxDropSource()
771 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
773 // get the right icon to display
775 if ( action
& GDK_ACTION_MOVE
)
777 else if ( action
& GDK_ACTION_COPY
)
783 if ( icon
->GetMask() )
784 mask
= icon
->GetMask()->GetBitmap();
788 GdkPixmap
*pixmap
= icon
->GetPixmap();
791 gdk_drawable_get_size (pixmap
, &width
, &height
);
793 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
794 gtk_widget_push_colormap (colormap
);
796 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
797 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
798 gtk_widget_set_app_paintable (m_iconWindow
, TRUE
);
800 gtk_widget_pop_colormap ();
802 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
803 gtk_widget_realize (m_iconWindow
);
805 g_signal_connect (m_iconWindow
, "configure_event",
806 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
808 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
811 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
813 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
816 wxDragResult
wxDropSource::DoDragDrop(int flags
)
818 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
819 wxT("Drop source: no data") );
822 if (g_blockEventsOnDrag
)
825 // don't start dragging if no button is down
826 if (g_lastButtonNumber
== 0)
829 // we can only start a drag after a mouse event
830 if (g_lastMouseEvent
== NULL
)
833 GTKConnectDragSignals();
834 wxON_BLOCK_EXIT_OBJ0(*this, wxDropSource::GTKDisconnectDragSignals
);
838 GtkTargetList
*target_list
= gtk_target_list_new( NULL
, 0 );
840 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
841 m_data
->GetAllFormats( array
);
842 size_t count
= m_data
->GetFormatCount();
843 for (size_t i
= 0; i
< count
; i
++)
845 GdkAtom atom
= array
[i
];
847 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom
));
849 gtk_target_list_add( target_list
, atom
, 0, 0 );
853 int action
= GDK_ACTION_COPY
;
854 if ( flags
& wxDrag_AllowMove
)
855 action
|= GDK_ACTION_MOVE
;
857 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
858 // to use a global to pass the flags to the drop target but I'd
859 // surely prefer a better way to do it
860 gs_flagsForDrag
= flags
;
862 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
864 (GdkDragAction
)action
,
865 g_lastButtonNumber
, // number of mouse button which started drag
866 (GdkEvent
*) g_lastMouseEvent
);
870 // this can happen e.g. if gdk_pointer_grab() failed
874 m_dragContext
= context
;
876 PrepareIcon( action
, context
);
879 gtk_main_iteration();
881 m_retValue
= ConvertFromGTK(context
->action
);
882 if ( m_retValue
== wxDragNone
)
883 m_retValue
= wxDragCancel
;
888 void wxDropSource::GTKConnectDragSignals()
893 g_blockEventsOnDrag
= true;
895 g_signal_connect (m_widget
, "drag_data_get",
896 G_CALLBACK (source_drag_data_get
), this);
897 g_signal_connect (m_widget
, "drag_end",
898 G_CALLBACK (source_drag_end
), this);
902 void wxDropSource::GTKDisconnectDragSignals()
907 g_blockEventsOnDrag
= false;
909 g_signal_handlers_disconnect_by_func (m_widget
,
910 (gpointer
) source_drag_data_get
,
912 g_signal_handlers_disconnect_by_func (m_widget
,
913 (gpointer
) source_drag_end
,
918 // wxUSE_DRAG_AND_DROP