/* arclock.c - yet another clock using cairo with gtk+
 *
 * Copyright © 2005, Carl Worth
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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.
 */

#define WIDTH 800
#define HEIGHT 800 
#include <cairo.h>
#include <math.h>

#include <sys/time.h>

#include <gtk/gtk.h>
#include <cairo-xlib.h>
#include <gdk/gdkx.h>

#if ! GTK_CHECK_VERSION(2,7,0)
/* copied from gtk+/gdk/gdkcairo.c and gtk+/gdk/x11/gdkdrawable-x11.c
 * gdk_cairo_create() which is available in 2.7.0 and later.
 */
static cairo_t *
gdk_cairo_create (GdkDrawable *drawable)
{
    int width, height;
    cairo_t *cr = NULL;
    cairo_surface_t *surface = NULL;
    GdkVisual *visual = gdk_drawable_get_visual (drawable);
    GdkDrawable *dr;
    gint x_off, y_off;

    gdk_drawable_get_size (drawable, &width, &height);
    if (visual) {
	gdk_window_get_internal_paint_info(drawable, &dr, &x_off, &y_off);
	surface = cairo_xlib_surface_create (GDK_DRAWABLE_XDISPLAY (drawable),
					     GDK_DRAWABLE_XID (dr),
					     GDK_VISUAL_XVISUAL (visual),
					     width, height);
    }
    else {
	g_warning ("Using Cairo rendering requires the drawable argument to\n"
		   "have a specified colormap. All windows have a colormap,\n"
		   "however, pixmaps only have colormap by default if they\n"
		   "were created with a non-NULL window argument. Otherwise\n"
		   "a colormap must be set on them with "
		   "gdk_drawable_set_colormap");
	return NULL;
    }
    if (surface) {
	cr = cairo_create (surface);
	cairo_surface_destroy (surface);
    }
    return cr;
}
#endif

static void
draw (cairo_t *cr)
{
    struct timeval t;
    struct timezone tz;
    double angle;
    int i;

    cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
    cairo_set_line_width (cr, .05);
    cairo_set_source_rgb (cr, .5, .1, .1);
    for (i = 0; i < 12; i++) {
	cairo_move_to (cr, cos (i * M_PI / 6), sin (i * M_PI / 6));
	cairo_line_to (cr, (1.003)*cos (i * M_PI / 6), (1.003)*sin (i * M_PI / 6));
        cairo_stroke (cr);
    }

    gettimeofday (&t, &tz);
    angle = ((t.tv_sec + t.tv_usec / 1000000.0) / 60.0 - tz.tz_minuteswest) * (2*M_PI);

    cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
    cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);

    cairo_set_line_width (cr, .05);
    cairo_set_source_rgb (cr, .5, .5, .9);
    cairo_move_to (cr, .7*cos(angle/60/12), .7*sin(angle/60/12));
    cairo_line_to (cr, 0, 0);
    cairo_line_to (cr, cos(angle/60), sin(angle/60));
    cairo_stroke (cr);

    cairo_set_line_width (cr, .02);
    cairo_set_source_rgb (cr, .9, .1, .3);
    cairo_move_to (cr, cos(angle/60), sin(angle/60));
    cairo_arc_to (cr, 0, 0, 1.05*cos(angle), 1.05*sin(angle), 0.1);
    cairo_stroke (cr);
}

static gboolean
handle_expose (GtkWidget      *widget,
	       GdkEventExpose *event,
	       gpointer	       data)
{
    cairo_t *cr;
    cr = gdk_cairo_create (widget->window);

    cairo_translate (cr, 400, 400);
    cairo_scale (cr, 300, 300);
    cairo_rotate (cr, -M_PI_2);
    draw (cr);
    cairo_destroy (cr);
    return FALSE;
}

static void
tick (GtkWidget      *widget)
{
    GdkRectangle update_rect;

    update_rect.x = 0;
    update_rect.y = 0;
    update_rect.width = widget->allocation.width;
    update_rect.height = widget->allocation.height;

    gdk_window_invalidate_rect(widget->window,&update_rect,FALSE);
}

int 
main (int argc, char **argv)
{
    GtkWidget *window, *drawing_area;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    gtk_window_set_default_size (GTK_WINDOW (window), WIDTH, HEIGHT);
    gtk_window_set_title (GTK_WINDOW (window), "cairo demo");

    g_signal_connect (window, "destroy-event",
		      G_CALLBACK (gtk_main_quit), NULL);

    drawing_area = gtk_drawing_area_new ();
    gtk_container_add (GTK_CONTAINER (window), drawing_area);

    g_signal_connect (drawing_area, "expose-event",
		      G_CALLBACK (handle_expose), NULL);

    g_timeout_add (100, (GSourceFunc) tick, drawing_area);

    gtk_widget_show_all (window);

    gtk_main ();
    
    return 0;
}

