Browse Source

Made a stub implementation of copying and pasting within the application.

David Piuva 3 years ago
parent
commit
d9abaaeb59
2 changed files with 80 additions and 39 deletions
  1. 78 35
      Source/DFPSR/gui/components/TextBox.cpp
  2. 2 4
      Source/DFPSR/gui/components/TextBox.h

+ 78 - 35
Source/DFPSR/gui/components/TextBox.cpp

@@ -293,6 +293,21 @@ void TextBox::receiveMouseEvent(const MouseEvent& event) {
 	}
 }
 
+// TODO: Move stub implementation to an API and allow system wrappers to override it with a real implementation copying and pasting across different applications.
+String pasteBinStub;
+void saveToClipBoard(const ReadableString &text) {
+	pasteBinStub = text;
+}
+ReadableString readFromClipBoard() {
+	return pasteBinStub;
+}
+
+ReadableString TextBox::getSelectedText() {
+	int64_t selectionLeft = std::min(this->selectionStart, this->beamLocation);
+	int64_t selectionRight = std::max(this->selectionStart, this->beamLocation);
+	return string_exclusiveRange(this->text.value, selectionLeft, selectionRight);
+}
+
 void TextBox::replaceSelection(const ReadableString &replacingText) {
 	int64_t selectionLeft = std::min(this->selectionStart, this->beamLocation);
 	int64_t selectionRight = std::max(this->selectionStart, this->beamLocation);
@@ -373,7 +388,6 @@ static int64_t getLineEnd(const ReadableString &text, int64_t searchStart) {
 	return string_length(text);
 }
 
-// TODO: Copy and paste using a clipboard. (With automatic removal of new lines when multi-line is disabled)
 void TextBox::receiveKeyboardEvent(const KeyboardEvent& event) {
 	// Insert and scroll-lock is not supported.
 	if (event.keyboardEventType == KeyboardEventType::KeyDown) {
@@ -405,41 +419,70 @@ void TextBox::receiveKeyboardEvent(const KeyboardEvent& event) {
 		bool holdShift = this->combinationKeys & combinationKey_shift;
 		bool holdControl = this->combinationKeys & combinationKey_control;
 		bool removeSelection = !holdShift;
-		if (selected && (event.dsrKey == DsrKey_BackSpace || event.dsrKey == DsrKey_Delete)) {
-			// Remove selection
-			this->replaceSelection(U"");
-		} else if (event.dsrKey == DsrKey_BackSpace && canGoLeft) {
-			// Erase left of beam
-			this->beamLocation--;
-			this->replaceSelection(U"");
-		} else if (event.dsrKey == DsrKey_Delete && canGoRight) {
-			// Erase right of beam
-			this->beamLocation++;
-			this->replaceSelection(U"");
-		} else if (event.dsrKey == DsrKey_Home || (event.dsrKey == DsrKey_LeftArrow && holdControl)) {
-			// Move to the line start using Home or Ctrl + LeftArrow
-			this->placeBeamAtCharacter(getLineStart(this->text.value, this->beamLocation), removeSelection);
-		} else if (event.dsrKey == DsrKey_End || (event.dsrKey == DsrKey_RightArrow && holdControl)) {
-			// Move to the line end using End or Ctrl + RightArrow
-			this->placeBeamAtCharacter(getLineEnd(this->text.value, this->beamLocation), removeSelection);
-		} else if (event.dsrKey == DsrKey_LeftArrow && canGoLeft) {
-			// Move left using LeftArrow
-			this->placeBeamAtCharacter(this->beamLocation - 1, removeSelection);
-		} else if (event.dsrKey == DsrKey_RightArrow && canGoRight) {
-			// Move right using RightArrow
-			this->placeBeamAtCharacter(this->beamLocation + 1, removeSelection);
-		} else if (event.dsrKey == DsrKey_UpArrow) {
-			// Move up using UpArrow
-			this->moveBeamVertically(-1, removeSelection);
-		} else if (event.dsrKey == DsrKey_DownArrow) {
-			// Move down using DownArrow
-			this->moveBeamVertically(1, removeSelection);
-		} else if (event.dsrKey == DsrKey_Return) {
-			if (this->multiLine.value) {
-				this->replaceSelection(U'\n');
+		if (holdControl) {
+			if (event.dsrKey == DsrKey_LeftArrow) {
+				// Move to the line start using Ctrl + LeftArrow instead of Home
+				this->placeBeamAtCharacter(getLineStart(this->text.value, this->beamLocation), removeSelection);
+			} else if (event.dsrKey == DsrKey_RightArrow) {
+				// Move to the line end using Ctrl + RightArrow instead of End
+				this->placeBeamAtCharacter(getLineEnd(this->text.value, this->beamLocation), removeSelection);
+			} else if (event.dsrKey == DsrKey_X) {
+				// Cut selection using Ctrl + X
+				saveToClipBoard(this->getSelectedText());
+				this->replaceSelection(U"");
+			} else if (event.dsrKey == DsrKey_C) {
+				// Copy selection using Ctrl + C
+				saveToClipBoard(this->getSelectedText());
+			} else if (event.dsrKey == DsrKey_V) {
+				// Paste selection using Ctrl + V
+				this->replaceSelection(readFromClipBoard());
+			} else if (event.dsrKey == DsrKey_A) {
+				// Select all using Ctrl + A
+				this->selectionStart = 0;
+				this->beamLocation = string_length(this->text.value);
+				this->hasImages = false;
+			} else if (event.dsrKey == DsrKey_N) {
+				// Select nothing using Ctrl + N
+				this->selectionStart = this->beamLocation;
+				this->hasImages = false;
+			}
+		} else {
+			if (selected && (event.dsrKey == DsrKey_BackSpace || event.dsrKey == DsrKey_Delete)) {
+				// Remove selection
+				this->replaceSelection(U"");
+			} else if (event.dsrKey == DsrKey_BackSpace && canGoLeft) {
+				// Erase left of beam
+				this->beamLocation--;
+				this->replaceSelection(U"");
+			} else if (event.dsrKey == DsrKey_Delete && canGoRight) {
+				// Erase right of beam
+				this->beamLocation++;
+				this->replaceSelection(U"");
+			} else if (event.dsrKey == DsrKey_Home) {
+				// Move to the line start using Home
+				this->placeBeamAtCharacter(getLineStart(this->text.value, this->beamLocation), removeSelection);
+			} else if (event.dsrKey == DsrKey_End) {
+				// Move to the line end using End
+				this->placeBeamAtCharacter(getLineEnd(this->text.value, this->beamLocation), removeSelection);
+			} else if (event.dsrKey == DsrKey_LeftArrow && canGoLeft) {
+				// Move left using LeftArrow
+				this->placeBeamAtCharacter(this->beamLocation - 1, removeSelection);
+			} else if (event.dsrKey == DsrKey_RightArrow && canGoRight) {
+				// Move right using RightArrow
+				this->placeBeamAtCharacter(this->beamLocation + 1, removeSelection);
+			} else if (event.dsrKey == DsrKey_UpArrow) {
+				// Move up using UpArrow
+				this->moveBeamVertically(-1, removeSelection);
+			} else if (event.dsrKey == DsrKey_DownArrow) {
+				// Move down using DownArrow
+				this->moveBeamVertically(1, removeSelection);
+			} else if (event.dsrKey == DsrKey_Return) {
+				if (this->multiLine.value) {
+					this->replaceSelection(U'\n');
+				}
+			} else if (printable) {
+				this->replaceSelection(event.character);
 			}
-		} else if (printable) {
-			this->replaceSelection(event.character);
 		}
 		//printText(U"KeyType char=", event.character, " key=", event.dsrKey, U"\n");
 	}

+ 2 - 4
Source/DFPSR/gui/components/TextBox.h

@@ -29,10 +29,7 @@
 #include "../../api/fontAPI.h"
 #include "../../math/LVector.h"
 
-// TODO: Make a theme for the horizontal scroll-bar and activate it.
-// TODO: At least make a stub implementation for cutting, copying and pasting text within the same application.
-//       Then make it optional to integrate the clipboard with the external operating system using a window handle from somewhere.
-//       Copying within the same application is still useful if one has multiple files open at the same time or just need to move things around.
+// TODO: Make a theme for the horizontal scroll-bar.
 
 namespace dsr {
 
@@ -87,6 +84,7 @@ public:
 	LVector2D getTextOrigin(bool includeVerticalScroll);
 	int64_t findBeamLocationInLine(int64_t rowIndex, int64_t pixelX);
 	BeamLocation findBeamLocation(const LVector2D &pixelLocation);
+	ReadableString getSelectedText();
 	void replaceSelection(const ReadableString &replacingText);
 	void replaceSelection(DsrChar replacingCharacter);
 	void placeBeamAtCharacter(int64_t characterIndex, bool removeSelection);