[gtk/wip/layout-manager: 70/75] Add GtkLayoutManager



commit f716ca3c254ad505c9638711dbe622a72ded3ab3
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Dec 12 15:24:41 2018 +0000

    Add GtkLayoutManager
    
    A base abstract class for layout manager delegate objects.
    
    Layout managers are associated to a single widget, like event
    controllers, and are responsible for measuring and allocating the
    children of the widget they are bound to.

 gtk/gtk.h                     |   1 +
 gtk/gtklayoutmanager.c        | 222 ++++++++++++++++++++++++++++++++++++++++++
 gtk/gtklayoutmanager.h        |  94 ++++++++++++++++++
 gtk/gtklayoutmanagerprivate.h |  10 ++
 gtk/meson.build               |   2 +
 5 files changed, 329 insertions(+)
---
diff --git a/gtk/gtk.h b/gtk/gtk.h
index cf89eca9ab..67b101f34d 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -136,6 +136,7 @@
 #include <gtk/gtkinvisible.h>
 #include <gtk/gtklabel.h>
 #include <gtk/gtklayout.h>
+#include <gtk/gtklayoutmanager.h>
 #include <gtk/gtklevelbar.h>
 #include <gtk/gtklinkbutton.h>
 #include <gtk/gtklistbox.h>
diff --git a/gtk/gtklayoutmanager.c b/gtk/gtklayoutmanager.c
new file mode 100644
index 0000000000..bb23c5a598
--- /dev/null
+++ b/gtk/gtklayoutmanager.c
@@ -0,0 +1,222 @@
+/* gtklayoutmanager.c: Layout manager base class
+ * Copyright 2018  The GNOME Foundation
+ *
+ * This library 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.
+ *
+ * This library 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Emmanuele Bassi
+ */
+
+/**
+ * SECTION:gtklayoutmanager
+ * @Title: GtkLayoutManager
+ * @Short_description: Base class for layout manager
+ *
+ * ...
+ */
+
+#include "config.h"
+
+#include "gtklayoutmanager.h"
+
+#ifdef G_ENABLE_DEBUG
+#define LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED(m,method)   G_STMT_START {  \
+        GObject *_obj = G_OBJECT (m);                                   \
+        g_warning ("Layout managers of type %s do not implement "       \
+                   "the GtkLayoutManager::%s method",                   \
+                   G_OBJECT_TYPE_NAME (_obj),                           \
+                   #method);                           } G_STMT_END
+#else
+#define LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED(m,method)
+#endif
+
+typedef struct {
+  GtkWidget *widget;
+} GtkLayoutManagerPrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkLayoutManager, gtk_layout_manager, G_TYPE_OBJECT)
+
+static GtkSizeRequestMode
+gtk_layout_manager_real_get_request_mode (GtkLayoutManager *manager,
+                                          GtkWidget        *widget)
+{
+  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, get_request_mode);
+
+  return GTK_SIZE_REQUEST_CONSTANT_SIZE;
+}
+
+static void
+gtk_layout_manager_real_measure (GtkLayoutManager *manager,
+                                 GtkWidget        *widget,
+                                 GtkOrientation    orientation,
+                                 int               for_size,
+                                 int              *minimum,
+                                 int              *natural,
+                                 int              *baseline_minimum,
+                                 int              *baseline_natural)
+{
+  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, measure);
+
+  if (minimum != NULL)
+    *minimum = 0;
+
+  if (natural != NULL)
+    *natural = 0;
+
+  if (baseline_minimum != NULL)
+    *baseline_minimum = 0;
+
+  if (baseline_natural != NULL)
+    *baseline_natural = 0;
+}
+
+static void
+gtk_layout_manager_real_allocate (GtkLayoutManager *manager,
+                                  GtkWidget        *widget,
+                                  int               width,
+                                  int               height,
+                                  int               baseline)
+{
+  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, allocate);
+}
+
+static void
+gtk_layout_manager_class_init (GtkLayoutManagerClass *klass)
+{
+  klass->get_request_mode = gtk_layout_manager_real_get_request_mode;
+  klass->measure = gtk_layout_manager_real_measure;
+  klass->allocate = gtk_layout_manager_real_allocate;
+}
+
+static void
+gtk_layout_manager_init (GtkLayoutManager *self)
+{
+}
+
+/*< private >
+ * gtk_layout_manager_set_widget:
+ * @layout_manager: a #GtkLayoutManager
+ * @widget: (nullable): a #GtkWidget
+ *
+ * ...
+ */
+void
+gtk_layout_manager_set_widget (GtkLayoutManager *layout_manager,
+                               GtkWidget        *widget)
+{
+  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (layout_manager);
+
+  priv->widget = widget;
+}
+
+/**
+ * gtk_layout_manager_measure:
+ * @manager:
+ * @widget:
+ * @orientation:
+ * @for_size:
+ * @minimum: (out):
+ * @natural: (out):
+ * @minimum_baseline: (out):
+ * @natural_baseline: (out):
+ *
+ * ...
+ *
+ */
+void
+gtk_layout_manager_measure (GtkLayoutManager *manager,
+                            GtkWidget        *widget,
+                            GtkOrientation    orientation,
+                            int               for_size,
+                            int              *minimum,
+                            int              *natural,
+                            int              *minimum_baseline,
+                            int              *natural_baseline)
+{
+  GtkLayoutManagerClass *klass;
+
+  g_return_if_fail (GTK_IS_LAYOUT_MANAGER (manager));
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);
+
+  klass->measure (manager, widget, orientation,
+                  for_size,
+                  minimum, natural,
+                  minimum_baseline, natural_baseline);
+}
+
+/**
+ * gtk_layout_manager_allocate:
+ * @manager:
+ * @widget:
+ * @width:
+ * @height:
+ * @baseline:
+ *
+ * ...
+ */
+void
+gtk_layout_manager_allocate (GtkLayoutManager *manager,
+                             GtkWidget        *widget,
+                             int               width,
+                             int               height,
+                             int               baseline)
+{
+  GtkLayoutManagerClass *klass;
+
+  g_return_if_fail (GTK_IS_LAYOUT_MANAGER (manager));
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);
+
+  klass->allocate (manager, widget, width, height, baseline);
+}
+
+/**
+ * gtk_layout_manager_get_request_mode:
+ * @manager:
+ * @widget:
+ *
+ * ...
+ *
+ * Returns: ...
+ */
+GtkSizeRequestMode
+gtk_layout_manager_get_request_mode (GtkLayoutManager *manager,
+                                     GtkWidget        *widget)
+{
+  GtkLayoutManagerClass *klass;
+
+  g_return_val_if_fail (GTK_IS_LAYOUT_MANAGER (manager), GTK_SIZE_REQUEST_CONSTANT_SIZE);
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
+
+  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);
+
+  return klass->get_request_mode (manager, widget);
+}
+
+/**
+ * gtk_layout_manager_layout_changed:
+ * @manager: a #GtkLayoutManager
+ *
+ * ...
+ */
+void
+gtk_layout_manager_layout_changed (GtkLayoutManager *manager)
+{
+  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (manager);
+
+  if (priv->widget != NULL)
+    gtk_widget_queue_resize (priv->widget);
+}
diff --git a/gtk/gtklayoutmanager.h b/gtk/gtklayoutmanager.h
new file mode 100644
index 0000000000..e849e300a2
--- /dev/null
+++ b/gtk/gtklayoutmanager.h
@@ -0,0 +1,94 @@
+/* gtklayoutmanager.h: Layout manager base class
+ * Copyright 2018  The GNOME Foundation
+ *
+ * This library 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.
+ *
+ * This library 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Emmanuele Bassi
+ */
+#pragma once
+
+#include <gtk/gtkcontainer.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_LAYOUT_MANAGER (gtk_layout_manager_get_type ())
+
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_DERIVABLE_TYPE (GtkLayoutManager, gtk_layout_manager, GTK, LAYOUT_MANAGER, GObject)
+
+/**
+ * GtkLayoutManagerClass:
+ * @get_request_mode: a virtual function, used to return the preferred
+ *   request mode for the layout manager; for instance, "width for height"
+ *   or "height for width"; see #GtkSizeRequestMode
+ * @measure: a virtual function, used to measure the minimum and preferred
+ *   sizes of the widget using the layout manager for a given orientation
+ * @allocate: a virtual function, used to allocate the size of the widget
+ *   using the layout manager
+ *
+ * The `GtkLayoutManagerClass` structure contains only private data, and
+ * should only be accessed through the provided API, or when subclassing
+ * #GtkLayoutManager.
+ */
+struct _GtkLayoutManagerClass
+{
+  /*< private >*/
+  GObjectClass parent_class;
+
+  /*< public >*/
+  GtkSizeRequestMode (* get_request_mode) (GtkLayoutManager *manager,
+                                           GtkWidget        *widget);
+
+  void               (* measure)          (GtkLayoutManager *manager,
+                                           GtkWidget        *widget,
+                                           GtkOrientation    orientation,
+                                           int               for_size,
+                                           int              *minimum,
+                                           int              *natural,
+                                           int              *minimum_baseline,
+                                           int              *natural_baseline);
+
+  void               (* allocate)         (GtkLayoutManager *manager,
+                                           GtkWidget        *widget,
+                                           int               width,
+                                           int               height,
+                                           int               baseline);
+
+  /*< private >*/
+  gpointer _padding[16];
+};
+
+GDK_AVAILABLE_IN_ALL
+void                    gtk_layout_manager_measure              (GtkLayoutManager *manager,
+                                                                 GtkWidget        *widget,
+                                                                 GtkOrientation    orientation,
+                                                                 int               for_size,
+                                                                 int              *minimum,
+                                                                 int              *natural,
+                                                                 int              *minimum_baseline,
+                                                                 int              *natural_baseline);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_layout_manager_allocate             (GtkLayoutManager *manager,
+                                                                 GtkWidget        *widget,
+                                                                 int               width,
+                                                                 int               height,
+                                                                 int               baseline);
+GDK_AVAILABLE_IN_ALL
+GtkSizeRequestMode      gtk_layout_manager_get_request_mode     (GtkLayoutManager *manager,
+                                                                 GtkWidget        *widget);
+
+GDK_AVAILABLE_IN_ALL
+void                    gtk_layout_manager_layout_changed       (GtkLayoutManager *manager);
+
+G_END_DECLS
diff --git a/gtk/gtklayoutmanagerprivate.h b/gtk/gtklayoutmanagerprivate.h
new file mode 100644
index 0000000000..d02ed56a6f
--- /dev/null
+++ b/gtk/gtklayoutmanagerprivate.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include "gtklayoutmanager.h"
+
+G_BEGIN_DECLS
+
+void gtk_layout_manager_set_widget (GtkLayoutManager *manager,
+                                    GtkWidget        *widget);
+
+G_END_DECLS
diff --git a/gtk/meson.build b/gtk/meson.build
index cc021d7fea..b49af414be 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -263,6 +263,7 @@ gtk_public_sources = files([
   'gtkinvisible.c',
   'gtklabel.c',
   'gtklayout.c',
+  'gtklayoutmanager.c',
   'gtklevelbar.c',
   'gtklinkbutton.c',
   'gtklistbox.c',
@@ -510,6 +511,7 @@ gtk_public_headers = files([
   'gtkinvisible.h',
   'gtklabel.h',
   'gtklayout.h',
+  'gtklayoutmanager.h',
   'gtklevelbar.h',
   'gtklinkbutton.h',
   'gtklistbox.h',


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