summaryrefslogtreecommitdiff
path: root/vcl/unx/gtk3/customcellrenderer.cxx
blob: c7c6661ed9123797c817d4eee12afda62ca90291 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* -*- 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 "customcellrenderer.hxx"
#if !GTK_CHECK_VERSION(4, 0, 0)
#include <gtk/gtk-a11y.h>
#endif

namespace
{
struct _CustomCellRendererClass : public GtkCellRendererTextClass
{
};

enum
{
    PROP_ID = 10000,
    PROP_INSTANCE_TREE_VIEW = 10001
};
}

G_DEFINE_TYPE(CustomCellRenderer, custom_cell_renderer, GTK_TYPE_CELL_RENDERER_TEXT)

static void custom_cell_renderer_init(CustomCellRenderer* self)
{
    {
        SolarMutexGuard aGuard;
        new (&self->device) VclPtr<VirtualDevice>;
    }

    // prevent loplugin:unreffun firing on macro generated function
    (void)custom_cell_renderer_get_instance_private(self);
}

static void custom_cell_renderer_get_property(GObject* object, guint param_id, GValue* value,
                                              GParamSpec* pspec)
{
    CustomCellRenderer* cellsurface = CUSTOM_CELL_RENDERER(object);

    switch (param_id)
    {
        case PROP_ID:
            g_value_set_string(value, cellsurface->id);
            break;
        case PROP_INSTANCE_TREE_VIEW:
            g_value_set_pointer(value, cellsurface->instance);
            break;
        default:
            G_OBJECT_CLASS(custom_cell_renderer_parent_class)
                ->get_property(object, param_id, value, pspec);
            break;
    }
}

static void custom_cell_renderer_set_property(GObject* object, guint param_id, const GValue* value,
                                              GParamSpec* pspec)
{
    CustomCellRenderer* cellsurface = CUSTOM_CELL_RENDERER(object);

    switch (param_id)
    {
        case PROP_ID:
            g_free(cellsurface->id);
            cellsurface->id = g_value_dup_string(value);
            break;
        case PROP_INSTANCE_TREE_VIEW:
            cellsurface->instance = g_value_get_pointer(value);
            break;
        default:
            G_OBJECT_CLASS(custom_cell_renderer_parent_class)
                ->set_property(object, param_id, value, pspec);
            break;
    }
}

static bool custom_cell_renderer_get_preferred_size(GtkCellRenderer* cell,
                                                    GtkOrientation orientation, gint* minimum_size,
                                                    gint* natural_size);

#if GTK_CHECK_VERSION(4, 0, 0)
static void custom_cell_renderer_snapshot(GtkCellRenderer* cell, GtkSnapshot* snapshot,
                                          GtkWidget* widget, const GdkRectangle* background_area,
                                          const GdkRectangle* cell_area,
                                          GtkCellRendererState flags);
#endif

static void custom_cell_renderer_render(GtkCellRenderer* cell, cairo_t* cr, GtkWidget* widget,
                                        const GdkRectangle* background_area,
                                        const GdkRectangle* cell_area, GtkCellRendererState flags);

static void custom_cell_renderer_finalize(GObject* object)
{
    CustomCellRenderer* cellsurface = CUSTOM_CELL_RENDERER(object);

    g_free(cellsurface->id);

    {
        SolarMutexGuard aGuard;
        cellsurface->device.disposeAndClear();
        cellsurface->device.~VclPtr<VirtualDevice>();
    }

    G_OBJECT_CLASS(custom_cell_renderer_parent_class)->finalize(object);
}

static void custom_cell_renderer_get_preferred_width(GtkCellRenderer* cell, GtkWidget* widget,
                                                     gint* minimum_size, gint* natural_size)
{
    if (!custom_cell_renderer_get_preferred_size(cell, GTK_ORIENTATION_HORIZONTAL, minimum_size,
                                                 natural_size))
    {
        // fallback to parent if we're empty
        GTK_CELL_RENDERER_CLASS(custom_cell_renderer_parent_class)
            ->get_preferred_width(cell, widget, minimum_size, natural_size);
    }
}

static void custom_cell_renderer_get_preferred_height(GtkCellRenderer* cell, GtkWidget* widget,
                                                      gint* minimum_size, gint* natural_size)
{
    if (!custom_cell_renderer_get_preferred_size(cell, GTK_ORIENTATION_VERTICAL, minimum_size,
                                                 natural_size))
    {
        // fallback to parent if we're empty
        GTK_CELL_RENDERER_CLASS(custom_cell_renderer_parent_class)
            ->get_preferred_height(cell, widget, minimum_size, natural_size);
    }
}

static void custom_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 custom_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 custom_cell_renderer_class_init(CustomCellRendererClass* 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 = custom_cell_renderer_get_property;
    object_class->set_property = custom_cell_renderer_set_property;

    custom_cell_renderer_parent_class = g_type_class_peek_parent(klass);
    object_class->finalize = custom_cell_renderer_finalize;

    cell_class->get_preferred_width = custom_cell_renderer_get_preferred_width;
    cell_class->get_preferred_height = custom_cell_renderer_get_preferred_height;
    cell_class->get_preferred_width_for_height
        = custom_cell_renderer_get_preferred_width_for_height;
    cell_class->get_preferred_height_for_width
        = custom_cell_renderer_get_preferred_height_for_width;

#if GTK_CHECK_VERSION(4, 0, 0)
    cell_class->snapshot = custom_cell_renderer_snapshot;
#else
    cell_class->render = custom_cell_renderer_render;
#endif

    g_object_class_install_property(
        object_class, PROP_ID,
        g_param_spec_string("id", "ID", "The ID of the custom data", nullptr, G_PARAM_READWRITE));

    g_object_class_install_property(
        object_class, PROP_INSTANCE_TREE_VIEW,
        g_param_spec_pointer("instance", "Instance", "The GtkInstanceTreeView", G_PARAM_READWRITE));

#if !GTK_CHECK_VERSION(4, 0, 0)
    gtk_cell_renderer_class_set_accessible_type(cell_class, GTK_TYPE_TEXT_CELL_ACCESSIBLE);
#endif
}

GtkCellRenderer* custom_cell_renderer_new()
{
    return GTK_CELL_RENDERER(g_object_new(CUSTOM_TYPE_CELL_RENDERER, nullptr));
}

bool custom_cell_renderer_get_preferred_size(GtkCellRenderer* cell, GtkOrientation orientation,
                                             gint* minimum_size, gint* natural_size)
{
    GValue value = G_VALUE_INIT;
    g_value_init(&value, G_TYPE_STRING);
    g_object_get_property(G_OBJECT(cell), "id", &value);

    const char* pStr = g_value_get_string(&value);

    OUString sId(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);

    value = G_VALUE_INIT;
    g_value_init(&value, G_TYPE_POINTER);
    g_object_get_property(G_OBJECT(cell), "instance", &value);

    CustomCellRenderer* cellsurface = CUSTOM_CELL_RENDERER(cell);

    Size aSize;

    gpointer pWidget = g_value_get_pointer(&value);
    if (pWidget)
    {
        SolarMutexGuard aGuard;
        custom_cell_renderer_ensure_device(cellsurface, pWidget);
        aSize = custom_cell_renderer_get_size(*cellsurface->device, sId, pWidget);
    }

    if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
        if (minimum_size)
            *minimum_size = aSize.Width();

        if (natural_size)
            *natural_size = aSize.Width();
    }
    else
    {
        if (minimum_size)
            *minimum_size = aSize.Height();

        if (natural_size)
            *natural_size = aSize.Height();
    }

    return true;
}

void custom_cell_renderer_render(GtkCellRenderer* cell, cairo_t* cr, GtkWidget* /*widget*/,
                                 const GdkRectangle* /*background_area*/,
                                 const GdkRectangle* cell_area, GtkCellRendererState flags)
{
    GValue value = G_VALUE_INIT;
    g_value_init(&value, G_TYPE_STRING);
    g_object_get_property(G_OBJECT(cell), "id", &value);

    const char* pStr = g_value_get_string(&value);
    OUString sId(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);

    value = G_VALUE_INIT;
    g_value_init(&value, G_TYPE_POINTER);
    g_object_get_property(G_OBJECT(cell), "instance", &value);

    CustomCellRenderer* cellsurface = CUSTOM_CELL_RENDERER(cell);

    gpointer pWidget = g_value_get_pointer(&value);
    if (!pWidget)
        return;

    SolarMutexGuard aGuard;

    custom_cell_renderer_ensure_device(cellsurface, pWidget);

    Size aSize(cell_area->width, cell_area->height);
    // false to not bother setting the bg on resize as we'll do that
    // ourself via cairo
    cellsurface->device->SetOutputSizePixel(aSize, false);

    cairo_surface_t* pSurface = get_underlying_cairo_surface(*cellsurface->device);

    // fill surface as transparent so it can be blended with the potentially
    // selected background
    cairo_t* tempcr = cairo_create(pSurface);
    cairo_set_source_rgba(tempcr, 0, 0, 0, 0);
    cairo_set_operator(tempcr, CAIRO_OPERATOR_SOURCE);
    cairo_paint(tempcr);
    cairo_destroy(tempcr);
    cairo_surface_flush(pSurface);

    custom_cell_renderer_render(*cellsurface->device, tools::Rectangle(Point(0, 0), aSize),
                                static_cast<bool>(flags & GTK_CELL_RENDERER_SELECTED), sId,
                                pWidget);

    cairo_surface_mark_dirty(pSurface);

    cairo_set_source_surface(cr, pSurface, cell_area->x, cell_area->y);
    cairo_paint(cr);
}

#if GTK_CHECK_VERSION(4, 0, 0)
static void custom_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);
    custom_cell_renderer_render(cell, cr, widget, background_area, cell_area, flags);
    cairo_destroy(cr);
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */