| 1 |
gui.widgets.LineEdit = { |
| 2 |
new: func(parent, style = nil, cfg = nil) |
| 3 |
{ |
| 4 |
style = style or canvas.style; |
| 5 |
cfg = Config.new(cfg); |
| 6 |
var m = gui.Widget.new(gui.widgets.LineEdit, cfg); |
| 7 |
m._focus_policy = m.StrongFocus; |
| 8 |
m._setView( style.createWidget(parent, "line-edit", cfg) ); |
| 9 |
|
| 10 |
m.setLayoutMinimumSize([28, 16]); |
| 11 |
m.setLayoutSizeHint([150, 28]); |
| 12 |
|
| 13 |
m._text = ""; |
| 14 |
m._placeholder = ""; |
| 15 |
m._max_length = 32767; |
| 16 |
m._cursor = 0; |
| 17 |
m._selection_start = 0; |
| 18 |
m._selection_end = 0; |
| 19 |
|
| 20 |
m.context_menu = gui.Menu.new(); |
| 21 |
m.context_menu.createItem(text: "Copy", cb: func() { m.copy(); }, shortcut: "<Ctrl>+C"); |
| 22 |
m.context_menu.createItem(text: "Cut", cb: func() { m.cut(); }, shortcut: "<Ctrl>+X"); |
| 23 |
m.context_menu.createItem(text: "Paste", cb: func() { m.paste(); }, shortcut: "<Ctrl>+V"); |
| 24 |
m.context_menu.createItem(text: "Clear", cb: func() { m.clear(); }, shortcut: "<Ctrl>+D"); |
| 25 |
m.context_menu.createItem(text: "Select all", cb: func() { m.selectAll(); }, shortcut: "<Ctrl>+A"); |
| 26 |
m.context_menu.setCanvasItem(m); |
| 27 |
|
| 28 |
m.setText(cfg.get("text", "")); |
| 29 |
m.setPlaceholder(cfg.get("placeholder", "")); |
| 30 |
m.setMaxLength(cfg.get("max-length", m._max_length)); |
| 31 |
|
| 32 |
return m; |
| 33 |
}, |
| 34 |
showContextMenu: func(e) { |
| 35 |
me.context_menu.show(e.screenX, e.screenY); |
| 36 |
}, |
| 37 |
setText: func(text) |
| 38 |
{ |
| 39 |
if (text == nil) { |
| 40 |
me.clear(); |
| 41 |
return me; |
| 42 |
} |
| 43 |
|
| 44 |
me._text = utf8.substr(text, 0, me._max_length); |
| 45 |
me._cursor = utf8.size(me._text); |
| 46 |
me.clearSelection(); |
| 47 |
|
| 48 |
if( me._view != nil ) |
| 49 |
me._view.setText(me, me._text); |
| 50 |
me._trigger("text-changed"); |
| 51 |
|
| 52 |
return me; |
| 53 |
}, |
| 54 |
setPlaceholder: func(placeholder) { |
| 55 |
me._placeholder = placeholder; |
| 56 |
if (me._view != nil) { |
| 57 |
me._view.setPlaceholder(me, me._placeholder); |
| 58 |
} |
| 59 |
|
| 60 |
return me; |
| 61 |
}, |
| 62 |
clear: func |
| 63 |
{ |
| 64 |
me._text = ""; |
| 65 |
me._cursor = 0; |
| 66 |
me.clearSelection(); |
| 67 |
|
| 68 |
if( me._view != nil ) |
| 69 |
me._view.setText(me, ""); |
| 70 |
me._trigger("text-changed"); |
| 71 |
me._onStateChange(); |
| 72 |
}, |
| 73 |
text: func() |
| 74 |
{ |
| 75 |
return me._text; |
| 76 |
}, |
| 77 |
selectedText: func() { |
| 78 |
return utf8.substr(me._text, me._selection_start, me._selection_end); |
| 79 |
}, |
| 80 |
setMaxLength: func(len) |
| 81 |
{ |
| 82 |
me._max_length = len; |
| 83 |
|
| 84 |
if (utf8.size(me._text) <= len) { |
| 85 |
return me; |
| 86 |
} |
| 87 |
|
| 88 |
me._text = utf8.substr(me._text, 0, me._max_length); |
| 89 |
if( me._view != nil ) |
| 90 |
me._view.setText(me, ""); |
| 91 |
me._trigger("text-changed"); |
| 92 |
me.moveCursor(me._cursor); |
| 93 |
return me; |
| 94 |
}, |
| 95 |
moveCursor: func(pos, mark = 0) |
| 96 |
{ |
| 97 |
var len = utf8.size(me._text); |
| 98 |
me._cursor = math.max(0, math.min(pos, len)); |
| 99 |
|
| 100 |
me._onStateChange(); |
| 101 |
return me; |
| 102 |
}, |
| 103 |
clearSelection: func { |
| 104 |
me._selection_start = me._selection_end = 0; |
| 105 |
me._onStateChange(); |
| 106 |
}, |
| 107 |
setSelection: func(start, end) { |
| 108 |
me._selection_start = start; |
| 109 |
me._selection_end = end; |
| 110 |
me._onStateChange(); |
| 111 |
}, |
| 112 |
getSelection: func { |
| 113 |
if (me._selection_start != me._selection_end) { |
| 114 |
return [me._selection_start, me._selection_end]; |
| 115 |
} else { |
| 116 |
return nil; |
| 117 |
} |
| 118 |
}, |
| 119 |
_getNearestCursorPos: func(x) { |
| 120 |
var crs = me._getNearestCursor(x); |
| 121 |
return me._view._text.getCursorPos(crs[0], crs[1]); |
| 122 |
}, |
| 123 |
_getNearestCursor: func(x) { |
| 124 |
return me._view._text.getNearestCursor([x, 5]); |
| 125 |
}, |
| 126 |
moveCursorX: func(x) { |
| 127 |
me._cursor = me._getNearestCursor(x)[1]; |
| 128 |
me._onStateChange(); |
| 129 |
return me; |
| 130 |
}, |
| 131 |
home: func() |
| 132 |
{ |
| 133 |
me.moveCursor(0); |
| 134 |
me.clearSelection(); |
| 135 |
}, |
| 136 |
end: func() |
| 137 |
{ |
| 138 |
me.moveCursor(utf8.size(me._text)); |
| 139 |
me.clearSelection(); |
| 140 |
}, |
| 141 |
# Insert given text after cursor (and first remove selection if set) |
| 142 |
insert: func(text) |
| 143 |
{ |
| 144 |
var after = utf8.substr(me._text, me._cursor); |
| 145 |
me._text = utf8.substr(me._text, 0, me._cursor); |
| 146 |
|
| 147 |
# Replace selected text, insert new text and place cursor after inserted |
| 148 |
# text |
| 149 |
var remaining = me._max_length - me._cursor - utf8.size(after); |
| 150 |
if (remaining > 0) { |
| 151 |
me._text ~= utf8.substr(text, 0, remaining); |
| 152 |
} |
| 153 |
|
| 154 |
#me.clearSelection(); |
| 155 |
me._cursor = utf8.size(me._text); |
| 156 |
|
| 157 |
me._text ~= after; |
| 158 |
|
| 159 |
if (me._view != nil) { |
| 160 |
me._view.setText(me, me._text); |
| 161 |
} |
| 162 |
me._trigger("text-changed"); |
| 163 |
|
| 164 |
me._onStateChange(); |
| 165 |
return me; |
| 166 |
}, |
| 167 |
copy: func() { |
| 168 |
clipboard.setText(me.selectedText()); |
| 169 |
}, |
| 170 |
cut: func() { |
| 171 |
clipboard.setText(me.selectedText()); |
| 172 |
me.removeSelection(); |
| 173 |
}, |
| 174 |
paste: func(mode = nil) |
| 175 |
{ |
| 176 |
me.insert(clipboard.getText(mode != nil ? mode : clipboard.CLIPBOARD)); |
| 177 |
}, |
| 178 |
selectAll: func() { |
| 179 |
me.setSelection(0, utf8.size(me._text)); |
| 180 |
}, |
| 181 |
# Remove selected text |
| 182 |
removeSelection: func() |
| 183 |
{ |
| 184 |
if (me._selection_start == me._selection_end) { |
| 185 |
me._selection_start = me._selection_end = 0; |
| 186 |
return me; |
| 187 |
} |
| 188 |
|
| 189 |
me._text = utf8.substr(me._text, 0, me._selection_start) |
| 190 |
~ utf8.substr(me._text, me._selection_end); |
| 191 |
|
| 192 |
me._cursor = me._selection_start; |
| 193 |
me.clearSelection(); |
| 194 |
|
| 195 |
if (me._view != nil) { |
| 196 |
me._view.setText(me, me._text); |
| 197 |
} |
| 198 |
me._trigger("text-changed"); |
| 199 |
|
| 200 |
me._onStateChange(); |
| 201 |
return me |
| 202 |
}, |
| 203 |
# Remove selection or if nothing is selected the character before the cursor |
| 204 |
backspace: func() |
| 205 |
{ |
| 206 |
if (me._selection_start == me._selection_end) { |
| 207 |
if (me._cursor == 0) { |
| 208 |
# Before first character... |
| 209 |
return me; |
| 210 |
} |
| 211 |
me._selection_start = me._cursor - 1; |
| 212 |
me._selection_end = me._cursor; |
| 213 |
} |
| 214 |
|
| 215 |
me.removeSelection(); |
| 216 |
return me; |
| 217 |
}, |
| 218 |
# Remove selection or if nothing is selected the character after the cursor |
| 219 |
delete: func() |
| 220 |
{ |
| 221 |
if (me._selection_start == me._selection_end) { |
| 222 |
if (me._cursor == utf8.size(me._text)) { |
| 223 |
# After last character... |
| 224 |
return me; |
| 225 |
} |
| 226 |
|
| 227 |
me._selection_start = me._cursor; |
| 228 |
me._selection_end = me._cursor + 1; |
| 229 |
} |
| 230 |
|
| 231 |
me.removeSelection(); |
| 232 |
return me; |
| 233 |
}, |
| 234 |
# protected: |
| 235 |
_setView: func(view) |
| 236 |
{ |
| 237 |
call(gui.Widget._setView, [view], me); |
| 238 |
|
| 239 |
var el = view._root; |
| 240 |
el.addEventListener("keypress", func (e) { |
| 241 |
if (!e.ctrlKey and !e.altKey and !e.metaKey) { |
| 242 |
me.removeSelection(); |
| 243 |
me.insert(e.key); |
| 244 |
} |
| 245 |
}); |
| 246 |
el.addEventListener("keydown", func (e) |
| 247 |
{ |
| 248 |
if( me._view == nil ) |
| 249 |
return; |
| 250 |
|
| 251 |
if (e.key == "Enter") { |
| 252 |
me._trigger("editingFinished", {text: me.text()}); # TODO validator/etc. |
| 253 |
} elsif (e.key == "Backspace") { |
| 254 |
me.backspace(); |
| 255 |
} elsif (e.key == "Delete") { |
| 256 |
me.delete(); |
| 257 |
} elsif (e.key == "Left") { |
| 258 |
if (e.shiftKey) { |
| 259 |
if (me._selection_start == 0 and me._selection_end == 0) { |
| 260 |
var start = me._cursor; |
| 261 |
var end = me._cursor; |
| 262 |
} else { |
| 263 |
var start = me._selection_start; |
| 264 |
var end = me._selection_end; |
| 265 |
} |
| 266 |
if (start > 0) { |
| 267 |
me.setSelection(start - 1, end); |
| 268 |
} |
| 269 |
} else { |
| 270 |
if (me._selection_start != 0 or me._selection_end != 0) { |
| 271 |
me.moveCursor(me._selection_start); |
| 272 |
me.clearSelection(); |
| 273 |
} else { |
| 274 |
me.moveCursor(me._cursor - 1); |
| 275 |
} |
| 276 |
} |
| 277 |
} elsif (e.key == "Right") { |
| 278 |
if (e.shiftKey) { |
| 279 |
if (me._selection_start == 0 and me._selection_end == 0) { |
| 280 |
var start = me._cursor; |
| 281 |
var end = me._cursor; |
| 282 |
} else { |
| 283 |
var start = me._selection_start; |
| 284 |
var end = me._selection_end; |
| 285 |
} |
| 286 |
if (end + 1< utf8.size(me._text)) { |
| 287 |
me.setSelection(start, end + 1); |
| 288 |
} |
| 289 |
} else { |
| 290 |
if (me._selection_end != 0 or me._selection_start != 0) { |
| 291 |
me.moveCursor(me._selection_end); |
| 292 |
me.clearSelection(); |
| 293 |
} else { |
| 294 |
me.moveCursor(me._cursor + 1); |
| 295 |
} |
| 296 |
} |
| 297 |
} elsif (e.key == "Home") { |
| 298 |
me.home(); |
| 299 |
} elsif (e.key == "End") { |
| 300 |
me.end(); |
| 301 |
} |
| 302 |
}); |
| 303 |
el.addEventListener("click", func(e) { |
| 304 |
if (e.button == 2) { |
| 305 |
me.showContextMenu(e); |
| 306 |
} elsif (e.button == 0) { |
| 307 |
me.clearSelection(); |
| 308 |
me.moveCursorX(e.localX - view._text.getTranslation()[0]); |
| 309 |
} |
| 310 |
}); |
| 311 |
el.addEventListener("dblclick", func(e) { |
| 312 |
me.selectAll(); |
| 313 |
}); |
| 314 |
el.addEventListener("drag", func(e) { |
| 315 |
var pos = me._getNearestCursor(e.localX - view._text.getTranslation()[0])[1]; |
| 316 |
if (me._selection_start < pos and me._selection_end > pos) { # dragging within existing selection |
| 317 |
# TODO: implement full drag / drop support |
| 318 |
} elsif (me._selection_start != me._selection_end) { # existing selection, but dragging outside |
| 319 |
if (e.deltaX < 0) { |
| 320 |
me.setSelection(pos, me._selection_end); |
| 321 |
} elsif (e.deltaX > 0) { |
| 322 |
me.setSelection(me._selection_start, pos); |
| 323 |
} |
| 324 |
} else { # no existing selection, create one from drag position |
| 325 |
if (math.abs(e.deltaX) > 1) { |
| 326 |
var start = pos; |
| 327 |
var end = pos + math.sgn(e.deltaX); |
| 328 |
me.setSelection(math.min(start, end), math.max(start, end)); |
| 329 |
} |
| 330 |
} |
| 331 |
}); |
| 332 |
}, |
| 333 |
del: func() { |
| 334 |
me.context_menu.del(); |
| 335 |
me._view._cursor_blink_timer.stop(); |
| 336 |
} |
| 337 |
}; |