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-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]);
|
|
|
|
|
|
|
|
m.setSetGeometryFunc(m.setGeometry);
|
|
|
|
|
|
|
|
return m;
|
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 )
|
|
|
|
me._view.setSize(w, h);
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
# Set geometry of widget (usually used by layouting system)
|
|
|
|
#
|
|
|
|
# @param geom [<min-x>, <min-y>, <max-x>, <max-y>]
|
|
|
|
setGeometry: func(geom)
|
|
|
|
{
|
|
|
|
me.move(geom[0], geom[1]);
|
|
|
|
me.setSize(geom[2] - geom[0], geom[3] - geom[1]);
|
|
|
|
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
|
|
|
|
|
|
|
me._focused = 1;
|
2014-06-01 10:25:34 +00:00
|
|
|
canvas._focused_widget = me;
|
2013-07-27 09:54:41 +00:00
|
|
|
|
|
|
|
me.onFocusIn();
|
|
|
|
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
|
|
|
|
|
|
|
me.onFocusOut();
|
|
|
|
me._onStateChange();
|
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
onFocusIn: func {},
|
|
|
|
onFocusOut: func {},
|
|
|
|
onMouseEnter: func {},
|
|
|
|
onMouseLeave: func {},
|
|
|
|
# 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;
|
|
|
|
me.onMouseEnter();
|
|
|
|
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;
|
|
|
|
me.onMouseLeave();
|
|
|
|
me._onStateChange();
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
};
|