2014-08-31 16:08:00 +00:00
|
|
|
gui.widgets.LineEdit = {
|
|
|
|
new: func(parent, style, cfg)
|
|
|
|
{
|
|
|
|
var m = gui.Widget.new(gui.widgets.LineEdit);
|
|
|
|
m._cfg = Config.new(cfg);
|
|
|
|
m._focus_policy = m.StrongFocus;
|
|
|
|
m._setView( style.createWidget(parent, "line-edit", m._cfg) );
|
|
|
|
|
|
|
|
m.setLayoutMinimumSize([28, 16]);
|
|
|
|
m.setLayoutSizeHint([150, 28]);
|
|
|
|
|
|
|
|
m._text = "";
|
|
|
|
m._max_length = 32767;
|
|
|
|
m._cursor = 0;
|
|
|
|
m._selection_start = 0;
|
|
|
|
m._selection_end = 0;
|
|
|
|
|
2023-01-06 11:21:26 +00:00
|
|
|
m.context_menu = gui.Menu.new();
|
2023-01-08 14:35:46 +00:00
|
|
|
m.context_menu.createItem(text: "Copy", cb: func() { m.copy(); }, shortcut: "<Ctrl>+C");
|
|
|
|
m.context_menu.createItem(text: "Cut", cb: func() { m.cut(); }, shortcut: "<Ctrl>+X");
|
|
|
|
m.context_menu.createItem(text: "Paste", cb: func() { m.paste(); }, shortcut: "<Ctrl>+V");
|
|
|
|
m.context_menu.createItem(text: "Clear", cb: func() { m.clear(); }, shortcut: "<Ctrl>+D");
|
|
|
|
m.context_menu.createItem(text: "Select all", cb: func() { m.selectAll(); }, shortcut: "<Ctrl>+A");
|
|
|
|
m.context_menu.setCanvasItem(m);
|
2023-01-06 11:21:26 +00:00
|
|
|
|
2014-08-31 16:08:00 +00:00
|
|
|
return m;
|
|
|
|
},
|
2023-01-06 11:21:26 +00:00
|
|
|
showContextMenu: func(e) {
|
|
|
|
me.context_menu.show(e.screenX, e.screenY);
|
|
|
|
},
|
2014-09-01 17:31:25 +00:00
|
|
|
setText: func(text)
|
|
|
|
{
|
2022-06-30 10:39:41 +00:00
|
|
|
if (text == nil) {
|
|
|
|
me.clear();
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
2014-09-01 17:31:25 +00:00
|
|
|
me._text = utf8.substr(text, 0, me._max_length);
|
|
|
|
me._cursor = utf8.size(me._text);
|
2023-01-27 21:59:58 +00:00
|
|
|
me.clearSelection();
|
2014-09-01 17:31:25 +00:00
|
|
|
|
|
|
|
if( me._view != nil )
|
|
|
|
me._view.setText(me, me._text);
|
2023-01-27 21:59:58 +00:00
|
|
|
me._trigger("text-changed");
|
2014-11-23 22:35:26 +00:00
|
|
|
|
|
|
|
return me;
|
2014-09-01 17:31:25 +00:00
|
|
|
},
|
2022-06-30 10:39:41 +00:00
|
|
|
clear: func
|
|
|
|
{
|
|
|
|
me._text = "";
|
|
|
|
me._cursor = 0;
|
2023-01-27 21:59:58 +00:00
|
|
|
me.clearSelection();
|
2022-06-30 10:39:41 +00:00
|
|
|
|
|
|
|
if( me._view != nil )
|
|
|
|
me._view.setText(me, "");
|
2023-01-27 21:59:58 +00:00
|
|
|
me._trigger("text-changed");
|
|
|
|
me._onStateChange();
|
2022-06-30 10:39:41 +00:00
|
|
|
},
|
2014-09-01 17:31:25 +00:00
|
|
|
text: func()
|
|
|
|
{
|
|
|
|
return me._text;
|
|
|
|
},
|
2023-01-06 11:21:26 +00:00
|
|
|
selectedText: func() {
|
|
|
|
return utf8.substr(me._text, me._selection_start, me._selection_end);
|
|
|
|
},
|
2014-08-31 16:08:00 +00:00
|
|
|
setMaxLength: func(len)
|
|
|
|
{
|
|
|
|
me._max_length = len;
|
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
if (utf8.size(me._text) <= len) {
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
2014-08-31 16:08:00 +00:00
|
|
|
|
|
|
|
me._text = utf8.substr(me._text, 0, me._max_length);
|
2023-01-27 21:59:58 +00:00
|
|
|
if( me._view != nil )
|
|
|
|
me._view.setText(me, "");
|
|
|
|
me._trigger("text-changed");
|
2014-08-31 16:08:00 +00:00
|
|
|
me.moveCursor(me._cursor);
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
|
|
|
moveCursor: func(pos, mark = 0)
|
|
|
|
{
|
|
|
|
var len = utf8.size(me._text);
|
|
|
|
me._cursor = math.max(0, math.min(pos, len));
|
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
clearSelection: func {
|
|
|
|
me._selection_start = me._selection_end = 0;
|
|
|
|
me._onStateChange();
|
|
|
|
},
|
|
|
|
setSelection: func(start, end) {
|
|
|
|
me._selection_start = start;
|
|
|
|
me._selection_end = end;
|
|
|
|
me._onStateChange();
|
|
|
|
},
|
|
|
|
getSelection: func {
|
|
|
|
if (me._selection_start != me._selection_end) {
|
|
|
|
return [me._selection_start, me._selection_end];
|
|
|
|
} else {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_getNearestCursorPos: func(x) {
|
|
|
|
var crs = me._getNearestCursor(x);
|
|
|
|
return me._view._text.getCursorPos(crs[0], crs[1]);
|
|
|
|
},
|
|
|
|
_getNearestCursor: func(x) {
|
|
|
|
return me._view._text.getNearestCursor([x, 5]);
|
|
|
|
},
|
|
|
|
moveCursorX: func(x) {
|
|
|
|
me._cursor = me._getNearestCursor(x)[1];
|
2014-08-31 16:08:00 +00:00
|
|
|
me._onStateChange();
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
|
|
|
home: func()
|
|
|
|
{
|
|
|
|
me.moveCursor(0);
|
2023-01-27 21:59:58 +00:00
|
|
|
me.clearSelection();
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
|
|
|
end: func()
|
|
|
|
{
|
|
|
|
me.moveCursor(utf8.size(me._text));
|
2023-01-27 21:59:58 +00:00
|
|
|
me.clearSelection();
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
|
|
|
# Insert given text after cursor (and first remove selection if set)
|
|
|
|
insert: func(text)
|
|
|
|
{
|
2023-01-27 21:59:58 +00:00
|
|
|
var after = utf8.substr(me._text, me._cursor);
|
|
|
|
me._text = utf8.substr(me._text, 0, me._cursor);
|
2014-08-31 16:08:00 +00:00
|
|
|
|
|
|
|
# Replace selected text, insert new text and place cursor after inserted
|
|
|
|
# text
|
2023-01-27 21:59:58 +00:00
|
|
|
var remaining = me._max_length - me._cursor - utf8.size(after);
|
|
|
|
if (remaining > 0) {
|
2014-08-31 16:08:00 +00:00
|
|
|
me._text ~= utf8.substr(text, 0, remaining);
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
2014-08-31 16:08:00 +00:00
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
#me.clearSelection();
|
2014-08-31 16:08:00 +00:00
|
|
|
me._cursor = utf8.size(me._text);
|
|
|
|
|
|
|
|
me._text ~= after;
|
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
if (me._view != nil) {
|
2014-08-31 16:08:00 +00:00
|
|
|
me._view.setText(me, me._text);
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
|
|
|
me._trigger("text-changed");
|
2014-11-23 22:35:26 +00:00
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
me._onStateChange();
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
2023-01-06 11:21:26 +00:00
|
|
|
copy: func() {
|
|
|
|
clipboard.setText(me.selectedText());
|
|
|
|
},
|
|
|
|
cut: func() {
|
|
|
|
clipboard.setText(me.selectedText());
|
|
|
|
me.removeSelection();
|
|
|
|
},
|
2014-08-31 16:08:00 +00:00
|
|
|
paste: func(mode = nil)
|
|
|
|
{
|
|
|
|
me.insert(clipboard.getText(mode != nil ? mode : clipboard.CLIPBOARD));
|
|
|
|
},
|
2023-01-06 11:21:26 +00:00
|
|
|
selectAll: func() {
|
2023-01-27 21:59:58 +00:00
|
|
|
me.setSelection(0, utf8.size(me._text));
|
2023-01-06 11:21:26 +00:00
|
|
|
},
|
2014-08-31 16:08:00 +00:00
|
|
|
# Remove selected text
|
|
|
|
removeSelection: func()
|
|
|
|
{
|
2023-01-27 21:59:58 +00:00
|
|
|
if (me._selection_start == me._selection_end) {
|
|
|
|
me._selection_start = me._selection_end = 0;
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
2014-08-31 16:08:00 +00:00
|
|
|
|
|
|
|
me._text = utf8.substr(me._text, 0, me._selection_start)
|
|
|
|
~ utf8.substr(me._text, me._selection_end);
|
|
|
|
|
|
|
|
me._cursor = me._selection_start;
|
2023-01-27 21:59:58 +00:00
|
|
|
me.clearSelection();
|
2014-08-31 16:08:00 +00:00
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
if (me._view != nil) {
|
2014-08-31 16:08:00 +00:00
|
|
|
me._view.setText(me, me._text);
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
|
|
|
me._trigger("text-changed");
|
2014-11-23 22:35:26 +00:00
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
me._onStateChange();
|
|
|
|
return me
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
|
|
|
# Remove selection or if nothing is selected the character before the cursor
|
|
|
|
backspace: func()
|
|
|
|
{
|
2023-01-27 21:59:58 +00:00
|
|
|
if (me._selection_start == me._selection_end) {
|
|
|
|
if (me._cursor == 0) {
|
2014-08-31 16:08:00 +00:00
|
|
|
# Before first character...
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
|
|
|
me._selection_start = me._cursor - 1;
|
|
|
|
me._selection_end = me._cursor;
|
2014-08-31 16:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me.removeSelection();
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
|
|
|
# Remove selection or if nothing is selected the character after the cursor
|
2023-01-06 11:21:26 +00:00
|
|
|
delete: func()
|
2014-08-31 16:08:00 +00:00
|
|
|
{
|
2023-01-27 21:59:58 +00:00
|
|
|
if (me._selection_start == me._selection_end) {
|
|
|
|
if (me._cursor == utf8.size(me._text)) {
|
2014-08-31 16:08:00 +00:00
|
|
|
# After last character...
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
2014-08-31 16:08:00 +00:00
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
me._selection_start = me._cursor;
|
|
|
|
me._selection_end = me._cursor + 1;
|
2014-08-31 16:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me.removeSelection();
|
2014-11-23 22:35:26 +00:00
|
|
|
return me;
|
2014-08-31 16:08:00 +00:00
|
|
|
},
|
|
|
|
# protected:
|
|
|
|
_setView: func(view)
|
|
|
|
{
|
|
|
|
call(gui.Widget._setView, [view], me);
|
|
|
|
|
|
|
|
var el = view._root;
|
2023-01-08 14:35:46 +00:00
|
|
|
el.addEventListener("keypress", func (e) {
|
|
|
|
if (!e.ctrlKey and !e.altKey and !e.metaKey) {
|
2023-01-27 21:59:58 +00:00
|
|
|
me.removeSelection();
|
2023-01-08 14:35:46 +00:00
|
|
|
me.insert(e.key);
|
|
|
|
}
|
|
|
|
});
|
2014-08-31 16:08:00 +00:00
|
|
|
el.addEventListener("keydown", func (e)
|
|
|
|
{
|
|
|
|
if( me._view == nil )
|
|
|
|
return;
|
|
|
|
|
2023-01-27 21:59:58 +00:00
|
|
|
if (e.key == "Enter") {
|
2015-06-02 16:27:19 +00:00
|
|
|
me._trigger("editingFinished", {text: me.text()}); # TODO validator/etc.
|
2023-01-27 21:59:58 +00:00
|
|
|
} elsif (e.key == "Backspace") {
|
2014-08-31 16:08:00 +00:00
|
|
|
me.backspace();
|
2023-01-27 21:59:58 +00:00
|
|
|
} elsif (e.key == "Delete") {
|
2023-01-06 11:21:26 +00:00
|
|
|
me.delete();
|
2023-01-27 21:59:58 +00:00
|
|
|
} elsif (e.key == "Left") {
|
|
|
|
if (e.shiftKey) {
|
|
|
|
if (me._selection_start == 0 and me._selection_end == 0) {
|
|
|
|
var start = me._cursor;
|
|
|
|
var end = me._cursor;
|
|
|
|
} else {
|
|
|
|
var start = me._selection_start;
|
|
|
|
var end = me._selection_end;
|
|
|
|
}
|
|
|
|
if (start > 0) {
|
|
|
|
me.setSelection(start - 1, end);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (me._selection_start != 0 or me._selection_end != 0) {
|
|
|
|
me.moveCursor(me._selection_start);
|
|
|
|
me.clearSelection();
|
|
|
|
} else {
|
|
|
|
me.moveCursor(me._cursor - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elsif (e.key == "Right") {
|
|
|
|
if (e.shiftKey) {
|
|
|
|
if (me._selection_start == 0 and me._selection_end == 0) {
|
|
|
|
var start = me._cursor;
|
|
|
|
var end = me._cursor;
|
|
|
|
} else {
|
|
|
|
var start = me._selection_start;
|
|
|
|
var end = me._selection_end;
|
|
|
|
}
|
|
|
|
if (end + 1< utf8.size(me._text)) {
|
|
|
|
me.setSelection(start, end + 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (me._selection_end != 0 or me._selection_start != 0) {
|
|
|
|
me.moveCursor(me._selection_end);
|
|
|
|
me.clearSelection();
|
|
|
|
} else {
|
|
|
|
me.moveCursor(me._cursor + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elsif (e.key == "Home") {
|
2014-08-31 16:08:00 +00:00
|
|
|
me.home();
|
2023-01-27 21:59:58 +00:00
|
|
|
} elsif (e.key == "End") {
|
2014-08-31 16:08:00 +00:00
|
|
|
me.end();
|
2023-01-27 21:59:58 +00:00
|
|
|
}
|
2014-08-31 16:08:00 +00:00
|
|
|
});
|
2023-01-06 11:21:26 +00:00
|
|
|
el.addEventListener("click", func(e) {
|
|
|
|
if (e.button == 2) {
|
|
|
|
me.showContextMenu(e);
|
2023-01-27 21:59:58 +00:00
|
|
|
} elsif (e.button == 0) {
|
|
|
|
me.clearSelection();
|
|
|
|
me.moveCursorX(e.localX - view._text.getTranslation()[0]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
el.addEventListener("dblclick", func(e) {
|
|
|
|
me.selectAll();
|
|
|
|
});
|
|
|
|
el.addEventListener("drag", func(e) {
|
|
|
|
var pos = me._getNearestCursor(e.localX - view._text.getTranslation()[0])[1];
|
|
|
|
if (me._selection_start < pos and me._selection_end > pos) { # dragging within existing selection
|
|
|
|
# TODO: implement full drag / drop support
|
|
|
|
} elsif (me._selection_start != me._selection_end) { # existing selection, but dragging outside
|
|
|
|
if (e.deltaX < 0) {
|
|
|
|
me.setSelection(pos, me._selection_end);
|
|
|
|
} elsif (e.deltaX > 0) {
|
|
|
|
me.setSelection(me._selection_start, pos);
|
|
|
|
}
|
|
|
|
} else { # no existing selection, create one from drag position
|
|
|
|
if (math.abs(e.deltaX) > 1) {
|
|
|
|
var start = pos;
|
|
|
|
var end = pos + math.sgn(e.deltaX);
|
|
|
|
me.setSelection(math.min(start, end), math.max(start, end));
|
|
|
|
}
|
2023-01-06 11:21:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
del: func() {
|
|
|
|
me.context_menu.del();
|
2023-01-27 21:59:58 +00:00
|
|
|
me._view._cursor_blink_timer.stop();
|
2014-08-31 16:08:00 +00:00
|
|
|
}
|
|
|
|
};
|