1
0
Fork 0
fgdata/Nasal/canvas/gui/Widget.nas

134 lines
2.6 KiB
Text
Raw Normal View History

gui.Widget = {
# enum FocusPolicy:
NoFocus: 0,
TabFocus: 1,
ClickFocus: 2,
StrongFocus: 1 + 2,
#
new: func(derived)
{
var m = canvas.Widget.new({
parents: [derived, gui.Widget],
_focused: 0,
_focus_policy: gui.Widget.NoFocus,
_hover: 0,
_view: nil,
_pos: [0, 0],
_size: [32, 32]
});
m.setMinimumSize([16, 16]);
m.setSizeHint([32, 32]);
m.setMaximumSize([m._MAX_SIZE, m._MAX_SIZE]);
m.setSetGeometryFunc(m.setGeometry);
return m;
},
# Move the widget to the given position (relative to its parent)
move: func(x, y)
{
me._pos[0] = x;
me._pos[1] = y;
if( me._view != nil )
me._view._root.setTranslation(x, y);
return me;
},
#
2014-03-19 22:20:09 +00:00
setSize: func(w, h)
{
me._size[0] = w;
me._size[1] = h;
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
},
#
setFocus: func
{
if( me._focused )
return me;
var canvas = me.getCanvas();
if( canvas._focused_widget != nil )
canvas._focused_widget.clearFocus();
me._focused = 1;
canvas._focused_widget = me;
me.onFocusIn();
me._onStateChange();
return me;
},
#
clearFocus: func
{
if( !me._focused )
return me;
me._focused = 0;
me.getCanvas()._focused_widget = nil;
me.onFocusOut();
me._onStateChange();
return me;
},
onFocusIn: func {},
onFocusOut: func {},
onMouseEnter: func {},
onMouseLeave: func {},
# protected:
_MAX_SIZE: 32768, # size for "no size-limit"
_onStateChange: func {},
_setView: func(view)
{
me._view = view;
var root = view._root;
var canvas = root.getCanvas();
me.setCanvas(canvas);
canvas.addEventListener("wm.focus-in", func {
me._onStateChange();
});
canvas.addEventListener("wm.focus-out", func {
me._onStateChange();
});
root.addEventListener("mouseenter", func {
me._hover = 1;
me.onMouseEnter();
me._onStateChange();
});
root.addEventListener("mousedown", func {
if( bits.test(me._focus_policy, me.ClickFocus / 2) )
me.setFocus();
});
root.addEventListener("mouseleave", func {
me._hover = 0;
me.onMouseLeave();
me._onStateChange();
});
},
_windowFocus: func
{
var canvas = me.getCanvas();
return canvas != nil ? canvas.data("focused") : 0;
}
};