X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/519cb848a8f4c91c73421bb75314754284e593a4..0c1cc9483ecba053bc9a0983a4a8d48898e334f2:/src/mac/carbon/clipbrd.cpp diff --git a/src/mac/carbon/clipbrd.cpp b/src/mac/carbon/clipbrd.cpp index 28a4b9ef3f..7acd5845f4 100644 --- a/src/mac/carbon/clipbrd.cpp +++ b/src/mac/carbon/clipbrd.cpp @@ -1,236 +1,209 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: clipbrd.cpp +// Name: src/mac/carbon/clipbrd.cpp // Purpose: Clipboard functionality -// Author: AUTHOR +// Author: Stefan Csomor; +// Generalized clipboard implementation by Matthew Flatt // Modified by: -// Created: ??/??/98 +// Created: 1998-01-01 // RCS-ID: $Id$ -// Copyright: (c) AUTHOR -// Licence: wxWindows licence +// Copyright: (c) Stefan Csomor +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation -#pragma implementation "clipbrd.h" +#include "wx/wxprec.h" + +#if wxUSE_CLIPBOARD + +#include "wx/clipbrd.h" + +#ifndef WX_PRECOMP + #include "wx/intl.h" + #include "wx/log.h" + #include "wx/app.h" + #include "wx/utils.h" + #include "wx/frame.h" + #include "wx/bitmap.h" #endif -#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/mac/uma.h" + +#define wxUSE_DATAOBJ 1 #include -#if !USE_SHARED_LIBRARY +// the trace mask we use with wxLogTrace() - call +// wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here +// (there will be a *lot* of them!) +#define TRACE_CLIPBOARD _T("clipboard") + IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) -IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject) -#endif -bool wxOpenClipboard() +wxClipboard::wxClipboard() { - return TRUE; + m_open = false; + m_data = NULL ; + PasteboardRef clipboard = 0; + OSStatus err = PasteboardCreate( kPasteboardClipboard, &clipboard ); + if (err != noErr) + { + wxLogSysError( wxT("Failed to create the clipboard.") ); + } + m_pasteboard.reset(clipboard); } -bool wxCloseClipboard() +wxClipboard::~wxClipboard() { - return FALSE; + m_pasteboard.reset((PasteboardRef)0); + delete m_data; } -bool wxEmptyClipboard() +void wxClipboard::Clear() { - ZeroScrap() ; - return FALSE; -} + if (m_data != NULL) + { + delete m_data; + m_data = NULL; + } -bool wxClipboardOpen() -{ - // TODO - return FALSE; + OSStatus err = PasteboardClear( m_pasteboard ); + if (err != noErr) + { + wxLogSysError( wxT("Failed to empty the clipboard.") ); + } } -bool wxIsClipboardFormatAvailable(int dataFormat) +bool wxClipboard::Flush() { - // TODO - return FALSE; + return false; } -bool wxSetClipboardData(int dataFormat, wxObject *obj, int width, int height) +bool wxClipboard::Open() { - // TODO - return FALSE; -} + wxCHECK_MSG( !m_open, false, wxT("clipboard already open") ); -wxObject *wxGetClipboardData(int dataFormat, long *len) -{ - // TODO - return NULL; -} + m_open = true; -int wxEnumClipboardFormats(int dataFormat) -{ - // TODO - return 0; + return true; } -int wxRegisterClipboardFormat(char *formatName) +bool wxClipboard::IsOpened() const { - // TODO - return 0; + return m_open; } -bool wxGetClipboardFormatName(int dataFormat, char *formatName, int maxCount) +bool wxClipboard::SetData( wxDataObject *data ) { - // TODO - return FALSE; -} + if ( IsUsingPrimarySelection() ) + return false; -/* - * Generalized clipboard implementation by Matthew Flatt - */ + wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); + wxCHECK_MSG( data, false, wxT("data is invalid") ); -wxClipboard *wxTheClipboard = NULL; + Clear(); -void wxInitClipboard() -{ - if (!wxTheClipboard) - wxTheClipboard = new wxClipboard; + // as we can only store one wxDataObject, + // this is the same in this implementation + return AddData( data ); } -wxClipboard::wxClipboard() +bool wxClipboard::AddData( wxDataObject *data ) { - clipOwner = NULL; - cbString = NULL; -} + if ( IsUsingPrimarySelection() ) + return false; -wxClipboard::~wxClipboard() -{ - if (clipOwner) - clipOwner->BeingReplaced(); - if (cbString) - delete[] cbString; -} + wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); + wxCHECK_MSG( data, false, wxT("data is invalid") ); -static int FormatStringToID(char *str) -{ - if (!strcmp(str, "TEXT")) - return wxDF_TEXT; + // we can only store one wxDataObject + Clear(); + + PasteboardSyncFlags syncFlags = PasteboardSynchronize( m_pasteboard ); + wxCHECK_MSG( !(syncFlags&kPasteboardModified), false, wxT("clipboard modified after clear") ); + wxCHECK_MSG( (syncFlags&kPasteboardClientIsOwner), false, wxT("client couldn't own clipboard") ); - return wxRegisterClipboardFormat(str); + m_data = data; + + data->AddToPasteboard( m_pasteboard, 1 ); + + return true; } -void wxClipboard::SetClipboardClient(wxClipboardClient *client, long time) +void wxClipboard::Close() { - 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; - } - } + wxCHECK_RET( m_open, wxT("clipboard not open") ); - 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; - } + m_open = false; + + // Get rid of cached object. + // If this is not done, copying data from + // another application will only work once + if (m_data) + { + delete m_data; + m_data = (wxDataObject*) NULL; + } } -wxClipboardClient *wxClipboard::GetClipboardClient() +bool wxClipboard::IsSupported( const wxDataFormat &dataFormat ) { - return clipOwner; -} + wxLogTrace(TRACE_CLIPBOARD, wxT("Checking if format %s is available"), + dataFormat.GetId().c_str()); -void wxClipboard::SetClipboardString(char *str, long time) -{/* - bool got_selection; - - if (clipOwner) { - clipOwner->BeingReplaced(); - clipOwner = NULL; - } - if (cbString) - delete[] cbString; - - cbString = str; - - if (wxOpenClipboard()) { - if (!wxSetClipboardData(wxDF_TEXT, (wxObject *)str)) - got_selection = FALSE; - else - got_selection = wxCloseClipboard(); - } else - got_selection = FALSE; - - got_selection = FALSE; // Assume another process takes over - - if (!got_selection) { - delete[] cbString; - cbString = NULL; - } - */ + if ( m_data ) + return m_data->IsSupported( dataFormat ); + return wxDataObject::IsFormatInPasteboard( m_pasteboard, dataFormat ); } -char *wxClipboard::GetClipboardString(long time) +bool wxClipboard::GetData( wxDataObject& data ) { - char *str; - long length; + if ( IsUsingPrimarySelection() ) + return false; - str = GetClipboardData("TEXT", &length, time); - if (!str) { - str = new char[1]; - *str = 0; - } + wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); - return str; -} + size_t formatcount = data.GetFormatCount() + 1; + wxDataFormat *array = new wxDataFormat[ formatcount ]; + array[0] = data.GetPreferredFormat(); + data.GetAllFormats( &array[1] ); -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; - } + bool transferred = false; + + if ( m_data ) + { + for (size_t i = 0; !transferred && i < formatcount; i++) + { + wxDataFormat format = array[ i ]; + if ( m_data->IsSupported( format ) ) + { + int dataSize = m_data->GetDataSize( format ); + transferred = true; + + if (dataSize == 0) + { + data.SetData( format, 0, 0 ); + } + else + { + char *d = new char[ dataSize ]; + m_data->GetDataHere( format, (void*)d ); + data.SetData( format, dataSize, d ); + delete [] d; + } + } + } + } + + // get formats from wxDataObjects + if ( !transferred ) + { + transferred = data.GetFromPasteboard( m_pasteboard ) ; + } + + delete [] array; + + return transferred; } +#endif