]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/dnd.cpp
wxGTK's built-in dockable menu now give their
[wxWidgets.git] / src / gtk / dnd.cpp
index 00f4e0d90ab246f3190b35240bc29f609d1f320d..7ffdb34a1166cf3dc3f8f5a5d92df0d463f4eb1f 100644 (file)
@@ -128,6 +128,7 @@ static char * page_xpm[] = {
 #include "gtk/gtkdnd.h"
 #include "gtk/gtkselection.h"
 
+
 // ----------------------------------------------------------------------------
 // "drag_leave"
 // ----------------------------------------------------------------------------
@@ -210,6 +211,8 @@ static gboolean target_drag_drop( GtkWidget *widget,
        return FALSE, otherwise, if you aren't accepting
        the drop, call gtk_drag_finish() with success == FALSE
        otherwise call gtk_drag_data_get()" */
+
+//    printf( "drop.\n" );
        
     /* this seems to make a difference between not accepting
        due to wrong target area and due to wrong format. let
@@ -229,15 +232,7 @@ static gboolean target_drag_drop( GtkWidget *widget,
     
     bool ret = drop_target->OnDrop( x, y );
     
-    if (ret)
-    {
-        /* this should trigger an "drag_data_received" event */
-        gtk_drag_get_data( widget, 
-                          context, 
-                          GPOINTER_TO_INT (context->targets->data), 
-                          time );
-    }
-    else
+    if (!ret)
     {
         /* cancel the whole thing */
         gtk_drag_finish( context,
@@ -274,61 +269,47 @@ static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
     /* Owen Taylor: "call gtk_drag_finish() with
        success == TRUE" */
 
-    /* strangely, we get a "drag_data_received" event even when
-       we don't request them. this checks this. */
-    if (!drop_target->m_currentDataObject) return;
-    
-    wxDataObject *data_object = drop_target->m_currentDataObject;
-
+//    printf( "data received.\n" );
+       
     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;
     }
-    else
+    
+    /* inform the wxDropTarget about the current GtkSelectionData.
+       this is only valid for the duration of this call */
+    drop_target->SetDragData( data );
+    
+    if (drop_target->OnData( x, y ))
     {
-        wxASSERT_MSG( data->target ==  data_object->GetFormat().GetAtom(), "DnD GetData target mismatch."  );
-       
-       if (data_object->GetFormat().GetType() == wxDF_TEXT)
-       {
-           wxTextDataObject *text_object = (wxTextDataObject*)data_object;
-           text_object->SetText( (const char*)data->data );
-       } else
-       
-       if (data_object->GetFormat().GetType() == wxDF_FILENAME)
-       {
-       } else
-       
-       if (data_object->GetFormat().GetType() == wxDF_PRIVATE)
-       {
-           wxPrivateDataObject *priv_object = (wxPrivateDataObject*)data_object;
-           priv_object->SetData( (const char*)data->data, (size_t)data->length );
-       }
-       
-       /* tell wxDropTarget that data transfer was successfull */
-       drop_target->m_dataRetrieveSuccess = TRUE;
-       
        /* tell GTK that data transfer was successfull */
         gtk_drag_finish( context, TRUE, FALSE, time );
     }
-
-    /* tell wxDropTarget that data has arrived (or not) */
-    drop_target->m_waiting = FALSE;
+    else
+    {
+       /* 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()
 {
     m_firstMotion = TRUE;
     m_dragContext = (GdkDragContext*) NULL;
     m_dragWidget = (GtkWidget*) NULL;
+    m_dragData = (GtkSelectionData*) NULL;
     m_dragTime = 0;
-    m_currentDataObject = (wxDataObject*) NULL;
-    m_dataRetrieveSuccess = FALSE;
 }
 
 wxDropTarget::~wxDropTarget()
@@ -353,6 +334,25 @@ bool wxDropTarget::OnDrop( int WXUNUSED(x), int WXUNUSED(y) )
     return FALSE;
 }
 
+bool wxDropTarget::OnData( int WXUNUSED(x), int WXUNUSED(y) )
+{
+    return FALSE;
+}
+
+bool wxDropTarget::RequestData( wxDataFormat format )
+{
+    if (!m_dragContext) return FALSE;
+    if (!m_dragWidget) return FALSE;
+    
+    /* this should trigger an "drag_data_received" event */
+    gtk_drag_get_data( m_dragWidget, 
+                      m_dragContext,
+                      format.GetAtom(),
+                      m_dragTime );
+                      
+    return TRUE;
+}
+
 bool wxDropTarget::IsSupported( wxDataFormat format )
 { 
     if (!m_dragContext) return FALSE;
@@ -362,8 +362,8 @@ bool wxDropTarget::IsSupported( wxDataFormat format )
     {
         GdkAtom formatAtom = (GdkAtom) GPOINTER_TO_INT(child->data);
   
-/*        char *name = gdk_atom_name( formatAtom );
-        if (name) printf( "Format available: %s.\n", name ); */
+//        char *name = gdk_atom_name( formatAtom );
+//        if (name) printf( "Format available: %s.\n", name );
   
         if (formatAtom == format.GetAtom()) return TRUE;
         child = child->next;
@@ -372,27 +372,29 @@ bool wxDropTarget::IsSupported( wxDataFormat format )
     return FALSE;
 }
   
-bool wxDropTarget::GetData( wxDataObject *data )
+bool wxDropTarget::GetData( wxDataObject *data_object )
 {
-    if (!m_dragContext) return FALSE;
-    if (!m_dragWidget) return FALSE;
-    
-    m_currentDataObject = data;
-    m_dataRetrieveSuccess = FALSE;
-    
-    /* this should trigger an "drag_data_received" event */
-    gtk_drag_get_data( m_dragWidget, 
-                      m_dragContext,
-                      data->GetFormat().GetAtom(),
-                      m_dragTime );
-                      
-    /* wait for the "drag_data_received" event */
-    m_waiting = TRUE;
-    while (m_waiting) gtk_main_iteration();
+    if (!m_dragData) return FALSE;
     
-    m_currentDataObject = (wxDataObject*) NULL;
+    if (m_dragData->target !=  data_object->GetFormat().GetAtom()) return FALSE;
+       
+    if (data_object->GetFormat().GetType() == wxDF_TEXT)
+    {
+        wxTextDataObject *text_object = (wxTextDataObject*)data_object;
+        text_object->SetText( (const char*)m_dragData->data );
+    } else
+       
+    if (data_object->GetFormat().GetType() == wxDF_FILENAME)
+    {
+    } else
+       
+    if (data_object->GetFormat().GetType() == wxDF_PRIVATE)
+    {
+       wxPrivateDataObject *priv_object = (wxPrivateDataObject*)data_object;
+       priv_object->SetData( (const char*)m_dragData->data, (size_t)m_dragData->length );
+    }
     
-    return m_dataRetrieveSuccess;
+    return TRUE;
 }
   
 void wxDropTarget::UnregisterWidget( GtkWidget *widget )
@@ -453,17 +455,27 @@ void wxDropTarget::RegisterWidget( GtkWidget *widget )
 
 bool wxTextDropTarget::OnMove( int WXUNUSED(x), int WXUNUSED(y) )
 {
-    return IsSupported( wxDF_TEXT );  // same as "STRING"
+    return IsSupported( wxDF_TEXT );
 }
 
-bool wxTextDropTarget::OnDrop( int x, int y )
+bool wxTextDropTarget::OnDrop( int WXUNUSED(x), int WXUNUSED(y) )
 {
-    if (!IsSupported( wxDF_TEXT )) return FALSE;
+    if (IsSupported( wxDF_TEXT ))
+    {
+        RequestData( wxDF_TEXT );
+       return TRUE;
+    }
     
+    return FALSE;
+}
+
+bool wxTextDropTarget::OnData( int x, int y )
+{
     wxTextDataObject data;
     if (!GetData( &data )) return FALSE;
     
     OnDropText( x, y, data.GetText() );
+    
     return TRUE;
 }
 
@@ -486,7 +498,18 @@ bool wxPrivateDropTarget::OnMove( int WXUNUSED(x), int WXUNUSED(y) )
     return IsSupported( m_id );
 }
 
-bool wxPrivateDropTarget::OnDrop( int x, int y )
+bool wxPrivateDropTarget::OnDrop( int WXUNUSED(x), int WXUNUSED(y) )
+{
+    if (!IsSupported( m_id ))
+    { 
+        RequestData( m_id );
+        return FALSE;
+    }
+    
+    return FALSE;
+}
+
+bool wxPrivateDropTarget::OnData( int x, int y )
 {
     if (!IsSupported( m_id )) return FALSE;
     
@@ -504,18 +527,24 @@ bool wxPrivateDropTarget::OnDrop( int x, int y )
 
 bool wxFileDropTarget::OnMove( int WXUNUSED(x), int WXUNUSED(y) )
 {
-    return IsSupported( wxDF_FILENAME );  // same as "file:ALL"
+    return IsSupported( wxDF_FILENAME );
 }
 
 bool wxFileDropTarget::OnDrop( int x, int y )
 {
-    return IsSupported( wxDF_FILENAME );  // same as "file:ALL"
+    if (IsSupported( wxDF_FILENAME ))
+    {
+        RequestData( wxDF_FILENAME );
+       return TRUE;
+    }
+    
+    return FALSE;
 }
 
-void wxFileDropTarget::OnData( int x, int y )
+bool wxFileDropTarget::OnData( int x, int y )
 {
     wxFileDataObject data;
-    if (!GetData( &data )) return;
+    if (!GetData( &data )) return FALSE;
 
     /* get number of substrings /root/mytext.txt/0/root/myothertext.txt/0/0 */
     size_t number = 0;
@@ -525,7 +554,7 @@ void wxFileDropTarget::OnData( int x, int y )
     for ( i = 0; i < size; i++)
         if (text[i] == 0) number++;
 
-    if (number == 0) return;
+    if (number == 0) return FALSE;
     
     char **files = new char*[number];
   
@@ -540,16 +569,103 @@ void wxFileDropTarget::OnData( int x, int y )
     OnDropFiles( x, y, number, files ); 
   
     free( files );
+    
+    return TRUE;
 }
 
-//-------------------------------------------------------------------------
-// wxDropSource
-//-------------------------------------------------------------------------
+//----------------------------------------------------------------------------
+// "drag_data_get"
+//----------------------------------------------------------------------------
 
+static void  
+source_drag_data_get  (GtkWidget          *WXUNUSED(widget),
+                      GdkDragContext     *context,
+                      GtkSelectionData   *selection_data,
+                      guint               WXUNUSED(info),
+                      guint               WXUNUSED(time),
+                      wxDropSource       *drop_source )
+{
+//    char *name = gdk_atom_name( selection_data->target );
+//    if (name) printf( "Format requested: %s.\n", name );
+    
+    wxNode *node = drop_source->m_data->m_dataObjects.First();
+    while (node)
+    {
+        wxDataObject *data_object = (wxDataObject*) node->Data();
+       if (data_object->GetFormat().GetAtom() == selection_data->target)
+       {
+           size_t data_size = data_object->GetSize();
+           if (data_size > 0)
+           {
+               guchar *buffer = new guchar[data_size];
+               data_object->WriteData( buffer );
+               
+                gtk_selection_data_set( selection_data,
+                                       selection_data->target,
+                                       8,   /* 8-bit */
+                                       buffer, 
+                                       data_size );
+                                       
+               free( buffer );
+               
+               /* so far only copy, no moves. TODO. */
+                drop_source->m_retValue = wxDragCopy;
+           
+               return;
+           }
+       }
+    
+        node = node->Next();
+    }
+    
+    drop_source->m_retValue = wxDragCancel;
+}
+  
+//----------------------------------------------------------------------------
+// "drag_data_delete"
+//----------------------------------------------------------------------------
+
+static void source_drag_data_delete( GtkWidget          *WXUNUSED(widget),
+                                    GdkDragContext     *WXUNUSED(context),
+                                    wxDropSource       *drop_source )
+{
+//    printf( "Delete the data!\n" );
+
+    drop_source->m_retValue = wxDragMove;
+}
+  
+//----------------------------------------------------------------------------
+// "drag_begin"
+//----------------------------------------------------------------------------
+
+static void source_drag_begin( GtkWidget          *WXUNUSED(widget),
+                              GdkDragContext     *WXUNUSED(context),
+                              wxDropSource       *WXUNUSED(drop_source) )
+{
+//    printf( "drag_begin.\n" );
+}
+  
+//----------------------------------------------------------------------------
+// "drag_end"
+//----------------------------------------------------------------------------
+
+static void source_drag_end( GtkWidget          *WXUNUSED(widget),
+                            GdkDragContext     *WXUNUSED(context),
+                            wxDropSource       *drop_source )
+{
+//    printf( "drag_end.\n" );
+
+    drop_source->m_waiting = FALSE;
+}
+  
+//---------------------------------------------------------------------------
+// wxDropSource
+//---------------------------------------------------------------------------
 
 wxDropSource::wxDropSource( wxWindow *win, const wxIcon &go, const wxIcon &stop )
 {
     g_blockEventsOnDrag = TRUE;
+    m_waiting = TRUE;
   
     m_window = win;
     m_widget = win->m_widget;
@@ -569,7 +685,7 @@ wxDropSource::wxDropSource( wxWindow *win, const wxIcon &go, const wxIcon &stop
 
 wxDropSource::wxDropSource( wxDataObject *data, wxWindow *win, const wxIcon &go, const wxIcon &stop )
 {
-    g_blockEventsOnDrag = TRUE;
+    m_waiting = TRUE;
   
     m_window = win;
     m_widget = win->m_widget;
@@ -597,8 +713,6 @@ wxDropSource::wxDropSource( wxDataObject *data, wxWindow *win, const wxIcon &go,
 
 wxDropSource::wxDropSource( wxDataBroker *data, wxWindow *win )
 {
-    g_blockEventsOnDrag = TRUE;
-  
     m_window = win;
     m_widget = win->m_widget;
     if (win->m_wxwindow) m_widget = win->m_wxwindow;
@@ -645,53 +759,91 @@ wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) )
   
     if (!m_data) return (wxDragResult) wxDragNone;
   
-    static GtkWidget *drag_icon = (GtkWidget*) NULL;
-    static GtkWidget *drop_icon = (GtkWidget*) NULL;
+    g_blockEventsOnDrag = TRUE;
+  
+    RegisterWindow();
+    
+    m_waiting = TRUE;
 
-    GdkPoint hotspot_1 = {0,-5 };
-      
-    if (!drag_icon)
+    GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
+    gtk_target_list_add( target_list, gdk_atom_intern( "STRING", FALSE ), 0, 0 );
+    
+    GdkEventMotion event;
+    event.window = m_widget->window;
+    int x = 0;
+    int y = 0;
+    GdkModifierType state;
+    gdk_window_get_pointer( event.window, &x, &y, &state );
+    event.x = x;
+    event.y = y;
+    event.state = state;
+    
+    /* GTK wants to know which button was pressed which caused the dragging */
+    int button_number = 0;
+    if (event.state & GDK_BUTTON1_MASK) button_number = 1;
+    else if (event.state & GDK_BUTTON2_MASK) button_number = 2;
+    else if (event.state & GDK_BUTTON3_MASK) button_number = 3;
+
+    /* don't start dragging if no button is down */
+    if (button_number)
     {
-/*
-         drag_icon = shape_create_icon ( m_stopIcon,
-                                        440, 140, 0,0, GTK_WINDOW_POPUP);
-         
-         gtk_signal_connect (GTK_OBJECT (drag_icon), "destroy",
-                             GTK_SIGNAL_FUNC(gtk_widget_destroyed),
-                             &drag_icon);
-
-         gtk_widget_hide (drag_icon);
-*/
-    }
-      
-    GdkPoint hotspot_2 = {-5,-5};
+        GdkDragContext *context = gtk_drag_begin( m_widget,
+                                              target_list,
+                                             GDK_ACTION_COPY,
+                                             button_number,  /* number of mouse button which started drag */
+                                             (GdkEvent*) &event );
        
-    if (!drop_icon)
-    {
-/*
-         drop_icon = shape_create_icon ( m_goIcon,
-                                        440, 140, 0,0, GTK_WINDOW_POPUP);
-         
-         gtk_signal_connect (GTK_OBJECT (drop_icon), "destroy",
-                             GTK_SIGNAL_FUNC(gtk_widget_destroyed),
-                             &drop_icon);
-
-         gtk_widget_hide (drop_icon);
-*/
-    }
+        wxMask *mask = m_goIcon.GetMask();
+        GdkBitmap *bm = (GdkBitmap *) NULL;
+        if (mask) bm = mask->GetBitmap();
+       GdkPixmap *pm = m_goIcon.GetPixmap();
        
+        gtk_drag_set_icon_pixmap( context,
+                                 gtk_widget_get_colormap( m_widget ),
+                                 pm,
+                                 bm,
+                                 0,
+                                 0 );
+    
+        gdk_flush();
+    
+        while (m_waiting) wxYield();
+    }
 
-    return FALSE;
+    g_blockEventsOnDrag = FALSE;
+    
+    UnregisterWindow();
+
+    return m_retValue;
 }
 
-void wxDropSource::RegisterWindow(void)
+void wxDropSource::RegisterWindow()
 {
-    if (!m_data) return;
+    if (!m_widget) return;
+    
+    gtk_signal_connect( GTK_OBJECT(m_widget), "drag_data_get",
+                     GTK_SIGNAL_FUNC (source_drag_data_get), (gpointer) this);
+    gtk_signal_connect (GTK_OBJECT(m_widget), "drag_data_delete",
+                     GTK_SIGNAL_FUNC (source_drag_data_delete),  (gpointer) this );
+    gtk_signal_connect (GTK_OBJECT(m_widget), "drag_begin",
+                     GTK_SIGNAL_FUNC (source_drag_begin),  (gpointer) this );
+    gtk_signal_connect (GTK_OBJECT(m_widget), "drag_end",
+                     GTK_SIGNAL_FUNC (source_drag_end),  (gpointer) this );
+
 }
 
-void wxDropSource::UnregisterWindow(void)
+void wxDropSource::UnregisterWindow()
 {
     if (!m_widget) return;
+    
+    gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+                     GTK_SIGNAL_FUNC(source_drag_data_get), (gpointer) this );
+    gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+                     GTK_SIGNAL_FUNC(source_drag_data_delete), (gpointer) this );
+    gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+                     GTK_SIGNAL_FUNC(source_drag_begin), (gpointer) this );
+    gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+                     GTK_SIGNAL_FUNC(source_drag_end), (gpointer) this );
 }