diff options
author | Keith Packard <keithp@keithp.com> | 2012-03-19 11:30:59 -0700 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2012-03-19 11:30:59 -0700 |
commit | 853715a6e4a96131be3eb7b7be940965e8e51a99 (patch) | |
tree | 7b744daff6e02445acd1726afb70b6a691ef84d8 | |
parent | 54c1aa254250cb73892ae823d6e5b23cd0f4c9b1 (diff) |
Add Border widget
Draws an outline around another widget
Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r-- | nichrome/nichrome-border.5c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/nichrome/nichrome-border.5c b/nichrome/nichrome-border.5c new file mode 100644 index 0000000..5888b11 --- /dev/null +++ b/nichrome/nichrome-border.5c @@ -0,0 +1,124 @@ +/* + * Copyright © 2012 Keith Packard <keithp@keithp.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +autoload Nichrome; + +extend namespace Nichrome { + public namespace Border { + + import Cairo; + + protected typedef widget_t + struct { + real left, right, top, bottom; + rgba_color_t color; + *widget_t child; + } border_t; + + protected void natural (cairo_t cr, *border_t widget) { + save(cr); + translate(cr, widget->left, widget->top); + widget->child->natural(cr, widget->child); + restore(cr); + rect_t r = Cairo::fill_extents(cr); + new_path(cr); + rectangle (cr, r.x - widget->left, r.y -widget-> top, + r.width + widget->left + widget->right, + r.height + widget->top + widget->bottom); + } + + protected void outline (cairo_t cr, *border_t widget) { + rectangle (cr, 0, 0, 0, 0); + } + + protected void draw (cairo_t cr, *border_t widget) { + save(cr); + set_fill_rule(cr, fill_rule_t.EVEN_ODD); + set_source_rgba (cr, widget->color.red, widget->color.green, + widget->color.blue, widget->color.alpha); + rectangle (cr, 0, 0, widget->geometry.width, widget->geometry.height); + rectangle (cr, + widget->left, + widget->top, + widget->geometry.width - widget->right - widget->left, + widget->geometry.height - widget->bottom - widget->top); + fill (cr); + restore(cr); + } + + protected void configure (*border_t widget, rect_t geometry) { + widget->geometry = geometry; + widget->child->configure (widget->child, (rect_t) { + .x = geometry.x + widget->left, + .y = geometry.y + widget->top, + .width = geometry.width - widget->left - widget->right, + .height = geometry.height - widget->top - widget->bottom + }); + } + + protected void remove (*border_t widget) { + widget->child->remove (widget->child); + Widget::remove(widget); + } + + protected void print (*border_t widget, int indent) { + do_indent(indent); + printf ("border %d %d %d %d\n", widget->left, widget->right, widget->top, widget->bottom); + widget->child->print (widget->child, indent + 8); + } + + protected void init (*border_t widget, *widget_t child, real args ...) { + Widget::init(&child->nichrome, widget); + widget->child = child; + + switch (dim(args)) { + case 0: + widget->left = widget->top = widget->right = widget->bottom = 1; + break; + case 1: + widget->left = widget->top = widget->right = widget->bottom = args[0]; + break; + case 2: + case 3: + widget->left = widget->right = args[0]; + widget->top = widget->bottom = args[1]; + break; + case 4: + widget->left = args[0]; + widget->right = args[1]; + widget->top = args[2]; + widget->bottom = args[3]; + break; + } + widget->draw = draw; + widget->outline = outline; + widget->natural = natural; + widget->configure = configure; + widget->remove = remove; + widget->print = print; + widget->color = (rgba_color_t) { + red = 0, green = 0, blue = 0, alpha = 1 + }; + } + + protected *widget_t new (*widget_t child, real args...) { + *border_t widget = &(border_t) {}; + + init(widget, child, args...); + return widget; + } + } +} |