@@ -502,33 +502,45 @@ PyObject*
502502PyImaging_GrabClipboardWin32 (PyObject * self , PyObject * args )
503503{
504504 int clip ;
505- HANDLE handle ;
505+ HANDLE handle = NULL ;
506506 int size ;
507507 void * data ;
508508 PyObject * result ;
509+ UINT format ;
510+ UINT formats [] = { CF_DIB , CF_DIBV5 , CF_HDROP , RegisterClipboardFormatA ("PNG" ), 0 };
511+ LPCSTR format_names [] = { "DIB" , "DIB" , "file" , "png" , NULL };
509512
510- clip = OpenClipboard (NULL );
511- /* FIXME: check error status */
513+ if (!OpenClipboard (NULL )) {
514+ PyErr_SetString (PyExc_OSError , "failed to open clipboard" );
515+ return NULL ;
516+ }
517+
518+ // find best format as set by clipboard owner
519+ format = 0 ;
520+ while (!handle && (format = EnumClipboardFormats (format ))) {
521+ for (UINT i = 0 ; formats [i ] != 0 ; i ++ ) {
522+ if (format == formats [i ]) {
523+ handle = GetClipboardData (format );
524+ format = i ;
525+ break ;
526+ }
527+ }
528+ }
512529
513- handle = GetClipboardData (CF_DIB );
514530 if (!handle ) {
515- /* FIXME: add CF_HDROP support to allow cut-and-paste from
516- the explorer */
517531 CloseClipboard ();
518- Py_INCREF (Py_None );
519- return Py_None ;
532+ return Py_BuildValue ("zO" , NULL , Py_None );
520533 }
521534
522- size = GlobalSize (handle );
523535 data = GlobalLock (handle );
536+ size = GlobalSize (handle );
524537
525538 result = PyBytes_FromStringAndSize (data , size );
526539
527540 GlobalUnlock (handle );
528-
529541 CloseClipboard ();
530542
531- return result ;
543+ return Py_BuildValue ( "zN" , format_names [ format ], result ) ;
532544}
533545
534546/* -------------------------------------------------------------------- */
0 commit comments