]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/clipbrd.cpp
cleanup mac
[wxWidgets.git] / src / mac / carbon / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/uma.h"
31
32 #define wxUSE_DATAOBJ 1
33
34 #include <string.h>
35
36
37 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
38
39 wxClipboard::wxClipboard()
40 {
41 m_open = false;
42 m_data = NULL ;
43 PasteboardRef clipboard = 0;
44 OSStatus err = PasteboardCreate( kPasteboardClipboard, &clipboard );
45 if (err != noErr)
46 {
47 wxLogSysError( wxT("Failed to create the clipboard.") );
48 }
49 m_pasteboard.reset(clipboard);
50 }
51
52 wxClipboard::~wxClipboard()
53 {
54 m_pasteboard.reset((PasteboardRef)0);
55 delete m_data;
56 }
57
58 void wxClipboard::Clear()
59 {
60 if (m_data != NULL)
61 {
62 delete m_data;
63 m_data = NULL;
64 }
65
66 OSStatus err = PasteboardClear( m_pasteboard );
67 if (err != noErr)
68 {
69 wxLogSysError( wxT("Failed to empty the clipboard.") );
70 }
71 }
72
73 bool wxClipboard::Flush()
74 {
75 return false;
76 }
77
78 bool wxClipboard::Open()
79 {
80 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
81
82 m_open = true;
83
84 return true;
85 }
86
87 bool wxClipboard::IsOpened() const
88 {
89 return m_open;
90 }
91
92 bool wxClipboard::SetData( wxDataObject *data )
93 {
94 if ( IsUsingPrimarySelection() )
95 return false;
96
97 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
98 wxCHECK_MSG( data, false, wxT("data is invalid") );
99
100 Clear();
101
102 // as we can only store one wxDataObject,
103 // this is the same in this implementation
104 return AddData( data );
105 }
106
107 bool wxClipboard::AddData( wxDataObject *data )
108 {
109 if ( IsUsingPrimarySelection() )
110 return false;
111
112 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
113 wxCHECK_MSG( data, false, wxT("data is invalid") );
114
115 // we can only store one wxDataObject
116 Clear();
117
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") );
121
122 m_data = data;
123
124 data->AddToPasteboard( m_pasteboard, 1 );
125
126 return true;
127 }
128
129 void wxClipboard::Close()
130 {
131 wxCHECK_RET( m_open, wxT("clipboard not open") );
132
133 m_open = false;
134
135 // Get rid of cached object.
136 // If this is not done, copying data from
137 // another application will only work once
138 if (m_data)
139 {
140 delete m_data;
141 m_data = (wxDataObject*) NULL;
142 }
143 }
144
145 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
146 {
147 wxLogTrace(TRACE_CLIPBOARD, wxT("Checking if format %s is available"),
148 dataFormat.GetId().c_str());
149
150 if ( m_data )
151 return m_data->IsSupported( dataFormat );
152 return wxDataObject::IsFormatInPasteboard( m_pasteboard, dataFormat );
153 }
154
155 bool wxClipboard::GetData( wxDataObject& data )
156 {
157 if ( IsUsingPrimarySelection() )
158 return false;
159
160 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
161
162 size_t formatcount = data.GetFormatCount() + 1;
163 wxDataFormat *array = new wxDataFormat[ formatcount ];
164 array[0] = data.GetPreferredFormat();
165 data.GetAllFormats( &array[1] );
166
167 bool transferred = false;
168
169 if ( m_data )
170 {
171 for (size_t i = 0; !transferred && i < formatcount; i++)
172 {
173 wxDataFormat format = array[ i ];
174 if ( m_data->IsSupported( format ) )
175 {
176 int dataSize = m_data->GetDataSize( format );
177 transferred = true;
178
179 if (dataSize == 0)
180 {
181 data.SetData( format, 0, 0 );
182 }
183 else
184 {
185 char *d = new char[ dataSize ];
186 m_data->GetDataHere( format, (void*)d );
187 data.SetData( format, dataSize, d );
188 delete [] d;
189 }
190 }
191 }
192 }
193
194 // get formats from wxDataObjects
195 if ( !transferred )
196 {
197 transferred = data.GetFromPasteboard( m_pasteboard ) ;
198 }
199
200 delete [] array;
201
202 return transferred;
203 }
204
205 #endif