[nautilus] overlay: borrow GeditOverlay implementation from gedit



commit c16127a438ff9654cbd63dbbc3859dc9916d0160
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Fri Feb 11 15:44:10 2011 -0500

    overlay: borrow GeditOverlay implementation from gedit

 src/Makefile.am           |    4 +
 src/gedit-overlay-child.c |  407 +++++++++++++++++++++++++++
 src/gedit-overlay-child.h |   89 ++++++
 src/gedit-overlay.c       |  687 +++++++++++++++++++++++++++++++++++++++++++++
 src/gedit-overlay.h       |   70 +++++
 5 files changed, 1257 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 13efcf6..a79978c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -49,6 +49,10 @@ BUILT_SOURCES = \
 	$(NULL)
 
 nautilus_SOURCES = \
+	gedit-overlay.h				\
+	gedit-overlay.c				\
+	gedit-overlay-child.h			\
+	gedit-overlay-child.c			\
 	nautilus-actions.h			\
 	nautilus-application.c			\
 	nautilus-application.h			\
diff --git a/src/gedit-overlay-child.c b/src/gedit-overlay-child.c
new file mode 100644
index 0000000..c460c7f
--- /dev/null
+++ b/src/gedit-overlay-child.c
@@ -0,0 +1,407 @@
+/*
+ * gedit-overlay-child.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2011 - Ignacio Casal Quinteiro
+ *
+ * gedit is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * gedit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "gedit-overlay-child.h"
+
+struct _GeditOverlayChildPrivate
+{
+	GtkWidget                *widget;
+	GtkAllocation             widget_alloc;
+	GeditOverlayChildPosition position;
+	guint                     offset;
+	gboolean                  fixed;
+};
+
+enum
+{
+	PROP_0,
+	PROP_WIDGET,
+	PROP_POSITION,
+	PROP_OFFSET,
+	PROP_FIXED
+};
+
+G_DEFINE_TYPE (GeditOverlayChild, gedit_overlay_child, GTK_TYPE_BIN)
+
+static void
+gedit_overlay_child_get_property (GObject    *object,
+                                  guint       prop_id,
+                                  GValue     *value,
+                                  GParamSpec *pspec)
+{
+	GeditOverlayChild *child = GEDIT_OVERLAY_CHILD (object);
+
+	switch (prop_id)
+	{
+		case PROP_WIDGET:
+			g_value_set_object (value, child->priv->widget);
+			break;
+		case PROP_POSITION:
+			g_value_set_uint (value, child->priv->position);
+			break;
+		case PROP_OFFSET:
+			g_value_set_uint (value, child->priv->offset);
+			break;
+		case PROP_FIXED:
+			g_value_set_boolean (value, child->priv->fixed);
+			break;
+		default:
+			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+			break;
+	}
+}
+
+static void
+gedit_overlay_child_set_property (GObject      *object,
+                                  guint         prop_id,
+                                  const GValue *value,
+                                  GParamSpec   *pspec)
+{
+	GeditOverlayChild *child = GEDIT_OVERLAY_CHILD (object);
+
+	switch (prop_id)
+	{
+		case PROP_WIDGET:
+			gtk_container_add (GTK_CONTAINER (child),
+			                   g_value_get_object (value));
+			break;
+		case PROP_POSITION:
+			child->priv->position = g_value_get_uint (value);
+			break;
+		case PROP_OFFSET:
+			child->priv->offset = g_value_get_uint (value);
+			break;
+		case PROP_FIXED:
+			child->priv->fixed = g_value_get_boolean (value);
+			break;
+		default:
+			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+			break;
+	}
+}
+
+static void
+gedit_overlay_child_realize (GtkWidget *widget)
+{
+	GdkWindowAttr attributes;
+	GdkWindow *parent_window;
+	GdkWindow *window;
+	GtkStyleContext *context;
+
+	gtk_widget_set_realized (widget, TRUE);
+
+	parent_window = gtk_widget_get_parent_window (widget);
+	context = gtk_widget_get_style_context (widget);
+
+	attributes.window_type = GDK_WINDOW_CHILD;
+	attributes.wclass = GDK_INPUT_OUTPUT;
+	attributes.event_mask = GDK_EXPOSURE_MASK;
+
+	window = gdk_window_new (parent_window, &attributes, 0);
+	gdk_window_set_user_data (window, widget);
+	gtk_widget_set_window (widget, window);
+	gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
+	gtk_style_context_set_background (context, window);
+}
+
+static void
+gedit_overlay_child_get_preferred_width (GtkWidget *widget,
+                                         gint      *minimum,
+                                         gint      *natural)
+{
+	GeditOverlayChild *child = GEDIT_OVERLAY_CHILD (widget);
+	gint width;
+
+	if (child->priv->widget != NULL)
+	{
+		gint child_min, child_nat;
+
+		gtk_widget_get_preferred_width (child->priv->widget,
+		                                &child_min, &child_nat);
+		child->priv->widget_alloc.width = child_min;
+	}
+
+	width = child->priv->widget_alloc.width;
+	*minimum = *natural = width;
+}
+
+static void
+gedit_overlay_child_get_preferred_height (GtkWidget *widget,
+                                          gint      *minimum,
+                                          gint      *natural)
+{
+	GeditOverlayChild *child = GEDIT_OVERLAY_CHILD (widget);
+	gint height;
+
+	if (child->priv->widget != NULL)
+	{
+		gint child_min, child_nat;
+
+		gtk_widget_get_preferred_height (child->priv->widget,
+		                                 &child_min, &child_nat);
+		child->priv->widget_alloc.height = child_min;
+	}
+
+	height = child->priv->widget_alloc.height;
+	*minimum = *natural = height;
+}
+
+static void
+gedit_overlay_child_size_allocate (GtkWidget     *widget,
+                                   GtkAllocation *allocation)
+{
+	GeditOverlayChild *child = GEDIT_OVERLAY_CHILD (widget);
+
+	GTK_WIDGET_CLASS (gedit_overlay_child_parent_class)->size_allocate (widget, allocation);
+
+	if (child->priv->widget != NULL &&
+	    child->priv->widget_alloc.height &&
+	    child->priv->widget_alloc.width)
+	{
+		gtk_widget_size_allocate (child->priv->widget,
+		                          &child->priv->widget_alloc);
+	}
+}
+
+static void
+gedit_overlay_child_add (GtkContainer *container,
+                         GtkWidget    *widget)
+{
+	GeditOverlayChild *child = GEDIT_OVERLAY_CHILD (container);
+
+	child->priv->widget = widget;
+
+	GTK_CONTAINER_CLASS (gedit_overlay_child_parent_class)->add (container, widget);
+}
+
+static void
+gedit_overlay_child_remove (GtkContainer *container,
+                            GtkWidget    *widget)
+{
+	GeditOverlayChild *child = GEDIT_OVERLAY_CHILD (container);
+
+	child->priv->widget = NULL;
+
+	GTK_CONTAINER_CLASS (gedit_overlay_child_parent_class)->remove (container, widget);
+}
+
+static void
+gedit_overlay_child_class_init (GeditOverlayChildClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+	GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
+	
+	object_class->get_property = gedit_overlay_child_get_property;
+	object_class->set_property = gedit_overlay_child_set_property;
+
+	widget_class->realize = gedit_overlay_child_realize;
+	widget_class->get_preferred_width = gedit_overlay_child_get_preferred_width;
+	widget_class->get_preferred_height = gedit_overlay_child_get_preferred_height;
+	widget_class->size_allocate = gedit_overlay_child_size_allocate;
+
+	container_class->add = gedit_overlay_child_add;
+	container_class->remove = gedit_overlay_child_remove;
+
+	g_object_class_install_property (object_class, PROP_WIDGET,
+	                                 g_param_spec_object ("widget",
+	                                                      "Widget",
+	                                                      "The Widget",
+	                                                      GTK_TYPE_WIDGET,
+	                                                      G_PARAM_READWRITE |
+	                                                      G_PARAM_CONSTRUCT_ONLY |
+	                                                      G_PARAM_STATIC_STRINGS));
+
+	g_object_class_install_property (object_class, PROP_POSITION,
+	                                 g_param_spec_uint ("position",
+	                                                    "Position",
+	                                                    "The Widget Position",
+	                                                    1, GEDIT_OVERLAY_CHILD_POSITION_STATIC,
+	                                                    GEDIT_OVERLAY_CHILD_POSITION_STATIC,
+	                                                    G_PARAM_READWRITE |
+	                                                    G_PARAM_CONSTRUCT |
+	                                                    G_PARAM_STATIC_STRINGS));
+
+	g_object_class_install_property (object_class, PROP_OFFSET,
+	                                 g_param_spec_uint ("offset",
+	                                                    "Offset",
+	                                                    "The Widget Offset",
+	                                                    0,
+	                                                    G_MAXUINT,
+	                                                    0,
+	                                                    G_PARAM_READWRITE |
+	                                                    G_PARAM_CONSTRUCT |
+	                                                    G_PARAM_STATIC_STRINGS));
+
+	g_object_class_install_property (object_class, PROP_FIXED,
+	                                 g_param_spec_boolean ("fixed",
+	                                                       "Fixed",
+	                                                       "Wether the Widget is in a fixed position",
+	                                                       TRUE,
+	                                                       G_PARAM_READWRITE |
+	                                                       G_PARAM_CONSTRUCT |
+	                                                       G_PARAM_STATIC_STRINGS));
+
+	g_type_class_add_private (object_class, sizeof (GeditOverlayChildPrivate));
+}
+
+static void
+gedit_overlay_child_init (GeditOverlayChild *child)
+{
+	child->priv = G_TYPE_INSTANCE_GET_PRIVATE (child,
+	                                           GEDIT_TYPE_OVERLAY_CHILD,
+	                                           GeditOverlayChildPrivate);
+
+	gtk_widget_set_has_window (GTK_WIDGET (child), TRUE);
+}
+
+/**
+ * gedit_overlay_child_new:
+ * @widget: a #GtkWidget
+ *
+ * Creates a new #GeditOverlayChild object
+ *
+ * Returns: a new #GeditOverlayChild object
+ */
+GeditOverlayChild *
+gedit_overlay_child_new (GtkWidget *widget)
+{
+	return g_object_new (GEDIT_TYPE_OVERLAY_CHILD,
+	                     "widget", widget,
+	                     NULL);
+}
+
+/**
+ * gedit_overlay_child_get_position:
+ * @child: a #GeditOverlayChild
+ *
+ * Gets the position of the widget
+ *
+ * Returns: wether the child should be placed in a #GeditOverlay
+ */
+GeditOverlayChildPosition
+gedit_overlay_child_get_position (GeditOverlayChild *child)
+{
+	g_return_val_if_fail (GEDIT_IS_OVERLAY_CHILD (child), GEDIT_OVERLAY_CHILD_POSITION_STATIC);
+
+	return child->priv->position;
+}
+
+/**
+ * gedit_overlay_child_set_position:
+ * @child: a #GeditOverlayChild
+ * @position: a #GeditOverlayChildPosition
+ *
+ * Sets the new position for @child
+ */
+void
+gedit_overlay_child_set_position (GeditOverlayChild        *child,
+                                  GeditOverlayChildPosition position)
+{
+	g_return_if_fail (GEDIT_IS_OVERLAY_CHILD (child));
+
+	if (child->priv->position != position)
+	{
+		child->priv->position = position;
+
+		g_object_notify (G_OBJECT (child), "position");
+	}
+}
+
+/**
+ * gedit_overlay_child_get_offset:
+ * @child: a #GeditOverlayChild
+ *
+ * Gets the offset for @child. The offset is usually used by #GeditOverlay
+ * to not place the widget directly in the border of the container
+ *
+ * Returns: the offset for @child
+ */
+guint
+gedit_overlay_child_get_offset (GeditOverlayChild *child)
+{
+	g_return_val_if_fail (GEDIT_IS_OVERLAY_CHILD (child), 0);
+
+	return child->priv->offset;
+}
+
+/**
+ * gedit_overlay_child_set_offset:
+ * @child: a #GeditOverlayChild
+ * @offset: the offset for @child
+ *
+ * Sets the new offset for @child
+ */
+void
+gedit_overlay_child_set_offset (GeditOverlayChild *child,
+                                guint              offset)
+{
+	g_return_if_fail (GEDIT_IS_OVERLAY_CHILD (child));
+
+	if (child->priv->offset != offset)
+	{
+		child->priv->offset = offset;
+
+		g_object_notify (G_OBJECT (child), "offset");
+	}
+}
+
+/**
+ * gedit_overlay_child_get_fixed:
+ * @child: a #GeditOverlayChild
+ *
+ * Gets wether @child is fixed in its position. If @child is not fixed the position
+ * will change when for example you scroll the container.
+ *
+ * Returns: wether @child is fixed in its position
+ */
+gboolean
+gedit_overlay_child_get_fixed (GeditOverlayChild *child)
+{
+	g_return_val_if_fail (GEDIT_IS_OVERLAY_CHILD (child), TRUE);
+
+	return child->priv->fixed;
+}
+
+/**
+ * gedit_overlay_child_set_fixed:
+ * @child: a #GeditOverlayChild
+ * @fixed: wether @child is in a fixed position
+ *
+ * Sets wether @child is in a fixed position
+ */
+void
+gedit_overlay_child_set_fixed (GeditOverlayChild *child,
+                               gboolean           fixed)
+{
+	g_return_if_fail (GEDIT_IS_OVERLAY_CHILD (child));
+
+	fixed = (fixed != FALSE);
+
+	if (child->priv->fixed != fixed)
+	{
+		child->priv->fixed = fixed;
+
+		g_object_notify (G_OBJECT (child), "fixed");
+	}
+}
+
+/* ex:set ts=8 noet: */
diff --git a/src/gedit-overlay-child.h b/src/gedit-overlay-child.h
new file mode 100644
index 0000000..86e051b
--- /dev/null
+++ b/src/gedit-overlay-child.h
@@ -0,0 +1,89 @@
+/*
+ * gedit-overlay-child.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2011 - Ignacio Casal Quinteiro
+ *
+ * gedit is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * gedit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GEDIT_OVERLAY_CHILD_H__
+#define __GEDIT_OVERLAY_CHILD_H__
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_OVERLAY_CHILD		(gedit_overlay_child_get_type ())
+#define GEDIT_OVERLAY_CHILD(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_OVERLAY_CHILD, GeditOverlayChild))
+#define GEDIT_OVERLAY_CHILD_CONST(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_OVERLAY_CHILD, GeditOverlayChild const))
+#define GEDIT_OVERLAY_CHILD_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), GEDIT_TYPE_OVERLAY_CHILD, GeditOverlayChildClass))
+#define GEDIT_IS_OVERLAY_CHILD(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_OVERLAY_CHILD))
+#define GEDIT_IS_OVERLAY_CHILD_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_OVERLAY_CHILD))
+#define GEDIT_OVERLAY_CHILD_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GEDIT_TYPE_OVERLAY_CHILD, GeditOverlayChildClass))
+
+typedef struct _GeditOverlayChild		GeditOverlayChild;
+typedef struct _GeditOverlayChildClass		GeditOverlayChildClass;
+typedef struct _GeditOverlayChildPrivate	GeditOverlayChildPrivate;
+
+struct _GeditOverlayChild
+{
+	GtkBin parent;
+
+	GeditOverlayChildPrivate *priv;
+};
+
+struct _GeditOverlayChildClass
+{
+	GtkBinClass parent_class;
+};
+
+typedef enum
+{
+	GEDIT_OVERLAY_CHILD_POSITION_NORTH_WEST = 1,
+	GEDIT_OVERLAY_CHILD_POSITION_NORTH,
+	GEDIT_OVERLAY_CHILD_POSITION_NORTH_EAST,
+	GEDIT_OVERLAY_CHILD_POSITION_WEST,
+	GEDIT_OVERLAY_CHILD_POSITION_CENTER,
+	GEDIT_OVERLAY_CHILD_POSITION_EAST,
+	GEDIT_OVERLAY_CHILD_POSITION_SOUTH_WEST,
+	GEDIT_OVERLAY_CHILD_POSITION_SOUTH,
+	GEDIT_OVERLAY_CHILD_POSITION_SOUTH_EAST,
+	GEDIT_OVERLAY_CHILD_POSITION_STATIC
+} GeditOverlayChildPosition;
+
+GType                     gedit_overlay_child_get_type     (void) G_GNUC_CONST;
+
+GeditOverlayChild        *gedit_overlay_child_new          (GtkWidget *widget);
+
+GeditOverlayChildPosition gedit_overlay_child_get_position (GeditOverlayChild *child);
+
+void                      gedit_overlay_child_set_position (GeditOverlayChild        *child,
+                                                            GeditOverlayChildPosition position);
+
+guint                     gedit_overlay_child_get_offset   (GeditOverlayChild *child);
+
+void                      gedit_overlay_child_set_offset   (GeditOverlayChild *child,
+                                                            guint              offset);
+
+gboolean                  gedit_overlay_child_get_fixed    (GeditOverlayChild *child);
+
+void                      gedit_overlay_child_set_fixed    (GeditOverlayChild *child,
+                                                            gboolean           fixed);
+
+G_END_DECLS
+
+#endif /* __GEDIT_OVERLAY_CHILD_H__ */
diff --git a/src/gedit-overlay.c b/src/gedit-overlay.c
new file mode 100644
index 0000000..6a9e3e6
--- /dev/null
+++ b/src/gedit-overlay.c
@@ -0,0 +1,687 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+ * gedit-overlay.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2011 - Ignacio Casal Quinteiro
+ *
+ * Based on Mike Krüger <mkrueger novell com> work.
+ *
+ * gedit is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * gedit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "gedit-overlay.h"
+#include "gedit-overlay-child.h"
+
+#define GEDIT_OVERLAY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GEDIT_TYPE_OVERLAY, GeditOverlayPrivate))
+
+struct _GeditOverlayPrivate
+{
+	GtkWidget *main_widget;
+	GSList    *children;
+	GtkAllocation main_alloc;
+
+	GtkAdjustment *hadjustment;
+	GtkAdjustment *vadjustment;
+	glong          hadjustment_signal_id;
+	glong          vadjustment_signal_id;
+
+	/* GtkScrollablePolicy needs to be checked when
+	 * driving the scrollable adjustment values */
+	guint hscroll_policy : 1;
+	guint vscroll_policy : 1;
+};
+
+enum
+{
+	PROP_0,
+	PROP_MAIN_WIDGET,
+	PROP_HADJUSTMENT,
+	PROP_VADJUSTMENT,
+	PROP_HSCROLL_POLICY,
+	PROP_VSCROLL_POLICY
+};
+
+static void	gedit_overlay_set_hadjustment		(GeditOverlay  *overlay,
+							 GtkAdjustment *adjustment);
+static void	gedit_overlay_set_vadjustment		(GeditOverlay  *overlay,
+							 GtkAdjustment *adjustment);
+
+G_DEFINE_TYPE_WITH_CODE (GeditOverlay, gedit_overlay, GTK_TYPE_CONTAINER,
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
+
+static void
+add_toplevel_widget (GeditOverlay *overlay,
+                     GtkWidget    *child)
+{
+	gtk_widget_set_parent (child, GTK_WIDGET (overlay));
+
+	overlay->priv->children = g_slist_append (overlay->priv->children,
+	                                          child);
+}
+
+static void
+gedit_overlay_finalize (GObject *object)
+{
+	GeditOverlay *overlay = GEDIT_OVERLAY (object);
+
+	g_slist_free (overlay->priv->children);
+        overlay->priv->children = NULL;
+
+	G_OBJECT_CLASS (gedit_overlay_parent_class)->finalize (object);
+}
+
+static void
+gedit_overlay_dispose (GObject *object)
+{
+	GeditOverlay *overlay = GEDIT_OVERLAY (object);
+
+	if (overlay->priv->hadjustment != NULL)
+	{
+		g_signal_handler_disconnect (overlay->priv->hadjustment,
+		                             overlay->priv->hadjustment_signal_id);
+		overlay->priv->hadjustment = NULL;
+	}
+
+	if (overlay->priv->vadjustment != NULL)
+	{
+		g_signal_handler_disconnect (overlay->priv->vadjustment,
+		                             overlay->priv->vadjustment_signal_id);
+		overlay->priv->vadjustment = NULL;
+	}
+
+	G_OBJECT_CLASS (gedit_overlay_parent_class)->dispose (object);
+}
+
+static void
+gedit_overlay_get_property (GObject    *object,
+                            guint       prop_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
+{
+	GeditOverlay *overlay = GEDIT_OVERLAY (object);
+	GeditOverlayPrivate *priv = overlay->priv;
+
+	switch (prop_id)
+	{
+		case PROP_MAIN_WIDGET:
+			g_value_set_object (value, priv->main_widget);
+			break;
+
+		case PROP_HADJUSTMENT:
+			g_value_set_object (value, priv->hadjustment);
+			break;
+
+		case PROP_VADJUSTMENT:
+			g_value_set_object (value, priv->vadjustment);
+			break;
+
+		case PROP_HSCROLL_POLICY:
+			if (GTK_IS_SCROLLABLE (priv->main_widget))
+			{
+				g_value_set_enum (value,
+				                  gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (priv->main_widget)));
+			}
+			else
+			{
+				g_value_set_enum (value, priv->hscroll_policy);
+			}
+			break;
+
+		case PROP_VSCROLL_POLICY:
+			if (GTK_IS_SCROLLABLE (priv->main_widget))
+			{
+				g_value_set_enum (value,
+				                  gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (priv->main_widget)));
+			}
+			else
+			{
+				g_value_set_enum (value, priv->vscroll_policy);
+			}
+			break;
+
+		default:
+			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+			break;
+	}
+}
+
+static void
+gedit_overlay_set_property (GObject      *object,
+                            guint         prop_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+	GeditOverlay *overlay = GEDIT_OVERLAY (object);
+	GeditOverlayPrivate *priv = overlay->priv;
+
+	switch (prop_id)
+	{
+		case PROP_MAIN_WIDGET:
+			overlay->priv->main_widget = g_value_get_object (value);
+			add_toplevel_widget (overlay,
+			                     overlay->priv->main_widget);
+			break;
+
+		case PROP_HADJUSTMENT:
+			gedit_overlay_set_hadjustment (overlay,
+						       g_value_get_object (value));
+			break;
+
+		case PROP_VADJUSTMENT:
+			gedit_overlay_set_vadjustment (overlay,
+						       g_value_get_object (value));
+			break;
+
+		case PROP_HSCROLL_POLICY:
+			if (GTK_IS_SCROLLABLE (priv->main_widget))
+			{
+				gtk_scrollable_set_hscroll_policy (GTK_SCROLLABLE (priv->main_widget),
+				                                   g_value_get_uint (value));
+			}
+			else
+			{
+				priv->hscroll_policy = g_value_get_enum (value);
+				gtk_widget_queue_resize (GTK_WIDGET (overlay));
+			}
+			break;
+
+		case PROP_VSCROLL_POLICY:
+			if (GTK_IS_SCROLLABLE (priv->main_widget))
+			{
+				gtk_scrollable_set_vscroll_policy (GTK_SCROLLABLE (priv->main_widget),
+				                                   g_value_get_enum (value));
+			}
+			else
+			{
+				priv->vscroll_policy = g_value_get_enum (value);
+				gtk_widget_queue_resize (GTK_WIDGET (overlay));
+			}
+			break;
+
+		default:
+			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+			break;
+	}
+}
+
+static void
+gedit_overlay_realize (GtkWidget *widget)
+{
+	GtkAllocation allocation;
+	GdkWindow *window;
+	GdkWindowAttr attributes;
+	gint attributes_mask;
+	GtkStyleContext *context;
+
+	gtk_widget_set_realized (widget, TRUE);
+
+	gtk_widget_get_allocation (widget, &allocation);
+
+	attributes.window_type = GDK_WINDOW_CHILD;
+	attributes.x = allocation.x;
+	attributes.y = allocation.y;
+	attributes.width = allocation.width;
+	attributes.height = allocation.height;
+	attributes.wclass = GDK_INPUT_OUTPUT;
+	attributes.visual = gtk_widget_get_visual (widget);
+	attributes.event_mask = gtk_widget_get_events (widget);
+	attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
+
+	attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
+
+	window = gdk_window_new (gtk_widget_get_parent_window (widget),
+	                         &attributes, attributes_mask);
+	gtk_widget_set_window (widget, window);
+	gdk_window_set_user_data (window, widget);
+
+	context = gtk_widget_get_style_context (widget);
+	gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
+	gtk_style_context_set_background (context, window);
+}
+
+static void
+gedit_overlay_get_preferred_width (GtkWidget *widget,
+                                   gint      *minimum,
+                                   gint      *natural)
+{
+	GeditOverlayPrivate *priv = GEDIT_OVERLAY (widget)->priv;
+	GtkWidget *child;
+	GSList *children;
+	gint child_min, child_nat;
+
+	*minimum = 0;
+	*natural = 0;
+
+	for (children = priv->children; children; children = children->next)
+	{
+		child = children->data;
+
+		if (!gtk_widget_get_visible (child))
+			continue;
+
+		gtk_widget_get_preferred_width (child, &child_min, &child_nat);
+
+		*minimum = MAX (*minimum, child_min);
+		*natural = MAX (*natural, child_nat);
+	}
+}
+
+static void
+gedit_overlay_get_preferred_height (GtkWidget *widget,
+                                    gint      *minimum,
+                                    gint      *natural)
+{
+	GeditOverlayPrivate *priv = GEDIT_OVERLAY (widget)->priv;
+	GtkWidget *child;
+	GSList *children;
+	gint child_min, child_nat;
+
+	*minimum = 0;
+	*natural = 0;
+
+	for (children = priv->children; children; children = children->next)
+	{
+		child = children->data;
+
+		if (!gtk_widget_get_visible (child))
+			continue;
+
+		gtk_widget_get_preferred_height (child, &child_min, &child_nat);
+
+		*minimum = MAX (*minimum, child_min);
+		*natural = MAX (*natural, child_nat);
+	}
+}
+
+static void
+set_children_positions (GeditOverlay *overlay)
+{
+	GSList *l;
+
+	for (l = overlay->priv->children; l != NULL; l = g_slist_next (l))
+	{
+		GeditOverlayPrivate *priv = overlay->priv;
+		GtkWidget *child = GTK_WIDGET (l->data);
+		GtkRequisition req;
+		GtkAllocation alloc;
+		guint offset;
+
+		if (child == priv->main_widget)
+			continue;
+
+		gtk_widget_get_preferred_size (child, &req, NULL);
+		offset = gedit_overlay_child_get_offset (GEDIT_OVERLAY_CHILD (child));
+
+		switch (gedit_overlay_child_get_position (GEDIT_OVERLAY_CHILD (child)))
+		{
+			/* The gravity is treated as position and not as a gravity */
+			case GEDIT_OVERLAY_CHILD_POSITION_NORTH_EAST:
+				alloc.x = priv->main_alloc.width - req.width - offset;
+				alloc.y = 0;
+				break;
+			case GEDIT_OVERLAY_CHILD_POSITION_NORTH_WEST:
+				alloc.x = offset;
+				alloc.y = 0;
+				break;
+			case GEDIT_OVERLAY_CHILD_POSITION_SOUTH_WEST:
+				alloc.x = offset;
+				alloc.y = priv->main_alloc.height - req.height;
+				break;
+                case GEDIT_OVERLAY_CHILD_POSITION_SOUTH_EAST:
+                  alloc.x = priv->main_alloc.width - req.width - offset;
+                  alloc.y = priv->main_alloc.height - req.height;
+                  break;
+			default:
+				alloc.x = 0;
+				alloc.y = 0;
+		}
+
+		if (!gedit_overlay_child_get_fixed (GEDIT_OVERLAY_CHILD (child)))
+		{
+			alloc.x *= gtk_adjustment_get_value (priv->hadjustment);
+			alloc.y *= gtk_adjustment_get_value (priv->vadjustment);
+		}
+
+		alloc.width = req.width;
+		alloc.height = req.height;
+
+		gtk_widget_size_allocate (child, &alloc);
+	}
+}
+
+static void
+gedit_overlay_size_allocate (GtkWidget     *widget,
+                             GtkAllocation *allocation)
+{
+	GeditOverlay *overlay = GEDIT_OVERLAY (widget);
+
+	GTK_WIDGET_CLASS (gedit_overlay_parent_class)->size_allocate (widget, allocation);
+
+	overlay->priv->main_alloc.x = 0;
+	overlay->priv->main_alloc.y = 0;
+	overlay->priv->main_alloc.width = allocation->width;
+	overlay->priv->main_alloc.height = allocation->height;
+
+	gtk_widget_size_allocate (overlay->priv->main_widget,
+	                          &overlay->priv->main_alloc);
+	set_children_positions (overlay);
+}
+
+static GeditOverlayChild *
+get_overlay_child (GeditOverlay *overlay,
+                   GtkWidget    *widget)
+{
+	GeditOverlayChild *overlay_child = NULL;
+	GSList *l;
+
+	for (l = overlay->priv->children; l != NULL; l = g_slist_next (l))
+	{
+		GtkWidget *child = GTK_WIDGET (l->data);
+
+		/* skip the main widget as it is not a OverlayChild */
+		if (child == overlay->priv->main_widget)
+			continue;
+
+		if (child == widget)
+		{
+			overlay_child = GEDIT_OVERLAY_CHILD (child);
+			break;
+		}
+		else
+		{
+			GtkWidget *in_widget;
+
+			/* let's try also with the internal widget */
+			g_object_get (child, "widget", &in_widget, NULL);
+			g_assert (in_widget != NULL);
+
+			if (in_widget == widget)
+			{
+				overlay_child = GEDIT_OVERLAY_CHILD (child);
+				break;
+			}
+		}
+	}
+
+	return overlay_child;
+}
+
+static void
+overlay_add (GtkContainer *overlay,
+             GtkWidget    *widget)
+{
+	GeditOverlayChild *child;
+
+	/* check that the widget is not added yet */
+	child = get_overlay_child (GEDIT_OVERLAY (overlay), widget);
+
+	if (child == NULL)
+	{
+		if (GEDIT_IS_OVERLAY_CHILD (widget))
+		{
+			child = GEDIT_OVERLAY_CHILD (widget);
+		}
+		else
+		{
+			child = gedit_overlay_child_new (widget);
+			gtk_widget_show (GTK_WIDGET (child));
+		}
+
+		add_toplevel_widget (GEDIT_OVERLAY (overlay), GTK_WIDGET (child));
+	}
+}
+
+static void
+gedit_overlay_remove (GtkContainer *overlay,
+                      GtkWidget    *widget)
+{
+	GeditOverlayPrivate *priv = GEDIT_OVERLAY (overlay)->priv;
+	GSList *l;
+
+	for (l = priv->children; l != NULL; l = g_slist_next (l))
+	{
+		GtkWidget *child = l->data;
+
+		if (child == widget)
+		{
+			gtk_widget_unparent (widget);
+                        priv->children = g_slist_remove_link (priv->children,
+                                                              l);
+
+                        g_slist_free (l);
+			break;
+		}
+	}
+}
+
+static void
+gedit_overlay_forall (GtkContainer *overlay,
+                      gboolean      include_internals,
+                      GtkCallback   callback,
+                      gpointer      callback_data)
+{
+	GeditOverlay *goverlay = GEDIT_OVERLAY (overlay);
+	GSList *l, *next;
+
+        l = goverlay->priv->children;
+
+        while (l != NULL)
+	{
+		GtkWidget *child = GTK_WIDGET (l->data);
+
+		next = l->next;
+
+		(* callback) (child, callback_data);
+
+		l = next;
+	}
+}
+
+static GType
+gedit_overlay_child_type (GtkContainer *overlay)
+{
+	return GTK_TYPE_WIDGET;
+}
+
+static void
+adjustment_value_changed (GtkAdjustment *adjustment,
+                          GeditOverlay  *overlay)
+{
+	set_children_positions (overlay);
+}
+
+static void
+gedit_overlay_set_hadjustment (GeditOverlay  *overlay,
+                               GtkAdjustment *adjustment)
+{
+	GeditOverlayPrivate *priv = overlay->priv;
+
+	if (adjustment && priv->vadjustment == adjustment)
+		return;
+
+	if (priv->hadjustment != NULL)
+	{
+		g_signal_handler_disconnect (priv->hadjustment,
+		                             priv->hadjustment_signal_id);
+		g_object_unref (priv->hadjustment);
+	}
+
+	if (adjustment == NULL)
+	{
+		adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
+		                                 0.0, 0.0, 0.0);
+	}
+
+	priv->hadjustment_signal_id =
+		g_signal_connect (adjustment,
+		                  "value-changed",
+		                  G_CALLBACK (adjustment_value_changed),
+		                  overlay);
+
+	priv->hadjustment = g_object_ref_sink (adjustment);
+
+	if (GTK_IS_SCROLLABLE (priv->main_widget))
+	{
+		g_object_set (priv->main_widget,
+		              "hadjustment", adjustment,
+		              NULL);
+
+	}
+
+	g_object_notify (G_OBJECT (overlay), "hadjustment");
+}
+
+static void
+gedit_overlay_set_vadjustment (GeditOverlay  *overlay,
+                               GtkAdjustment *adjustment)
+{
+	GeditOverlayPrivate *priv = overlay->priv;
+
+	if (adjustment && priv->vadjustment == adjustment)
+		return;
+
+	if (priv->vadjustment != NULL)
+	{
+		g_signal_handler_disconnect (priv->vadjustment,
+		                             priv->vadjustment_signal_id);
+		g_object_unref (priv->vadjustment);
+	}
+
+	if (adjustment == NULL)
+	{
+		adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
+		                                 0.0, 0.0, 0.0);
+	}
+
+	overlay->priv->vadjustment_signal_id =
+		g_signal_connect (adjustment,
+		                  "value-changed",
+		                  G_CALLBACK (adjustment_value_changed),
+		                  overlay);
+
+	priv->vadjustment = g_object_ref_sink (adjustment);
+
+	if (GTK_IS_SCROLLABLE (priv->main_widget))
+	{
+		g_object_set (priv->main_widget,
+		              "vadjustment", adjustment,
+		              NULL);
+	}
+
+	g_object_notify (G_OBJECT (overlay), "vadjustment");
+}
+
+static void
+gedit_overlay_class_init (GeditOverlayClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+	GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
+
+	object_class->finalize = gedit_overlay_finalize;
+	object_class->dispose = gedit_overlay_dispose;
+	object_class->get_property = gedit_overlay_get_property;
+	object_class->set_property = gedit_overlay_set_property;
+
+	widget_class->realize = gedit_overlay_realize;
+	widget_class->get_preferred_width = gedit_overlay_get_preferred_width;
+	widget_class->get_preferred_height = gedit_overlay_get_preferred_height;
+	widget_class->size_allocate = gedit_overlay_size_allocate;
+
+	container_class->add = overlay_add;
+	container_class->remove = gedit_overlay_remove;
+	container_class->forall = gedit_overlay_forall;
+	container_class->child_type = gedit_overlay_child_type;
+
+	g_object_class_install_property (object_class, PROP_MAIN_WIDGET,
+	                                 g_param_spec_object ("main-widget",
+	                                                      "Main Widget",
+	                                                      "The Main Widget",
+	                                                      GTK_TYPE_WIDGET,
+	                                                      G_PARAM_READWRITE |
+	                                                      G_PARAM_CONSTRUCT_ONLY |
+	                                                      G_PARAM_STATIC_STRINGS));
+
+	g_object_class_override_property (object_class,
+	                                  PROP_HADJUSTMENT,
+	                                  "hadjustment");
+	g_object_class_override_property (object_class,
+	                                  PROP_VADJUSTMENT,
+	                                  "vadjustment");
+	g_object_class_override_property (object_class,
+	                                  PROP_HSCROLL_POLICY,
+	                                  "hscroll-policy");
+	g_object_class_override_property (object_class,
+	                                  PROP_VSCROLL_POLICY,
+	                                  "vscroll-policy");
+
+	g_type_class_add_private (object_class, sizeof (GeditOverlayPrivate));
+}
+
+static void
+gedit_overlay_init (GeditOverlay *overlay)
+{
+	overlay->priv = GEDIT_OVERLAY_GET_PRIVATE (overlay);
+        overlay->priv->children = NULL;
+}
+
+/**
+ * gedit_overlay_new:
+ * @main_widget: a #GtkWidget
+ *
+ * Creates a new #GeditOverlay
+ *
+ * Returns: a new #GeditOverlay object.
+ */
+GtkWidget *
+gedit_overlay_new (GtkWidget *main_widget)
+{
+	g_return_val_if_fail (GTK_IS_WIDGET (main_widget), NULL);
+
+	return GTK_WIDGET (g_object_new (GEDIT_TYPE_OVERLAY,
+	                                 "main-widget", main_widget,
+	                                 NULL));
+}
+
+/**
+ * gedit_overlay_add:
+ * @overlay: a #GeditOverlay
+ * @widget: a #GtkWidget to be added to the container
+ * @position: a #GeditOverlayChildPosition
+ * @offset: offset for @widget
+ *
+ * Adds @widget to @overlay in a specific position.
+ */
+void
+gedit_overlay_add (GeditOverlay             *overlay,
+                   GtkWidget                *widget,
+                   GeditOverlayChildPosition position,
+                   guint                     offset)
+{
+	GeditOverlayChild *child;
+
+	g_return_if_fail (GEDIT_IS_OVERLAY (overlay));
+	g_return_if_fail (GTK_IS_WIDGET (widget));
+
+	gtk_container_add (GTK_CONTAINER (overlay), widget);
+
+	/* NOTE: can we improve this without exposing overlay child? */
+	child = get_overlay_child (overlay, widget);
+	g_assert (child != NULL);
+
+	gedit_overlay_child_set_position (child, position);
+	gedit_overlay_child_set_offset (child, offset);
+}
diff --git a/src/gedit-overlay.h b/src/gedit-overlay.h
new file mode 100644
index 0000000..8817d33
--- /dev/null
+++ b/src/gedit-overlay.h
@@ -0,0 +1,70 @@
+/*
+ * gedit-overlay.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2011 - Ignacio Casal Quinteiro
+ *
+ * gedit is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * gedit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GEDIT_OVERLAY_H__
+#define __GEDIT_OVERLAY_H__
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+#include "gedit-overlay-child.h"
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_OVERLAY		(gedit_overlay_get_type ())
+#define GEDIT_OVERLAY(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_OVERLAY, GeditOverlay))
+#define GEDIT_OVERLAY_CONST(obj)	(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_OVERLAY, GeditOverlay const))
+#define GEDIT_OVERLAY_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), GEDIT_TYPE_OVERLAY, GeditOverlayClass))
+#define GEDIT_IS_OVERLAY(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_OVERLAY))
+#define GEDIT_IS_OVERLAY_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_OVERLAY))
+#define GEDIT_OVERLAY_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GEDIT_TYPE_OVERLAY, GeditOverlayClass))
+
+typedef struct _GeditOverlay		GeditOverlay;
+typedef struct _GeditOverlayClass	GeditOverlayClass;
+typedef struct _GeditOverlayPrivate	GeditOverlayPrivate;
+
+struct _GeditOverlay
+{
+	GtkContainer parent;
+
+	GeditOverlayPrivate *priv;
+};
+
+struct _GeditOverlayClass
+{
+	GtkContainerClass parent_class;
+
+	void (* set_scroll_adjustments)	  (GeditOverlay	 *overlay,
+					   GtkAdjustment *hadjustment,
+					   GtkAdjustment *vadjustment);
+};
+
+GType		 gedit_overlay_get_type			(void) G_GNUC_CONST;
+
+GtkWidget	*gedit_overlay_new			(GtkWidget *main_widget);
+
+void		 gedit_overlay_add			(GeditOverlay             *overlay,
+							 GtkWidget                *widget,
+							 GeditOverlayChildPosition position,
+							 guint                     offset);
+
+G_END_DECLS
+
+#endif /* __GEDIT_OVERLAY_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]