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;
42 // the trace mask we use with wxLogTrace() - call
43 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
44 // (there are quite a few of them, so don't enable this by default)
45 static const wxChar
*TRACE_DND
= _T("dnd");
47 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 if (g_isIdle
) wxapp_install_idle_handler();
169 /* inform the wxDropTarget about the current GdkDragContext.
170 this is only valid for the duration of this call */
171 drop_target
->SetDragContext( context
);
173 /* we don't need return values. this event is just for
175 drop_target
->OnLeave();
177 /* this has to be done because GDK has no "drag_enter" event */
178 drop_target
->m_firstMotion
= true;
180 /* after this, invalidate the drop_target's GdkDragContext */
181 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
191 GdkDragContext
*context
,
195 wxDropTarget
*drop_target
)
197 if (g_isIdle
) wxapp_install_idle_handler();
199 /* Owen Taylor: "if the coordinates not in a drop zone,
200 return FALSE, otherwise call gtk_drag_status() and
203 /* inform the wxDropTarget about the current GdkDragContext.
204 this is only valid for the duration of this call */
205 drop_target
->SetDragContext( context
);
207 // GTK+ always supposes that we want to copy the data by default while we
208 // might want to move it, so examine not only suggested_action - which is
209 // only good if we don't have our own preferences - but also the actions
212 if (drop_target
->GetDefaultAction() == wxDragNone
)
214 // use default action set by wxDropSource::DoDragDrop()
215 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
216 (context
->actions
& GDK_ACTION_MOVE
) )
218 // move is requested by the program and allowed by GTK+ - do it, even
219 // though suggested_action may be currently wxDragCopy
222 else // use whatever GTK+ says we should
224 result
= ConvertFromGTK(context
->suggested_action
);
226 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
228 // we're requested to move but we can't
233 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
234 (context
->actions
& GDK_ACTION_MOVE
))
240 if (context
->actions
& GDK_ACTION_COPY
)
242 else if (context
->actions
& GDK_ACTION_MOVE
)
248 if (drop_target
->m_firstMotion
)
250 /* the first "drag_motion" event substitutes a "drag_enter" event */
251 result
= drop_target
->OnEnter( x
, y
, result
);
255 /* give program a chance to react (i.e. to say no by returning FALSE) */
256 result
= drop_target
->OnDragOver( x
, y
, result
);
259 bool ret
= wxIsDragResultOk( result
);
262 GdkDragAction action
;
263 if (result
== wxDragCopy
)
264 action
= GDK_ACTION_COPY
;
265 else if (result
== wxDragLink
)
266 action
= GDK_ACTION_LINK
;
268 action
= GDK_ACTION_MOVE
;
270 gdk_drag_status( context
, action
, time
);
273 /* after this, invalidate the drop_target's GdkDragContext */
274 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
276 /* this has to be done because GDK has no "drag_enter" event */
277 drop_target
->m_firstMotion
= false;
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
288 static gboolean
target_drag_drop( GtkWidget
*widget
,
289 GdkDragContext
*context
,
293 wxDropTarget
*drop_target
)
295 if (g_isIdle
) wxapp_install_idle_handler();
297 /* Owen Taylor: "if the drop is not in a drop zone,
298 return FALSE, otherwise, if you aren't accepting
299 the drop, call gtk_drag_finish() with success == FALSE
300 otherwise call gtk_drag_data_get()" */
302 // printf( "drop.\n" );
304 /* this seems to make a difference between not accepting
305 due to wrong target area and due to wrong format. let
306 us hope that this is not required.. */
308 /* inform the wxDropTarget about the current GdkDragContext.
309 this is only valid for the duration of this call */
310 drop_target
->SetDragContext( context
);
312 /* inform the wxDropTarget about the current drag widget.
313 this is only valid for the duration of this call */
314 drop_target
->SetDragWidget( widget
);
316 /* inform the wxDropTarget about the current drag time.
317 this is only valid for the duration of this call */
318 drop_target
->SetDragTime( time
);
321 wxDragResult result = wxDragMove;
322 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
325 /* reset the block here as someone might very well
326 show a dialog as a reaction to a drop and this
327 wouldn't work without events */
328 g_blockEventsOnDrag
= false;
330 bool ret
= drop_target
->OnDrop( x
, y
);
334 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
336 /* cancel the whole thing */
337 gtk_drag_finish( context
,
338 FALSE
, /* no success */
339 FALSE
, /* don't delete data on dropping side */
344 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
347 /* disable GUI threads */
350 GdkAtom format
= drop_target
->GetMatchingPair();
352 // this does happen somehow, see bug 555111
353 wxCHECK_MSG( format
, FALSE
, _T("no matching GdkAtom for format?") );
356 GdkDragAction action = GDK_ACTION_MOVE;
357 if (result == wxDragCopy) action == GDK_ACTION_COPY;
358 context->action = action;
360 /* this should trigger an "drag_data_received" event */
361 gtk_drag_get_data( widget
,
367 /* re-enable GUI threads */
371 /* after this, invalidate the drop_target's GdkDragContext */
372 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
374 /* after this, invalidate the drop_target's drag widget */
375 drop_target
->SetDragWidget( (GtkWidget
*) NULL
);
377 /* this has to be done because GDK has no "drag_enter" event */
378 drop_target
->m_firstMotion
= true;
384 // ----------------------------------------------------------------------------
385 // "drag_data_received"
386 // ----------------------------------------------------------------------------
389 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
390 GdkDragContext
*context
,
393 GtkSelectionData
*data
,
394 guint
WXUNUSED(info
),
396 wxDropTarget
*drop_target
)
398 if (g_isIdle
) wxapp_install_idle_handler();
400 /* Owen Taylor: "call gtk_drag_finish() with
403 if ((data
->length
<= 0) || (data
->format
!= 8))
405 /* negative data length and non 8-bit data format
406 qualifies for junk */
407 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
412 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
414 /* inform the wxDropTarget about the current GtkSelectionData.
415 this is only valid for the duration of this call */
416 drop_target
->SetDragData( data
);
418 wxDragResult result
= ConvertFromGTK(context
->action
);
420 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
422 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
);
429 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 if (g_isIdle
) wxapp_install_idle_handler();
599 wxDataFormat
format( selection_data
->target
);
601 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
602 format
.GetId().c_str());
604 drop_source
->m_retValue
= wxDragCancel
;
606 wxDataObject
*data
= drop_source
->GetDataObject();
610 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
614 if (!data
->IsSupportedFormat(format
))
616 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
620 if (data
->GetDataSize(format
) == 0)
622 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
626 size_t size
= data
->GetDataSize(format
);
628 // printf( "data size: %d.\n", (int)data_size );
630 guchar
*d
= new guchar
[size
];
632 if (!data
->GetDataHere( format
, (void*)d
))
639 /* disable GUI threads */
642 gtk_selection_data_set( selection_data
,
643 selection_data
->target
,
649 /* enable GUI threads */
656 //----------------------------------------------------------------------------
657 // "drag_data_delete"
658 //----------------------------------------------------------------------------
661 static void source_drag_data_delete( GtkWidget
*WXUNUSED(widget
),
662 GdkDragContext
*WXUNUSED(context
),
663 wxDropSource
*WXUNUSED(drop_source
) )
666 wxapp_install_idle_handler();
668 // printf( "Drag source: drag_data_delete\n" );
672 //----------------------------------------------------------------------------
674 //----------------------------------------------------------------------------
677 static void source_drag_begin( GtkWidget
*WXUNUSED(widget
),
678 GdkDragContext
*WXUNUSED(context
),
679 wxDropSource
*WXUNUSED(drop_source
) )
682 wxapp_install_idle_handler();
684 // printf( "Drag source: drag_begin.\n" );
688 //----------------------------------------------------------------------------
690 //----------------------------------------------------------------------------
693 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
694 GdkDragContext
*WXUNUSED(context
),
695 wxDropSource
*drop_source
)
697 if (g_isIdle
) wxapp_install_idle_handler();
699 // printf( "Drag source: drag_end.\n" );
701 drop_source
->m_waiting
= false;
705 //-----------------------------------------------------------------------------
706 // "configure_event" from m_iconWindow
707 //-----------------------------------------------------------------------------
711 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
713 // don't need to install idle handler, its done from "event" signal
715 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
721 //---------------------------------------------------------------------------
723 //---------------------------------------------------------------------------
725 wxDropSource::wxDropSource(wxWindow
*win
,
726 const wxIcon
&iconCopy
,
727 const wxIcon
&iconMove
,
728 const wxIcon
&iconNone
)
732 m_iconWindow
= (GtkWidget
*) NULL
;
735 m_widget
= win
->m_widget
;
736 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
738 m_retValue
= wxDragCancel
;
740 SetIcons(iconCopy
, iconMove
, iconNone
);
743 wxDropSource::wxDropSource(wxDataObject
& data
,
745 const wxIcon
&iconCopy
,
746 const wxIcon
&iconMove
,
747 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 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
765 const wxIcon
&iconMove
,
766 const wxIcon
&iconNone
)
768 m_iconCopy
= iconCopy
;
769 m_iconMove
= iconMove
;
770 m_iconNone
= iconNone
;
772 if ( !m_iconCopy
.Ok() )
773 m_iconCopy
= wxIcon(page_xpm
);
774 if ( !m_iconMove
.Ok() )
775 m_iconMove
= m_iconCopy
;
776 if ( !m_iconNone
.Ok() )
777 m_iconNone
= m_iconCopy
;
780 wxDropSource::~wxDropSource()
784 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
786 // get the right icon to display
788 if ( action
& GDK_ACTION_MOVE
)
790 else if ( action
& GDK_ACTION_COPY
)
796 if ( icon
->GetMask() )
797 mask
= icon
->GetMask()->GetBitmap();
799 mask
= (GdkBitmap
*)NULL
;
801 GdkPixmap
*pixmap
= icon
->GetPixmap();
804 gdk_drawable_get_size (pixmap
, &width
, &height
);
806 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
807 gtk_widget_push_colormap (colormap
);
809 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
810 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
811 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow
), TRUE
);
813 gtk_widget_pop_colormap ();
815 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
816 gtk_widget_realize (m_iconWindow
);
818 g_signal_connect (m_iconWindow
, "configure_event",
819 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
821 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
824 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
826 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
829 wxDragResult
wxDropSource::DoDragDrop(int flags
)
831 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
832 wxT("Drop source: no data") );
835 if (g_blockEventsOnDrag
)
839 g_blockEventsOnDrag
= true;
845 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
847 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
848 m_data
->GetAllFormats( array
);
849 size_t count
= m_data
->GetFormatCount();
850 for (size_t i
= 0; i
< count
; i
++)
852 GdkAtom atom
= array
[i
];
853 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom
));
854 gtk_target_list_add( target_list
, atom
, 0, 0 );
858 /* don't start dragging if no button is down */
859 if (g_lastButtonNumber
)
861 int action
= GDK_ACTION_COPY
;
862 if ( flags
& wxDrag_AllowMove
)
863 action
|= GDK_ACTION_MOVE
;
865 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
866 // to use a global to pass the flags to the drop target but I'd
867 // surely prefer a better way to do it
868 gs_flagsForDrag
= flags
;
870 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
872 (GdkDragAction
)action
,
873 g_lastButtonNumber
, // number of mouse button which started drag
874 (GdkEvent
*) g_lastMouseEvent
);
876 m_dragContext
= context
;
878 PrepareIcon( action
, context
);
881 gtk_main_iteration();
883 m_retValue
= ConvertFromGTK(context
->action
);
884 if ( m_retValue
== wxDragNone
)
885 m_retValue
= wxDragCancel
;
888 g_blockEventsOnDrag
= false;
895 void wxDropSource::RegisterWindow()
897 if (!m_widget
) return;
899 g_signal_connect (m_widget
, "drag_data_get",
900 G_CALLBACK (source_drag_data_get
), this);
901 g_signal_connect (m_widget
, "drag_data_delete",
902 G_CALLBACK (source_drag_data_delete
), this);
903 g_signal_connect (m_widget
, "drag_begin",
904 G_CALLBACK (source_drag_begin
), this);
905 g_signal_connect (m_widget
, "drag_end",
906 G_CALLBACK (source_drag_end
), this);
910 void wxDropSource::UnregisterWindow()
915 g_signal_handlers_disconnect_by_func (m_widget
,
916 (gpointer
) source_drag_data_get
,
918 g_signal_handlers_disconnect_by_func (m_widget
,
919 (gpointer
) source_drag_data_delete
,
921 g_signal_handlers_disconnect_by_func (m_widget
,
922 (gpointer
) source_drag_begin
,
924 g_signal_handlers_disconnect_by_func (m_widget
,
925 (gpointer
) source_drag_end
,
930 // wxUSE_DRAG_AND_DROP