]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/clipbrd.cpp
*** empty log message ***
[wxWidgets.git] / src / gtk / clipbrd.cpp
index 023cdad83c1c47a67c6fc5bf905b7b5236782ef6..229cfd3e13d5acc162d74ca0e99c19893ad9fb38 100644 (file)
@@ -36,8 +36,6 @@ extern void wxapp_uninstall_thread_wakeup();
 // data
 //-----------------------------------------------------------------------------
 
-wxClipboard *wxTheClipboard = (wxClipboard*) NULL;
-
 GdkAtom  g_clipboardAtom   = 0;
 GdkAtom  g_targetsAtom     = 0;
 
@@ -96,6 +94,11 @@ targets_selection_received( GtkWidget *WXUNUSED(widget),
         clipboard->m_waiting = FALSE;
         return;
     }
+    
+/*
+    wxDataFormat clip( selection_data->selection );
+    wxLogDebug( wxT("selection received for targets, clipboard %s"), clip.GetId().c_str() );
+*/
   
     // the atoms we received, holding a list of targets (= formats) 
     GdkAtom *atoms = (GdkAtom *)selection_data->data;
@@ -103,7 +106,11 @@ targets_selection_received( GtkWidget *WXUNUSED(widget),
     for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
     {
         wxDataFormat format( atoms[i] );
-      
+       
+/*
+       wxLogDebug( wxT("selection received for targets, format %s"), format.GetId().c_str() );
+*/
+
         if (format == clipboard->m_targetRequested)
         {
             clipboard->m_waiting = FALSE;
@@ -201,6 +208,8 @@ selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
         /* the clipboard is no longer in our hands. we can the delete clipboard data. */
         if (wxTheClipboard->m_data)
         {
+           wxLogDebug( wxT("wxClipboard will get cleared" ) );
+           
             delete wxTheClipboard->m_data;
             wxTheClipboard->m_data = (wxDataObject*) NULL;
         }
@@ -339,7 +348,8 @@ void wxClipboard::Clear()
         {
             m_waiting = TRUE;
             
-            gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME );
+            gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
+                                     (guint32) GDK_CURRENT_TIME );
             
             while (m_waiting) gtk_main_iteration();
         }
@@ -348,7 +358,8 @@ void wxClipboard::Clear()
         {
             m_waiting = TRUE;
             
-            gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME );
+            gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
+                                     (guint32) GDK_CURRENT_TIME );
             
             while (m_waiting) gtk_main_iteration();
         }
@@ -400,32 +411,23 @@ bool wxClipboard::AddData( wxDataObject *data )
   
     m_data = data;
 
-    /* This should happen automatically, but to be on the safe side */
-    m_ownsClipboard = FALSE;
-    m_ownsPrimarySelection = FALSE;
-    
     /* get formats from wxDataObjects */
     wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
     m_data->GetAllFormats( array );
+
+    /* primary selection or clipboard */
+    GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
+                                     : g_clipboardAtom;
+
     
     for (size_t i = 0; i < m_data->GetFormatCount(); i++)
     {
-        GdkAtom atom = array[i];
-        wxLogDebug( wxT("Clipboard Supported atom %s"), gdk_atom_name( atom ) );
-
-        /* Add handlers if someone requests data. We currently always
-           offer data to the clipboard and the primary selection. Maybe
-           we should make that depend on the usePrimary flag */
-           
-        gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), 
-                              GDK_SELECTION_PRIMARY,
-                              atom, 
-                              0 );  /* what is info ? */
-                             
-        gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), 
-                              g_clipboardAtom,
-                              atom, 
-                              0 );  /* what is info ? */
+        wxLogDebug( wxT("wxClipboard now supports atom %s"), array[i].GetId().c_str() );
+
+        gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
+                                 clipboard,
+                                  array[i], 
+                                  0 );  /* what is info ? */
     }
 
     delete[] array;
@@ -436,41 +438,26 @@ bool wxClipboard::AddData( wxDataObject *data )
                         (gpointer) NULL );
 
 #if wxUSE_THREADS
-        /* disable GUI threads */
-        wxapp_uninstall_thread_wakeup();
+    /* disable GUI threads */
+    wxapp_uninstall_thread_wakeup();
 #endif
     
     /* Tell the world we offer clipboard data */
-    if (!gtk_selection_owner_set( m_clipboardWidget, 
-                                  g_clipboardAtom,
-                                  GDK_CURRENT_TIME ))
-    {
-#if wxUSE_THREADS
-        /* re-enable GUI threads */
-        wxapp_install_thread_wakeup();
-#endif
-        return FALSE;
-    }
-    m_ownsClipboard = TRUE;
+    bool res = (gtk_selection_owner_set( m_clipboardWidget,
+                                         clipboard,
+                                         (guint32) GDK_CURRENT_TIME ));
     
-    if (!gtk_selection_owner_set( m_clipboardWidget, 
-                                  GDK_SELECTION_PRIMARY,
-                                  GDK_CURRENT_TIME ))
-    {  
-#if wxUSE_THREADS
-        /* re-enable GUI threads */
-        wxapp_install_thread_wakeup();
-#endif
-        return FALSE;
-    }
-    m_ownsPrimarySelection = TRUE;
+    if (m_usePrimary)
+        m_ownsPrimarySelection = res;
+    else
+        m_ownsClipboard = res;
     
 #if wxUSE_THREADS
     /* re-enable GUI threads */
     wxapp_install_thread_wakeup();
 #endif
         
-    return TRUE;
+    return res;
 }
 
 void wxClipboard::Close()
@@ -511,7 +498,7 @@ bool wxClipboard::IsSupported( const wxDataFormat& format )
                            m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
                                         : g_clipboardAtom, 
                            g_targetsAtom,
-                           GDK_CURRENT_TIME );
+                           (guint32) GDK_CURRENT_TIME );
 
     while (m_waiting) gtk_main_iteration();
     
@@ -532,11 +519,36 @@ bool wxClipboard::GetData( wxDataObject& data )
     {
         wxDataFormat format( array[i] );
     
-        wxLogDebug( wxT("wxClipboard::GetData: request format %s"), format.GetId().c_str() );
+        wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format.GetId().c_str() );
     
         /* is data supported by clipboard ? */
-        if (!IsSupported( format ))
-           continue;
+       
+        /* store requested format to be asked for by callbacks */
+        m_targetRequested = format;
+  
+        wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
+    
+        m_formatSupported = FALSE;
+  
+       /* perform query. this will set m_formatSupported to 
+          TRUE if m_targetRequested is supported.
+          also, we have to wait for the "answer" from the 
+          clipboard owner which is an asynchronous process.
+          therefore we set m_waiting = TRUE here and wait
+          until the callback "targets_selection_received" 
+          sets it to FALSE */
+
+        m_waiting = TRUE;
+
+        gtk_selection_convert( m_targetsWidget,
+                           m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
+                                        : g_clipboardAtom, 
+                           g_targetsAtom,
+                           (guint32) GDK_CURRENT_TIME );
+
+        while (m_waiting) gtk_main_iteration();
+    
+        if (!m_formatSupported) continue;
     
         /* store pointer to data object to be filled up by callbacks */
         m_receivedData = &data;
@@ -566,7 +578,7 @@ bool wxClipboard::GetData( wxDataObject& data )
                                m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
                                             : g_clipboardAtom, 
                                m_targetRequested,
-                               GDK_CURRENT_TIME );
+                               (guint32) GDK_CURRENT_TIME );
   
         while (m_waiting) gtk_main_iteration();
 
@@ -578,6 +590,8 @@ bool wxClipboard::GetData( wxDataObject& data )
         return TRUE;
     }
   
+    wxLogDebug( wxT("wxClipboard::GetData: format not found") );
+    
     /* return failure */
     delete[] array;
     return FALSE;