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