int pygame_scrap_put (char *type, int srclen, char *src) { UINT format; int nulledlen = srclen + 1; HANDLE hMem; if (!pygame_scrap_initialized ()) { PyErr_SetString (PyExc_SDLError, "scrap system not initialized."); return 0; } format = _convert_internal_type (type); if (format == -1) format = _convert_format (type); if (!OpenClipboard (SDL_Window)) return 0; /* Could not open the clipboard. */ if (format == CF_DIB || format == CF_DIBV5) nulledlen -= sizeof (BITMAPFILEHEADER); /* We won't copy the header */ hMem = GlobalAlloc ((GMEM_MOVEABLE | GMEM_DDESHARE), nulledlen); if (hMem) { char *dst = GlobalLock (hMem); memset (dst, 0, nulledlen); if (format == CF_DIB || format == CF_DIBV5) memcpy (dst, src + sizeof (BITMAPFILEHEADER), nulledlen - 1); else memcpy (dst, src, srclen); GlobalUnlock (hMem); EmptyClipboard (); SetClipboardData (format, hMem); if (format == _format_MIME_PLAIN) { /* Setting SCRAP_TEXT, also set CF_TEXT. */ SetClipboardData (CF_TEXT, hMem); } } else { /* Could not access the clipboard, raise an error. */ CloseClipboard (); return 0; } CloseClipboard (); return 1; }
int scrap_put_win (char *type, char *data, unsigned int size) { UINT format; int nulledlen = size + 1; HANDLE hMem; format = _convert_internal_type (type); if (format == (UINT)-1) format = _convert_format (type); if (!OpenClipboard (_sdlwindow)) { SDL_SetError ("could not access clipboard"); return -1; /* Could not open the clipboard. */ } if (format == CF_DIB || format == CF_DIBV5) nulledlen -= sizeof (BITMAPFILEHEADER); /* We won't copy the header */ hMem = GlobalAlloc ((GMEM_MOVEABLE | GMEM_DDESHARE), nulledlen); if (hMem) { char *dst = GlobalLock (hMem); memset (dst, 0, nulledlen); if (format == CF_DIB || format == CF_DIBV5) memcpy (dst, data + sizeof (BITMAPFILEHEADER), nulledlen - 1); else memcpy (dst, data, size); GlobalUnlock (hMem); EmptyClipboard (); SetClipboardData (format, hMem); if (format == _format_MIME_PLAIN) { /* Setting SCRAP_TEXT, also set CF_TEXT. */ SetClipboardData (CF_TEXT, hMem); } } else { /* Could not access the clipboard, raise an error. */ SDL_SetError ("could not acquire the memory pointer"); CloseClipboard (); return -1; } CloseClipboard (); return 1; }
int scrap_get_win (char *type, char **data, unsigned int *size) { UINT format = _convert_format (type); if (!OpenClipboard (_sdlwindow)) { SDL_SetError ("could not access clipboard"); return -1; } if (!IsClipboardFormatAvailable (format)) { /* The format was not found - was it a mapped type? */ format = _convert_internal_type (type); if (format == (UINT)-1) { CloseClipboard (); SDL_SetError ("no matching format on clipboard found"); return -1; } } if (IsClipboardFormatAvailable (format)) { HANDLE hMem; char *src = NULL; int retval = 0; hMem = GetClipboardData (format); if (hMem) { *size = 0; /* CF_BITMAP is not a global, so do not lock it. */ if (format != CF_BITMAP) { src = GlobalLock (hMem); if (!src) { CloseClipboard (); SDL_SetError ("could not acquire the memory pointer"); return -1; } *size = GlobalSize (hMem); } if (format == CF_DIB || format == CF_DIBV5) { /* size will be increased accordingly in * _create_dib_buffer. */ *data = _create_dib_buffer (src, size); retval = 1; } else if (*size != 0) { *data = malloc (*size); if (*data) { memset (*data, 0, *size); memcpy (*data, src, *size); retval = 1; } else { SDL_SetError ("could not allocate memory"); retval = -1; } } GlobalUnlock (hMem); CloseClipboard (); return retval; } } CloseClipboard (); return 0; }
char* pygame_scrap_get (char *type, unsigned long *count) { UINT format = _convert_format (type); char *retval = NULL; if (!pygame_scrap_initialized ()) { PyErr_SetString (PyExc_SDLError, "scrap system not initialized."); return NULL; } if (!pygame_scrap_lost ()) return Bytes_AsString (PyDict_GetItemString (_clipdata, type)); if (!OpenClipboard (SDL_Window)) return NULL; if (!IsClipboardFormatAvailable (format)) { /* The format was not found - was it a mapped type? */ format = _convert_internal_type (type); if (format == -1) { CloseClipboard (); return NULL; } } if (IsClipboardFormatAvailable (format)) { HANDLE hMem; char *src; src = NULL; hMem = GetClipboardData (format); if (hMem) { *count = 0; /* CF_BITMAP is not a global, so do not lock it. */ if (format != CF_BITMAP) { src = GlobalLock (hMem); if (!src) { CloseClipboard (); return NULL; } *count = GlobalSize (hMem); } if (format == CF_DIB || format == CF_DIBV5) { /* Count will be increased accordingly in * _create_dib_buffer. */ src = _create_dib_buffer (src, count); GlobalUnlock (hMem); CloseClipboard (); return src; } else if (*count != 0) { /* weird error, shouldn't get here. */ if(!src) { return NULL; } retval = malloc (*count); if (retval) { memset (retval, 0, *count); memcpy (retval, src, *count); } } GlobalUnlock (hMem); } } CloseClipboard (); return retval; }