1 ///////////////////////////////////////////////////////////////////////////////
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"
17 #if wxUSE_DRAG_AND_DROP
19 #include "wx/window.h"
21 #include "wx/gdicmn.h"
25 #include "wx/gtk/private.h"
27 #include <gdk/gdkprivate.h>
29 #include <gtk/gtkdnd.h>
30 #include <gtk/gtkselection.h>
32 //----------------------------------------------------------------------------
34 //----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 // the flags used for the last DoDragDrop()
39 static long gs_flagsForDrag
= 0;
41 // the trace mask we use with wxLogTrace() - call
42 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
43 // (there are quite a few of them, so don't enable this by default)
44 static const wxChar
*TRACE_DND
= _T("dnd");
46 //----------------------------------------------------------------------------
48 //----------------------------------------------------------------------------
50 /* Copyright (c) Julian Smart */
51 static const char * page_xpm
[] = {
52 /* columns rows colors chars-per-pixel */
101 " 56477<<<<8<<9&:X ",
102 " 59642%&*=-;<09&:5 ",
103 " 5q9642%&*=-<<<<<# ",
104 " 5qqw777<<<<<88:>+ ",
105 " erqq9642%&*=t;::+ ",
106 " eyrqq9642%&*=t;:O ",
107 " eyywwww777<<<<t;O ",
108 " e0yyrqq9642%&*=to ",
109 " e00yyrqq9642%&*=o ",
110 " eu0wwwwwww777<&*X ",
111 " euu00yyrqq9642%&X ",
112 " eiuu00yyrqq9642%X ",
113 " eiiwwwwwwwwww742$ ",
114 " eiiiuu00yyrqq964$ ",
115 " eiiiiuu00yyrqq96$ ",
116 " eiiiiiuu00yyrqq95 ",
117 " eiiiiiiuu00yyrqq5 ",
118 " eeeeeeeeeeeeee55e ",
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
132 // convert between GTK+ and wxWidgets DND constants
133 // ----------------------------------------------------------------------------
135 static wxDragResult
ConvertFromGTK(long action
)
139 case GDK_ACTION_COPY
:
142 case GDK_ACTION_LINK
:
145 case GDK_ACTION_MOVE
:
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
157 static void target_drag_leave( GtkWidget
*WXUNUSED(widget
),
158 GdkDragContext
*context
,
159 guint
WXUNUSED(time
),
160 wxDropTarget
*drop_target
)
162 if (g_isIdle
) wxapp_install_idle_handler();
164 /* inform the wxDropTarget about the current GdkDragContext.
165 this is only valid for the duration of this call */
166 drop_target
->SetDragContext( context
);
168 /* we don't need return values. this event is just for
170 drop_target
->OnLeave();
172 /* this has to be done because GDK has no "drag_enter" event */
173 drop_target
->m_firstMotion
= true;
175 /* after this, invalidate the drop_target's GdkDragContext */
176 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
185 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
186 GdkDragContext
*context
,
190 wxDropTarget
*drop_target
)
192 if (g_isIdle
) wxapp_install_idle_handler();
194 /* Owen Taylor: "if the coordinates not in a drop zone,
195 return FALSE, otherwise call gtk_drag_status() and
198 /* inform the wxDropTarget about the current GdkDragContext.
199 this is only valid for the duration of this call */
200 drop_target
->SetDragContext( context
);
202 // GTK+ always supposes that we want to copy the data by default while we
203 // might want to move it, so examine not only suggested_action - which is
204 // only good if we don't have our own preferences - but also the actions
207 if (drop_target
->GetDefaultAction() == wxDragNone
)
209 // use default action set by wxDropSource::DoDragDrop()
210 if ( (gs_flagsForDrag
& wxDrag_DefaultMove
) == wxDrag_DefaultMove
&&
211 (context
->actions
& GDK_ACTION_MOVE
) )
213 // move is requested by the program and allowed by GTK+ - do it, even
214 // though suggested_action may be currently wxDragCopy
217 else // use whatever GTK+ says we should
219 result
= ConvertFromGTK(context
->suggested_action
);
221 if ( (result
== wxDragMove
) && !(gs_flagsForDrag
& wxDrag_AllowMove
) )
223 // we're requested to move but we can't
228 else if (drop_target
->GetDefaultAction() == wxDragMove
&&
229 (context
->actions
& GDK_ACTION_MOVE
))
235 if (context
->actions
& GDK_ACTION_COPY
)
237 else if (context
->actions
& GDK_ACTION_MOVE
)
243 if (drop_target
->m_firstMotion
)
245 /* the first "drag_motion" event substitutes a "drag_enter" event */
246 result
= drop_target
->OnEnter( x
, y
, result
);
250 /* give program a chance to react (i.e. to say no by returning FALSE) */
251 result
= drop_target
->OnDragOver( x
, y
, result
);
254 bool ret
= wxIsDragResultOk( result
);
257 GdkDragAction action
;
258 if (result
== wxDragCopy
)
259 action
= GDK_ACTION_COPY
;
260 else if (result
== wxDragLink
)
261 action
= GDK_ACTION_LINK
;
263 action
= GDK_ACTION_MOVE
;
265 gdk_drag_status( context
, action
, time
);
268 /* after this, invalidate the drop_target's GdkDragContext */
269 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
271 /* this has to be done because GDK has no "drag_enter" event */
272 drop_target
->m_firstMotion
= false;
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
283 static gboolean
target_drag_drop( GtkWidget
*widget
,
284 GdkDragContext
*context
,
288 wxDropTarget
*drop_target
)
290 if (g_isIdle
) wxapp_install_idle_handler();
292 /* Owen Taylor: "if the drop is not in a drop zone,
293 return FALSE, otherwise, if you aren't accepting
294 the drop, call gtk_drag_finish() with success == FALSE
295 otherwise call gtk_drag_data_get()" */
297 // printf( "drop.\n" );
299 /* this seems to make a difference between not accepting
300 due to wrong target area and due to wrong format. let
301 us hope that this is not required.. */
303 /* inform the wxDropTarget about the current GdkDragContext.
304 this is only valid for the duration of this call */
305 drop_target
->SetDragContext( context
);
307 /* inform the wxDropTarget about the current drag widget.
308 this is only valid for the duration of this call */
309 drop_target
->SetDragWidget( widget
);
311 /* inform the wxDropTarget about the current drag time.
312 this is only valid for the duration of this call */
313 drop_target
->SetDragTime( time
);
316 wxDragResult result = wxDragMove;
317 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
320 /* reset the block here as someone might very well
321 show a dialog as a reaction to a drop and this
322 wouldn't work without events */
323 g_blockEventsOnDrag
= false;
325 bool ret
= drop_target
->OnDrop( x
, y
);
329 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
331 /* cancel the whole thing */
332 gtk_drag_finish( context
,
333 FALSE
, /* no success */
334 FALSE
, /* don't delete data on dropping side */
339 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned TRUE") );
342 /* disable GUI threads */
345 GdkAtom format
= drop_target
->GetMatchingPair();
347 // this does happen somehow, see bug 555111
348 wxCHECK_MSG( format
, FALSE
, _T("no matching GdkAtom for format?") );
351 GdkDragAction action = GDK_ACTION_MOVE;
352 if (result == wxDragCopy) action == GDK_ACTION_COPY;
353 context->action = action;
355 /* this should trigger an "drag_data_received" event */
356 gtk_drag_get_data( widget
,
362 /* re-enable GUI threads */
366 /* after this, invalidate the drop_target's GdkDragContext */
367 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
369 /* after this, invalidate the drop_target's drag widget */
370 drop_target
->SetDragWidget( (GtkWidget
*) NULL
);
372 /* this has to be done because GDK has no "drag_enter" event */
373 drop_target
->m_firstMotion
= true;
379 // ----------------------------------------------------------------------------
380 // "drag_data_received"
381 // ----------------------------------------------------------------------------
384 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
385 GdkDragContext
*context
,
388 GtkSelectionData
*data
,
389 guint
WXUNUSED(info
),
391 wxDropTarget
*drop_target
)
393 if (g_isIdle
) wxapp_install_idle_handler();
395 /* Owen Taylor: "call gtk_drag_finish() with
398 if ((data
->length
<= 0) || (data
->format
!= 8))
400 /* negative data length and non 8-bit data format
401 qualifies for junk */
402 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
407 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
409 /* inform the wxDropTarget about the current GtkSelectionData.
410 this is only valid for the duration of this call */
411 drop_target
->SetDragData( data
);
413 wxDragResult result
= ConvertFromGTK(context
->action
);
415 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
417 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned TRUE") );
419 /* tell GTK that data transfer was successful */
420 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
424 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
426 /* tell GTK that data transfer was not successful */
427 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
430 /* after this, invalidate the drop_target's drag data */
431 drop_target
->SetDragData( (GtkSelectionData
*) NULL
);
435 //----------------------------------------------------------------------------
437 //----------------------------------------------------------------------------
439 wxDropTarget::wxDropTarget( wxDataObject
*data
)
440 : wxDropTargetBase( data
)
442 m_firstMotion
= true;
443 m_dragContext
= (GdkDragContext
*) NULL
;
444 m_dragWidget
= (GtkWidget
*) NULL
;
445 m_dragData
= (GtkSelectionData
*) NULL
;
449 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
453 // GetMatchingPair() checks for m_dataObject too, no need to do it here
455 // disable the debug message from GetMatchingPair() - there are too many
461 return (GetMatchingPair() != (GdkAtom
) 0) ? def
: wxDragNone
;
464 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
469 return (GetMatchingPair() != (GdkAtom
) 0);
472 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
478 if (GetMatchingPair() == (GdkAtom
) 0)
481 return GetData() ? def
: wxDragNone
;
484 GdkAtom
wxDropTarget::GetMatchingPair()
492 GList
*child
= m_dragContext
->targets
;
495 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
496 wxDataFormat
format( formatAtom
);
499 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
500 format
.GetId().c_str());
503 if (m_dataObject
->IsSupportedFormat( format
))
512 bool wxDropTarget::GetData()
520 wxDataFormat
dragFormat( m_dragData
->target
);
522 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
525 m_dataObject
->SetData( dragFormat
, (size_t)m_dragData
->length
, (const void*)m_dragData
->data
);
530 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
532 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
534 gtk_drag_dest_unset( widget
);
536 g_signal_handlers_disconnect_by_func (widget
,
537 (gpointer
) target_drag_leave
, this);
538 g_signal_handlers_disconnect_by_func (widget
,
539 (gpointer
) target_drag_motion
, this);
540 g_signal_handlers_disconnect_by_func (widget
,
541 (gpointer
) target_drag_drop
, this);
542 g_signal_handlers_disconnect_by_func (widget
,
543 (gpointer
) target_drag_data_received
, this);
546 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
548 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
550 /* gtk_drag_dest_set() determines what default behaviour we'd like
551 GTK to supply. we don't want to specify out targets (=formats)
552 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
553 not GTK_DEST_DEFAULT_DROP). instead we react individually to
554 "drag_motion" and "drag_drop" events. this makes it possible
555 to allow dropping on only a small area. we should set
556 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
557 highlighting if dragging over standard controls, but this
558 seems to be broken without the other two. */
560 gtk_drag_dest_set( widget
,
561 (GtkDestDefaults
) 0, /* no default behaviour */
562 (GtkTargetEntry
*) NULL
, /* we don't supply any formats here */
563 0, /* number of targets = 0 */
564 (GdkDragAction
) 0 ); /* we don't supply any actions here */
566 g_signal_connect (widget
, "drag_leave",
567 G_CALLBACK (target_drag_leave
), this);
569 g_signal_connect (widget
, "drag_motion",
570 G_CALLBACK (target_drag_motion
), this);
572 g_signal_connect (widget
, "drag_drop",
573 G_CALLBACK (target_drag_drop
), this);
575 g_signal_connect (widget
, "drag_data_received",
576 G_CALLBACK (target_drag_data_received
), this);
579 //----------------------------------------------------------------------------
581 //----------------------------------------------------------------------------
585 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
586 GdkDragContext
*WXUNUSED(context
),
587 GtkSelectionData
*selection_data
,
588 guint
WXUNUSED(info
),
589 guint
WXUNUSED(time
),
590 wxDropSource
*drop_source
)
592 if (g_isIdle
) wxapp_install_idle_handler();
594 wxDataFormat
format( selection_data
->target
);
596 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
597 format
.GetId().c_str());
599 drop_source
->m_retValue
= wxDragCancel
;
601 wxDataObject
*data
= drop_source
->GetDataObject();
605 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
609 if (!data
->IsSupportedFormat(format
))
611 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
615 if (data
->GetDataSize(format
) == 0)
617 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
621 size_t size
= data
->GetDataSize(format
);
623 // printf( "data size: %d.\n", (int)data_size );
625 guchar
*d
= new guchar
[size
];
627 if (!data
->GetDataHere( format
, (void*)d
))
634 /* disable GUI threads */
637 gtk_selection_data_set( selection_data
,
638 selection_data
->target
,
644 /* enable GUI threads */
651 //----------------------------------------------------------------------------
652 // "drag_data_delete"
653 //----------------------------------------------------------------------------
656 static void source_drag_data_delete( GtkWidget
*WXUNUSED(widget
),
657 GdkDragContext
*context
,
658 wxDropSource
*WXUNUSED(drop_source
) )
661 wxapp_install_idle_handler();
663 // printf( "Drag source: drag_data_delete\n" );
667 //----------------------------------------------------------------------------
669 //----------------------------------------------------------------------------
672 static void source_drag_begin( GtkWidget
*WXUNUSED(widget
),
673 GdkDragContext
*WXUNUSED(context
),
674 wxDropSource
*WXUNUSED(drop_source
) )
677 wxapp_install_idle_handler();
679 // printf( "Drag source: drag_begin.\n" );
683 //----------------------------------------------------------------------------
685 //----------------------------------------------------------------------------
688 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
689 GdkDragContext
*WXUNUSED(context
),
690 wxDropSource
*drop_source
)
692 if (g_isIdle
) wxapp_install_idle_handler();
694 // printf( "Drag source: drag_end.\n" );
696 drop_source
->m_waiting
= false;
700 //-----------------------------------------------------------------------------
701 // "configure_event" from m_iconWindow
702 //-----------------------------------------------------------------------------
706 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
709 wxapp_install_idle_handler();
711 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
717 //---------------------------------------------------------------------------
719 //---------------------------------------------------------------------------
721 wxDropSource::wxDropSource(wxWindow
*win
,
722 const wxIcon
&iconCopy
,
723 const wxIcon
&iconMove
,
724 const wxIcon
&iconNone
)
728 m_iconWindow
= (GtkWidget
*) NULL
;
731 m_widget
= win
->m_widget
;
732 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
734 m_retValue
= wxDragCancel
;
736 SetIcons(iconCopy
, iconMove
, iconNone
);
739 wxDropSource::wxDropSource(wxDataObject
& data
,
741 const wxIcon
&iconCopy
,
742 const wxIcon
&iconMove
,
743 const wxIcon
&iconNone
)
749 m_iconWindow
= (GtkWidget
*) NULL
;
752 m_widget
= win
->m_widget
;
753 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
755 m_retValue
= wxDragCancel
;
757 SetIcons(iconCopy
, iconMove
, iconNone
);
760 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
761 const wxIcon
&iconMove
,
762 const wxIcon
&iconNone
)
764 m_iconCopy
= iconCopy
;
765 m_iconMove
= iconMove
;
766 m_iconNone
= iconNone
;
768 if ( !m_iconCopy
.Ok() )
769 m_iconCopy
= wxIcon(page_xpm
);
770 if ( !m_iconMove
.Ok() )
771 m_iconMove
= m_iconCopy
;
772 if ( !m_iconNone
.Ok() )
773 m_iconNone
= m_iconCopy
;
776 wxDropSource::~wxDropSource()
780 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
782 // get the right icon to display
784 if ( action
& GDK_ACTION_MOVE
)
786 else if ( action
& GDK_ACTION_COPY
)
792 if ( icon
->GetMask() )
793 mask
= icon
->GetMask()->GetBitmap();
795 mask
= (GdkBitmap
*)NULL
;
797 GdkPixmap
*pixmap
= icon
->GetPixmap();
800 gdk_window_get_size (pixmap
, &width
, &height
);
802 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
803 gtk_widget_push_colormap (colormap
);
805 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
806 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
807 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow
), TRUE
);
809 gtk_widget_pop_colormap ();
811 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
812 gtk_widget_realize (m_iconWindow
);
814 g_signal_connect (m_iconWindow
, "configure_event",
815 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
817 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
820 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
822 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
825 wxDragResult
wxDropSource::DoDragDrop(int flags
)
827 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
828 wxT("Drop source: no data") );
831 if (g_blockEventsOnDrag
)
835 g_blockEventsOnDrag
= true;
841 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
843 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
844 m_data
->GetAllFormats( array
);
845 size_t count
= m_data
->GetFormatCount();
846 for (size_t i
= 0; i
< count
; i
++)
848 GdkAtom atom
= array
[i
];
849 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom
));
850 gtk_target_list_add( target_list
, atom
, 0, 0 );
854 GdkEventMotion event
;
855 event
.window
= m_widget
->window
;
858 GdkModifierType state
;
859 gdk_window_get_pointer( event
.window
, &x
, &y
, &state
);
863 event
.time
= (guint32
)GDK_CURRENT_TIME
;
865 /* GTK wants to know which button was pressed which caused the dragging */
866 int button_number
= 0;
867 if (event
.state
& GDK_BUTTON1_MASK
) button_number
= 1;
868 else if (event
.state
& GDK_BUTTON2_MASK
) button_number
= 2;
869 else if (event
.state
& GDK_BUTTON3_MASK
) button_number
= 3;
872 /* disable GUI threads */
875 /* don't start dragging if no button is down */
878 int action
= GDK_ACTION_COPY
;
879 if ( flags
& wxDrag_AllowMove
)
880 action
|= GDK_ACTION_MOVE
;
882 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
883 // to use a global to pass the flags to the drop target but I'd
884 // surely prefer a better way to do it
885 gs_flagsForDrag
= flags
;
887 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
889 (GdkDragAction
)action
,
890 button_number
, /* number of mouse button which started drag */
891 (GdkEvent
*) &event
);
893 m_dragContext
= context
;
895 PrepareIcon( action
, context
);
898 gtk_main_iteration();
900 m_retValue
= ConvertFromGTK(context
->action
);
901 if ( m_retValue
== wxDragNone
)
902 m_retValue
= wxDragCancel
;
906 /* re-enable GUI threads */
909 g_blockEventsOnDrag
= false;
916 void wxDropSource::RegisterWindow()
918 if (!m_widget
) return;
920 g_signal_connect (m_widget
, "drag_data_get",
921 G_CALLBACK (source_drag_data_get
), this);
922 g_signal_connect (m_widget
, "drag_data_delete",
923 G_CALLBACK (source_drag_data_delete
), this);
924 g_signal_connect (m_widget
, "drag_begin",
925 G_CALLBACK (source_drag_begin
), this);
926 g_signal_connect (m_widget
, "drag_end",
927 G_CALLBACK (source_drag_end
), this);
931 void wxDropSource::UnregisterWindow()
936 g_signal_handlers_disconnect_by_func (m_widget
,
937 (gpointer
) source_drag_data_get
,
939 g_signal_handlers_disconnect_by_func (m_widget
,
940 (gpointer
) source_drag_data_delete
,
942 g_signal_handlers_disconnect_by_func (m_widget
,
943 (gpointer
) source_drag_begin
,
945 g_signal_handlers_disconnect_by_func (m_widget
,
946 (gpointer
) source_drag_end
,
951 // wxUSE_DRAG_AND_DROP