?? z177.html
字號:
<!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=
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -