📄 z192.html
字号:
<!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 doublegnome_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 voidgnome_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 voidgnome_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 voidgnome_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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -