Example #1
0
void Editbox::eraseSelectedText(bool modify_text)
{
    if (getSelectionLength() != 0)
    {
        // setup new caret position and remove selection highlight.
        setCaretIndex(d_selectionStart);
        clearSelection();

        // erase the selected characters (if required)
        if (modify_text)
        {
            String newText = getText();
            UndoHandler::UndoAction undo;
            undo.d_type = UndoHandler::UAT_DELETE;
            undo.d_startIdx = getSelectionStart();
            undo.d_text = newText.substr(getSelectionStart(), getSelectionLength());
            d_undoHandler->addUndoHistory(undo);

            newText.erase(getSelectionStart(), getSelectionLength());
            setText(newText);

            // trigger notification that text has changed.
            WindowEventArgs args(this);
            onTextChanged(args);
        }

    }

}
Example #2
0
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);
            }
        }

    }

}
Example #3
0
	const std::string TextField::getSelectedText() const
	{
		if(getSelectionStart() == getSelectionEnd())
		{
			return std::string("");
		}
		else
		{
			return unicodeFunctions.subStr(getText(),
				getSelectionStart(),getSelectionLength());
		}
	}
Example #4
0
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;
}
Example #5
0
	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);
	}
Example #6
0
	void TextField::paintComponent( const PaintEvent &paintEvent )
	{
		int caretLoc = getCaretLocation();
		int textLoc = getTextOffset();

		Rectangle sideclip = getInnerRectangle();
		sideclip = Rectangle(sideclip.getX() + getLeftPadding() ,
			sideclip.getY() + 2,sideclip.getSize().getWidth() - getLeftPadding()
			- getRightPadding() + 1, sideclip.getHeight() - 4);

		

		if(isReadOnly())
		{
			paintEvent.graphics()->drawFilledRectangle(
				getSizeRectangle(),frameColor);
		}
		else
		{
			paintEvent.graphics()->drawFilledRectangle(
				getSizeRectangle(),getBackColor());
		}
		

		paintEvent.graphics()->pushClippingRect(sideclip);

		if(getSelectionStart() != getSelectionEnd() && (isFocused() || !isHidingSelection()) )
		{
			Rectangle selRect = Rectangle(
				getSelectionLocation(),
				(getInnerHeight() / 2) - 
				(getFont()->getLineHeight() / 2),
				getSelectionWidth(),
				getFont()->getLineHeight());

			paintEvent.graphics()->drawFilledRectangle(
				selRect,getSelectionBackColor());
		}


			paintEvent.graphics()->drawText(Point(textLoc, +
				((getInnerSize().getHeight() - getFont()->getLineHeight()) / 2)),getText().c_str(),
				getFontColor(),getFont());
		

			if(isFocused())
			{
				if(isBlinking())
					paintEvent.graphics()->drawLine(Point(caretLoc + 1,
					((getInnerSize().getHeight() / 2) + (getFont()->getLineHeight() / 2))),
					Point(caretLoc + 1, ((getInnerSize().getHeight() / 2) - 
					(getFont()->getLineHeight() / 2))),
					Color(0,0,0));
			}


		paintEvent.graphics()->popClippingRect();

		
	}
Example #7
0
void Text::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    if (!doc)
        return;

    painter->setClipRect(boundingRect());

    // draw selection
    QAbstractTextDocumentLayout::PaintContext ctx;
    QAbstractTextDocumentLayout::Selection sel;

    if (hasSelection())
    {
        sel.cursor = QTextCursor(doc);
        sel.cursor.setPosition(getSelectionStart());
        sel.cursor.setPosition(getSelectionEnd(), QTextCursor::KeepAnchor);
    }

    const QColor selectionColor = QColor::fromRgbF(0.23, 0.68, 0.91);
    sel.format.setBackground(selectionColor.lighter(selectionHasFocus ? 100 : 160));
    sel.format.setForeground(selectionHasFocus ? Qt::white : Qt::black);
    ctx.selections.append(sel);
    ctx.palette.setColor(QPalette::Text, color);

    // draw text
    doc->documentLayout()->draw(painter, ctx);
}
Example #8
0
	void TextField::setSize( const Dimension& size )
	{
		Widget::setSize(size);

		scrollToCaret(false,false);
		relocateCaret();
		setSelection(getSelectionStart(),getSelectionEnd());
	}
Example #9
0
bool Text::isOverSelection(QPointF scenePos) const
{
    int cur = cursorFromPos(scenePos);
    if (getSelectionStart() < cur && getSelectionEnd() >= cur)
        return true;

    return false;
}
Example #10
0
File: text.cpp Project: iphydf/qTox
void Text::selectionMouseMove(QPointF scenePos)
{
    if (!doc)
        return;

    int cur = cursorFromPos(scenePos);
    if (cur >= 0) {
        selectionEnd = cur;
        selectedText = extractSanitizedText(getSelectionStart(), getSelectionEnd());
    }

    update();
}
Example #11
0
void UIHexEditorWnd::keyPressEdit(QKeyEvent *event, u64 posAddr)
{
   int key = int(event->text()[0].toLatin1());
   if (textEdit)
   {
      if ((key>='0' && key<='9') || (key>='a' && key <= 'z') || (key>='A' && key <= 'Z'))
      {
         if (getSelectionStart() != getSelectionEnd())
         {
            posAddr = getSelectionStart();
            clear(posAddr, getSelectionEnd() - posAddr);
            setCursorPos(2*posAddr);
            resetSelection(2*posAddr);
         }

         // Patch byte
         overwrite((u32)cursorAddr >> 1, (u8)key);
         setCursorPos(cursorAddr + 2);
         resetSelection(cursorAddr);
      }
   }
   else
   {
      if ((key>='0' && key<='9') || (key>='a' && key <= 'f') || (key>='A' && key <= 'F'))
Example #12
0
File: text.cpp Project: iphydf/qTox
void Text::selectionDoubleClick(QPointF scenePos)
{
    if (!doc)
        return;

    int cur = cursorFromPos(scenePos);

    if (cur >= 0) {
        QTextCursor cursor(doc);
        cursor.setPosition(cur);
        cursor.select(QTextCursor::WordUnderCursor);

        selectionAnchor = cursor.selectionStart();
        selectionEnd = cursor.selectionEnd();

        selectedText = extractSanitizedText(getSelectionStart(), getSelectionEnd());
    }

    update();
}
Example #13
0
File: text.cpp Project: iphydf/qTox
void Text::selectionTripleClick(QPointF scenePos)
{
    if (!doc)
        return;

    int cur = cursorFromPos(scenePos);

    if (cur >= 0) {
        QTextCursor cursor(doc);
        cursor.setPosition(cur);
        cursor.select(QTextCursor::BlockUnderCursor);

        selectionAnchor = cursor.selectionStart();
        selectionEnd = cursor.selectionEnd();

        if (cursor.block().isValid() && cursor.block().blockNumber() != 0)
            selectionAnchor++;

        selectedText = extractSanitizedText(getSelectionStart(), getSelectionEnd());
    }

    update();
}
Example #14
0
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);
            }
        }

    }

}
Example #15
0
            resetSelection(2*posAddr);
         }

         // Patch byte
         overwrite((u32)cursorAddr >> 1, (u8)key);
         setCursorPos(cursorAddr + 2);
         resetSelection(cursorAddr);
      }
   }
   else
   {
      if ((key>='0' && key<='9') || (key>='a' && key <= 'f') || (key>='A' && key <= 'F'))
      {
         if (getSelectionStart() != getSelectionEnd())
         {
            posAddr = getSelectionStart();
            clear(posAddr, getSelectionEnd() - posAddr);
            setCursorPos(2*posAddr);
            resetSelection(2*posAddr);
         }

         // Patch byte
         overwrite((s64)cursorAddr, (char)key);
         setCursorPos(cursorAddr + 1);
         resetSelection(cursorAddr);
      }
   }
   if (event->key()==Qt::Key_Tab)
   {
      textEdit ^= true;
   }
Example #16
0
	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;
		}

	}
Example #17
0
	void TextField::setSelection( int start, int end )
	{
		if(!isSelectable())
		{
			if(getSelectionStart() != getSelectionEnd())
			{
				start = 0;
				end = 0;
			}
			else
			{
				return;
			}
		}
		if(start == end)
		{
			selStart = 0;
			selEnd = 0;
			selWidth = 0;
		}

		if(start == -1 && end == -1)
		{
			start = 0;
			end = getTextLength();
		}
		else if( end == -1)
		{
			end = getTextLength();
		}
		if( start > end)
		{
			int temp = start;
			start = end;
			end = temp;
		}

		if(start < 0)
		{
			start = 0;
		}
		if( end > getTextLength() )
		{
			end = getTextLength();
		}

		for(std::vector<TextFieldListener*>::iterator it = tFieldListeners.begin();
			it != tFieldListeners.end(); ++it)
		{
			(*it)->selectionChanged(this,start,end);
		}

		selStart = start;
		selEnd = end;
		selLength = end - start;
		selPos = getFont()->getTextWidth(unicodeFunctions.subStr(getText(),
			0,start)) + getTextOffset();

		selWidth = getFont()->getTextWidth(unicodeFunctions.subStr(getText(),
			start,selLength));

	}
Example #18
0
void Editbox::onCharacter(TextEventArgs& e)
{
    // NB: We are not calling the base class handler here because it propagates
    // inputs back up the window hierarchy, whereas, as a consumer of input
    // events, we want such propagation 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.d_character))
    {
        // 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 = getSelectionStart();
            undo.d_text = e.d_character;

            tmp.insert(getSelectionStart(), 1, e.d_character);

            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;
                d_undoHandler->addUndoHistory(undo);
                if (getSelectionLength() > 0)
                    d_undoHandler->addUndoHistory(undoSelection);
            }
        }
        else
        {
            // Trigger text box full event
            WindowEventArgs args(this);
            onEditboxFullEvent(args);
        }

    }

    // event was (possibly) not handled
}