2013-08-09 08:01:20 +08:00
|
|
|
// Aseprite UI Library
|
2013-01-27 23:13:13 +08:00
|
|
|
// Copyright (C) 2001-2013 David Capello
|
2010-09-28 06:18:17 +08:00
|
|
|
//
|
2013-08-09 08:01:20 +08:00
|
|
|
// This source file is distributed under MIT license,
|
|
|
|
// please 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-04-06 10:28:05 +08:00
|
|
|
#include <allegro/keyboard.h>
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
namespace ui {
|
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
TextBox::TextBox(const char* 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();
|
2012-04-06 10:28:05 +08:00
|
|
|
int textheight = jwidget_get_text_height(this);
|
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:
|
2012-04-06 10:28:05 +08:00
|
|
|
scroll.y = jrect_h(this->rc) - vp.h;
|
2012-01-06 06:45:03 +08:00
|
|
|
view->setViewScroll(scroll);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
2012-08-11 10:14:54 +08:00
|
|
|
jmouse_set_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::Rect vp = view->getViewportBounds();
|
|
|
|
gfx::Point scroll = view->getViewScroll();
|
2011-02-21 05:35:21 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
scroll.x += jmouse_x(1) - jmouse_x(0);
|
|
|
|
scroll.y += jmouse_y(1) - jmouse_y(0);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
view->setViewScroll(scroll);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
jmouse_control_infinite_scroll(vp);
|
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();
|
2012-08-11 10:14:54 +08:00
|
|
|
jmouse_set_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
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(this)*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
|
|
|
|
2013-03-31 00:38:24 +08:00
|
|
|
drawTextBox(NULL, this, &w, &h, ColorNone, ColorNone);
|
2012-04-06 10:28:05 +08:00
|
|
|
|
|
|
|
if (this->getAlign() & JI_WORDWRAP) {
|
|
|
|
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 {
|
2012-04-06 10:28:05 +08:00
|
|
|
width = jrect_w(this->rc);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2012-04-06 10:28:05 +08:00
|
|
|
w = MAX(min, width);
|
2013-03-31 00:38:24 +08:00
|
|
|
drawTextBox(NULL, this, &w, &h, ColorNone, 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
|