1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
14 #pragma implementation "clipbrd.h"
19 #include "wx/bitmap.h"
21 #include "wx/metafile.h"
22 #include "wx/clipbrd.h"
25 #define wxUSE_DATAOBJ 1
31 bool clipboard_opened
= false ;
33 bool wxOpenClipboard()
35 clipboard_opened
= true ;
39 bool wxCloseClipboard()
41 clipboard_opened
= false ;
45 bool wxIsClipboardOpened()
47 return clipboard_opened
;
50 bool wxEmptyClipboard()
55 err
= ClearCurrentScrap( );
62 wxLogSysError(_("Failed to empty the clipboard."));
72 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
78 err
= GetCurrentScrap( &scrapRef
);
79 if ( err
!= noTypeErr
&& err
!= memFullErr
)
81 ScrapFlavorFlags flavorFlags
;
84 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
86 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
96 if ( GetScrap( NULL
, dataFormat
.GetFormatId() , &offset
) > 0 )
104 bool wxSetClipboardData(wxDataFormat dataFormat
,const void *data
,int width
, int height
)
109 OSStatus err
= noErr
;
112 switch (dataFormat
.GetType())
117 wxBitmap *bitmap = (wxBitmap *)data;
119 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
120 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
121 HBITMAP old = (HBITMAP)
122 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
123 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
125 bitmap->GetHeight());
128 SelectObject(hdcSrc, old);
134 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
135 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
136 hdcSrc, 0, 0, SRCCOPY);
138 // Select new bitmap out of memory DC
139 SelectObject(hdcMem, old1);
142 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
145 SelectObject(hdcSrc, old);
155 #if wxUSE_IMAGE_LOADING_IN_MSW
156 wxBitmap *bitmap = (wxBitmap *)data;
157 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
158 // NULL palette means to use the system one
159 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
160 handle = SetClipboardData(CF_DIB, hDIB);
161 #endif // wxUSE_IMAGE_LOADING_IN_MSW
169 wxMetafile
*wxMF
= (wxMetafile
*)data
;
170 PicHandle pict
= wxMF
->GetHMETAFILE() ;
171 HLock( (Handle
) pict
) ;
173 err
= PutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
176 err
= GetCurrentScrap (&scrap
);
179 err
= PutScrapFlavor (scrap
, 'PICT', 0, GetHandleSize((Handle
) pict
), *pict
);
182 HUnlock( (Handle
) pict
) ;
192 wxLogError(_("Unsupported clipboard format."));
197 dataFormat
= wxDF_TEXT
;
203 if ( wxApp::s_macDefaultEncodingIsPC
)
205 mac
= wxMacMakeMacStringFromPC((char *)data
) ;
212 err
= PutScrap( mac
.Length() , 'TEXT' , mac
.c_str() ) ;
215 err
= GetCurrentScrap (&scrap
);
218 err
= PutScrapFlavor (scrap
, 'TEXT', 0, mac
.Length(), mac
.c_str());
227 wxLogSysError(_("Failed to set clipboard data."));
235 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
237 return wxDataFormat();
240 int wxRegisterClipboardFormat(wxChar
*formatName
)
245 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, wxChar
*formatName
, int maxCount
)
250 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
257 * Generalized clipboard implementation by Matthew Flatt
260 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxClipboardBase
)
262 wxClipboard::wxClipboard()
264 m_clearOnExit
= FALSE
;
267 wxClipboard::~wxClipboard()
275 void wxClipboard::Clear()
279 bool wxClipboard::Flush()
284 bool wxClipboard::Open()
286 return wxOpenClipboard();
289 bool wxClipboard::IsOpened() const
291 return wxIsClipboardOpened();
294 static int FormatStringToID(char *str
)
296 if (!strcmp(str
, "TEXT"))
299 return wxRegisterClipboardFormat(str
);
302 bool wxClipboard::SetData( wxDataObject
*data
)
304 (void)wxEmptyClipboard();
307 return AddData(data
);
312 bool wxClipboard::AddData( wxDataObject
*data
)
314 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
317 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
319 wxDataFormat format
= data
->GetPreferredFormat();
321 switch ( format
.GetType() )
326 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
327 wxString
str(textDataObject
->GetText());
328 return wxSetClipboardData(format
, str
.c_str());
334 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
335 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
336 return wxSetClipboardData(format
, &bitmap
);
339 #if 0 // wxUSE_METAFILE
342 wxMetafileDataObject
* metaFileDataObject
=
343 (wxMetafileDataObject
*) data
;
344 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
345 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
346 metaFileDataObject
->GetWidth(),
347 metaFileDataObject
->GetHeight());
349 #endif // wxUSE_METAFILE
352 // return wxSetClipboardData(data);
355 #else // !wxUSE_DATAOBJ
360 void wxClipboard::Close()
365 bool wxClipboard::IsSupported( const wxDataFormat
&format
)
367 return wxIsClipboardFormatAvailable(format
);
370 bool wxClipboard::GetData( wxDataObject
& data
)
373 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
375 wxDataFormat format
= data
.GetPreferredFormat();
381 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
382 char* s
= (char*)wxGetClipboardData(format
);
386 textDataObject
.SetText(s
);
395 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
396 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(format
);
400 bitmapDataObject
.SetBitmap(*bitmap
);
405 #if 0 // wxUSE_METAFILE
408 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
409 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
413 metaFileDataObject
.SetMetafile(*metaFile
);
418 #endif // wxUSE_METAFILE
420 #else // !wxUSE_DATAOBJ
421 wxFAIL_MSG( wxT("no clipboard implementation") );
426 void wxClipboard::SetClipboardClient(wxClipboardClient *client, long time)
431 clipOwner->BeingReplaced();
438 if (wxOpenClipboard()) {
439 char **formats, *data;
444 formats = clipOwner->formats.ListToArray(FALSE);
445 for (i = clipOwner->formats.Number(); i--; ) {
446 ftype = FormatStringToID(formats[i]);
447 data = clipOwner->GetData(formats[i], &size);
448 if (!wxSetClipboardData(ftype, (wxObject *)data, size, 1)) {
449 got_selection = FALSE;
455 got_selection = wxCloseClipboard();
457 got_selection = FALSE;
459 got_selection = FALSE; // Assume another process takes over
461 if (!got_selection) {
462 clipOwner->BeingReplaced();
467 wxClipboardClient *wxClipboard::GetClipboardClient()
472 void wxClipboard::SetClipboardString(char *str, long time)
477 clipOwner->BeingReplaced();
485 if (wxOpenClipboard()) {
486 if (!wxSetClipboardData(wxDF_TEXT, (wxObject *)str))
487 got_selection = FALSE;
489 got_selection = wxCloseClipboard();
491 got_selection = FALSE;
493 got_selection = FALSE; // Assume another process takes over
495 if (!got_selection) {
500 char *wxClipboard::GetClipboardString(long time)
505 str = GetClipboardData("TEXT", &length, time);
515 char *wxClipboard::GetClipboardData(char *format, long *length, long time)
518 if (clipOwner->formats.Member(format))
519 return clipOwner->GetData(format, length);
522 } else if (cbString) {
523 if (!strcmp(format, "TEXT"))
524 return copystring(cbString);
528 if (wxOpenClipboard()) {
529 receivedString = (char *)wxGetClipboardData(FormatStringToID(format),
533 receivedString = NULL;
535 return receivedString;