/*
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Keith Packard
 *
 * Contributor(s):
 *      Keith Packard <keithp@keithp.com>
 */

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;
		}
	}
}
