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/gtk/private.h"
28 #include <gdk/gdkprivate.h>
30 #include <gtk/gtkdnd.h>
31 #include <gtk/gtkselection.h>
33 //----------------------------------------------------------------------------
35 //----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 // the flags used for the last DoDragDrop()
40 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");
49 // global variables because GTK+ DnD want to have the
50 // mouse event that caused it
51 extern GdkEvent
*g_lastMouseEvent
;
52 extern int g_lastButtonNumber
;
54 //----------------------------------------------------------------------------
56 //----------------------------------------------------------------------------
58 /* Copyright (c) Julian Smart */
59 static const char * page_xpm
[] = {
60 /* columns rows colors chars-per-pixel */
109 " 56477<<<<8<<9&:X ",
110 " 59642%&*=-;<09&:5 ",
111 " 5q9642%&*=-<<<<<# ",
112 " 5qqw777<<<<<88:>+ ",
113 " erqq9642%&*=t;::+ ",
114 " eyrqq9642%&*=t;:O ",
115 " eyywwww777<<<<t;O ",
116 " e0yyrqq9642%&*=to ",
117 " e00yyrqq9642%&*=o ",
118 " eu0wwwwwww777<&*X ",
119 " euu00yyrqq9642%&X ",
120 " eiuu00yyrqq9642%X ",
121 " eiiwwwwwwwwww742$ ",
122 " eiiiuu00yyrqq964$ ",
123 " eiiiiuu00yyrqq96$ ",
124 " eiiiiiuu00yyrqq95 ",
125 " eiiiiiiuu00yyrqq5 ",
126 " eeeeeeeeeeeeee55e ",
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
140 // convert between GTK+ and wxWidgets DND constants
141 // ----------------------------------------------------------------------------
143 static wxDragResult
ConvertFromGTK(long action
)
147 case GDK_ACTION_COPY
:
150 case GDK_ACTION_LINK
:
153 case GDK_ACTION_MOVE
:
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
165 static void target_drag_leave( GtkWidget
*WXUNUSED(widget
),
166 GdkDragContext
*context
,
167 guint
WXUNUSED(time
),
168 wxDropTarget
*drop_target
)
170 if (g_isIdle
) wxapp_install_idle_handler();
172 /* inform the wxDropTarget about the current GdkDragContext.
173 this is only valid for the duration of this call */
174 drop_target
->SetDragContext( context
);
176 /* we don't need return values. this event is just for
178 drop_target
->OnLeave();
180 /* this has to be done because GDK has no "drag_enter" event */
181 drop_target
->m_firstMotion
= true;
183 /* after this, invalidate the drop_target's GdkDragContext */
184 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
193 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
194 GdkDragContext
*context
,
198 wxDropTarget
*drop_target
)
200 if (g_isIdle
) wxapp_install_idle_handler();
202 /* Owen Taylor: "if the coordinates not in a drop zone,
203 return FALSE, otherwise call gtk_drag_status() and
206 /* inform the wxDropTarget about the current GdkDragContext.
207 this is only valid for the duration of this call */
208 drop_target
->SetDragContext( context
);
210 // GTK+ always supposes that we want to copy the data by default while we
211 // might want to move it, so examine not only suggested_action - which is
212 // only good if we don't have our own preferences - but also the actions
215 if (drop_target
->GetDefaultAction() == wxDragNone
)
217 // use default action set by wxDropSource::DoDragDrop()
218 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
219 (context
->actions
& GDK_ACTION_MOVE
) )
221 // move is requested by the program and allowed by GTK+ - do it, even
222 // though suggested_action may be currently wxDragCopy
225 else // use whatever GTK+ says we should
227 result
= ConvertFromGTK(context
->suggested_action
);
229 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
231 // we're requested to move but we can't
236 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
237 (context
->actions
& GDK_ACTION_MOVE
))
243 if (context
->actions
& GDK_ACTION_COPY
)
245 else if (context
->actions
& GDK_ACTION_MOVE
)
251 if (drop_target
->m_firstMotion
)
253 /* the first "drag_motion" event substitutes a "drag_enter" event */
254 result
= drop_target
->OnEnter( x
, y
, result
);
258 /* give program a chance to react (i.e. to say no by returning FALSE) */
259 result
= drop_target
->OnDragOver( x
, y
, result
);
262 bool ret
= wxIsDragResultOk( result
);
265 GdkDragAction action
;
266 if (result
== wxDragCopy
)
267 action
= GDK_ACTION_COPY
;
268 else if (result
== wxDragLink
)
269 action
= GDK_ACTION_LINK
;
271 action
= GDK_ACTION_MOVE
;
273 gdk_drag_status( context
, action
, time
);
276 /* after this, invalidate the drop_target's GdkDragContext */
277 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
279 /* this has to be done because GDK has no "drag_enter" event */
280 drop_target
->m_firstMotion
= false;
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
291 static gboolean
target_drag_drop( GtkWidget
*widget
,
292 GdkDragContext
*context
,
296 wxDropTarget
*drop_target
)
298 if (g_isIdle
) wxapp_install_idle_handler();
300 /* Owen Taylor: "if the drop is not in a drop zone,
301 return FALSE, otherwise, if you aren't accepting
302 the drop, call gtk_drag_finish() with success == FALSE
303 otherwise call gtk_drag_data_get()" */
305 // printf( "drop.\n" );
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
->SetDragContext( context
);
315 /* inform the wxDropTarget about the current drag widget.
316 this is only valid for the duration of this call */
317 drop_target
->SetDragWidget( widget
);
319 /* inform the wxDropTarget about the current drag time.
320 this is only valid for the duration of this call */
321 drop_target
->SetDragTime( 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
->GetMatchingPair();
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
->SetDragContext( (GdkDragContext
*) NULL
);
381 /* after this, invalidate the drop_target's drag widget */
382 drop_target
->SetDragWidget( (GtkWidget
*) 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 if (g_isIdle
) wxapp_install_idle_handler();
407 /* Owen Taylor: "call gtk_drag_finish() with
410 if ((data
->length
<= 0) || (data
->format
!= 8))
412 /* negative data length and non 8-bit data format
413 qualifies for junk */
414 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
420 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
423 /* inform the wxDropTarget about the current GtkSelectionData.
424 this is only valid for the duration of this call */
425 drop_target
->SetDragData( data
);
427 wxDragResult result
= ConvertFromGTK(context
->action
);
429 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
432 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
435 /* tell GTK that data transfer was successful */
436 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
441 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
444 /* tell GTK that data transfer was not successful */
445 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
448 /* after this, invalidate the drop_target's drag data */
449 drop_target
->SetDragData( (GtkSelectionData
*) NULL
);
453 //----------------------------------------------------------------------------
455 //----------------------------------------------------------------------------
457 wxDropTarget::wxDropTarget( wxDataObject
*data
)
458 : wxDropTargetBase( data
)
460 m_firstMotion
= true;
461 m_dragContext
= (GdkDragContext
*) NULL
;
462 m_dragWidget
= (GtkWidget
*) NULL
;
463 m_dragData
= (GtkSelectionData
*) NULL
;
467 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
471 // GetMatchingPair() checks for m_dataObject too, no need to do it here
473 // disable the debug message from GetMatchingPair() - there are too many
479 return (GetMatchingPair() != (GdkAtom
) 0) ? def
: wxDragNone
;
482 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
487 return (GetMatchingPair() != (GdkAtom
) 0);
490 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
496 if (GetMatchingPair() == (GdkAtom
) 0)
499 return GetData() ? def
: wxDragNone
;
502 GdkAtom
wxDropTarget::GetMatchingPair()
510 GList
*child
= m_dragContext
->targets
;
513 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
514 wxDataFormat
format( formatAtom
);
517 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
518 format
.GetId().c_str());
521 if (m_dataObject
->IsSupportedFormat( format
))
530 bool wxDropTarget::GetData()
538 wxDataFormat
dragFormat( m_dragData
->target
);
540 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
543 m_dataObject
->SetData( dragFormat
, (size_t)m_dragData
->length
, (const void*)m_dragData
->data
);
548 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
550 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
552 gtk_drag_dest_unset( widget
);
554 g_signal_handlers_disconnect_by_func (widget
,
555 (gpointer
) target_drag_leave
, this);
556 g_signal_handlers_disconnect_by_func (widget
,
557 (gpointer
) target_drag_motion
, this);
558 g_signal_handlers_disconnect_by_func (widget
,
559 (gpointer
) target_drag_drop
, this);
560 g_signal_handlers_disconnect_by_func (widget
,
561 (gpointer
) target_drag_data_received
, this);
564 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
566 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
568 /* gtk_drag_dest_set() determines what default behaviour we'd like
569 GTK to supply. we don't want to specify out targets (=formats)
570 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
571 not GTK_DEST_DEFAULT_DROP). instead we react individually to
572 "drag_motion" and "drag_drop" events. this makes it possible
573 to allow dropping on only a small area. we should set
574 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
575 highlighting if dragging over standard controls, but this
576 seems to be broken without the other two. */
578 gtk_drag_dest_set( widget
,
579 (GtkDestDefaults
) 0, /* no default behaviour */
580 (GtkTargetEntry
*) NULL
, /* we don't supply any formats here */
581 0, /* number of targets = 0 */
582 (GdkDragAction
) 0 ); /* we don't supply any actions here */
584 g_signal_connect (widget
, "drag_leave",
585 G_CALLBACK (target_drag_leave
), this);
587 g_signal_connect (widget
, "drag_motion",
588 G_CALLBACK (target_drag_motion
), this);
590 g_signal_connect (widget
, "drag_drop",
591 G_CALLBACK (target_drag_drop
), this);
593 g_signal_connect (widget
, "drag_data_received",
594 G_CALLBACK (target_drag_data_received
), this);
597 //----------------------------------------------------------------------------
599 //----------------------------------------------------------------------------
603 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
604 GdkDragContext
*WXUNUSED(context
),
605 GtkSelectionData
*selection_data
,
606 guint
WXUNUSED(info
),
607 guint
WXUNUSED(time
),
608 wxDropSource
*drop_source
)
610 if (g_isIdle
) wxapp_install_idle_handler();
612 wxDataFormat
format( selection_data
->target
);
615 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
616 format
.GetId().c_str());
619 drop_source
->m_retValue
= wxDragCancel
;
621 wxDataObject
*data
= drop_source
->GetDataObject();
626 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
631 if (!data
->IsSupportedFormat(format
))
634 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
639 if (data
->GetDataSize(format
) == 0)
642 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
647 size_t size
= data
->GetDataSize(format
);
649 // printf( "data size: %d.\n", (int)data_size );
651 guchar
*d
= new guchar
[size
];
653 if (!data
->GetDataHere( format
, (void*)d
))
660 /* disable GUI threads */
663 gtk_selection_data_set( selection_data
,
664 selection_data
->target
,
670 /* enable GUI threads */
677 //----------------------------------------------------------------------------
678 // "drag_data_delete"
679 //----------------------------------------------------------------------------
682 static void source_drag_data_delete( GtkWidget
*WXUNUSED(widget
),
683 GdkDragContext
*WXUNUSED(context
),
684 wxDropSource
*WXUNUSED(drop_source
) )
687 wxapp_install_idle_handler();
689 // printf( "Drag source: drag_data_delete\n" );
693 //----------------------------------------------------------------------------
695 //----------------------------------------------------------------------------
698 static void source_drag_begin( GtkWidget
*WXUNUSED(widget
),
699 GdkDragContext
*WXUNUSED(context
),
700 wxDropSource
*WXUNUSED(drop_source
) )
703 wxapp_install_idle_handler();
705 // printf( "Drag source: drag_begin.\n" );
709 //----------------------------------------------------------------------------
711 //----------------------------------------------------------------------------
714 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
715 GdkDragContext
*WXUNUSED(context
),
716 wxDropSource
*drop_source
)
718 if (g_isIdle
) wxapp_install_idle_handler();
720 // printf( "Drag source: drag_end.\n" );
722 drop_source
->m_waiting
= false;
726 //-----------------------------------------------------------------------------
727 // "configure_event" from m_iconWindow
728 //-----------------------------------------------------------------------------
732 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
734 // don't need to install idle handler, its done from "event" signal
736 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
742 //---------------------------------------------------------------------------
744 //---------------------------------------------------------------------------
746 wxDropSource::wxDropSource(wxWindow
*win
,
747 const wxIcon
&iconCopy
,
748 const wxIcon
&iconMove
,
749 const wxIcon
&iconNone
)
753 m_iconWindow
= (GtkWidget
*) NULL
;
756 m_widget
= win
->m_widget
;
757 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
759 m_retValue
= wxDragCancel
;
761 SetIcons(iconCopy
, iconMove
, iconNone
);
764 wxDropSource::wxDropSource(wxDataObject
& data
,
766 const wxIcon
&iconCopy
,
767 const wxIcon
&iconMove
,
768 const wxIcon
&iconNone
)
774 m_iconWindow
= (GtkWidget
*) NULL
;
777 m_widget
= win
->m_widget
;
778 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
780 m_retValue
= wxDragCancel
;
782 SetIcons(iconCopy
, iconMove
, iconNone
);
785 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
786 const wxIcon
&iconMove
,
787 const wxIcon
&iconNone
)
789 m_iconCopy
= iconCopy
;
790 m_iconMove
= iconMove
;
791 m_iconNone
= iconNone
;
793 if ( !m_iconCopy
.Ok() )
794 m_iconCopy
= wxIcon(page_xpm
);
795 if ( !m_iconMove
.Ok() )
796 m_iconMove
= m_iconCopy
;
797 if ( !m_iconNone
.Ok() )
798 m_iconNone
= m_iconCopy
;
801 wxDropSource::~wxDropSource()
805 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
807 // get the right icon to display
809 if ( action
& GDK_ACTION_MOVE
)
811 else if ( action
& GDK_ACTION_COPY
)
817 if ( icon
->GetMask() )
818 mask
= icon
->GetMask()->GetBitmap();
820 mask
= (GdkBitmap
*)NULL
;
822 GdkPixmap
*pixmap
= icon
->GetPixmap();
825 gdk_drawable_get_size (pixmap
, &width
, &height
);
827 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
828 gtk_widget_push_colormap (colormap
);
830 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
831 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
832 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow
), TRUE
);
834 gtk_widget_pop_colormap ();
836 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
837 gtk_widget_realize (m_iconWindow
);
839 g_signal_connect (m_iconWindow
, "configure_event",
840 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
842 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
845 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
847 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
850 wxDragResult
wxDropSource::DoDragDrop(int flags
)
852 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
853 wxT("Drop source: no data") );
856 if (g_blockEventsOnDrag
)
859 // don't start dragging if no button is down
860 if (g_lastButtonNumber
== 0)
863 // we can only start a drag after a mouse event
864 if (g_lastMouseEvent
== NULL
)
868 g_blockEventsOnDrag
= true;
874 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
876 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
877 m_data
->GetAllFormats( array
);
878 size_t count
= m_data
->GetFormatCount();
879 for (size_t i
= 0; i
< count
; i
++)
881 GdkAtom atom
= array
[i
];
883 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom
));
885 gtk_target_list_add( target_list
, atom
, 0, 0 );
889 int action
= GDK_ACTION_COPY
;
890 if ( flags
& wxDrag_AllowMove
)
891 action
|= GDK_ACTION_MOVE
;
893 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
894 // to use a global to pass the flags to the drop target but I'd
895 // surely prefer a better way to do it
896 gs_flagsForDrag
= flags
;
898 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
900 (GdkDragAction
)action
,
901 g_lastButtonNumber
, // number of mouse button which started drag
902 (GdkEvent
*) g_lastMouseEvent
);
904 m_dragContext
= context
;
906 PrepareIcon( action
, context
);
909 gtk_main_iteration();
911 m_retValue
= ConvertFromGTK(context
->action
);
912 if ( m_retValue
== wxDragNone
)
913 m_retValue
= wxDragCancel
;
915 g_blockEventsOnDrag
= false;
922 void wxDropSource::RegisterWindow()
924 if (!m_widget
) return;
926 g_signal_connect (m_widget
, "drag_data_get",
927 G_CALLBACK (source_drag_data_get
), this);
928 g_signal_connect (m_widget
, "drag_data_delete",
929 G_CALLBACK (source_drag_data_delete
), this);
930 g_signal_connect (m_widget
, "drag_begin",
931 G_CALLBACK (source_drag_begin
), this);
932 g_signal_connect (m_widget
, "drag_end",
933 G_CALLBACK (source_drag_end
), this);
937 void wxDropSource::UnregisterWindow()
942 g_signal_handlers_disconnect_by_func (m_widget
,
943 (gpointer
) source_drag_data_get
,
945 g_signal_handlers_disconnect_by_func (m_widget
,
946 (gpointer
) source_drag_data_delete
,
948 g_signal_handlers_disconnect_by_func (m_widget
,
949 (gpointer
) source_drag_begin
,
951 g_signal_handlers_disconnect_by_func (m_widget
,
952 (gpointer
) source_drag_end
,
957 // wxUSE_DRAG_AND_DROP