Define _CRT_NONSTDC_NO_WARNINGS for zlib compilation with MSVC.
[wxWidgets.git] / src / osx / carbon / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor;
5 // Generalized clipboard implementation by Matthew Flatt
6 // Modified by:
7 // Created: 1998-01-01
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_CLIPBOARD
15
16 #include "wx/clipbrd.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #include "wx/app.h"
22 #include "wx/utils.h"
23 #include "wx/frame.h"
24 #include "wx/bitmap.h"
25 #endif
26
27 #include "wx/metafile.h"
28
29 #include "wx/osx/private.h"
30
31 #define wxUSE_DATAOBJ 1
32
33 #include <string.h>
34
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")
39
40 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
41
42 wxClipboard::wxClipboard()
43 {
44 m_open = false;
45 m_data = NULL ;
46 PasteboardRef clipboard = 0;
47 OSStatus err = PasteboardCreate( kPasteboardClipboard, &clipboard );
48 if (err != noErr)
49 {
50 wxLogSysError( wxT("Failed to create the clipboard.") );
51 }
52 m_pasteboard.reset(clipboard);
53 }
54
55 wxClipboard::~wxClipboard()
56 {
57 m_pasteboard.reset((PasteboardRef)0);
58 delete m_data;
59 }
60
61 void wxClipboard::Clear()
62 {
63 wxDELETE(m_data);
64
65 wxCHECK_RET( m_pasteboard, "Clipboard creation failed." );
66
67 OSStatus err = PasteboardClear( m_pasteboard );
68 if (err != noErr)
69 {
70 wxLogSysError( wxT("Failed to empty the clipboard.") );
71 }
72 }
73
74 bool wxClipboard::Flush()
75 {
76 return false;
77 }
78
79 bool wxClipboard::Open()
80 {
81 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
82
83 m_open = true;
84
85 return true;
86 }
87
88 bool wxClipboard::IsOpened() const
89 {
90 return m_open;
91 }
92
93 bool wxClipboard::SetData( wxDataObject *data )
94 {
95 if ( IsUsingPrimarySelection() )
96 return false;
97
98 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
99 wxCHECK_MSG( data, false, wxT("data is invalid") );
100
101 Clear();
102
103 // as we can only store one wxDataObject,
104 // this is the same in this implementation
105 return AddData( data );
106 }
107
108 bool wxClipboard::AddData( wxDataObject *data )
109 {
110 if ( IsUsingPrimarySelection() )
111 return false;
112
113 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
114 wxCHECK_MSG( data, false, wxT("data is invalid") );
115
116 // we can only store one wxDataObject
117 Clear();
118
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") );
122
123 m_data = data;
124
125 data->AddToPasteboard( m_pasteboard, 1 );
126
127 return true;
128 }
129
130 void wxClipboard::Close()
131 {
132 wxCHECK_RET( m_open, wxT("clipboard not open") );
133
134 m_open = false;
135
136 // Get rid of cached object.
137 // If this is not done, copying data from
138 // another application will only work once
139 wxDELETE(m_data);
140 }
141
142 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
143 {
144 wxLogTrace(TRACE_CLIPBOARD, wxT("Checking if format %s is available"),
145 dataFormat.GetId().c_str());
146
147 if ( m_data )
148 return m_data->IsSupported( dataFormat );
149 return wxDataObject::IsFormatInPasteboard( m_pasteboard, dataFormat );
150 }
151
152 bool wxClipboard::GetData( wxDataObject& data )
153 {
154 if ( IsUsingPrimarySelection() )
155 return false;
156
157 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
158
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 );
163
164 bool transferred = false;
165
166 if ( m_data )
167 {
168 for (size_t i = 0; !transferred && i < formatcount; i++)
169 {
170 wxDataFormat format = array[ i ];
171 if ( m_data->IsSupported( format ) )
172 {
173 int dataSize = m_data->GetDataSize( format );
174 transferred = true;
175
176 if (dataSize == 0)
177 {
178 data.SetData( format, 0, 0 );
179 }
180 else
181 {
182 char *d = new char[ dataSize ];
183 m_data->GetDataHere( format, (void*)d );
184 data.SetData( format, dataSize, d );
185 delete [] d;
186 }
187 }
188 }
189 }
190
191 // get formats from wxDataObjects
192 if ( !transferred )
193 {
194 transferred = data.GetFromPasteboard( m_pasteboard ) ;
195 }
196
197 delete [] array;
198
199 return transferred;
200 }
201
202 #endif