2013-07-27 09:54:41 +00:00
|
|
|
gui.Widget = {
|
|
|
|
# enum FocusPolicy:
|
|
|
|
NoFocus: 0,
|
|
|
|
TabFocus: 1,
|
|
|
|
ClickFocus: 2,
|
|
|
|
StrongFocus: 1 + 2,
|
|
|
|
|
|
|
|
#
|
|
|
|
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],
|
|
|
|
_size: [32, 32]
|
2014-06-01 10:25:34 +00:00
|
|
|
});
|
2014-06-08 22:37:14 +00:00
|
|
|
|
|
|
|
m.setMinimumSize([16, 16]);
|
|
|
|
m.setSizeHint([32, 32]);
|
|
|
|
m.setMaximumSize([m._MAX_SIZE, m._MAX_SIZE]);
|
|
|
|
|
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-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();
|
|
|
|
if( canvas._focused_widget != nil )
|
|
|
|
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-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;
|
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;
|
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
listen: func(type, cb)
|
|
|
|
{
|
|
|
|
me._view._root.addEventListener("cb." ~ type, cb);
|
|
|
|
return me;
|
|
|
|
},
|
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"
|
2013-07-27 09:54:41 +00:00
|
|
|
_onStateChange: func {},
|
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 {
|
2013-07-27 09:54:41 +00:00
|
|
|
if( bits.test(me._focus_policy, me.ClickFocus / 2) )
|
|
|
|
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();
|
|
|
|
});
|
2014-06-01 10:25:34 +00:00
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
_trigger: func(type, data = nil)
|
|
|
|
{
|
|
|
|
me._view._root.dispatchEvent(
|
|
|
|
canvas.CustomEvent.new("cb." ~ type, {detail: data})
|
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|