📄 gnome-canvas-items.html
字号:
<HTML><HEAD><TITLE>Canvas Items</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.61"><LINKREL="HOME"TITLE="Writing GNOME Applications"HREF="index.html"><LINKREL="UP"TITLE="The GNOME Canvas"HREF="gnome-canvas.html"><LINKREL="PREVIOUS"TITLE="Using the Canvas"HREF="gnome-canvas-using.html"><LINKREL="NEXT"TITLE="Canvas Events"HREF="gnome-canvas-events.html"></HEAD><BODYCLASS="SECT1"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Writing GNOME Applications</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="gnome-canvas-using.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 11. The GNOME Canvas</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="gnome-canvas-events.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="GNOME-CANVAS-ITEMS">Canvas Items</A></H1><P> The GNOME Canvas wouldn't amount to as much without its ample selection of Canvas items. By choosing your Canvas items carefully, you can drastically reduce the amount of coding you need to do to mold the Canvas to your purposes. Out of the box, the Canvas comes with several vector-based items, a couple of pixmap items, a text-rendering item, and even an item for embedding GTK+ widgets into the Canvas. If these aren't enough for you, you can always create new customized Canvas items for your application. Unfortunately, we don't have room in this book to describe how to create new Canvas items. If you're interested, take a look at the source code for the current crop of Canvas items and read up on how to derive new objects from GtkObject. </P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1161">Vector-Based Drawing Items</A></H2><P> The Canvas comes with four vector-based drawing items: line, rectangle, ellipse, and polygon. These Canvas items share certain common properties for defining the appearance of the drawing items (see Table 11.1). </P><P> The three categories of properties here are the fill color, the stipple pattern, and the line width. Each of these three categories can hold only one value, so, for example, if you set the fill color property as fill_color, and then as fill_color_rgba, the second option would override the first. This is also the case with the two width properties. </P><P> Let's look at fill_color first. This property determines the item's interior color. For a closed polygon, rectangle, or ellipse, this is the color for the inside of the shape; for a GnomeCanvasLine item, fill_color refers to the color of the line itself. This is somewhat confusing because the line item doesn't have an interior area to fill. The interior of the line is the line. </P><P> The fill_color property has a gchar* type, which it passes on to gdk_color_parse( ) to retrieve an RGB value for that color (see Section 10.2.2). The other two related properties let you set the fill color with a GdkColor you've already allocated for fill_color_gdk, or with the native RGBA guint value for the fill_color_rgba property. Don't forget that the alpha channel is ignored in a GDK-mode Canvas, so unless you are using the anti-aliased Canvas, you should always set the alpha channel to 0xff. </P><P> In addition to setting the color of the item's fill area, you can set up a bitmap to mask out a repeating pattern throughout the area of your fill. The fill_stipple property expects a GdkBitmap* value, most likely created with gdk_bitmap_create_from_data( ). The respective Canvas item will tile this bitmap across the entire fill area covered by the fill_color properties. In the case of the GnomeCanvasLine item, the fill_stipple pattern will mask out the line itself. Note that the stipple is not adaptive at all. If your line changes direction, the stipple will not change direction. If you want a stipple pattern that, for example, is always perpendicular to your line, you will have to create different stipple patterns for each new angle of line segment. An alternative to this is the line_style property, which we'll discuss in Section 11.4.2. Note that the AA Canvas does not currently support the stipple properties, so stippling works only in a GDK Canvas. </P><P> The width properties are a little more straightforward; they refer to the width of the lines. The width_pixels property sets the width of all lines in that Canvas item- whether it's a line item or the outline of a closed shape-to a fixed width in canvas coordinates (i.e., screen pixels). If you zoom the Canvas in or out with gnome_canvas_set_pixels_per_unit( ), your lines will stay the same visible width. Conversely, width_units sets the line width in world coordinates, so when you zoom in, your lines will be visibly wider and vice versa. Note that width_pixels has a type of guint, just like the canvas coordinate system, and width_units has a type of gdouble, like world coordinates. Be very careful not to mix these units up in the parameter list because the two types are of different size and will disrupt the variable argument pars- ing if reversed. To illustrate, this function call would probably crash your application: </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gnome_canvas_item_set(item, "width_units", 2, NULL); </PRE></TD></TR></TABLE><P> The correct way to specify width_units is either to cast the value to a gdouble type or to explicitly include the decimal point, as in these two examples: </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gnome_canvas_item_set(item, "width_units", (gdouble)2, NULL);gnome_canvas_item_set(item, "width_units", 2.0, NULL); </PRE></TD></TR></TABLE><P> The closed shapes have another set of properties for dealing with the appearance of their outlines. These properties have a one-to-one correspondence with the fill properties. Thus you can arrange for colors, transparencies, and stipple patterns for the outlines of an ellipse, rectangle, or polygon that are separate from those you have established for its interior. Conversely, you won't find the outline properties in a GnomeCanvasLine item because exactly the same characteristics are covered by the line's fill properties. Table 11.2 lists the outline properties. </P><P> We'll wrap up our discussion of fills and stipples with a brief example. The source code is given in Listing 11.1. This example will draw a tan ellipse with a red outline, 6.0 world coordinate units wide. The tan interior will have a diagonal hatching pattern, while the red outline will have a stipple pattern with horizontal stripes. Figure 11.3 shows how we arrived at the hexadecimal contents of the stipple bitmaps, and Figure 11.4 shows how this ellipse item will look on a Canvas. The code assumes that you've already created the Canvas elsewhere, and that group is a pointer to a GnomeCanvasGroup item inside that Canvas. For now, you can take for granted that the x1, y1, x2, and y2 properties define the ellipse's bounding box. Notice that we must put the top and left coordinates at 3.0 rather than 0.0, to avoid clipping the 6.0-unit-wide outline (assuming that our scrolling region starts at 0.0 in both axes and has a one-to-one scaling ratio, of course). This demonstrates the policy that the true coordinates of Canvas lines are centered in the middle of the line rather than on a rendered edge. We'll delve into shapes and sizes more deeply in Section 11.4.3. </P><DIVCLASS="FIGURE"><ANAME="AEN1174"></A><P><B>Figure 11-3. Bitmaps for Stipple Masks</B></P><DIVCLASS="MEDIAOBJECT"><P><IMGSRC="figures/11f3.png"></IMG></P></DIV></DIV><DIVCLASS="FIGURE"><ANAME="AEN1179"></A><P><B>Figure 11-4. Stipple Mask Example</B></P><DIVCLASS="MEDIAOBJECT"><P><IMGSRC="figures/11f4.png"></IMG></P></DIV></DIV><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">Listing 11.1 Sample Code for Stippled Ellipse Canvas ItemGnomeCanvasItem *item;GdkBitmap *dstipple;GdkBitmap *hstipple;static char diagonal_stipple[] = { 0x0d, 0x0e, 0x07, 0x0b };static char horizontal_stipple[] = { 0x03, 0x00, 0x00 };/* Create bitmaps for stipple masks */dstipple = gdk_bitmap_create_from_data(NULL, diagonal_stipple, 4, 4);hstipple = gdk_bitmap_create_from_data(NULL, horizontal_stipple, 2, 3);item = gnome_canvas_item_new(group, GNOME_TYPE_CANVAS_ELLIPSE, "x1", 3.0, "y1", 3.0, "x2", 250.0, "y2", 100.0, "fill_color", "tan", "fill_stipple", dstipple, "outline_color", "red", "outline_stipple", hstipple, "width_units", 6.0, NULL); </PRE></TD></TR></TABLE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1185">Line Styles</A></H2><P> Just as the closed-shape Canvas items have outline properties that the line item doesn't have, the line item has some special properties that none of the closed-shape items have. Table 11.3 lists these properties. The GnomeCanvasLine item has quite a number of interesting features, allowing you to customize the arrowheads, the dotted line style, elbow shapes, and the general quality of the lines. Each feature has exactly one property name, unlike the fill, outline, and width properties. </P><P> Some of the line style properties correspond directly to native GDK line styles, and as such they work only in GDK mode. Don't be confused if your line styles suddenly disappear when you switch to AA mode, because they may not be supported in that mode. Listings 11.2 through 11.4 show the possible values of these enumerations, as defined in gtk+/gdk/gdktypes.h. </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">Listing 11.2 GdkCapStyle Enumerationtypedef enum{ GDK_CAP_NOT_LAST, GDK_CAP_BUTT, GDK_CAP_ROUND, GDK_CAP_PROJECTING} GdkCapStyle; </PRE></TD></TR></TABLE><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">Listing 11.3 GdkJoinStyle Enumerationtypedef enum{ GDK_JOIN_MITER, GDK_JOIN_ROUND, GDK_JOIN_BEVEL} GdkJoinStyle; </PRE></TD></TR></TABLE><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">Listing 11.4 GdkLineStyle Enumerationtypedef enum{ GDK_LINE_SOLID, GDK_LINE_ON_OFF_DASH, GDK_LINE_DOUBLE_DASH} GdkLineStyle; </PRE></TD></TR></TABLE><P> Arrowheads are available in both Canvas modes. Each
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -