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