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 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
39 wxClipboard::wxClipboard()
43 PasteboardRef clipboard
= 0;
44 OSStatus err
= PasteboardCreate( kPasteboardClipboard
, &clipboard
);
47 wxLogSysError( wxT("Failed to create the clipboard.") );
49 m_pasteboard
.reset(clipboard
);
52 wxClipboard::~wxClipboard()
54 m_pasteboard
.reset((PasteboardRef
)0);
58 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 m_data
= (wxDataObject
*) NULL
;
145 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
147 wxLogTrace(TRACE_CLIPBOARD
, wxT("Checking if format %s is available"),
148 dataFormat
.GetId().c_str());
151 return m_data
->IsSupported( dataFormat
);
152 return wxDataObject::IsFormatInPasteboard( m_pasteboard
, dataFormat
);
155 bool wxClipboard::GetData( wxDataObject
& data
)
157 if ( IsUsingPrimarySelection() )
160 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
162 size_t formatcount
= data
.GetFormatCount() + 1;
163 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
164 array
[0] = data
.GetPreferredFormat();
165 data
.GetAllFormats( &array
[1] );
167 bool transferred
= false;
171 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
173 wxDataFormat format
= array
[ i
];
174 if ( m_data
->IsSupported( format
) )
176 int dataSize
= m_data
->GetDataSize( format
);
181 data
.SetData( format
, 0, 0 );
185 char *d
= new char[ dataSize
];
186 m_data
->GetDataHere( format
, (void*)d
);
187 data
.SetData( format
, dataSize
, d
);
194 // get formats from wxDataObjects
197 transferred
= data
.GetFromPasteboard( m_pasteboard
) ;