From f8d083966703a2b0efb6db1782a14a1a0b5d56f4 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 30 Nov 2006 22:19:08 -0800 Subject: [PATCH] Add shell for rack-fancy (graphical version of rack) So far, this is basically just a cleaned up version of simple-demo from goocanvas/demo. --- Makefile | 11 +++-- demo-item.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ demo-item.h | 47 ++++++++++++++++++ rack-fancy.c | 100 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 287 insertions(+), 3 deletions(-) create mode 100644 demo-item.c create mode 100644 demo-item.h create mode 100644 rack-fancy.c diff --git a/Makefile b/Makefile index 79c066c..6faae15 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,23 @@ -WGCFLAGS=-Wall -Wextra -Wmissing-prototypes -Wno-unused-parameter -Wno-sign-compare +WGCFLAGS=-Wall -Wextra -Wmissing-prototypes -Wno-unused-parameter -Wno-sign-compare `pkg-config --cflags goocanvas` +FANCYLIBS=`pkg-config --libs goocanvas` -PROGRAMS=grid4 grid5 drill2 rack +PROGRAMS=grid4 grid5 drill2 rack rack-fancy all: $(PROGRAMS) LIBRARY=dict.o grid.o word-game.o +FANCYLIBRARY=demo-item.o %: %.o $(LIBRARY) $(CC) $(CFLAGS) $(WGCFLAGS) $(LDFLAGS) -lreadline -lm -o $@ $^ +%-fancy: %-fancy.o $(LIBRARY) $(FANCYLIBRARY) + $(CC) $(CFLAGS) $(WGCFLAGS) $(LDFLAGS) $(FANCYLIBS) -lreadline -lm -o $@ $^ + %.o: %.c $(CC) $(CFLAGS) $(WGCFLAGS) -c -o $@ $< Makefile.dep: *.c - $(CC) -M $(CPPFLAGS) $^ > $@ + $(CC) -M $(CPPFLAGS) $(WGCFLAGS) $^ > $@ -include Makefile.dep .PHONY: clean diff --git a/demo-item.c b/demo-item.c new file mode 100644 index 0000000..2eaf830 --- /dev/null +++ b/demo-item.c @@ -0,0 +1,132 @@ +/* + * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin. + * Released under the GNU LGPL license. See COPYING for details. + * + * demo-item.c - a simple demo item. + */ +#include "goocanvas.h" +#include "demo-item.h" + +/* Use the GLib convenience macro to define the type. GooDemoItem is the + class struct, goo_demo_item is the function prefix, and our class is a + subclass of GOO_TYPE_CANVAS_ITEM_SIMPLE. */ +G_DEFINE_TYPE (GooDemoItem, goo_demo_item, GOO_TYPE_CANVAS_ITEM_SIMPLE) + +/* The standard object initialization function. */ + static void +goo_demo_item_init (GooDemoItem *demo_item) +{ + demo_item->x = 0.0; + demo_item->y = 0.0; + demo_item->width = 0.0; + demo_item->height = 0.0; +} + +/* The convenience function to create new items. This should start with a + parent argument and end with a variable list of object properties to fit + in with the standard canvas items. */ +GooCanvasItem* +goo_demo_item_new (GooCanvasItem *parent, + gdouble x, + gdouble y, + gdouble width, + gdouble height, + ...) +{ + GooCanvasItem *item; + GooDemoItem *demo_item; + const char *first_property; + va_list var_args; + + item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL); + + demo_item = (GooDemoItem*) item; + demo_item->x = x; + demo_item->y = y; + demo_item->width = width; + demo_item->height = height; + + va_start (var_args, height); + first_property = va_arg (var_args, char*); + if (first_property) + g_object_set_valist ((GObject*) item, first_property, var_args); + va_end (var_args); + + if (parent) + { + goo_canvas_item_add_child (parent, item, -1); + g_object_unref (item); + } + + return item; +} + +/* The update method. This is called when the canvas is initially shown and + also whenever the object is updated and needs to change its size and/or + shape. It should calculate its new bounds, storing them in simple->bounds, + and it should convert these to device coordinates. */ +static void +goo_demo_item_update (GooCanvasItemSimple *simple, + cairo_t *cr) +{ + GooDemoItem *demo_item = (GooDemoItem*) simple; + + /* Compute the new bounds. */ + simple->bounds.x1 = demo_item->x; + simple->bounds.y1 = demo_item->y; + simple->bounds.x2 = demo_item->x + demo_item->width; + simple->bounds.y2 = demo_item->y + demo_item->height; + + /* Convert to device coordinates. */ + goo_canvas_item_simple_user_bounds_to_device (simple, cr, &simple->bounds); +} + +/* The paint method. This should draw the item on the given cairo_t, using + the item's own coordinate space. */ +static void +goo_demo_item_paint (GooCanvasItemSimple *simple, + cairo_t *cr, + GooCanvasBounds *bounds) +{ + GooDemoItem *demo_item = (GooDemoItem*) simple; + + cairo_move_to (cr, demo_item->x, demo_item->y); + cairo_line_to (cr, demo_item->x, demo_item->y + demo_item->height); + cairo_line_to (cr, demo_item->x + demo_item->width, + demo_item->y + demo_item->height); + cairo_line_to (cr, demo_item->x + demo_item->width, demo_item->y); + cairo_close_path (cr); + goo_canvas_style_set_fill_options (simple->simple_data->style, cr); + cairo_fill (cr); +} + +/* Hit detection. This should check if the given coordinate (in the item's + coordinate space) is within the item. If it is it should return the item, + otherwise it should return NULL. */ +static GooCanvasItem* +goo_demo_item_get_item_at (GooCanvasItemSimple *simple, + gdouble x, + gdouble y, + cairo_t *cr, + gboolean is_pointer_event) +{ + GooDemoItem *demo_item = (GooDemoItem*) simple; + + if (x < demo_item->x || (x > demo_item->x + demo_item->width) + || y < demo_item->y || (y > demo_item->y + demo_item->height)) + return NULL; + + return (GooCanvasItem*) simple; +} + +/* The class initialization function. Here we set the class' update(), paint() + and get_item_at() methods. */ +static void +goo_demo_item_class_init (GooDemoItemClass *klass) +{ + GooCanvasItemSimpleClass *simple_class = (GooCanvasItemSimpleClass*) klass; + + simple_class->update = goo_demo_item_update; + simple_class->paint = goo_demo_item_paint; + simple_class->get_item_at = goo_demo_item_get_item_at; +} diff --git a/demo-item.h b/demo-item.h new file mode 100644 index 0000000..81ad4b0 --- /dev/null +++ b/demo-item.h @@ -0,0 +1,47 @@ +/* + * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin. + * Released under the GNU LGPL license. See COPYING for details. + * + * demo-item.c - a simple demo item. + */ +#ifndef __GOO_DEMO_ITEM_H__ +#define __GOO_DEMO_ITEM_H__ + +#include +#include "goocanvasitemsimple.h" + +G_BEGIN_DECLS + +#define GOO_TYPE_DEMO_ITEM (goo_demo_item_get_type ()) +#define GOO_DEMO_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GOO_TYPE_DEMO_ITEM, GooDemoItem)) +#define GOO_DEMO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GOO_TYPE_DEMO_ITEM, GooDemoItemClass)) +#define GOO_IS_DEMO_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GOO_TYPE_DEMO_ITEM)) +#define GOO_IS_DEMO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GOO_TYPE_DEMO_ITEM)) +#define GOO_DEMO_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GOO_TYPE_DEMO_ITEM, GooDemoItemClass)) + +typedef struct _GooDemoItem GooDemoItem; +typedef struct _GooDemoItemClass GooDemoItemClass; + +struct _GooDemoItem +{ + GooCanvasItemSimple parent_object; + + gdouble x, y, width, height; +}; + +struct _GooDemoItemClass +{ + GooCanvasItemSimpleClass parent_class; +}; + +GType goo_demo_item_get_type (void) G_GNUC_CONST; + +GooCanvasItem* goo_demo_item_new (GooCanvasItem *parent, + gdouble x, + gdouble y, + gdouble width, + gdouble height, + ...); +G_END_DECLS + +#endif /* __GOO_DEMO_ITEM_H__ */ diff --git a/rack-fancy.c b/rack-fancy.c new file mode 100644 index 0000000..38c1253 --- /dev/null +++ b/rack-fancy.c @@ -0,0 +1,100 @@ +/* + * Copyright © 2006 Carl Worth + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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." + */ +#include +#include +#include "demo-item.h" + +static gboolean +on_delete_event (GtkWidget *window, + GdkEvent *event, + gpointer unused_data) +{ + exit (0); +} + +static GtkWidget * +create_window (void) +{ + GtkWidget *window, *scrolled_window; + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW (window), 500, 500); + gtk_widget_show (window); + g_signal_connect (window, "delete_event", + (GtkSignalFunc) on_delete_event, NULL); + + scrolled_window = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), + GTK_SHADOW_IN); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_widget_show (scrolled_window); + gtk_container_add (GTK_CONTAINER (window), scrolled_window); + + return scrolled_window; +} + +static gboolean +on_button_press (GooCanvasItem *item, + GooCanvasItem *target, + GdkEventButton *event, + gpointer data) +{ + g_print ("demo item received button press event\n"); + return TRUE; +} + +static void +create_canvas (GtkWidget *parent) +{ + GtkWidget *canvas; + GooCanvasItem *root, *rect; + + canvas = goo_canvas_new (); + gtk_widget_set_size_request (canvas, 400, 400); + goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400); + gtk_widget_show (canvas); + gtk_container_add (GTK_CONTAINER (parent), canvas); + + root = goo_canvas_get_root_item (GOO_CANVAS (canvas)); + + rect = goo_demo_item_new (root, 30, 20, 50, 30, + "fill-color", "purple", + NULL); + + g_signal_connect (rect, "button_press_event", + (GtkSignalFunc) on_button_press, NULL); + +} + +int +main (int argc, char *argv[]) +{ + GtkWidget *window; + + gtk_init (&argc, &argv); + + window = create_window (); + + create_canvas (window); + + gtk_main (); + + return 0; +} -- 2.43.0