]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: clipbrd.cpp | |
3 | // Purpose: Clipboard functionality | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
26f86486 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
26f86486 | 21 | #pragma implementation "clipbrd.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
26f86486 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
26f86486 | 32 | #include "wx/setup.h" |
2bda0e17 KB |
33 | #endif |
34 | ||
47d67540 | 35 | #if wxUSE_CLIPBOARD |
2bda0e17 KB |
36 | |
37 | #ifndef WX_PRECOMP | |
26f86486 VZ |
38 | #include "wx/object.h" |
39 | #include "wx/event.h" | |
40 | #include "wx/app.h" | |
41 | #include "wx/frame.h" | |
42 | #include "wx/bitmap.h" | |
43 | #include "wx/utils.h" | |
0c589ad0 | 44 | #include "wx/intl.h" |
2bda0e17 KB |
45 | #endif |
46 | ||
06e43511 | 47 | #if wxUSE_METAFILE |
26f86486 | 48 | #include "wx/metafile.h" |
06e43511 JS |
49 | #endif |
50 | ||
dbda9e86 | 51 | #include "wx/log.h" |
2bda0e17 | 52 | #include "wx/clipbrd.h" |
06e43511 | 53 | |
3f480da3 | 54 | #include <string.h> |
06e43511 | 55 | |
2bda0e17 | 56 | #include "wx/msw/private.h" |
8cb172b4 | 57 | |
4676948b | 58 | #if wxUSE_WXDIB |
2bda0e17 | 59 | #include "wx/msw/dib.h" |
8cb172b4 | 60 | #endif |
2bda0e17 | 61 | |
d59ceba5 VZ |
62 | // wxDataObject is tied to OLE/drag and drop implementation, therefore so are |
63 | // the functions using wxDataObject in wxClipboard | |
1e6feb95 | 64 | //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP |
d59ceba5 VZ |
65 | |
66 | #if wxUSE_DATAOBJ | |
26f86486 | 67 | #include "wx/dataobj.h" |
bd52bee1 | 68 | #endif |
3f480da3 | 69 | |
f07dc2e2 | 70 | #if wxUSE_OLE && !defined(__WXWINCE__) |
b94e73ae VZ |
71 | // use OLE clipboard |
72 | #define wxUSE_OLE_CLIPBOARD 1 | |
d59ceba5 VZ |
73 | #else // !wxUSE_DATAOBJ |
74 | // use Win clipboard API | |
75 | #define wxUSE_OLE_CLIPBOARD 0 | |
2bda0e17 KB |
76 | #endif |
77 | ||
d59ceba5 VZ |
78 | #if wxUSE_OLE_CLIPBOARD |
79 | #include <ole2.h> | |
80 | #endif // wxUSE_OLE_CLIPBOARD | |
81 | ||
26f86486 VZ |
82 | // =========================================================================== |
83 | // implementation | |
84 | // =========================================================================== | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | // old-style clipboard functions using Windows API | |
88 | // --------------------------------------------------------------------------- | |
2bda0e17 | 89 | |
26f86486 VZ |
90 | static bool gs_wxClipboardIsOpen = FALSE; |
91 | ||
92 | bool wxOpenClipboard() | |
2bda0e17 | 93 | { |
223d09f6 | 94 | wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, wxT("clipboard already opened.") ); |
26f86486 VZ |
95 | |
96 | wxWindow *win = wxTheApp->GetTopWindow(); | |
97 | if ( win ) | |
98 | { | |
99 | gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0; | |
100 | ||
101 | if ( !gs_wxClipboardIsOpen ) | |
102 | wxLogSysError(_("Failed to open the clipboard.")); | |
103 | ||
104 | return gs_wxClipboardIsOpen; | |
105 | } | |
106 | else | |
107 | { | |
223d09f6 | 108 | wxLogDebug(wxT("Can not open clipboard without a main window.")); |
26f86486 VZ |
109 | |
110 | return FALSE; | |
111 | } | |
2bda0e17 KB |
112 | } |
113 | ||
26f86486 | 114 | bool wxCloseClipboard() |
2bda0e17 | 115 | { |
223d09f6 | 116 | wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, wxT("clipboard is not opened") ); |
26f86486 VZ |
117 | |
118 | gs_wxClipboardIsOpen = FALSE; | |
119 | ||
120 | if ( ::CloseClipboard() == 0 ) | |
121 | { | |
122 | wxLogSysError(_("Failed to close the clipboard.")); | |
123 | ||
124 | return FALSE; | |
125 | } | |
126 | ||
127 | return TRUE; | |
2bda0e17 KB |
128 | } |
129 | ||
26f86486 | 130 | bool wxEmptyClipboard() |
2bda0e17 | 131 | { |
26f86486 VZ |
132 | if ( ::EmptyClipboard() == 0 ) |
133 | { | |
134 | wxLogSysError(_("Failed to empty the clipboard.")); | |
135 | ||
136 | return FALSE; | |
137 | } | |
138 | ||
139 | return TRUE; | |
2bda0e17 KB |
140 | } |
141 | ||
26f86486 | 142 | bool wxIsClipboardOpened() |
2bda0e17 | 143 | { |
26f86486 | 144 | return gs_wxClipboardIsOpen; |
2bda0e17 KB |
145 | } |
146 | ||
06e43511 | 147 | bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat) |
2bda0e17 | 148 | { |
8cc4850c | 149 | wxDataFormat::NativeFormat cf = dataFormat.GetFormatId(); |
6c29712c JS |
150 | |
151 | if ( ::IsClipboardFormatAvailable(cf) ) | |
d9317fd4 VZ |
152 | { |
153 | // ok from the first try | |
154 | return TRUE; | |
155 | } | |
156 | ||
157 | // for several standard formats, we can convert from some other ones too | |
6c29712c | 158 | switch ( cf ) |
d9317fd4 VZ |
159 | { |
160 | // for bitmaps, DIBs will also do | |
161 | case CF_BITMAP: | |
162 | return ::IsClipboardFormatAvailable(CF_DIB) != 0; | |
163 | ||
3a5bcc4d | 164 | #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__) |
d9317fd4 VZ |
165 | case CF_METAFILEPICT: |
166 | return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0; | |
167 | #endif // wxUSE_ENH_METAFILE | |
168 | ||
169 | default: | |
170 | return FALSE; | |
171 | } | |
2bda0e17 KB |
172 | } |
173 | ||
e9196d9c | 174 | |
26f86486 VZ |
175 | bool wxSetClipboardData(wxDataFormat dataFormat, |
176 | const void *data, | |
177 | int width, int height) | |
2bda0e17 | 178 | { |
26f86486 VZ |
179 | HANDLE handle = 0; // return value of SetClipboardData |
180 | ||
181 | switch (dataFormat) | |
2bda0e17 | 182 | { |
26f86486 VZ |
183 | case wxDF_BITMAP: |
184 | { | |
185 | wxBitmap *bitmap = (wxBitmap *)data; | |
186 | ||
187 | HDC hdcMem = CreateCompatibleDC((HDC) NULL); | |
188 | HDC hdcSrc = CreateCompatibleDC((HDC) NULL); | |
189 | HBITMAP old = (HBITMAP) | |
190 | ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP()); | |
191 | HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, | |
192 | bitmap->GetWidth(), | |
193 | bitmap->GetHeight()); | |
194 | if (!hBitmap) | |
195 | { | |
196 | SelectObject(hdcSrc, old); | |
197 | DeleteDC(hdcMem); | |
198 | DeleteDC(hdcSrc); | |
199 | return FALSE; | |
200 | } | |
201 | ||
202 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap); | |
203 | BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(), | |
204 | hdcSrc, 0, 0, SRCCOPY); | |
205 | ||
206 | // Select new bitmap out of memory DC | |
207 | SelectObject(hdcMem, old1); | |
208 | ||
209 | // Set the data | |
210 | handle = ::SetClipboardData(CF_BITMAP, hBitmap); | |
211 | ||
212 | // Clean up | |
213 | SelectObject(hdcSrc, old); | |
214 | DeleteDC(hdcSrc); | |
215 | DeleteDC(hdcMem); | |
216 | break; | |
217 | } | |
218 | ||
4676948b | 219 | #if wxUSE_WXDIB |
26f86486 VZ |
220 | case wxDF_DIB: |
221 | { | |
26f86486 | 222 | wxBitmap *bitmap = (wxBitmap *)data; |
2b254edf | 223 | |
c2517b52 | 224 | if ( bitmap && bitmap->Ok() ) |
2b254edf | 225 | { |
c2517b52 VZ |
226 | wxDIB dib(*bitmap); |
227 | if ( dib.IsOk() ) | |
228 | { | |
229 | handle = ::SetClipboardData(CF_DIB, dib.Detach()); | |
230 | } | |
2b254edf | 231 | } |
26f86486 VZ |
232 | break; |
233 | } | |
4676948b | 234 | #endif |
26f86486 | 235 | |
d9317fd4 | 236 | // VZ: I'm told that this code works, but it doesn't seem to work for me |
4676948b | 237 | // and, anyhow, I'd be highly surprised if it did. So I leave it here |
d9317fd4 | 238 | // but IMNSHO it is completely broken. |
4676948b | 239 | #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__) |
26f86486 VZ |
240 | case wxDF_METAFILE: |
241 | { | |
242 | wxMetafile *wxMF = (wxMetafile *)data; | |
243 | HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1); | |
26f86486 | 244 | METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data); |
2bda0e17 | 245 | |
26f86486 VZ |
246 | mf->mm = wxMF->GetWindowsMappingMode(); |
247 | mf->xExt = width; | |
248 | mf->yExt = height; | |
249 | mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE(); | |
250 | GlobalUnlock(data); | |
251 | wxMF->SetHMETAFILE((WXHANDLE) NULL); | |
2bda0e17 | 252 | |
26f86486 VZ |
253 | handle = SetClipboardData(CF_METAFILEPICT, data); |
254 | break; | |
255 | } | |
d9317fd4 VZ |
256 | #endif // wxUSE_METAFILE |
257 | ||
3a5bcc4d | 258 | #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__) |
d9317fd4 VZ |
259 | case wxDF_ENHMETAFILE: |
260 | { | |
261 | wxEnhMetaFile *emf = (wxEnhMetaFile *)data; | |
262 | wxEnhMetaFile emfCopy = *emf; | |
263 | ||
264 | handle = SetClipboardData(CF_ENHMETAFILE, | |
265 | (void *)emfCopy.GetHENHMETAFILE()); | |
266 | } | |
267 | break; | |
268 | #endif // wxUSE_ENH_METAFILE | |
269 | ||
26f86486 VZ |
270 | case CF_SYLK: |
271 | case CF_DIF: | |
272 | case CF_TIFF: | |
273 | case CF_PALETTE: | |
274 | default: | |
275 | { | |
276 | wxLogError(_("Unsupported clipboard format.")); | |
277 | return FALSE; | |
278 | } | |
2bda0e17 | 279 | |
26f86486 VZ |
280 | case wxDF_OEMTEXT: |
281 | dataFormat = wxDF_TEXT; | |
282 | // fall through | |
2bda0e17 | 283 | |
26f86486 VZ |
284 | case wxDF_TEXT: |
285 | { | |
286 | char *s = (char *)data; | |
287 | ||
288 | width = strlen(s) + 1; | |
289 | height = 1; | |
290 | DWORD l = (width * height); | |
291 | HANDLE hGlobalMemory = GlobalAlloc(GHND, l); | |
292 | if ( hGlobalMemory ) | |
293 | { | |
26f86486 | 294 | LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory); |
2bda0e17 | 295 | |
26f86486 | 296 | memcpy(lpGlobalMemory, s, l); |
2bda0e17 | 297 | |
26f86486 VZ |
298 | GlobalUnlock(hGlobalMemory); |
299 | } | |
2bda0e17 | 300 | |
26f86486 VZ |
301 | handle = SetClipboardData(dataFormat, hGlobalMemory); |
302 | break; | |
303 | } | |
387ebd3e JS |
304 | // Only tested with non-Unicode, Visual C++ 6.0 so far |
305 | #if defined(__VISUALC__) && !defined(UNICODE) | |
306 | case wxDF_HTML: | |
307 | { | |
308 | char* html = (char *)data; | |
309 | ||
310 | // Create temporary buffer for HTML header... | |
311 | char *buf = new char [400 + strlen(html)]; | |
312 | if(!buf) return FALSE; | |
313 | ||
314 | // Get clipboard id for HTML format... | |
315 | static int cfid = 0; | |
316 | if(!cfid) cfid = RegisterClipboardFormat(wxT("HTML Format")); | |
317 | ||
318 | // Create a template string for the HTML header... | |
319 | strcpy(buf, | |
320 | "Version:0.9\r\n" | |
321 | "StartHTML:00000000\r\n" | |
322 | "EndHTML:00000000\r\n" | |
323 | "StartFragment:00000000\r\n" | |
324 | "EndFragment:00000000\r\n" | |
325 | "<html><body>\r\n" | |
326 | "<!--StartFragment -->\r\n"); | |
327 | ||
328 | // Append the HTML... | |
329 | strcat(buf, html); | |
330 | strcat(buf, "\r\n"); | |
331 | // Finish up the HTML format... | |
332 | strcat(buf, | |
333 | "<!--EndFragment-->\r\n" | |
334 | "</body>\r\n" | |
335 | "</html>"); | |
336 | ||
337 | // Now go back, calculate all the lengths, and write out the | |
338 | // necessary header information. Note, wsprintf() truncates the | |
339 | // string when you overwrite it so you follow up with code to replace | |
340 | // the 0 appended at the end with a '\r'... | |
341 | char *ptr = strstr(buf, "StartHTML"); | |
342 | wsprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf); | |
343 | *(ptr+10+8) = '\r'; | |
344 | ||
345 | ptr = strstr(buf, "EndHTML"); | |
346 | wsprintf(ptr+8, "%08u", strlen(buf)); | |
347 | *(ptr+8+8) = '\r'; | |
348 | ||
349 | ptr = strstr(buf, "StartFragment"); | |
350 | wsprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf); | |
351 | *(ptr+14+8) = '\r'; | |
352 | ||
353 | ptr = strstr(buf, "EndFragment"); | |
354 | wsprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf); | |
355 | *(ptr+12+8) = '\r'; | |
356 | ||
357 | // Now you have everything in place ready to put on the | |
358 | // clipboard. | |
359 | ||
360 | // Allocate global memory for transfer... | |
361 | HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4); | |
362 | ||
363 | // Put your string in the global memory... | |
364 | ptr = (char *)GlobalLock(hText); | |
365 | strcpy(ptr, buf); | |
366 | GlobalUnlock(hText); | |
367 | ||
368 | handle = ::SetClipboardData(cfid, hText); | |
369 | ||
370 | // Free memory... | |
371 | GlobalFree(hText); | |
372 | ||
373 | // Clean up... | |
374 | delete [] buf; | |
375 | break; | |
376 | } | |
377 | #endif | |
2bda0e17 | 378 | } |
26f86486 VZ |
379 | |
380 | if ( handle == 0 ) | |
2bda0e17 | 381 | { |
26f86486 VZ |
382 | wxLogSysError(_("Failed to set clipboard data.")); |
383 | ||
384 | return FALSE; | |
2bda0e17 | 385 | } |
26f86486 VZ |
386 | |
387 | return TRUE; | |
388 | } | |
389 | ||
390 | void *wxGetClipboardData(wxDataFormat dataFormat, long *len) | |
391 | { | |
392 | void *retval = NULL; | |
393 | ||
394 | switch ( dataFormat ) | |
2bda0e17 | 395 | { |
4676948b | 396 | #ifndef __WXWINCE__ |
26f86486 VZ |
397 | case wxDF_BITMAP: |
398 | { | |
399 | BITMAP bm; | |
400 | HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP); | |
401 | if (!hBitmap) | |
402 | break; | |
403 | ||
404 | HDC hdcMem = CreateCompatibleDC((HDC) NULL); | |
405 | HDC hdcSrc = CreateCompatibleDC((HDC) NULL); | |
406 | ||
407 | HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap); | |
408 | GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm); | |
409 | ||
410 | HBITMAP hNewBitmap = CreateBitmapIndirect(&bm); | |
411 | ||
412 | if (!hNewBitmap) | |
413 | { | |
414 | SelectObject(hdcSrc, old); | |
415 | DeleteDC(hdcMem); | |
416 | DeleteDC(hdcSrc); | |
417 | break; | |
418 | } | |
419 | ||
420 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap); | |
421 | BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, | |
422 | hdcSrc, 0, 0, SRCCOPY); | |
423 | ||
424 | // Select new bitmap out of memory DC | |
425 | SelectObject(hdcMem, old1); | |
426 | ||
427 | // Clean up | |
428 | SelectObject(hdcSrc, old); | |
429 | DeleteDC(hdcSrc); | |
430 | DeleteDC(hdcMem); | |
431 | ||
432 | // Create and return a new wxBitmap | |
433 | wxBitmap *wxBM = new wxBitmap; | |
434 | wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap); | |
435 | wxBM->SetWidth(bm.bmWidth); | |
436 | wxBM->SetHeight(bm.bmHeight); | |
437 | wxBM->SetDepth(bm.bmPlanes); | |
26f86486 VZ |
438 | retval = wxBM; |
439 | break; | |
440 | } | |
4676948b | 441 | #endif |
26f86486 VZ |
442 | case wxDF_METAFILE: |
443 | case CF_SYLK: | |
444 | case CF_DIF: | |
445 | case CF_TIFF: | |
446 | case CF_PALETTE: | |
447 | case wxDF_DIB: | |
f7f50f49 VZ |
448 | wxLogError(_("Unsupported clipboard format.")); |
449 | return NULL; | |
26f86486 VZ |
450 | |
451 | case wxDF_OEMTEXT: | |
452 | dataFormat = wxDF_TEXT; | |
453 | // fall through | |
454 | ||
455 | case wxDF_TEXT: | |
456 | { | |
457 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
458 | if (!hGlobalMemory) | |
459 | break; | |
2bda0e17 | 460 | |
26f86486 VZ |
461 | DWORD hsize = ::GlobalSize(hGlobalMemory); |
462 | if (len) | |
463 | *len = hsize; | |
2bda0e17 | 464 | |
26f86486 VZ |
465 | char *s = new char[hsize]; |
466 | if (!s) | |
467 | break; | |
2bda0e17 | 468 | |
4676948b | 469 | LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory); |
2bda0e17 | 470 | |
26f86486 | 471 | memcpy(s, lpGlobalMemory, hsize); |
2bda0e17 | 472 | |
4676948b | 473 | GlobalUnlock(hGlobalMemory); |
26f86486 VZ |
474 | |
475 | retval = s; | |
476 | break; | |
477 | } | |
3f480da3 VZ |
478 | |
479 | default: | |
480 | { | |
481 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
482 | if ( !hGlobalMemory ) | |
483 | break; | |
484 | ||
485 | DWORD size = ::GlobalSize(hGlobalMemory); | |
486 | if ( len ) | |
487 | *len = size; | |
488 | ||
489 | void *buf = malloc(size); | |
490 | if ( !buf ) | |
491 | break; | |
492 | ||
4676948b | 493 | LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory); |
3f480da3 VZ |
494 | |
495 | memcpy(buf, lpGlobalMemory, size); | |
496 | ||
4676948b | 497 | GlobalUnlock(hGlobalMemory); |
3f480da3 VZ |
498 | |
499 | retval = buf; | |
500 | break; | |
501 | } | |
26f86486 | 502 | } |
2bda0e17 | 503 | |
26f86486 VZ |
504 | if ( !retval ) |
505 | { | |
506 | wxLogSysError(_("Failed to retrieve data from the clipboard.")); | |
2bda0e17 | 507 | } |
26f86486 VZ |
508 | |
509 | return retval; | |
2bda0e17 KB |
510 | } |
511 | ||
3f480da3 | 512 | wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat) |
2bda0e17 | 513 | { |
33ac7e6f | 514 | return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat); |
2bda0e17 KB |
515 | } |
516 | ||
837e5743 | 517 | int wxRegisterClipboardFormat(wxChar *formatName) |
2bda0e17 KB |
518 | { |
519 | return ::RegisterClipboardFormat(formatName); | |
520 | } | |
521 | ||
26f86486 | 522 | bool wxGetClipboardFormatName(wxDataFormat dataFormat, |
837e5743 | 523 | wxChar *formatName, |
26f86486 | 524 | int maxCount) |
2bda0e17 | 525 | { |
26f86486 | 526 | return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0; |
2bda0e17 KB |
527 | } |
528 | ||
26f86486 | 529 | // --------------------------------------------------------------------------- |
06e43511 | 530 | // wxClipboard |
26f86486 | 531 | // --------------------------------------------------------------------------- |
2bda0e17 | 532 | |
26f86486 | 533 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) |
2bda0e17 KB |
534 | |
535 | wxClipboard::wxClipboard() | |
536 | { | |
d59ceba5 | 537 | m_clearOnExit = FALSE; |
a36d790a | 538 | m_isOpened = FALSE; |
2bda0e17 KB |
539 | } |
540 | ||
541 | wxClipboard::~wxClipboard() | |
542 | { | |
d59ceba5 VZ |
543 | if ( m_clearOnExit ) |
544 | { | |
545 | Clear(); | |
546 | } | |
2bda0e17 KB |
547 | } |
548 | ||
06e43511 | 549 | void wxClipboard::Clear() |
2bda0e17 | 550 | { |
d59ceba5 VZ |
551 | #if wxUSE_OLE_CLIPBOARD |
552 | if ( FAILED(OleSetClipboard(NULL)) ) | |
553 | { | |
f6bcfd97 | 554 | wxLogLastError(wxT("OleSetClipboard(NULL)")); |
d59ceba5 VZ |
555 | } |
556 | #endif | |
557 | } | |
558 | ||
559 | bool wxClipboard::Flush() | |
560 | { | |
7ffdaf81 | 561 | #if wxUSE_OLE_CLIPBOARD |
d59ceba5 VZ |
562 | if ( FAILED(OleFlushClipboard()) ) |
563 | { | |
f6bcfd97 | 564 | wxLogLastError(wxT("OleFlushClipboard")); |
d59ceba5 VZ |
565 | |
566 | return FALSE; | |
567 | } | |
568 | else | |
569 | { | |
570 | m_clearOnExit = FALSE; | |
571 | ||
572 | return TRUE; | |
573 | } | |
7ffdaf81 VZ |
574 | #else // !wxUSE_OLE_CLIPBOARD |
575 | return FALSE; | |
576 | #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD | |
2bda0e17 KB |
577 | } |
578 | ||
06e43511 | 579 | bool wxClipboard::Open() |
2bda0e17 | 580 | { |
d59ceba5 | 581 | // OLE opens clipboard for us |
a36d790a | 582 | m_isOpened = TRUE; |
d59ceba5 VZ |
583 | #if wxUSE_OLE_CLIPBOARD |
584 | return TRUE; | |
585 | #else | |
06e43511 | 586 | return wxOpenClipboard(); |
d59ceba5 | 587 | #endif |
2bda0e17 KB |
588 | } |
589 | ||
f536e0f2 VZ |
590 | bool wxClipboard::IsOpened() const |
591 | { | |
592 | #if wxUSE_OLE_CLIPBOARD | |
a36d790a | 593 | return m_isOpened; |
f536e0f2 VZ |
594 | #else |
595 | return wxIsClipboardOpened(); | |
596 | #endif | |
597 | } | |
598 | ||
06e43511 | 599 | bool wxClipboard::SetData( wxDataObject *data ) |
2bda0e17 | 600 | { |
51edda6a | 601 | #if !wxUSE_OLE_CLIPBOARD |
26f86486 | 602 | (void)wxEmptyClipboard(); |
51edda6a | 603 | #endif // wxUSE_OLE_CLIPBOARD |
26f86486 VZ |
604 | |
605 | if ( data ) | |
606 | return AddData(data); | |
607 | else | |
608 | return TRUE; | |
609 | } | |
610 | ||
611 | bool wxClipboard::AddData( wxDataObject *data ) | |
612 | { | |
223d09f6 | 613 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); |
06e43511 | 614 | |
d59ceba5 VZ |
615 | #if wxUSE_OLE_CLIPBOARD |
616 | HRESULT hr = OleSetClipboard(data->GetInterface()); | |
617 | if ( FAILED(hr) ) | |
618 | { | |
619 | wxLogSysError(hr, _("Failed to put data on the clipboard")); | |
620 | ||
621 | // don't free anything in this case | |
622 | ||
623 | return FALSE; | |
624 | } | |
625 | ||
626 | // we have a problem here because we should delete wxDataObject, but we | |
627 | // can't do it because IDataObject which we just gave to the clipboard | |
628 | // would try to use it when it will need the data. IDataObject is ref | |
629 | // counted and so doesn't suffer from such problem, so we release it now | |
630 | // and tell it to delete wxDataObject when it is deleted itself. | |
631 | data->SetAutoDelete(); | |
632 | ||
633 | // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when | |
634 | // using OLE clipboard when the app terminates - by default, we call | |
635 | // OleSetClipboard(NULL) which won't waste RAM, but the app can call | |
636 | // wxClipboard::Flush() to chaneg this | |
637 | m_clearOnExit = TRUE; | |
638 | ||
639 | return TRUE; | |
640 | #elif wxUSE_DATAOBJ | |
223d09f6 | 641 | wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") ); |
26f86486 | 642 | |
7fc0bd1c | 643 | wxDataFormat format = data->GetPreferredFormat(); |
26f86486 VZ |
644 | |
645 | switch ( format ) | |
06e43511 JS |
646 | { |
647 | case wxDF_TEXT: | |
648 | case wxDF_OEMTEXT: | |
649 | { | |
650 | wxTextDataObject* textDataObject = (wxTextDataObject*) data; | |
651 | wxString str(textDataObject->GetText()); | |
26f86486 | 652 | return wxSetClipboardData(format, str.c_str()); |
06e43511 | 653 | } |
26f86486 | 654 | |
06e43511 JS |
655 | case wxDF_BITMAP: |
656 | case wxDF_DIB: | |
657 | { | |
658 | wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data; | |
659 | wxBitmap bitmap(bitmapDataObject->GetBitmap()); | |
7fc0bd1c | 660 | return wxSetClipboardData(data->GetPreferredFormat(), &bitmap); |
06e43511 | 661 | } |
26f86486 | 662 | |
06e43511 JS |
663 | #if wxUSE_METAFILE |
664 | case wxDF_METAFILE: | |
665 | { | |
7fc0bd1c JS |
666 | #if 1 |
667 | // TODO | |
668 | wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values."); | |
669 | return FALSE; | |
670 | #else | |
33ac7e6f | 671 | wxMetafileDataObject* metaFileDataObject = |
26f86486 | 672 | (wxMetafileDataObject*) data; |
06e43511 | 673 | wxMetafile metaFile = metaFileDataObject->GetMetafile(); |
26f86486 VZ |
674 | return wxSetClipboardData(wxDF_METAFILE, &metaFile, |
675 | metaFileDataObject->GetWidth(), | |
676 | metaFileDataObject->GetHeight()); | |
7fc0bd1c | 677 | #endif |
06e43511 | 678 | } |
26f86486 VZ |
679 | #endif // wxUSE_METAFILE |
680 | ||
06e43511 | 681 | default: |
7fc0bd1c JS |
682 | { |
683 | // This didn't compile, of course | |
684 | // return wxSetClipboardData(data); | |
685 | // TODO | |
f07dc2e2 | 686 | wxLogError(wxT("Not implemented.")); |
7fc0bd1c JS |
687 | return FALSE; |
688 | } | |
06e43511 | 689 | } |
d59ceba5 | 690 | #else // !wxUSE_DATAOBJ |
06e43511 | 691 | return FALSE; |
d59ceba5 | 692 | #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ |
2bda0e17 KB |
693 | } |
694 | ||
06e43511 | 695 | void wxClipboard::Close() |
2bda0e17 | 696 | { |
a36d790a | 697 | m_isOpened = FALSE; |
d59ceba5 VZ |
698 | // OLE closes clipboard for us |
699 | #if !wxUSE_OLE_CLIPBOARD | |
06e43511 | 700 | wxCloseClipboard(); |
d59ceba5 | 701 | #endif |
2bda0e17 KB |
702 | } |
703 | ||
26f86486 | 704 | bool wxClipboard::IsSupported( wxDataFormat format ) |
2bda0e17 | 705 | { |
06e43511 | 706 | return wxIsClipboardFormatAvailable(format); |
2bda0e17 KB |
707 | } |
708 | ||
1e8335b0 | 709 | bool wxClipboard::GetData( wxDataObject& data ) |
2bda0e17 | 710 | { |
d59ceba5 VZ |
711 | #if wxUSE_OLE_CLIPBOARD |
712 | IDataObject *pDataObject = NULL; | |
713 | HRESULT hr = OleGetClipboard(&pDataObject); | |
714 | if ( FAILED(hr) || !pDataObject ) | |
715 | { | |
716 | wxLogSysError(hr, _("Failed to get data from the clipboard")); | |
717 | ||
718 | return FALSE; | |
719 | } | |
720 | ||
721 | // build the list of supported formats | |
1e8335b0 | 722 | size_t nFormats = data.GetFormatCount(wxDataObject::Set); |
33ac7e6f | 723 | wxDataFormat format; |
c5639a87 | 724 | wxDataFormat *formats; |
d59ceba5 VZ |
725 | if ( nFormats == 1 ) |
726 | { | |
727 | // the most common case | |
728 | formats = &format; | |
729 | } | |
730 | else | |
731 | { | |
732 | // bad luck, need to alloc mem | |
733 | formats = new wxDataFormat[nFormats]; | |
734 | } | |
735 | ||
1e8335b0 | 736 | data.GetAllFormats(formats, wxDataObject::Set); |
d59ceba5 | 737 | |
6c29712c JS |
738 | // get the data for the given formats |
739 | FORMATETC formatEtc; | |
740 | CLIPFORMAT cf; | |
d59ceba5 | 741 | bool result = FALSE; |
6c29712c JS |
742 | |
743 | // enumerate all explicit formats on the clipboard. | |
744 | // note that this does not include implicit / synthetic (automatically | |
745 | // converted) formats. | |
746 | #ifdef __WXDEBUG__ | |
747 | // get the format enumerator | |
d59ceba5 VZ |
748 | IEnumFORMATETC *pEnumFormatEtc = NULL; |
749 | hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc); | |
750 | if ( FAILED(hr) || !pEnumFormatEtc ) | |
751 | { | |
752 | wxLogSysError(hr, | |
753 | _("Failed to retrieve the supported clipboard formats")); | |
754 | } | |
755 | else | |
756 | { | |
757 | // ask for the supported formats and see if there are any we support | |
d59ceba5 VZ |
758 | for ( ;; ) |
759 | { | |
760 | ULONG nCount; | |
761 | hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount); | |
762 | ||
763 | // don't use FAILED() because S_FALSE would pass it | |
764 | if ( hr != S_OK ) | |
765 | { | |
766 | // no more formats | |
767 | break; | |
768 | } | |
769 | ||
6c29712c | 770 | cf = formatEtc.cfFormat; |
d59ceba5 | 771 | |
d59ceba5 VZ |
772 | wxLogTrace(wxTRACE_OleCalls, |
773 | wxT("Object on the clipboard supports format %s."), | |
774 | wxDataObject::GetFormatName(cf)); | |
d59ceba5 VZ |
775 | } |
776 | ||
777 | pEnumFormatEtc->Release(); | |
778 | } | |
6c29712c | 779 | #endif // Debug |
d59ceba5 | 780 | |
6c29712c JS |
781 | STGMEDIUM medium; |
782 | // stop at the first valid format found on the clipboard | |
783 | for ( size_t n = 0; !result && (n < nFormats); n++ ) | |
d59ceba5 | 784 | { |
6c29712c JS |
785 | // convert to NativeFormat Id |
786 | cf = formats[n].GetFormatId(); | |
787 | ||
788 | // if the format is not available, try the next one | |
789 | // this test includes implicit / sythetic formats | |
790 | if ( !::IsClipboardFormatAvailable(cf) ) | |
791 | continue; | |
792 | ||
793 | formatEtc.cfFormat = cf; | |
d59ceba5 VZ |
794 | formatEtc.ptd = NULL; |
795 | formatEtc.dwAspect = DVASPECT_CONTENT; | |
796 | formatEtc.lindex = -1; | |
d59ceba5 | 797 | |
6c29712c JS |
798 | // use the appropriate tymed |
799 | switch ( formatEtc.cfFormat ) | |
d59ceba5 | 800 | { |
6c29712c JS |
801 | case CF_BITMAP: |
802 | formatEtc.tymed = TYMED_GDI; | |
803 | break; | |
d59ceba5 | 804 | |
4676948b | 805 | #ifndef __WXWINCE__ |
6c29712c JS |
806 | case CF_METAFILEPICT: |
807 | formatEtc.tymed = TYMED_MFPICT; | |
808 | break; | |
265b0c07 | 809 | |
6c29712c JS |
810 | case CF_ENHMETAFILE: |
811 | formatEtc.tymed = TYMED_ENHMF; | |
812 | break; | |
4676948b | 813 | #endif |
265b0c07 | 814 | |
6c29712c JS |
815 | default: |
816 | formatEtc.tymed = TYMED_HGLOBAL; | |
817 | } | |
818 | ||
819 | // try to get data | |
820 | hr = pDataObject->GetData(&formatEtc, &medium); | |
821 | if ( FAILED(hr) ) | |
822 | { | |
823 | // try other tymed for GDI objects | |
824 | if ( formatEtc.cfFormat == CF_BITMAP ) | |
d59ceba5 | 825 | { |
6c29712c JS |
826 | formatEtc.tymed = TYMED_HGLOBAL; |
827 | hr = pDataObject->GetData(&formatEtc, &medium); | |
d59ceba5 | 828 | } |
6c29712c | 829 | } |
d59ceba5 | 830 | |
6c29712c JS |
831 | if ( SUCCEEDED(hr) ) |
832 | { | |
833 | // pass the data to the data object | |
834 | hr = data.GetInterface()->SetData(&formatEtc, &medium, TRUE); | |
835 | if ( FAILED(hr) ) | |
d59ceba5 | 836 | { |
6c29712c | 837 | wxLogDebug(wxT("Failed to set data in wxIDataObject")); |
d59ceba5 | 838 | |
6c29712c JS |
839 | // IDataObject only takes the ownership of data if it |
840 | // successfully got it - which is not the case here | |
841 | ReleaseStgMedium(&medium); | |
842 | } | |
843 | else | |
844 | { | |
845 | result = TRUE; | |
d59ceba5 | 846 | } |
d59ceba5 | 847 | } |
6c29712c | 848 | //else: unsupported tymed? |
d59ceba5 | 849 | } |
6c29712c JS |
850 | |
851 | if ( formats != &format ) | |
852 | { | |
853 | delete [] formats; | |
854 | } | |
855 | //else: we didn't allocate any memory | |
d59ceba5 VZ |
856 | |
857 | // clean up and return | |
858 | pDataObject->Release(); | |
859 | ||
860 | return result; | |
861 | #elif wxUSE_DATAOBJ | |
223d09f6 | 862 | wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") ); |
26f86486 | 863 | |
7fc0bd1c | 864 | wxDataFormat format = data.GetPreferredFormat(); |
26f86486 | 865 | switch ( format ) |
06e43511 JS |
866 | { |
867 | case wxDF_TEXT: | |
868 | case wxDF_OEMTEXT: | |
869 | { | |
1e8335b0 VZ |
870 | wxTextDataObject& textDataObject = (wxTextDataObject &)data; |
871 | char* s = (char*)wxGetClipboardData(format); | |
872 | if ( !s ) | |
06e43511 | 873 | return FALSE; |
1e8335b0 | 874 | |
f07dc2e2 | 875 | textDataObject.SetText(wxString::FromAscii(s)); |
1e8335b0 VZ |
876 | delete [] s; |
877 | ||
878 | return TRUE; | |
06e43511 | 879 | } |
26f86486 | 880 | |
06e43511 JS |
881 | case wxDF_BITMAP: |
882 | case wxDF_DIB: | |
883 | { | |
1e8335b0 | 884 | wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data; |
7fc0bd1c | 885 | wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat()); |
1e8335b0 | 886 | if ( !bitmap ) |
06e43511 | 887 | return FALSE; |
1e8335b0 VZ |
888 | |
889 | bitmapDataObject.SetBitmap(*bitmap); | |
890 | delete bitmap; | |
891 | ||
892 | return TRUE; | |
06e43511 JS |
893 | } |
894 | #if wxUSE_METAFILE | |
895 | case wxDF_METAFILE: | |
896 | { | |
1e8335b0 | 897 | wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data; |
26f86486 | 898 | wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE); |
1e8335b0 | 899 | if ( !metaFile ) |
06e43511 | 900 | return FALSE; |
3f480da3 | 901 | |
1e8335b0 VZ |
902 | metaFileDataObject.SetMetafile(*metaFile); |
903 | delete metaFile; | |
26f86486 | 904 | |
1e8335b0 VZ |
905 | return TRUE; |
906 | } | |
907 | #endif // wxUSE_METAFILE | |
06e43511 | 908 | } |
3897b707 | 909 | return FALSE; |
d59ceba5 | 910 | #else // !wxUSE_DATAOBJ |
1e8335b0 | 911 | wxFAIL_MSG( wxT("no clipboard implementation") ); |
06e43511 | 912 | return FALSE; |
3897b707 | 913 | #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ |
2bda0e17 KB |
914 | } |
915 | ||
47d67540 | 916 | #endif // wxUSE_CLIPBOARD |
4ce81a75 | 917 |