/*
 * Copyright © 2008 Keith Packard
 *
 * 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;
autoload Nichrome::Lozenge;

extend namespace Nichrome {
    public namespace Button {

	public typedef button_t;

	protected typedef struct {
	    real x, y;
	    real pad;
	    real width, height;
	    real preferred_width, preferred_height;
	} metrics_t;
	
	protected typedef void (&widget_t widget, bool state) callback_t;
    
	protected typedef metrics_t (cairo_t cr, &button_t widget) get_metrics_t;

	public typedef widget_t + struct {
	    string  label;
	    string  font;
	    bool    pressed;
	    bool    inside;
	    real    pad_ratio;
	    rgba_color_t    color;
	    rgba_color_t    text_color;
	    callback_t	callback;
	    get_metrics_t get_metrics;
	} button_t;
	
	protected metrics_t get_metrics (cairo_t cr, &button_t widget) {
	    set_font (cr, widget.font);
	    text_extents_t text = text_extents (cr, widget.label);
	    text_extents_t m = text_extents (cr, "M");
	    text_extents_t x = text_extents (cr, "x");
	    font_extents_t font = font_extents(cr);
	    metrics_t metrics;
	    
	    real pad = floor (widget.pad_ratio * -m.y_bearing + 0.5);
	    metrics.pad = pad;
	    metrics.preferred_height = floor (pad + m.height + pad + 1.5) & ~1;
	    metrics.preferred_width = floor (widget.pad_ratio * 2.5 * pad +
	    text.width + 0.5);
	    metrics.width = max (widget.geometry.width, metrics.preferred_width);
	    metrics.height = max (widget.geometry.height, metrics.preferred_height);
	    real text_height = (x.height + m.height) / 2;
	    real text_bearing = (x.y_bearing + m.y_bearing) / 2;
	    metrics.y = floor ((metrics.height - text_height) / 2 
			       - text_bearing + 0.5);
	    metrics.x = floor ((metrics.width - text.width) / 2 
			       - text.x_bearing + 0.5);
	    return metrics;
	}
	
	protected void natural (cairo_t cr, &button_t widget) {
	    metrics_t	metrics = widget.get_metrics (cr, &widget);

	    rectangle (cr, 0, 0, metrics.preferred_width, metrics.preferred_height);
	}
	
	protected void outline (cairo_t cr, &button_t widget) {
	    metrics_t	metrics = widget.get_metrics (cr, &widget);

	    save (cr);
	    translate (cr, floor (metrics.pad/2), floor (metrics.pad/2));
	    Lozenge::outline (cr, floor (metrics.width - metrics.pad),
			     floor (metrics.height - metrics.pad));
	    restore (cr);
	}
	
	protected void draw (cairo_t cr, &button_t widget) {
	    metrics_t	metrics = widget.get_metrics (cr, &widget);
	    real    highlight = -floor (metrics.height/20);

	    if (widget.pressed && widget.inside)
		highlight = -highlight;

	    save (cr);
	    translate (cr, floor (metrics.pad/2), floor (metrics.pad/2));
	    Lozenge::draw (cr, floor (metrics.width - metrics.pad),
			   floor (metrics.height - metrics.pad),
			   widget.color, highlight);
	    restore (cr);
	    real text_x = metrics.x + highlight/2;
	    real text_y = metrics.y + highlight/2;
	    
	    int    offset = floor (min (metrics.preferred_height,
					metrics.preferred_width) / 120 +
				   0.5);
	    if (offset > 0)
	    {
		rgba_color_t darker = Lozenge::illum (widget.text_color, -0.2);
		rgba_color_t lighter = Lozenge::illum (widget.text_color, 0.2);
		move_to (cr, text_x - offset, text_y - offset);
		set_source_rgba (cr, darker.red, darker.green,
				 darker.blue, darker.alpha);
		show_text (cr, widget.label);
		
		move_to (cr, text_x + offset, text_y + offset);
		set_source_rgba (cr, lighter.red, lighter.green,
				 lighter.blue, lighter.alpha);
		show_text (cr, widget.label);
	    }
	    move_to (cr, text_x, text_y);
	    set_source_rgba (cr, widget.text_color.red, widget.text_color.green,
			     widget.text_color.blue, widget.text_color.alpha);
	    show_text (cr, widget.label);
	}
	
	protected void relabel (&button_t widget, string label) {
	    widget.label = label;
	    Widget::resize (&widget);
	}

	protected void button (&button_t widget, &button_event_t event) {
	    enum switch (event.type) {
	    case press:
		if (event.button > 3)
		    break;
		widget.pressed = true;
		Widget::redraw (&widget);
		break;
	    case release:
		if (event.button > 3)
		    break;
		Widget::redraw (&widget);
		widget.pressed = false;
		if (widget.inside)
		    widget.callback (&widget, true);
		break;
	    default:
		break;
	    }
	}
	
	protected void motion (&button_t widget, &motion_event_t event) {
	    enum switch (event.type) {
	    case leaving:
		widget.inside = false;
		if (widget.pressed)
		    Widget::redraw (&widget);
		break;
	    case entering:
		widget.inside = true;
		if (widget.pressed)
		    Widget::redraw (&widget);
		break;
	    case inside:
	    case outside:
		break;
	    }
	}

	protected void print(&button_t widget, int indent) {
	    do_indent(indent);
	    printf("button %s %v\n", widget.label, widget.geometry);
	}

	protected void init (&button_t widget,
			     &nichrome_t nichrome,
			     string label,
			     callback_t callback) {
	    Widget::init (&nichrome, &widget);
	    widget.label = label;
	    widget.font = Widget::default_font;
	    widget.draw = draw;
	    widget.pressed = false;
	    widget.inside = false;
	    widget.button = button;
	    widget.motion = motion;
	    widget.callback = callback;
	    widget.outline = outline;
	    widget.natural = natural;
	    widget.get_metrics = get_metrics;
	    widget.pad_ratio = 1;
	    widget.color = Widget::default_color;
	    widget.text_color = (rgba_color_t) {
		red = 0, green = 0, blue = 0, alpha = 1
	    };
	    widget.print = print;
	}
	
	protected *widget_t new (&nichrome_t nichrome,
				 string label,
				 callback_t callback) {
	    button_t	widget;

	    init (&widget, &nichrome, label, callback);
	    return &widget;
	}
    }
}
