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;
|
2014-06-10 16:46:52 +00:00
|
|
|
m._down = 0;
|
|
|
|
m._checkable = 0;
|
2013-07-27 09:54:41 +00:00
|
|
|
m._flat = cfg.get("flat", 0);
|
|
|
|
|
|
|
|
if( style != nil and !m._flat )
|
2014-06-08 22:37:14 +00:00
|
|
|
m._setView( style.createWidget(parent, "button", cfg) );
|
2014-06-01 10:25:34 +00:00
|
|
|
|
2013-07-27 09:54:41 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
setText: func(text)
|
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
me._view.setText(me, text);
|
2013-07-27 09:54:41 +00:00
|
|
|
return me;
|
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
setCheckable: func(checkable)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2014-06-10 16:46:52 +00:00
|
|
|
me._checkable = checkable;
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
setChecked: func(checked = 1)
|
|
|
|
{
|
|
|
|
if( !me._checkable or me._down == checked )
|
2013-07-27 09:54:41 +00:00
|
|
|
return me;
|
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
me._trigger("clicked", {checked: checked});
|
|
|
|
me._trigger("toggled", {checked: checked});
|
|
|
|
|
|
|
|
me._down = checked;
|
2013-07-27 09:54:41 +00:00
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
setDown: func(down = 1)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2014-06-10 16:46:52 +00:00
|
|
|
if( me._checkable or me._down == down )
|
2013-07-27 09:54:41 +00:00
|
|
|
return me;
|
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
me._down = down;
|
2013-07-27 09:54:41 +00:00
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
toggle: func
|
|
|
|
{
|
|
|
|
if( !me._checkable )
|
|
|
|
me._trigger("clicked", {checked: 0});
|
|
|
|
else
|
|
|
|
me.setChecked(!me._down);
|
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
2013-07-27 09:54:41 +00:00
|
|
|
# protected:
|
|
|
|
_onStateChange: func
|
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
if( me._view != nil )
|
|
|
|
me._view.update(me);
|
2013-07-27 09:54:41 +00:00
|
|
|
},
|
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
|
|
|
var el = view._root;
|
2014-06-10 22:00:55 +00:00
|
|
|
el.addEventListener("mousedown", func if( me._enabled ) me.setDown(1));
|
|
|
|
el.addEventListener("mouseup", func if( me._enabled ) me.setDown(0));
|
|
|
|
el.addEventListener("click", func if( me._enabled ) me.toggle());
|
2013-07-27 09:54:41 +00:00
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
el.addEventListener("mouseleave",func me.setDown(0));
|
2013-07-27 09:54:41 +00:00
|
|
|
el.addEventListener("drag", func(e) e.stopPropagation());
|
|
|
|
|
2014-06-08 22:37:14 +00:00
|
|
|
call(gui.Widget._setView, [view], me);
|
2013-07-27 09:54:41 +00:00
|
|
|
}
|
|
|
|
};
|