compilerplugins/clang/reservedid.cxx |    2 
 vcl/Library_vclplug_gtk4.mk          |    1 
 vcl/unx/gtk4/gtkinst.cxx             |    1 
 vcl/unx/gtk4/surfacecellrenderer.cxx |  229 +++++++++++++++++++++++++++++++++++
 vcl/unx/gtk4/surfacecellrenderer.hxx |   42 ++++++
 5 files changed, 275 insertions(+)

New commits:
commit 77bc0bcfb610b77a9191af2343f64eae36073540
Author:     Caolán McNamara <caol...@redhat.com>
AuthorDate: Mon Apr 11 15:26:40 2022 +0100
Commit:     Caolán McNamara <caol...@redhat.com>
CommitDate: Tue Apr 12 20:51:47 2022 +0200

    gtk4: add a surface_cell_renderer
    
    to bridge our continuing usage of cairo and gtk4 GtkComboBox cell renderers
    without dropping any existing hidpi wins
    
    Change-Id: If5244766c13dd5d82445cb626ef4096e3c6ea244
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132926
    Tested-by: Caolán McNamara <caol...@redhat.com>
    Reviewed-by: Caolán McNamara <caol...@redhat.com>

diff --git a/compilerplugins/clang/reservedid.cxx 
b/compilerplugins/clang/reservedid.cxx
index 2ef713916569..be0a2f209f71 100644
--- a/compilerplugins/clang/reservedid.cxx
+++ b/compilerplugins/clang/reservedid.cxx
@@ -215,6 +215,8 @@ bool ReservedId::VisitNamedDecl(NamedDecl const * decl) {
             && s != "_NotifyingLayout" // vcl/unx/gtk4/notifyinglayout.cxx
             && s != "_SurfacePaintable" // vcl/unx/gtk3/gtkinst.cxx
             && s != "_SurfacePaintableClass" // vcl/unx/gtk3/gtkinst.cxx
+            && s != "_SurfaceCellRenderer" // 
vcl/unx/gtk4/surfacecellrenderer.cxx
+            && s != "_SurfaceCellRendererClass" // 
vcl/unx/gtk4/surfacecellrenderer.cxx
             && s != "_TransferableContent" // 
vcl/unx/gtk4/transferableprovider.cxx
             && s != "_TransferableContentClass" // 
vcl/unx/gtk4/transferableprovider.cxx
             && s != "_XRegion" // vcl/unx/generic/gdi/x11cairotextrender.cxx
diff --git a/vcl/Library_vclplug_gtk4.mk b/vcl/Library_vclplug_gtk4.mk
index 83695a8c1c34..d9c19f5521b0 100644
--- a/vcl/Library_vclplug_gtk4.mk
+++ b/vcl/Library_vclplug_gtk4.mk
@@ -99,6 +99,7 @@ $(eval $(call gb_Library_add_exception_objects,vclplug_gtk4,\
     vcl/unx/gtk4/gloactiongroup \
     vcl/unx/gtk4/hudawareness \
     vcl/unx/gtk4/notifyinglayout \
+    vcl/unx/gtk4/surfacecellrenderer \
     vcl/unx/gtk4/surfacepaintable \
     vcl/unx/gtk4/transferableprovider \
 ))
diff --git a/vcl/unx/gtk4/gtkinst.cxx b/vcl/unx/gtk4/gtkinst.cxx
index 502eed2aa246..658c01233c84 100644
--- a/vcl/unx/gtk4/gtkinst.cxx
+++ b/vcl/unx/gtk4/gtkinst.cxx
@@ -12,6 +12,7 @@
 
 #include "convert3to4.hxx"
 #include "notifyinglayout.hxx"
+#include "surfacecellrenderer.hxx"
 #include "surfacepaintable.hxx"
 #include "transferableprovider.hxx"
 
diff --git a/vcl/unx/gtk4/surfacecellrenderer.cxx 
b/vcl/unx/gtk4/surfacecellrenderer.cxx
new file mode 100644
index 000000000000..f8015bb110b2
--- /dev/null
+++ b/vcl/unx/gtk4/surfacecellrenderer.cxx
@@ -0,0 +1,229 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <vcl/svapp.hxx>
+#include "surfacecellrenderer.hxx"
+#include <cairo/cairo-gobject.h>
+
+namespace
+{
+struct _SurfaceCellRendererClass : public GtkCellRendererClass
+{
+};
+
+enum
+{
+    PROP_SURFACE = 10000,
+};
+}
+
+G_DEFINE_TYPE(SurfaceCellRenderer, surface_cell_renderer, 
GTK_TYPE_CELL_RENDERER)
+
+static void surface_cell_renderer_init(SurfaceCellRenderer* self)
+{
+    self->surface = nullptr;
+    // prevent loplugin:unreffun firing on macro generated function
+    (void)surface_cell_renderer_get_instance_private(self);
+}
+
+static void surface_cell_renderer_get_property(GObject* object, guint 
param_id, GValue* value,
+                                               GParamSpec* pspec)
+{
+    SurfaceCellRenderer* cellsurface = SURFACE_CELL_RENDERER(object);
+
+    switch (param_id)
+    {
+        case PROP_SURFACE:
+            g_value_set_boxed(value, cellsurface->surface);
+            break;
+        default:
+            G_OBJECT_CLASS(surface_cell_renderer_parent_class)
+                ->get_property(object, param_id, value, pspec);
+            break;
+    }
+}
+
+static void surface_cell_renderer_set_property(GObject* object, guint 
param_id, const GValue* value,
+                                               GParamSpec* pspec)
+{
+    SurfaceCellRenderer* cellsurface = SURFACE_CELL_RENDERER(object);
+
+    switch (param_id)
+    {
+        case PROP_SURFACE:
+            if (cellsurface->surface)
+                cairo_surface_destroy(cellsurface->surface);
+            cellsurface->surface = 
static_cast<cairo_surface_t*>(g_value_get_boxed(value));
+            if (cellsurface->surface)
+                cairo_surface_reference(cellsurface->surface);
+            break;
+        default:
+            G_OBJECT_CLASS(surface_cell_renderer_parent_class)
+                ->set_property(object, param_id, value, pspec);
+            break;
+    }
+}
+
+static bool surface_cell_renderer_get_preferred_size(GtkCellRenderer* cell,
+                                                     GtkOrientation 
orientation, gint* minimum_size,
+                                                     gint* natural_size);
+
+static void surface_cell_renderer_snapshot(GtkCellRenderer* cell, GtkSnapshot* 
snapshot,
+                                           GtkWidget* widget, const 
GdkRectangle* background_area,
+                                           const GdkRectangle* cell_area,
+                                           GtkCellRendererState flags);
+
+static void surface_cell_renderer_render(GtkCellRenderer* cell, cairo_t* cr, 
GtkWidget* widget,
+                                         const GdkRectangle* background_area,
+                                         const GdkRectangle* cell_area, 
GtkCellRendererState flags);
+
+static void surface_cell_renderer_finalize(GObject* object)
+{
+    SurfaceCellRenderer* cellsurface = SURFACE_CELL_RENDERER(object);
+
+    if (cellsurface->surface)
+        cairo_surface_destroy(cellsurface->surface);
+
+    G_OBJECT_CLASS(surface_cell_renderer_parent_class)->finalize(object);
+}
+
+static void surface_cell_renderer_get_preferred_width(GtkCellRenderer* cell, 
GtkWidget* widget,
+                                                      gint* minimum_size, 
gint* natural_size)
+{
+    if (!surface_cell_renderer_get_preferred_size(cell, 
GTK_ORIENTATION_HORIZONTAL, minimum_size,
+                                                  natural_size))
+    {
+        // fallback to parent if we're empty
+        GTK_CELL_RENDERER_CLASS(surface_cell_renderer_parent_class)
+            ->get_preferred_width(cell, widget, minimum_size, natural_size);
+    }
+}
+
+static void surface_cell_renderer_get_preferred_height(GtkCellRenderer* cell, 
GtkWidget* widget,
+                                                       gint* minimum_size, 
gint* natural_size)
+{
+    if (!surface_cell_renderer_get_preferred_size(cell, 
GTK_ORIENTATION_VERTICAL, minimum_size,
+                                                  natural_size))
+    {
+        // fallback to parent if we're empty
+        GTK_CELL_RENDERER_CLASS(surface_cell_renderer_parent_class)
+            ->get_preferred_height(cell, widget, minimum_size, natural_size);
+    }
+}
+
+static void 
surface_cell_renderer_get_preferred_height_for_width(GtkCellRenderer* cell,
+                                                                 GtkWidget* 
widget, gint /*width*/,
+                                                                 gint* 
minimum_height,
+                                                                 gint* 
natural_height)
+{
+    gtk_cell_renderer_get_preferred_height(cell, widget, minimum_height, 
natural_height);
+}
+
+static void 
surface_cell_renderer_get_preferred_width_for_height(GtkCellRenderer* cell,
+                                                                 GtkWidget* 
widget, gint /*height*/,
+                                                                 gint* 
minimum_width,
+                                                                 gint* 
natural_width)
+{
+    gtk_cell_renderer_get_preferred_width(cell, widget, minimum_width, 
natural_width);
+}
+
+void surface_cell_renderer_class_init(SurfaceCellRendererClass* klass)
+{
+    GtkCellRendererClass* cell_class = GTK_CELL_RENDERER_CLASS(klass);
+    GObjectClass* object_class = G_OBJECT_CLASS(klass);
+
+    /* Hook up functions to set and get our custom cell renderer properties */
+    object_class->get_property = surface_cell_renderer_get_property;
+    object_class->set_property = surface_cell_renderer_set_property;
+
+    surface_cell_renderer_parent_class = g_type_class_peek_parent(klass);
+    object_class->finalize = surface_cell_renderer_finalize;
+
+    cell_class->get_preferred_width = 
surface_cell_renderer_get_preferred_width;
+    cell_class->get_preferred_height = 
surface_cell_renderer_get_preferred_height;
+    cell_class->get_preferred_width_for_height
+        = surface_cell_renderer_get_preferred_width_for_height;
+    cell_class->get_preferred_height_for_width
+        = surface_cell_renderer_get_preferred_height_for_width;
+
+    cell_class->snapshot = surface_cell_renderer_snapshot;
+
+    g_object_class_install_property(
+        object_class, PROP_SURFACE,
+        g_param_spec_boxed("surface", "Surface", "The cairo surface to render",
+                           CAIRO_GOBJECT_TYPE_SURFACE, G_PARAM_READWRITE));
+}
+
+GtkCellRenderer* surface_cell_renderer_new()
+{
+    return GTK_CELL_RENDERER(g_object_new(SURFACE_TYPE_CELL_RENDERER, 
nullptr));
+}
+
+bool surface_cell_renderer_get_preferred_size(GtkCellRenderer* cell, 
GtkOrientation orientation,
+                                              gint* minimum_size, gint* 
natural_size)
+{
+    SurfaceCellRenderer* cellsurface = SURFACE_CELL_RENDERER(cell);
+
+    int nWidth = 0;
+    int nHeight = 0;
+
+    if (cellsurface->surface)
+    {
+        double x1, x2, y1, y2;
+        cairo_t* cr = cairo_create(cellsurface->surface);
+        cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
+        cairo_destroy(cr);
+
+        nWidth = x2 - x1;
+        nHeight = y2 - y1;
+    }
+
+    if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+        if (minimum_size)
+            *minimum_size = nWidth;
+
+        if (natural_size)
+            *natural_size = nWidth;
+    }
+    else
+    {
+        if (minimum_size)
+            *minimum_size = nHeight;
+
+        if (natural_size)
+            *natural_size = nHeight;
+    }
+
+    return true;
+}
+
+void surface_cell_renderer_render(GtkCellRenderer* cell, cairo_t* cr, 
GtkWidget* /*widget*/,
+                                  const GdkRectangle* /*background_area*/,
+                                  const GdkRectangle* cell_area, 
GtkCellRendererState /*flags*/)
+{
+    SurfaceCellRenderer* cellsurface = SURFACE_CELL_RENDERER(cell);
+    cairo_set_source_surface(cr, cellsurface->surface, cell_area->x, 
cell_area->y);
+    cairo_paint(cr);
+}
+
+static void surface_cell_renderer_snapshot(GtkCellRenderer* cell, GtkSnapshot* 
snapshot,
+                                           GtkWidget* widget, const 
GdkRectangle* background_area,
+                                           const GdkRectangle* cell_area,
+                                           GtkCellRendererState flags)
+{
+    graphene_rect_t rect = GRAPHENE_RECT_INIT(
+        static_cast<float>(cell_area->x), static_cast<float>(cell_area->y),
+        static_cast<float>(cell_area->width), 
static_cast<float>(cell_area->height));
+    cairo_t* cr = gtk_snapshot_append_cairo(GTK_SNAPSHOT(snapshot), &rect);
+    surface_cell_renderer_render(cell, cr, widget, background_area, cell_area, 
flags);
+    cairo_destroy(cr);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/unx/gtk4/surfacecellrenderer.hxx 
b/vcl/unx/gtk4/surfacecellrenderer.hxx
new file mode 100644
index 000000000000..d908c04cebf7
--- /dev/null
+++ b/vcl/unx/gtk4/surfacecellrenderer.hxx
@@ -0,0 +1,42 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+#include <cairo.h>
+
+G_BEGIN_DECLS
+
+struct _SurfaceCellRenderer
+{
+    GtkCellRenderer parent;
+    cairo_surface_t* surface;
+};
+
+/*
+   Provide a mechanism to support rendering a cairo surface in a GtkComboBox
+*/
+
+G_DECLARE_FINAL_TYPE(SurfaceCellRenderer, surface_cell_renderer, SURFACE, 
CELL_RENDERER,
+                     GtkCellRenderer)
+
+#define SURFACE_TYPE_CELL_RENDERER (surface_cell_renderer_get_type())
+
+#define SURFACE_CELL_RENDERER(obj)                                             
                    \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), SURFACE_TYPE_CELL_RENDERER, 
SurfaceCellRenderer))
+
+#define SURFACE_IS_CELL_RENDERER(obj)                                          
                    \
+    (G_TYPE_CHECK_INSTANCE_TYPE((obj), SURFACE_TYPE_CELL_RENDERER))
+
+GtkCellRenderer* surface_cell_renderer_new();
+
+G_END_DECLS
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to