diff --git a/pygame_gui/core/utility.py b/pygame_gui/core/utility.py index 35899ec1..e35e6455 100644 --- a/pygame_gui/core/utility.py +++ b/pygame_gui/core/utility.py @@ -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(): @@ -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]): diff --git a/pygame_gui/elements/ui_text_entry_box.py b/pygame_gui/elements/ui_text_entry_box.py index 3db473f0..133c4030 100644 --- a/pygame_gui/elements/ui_text_entry_box.py +++ b/pygame_gui/elements/ui_text_entry_box.py @@ -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 diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py index 95f2afc8..77668a3b 100644 --- a/pygame_gui/elements/ui_text_entry_line.py +++ b/pygame_gui/elements/ui_text_entry_line.py @@ -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