]> git.saurik.com Git - wxWidgets.git/blobdiff - src/motif/clipbrd.cpp
Added Unix implementation of platform specific files for OS/2.
[wxWidgets.git] / src / motif / clipbrd.cpp
index e22f9f83497c7d6eae46afbbe6b81a3d4883291d..6d13b5b35cffa26bcaa8a7f4f37c1041b5785b3a 100644 (file)
 // Name:        clipbrd.cpp
 // Purpose:     Clipboard functionality
 // Author:      Julian Smart
-// Modified by:
+// Modified by: Mattia Barbon (added support for generic wxDataObjects)
 // Created:     17/09/98
 // RCS-ID:      $Id$
 // Copyright:   (c) Julian Smart
 // Licence:    wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
 #pragma implementation
 #pragma implementation "clipbrd.h"
 #endif
 
+#ifdef __VMS
+#include "wx/vms_x_fix.h"
+#define XtWindow XTWINDOW
+#define XtScreen XTSCREEN
+#define XtParent XTPARENT
+#define XtIsRealized XTISREALIZED
+#define XtDisplay XTDISPLAY
+#endif
+
+#include "wx/defs.h"
+
+#if wxUSE_CLIPBOARD
+
 #include "wx/app.h"
-#include "wx/frame.h"
 #include "wx/bitmap.h"
 #include "wx/utils.h"
-#include "wx/metafile.h"
 #include "wx/clipbrd.h"
+#include "wx/dataobj.h"
+#include "wx/ptr_scpd.h"
 
-#include <string.h>
+#ifdef __VMS__
+#pragma message disable nosimpint
 
-#if !USE_SHARED_LIBRARY
-IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
-IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
 #endif
+#include <Xm/Xm.h>
+#include <Xm/CutPaste.h>
+#ifdef __VMS__
+#pragma message enable nosimpint
+#endif
+
+#include "wx/motif/private.h"
 
 bool wxOpenClipboard()
 {
-    // TODO
-    return FALSE;
+    return wxTheClipboard->Open();
 }
 
 bool wxCloseClipboard()
 {
-    // TODO
-    return FALSE;
+    wxTheClipboard->Close();
+
+    return true;
 }
 
 bool wxEmptyClipboard()
 {
-    // TODO
-    return FALSE;
+    wxTheClipboard->Clear();
+    return true;
 }
 
 bool wxClipboardOpen()
 {
-    // TODO
-    return FALSE;
+    return wxTheClipboard->IsOpened();
 }
 
-bool wxIsClipboardFormatAvailable(int dataFormat)
+bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
 {
-    // TODO
-    return FALSE;
+    return wxTheClipboard->IsSupported( dataFormat );
 }
 
-bool wxSetClipboardData(int dataFormat, wxObject *obj, int width, int height)
+bool wxSetClipboardData(wxDataFormat dataFormat, wxObject *obj,
+                        int WXUNUSED(width), int WXUNUSED(height))
 {
-    // TODO
-    return FALSE;
+    wxDataObject* dobj = NULL;
+
+    if( dataFormat == wxDF_TEXT )
+    {
+        wxChar* data = (wxChar*)obj;
+        dobj = new wxTextDataObject( data );
+    }
+    else if( dataFormat = wxDF_BITMAP )
+    {
+        wxBitmap* data = (wxBitmap*)obj;
+        dobj = new wxBitmapDataObject( *data );
+    }
+
+    if( !dobj )
+        return false;
+
+    return wxTheClipboard->SetData( dobj );
 }
 
-wxObject *wxGetClipboardData(int dataFormat, long *len)
+wxObject *wxGetClipboardData(wxDataFormat dataFormat, long *len)
 {
-    // TODO
-    return NULL;
+    wxDataObject* dobj = NULL;
+    wxTextDataObject* tobj = NULL;
+    wxBitmapDataObject* bobj = NULL;
+
+    if( dataFormat == wxDF_TEXT )
+    {
+        dobj = tobj = new wxTextDataObject;
+    }
+    else if( dataFormat = wxDF_BITMAP )
+    {
+        dobj = bobj = new wxBitmapDataObject;
+    }
+
+    if( !dobj || !wxTheClipboard->GetData( *dobj ) )
+        return NULL;
+
+    if( tobj )
+    {
+        wxString text = tobj->GetText();
+        wxChar* buf = new wxChar[text.length() + 1];
+
+        if( len ) *len = text.length();
+        return (wxObject*)wxStrcpy( buf, text.c_str() );
+    }
+    else if( bobj )
+    {
+        if( len ) *len = 0;
+        return new wxBitmap( bobj->GetBitmap() );
+    }
+
+    return NULL; // just in case...
 }
 
-int  wxEnumClipboardFormats(int dataFormat)
+wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
 {
-    // TODO
-    return 0;
+    // Only wxDF_TEXT supported
+    if (dataFormat == wxDF_TEXT)
+       return wxDF_TEXT;
+    else
+       return wxDF_INVALID;
 }
 
-int  wxRegisterClipboardFormat(char *formatName)
+wxDataFormat wxRegisterClipboardFormat(char *WXUNUSED(formatName))
 {
-    // TODO
-    return 0;
+    // Not supported
+    return wxDF_INVALID;
 }
 
-bool wxGetClipboardFormatName(int dataFormat, char *formatName, int maxCount)
+bool wxGetClipboardFormatName(wxDataFormat dataFormat, char *formatName,
+                              int maxCount)
 {
-    // TODO
-    return FALSE;
-}
+    wxStrncpy( formatName, dataFormat.GetId().c_str(), maxCount );
 
-/*
- * Generalized clipboard implementation by Matthew Flatt
- */
+    return true;
+}
 
-wxClipboard *wxTheClipboard = NULL;
+//-----------------------------------------------------------------------------
+// wxClipboard
+//-----------------------------------------------------------------------------
 
-void wxInitClipboard()
+struct wxDataIdToDataObject
 {
-  if (!wxTheClipboard)
-    wxTheClipboard = new wxClipboard;
-}
+    wxDataIdToDataObject( wxDataObject* o, long d, size_t s )
+        : object( o ), size( s ), dataId( d ) { }
+
+    wxDataObject* object;
+    size_t        size;
+    long          dataId;
+};
+
+#include "wx/listimpl.cpp"
+
+WX_DEFINE_LIST(wxDataObjectList);
+WX_DEFINE_LIST(wxDataIdToDataObjectList);
+
+#if wxCHECK_LESSTIF()
+static void wxClipboardCallback( Widget widget, int* data_id,
+                                 int* priv, int* reason );
+#else
+static void wxClipboardCallback( Widget widget, long* data_id,
+                                 long* priv, int* reason );
+#endif
+
+IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
 
 wxClipboard::wxClipboard()
 {
-  clipOwner = NULL;
-  cbString = NULL;
+    m_open = false;
 }
 
 wxClipboard::~wxClipboard()
 {
-  if (clipOwner)
-    clipOwner->BeingReplaced();
-  if (cbString)
-    delete[] cbString;
+    Clear();  
 }
 
-static int FormatStringToID(char *str)
+void wxClipboard::Clear()
 {
-  if (!strcmp(str, "TEXT"))
-    return wxDF_TEXT;
+    wxDataObjectList::compatibility_iterator node = m_data.GetFirst();
+    while (node)
+    {
+        delete node->GetData();
+        node = node->GetNext();
+    }
+    m_data.Clear();
 
-  return wxRegisterClipboardFormat(str);
+    for( wxDataIdToDataObjectList::compatibility_iterator node2 = m_idToObject.GetFirst();
+         node2; node2 = node2->GetNext() )
+        delete node->GetData();
+    m_idToObject.Clear();
 }
 
-void wxClipboard::SetClipboardClient(wxClipboardClient *client, long time)
+bool wxClipboard::Open()
 {
-  bool got_selection;
-
-  if (clipOwner)
-    clipOwner->BeingReplaced();
-  clipOwner = client;
-  if (cbString) {
-    delete[] cbString;
-    cbString = NULL;
-  }
-
-  if (wxOpenClipboard()) {
-    char **formats, *data;
-         int i;
-    int ftype;
-    long size;
-
-    formats = clipOwner->formats.ListToArray(FALSE);
-    for (i = clipOwner->formats.Number(); i--; ) {
-      ftype = FormatStringToID(formats[i]);
-      data = clipOwner->GetData(formats[i], &size);
-      if (!wxSetClipboardData(ftype, (wxObject *)data, size, 1)) {
-        got_selection = FALSE;
-        break;
-      }
-    }
-
-    if (i < 0)
-      got_selection = wxCloseClipboard();
-  } else
-    got_selection = FALSE;
+    wxCHECK_MSG( !m_open, false, "clipboard already open" );
   
-  got_selection = FALSE; // Assume another process takes over
+    m_open = true;
 
-  if (!got_selection) {
-    clipOwner->BeingReplaced();
-         clipOwner = NULL;
-  }
+    return true;
 }
 
-wxClipboardClient *wxClipboard::GetClipboardClient()
+bool wxClipboard::SetData( wxDataObject *data )
 {
-  return clipOwner;
+    wxCHECK_MSG( data, false, "data is invalid" );
+    wxCHECK_MSG( m_open, false, "clipboard not open" );
+
+    Clear();
+
+    return AddData( data );
 }
 
-void wxClipboard::SetClipboardString(char *str, long time)
+wxDECLARE_SCOPED_ARRAY( wxDataFormat, wxDataFormatScopedArray );
+wxDEFINE_SCOPED_ARRAY( wxDataFormat, wxDataFormatScopedArray );
+
+#if wxCHECK_LESSTIF()
+void wxClipboardCallback( Widget xwidget, int* data_id,
+                          int* priv, int* reason )
+#else
+void wxClipboardCallback( Widget xwidget, long* data_id,
+                          long* priv, int* reason )
+#endif
 {
-  bool got_selection;
+    Display* xdisplay = XtDisplay( xwidget );
+    Window xwindow = XtWindow( xwidget );
+    wxDataObject* dobj = NULL;
+    size_t size = 0;
+
+    for( wxDataIdToDataObjectList::compatibility_iterator node2 =
+             wxTheClipboard->m_idToObject.GetFirst();
+         node2; node2 = node2->GetNext() )
+    {
+        wxDataIdToDataObject* dido = node2->GetData();
+        if( dido->dataId == *data_id )
+        {
+            dobj = dido->object;
+            size = dido->size;
+            break;
+        }
+    }
 
-  if (clipOwner) {
-    clipOwner->BeingReplaced();
-    clipOwner = NULL;
-  }
-  if (cbString)
-    delete[] cbString;
+    if( !dobj ) return;
 
-  cbString = str;
+    wxCharBuffer buffer(size);
+    size_t count = dobj->GetFormatCount( wxDataObject::Get );
+    wxDataFormatScopedArray dfarr( new wxDataFormat[count] );
+    dobj->GetAllFormats( dfarr.get(), wxDataObject::Get );
 
-  if (wxOpenClipboard()) {
-    if (!wxSetClipboardData(wxDF_TEXT, (wxObject *)str))
-      got_selection = FALSE;
-    else
-                got_selection = wxCloseClipboard();
-  } else
-    got_selection = FALSE;
+    if( !dobj->GetDataHere( dfarr[*priv], buffer.data() ) )
+        return;
 
-  got_selection = FALSE; // Assume another process takes over
+    while( XmClipboardCopyByName( xdisplay, xwindow, *data_id,
+                                  buffer.data(), size, 0 )
+           == XmClipboardLocked );
+}
 
-  if (!got_selection) {
-    delete[] cbString;
-    cbString = NULL;
-  }
+bool wxClipboard::AddData( wxDataObject *data )
+{
+    wxCHECK_MSG( data, false, "data is invalid" );
+    wxCHECK_MSG( m_open, false, "clipboard not open" );
+
+    m_data.Append( data );
+
+    Display* xdisplay = wxGlobalDisplay();
+    Widget xwidget = (Widget)wxTheApp->GetTopLevelWidget();
+    Window xwindow = XtWindow( xwidget );
+    wxXmString label( wxTheApp->GetAppName() );
+    Time timestamp = XtLastTimestampProcessed( xdisplay );
+    long itemId;
+
+    int retval;
+
+    while( ( retval = XmClipboardStartCopy( xdisplay, xwindow, label(),
+                                            timestamp, xwidget,
+                                            wxClipboardCallback,
+                                            &itemId ) )
+           == XmClipboardLocked );
+    if( retval != XmClipboardSuccess )
+        return false;
+
+    size_t count = data->GetFormatCount( wxDataObject::Get );
+    wxDataFormatScopedArray dfarr( new wxDataFormat[count] );
+    data->GetAllFormats( dfarr.get(), wxDataObject::Get );
+
+    for( size_t i = 0; i < count; ++i )
+    {
+        size_t size = data->GetDataSize( dfarr[i] );
+        long data_id;
+        wxString id = dfarr[i].GetId();
+
+        while( ( retval = XmClipboardCopy( xdisplay, xwindow, itemId,
+                                           wxConstCast(id.c_str(), char),
+                                           NULL, size, i, &data_id ) )
+               == XmClipboardLocked );
+
+        m_idToObject.Append( new wxDataIdToDataObject( data, data_id, size ) );
+    }
+
+    while( XmClipboardEndCopy( xdisplay, xwindow, itemId )
+           == XmClipboardLocked );
+
+    return true;
+}
+
+void wxClipboard::Close()
+{
+    wxCHECK_RET( m_open, "clipboard not open" );
+    
+    m_open = false;
 }
 
-char *wxClipboard::GetClipboardString(long time)
+bool wxClipboard::IsSupported(const wxDataFormat& format)
 {
-  char *str;
-  long length;
+    Display* xdisplay = wxGlobalDisplay();
+    Window xwindow = XtWindow( (Widget)wxTheApp->GetTopLevelWidget() );
+    bool isSupported = false;
+    int retval, count;
+    unsigned long  max_name_length;
+    wxString id = format.GetId();
+
+    while( ( retval = XmClipboardLock( xdisplay, xwindow ) )
+           == XmClipboardLocked );
+    if( retval != XmClipboardSuccess )
+        return false;
+
+    if( XmClipboardInquireCount( xdisplay, xwindow, &count, &max_name_length )
+        == XmClipboardSuccess )
+    {
+        wxCharBuffer buf( max_name_length + 1 );
+        unsigned long copied;
+
+        for( int i = 0; i < count; ++i )
+        {
+            if( XmClipboardInquireFormat( xdisplay, xwindow, i + 1,
+                                          (XtPointer)buf.data(),
+                                          max_name_length, &copied )
+                != XmClipboardSuccess )
+                continue;
+
+            buf.data()[copied] = '\0';
+
+            if( buf == id )
+            {
+                isSupported = true;
+                break;
+            }
+        }
+    }
 
-  str = GetClipboardData("TEXT", &length, time);
-  if (!str) {
-    str = new char[1];
-    *str = 0;
-  }
+    XmClipboardUnlock( xdisplay, xwindow, False );
 
-  return str;
+    return isSupported;
 }
 
-char *wxClipboard::GetClipboardData(char *format, long *length, long time)
+class wxClipboardEndRetrieve
 {
-  if (clipOwner)  {
-         if (clipOwner->formats.Member(format))
-      return clipOwner->GetData(format, length);
-    else
-      return NULL;
-  } else if (cbString) {
-    if (!strcmp(format, "TEXT"))
-      return copystring(cbString);
-    else
-      return NULL;
-  } else {
-    if (wxOpenClipboard()) {
-      receivedString = (char *)wxGetClipboardData(FormatStringToID(format), 
-                                                  length);
-      wxCloseClipboard();
-    } else
-      receivedString = NULL;
+public:
+    wxClipboardEndRetrieve( Display* display, Window window )
+        : m_display( display ), m_window( window ) { }
+    ~wxClipboardEndRetrieve()
+    {
+        while( XmClipboardEndRetrieve( m_display, m_window )
+               == XmClipboardLocked );
+    }
+private:
+    Display* m_display;
+    Window m_window;
+};
+
+bool wxClipboard::GetData( wxDataObject& data )
+{
+    wxCHECK_MSG( m_open, false, "clipboard not open" );
+
+    Display* xdisplay = wxGlobalDisplay();
+    Window xwindow = XtWindow( (Widget)wxTheApp->GetTopLevelWidget() );
+    Time timestamp = XtLastTimestampProcessed( xdisplay );
+
+    wxDataFormat chosenFormat;
+    int retval;
+
+    ///////////////////////////////////////////////////////////////////////////
+    // determine if the cliboard holds any format we like
+    ///////////////////////////////////////////////////////////////////////////
+    while( ( retval = XmClipboardStartRetrieve( xdisplay, xwindow,
+                                                timestamp ) )
+           == XmClipboardLocked );
+    if( retval != XmClipboardSuccess )
+        return false;
+
+    wxClipboardEndRetrieve endRetrieve( xdisplay, xwindow );
+
+    int count;
+    unsigned long max_name_length;
+    size_t dfcount = data.GetFormatCount( wxDataObject::Set );
+    wxDataFormatScopedArray dfarr( new wxDataFormat[dfcount] );
+    data.GetAllFormats( dfarr.get(), wxDataObject::Set );
+    
+    if( XmClipboardInquireCount( xdisplay, xwindow, &count, &max_name_length )
+        == XmClipboardSuccess )
+    {
+        wxCharBuffer buf( max_name_length + 1 );
+        unsigned long copied;
+
+        for( int i = 0; i < count; ++i )
+        {
+            if( XmClipboardInquireFormat( xdisplay, xwindow, i + 1,
+                                          (XtPointer)buf.data(),
+                                          max_name_length, &copied )
+                != XmClipboardSuccess )
+                continue;
+
+            buf.data()[copied] = '\0';
+
+            // try preferred format
+            if( buf == data.GetPreferredFormat( wxDataObject::Set ).GetId() )
+            {
+                chosenFormat = data.GetPreferredFormat( wxDataObject::Set );
+                break;
+            }
+
+            // try all other formats
+            for( size_t i = 0; i < dfcount; ++i )
+            {
+                if( buf == dfarr[i].GetId() )
+                    chosenFormat = dfarr[i];
+            }
+        }
+    }
 
-    return receivedString;
-  }
+    if( chosenFormat == wxDF_INVALID )
+        return false;
+
+    ///////////////////////////////////////////////////////////////////////////
+    // now retrieve the data
+    ///////////////////////////////////////////////////////////////////////////
+    unsigned long length, dummy1;
+    long dummy2;
+    wxString id = chosenFormat.GetId();
+
+    while( ( retval = XmClipboardInquireLength( xdisplay, xwindow,
+                                                wxConstCast(id.c_str(), char),
+                                                &length ) )
+           == XmClipboardLocked );
+    if( retval != XmClipboardSuccess )
+        return false;
+
+    wxCharBuffer buf(length);
+
+    while( ( retval = XmClipboardRetrieve( xdisplay, xwindow,
+                                           wxConstCast(id.c_str(), char),
+                                           (XtPointer)buf.data(),
+                                           length, &dummy1, &dummy2 ) )
+           == XmClipboardLocked );
+    if( retval != XmClipboardSuccess )
+        return false;
+
+    if( !data.SetData( chosenFormat, length, buf.data() ) )
+        return false;
+
+    return true;
 }
 
+#endif // wxUSE_CLIPBOARD