~ubuntu-branches/ubuntu/breezy/libgnomecanvas/breezy

« back to all changes in this revision

Viewing changes to demos/canvas-arrowhead.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2004-09-13 22:40:39 UTC
  • Revision ID: james.westby@ubuntu.com-20040913224039-giftt9jhvyoorcuv
Tags: upstream-2.8.0
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <config.h>
 
2
#include <stdio.h>
 
3
#include <math.h>
 
4
#include "canvas_demo.h"
 
5
 
 
6
#define LEFT    50.0
 
7
#define RIGHT  350.0
 
8
#define MIDDLE 150.0
 
9
#define DEFAULT_WIDTH   2
 
10
#define DEFAULT_SHAPE_A 8
 
11
#define DEFAULT_SHAPE_B 10
 
12
#define DEFAULT_SHAPE_C 3
 
13
 
 
14
 
 
15
static void
 
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)
 
18
{
 
19
        GnomeCanvasPoints *points;
 
20
        char buf[100];
 
21
 
 
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;
 
27
 
 
28
        gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), arrow_name),
 
29
                               "points", points,
 
30
                               NULL);
 
31
        sprintf (buf, "%d", dim);
 
32
        gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), text_name),
 
33
                               "text", buf,
 
34
                               "x", tx,
 
35
                               "y", ty,
 
36
                               NULL);
 
37
 
 
38
        gnome_canvas_points_free (points);
 
39
}
 
40
 
 
41
static void
 
42
move_drag_box (GnomeCanvasItem *item, double x, double y)
 
43
{
 
44
        gnome_canvas_item_set (item,
 
45
                               "x1", x - 5.0,
 
46
                               "y1", y - 5.0,
 
47
                               "x2", x + 5.0,
 
48
                               "y2", y + 5.0,
 
49
                               NULL);
 
50
}
 
51
 
 
52
static void
 
53
set_arrow_shape (GnomeCanvas *canvas)
 
54
{
 
55
        int width;
 
56
        int shape_a, shape_b, shape_c;
 
57
        GnomeCanvasPoints *points;
 
58
        char buf[100];
 
59
 
 
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"));
 
64
 
 
65
        /* Big arrow */
 
66
 
 
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),
 
72
                               NULL);
 
73
 
 
74
        /* Outline */
 
75
 
 
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"),
 
88
                               "points", points,
 
89
                               NULL);
 
90
        gnome_canvas_points_free (points);
 
91
 
 
92
        /* Drag boxes */
 
93
 
 
94
        move_drag_box (g_object_get_data (G_OBJECT (canvas), "width_drag_box"),
 
95
                       LEFT,
 
96
                       MIDDLE - 10 * width / 2.0);
 
97
 
 
98
        move_drag_box (g_object_get_data (G_OBJECT (canvas), "shape_a_drag_box"),
 
99
                       RIGHT - 10 * shape_a,
 
100
                       MIDDLE);
 
101
 
 
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));
 
105
 
 
106
        /* Dimensions */
 
107
 
 
108
        set_dimension (canvas, "width_arrow", "width_text",
 
109
                       LEFT - 10,
 
110
                       MIDDLE - 10 * width / 2.0,
 
111
                       LEFT - 10,
 
112
                       MIDDLE + 10 * width / 2.0,
 
113
                       LEFT - 15,
 
114
                       MIDDLE,
 
115
                       width);
 
116
 
 
117
        set_dimension (canvas, "shape_a_arrow", "shape_a_text",
 
118
                       RIGHT - 10 * shape_a,
 
119
                       MIDDLE + 10 * (width / 2.0 + shape_c) + 10,
 
120
                       RIGHT,
 
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,
 
124
                       shape_a);
 
125
 
 
126
        set_dimension (canvas, "shape_b_arrow", "shape_b_text",
 
127
                       RIGHT - 10 * shape_b,
 
128
                       MIDDLE + 10 * (width / 2.0 + shape_c) + 35,
 
129
                       RIGHT,
 
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,
 
133
                       shape_b);
 
134
 
 
135
        set_dimension (canvas, "shape_c_arrow", "shape_c_text",
 
136
                       RIGHT + 10,
 
137
                       MIDDLE - 10 * width / 2.0,
 
138
                       RIGHT + 10,
 
139
                       MIDDLE - 10 * (width / 2.0 + shape_c),
 
140
                       RIGHT + 15,
 
141
                       MIDDLE - 10 * (width / 2.0 + shape_c / 2.0),
 
142
                       shape_c);
 
143
 
 
144
        /* Info */
 
145
 
 
146
        sprintf (buf, "width: %d", width);
 
147
        gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "width_info"),
 
148
                               "text", buf,
 
149
                               NULL);
 
150
 
 
151
        sprintf (buf, "arrow_shape_a: %d", shape_a);
 
152
        gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "shape_a_info"),
 
153
                               "text", buf,
 
154
                               NULL);
 
155
 
 
156
        sprintf (buf, "arrow_shape_b: %d", shape_b);
 
157
        gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "shape_b_info"),
 
158
                               "text", buf,
 
159
                               NULL);
 
160
        sprintf (buf, "arrow_shape_c: %d", shape_c);
 
161
        gnome_canvas_item_set (g_object_get_data (G_OBJECT (canvas), "shape_c_info"),
 
162
                               "text", buf,
 
163
                               NULL);
 
164
 
 
165
        /* Sample arrows */
 
166
 
 
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,
 
172
                               NULL);
 
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,
 
178
                               NULL);
 
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,
 
184
                               NULL);
 
185
}
 
186
 
 
187
static gint
 
188
highlight_box (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
 
189
{
 
190
        GdkCursor *fleur;
 
191
 
 
192
        switch (event->type) {
 
193
        case GDK_ENTER_NOTIFY:
 
194
                gnome_canvas_item_set (item,
 
195
                                       "fill_color", "red",
 
196
                                       NULL);
 
197
                break;
 
198
 
 
199
        case GDK_LEAVE_NOTIFY:
 
200
                if (!(event->crossing.state & GDK_BUTTON1_MASK))
 
201
                        gnome_canvas_item_set (item,
 
202
                                               "fill_color", NULL,
 
203
                                               NULL);
 
204
                break;
 
205
 
 
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,
 
210
                                        fleur,
 
211
                                        event->button.time);
 
212
                gdk_cursor_unref (fleur);
 
213
                break;
 
214
 
 
215
        case GDK_BUTTON_RELEASE:
 
216
                gnome_canvas_item_ungrab (item, event->button.time);
 
217
                break;
 
218
 
 
219
        default:
 
220
                break;
 
221
        }
 
222
 
 
223
        return FALSE;
 
224
}
 
225
 
 
226
static void
 
227
create_drag_box (GnomeCanvasGroup *root, char *box_name, GCallback callback)
 
228
{
 
229
        GnomeCanvasItem *box;
 
230
 
 
231
        box = gnome_canvas_item_new (root,
 
232
                                     gnome_canvas_rect_get_type (),
 
233
                                     "fill_color", NULL,
 
234
                                     "outline_color", "black",
 
235
                                     "width_pixels", 0,
 
236
                                     NULL);
 
237
        g_signal_connect (box, "event",
 
238
                          G_CALLBACK (highlight_box),
 
239
                          NULL);
 
240
        g_signal_connect (box, "event",
 
241
                          callback,
 
242
                          NULL);
 
243
 
 
244
        g_object_set_data (G_OBJECT (root->item.canvas), box_name, box);
 
245
}
 
246
 
 
247
static gint
 
248
width_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
 
249
{
 
250
        int y;
 
251
        int width;
 
252
 
 
253
        if ((event->type != GDK_MOTION_NOTIFY) || !(event->motion.state & GDK_BUTTON1_MASK))
 
254
                return FALSE;
 
255
 
 
256
        y = event->motion.y;
 
257
        width = (MIDDLE - y) / 5;
 
258
        if (width < 0)
 
259
                return FALSE;
 
260
 
 
261
        g_object_set_data (G_OBJECT (item->canvas), "width", GINT_TO_POINTER (width));
 
262
        set_arrow_shape (item->canvas);
 
263
 
 
264
        return FALSE;
 
265
}
 
266
 
 
267
static gint
 
268
shape_a_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
 
269
{
 
270
        int x;
 
271
        int shape_a;
 
272
 
 
273
        if ((event->type != GDK_MOTION_NOTIFY) || !(event->motion.state & GDK_BUTTON1_MASK))
 
274
                return FALSE;
 
275
 
 
276
        x = event->motion.x;
 
277
        shape_a = (RIGHT - x) / 10;
 
278
        if ((shape_a < 0) || (shape_a > 30))
 
279
                return FALSE;
 
280
 
 
281
        g_object_set_data (G_OBJECT (item->canvas), "shape_a", GINT_TO_POINTER (shape_a));
 
282
        set_arrow_shape (item->canvas);
 
283
 
 
284
        return FALSE;
 
285
}
 
286
 
 
287
static gint
 
288
shape_b_c_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
 
289
{
 
290
        int x, y;
 
291
        int width, shape_b, shape_c;
 
292
        int change;
 
293
 
 
294
        if ((event->type != GDK_MOTION_NOTIFY) || !(event->motion.state & GDK_BUTTON1_MASK))
 
295
                return FALSE;
 
296
 
 
297
        change = FALSE;
 
298
 
 
299
        x = event->motion.x;
 
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));
 
303
                change = TRUE;
 
304
        }
 
305
 
 
306
        y = event->motion.y;
 
307
        width = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item->canvas), "width"));
 
308
        shape_c = ((MIDDLE - 5 * width) - y) / 10;
 
309
        if (shape_c >= 0) {
 
310
                g_object_set_data (G_OBJECT (item->canvas), "shape_c", GINT_TO_POINTER (shape_c));
 
311
                change = TRUE;
 
312
        }
 
313
 
 
314
        if (change)
 
315
                set_arrow_shape (item->canvas);
 
316
 
 
317
        return FALSE;
 
318
}
 
319
 
 
320
static void
 
321
create_dimension (GnomeCanvasGroup *root, char *arrow_name, char *text_name, GtkAnchorType anchor)
 
322
{
 
323
        GnomeCanvasItem *item;
 
324
 
 
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,
 
333
                                      NULL);
 
334
        g_object_set_data (G_OBJECT (root->item.canvas), arrow_name, item);
 
335
 
 
336
        item = gnome_canvas_item_new (root,
 
337
                                      gnome_canvas_text_get_type (),
 
338
                                      "fill_color", "black",
 
339
                                      "font", "Sans 12",
 
340
                                      "anchor", anchor,
 
341
                                      NULL);
 
342
        g_object_set_data (G_OBJECT (root->item.canvas), text_name, item);
 
343
}
 
344
 
 
345
static void
 
346
create_info (GnomeCanvasGroup *root, char *info_name, double x, double y)
 
347
{
 
348
        GnomeCanvasItem *item;
 
349
 
 
350
        item = gnome_canvas_item_new (root,
 
351
                                      gnome_canvas_text_get_type (),
 
352
                                      "x", x,
 
353
                                      "y", y,
 
354
                                      "fill_color", "black",
 
355
                                      "font", "Sans 14",
 
356
                                      "anchor", GTK_ANCHOR_NW,
 
357
                                      NULL);
 
358
        g_object_set_data (G_OBJECT (root->item.canvas), info_name, item);
 
359
}
 
360
 
 
361
static void
 
362
create_sample_arrow (GnomeCanvasGroup *root, char *sample_name, double x1, double y1, double x2, double y2)
 
363
{
 
364
        GnomeCanvasItem *item;
 
365
        GnomeCanvasPoints *points;
 
366
 
 
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 (),
 
374
                                      "points", points,
 
375
                                      "fill_color", "black",
 
376
                                      "first_arrowhead", TRUE,
 
377
                                      "last_arrowhead", TRUE,
 
378
                                      NULL);
 
379
        g_object_set_data (G_OBJECT (root->item.canvas), sample_name, item);
 
380
        gnome_canvas_points_free (points);
 
381
}
 
382
 
 
383
GtkWidget *
 
384
create_canvas_arrowhead (void)
 
385
{
 
386
        GtkWidget *vbox;
 
387
        GtkWidget *w;
 
388
        GtkWidget *frame;
 
389
        GtkWidget *canvas;
 
390
        GnomeCanvasGroup *root;
 
391
        GnomeCanvasItem *item;
 
392
        GnomeCanvasPoints *points;
 
393
 
 
394
        vbox = gtk_vbox_new (FALSE, 4);
 
395
        gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
 
396
        gtk_widget_show (vbox);
 
397
 
 
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);
 
402
        gtk_widget_show (w);
 
403
 
 
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);
 
406
        gtk_widget_show (w);
 
407
 
 
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);
 
412
 
 
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);
 
418
 
 
419
        root = gnome_canvas_root (GNOME_CANVAS (canvas));
 
420
 
 
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));
 
425
 
 
426
        /* Big arrow */
 
427
 
 
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;
 
433
 
 
434
        item = gnome_canvas_item_new (root,
 
435
                                      gnome_canvas_line_get_type (),
 
436
                                      "points", points,
 
437
                                      "fill_color", "mediumseagreen",
 
438
                                      "width_pixels", DEFAULT_WIDTH * 10,
 
439
                                      "last_arrowhead", TRUE,
 
440
                                      NULL);
 
441
        g_object_set_data (G_OBJECT (canvas), "big_arrow", item);
 
442
        gnome_canvas_points_free (points);
 
443
 
 
444
        /* Arrow outline */
 
445
 
 
446
        item = gnome_canvas_item_new (root,
 
447
                                      gnome_canvas_line_get_type (),
 
448
                                      "fill_color", "black",
 
449
                                      "width_pixels", 2,
 
450
                                      "cap_style", GDK_CAP_ROUND,
 
451
                                      "join_style", GDK_JOIN_ROUND,
 
452
                                      NULL);
 
453
        g_object_set_data (G_OBJECT (canvas), "outline", item);
 
454
 
 
455
        /* Drag boxes */
 
456
 
 
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));
 
460
 
 
461
        /* Dimensions */
 
462
 
 
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);
 
467
 
 
468
        /* Info */
 
469
 
 
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);
 
474
 
 
475
        /* Division line */
 
476
 
 
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 (),
 
483
                               "points", points,
 
484
                               "fill_color", "black",
 
485
                               "width_pixels", 2,
 
486
                               NULL);
 
487
 
 
488
        /* Sample arrows */
 
489
 
 
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);
 
493
 
 
494
        /* Done! */
 
495
        
 
496
        set_arrow_shape (GNOME_CANVAS (canvas));
 
497
        return vbox;
 
498
}