1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor;
5 // Generalized clipboard implementation by Matthew Flatt
9 // Copyright: (c) Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
17 #include "wx/clipbrd.h"
25 #include "wx/bitmap.h"
28 #include "wx/metafile.h"
30 #include "wx/osx/private.h"
32 #define wxUSE_DATAOBJ 1
36 // the trace mask we use with wxLogTrace() - call
37 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
38 // (there will be a *lot* of them!)
39 #define TRACE_CLIPBOARD wxT("clipboard")
41 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
43 wxClipboard::wxClipboard()
47 PasteboardRef clipboard
= 0;
48 OSStatus err
= PasteboardCreate( kPasteboardClipboard
, &clipboard
);
51 wxLogSysError( wxT("Failed to create the clipboard.") );
53 m_pasteboard
.reset(clipboard
);
56 wxClipboard::~wxClipboard()
58 m_pasteboard
.reset((PasteboardRef
)0);
62 void wxClipboard::Clear()
66 OSStatus err
= PasteboardClear( m_pasteboard
);
69 wxLogSysError( wxT("Failed to empty the clipboard.") );
73 bool wxClipboard::Flush()
78 bool wxClipboard::Open()
80 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
87 bool wxClipboard::IsOpened() const
92 bool wxClipboard::SetData( wxDataObject
*data
)
94 if ( IsUsingPrimarySelection() )
97 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
98 wxCHECK_MSG( data
, false, wxT("data is invalid") );
102 // as we can only store one wxDataObject,
103 // this is the same in this implementation
104 return AddData( data
);
107 bool wxClipboard::AddData( wxDataObject
*data
)
109 if ( IsUsingPrimarySelection() )
112 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
113 wxCHECK_MSG( data
, false, wxT("data is invalid") );
115 // we can only store one wxDataObject
118 PasteboardSyncFlags syncFlags
= PasteboardSynchronize( m_pasteboard
);
119 wxCHECK_MSG( !(syncFlags
&kPasteboardModified
), false, wxT("clipboard modified after clear") );
120 wxCHECK_MSG( (syncFlags
&kPasteboardClientIsOwner
), false, wxT("client couldn't own clipboard") );
124 data
->AddToPasteboard( m_pasteboard
, 1 );
129 void wxClipboard::Close()
131 wxCHECK_RET( m_open
, wxT("clipboard not open") );
135 // Get rid of cached object.
136 // If this is not done, copying data from
137 // another application will only work once
141 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
143 wxLogTrace(TRACE_CLIPBOARD
, wxT("Checking if format %s is available"),
144 dataFormat
.GetId().c_str());
147 return m_data
->IsSupported( dataFormat
);
148 return wxDataObject::IsFormatInPasteboard( m_pasteboard
, dataFormat
);
151 bool wxClipboard::GetData( wxDataObject
& data
)
153 if ( IsUsingPrimarySelection() )
156 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
158 size_t formatcount
= data
.GetFormatCount() + 1;
159 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
160 array
[0] = data
.GetPreferredFormat();
161 data
.GetAllFormats( &array
[1] );
163 bool transferred
= false;
167 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
169 wxDataFormat format
= array
[ i
];
170 if ( m_data
->IsSupported( format
) )
172 int dataSize
= m_data
->GetDataSize( format
);
177 data
.SetData( format
, 0, 0 );
181 char *d
= new char[ dataSize
];
182 m_data
->GetDataHere( format
, (void*)d
);
183 data
.SetData( format
, dataSize
, d
);
190 // get formats from wxDataObjects
193 transferred
= data
.GetFromPasteboard( m_pasteboard
) ;