mirror of
https://github.com/aseprite/aseprite.git
synced 2025-04-01 10:21:04 +00:00
Add ui::Entry::Range to simplify code
This commit is contained in:
parent
fa187b35a8
commit
f491a2a506
2
laf
2
laf
@ -1 +1 @@
|
|||||||
Subproject commit 755ad130b9c5d55354fd5d8c4ce74bf24c012b09
|
Subproject commit 207de6662911e9471c01681fb16912b756646f07
|
@ -1015,7 +1015,7 @@ public:
|
|||||||
, m_y(pos.y)
|
, m_y(pos.y)
|
||||||
, m_h(h)
|
, m_h(h)
|
||||||
{
|
{
|
||||||
m_widget->getEntryThemeInfo(&m_index, &m_caret, &m_state, &m_selbeg, &m_selend);
|
m_widget->getEntryThemeInfo(&m_index, &m_caret, &m_state, &m_range);
|
||||||
}
|
}
|
||||||
|
|
||||||
int index() const { return m_index; }
|
int index() const { return m_index; }
|
||||||
@ -1033,8 +1033,8 @@ public:
|
|||||||
fg = colors.text();
|
fg = colors.text();
|
||||||
|
|
||||||
// Selected
|
// Selected
|
||||||
if ((m_index >= m_selbeg) &&
|
if ((m_index >= m_range.from) &&
|
||||||
(m_index <= m_selend)) {
|
(m_index < m_range.to)) {
|
||||||
if (m_widget->hasFocus())
|
if (m_widget->hasFocus())
|
||||||
bg = colors.selected();
|
bg = colors.selected();
|
||||||
else
|
else
|
||||||
@ -1093,8 +1093,7 @@ private:
|
|||||||
int m_index;
|
int m_index;
|
||||||
int m_caret;
|
int m_caret;
|
||||||
int m_state;
|
int m_state;
|
||||||
int m_selbeg;
|
Entry::Range m_range;
|
||||||
int m_selend;
|
|
||||||
gfx::Rect m_textBounds;
|
gfx::Rect m_textBounds;
|
||||||
bool m_caretDrawn;
|
bool m_caretDrawn;
|
||||||
gfx::Color m_bg;
|
gfx::Color m_bg;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
Copyright (C) 2018-2020 Igara Studio S.A.
|
Copyright (c) 2018-2020 Igara Studio S.A.
|
||||||
Copyright (c) 2001-2018 David Capello
|
Copyright (c) 2001-2018 David Capello
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
@ -172,19 +172,28 @@ void Entry::deselectText()
|
|||||||
|
|
||||||
std::string Entry::selectedText() const
|
std::string Entry::selectedText() const
|
||||||
{
|
{
|
||||||
int selbeg, selend;
|
Range range = selectedRange();
|
||||||
getEntryThemeInfo(nullptr, nullptr, nullptr, &selbeg, &selend);
|
if (!range.isEmpty())
|
||||||
|
return text().substr(m_boxes[range.from].from,
|
||||||
if (selbeg >= 0 && selend >= 0) {
|
m_boxes[range.to-1].to - m_boxes[range.from].from);
|
||||||
ASSERT(selbeg < int(m_boxes.size()));
|
|
||||||
ASSERT(selend < int(m_boxes.size()));
|
|
||||||
return text().substr(m_boxes[selbeg].from,
|
|
||||||
m_boxes[selend].to - m_boxes[selbeg].from);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Entry::Range Entry::selectedRange() const
|
||||||
|
{
|
||||||
|
Range range;
|
||||||
|
if ((m_select >= 0) &&
|
||||||
|
(m_caret != m_select)) {
|
||||||
|
range.from = std::min(m_caret, m_select);
|
||||||
|
range.to = std::max(m_caret, m_select);
|
||||||
|
|
||||||
|
ASSERT(range.from >= 0 && range.from < int(m_boxes.size()));
|
||||||
|
ASSERT(range.to >= 0 && range.to <= int(m_boxes.size()));
|
||||||
|
}
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
void Entry::setSuffix(const std::string& suffix)
|
void Entry::setSuffix(const std::string& suffix)
|
||||||
{
|
{
|
||||||
if (m_suffix != suffix) {
|
if (m_suffix != suffix) {
|
||||||
@ -198,22 +207,12 @@ void Entry::setTranslateDeadKeys(bool state)
|
|||||||
m_translate_dead_keys = state;
|
m_translate_dead_keys = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entry::getEntryThemeInfo(int* scroll, int* caret, int* state,
|
void Entry::getEntryThemeInfo(int* scroll, int* caret, int* state, Range* range) const
|
||||||
int* selbeg, int* selend) const
|
|
||||||
{
|
{
|
||||||
if (scroll) *scroll = m_scroll;
|
if (scroll) *scroll = m_scroll;
|
||||||
if (caret) *caret = m_caret;
|
if (caret) *caret = m_caret;
|
||||||
if (state) *state = !m_hidden && m_state;
|
if (state) *state = !m_hidden && m_state;
|
||||||
|
if (range) *range = selectedRange();
|
||||||
if ((m_select >= 0) &&
|
|
||||||
(m_caret != m_select)) {
|
|
||||||
*selbeg = std::min(m_caret, m_select);
|
|
||||||
*selend = std::max(m_caret, m_select)-1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*selbeg = -1;
|
|
||||||
*selend = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gfx::Rect Entry::getEntryTextBounds() const
|
gfx::Rect Entry::getEntryTextBounds() const
|
||||||
@ -545,9 +544,7 @@ int Entry::getCaretFromMouse(MouseMessage* mousemsg)
|
|||||||
void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
|
void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
|
||||||
{
|
{
|
||||||
std::string text = this->text();
|
std::string text = this->text();
|
||||||
int selbeg, selend;
|
const Range range = selectedRange();
|
||||||
|
|
||||||
getEntryThemeInfo(nullptr, nullptr, nullptr, &selbeg, &selend);
|
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
|
|
||||||
@ -556,11 +553,8 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
|
|||||||
|
|
||||||
case EntryCmd::InsertChar:
|
case EntryCmd::InsertChar:
|
||||||
// delete the entire selection
|
// delete the entire selection
|
||||||
if (selbeg >= 0) {
|
if (!range.isEmpty()) {
|
||||||
text.erase(m_boxes[selbeg].from,
|
deleteRange(range, text);
|
||||||
m_boxes[selend].to - m_boxes[selbeg].from);
|
|
||||||
|
|
||||||
m_caret = selbeg;
|
|
||||||
|
|
||||||
// We set the caret to the beginning of the erased selection,
|
// We set the caret to the beginning of the erased selection,
|
||||||
// needed to show the first inserted character in case
|
// needed to show the first inserted character in case
|
||||||
@ -657,16 +651,13 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
|
|||||||
case EntryCmd::DeleteForward:
|
case EntryCmd::DeleteForward:
|
||||||
case EntryCmd::Cut:
|
case EntryCmd::Cut:
|
||||||
// delete the entire selection
|
// delete the entire selection
|
||||||
if (selbeg >= 0) {
|
if (!range.isEmpty()) {
|
||||||
// *cut* text!
|
// *cut* text!
|
||||||
if (cmd == EntryCmd::Cut)
|
if (cmd == EntryCmd::Cut)
|
||||||
set_clipboard_text(selectedText());
|
set_clipboard_text(selectedText());
|
||||||
|
|
||||||
// remove text
|
// remove text
|
||||||
text.erase(m_boxes[selbeg].from,
|
deleteRange(range, text);
|
||||||
m_boxes[selend].to - m_boxes[selbeg].from);
|
|
||||||
|
|
||||||
m_caret = selbeg;
|
|
||||||
}
|
}
|
||||||
// delete the next character
|
// delete the next character
|
||||||
else {
|
else {
|
||||||
@ -682,11 +673,8 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
|
|||||||
std::string clipboard;
|
std::string clipboard;
|
||||||
if (get_clipboard_text(clipboard)) {
|
if (get_clipboard_text(clipboard)) {
|
||||||
// delete the entire selection
|
// delete the entire selection
|
||||||
if (selbeg >= 0) {
|
if (!range.isEmpty()) {
|
||||||
text.erase(m_boxes[selbeg].from,
|
deleteRange(range, text);
|
||||||
m_boxes[selend].to - m_boxes[selbeg].from);
|
|
||||||
|
|
||||||
m_caret = selbeg;
|
|
||||||
m_select = -1;
|
m_select = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -711,17 +699,14 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
|
|||||||
}
|
}
|
||||||
|
|
||||||
case EntryCmd::Copy:
|
case EntryCmd::Copy:
|
||||||
if (selbeg >= 0)
|
if (!range.isEmpty())
|
||||||
set_clipboard_text(selectedText());
|
set_clipboard_text(selectedText());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EntryCmd::DeleteBackward:
|
case EntryCmd::DeleteBackward:
|
||||||
// delete the entire selection
|
// delete the entire selection
|
||||||
if (selbeg >= 0) {
|
if (!range.isEmpty()) {
|
||||||
text.erase(m_boxes[selbeg].from,
|
deleteRange(range, text);
|
||||||
m_boxes[selend].to - m_boxes[selbeg].from);
|
|
||||||
|
|
||||||
m_caret = selbeg;
|
|
||||||
}
|
}
|
||||||
// delete the previous character
|
// delete the previous character
|
||||||
else {
|
else {
|
||||||
@ -898,4 +883,11 @@ bool Entry::shouldStartTimer(bool hasFocus)
|
|||||||
return (!m_hidden && hasFocus && isEnabled());
|
return (!m_hidden && hasFocus && isEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entry::deleteRange(const Range& range, std::string& text)
|
||||||
|
{
|
||||||
|
text.erase(m_boxes[range.from].from,
|
||||||
|
m_boxes[range.to-1].to - m_boxes[range.from].from);
|
||||||
|
m_caret = range.from;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ui
|
} // namespace ui
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Aseprite UI Library
|
// Aseprite UI Library
|
||||||
// Copyright (C) 2018 Igara Studio S.A.
|
// Copyright (C) 2018-2020 Igara Studio S.A.
|
||||||
// Copyright (C) 2001-2017 David Capello
|
// Copyright (C) 2001-2017 David Capello
|
||||||
//
|
//
|
||||||
// This file is released under the terms of the MIT license.
|
// This file is released under the terms of the MIT license.
|
||||||
@ -19,6 +19,12 @@ namespace ui {
|
|||||||
|
|
||||||
class Entry : public Widget {
|
class Entry : public Widget {
|
||||||
public:
|
public:
|
||||||
|
struct Range {
|
||||||
|
int from = -1, to = -1;
|
||||||
|
bool isEmpty() const { return from < 0; }
|
||||||
|
int size() const { return to - from; }
|
||||||
|
};
|
||||||
|
|
||||||
Entry(const int maxsize, const char *format, ...);
|
Entry(const int maxsize, const char *format, ...);
|
||||||
~Entry();
|
~Entry();
|
||||||
|
|
||||||
@ -35,10 +41,12 @@ namespace ui {
|
|||||||
|
|
||||||
void setCaretPos(int pos);
|
void setCaretPos(int pos);
|
||||||
void setCaretToEnd();
|
void setCaretToEnd();
|
||||||
|
|
||||||
void selectText(int from, int to);
|
void selectText(int from, int to);
|
||||||
void selectAllText();
|
void selectAllText();
|
||||||
void deselectText();
|
void deselectText();
|
||||||
std::string selectedText() const;
|
std::string selectedText() const;
|
||||||
|
Range selectedRange() const;
|
||||||
|
|
||||||
void setSuffix(const std::string& suffix);
|
void setSuffix(const std::string& suffix);
|
||||||
const std::string& getSuffix() { return m_suffix; }
|
const std::string& getSuffix() { return m_suffix; }
|
||||||
@ -46,8 +54,7 @@ namespace ui {
|
|||||||
void setTranslateDeadKeys(bool state);
|
void setTranslateDeadKeys(bool state);
|
||||||
|
|
||||||
// for themes
|
// for themes
|
||||||
void getEntryThemeInfo(int* scroll, int* caret, int* state,
|
void getEntryThemeInfo(int* scroll, int* caret, int* state, Range* range) const;
|
||||||
int* selbeg, int* selend) const;
|
|
||||||
gfx::Rect getEntryTextBounds() const;
|
gfx::Rect getEntryTextBounds() const;
|
||||||
|
|
||||||
static gfx::Size sizeHintWithText(Entry* entry,
|
static gfx::Size sizeHintWithText(Entry* entry,
|
||||||
@ -95,6 +102,7 @@ namespace ui {
|
|||||||
void showEditPopupMenu(const gfx::Point& pt);
|
void showEditPopupMenu(const gfx::Point& pt);
|
||||||
void recalcCharBoxes(const std::string& text);
|
void recalcCharBoxes(const std::string& text);
|
||||||
bool shouldStartTimer(const bool hasFocus);
|
bool shouldStartTimer(const bool hasFocus);
|
||||||
|
void deleteRange(const Range& range, std::string& text);
|
||||||
|
|
||||||
class CalcBoxesTextDelegate;
|
class CalcBoxesTextDelegate;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user