2013-08-09 08:01:20 +08:00
|
|
|
// Aseprite UI Library
|
2015-06-24 01:00:00 +08:00
|
|
|
// Copyright (C) 2001-2013, 2015 David Capello
|
2010-09-28 06:18:17 +08:00
|
|
|
//
|
2014-03-30 07:08:05 +08:00
|
|
|
// This file is released under the terms of the MIT license.
|
|
|
|
// Read LICENSE.txt for more information.
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-07-13 04:29:16 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2009-07-13 04:29:16 +08:00
|
|
|
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/textbox.h"
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
#include "gfx/size.h"
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/intern.h"
|
|
|
|
#include "ui/message.h"
|
|
|
|
#include "ui/preferred_size_event.h"
|
|
|
|
#include "ui/system.h"
|
|
|
|
#include "ui/theme.h"
|
|
|
|
#include "ui/view.h"
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
namespace ui {
|
|
|
|
|
2014-04-21 06:53:27 +08:00
|
|
|
TextBox::TextBox(const std::string& text, int align)
|
2013-04-04 09:07:24 +08:00
|
|
|
: Widget(kTextBoxWidget)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2012-04-06 10:28:05 +08:00
|
|
|
setFocusStop(true);
|
|
|
|
setAlign(align);
|
|
|
|
setText(text);
|
|
|
|
initTheme();
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
bool TextBox::onProcessMessage(Message* msg)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2013-07-29 08:17:07 +08:00
|
|
|
switch (msg->type()) {
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kKeyDownMessage:
|
2012-04-06 10:28:05 +08:00
|
|
|
if (hasFocus()) {
|
|
|
|
View* view = View::getView(this);
|
2012-01-06 06:45:03 +08:00
|
|
|
if (view) {
|
|
|
|
gfx::Rect vp = view->getViewportBounds();
|
|
|
|
gfx::Point scroll = view->getViewScroll();
|
2014-03-22 06:45:35 +08:00
|
|
|
int textheight = getTextHeight();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
switch (static_cast<KeyMessage*>(msg)->scancode()) {
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyLeft:
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.x -= vp.w/2;
|
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyRight:
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.x += vp.w/2;
|
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyUp:
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.y -= vp.h/2;
|
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyDown:
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.y += vp.h/2;
|
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyPageUp:
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.y -= (vp.h-textheight);
|
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyPageDown:
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.y += (vp.h-textheight);
|
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyHome:
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.y = 0;
|
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
2013-07-29 08:17:07 +08:00
|
|
|
case kKeyEnd:
|
2013-10-26 23:50:55 +08:00
|
|
|
scroll.y = getBounds().h - vp.h;
|
2012-01-06 06:45:03 +08:00
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-02-03 07:13:52 +08:00
|
|
|
return Widget::onProcessMessage(msg);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseDownMessage: {
|
2012-04-06 10:28:05 +08:00
|
|
|
View* view = View::getView(this);
|
2007-09-19 07:57:02 +08:00
|
|
|
if (view) {
|
2012-04-06 10:28:05 +08:00
|
|
|
captureMouse();
|
2014-02-03 05:46:19 +08:00
|
|
|
m_oldPos = static_cast<MouseMessage*>(msg)->position();
|
2014-11-26 08:30:56 +08:00
|
|
|
set_mouse_cursor(kScrollCursor);
|
2012-01-06 06:45:03 +08:00
|
|
|
return true;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseMoveMessage: {
|
2012-04-06 10:28:05 +08:00
|
|
|
View* view = View::getView(this);
|
|
|
|
if (view && hasCapture()) {
|
2012-01-06 06:45:03 +08:00
|
|
|
gfx::Point scroll = view->getViewScroll();
|
2014-02-03 05:46:19 +08:00
|
|
|
gfx::Point newPos = static_cast<MouseMessage*>(msg)->position();
|
2011-02-21 05:35:21 +08:00
|
|
|
|
2014-02-03 05:46:19 +08:00
|
|
|
scroll += m_oldPos - newPos;
|
2012-01-06 06:45:03 +08:00
|
|
|
view->setViewScroll(scroll);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-08-26 09:49:19 +08:00
|
|
|
m_oldPos = newPos;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseUpMessage: {
|
2012-04-06 10:28:05 +08:00
|
|
|
View* view = View::getView(this);
|
|
|
|
if (view && hasCapture()) {
|
|
|
|
releaseMouse();
|
2014-11-26 08:30:56 +08:00
|
|
|
set_mouse_cursor(kArrowCursor);
|
2012-01-06 06:45:03 +08:00
|
|
|
return true;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseWheelMessage: {
|
2012-04-06 10:28:05 +08:00
|
|
|
View* view = View::getView(this);
|
2007-09-19 07:57:02 +08:00
|
|
|
if (view) {
|
2012-01-06 06:45:03 +08:00
|
|
|
gfx::Point scroll = view->getViewScroll();
|
2011-02-21 05:35:21 +08:00
|
|
|
|
2014-04-29 09:02:56 +08:00
|
|
|
scroll += static_cast<MouseMessage*>(msg)->wheelDelta() * getTextHeight()*3;
|
2011-02-21 05:35:21 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
view->setViewScroll(scroll);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
return Widget::onProcessMessage(msg);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2013-05-21 07:40:18 +08:00
|
|
|
void TextBox::onPaint(PaintEvent& ev)
|
|
|
|
{
|
|
|
|
getTheme()->paintTextBox(ev);
|
|
|
|
}
|
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
void TextBox::onPreferredSize(PreferredSizeEvent& ev)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2012-04-06 10:28:05 +08:00
|
|
|
int w = 0;
|
|
|
|
int h = 0;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
// TODO is it necessary?
|
|
|
|
//w = widget->border_width.l + widget->border_width.r;
|
|
|
|
//h = widget->border_width.t + widget->border_width.b;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-06-29 03:10:39 +08:00
|
|
|
drawTextBox(NULL, this, &w, &h, gfx::ColorNone, gfx::ColorNone);
|
2012-04-06 10:28:05 +08:00
|
|
|
|
2015-06-24 01:00:00 +08:00
|
|
|
if (this->getAlign() & WORDWRAP) {
|
2012-04-06 10:28:05 +08:00
|
|
|
View* view = View::getView(this);
|
|
|
|
int width, min = w;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
|
|
|
if (view) {
|
2011-02-21 05:35:21 +08:00
|
|
|
width = view->getViewportBounds().w;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-10-26 23:50:55 +08:00
|
|
|
width = getBounds().w;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
w = MAX(min, width);
|
2014-06-29 03:10:39 +08:00
|
|
|
drawTextBox(NULL, this, &w, &h, gfx::ColorNone, gfx::ColorNone);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
w = min;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
2012-04-06 10:28:05 +08:00
|
|
|
|
|
|
|
ev.setPreferredSize(gfx::Size(w, h));
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
2012-06-16 10:37:59 +08:00
|
|
|
|
|
|
|
void TextBox::onSetText()
|
|
|
|
{
|
|
|
|
View* view = View::getView(this);
|
|
|
|
if (view)
|
|
|
|
view->updateView();
|
|
|
|
|
|
|
|
Widget::onSetText();
|
|
|
|
}
|
2012-06-18 09:02:54 +08:00
|
|
|
|
|
|
|
} // namespace ui
|