From 572f724a71a851b717f1a9938fd21e4fd24682c2 Mon Sep 17 00:00:00 2001
From: David Sangrey <rixxan@hullseals.space>
Date: Thu, 28 Mar 2024 12:59:22 -0400
Subject: [PATCH] [1471] Add PIL to Improve Clipboard

---
 L10n/en.template |  5 ++++-
 build.py         |  1 -
 myNotebook.py    | 22 ++++++++++++++++++----
 requirements.txt |  1 +
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/L10n/en.template b/L10n/en.template
index f5acb377..bf8d022e 100644
--- a/L10n/en.template
+++ b/L10n/en.template
@@ -802,4 +802,7 @@
 "Ships" = "Ships";
 
 /* update.py: Update Available Text; In files: update.py:229; */
-"{NEWVER} is available" = "{NEWVER} is available";
\ No newline at end of file
+"{NEWVER} is available" = "{NEWVER} is available";
+
+/* myNotebook.py: Can't Paste Images or Files in Text; */
+"Cannot paste non-text content." = "Cannot paste non-text content.";
diff --git a/build.py b/build.py
index 1bc96765..2d4ef790 100644
--- a/build.py
+++ b/build.py
@@ -133,7 +133,6 @@ def build() -> None:
                 "distutils",
                 "_markerlib",
                 "optparse",
-                "PIL",
                 "simplejson",
                 "unittest",
                 "doctest",
diff --git a/myNotebook.py b/myNotebook.py
index 63bb7dc8..398b2136 100644
--- a/myNotebook.py
+++ b/myNotebook.py
@@ -14,7 +14,12 @@ from __future__ import annotations
 
 import sys
 import tkinter as tk
-from tkinter import ttk
+from tkinter import ttk, messagebox
+from typing import TYPE_CHECKING
+from PIL import ImageGrab
+
+if TYPE_CHECKING:
+    def _(x: str) -> str: return x
 
 # Can't do this with styles on OSX - http://www.tkdocs.com/tutorial/styles.html#whydifficult
 if sys.platform == 'darwin':
@@ -126,13 +131,22 @@ class EntryMenu(ttk.Entry):
 
     def paste(self) -> None:
         """Paste the selected Entry text."""
-        if self.selection_present():
-            self.delete(tk.SEL_FIRST, tk.SEL_LAST)
         try:
+            # Attempt to grab an image from the clipboard (apprently also works for files)
+            img = ImageGrab.grabclipboard()
+            if img:
+                # Hijack existing translation, yes it doesn't exactly match here.
+                # LANG: Generic error prefix - following text is from Frontier auth service;
+                messagebox.showwarning(_('Error'),
+                                       _('Cannot paste non-text content.'))  # LANG: Can't Paste Images or Files in Text
+                return
             text = self.clipboard_get()
+            if self.selection_present() and text:
+                self.delete(tk.SEL_FIRST, tk.SEL_LAST)
             self.insert(tk.INSERT, text)
         except tk.TclError:
-            pass  # No text in clipboard or clipboard is not text
+            # No text in clipboard or clipboard is not text
+            pass
 
 
 class Entry(sys.platform == 'darwin' and tk.Entry or EntryMenu or ttk.Entry):  # type: ignore
diff --git a/requirements.txt b/requirements.txt
index 398041a1..e0e6138f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,5 +1,6 @@
 certifi==2023.11.17
 requests==2.31.0
+pillow==10.2.0
 # requests depends on this now ?
 charset-normalizer==2.1.1