summaryrefslogtreecommitdiff
path: root/gtk/gtkwidgetpaintable.c
blob: 81512cb437820b12d2c58b1e87fca3841a0cd9b8 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright © 2018 Benjamin Otte
 *
 * 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/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gtkwidgetpaintableprivate.h"

#include "gtkintl.h"
#include "gtksnapshot.h"
#include "gtkwidgetprivate.h"

/**
 * SECTION:gtkwidgetpaintable
 * @Short_description: Drawing a widget elsewhere
 * @Title: GtkWidgetPaintable
 * @see_also: #GtkWidget, #GdkPaintable
 *
 * #GtkWidgetPaintable is an implementation of the #GdkPaintable interface 
 * that allows displaying the contents of a #GtkWidget.
 *
 * #GtkWidgetPaintable will also take care of the widget not being in a
 * state where it can be drawn (like when it isn't shown) and just draw
 * nothing or where it does not have a size (like when it is hidden) and
 * report no size in that case.
 *
 * Of course, #GtkWidgetPaintable allows you to monitor widgets for size
 * changes by emitting the GdkPaintable::invalidate-size signal whenever
 * the size of the widget changes as well as for visual changes by
 * emitting the GdkPaintable::invalidate-contents signal whenever the
 * widget changes.
 *
 * You can of course use a #GtkWidgetPaintable everywhere a
 * #GdkPaintable is allowed, including using it on a #GtkImage (or one
 * of its parents) that it was set on itself via
 * gtk_image_set_from_paintable(). The paintable will take care of recursion
 * when this happens.  
 * If you do this however, make sure to set the GtkImage:can-shrink property
 * to %TRUE or you might end up with an infinitely growing image.
 */
struct _GtkWidgetPaintable
{
  GObject parent_instance;

  GtkWidget *widget;
  guint loop_tracker;

  guint size_invalid : 1;
  guint contents_invalid : 1;
};

struct _GtkWidgetPaintableClass
{
  GObjectClass parent_class;
};

enum {
  PROP_0,
  PROP_WIDGET,

  N_PROPS,
};

static GParamSpec *properties[N_PROPS] = { NULL, };

static void
gtk_widget_paintable_paintable_snapshot (GdkPaintable *paintable,
                                         GdkSnapshot  *snapshot,
                                         double        width,
                                         double        height)
{
  GtkWidgetPaintable *self = GTK_WIDGET_PAINTABLE (paintable);
  graphene_matrix_t transform;

  self->contents_invalid = FALSE;

  if (self->widget == NULL ||
      !_gtk_widget_is_drawable (self->widget) ||
      _gtk_widget_get_alloc_needed (self->widget))
    return;

  if (self->loop_tracker >= 5)
    return;
  self->loop_tracker++;

  /* need to clip because widgets may draw out of bounds */
  gtk_snapshot_push_clip (snapshot,
                          &GRAPHENE_RECT_INIT(0, 0, width, height),
                          "WidgetPaintableClip<%g,%g>",
                          width, height);
  graphene_matrix_init_scale (&transform,
                              width / gtk_widget_get_allocated_width (self->widget),
                              height / gtk_widget_get_allocated_height (self->widget),
                              1.0);
  gtk_snapshot_push_transform (snapshot,
                               &transform,
                               "WidgetPaintableScale<%g,%g>",
                               width / gtk_widget_get_allocated_width (self->widget),
                               height / gtk_widget_get_allocated_height (self->widget));

  gtk_widget_snapshot (self->widget, snapshot);

  gtk_snapshot_pop (snapshot);
  gtk_snapshot_pop (snapshot);

  self->loop_tracker--;
}

static GdkPaintable *
gtk_widget_paintable_paintable_get_current_image (GdkPaintable *paintable)
{
  GtkWidgetPaintable *self = GTK_WIDGET_PAINTABLE (paintable);
  GtkSnapshot *snapshot;
  int width, height;

  self->contents_invalid = FALSE;
  self->size_invalid = FALSE;

  width = gdk_paintable_get_intrinsic_width (paintable);
  height = gdk_paintable_get_intrinsic_width (paintable);
  if (width == 0 || height == 0)
    return gdk_paintable_new_empty (width, height);

  snapshot = gtk_snapshot_new (FALSE, "WidgetPaintableCurrentImage");
  gdk_paintable_snapshot (GDK_PAINTABLE (self), 
                          snapshot,
                          width, height);
  return gtk_snapshot_free_to_paintable (snapshot, &(graphene_size_t) { width, height });
}

static int
gtk_widget_paintable_paintable_get_intrinsic_width (GdkPaintable *paintable)
{
  GtkWidgetPaintable *self = GTK_WIDGET_PAINTABLE (paintable);

  self->size_invalid = FALSE;

  if (self->widget == NULL)
    return 0;

  return gtk_widget_get_allocated_width (self->widget);
}

static int
gtk_widget_paintable_paintable_get_intrinsic_height (GdkPaintable *paintable)
{
  GtkWidgetPaintable *self = GTK_WIDGET_PAINTABLE (paintable);

  self->size_invalid = FALSE;

  if (self->widget == NULL)
    return 0;

  return gtk_widget_get_allocated_height (self->widget);
}

static void
gtk_widget_paintable_paintable_init (GdkPaintableInterface *iface)
{
  iface->snapshot = gtk_widget_paintable_paintable_snapshot;
  iface->get_current_image = gtk_widget_paintable_paintable_get_current_image;
  iface->get_intrinsic_width = gtk_widget_paintable_paintable_get_intrinsic_width;
  iface->get_intrinsic_height = gtk_widget_paintable_paintable_get_intrinsic_height;
}

G_DEFINE_TYPE_EXTENDED (GtkWidgetPaintable, gtk_widget_paintable, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
                                               gtk_widget_paintable_paintable_init))

static void
gtk_widget_paintable_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)

{
  GtkWidgetPaintable *self = GTK_WIDGET_PAINTABLE (object);

  switch (prop_id)
    {
    case PROP_WIDGET:
      gtk_widget_paintable_set_widget (self, g_value_get_object (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_widget_paintable_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  GtkWidgetPaintable *self = GTK_WIDGET_PAINTABLE (object);

  switch (prop_id)
    {
    case PROP_WIDGET:
      g_value_set_object (value, self->widget);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_widget_paintable_dispose (GObject *object)
{
  GtkWidgetPaintable *self = GTK_WIDGET_PAINTABLE (object);

  gtk_widget_paintable_set_widget (self, NULL);

  G_OBJECT_CLASS (gtk_widget_paintable_parent_class)->dispose (object);
}

static void
gtk_widget_paintable_class_init (GtkWidgetPaintableClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->get_property = gtk_widget_paintable_get_property;
  gobject_class->set_property = gtk_widget_paintable_set_property;
  gobject_class->dispose = gtk_widget_paintable_dispose;

  /**
   * GtkWidgetPaintable:widget
   *
   * The observed widget or %NULL if none.
   */
  properties[PROP_WIDGET] =
    g_param_spec_object ("widget",
                         P_("Widget"),
                         P_("Observed widget"),
                         GTK_TYPE_WIDGET,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (gobject_class, N_PROPS, properties);
}

static void
gtk_widget_paintable_init (GtkWidgetPaintable *self)
{
}

/**
 * gtk_widget_paintable_new:
 * @widget: (allow-none) (transfer none): a #GtkWidget or %NULL
 *
 * Creates a new widget paintable observing the given widget.
 *
 * Returns: (transfer full) (type GtkWidgetPaintable): a new #GtkWidgetPaintable
 **/
GdkPaintable *
gtk_widget_paintable_new (GtkWidget *widget)
{
  g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);

  return g_object_new (GTK_TYPE_WIDGET_PAINTABLE,
                       "widget", widget,
                       NULL);
}

/**
 * gtk_widget_paintable_get_widget:
 * @self: a #GtkWidgetPaintable
 *
 * Returns the widget that is observed or %NULL
 * if none.
 *
 * Returns: (transfer none) (nullable): the observed widget.
 **/
GtkWidget *
gtk_widget_paintable_get_widget (GtkWidgetPaintable *self)
{
  g_return_val_if_fail (GTK_IS_WIDGET_PAINTABLE (self), NULL);

  return self->widget;
}

/**
 * gtk_widget_paintable_set_widget:
 * @self: a #GtkWidgetPaintable
 * @widget: (allow-none): the widget to observe or %NULL
 *
 * Sets the widget that should be observed.
 **/
void
gtk_widget_paintable_set_widget (GtkWidgetPaintable *self,
                                 GtkWidget          *widget)
{

  g_return_if_fail (GTK_IS_WIDGET_PAINTABLE (self));
  g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));

  if (self->widget == widget)
    return;

  if (self->widget)
    {
      self->widget->priv->paintables = g_slist_remove (self->widget->priv->paintables,
                                                       self);
    }

  /* We do not ref the widget to not cause ref cycles when a widget
   * is told to observe itself or one of its parent.
   */
  self->widget = widget;

  if (widget)
    {
      widget->priv->paintables = g_slist_prepend (widget->priv->paintables,
                                                  self);
    }

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_WIDGET]);
  gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
  gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
}

void
gtk_widget_paintable_invalidate_size (GtkWidgetPaintable *self)
{
  if (self->size_invalid)
    return;

  self->size_invalid = TRUE;
  gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
}

void
gtk_widget_paintable_invalidate_contents (GtkWidgetPaintable *self)
{
  if (self->contents_invalid)
    return;

  self->contents_invalid = TRUE;
  gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
}