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"
24 #define wxUSE_DATAOBJ 1
30 bool clipboard_opened
= false ;
32 bool wxOpenClipboard()
34 clipboard_opened
= true ;
38 bool wxCloseClipboard()
40 clipboard_opened
= false ;
44 bool wxIsClipboardOpened()
46 return clipboard_opened
;
49 bool wxEmptyClipboard()
54 err
= ClearCurrentScrap( );
61 wxLogSysError(_("Failed to empty the clipboard."));
71 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
77 err
= GetCurrentScrap( &scrapRef
);
78 if ( err
!= noTypeErr
&& err
!= memFullErr
)
80 ScrapFlavorFlags flavorFlags
;
83 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
85 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
95 if ( GetScrap( NULL
, dataFormat
.GetFormatId() , &offset
) > 0 )
103 bool wxSetClipboardData(wxDataFormat dataFormat
,const void *data
,int width
, int height
)
108 OSStatus err
= noErr
;
111 switch (dataFormat
.GetType())
116 wxBitmap *bitmap = (wxBitmap *)data;
118 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
119 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
120 HBITMAP old = (HBITMAP)
121 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
122 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
124 bitmap->GetHeight());
127 SelectObject(hdcSrc, old);
133 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
134 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
135 hdcSrc, 0, 0, SRCCOPY);
137 // Select new bitmap out of memory DC
138 SelectObject(hdcMem, old1);
141 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
144 SelectObject(hdcSrc, old);
154 #if wxUSE_IMAGE_LOADING_IN_MSW
155 wxBitmap *bitmap = (wxBitmap *)data;
156 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
157 // NULL palette means to use the system one
158 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
159 handle = SetClipboardData(CF_DIB, hDIB);
160 #endif // wxUSE_IMAGE_LOADING_IN_MSW
168 wxMetafile
*wxMF
= (wxMetafile
*)data
;
169 PicHandle pict
= wxMF
->GetHMETAFILE() ;
170 HLock( (Handle
) pict
) ;
172 err
= PutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
175 err
= GetCurrentScrap (&scrap
);
178 err
= PutScrapFlavor (scrap
, 'PICT', 0, GetHandleSize((Handle
) pict
), *pict
);
181 HUnlock( (Handle
) pict
) ;
191 wxLogError(_("Unsupported clipboard format."));
196 dataFormat
= wxDF_TEXT
;
202 if ( wxApp::s_macDefaultEncodingIsPC
)
204 mac
= wxMacMakeMacStringFromPC((char *)data
) ;
211 err
= PutScrap( mac
.Length() , 'TEXT' , mac
.c_str() ) ;
214 err
= GetCurrentScrap (&scrap
);
217 err
= PutScrapFlavor (scrap
, 'TEXT', 0, mac
.Length(), mac
.c_str());
226 wxLogSysError(_("Failed to set clipboard data."));
234 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
236 return wxDataFormat();
239 int wxRegisterClipboardFormat(wxChar
*formatName
)
244 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, wxChar
*formatName
, int maxCount
)
249 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
256 * Generalized clipboard implementation by Matthew Flatt
259 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxClipboardBase
)
261 wxClipboard::wxClipboard()
263 m_clearOnExit
= FALSE
;
266 wxClipboard::~wxClipboard()
274 void wxClipboard::Clear()
278 bool wxClipboard::Flush()
283 bool wxClipboard::Open()
285 return wxOpenClipboard();
288 bool wxClipboard::IsOpened() const
290 return wxIsClipboardOpened();
293 static int FormatStringToID(char *str
)
295 if (!strcmp(str
, "TEXT"))
298 return wxRegisterClipboardFormat(str
);
301 bool wxClipboard::SetData( wxDataObject
*data
)
303 (void)wxEmptyClipboard();
306 return AddData(data
);
311 bool wxClipboard::AddData( wxDataObject
*data
)
313 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
316 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
318 wxDataFormat format
= data
->GetPreferredFormat();
320 switch ( format
.GetType() )
325 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
326 wxString
str(textDataObject
->GetText());
327 return wxSetClipboardData(format
, str
.c_str());
333 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
334 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
335 return wxSetClipboardData(format
, &bitmap
);
338 #if 0 // wxUSE_METAFILE
341 wxMetafileDataObject
* metaFileDataObject
=
342 (wxMetafileDataObject
*) data
;
343 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
344 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
345 metaFileDataObject
->GetWidth(),
346 metaFileDataObject
->GetHeight());
348 #endif // wxUSE_METAFILE
351 // return wxSetClipboardData(data);
354 #else // !wxUSE_DATAOBJ
359 void wxClipboard::Close()
364 bool wxClipboard::IsSupported( const wxDataFormat
&format
)
366 return wxIsClipboardFormatAvailable(format
);
369 bool wxClipboard::GetData( wxDataObject
& data
)
372 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
374 wxDataFormat format
= data
.GetPreferredFormat();
380 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
381 char* s
= (char*)wxGetClipboardData(format
);
385 textDataObject
.SetText(s
);
394 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
395 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(format
);
399 bitmapDataObject
.SetBitmap(*bitmap
);
404 #if 0 // wxUSE_METAFILE
407 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
408 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
412 metaFileDataObject
.SetMetafile(*metaFile
);
417 #endif // wxUSE_METAFILE
419 #else // !wxUSE_DATAOBJ
420 wxFAIL_MSG( wxT("no clipboard implementation") );
425 void wxClipboard::SetClipboardClient(wxClipboardClient *client, long time)
430 clipOwner->BeingReplaced();
437 if (wxOpenClipboard()) {
438 char **formats, *data;
443 formats = clipOwner->formats.ListToArray(FALSE);
444 for (i = clipOwner->formats.Number(); i--; ) {
445 ftype = FormatStringToID(formats[i]);
446 data = clipOwner->GetData(formats[i], &size);
447 if (!wxSetClipboardData(ftype, (wxObject *)data, size, 1)) {
448 got_selection = FALSE;
454 got_selection = wxCloseClipboard();
456 got_selection = FALSE;
458 got_selection = FALSE; // Assume another process takes over
460 if (!got_selection) {
461 clipOwner->BeingReplaced();
466 wxClipboardClient *wxClipboard::GetClipboardClient()
471 void wxClipboard::SetClipboardString(char *str, long time)
476 clipOwner->BeingReplaced();
484 if (wxOpenClipboard()) {
485 if (!wxSetClipboardData(wxDF_TEXT, (wxObject *)str))
486 got_selection = FALSE;
488 got_selection = wxCloseClipboard();
490 got_selection = FALSE;
492 got_selection = FALSE; // Assume another process takes over
494 if (!got_selection) {
499 char *wxClipboard::GetClipboardString(long time)
504 str = GetClipboardData("TEXT", &length, time);
514 char *wxClipboard::GetClipboardData(char *format, long *length, long time)
517 if (clipOwner->formats.Member(format))
518 return clipOwner->GetData(format, length);
521 } else if (cbString) {
522 if (!strcmp(format, "TEXT"))
523 return copystring(cbString);
527 if (wxOpenClipboard()) {
528 receivedString = (char *)wxGetClipboardData(FormatStringToID(format),
532 receivedString = NULL;
534 return receivedString;