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
);
337 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned FALSE") );
339 /* cancel the whole thing */
340 gtk_drag_finish( context
,
341 FALSE
, /* no success */
342 FALSE
, /* don't delete data on dropping side */
347 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnDrop returned true") );
350 /* disable GUI threads */
353 GdkAtom format
= drop_target
->GetMatchingPair();
355 // this does happen somehow, see bug 555111
356 wxCHECK_MSG( format
, FALSE
, _T("no matching GdkAtom for format?") );
359 GdkDragAction action = GDK_ACTION_MOVE;
360 if (result == wxDragCopy) action == GDK_ACTION_COPY;
361 context->action = action;
363 /* this should trigger an "drag_data_received" event */
364 gtk_drag_get_data( widget
,
370 /* re-enable GUI threads */
374 /* after this, invalidate the drop_target's GdkDragContext */
375 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
377 /* after this, invalidate the drop_target's drag widget */
378 drop_target
->SetDragWidget( (GtkWidget
*) NULL
);
380 /* this has to be done because GDK has no "drag_enter" event */
381 drop_target
->m_firstMotion
= true;
387 // ----------------------------------------------------------------------------
388 // "drag_data_received"
389 // ----------------------------------------------------------------------------
392 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
393 GdkDragContext
*context
,
396 GtkSelectionData
*data
,
397 guint
WXUNUSED(info
),
399 wxDropTarget
*drop_target
)
401 if (g_isIdle
) wxapp_install_idle_handler();
403 /* Owen Taylor: "call gtk_drag_finish() with
406 if ((data
->length
<= 0) || (data
->format
!= 8))
408 /* negative data length and non 8-bit data format
409 qualifies for junk */
410 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
415 wxLogTrace(TRACE_DND
, wxT( "Drop target: data received event") );
417 /* inform the wxDropTarget about the current GtkSelectionData.
418 this is only valid for the duration of this call */
419 drop_target
->SetDragData( data
);
421 wxDragResult result
= ConvertFromGTK(context
->action
);
423 if ( wxIsDragResultOk( drop_target
->OnData( x
, y
, result
) ) )
425 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned true") );
427 /* tell GTK that data transfer was successful */
428 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
432 wxLogTrace(TRACE_DND
, wxT( "Drop target: OnData returned FALSE") );
434 /* tell GTK that data transfer was not successful */
435 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
438 /* after this, invalidate the drop_target's drag data */
439 drop_target
->SetDragData( (GtkSelectionData
*) NULL
);
443 //----------------------------------------------------------------------------
445 //----------------------------------------------------------------------------
447 wxDropTarget::wxDropTarget( wxDataObject
*data
)
448 : wxDropTargetBase( data
)
450 m_firstMotion
= true;
451 m_dragContext
= (GdkDragContext
*) NULL
;
452 m_dragWidget
= (GtkWidget
*) NULL
;
453 m_dragData
= (GtkSelectionData
*) NULL
;
457 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
461 // GetMatchingPair() checks for m_dataObject too, no need to do it here
463 // disable the debug message from GetMatchingPair() - there are too many
469 return (GetMatchingPair() != (GdkAtom
) 0) ? def
: wxDragNone
;
472 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
477 return (GetMatchingPair() != (GdkAtom
) 0);
480 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
486 if (GetMatchingPair() == (GdkAtom
) 0)
489 return GetData() ? def
: wxDragNone
;
492 GdkAtom
wxDropTarget::GetMatchingPair()
500 GList
*child
= m_dragContext
->targets
;
503 GdkAtom formatAtom
= (GdkAtom
)(child
->data
);
504 wxDataFormat
format( formatAtom
);
507 wxLogTrace(TRACE_DND
, wxT("Drop target: drag has format: %s"),
508 format
.GetId().c_str());
511 if (m_dataObject
->IsSupportedFormat( format
))
520 bool wxDropTarget::GetData()
528 wxDataFormat
dragFormat( m_dragData
->target
);
530 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
533 m_dataObject
->SetData( dragFormat
, (size_t)m_dragData
->length
, (const void*)m_dragData
->data
);
538 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
540 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
542 gtk_drag_dest_unset( widget
);
544 g_signal_handlers_disconnect_by_func (widget
,
545 (gpointer
) target_drag_leave
, this);
546 g_signal_handlers_disconnect_by_func (widget
,
547 (gpointer
) target_drag_motion
, this);
548 g_signal_handlers_disconnect_by_func (widget
,
549 (gpointer
) target_drag_drop
, this);
550 g_signal_handlers_disconnect_by_func (widget
,
551 (gpointer
) target_drag_data_received
, this);
554 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
556 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
558 /* gtk_drag_dest_set() determines what default behaviour we'd like
559 GTK to supply. we don't want to specify out targets (=formats)
560 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
561 not GTK_DEST_DEFAULT_DROP). instead we react individually to
562 "drag_motion" and "drag_drop" events. this makes it possible
563 to allow dropping on only a small area. we should set
564 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
565 highlighting if dragging over standard controls, but this
566 seems to be broken without the other two. */
568 gtk_drag_dest_set( widget
,
569 (GtkDestDefaults
) 0, /* no default behaviour */
570 (GtkTargetEntry
*) NULL
, /* we don't supply any formats here */
571 0, /* number of targets = 0 */
572 (GdkDragAction
) 0 ); /* we don't supply any actions here */
574 g_signal_connect (widget
, "drag_leave",
575 G_CALLBACK (target_drag_leave
), this);
577 g_signal_connect (widget
, "drag_motion",
578 G_CALLBACK (target_drag_motion
), this);
580 g_signal_connect (widget
, "drag_drop",
581 G_CALLBACK (target_drag_drop
), this);
583 g_signal_connect (widget
, "drag_data_received",
584 G_CALLBACK (target_drag_data_received
), this);
587 //----------------------------------------------------------------------------
589 //----------------------------------------------------------------------------
593 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
594 GdkDragContext
*WXUNUSED(context
),
595 GtkSelectionData
*selection_data
,
596 guint
WXUNUSED(info
),
597 guint
WXUNUSED(time
),
598 wxDropSource
*drop_source
)
600 if (g_isIdle
) wxapp_install_idle_handler();
602 wxDataFormat
format( selection_data
->target
);
604 wxLogTrace(TRACE_DND
, wxT("Drop source: format requested: %s"),
605 format
.GetId().c_str());
607 drop_source
->m_retValue
= wxDragCancel
;
609 wxDataObject
*data
= drop_source
->GetDataObject();
613 wxLogTrace(TRACE_DND
, wxT("Drop source: no data object") );
617 if (!data
->IsSupportedFormat(format
))
619 wxLogTrace(TRACE_DND
, wxT("Drop source: unsupported format") );
623 if (data
->GetDataSize(format
) == 0)
625 wxLogTrace(TRACE_DND
, wxT("Drop source: empty data") );
629 size_t size
= data
->GetDataSize(format
);
631 // printf( "data size: %d.\n", (int)data_size );
633 guchar
*d
= new guchar
[size
];
635 if (!data
->GetDataHere( format
, (void*)d
))
642 /* disable GUI threads */
645 gtk_selection_data_set( selection_data
,
646 selection_data
->target
,
652 /* enable GUI threads */
659 //----------------------------------------------------------------------------
660 // "drag_data_delete"
661 //----------------------------------------------------------------------------
664 static void source_drag_data_delete( GtkWidget
*WXUNUSED(widget
),
665 GdkDragContext
*WXUNUSED(context
),
666 wxDropSource
*WXUNUSED(drop_source
) )
669 wxapp_install_idle_handler();
671 // printf( "Drag source: drag_data_delete\n" );
675 //----------------------------------------------------------------------------
677 //----------------------------------------------------------------------------
680 static void source_drag_begin( GtkWidget
*WXUNUSED(widget
),
681 GdkDragContext
*WXUNUSED(context
),
682 wxDropSource
*WXUNUSED(drop_source
) )
685 wxapp_install_idle_handler();
687 // printf( "Drag source: drag_begin.\n" );
691 //----------------------------------------------------------------------------
693 //----------------------------------------------------------------------------
696 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
697 GdkDragContext
*WXUNUSED(context
),
698 wxDropSource
*drop_source
)
700 if (g_isIdle
) wxapp_install_idle_handler();
702 // printf( "Drag source: drag_end.\n" );
704 drop_source
->m_waiting
= false;
708 //-----------------------------------------------------------------------------
709 // "configure_event" from m_iconWindow
710 //-----------------------------------------------------------------------------
714 gtk_dnd_window_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDropSource
*source
)
716 // don't need to install idle handler, its done from "event" signal
718 source
->GiveFeedback( ConvertFromGTK(source
->m_dragContext
->action
) );
724 //---------------------------------------------------------------------------
726 //---------------------------------------------------------------------------
728 wxDropSource::wxDropSource(wxWindow
*win
,
729 const wxIcon
&iconCopy
,
730 const wxIcon
&iconMove
,
731 const wxIcon
&iconNone
)
735 m_iconWindow
= (GtkWidget
*) NULL
;
738 m_widget
= win
->m_widget
;
739 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
741 m_retValue
= wxDragCancel
;
743 SetIcons(iconCopy
, iconMove
, iconNone
);
746 wxDropSource::wxDropSource(wxDataObject
& data
,
748 const wxIcon
&iconCopy
,
749 const wxIcon
&iconMove
,
750 const wxIcon
&iconNone
)
756 m_iconWindow
= (GtkWidget
*) NULL
;
759 m_widget
= win
->m_widget
;
760 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
762 m_retValue
= wxDragCancel
;
764 SetIcons(iconCopy
, iconMove
, iconNone
);
767 void wxDropSource::SetIcons(const wxIcon
&iconCopy
,
768 const wxIcon
&iconMove
,
769 const wxIcon
&iconNone
)
771 m_iconCopy
= iconCopy
;
772 m_iconMove
= iconMove
;
773 m_iconNone
= iconNone
;
775 if ( !m_iconCopy
.Ok() )
776 m_iconCopy
= wxIcon(page_xpm
);
777 if ( !m_iconMove
.Ok() )
778 m_iconMove
= m_iconCopy
;
779 if ( !m_iconNone
.Ok() )
780 m_iconNone
= m_iconCopy
;
783 wxDropSource::~wxDropSource()
787 void wxDropSource::PrepareIcon( int action
, GdkDragContext
*context
)
789 // get the right icon to display
791 if ( action
& GDK_ACTION_MOVE
)
793 else if ( action
& GDK_ACTION_COPY
)
799 if ( icon
->GetMask() )
800 mask
= icon
->GetMask()->GetBitmap();
802 mask
= (GdkBitmap
*)NULL
;
804 GdkPixmap
*pixmap
= icon
->GetPixmap();
807 gdk_drawable_get_size (pixmap
, &width
, &height
);
809 GdkColormap
*colormap
= gtk_widget_get_colormap( m_widget
);
810 gtk_widget_push_colormap (colormap
);
812 m_iconWindow
= gtk_window_new (GTK_WINDOW_POPUP
);
813 gtk_widget_set_events (m_iconWindow
, GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
);
814 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow
), TRUE
);
816 gtk_widget_pop_colormap ();
818 gtk_widget_set_size_request (m_iconWindow
, width
, height
);
819 gtk_widget_realize (m_iconWindow
);
821 g_signal_connect (m_iconWindow
, "configure_event",
822 G_CALLBACK (gtk_dnd_window_configure_callback
), this);
824 gdk_window_set_back_pixmap (m_iconWindow
->window
, pixmap
, FALSE
);
827 gtk_widget_shape_combine_mask (m_iconWindow
, mask
, 0, 0);
829 gtk_drag_set_icon_widget( context
, m_iconWindow
, 0, 0 );
832 wxDragResult
wxDropSource::DoDragDrop(int flags
)
834 wxCHECK_MSG( m_data
&& m_data
->GetFormatCount(), wxDragNone
,
835 wxT("Drop source: no data") );
838 if (g_blockEventsOnDrag
)
841 // don't start dragging if no button is down
842 if (g_lastButtonNumber
== 0)
845 // we can only start a drag after a mouse event
846 if (g_lastMouseEvent
== NULL
)
850 g_blockEventsOnDrag
= true;
856 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
858 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
859 m_data
->GetAllFormats( array
);
860 size_t count
= m_data
->GetFormatCount();
861 for (size_t i
= 0; i
< count
; i
++)
863 GdkAtom atom
= array
[i
];
864 wxLogTrace(TRACE_DND
, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom
));
865 gtk_target_list_add( target_list
, atom
, 0, 0 );
869 int action
= GDK_ACTION_COPY
;
870 if ( flags
& wxDrag_AllowMove
)
871 action
|= GDK_ACTION_MOVE
;
873 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
874 // to use a global to pass the flags to the drop target but I'd
875 // surely prefer a better way to do it
876 gs_flagsForDrag
= flags
;
878 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
880 (GdkDragAction
)action
,
881 g_lastButtonNumber
, // number of mouse button which started drag
882 (GdkEvent
*) g_lastMouseEvent
);
884 m_dragContext
= context
;
886 PrepareIcon( action
, context
);
889 gtk_main_iteration();
891 m_retValue
= ConvertFromGTK(context
->action
);
892 if ( m_retValue
== wxDragNone
)
893 m_retValue
= wxDragCancel
;
895 g_blockEventsOnDrag
= false;
902 void wxDropSource::RegisterWindow()
904 if (!m_widget
) return;
906 g_signal_connect (m_widget
, "drag_data_get",
907 G_CALLBACK (source_drag_data_get
), this);
908 g_signal_connect (m_widget
, "drag_data_delete",
909 G_CALLBACK (source_drag_data_delete
), this);
910 g_signal_connect (m_widget
, "drag_begin",
911 G_CALLBACK (source_drag_begin
), this);
912 g_signal_connect (m_widget
, "drag_end",
913 G_CALLBACK (source_drag_end
), this);
917 void wxDropSource::UnregisterWindow()
922 g_signal_handlers_disconnect_by_func (m_widget
,
923 (gpointer
) source_drag_data_get
,
925 g_signal_handlers_disconnect_by_func (m_widget
,
926 (gpointer
) source_drag_data_delete
,
928 g_signal_handlers_disconnect_by_func (m_widget
,
929 (gpointer
) source_drag_begin
,
931 g_signal_handlers_disconnect_by_func (m_widget
,
932 (gpointer
) source_drag_end
,
937 // wxUSE_DRAG_AND_DROP