[pywin32-checkins] pywin32/win32/src win32clipboardmodule.cpp, 1.23, 1.24
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Mark H. <mha...@us...> - 2008-11-26 01:12:06
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv13175/src Modified Files: win32clipboardmodule.cpp Log Message: Merge win32clipboard changes from py3k branch, with a change to SetClipboardText which should be more backwards compatible. Index: win32clipboardmodule.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/win32clipboardmodule.cpp,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** win32clipboardmodule.cpp 7 Jan 2008 03:10:53 -0000 1.23 --- win32clipboardmodule.cpp 26 Nov 2008 01:11:54 -0000 1.24 *************** *** 313,316 **** --- 313,317 ---- // @pyparm int|format|CF_TEXT|Specifies a clipboard format. For a description of // the standard clipboard formats, see Standard Clipboard Formats. + // In Unicode builds (ie, python 3k), the default is CF_UNICODETEXT. #ifdef UNICODE int format = CF_UNICODETEXT; *************** *** 480,484 **** return NULL; if (!PyWinObject_AsHANDLE(obhglobal, &hglobal)) ! return NULL; size_t size = GlobalSize(hglobal); if (!size) --- 481,485 ---- return NULL; if (!PyWinObject_AsHANDLE(obhglobal, &hglobal)) ! return NULL; size_t size = GlobalSize(hglobal); if (!size) *************** *** 867,874 **** // @pyparm int|format||Specifies a clipboard format. For a description of ! // the standard clipboard formats, see Standard Clipboard Formats. ! // @pyparm int/buffer|hMem||Integer handle to the data in the specified ! // format, or a buffer object (eg, string). // This parameter can be 0, indicating that the window provides data in // the specified clipboard format (renders the format) upon request. If a --- 868,875 ---- // @pyparm int|format||Specifies a clipboard format. For a description of ! // the standard clipboard formats, see Standard Clipboard Formats. // @pyparm int/buffer|hMem||Integer handle to the data in the specified ! // format, or string, unicode, or any object that supports the buffer interface. ! // A global memory object is allocated, and the object's buffer is copied to the new memory. // This parameter can be 0, indicating that the window provides data in // the specified clipboard format (renders the format) upon request. If a *************** *** 889,908 **** if (!PyWinObject_AsHANDLE(obhandle , &handle)){ PyErr_Clear(); ! // @pyparmalt1 int|format||Specifies a clipboard format. For a description of ! // the standard clipboard formats, see Standard Clipboard Formats. ! // @pyparmalt1 object|ob||An object that has a read-buffer interface. ! // A global memory object is allocated, and the objects buffer is copied ! // to the new memory. const void * buf = NULL; Py_ssize_t bufSize = 0; ! ! if (PyObject_AsReadBuffer(obhandle,&buf,&bufSize)==-1) ! RETURN_TYPE_ERR("The object must support the buffer interfaces"); ! // size doesnt include nulls! ! if (PyString_Check(obhandle)) ! bufSize += 1; ! else if (PyUnicode_Check(obhandle)) ! bufSize += sizeof(wchar_t); ! // else assume buffer needs no terminator... handle = GlobalAlloc(GHND, bufSize); if (handle == NULL) { --- 890,907 ---- if (!PyWinObject_AsHANDLE(obhandle , &handle)){ PyErr_Clear(); ! const void * buf = NULL; Py_ssize_t bufSize = 0; ! // In py3k, unicode no longer supports buffer interface ! if (PyUnicode_Check(obhandle)){ ! bufSize = PyUnicode_GET_DATA_SIZE(obhandle) + sizeof(Py_UNICODE); ! buf=(void *)PyUnicode_AS_UNICODE(obhandle); ! } else { ! if (PyObject_AsReadBuffer(obhandle,&buf,&bufSize)==-1) ! return NULL; ! if (PyString_Check(obhandle)) ! bufSize++; // size doesnt include nulls! ! // else assume buffer needs no terminator... ! } handle = GlobalAlloc(GHND, bufSize); if (handle == NULL) { *************** *** 919,922 **** --- 918,922 ---- if (!data) + // XXX - should we GlobalFree the mem? return ReturnAPIError("SetClipboardData"); return PyWinLong_FromHANDLE(data); *************** *** 945,976 **** // @pymethod int|win32clipboard|SetClipboardText|Convienience function to // call SetClipboardData with text. ! static PyObject * py_set_clipboard_text(PyObject* self, PyObject* args) { - #ifdef UNICODE - int format = CF_UNICODETEXT; - #else int format = CF_TEXT; - #endif - TCHAR *text; PyObject *obtext, *ret=NULL; ! DWORD size; ! if (!PyArg_ParseTuple(args, "O:SetClipboardText", ! &obtext)) // @pyparm str/unicode|text||The text to place on the clipboard. return NULL; ! if (!PyWinObject_AsTCHAR(obtext, &text, FALSE, &size)) ! return NULL; HGLOBAL hMem; ! LPTSTR pszDst=NULL; ! hMem = GlobalAlloc(GHND, (size+1) * sizeof(TCHAR)); if (hMem == NULL) PyWin_SetAPIError("GlobalAlloc"); else{ ! pszDst = (TCHAR *)GlobalLock(hMem); ! _tcscpy(pszDst, text); ! pszDst[size] = 0; GlobalUnlock(hMem); HANDLE data; --- 945,992 ---- // @pymethod int|win32clipboard|SetClipboardText|Convienience function to // call SetClipboardData with text. ! // @comm You may pass a Unicode or string/bytes object to this function, ! // but depending on the value of the 'format' param, it may be converted ! // to the appropriate type for that param. ! // @comm Many applications will want to call this function twice, with the ! // same string specified but CF_UNICODETEXT specified the second. static PyObject * py_set_clipboard_text(PyObject* self, PyObject* args) { int format = CF_TEXT; PyObject *obtext, *ret=NULL; ! if (!PyArg_ParseTuple(args, "O|i:SetClipboardText", ! &obtext, // @pyparm str/unicode|text||The text to place on the clipboard. ! &format)) // @pyparm int|format|CF_TEXT|The clipboard format to use - must be CF_TEXT or CF_UNICODETEXT return NULL; ! const void *src = 0; ! DWORD cb = 0; // number of bytes *excluding* NULL ! size_t size_null = 0; ! if (format == CF_TEXT) { ! if (!PyWinObject_AsString(obtext, (char **)&src, FALSE, &cb)) ! return NULL; ! size_null = sizeof(char); ! } else if (format == CF_UNICODETEXT) { ! DWORD cchars; ! if (!PyWinObject_AsWCHAR(obtext, (WCHAR **)&src, FALSE, &cchars)) ! return NULL; ! cb = cchars * sizeof(WCHAR); ! size_null = sizeof(WCHAR); ! } else { ! return PyErr_Format(PyExc_ValueError, "Format arg must be one of CF_TEXT (%d) or CF_UNICODETEXT (%d) - got %d", ! CF_TEXT, CF_UNICODETEXT, format); ! } ! HGLOBAL hMem; ! BYTE *dest=NULL; ! hMem = GlobalAlloc(GHND, cb + size_null); if (hMem == NULL) PyWin_SetAPIError("GlobalAlloc"); else{ ! dest = (BYTE *)GlobalLock(hMem); ! memcpy(dest, src, cb); ! // whack the terminator on. ! memset(dest+cb, 0, size_null); GlobalUnlock(hMem); HANDLE data; *************** *** 983,987 **** ret = PyWinLong_FromHANDLE(data); } ! PyWinObject_FreeTCHAR(text); return ret; // @pyseeapi SetClipboardData --- 999,1006 ---- ret = PyWinLong_FromHANDLE(data); } ! if (format == CF_TEXT) ! PyWinObject_FreeString((char *)src); ! else ! PyWinObject_FreeWCHAR((WCHAR *)src); return ret; // @pyseeapi SetClipboardData *************** *** 1132,1153 **** ! static int AddConstant(PyObject *dict, char *key, long value) ! { ! PyObject *okey = PyString_FromString(key); ! PyObject *oval = PyInt_FromLong(value); ! if (!okey || !oval) { ! Py_XDECREF(okey); ! Py_XDECREF(oval); ! return 1; ! } ! int rc = PyDict_SetItem(dict,okey, oval); ! Py_XDECREF(okey); ! Py_XDECREF(oval); ! return rc; ! } ! ! #define ADD_CONSTANT(tok) if (rc=AddConstant(dict,#tok, tok)) return rc ! static int AddConstants(PyObject *dict) { int rc; --- 1151,1157 ---- ! #define ADD_CONSTANT(tok) if (rc=PyModule_AddIntConstant(module, #tok, tok)) return rc ! static int AddConstants(PyObject *module) { int rc; *************** *** 1189,1193 **** if (!dict) return; /* Another serious error!*/ PyWinGlobals_Ensure(); ! AddConstants(dict); Py_INCREF(PyWinExc_ApiError); PyDict_SetItemString(dict, "error", PyWinExc_ApiError); --- 1193,1197 ---- if (!dict) return; /* Another serious error!*/ PyWinGlobals_Ensure(); ! AddConstants(module); Py_INCREF(PyWinExc_ApiError); PyDict_SetItemString(dict, "error", PyWinExc_ApiError); |