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