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"
28 //----------------------------------------------------------------------------
30 //----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
34 // the flags used for the last DoDragDrop()
35 static long gs_flagsForDrag
= 0;
38 // the trace mask we use with wxLogTrace() - call
39 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
40 // (there are quite a few of them, so don't enable this by default)
41 static const wxChar
*TRACE_DND
= _T("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
->SetDragContext( 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
->SetDragContext( (GdkDragContext
*) 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
197 /* inform the wxDropTarget about the current GdkDragContext.
198 this is only valid for the duration of this call */
199 drop_target
->SetDragContext( context
);
201 // GTK+ always supposes that we want to copy the data by default while we
202 // might want to move it, so examine not only suggested_action - which is
203 // only good if we don't have our own preferences - but also the actions
206 if (drop_target
->GetDefaultAction() == wxDragNone
)
208 // use default action set by wxDropSource::DoDragDrop()
209 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
210 (context
->actions
& GDK_ACTION_MOVE
) )
212 // move is requested by the program and allowed by GTK+ - do it, even
213 // though suggested_action may be currently wxDragCopy
216 else // use whatever GTK+ says we should
218 result
= ConvertFromGTK(context
->suggested_action
);
220 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
222 // we're requested to move but we can't
227 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
228 (context
->actions
& GDK_ACTION_MOVE
))
234 if (context
->actions
& GDK_ACTION_COPY
)
236 else if (context
->actions
& GDK_ACTION_MOVE
)
242 if (drop_target
->m_firstMotion
)
244 /* the first "drag_motion" event substitutes a "drag_enter" event */
245 result
= drop_target
->OnEnter( x
, y
, result
);
249 /* give program a chance to react (i.e. to say no by returning FALSE) */
250 result
= drop_target
->OnDragOver( x
, y
, result
);
253 bool ret
= wxIsDragResultOk( result
);
256 GdkDragAction action
;
257 if (result
== wxDragCopy
)
258 action
= GDK_ACTION_COPY
;
259 else if (result
== wxDragLink
)
260 action
= GDK_ACTION_LINK
;
262 action
= GDK_ACTION_MOVE
;
264 gdk_drag_status( context
, action
, time
);
267 /* after this, invalidate the drop_target's GdkDragContext */
268 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
270 /* this has to be done because GDK has no "drag_enter" event */
271 drop_target
->m_firstMotion
= false;
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
282 static gboolean
target_drag_drop( GtkWidget
*widget
,
283 GdkDragContext
*context
,
287 wxDropTarget
*drop_target
)
289 /* Owen Taylor: "if the drop is not in a drop zone,
290 return FALSE, otherwise, if you aren't accepting
291 the drop, call gtk_drag_finish() with success == FALSE
292 otherwise call gtk_drag_data_get()" */
294 // printf( "drop.\n" );
296 /* this seems to make a difference between not accepting
297 due to wrong target area and due to wrong format. let
298 us hope that this is not required.. */
300 /* inform the wxDropTarget about the current GdkDragContext.
301 this is only valid for the duration of this call */
302 drop_target
->SetDragContext( context
);
304 /* inform the wxDropTarget about the current drag widget.
305 this is only valid for the duration of this call */
306 drop_target
->SetDragWidget( widget
);
308 /* inform the wxDropTarget about the current drag time.
309 this is only valid for the duration of this call */
310 drop_target
->SetDragTime( time
);
313 wxDragResult result = wxDragMove;
314 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
317 /* reset the block here as someone might very well
318 show a dialog as a reaction to a drop and this
319 wouldn't work without events */
320 g_blockEventsOnDrag
= false;
322 bool ret
= drop_target
->OnDrop( x
, y
);
327 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
330 /* cancel the whole thing */
331 gtk_drag_finish( context
,
332 FALSE
, /* no success */
333 FALSE
, /* don't delete data on dropping side */
339 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
343 /* disable GUI threads */
346 GdkAtom format
= drop_target
->GetMatchingPair();
348 // this does happen somehow, see bug 555111
349 wxCHECK_MSG( format
, FALSE
, _T("no matching GdkAtom for format?") );
352 GdkDragAction action = GDK_ACTION_MOVE;
353 if (result == wxDragCopy) action == GDK_ACTION_COPY;
354 context->action = action;
356 /* this should trigger an "drag_data_received" event */
357 gtk_drag_get_data( widget
,
363 /* re-enable GUI threads */
367 /* after this, invalidate the drop_target's GdkDragContext */
368 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
370 /* after this, invalidate the drop_target's drag widget */
371 drop_target
->SetDragWidget( (GtkWidget
*) NULL
);
373 /* this has to be done because GDK has no "drag_enter" event */
374 drop_target
->m_firstMotion
= true;
380 // ----------------------------------------------------------------------------
381 // "drag_data_received"
382 // ----------------------------------------------------------------------------
385 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
386 GdkDragContext
*context
,
389 GtkSelectionData
*data
,
390 guint
WXUNUSED(info
),
392 wxDropTarget
*drop_target
)
394 /* Owen Taylor: "call gtk_drag_finish() with
397 if ((data
->length
<= 0) || (data
->format
!= 8))
399 /* negative data length and non 8-bit data format
400 qualifies for junk */
401 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
407 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
410 /* inform the wxDropTarget about the current GtkSelectionData.
411 this is only valid for the duration of this call */
412 drop_target
->SetDragData( data
);
414 wxDragResult result
= ConvertFromGTK(context
->action
);
416 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
419 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
422 /* tell GTK that data transfer was successful */
423 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
428 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
431 /* tell GTK that data transfer was not successful */
432 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
435 /* after this, invalidate the drop_target's drag data */
436 drop_target
->SetDragData( (GtkSelectionData
*) NULL
);
440 //----------------------------------------------------------------------------
442 //----------------------------------------------------------------------------
444 wxDropTarget::wxDropTarget( wxDataObject
*data
)
445 : wxDropTargetBase( data
)
447 m_firstMotion
= true;
448 m_dragContext
= (GdkDragContext
*) NULL
;
449 m_dragWidget
= (GtkWidget
*) NULL
;
450 m_dragData
= (GtkSelectionData
*) NULL
;
454 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
458 // GetMatchingPair() checks for m_dataObject too, no need to do it here
460 // disable the debug message from GetMatchingPair() - there are too many
466 return (GetMatchingPair() != (GdkAtom
) 0) ? def
: wxDragNone
;
469 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
474 return (GetMatchingPair() != (GdkAtom
) 0);
477 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
483 if (GetMatchingPair() == (GdkAtom
) 0)
486 return GetData() ? def
: wxDragNone
;
489 GdkAtom
wxDropTarget::GetMatchingPair()
497 GList
*child
= m_dragContext
->targets
;
500 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
501 wxDataFormat
format( formatAtom
);
504 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
505 format
.GetId().c_str());
508 if (m_dataObject
->IsSupportedFormat( format
))
517 bool wxDropTarget::GetData()
525 wxDataFormat
dragFormat( m_dragData
->target
);
527 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
530 m_dataObject
->SetData( dragFormat
, (size_t)m_dragData
->length
, (const void*)m_dragData
->data
);
535 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
537 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
539 gtk_drag_dest_unset( widget
);
541 g_signal_handlers_disconnect_by_func (widget
,
542 (gpointer
) target_drag_leave
, this);
543 g_signal_handlers_disconnect_by_func (widget
,
544 (gpointer
) target_drag_motion
, this);
545 g_signal_handlers_disconnect_by_func (widget
,
546 (gpointer
) target_drag_drop
, this);
547 g_signal_handlers_disconnect_by_func (widget
,
548 (gpointer
) target_drag_data_received
, this);
551 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
553 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
555 /* gtk_drag_dest_set() determines what default behaviour we'd like
556 GTK to supply. we don't want to specify out targets (=formats)
557 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
558 not GTK_DEST_DEFAULT_DROP). instead we react individually to
559 "drag_motion" and "drag_drop" events. this makes it possible
560 to allow dropping on only a small area. we should set
561 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
562 highlighting if dragging over standard controls, but this
563 seems to be broken without the other two. */
565 gtk_drag_dest_set( widget
,
566 (GtkDestDefaults
) 0, /* no default behaviour */
567 (GtkTargetEntry
*) NULL
, /* we don't supply any formats here */
568 0, /* number of targets = 0 */
569 (GdkDragAction
) 0 ); /* we don't supply any actions here */
571 g_signal_connect (widget
, "drag_leave",
572 G_CALLBACK (target_drag_leave
), this);
574 g_signal_connect (widget
, "drag_motion",
575 G_CALLBACK (target_drag_motion
), this);
577 g_signal_connect (widget
, "drag_drop",
578 G_CALLBACK (target_drag_drop
), this);
580 g_signal_connect (widget
, "drag_data_received",
581 G_CALLBACK (target_drag_data_received
), this);
584 //----------------------------------------------------------------------------
586 //----------------------------------------------------------------------------
590 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
591 GdkDragContext
*WXUNUSED(context
),
592 GtkSelectionData
*selection_data
,
593 guint
WXUNUSED(info
),
594 guint
WXUNUSED(time
),
595 wxDropSource
*drop_source
)
597 wxDataFormat
format( selection_data
->target
);
600 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
601 format
.GetId().c_str());
604 drop_source
->m_retValue
= wxDragCancel
;
606 wxDataObject
*data
= drop_source
->GetDataObject();
611 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
616 if (!data
->IsSupportedFormat(format
))
619 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
624 if (data
->GetDataSize(format
) == 0)
627 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
632 size_t size
= data
->GetDataSize(format
);
634 // printf( "data size: %d.\n", (int)data_size );
636 guchar
*d
= new guchar
[size
];
638 if (!data
->GetDataHere( format
, (void*)d
))
645 /* disable GUI threads */
648 gtk_selection_data_set( selection_data
,
649 selection_data
->target
,
655 /* enable GUI threads */
662 //----------------------------------------------------------------------------
664 //----------------------------------------------------------------------------
667 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
668 GdkDragContext
*WXUNUSED(context
),
669 wxDropSource
*drop_source
)
671 // printf( "Drag source: drag_end.\n" );
673 drop_source
->m_waiting
= false;
677 //-----------------------------------------------------------------------------
678 // "configure_event" from m_iconWindow
679 //-----------------------------------------------------------------------------
683 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
685 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
691 //---------------------------------------------------------------------------
693 //---------------------------------------------------------------------------
695 wxDropSource::wxDropSource(wxWindow
*win
,
696 const wxIcon
&iconCopy
,
697 const wxIcon
&iconMove
,
698 const wxIcon
&iconNone
)
702 m_iconWindow
= (GtkWidget
*) NULL
;
705 m_widget
= win
->m_widget
;
706 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
708 m_retValue
= wxDragCancel
;
710 SetIcons(iconCopy
, iconMove
, iconNone
);
713 wxDropSource::wxDropSource(wxDataObject
& data
,
715 const wxIcon
&iconCopy
,
716 const wxIcon
&iconMove
,
717 const wxIcon
&iconNone
)
723 m_iconWindow
= (GtkWidget
*) NULL
;
726 m_widget
= win
->m_widget
;
727 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
729 m_retValue
= wxDragCancel
;
731 SetIcons(iconCopy
, iconMove
, iconNone
);
734 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
735 const wxIcon
&iconMove
,
736 const wxIcon
&iconNone
)
738 m_iconCopy
= iconCopy
;
739 m_iconMove
= iconMove
;
740 m_iconNone
= iconNone
;
742 if ( !m_iconCopy
.Ok() )
743 m_iconCopy
= wxIcon(page_xpm
);
744 if ( !m_iconMove
.Ok() )
745 m_iconMove
= m_iconCopy
;
746 if ( !m_iconNone
.Ok() )
747 m_iconNone
= m_iconCopy
;
750 wxDropSource::~wxDropSource()
754 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
756 // get the right icon to display
758 if ( action
& GDK_ACTION_MOVE
)
760 else if ( action
& GDK_ACTION_COPY
)
766 if ( icon
->GetMask() )
767 mask
= icon
->GetMask()->GetBitmap();
769 mask
= (GdkBitmap
*)NULL
;
771 GdkPixmap
*pixmap
= icon
->GetPixmap();
774 gdk_drawable_get_size (pixmap
, &width
, &height
);
776 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
777 gtk_widget_push_colormap (colormap
);
779 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
780 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
781 gtk_widget_set_app_paintable (m_iconWindow
, TRUE
);
783 gtk_widget_pop_colormap ();
785 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
786 gtk_widget_realize (m_iconWindow
);
788 g_signal_connect (m_iconWindow
, "configure_event",
789 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
791 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
794 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
796 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
799 wxDragResult
wxDropSource::DoDragDrop(int flags
)
801 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
802 wxT("Drop source: no data") );
805 if (g_blockEventsOnDrag
)
808 // don't start dragging if no button is down
809 if (g_lastButtonNumber
== 0)
812 // we can only start a drag after a mouse event
813 if (g_lastMouseEvent
== NULL
)
817 g_blockEventsOnDrag
= true;
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
);
853 m_dragContext
= context
;
855 PrepareIcon( action
, context
);
858 gtk_main_iteration();
860 m_retValue
= ConvertFromGTK(context
->action
);
861 if ( m_retValue
== wxDragNone
)
862 m_retValue
= wxDragCancel
;
864 g_blockEventsOnDrag
= false;
871 void wxDropSource::RegisterWindow()
873 if (!m_widget
) return;
875 g_signal_connect (m_widget
, "drag_data_get",
876 G_CALLBACK (source_drag_data_get
), this);
877 g_signal_connect (m_widget
, "drag_end",
878 G_CALLBACK (source_drag_end
), this);
882 void wxDropSource::UnregisterWindow()
887 g_signal_handlers_disconnect_by_func (m_widget
,
888 (gpointer
) source_drag_data_get
,
890 g_signal_handlers_disconnect_by_func (m_widget
,
891 (gpointer
) source_drag_end
,
896 // wxUSE_DRAG_AND_DROP