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
->SetDragContext( 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
->SetDragContext( (GdkDragContext
*) 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
199 /* inform the wxDropTarget about the current GdkDragContext.
200 this is only valid for the duration of this call */
201 drop_target
->SetDragContext( context
);
203 // GTK+ always supposes that we want to copy the data by default while we
204 // might want to move it, so examine not only suggested_action - which is
205 // only good if we don't have our own preferences - but also the actions
208 if (drop_target
->GetDefaultAction() == wxDragNone
)
210 // use default action set by wxDropSource::DoDragDrop()
211 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
212 (context
->actions
& GDK_ACTION_MOVE
) )
214 // move is requested by the program and allowed by GTK+ - do it, even
215 // though suggested_action may be currently wxDragCopy
218 else // use whatever GTK+ says we should
220 result
= ConvertFromGTK(context
->suggested_action
);
222 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
224 // we're requested to move but we can't
229 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
230 (context
->actions
& GDK_ACTION_MOVE
))
236 if (context
->actions
& GDK_ACTION_COPY
)
238 else if (context
->actions
& GDK_ACTION_MOVE
)
244 if (drop_target
->m_firstMotion
)
246 /* the first "drag_motion" event substitutes a "drag_enter" event */
247 result
= drop_target
->OnEnter( x
, y
, result
);
251 /* give program a chance to react (i.e. to say no by returning FALSE) */
252 result
= drop_target
->OnDragOver( x
, y
, result
);
255 bool ret
= wxIsDragResultOk( result
);
258 GdkDragAction action
;
259 if (result
== wxDragCopy
)
260 action
= GDK_ACTION_COPY
;
261 else if (result
== wxDragLink
)
262 action
= GDK_ACTION_LINK
;
264 action
= GDK_ACTION_MOVE
;
266 gdk_drag_status( context
, action
, time
);
269 /* after this, invalidate the drop_target's GdkDragContext */
270 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
272 /* this has to be done because GDK has no "drag_enter" event */
273 drop_target
->m_firstMotion
= false;
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
284 static gboolean
target_drag_drop( GtkWidget
*widget
,
285 GdkDragContext
*context
,
289 wxDropTarget
*drop_target
)
291 /* Owen Taylor: "if the drop is not in a drop zone,
292 return FALSE, otherwise, if you aren't accepting
293 the drop, call gtk_drag_finish() with success == FALSE
294 otherwise call gtk_drag_data_get()" */
296 // printf( "drop.\n" );
298 /* this seems to make a difference between not accepting
299 due to wrong target area and due to wrong format. let
300 us hope that this is not required.. */
302 /* inform the wxDropTarget about the current GdkDragContext.
303 this is only valid for the duration of this call */
304 drop_target
->SetDragContext( context
);
306 /* inform the wxDropTarget about the current drag widget.
307 this is only valid for the duration of this call */
308 drop_target
->SetDragWidget( widget
);
310 /* inform the wxDropTarget about the current drag time.
311 this is only valid for the duration of this call */
312 drop_target
->SetDragTime( time
);
315 wxDragResult result = wxDragMove;
316 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
319 /* reset the block here as someone might very well
320 show a dialog as a reaction to a drop and this
321 wouldn't work without events */
322 g_blockEventsOnDrag
= false;
324 bool ret
= drop_target
->OnDrop( x
, y
);
329 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
332 /* cancel the whole thing */
333 gtk_drag_finish( context
,
334 FALSE
, /* no success */
335 FALSE
, /* don't delete data on dropping side */
341 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
345 /* disable GUI threads */
348 GdkAtom format
= drop_target
->GetMatchingPair();
350 // this does happen somehow, see bug 555111
351 wxCHECK_MSG( format
, FALSE
, _T("no matching GdkAtom for format?") );
354 GdkDragAction action = GDK_ACTION_MOVE;
355 if (result == wxDragCopy) action == GDK_ACTION_COPY;
356 context->action = action;
358 /* this should trigger an "drag_data_received" event */
359 gtk_drag_get_data( widget
,
365 /* re-enable GUI threads */
369 /* after this, invalidate the drop_target's GdkDragContext */
370 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
372 /* after this, invalidate the drop_target's drag widget */
373 drop_target
->SetDragWidget( (GtkWidget
*) NULL
);
375 /* this has to be done because GDK has no "drag_enter" event */
376 drop_target
->m_firstMotion
= true;
382 // ----------------------------------------------------------------------------
383 // "drag_data_received"
384 // ----------------------------------------------------------------------------
387 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
388 GdkDragContext
*context
,
391 GtkSelectionData
*data
,
392 guint
WXUNUSED(info
),
394 wxDropTarget
*drop_target
)
396 /* Owen Taylor: "call gtk_drag_finish() with
399 if ((data
->length
<= 0) || (data
->format
!= 8))
401 /* negative data length and non 8-bit data format
402 qualifies for junk */
403 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
409 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
412 /* inform the wxDropTarget about the current GtkSelectionData.
413 this is only valid for the duration of this call */
414 drop_target
->SetDragData( data
);
416 wxDragResult result
= ConvertFromGTK(context
->action
);
418 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
421 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
424 /* tell GTK that data transfer was successful */
425 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
430 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
433 /* tell GTK that data transfer was not successful */
434 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
437 /* after this, invalidate the drop_target's drag data */
438 drop_target
->SetDragData( (GtkSelectionData
*) NULL
);
442 //----------------------------------------------------------------------------
444 //----------------------------------------------------------------------------
446 wxDropTarget::wxDropTarget( wxDataObject
*data
)
447 : wxDropTargetBase( data
)
449 m_firstMotion
= true;
450 m_dragContext
= (GdkDragContext
*) NULL
;
451 m_dragWidget
= (GtkWidget
*) NULL
;
452 m_dragData
= (GtkSelectionData
*) NULL
;
456 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
460 // GetMatchingPair() checks for m_dataObject too, no need to do it here
462 // disable the debug message from GetMatchingPair() - there are too many
468 return (GetMatchingPair() != (GdkAtom
) 0) ? def
: wxDragNone
;
471 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
476 return (GetMatchingPair() != (GdkAtom
) 0);
479 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
485 if (GetMatchingPair() == (GdkAtom
) 0)
488 return GetData() ? def
: wxDragNone
;
491 GdkAtom
wxDropTarget::GetMatchingPair()
499 GList
*child
= m_dragContext
->targets
;
502 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
503 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::UnregisterWidget( 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::RegisterWidget( 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 (GtkTargetEntry
*) 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
);
602 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
603 format
.GetId().c_str());
606 drop_source
->m_retValue
= wxDragCancel
;
608 wxDataObject
*data
= drop_source
->GetDataObject();
613 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
618 if (!data
->IsSupportedFormat(format
))
621 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
626 if (data
->GetDataSize(format
) == 0)
629 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
634 size_t size
= data
->GetDataSize(format
);
636 // printf( "data size: %d.\n", (int)data_size );
638 guchar
*d
= new guchar
[size
];
640 if (!data
->GetDataHere( format
, (void*)d
))
647 /* disable GUI threads */
650 gtk_selection_data_set( selection_data
,
651 selection_data
->target
,
657 /* enable GUI threads */
664 //----------------------------------------------------------------------------
666 //----------------------------------------------------------------------------
669 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
670 GdkDragContext
*WXUNUSED(context
),
671 wxDropSource
*drop_source
)
673 // printf( "Drag source: drag_end.\n" );
675 drop_source
->m_waiting
= false;
679 //-----------------------------------------------------------------------------
680 // "configure_event" from m_iconWindow
681 //-----------------------------------------------------------------------------
685 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
687 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
693 //---------------------------------------------------------------------------
695 //---------------------------------------------------------------------------
697 wxDropSource::wxDropSource(wxWindow
*win
,
698 const wxIcon
&iconCopy
,
699 const wxIcon
&iconMove
,
700 const wxIcon
&iconNone
)
704 m_iconWindow
= (GtkWidget
*) NULL
;
707 m_widget
= win
->m_widget
;
708 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
710 m_retValue
= wxDragCancel
;
712 SetIcons(iconCopy
, iconMove
, iconNone
);
715 wxDropSource::wxDropSource(wxDataObject
& data
,
717 const wxIcon
&iconCopy
,
718 const wxIcon
&iconMove
,
719 const wxIcon
&iconNone
)
725 m_iconWindow
= (GtkWidget
*) NULL
;
728 m_widget
= win
->m_widget
;
729 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
731 m_retValue
= wxDragCancel
;
733 SetIcons(iconCopy
, iconMove
, iconNone
);
736 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
737 const wxIcon
&iconMove
,
738 const wxIcon
&iconNone
)
740 m_iconCopy
= iconCopy
;
741 m_iconMove
= iconMove
;
742 m_iconNone
= iconNone
;
744 if ( !m_iconCopy
.Ok() )
745 m_iconCopy
= wxIcon(page_xpm
);
746 if ( !m_iconMove
.Ok() )
747 m_iconMove
= m_iconCopy
;
748 if ( !m_iconNone
.Ok() )
749 m_iconNone
= m_iconCopy
;
752 wxDropSource::~wxDropSource()
756 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
758 // get the right icon to display
760 if ( action
& GDK_ACTION_MOVE
)
762 else if ( action
& GDK_ACTION_COPY
)
768 if ( icon
->GetMask() )
769 mask
= icon
->GetMask()->GetBitmap();
771 mask
= (GdkBitmap
*)NULL
;
773 GdkPixmap
*pixmap
= icon
->GetPixmap();
776 gdk_drawable_get_size (pixmap
, &width
, &height
);
778 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
779 gtk_widget_push_colormap (colormap
);
781 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
782 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
783 gtk_widget_set_app_paintable (m_iconWindow
, TRUE
);
785 gtk_widget_pop_colormap ();
787 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
788 gtk_widget_realize (m_iconWindow
);
790 g_signal_connect (m_iconWindow
, "configure_event",
791 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
793 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
796 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
798 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
801 wxDragResult
wxDropSource::DoDragDrop(int flags
)
803 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
804 wxT("Drop source: no data") );
807 if (g_blockEventsOnDrag
)
810 // don't start dragging if no button is down
811 if (g_lastButtonNumber
== 0)
814 // we can only start a drag after a mouse event
815 if (g_lastMouseEvent
== NULL
)
818 GTKConnectDragSignals();
819 wxON_BLOCK_EXIT_OBJ0(*this, wxDropSource::GTKDisconnectDragSignals
);
823 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
825 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
826 m_data
->GetAllFormats( array
);
827 size_t count
= m_data
->GetFormatCount();
828 for (size_t i
= 0; i
< count
; i
++)
830 GdkAtom atom
= array
[i
];
832 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom
));
834 gtk_target_list_add( target_list
, atom
, 0, 0 );
838 int action
= GDK_ACTION_COPY
;
839 if ( flags
& wxDrag_AllowMove
)
840 action
|= GDK_ACTION_MOVE
;
842 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
843 // to use a global to pass the flags to the drop target but I'd
844 // surely prefer a better way to do it
845 gs_flagsForDrag
= flags
;
847 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
849 (GdkDragAction
)action
,
850 g_lastButtonNumber
, // number of mouse button which started drag
851 (GdkEvent
*) g_lastMouseEvent
);
855 // this can happen e.g. if gdk_pointer_grab() failed
859 m_dragContext
= context
;
861 PrepareIcon( action
, context
);
864 gtk_main_iteration();
866 m_retValue
= ConvertFromGTK(context
->action
);
867 if ( m_retValue
== wxDragNone
)
868 m_retValue
= wxDragCancel
;
873 void wxDropSource::GTKConnectDragSignals()
878 g_blockEventsOnDrag
= true;
880 g_signal_connect (m_widget
, "drag_data_get",
881 G_CALLBACK (source_drag_data_get
), this);
882 g_signal_connect (m_widget
, "drag_end",
883 G_CALLBACK (source_drag_end
), this);
887 void wxDropSource::GTKDisconnectDragSignals()
892 g_blockEventsOnDrag
= false;
894 g_signal_handlers_disconnect_by_func (m_widget
,
895 (gpointer
) source_drag_data_get
,
897 g_signal_handlers_disconnect_by_func (m_widget
,
898 (gpointer
) source_drag_end
,
903 // wxUSE_DRAG_AND_DROP