2013-07-27 09:54:41 +00:00
|
|
|
gui.Widget = {
|
|
|
|
#
|
|
|
|
new: func(derived)
|
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
var m = canvas.Widget.new({
|
2013-07-27 09:54:41 +00:00
|
|
|
parents: [derived, gui.Widget],
|
|
|
|
_focused: 0,
|
|
|
|
_focus_policy: gui.Widget.NoFocus,
|
|
|
|
_hover: 0,
|
2014-06-10 22:00:55 +00:00
|
|
|
_enabled: 1,
|
2014-06-08 22:37:14 +00:00
|
|
|
_view: nil,
|
|
|
|
_pos: [0, 0],
|
2023-01-11 11:46:24 +00:00
|
|
|
_size: [32, 32],
|
|
|
|
_bindings: []
|
2014-06-01 10:25:34 +00:00
|
|
|
});
|
2014-06-08 22:37:14 +00:00
|
|
|
|
2014-07-12 15:55:45 +00:00
|
|
|
m.setLayoutMinimumSize([16, 16]);
|
|
|
|
m.setLayoutSizeHint([32, 32]);
|
|
|
|
m.setLayoutMaximumSize([m._MAX_SIZE, m._MAX_SIZE]);
|
2014-06-08 22:37:14 +00:00
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
m.setSetGeometryFunc(m._impl.setGeometry);
|
2014-06-08 22:37:14 +00:00
|
|
|
|
|
|
|
return m;
|
2013-07-27 09:54:41 +00:00
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
setFixedSize: func(x, y)
|
|
|
|
{
|
|
|
|
me.setMinimumSize([x, y]);
|
|
|
|
me.setSizeHint([x, y]);
|
|
|
|
me.setMaximumSize([x, y]);
|
2014-06-17 15:12:21 +00:00
|
|
|
return me;
|
2014-06-10 16:46:52 +00:00
|
|
|
},
|
2014-06-10 22:00:55 +00:00
|
|
|
setEnabled: func(enabled)
|
|
|
|
{
|
|
|
|
if( me._enabled == enabled )
|
|
|
|
return me;
|
|
|
|
|
|
|
|
me._enabled = enabled;
|
|
|
|
me.clearFocus();
|
|
|
|
|
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
2013-07-27 09:54:41 +00:00
|
|
|
# Move the widget to the given position (relative to its parent)
|
|
|
|
move: func(x, y)
|
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
me._pos[0] = x;
|
|
|
|
me._pos[1] = y;
|
|
|
|
|
|
|
|
if( me._view != nil )
|
|
|
|
me._view._root.setTranslation(x, y);
|
2013-07-27 09:54:41 +00:00
|
|
|
return me;
|
|
|
|
},
|
|
|
|
#
|
2014-03-19 22:20:09 +00:00
|
|
|
setSize: func(w, h)
|
|
|
|
{
|
|
|
|
me._size[0] = w;
|
|
|
|
me._size[1] = h;
|
2014-06-08 22:37:14 +00:00
|
|
|
|
|
|
|
if( me._view != nil )
|
2014-06-12 22:15:30 +00:00
|
|
|
me._view.setSize(me, w, h);
|
2014-06-08 22:37:14 +00:00
|
|
|
return me;
|
|
|
|
},
|
|
|
|
# Set geometry of widget (usually used by layouting system)
|
|
|
|
#
|
2014-06-10 16:46:52 +00:00
|
|
|
# @param geom [<x>, <y>, <width>, <height>]
|
2014-06-08 22:37:14 +00:00
|
|
|
setGeometry: func(geom)
|
|
|
|
{
|
|
|
|
me.move(geom[0], geom[1]);
|
2014-06-10 16:46:52 +00:00
|
|
|
me.setSize(geom[2], geom[3]);
|
2014-06-08 22:37:14 +00:00
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
2014-03-19 22:20:09 +00:00
|
|
|
},
|
|
|
|
#
|
2013-07-27 09:54:41 +00:00
|
|
|
setFocus: func
|
|
|
|
{
|
|
|
|
if( me._focused )
|
|
|
|
return me;
|
|
|
|
|
2014-06-01 10:25:34 +00:00
|
|
|
var canvas = me.getCanvas();
|
2014-06-29 10:03:47 +00:00
|
|
|
if( canvas._impl['_focused_widget'] != nil )
|
2014-06-01 10:25:34 +00:00
|
|
|
canvas._focused_widget.clearFocus();
|
2013-07-27 09:54:41 +00:00
|
|
|
|
2014-06-10 22:00:55 +00:00
|
|
|
if( !me._enabled )
|
|
|
|
return me;
|
|
|
|
|
2013-07-27 09:54:41 +00:00
|
|
|
me._focused = 1;
|
2014-06-01 10:25:34 +00:00
|
|
|
canvas._focused_widget = me;
|
2013-07-27 09:54:41 +00:00
|
|
|
|
2014-08-31 16:08:00 +00:00
|
|
|
if( me._view != nil )
|
|
|
|
me._view._root.setFocus();
|
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
me._trigger("focus-in");
|
2013-07-27 09:54:41 +00:00
|
|
|
me._onStateChange();
|
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
#
|
|
|
|
clearFocus: func
|
|
|
|
{
|
|
|
|
if( !me._focused )
|
|
|
|
return me;
|
|
|
|
|
|
|
|
me._focused = 0;
|
2014-06-01 10:25:34 +00:00
|
|
|
me.getCanvas()._focused_widget = nil;
|
2014-08-31 16:08:00 +00:00
|
|
|
me.getCanvas().clearFocusElement();
|
2013-07-27 09:54:41 +00:00
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
me._trigger("focus-out");
|
2013-07-27 09:54:41 +00:00
|
|
|
me._onStateChange();
|
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
2022-06-30 10:39:41 +00:00
|
|
|
#
|
|
|
|
hasActiveFocus:func
|
|
|
|
{
|
|
|
|
return me.getCanvas()._focused_widget == me;
|
|
|
|
},
|
|
|
|
#
|
2014-06-10 16:46:52 +00:00
|
|
|
listen: func(type, cb)
|
|
|
|
{
|
|
|
|
me._view._root.addEventListener("cb." ~ type, cb);
|
|
|
|
return me;
|
|
|
|
},
|
2014-06-16 19:54:26 +00:00
|
|
|
onRemove: func
|
|
|
|
{
|
|
|
|
if( me._view != nil )
|
2014-06-29 10:03:47 +00:00
|
|
|
{
|
2014-06-16 19:54:26 +00:00
|
|
|
me._view._root.del();
|
2014-06-29 10:03:47 +00:00
|
|
|
me._view = nil;
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:30:50 +00:00
|
|
|
if( me._focused and me.getCanvas() )
|
2014-06-29 10:03:47 +00:00
|
|
|
me.getCanvas()._focused_widget = nil;
|
2014-06-16 19:54:26 +00:00
|
|
|
},
|
2013-07-27 09:54:41 +00:00
|
|
|
# protected:
|
2014-06-04 15:52:48 +00:00
|
|
|
_MAX_SIZE: 32768, # size for "no size-limit"
|
2014-06-21 14:07:15 +00:00
|
|
|
_onStateChange: func
|
|
|
|
{
|
|
|
|
if( me._view != nil and me._view.update != nil )
|
|
|
|
me._view.update(me);
|
|
|
|
},
|
2014-07-20 22:28:44 +00:00
|
|
|
visibilityChanged: func(visible)
|
|
|
|
{
|
|
|
|
me._view._root.setVisible(visible);
|
|
|
|
},
|
2023-01-11 11:46:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
bindShortcut: func(s, f)
|
|
|
|
{
|
|
|
|
if (!isa(s, keyboard.Shortcut)) {
|
|
|
|
s = keyboard.Shortcut.new(s);
|
|
|
|
}
|
|
|
|
foreach (var b; me._bindings) {
|
|
|
|
if (b.shortcut.equals(s)) {
|
|
|
|
b.f = f;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
append(me._bindings, keyboard.Binding.new(s, f));
|
|
|
|
# add listener when first binding is added
|
|
|
|
# if this happens before the view is set, we instead do it in _setView below
|
|
|
|
if ((size(me._bindings) == 1) and me._view) {
|
2023-01-20 16:11:49 +00:00
|
|
|
me._view._root.addEventListener("keydown", func(e) me._onKeyPressed(e));
|
2023-01-11 11:46:24 +00:00
|
|
|
}
|
2023-01-08 14:35:46 +00:00
|
|
|
},
|
2023-01-11 11:46:24 +00:00
|
|
|
|
|
|
|
_onKeyPressed: func(e) {
|
|
|
|
foreach (var b; me._bindings) {
|
|
|
|
if (b.shortcut.match(keyboard.findKeyName(e.keyCode), e.shiftKey, e.ctrlKey, e.altKey, e.metaKey)) {
|
|
|
|
b.fire(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-08 22:37:14 +00:00
|
|
|
_setView: func(view)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
me._view = view;
|
2014-06-01 10:25:34 +00:00
|
|
|
|
2014-06-08 22:37:14 +00:00
|
|
|
var root = view._root;
|
|
|
|
var canvas = root.getCanvas();
|
2014-06-01 10:25:34 +00:00
|
|
|
me.setCanvas(canvas);
|
|
|
|
|
|
|
|
canvas.addEventListener("wm.focus-in", func {
|
|
|
|
me._onStateChange();
|
|
|
|
});
|
|
|
|
canvas.addEventListener("wm.focus-out", func {
|
|
|
|
me._onStateChange();
|
|
|
|
});
|
|
|
|
|
2014-06-08 22:37:14 +00:00
|
|
|
root.addEventListener("mouseenter", func {
|
2013-07-27 09:54:41 +00:00
|
|
|
me._hover = 1;
|
2014-06-10 16:46:52 +00:00
|
|
|
me._trigger("mouse-enter");
|
2013-07-27 09:54:41 +00:00
|
|
|
me._onStateChange();
|
|
|
|
});
|
2014-06-08 22:37:14 +00:00
|
|
|
root.addEventListener("mousedown", func {
|
2014-06-29 10:03:47 +00:00
|
|
|
if( me._focus_policy & me.ClickFocus )
|
2013-07-27 09:54:41 +00:00
|
|
|
me.setFocus();
|
|
|
|
});
|
2014-06-08 22:37:14 +00:00
|
|
|
root.addEventListener("mouseleave", func {
|
2013-07-27 09:54:41 +00:00
|
|
|
me._hover = 0;
|
2014-06-10 16:46:52 +00:00
|
|
|
me._trigger("mouse-leave");
|
2013-07-27 09:54:41 +00:00
|
|
|
me._onStateChange();
|
|
|
|
});
|
2023-01-08 14:35:46 +00:00
|
|
|
root.addEventListener("keypress", func(e) {
|
|
|
|
if (me._focused) {
|
|
|
|
root.onKeyPressed(e);
|
|
|
|
}
|
|
|
|
});
|
2023-01-11 11:46:24 +00:00
|
|
|
|
|
|
|
# if we have keyboard bindings defined, add the listener for them
|
|
|
|
if (size(me._bindings)) {
|
|
|
|
root.addEventListener("keydown", func(e) me._onKeyPressed(e));
|
|
|
|
}
|
2014-06-01 10:25:34 +00:00
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
_trigger: func(type, data = nil)
|
|
|
|
{
|
2014-06-29 10:03:47 +00:00
|
|
|
if( me._view != nil )
|
|
|
|
me._view._root.dispatchEvent(
|
|
|
|
canvas.CustomEvent.new("cb." ~ type, {detail: data})
|
|
|
|
);
|
2014-06-10 16:46:52 +00:00
|
|
|
return me;
|
|
|
|
},
|
2014-06-01 10:25:34 +00:00
|
|
|
_windowFocus: func
|
|
|
|
{
|
|
|
|
var canvas = me.getCanvas();
|
|
|
|
return canvas != nil ? canvas.data("focused") : 0;
|
2013-07-27 09:54:41 +00:00
|
|
|
}
|
|
|
|
};
|
2014-06-29 10:03:47 +00:00
|
|
|
|
|
|
|
# enum FocusPolicy:
|
|
|
|
gui.Widget.NoFocus = 0;
|
|
|
|
gui.Widget.TabFocus = 1;
|
|
|
|
gui.Widget.ClickFocus = 2;
|
|
|
|
gui.Widget.StrongFocus = gui.Widget.TabFocus
|
|
|
|
| gui.Widget.ClickFocus;
|