]>
Commit | Line | Data |
---|---|---|
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 | ||
657309a5 SC |
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") | |
3e822cd8 | 40 | |
6239ee05 SC |
41 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) |
42 | ||
e7549107 SC |
43 | wxClipboard::wxClipboard() |
44 | { | |
3e822cd8 | 45 | m_open = false; |
9d463591 | 46 | m_data = NULL ; |
6239ee05 SC |
47 | PasteboardRef clipboard = 0; |
48 | OSStatus err = PasteboardCreate( kPasteboardClipboard, &clipboard ); | |
49 | if (err != noErr) | |
50 | { | |
51 | wxLogSysError( wxT("Failed to create the clipboard.") ); | |
52 | } | |
9d463591 | 53 | m_pasteboard.reset(clipboard); |
e7549107 | 54 | } |
e9576ca5 | 55 | |
e7549107 | 56 | wxClipboard::~wxClipboard() |
e9576ca5 | 57 | { |
9d463591 SC |
58 | m_pasteboard.reset((PasteboardRef)0); |
59 | delete m_data; | |
e9576ca5 SC |
60 | } |
61 | ||
e7549107 | 62 | void wxClipboard::Clear() |
e9576ca5 | 63 | { |
9d463591 | 64 | if (m_data != NULL) |
f0822896 | 65 | { |
9d463591 SC |
66 | delete m_data; |
67 | m_data = NULL; | |
f0822896 | 68 | } |
3e822cd8 | 69 | |
9d463591 | 70 | OSStatus err = PasteboardClear( m_pasteboard ); |
3e822cd8 | 71 | if (err != noErr) |
ed60b502 | 72 | { |
3e822cd8 | 73 | wxLogSysError( wxT("Failed to empty the clipboard.") ); |
ed60b502 | 74 | } |
e9576ca5 SC |
75 | } |
76 | ||
e7549107 | 77 | bool wxClipboard::Flush() |
e9576ca5 | 78 | { |
3dee36ae | 79 | return false; |
e7549107 SC |
80 | } |
81 | ||
82 | bool wxClipboard::Open() | |
83 | { | |
3dee36ae | 84 | wxCHECK_MSG( !m_open, false, wxT("clipboard already open") ); |
3e822cd8 DS |
85 | |
86 | m_open = true; | |
87 | ||
88 | return true; | |
e7549107 SC |
89 | } |
90 | ||
91 | bool wxClipboard::IsOpened() const | |
92 | { | |
f0822896 | 93 | return m_open; |
e9576ca5 SC |
94 | } |
95 | ||
f0822896 | 96 | bool wxClipboard::SetData( wxDataObject *data ) |
e9576ca5 | 97 | { |
9005f2ed VZ |
98 | if ( IsUsingPrimarySelection() ) |
99 | return false; | |
100 | ||
3dee36ae | 101 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); |
3dee36ae | 102 | wxCHECK_MSG( data, false, wxT("data is invalid") ); |
634bafa9 SC |
103 | |
104 | Clear(); | |
3e822cd8 DS |
105 | |
106 | // as we can only store one wxDataObject, | |
107 | // this is the same in this implementation | |
f0822896 | 108 | return AddData( data ); |
e7549107 SC |
109 | } |
110 | ||
111 | bool wxClipboard::AddData( wxDataObject *data ) | |
112 | { | |
9005f2ed VZ |
113 | if ( IsUsingPrimarySelection() ) |
114 | return false; | |
115 | ||
3dee36ae | 116 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); |
3dee36ae | 117 | wxCHECK_MSG( data, false, wxT("data is invalid") ); |
e7549107 | 118 | |
3e822cd8 | 119 | // we can only store one wxDataObject |
f0822896 | 120 | Clear(); |
e7549107 | 121 | |
9d463591 | 122 | PasteboardSyncFlags syncFlags = PasteboardSynchronize( m_pasteboard ); |
6239ee05 SC |
123 | wxCHECK_MSG( !(syncFlags&kPasteboardModified), false, wxT("clipboard modified after clear") ); |
124 | wxCHECK_MSG( (syncFlags&kPasteboardClientIsOwner), false, wxT("client couldn't own clipboard") ); | |
3e822cd8 | 125 | |
9d463591 | 126 | m_data = data; |
e9576ca5 | 127 | |
9d463591 | 128 | data->AddToPasteboard( m_pasteboard, 1 ); |
e9576ca5 | 129 | |
3e822cd8 | 130 | return true; |
e9576ca5 SC |
131 | } |
132 | ||
f0822896 | 133 | void wxClipboard::Close() |
e9576ca5 | 134 | { |
634bafa9 SC |
135 | wxCHECK_RET( m_open, wxT("clipboard not open") ); |
136 | ||
3e822cd8 | 137 | m_open = false; |
3dee36ae | 138 | |
3e822cd8 DS |
139 | // Get rid of cached object. |
140 | // If this is not done, copying data from | |
141 | // another application will only work once | |
9d463591 | 142 | if (m_data) |
89a69c60 | 143 | { |
9d463591 SC |
144 | delete m_data; |
145 | m_data = (wxDataObject*) NULL; | |
3dee36ae | 146 | } |
e9576ca5 SC |
147 | } |
148 | ||
f0822896 | 149 | bool wxClipboard::IsSupported( const wxDataFormat &dataFormat ) |
e7549107 | 150 | { |
e1673e52 SC |
151 | wxLogTrace(TRACE_CLIPBOARD, wxT("Checking if format %s is available"), |
152 | dataFormat.GetId().c_str()); | |
153 | ||
9d463591 SC |
154 | if ( m_data ) |
155 | return m_data->IsSupported( dataFormat ); | |
156 | return wxDataObject::IsFormatInPasteboard( m_pasteboard, dataFormat ); | |
e9576ca5 | 157 | } |
f0822896 SC |
158 | |
159 | bool wxClipboard::GetData( wxDataObject& data ) | |
e9576ca5 | 160 | { |
9005f2ed VZ |
161 | if ( IsUsingPrimarySelection() ) |
162 | return false; | |
163 | ||
3dee36ae | 164 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); |
e9576ca5 | 165 | |
3e822cd8 DS |
166 | size_t formatcount = data.GetFormatCount() + 1; |
167 | wxDataFormat *array = new wxDataFormat[ formatcount ]; | |
f0822896 SC |
168 | array[0] = data.GetPreferredFormat(); |
169 | data.GetAllFormats( &array[1] ); | |
e9576ca5 | 170 | |
3e822cd8 | 171 | bool transferred = false; |
e9576ca5 | 172 | |
9d463591 | 173 | if ( m_data ) |
f0822896 | 174 | { |
3e822cd8 | 175 | for (size_t i = 0; !transferred && i < formatcount; i++) |
21ec3bf9 | 176 | { |
3e822cd8 | 177 | wxDataFormat format = array[ i ]; |
9d463591 | 178 | if ( m_data->IsSupported( format ) ) |
f0822896 | 179 | { |
9d463591 | 180 | int dataSize = m_data->GetDataSize( format ); |
3e822cd8 | 181 | transferred = true; |
21ec3bf9 | 182 | |
3e822cd8 | 183 | if (dataSize == 0) |
21ec3bf9 | 184 | { |
3e822cd8 | 185 | data.SetData( format, 0, 0 ); |
21ec3bf9 SC |
186 | } |
187 | else | |
188 | { | |
3e822cd8 | 189 | char *d = new char[ dataSize ]; |
9d463591 | 190 | m_data->GetDataHere( format, (void*)d ); |
3e822cd8 DS |
191 | data.SetData( format, dataSize, d ); |
192 | delete [] d; | |
21ec3bf9 | 193 | } |
f0822896 | 194 | } |
21ec3bf9 | 195 | } |
f0822896 | 196 | } |
3e822cd8 DS |
197 | |
198 | // get formats from wxDataObjects | |
e135f093 | 199 | if ( !transferred ) |
f0822896 | 200 | { |
9d463591 | 201 | transferred = data.GetFromPasteboard( m_pasteboard ) ; |
f0822896 | 202 | } |
e9576ca5 | 203 | |
3e822cd8 | 204 | delete [] array; |
e542ecc6 | 205 | |
3e822cd8 | 206 | return transferred; |
f0822896 | 207 | } |
179e085f RN |
208 | |
209 | #endif |