2022-09-26 07:58:24 +00:00
|
|
|
# SPDX-FileCopyrightText: (C) 2016 Thomas Geymayer
|
|
|
|
# SPDX-FileCopyrightText: (C) 2022 James Turner <james@flightgear.org>
|
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2013-07-27 09:54:41 +00:00
|
|
|
gui.widgets.Button = {
|
2023-01-25 16:03:16 +00:00
|
|
|
new: func(parent, style = nil, cfg = nil)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2023-01-25 16:03:16 +00:00
|
|
|
style = style or canvas.style;
|
2013-07-27 09:54:41 +00:00
|
|
|
var m = gui.Widget.new(gui.widgets.Button);
|
2023-01-25 16:03:16 +00:00
|
|
|
m._cfg = Config.new(cfg or {});
|
2013-07-27 09:54:41 +00:00
|
|
|
m._focus_policy = m.StrongFocus;
|
2014-06-10 16:46:52 +00:00
|
|
|
m._down = 0;
|
|
|
|
m._checkable = 0;
|
2023-01-21 22:48:44 +00:00
|
|
|
m._flat = m._cfg.get("flat", 0);
|
|
|
|
m._isDefault = m._cfg.get("default", 0);
|
|
|
|
m._destructive = m._cfg.get("destructive", 0);
|
2013-07-27 09:54:41 +00:00
|
|
|
|
|
|
|
if( style != nil and !m._flat )
|
2023-01-21 22:48:44 +00:00
|
|
|
m._setView( style.createWidget(parent, m._cfg.get("type", "button"), m._cfg) );
|
2014-06-01 10:25:34 +00:00
|
|
|
|
2013-07-27 09:54:41 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
setText: func(text)
|
|
|
|
{
|
2014-06-30 16:16:22 +00:00
|
|
|
if( me._view != nil )
|
|
|
|
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;
|
|
|
|
},
|
2022-06-30 10:39:41 +00:00
|
|
|
setDefault: func(isDefault)
|
|
|
|
{
|
|
|
|
me._isDefault = isDefault;
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
setDestructive: func(isDestructive)
|
|
|
|
{
|
|
|
|
me._destructive = isDestructive;
|
|
|
|
return me;
|
|
|
|
},
|
2013-07-27 09:54:41 +00:00
|
|
|
# protected:
|
2014-06-08 22:37:14 +00:00
|
|
|
_setView: func(view)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2014-08-31 16:08:00 +00:00
|
|
|
call(gui.Widget._setView, [view], me);
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
};
|