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],
|
|
|
|
_size: [32, 32]
|
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-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;
|
|
|
|
},
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( me._focused )
|
|
|
|
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-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();
|
|
|
|
});
|
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;
|