/************************************************************************* Handler for when character (printable keys) are typed *************************************************************************/ void MultiLineEditbox::onCharacter(KeyEventArgs& e) { // base class processing Window::onCharacter(e); // only need to take notice if we have focus if (hasInputFocus() && !isReadOnly() && getFont()->isCodepointAvailable(e.codepoint)) { // erase selected text eraseSelectedText(); // if there is room if (d_text.length() - 1 < d_maxTextLen) { d_text.insert(getCaratIndex(), 1, e.codepoint); d_caratPos++; WindowEventArgs args(this); onTextChanged(args); } else { // Trigger text box full event WindowEventArgs args(this); onEditboxFullEvent(args); } } e.handled = true; }
//----------------------------------------------------------------------------// void Editbox::handleDelete(void) { if (!isReadOnly()) { String tmp(getText()); if (getSelectionLength() != 0) { tmp.erase(getSelectionStartIndex(), getSelectionLength()); if (handleValidityChangeForString(tmp)) { // erase selection using mode that does not modify getText() // (we just want to update state) eraseSelectedText(false); // set text to the newly modified string setText(tmp); } } else if (getCaretIndex() < tmp.length()) { tmp.erase(d_caretPos, 1); if (handleValidityChangeForString(tmp)) { // set text to the newly modified string setText(tmp); } } } }
void Editbox::handleDelete(void) { if (!isReadOnly()) { String tmp(getText()); if (getSelectionLength() != 0) { UndoHandler::UndoAction undoSelection; undoSelection.d_type = UndoHandler::UAT_DELETE; undoSelection.d_startIdx = getSelectionStart(); undoSelection.d_text = tmp.substr(getSelectionStart(), getSelectionLength()); tmp.erase(getSelectionStart(), getSelectionLength()); if (handleValidityChangeForString(tmp)) { // erase selection using mode that does not modify getText() // (we just want to update state) eraseSelectedText(false); // set text to the newly modified string setText(tmp); d_undoHandler->addUndoHistory(undoSelection); } } else if (getCaretIndex() < tmp.length()) { UndoHandler::UndoAction undo; undo.d_type = UndoHandler::UAT_DELETE; undo.d_startIdx = d_caretPos; #if CEGUI_STRING_CLASS != CEGUI_STRING_CLASS_UTF_8 size_t eraseLength = 1; #else size_t eraseLength = String::getCodePointSize(tmp[d_caretPos]); #endif undo.d_text = tmp.substr(d_caretPos, eraseLength); tmp.erase(d_caretPos, eraseLength); if (handleValidityChangeForString(tmp)) { // set text to the newly modified string setText(tmp); d_undoHandler->addUndoHistory(undo); } } } }
bool Editbox::performPaste(Clipboard& clipboard) { if (isReadOnly()) return false; String clipboardText = clipboard.getText(); if (clipboardText.empty()) return false; // backup current text String tmp(getText()); UndoHandler::UndoAction undoSelection; undoSelection.d_type = UndoHandler::UAT_DELETE; undoSelection.d_startIdx = getSelectionStart(); undoSelection.d_text = tmp.substr(getSelectionStart(), getSelectionLength()); tmp.erase(getSelectionStart(), getSelectionLength()); // if there is room if (tmp.length() < d_maxTextLen) { UndoHandler::UndoAction undo; undo.d_type = UndoHandler::UAT_INSERT; undo.d_startIdx = getCaretIndex(); undo.d_text = clipboardText; tmp.insert(getSelectionStart(), clipboardText); if (handleValidityChangeForString(tmp)) { // erase selection using mode that does not modify getText() // (we just want to update state) eraseSelectedText(false); // advance caret (done first so we can "do stuff" in event // handlers!) d_caretPos += clipboardText.length(); // set text to the newly modified string setText(tmp); d_undoHandler->addUndoHistory(undo); if (getSelectionLength() > 0) d_undoHandler->addUndoHistory(undoSelection); return true; } } return false; }
//----------------------------------------------------------------------------// void Editbox::onCharacter(KeyEventArgs& e) { // NB: We are not calling the base class handler here because it propogates // inputs back up the window hierarchy, whereas, as a consumer of key // events, we want such propogation to cease with us regardless of whether // we actually handle the event. // fire event. fireEvent(EventCharacterKey, e, Window::EventNamespace); // only need to take notice if we have focus if (e.handled == 0 && hasInputFocus() && !isReadOnly() && getFont()->isCodepointAvailable(e.codepoint)) { // backup current text String tmp(getText()); tmp.erase(getSelectionStartIndex(), getSelectionLength()); // if there is room if (tmp.length() < d_maxTextLen) { tmp.insert(getSelectionStartIndex(), 1, e.codepoint); if (handleValidityChangeForString(tmp)) { // erase selection using mode that does not modify getText() // (we just want to update state) eraseSelectedText(false); // advance caret (done first so we can "do stuff" in event // handlers!) d_caretPos++; // set text to the newly modified string setText(tmp); // char was accepted into the Editbox - mark event as handled. ++e.handled; } } else { // Trigger text box full event WindowEventArgs args(this); onEditboxFullEvent(args); } } // event was (possibly) not handled }
/************************************************************************* Processing for backspace key *************************************************************************/ void MultiLineEditbox::handleBackspace(void) { if (!isReadOnly()) { if (getSelectionLength() != 0) { eraseSelectedText(); } else if (d_caratPos > 0) { d_text.erase(d_caratPos - 1, 1); setCaratIndex(d_caratPos - 1); WindowEventArgs args(this); onTextChanged(args); } } }
/************************************************************************* Processing for Delete key *************************************************************************/ void MultiLineEditbox::handleDelete(void) { if (!isReadOnly()) { if (getSelectionLength() != 0) { eraseSelectedText(); } else if (getCaratIndex() < d_text.length() - 1) { d_text.erase(d_caratPos, 1); ensureCaratIsVisible(); WindowEventArgs args(this); onTextChanged(args); } } }
/************************************************************************* Processing to insert a new line / paragraph. *************************************************************************/ void MultiLineEditbox::handleNewLine(uint sysKeys) { if (!isReadOnly()) { // erase selected text eraseSelectedText(); // if there is room if (d_text.length() - 1 < d_maxTextLen) { d_text.insert(getCaratIndex(), 1, 0x0a); d_caratPos++; WindowEventArgs args(this); onTextChanged(args); } } }
//----------------------------------------------------------------------------// bool Editbox::performPaste(Clipboard& clipboard) { if (isReadOnly()) return false; String clipboardText = clipboard.getText(); if (clipboardText.empty()) return false; // backup current text String tmp(getText()); tmp.erase(getSelectionStartIndex(), getSelectionLength()); // if there is room if (tmp.length() < d_maxTextLen) { tmp.insert(getSelectionStartIndex(), clipboardText); if (handleValidityChangeForString(tmp)) { // erase selection using mode that does not modify getText() // (we just want to update state) eraseSelectedText(false); // advance caret (done first so we can "do stuff" in event // handlers!) d_caretPos += clipboardText.length(); // set text to the newly modified string setText(tmp); return true; } } return false; }
void Editbox::handleBackspace(void) { if (!isReadOnly()) { String tmp(getText()); if (getSelectionLength() != 0) { UndoHandler::UndoAction undoSelection; undoSelection.d_type = UndoHandler::UAT_DELETE; undoSelection.d_startIdx = getSelectionStart(); undoSelection.d_text = tmp.substr(getSelectionStart(), getSelectionLength()); tmp.erase(getSelectionStart(), getSelectionLength()); if (handleValidityChangeForString(tmp)) { // erase selection using mode that does not modify getText() // (we just want to update state) eraseSelectedText(false); // set text to the newly modified string setText(tmp); d_undoHandler->addUndoHistory(undoSelection); } } else if (getCaretIndex() > 0) { UndoHandler::UndoAction undo; undo.d_type = UndoHandler::UAT_DELETE; #if CEGUI_STRING_CLASS != CEGUI_STRING_CLASS_UTF_8 size_t deleteStartPos = d_caretPos - 1; size_t deleteLength = 1; #else String::codepoint_iterator caretIter(tmp.begin() + d_caretPos, tmp.begin(), tmp.end()); --caretIter; size_t deleteStartPos = caretIter.getCodeUnitIndexFromStart(); size_t deleteLength = d_caretPos - deleteStartPos; #endif undo.d_startIdx = deleteStartPos; undo.d_text = tmp.substr(deleteStartPos, deleteLength); tmp.erase(deleteStartPos, deleteLength); if (handleValidityChangeForString(tmp)) { setCaretIndex(deleteStartPos); // set text to the newly modified string setText(tmp); d_undoHandler->addUndoHistory(undo); } } } }