//adds a character to the text array void textIn::addChar(int _c){ //creates a text graphic assigns a font and colour and size and pushes it to back of text vector sf::Text temp; temp.setFont(font); temp.setString( static_cast<char>(_c)); temp.setColor(sf::Color::White); temp.setCharacterSize(getHeight()); text.push_back(temp); if(_c == 32){ text.back().setString("_"); } //if it is the first in the array set the position of the first character to be within the start of the text box if(text.size() == 1){ text.at(0).setPosition(getPosX(), getPosY() - (getHeight()/5)); }else{ //this puts the char in args after the previous char in the array text.back().setPosition(text.end()[-2].getPosition().x + text.end()[-2].getLocalBounds().width , getPosY() - (getHeight()/5)); } //moves the caret along one since a caret has been added setCaret( text.back().getPosition().x + text.back().getLocalBounds().width, getPosY(), getHeight()); //stores the position of the caret on an array and iterates an iterator for it by one caretPosition.push_back(caret.getPosition().x); caretIndex++; }
void CHexViewView::ctrlEnd(bool shift) { m_shift = shift; if((m_topLine < m_maxTopLine) || (m_lineOffset < m_maxLineOffset)) { setTopLine(m_maxTopLine, false).setLineOffset(m_maxLineOffset, false).setCaret(m_maxCaret.x, m_maxCaret.y); } else { setCaret(m_maxCaret.x, m_maxCaret.y, false); } }
void CHexViewView::ctrlHome(bool shift) { m_shift = shift; if(m_topLine || m_lineOffset) { setTopLine(0, false).setLineOffset(0, false).setCaret(0,0); } else { setCaret(0,0, false); } }
//removes last character void textIn::removeChar(){ if(text.size() > 0){ //removes last char and chars position on an array as well as an iterator for iterating over it text.pop_back(); caretPosition.pop_back(); //moves caret back setCaret( text.back().getPosition().x + text.back().getLocalBounds().width, getPosY(), getHeight()); caretIndex--; }else{ //if the text array is empty then return the caret to start setCaret( getPosX() + 1, getPosY(), getHeight()); caretIndex = 0; } }
void cTextField::replaceSelection(const QCString &replacement) { // Delete the selection, reposition caret_, then reinsert if (selection_ != 0) { if (selection_ < 0) { setCaret(caret_ + selection_); // Place the caret at the start of the selection selection_ = - selection_; } for (int i = 0; i < selection_ && caret_ < text_.length(); ++i) { text_.remove(caret_, 1); } selection_ = 0; invalidateText(); } // Insert text at the caret int i; for (i = 0; i < (int)replacement.length() && text_.length() + 1 <= maxLength_; ++i) { text_.insert(caret_ + i, replacement.at(i)); } setCaret(caret_ + i); }
//removes char from position in args and changes subsequent objects to be drawn properly void textIn::removeChar(int _i){ try{ //creates an iterator for text vector assigned from arg 1 auto it = text.begin(); if(_i != 1){ //remove the character in the vector at index using the iterator text.erase(it + (_i -1)); //iterates all text in front of inserted text to be drawn in front of the char before it (shimmies all the other letters along) for(int x = (_i-1); x < text.size(); x++){ text.at(x).setPosition((text.at(x -1).getPosition().x + text.at(x -1).getLocalBounds().width) , getPosY() - (getHeight()/5)); } }else if(_i != 0){ //delete first element text.erase(it); //set first element to be at the front of the text box text.at(0).setPosition(getPosX() , getPosY() - (getHeight()/5)); //iterates all text in front of inserted text to be drawn in front of the char before it (shimmies all the other letters along) for(int x = 1; x < text.size(); x++){ text.at(x).setPosition((text.at(x -1).getPosition().x + text.at(x -1).getLocalBounds().width) , getPosY()- (getHeight()/5)); } } if(_i != 0){ //reorganises all positions of caret resetPositions(); caretIndex--; setCaret(caretPosition.at(caretIndex), getPosY(), getHeight()); //stores the new position of the character at the very front caretPosition.push_back(text.at(text.size() -1).getPosition().x + text.at(text.size() -1).getLocalBounds().width); } }catch( std::out_of_range){ std::cout<< "Range error removing char"; } }
void CHexViewView::setCurrentAddr(unsigned __int64 addr, bool invalidate) { if(addr > m_docSize) { return; } keepSelection(); if(addr != getCurrentAddr() && hasAnchor()) { invalidate = true; } const __int64 newTopLine = getBestTopLine( addr); const int newOffset = getBestLineOffset(addr); if((newTopLine != m_topLine) || (newOffset != m_lineOffset)) { setTopLine(newTopLine, false).setLineOffset(newOffset, false); invalidate = true; } setCaret(GETLINEOFFSET(addr) - m_lineOffset, (int)(GETLINENO(addr) - m_topLine), invalidate); }
void CHexViewView::charRight(bool shift) { m_shift = shift; if(m_caret.x < m_maxCaret.x) { setCaretX(m_caret.x+1, false); } else if(m_lineOffset < m_maxLineOffset) { scrollHorz(1); } else if(getSettings().getWrapEndOfLine() && getCurrentAddr() < m_docSize - 1) { if(m_caret.y < m_maxCaret.y) { if(m_lineOffset > 0) { setLineOffset(0, false).setCaret(0, m_caret.y+1); } else { setCaret(0, m_caret.y+1, false); } } else { setTopLine(m_topLine+1, false).setLineOffset(0, false).setCaretX(0); } } }
void CHexViewView::charLeft(bool shift) { m_shift = shift; if(m_caret.x > 0) { setCaretX(m_caret.x-1, false); } else if(m_lineOffset > 0) { scrollHorz(-1); } else if(getSettings().getWrapEndOfLine() && getCurrentAddr() > 0) { if(m_caret.y > 0) { if(m_lineOffset < m_maxLineOffset) { setLineOffset(m_maxLineOffset, false).setCaret(m_maxCaret.x, m_caret.y-1); } else { setCaret(m_maxCaret.x, m_caret.y-1, false); } } else { setTopLine(m_topLine-1, false).setLineOffset(m_maxLineOffset, false).setCaretX(m_maxCaret.x); } } }
void cTextField::onMouseDown(QMouseEvent *e) { cControl::onMouseDown(e); QPoint local = mapFromGlobal(e->pos()); int index = getOffset(local.x()); selection_ = 0; // XXXXXX TODO: Normal windows selection rules with mouse // Selection? if ((e->state() & Key_Shift) != 0) { // In which direction do we select? int diff = caret_ - index; // The offset would be the new selection value selection_ = diff; } setCaret(index); }
//constructor with button object initialised in initialiser list textIn::textIn ( int _x, int _y, int _width, int _height, sf::RenderWindow &_rw): Sprites( _rw, "media/cb.bmp") { //selected is default false setSelected(false); //set x y and width of box heights set at 30 for now setXY( _x, _y); setWH(_width, _height); setCaret(_x, _y, getHeight()); caretIndex = 1; caretPosition.push_back(caretX); if (!font.loadFromFile("media/Pink Bunny.ttf")){ std::cout<<"Likely name of font incorrect"; } setRects(); }
void cTextField::onKeyDown(QKeyEvent *e) { int key = e->key(); Qt::ButtonState state = e->state(); // Handle special chars if (key == Qt::Key_Backspace) { // Replace the selection with an empty string if (selection_ != 0) { replaceSelection(""); } else if (caret_ > 0) { setCaret(caret_ - 1); text_.remove(caret_, 1); invalidateText(); } } else if (key == Qt::Key_Delete) { if (selection_ != 0) { replaceSelection(""); } else if (caret_ < text_.length()) { text_.remove(caret_, 1); invalidateText(); } } else if (key == Qt::Key_Left) { if (caret_ > 0) { setCaret(caret_ - 1); if ((state & Qt::ShiftButton) != 0) { selection_++; invalidateText(); } else { if (selection_ != 0) { selection_ = 0; invalidateText(); } } } } else if (key == Qt::Key_Right) { if (caret_ < text_.length()) { setCaret(caret_ + 1); if ((state & Qt::ShiftButton) != 0) { selection_--; invalidateText(); } else { if (selection_ != 0) { selection_ = 0; invalidateText(); } } } } else if (key == Qt::Key_V && (state & Qt::ControlButton) != 0) { QClipboard *clipboard = qApp->clipboard(); QString text = clipboard->text(); if (!text.isEmpty()) { QCString ltext = text.latin1(); replaceSelection(ltext); } } else if (key == Qt::Key_C && (state & Qt::ControlButton) != 0) { QClipboard *clipboard = qApp->clipboard(); QCString text = getSelection(); if (!text.isEmpty()) { clipboard->setText(QString(text), QClipboard::Clipboard); } } else if (key == Qt::Key_X && (state & Qt::ControlButton) != 0) { QClipboard *clipboard = qApp->clipboard(); QCString text = getSelection(); if (!text.isEmpty()) { clipboard->setText(QString(text), QClipboard::Clipboard); replaceSelection(""); } } else if (key == Qt::Key_Return) { onEnter(); } else if (key == Qt::Key_Home) { int oldCaret = caret_; selection_ = 0; setCaret(0); invalidateText(); /*if (key.mod & KMOD_SHIFT) { selection_--; invalidateText(); } else { if (selection_ != 0) { selection_ = 0; invalidateText(); } }*/ } else if (key == Qt::Key_End) { setCaret(text_.length()); selection_ = 0; invalidateText(); } else if (text_.length() < maxLength_) { char ch = e->text().at(0).latin1(); // Check if the character is supported by the current font. if (ch != 0) { cSurface *chs = AsciiFonts->getCharacter(font_, ch); if (chs) { QCString replacement; replacement.insert(0, ch); replaceSelection(replacement); } } } }
//listens to key presses when selected, records all keyboard input void textIn::keyListen(sf::Event &e){ try{ //if there are any text objects to cycle through if(text.size() != 0){ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && caretIndex > 0){ //this is on the condition that the caret is not moved to the back of the box when it is //writing text that is longer than the box, the user can delete stuff rather than go back //to see what they have written as this kind of input box is very small and needs to be lower in //overhead if(caret2.getPosition().x - 16 > getPosX()){ caretIndex--; setCaret(caretPosition.at(caretIndex), getPosY(), getHeight()); } } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (caretIndex < caretPosition.size() -1)){ caretIndex++; setCaret(caretPosition.at(caretIndex), getPosY(), getHeight()); } } //this is not key pressed event at all this is textEntered its entirely different if (e.type == sf::Event::TextEntered){ if (e.text.unicode < 128 && getSelected()){ //if the same key as the last one was pressed not too recently if(lastKey == e.text.unicode ){ //if a second has passed if(getTicks(0.7)){ //if the caret is next to the front most text object append text to the front of the vector else //insert the character to the point where the caret is colliding if(text.size() == 0 || caretX > (text.back().getPosition().x + text.back().getLocalBounds().width) - 10){ addChar(e.text.unicode); }else{ insertChar( textIndex(), e.text.unicode); } } //if backspace has been hit }else if( 8 == e.text.unicode){ //if the caret is next to the front most text object remove the front most object with remove char //else remove the character at the point where the caret is colliding if( text.size() == 0 || caretX > (text.back().getPosition().x + text.back().getLocalBounds().width) - 10){ removeChar(); }else{ if(caretIndex != 0) removeChar(textIndex()); } //if the caret is next to the front most text object append text to the front of the vector else //insert the character to the point where the caret is colliding }else if(text.size() == 0 || caretX > (text.back().getPosition().x + text.back().getLocalBounds().width) - 10) { addChar(e.text.unicode); lastKey = e.text.unicode; getTicks(0.03); }else{ insertChar(textIndex(), e.text.unicode); lastKey = e.text.unicode; getTicks(0.03); } } } }catch(...){ std::cout<< "mystery error in key listen function caught"; //seems to stabilise mystery eror 75% of the time resetPositions(); } }
void textIn::insertChar( int _i, int _c){ try{ //creates an iterator for text vector assigned from arg 1 auto it = text.begin(); //creates a text graphic assigns a font and colour and size and pushes it to back of text vector sf::Text temp; temp.setFont(font); temp.setColor(sf::Color::White); temp.setCharacterSize(getHeight()); //if char to insert is pesky spacebar character... if(_c == 32){ //...then replace it with an underscore, no need to draw that underscore just a space must behave like a character temp.setString("_"); }else{ temp.setString( static_cast<char>(_c)); } if(_i != 0){ //insert the text in the vector at index using the iterator text.insert(it + _i, temp); text.at(_i).setPosition((text.at(_i ).getPosition().x + text.at(_i ).getLocalBounds().width) , getPosY() - (getHeight()/5)); //iterates all text in front of inserted text to be drawn in front of the char before it (shimmies all the other letters along) for(int x = _i; x < text.size(); x++){ text.at(x).setPosition((text.at(x -1).getPosition().x + text.at(x -1).getLocalBounds().width) , getPosY() - (getHeight()/5)); } }else{ //insert the text in the vector at index using the iterator text.insert(it, temp); //sets position of first character to be at front of text box text.at(0).setPosition(getPosX() , getPosY() - - (getHeight()/5)); //iterates all text in front of inserted text to be drawn in front of the char before it (shimmies all the other letters along) for(int x = 1; x < text.size(); x++){ text.at(x).setPosition((text.at(x -1).getPosition().x + text.at(x -1).getLocalBounds().width) , getPosY() - - (getHeight()/5)); } } }catch( std::out_of_range){ std::cout << "range error in textIn"; } //reorganises all positions of text resetPositions(); //moves caret along one setCaret( text.at(caretIndex).getPosition().x + text.at(caretIndex).getLocalBounds().width, getPosY(), getHeight()); //stores the new position of the character at the very front caretPosition.push_back(text.at(text.size() -1).getPosition().x + text.at(text.size() -1).getLocalBounds().width); caretIndex++; }
//moves caret to first position in array void textIn::caretFront(){ setCaret(caretPosition.at(caretPosition.size()), getPosY(), getHeight()); caretIndex = caretPosition.size(); }
//moves caret to last position in array //moves caret to first position in array void textIn::caretBack(){ setCaret(caretPosition.at(0), getPosY(), getHeight()); caretIndex = 0; }