Windows

## Windows Windows — Onscreen display areas in the target window system |

## Includes

    #include <gdk/gdk.h>

## Description

A GdkWindow is a rectangular region on the screen. It's a low-level object, used to implement high-level objects such as GtkWidget and GtkWindow on the GTK+ level. A GtkWindow is a toplevel window, the thing a user might think of as a "window" with a titlebar and so on; a GtkWindow may contain many GdkWindow. For example, each GtkButton has a GdkWindow associated with it.

### Composited Windows

Normally, the windowing system takes care of rendering the contents of a child window onto its parent window. This mechanism can be intercepted by calling `gdk_window_set_composited()` on the child window. For a composited window it is the responsibility of the application to render the window contents at the right spot.

Example 4. Composited windows

    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
    #include <gtk/gtk.h>
    /* The expose event handler for the event box.
     *
     * This function simply draws a transparency onto a widget on the area
     * for which it receives expose events.  This is intended to give the
     * event box a "transparent" background.
     *
     * In order for this to work properly, the widget must have an RGBA
     * colourmap.  The widget should also be set as app-paintable since it
     * doesn't make sense for GTK+ to draw a background if we are drawing it
     * (and because GTK+ might actually replace our transparency with its
     * default background colour).
     */
    static gboolean
    transparent_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_connect_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.6
     * feature to make this work correctly.
     */
    static gboolean
    window_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;
    }
    int
    main (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;
    }

In the example Example 4, “Composited windows”, 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.

When the contents of the event box change, an expose event is generated on its parent window (which, in this case, belongs to the toplevel GtkWindow). The expose handler for this widget is responsible for merging the changes back on the screen in the way that it wishes.

In our case, we merge the contents with a 50% transparency. We also set the background colour of the window to red. The effect is that the background shows through the button.

### Offscreen Windows

Offscreen windows are more general than composited windows, since they allow not only to modify the rendering of the child window onto its parent, but also to apply coordinate transformations.

To integrate an offscreen window into a window hierarchy, one has to call `gdk_offscreen_window_set_embedder()` and handle a number of signals. The “pick-embedded-child” signal on the embedder window is used to select an offscreen child at given coordinates, and the “to-embedder” and “from-embedder” signals on the offscreen window are used to translate coordinates between the embedder and the offscreen window.

For rendering an offscreen window onto its embedder, the contents of the offscreen window are available as a pixmap, via `gdk_offscreen_window_get_pixmap()`.

## Functions

## Types and Values