📄 gdk-windows.html
字号:
*/static gbooleantransparent_expose (GtkWidget *widget, GdkEventExpose *event){ cairo_t *cr; cr = gdk_cairo_create (widget->window); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); gdk_cairo_region (cr, event->region); cairo_fill (cr); cairo_destroy (cr); return FALSE;}/* The expose event handler for the window. * * This function performs the actual compositing of the event box onto * the already-existing background of the window at 50% normal opacity. * * In this case we do not want app-paintable to be set on the widget * since we want it to draw its own (red) background. Because of this, * however, we must ensure that we use g_signal_register_after so that * this handler is called after the red has been drawn. If it was * called before then GTK would just blindly paint over our work. * * Note: if the child window has children, then you need a cairo 1.16 * feature to make this work correctly. */static gbooleanwindow_expose_event (GtkWidget *widget, GdkEventExpose *event){ GdkRegion *region; GtkWidget *child; cairo_t *cr; /* get our child (in this case, the event box) */ child = gtk_bin_get_child (GTK_BIN (widget)); /* create a cairo context to draw to the window */ cr = gdk_cairo_create (widget->window); /* the source data is the (composited) event box */ gdk_cairo_set_source_pixmap (cr, child->window, child->allocation.x, child->allocation.y); /* draw no more than our expose event intersects our child */ region = gdk_region_rectangle (&child->allocation); gdk_region_intersect (region, event->region); gdk_cairo_region (cr, region); cairo_clip (cr); /* composite, with a 50% opacity */ cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_paint_with_alpha (cr, 0.5); /* we're done */ cairo_destroy (cr); return FALSE;}intmain (int argc, char **argv){ GtkWidget *window, *event, *button; GdkScreen *screen; GdkColormap *rgba; GdkColor red; gtk_init (&argc, &argv); /* Make the widgets */ button = gtk_button_new_with_label ("A Button"); event = gtk_event_box_new (); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); /* Put a red background on the window */ gdk_color_parse ("red", &red); gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &red); /* Set the colourmap for the event box. * Must be done before the event box is realised. */ screen = gtk_widget_get_screen (event); rgba = gdk_screen_get_rgba_colormap (screen); gtk_widget_set_colormap (event, rgba); /* Set our event box to have a fully-transparent background * drawn on it. Currently there is no way to simply tell GTK+ * that "transparency" is the background colour for a widget. */ gtk_widget_set_app_paintable (GTK_WIDGET (event), TRUE); g_signal_connect (event, "expose-event", G_CALLBACK (transparent_expose), NULL); /* Put them inside one another */ gtk_container_set_border_width (GTK_CONTAINER (window), 10); gtk_container_add (GTK_CONTAINER (window), event); gtk_container_add (GTK_CONTAINER (event), button); /* Realise and show everything */ gtk_widget_show_all (window); /* Set the event box GdkWindow to be composited. * Obviously must be performed after event box is realised. */ gdk_window_set_composited (event->window, TRUE); /* Set up the compositing handler. * Note that we do _after_ so that the normal (red) background is drawn * by gtk before our compositing occurs. */ g_signal_connect_after (window, "expose-event", G_CALLBACK (window_expose_event), NULL); gtk_main (<!-- -->); return 0;}</pre></div></div><br class="example-break"><p>In the example <a class="xref" href="gdk-Windows.html#composited-window-example" title="Example 7. Composited windows">Example 7, “Composited windows”</a>, a button is placed inside of an event box inside of a window. The event box is set as composited and therefore is no longer automatically drawn to the screen. </p><p>When the contents of the event box change, an expose event isgenerated on its parent window (which, in this case, belongs tothe toplevel <ahref="/usr/share/gtk-doc/html/gtk/GtkWindow.html"><span class="type">GtkWindow</span></a>). The expose handler for this widget isresponsible for merging the changes back on the screen in the waythat it wishes.</p><p>In our case, we merge the contents with a 50% transparency. Wealso set the background colour of the window to red. The effect isthat the background shows through the button. </p></div><div class="refsect1" lang="en"><a name="id3179322"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3179331"></a><h3><a name="GdkWindow-struct"></a>struct GdkWindow</h3><a class="indexterm" name="id3179344"></a><pre class="programlisting">struct GdkWindow;</pre><p>An opaque structure representing an onscreen drawable.Pointers to structures of type <a class="link" href="gdk-Bitmaps-and-Pixmaps.html#GdkPixmap"><span class="type">GdkPixmap</span></a>, <a class="link" href="gdk-Bitmaps-and-Pixmaps.html#GdkBitmap"><span class="type">GdkBitmap</span></a>,and <a class="link" href="gdk-Windows.html#GdkWindow"><span class="type">GdkWindow</span></a>, can often be used interchangeably.The type <a class="link" href="gdk-Drawing-Primitives.html#GdkDrawable"><span class="type">GdkDrawable</span></a> refers generically to any ofthese types.</p></div><hr><div class="refsect2" lang="en"><a name="id3179393"></a><h3><a name="GdkWindowType"></a>enum GdkWindowType</h3><a class="indexterm" name="id3179406"></a><pre class="programlisting">typedef enum{ GDK_WINDOW_ROOT, GDK_WINDOW_TOPLEVEL, GDK_WINDOW_CHILD, GDK_WINDOW_DIALOG, GDK_WINDOW_TEMP, GDK_WINDOW_FOREIGN} GdkWindowType;</pre><p>Describes the kind of window.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><a name="GDK-WINDOW-ROOT:CAPS"></a><code class="literal">GDK_WINDOW_ROOT</code></span></p></td><td>root window; this window has no parent, covers the entire screen, and is created by the window system</td></tr><tr><td><p><span class="term"><a name="GDK-WINDOW-TOPLEVEL:CAPS"></a><code class="literal">GDK_WINDOW_TOPLEVEL</code></span></p></td><td>toplevel window (used to implement <ahref="/usr/share/gtk-doc/html/gtk/GtkWindow.html"><span class="type">GtkWindow</span></a>)</td></tr><tr><td><p><span class="term"><a name="GDK-WINDOW-CHILD:CAPS"></a><code class="literal">GDK_WINDOW_CHILD</code></span></p></td><td>child window (used to implement e.g. <ahref="/usr/share/gtk-doc/html/gtk/GtkEntry.html"><span class="type">GtkEntry</span></a>)</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -