1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Using the Canvas
</title>
<meta name="GENERATOR" content=
"Modular DocBook HTML Stylesheet Version 1.45">
<link rel="HOME" title="GTK+ / Gnome Application Development"
href="ggad.html">
<link rel="UP" title="GnomeCanvas" href="cha-canvas.html">
<link rel="PREVIOUS" title="Basic Canvas Architecture" href=
"z174.html">
<link rel="NEXT" title="Standard Canvas Item Reference" href=
"sec-itemreference.html">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
"#840084" alink="#0000FF">
<div class="NAVHEADER">
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<th colspan="4" align="center">
<font color="#000000" size="2">GTK+ / Gnome Application
Development</font>
</th>
</tr>
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="z174.html"><font color="#0000ff" size="2"><b>
<<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="sec-itemreference.html"><font color="#0000ff"
size="2"><b>Next >>></b></font></a>
</td>
</tr>
</table>
</div>
<div class="SECT1">
<h1 class="SECT1">
<a name="Z177">Using the Canvas</a>
</h1>
<p>
<tt class="CLASSNAME">GnomeCanvas</tt> is easy to use; this
is its virtue compared to <tt class="CLASSNAME">
GtkDrawingArea</tt> or some other low-level approach. This
section describes how to create a canvas, and work with
canvas items. It ends with a programming example.
</p>
<div class="SECT2">
<h2 class="SECT2">
<a name="SEC-CANVASPREPARE">Preparing the <tt class=
"CLASSNAME">GnomeCanvas</tt> Widget</a>
</h2>
<p>
The first decision you have to make is whether to use the
canvas in GDK mode or antialiased mode. When you create a
canvas widget, you must specify the mode you want; there
is no way to change it later. <tt class="FUNCTION">
gnome_canvas_new()</tt> creates a GDK canvas. <tt class=
"FUNCTION">gnome_canvas_new_aa()</tt> creates an
antialiased canvas. These are shown in <a href=
"z177.html#FL-CANVASCONSTRUCT">Figure 5</a>.
</p>
<p>
Sometimes it matters which visual and colormap the canvas
will use. In particular:
</p>
<ul>
<li>
<p>
In GDK mode, if you want to use the <span class=
"STRUCTNAME">GnomeCanvasImage</span> item to display
images, you must use Imlib's visual and colormap.
<span class="STRUCTNAME">GnomeCanvasImage</span> uses
Imlib to render images.
</p>
</li>
<li>
<p>
In antialiased mode, GDK's RGB buffer rendering
facilities (see <a href="z132.html#SEC-GDKRGB">the
section called <i>RGB Buffers</i> in the chapter
called <i>GDK Basics</i></a>) are used to copy the
RGB buffer to the screen. You must use the visual and
colormap from the GDK RGB module.
</p>
</li>
</ul>
<p>
To create a widget with a non-default visual and
colormap, <tt class="FUNCTION">
gtk_widget_push_visual()</tt> and <tt class="FUNCTION">
gtk_widget_push_colormap()</tt> are used. Here is the
code to create a GDK canvas that supports the image item:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
GtkWidget* canvas;
gtk_widget_push_visual(gdk_imlib_get_visual());
gtk_widget_push_colormap(gdk_imlib_get_colormap());
canvas = gnome_canvas_new();
gtk_widget_pop_visual();
gtk_widget_pop_colormap();
</pre>
</td>
</tr>
</table>
<p>
To create an antialiased canvas, do this:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
GtkWidget* canvas;
gtk_widget_push_visual(gdk_rgb_get_visual());
gtk_widget_push_colormap(gdk_rgb_get_cmap());
canvas = gnome_canvas_new_aa();
gtk_widget_pop_colormap();
gtk_widget_pop_visual();
</pre>
</td>
</tr>
</table>
<div class="FIGURE">
<a name="FL-CANVASCONSTRUCT"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-CANVASCONSTRUCT.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <libgnomeui/gnome-canvas.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">GtkWidget* <tt class=
"FUNCTION">gnome_canvas_new</tt></code>(void);</code>
</p>
<p>
<code><code class="FUNCDEF">GtkWidget* <tt class=
"FUNCTION">
gnome_canvas_new_aa</tt></code>(void);</code>
</p>
</div>
<p>
<b>Figure 5. Canvas Constructors</b>
</p>
</div>
<div class="SECT3">
<h3 class="SECT3">
<a name="Z178">Scroll Region</a>
</h3>
<p>
The canvas is practically infinite from a programmer's
standpoint; however, in reality your application
probably uses only a small area. When using the canvas
you must specify which region is interesting to the
user with <tt class="FUNCTION">
gnome_canvas_set_scroll_region()</tt> (<a href=
"z177.html#FL-CANVASSCROLLING">Figure 6</a>). The
scroll region is given in world coordinates. You can
query the scroll region with <tt class="FUNCTION">
gnome_canvas_get_scroll_region()</tt>.
</p>
<p>
To add scroll bars to the canvas, simply create a <tt
class="CLASSNAME">GtkScrolledWindow</tt> and add the
canvas to it:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
GtkWidget* sw;
sw = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(sw), canvas);
</pre>
</td>
</tr>
</table>
<p>
If you want to implement scrolling via some mechanism
other than the scroll bars, you can get and set the
"scroll offsets." The scroll offsets are in canvas
pixel coordinates; they specify the top left visible
pixel. Remember that canvas pixel coordinates are
relative to the scroll region.
</p>
<div class="FIGURE">
<a name="FL-CANVASSCROLLING"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-CANVASSCROLLING.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <libgnomeui/gnome-canvas.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_set_scroll_region</tt></code>(GnomeCanvas*
<tt class="PARAMETER"><i>canvas</i></tt>, double
<tt class="PARAMETER"><i>x1</i></tt>, double <tt
class="PARAMETER"><i>y1</i></tt>, double <tt class=
"PARAMETER"><i>x2</i></tt>, double <tt class=
"PARAMETER"><i>y2</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_get_scroll_region</tt></code>(GnomeCanvas*
<tt class="PARAMETER"><i>canvas</i></tt>, double*
<tt class="PARAMETER"><i>x1</i></tt>, double* <tt
class="PARAMETER"><i>y1</i></tt>, double* <tt
class="PARAMETER"><i>x2</i></tt>, double* <tt
class="PARAMETER"><i>y2</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_scroll_to</tt></code>(GnomeCanvas* <tt
class="PARAMETER"><i>canvas</i></tt>, gint <tt
class="PARAMETER"><i>cx</i></tt>, gint <tt class=
"PARAMETER"><i>cy</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_get_scroll_offsets</tt></code>(GnomeCanvas*
<tt class="PARAMETER"><i>canvas</i></tt>, gint* <tt
class="PARAMETER"><i>cx</i></tt>, gint* <tt class=
"PARAMETER"><i>cy</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 6. Canvas Scrolling</b>
</p>
</div>
</div>
<div class="SECT3">
<h3 class="SECT3">
<a name="Z179">Zooming</a>
</h3>
<p>
The canvas gives you zooming "for free"; it is included
in the world-to-canvas and canvas-to-world coordinate
system conversions. You can set the zoom factor with
<tt class="FUNCTION">
gnome_canvas_set_pixels_per_unit()</tt> (<a href=
"z177.html#FL-CANVASZOOMING">Figure 7</a>). By default,
there ratio of pixels to canvas units is 1.0, meaning
no zoom. Specifying a value less than 1.0 means reduced
size; greater than 1.0 means increased size.
</p>
<p>
In antialiased mode, you could achieve the same visual
effect by applying a scaling affine transformation to
the root canvas group. The <span class="STRUCTNAME">
pixels_per_unit</span> member of the <span class=
"STRUCTNAME">GnomeCanvas</span> struct predates the
canvas's use of affines. Still, <tt class="FUNCTION">
gnome_canvas_set_pixels_per_unit()</tt> is a bit more
convenient than the affine transform method, and it
does work in GDK mode. (Because GDK mode uses Xlib
primitives, it's nontrivial to implement arbitrary
affine transformations; a future version of Gnome may
do so, however.)
</p>
<div class="FIGURE">
<a name="FL-CANVASZOOMING"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-CANVASZOOMING.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <libgnomeui/gnome-canvas.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_set_pixels_per_unit</tt></code>(GnomeCanvas*
<tt class="PARAMETER"><i>canvas</i></tt>, double
<tt class="PARAMETER"><i>ppu</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 7. Canvas Zooming</b>
</p>
</div>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z180">Canvas Items</a>
</h2>
<p>
Most of the time you will be interested in canvas items
rather than the canvas itself. Canvas items are typically
very easy to use, compared to widgets; none of the
standard items have any unique signals, since they are
not interactive. (Since <span class="STRUCTNAME">
GnomeCanvasItem</span> is a subclass of <span class=
"STRUCTNAME">GtkObject</span>, however, you could
certainly have an item with signals if you wanted to.)
The <span class="STRUCTNAME">GnomeCanvasItem</span> base
class has a single signal, <span class="SYMBOL">
"event"</span>, which is used to convey all types of
event. The <span class="SYMBOL">"event"</span> signal has
no default handler; canvas items do not respond to events
unless you connect handlers of your own. <a href=
"z177.html#FL-CANVASITEMS">Figure 8</a> lists all the
useful functions for working with the <span class=
"STRUCTNAME">GnomeCanvasItem</span> base class.
</p>
<p>
To create a canvas item, you use the generic <tt class=
"FUNCTION">gnome_canvas_item_new()</tt> (or <tt class=
"FUNCTION">gnome_canvas_item_newv()</tt>). This function
accepts the group to place the item in, the <span class=
"STRUCTNAME">GtkType</span> of the <span class=
"STRUCTNAME">GnomeCanvasItem</span> subclass to create,
and finally a NULL-terminated list of arguments to set.
The argument list is purely for convenience, so you don't
have to call <tt class="FUNCTION">
gnome_canvas_item_set()</tt> immediately. <tt class=
"FUNCTION">gnome_canvas_item_new()</tt> creates a new
instance of the type with <tt class="FUNCTION">
gtk_type_new()</tt>, adds the item to its <span class=
"STRUCTNAME">GnomeCanvasGroup</span>, and schedules it to
be redrawn.
</p>
<p>
To destroy an item and remove it from the canvas, simply
call <tt class="FUNCTION">gtk_object_destroy()</tt>. You
can also use the standard reference counting mechanism
with canvas items.
</p>
<p>
You can set an item's affine using <tt class="FUNCTION">
gnome_canvas_item_affine_absolute()</tt>, or compose a
new affine with the item's existing affine using <tt
class="FUNCTION">
gnome_canvas_item_affine_relative()</tt>. These functions
can be used to translate, scale, or rotate a canvas item
(however, scaling and rotation only work in antialiased
mode).
</p>
<p>
Items in a group are normally stacked in the order you
add them, with the most recently-added item "on top" and
the oldest item on the bottom. You can manipulate the
stacking order with <tt class="FUNCTION">
gnome_canvas_item_raise()</tt> and <tt class="FUNCTION">
gnome_canvas_item_lower()</tt>. These move an item up or
down by the given number of positions. It is safe to pass
in a too-large value for <span class="STRUCTNAME">
positions</span>; the item will be moved as far as
possible and no more. You can also request that an item
is moved to one extreme or the other, using <tt class=
"FUNCTION">gnome_canvas_item_raise_to_top()</tt> and <tt
class="FUNCTION">gnome_canvas_item_lower_to_bottom</tt>.
</p>
<p>
Items can be shown and hidden; hidden items are not
rendered by the canvas and do not receive events. All
items are visible by default. The routines are <tt class=
"FUNCTION">gnome_canvas_item_show()</tt> and <tt class=
"FUNCTION">gnome_canvas_item_hide()</tt>.
</p>
<p>
Reparenting a canvas item is straightforward; the only
rule is that the new group must be on the same canvas as
the old group.
</p>
<p>
<tt class="FUNCTION">gnome_canvas_item_grab_focus()</tt>
is analagous to <tt class="FUNCTION">
gtk_widget_grab_focus()</tt>; it sends all key events to
the item with the grab. It also sends focus change events
to the item (when the item gains or loses the focus).
</p>
<p>
Canvas items can grab and ungrab the mouse pointer just
as a <span class="STRUCTNAME">GdkWindow</span> can; the
arguments to <tt class="FUNCTION">
gnome_canvas_item_grab()</tt> are exactly analagous to
those of <tt class="FUNCTION">gdk_pointer_grab()</tt>
(see <a href="cha-gdk.html">the chapter called <i>GDK
Basics</i></a>). While a canvas item has the pointer
grabbed, no other item receives events. Behind the
scenes, <tt class="CLASSNAME">GnomeCanvas</tt> uses <tt
class="FUNCTION">gdk_pointer_grab()</tt> to implement <tt
class="FUNCTION">gnome_canvas_item_grab()</tt>, so an
item grabbing the mouse away from other items implies the
canvas grabbing the mouse away from other widgets.
</p>
<p>
The visual properties of canvas items are manipulated
almost entirely via object arguments. If you skipped <a
href="cha-objects.html">the chapter called <i>The GTK+
Object and Type System</i></a>, go back and read the
section on object arguments now. Two functions are used
to set canvas item properties: <tt class="FUNCTION">
gnome_canvas_item_set()</tt> and <tt class="FUNCTION">
gnome_canvas_item_setv()</tt>. These are almost but not
quite equivalent to <tt class="FUNCTION">
gtk_object_set()</tt> and <tt class="FUNCTION">
gtk_object_setv()</tt>---they set object arguments in the
same way, but they also mark the canvas item to be
redrawn. So you should prefer them to the <span class=
"STRUCTNAME">GtkObject</span> variants. (This is
something of a design bug, and future canvas versions
will most likely allow you to use <tt class="FUNCTION">
gtk_object_set()</tt>.)
</p>
<p>
<tt class="FUNCTION">
gnome_canvas_item_request_update()</tt> marks the canvas
item as "dirty" and queues it to be redrawn. Internally,
the canvas uses a one-shot idle function to perform
redraws; that is, it waits until no more GTK+ events are
pending, then redraws itself a single time. It does this
by installing an idle function with <tt class="FUNCTION">
gtk_idle_add()</tt> and removing it after it runs once.
Thus <tt class="FUNCTION">
gnome_canvas_item_request_update()</tt> can be called
many times without creating an efficiency problem---it
pretty much does nothing at all if an update is already
pending.
</p>
<div class="FIGURE">
<a name="FL-CANVASITEMS"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-CANVASITEMS.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <libgnomeui/gnome-canvas.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">GnomeCanvasItem* <tt
class="FUNCTION">
gnome_canvas_item_new</tt></code>(GnomeCanvasGroup*
<tt class="PARAMETER"><i>parent</i></tt>, GtkType <tt
class="PARAMETER"><i>type</i></tt>, const gchar* <tt
class="PARAMETER"><i>first_arg_name</i></tt>, <tt
class="PARAMETER"><i>...</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">GnomeCanvasItem* <tt
class="FUNCTION">
gnome_canvas_item_newv</tt></code>(GnomeCanvasGroup*
<tt class="PARAMETER"><i>parent</i></tt>, GtkType <tt
class="PARAMETER"><i>type</i></tt>, guint <tt class=
"PARAMETER"><i>nargs</i></tt>, GtkArg* <tt class=
"PARAMETER"><i>args</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_set</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, const gchar*
<tt class="PARAMETER"><i>first_arg_name</i></tt>, <tt
class="PARAMETER"><i>...</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_setv</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, guint <tt
class="PARAMETER"><i>nargs</i></tt>, GtkArg* <tt
class="PARAMETER"><i>args</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_affine_relative</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, const double
<tt class="PARAMETER"><i>affine[6]</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_affine_absolute</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, const double
<tt class="PARAMETER"><i>affine[6]</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_raise</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, int <tt
class="PARAMETER"><i>positions</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_lower</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, int <tt
class="PARAMETER"><i>positions</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_raise_to_top</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_lower_to_bottom</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_show</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_hide</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_reparent</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>,
GnomeCanvasGroup* <tt class="PARAMETER"><i>
new_group</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_grab_focus</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">int <tt class="FUNCTION">
gnome_canvas_item_grab</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, unsigned int
<tt class="PARAMETER"><i>event_mask</i></tt>,
GdkCursor* <tt class="PARAMETER"><i>cursor</i></tt>,
guint32 <tt class="PARAMETER"><i>
etime</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_ungrab</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, guint32 <tt
class="PARAMETER"><i>etime</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_get_bounds</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>, double* <tt
class="PARAMETER"><i>x1</i></tt>, double* <tt class=
"PARAMETER"><i>y1</i></tt>, double* <tt class=
"PARAMETER"><i>x2</i></tt>, double* <tt class=
"PARAMETER"><i>y2</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_canvas_item_request_update</tt></code>(GnomeCanvasItem*
<tt class="PARAMETER"><i>item</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 8. Using <span class="STRUCTNAME">
GnomeCanvasItem</span></b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z181">Canvas Items and Events</a>
</h2>
<p>
The standard Gnome canvas items have only one signal,
<span class="SYMBOL">"event"</span>, which is emitted for
<i class="EMPHASIS">all</i> types of event. The canvas
widget preprocesses all GDK events that it receives, and
forwards some of them to canvas items. It also sythesizes
certain events. Remember that X sends events only to X
windows (<span class="STRUCTNAME">GdkWindow</span>s), and
canvas items do not have an associated <span class=
"STRUCTNAME">GdkWindow</span>. Thus the canvas widget
must act as intermediary. Here are some of the actions it
takes:
</p>
<ul>
<li>
<p>
Coordinates are automatically converted to canvas
world coordinates. For example, if a canvas item
receives an event of type <span class="STRUCTNAME">
GDK_BUTTON_PRESS</span>, the <span class=
"STRUCTNAME">x</span> and <span class="STRUCTNAME">
y</span> fields of the event will be in world
coordinates. (The raw event was received on the
canvas's <span class="STRUCTNAME">GdkWindow</span>
and thus had window coordinates.)
</p>
</li>
<li>
<p>
Enter/leave events are synthesized for canvas items
as the mouse pointer moves across the canvas.
</p>
</li>
<li>
<p>
Events are propagated up the canvas item hierarchy,
until some item's <span class="SYMBOL">"event"</span>
signal handler returns <span class="STRUCTNAME">
TRUE</span>. This works just as it does with <tt
class="CLASSNAME">GtkWidget</tt>; events are first
sent to the bottommost or leaf canvas item, and
eventually make it up to the root item.
</p>
</li>
<li>
<p>
Only user-generated events are sent to canvas items;
many events you might expect to receive on a <span
class="STRUCTNAME">GdkWindow</span>, such as expose
and configure events, are not forwarded to canvas
items.
</p>
</li>
</ul>
<p>
The canvas does this work behind the scenes, so item
events work intuitively and much like normal GDK events.
</p>
<p>
A canvas item event callback looks like this:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static gint
item_event_callback(GnomeCanvasItem* item,
GdkEvent* event,
gpointer data)
{
switch (event->type) {
case GDK_BUTTON_PRESS:
break;
case GDK_MOTION_NOTIFY:
break;
case GDK_BUTTON_RELEASE:
break;
default:
break;
}
/* Returning FALSE propagates the event to parent items;
* returning TRUE ends event propagation.
*/
return FALSE;
}
</pre>
</td>
</tr>
</table>
<p>
Of course, a real callback would probably examine the
contents of the event and take some action in response to
some of them.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z182">A Canvas Example</a>
</h2>
<p>
This section gives a brief example program, demonstrating
the user of the canvas. It does not explain the
particulars of the canvas items being created; see <a
href="sec-itemreference.html">the section called <i>
Standard Canvas Item Reference</i></a> for that. <a href=
"z177.html#FIG-CANVAS-EXAMPLE">Figure 9</a> shows the
example program in action. You can drag canvas items
around the screen with the left mouse button; clicking an
item with the Shift key held down destroys it.
</p>
<div class="FIGURE">
<a name="FIG-CANVAS-EXAMPLE"></a>
<p>
<img src="figures/canvas-example.png">
</p>
<p>
<b>Figure 9. Simple <tt class="CLASSNAME">
GnomeCanvas</tt> program</b>
</p>
</div>
<p>
Here is the code to create an antialiased canvas. Notice
the call to <tt class="FUNCTION">gdk_rgb_init()</tt>;
notice that the canvas's scroll region is set; finally,
notice that the GdkRGB colormap and visual are pushed
when creating the canvas.
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
#include <gnome.h>
static gint delete_event_cb(GtkWidget* window, GdkEventAny* e, gpointer data);
static void create_canvas_items(GtkWidget* canvas);
int
main(int argc, char* argv[])
{
GtkWidget* window;
GtkWidget* sw;
GtkWidget* canvas;
gnome_init("canvas-example", "0.0", argc, argv);
gdk_rgb_init();
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Canvas Example");
gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, TRUE);
gtk_signal_connect(GTK_OBJECT(window),
"delete_event",
GTK_SIGNAL_FUNC(delete_event_cb),
NULL);
sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_widget_push_visual(gdk_rgb_get_visual());
gtk_widget_push_colormap(gdk_rgb_get_cmap());
canvas = gnome_canvas_new_aa();
gtk_widget_pop_colormap();
gtk_widget_pop_visual();
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;
}
static gint
delete_event_cb(GtkWidget* window, GdkEventAny* e, gpointer data)
{
gtk_main_quit();
return FALSE;
}
</pre>
</td>
</tr>
</table>
<p>
Once the canvas has been created, the program adds some
items to it, and connects a simple callback to the item's
<span class="SYMBOL">"event"</span> signal. Here's the
code:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static gint
item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
{
static double x, y;
double new_x, new_y;
GdkCursor *fleur;
static int dragging;
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;
}
static void
setup_item(GnomeCanvasItem *item)
{
gtk_signal_connect(GTK_OBJECT(item), "event",
(GtkSignalFunc) item_event,
NULL);
}
static 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);
setup_item(item);
gnome_canvas_points_unref(points);
/* Translate the polygon */
art_affine_translate(affine, -150.0, -300.0);
gnome_canvas_item_affine_relative(item, affine);
/* A translucent rectangle */
setup_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));
/* A translucent ellipse */
setup_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));
/* 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));
setup_item(GNOME_CANVAS_ITEM(group));
{
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;
}
}
}
</pre>
</td>
</tr>
</table>
</div>
</div>
<div class="NAVFOOTER">
<br>
<br>
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="z174.html"><font color="#0000ff" size="2"><b>
<<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="sec-itemreference.html"><font color="#0000ff"
size="2"><b>Next >>></b></font></a>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<font color="#000000" size="2"><b>Basic Canvas
Architecture</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>Standard Canvas Item
Reference</b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|