4
#include "canvas_demo.h"
9
#define DEFAULT_WIDTH 2
10
#define DEFAULT_SHAPE_A 8
11
#define DEFAULT_SHAPE_B 10
12
#define DEFAULT_SHAPE_C 3
16
set_dimension (GnomeCanvas *canvas, char *arrow_name, char *text_name,
17
double x1, double y1, double x2, double y2, double tx, double ty, int dim)
19
GnomeCanvasPoints *points;
22
points = gnome_canvas_points_new (2);
23
points->coords[0] = x1;
24
points->coords[1] = y1;
25
points->coords[2] = x2;
26
points->coords[3] = y2;
28
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), arrow_name),
31
sprintf (buf, "%d", dim);
32
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), text_name),
38
gnome_canvas_points_free (points);
42
move_drag_box (GnomeCanvasItem *item, double x, double y)
44
gnome_canvas_item_set (item,
53
set_arrow_shape (GnomeCanvas *canvas)
56
int shape_a, shape_b, shape_c;
57
GnomeCanvasPoints *points;
60
width = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (canvas), "width"));
61
shape_a = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (canvas), "shape_a"));
62
shape_b = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (canvas), "shape_b"));
63
shape_c = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (canvas), "shape_c"));
67
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "big_arrow"),
68
"width_pixels", 10 * width,
69
"arrow_shape_a", (double) (shape_a * 10),
70
"arrow_shape_b", (double) (shape_b * 10),
71
"arrow_shape_c", (double) (shape_c * 10),
76
points = gnome_canvas_points_new (5);
77
points->coords[0] = RIGHT - 10 * shape_a;
78
points->coords[1] = MIDDLE;
79
points->coords[2] = RIGHT - 10 * shape_b;
80
points->coords[3] = MIDDLE - 10 * (shape_c + width / 2.0);
81
points->coords[4] = RIGHT;
82
points->coords[5] = MIDDLE;
83
points->coords[6] = points->coords[2];
84
points->coords[7] = MIDDLE + 10 * (shape_c + width / 2.0);
85
points->coords[8] = points->coords[0];
86
points->coords[9] = points->coords[1];
87
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "outline"),
90
gnome_canvas_points_free (points);
94
move_drag_box (g_object_get_data (G_OBJECT (canvas), "width_drag_box"),
96
MIDDLE - 10 * width / 2.0);
98
move_drag_box (g_object_get_data (G_OBJECT (canvas), "shape_a_drag_box"),
102
move_drag_box (g_object_get_data (G_OBJECT (canvas), "shape_b_c_drag_box"),
103
RIGHT - 10 * shape_b,
104
MIDDLE - 10 * (shape_c + width / 2.0));
108
set_dimension (canvas, "width_arrow", "width_text",
110
MIDDLE - 10 * width / 2.0,
112
MIDDLE + 10 * width / 2.0,
117
set_dimension (canvas, "shape_a_arrow", "shape_a_text",
118
RIGHT - 10 * shape_a,
119
MIDDLE + 10 * (width / 2.0 + shape_c) + 10,
121
MIDDLE + 10 * (width / 2.0 + shape_c) + 10,
122
RIGHT - 10 * shape_a / 2.0,
123
MIDDLE + 10 * (width / 2.0 + shape_c) + 15,
126
set_dimension (canvas, "shape_b_arrow", "shape_b_text",
127
RIGHT - 10 * shape_b,
128
MIDDLE + 10 * (width / 2.0 + shape_c) + 35,
130
MIDDLE + 10 * (width / 2.0 + shape_c) + 35,
131
RIGHT - 10 * shape_b / 2.0,
132
MIDDLE + 10 * (width / 2.0 + shape_c) + 40,
135
set_dimension (canvas, "shape_c_arrow", "shape_c_text",
137
MIDDLE - 10 * width / 2.0,
139
MIDDLE - 10 * (width / 2.0 + shape_c),
141
MIDDLE - 10 * (width / 2.0 + shape_c / 2.0),
146
sprintf (buf, "width: %d", width);
147
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "width_info"),
151
sprintf (buf, "arrow_shape_a: %d", shape_a);
152
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "shape_a_info"),
156
sprintf (buf, "arrow_shape_b: %d", shape_b);
157
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "shape_b_info"),
160
sprintf (buf, "arrow_shape_c: %d", shape_c);
161
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "shape_c_info"),
167
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "sample_1"),
168
"width_pixels", width,
169
"arrow_shape_a", (double) shape_a,
170
"arrow_shape_b", (double) shape_b,
171
"arrow_shape_c", (double) shape_c,
173
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "sample_2"),
174
"width_pixels", width,
175
"arrow_shape_a", (double) shape_a,
176
"arrow_shape_b", (double) shape_b,
177
"arrow_shape_c", (double) shape_c,
179
gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "sample_3"),
180
"width_pixels", width,
181
"arrow_shape_a", (double) shape_a,
182
"arrow_shape_b", (double) shape_b,
183
"arrow_shape_c", (double) shape_c,
188
highlight_box (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
192
switch (event->type) {
193
case GDK_ENTER_NOTIFY:
194
gnome_canvas_item_set (item,
199
case GDK_LEAVE_NOTIFY:
200
if (!(event->crossing.state & GDK_BUTTON1_MASK))
201
gnome_canvas_item_set (item,
206
case GDK_BUTTON_PRESS:
207
fleur = gdk_cursor_new (GDK_FLEUR);
208
gnome_canvas_item_grab (item,
209
GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
212
gdk_cursor_unref (fleur);
215
case GDK_BUTTON_RELEASE:
216
gnome_canvas_item_ungrab (item, event->button.time);
227
create_drag_box (GnomeCanvasGroup *root, char *box_name, GCallback callback)
229
GnomeCanvasItem *box;
231
box = gnome_canvas_item_new (root,
232
gnome_canvas_rect_get_type (),
234
"outline_color", "black",
237
g_signal_connect (box, "event",
238
G_CALLBACK (highlight_box),
240
g_signal_connect (box, "event",
244
g_object_set_data (G_OBJECT (root->item.canvas), box_name, box);
248
width_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
253
if ((event->type != GDK_MOTION_NOTIFY) || !(event->motion.state & GDK_BUTTON1_MASK))
257
width = (MIDDLE - y) / 5;
261
g_object_set_data (G_OBJECT (item->canvas), "width", GINT_TO_POINTER (width));
262
set_arrow_shape (item->canvas);
268
shape_a_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
273
if ((event->type != GDK_MOTION_NOTIFY) || !(event->motion.state & GDK_BUTTON1_MASK))
277
shape_a = (RIGHT - x) / 10;
278
if ((shape_a < 0) || (shape_a > 30))
281
g_object_set_data (G_OBJECT (item->canvas), "shape_a", GINT_TO_POINTER (shape_a));
282
set_arrow_shape (item->canvas);
288
shape_b_c_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
291
int width, shape_b, shape_c;
294
if ((event->type != GDK_MOTION_NOTIFY) || !(event->motion.state & GDK_BUTTON1_MASK))
300
shape_b = (RIGHT - x) / 10;
301
if ((shape_b >= 0) && (shape_b <= 30)) {
302
g_object_set_data (G_OBJECT (item->canvas), "shape_b", GINT_TO_POINTER (shape_b));
307
width = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item->canvas), "width"));
308
shape_c = ((MIDDLE - 5 * width) - y) / 10;
310
g_object_set_data (G_OBJECT (item->canvas), "shape_c", GINT_TO_POINTER (shape_c));
315
set_arrow_shape (item->canvas);
321
create_dimension (GnomeCanvasGroup *root, char *arrow_name, char *text_name, GtkAnchorType anchor)
323
GnomeCanvasItem *item;
325
item = gnome_canvas_item_new (root,
326
gnome_canvas_line_get_type (),
327
"fill_color", "black",
328
"first_arrowhead", TRUE,
329
"last_arrowhead", TRUE,
330
"arrow_shape_a", 5.0,
331
"arrow_shape_b", 5.0,
332
"arrow_shape_c", 3.0,
334
g_object_set_data (G_OBJECT (root->item.canvas), arrow_name, item);
336
item = gnome_canvas_item_new (root,
337
gnome_canvas_text_get_type (),
338
"fill_color", "black",
342
g_object_set_data (G_OBJECT (root->item.canvas), text_name, item);
346
create_info (GnomeCanvasGroup *root, char *info_name, double x, double y)
348
GnomeCanvasItem *item;
350
item = gnome_canvas_item_new (root,
351
gnome_canvas_text_get_type (),
354
"fill_color", "black",
356
"anchor", GTK_ANCHOR_NW,
358
g_object_set_data (G_OBJECT (root->item.canvas), info_name, item);
362
create_sample_arrow (GnomeCanvasGroup *root, char *sample_name, double x1, double y1, double x2, double y2)
364
GnomeCanvasItem *item;
365
GnomeCanvasPoints *points;
367
points = gnome_canvas_points_new (2);
368
points->coords[0] = x1;
369
points->coords[1] = y1;
370
points->coords[2] = x2;
371
points->coords[3] = y2;
372
item = gnome_canvas_item_new (root,
373
gnome_canvas_line_get_type (),
375
"fill_color", "black",
376
"first_arrowhead", TRUE,
377
"last_arrowhead", TRUE,
379
g_object_set_data (G_OBJECT (root->item.canvas), sample_name, item);
380
gnome_canvas_points_free (points);
384
create_canvas_arrowhead (void)
390
GnomeCanvasGroup *root;
391
GnomeCanvasItem *item;
392
GnomeCanvasPoints *points;
394
vbox = gtk_vbox_new (FALSE, 4);
395
gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
396
gtk_widget_show (vbox);
398
w = gtk_label_new ("This demo allows you to edit arrowhead shapes. Drag the little boxes\n"
399
"to change the shape of the line and its arrowhead. You can see the\n"
400
"arrows at their normal scale on the right hand side of the window.");
401
gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
404
w = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
405
gtk_box_pack_start (GTK_BOX (vbox), w, TRUE, TRUE, 0);
408
frame = gtk_frame_new (NULL);
409
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
410
gtk_container_add (GTK_CONTAINER (w), frame);
411
gtk_widget_show (frame);
413
canvas = gnome_canvas_new ();
414
gtk_widget_set_size_request (canvas, 500, 350);
415
gnome_canvas_set_scroll_region (GNOME_CANVAS (canvas), 0, 0, 500, 350);
416
gtk_container_add (GTK_CONTAINER (frame), canvas);
417
gtk_widget_show (canvas);
419
root = gnome_canvas_root (GNOME_CANVAS (canvas));
421
g_object_set_data (G_OBJECT (canvas), "width", GINT_TO_POINTER (DEFAULT_WIDTH));
422
g_object_set_data (G_OBJECT (canvas), "shape_a", GINT_TO_POINTER (DEFAULT_SHAPE_A));
423
g_object_set_data (G_OBJECT (canvas), "shape_b", GINT_TO_POINTER (DEFAULT_SHAPE_B));
424
g_object_set_data (G_OBJECT (canvas), "shape_c", GINT_TO_POINTER (DEFAULT_SHAPE_C));
428
points = gnome_canvas_points_new (2);
429
points->coords[0] = LEFT;
430
points->coords[1] = MIDDLE;
431
points->coords[2] = RIGHT;
432
points->coords[3] = MIDDLE;
434
item = gnome_canvas_item_new (root,
435
gnome_canvas_line_get_type (),
437
"fill_color", "mediumseagreen",
438
"width_pixels", DEFAULT_WIDTH * 10,
439
"last_arrowhead", TRUE,
441
g_object_set_data (G_OBJECT (canvas), "big_arrow", item);
442
gnome_canvas_points_free (points);
446
item = gnome_canvas_item_new (root,
447
gnome_canvas_line_get_type (),
448
"fill_color", "black",
450
"cap_style", GDK_CAP_ROUND,
451
"join_style", GDK_JOIN_ROUND,
453
g_object_set_data (G_OBJECT (canvas), "outline", item);
457
create_drag_box (root, "width_drag_box", G_CALLBACK (width_event));
458
create_drag_box (root, "shape_a_drag_box", G_CALLBACK (shape_a_event));
459
create_drag_box (root, "shape_b_c_drag_box", G_CALLBACK (shape_b_c_event));
463
create_dimension (root, "width_arrow", "width_text", GTK_ANCHOR_E);
464
create_dimension (root, "shape_a_arrow", "shape_a_text", GTK_ANCHOR_N);
465
create_dimension (root, "shape_b_arrow", "shape_b_text", GTK_ANCHOR_N);
466
create_dimension (root, "shape_c_arrow", "shape_c_text", GTK_ANCHOR_W);
470
create_info (root, "width_info", LEFT, 260);
471
create_info (root, "shape_a_info", LEFT, 280);
472
create_info (root, "shape_b_info", LEFT, 300);
473
create_info (root, "shape_c_info", LEFT, 320);
477
points = gnome_canvas_points_new (2);
478
points->coords[0] = RIGHT + 50;
479
points->coords[1] = 0;
480
points->coords[2] = points->coords[0];
481
points->coords[3] = 1000;
482
gnome_canvas_item_new (root, gnome_canvas_line_get_type (),
484
"fill_color", "black",
490
create_sample_arrow (root, "sample_1", RIGHT + 100, 30, RIGHT + 100, MIDDLE - 30);
491
create_sample_arrow (root, "sample_2", RIGHT + 70, MIDDLE, RIGHT + 130, MIDDLE);
492
create_sample_arrow (root, "sample_3", RIGHT + 70, MIDDLE + 30, RIGHT + 130, MIDDLE + 120);
496
set_arrow_shape (GNOME_CANVAS (canvas));