Skip to content

Commit 6056016

Browse files
committed
clipboard_winctype fixes for windows
1 parent faefe56 commit 6056016

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

kivy/core/clipboard/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
'''
23
Clipboard
34
=========
@@ -119,11 +120,6 @@ def _paste(self):
119120
if _platform == 'android':
120121
_clipboards.append(
121122
('android', 'clipboard_android', 'ClipboardAndroid'))
122-
_clipboards.append(
123-
('sdl2', 'clipboard_sdl2', 'ClipboardSDL2'))
124-
elif _platform == 'ios':
125-
_clipboards.append(
126-
('sdl2', 'clipboard_sdl2', 'ClipboardSDL2'))
127123
elif _platform in ('macosx', 'linux', 'win'):
128124
if _platform == 'macosx':
129125
_clipboards.append(
@@ -136,12 +132,14 @@ def _paste(self):
136132

137133
_clipboards.append(
138134
('pygame', 'clipboard_pygame', 'ClipboardPygame'))
139-
_clipboards.append(
140-
('sdl2', 'clipboard_sdl2', 'ClipboardSDL2'))
135+
136+
_clipboards.append(
137+
('sdl2', 'clipboard_sdl2', 'ClipboardSDL2'))
141138
_clipboards.append(
142139
('dummy', 'clipboard_dummy', 'ClipboardDummy'))
143140

144141
Clipboard = core_select_lib('clipboard', _clipboards, True)
145142

146143
del _clipboards
147144
del _platform
145+

kivy/core/clipboard/clipboard_winctypes.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,39 @@
1010
if platform != 'win':
1111
raise SystemError('unsupported platform for Windows clipboard')
1212

13+
import ctypes
1314
user32 = ctypes.windll.user32
1415
kernel32 = ctypes.windll.kernel32
1516
msvcrt = ctypes.cdll.msvcrt
1617
c_char_p = ctypes.c_char_p
18+
c_wchar_p = ctypes.c_wchar_p
1719

1820

1921
class ClipboardWindows(ClipboardBase):
2022

2123
def get(self, mimetype='text/plain'):
2224
user32.OpenClipboard(0)
2325
# 1 is CF_TEXT
24-
pcontents = user32.GetClipboardData(1)
25-
data = c_char_p(pcontents).value
26+
pcontents = user32.GetClipboardData(13)
27+
data = c_wchar_p(pcontents).value.encode('utf-16')
2628
#ctypes.windll.kernel32.GlobalUnlock(pcontents)
2729
user32.CloseClipboard()
2830
return data
2931

30-
def put(self, data, mimetype='text/plain'):
32+
def put(self, text, mimetype='text/plain'):
33+
print text, type(text)
3134
GMEM_DDESHARE = 0x2000
32-
user32.OpenClipboard(0)
35+
CF_UNICODETEXT = 13
36+
user32.OpenClipboard(None)
3337
user32.EmptyClipboard()
34-
try:
35-
# works on Python 2 (bytes() only takes one argument)
36-
hCd = kernel32.GlobalAlloc(
37-
GMEM_DDESHARE, len(bytes(text)) + 1)
38-
except TypeError:
39-
# works on Python 3 (bytes() requires an encoding)
40-
hCd = kernel32.GlobalAlloc(
41-
GMEM_DDESHARE, len(bytes(text, 'ascii')) + 1)
38+
hCd = kernel32.GlobalAlloc(GMEM_DDESHARE, len(text) + 2)
4239
pchData = kernel32.GlobalLock(hCd)
43-
try:
44-
# works on Python 2 (bytes() only takes one argument)
45-
msvcrt.strcpy(
46-
c_char_p(pchData), bytes(text))
47-
except TypeError:
48-
# works on Python 3 (bytes() requires an encoding)
49-
msvcrt.strcpy(
50-
c_char_p(pchData), bytes(text, 'ascii'))
40+
msvcrt.wcscpy(c_wchar_p(pchData), text)
5141
kernel32.GlobalUnlock(hCd)
52-
user32.SetClipboardData(1, hCd)
42+
user32.SetClipboardData(CF_UNICODETEXT, hCd)
5343
user32.CloseClipboard()
5444

5545
def get_types(self):
5646
return list('text/plain',)
5747

48+

0 commit comments

Comments
 (0)