void TextField::deleteSelection() { if(getSelectionStart() == getSelectionEnd()) { return; } positionCaret(getSelectionEnd()); int e = getSelectionEnd(); int s = getSelectionStart(); for (int i = e; i > s; i--) { removeLastCharacter(); } setSelection(0,0); }
TEST(TextTest, removes_last_character_unicode) { std::string s("some utf8 text with ñ"); removeLastCharacter(s); EXPECT_EQ("some utf8 text with ", s); }
TEST(TextTest, removes_last_character) { std::string s("some utf8 texts"); removeLastCharacter(s); EXPECT_EQ("some utf8 text", s); }
void TextField::handleKeyboard( KeyEvent &keyEvent ) { if(handleHotkeys(keyEvent)) { return; } //delete the next character if(keyEvent.getKey() == KEY_DELETE) { if(getCaretPosition() == getTextLength() && getSelectionStart() == getSelectionEnd()) { return; } if(isReadOnly()) { return; } if(getSelectionStart() == getSelectionEnd()) { removeNextCharacter(); } else { deleteSelection(); } setBlinking(true); invalidateBlink(); return; } //delete the previous character if(keyEvent.getKey() == KEY_BACKSPACE) { if(getCaretPosition() == 0 && getSelectionStart() == getSelectionEnd()) { return; } if(isReadOnly()) { return; } if(getSelectionStart() == getSelectionEnd()) { removeLastCharacter(); } else { deleteSelection(); } setBlinking(true); invalidateBlink(); return; } if(keyEvent.getUnichar() >= ' ') { if(isReadOnly()) { setBlinking(true); invalidateBlink(); return; } if( isNumeric()) { if(keyEvent.getUnichar() >= 0x30 && keyEvent.getUnichar() <= 0x39 ) { deleteSelection(); addToNextCharacter(keyEvent.getUnichar()); setBlinking(true); invalidateBlink(); } else if(wantedDecimal() && keyEvent.getUnichar() == 0x2e ) { //check if there is already a decimal const char *text = getText().c_str(); for (int i = 0; i < getTextLength(); ++i) { if(text[i] == 0x2e) { return; } } deleteSelection(); addToNextCharacter(keyEvent.getUnichar()); setBlinking(true); invalidateBlink(); } else if(wantedMinus() && keyEvent.getUnichar() == 0x2d ) { //check if we are in the first position if(getCaretPosition() != 0) { return; } //check if there is already a minus const char *text = getText().c_str(); for (int i = 0; i < getTextLength(); ++i) { if(text[i] == 0x2d) { return; } } deleteSelection(); addToNextCharacter(keyEvent.getUnichar()); setBlinking(true); invalidateBlink(); } return; } deleteSelection(); addToNextCharacter(keyEvent.getUnichar()); setBlinking(true); invalidateBlink(); return; } switch (keyEvent.getExtendedKey()) { case EXT_KEY_RIGHT: if(getCaretPosition() == getTextLength() && getSelectionStart() != getSelectionEnd() && keyEvent.shift()) { return; } else if(getCaretPosition() == getTextLength() && getSelectionStart() == getSelectionEnd()) { return; } positionCaret(getCaretPosition() + 1); if(keyEvent.shift()) { if(getSelectionStart() == getSelectionEnd()) { setSelection(getCaretPosition() - 1, getCaretPosition()); } else { if(getCaretPosition() - 1 < getSelectionEnd()) setSelection(getSelectionEnd(), getCaretPosition()); else setSelection(getSelectionStart(), getCaretPosition()); } } else if(getSelectionStart() != getSelectionEnd()) { int caretPos = getSelectionEnd(); setSelection(0,0); positionCaret(caretPos); } setBlinking(true); invalidateBlink(); break; case EXT_KEY_LEFT: if(getCaretPosition() == 0 && getSelectionStart() != getSelectionEnd() && keyEvent.shift()) { return; } else if(getCaretPosition() == 0 && getSelectionStart() == getSelectionEnd()) { return; } positionCaret(getCaretPosition() - 1); if(keyEvent.shift()) { if(getSelectionStart() == getSelectionEnd()) { setSelection(getCaretPosition() + 1, getCaretPosition()); } else { if(getCaretPosition() + 1 < getSelectionEnd()) setSelection(getSelectionEnd(), getCaretPosition()); else setSelection(getSelectionStart(), getCaretPosition()); } } else if(getSelectionStart() != getSelectionEnd()) { int caretPos = getSelectionStart(); setSelection(0,0); positionCaret(caretPos); } setBlinking(true); invalidateBlink(); break; } }