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"
29 #include "wx/gtk/private/gtk2-compat.h"
31 //----------------------------------------------------------------------------
33 //----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 // the flags used for the last DoDragDrop()
38 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 #define TRACE_DND "dnd"
45 // global variables because GTK+ DnD want to have the
46 // mouse event that caused it
47 extern GdkEvent
*g_lastMouseEvent
;
48 extern int g_lastButtonNumber
;
50 //----------------------------------------------------------------------------
52 //----------------------------------------------------------------------------
54 /* Copyright (c) Julian Smart */
55 static const char * page_xpm
[] = {
56 /* columns rows colors chars-per-pixel */
105 " 56477<<<<8<<9&:X ",
106 " 59642%&*=-;<09&:5 ",
107 " 5q9642%&*=-<<<<<# ",
108 " 5qqw777<<<<<88:>+ ",
109 " erqq9642%&*=t;::+ ",
110 " eyrqq9642%&*=t;:O ",
111 " eyywwww777<<<<t;O ",
112 " e0yyrqq9642%&*=to ",
113 " e00yyrqq9642%&*=o ",
114 " eu0wwwwwww777<&*X ",
115 " euu00yyrqq9642%&X ",
116 " eiuu00yyrqq9642%X ",
117 " eiiwwwwwwwwww742$ ",
118 " eiiiuu00yyrqq964$ ",
119 " eiiiiuu00yyrqq96$ ",
120 " eiiiiiuu00yyrqq95 ",
121 " eiiiiiiuu00yyrqq5 ",
122 " eeeeeeeeeeeeee55e ",
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
136 // convert between GTK+ and wxWidgets DND constants
137 // ----------------------------------------------------------------------------
139 static wxDragResult
ConvertFromGTK(long action
)
143 case GDK_ACTION_COPY
:
146 case GDK_ACTION_LINK
:
149 case GDK_ACTION_MOVE
:
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
161 static void target_drag_leave( GtkWidget
*WXUNUSED(widget
),
162 GdkDragContext
*context
,
163 guint
WXUNUSED(time
),
164 wxDropTarget
*drop_target
)
166 /* inform the wxDropTarget about the current GdkDragContext.
167 this is only valid for the duration of this call */
168 drop_target
->GTKSetDragContext( context
);
170 /* we don't need return values. this event is just for
172 drop_target
->OnLeave();
174 /* this has to be done because GDK has no "drag_enter" event */
175 drop_target
->m_firstMotion
= true;
177 /* after this, invalidate the drop_target's GdkDragContext */
178 drop_target
->GTKSetDragContext( NULL
);
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
187 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
188 GdkDragContext
*context
,
192 wxDropTarget
*drop_target
)
194 /* Owen Taylor: "if the coordinates not in a drop zone,
195 return FALSE, otherwise call gtk_drag_status() and
199 wxPrintf( "motion\n" );
201 for (tmp_list
= context
->targets
; tmp_list
; tmp_list
= tmp_list
->next
)
203 wxString atom
= wxString::FromAscii( gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list
->data
)) );
204 wxPrintf( "Atom: %s\n", atom
);
208 // Inform the wxDropTarget about the current GdkDragContext.
209 // This is only valid for the duration of this call.
210 drop_target
->GTKSetDragContext( context
);
212 // Does the source actually accept the data type?
213 if (drop_target
->GTKGetMatchingPair() == (GdkAtom
) 0)
215 drop_target
->GTKSetDragContext( NULL
);
219 wxDragResult suggested_action
= drop_target
->GTKFigureOutSuggestedAction();
221 wxDragResult result
= wxDragNone
;
223 if (drop_target
->m_firstMotion
)
225 // the first "drag_motion" event substitutes a "drag_enter" event
226 result
= drop_target
->OnEnter( x
, y
, suggested_action
);
230 // give program a chance to react (i.e. to say no by returning FALSE)
231 result
= drop_target
->OnDragOver( x
, y
, suggested_action
);
234 GdkDragAction result_action
= GDK_ACTION_DEFAULT
;
235 if (result
== wxDragCopy
)
236 result_action
= GDK_ACTION_COPY
;
237 else if (result
== wxDragLink
)
238 result_action
= GDK_ACTION_LINK
;
240 result_action
= GDK_ACTION_MOVE
;
242 // is result action actually supported
243 bool ret ((result_action
!= GDK_ACTION_DEFAULT
) &&
244 (context
->actions
& result_action
));
247 gdk_drag_status( context
, result_action
, time
);
249 // after this, invalidate the drop_target's GdkDragContext
250 drop_target
->GTKSetDragContext( NULL
);
252 // this has to be done because GDK has no "drag_enter" event
253 drop_target
->m_firstMotion
= false;
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
264 static gboolean
target_drag_drop( GtkWidget
*widget
,
265 GdkDragContext
*context
,
269 wxDropTarget
*drop_target
)
271 /* Owen Taylor: "if the drop is not in a drop zone,
272 return FALSE, otherwise, if you aren't accepting
273 the drop, call gtk_drag_finish() with success == FALSE
274 otherwise call gtk_drag_data_get()" */
276 /* inform the wxDropTarget about the current GdkDragContext.
277 this is only valid for the duration of this call */
278 drop_target
->GTKSetDragContext( context
);
280 // Does the source actually accept the data type?
281 if (drop_target
->GTKGetMatchingPair() == (GdkAtom
) 0)
283 // cancel the whole thing
284 gtk_drag_finish( context
,
286 FALSE
, // don't delete data on dropping side
289 drop_target
->GTKSetDragContext( NULL
);
291 drop_target
->m_firstMotion
= true;
296 /* inform the wxDropTarget about the current drag widget.
297 this is only valid for the duration of this call */
298 drop_target
->GTKSetDragWidget( widget
);
300 /* inform the wxDropTarget about the current drag time.
301 this is only valid for the duration of this call */
302 drop_target
->GTKSetDragTime( time
);
304 /* reset the block here as someone might very well
305 show a dialog as a reaction to a drop and this
306 wouldn't work without events */
307 g_blockEventsOnDrag
= false;
309 bool ret
= drop_target
->OnDrop( x
, y
);
313 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
315 /* cancel the whole thing */
316 gtk_drag_finish( context
,
317 FALSE
, /* no success */
318 FALSE
, /* don't delete data on dropping side */
323 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
325 GdkAtom format
= drop_target
->GTKGetMatchingPair();
327 // this does happen somehow, see bug 555111
328 wxCHECK_MSG( format
, FALSE
, wxT("no matching GdkAtom for format?") );
330 /* this should trigger an "drag_data_received" event */
331 gtk_drag_get_data( widget
,
337 /* after this, invalidate the drop_target's GdkDragContext */
338 drop_target
->GTKSetDragContext( NULL
);
340 /* after this, invalidate the drop_target's drag widget */
341 drop_target
->GTKSetDragWidget( NULL
);
343 /* this has to be done because GDK has no "drag_enter" event */
344 drop_target
->m_firstMotion
= true;
350 // ----------------------------------------------------------------------------
351 // "drag_data_received"
352 // ----------------------------------------------------------------------------
355 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
356 GdkDragContext
*context
,
359 GtkSelectionData
*data
,
360 guint
WXUNUSED(info
),
362 wxDropTarget
*drop_target
)
364 /* Owen Taylor: "call gtk_drag_finish() with
367 if (gtk_selection_data_get_length(data
) <= 0 || gtk_selection_data_get_format(data
) != 8)
369 /* negative data length and non 8-bit data format
370 qualifies for junk */
371 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
376 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
378 /* inform the wxDropTarget about the current GtkSelectionData.
379 this is only valid for the duration of this call */
380 drop_target
->GTKSetDragData( data
);
382 wxDragResult result
= ConvertFromGTK(context
->action
);
384 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
386 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
388 /* tell GTK that data transfer was successful */
389 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
393 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
395 /* tell GTK that data transfer was not successful */
396 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
399 /* after this, invalidate the drop_target's drag data */
400 drop_target
->GTKSetDragData( NULL
);
404 //----------------------------------------------------------------------------
406 //----------------------------------------------------------------------------
408 wxDropTarget::wxDropTarget( wxDataObject
*data
)
409 : wxDropTargetBase( data
)
411 m_firstMotion
= true;
412 m_dragContext
= NULL
;
418 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
425 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
430 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
433 return GetData() ? def
: wxDragNone
;
436 wxDragResult
wxDropTarget::GTKFigureOutSuggestedAction()
441 // GTK+ always supposes that we want to copy the data by default while we
442 // might want to move it, so examine not only suggested_action - which is
443 // only good if we don't have our own preferences - but also the actions
445 wxDragResult suggested_action
= wxDragNone
;
446 if (GetDefaultAction() == wxDragNone
)
448 // use default action set by wxDropSource::DoDragDrop()
449 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
450 (m_dragContext
->actions
& GDK_ACTION_MOVE
) )
452 // move is requested by the program and allowed by GTK+ - do it, even
453 // though suggested_action may be currently wxDragCopy
454 suggested_action
= wxDragMove
;
456 else // use whatever GTK+ says we should
458 suggested_action
= ConvertFromGTK(m_dragContext
->suggested_action
);
461 // RR: I don't understand the code below: if the drag comes from
462 // a different app, the gs_flagsForDrag is invalid; if it
463 // comes from the same wx app, then GTK+ hopefully won't
464 // suggest something we didn't allow in the frist place
466 if ( (suggested_action
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
468 // we're requested to move but we can't
469 suggested_action
= wxDragCopy
;
474 else if (GetDefaultAction() == wxDragMove
&&
475 (m_dragContext
->actions
& GDK_ACTION_MOVE
))
478 suggested_action
= wxDragMove
;
482 if (m_dragContext
->actions
& GDK_ACTION_COPY
)
483 suggested_action
= wxDragCopy
;
484 else if (m_dragContext
->actions
& GDK_ACTION_MOVE
)
485 suggested_action
= wxDragMove
;
486 else if (m_dragContext
->actions
& GDK_ACTION_LINK
)
487 suggested_action
= wxDragLink
;
489 suggested_action
= wxDragNone
;
492 return suggested_action
;
495 wxDataFormat
wxDropTarget::GetMatchingPair()
497 return wxDataFormat( GTKGetMatchingPair() );
500 GdkAtom
wxDropTarget::GTKGetMatchingPair(bool quiet
)
508 GList
*child
= m_dragContext
->targets
;
511 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
512 wxDataFormat
format( formatAtom
);
516 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
517 format
.GetId().c_str());
520 if (m_dataObject
->IsSupportedFormat( format
))
529 bool wxDropTarget::GetData()
537 wxDataFormat
dragFormat(gtk_selection_data_get_target(m_dragData
));
539 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
542 m_dataObject
->SetData(dragFormat
,
543 (size_t)gtk_selection_data_get_length(m_dragData
),
544 (const void*)gtk_selection_data_get_data(m_dragData
));
549 void wxDropTarget::GtkUnregisterWidget( GtkWidget
*widget
)
551 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
553 gtk_drag_dest_unset( widget
);
555 g_signal_handlers_disconnect_by_func (widget
,
556 (gpointer
) target_drag_leave
, this);
557 g_signal_handlers_disconnect_by_func (widget
,
558 (gpointer
) target_drag_motion
, this);
559 g_signal_handlers_disconnect_by_func (widget
,
560 (gpointer
) target_drag_drop
, this);
561 g_signal_handlers_disconnect_by_func (widget
,
562 (gpointer
) target_drag_data_received
, this);
565 void wxDropTarget::GtkRegisterWidget( GtkWidget
*widget
)
567 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
569 /* gtk_drag_dest_set() determines what default behaviour we'd like
570 GTK to supply. we don't want to specify out targets (=formats)
571 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
572 not GTK_DEST_DEFAULT_DROP). instead we react individually to
573 "drag_motion" and "drag_drop" events. this makes it possible
574 to allow dropping on only a small area. we should set
575 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
576 highlighting if dragging over standard controls, but this
577 seems to be broken without the other two. */
579 gtk_drag_dest_set( widget
,
580 (GtkDestDefaults
) 0, /* no default behaviour */
581 NULL
, /* we don't supply any formats here */
582 0, /* number of targets = 0 */
583 (GdkDragAction
) 0 ); /* we don't supply any actions here */
585 g_signal_connect (widget
, "drag_leave",
586 G_CALLBACK (target_drag_leave
), this);
588 g_signal_connect (widget
, "drag_motion",
589 G_CALLBACK (target_drag_motion
), this);
591 g_signal_connect (widget
, "drag_drop",
592 G_CALLBACK (target_drag_drop
), this);
594 g_signal_connect (widget
, "drag_data_received",
595 G_CALLBACK (target_drag_data_received
), this);
598 //----------------------------------------------------------------------------
600 //----------------------------------------------------------------------------
604 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
605 GdkDragContext
*context
,
606 GtkSelectionData
*selection_data
,
607 guint
WXUNUSED(info
),
608 guint
WXUNUSED(time
),
609 wxDropSource
*drop_source
)
611 wxDataFormat
format(gtk_selection_data_get_target(selection_data
));
613 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
614 format
.GetId().c_str());
616 drop_source
->m_retValue
= wxDragError
;
618 wxDataObject
*data
= drop_source
->GetDataObject();
622 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
626 if (!data
->IsSupportedFormat(format
))
628 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
632 if (data
->GetDataSize(format
) == 0)
634 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
638 size_t size
= data
->GetDataSize(format
);
640 // printf( "data size: %d.\n", (int)data_size );
642 guchar
*d
= new guchar
[size
];
644 if (!data
->GetDataHere( format
, (void*)d
))
650 drop_source
->m_retValue
= ConvertFromGTK( context
->action
);
652 gtk_selection_data_set( selection_data
,
653 gtk_selection_data_get_target(selection_data
),
662 //----------------------------------------------------------------------------
664 //----------------------------------------------------------------------------
667 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
668 GdkDragContext
*WXUNUSED(context
),
669 wxDropSource
*drop_source
)
671 drop_source
->m_waiting
= false;
675 //-----------------------------------------------------------------------------
676 // "configure_event" from m_iconWindow
677 //-----------------------------------------------------------------------------
681 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
683 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
689 //---------------------------------------------------------------------------
691 //---------------------------------------------------------------------------
693 wxDropSource::wxDropSource(wxWindow
*win
,
694 const wxIcon
&iconCopy
,
695 const wxIcon
&iconMove
,
696 const wxIcon
&iconNone
)
703 m_widget
= win
->m_widget
;
704 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
706 m_retValue
= wxDragNone
;
708 SetIcons(iconCopy
, iconMove
, iconNone
);
711 wxDropSource::wxDropSource(wxDataObject
& data
,
713 const wxIcon
&iconCopy
,
714 const wxIcon
&iconMove
,
715 const wxIcon
&iconNone
)
724 m_widget
= win
->m_widget
;
725 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
727 m_retValue
= wxDragNone
;
729 SetIcons(iconCopy
, iconMove
, iconNone
);
732 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
733 const wxIcon
&iconMove
,
734 const wxIcon
&iconNone
)
736 m_iconCopy
= iconCopy
;
737 m_iconMove
= iconMove
;
738 m_iconNone
= iconNone
;
740 if ( !m_iconCopy
.Ok() )
741 m_iconCopy
= wxIcon(page_xpm
);
742 if ( !m_iconMove
.Ok() )
743 m_iconMove
= m_iconCopy
;
744 if ( !m_iconNone
.Ok() )
745 m_iconNone
= m_iconCopy
;
748 wxDropSource::~wxDropSource()
752 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
754 // get the right icon to display
756 if ( action
& GDK_ACTION_MOVE
)
758 else if ( action
& GDK_ACTION_COPY
)
764 if ( icon
->GetMask() )
765 mask
= icon
->GetMask()->GetBitmap();
769 GdkPixmap
*pixmap
= icon
->GetPixmap();
772 gdk_drawable_get_size (pixmap
, &width
, &height
);
774 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
775 gtk_widget_push_colormap (colormap
);
777 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
778 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
779 gtk_widget_set_app_paintable (m_iconWindow
, TRUE
);
781 gtk_widget_pop_colormap ();
783 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
784 gtk_widget_realize (m_iconWindow
);
786 g_signal_connect (m_iconWindow
, "configure_event",
787 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
789 gdk_window_set_back_pixmap(gtk_widget_get_window(m_iconWindow
), pixmap
, false);
792 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
794 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
797 wxDragResult
wxDropSource::DoDragDrop(int flags
)
799 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
800 wxT("Drop source: no data") );
803 if (g_blockEventsOnDrag
)
806 // don't start dragging if no button is down
807 if (g_lastButtonNumber
== 0)
810 // we can only start a drag after a mouse event
811 if (g_lastMouseEvent
== NULL
)
814 GTKConnectDragSignals();
815 wxON_BLOCK_EXIT_OBJ0(*this, wxDropSource::GTKDisconnectDragSignals
);
819 GtkTargetList
*target_list
= gtk_target_list_new( NULL
, 0 );
821 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
822 m_data
->GetAllFormats( array
);
823 size_t count
= m_data
->GetFormatCount();
824 for (size_t i
= 0; i
< count
; i
++)
826 GdkAtom atom
= array
[i
];
827 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"),
828 gdk_atom_name( atom
));
829 gtk_target_list_add( target_list
, atom
, 0, 0 );
833 int allowed_actions
= GDK_ACTION_COPY
;
834 if ( flags
& wxDrag_AllowMove
)
835 allowed_actions
|= GDK_ACTION_MOVE
;
837 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
838 // to use a global to pass the flags to the drop target but I'd
839 // surely prefer a better way to do it
840 gs_flagsForDrag
= flags
;
842 m_retValue
= wxDragCancel
;
844 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
846 (GdkDragAction
)allowed_actions
,
847 g_lastButtonNumber
, // number of mouse button which started drag
848 (GdkEvent
*) g_lastMouseEvent
);
852 // this can happen e.g. if gdk_pointer_grab() failed
856 m_dragContext
= context
;
858 PrepareIcon( allowed_actions
, context
);
861 gtk_main_iteration();
863 g_signal_handlers_disconnect_by_func (m_iconWindow
,
864 (gpointer
) gtk_dnd_window_configure_callback
, this);
869 void wxDropSource::GTKConnectDragSignals()
874 g_blockEventsOnDrag
= true;
876 g_signal_connect (m_widget
, "drag_data_get",
877 G_CALLBACK (source_drag_data_get
), this);
878 g_signal_connect (m_widget
, "drag_end",
879 G_CALLBACK (source_drag_end
), this);
883 void wxDropSource::GTKDisconnectDragSignals()
888 g_blockEventsOnDrag
= false;
890 g_signal_handlers_disconnect_by_func (m_widget
,
891 (gpointer
) source_drag_data_get
,
893 g_signal_handlers_disconnect_by_func (m_widget
,
894 (gpointer
) source_drag_end
,
899 // wxUSE_DRAG_AND_DROP