1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/uma.h"
32 #define wxUSE_DATAOBJ 1
37 // the trace mask we use with wxLogTrace() - call
38 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
39 // (there will be a *lot* of them!)
40 static const wxChar
*TRACE_CLIPBOARD
= wxT("clipboard");
42 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
44 wxClipboard::wxClipboard()
48 PasteboardRef clipboard
= 0;
49 OSStatus err
= PasteboardCreate( kPasteboardClipboard
, &clipboard
);
52 wxLogSysError( wxT("Failed to create the clipboard.") );
54 m_pasteboard
.reset(clipboard
);
57 wxClipboard::~wxClipboard()
59 m_pasteboard
.reset((PasteboardRef
)0);
63 void wxClipboard::Clear()
71 OSStatus err
= PasteboardClear( m_pasteboard
);
74 wxLogSysError( wxT("Failed to empty the clipboard.") );
78 bool wxClipboard::Flush()
83 bool wxClipboard::Open()
85 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
92 bool wxClipboard::IsOpened() const
97 bool wxClipboard::SetData( wxDataObject
*data
)
99 if ( IsUsingPrimarySelection() )
102 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
103 wxCHECK_MSG( data
, false, wxT("data is invalid") );
107 // as we can only store one wxDataObject,
108 // this is the same in this implementation
109 return AddData( data
);
112 bool wxClipboard::AddData( wxDataObject
*data
)
114 if ( IsUsingPrimarySelection() )
117 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
118 wxCHECK_MSG( data
, false, wxT("data is invalid") );
120 // we can only store one wxDataObject
123 PasteboardSyncFlags syncFlags
= PasteboardSynchronize( m_pasteboard
);
124 wxCHECK_MSG( !(syncFlags
&kPasteboardModified
), false, wxT("clipboard modified after clear") );
125 wxCHECK_MSG( (syncFlags
&kPasteboardClientIsOwner
), false, wxT("client couldn't own clipboard") );
129 data
->AddToPasteboard( m_pasteboard
, 1 );
134 void wxClipboard::Close()
136 wxCHECK_RET( m_open
, wxT("clipboard not open") );
140 // Get rid of cached object.
141 // If this is not done, copying data from
142 // another application will only work once
146 m_data
= (wxDataObject
*) NULL
;
150 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
153 return m_data
->IsSupported( dataFormat
);
154 return wxDataObject::IsFormatInPasteboard( m_pasteboard
, dataFormat
);
157 bool wxClipboard::GetData( wxDataObject
& data
)
159 if ( IsUsingPrimarySelection() )
162 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
164 size_t formatcount
= data
.GetFormatCount() + 1;
165 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
166 array
[0] = data
.GetPreferredFormat();
167 data
.GetAllFormats( &array
[1] );
169 bool transferred
= false;
173 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
175 wxDataFormat format
= array
[ i
];
176 if ( m_data
->IsSupported( format
) )
178 int dataSize
= m_data
->GetDataSize( format
);
183 data
.SetData( format
, 0, 0 );
187 char *d
= new char[ dataSize
];
188 m_data
->GetDataHere( format
, (void*)d
);
189 data
.SetData( format
, dataSize
, d
);
196 // get formats from wxDataObjects
199 transferred
= data
.GetFromPasteboard( m_pasteboard
) ;