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/osx/uma.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 _T("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()
70 OSStatus err
= PasteboardClear( m_pasteboard
);
73 wxLogSysError( wxT("Failed to empty the clipboard.") );
77 bool wxClipboard::Flush()
82 bool wxClipboard::Open()
84 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
91 bool wxClipboard::IsOpened() const
96 bool wxClipboard::SetData( wxDataObject
*data
)
98 if ( IsUsingPrimarySelection() )
101 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
102 wxCHECK_MSG( data
, false, wxT("data is invalid") );
106 // as we can only store one wxDataObject,
107 // this is the same in this implementation
108 return AddData( data
);
111 bool wxClipboard::AddData( wxDataObject
*data
)
113 if ( IsUsingPrimarySelection() )
116 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
117 wxCHECK_MSG( data
, false, wxT("data is invalid") );
119 // we can only store one wxDataObject
122 PasteboardSyncFlags syncFlags
= PasteboardSynchronize( m_pasteboard
);
123 wxCHECK_MSG( !(syncFlags
&kPasteboardModified
), false, wxT("clipboard modified after clear") );
124 wxCHECK_MSG( (syncFlags
&kPasteboardClientIsOwner
), false, wxT("client couldn't own clipboard") );
128 data
->AddToPasteboard( m_pasteboard
, 1 );
133 void wxClipboard::Close()
135 wxCHECK_RET( m_open
, wxT("clipboard not open") );
139 // Get rid of cached object.
140 // If this is not done, copying data from
141 // another application will only work once
145 m_data
= (wxDataObject
*) NULL
;
149 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
151 wxLogTrace(TRACE_CLIPBOARD
, wxT("Checking if format %s is available"),
152 dataFormat
.GetId().c_str());
155 return m_data
->IsSupported( dataFormat
);
156 return wxDataObject::IsFormatInPasteboard( m_pasteboard
, dataFormat
);
159 bool wxClipboard::GetData( wxDataObject
& data
)
161 if ( IsUsingPrimarySelection() )
164 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
166 size_t formatcount
= data
.GetFormatCount() + 1;
167 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
168 array
[0] = data
.GetPreferredFormat();
169 data
.GetAllFormats( &array
[1] );
171 bool transferred
= false;
175 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
177 wxDataFormat format
= array
[ i
];
178 if ( m_data
->IsSupported( format
) )
180 int dataSize
= m_data
->GetDataSize( format
);
185 data
.SetData( format
, 0, 0 );
189 char *d
= new char[ dataSize
];
190 m_data
->GetDataHere( format
, (void*)d
);
191 data
.SetData( format
, dataSize
, d
);
198 // get formats from wxDataObjects
201 transferred
= data
.GetFromPasteboard( m_pasteboard
) ;