2013-07-27 09:54:41 +00:00
|
|
|
gui.widgets.Button = {
|
|
|
|
new: func(parent, style, cfg)
|
|
|
|
{
|
|
|
|
var cfg = Config.new(cfg);
|
|
|
|
var m = gui.Widget.new(gui.widgets.Button);
|
|
|
|
m._focus_policy = m.StrongFocus;
|
|
|
|
m._active = 0;
|
|
|
|
m._flat = cfg.get("flat", 0);
|
|
|
|
|
|
|
|
if( style != nil and !m._flat )
|
|
|
|
{
|
|
|
|
m._button = style.createWidget(parent, "button", cfg);
|
|
|
|
m._setRoot(m._button.element);
|
|
|
|
}
|
|
|
|
|
2014-06-01 10:25:34 +00:00
|
|
|
m.setMinimumSize([16, 16]);
|
2014-06-04 15:52:48 +00:00
|
|
|
m.setSizeHint([32, 32]);
|
|
|
|
m.setMaximumSize([m._MAX_SIZE, m._MAX_SIZE]);
|
2014-06-01 10:25:34 +00:00
|
|
|
|
|
|
|
m.setSetGeometryFunc(m.setGeometry);
|
|
|
|
|
2013-07-27 09:54:41 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
setText: func(text)
|
|
|
|
{
|
|
|
|
me._button.setText(text);
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
setActive: func
|
|
|
|
{
|
|
|
|
if( me._active )
|
|
|
|
return me;
|
|
|
|
|
|
|
|
me._active = 1;
|
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
clearActive: func
|
|
|
|
{
|
|
|
|
if( !me._active )
|
|
|
|
return me;
|
|
|
|
|
|
|
|
me._active = 0;
|
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
onClick: func {},
|
2014-06-01 10:25:34 +00:00
|
|
|
setGeometry: func(geom)
|
|
|
|
{
|
|
|
|
me.move(geom[0], geom[1]);
|
|
|
|
me._button.setSize([geom[2] - geom[0], geom[3] - geom[1]]);
|
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
2013-07-27 09:54:41 +00:00
|
|
|
# protected:
|
|
|
|
_onStateChange: func
|
|
|
|
{
|
|
|
|
if( me._button != nil )
|
2014-06-01 10:25:34 +00:00
|
|
|
me._button.update(me._active, me._focused, me._hover, !me._windowFocus());
|
2013-07-27 09:54:41 +00:00
|
|
|
},
|
|
|
|
_setRoot: func(el)
|
|
|
|
{
|
|
|
|
el.addEventListener("mousedown", func me.setActive());
|
|
|
|
el.addEventListener("mouseup", func me.clearActive());
|
|
|
|
|
|
|
|
# Use 'call' to ensure 'me' is not set and can be used in the closure of
|
|
|
|
# custom callbacks. TODO pass 'me' as argument?
|
|
|
|
el.addEventListener("click", func call(me.onClick));
|
|
|
|
|
|
|
|
el.addEventListener("mouseleave",func me.clearActive());
|
|
|
|
el.addEventListener("drag", func(e) e.stopPropagation());
|
|
|
|
|
|
|
|
call(gui.Widget._setRoot, [el], me);
|
|
|
|
}
|
|
|
|
};
|