]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "clipbrd.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/object.h"
42 #include "wx/bitmap.h"
48 #include "wx/metafile.h"
52 #include "wx/clipbrd.h"
57 #include "wx/msw/private.h"
58 #include "wx/msw/dib.h"
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"
65 static bool wxSetClipboardData(wxDataObject
*data
);
69 #define memcpy hmemcpy
72 // ===========================================================================
74 // ===========================================================================
76 // ---------------------------------------------------------------------------
77 // old-style clipboard functions using Windows API
78 // ---------------------------------------------------------------------------
80 static bool gs_wxClipboardIsOpen
= FALSE
;
82 bool wxOpenClipboard()
84 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
86 wxWindow
*win
= wxTheApp
->GetTopWindow();
89 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
91 if ( !gs_wxClipboardIsOpen
)
92 wxLogSysError(_("Failed to open the clipboard."));
94 return gs_wxClipboardIsOpen
;
98 wxLogDebug(wxT("Can not open clipboard without a main window."));
104 bool wxCloseClipboard()
106 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
108 gs_wxClipboardIsOpen
= FALSE
;
110 if ( ::CloseClipboard() == 0 )
112 wxLogSysError(_("Failed to close the clipboard."));
120 bool wxEmptyClipboard()
122 if ( ::EmptyClipboard() == 0 )
124 wxLogSysError(_("Failed to empty the clipboard."));
132 bool wxIsClipboardOpened()
134 return gs_wxClipboardIsOpen
;
137 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
139 return ::IsClipboardFormatAvailable(dataFormat
) != 0;
142 #if wxUSE_DRAG_AND_DROP
143 static bool wxSetClipboardData(wxDataObject
*data
)
145 size_t size
= data
->GetDataSize();
146 HANDLE hGlobal
= ::GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, size
);
149 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
150 "transfer."), size
/ 1024);
155 LPVOID lpGlobalMemory
= ::GlobalLock(hGlobal
);
157 data
->GetDataHere(lpGlobalMemory
);
159 GlobalUnlock(hGlobal
);
161 wxDataFormat format
= data
->GetPreferredFormat();
162 if ( !::SetClipboardData(format
, hGlobal
) )
164 wxLogSysError(_("Failed to set clipboard data in format %s"),
165 wxDataObject::GetFormatName(format
));
172 #endif // wxUSE_DRAG_AND_DROP
174 bool wxSetClipboardData(wxDataFormat dataFormat
,
176 int width
, int height
)
178 HANDLE handle
= 0; // return value of SetClipboardData
184 wxBitmap
*bitmap
= (wxBitmap
*)data
;
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
,
192 bitmap
->GetHeight());
195 SelectObject(hdcSrc
, old
);
201 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
202 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
203 hdcSrc
, 0, 0, SRCCOPY
);
205 // Select new bitmap out of memory DC
206 SelectObject(hdcMem
, old1
);
209 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
212 SelectObject(hdcSrc
, old
);
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
= wxBitmapToDIB(hBitmap
, (HPALETTE
)NULL
);
225 handle
= SetClipboardData(CF_DIB
, hDIB
);
233 wxMetafile
*wxMF
= (wxMetafile
*)data
;
234 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
235 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
237 mf
->mm
= wxMF
->GetWindowsMappingMode();
240 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
242 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
244 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
254 wxLogError(_("Unsupported clipboard format."));
259 dataFormat
= wxDF_TEXT
;
264 char *s
= (char *)data
;
266 width
= strlen(s
) + 1;
268 DWORD l
= (width
* height
);
269 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
272 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
274 memcpy(lpGlobalMemory
, s
, l
);
276 GlobalUnlock(hGlobalMemory
);
279 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
286 wxLogSysError(_("Failed to set clipboard data."));
294 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
298 switch ( dataFormat
)
303 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
307 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
308 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
310 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
311 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
313 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
317 SelectObject(hdcSrc
, old
);
323 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
324 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
325 hdcSrc
, 0, 0, SRCCOPY
);
327 // Select new bitmap out of memory DC
328 SelectObject(hdcMem
, old1
);
331 SelectObject(hdcSrc
, old
);
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
);
353 wxLogError(_("Unsupported clipboard format."));
358 dataFormat
= wxDF_TEXT
;
363 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
367 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
371 char *s
= new char[hsize
];
375 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
377 memcpy(s
, lpGlobalMemory
, hsize
);
379 ::GlobalUnlock(hGlobalMemory
);
387 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
388 if ( !hGlobalMemory
)
391 DWORD size
= ::GlobalSize(hGlobalMemory
);
395 void *buf
= malloc(size
);
399 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
401 memcpy(buf
, lpGlobalMemory
, size
);
403 ::GlobalUnlock(hGlobalMemory
);
412 wxLogSysError(_("Failed to retrieve data from the clipboard."));
418 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
420 return ::EnumClipboardFormats(dataFormat
);
423 int wxRegisterClipboardFormat(wxChar
*formatName
)
425 return ::RegisterClipboardFormat(formatName
);
428 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
432 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
435 // ---------------------------------------------------------------------------
437 // ---------------------------------------------------------------------------
439 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
441 wxClipboard
* wxTheClipboard
= (wxClipboard
*)NULL
;
443 wxClipboard::wxClipboard()
447 wxClipboard::~wxClipboard()
452 void wxClipboard::Clear()
456 bool wxClipboard::Open()
458 return wxOpenClipboard();
461 bool wxClipboard::SetData( wxDataObject
*data
)
463 (void)wxEmptyClipboard();
466 return AddData(data
);
471 bool wxClipboard::AddData( wxDataObject
*data
)
473 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
475 #if wxUSE_DRAG_AND_DROP
476 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
478 wxDataFormat format
= data
->GetFormat();
485 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
486 wxString
str(textDataObject
->GetText());
487 return wxSetClipboardData(format
, str
.c_str());
493 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
494 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
495 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
501 wxMetafileDataObject
* metaFileDataObject
=
502 (wxMetafileDataObject
*) data
;
503 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
504 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
505 metaFileDataObject
->GetWidth(),
506 metaFileDataObject
->GetHeight());
508 #endif // wxUSE_METAFILE
511 return wxSetClipboardData(data
);
513 #else // !wxUSE_DRAG_AND_DROP
515 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
518 void wxClipboard::Close()
523 bool wxClipboard::IsSupported( wxDataFormat format
)
525 return wxIsClipboardFormatAvailable(format
);
528 bool wxClipboard::GetData( wxDataObject
*data
)
530 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
532 #if wxUSE_DRAG_AND_DROP
533 wxDataFormat format
= data
->GetFormat();
539 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
540 char* s
= (char*) wxGetClipboardData(format
);
543 textDataObject
->SetText(s
);
554 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*)data
;
555 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
558 bitmapDataObject
->SetBitmap(* bitmap
);
568 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*)data
;
569 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
572 metaFileDataObject
->SetMetafile(*metaFile
);
583 void *buf
= wxGetClipboardData(format
, &len
);
586 // FIXME this is for testing only!!
587 ((wxPrivateDataObject
*)data
)->SetData(buf
, len
);
601 //-----------------------------------------------------------------------------
603 //-----------------------------------------------------------------------------
605 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
607 bool wxClipboardModule::OnInit()
609 wxTheClipboard
= new wxClipboard();
614 void wxClipboardModule::OnExit()
616 if (wxTheClipboard
) delete wxTheClipboard
;
617 wxTheClipboard
= (wxClipboard
*) NULL
;
621 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
622 #endif // wxUSE_CLIPBOARD