aseprite/src/ui/entry.cpp

748 lines
16 KiB
C++
Raw Normal View History

// Aseprite UI Library
// Copyright (C) 2001-2014 David Capello
//
// 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
#ifdef HAVE_CONFIG_H
2009-07-13 04:29:16 +08:00
#include "config.h"
#endif
2009-07-13 04:29:16 +08:00
2013-03-31 00:11:49 +08:00
#include "ui/entry.h"
2007-09-19 07:57:02 +08:00
#include "base/bind.h"
#include "base/string.h"
#include "she/font.h"
2012-06-18 09:49:58 +08:00
#include "ui/clipboard.h"
#include "ui/manager.h"
#include "ui/menu.h"
2012-06-18 09:49:58 +08:00
#include "ui/message.h"
#include "ui/preferred_size_event.h"
#include "ui/system.h"
#include "ui/theme.h"
#include "ui/widget.h"
2007-09-19 07:57:02 +08:00
#include <cctype>
#include <cstdarg>
#include <cstdio>
2007-09-19 07:57:02 +08:00
namespace ui {
2010-12-09 01:28:13 +08:00
Entry::Entry(size_t maxsize, const char *format, ...)
: Widget(kEntryWidget)
, m_timer(500, this)
, m_maxsize(maxsize)
, m_caret(0)
, m_scroll(0)
, m_select(0)
, m_hidden(false)
, m_state(false)
, m_readonly(false)
, m_password(false)
, m_recent_focused(false)
, m_lock_selection(false)
2007-09-19 07:57:02 +08:00
{
char buf[4096];
// formatted string
2007-09-19 07:57:02 +08:00
if (format) {
va_list ap;
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
2007-09-19 07:57:02 +08:00
}
// empty string
2007-09-19 07:57:02 +08:00
else {
buf[0] = 0;
2007-09-19 07:57:02 +08:00
}
// TODO support for text alignment and multi-line
// widget->align = JI_LEFT | JI_MIDDLE;
2010-12-09 01:28:13 +08:00
setText(buf);
2007-09-19 07:57:02 +08:00
setFocusStop(true);
initTheme();
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
Entry::~Entry()
2007-09-19 07:57:02 +08:00
{
}
2010-12-09 01:28:13 +08:00
bool Entry::isReadOnly() const
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
return m_readonly;
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
bool Entry::isPassword() const
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
return m_password;
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
void Entry::setReadOnly(bool state)
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
m_readonly = state;
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
void Entry::setPassword(bool state)
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
m_password = state;
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
void Entry::showCaret()
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
m_hidden = false;
invalidate();
2010-12-09 01:28:13 +08:00
}
2007-09-19 07:57:02 +08:00
2010-12-09 01:28:13 +08:00
void Entry::hideCaret()
{
m_hidden = true;
invalidate();
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
void Entry::setCaretPos(int pos)
2007-09-19 07:57:02 +08:00
{
base::utf8_const_iterator utf8_begin = base::utf8_const_iterator(getText().begin());
int textlen = base::utf8_length(getText());
2007-09-19 07:57:02 +08:00
int x, c;
2010-12-09 01:28:13 +08:00
m_caret = pos;
2007-09-19 07:57:02 +08:00
// Backward scroll
2010-12-09 01:28:13 +08:00
if (m_caret < m_scroll)
m_scroll = m_caret;
2007-09-19 07:57:02 +08:00
// Forward scroll
2010-12-09 01:28:13 +08:00
m_scroll--;
2007-09-19 07:57:02 +08:00
do {
x = getBounds().x + this->border_width.l;
2010-12-09 01:28:13 +08:00
for (c=++m_scroll; ; c++) {
int ch = (c < textlen ? *(utf8_begin+c) : ' ');
x += getFont()->charWidth(ch);
2007-09-19 07:57:02 +08:00
if (x >= getBounds().x2()-this->border_width.r)
2007-09-19 07:57:02 +08:00
break;
}
2010-12-09 01:28:13 +08:00
} while (m_caret >= c);
2007-09-19 07:57:02 +08:00
m_timer.start();
2010-12-09 01:28:13 +08:00
m_state = true;
2007-09-19 07:57:02 +08:00
invalidate();
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
void Entry::selectText(int from, int to)
2007-09-19 07:57:02 +08:00
{
int end = base::utf8_length(getText());
2007-09-19 07:57:02 +08:00
2010-12-09 01:28:13 +08:00
m_select = from;
setCaretPos(from); // to move scroll
setCaretPos((to >= 0)? to: end+to+1);
2007-09-19 07:57:02 +08:00
invalidate();
2007-09-19 07:57:02 +08:00
}
void Entry::selectAllText()
{
selectText(0, -1);
}
2010-12-09 01:28:13 +08:00
void Entry::deselectText()
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
m_select = -1;
invalidate();
2007-09-19 07:57:02 +08:00
}
2013-03-30 03:20:32 +08:00
void Entry::setSuffix(const std::string& suffix)
{
m_suffix = suffix;
invalidate();
}
2010-12-09 01:28:13 +08:00
void Entry::getEntryThemeInfo(int* scroll, int* caret, int* state,
int* selbeg, int* selend)
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
if (scroll) *scroll = m_scroll;
if (caret) *caret = m_caret;
if (state) *state = !m_hidden && m_state;
if ((m_select >= 0) &&
(m_caret != m_select)) {
*selbeg = MIN(m_caret, m_select);
*selend = MAX(m_caret, m_select)-1;
2007-09-19 07:57:02 +08:00
}
else {
*selbeg = -1;
*selend = -1;
}
}
bool Entry::onProcessMessage(Message* msg)
2007-09-19 07:57:02 +08:00
{
switch (msg->type()) {
2007-09-19 07:57:02 +08:00
case kTimerMessage:
if (hasFocus() && static_cast<TimerMessage*>(msg)->timer() == &m_timer) {
// Blinking caret
m_state = m_state ? false: true;
invalidate();
2007-09-19 07:57:02 +08:00
}
break;
case kFocusEnterMessage:
m_timer.start();
2010-12-09 01:28:13 +08:00
m_state = true;
invalidate();
2007-09-19 07:57:02 +08:00
if (m_lock_selection) {
m_lock_selection = false;
}
else {
selectAllText();
m_recent_focused = true;
}
2007-09-19 07:57:02 +08:00
break;
case kFocusLeaveMessage:
invalidate();
2007-09-19 07:57:02 +08:00
m_timer.stop();
if (!m_lock_selection)
deselectText();
2010-12-09 01:28:13 +08:00
m_recent_focused = false;
2007-09-19 07:57:02 +08:00
break;
case kKeyDownMessage:
if (hasFocus() && !isReadOnly()) {
// Command to execute
EntryCmd cmd = EntryCmd::NoOp;
KeyMessage* keymsg = static_cast<KeyMessage*>(msg);
KeyScancode scancode = keymsg->scancode();
switch (scancode) {
case kKeyLeft:
if (msg->ctrlPressed())
cmd = EntryCmd::BackwardWord;
else
cmd = EntryCmd::BackwardChar;
break;
case kKeyRight:
if (msg->ctrlPressed())
cmd = EntryCmd::ForwardWord;
else
cmd = EntryCmd::ForwardChar;
break;
case kKeyHome:
cmd = EntryCmd::BeginningOfLine;
break;
case kKeyEnd:
cmd = EntryCmd::EndOfLine;
break;
case kKeyDel:
if (msg->shiftPressed())
cmd = EntryCmd::Cut;
else
cmd = EntryCmd::DeleteForward;
break;
case kKeyInsert:
if (msg->shiftPressed())
cmd = EntryCmd::Paste;
else if (msg->ctrlPressed())
cmd = EntryCmd::Copy;
break;
case kKeyBackspace:
cmd = EntryCmd::DeleteBackward;
break;
default:
// Map common Windows shortcuts for Cut/Copy/Paste
#if defined __APPLE__
if (msg->onlyCmdPressed())
#else
if (msg->onlyCtrlPressed())
#endif
{
switch (scancode) {
case kKeyX: cmd = EntryCmd::Cut; break;
case kKeyC: cmd = EntryCmd::Copy; break;
case kKeyV: cmd = EntryCmd::Paste; break;
}
}
else if (getManager()->isFocusMovementKey(msg)) {
break;
}
else if (keymsg->unicodeChar() >= 32) {
// Ctrl and Alt must be unpressed to insert a character
// in the text-field.
if ((msg->keyModifiers() & (kKeyCtrlModifier | kKeyAltModifier)) == 0) {
cmd = EntryCmd::InsertChar;
}
}
break;
}
if (cmd == EntryCmd::NoOp)
break;
executeCmd(cmd, keymsg->unicodeChar(),
(msg->shiftPressed()) ? true: false);
return true;
2007-09-19 07:57:02 +08:00
}
break;
case kMouseDownMessage:
captureMouse();
2007-09-19 07:57:02 +08:00
case kMouseMoveMessage:
if (hasCapture()) {
gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();
base::utf8_const_iterator utf8_begin = base::utf8_const_iterator(getText().begin());
base::utf8_const_iterator utf8_end = base::utf8_const_iterator(getText().end());
int textlen = base::utf8_length(getText());
int c, x;
bool move = true;
bool is_dirty = false;
// Backward scroll
if (mousePos.x < getBounds().x) {
if (m_scroll > 0) {
m_caret = --m_scroll;
move = false;
is_dirty = true;
invalidate();
}
}
// Forward scroll
else if (mousePos.x >= getBounds().x2()) {
if (m_scroll < textlen - getAvailableTextLength()) {
m_scroll++;
x = getBounds().x + this->border_width.l;
for (c=m_scroll; utf8_begin != utf8_end; ++c) {
int ch = (c < textlen ? *(utf8_begin+c) : ' ');
x += getFont()->charWidth(ch);
if (x > getBounds().x2()-this->border_width.r) {
c--;
break;
}
}
m_caret = c;
move = false;
is_dirty = true;
invalidate();
}
}
c = getCaretFromMouse(static_cast<MouseMessage*>(msg));
if (static_cast<MouseMessage*>(msg)->left() ||
(move && !isPosInSelection(c))) {
// Move caret
if (move) {
if (m_caret != c) {
m_caret = c;
is_dirty = true;
invalidate();
}
}
// Move selection
if (m_recent_focused) {
m_recent_focused = false;
m_select = m_caret;
}
else if (msg->type() == kMouseDownMessage)
m_select = m_caret;
}
// Show the caret
if (is_dirty) {
m_timer.start();
m_state = true;
}
return true;
2007-09-19 07:57:02 +08:00
}
break;
case kMouseUpMessage:
if (hasCapture()) {
releaseMouse();
MouseMessage* mouseMsg = static_cast<MouseMessage*>(msg);
if (mouseMsg->right()) {
// This flag is disabled in kFocusEnterMessage message handler.
m_lock_selection = true;
showEditPopupMenu(mouseMsg->position());
requestFocus();
}
}
return true;
2007-09-19 07:57:02 +08:00
case kDoubleClickMessage:
2010-12-09 01:28:13 +08:00
forwardWord();
m_select = m_caret;
backwardWord();
invalidate();
return true;
2007-09-19 07:57:02 +08:00
case kMouseEnterMessage:
case kMouseLeaveMessage:
// TODO theme stuff
if (isEnabled())
invalidate();
2007-09-19 07:57:02 +08:00
break;
}
2010-12-09 01:28:13 +08:00
return Widget::onProcessMessage(msg);
2007-09-19 07:57:02 +08:00
}
2010-12-09 01:28:13 +08:00
void Entry::onPreferredSize(PreferredSizeEvent& ev)
2007-09-19 07:57:02 +08:00
{
2010-12-09 01:28:13 +08:00
int w =
+ border_width.l
+ getFont()->charWidth('w') * MIN(m_maxsize, 6)
+ 2*guiscale()
+ border_width.r;
w = MIN(w, ui::display_w()/2);
2007-09-19 07:57:02 +08:00
int h =
2010-12-09 01:28:13 +08:00
+ border_width.t
+ getFont()->height()
2010-12-09 01:28:13 +08:00
+ border_width.b;
2007-09-19 07:57:02 +08:00
2010-12-09 01:28:13 +08:00
ev.setPreferredSize(w, h);
}
void Entry::onPaint(PaintEvent& ev)
{
getTheme()->paintEntry(ev);
}
void Entry::onSetText()
{
Widget::onSetText();
if (m_caret >= 0 && (size_t)m_caret > getTextLength())
m_caret = (int)getTextLength();
}
2010-12-09 01:28:13 +08:00
void Entry::onEntryChange()
{
EntryChange();
2007-09-19 07:57:02 +08:00
}
int Entry::getCaretFromMouse(MouseMessage* mousemsg)
2007-09-19 07:57:02 +08:00
{
base::utf8_const_iterator utf8_begin = base::utf8_const_iterator(getText().begin());
base::utf8_const_iterator utf8_end = base::utf8_const_iterator(getText().end());
2010-12-09 01:28:13 +08:00
int c, x, w, mx, caret = m_caret;
int textlen = base::utf8_length(getText());
2007-09-19 07:57:02 +08:00
mx = mousemsg->position().x;
mx = MID(getBounds().x+this->border_width.l,
mx,
getBounds().x2()-this->border_width.r-1);
2007-09-19 07:57:02 +08:00
x = getBounds().x + this->border_width.l;
base::utf8_const_iterator utf8_it =
(m_scroll < textlen ?
utf8_begin + m_scroll:
utf8_end);
for (c=m_scroll; utf8_it != utf8_end; ++c, ++utf8_it) {
w = getFont()->charWidth(*utf8_it);
if (x+w >= getBounds().x2()-this->border_width.r)
2007-09-19 07:57:02 +08:00
break;
if ((mx >= x) && (mx < x+w)) {
2010-12-09 01:28:13 +08:00
caret = c;
2007-09-19 07:57:02 +08:00
break;
}
x += w;
}
if (utf8_it == utf8_end) {
2007-09-19 07:57:02 +08:00
if ((mx >= x) &&
(mx <= getBounds().x2()-this->border_width.r-1)) {
2010-12-09 01:28:13 +08:00
caret = c;
}
}
2007-09-19 07:57:02 +08:00
2010-12-09 01:28:13 +08:00
return caret;
2007-09-19 07:57:02 +08:00
}
void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
{
std::wstring text = base::from_utf8(getText());
int c, selbeg, selend;
2010-12-09 01:28:13 +08:00
getEntryThemeInfo(NULL, NULL, NULL, &selbeg, &selend);
switch (cmd) {
case EntryCmd::NoOp:
break;
case EntryCmd::InsertChar:
// delete the entire selection
if (selbeg >= 0) {
text.erase(selbeg, selend-selbeg+1);
m_caret = selbeg;
}
// put the character
if (text.size() < m_maxsize) {
ASSERT((size_t)m_caret <= text.size());
text.insert(m_caret++, 1, unicodeChar);
}
2010-12-09 01:28:13 +08:00
m_select = -1;
break;
case EntryCmd::BackwardChar:
case EntryCmd::BackwardWord:
// selection
if (shift_pressed) {
if (m_select < 0)
m_select = m_caret;
}
else
m_select = -1;
// backward word
if (cmd == EntryCmd::BackwardWord) {
backwardWord();
}
// backward char
2010-12-09 01:28:13 +08:00
else if (m_caret > 0) {
m_caret--;
}
break;
case EntryCmd::ForwardChar:
case EntryCmd::ForwardWord:
// selection
if (shift_pressed) {
if (m_select < 0)
m_select = m_caret;
}
else
m_select = -1;
// forward word
if (cmd == EntryCmd::ForwardWord) {
forwardWord();
}
// forward char
2010-12-09 01:28:13 +08:00
else if (m_caret < (int)text.size()) {
m_caret++;
}
break;
case EntryCmd::BeginningOfLine:
// selection
if (shift_pressed) {
if (m_select < 0)
m_select = m_caret;
}
else
m_select = -1;
2010-12-09 01:28:13 +08:00
m_caret = 0;
break;
case EntryCmd::EndOfLine:
// selection
if (shift_pressed) {
if (m_select < 0)
m_select = m_caret;
}
else
m_select = -1;
2010-12-09 01:28:13 +08:00
m_caret = text.size();
break;
case EntryCmd::DeleteForward:
case EntryCmd::Cut:
// delete the entire selection
if (selbeg >= 0) {
// *cut* text!
if (cmd == EntryCmd::Cut) {
std::wstring selected = text.substr(selbeg, selend - selbeg + 1);
clipboard::set_text(base::to_utf8(selected).c_str());
}
// remove text
text.erase(selbeg, selend-selbeg+1);
m_caret = selbeg;
}
// delete the next character
else {
if (m_caret < (int)text.size())
text.erase(m_caret, 1);
}
2010-12-09 01:28:13 +08:00
m_select = -1;
break;
case EntryCmd::Paste: {
const char* clipboard_str;
if ((clipboard_str = clipboard::get_text())) {
std::string clipboard(clipboard_str);
// delete the entire selection
if (selbeg >= 0) {
text.erase(selbeg, selend-selbeg+1);
m_caret = selbeg;
m_select = -1;
}
// paste text
for (c=0; c<base::utf8_length(clipboard); c++) {
if (text.size() < m_maxsize)
text.insert(m_caret+c, 1,
*(base::utf8_const_iterator(clipboard.begin())+c));
else
break;
}
setCaretPos(m_caret+c);
}
break;
}
case EntryCmd::Copy:
if (selbeg >= 0) {
std::wstring selected = text.substr(selbeg, selend - selbeg + 1);
clipboard::set_text(base::to_utf8(selected).c_str());
}
break;
case EntryCmd::DeleteBackward:
// delete the entire selection
if (selbeg >= 0) {
text.erase(selbeg, selend-selbeg+1);
m_caret = selbeg;
}
// delete the previous character
else {
if (m_caret > 0)
text.erase(--m_caret, 1);
}
2010-12-09 01:28:13 +08:00
m_select = -1;
break;
}
std::string newText = base::to_utf8(text);
if (newText != getText()) {
setText(newText.c_str());
2010-12-09 01:28:13 +08:00
onEntryChange();
}
2010-12-09 01:28:13 +08:00
setCaretPos(m_caret);
invalidate();
}
#define IS_WORD_CHAR(ch) \
(!((!ch) || (std::isspace(ch)) || \
((ch) == '/') || ((ch) == '\\')))
2007-09-19 07:57:02 +08:00
2010-12-09 01:28:13 +08:00
void Entry::forwardWord()
2007-09-19 07:57:02 +08:00
{
base::utf8_const_iterator utf8_begin = base::utf8_const_iterator(getText().begin());
int textlen = base::utf8_length(getText());
2007-09-19 07:57:02 +08:00
int ch;
for (; m_caret < textlen; m_caret++) {
ch = *(utf8_begin + m_caret);
if (IS_WORD_CHAR(ch))
2007-09-19 07:57:02 +08:00
break;
}
for (; m_caret < textlen; m_caret++) {
ch = *(utf8_begin + m_caret);
if (!IS_WORD_CHAR(ch)) {
++m_caret;
2007-09-19 07:57:02 +08:00
break;
}
}
}
2010-12-09 01:28:13 +08:00
void Entry::backwardWord()
2007-09-19 07:57:02 +08:00
{
base::utf8_const_iterator utf8_begin = base::utf8_const_iterator(getText().begin());
2007-09-19 07:57:02 +08:00
int ch;
for (--m_caret; m_caret >= 0; --m_caret) {
ch = *(utf8_begin + m_caret);
if (IS_WORD_CHAR(ch))
2007-09-19 07:57:02 +08:00
break;
}
for (; m_caret >= 0; --m_caret) {
ch = *(utf8_begin + m_caret);
if (!IS_WORD_CHAR(ch)) {
++m_caret;
2007-09-19 07:57:02 +08:00
break;
}
}
2010-12-09 01:28:13 +08:00
if (m_caret < 0)
m_caret = 0;
2007-09-19 07:57:02 +08:00
}
int Entry::getAvailableTextLength()
{
return getClientChildrenBounds().w / getFont()->charWidth('w');
}
bool Entry::isPosInSelection(int pos)
{
return (pos >= MIN(m_caret, m_select) && pos <= MAX(m_caret, m_select));
}
void Entry::showEditPopupMenu(const gfx::Point& pt)
{
Menu menu;
MenuItem cut("Cut");
MenuItem copy("Copy");
MenuItem paste("Paste");
menu.addChild(&cut);
menu.addChild(&copy);
menu.addChild(&paste);
cut.Click.connect(Bind(&Entry::executeCmd, this, EntryCmd::Cut, 0, false));
copy.Click.connect(Bind(&Entry::executeCmd, this, EntryCmd::Copy, 0, false));
paste.Click.connect(Bind(&Entry::executeCmd, this, EntryCmd::Paste, 0, false));
if (isReadOnly()) {
cut.setEnabled(false);
paste.setEnabled(false);
}
menu.showPopup(pt);
}
} // namespace ui