+/* There are multiple alpha related sources. First of all the user can specify alpha
+ * in gtk_widget_set_opacity, secondly we can get it from the css opacity. These two
+ * are multiplied together to form the total alpha. Secondly, the user can specify
+ * an opacity group for a widget, which means we must essentially handle it as having alpha.
+ *
+ * We handle opacity in two ways. For a windowed widget, with opacity set but no opacity
+ * group we directly set the opacity of widget->window. This will cause gdk to properly
+ * redirect drawing inside the window to a buffer and do OVER paint_with_alpha.
+ *
+ * However, if the widget is not windowed, or the user specified an opacity group for the
+ * widget we do the opacity handling in the ::draw marshaller for the widget. A naive
+ * implementation of this would break for windowed widgets or descendant widgets with
+ * windows, as these would not be handled by the ::draw signal. To handle this we set
+ * all such gdkwindows as fully transparent and then override gtk_cairo_should_draw_window()
+ * to make the draw signal propagate to *all* child widgets/windows.
+ *
+ * Note: We don't make all child windows fully transparent, we stop at the first one
+ * in each branch when propagating down the hierarchy.
+ */
+
+
+/* This is called when priv->alpha or priv->opacity_group group changes, and should
+ * update priv->norender and GdkWindow opacity for this widget and any children that
+ * needs changing. It is also called whenver the parent changes, the parents
+ * norender_children state changes, or the has_window state of the widget changes.
+ */
+static void
+gtk_widget_propagate_alpha (GtkWidget *widget)
+{
+ GtkWidgetPrivate *priv = widget->priv;
+ GtkWidget *parent;
+ gboolean norender, norender_children;
+ GList *l;
+
+ parent = priv->parent;
+
+ /* Norender affects only windowed widget and means don't render widget->window in the
+ normal fashion.
+ We only set this if the parent has norender_children, because:
+ a) For an opacity group (that does not have a norender_children parent) we still
+ need to render the window or we will never get an expose event.
+ b) For alpha we set the opacity of window->widget directly, so no other
+ work is needed.
+ */
+ norender = (parent != NULL && parent->priv->norender_children);
+
+ /* windows under this widget should not render if:
+ a) This widget has an opacity group
+ b) This widget has alpha and is no-windowed (otherwise we'd set alpha on widget->window)
+ c) This widget has norender but is no-windowed (a windowed widget would "swallow" the norender)
+ */
+ norender_children =
+ priv->opacity_group ||
+ (!gtk_widget_get_has_window (widget) &&
+ ( norender || priv->alpha != 255));
+
+ if (gtk_widget_get_has_window (widget))
+ {
+ if (priv->window != NULL && !gdk_window_has_native (priv->window))
+ gdk_window_set_opacity (priv->window,
+ norender ? 0 : priv->alpha / 255.0);
+ }
+
+ for (l = priv->registered_windows; l != NULL; l = l->next)
+ {
+ GdkWindow *w = l->data;
+ if (w != priv->window && !gdk_window_has_native (w))
+ gdk_window_set_opacity (w, norender_children ? 0.0 : 1.0);
+ }
+
+ priv->norender = norender;
+ if (priv->norender_children != norender_children)
+ {
+ priv->norender_children = norender_children;
+
+ if (GTK_IS_CONTAINER (widget))
+ gtk_container_forall (GTK_CONTAINER (widget), (GtkCallback)gtk_widget_propagate_alpha, NULL);
+ }
+
+ if (gtk_widget_get_realized (widget))
+ gtk_widget_queue_draw (widget);
+}
+
+static void
+gtk_widget_update_alpha (GtkWidget *widget)
+{
+ GtkWidgetPrivate *priv;
+ double opacity;
+ guint8 alpha;
+
+ priv = widget->priv;
+
+ alpha = priv->user_alpha;
+
+ if (priv->context)
+ {
+ opacity =
+ _gtk_css_number_value_get (_gtk_style_context_peek_property (priv->context,
+ GTK_CSS_PROPERTY_OPACITY),
+ 100);
+ opacity = CLAMP (opacity, 0.0, 1.0);
+ alpha = round (priv->user_alpha * opacity);
+ }
+
+ if (alpha == priv->alpha)
+ return;
+
+ priv->alpha = alpha;
+
+ gtk_widget_propagate_alpha (widget);
+
+}
+
+static void
+gtk_widget_set_has_opacity_group (GtkWidget *widget,
+ gboolean has_opacity_group)
+{
+ GtkWidgetPrivate *priv;
+
+ g_return_if_fail (GTK_IS_WIDGET (widget));
+
+ priv = widget->priv;
+
+ has_opacity_group = !!has_opacity_group;
+
+ if (priv->opacity_group == has_opacity_group)
+ return;
+
+ priv->opacity_group = has_opacity_group;
+
+ gtk_widget_propagate_alpha (widget);
+}
+
+/**
+ * gtk_widget_set_opacity:
+ * @widget: a #GtkWidget
+ * @opacity: desired opacity, between 0 and 1
+ *
+ * Request the @widget to be rendered partially transparent,
+ * with opacity 0 being fully transparent and 1 fully opaque. (Opacity values
+ * are clamped to the [0,1] range.).
+ * This works on both toplevel widget, and child widgets, although there
+ * are some limitations:
+ *
+ * For toplevel widgets this depends on the capabilities of the windowing
+ * system. On X11 this has any effect only on X screens with a compositing manager
+ * running. See gtk_widget_is_composited(). On Windows it should work
+ * always, although setting a window's opacity after the window has been
+ * shown causes it to flicker once on Windows.
+ *
+ * For child widgets it doesn't work if any affected widget has a native window, or
+ * disables double buffering.
+ *
+ * Since: 3.8
+ **/
+void
+gtk_widget_set_opacity (GtkWidget *widget,
+ gdouble opacity)
+{
+ GtkWidgetPrivate *priv;
+ guint8 alpha;
+
+ g_return_if_fail (GTK_IS_WIDGET (widget));
+
+ priv = widget->priv;
+
+ opacity = CLAMP (opacity, 0.0, 1.0);
+
+ alpha = round (opacity * 255);
+
+ /* As a kind of hack for internal use we treat an alpha very
+ close to 1.0 (rounds to 255) but not 1.0 as specifying that
+ we want the opacity group behaviour wrt draw handling, but
+ not actually an alpha value. See bug #687842 for discussions. */
+ gtk_widget_set_has_opacity_group (widget,
+ alpha == 255 && opacity != 1.0);
+
+ if (alpha == priv->user_alpha)
+ return;
+
+ priv->user_alpha = alpha;
+
+ gtk_widget_update_alpha (widget);
+
+}
+
+/**
+ * gtk_widget_get_opacity:
+ * @widget: a #GtkWidget
+ *
+ * Fetches the requested opacity for this widget. See
+ * gtk_widget_set_opacity().
+ *
+ * Return value: the requested opacity for this widget.
+ *
+ * Since: 3.8
+ **/
+gdouble
+gtk_widget_get_opacity (GtkWidget *widget)
+{
+ g_return_val_if_fail (GTK_IS_WIDGET (widget), 0.0);
+
+ return widget->priv->alpha / 255.0;
+}
+