diff options
Diffstat (limited to 'src/interface/Textbox.cpp')
| -rw-r--r-- | src/interface/Textbox.cpp | 163 |
1 files changed, 155 insertions, 8 deletions
diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp index fbe3c6b..54a9d40 100644 --- a/src/interface/Textbox.cpp +++ b/src/interface/Textbox.cpp @@ -5,6 +5,7 @@ #include "interface/Point.h" #include "interface/Textbox.h" #include "interface/Keys.h" +#include "ContextMenu.h" using namespace ui; @@ -19,6 +20,11 @@ Textbox::Textbox(Point position, Point size, std::string textboxText, std::strin SetText(textboxText); cursor = text.length(); + + menu->RemoveItem(0); + menu->AddItem(ContextMenuItem("Cut", 1, true)); + menu->AddItem(ContextMenuItem("Copy", 0, true)); + menu->AddItem(ContextMenuItem("Paste", 2, true)); } Textbox::~Textbox() @@ -27,6 +33,18 @@ Textbox::~Textbox() delete actionCallback; } +void Textbox::SetHidden(bool hidden) +{ + menu->RemoveItem(0); + menu->RemoveItem(1); + menu->RemoveItem(2); + menu->AddItem(ContextMenuItem("Cut", 1, !hidden)); + menu->AddItem(ContextMenuItem("Copy", 0, !hidden)); + menu->AddItem(ContextMenuItem("Paste", 2, true)); + + masked = hidden; +} + void Textbox::SetPlaceholder(std::string text) { placeHolder = text; @@ -65,9 +83,134 @@ std::string Textbox::GetText() return backingText; } +void Textbox::OnContextMenuAction(int item) +{ + switch(item) + { + case 0: + copySelection(); + break; + case 1: + cutSelection(); + break; + case 2: + pasteIntoSelection(); + break; + } +} + +void Textbox::cutSelection() +{ + char * clipboardText; + clipboardText = clipboard_pull_text(); + std::string newText = std::string(clipboardText); + free(clipboardText); + if(HasSelection()) + { + if(getLowerSelectionBound() < 0 || getHigherSelectionBound() > backingText.length()) + return; + clipboard_push_text((char*)backingText.substr(getLowerSelectionBound(), getHigherSelectionBound()-getLowerSelectionBound()).c_str()); + backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound()); + cursor = getLowerSelectionBound(); + } + else + { + clipboard_push_text((char*)backingText.c_str()); + backingText.clear(); + } + ClearSelection(); + + if(masked) + { + std::string maskedText = std::string(backingText); + std::fill(maskedText.begin(), maskedText.end(), '\x8D'); + Label::SetText(maskedText); + } + else + { + text = backingText; + } + if(actionCallback) + actionCallback->TextChangedCallback(this); + + if(multiline) + updateMultiline(); + updateSelection(); + TextPosition(text); + + if(cursor) + { + Graphics::PositionAtCharIndex(multiline?((char*)textLines.c_str()):((char*)text.c_str()), cursor, cursorPositionX, cursorPositionY); + } + else + { + cursorPositionY = cursorPositionX = 0; + } +} + +void Textbox::pasteIntoSelection() +{ + char * clipboardText; + clipboardText = clipboard_pull_text(); + std::string newText = std::string(clipboardText); + free(clipboardText); + if(HasSelection()) + { + if(getLowerSelectionBound() < 0 || getHigherSelectionBound() > backingText.length()) + return; + backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound()); + cursor = getLowerSelectionBound(); + } + backingText.insert(cursor, newText); + ClearSelection(); + + if(masked) + { + std::string maskedText = std::string(backingText); + std::fill(maskedText.begin(), maskedText.end(), '\x8D'); + Label::SetText(maskedText); + } + else + { + text = backingText; + } + if(actionCallback) + actionCallback->TextChangedCallback(this); + + if(multiline) + updateMultiline(); + updateSelection(); + TextPosition(text); + + if(cursor) + { + Graphics::PositionAtCharIndex(multiline?((char*)textLines.c_str()):((char*)text.c_str()), cursor, cursorPositionX, cursorPositionY); + } + else + { + cursorPositionY = cursorPositionX = 0; + } +} + void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) { bool changed = false; + if(ctrl && key == 'c' && !masked) + { + copySelection(); + return; + } + if(ctrl && key == 'v') + { + pasteIntoSelection(); + return; + } + if(ctrl && key == 'x' && !masked) + { + cutSelection(); + return; + } + try { switch(key) @@ -190,15 +333,19 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool void Textbox::OnMouseClick(int x, int y, unsigned button) { - mouseDown = true; - cursor = Graphics::CharIndexAtPosition(multiline?((char*)textLines.c_str()):((char*)text.c_str()), x-textPosition.X, y-textPosition.Y); - if(cursor) - { - Graphics::PositionAtCharIndex(multiline?((char*)textLines.c_str()):((char*)text.c_str()), cursor, cursorPositionX, cursorPositionY); - } - else + + if(button != BUTTON_RIGHT) { - cursorPositionY = cursorPositionX = 0; + mouseDown = true; + cursor = Graphics::CharIndexAtPosition(multiline?((char*)textLines.c_str()):((char*)text.c_str()), x-textPosition.X, y-textPosition.Y); + if(cursor) + { + Graphics::PositionAtCharIndex(multiline?((char*)textLines.c_str()):((char*)text.c_str()), cursor, cursorPositionX, cursorPositionY); + } + else + { + cursorPositionY = cursorPositionX = 0; + } } Label::OnMouseClick(x, y, button); } |
