[gnome-builder/wip/chergert/perspective] egg: add EggCenteringBin



commit 53579ed6c18a0f5680181b87c389d2d2ec2bc53d
Author: Christian Hergert <chergert redhat com>
Date:   Fri Nov 6 14:39:14 2015 -0800

    egg: add EggCenteringBin
    
    This is a simple widget that will try to center it's child with respect
    to the toplevel window. Contrast with GtkBox's center widget which only
    centers with respects to itself.

 contrib/egg/Makefile.am         |    2 +
 contrib/egg/egg-centering-bin.c |   97 +++++++++++++++++++++++++++++++++++++++
 contrib/egg/egg-centering-bin.h |   39 ++++++++++++++++
 3 files changed, 138 insertions(+), 0 deletions(-)
---
diff --git a/contrib/egg/Makefile.am b/contrib/egg/Makefile.am
index 7ca0a1f..12e8eae 100644
--- a/contrib/egg/Makefile.am
+++ b/contrib/egg/Makefile.am
@@ -11,6 +11,8 @@ libegg_private_la_SOURCES = \
        egg-animation.h \
        egg-binding-group.c \
        egg-binding-group.h \
+       egg-centering-bin.c \
+       egg-centering-bin.h \
        egg-counter.c \
        egg-counter.h \
        egg-date-time.c \
diff --git a/contrib/egg/egg-centering-bin.c b/contrib/egg/egg-centering-bin.c
new file mode 100644
index 0000000..7e56bd8
--- /dev/null
+++ b/contrib/egg/egg-centering-bin.c
@@ -0,0 +1,97 @@
+/* egg-centering-bin.c
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "egg-centering-bin.h"
+
+/**
+ * SECTION:egg-centering-bin:
+ * @title: EggCenteringBin
+ * @short_description: center a widget with respect to the toplevel
+ *
+ * First off, you probably want to use GtkBox with a center widget instead
+ * of this widget. However, the case where this widget is useful is when
+ * you cannot control your layout within the width of the toplevel, but
+ * still want your child centered within the toplevel.
+ *
+ * This is done by translating coordinates of the widget with respect to
+ * the toplevel and anchoring the child at TRUE_CENTER-(alloc.width/2).
+ */
+
+G_DEFINE_TYPE (EggCenteringBin, egg_centering_bin, GTK_TYPE_BIN)
+
+GtkWidget *
+egg_centering_bin_new (void)
+{
+  return g_object_new (EGG_TYPE_CENTERING_BIN, NULL);
+}
+
+static void
+egg_centering_bin_size_allocate (GtkWidget     *widget,
+                                 GtkAllocation *allocation)
+{
+  GtkWidget *child;
+
+  g_assert (GTK_IS_WIDGET (widget));
+  g_assert (allocation != NULL);
+
+  gtk_widget_set_allocation (widget, allocation);
+
+  child = gtk_bin_get_child (GTK_BIN (widget));
+
+  if (child != NULL)
+    {
+      GtkWidget *toplevel = gtk_widget_get_toplevel (child);
+      GtkAllocation top_allocation;
+      GtkAllocation child_allocation;
+      GtkRequisition nat_child_req;
+      gint translated_x;
+      gint translated_y;
+
+      gtk_widget_get_allocation (toplevel, &top_allocation);
+      gtk_widget_translate_coordinates (toplevel, widget,
+                                        top_allocation.x + (top_allocation.width / 2),
+                                        0,
+                                        &translated_x,
+                                        &translated_y);
+
+      gtk_widget_get_preferred_size (child, NULL, &nat_child_req);
+
+      child_allocation.x = allocation->x;
+      child_allocation.y = allocation->y;
+      child_allocation.height = allocation->height;
+      child_allocation.width = translated_x * 2;
+
+      if (nat_child_req.width > child_allocation.width)
+        child_allocation.width = MIN (nat_child_req.width, allocation->width);
+
+      gtk_widget_size_allocate (child, &child_allocation);
+    }
+}
+
+static void
+egg_centering_bin_class_init (EggCenteringBinClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  widget_class->size_allocate = egg_centering_bin_size_allocate;
+}
+
+static void
+egg_centering_bin_init (EggCenteringBin *self)
+{
+}
diff --git a/contrib/egg/egg-centering-bin.h b/contrib/egg/egg-centering-bin.h
new file mode 100644
index 0000000..94d4b50
--- /dev/null
+++ b/contrib/egg/egg-centering-bin.h
@@ -0,0 +1,39 @@
+/* egg-centering-bin.h
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EGG_CENTERING_BIN_H
+#define EGG_CENTERING_BIN_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define EGG_TYPE_CENTERING_BIN (egg_centering_bin_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (EggCenteringBin, egg_centering_bin, EGG, CENTERING_BIN, GtkBin)
+
+struct _EggCenteringBinClass
+{
+  GtkBinClass parent;
+};
+
+GtkWidget *egg_centering_bin_new (void);
+
+G_END_DECLS
+
+#endif /* EGG_CENTERING_BIN_H */


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