Skip to content

Commit

Permalink
Merge pull request #424 from MyreMylar/use-scrap-for-paste
Browse files Browse the repository at this point in the history
Switch to using scrap for copy and paste post 2.2 pygame CE
  • Loading branch information
MyreMylar authored Apr 6, 2023
2 parents 9e728ef + 7991bd7 commit cf5d69d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
30 changes: 18 additions & 12 deletions pygame_gui/core/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,16 @@ def clipboard_copy(data: str):
:return: A platform specific copy function.
"""
current_platform = platform.system().upper()
if current_platform == 'WINDOWS':
__windows_copy(data)
elif current_platform == 'LINUX':
__linux_copy(data)
if pygame.vernum.major == 2 and pygame.vernum.minor >= 2:
pygame.scrap.put_text(data)
else:
__mac_copy(data)
current_platform = platform.system().upper()
if current_platform == 'WINDOWS':
__windows_copy(data)
elif current_platform == 'LINUX':
__linux_copy(data)
else:
__mac_copy(data)


def clipboard_paste():
Expand All @@ -208,13 +211,16 @@ def clipboard_paste():
:return: A platform specific paste function.
"""
current_platform = platform.system().upper()
if current_platform == 'WINDOWS':
return __windows_paste()
elif current_platform == 'LINUX':
return __linux_paste()
if pygame.vernum.major == 2 and pygame.vernum.minor >= 2:
return pygame.scrap.get_text()
else:
return __mac_paste()
current_platform = platform.system().upper()
if current_platform == 'WINDOWS':
return __windows_paste()
elif current_platform == 'LINUX':
return __linux_paste()
else:
return __mac_paste()


def create_resource_path(relative_path: Union[str, Path]):
Expand Down
44 changes: 23 additions & 21 deletions pygame_gui/elements/ui_text_entry_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,27 +522,29 @@ def _process_paste_event(self, event: Event) -> bool:
"""
consumed_event = False
if event.key == K_v and (event.mod & KMOD_CTRL or event.mod & KMOD_META):
new_text = self.convert_all_line_endings_to_unix(clipboard_paste())

if abs(self.select_range[0] - self.select_range[1]) > 0:
low_end = min(self.select_range[0], self.select_range[1])
high_end = max(self.select_range[0], self.select_range[1])
self.html_text = self.html_text[:low_end] + new_text + self.html_text[high_end:]
self.set_text(self.html_text)
self.edit_position = low_end + len(new_text)
self.text_box_layout.set_cursor_position(self.edit_position)
self.redraw_from_text_block()
self.select_range = [0, 0]
self.cursor_has_moved_recently = True
elif len(new_text) > 0:
self.html_text = (self.html_text[:self.edit_position] +
new_text +
self.html_text[self.edit_position:])
self.set_text(self.html_text)
self.edit_position += len(new_text)
self.text_box_layout.set_cursor_position(self.edit_position)
self.redraw_from_text_block()
self.cursor_has_moved_recently = True
paste = clipboard_paste()
if paste is not None:
new_text = self.convert_all_line_endings_to_unix(clipboard_paste())

if abs(self.select_range[0] - self.select_range[1]) > 0:
low_end = min(self.select_range[0], self.select_range[1])
high_end = max(self.select_range[0], self.select_range[1])
self.html_text = self.html_text[:low_end] + new_text + self.html_text[high_end:]
self.set_text(self.html_text)
self.edit_position = low_end + len(new_text)
self.text_box_layout.set_cursor_position(self.edit_position)
self.redraw_from_text_block()
self.select_range = [0, 0]
self.cursor_has_moved_recently = True
elif len(new_text) > 0:
self.html_text = (self.html_text[:self.edit_position] +
new_text +
self.html_text[self.edit_position:])
self.set_text(self.html_text)
self.edit_position += len(new_text)
self.text_box_layout.set_cursor_position(self.edit_position)
self.redraw_from_text_block()
self.cursor_has_moved_recently = True
consumed_event = True
return consumed_event

Expand Down
21 changes: 12 additions & 9 deletions pygame_gui/elements/ui_text_entry_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,18 @@ def validate_text_string(self, text_to_validate: str) -> bool:
"""
is_valid = True
if self.forbidden_characters is not None:
for character in text_to_validate:
if character in self.forbidden_characters:
is_valid = False

if is_valid and self.allowed_characters is not None:
for character in text_to_validate:
if character not in self.allowed_characters:
is_valid = False
if text_to_validate is not None:
if self.forbidden_characters is not None:
for character in text_to_validate:
if character in self.forbidden_characters:
is_valid = False

if is_valid and self.allowed_characters is not None:
for character in text_to_validate:
if character not in self.allowed_characters:
is_valid = False
else:
is_valid = False

return is_valid

Expand Down

0 comments on commit cf5d69d

Please sign in to comment.