]> git.saurik.com Git - wxWidgets.git/blobdiff - src/motif/clipbrd.cpp
call to pthread_attr_setscope() added (disabled for now) and several other small...
[wxWidgets.git] / src / motif / clipbrd.cpp
index e22f9f83497c7d6eae46afbbe6b81a3d4883291d..21df675bf2dba318d71fb4de42a85919d8fd20d8 100644 (file)
 #include "wx/utils.h"
 #include "wx/metafile.h"
 #include "wx/clipbrd.h"
+#include "wx/dataobj.h"
+
+#include <Xm/Xm.h>
+#include <Xm/CutPaste.h>
 
 #include <string.h>
 
 #if !USE_SHARED_LIBRARY
-IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
-IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
+// IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
+// IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
 #endif
 
+static bool gs_clipboardIsOpen = FALSE;
+
 bool wxOpenClipboard()
 {
-    // TODO
-    return FALSE;
+    if (!gs_clipboardIsOpen)
+    {
+        gs_clipboardIsOpen = TRUE;
+        return TRUE;
+    }
+    else
+        return FALSE;
 }
 
 bool wxCloseClipboard()
 {
-    // TODO
-    return FALSE;
+    if (gs_clipboardIsOpen)
+    {
+        gs_clipboardIsOpen = FALSE;
+        return TRUE;
+    }
+    else
+        return FALSE;
 }
 
 bool wxEmptyClipboard()
 {
-    // TODO
-    return FALSE;
+    // No equivalent in Motif
+    return TRUE;
 }
 
 bool wxClipboardOpen()
 {
-    // TODO
-    return FALSE;
+    return gs_clipboardIsOpen;
 }
 
-bool wxIsClipboardFormatAvailable(int dataFormat)
+bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
 {
-    // TODO
-    return FALSE;
+    // Only text is supported.
+    if (dataFormat != wxDF_TEXT)
+        return FALSE;
+
+    unsigned long numBytes = 0;
+    long privateId = 0;
+    
+    Window window = (Window) 0;
+    if (wxTheApp->GetTopWindow())
+        window = XtWindow( (Widget) wxTheApp->GetTopWindow()->GetTopWidget() );
+
+    int success = XmClipboardRetrieve((Display*) wxGetDisplay(),
+      window, "TEXT", (XtPointer) 0, 0, & numBytes, & privateId) ;
+
+    // Assume only text is supported. If we have anything at all,
+    // or the clipboard is locked so we're not sure, we say we support it.
+    if (success == ClipboardNoData)
+        return FALSE;
+    else
+        return TRUE;
 }
 
-bool wxSetClipboardData(int dataFormat, wxObject *obj, int width, int height)
+bool wxSetClipboardData(wxDataFormat dataFormat, wxObject *obj, int width, int height)
 {
-    // TODO
-    return FALSE;
+    if (dataFormat != wxDF_TEXT)
+        return FALSE;
+
+    char* data = (char*) obj;
+
+    XmString text = XmStringCreateSimple ("CLIPBOARD");
+    Window window = (Window) 0;
+    if (wxTheApp->GetTopWindow())
+        window = XtWindow( (Widget) wxTheApp->GetTopWindow()->GetTopWidget() );
+
+    long itemId = 0;
+    int result = 0;
+
+    while ((result =
+      XmClipboardStartCopy((Display*) wxGetDisplay(),
+       window, 
+       text,
+       XtLastTimestampProcessed((Display*) wxGetDisplay()),
+       (Widget) 0,
+       (XmCutPasteProc) 0,
+       & itemId)) != ClipboardSuccess)
+
+       ;
+
+    XmStringFree (text);
+
+    long dataId = 0;
+    while ((result =
+      XmClipboardCopy((Display*) wxGetDisplay(),
+       window, 
+       itemId,
+       "TEXT",
+       (XtPointer) data,
+       strlen(data) + 1,
+       0,
+       & dataId)) != ClipboardSuccess)
+
+       ;
+
+    while (( result =
+       XmClipboardEndCopy((Display*) wxGetDisplay(),
+         window, itemId) ) != ClipboardSuccess)
+
+       ;
+    
+    return TRUE;
 }
 
-wxObject *wxGetClipboardData(int dataFormat, long *len)
+wxObject *wxGetClipboardData(wxDataFormat dataFormat, long *len)
 {
-    // TODO
+    if (dataFormat != wxDF_TEXT)
+        return (wxObject*) NULL;
+
+    bool done = FALSE;
+    long id = 0;
+    unsigned long numBytes = 0;
+    int result = 0;
+    Window window = (Window) 0;
+    if (wxTheApp->GetTopWindow())
+        window = XtWindow( (Widget) wxTheApp->GetTopWindow()->GetTopWidget() );
+
+    int currentDataSize = 256;
+    char* data = new char[currentDataSize];
+
+    while (!done)
+    {
+        if (result == ClipboardTruncate)
+        {
+            delete[] data;
+            currentDataSize = 2*currentDataSize;
+            data = new char[currentDataSize];
+       }
+        result = XmClipboardRetrieve((Display*) wxGetDisplay(),
+          window,
+          "TEXT",
+          (XtPointer) data,
+          currentDataSize,
+          &numBytes,
+          &id);
+
+        switch (result)
+         {
+         case ClipboardSuccess:
+           {
+              if (len)
+                *len = strlen(data) + 1;
+              return (wxObject*) data;
+              break;
+           }
+         case ClipboardTruncate:
+         case ClipboardLocked:
+           {
+              break;
+           }
+          default:
+         case ClipboardNoData:
+           {
+              return (wxObject*) NULL;
+              break;
+           }
+         }
+          
+    }
+
     return NULL;
 }
 
-int  wxEnumClipboardFormats(int dataFormat)
+wxDataFormat  wxEnumClipboardFormats(wxDataFormat dataFormat)
 {
-    // TODO
-    return 0;
+    // Only wxDF_TEXT supported
+    if (dataFormat == (wxDataFormat) 0)
+       return wxDF_TEXT;
+    else
+       return (wxDataFormat) 0;
 }
 
-int  wxRegisterClipboardFormat(char *formatName)
+wxDataFormat  wxRegisterClipboardFormat(char *formatName)
 {
-    // TODO
-    return 0;
+    // Not supported
+    return (wxDataFormat) 0;
 }
 
-bool wxGetClipboardFormatName(int dataFormat, char *formatName, int maxCount)
+bool wxGetClipboardFormatName(wxDataFormat dataFormat, char *formatName, int maxCount)
 {
-    // TODO
-    return FALSE;
+    // Only wxDF_TEXT supported
+    if (dataFormat == wxDF_TEXT)
+    {
+       strcpy(formatName, "TEXT");
+       return TRUE;
+    }
+    else
+       return FALSE;
 }
 
-/*
- * Generalized clipboard implementation by Matthew Flatt
- */
+//-----------------------------------------------------------------------------
+// wxClipboard
+//-----------------------------------------------------------------------------
 
-wxClipboard *wxTheClipboard = NULL;
+IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
 
-void wxInitClipboard()
-{
-  if (!wxTheClipboard)
-    wxTheClipboard = new wxClipboard;
-}
+wxClipboard* wxTheClipboard = (wxClipboard*) NULL;
 
 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;
-
-  return wxRegisterClipboardFormat(str);
+    wxNode* node = m_data.First();
+    while (node)
+    {
+        wxDataObject* data = (wxDataObject*) node->Data();
+        delete data;
+        node = node->Next();
+    }
+    m_data.Clear();
 }
 
-void wxClipboard::SetClipboardClient(wxClipboardClient *client, long time)
+bool wxClipboard::Open()
 {
-  bool got_selection;
+    wxCHECK_MSG( !m_open, FALSE, "clipboard already open" );
+  
+    m_open = TRUE;
 
-  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;
-      }
-    }
+    return wxOpenClipboard();
+}
 
-    if (i < 0)
-      got_selection = wxCloseClipboard();
-  } else
-    got_selection = FALSE;
+bool wxClipboard::SetData( wxDataObject *data )
+{
+    wxCHECK_MSG( data, FALSE, "data is invalid" );
+    wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
+
+    switch (data->GetFormat())
+    {
+        case wxDF_TEXT:
+        case wxDF_OEMTEXT:
+        {
+            wxTextDataObject* textDataObject = (wxTextDataObject*) data;
+            wxString str(textDataObject->GetText());
+            return wxSetClipboardData(data->GetFormat(), (wxObject*) (const char*) str);
+            break;
+        }
+        case wxDF_BITMAP:
+        case wxDF_DIB:
+        {
+            wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
+            wxBitmap bitmap(bitmapDataObject->GetBitmap());
+            return wxSetClipboardData(data->GetFormat(), & bitmap);
+            break;
+        }
+        default:
+        {
+            return FALSE;
+        }
+    }
   
-  got_selection = FALSE; // Assume another process takes over
+    return FALSE;
+}
 
-  if (!got_selection) {
-    clipOwner->BeingReplaced();
-         clipOwner = NULL;
-  }
+void wxClipboard::Close()
+{
+    wxCHECK_RET( m_open, "clipboard not open" );
+    
+    m_open = FALSE;
+    wxCloseClipboard();
 }
 
-wxClipboardClient *wxClipboard::GetClipboardClient()
+bool wxClipboard::IsSupported( wxDataFormat format)
 {
-  return clipOwner;
+    return wxIsClipboardFormatAvailable(format);
 }
 
-void wxClipboard::SetClipboardString(char *str, long time)
+bool wxClipboard::GetData( wxDataObject *data )
 {
-  bool got_selection;
+    wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
+    
+    switch (data->GetFormat())
+    {
+        case wxDF_TEXT:
+        case wxDF_OEMTEXT:
+        {
+            wxTextDataObject* textDataObject = (wxTextDataObject*) data;
+            char* s = (char*) wxGetClipboardData(data->GetFormat());
+            if (s)
+            {
+                textDataObject->SetText(s);
+                delete[] s;
+                return TRUE;
+            }
+            else
+                return FALSE;
+            break;
+        }
+        case wxDF_BITMAP:
+        case wxDF_DIB:
+        {
+            wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
+            wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetFormat());
+            if (bitmap)
+            {
+                bitmapDataObject->SetBitmap(* bitmap);
+                delete bitmap;
+                return TRUE;
+            }
+            else
+                return FALSE;
+            break;
+        }
+        default:
+        {
+            return FALSE;
+        }
+    }
+    return FALSE;
+}
 
-  if (clipOwner) {
-    clipOwner->BeingReplaced();
-    clipOwner = NULL;
-  }
-  if (cbString)
-    delete[] cbString;
+//-----------------------------------------------------------------------------
+// wxClipboardModule
+//-----------------------------------------------------------------------------
 
-  cbString = str;
+IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
 
-  if (wxOpenClipboard()) {
-    if (!wxSetClipboardData(wxDF_TEXT, (wxObject *)str))
-      got_selection = FALSE;
-    else
-                got_selection = wxCloseClipboard();
-  } else
-    got_selection = FALSE;
+bool wxClipboardModule::OnInit()
+{
+    wxTheClipboard = new wxClipboard();
+  
+    return TRUE;
+}
 
-  got_selection = FALSE; // Assume another process takes over
+void wxClipboardModule::OnExit()
+{
+    if (wxTheClipboard) delete wxTheClipboard;
+    wxTheClipboard = (wxClipboard*) NULL;
+}
+
+
+#if 0
+
+/*
+* Old clipboard implementation by Matthew Flatt
+*/
+
+wxClipboard *wxTheClipboard = NULL;
+
+void wxInitClipboard()
+{
+    if (!wxTheClipboard)
+        wxTheClipboard = new wxClipboard;
+}
 
-  if (!got_selection) {
-    delete[] cbString;
+wxClipboard::wxClipboard()
+{
+    clipOwner = NULL;
     cbString = NULL;
-  }
 }
 
-char *wxClipboard::GetClipboardString(long time)
+wxClipboard::~wxClipboard()
+{
+    if (clipOwner)
+        clipOwner->BeingReplaced();
+    if (cbString)
+        delete[] cbString;
+}
+
+static int FormatStringToID(char *str)
 {
-  char *str;
-  long length;
+    if (!strcmp(str, "TEXT"))
+        return wxDF_TEXT;
+    
+    return wxRegisterClipboardFormat(str);
+}
 
-  str = GetClipboardData("TEXT", &length, time);
-  if (!str) {
-    str = new char[1];
-    *str = 0;
-  }
+void wxClipboard::SetClipboardClient(wxClipboardClient *client, long time)
+{
+    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;
+    
+    got_selection = FALSE; // Assume another process takes over
+    
+    if (!got_selection) {
+        clipOwner->BeingReplaced();
+        clipOwner = NULL;
+    }
+}
 
-  return str;
+wxClipboardClient *wxClipboard::GetClipboardClient()
+{
+    return clipOwner;
 }
 
-char *wxClipboard::GetClipboardData(char *format, long *length, long time)
+void wxClipboard::SetClipboardString(char *str, long time)
 {
-  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 {
+    bool got_selection;
+    
+    if (clipOwner) {
+        clipOwner->BeingReplaced();
+        clipOwner = NULL;
+    }
+    if (cbString)
+        delete[] cbString;
+    
+    cbString = str;
+    
     if (wxOpenClipboard()) {
-      receivedString = (char *)wxGetClipboardData(FormatStringToID(format), 
-                                                  length);
-      wxCloseClipboard();
+        if (!wxSetClipboardData(wxDF_TEXT, (wxObject *)str))
+            got_selection = FALSE;
+        else
+            got_selection = wxCloseClipboard();
     } else
-      receivedString = NULL;
+        got_selection = FALSE;
+    
+    got_selection = FALSE; // Assume another process takes over
+    
+    if (!got_selection) {
+        delete[] cbString;
+        cbString = NULL;
+    }
+}
+
+char *wxClipboard::GetClipboardString(long time)
+{
+    char *str;
+    long length;
+    
+    str = GetClipboardData("TEXT", &length, time);
+    if (!str) {
+        str = new char[1];
+        *str = 0;
+    }
+    
+    return str;
+}
 
-    return receivedString;
-  }
+char *wxClipboard::GetClipboardData(char *format, long *length, long time)
+{
+    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;
+        
+        return receivedString;
+    }
 }
+#endif