1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor;
5 // Generalized clipboard implementation by Matthew Flatt
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/clipbrd.h"
24 #include "wx/bitmap.h"
27 #include "wx/metafile.h"
29 #include "wx/osx/private.h"
31 #define wxUSE_DATAOBJ 1
35 // the trace mask we use with wxLogTrace() - call
36 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
37 // (there will be a *lot* of them!)
38 #define TRACE_CLIPBOARD wxT("clipboard")
40 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
42 wxClipboard::wxClipboard()
46 PasteboardRef clipboard
= 0;
47 OSStatus err
= PasteboardCreate( kPasteboardClipboard
, &clipboard
);
50 wxLogSysError( wxT("Failed to create the clipboard.") );
52 m_pasteboard
.reset(clipboard
);
55 wxClipboard::~wxClipboard()
57 m_pasteboard
.reset((PasteboardRef
)0);
61 void wxClipboard::Clear()
65 wxCHECK_RET( m_pasteboard
, "Clipboard creation failed." );
67 OSStatus err
= PasteboardClear( m_pasteboard
);
70 wxLogSysError( wxT("Failed to empty the clipboard.") );
74 bool wxClipboard::Flush()
79 bool wxClipboard::Open()
81 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
88 bool wxClipboard::IsOpened() const
93 bool wxClipboard::SetData( wxDataObject
*data
)
95 if ( IsUsingPrimarySelection() )
98 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
99 wxCHECK_MSG( data
, false, wxT("data is invalid") );
103 // as we can only store one wxDataObject,
104 // this is the same in this implementation
105 return AddData( data
);
108 bool wxClipboard::AddData( wxDataObject
*data
)
110 if ( IsUsingPrimarySelection() )
113 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
114 wxCHECK_MSG( data
, false, wxT("data is invalid") );
116 // we can only store one wxDataObject
119 PasteboardSyncFlags syncFlags
= PasteboardSynchronize( m_pasteboard
);
120 wxCHECK_MSG( !(syncFlags
&kPasteboardModified
), false, wxT("clipboard modified after clear") );
121 wxCHECK_MSG( (syncFlags
&kPasteboardClientIsOwner
), false, wxT("client couldn't own clipboard") );
125 data
->AddToPasteboard( m_pasteboard
, 1 );
130 void wxClipboard::Close()
132 wxCHECK_RET( m_open
, wxT("clipboard not open") );
136 // Get rid of cached object.
137 // If this is not done, copying data from
138 // another application will only work once
142 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
144 wxLogTrace(TRACE_CLIPBOARD
, wxT("Checking if format %s is available"),
145 dataFormat
.GetId().c_str());
148 return m_data
->IsSupported( dataFormat
);
149 return wxDataObject::IsFormatInPasteboard( m_pasteboard
, dataFormat
);
152 bool wxClipboard::GetData( wxDataObject
& data
)
154 if ( IsUsingPrimarySelection() )
157 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
159 size_t formatcount
= data
.GetFormatCount(wxDataObject::Set
) + 1;
160 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
161 array
[0] = data
.GetPreferredFormat(wxDataObject::Set
);
162 data
.GetAllFormats( &array
[1], wxDataObject::Set
);
164 bool transferred
= false;
168 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
170 wxDataFormat format
= array
[ i
];
171 if ( m_data
->IsSupported( format
) )
173 int dataSize
= m_data
->GetDataSize( format
);
178 data
.SetData( format
, 0, 0 );
182 char *d
= new char[ dataSize
];
183 m_data
->GetDataHere( format
, (void*)d
);
184 data
.SetData( format
, dataSize
, d
);
191 // get formats from wxDataObjects
194 transferred
= data
.GetFromPasteboard( m_pasteboard
) ;