+static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ GtkSelectionData *data,
+ guint WXUNUSED(info),
+ guint time,
+ wxDropTarget *drop_target )
+{
+ if (g_isIdle) wxapp_install_idle_handler();
+
+ /* Owen Taylor: "call gtk_drag_finish() with
+ success == TRUE" */
+
+ if ((data->length <= 0) || (data->format != 8))
+ {
+ /* negative data length and non 8-bit data format
+ qualifies for junk */
+ gtk_drag_finish (context, FALSE, FALSE, time);
+
+ return;
+ }
+
+ wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
+
+ /* inform the wxDropTarget about the current GtkSelectionData.
+ this is only valid for the duration of this call */
+ drop_target->SetDragData( data );
+
+ wxDragResult result = ConvertFromGTK(context->suggested_action);
+
+ if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
+ {
+ wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned TRUE") );
+
+ /* tell GTK that data transfer was successfull */
+ gtk_drag_finish( context, TRUE, FALSE, time );
+ }
+ else
+ {
+ wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
+
+ /* tell GTK that data transfer was not successfull */
+ gtk_drag_finish( context, FALSE, FALSE, time );
+ }
+
+ /* after this, invalidate the drop_target's drag data */
+ drop_target->SetDragData( (GtkSelectionData*) NULL );
+}
+
+//----------------------------------------------------------------------------
+// wxDropTarget
+//----------------------------------------------------------------------------
+
+wxDropTarget::wxDropTarget( wxDataObject *data )
+ : wxDropTargetBase( data )
+{
+ m_firstMotion = TRUE;
+ m_dragContext = (GdkDragContext*) NULL;
+ m_dragWidget = (GtkWidget*) NULL;
+ m_dragData = (GtkSelectionData*) NULL;
+ m_dragTime = 0;
+}
+
+wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
+ wxCoord WXUNUSED(y),
+ wxDragResult def )
+{
+ // GetMatchingPair() checks for m_dataObject too, no need to do it here
+
+ // disable the debug message from GetMatchingPair() - there are too many
+ // of them otherwise
+#ifdef __WXDEBUG__
+ wxLogNull noLog;
+#endif // Debug
+
+ return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
+}
+
+bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
+{
+ if (!m_dataObject)
+ return FALSE;
+
+ return (GetMatchingPair() != (GdkAtom) 0);
+}
+
+wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
+ wxDragResult def )
+{
+ if (!m_dataObject)
+ return wxDragNone;
+
+ if (GetMatchingPair() == (GdkAtom) 0)
+ return wxDragNone;
+
+ return GetData() ? def : wxDragNone;
+}
+
+GdkAtom wxDropTarget::GetMatchingPair()
+{
+ if (!m_dataObject)
+ return (GdkAtom) 0;
+
+ if (!m_dragContext)
+ return (GdkAtom) 0;
+
+ GList *child = m_dragContext->targets;
+ while (child)
+ {
+ GdkAtom formatAtom = (GdkAtom) GPOINTER_TO_INT(child->data);
+ wxDataFormat format( formatAtom );
+
+#ifdef __WXDEBUG__
+ wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
+ format.GetId().c_str());
+#endif // Debug
+
+ if (m_dataObject->IsSupportedFormat( format ))
+ return formatAtom;
+
+ child = child->next;
+ }
+
+ return (GdkAtom) 0;
+}
+
+bool wxDropTarget::GetData()
+{
+ if (!m_dragData)
+ return FALSE;
+
+ if (!m_dataObject)
+ return FALSE;
+
+ wxDataFormat dragFormat( m_dragData->target );
+
+ if (!m_dataObject->IsSupportedFormat( dragFormat ))
+ return FALSE;
+
+ m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
+
+ return TRUE;
+}
+
+void wxDropTarget::UnregisterWidget( GtkWidget *widget )
+{
+ wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
+
+ gtk_drag_dest_unset( widget );
+
+ gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
+ GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
+
+ gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
+ GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
+
+ gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
+ GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
+
+ gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
+ GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
+}
+
+void wxDropTarget::RegisterWidget( GtkWidget *widget )
+{
+ wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
+
+ /* gtk_drag_dest_set() determines what default behaviour we'd like
+ GTK to supply. we don't want to specify out targets (=formats)
+ or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
+ not GTK_DEST_DEFAULT_DROP). instead we react individually to
+ "drag_motion" and "drag_drop" events. this makes it possible
+ to allow dropping on only a small area. we should set
+ GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
+ highlighting if dragging over standard controls, but this
+ seems to be broken without the other two. */
+
+ gtk_drag_dest_set( widget,
+ (GtkDestDefaults) 0, /* no default behaviour */
+ (GtkTargetEntry*) NULL, /* we don't supply any formats here */
+ 0, /* number of targets = 0 */
+ (GdkDragAction) 0 ); /* we don't supply any actions here */
+
+ gtk_signal_connect( GTK_OBJECT(widget), "drag_leave",
+ GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
+
+ gtk_signal_connect( GTK_OBJECT(widget), "drag_motion",
+ GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
+
+ gtk_signal_connect( GTK_OBJECT(widget), "drag_drop",
+ GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
+
+ gtk_signal_connect( GTK_OBJECT(widget), "drag_data_received",
+ GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
+}
+
+//----------------------------------------------------------------------------
+// "drag_data_get"
+//----------------------------------------------------------------------------
+
+static void
+source_drag_data_get (GtkWidget *WXUNUSED(widget),
+ GdkDragContext *WXUNUSED(context),
+ GtkSelectionData *selection_data,
+ guint WXUNUSED(info),
+ guint WXUNUSED(time),
+ wxDropSource *drop_source )
+{
+ if (g_isIdle) wxapp_install_idle_handler();
+
+ wxDataFormat format( selection_data->target );
+
+ wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
+ format.GetId().c_str());
+
+ drop_source->m_retValue = wxDragCancel;
+
+ wxDataObject *data = drop_source->GetDataObject();
+
+ if (!data)
+ {
+ wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
+ return;
+ }
+
+ if (!data->IsSupportedFormat(format))
+ {
+ wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
+ return;
+ }
+
+ if (data->GetDataSize(format) == 0)
+ {
+ wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
+ return;
+ }
+
+ size_t size = data->GetDataSize(format);
+
+// printf( "data size: %d.\n", (int)data_size );
+
+ guchar *d = new guchar[size];
+
+ if (!data->GetDataHere( format, (void*)d ))
+ {
+ delete[] d;
+ return;
+ }
+
+#if wxUSE_THREADS
+ /* disable GUI threads */
+#endif
+
+ gtk_selection_data_set( selection_data,
+ selection_data->target,
+ 8, // 8-bit
+ d,
+ size );
+
+#if wxUSE_THREADS
+ /* enable GUI threads */
+#endif
+
+ delete[] d;
+}
+
+//----------------------------------------------------------------------------
+// "drag_data_delete"
+//----------------------------------------------------------------------------
+
+static void source_drag_data_delete( GtkWidget *WXUNUSED(widget),
+ GdkDragContext *context,
+ wxDropSource *WXUNUSED(drop_source) )