]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/clipbrd.cpp
Update to wxDataViewCtrl from Hartwig
[wxWidgets.git] / src / mac / carbon / clipbrd.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
3e822cd8 2// Name: src/mac/carbon/clipbrd.cpp
e9576ca5 3// Purpose: Clipboard functionality
3e822cd8 4// Author: Stefan Csomor;
de6185e2 5// Generalized clipboard implementation by Matthew Flatt
e9576ca5 6// Modified by:
a31a5f85 7// Created: 1998-01-01
e9576ca5 8// RCS-ID: $Id$
a31a5f85 9// Copyright: (c) Stefan Csomor
65571936 10// Licence: wxWindows licence
e9576ca5
SC
11/////////////////////////////////////////////////////////////////////////////
12
a8e9860d
SC
13#include "wx/wxprec.h"
14
179e085f
RN
15#if wxUSE_CLIPBOARD
16
88a7a4e1
WS
17#include "wx/clipbrd.h"
18
19#ifndef WX_PRECOMP
20 #include "wx/intl.h"
e4db172a 21 #include "wx/log.h"
670f9935 22 #include "wx/app.h"
de6185e2 23 #include "wx/utils.h"
76b49cf4 24 #include "wx/frame.h"
0bca0373 25 #include "wx/bitmap.h"
88a7a4e1
WS
26#endif
27
e9576ca5 28#include "wx/metafile.h"
e9576ca5 29
9c3c5849 30#include "wx/mac/uma.h"
76a5e5d2 31
2f1ae414
SC
32#define wxUSE_DATAOBJ 1
33
e9576ca5
SC
34#include <string.h>
35
3e822cd8 36
f0822896
SC
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!)
3e822cd8
DS
40static const wxChar *TRACE_CLIPBOARD = wxT("clipboard");
41
6239ee05
SC
42IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
43
e7549107
SC
44wxClipboard::wxClipboard()
45{
3e822cd8 46 m_open = false;
9d463591 47 m_data = NULL ;
6239ee05
SC
48 PasteboardRef clipboard = 0;
49 OSStatus err = PasteboardCreate( kPasteboardClipboard, &clipboard );
50 if (err != noErr)
51 {
52 wxLogSysError( wxT("Failed to create the clipboard.") );
53 }
9d463591 54 m_pasteboard.reset(clipboard);
e7549107 55}
e9576ca5 56
e7549107 57wxClipboard::~wxClipboard()
e9576ca5 58{
9d463591
SC
59 m_pasteboard.reset((PasteboardRef)0);
60 delete m_data;
e9576ca5
SC
61}
62
e7549107 63void wxClipboard::Clear()
e9576ca5 64{
9d463591 65 if (m_data != NULL)
f0822896 66 {
9d463591
SC
67 delete m_data;
68 m_data = NULL;
f0822896 69 }
3e822cd8 70
9d463591 71 OSStatus err = PasteboardClear( m_pasteboard );
3e822cd8 72 if (err != noErr)
ed60b502 73 {
3e822cd8 74 wxLogSysError( wxT("Failed to empty the clipboard.") );
ed60b502 75 }
e9576ca5
SC
76}
77
e7549107 78bool wxClipboard::Flush()
e9576ca5 79{
3dee36ae 80 return false;
e7549107
SC
81}
82
83bool wxClipboard::Open()
84{
3dee36ae 85 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
3e822cd8
DS
86
87 m_open = true;
88
89 return true;
e7549107
SC
90}
91
92bool wxClipboard::IsOpened() const
93{
f0822896 94 return m_open;
e9576ca5
SC
95}
96
f0822896 97bool wxClipboard::SetData( wxDataObject *data )
e9576ca5 98{
9005f2ed
VZ
99 if ( IsUsingPrimarySelection() )
100 return false;
101
3dee36ae 102 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
3dee36ae 103 wxCHECK_MSG( data, false, wxT("data is invalid") );
634bafa9
SC
104
105 Clear();
3e822cd8
DS
106
107 // as we can only store one wxDataObject,
108 // this is the same in this implementation
f0822896 109 return AddData( data );
e7549107
SC
110}
111
112bool wxClipboard::AddData( wxDataObject *data )
113{
9005f2ed
VZ
114 if ( IsUsingPrimarySelection() )
115 return false;
116
3dee36ae 117 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
3dee36ae 118 wxCHECK_MSG( data, false, wxT("data is invalid") );
e7549107 119
3e822cd8 120 // we can only store one wxDataObject
f0822896 121 Clear();
e7549107 122
9d463591 123 PasteboardSyncFlags syncFlags = PasteboardSynchronize( m_pasteboard );
6239ee05
SC
124 wxCHECK_MSG( !(syncFlags&kPasteboardModified), false, wxT("clipboard modified after clear") );
125 wxCHECK_MSG( (syncFlags&kPasteboardClientIsOwner), false, wxT("client couldn't own clipboard") );
3e822cd8 126
9d463591 127 m_data = data;
e9576ca5 128
9d463591 129 data->AddToPasteboard( m_pasteboard, 1 );
e9576ca5 130
3e822cd8 131 return true;
e9576ca5
SC
132}
133
f0822896 134void wxClipboard::Close()
e9576ca5 135{
634bafa9
SC
136 wxCHECK_RET( m_open, wxT("clipboard not open") );
137
3e822cd8 138 m_open = false;
3dee36ae 139
3e822cd8
DS
140 // Get rid of cached object.
141 // If this is not done, copying data from
142 // another application will only work once
9d463591 143 if (m_data)
89a69c60 144 {
9d463591
SC
145 delete m_data;
146 m_data = (wxDataObject*) NULL;
3dee36ae 147 }
e9576ca5
SC
148}
149
f0822896 150bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 151{
9d463591
SC
152 if ( m_data )
153 return m_data->IsSupported( dataFormat );
154 return wxDataObject::IsFormatInPasteboard( m_pasteboard, dataFormat );
e9576ca5 155}
f0822896
SC
156
157bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 158{
9005f2ed
VZ
159 if ( IsUsingPrimarySelection() )
160 return false;
161
3dee36ae 162 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
e9576ca5 163
3e822cd8
DS
164 size_t formatcount = data.GetFormatCount() + 1;
165 wxDataFormat *array = new wxDataFormat[ formatcount ];
f0822896
SC
166 array[0] = data.GetPreferredFormat();
167 data.GetAllFormats( &array[1] );
e9576ca5 168
3e822cd8 169 bool transferred = false;
e9576ca5 170
9d463591 171 if ( m_data )
f0822896 172 {
3e822cd8 173 for (size_t i = 0; !transferred && i < formatcount; i++)
21ec3bf9 174 {
3e822cd8 175 wxDataFormat format = array[ i ];
9d463591 176 if ( m_data->IsSupported( format ) )
f0822896 177 {
9d463591 178 int dataSize = m_data->GetDataSize( format );
3e822cd8 179 transferred = true;
21ec3bf9 180
3e822cd8 181 if (dataSize == 0)
21ec3bf9 182 {
3e822cd8 183 data.SetData( format, 0, 0 );
21ec3bf9
SC
184 }
185 else
186 {
3e822cd8 187 char *d = new char[ dataSize ];
9d463591 188 m_data->GetDataHere( format, (void*)d );
3e822cd8
DS
189 data.SetData( format, dataSize, d );
190 delete [] d;
21ec3bf9 191 }
f0822896 192 }
21ec3bf9 193 }
f0822896 194 }
3e822cd8
DS
195
196 // get formats from wxDataObjects
e135f093 197 if ( !transferred )
f0822896 198 {
9d463591 199 transferred = data.GetFromPasteboard( m_pasteboard ) ;
f0822896 200 }
e9576ca5 201
3e822cd8 202 delete [] array;
e542ecc6 203
3e822cd8 204 return transferred;
f0822896 205}
179e085f
RN
206
207#endif