| 597 | } |
| 598 | |
| 599 | void TextFieldEx::deleteBackward(size_t numChars) |
| 600 | { |
| 601 | if (!_editable || !this->_enabled || 0 == _charCount) |
| 602 | { |
| 603 | axbeep(0); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | size_t len = _inputText.length(); |
| 608 | if (0 == len || _insertPos == 0) |
| 609 | { |
| 610 | axbeep(0); |
| 611 | // there is no string |
| 612 | // __updateCursorPosition(); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | // Length of characters to delete is based on input editor, but the actual |
| 617 | // length of the displayed text may be less |
| 618 | numChars = std::min(numChars, len); |
| 619 | |
| 620 | size_t totalDeleteLen = 0; |
| 621 | for (auto i = 0; i < numChars; ++i) |
| 622 | { |
| 623 | // get the delete byte number |
| 624 | size_t deleteLen = 1; // default, erase 1 byte |
| 625 | |
| 626 | // Calculate the actual number of bytes to delete for a specific character |
| 627 | while (0x80 == (0xC0 & _inputText.at(_insertPos - totalDeleteLen - deleteLen))) |
| 628 | { |
| 629 | ++deleteLen; |
| 630 | } |
| 631 | totalDeleteLen += deleteLen; |
| 632 | } |
| 633 | |
| 634 | // if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText.c_str() + len - deleteLen, |
| 635 | // static_cast<int>(deleteLen))) |
| 636 | //{ |
| 637 | // // delegate doesn't want to delete backwards |
| 638 | // return; |
| 639 | // } |
| 640 | |
| 641 | // if all text deleted, show placeholder string |
| 642 | if (len <= totalDeleteLen) |
| 643 | { |
| 644 | __moveCursor(-1); |
| 645 | |
| 646 | _inputText.clear(); |
| 647 | _charCount = 0; |
| 648 | _renderLabel->setTextColor(_colorSpaceHolder); |
| 649 | _renderLabel->setString(_placeHolder); |
| 650 | |
| 651 | // __updateCursorPosition(); |
| 652 | |
| 653 | // this->contentDirty = true; |
| 654 | |
| 655 | if (this->onTextModify) |
| 656 | this->onTextModify(); |