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