2013-07-27 09:54:41 +00:00
|
|
|
var DefaultStyle = {
|
|
|
|
new: func(name)
|
|
|
|
{
|
|
|
|
return { parents: [gui.Style.new(name), DefaultStyle] };
|
|
|
|
},
|
|
|
|
createWidget: func(parent, type, cfg)
|
|
|
|
{
|
|
|
|
var factory = me.widgets[type];
|
|
|
|
if( factory == nil )
|
|
|
|
{
|
|
|
|
debug.warn("DefaultStyle: unknown widget type (" ~ type ~ ")");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2014-03-19 22:20:09 +00:00
|
|
|
var w = {
|
|
|
|
parents: [factory],
|
|
|
|
_style: me
|
|
|
|
};
|
|
|
|
call(factory.new, [parent, cfg], w);
|
|
|
|
return w;
|
2013-07-27 09:54:41 +00:00
|
|
|
},
|
|
|
|
widgets: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
# A button
|
|
|
|
DefaultStyle.widgets.button = {
|
|
|
|
padding: [6, 8, 6, 8],
|
2014-03-19 22:20:09 +00:00
|
|
|
new: func(parent, cfg)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
me._root = parent.createChild("group", "button");
|
2014-03-19 22:20:09 +00:00
|
|
|
me._bg =
|
2014-06-08 22:37:14 +00:00
|
|
|
me._root.createChild("path");
|
2014-03-19 22:20:09 +00:00
|
|
|
me._border =
|
2014-06-08 22:37:14 +00:00
|
|
|
me._root.createChild("image", "button")
|
|
|
|
.set("slice", "10 12"); #"7")
|
2014-03-19 22:20:09 +00:00
|
|
|
me._label =
|
2014-06-08 22:37:14 +00:00
|
|
|
me._root.createChild("text")
|
|
|
|
.set("font", "LiberationFonts/LiberationSans-Regular.ttf")
|
|
|
|
.set("character-size", 14)
|
|
|
|
.set("alignment", "center-baseline");
|
2014-06-01 10:25:34 +00:00
|
|
|
},
|
2014-06-12 22:15:30 +00:00
|
|
|
setSize: func(model, w, h)
|
2014-06-01 10:25:34 +00:00
|
|
|
{
|
|
|
|
me._bg.reset()
|
2014-06-08 22:37:14 +00:00
|
|
|
.rect(3, 3, w - 6, h - 6, {"border-radius": 5});
|
|
|
|
me._border.setSize(w, h);
|
2013-07-27 09:54:41 +00:00
|
|
|
},
|
2014-06-08 22:37:14 +00:00
|
|
|
setText: func(model, text)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
|
|
|
me._label.set("text", text);
|
2014-06-08 22:37:14 +00:00
|
|
|
|
|
|
|
# TODO get real font metrics
|
|
|
|
model.setMinimumSize([size(text) * 5 + 6, 16]);
|
|
|
|
model.setSizeHint([size(text) * 8 + 16, 32]);
|
|
|
|
|
|
|
|
return me;
|
2013-07-27 09:54:41 +00:00
|
|
|
},
|
2014-06-08 22:37:14 +00:00
|
|
|
update: func(model)
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
var backdrop = !model._windowFocus();
|
|
|
|
var (w, h) = model._size;
|
2013-07-27 09:54:41 +00:00
|
|
|
var file = me._style._dir_widgets ~ "/";
|
2014-06-08 22:37:14 +00:00
|
|
|
|
2013-07-27 09:54:41 +00:00
|
|
|
if( backdrop )
|
|
|
|
{
|
|
|
|
file ~= "backdrop-";
|
|
|
|
me._label.set("fill", me._style.getColor("backdrop_fg_color"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
me._label.set("fill", me._style.getColor("fg_color"));
|
|
|
|
file ~= "button";
|
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
if( model._down )
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
|
|
|
file ~= "-active";
|
2014-06-08 22:37:14 +00:00
|
|
|
me._label.setTranslation(w / 2 + 1, h / 2 + 6);
|
2013-07-27 09:54:41 +00:00
|
|
|
}
|
|
|
|
else
|
2014-06-08 22:37:14 +00:00
|
|
|
me._label.setTranslation(w / 2, h / 2 + 5);
|
2013-07-27 09:54:41 +00:00
|
|
|
|
|
|
|
|
2014-06-10 22:00:55 +00:00
|
|
|
if( model._enabled )
|
2013-07-27 09:54:41 +00:00
|
|
|
{
|
2014-06-10 22:00:55 +00:00
|
|
|
if( model._focused and !backdrop )
|
|
|
|
file ~= "-focused";
|
|
|
|
|
|
|
|
if( model._hover and !model._down )
|
|
|
|
{
|
|
|
|
file ~= "-hover";
|
|
|
|
me._bg.set("fill", me._style.getColor("button_bg_color_hover"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
me._bg.set("fill", me._style.getColor("button_bg_color"));
|
2013-07-27 09:54:41 +00:00
|
|
|
}
|
|
|
|
else
|
2014-06-10 22:00:55 +00:00
|
|
|
{
|
|
|
|
file ~= "-disabled";
|
|
|
|
me._bg.set("fill", me._style.getColor("button_bg_color_insensitive"));
|
|
|
|
}
|
2013-07-27 09:54:41 +00:00
|
|
|
|
2014-03-20 10:46:48 +00:00
|
|
|
me._border.set("src", file ~ ".png");
|
2013-07-27 09:54:41 +00:00
|
|
|
}
|
|
|
|
};
|
2014-03-19 22:20:09 +00:00
|
|
|
|
2014-06-08 22:37:14 +00:00
|
|
|
# A label
|
|
|
|
DefaultStyle.widgets.label = {
|
|
|
|
new: func(parent, cfg)
|
|
|
|
{
|
|
|
|
me._root = parent.createChild("group", "label");
|
|
|
|
},
|
2014-06-12 22:15:30 +00:00
|
|
|
setSize: func(model, w, h)
|
2014-06-08 22:37:14 +00:00
|
|
|
{
|
|
|
|
if( me['_bg'] != nil )
|
|
|
|
me._bg.reset().rect(0, 0, w, h);
|
2014-06-10 16:46:52 +00:00
|
|
|
if( me['_img'] != nil )
|
|
|
|
me._img.set("size[0]", w)
|
|
|
|
.set("size[1]", h);
|
2014-06-08 22:37:14 +00:00
|
|
|
if( me['_text'] != nil )
|
2014-06-12 22:15:30 +00:00
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
# TODO different alignment
|
2014-06-12 22:15:30 +00:00
|
|
|
me._text.setTranslation(2, 2 + h / 2);
|
|
|
|
me._text.set(
|
|
|
|
"max-width",
|
|
|
|
model._cfg.get("wordWrap", 0) ? (w - 4) : 0
|
|
|
|
);
|
|
|
|
}
|
2014-06-08 22:37:14 +00:00
|
|
|
return me;
|
|
|
|
},
|
|
|
|
setText: func(model, text)
|
|
|
|
{
|
|
|
|
if( text == nil or size(text) == 0 )
|
2014-06-12 22:15:30 +00:00
|
|
|
{
|
|
|
|
model.setHeightForWidthFunc(nil);
|
2014-06-08 22:37:14 +00:00
|
|
|
return me._deleteElement('text');
|
2014-06-12 22:15:30 +00:00
|
|
|
}
|
2014-06-08 22:37:14 +00:00
|
|
|
|
|
|
|
me._createElement("text", "text")
|
2014-06-10 16:46:52 +00:00
|
|
|
.set("text", text)
|
|
|
|
.set("fill", "black");
|
2014-06-08 22:37:14 +00:00
|
|
|
|
2014-06-12 22:15:30 +00:00
|
|
|
if( model._cfg.get("wordWrap", 0) )
|
|
|
|
{
|
|
|
|
var m = me;
|
|
|
|
model.setHeightForWidthFunc(func(w) m.heightForWidth(w));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# TODO get real font metrics
|
|
|
|
model.setMinimumSize([size(text) * 5 + 4, 14]);
|
|
|
|
model.setSizeHint([size(text) * 5 + 14, 24]);
|
|
|
|
}
|
2014-06-10 16:46:52 +00:00
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
setImage: func(model, img)
|
|
|
|
{
|
|
|
|
if( img == nil or size(img) == 0 )
|
|
|
|
return me._deleteElement('img');
|
|
|
|
|
|
|
|
me._createElement("img", "image")
|
|
|
|
.set("src", img)
|
|
|
|
.set("preserveAspectRatio", "xMidYMid slice");
|
2014-06-08 22:37:14 +00:00
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
# @param bg CSS color or 'none'
|
|
|
|
setBackground: func(model, bg)
|
|
|
|
{
|
|
|
|
if( bg == nil or bg == "none" )
|
|
|
|
return me._deleteElement("bg");
|
|
|
|
|
|
|
|
me._createElement("bg", "path")
|
|
|
|
.set("fill", bg);
|
|
|
|
|
2014-06-12 22:15:30 +00:00
|
|
|
me.setSize(model, model._size[0], model._size[1]);
|
2014-06-08 22:37:14 +00:00
|
|
|
return me;
|
|
|
|
},
|
2014-06-12 22:15:30 +00:00
|
|
|
heightForWidth: func(w)
|
|
|
|
{
|
|
|
|
if( me['_text'] == nil )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return math.max(14, me._text.heightForWidth(w - 4));
|
|
|
|
},
|
2014-06-08 22:37:14 +00:00
|
|
|
# protected:
|
|
|
|
_createElement: func(name, type)
|
|
|
|
{
|
|
|
|
var mem = '_' ~ name;
|
|
|
|
if( me[ mem ] == nil )
|
|
|
|
{
|
|
|
|
me[ mem ] = me._root.createChild(type, "label-" ~ name);
|
|
|
|
|
|
|
|
if( type == "text" )
|
|
|
|
{
|
|
|
|
me[ mem ].set("font", "LiberationFonts/LiberationSans-Regular.ttf")
|
|
|
|
.set("character-size", 14)
|
|
|
|
.set("alignment", "left-center");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return me[ mem ];
|
|
|
|
},
|
|
|
|
_deleteElement: func(name)
|
|
|
|
{
|
|
|
|
name = '_' ~ name;
|
|
|
|
if( me[ name ] != nil )
|
|
|
|
{
|
|
|
|
me[ name ].del();
|
|
|
|
me[ name ] = nil;
|
|
|
|
}
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-19 22:20:09 +00:00
|
|
|
# ScrollArea
|
|
|
|
DefaultStyle.widgets["scroll-area"] = {
|
|
|
|
new: func(parent, cfg)
|
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
me._root = parent.createChild("group", "scroll-area");
|
2014-03-19 22:20:09 +00:00
|
|
|
|
2014-06-08 22:37:14 +00:00
|
|
|
me._bg = me._root.createChild("path", "background")
|
|
|
|
.set("fill", "#e0e0e0");
|
|
|
|
me.content = me._root.createChild("group", "scroll-content")
|
|
|
|
.set("clip-frame", Element.PARENT);
|
|
|
|
me.vert = me._newScroll(me._root, "vert");
|
|
|
|
me.horiz = me._newScroll(me._root, "horiz");
|
2014-03-19 22:20:09 +00:00
|
|
|
},
|
2014-04-18 21:49:11 +00:00
|
|
|
setColorBackground: func
|
|
|
|
{
|
|
|
|
if( size(arg) == 1 )
|
|
|
|
var arg = arg[0];
|
|
|
|
me._bg.setColorFill(arg);
|
|
|
|
},
|
2014-06-08 22:37:14 +00:00
|
|
|
update: func(model)
|
2014-03-19 22:20:09 +00:00
|
|
|
{
|
|
|
|
me.horiz.reset();
|
2014-06-08 22:37:14 +00:00
|
|
|
if( model._max_scroll[0] > 1 )
|
2014-03-19 22:20:09 +00:00
|
|
|
# only show scroll bar if horizontally scrollable
|
2014-06-08 22:37:14 +00:00
|
|
|
me.horiz.moveTo(model._scroll_pos[0], model._size[1] - 2)
|
|
|
|
.horiz(model._size[0] - model._max_scroll[0]);
|
2014-03-19 22:20:09 +00:00
|
|
|
|
|
|
|
me.vert.reset();
|
2014-06-08 22:37:14 +00:00
|
|
|
if( model._max_scroll[1] > 1 )
|
2014-03-19 22:20:09 +00:00
|
|
|
# only show scroll bar if vertically scrollable
|
2014-06-08 22:37:14 +00:00
|
|
|
me.vert.moveTo(model._size[0] - 2, model._scroll_pos[1])
|
|
|
|
.vert(model._size[1] - model._max_scroll[1]);
|
2014-03-19 22:20:09 +00:00
|
|
|
|
|
|
|
me._bg.reset()
|
2014-06-08 22:37:14 +00:00
|
|
|
.rect(0, 0, model._size[0], model._size[1]);
|
2014-03-19 22:20:09 +00:00
|
|
|
me.content.set(
|
|
|
|
"clip",
|
2014-06-08 22:37:14 +00:00
|
|
|
"rect(0, " ~ model._size[0] ~ ", " ~ model._size[1] ~ ", 0)"
|
2014-03-19 22:20:09 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
# private:
|
|
|
|
_newScroll: func(el, orient)
|
|
|
|
{
|
|
|
|
return el.createChild("path", "scroll-" ~ orient)
|
|
|
|
.set("stroke", "#f07845")
|
|
|
|
.set("stroke-width", 4);
|
|
|
|
}
|
|
|
|
};
|