|
| 1 | +''' |
| 2 | +Clipboard windows: an implementation of the Clipboard using ctypes. |
| 3 | +''' |
| 4 | + |
| 5 | +__all__ = ('ClipboardWindows', ) |
| 6 | + |
| 7 | +from kivy.utils import platform |
| 8 | +from kivy.core.clipboard import ClipboardBase |
| 9 | + |
| 10 | +if platform != 'win': |
| 11 | + raise SystemError('unsupported platform for Windows clipboard') |
| 12 | + |
| 13 | + |
| 14 | +class ClipboardWindows(ClipboardBase): |
| 15 | + |
| 16 | + def get(self, mimetype='text/plain'): |
| 17 | + ctypes.windll.user32.OpenClipboard(0) |
| 18 | + # 1 is CF_TEXT |
| 19 | + pcontents = ctypes.windll.user32.GetClipboardData(1) |
| 20 | + data = ctypes.c_char_p(pcontents).value |
| 21 | + #ctypes.windll.kernel32.GlobalUnlock(pcontents) |
| 22 | + ctypes.windll.user32.CloseClipboard() |
| 23 | + return data |
| 24 | + |
| 25 | + def put(self, data, mimetype='text/plain'): |
| 26 | + GMEM_DDESHARE = 0x2000 |
| 27 | + ctypes.windll.user32.OpenClipboard(0) |
| 28 | + ctypes.windll.user32.EmptyClipboard() |
| 29 | + try: |
| 30 | + # works on Python 2 (bytes() only takes one argument) |
| 31 | + hCd = ctypes.windll.kernel32.GlobalAlloc( |
| 32 | + GMEM_DDESHARE, len(bytes(text)) + 1) |
| 33 | + except TypeError: |
| 34 | + # works on Python 3 (bytes() requires an encoding) |
| 35 | + hCd = ctypes.windll.kernel32.GlobalAlloc( |
| 36 | + GMEM_DDESHARE, len(bytes(text, 'ascii')) + 1) |
| 37 | + pchData = ctypes.windll.kernel32.GlobalLock(hCd) |
| 38 | + try: |
| 39 | + # works on Python 2 (bytes() only takes one argument) |
| 40 | + ctypes.cdll.msvcrt.strcpy( |
| 41 | + ctypes.c_char_p(pchData), bytes(text)) |
| 42 | + except TypeError: |
| 43 | + # works on Python 3 (bytes() requires an encoding) |
| 44 | + ctypes.cdll.msvcrt.strcpy( |
| 45 | + ctypes.c_char_p(pchData), bytes(text, 'ascii')) |
| 46 | + ctypes.windll.kernel32.GlobalUnlock(hCd) |
| 47 | + ctypes.windll.user32.SetClipboardData(1, hCd) |
| 48 | + ctypes.windll.user32.CloseClipboard() |
| 49 | + |
| 50 | + def get_types(self): |
| 51 | + return list('text/plain',) |
| 52 | + |
0 commit comments