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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Other Methods
</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="Writing a GnomeCanvasItem" href=
"cha-canvasitem.html">
<link rel="PREVIOUS" title="Drawing Methods" href="z186.html">
<link rel="NEXT" title="Appendices" href="appendices.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="z186.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="appendices.html"><font color="#0000ff" size=
"2"><b>Next >>></b></font></a>
</td>
</tr>
</table>
</div>
<div class="SECT1">
<h1 class="SECT1">
<a name="Z192">Other Methods</a>
</h1>
<p>
This section describes the remaining <span class=
"STRUCTNAME">GnomeCanvasItem</span> methods, including
<span class="STRUCTNAME">event</span>, <span class=
"STRUCTNAME">point</span>, <span class="STRUCTNAME">
bounds</span>, <span class="STRUCTNAME">realize</span>,
<span class="STRUCTNAME">unrealize</span>, <span class=
"STRUCTNAME">map</span>, and <span class="STRUCTNAME">
unmap</span>.
</p>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z193">Events</a>
</h2>
<p>
The <span class="STRUCTNAME">GnomeCanvasItem</span> class
has a slot in its vtable called <span class="STRUCTNAME">
event</span>; this is the only <span class="STRUCTNAME">
GnomeCanvasItem</span> class function associated with a
signal. None of the stock canvas items implement a
default handler for <span class="STRUCTNAME">
event</span>, but of course your own canvas item could.
</p>
<p>
The return value of <span class="STRUCTNAME">event</span>
works just like the <tt class="CLASSNAME">GtkWidget</tt>
<span class="SYMBOL">"event"</span> signal; if the last
signal handler returns <span class="STRUCTNAME">
FALSE</span>, the event is propagated to the item's
parent item (by emitting the <span class="STRUCTNAME">
event</span> signal on the parent), if <span class=
"STRUCTNAME">TRUE</span> propagation ends.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z194">Point</a>
</h2>
<p>
The <span class="STRUCTNAME">point</span> method is used
to determine which canvas item is located at a given
point. The canvas uses this information to decide which
item should receive events. A point method calculates the
distance from some point to the canvas item. Canvas items
<i class="EMPHASIS">must</i> correctly report a distance
of 0 if the point is on the canvas item, or they will not
receive events; they <i class="EMPHASIS">must</i> report
non-zero if the point is not on the item, or they will
receive too many events. The exact value returned is not
nearly as important as the zero/non-zero distinction.
</p>
<p>
For convenience, the point method receives the same point
pre-translated into both item and canvas pixel
coordinates.
</p>
<p>
The point method also receives a pointer to a pointer to
a <span class="STRUCTNAME">GnomeCanvasItem</span>;
non-group canvas items should store a pointer to
themselves in this space. Groups store the <span class=
"STRUCTNAME">*actual_item</span> received from the
topmost child which returns 0 from its point method. If
you think about it for a while, you will see the
implication: the root canvas group's point method stores
a pointer to the deepest child in the item tree at the
point in question. The canvas sends events occurring at
that point to this most-junior child. Note that the
canvas item tree corresponds to the item stacking order
(i.e. the root group is on the bottom), so events go to
the topmost items, as you might expect. Remember that
events are then propagated up the item tree hierarchy.
</p>
<p>
Here is the point method for <span class="STRUCTNAME">
GnomeCanvasRect</span>:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static double
gnome_canvas_rect_point (GnomeCanvasItem *item,
double x, double y, int cx, int cy,
GnomeCanvasItem **actual_item)
{
GnomeCanvasRE *re;
double x1, y1, x2, y2;
double hwidth;
double dx, dy;
double tmp;
re = GNOME_CANVAS_RE (item);
*actual_item = item;
/* Find the bounds for the rectangle plus its outline width */
x1 = re->x1;
y1 = re->y1;
x2 = re->x2;
y2 = re->y2;
if (re->outline_set) {
if (re->width_pixels)
hwidth = (re->width / item->canvas->pixels_per_unit) / 2.0;
else
hwidth = re->width / 2.0;
x1 -= hwidth;
y1 -= hwidth;
x2 += hwidth;
y2 += hwidth;
} else
hwidth = 0.0;
/* Is point inside rectangle (which can be hollow if it has no fill set)? */
if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
if (re->fill_set || !re->outline_set)
return 0.0;
dx = x - x1;
tmp = x2 - x;
if (tmp < dx)
dx = tmp;
dy = y - y1;
tmp = y2 - y;
if (tmp < dy)
dy = tmp;
if (dy < dx)
dx = dy;
dx -= 2.0 * hwidth;
if (dx < 0.0)
return 0.0;
else
return dx;
}
/* Point is outside rectangle */
if (x < x1)
dx = x1 - x;
else if (x > x2)
dx = x - x2;
else
dx = 0.0;
if (y < y1)
dy = y1 - y;
else if (y > y2)
dy = y - y2;
else
dy = 0.0;
return sqrt (dx * dx + dy * dy);
}
</pre>
</td>
</tr>
</table>
<p>
It should be obvious how this function works; it is
simple geometry. Again, notice the line:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
*actual_item = item;
</pre>
</td>
</tr>
</table>
<p>
If your item isn't receiving any events, make sure you
included a similar statement.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z195">Bounds</a>
</h2>
<p>
The <span class="STRUCTNAME">bounds</span> method
computes the approximate bounding box of a canvas item.
In Gnome 1.0, this method is only used in <tt class=
"FUNCTION">gnome_canvas_item_get_bounds()</tt>, a
user-visible function to return the bounds of a canvas
item. The canvas does not use it at all internally, and
most likely you could get away without implementing it,
though all the stock items do.
</p>
<p>
The function should return an item's bounding box in item
coordinates; here is the <span class="STRUCTNAME">
GnomeCanvasRE</span> version:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static void
gnome_canvas_re_bounds (GnomeCanvasItem *item,
double *x1, double *y1,
double *x2, double *y2)
{
GnomeCanvasRE *re;
double hwidth;
re = GNOME_CANVAS_RE (item);
if (re->width_pixels)
hwidth = (re->width / item->canvas->pixels_per_unit) / 2.0;
else
hwidth = re->width / 2.0;
*x1 = re->x1 - hwidth;
*y1 = re->y1 - hwidth;
*x2 = re->x2 + hwidth;
*y2 = re->y2 + hwidth;
}
</pre>
</td>
</tr>
</table>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z196">Realizing and Mapping</a>
</h2>
<p>
Canvas items are realized and mapped just as widgets are.
These methods play the same role they do for widgets;
realizing a canvas item allocates any GDK resources it
plans to use, unrealizing it deallocates the same
resources. Mapping a canvas item shows its <span class=
"STRUCTNAME">GdkWindow</span>, unmapping it hides the
<span class="STRUCTNAME">GdkWindow</span>. Very few
canvas items have a <span class="STRUCTNAME">
GdkWindow</span> (<span class=
"STRUCTNAME">GnomeCanvasWidget</span> is the big
exception), so most canvas items will not even implement
map and unmap methods. <span class="STRUCTNAME">
GnomeCanvasRect</span> does not. It does have realize and
unrealize methods, however.
</p>
<p>
Here is its realize method:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static void
gnome_canvas_re_realize (GnomeCanvasItem *item)
{
GnomeCanvasRE *re;
re = GNOME_CANVAS_RE (item);
if (re_parent_class->realize)
(* re_parent_class->realize) (item);
if (!item->canvas->aa) {
re->fill_gc = gdk_gc_new (item->canvas->layout.bin_window);
re->outline_gc = gdk_gc_new (item->canvas->layout.bin_window);
}
}
</pre>
</td>
</tr>
</table>
<p>
And unrealize:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static void
gnome_canvas_re_unrealize (GnomeCanvasItem *item)
{
GnomeCanvasRE *re;
re = GNOME_CANVAS_RE (item);
if (!item->canvas->aa) {
gdk_gc_unref (re->fill_gc);
gdk_gc_unref (re->outline_gc);
}
if (re_parent_class->unrealize)
(* re_parent_class->unrealize) (item);
}
</pre>
</td>
</tr>
</table>
<p>
Note that your realize and unrealize methods are unlikely
to have anything to do in antialiased mode, since there
won't be any GDK resources to worry about.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z197"><span class="STRUCTNAME">GtkObject</span>
Methods</a>
</h2>
<p>
Of course, any canvas item subclass must implement the
usual <span class="STRUCTNAME">GtkObject</span> methods,
including <span class="STRUCTNAME">destroy</span> if the
object allocates resources that need cleaning up, and a
<span class="STRUCTNAME">get_arg</span>/<span class=
"STRUCTNAME">set_arg</span> pair if the object defines
any arguments. The only canvas-item-specific concern is
that you must schedule an update or redraw as needed if
<span class="STRUCTNAME">set_arg</span> changes the
properties of the canvas item. These functions are quite
long due to the number of arguments, but not very
interesting, so they are omitted here. See the full <span
class="STRUCTNAME">GnomeCanvasRect</span> source code if
you're curious.
</p>
</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="z186.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="appendices.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>Drawing
Methods</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>Appendices</b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|