2011-04-10 02:51:35 +02:00
|
|
|
/* The MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "uitextedit.h"
|
2011-04-15 04:13:53 +02:00
|
|
|
#include "uitexteditskin.h"
|
2011-04-10 17:37:15 +02:00
|
|
|
#include "graphics/fonts.h"
|
2011-04-10 02:51:35 +02:00
|
|
|
|
2011-04-11 23:11:22 +02:00
|
|
|
UITextEdit::UITextEdit() :
|
2011-04-15 04:13:53 +02:00
|
|
|
UIElement(UI::TextEdit)
|
2011-04-10 02:51:35 +02:00
|
|
|
{
|
2011-04-15 04:13:53 +02:00
|
|
|
m_textArea.enableCursor();
|
2011-04-11 06:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UITextEdit::onInputEvent(const InputEvent& event)
|
|
|
|
{
|
|
|
|
if(event.type == EV_TEXT_ENTER) {
|
2011-04-15 04:13:53 +02:00
|
|
|
m_textArea.appendCharacter(event.keychar);
|
2011-04-11 06:08:56 +02:00
|
|
|
} else if(event.type == EV_KEY_DOWN) {
|
|
|
|
if(event.keycode == KC_DELETE)
|
2011-04-15 04:13:53 +02:00
|
|
|
m_textArea.removeCharacter(true);
|
2011-04-11 06:08:56 +02:00
|
|
|
else if(event.keycode == KC_BACK)
|
2011-04-15 04:13:53 +02:00
|
|
|
m_textArea.removeCharacter(false);
|
|
|
|
else if(event.keycode == KC_RIGHT)
|
|
|
|
m_textArea.moveCursor(true);
|
|
|
|
else if(event.keycode == KC_LEFT)
|
|
|
|
m_textArea.moveCursor(false);
|
2011-04-16 21:46:31 +02:00
|
|
|
else if(event.keycode == KC_HOME)
|
|
|
|
m_textArea.setCursorPos(0);
|
|
|
|
else if(event.keycode == KC_END)
|
|
|
|
m_textArea.setCursorPos(m_textArea.getText().length());
|
2011-04-16 00:07:23 +02:00
|
|
|
} else if(event.type == EV_MOUSE_LDOWN) {
|
2011-04-16 18:08:55 +02:00
|
|
|
|
2011-04-16 01:52:26 +02:00
|
|
|
} else if(event.type == EV_MOUSE_LUP && getRect().contains(event.mousePos)) {
|
|
|
|
m_textArea.setCursorPos(m_textArea.getTextPos(event.mousePos));
|
2011-04-11 06:08:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-15 04:13:53 +02:00
|
|
|
void UITextEdit::onLayoutRectChange(const Rect& rect)
|
2011-04-11 06:08:56 +02:00
|
|
|
{
|
2011-04-15 04:13:53 +02:00
|
|
|
UITextEditSkin *skin = static_cast<UITextEditSkin*>(getSkin());
|
|
|
|
Rect textRect = rect;
|
|
|
|
int margin = skin->getTextMargin();
|
|
|
|
textRect.setLeft(textRect.left()+margin);
|
|
|
|
textRect.setRight(textRect.right()-margin);
|
|
|
|
m_textArea.setScreenCoords(textRect);
|
2011-04-11 06:08:56 +02:00
|
|
|
}
|
|
|
|
|
2011-04-15 04:13:53 +02:00
|
|
|
void UITextEdit::onFocusChange()
|
2011-04-11 22:06:03 +02:00
|
|
|
{
|
2011-04-15 04:13:53 +02:00
|
|
|
m_textArea.setCursorVisible(isFocused());
|
2011-04-11 22:06:03 +02:00
|
|
|
}
|
|
|
|
|