📄 textbox.cpp
字号:
else if (key.getValue() == Key::BACKSPACE && mCaretColumn != 0 && mEditable) { int newpos = UTF8GetPrev(mTextRows[mCaretRow], mCaretColumn); mTextRows[mCaretRow].erase(newpos, mCaretColumn - newpos); mCaretColumn = newpos; ret = true; } else if (key.getValue() == Key::BACKSPACE && mCaretColumn == 0 && mCaretRow != 0 && mEditable) { mCaretColumn = mTextRows[mCaretRow - 1].size(); mTextRows[mCaretRow - 1] += mTextRows[mCaretRow]; mTextRows.erase(mTextRows.begin() + mCaretRow); --mCaretRow; ret = true; } else if (key.getValue() == Key::DELETE && mCaretColumn < (int)mTextRows[mCaretRow].size() && mEditable) { int newpos = UTF8GetNext(mTextRows[mCaretRow], mCaretColumn); mTextRows[mCaretRow].erase(mCaretColumn, newpos - mCaretColumn); ret = true; } else if (key.getValue() == Key::DELETE && mCaretColumn == (int)mTextRows[mCaretRow].size() && mCaretRow < ((int)mTextRows.size() - 1) && mEditable) { mTextRows[mCaretRow] += mTextRows[mCaretRow + 1]; mTextRows.erase(mTextRows.begin() + mCaretRow + 1); ret = true; } else if(key.getValue() == Key::PAGE_UP) { int w, h, rowsPerPage; getParent()->getDrawSize(w, h, this); rowsPerPage = h / getFont()->getHeight(); mCaretRow -= rowsPerPage; if (mCaretRow < 0) { mCaretRow = 0; } ret = true; } else if(key.getValue() == Key::PAGE_DOWN) { int w, h, rowsPerPage; getParent()->getDrawSize(w, h, this); rowsPerPage = h / getFont()->getHeight(); mCaretRow += rowsPerPage; if (mCaretRow >= (int)mTextRows.size()) { mCaretRow = mTextRows.size() - 1; } ret = true; } else if(key.getValue() == Key::TAB && mEditable) { mTextRows[mCaretRow].insert(mCaretColumn,std::string(" ")); mCaretColumn += 4; ret = true; } else if (key.getValue() == 'v' - 'a' + 1 && mEditable) // ctrl-v { std::string str; if (GetClipboard(str) >= 0) { for (size_t i = 0; i < str.size(); ++i) { keyPress(Key(str[i])); } ret = true; } } else if (key.isCharacter() && mEditable) { mTextRows[mCaretRow].insert(mCaretColumn,key.toString()); mCaretColumn = UTF8GetNext(mTextRows[mCaretRow], mCaretColumn); ret = true; } adjustSize(); scrollToCaret(); return ret; } void TextBox::adjustSize() { unsigned int i; int width = 0; for (i = 0; i < mTextRows.size(); ++i) { int w = getFont()->getWidth(mTextRows[i]); if (width < w) { width = w; } } setWidth(width + 1); setHeight(getFont()->getHeight() * mTextRows.size()); } void TextBox::setCaretPosition(unsigned int position) { int row; for (row = 0; row < (int)mTextRows.size(); row++) { if (position <= mTextRows[row].size()) { mCaretRow = row; mCaretColumn = position; return; // we are done } else { position--; } } // position beyond end of text mCaretRow = mTextRows.size() - 1; mCaretColumn = mTextRows[mCaretRow].size(); } unsigned int TextBox::getCaretPosition() const { int pos = 0, row; for (row = 0; row < mCaretRow; row++) { pos += mTextRows[row].size(); } return pos + mCaretColumn; } void TextBox::setCaretRowColumn(int row, int column) { setCaretRow(row); setCaretColumn(column); } void TextBox::setCaretRow(int row) { mCaretRow = row; if (mCaretRow >= (int)mTextRows.size()) { mCaretRow = mTextRows.size() - 1; } if (mCaretRow < 0) { mCaretRow = 0; } setCaretColumn(mCaretColumn); } unsigned int TextBox::getCaretRow() const { return mCaretRow; } void TextBox::setCaretColumn(int column) { mCaretColumn = column; if (mCaretColumn > (int)mTextRows[mCaretRow].size()) { mCaretColumn = mTextRows[mCaretRow].size(); } if (mCaretColumn < 0) { mCaretColumn = 0; } mCaretColumn = FindNext(mTextRows[mCaretRow], mCaretColumn); } unsigned int TextBox::getCaretColumn() const { return mCaretColumn; } const std::string& TextBox::getTextRow(int row) const { return mTextRows[row]; } void TextBox::setTextRow(int row, const std::string& text) { mTextRows[row] = text; if (mCaretRow == row) { setCaretColumn(mCaretColumn); } adjustSize(); } unsigned int TextBox::getNumberOfRows() const { return mTextRows.size(); } std::string TextBox::getText() const { if (mTextRows.size() == 0) { return std::string(""); } int i; std::string text; for (i = 0; i < (int)mTextRows.size() - 1; ++i) { text = text + mTextRows[i] + "\n"; } text = text + mTextRows[i]; return text; } void TextBox::fontChanged() { adjustSize(); } void TextBox::scrollToCaret() { Widget *par = getParent(); if (par == NULL) { return; } ScrollArea* scrollArea = dynamic_cast<ScrollArea *>(par); if (scrollArea != NULL) { Rectangle scroll; scroll.x = getFont()->getWidth(mTextRows[mCaretRow].substr(0, mCaretColumn)); scroll.y = getFont()->getHeight() * mCaretRow; scroll.width = 6; scroll.height = getFont()->getHeight() + 2; // add 2 for some extra space scrollArea->scrollToRectangle(scroll); } } void TextBox::setEditable(bool editable) { mEditable = editable; } bool TextBox::isEditable() const { return mEditable; } void TextBox::addRow(const std::string row) { mTextRows.push_back(row); adjustSize(); } bool TextBox::isOpaque() { return mOpaque; } void TextBox::setOpaque(bool opaque) { mOpaque = opaque; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -