|
该实例来源于《GTK+ / Gnome Application Development》一书,由于该书成书较早,一些代码已经不大适合于与现在的GTK+ API,所以我做了一些更改,以适用于现在的GTK+。代码在windows mingw gcc 3.4.5编译通过。gnomecanvas的运行以及开发环境可以在http://ftp.gnome.org/pub/GNOME/binaries/win32/获得。编译采用gcc main.c moduleimp.c -o canvas `pkg-config --cflags --libs libgnomecanvas-2.0`
文件main.c- #include <gtk/gtk.h>
- #include <libgnomecanvas/libgnomecanvas.h>
- extern void create_canvas_items( GtkWidget *canvas);
- int main( int argc, char *argv[])
- {
- gtk_init( &argc, &argv);
-
- GtkWidget *window, *sw, *canvas;
- window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_title(GTK_WINDOW(window), "Canvas Example");
- g_signal_connect(window,
- "delete_event",
- G_CALLBACK(gtk_main_quit),
- NULL);
- sw = gtk_scrolled_window_new(NULL, NULL);
- gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
- GTK_POLICY_AUTOMATIC,
- GTK_POLICY_AUTOMATIC);
- canvas = gnome_canvas_new_aa();
- gnome_canvas_set_scroll_region(GNOME_CANVAS(canvas), 0, 0, 600, 450);
- create_canvas_items(canvas);
- gtk_container_add(GTK_CONTAINER(sw), canvas);
- gtk_container_add(GTK_CONTAINER(window), sw);
- gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);
-
-
- gtk_widget_show_all( window);
- gtk_main();
- return 0;
- }
复制代码 文件moduleimp.c- #include <gtk/gtk.h>
- #include <libgnomecanvas/libgnomecanvas.h>
- static gboolean
- cb_item_event( GnomeCanvasItem *item, GdkEvent *event, gpointer data)
- {
- static double x, y;
- static int dragging;
- double new_x, new_y;
- GdkCursor *fleur;
- double item_x, item_y;
- item_x = event->button.x;
- item_y = event->button.y;
- gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
- switch (event->type)
- {
- case GDK_BUTTON_PRESS:
- switch(event->button.button)
- {
- case 1:
- if (event->button.state & GDK_SHIFT_MASK)
- {
- gtk_object_destroy(GTK_OBJECT(item));
- }
- else
- {
- x = item_x;
- y = item_y;
-
- fleur = gdk_cursor_new(GDK_FLEUR);
- gnome_canvas_item_grab(item,
- GDK_POINTER_MOTION_MASK |
- GDK_BUTTON_RELEASE_MASK,
- fleur,
- event->button.time);
- gdk_cursor_destroy( fleur);
- dragging = TRUE;
- }
- break;
- default:
- break;
- }
- break;
- case GDK_MOTION_NOTIFY:
- if (dragging && (event->motion.state & GDK_BUTTON1_MASK))
- {
- new_x = item_x;
- new_y = item_y;
-
- gnome_canvas_item_move(item, new_x - x, new_y - y);
- x = new_x;
- y = new_y;
- }
- break;
-
- case GDK_BUTTON_RELEASE:
- gnome_canvas_item_ungrab(item, event->button.time);
- dragging = FALSE;
- break;
-
- default:
- break;
- }
- return FALSE;
- }
- void
- create_canvas_items( GtkWidget *canvas)
- {
- GnomeCanvasPoints* points;
- GnomeCanvasGroup* group;
- GnomeCanvasItem* item;
- double affine[6];
- group = gnome_canvas_root(GNOME_CANVAS(canvas));
- /* A polygon */
- points = gnome_canvas_points_new(14);
- points->coords[0] = 270.0;
- points->coords[1] = 330.0;
- points->coords[2] = 270.0;
- points->coords[3] = 430.0;
- points->coords[4] = 390.0;
- points->coords[5] = 430.0;
- points->coords[6] = 390.0;
- points->coords[7] = 330.0;
- points->coords[8] = 310.0;
- points->coords[9] = 330.0;
- points->coords[10] = 310.0;
- points->coords[11] = 390.0;
- points->coords[12] = 350.0;
- points->coords[13] = 390.0;
- points->coords[14] = 350.0;
- points->coords[15] = 370.0;
- points->coords[16] = 330.0;
- points->coords[17] = 370.0;
- points->coords[18] = 330.0;
- points->coords[19] = 350.0;
- points->coords[20] = 370.0;
- points->coords[21] = 350.0;
- points->coords[22] = 370.0;
- points->coords[23] = 410.0;
- points->coords[24] = 290.0;
- points->coords[25] = 410.0;
- points->coords[26] = 290.0;
- points->coords[27] = 330.0;
- item = gnome_canvas_item_new(group,
- gnome_canvas_polygon_get_type (),
- "points", points,
- "fill_color", "tan",
- "outline_color", "black",
- "width_units", 3.0,
- NULL);
- g_signal_connect( item, "event", G_CALLBACK(cb_item_event), NULL);
- gnome_canvas_points_unref(points);
- /* A translucent rectangle */
- item = gnome_canvas_item_new (group,
- gnome_canvas_rect_get_type(),
- "x1", 90.0,
- "y1", 40.0,
- "x2", 180.0,
- "y2", 100.0,
- "fill_color_rgba", 0x3cb37180,
- "outline_color", "black",
- "width_units", 4.0,
- NULL);
- g_signal_connect( item, "event", G_CALLBACK(cb_item_event), NULL);
- /* A translucent ellipse */
- item = gnome_canvas_item_new (group,
- gnome_canvas_ellipse_get_type(),
- "x1", 210.0,
- "y1", 80.0,
- "x2", 280.0,
- "y2", 140.0,
- "fill_color_rgba", 0x5f9ea080,
- "outline_color", "black",
- "width_pixels", 0,
- NULL);
- g_signal_connect( item, "event", G_CALLBACK(cb_item_event), NULL);
-
- /* Create ellipses arranged in a line; they're manipulated as a
- single item. */
- group = GNOME_CANVAS_GROUP (gnome_canvas_item_new (group,
- gnome_canvas_group_get_type(),
- "x", 0.0,
- "y", 0.0,
- NULL));
- g_signal_connect( group, "event", G_CALLBACK(cb_item_event), NULL);
- {
- double xpos = 20.0;
- while (xpos < 300.0)
- {
- gnome_canvas_item_new(group,
- gnome_canvas_ellipse_get_type(),
- "x1", xpos,
- "y1", 100.0,
- "x2", xpos + 10.0,
- "y2", 110.0,
- "fill_color_rgba", 0x0000FFFF,
- "outline_color_rgba", 0xFF,
- NULL);
- xpos += 15.0;
- }
- }
- }
复制代码 |
|