]> git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
...fixing up a previous Unicode fix.
[wxWidgets.git] / src / msw / clipbrd.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "clipbrd.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/setup.h"
33 #endif
34
35 #if wxUSE_CLIPBOARD
36
37 #ifndef WX_PRECOMP
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"
44 #include "wx/intl.h"
45 #endif
46
47 #if wxUSE_METAFILE
48 #include "wx/metafile.h"
49 #endif
50
51 #include "wx/log.h"
52 #include "wx/clipbrd.h"
53
54 #include <string.h>
55 #include <windows.h>
56
57 #include "wx/msw/private.h"
58 #include "wx/msw/dib.h"
59
60 // wxDataObject is tied to OLE/drag and drop implementation,
61 // therefore so is wxClipboard :-(
62 #if wxUSE_DRAG_AND_DROP
63 #include "wx/dataobj.h"
64
65 static bool wxSetClipboardData(wxDataObject *data);
66 #endif
67
68 #ifdef __WIN16__
69 #define memcpy hmemcpy
70 #endif
71
72 // ===========================================================================
73 // implementation
74 // ===========================================================================
75
76 // ---------------------------------------------------------------------------
77 // old-style clipboard functions using Windows API
78 // ---------------------------------------------------------------------------
79
80 static bool gs_wxClipboardIsOpen = FALSE;
81
82 bool wxOpenClipboard()
83 {
84 wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, _T("clipboard already opened.") );
85
86 wxWindow *win = wxTheApp->GetTopWindow();
87 if ( win )
88 {
89 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
90
91 if ( !gs_wxClipboardIsOpen )
92 wxLogSysError(_("Failed to open the clipboard."));
93
94 return gs_wxClipboardIsOpen;
95 }
96 else
97 {
98 wxLogDebug(_T("Can not open clipboard without a main window."));
99
100 return FALSE;
101 }
102 }
103
104 bool wxCloseClipboard()
105 {
106 wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, _T("clipboard is not opened") );
107
108 gs_wxClipboardIsOpen = FALSE;
109
110 if ( ::CloseClipboard() == 0 )
111 {
112 wxLogSysError(_("Failed to close the clipboard."));
113
114 return FALSE;
115 }
116
117 return TRUE;
118 }
119
120 bool wxEmptyClipboard()
121 {
122 if ( ::EmptyClipboard() == 0 )
123 {
124 wxLogSysError(_("Failed to empty the clipboard."));
125
126 return FALSE;
127 }
128
129 return TRUE;
130 }
131
132 bool wxIsClipboardOpened()
133 {
134 return gs_wxClipboardIsOpen;
135 }
136
137 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
138 {
139 return ::IsClipboardFormatAvailable(dataFormat) != 0;
140 }
141
142 #if wxUSE_DRAG_AND_DROP
143 static bool wxSetClipboardData(wxDataObject *data)
144 {
145 size_t size = data->GetDataSize();
146 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
147 if ( !hGlobal )
148 {
149 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
150 "transfer."), size / 1024);
151
152 return FALSE;
153 }
154
155 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
156
157 data->GetDataHere(lpGlobalMemory);
158
159 GlobalUnlock(hGlobal);
160
161 wxDataFormat format = data->GetPreferredFormat();
162 if ( !::SetClipboardData(format, hGlobal) )
163 {
164 wxLogSysError(_("Failed to set clipboard data in format %s"),
165 wxDataObject::GetFormatName(format));
166
167 return FALSE;
168 }
169
170 return TRUE;
171 }
172 #endif // wxUSE_DRAG_AND_DROP
173
174 bool wxSetClipboardData(wxDataFormat dataFormat,
175 const void *data,
176 int width, int height)
177 {
178 HANDLE handle = 0; // return value of SetClipboardData
179
180 switch (dataFormat)
181 {
182 case wxDF_BITMAP:
183 {
184 wxBitmap *bitmap = (wxBitmap *)data;
185
186 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
187 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
188 HBITMAP old = (HBITMAP)
189 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
190 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
191 bitmap->GetWidth(),
192 bitmap->GetHeight());
193 if (!hBitmap)
194 {
195 SelectObject(hdcSrc, old);
196 DeleteDC(hdcMem);
197 DeleteDC(hdcSrc);
198 return FALSE;
199 }
200
201 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
202 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
203 hdcSrc, 0, 0, SRCCOPY);
204
205 // Select new bitmap out of memory DC
206 SelectObject(hdcMem, old1);
207
208 // Set the data
209 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
210
211 // Clean up
212 SelectObject(hdcSrc, old);
213 DeleteDC(hdcSrc);
214 DeleteDC(hdcMem);
215 break;
216 }
217
218 case wxDF_DIB:
219 {
220 #if wxUSE_IMAGE_LOADING_IN_MSW
221 wxBitmap *bitmap = (wxBitmap *)data;
222 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
223 // NULL palette means to use the system one
224 HANDLE hDIB = BitmapToDIB(hBitmap, (HPALETTE)NULL);
225 handle = SetClipboardData(CF_DIB, hDIB);
226 #endif
227 break;
228 }
229
230 #if wxUSE_METAFILE
231 case wxDF_METAFILE:
232 {
233 wxMetafile *wxMF = (wxMetafile *)data;
234 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
235 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
236
237 mf->mm = wxMF->GetWindowsMappingMode();
238 mf->xExt = width;
239 mf->yExt = height;
240 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
241 GlobalUnlock(data);
242 wxMF->SetHMETAFILE((WXHANDLE) NULL);
243
244 handle = SetClipboardData(CF_METAFILEPICT, data);
245 break;
246 }
247 #endif
248 case CF_SYLK:
249 case CF_DIF:
250 case CF_TIFF:
251 case CF_PALETTE:
252 default:
253 {
254 wxLogError(_("Unsupported clipboard format."));
255 return FALSE;
256 }
257
258 case wxDF_OEMTEXT:
259 dataFormat = wxDF_TEXT;
260 // fall through
261
262 case wxDF_TEXT:
263 {
264 char *s = (char *)data;
265
266 width = strlen(s) + 1;
267 height = 1;
268 DWORD l = (width * height);
269 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
270 if ( hGlobalMemory )
271 {
272 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
273
274 memcpy(lpGlobalMemory, s, l);
275
276 GlobalUnlock(hGlobalMemory);
277 }
278
279 handle = SetClipboardData(dataFormat, hGlobalMemory);
280 break;
281 }
282 }
283
284 if ( handle == 0 )
285 {
286 wxLogSysError(_("Failed to set clipboard data."));
287
288 return FALSE;
289 }
290
291 return TRUE;
292 }
293
294 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
295 {
296 void *retval = NULL;
297
298 switch ( dataFormat )
299 {
300 case wxDF_BITMAP:
301 {
302 BITMAP bm;
303 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
304 if (!hBitmap)
305 break;
306
307 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
308 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
309
310 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
311 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
312
313 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
314
315 if (!hNewBitmap)
316 {
317 SelectObject(hdcSrc, old);
318 DeleteDC(hdcMem);
319 DeleteDC(hdcSrc);
320 break;
321 }
322
323 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
324 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
325 hdcSrc, 0, 0, SRCCOPY);
326
327 // Select new bitmap out of memory DC
328 SelectObject(hdcMem, old1);
329
330 // Clean up
331 SelectObject(hdcSrc, old);
332 DeleteDC(hdcSrc);
333 DeleteDC(hdcMem);
334
335 // Create and return a new wxBitmap
336 wxBitmap *wxBM = new wxBitmap;
337 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
338 wxBM->SetWidth(bm.bmWidth);
339 wxBM->SetHeight(bm.bmHeight);
340 wxBM->SetDepth(bm.bmPlanes);
341 wxBM->SetOk(TRUE);
342 retval = wxBM;
343 break;
344 }
345
346 case wxDF_METAFILE:
347 case CF_SYLK:
348 case CF_DIF:
349 case CF_TIFF:
350 case CF_PALETTE:
351 case wxDF_DIB:
352 {
353 wxLogError(_("Unsupported clipboard format."));
354 return FALSE;
355 }
356
357 case wxDF_OEMTEXT:
358 dataFormat = wxDF_TEXT;
359 // fall through
360
361 case wxDF_TEXT:
362 {
363 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
364 if (!hGlobalMemory)
365 break;
366
367 DWORD hsize = ::GlobalSize(hGlobalMemory);
368 if (len)
369 *len = hsize;
370
371 char *s = new char[hsize];
372 if (!s)
373 break;
374
375 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
376
377 memcpy(s, lpGlobalMemory, hsize);
378
379 ::GlobalUnlock(hGlobalMemory);
380
381 retval = s;
382 break;
383 }
384
385 default:
386 {
387 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
388 if ( !hGlobalMemory )
389 break;
390
391 DWORD size = ::GlobalSize(hGlobalMemory);
392 if ( len )
393 *len = size;
394
395 void *buf = malloc(size);
396 if ( !buf )
397 break;
398
399 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
400
401 memcpy(buf, lpGlobalMemory, size);
402
403 ::GlobalUnlock(hGlobalMemory);
404
405 retval = buf;
406 break;
407 }
408 }
409
410 if ( !retval )
411 {
412 wxLogSysError(_("Failed to retrieve data from the clipboard."));
413 }
414
415 return retval;
416 }
417
418 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
419 {
420 return ::EnumClipboardFormats(dataFormat);
421 }
422
423 int wxRegisterClipboardFormat(wxChar *formatName)
424 {
425 return ::RegisterClipboardFormat(formatName);
426 }
427
428 bool wxGetClipboardFormatName(wxDataFormat dataFormat,
429 wxChar *formatName,
430 int maxCount)
431 {
432 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
433 }
434
435 // ---------------------------------------------------------------------------
436 // wxClipboard
437 // ---------------------------------------------------------------------------
438
439 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
440
441 wxClipboard* wxTheClipboard = (wxClipboard *)NULL;
442
443 wxClipboard::wxClipboard()
444 {
445 }
446
447 wxClipboard::~wxClipboard()
448 {
449 Clear();
450 }
451
452 void wxClipboard::Clear()
453 {
454 }
455
456 bool wxClipboard::Open()
457 {
458 return wxOpenClipboard();
459 }
460
461 bool wxClipboard::SetData( wxDataObject *data )
462 {
463 (void)wxEmptyClipboard();
464
465 if ( data )
466 return AddData(data);
467 else
468 return TRUE;
469 }
470
471 bool wxClipboard::AddData( wxDataObject *data )
472 {
473 wxCHECK_MSG( data, FALSE, _T("data is invalid") );
474
475 #if wxUSE_DRAG_AND_DROP
476 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, _T("clipboard not open") );
477
478 wxDataFormat format = data->GetFormat();
479
480 switch ( format )
481 {
482 case wxDF_TEXT:
483 case wxDF_OEMTEXT:
484 {
485 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
486 wxString str(textDataObject->GetText());
487 return wxSetClipboardData(format, str.c_str());
488 }
489
490 case wxDF_BITMAP:
491 case wxDF_DIB:
492 {
493 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
494 wxBitmap bitmap(bitmapDataObject->GetBitmap());
495 return wxSetClipboardData(data->GetFormat(), &bitmap);
496 }
497
498 #if wxUSE_METAFILE
499 case wxDF_METAFILE:
500 {
501 wxMetafileDataObject* metaFileDataObject =
502 (wxMetafileDataObject*) data;
503 wxMetafile metaFile = metaFileDataObject->GetMetafile();
504 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
505 metaFileDataObject->GetWidth(),
506 metaFileDataObject->GetHeight());
507 }
508 #endif // wxUSE_METAFILE
509
510 default:
511 return wxSetClipboardData(data);
512 }
513 #else // !wxUSE_DRAG_AND_DROP
514 return FALSE;
515 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
516 }
517
518 void wxClipboard::Close()
519 {
520 wxCloseClipboard();
521 }
522
523 bool wxClipboard::IsSupported( wxDataFormat format )
524 {
525 return wxIsClipboardFormatAvailable(format);
526 }
527
528 bool wxClipboard::GetData( wxDataObject *data )
529 {
530 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, _T("clipboard not open") );
531
532 #if wxUSE_DRAG_AND_DROP
533 wxDataFormat format = data->GetFormat();
534 switch ( format )
535 {
536 case wxDF_TEXT:
537 case wxDF_OEMTEXT:
538 {
539 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
540 char* s = (char*) wxGetClipboardData(format);
541 if ( s )
542 {
543 textDataObject->SetText(s);
544 delete[] s;
545 return TRUE;
546 }
547 else
548 return FALSE;
549 }
550
551 case wxDF_BITMAP:
552 case wxDF_DIB:
553 {
554 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject *)data;
555 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat());
556 if (bitmap)
557 {
558 bitmapDataObject->SetBitmap(* bitmap);
559 delete bitmap;
560 return TRUE;
561 }
562 else
563 return FALSE;
564 }
565 #if wxUSE_METAFILE
566 case wxDF_METAFILE:
567 {
568 wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject *)data;
569 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
570 if (metaFile)
571 {
572 metaFileDataObject->SetMetafile(*metaFile);
573 delete metaFile;
574 return TRUE;
575 }
576 else
577 return FALSE;
578 }
579 #endif
580 default:
581 {
582 long len;
583 void *buf = wxGetClipboardData(format, &len);
584 if ( buf )
585 {
586 // FIXME this is for testing only!!
587 ((wxPrivateDataObject *)data)->SetData(buf, len);
588 free(buf);
589
590 return TRUE;
591 }
592 }
593
594 return FALSE;
595 }
596 #else
597 return FALSE;
598 #endif
599 }
600
601 //-----------------------------------------------------------------------------
602 // wxClipboardModule
603 //-----------------------------------------------------------------------------
604
605 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
606
607 bool wxClipboardModule::OnInit()
608 {
609 wxTheClipboard = new wxClipboard();
610
611 return TRUE;
612 }
613
614 void wxClipboardModule::OnExit()
615 {
616 if (wxTheClipboard) delete wxTheClipboard;
617 wxTheClipboard = (wxClipboard*) NULL;
618 }
619
620 #else
621 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
622 #endif // wxUSE_CLIPBOARD
623