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"
25 #include "wx/gdicmn.h"
27 #include "wx/gtk/private.h"
29 #include <gdk/gdkprivate.h>
31 #include <gtk/gtkdnd.h>
32 #include <gtk/gtkselection.h>
34 //----------------------------------------------------------------------------
36 //----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 // the flags used for the last DoDragDrop()
41 static long gs_flagsForDrag
= 0;
43 // the trace mask we use with wxLogTrace() - call
44 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
45 // (there are quite a few of them, so don't enable this by default)
46 static const wxChar
*TRACE_DND
= _T("dnd");
48 //----------------------------------------------------------------------------
50 //----------------------------------------------------------------------------
52 /* Copyright (c) Julian Smart */
53 static const char * page_xpm
[] = {
54 /* columns rows colors chars-per-pixel */
103 " 56477<<<<8<<9&:X ",
104 " 59642%&*=-;<09&:5 ",
105 " 5q9642%&*=-<<<<<# ",
106 " 5qqw777<<<<<88:>+ ",
107 " erqq9642%&*=t;::+ ",
108 " eyrqq9642%&*=t;:O ",
109 " eyywwww777<<<<t;O ",
110 " e0yyrqq9642%&*=to ",
111 " e00yyrqq9642%&*=o ",
112 " eu0wwwwwww777<&*X ",
113 " euu00yyrqq9642%&X ",
114 " eiuu00yyrqq9642%X ",
115 " eiiwwwwwwwwww742$ ",
116 " eiiiuu00yyrqq964$ ",
117 " eiiiiuu00yyrqq96$ ",
118 " eiiiiiuu00yyrqq95 ",
119 " eiiiiiiuu00yyrqq5 ",
120 " eeeeeeeeeeeeee55e ",
129 // ============================================================================
131 // ============================================================================
133 // ----------------------------------------------------------------------------
134 // convert between GTK+ and wxWidgets DND constants
135 // ----------------------------------------------------------------------------
137 static wxDragResult
ConvertFromGTK(long action
)
141 case GDK_ACTION_COPY
:
144 case GDK_ACTION_LINK
:
147 case GDK_ACTION_MOVE
:
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
159 static void target_drag_leave( GtkWidget
*WXUNUSED(widget
),
160 GdkDragContext
*context
,
161 guint
WXUNUSED(time
),
162 wxDropTarget
*drop_target
)
164 if (g_isIdle
) wxapp_install_idle_handler();
166 /* inform the wxDropTarget about the current GdkDragContext.
167 this is only valid for the duration of this call */
168 drop_target
->SetDragContext( 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
->SetDragContext( (GdkDragContext
*) NULL
);
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
187 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
188 GdkDragContext
*context
,
192 wxDropTarget
*drop_target
)
194 if (g_isIdle
) wxapp_install_idle_handler();
196 /* Owen Taylor: "if the coordinates not in a drop zone,
197 return FALSE, otherwise call gtk_drag_status() and
200 /* inform the wxDropTarget about the current GdkDragContext.
201 this is only valid for the duration of this call */
202 drop_target
->SetDragContext( context
);
204 // GTK+ always supposes that we want to copy the data by default while we
205 // might want to move it, so examine not only suggested_action - which is
206 // only good if we don't have our own preferences - but also the actions
209 if (drop_target
->GetDefaultAction() == wxDragNone
)
211 // use default action set by wxDropSource::DoDragDrop()
212 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
213 (context
->actions
& GDK_ACTION_MOVE
) )
215 // move is requested by the program and allowed by GTK+ - do it, even
216 // though suggested_action may be currently wxDragCopy
219 else // use whatever GTK+ says we should
221 result
= ConvertFromGTK(context
->suggested_action
);
223 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
225 // we're requested to move but we can't
230 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
231 (context
->actions
& GDK_ACTION_MOVE
))
237 if (context
->actions
& GDK_ACTION_COPY
)
239 else if (context
->actions
& GDK_ACTION_MOVE
)
245 if (drop_target
->m_firstMotion
)
247 /* the first "drag_motion" event substitutes a "drag_enter" event */
248 result
= drop_target
->OnEnter( x
, y
, result
);
252 /* give program a chance to react (i.e. to say no by returning FALSE) */
253 result
= drop_target
->OnDragOver( x
, y
, result
);
256 bool ret
= wxIsDragResultOk( result
);
259 GdkDragAction action
;
260 if (result
== wxDragCopy
)
261 action
= GDK_ACTION_COPY
;
262 else if (result
== wxDragLink
)
263 action
= GDK_ACTION_LINK
;
265 action
= GDK_ACTION_MOVE
;
267 gdk_drag_status( context
, action
, time
);
270 /* after this, invalidate the drop_target's GdkDragContext */
271 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
273 /* this has to be done because GDK has no "drag_enter" event */
274 drop_target
->m_firstMotion
= false;
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
285 static gboolean
target_drag_drop( GtkWidget
*widget
,
286 GdkDragContext
*context
,
290 wxDropTarget
*drop_target
)
292 if (g_isIdle
) wxapp_install_idle_handler();
294 /* Owen Taylor: "if the drop is not in a drop zone,
295 return FALSE, otherwise, if you aren't accepting
296 the drop, call gtk_drag_finish() with success == FALSE
297 otherwise call gtk_drag_data_get()" */
299 // printf( "drop.\n" );
301 /* this seems to make a difference between not accepting
302 due to wrong target area and due to wrong format. let
303 us hope that this is not required.. */
305 /* inform the wxDropTarget about the current GdkDragContext.
306 this is only valid for the duration of this call */
307 drop_target
->SetDragContext( context
);
309 /* inform the wxDropTarget about the current drag widget.
310 this is only valid for the duration of this call */
311 drop_target
->SetDragWidget( widget
);
313 /* inform the wxDropTarget about the current drag time.
314 this is only valid for the duration of this call */
315 drop_target
->SetDragTime( time
);
318 wxDragResult result = wxDragMove;
319 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
322 /* reset the block here as someone might very well
323 show a dialog as a reaction to a drop and this
324 wouldn't work without events */
325 g_blockEventsOnDrag
= false;
327 bool ret
= drop_target
->OnDrop( x
, y
);
331 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
333 /* cancel the whole thing */
334 gtk_drag_finish( context
,
335 FALSE
, /* no success */
336 FALSE
, /* don't delete data on dropping side */
341 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
344 /* disable GUI threads */
347 GdkAtom format
= drop_target
->GetMatchingPair();
349 // this does happen somehow, see bug 555111
350 wxCHECK_MSG( format
, FALSE
, _T("no matching GdkAtom for format?") );
353 GdkDragAction action = GDK_ACTION_MOVE;
354 if (result == wxDragCopy) action == GDK_ACTION_COPY;
355 context->action = action;
357 /* this should trigger an "drag_data_received" event */
358 gtk_drag_get_data( widget
,
364 /* re-enable GUI threads */
368 /* after this, invalidate the drop_target's GdkDragContext */
369 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
371 /* after this, invalidate the drop_target's drag widget */
372 drop_target
->SetDragWidget( (GtkWidget
*) NULL
);
374 /* this has to be done because GDK has no "drag_enter" event */
375 drop_target
->m_firstMotion
= true;
381 // ----------------------------------------------------------------------------
382 // "drag_data_received"
383 // ----------------------------------------------------------------------------
386 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
387 GdkDragContext
*context
,
390 GtkSelectionData
*data
,
391 guint
WXUNUSED(info
),
393 wxDropTarget
*drop_target
)
395 if (g_isIdle
) wxapp_install_idle_handler();
397 /* Owen Taylor: "call gtk_drag_finish() with
400 if ((data
->length
<= 0) || (data
->format
!= 8))
402 /* negative data length and non 8-bit data format
403 qualifies for junk */
404 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
409 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
411 /* inform the wxDropTarget about the current GtkSelectionData.
412 this is only valid for the duration of this call */
413 drop_target
->SetDragData( data
);
415 wxDragResult result
= ConvertFromGTK(context
->action
);
417 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
419 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
421 /* tell GTK that data transfer was successful */
422 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
426 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
428 /* tell GTK that data transfer was not successful */
429 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
432 /* after this, invalidate the drop_target's drag data */
433 drop_target
->SetDragData( (GtkSelectionData
*) NULL
);
437 //----------------------------------------------------------------------------
439 //----------------------------------------------------------------------------
441 wxDropTarget::wxDropTarget( wxDataObject
*data
)
442 : wxDropTargetBase( data
)
444 m_firstMotion
= true;
445 m_dragContext
= (GdkDragContext
*) NULL
;
446 m_dragWidget
= (GtkWidget
*) NULL
;
447 m_dragData
= (GtkSelectionData
*) NULL
;
451 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
455 // GetMatchingPair() checks for m_dataObject too, no need to do it here
457 // disable the debug message from GetMatchingPair() - there are too many
463 return (GetMatchingPair() != (GdkAtom
) 0) ? def
: wxDragNone
;
466 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
471 return (GetMatchingPair() != (GdkAtom
) 0);
474 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
480 if (GetMatchingPair() == (GdkAtom
) 0)
483 return GetData() ? def
: wxDragNone
;
486 GdkAtom
wxDropTarget::GetMatchingPair()
494 GList
*child
= m_dragContext
->targets
;
497 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
498 wxDataFormat
format( formatAtom
);
501 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
502 format
.GetId().c_str());
505 if (m_dataObject
->IsSupportedFormat( format
))
514 bool wxDropTarget::GetData()
522 wxDataFormat
dragFormat( m_dragData
->target
);
524 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
527 m_dataObject
->SetData( dragFormat
, (size_t)m_dragData
->length
, (const void*)m_dragData
->data
);
532 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
534 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
536 gtk_drag_dest_unset( widget
);
538 g_signal_handlers_disconnect_by_func (widget
,
539 (gpointer
) target_drag_leave
, this);
540 g_signal_handlers_disconnect_by_func (widget
,
541 (gpointer
) target_drag_motion
, this);
542 g_signal_handlers_disconnect_by_func (widget
,
543 (gpointer
) target_drag_drop
, this);
544 g_signal_handlers_disconnect_by_func (widget
,
545 (gpointer
) target_drag_data_received
, this);
548 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
550 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
552 /* gtk_drag_dest_set() determines what default behaviour we'd like
553 GTK to supply. we don't want to specify out targets (=formats)
554 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
555 not GTK_DEST_DEFAULT_DROP). instead we react individually to
556 "drag_motion" and "drag_drop" events. this makes it possible
557 to allow dropping on only a small area. we should set
558 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
559 highlighting if dragging over standard controls, but this
560 seems to be broken without the other two. */
562 gtk_drag_dest_set( widget
,
563 (GtkDestDefaults
) 0, /* no default behaviour */
564 (GtkTargetEntry
*) NULL
, /* we don't supply any formats here */
565 0, /* number of targets = 0 */
566 (GdkDragAction
) 0 ); /* we don't supply any actions here */
568 g_signal_connect (widget
, "drag_leave",
569 G_CALLBACK (target_drag_leave
), this);
571 g_signal_connect (widget
, "drag_motion",
572 G_CALLBACK (target_drag_motion
), this);
574 g_signal_connect (widget
, "drag_drop",
575 G_CALLBACK (target_drag_drop
), this);
577 g_signal_connect (widget
, "drag_data_received",
578 G_CALLBACK (target_drag_data_received
), this);
581 //----------------------------------------------------------------------------
583 //----------------------------------------------------------------------------
587 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
588 GdkDragContext
*WXUNUSED(context
),
589 GtkSelectionData
*selection_data
,
590 guint
WXUNUSED(info
),
591 guint
WXUNUSED(time
),
592 wxDropSource
*drop_source
)
594 if (g_isIdle
) wxapp_install_idle_handler();
596 wxDataFormat
format( selection_data
->target
);
598 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
599 format
.GetId().c_str());
601 drop_source
->m_retValue
= wxDragCancel
;
603 wxDataObject
*data
= drop_source
->GetDataObject();
607 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
611 if (!data
->IsSupportedFormat(format
))
613 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
617 if (data
->GetDataSize(format
) == 0)
619 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
623 size_t size
= data
->GetDataSize(format
);
625 // printf( "data size: %d.\n", (int)data_size );
627 guchar
*d
= new guchar
[size
];
629 if (!data
->GetDataHere( format
, (void*)d
))
636 /* disable GUI threads */
639 gtk_selection_data_set( selection_data
,
640 selection_data
->target
,
646 /* enable GUI threads */
653 //----------------------------------------------------------------------------
654 // "drag_data_delete"
655 //----------------------------------------------------------------------------
658 static void source_drag_data_delete( GtkWidget
*WXUNUSED(widget
),
659 GdkDragContext
*context
,
660 wxDropSource
*WXUNUSED(drop_source
) )
663 wxapp_install_idle_handler();
665 // printf( "Drag source: drag_data_delete\n" );
669 //----------------------------------------------------------------------------
671 //----------------------------------------------------------------------------
674 static void source_drag_begin( GtkWidget
*WXUNUSED(widget
),
675 GdkDragContext
*WXUNUSED(context
),
676 wxDropSource
*WXUNUSED(drop_source
) )
679 wxapp_install_idle_handler();
681 // printf( "Drag source: drag_begin.\n" );
685 //----------------------------------------------------------------------------
687 //----------------------------------------------------------------------------
690 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
691 GdkDragContext
*WXUNUSED(context
),
692 wxDropSource
*drop_source
)
694 if (g_isIdle
) wxapp_install_idle_handler();
696 // printf( "Drag source: drag_end.\n" );
698 drop_source
->m_waiting
= false;
702 //-----------------------------------------------------------------------------
703 // "configure_event" from m_iconWindow
704 //-----------------------------------------------------------------------------
708 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
711 wxapp_install_idle_handler();
713 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
719 //---------------------------------------------------------------------------
721 //---------------------------------------------------------------------------
723 wxDropSource::wxDropSource(wxWindow
*win
,
724 const wxIcon
&iconCopy
,
725 const wxIcon
&iconMove
,
726 const wxIcon
&iconNone
)
730 m_iconWindow
= (GtkWidget
*) NULL
;
733 m_widget
= win
->m_widget
;
734 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
736 m_retValue
= wxDragCancel
;
738 SetIcons(iconCopy
, iconMove
, iconNone
);
741 wxDropSource::wxDropSource(wxDataObject
& data
,
743 const wxIcon
&iconCopy
,
744 const wxIcon
&iconMove
,
745 const wxIcon
&iconNone
)
751 m_iconWindow
= (GtkWidget
*) NULL
;
754 m_widget
= win
->m_widget
;
755 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
757 m_retValue
= wxDragCancel
;
759 SetIcons(iconCopy
, iconMove
, iconNone
);
762 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
763 const wxIcon
&iconMove
,
764 const wxIcon
&iconNone
)
766 m_iconCopy
= iconCopy
;
767 m_iconMove
= iconMove
;
768 m_iconNone
= iconNone
;
770 if ( !m_iconCopy
.Ok() )
771 m_iconCopy
= wxIcon(page_xpm
);
772 if ( !m_iconMove
.Ok() )
773 m_iconMove
= m_iconCopy
;
774 if ( !m_iconNone
.Ok() )
775 m_iconNone
= m_iconCopy
;
778 wxDropSource::~wxDropSource()
782 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
784 // get the right icon to display
786 if ( action
& GDK_ACTION_MOVE
)
788 else if ( action
& GDK_ACTION_COPY
)
794 if ( icon
->GetMask() )
795 mask
= icon
->GetMask()->GetBitmap();
797 mask
= (GdkBitmap
*)NULL
;
799 GdkPixmap
*pixmap
= icon
->GetPixmap();
802 gdk_drawable_get_size (pixmap
, &width
, &height
);
804 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
805 gtk_widget_push_colormap (colormap
);
807 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
808 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
809 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow
), TRUE
);
811 gtk_widget_pop_colormap ();
813 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
814 gtk_widget_realize (m_iconWindow
);
816 g_signal_connect (m_iconWindow
, "configure_event",
817 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
819 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
822 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
824 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
827 wxDragResult
wxDropSource::DoDragDrop(int flags
)
829 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
830 wxT("Drop source: no data") );
833 if (g_blockEventsOnDrag
)
837 g_blockEventsOnDrag
= true;
843 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
845 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
846 m_data
->GetAllFormats( array
);
847 size_t count
= m_data
->GetFormatCount();
848 for (size_t i
= 0; i
< count
; i
++)
850 GdkAtom atom
= array
[i
];
851 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom
));
852 gtk_target_list_add( target_list
, atom
, 0, 0 );
856 GdkEventMotion event
;
857 event
.window
= m_widget
->window
;
860 GdkModifierType state
;
861 gdk_window_get_pointer( event
.window
, &x
, &y
, &state
);
865 event
.time
= (guint32
)GDK_CURRENT_TIME
;
867 /* GTK wants to know which button was pressed which caused the dragging */
868 int button_number
= 0;
869 if (event
.state
& GDK_BUTTON1_MASK
) button_number
= 1;
870 else if (event
.state
& GDK_BUTTON2_MASK
) button_number
= 2;
871 else if (event
.state
& GDK_BUTTON3_MASK
) button_number
= 3;
874 /* disable GUI threads */
877 /* don't start dragging if no button is down */
880 int action
= GDK_ACTION_COPY
;
881 if ( flags
& wxDrag_AllowMove
)
882 action
|= GDK_ACTION_MOVE
;
884 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
885 // to use a global to pass the flags to the drop target but I'd
886 // surely prefer a better way to do it
887 gs_flagsForDrag
= flags
;
889 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
891 (GdkDragAction
)action
,
892 button_number
, /* number of mouse button which started drag */
893 (GdkEvent
*) &event
);
895 m_dragContext
= context
;
897 PrepareIcon( action
, context
);
900 gtk_main_iteration();
902 m_retValue
= ConvertFromGTK(context
->action
);
903 if ( m_retValue
== wxDragNone
)
904 m_retValue
= wxDragCancel
;
908 /* re-enable GUI threads */
911 g_blockEventsOnDrag
= false;
918 void wxDropSource::RegisterWindow()
920 if (!m_widget
) return;
922 g_signal_connect (m_widget
, "drag_data_get",
923 G_CALLBACK (source_drag_data_get
), this);
924 g_signal_connect (m_widget
, "drag_data_delete",
925 G_CALLBACK (source_drag_data_delete
), this);
926 g_signal_connect (m_widget
, "drag_begin",
927 G_CALLBACK (source_drag_begin
), this);
928 g_signal_connect (m_widget
, "drag_end",
929 G_CALLBACK (source_drag_end
), this);
933 void wxDropSource::UnregisterWindow()
938 g_signal_handlers_disconnect_by_func (m_widget
,
939 (gpointer
) source_drag_data_get
,
941 g_signal_handlers_disconnect_by_func (m_widget
,
942 (gpointer
) source_drag_data_delete
,
944 g_signal_handlers_disconnect_by_func (m_widget
,
945 (gpointer
) source_drag_begin
,
947 g_signal_handlers_disconnect_by_func (m_widget
,
948 (gpointer
) source_drag_end
,
953 // wxUSE_DRAG_AND_DROP