[cairo-commit] cairo-demo/gtk .cvsignore,NONE,1.1 Makefile,NONE,1.1 cairo-gtk-demo.c,NONE,1.1 cairo-widget.c,NONE,1.1 cairo-widget.h,NONE,1.1

Carl Worth commit at pdx.freedesktop.org
Tue Nov 4 12:23:31 PST 2003


Committed by: cworth

Update of /cvs/cairo/cairo-demo/gtk
In directory pdx:/tmp/cvs-serv15307

Added Files:
	.cvsignore Makefile cairo-gtk-demo.c cairo-widget.c 
	cairo-widget.h 
Log Message:
Initial import of cairo-gtk-demo program by Amaury Jacquot

--- NEW FILE: .cvsignore ---
cairo-gtk-demo

--- NEW FILE: Makefile ---
DEMOS=cairo-gtk-demo

CFLAGS=-g -Wall -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls `pkg-config --cflags cairo gtk+-2.0`
LDFLAGS=`pkg-config --libs cairo gtk+-2.0`

all: ${DEMOS}

cairo-gtk-demo: cairo-widget.o

write_png.o: cairo-widget.c cairo-widget.h

clean:
	rm -f ${DEMOS} *.o

--- NEW FILE: cairo-gtk-demo.c ---
/* 
 * cairo-gtk-demo: A simple demo showing how to draw a Gtk+ widget with cairo.
 *
 * Copyright (C) 2002, Amaury Jacquot
 *
 * Amaury Jacquot <sxpert at esitcom.org>
 * Carl Worth <cworth at isi.edu>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#include "cairo-widget.h"

typedef struct _CairoGtkDemo {
    GtkWidget *window;
    CairoWidget *cairo_widget;
} CairoGtkDemo;

static void
destroy (GtkWidget* widget, gpointer data)
{
    CairoGtkDemo *demo = data;

    demo->window = NULL;
    gtk_main_quit ();
}

static void
key_press (GtkWidget *widget, GdkEventKey *event, CairoGtkDemo *demo)
{
    switch (event->keyval) {
    case GDK_Q:
    case GDK_q:
	gtk_main_quit ();
	break;
    default:
	cairo_widget_randomize (demo->cairo_widget);
	break;
    }
}

int
main (int argc,char* argv[])
{
    CairoGtkDemo demo;

    gtk_init (&argc, &argv);

    demo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    demo.cairo_widget = CAIRO_WIDGET (cairo_widget_new ());

    g_signal_connect (G_OBJECT (demo.window), "destroy",
		      G_CALLBACK (destroy), &demo);

    g_signal_connect (G_OBJECT (demo.window), "key_press_event",
		      G_CALLBACK (key_press), &demo);
	
    gtk_container_add (GTK_CONTAINER (demo.window), GTK_WIDGET (demo.cairo_widget));
    
    gtk_widget_show_all (demo.window);
	
    gtk_main();

    return 0;
}

--- NEW FILE: cairo-widget.c ---
/* 
 * cairo-widget: A simple demo showing how to draw a Gtk+ widget with cairo.
 *
 * Copyright (C) 2002, Amaury Jacquot
 *
 * Amaury Jacquot <sxpert at esitcom.org>
 * Carl Worth <cworth at isi.edu>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

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

#include "cairo-widget.h"

static GtkWidgetClass* parent_class = NULL;

static void
cairo_widget_class_init (CairoWidgetClass* class);

static void
cairo_widget_init (CairoWidget* ge);

static void
cairo_widget_realize (GtkWidget* widget);

static void
cairo_widget_size_allocate (GtkWidget* widget, GtkAllocation* allocation);

static gint
cairo_widget_expose (GtkWidget* widget, GdkEventExpose* event);

GtkType
cairo_widget_get_type (void)
{
    static guint cairo_widget_type = 0;
    
    if (!cairo_widget_type) {
	GtkTypeInfo cairo_widget_info = {
	    "CairoWidget",
	    sizeof(CairoWidget),
	    sizeof(CairoWidgetClass),
	    (GtkClassInitFunc) cairo_widget_class_init,
	    (GtkObjectInitFunc) cairo_widget_init,
	    NULL, NULL, NULL
	};
	cairo_widget_type = gtk_type_unique (GTK_TYPE_WIDGET, &cairo_widget_info);
    }
    return cairo_widget_type;
}

GtkWidget*
cairo_widget_new (void)
{
    CairoWidget* cw;
    GtkWidget*   widget;
    
    cw = gtk_type_new (cairo_widget_get_type ());
    widget = GTK_WIDGET (cw);
    widget->style = gtk_style_new ();

    return widget;
}

static void
cairo_widget_class_init (CairoWidgetClass* class)
{
    GtkObjectClass* object_class;
    GtkWidgetClass* widget_class;
	
    object_class = (GtkObjectClass*) class;
    widget_class = (GtkWidgetClass*) class;
    parent_class = gtk_type_class (gtk_widget_get_type ());

    widget_class->realize = cairo_widget_realize;
    widget_class->size_allocate = cairo_widget_size_allocate;
    widget_class->expose_event = cairo_widget_expose;
}

static void
cairo_widget_set_size (CairoWidget *cw, int width, int height)
{
    cw->width = width;
    cw->height = height;
}

void
cairo_widget_randomize (CairoWidget *cw)
{
    CairoWidgetRectangle *r;
    int i;

    for (i = 0; i < CAIRO_WIDGET_NUM_RECTS; i++) {
	r = &cw->rects[i];
	r->x1 = (int) (cw->width * (rand()/(RAND_MAX+1.0)));
	r->x2 = (int) (cw->width * (rand()/(RAND_MAX+1.0)));
	r->y1 = (int) (cw->height * (rand()/(RAND_MAX+1.0)));
	r->y2 = (int) (cw->height * (rand()/(RAND_MAX+1.0)));
	r->red = rand()/(RAND_MAX+1.0);
	r->green = rand()/(RAND_MAX+1.0);
	r->blue = rand()/(RAND_MAX+1.0);
    }

    gtk_widget_queue_draw (GTK_WIDGET (cw));
}

static void
cairo_widget_init (CairoWidget* cw)
{

}

static void
cairo_widget_realize (GtkWidget* widget)
{
    CairoWidget *cw;
    GdkWindowAttr attributes;
    gint attributes_mask;
    GdkColor black = {0,0,0,0};
    
    g_return_if_fail (widget != NULL);
    g_return_if_fail (IS_CAIRO_WIDGET (widget));

    cw = CAIRO_WIDGET (widget);
    GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
    
    attributes.x = widget->allocation.x;
    attributes.y = widget->allocation.y;
    attributes.width = widget->allocation.width;
    attributes.height = widget->allocation.height;
    
    attributes.wclass = GDK_INPUT_OUTPUT;
    attributes.window_type = GDK_WINDOW_CHILD;
    attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
    attributes.visual = gtk_widget_get_visual (widget);
    attributes.colormap = gtk_widget_get_colormap (widget);
    
    attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
    widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
    
    widget->style = gtk_style_attach (widget->style, widget->window);
    
    gdk_window_set_user_data (widget->window, widget);
    gdk_window_set_background (widget->window, &black);

    cairo_widget_set_size (cw, widget->allocation.width, widget->allocation.height);
    cairo_widget_randomize (cw);
}

static void
cairo_widget_size_allocate (GtkWidget* widget, GtkAllocation* allocation)
{
    CairoWidget *cw;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (IS_CAIRO_WIDGET(widget));
    g_return_if_fail (allocation != NULL);

    cw = CAIRO_WIDGET (widget);
    
    widget->allocation = *allocation;
    if (GTK_WIDGET_REALIZED (widget)) {
	gdk_window_move_resize (widget->window,
				allocation->x, allocation->y,
				allocation->width, allocation->height);
    }

    cairo_widget_set_size (cw, widget->allocation.width, widget->allocation.height);
    cairo_widget_randomize (cw);
}

static void
cairo_gdk (cairo_t* ct, GdkDrawable* window)
{
    Display* dpy;
    Drawable drawable;
    GdkDrawable* real_drawable;
    gint x_off, y_off;
    
    if (GDK_IS_WINDOW (window)) {
	gdk_window_get_internal_paint_info (window, &real_drawable, &x_off, &y_off);
	dpy = gdk_x11_drawable_get_xdisplay (real_drawable);
	drawable = gdk_x11_drawable_get_xid (real_drawable);
    } else {
	dpy = gdk_x11_drawable_get_xdisplay (window);
	drawable = gdk_x11_drawable_get_xid (window);
    }
    cairo_set_target_drawable (ct, dpy, drawable);
    
    if (GDK_IS_WINDOW (window)) cairo_translate (ct, -(double)x_off,-(double)y_off);
}

static gint
cairo_widget_expose (GtkWidget* widget, GdkEventExpose* event)
{
    CairoWidget* cw;
    cairo_t *cr;

    int i;
    
    g_return_val_if_fail (widget!=NULL, FALSE);
    g_return_val_if_fail (IS_CAIRO_WIDGET (widget), FALSE);
    cw = CAIRO_WIDGET (widget);

    cr = cairo_create ();
    
    cairo_gdk (cr, widget->window);
    
    cairo_save (cr);
    cairo_set_rgb_color (cr, 159.0/256.0, 114.0/256.0, 211.0/256.0);
    cairo_select_font (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
    cairo_scale_font (cr, 20);
    cairo_move_to (cr, 10, 10);
    cairo_rotate (cr, M_PI/4);
    cairo_show_text (cr, "Hello cairo/Gtk+ world!");
    cairo_restore (cr);

    cairo_set_alpha (cr, 0.5);
    for (i = 0; i < CAIRO_WIDGET_NUM_RECTS; i++) {
	CairoWidgetRectangle *r = &cw->rects[i];
	cairo_set_rgb_color (cr, r->red, r->green, r->blue);

	cairo_rectangle (cr,
			 r->x1, r->y1,
			 r->x2 - r->x1,
			 r->y2 - r->y1);
	cairo_fill (cr);
    }
    
    cairo_destroy(cr);
    
    return FALSE;
}

--- NEW FILE: cairo-widget.h ---
/* 
 * cairo-widget: A simple demo showing how to draw a Gtk+ widget with cairo.
 *
 * Copyright (C) 2002, Amaury Jacquot
 *
 * Amaury Jacquot <sxpert at esitcom.org>
 * Carl Worth <cworth at isi.edu>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#ifndef _cairo_widget_h_
#define _cairo_widget_h_

#include <gdk/gdk.h>
#include <gtk/gtkwidget.h>

#define CAIRO_WIDGET(obj)		GTK_CHECK_CAST (obj, cairo_widget_get_type(), CairoWidget)
#define CAIRO_WIDGET_CLASS(klass)	GTK_CHECK_CLASS_CAST	(klass, cairo_widget_get_type(), CairoWidgetClass)
#define IS_CAIRO_WIDGET(obj)		GTK_CHECK_TYPE			(obj, cairo_widget_get_type())

typedef struct _CairoWidget	CairoWidget;
typedef struct _CairoWidgetClass CairoWidgetClass;

typedef struct _CairoWidgetRectangle {
    int x1, y1;
    int x2, y2;
    double red, green, blue;
} CairoWidgetRectangle;

#define CAIRO_WIDGET_NUM_RECTS 10

struct _CairoWidget {
    GtkWidget widget;
    int width, height;
    CairoWidgetRectangle rects[CAIRO_WIDGET_NUM_RECTS];
};

struct _CairoWidgetClass {
    GtkWidgetClass parent_class;
};

GtkType
cairo_widget_get_type (void);

GtkWidget *
cairo_widget_new (void);

void
cairo_widget_randomize (CairoWidget *cw);

#endif





More information about the cairo-commit mailing list