summaryrefslogtreecommitdiff
path: root/gtk/gtkscaler.c
blob: c07a083f36d9adf2a9a31bf9817eaa8420d34b38 (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
/*
 * 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 "gtkscalerprivate.h"

#include "gtksnapshot.h"

struct _GtkScaler
{
  GObject parent_instance;

  GdkPaintable *paintable;
  double scale_factor;
};

struct _GtkScalerClass
{
  GObjectClass parent_class;
};

static void
gtk_scaler_paintable_snapshot (GdkPaintable *paintable,
                               GdkSnapshot  *snapshot,
                               double        width,
                               double        height)
{
  GtkScaler *self = GTK_SCALER (paintable);

  gtk_snapshot_save (snapshot);

  gtk_snapshot_scale (snapshot, 1.0 / self->scale_factor, 1.0 / self->scale_factor);

  gdk_paintable_snapshot (self->paintable,
                          snapshot,
                          width * self->scale_factor,
                          height * self->scale_factor);

  gtk_snapshot_restore (snapshot);
}

static GdkPaintable *
gtk_scaler_paintable_get_current_image (GdkPaintable *paintable)
{
  GtkScaler *self = GTK_SCALER (paintable);
  GdkPaintable *current_paintable, *current_self;

  current_paintable = gdk_paintable_get_current_image (self->paintable);
  current_self = gtk_scaler_new (current_paintable, self->scale_factor);
  g_object_unref (current_paintable);

  return current_self;
}

static GdkPaintableFlags
gtk_scaler_paintable_get_flags (GdkPaintable *paintable)
{
  GtkScaler *self = GTK_SCALER (paintable);

  return gdk_paintable_get_flags (self->paintable);
}

static int
gtk_scaler_paintable_get_intrinsic_width (GdkPaintable *paintable)
{
  GtkScaler *self = GTK_SCALER (paintable);

  return gdk_paintable_get_intrinsic_width (self->paintable) / self->scale_factor;
}

static int
gtk_scaler_paintable_get_intrinsic_height (GdkPaintable *paintable)
{
  GtkScaler *self = GTK_SCALER (paintable);

  return gdk_paintable_get_intrinsic_height (self->paintable) / self->scale_factor;
}

static double gtk_scaler_paintable_get_intrinsic_aspect_ratio (GdkPaintable *paintable)
{
  GtkScaler *self = GTK_SCALER (paintable);

  return gdk_paintable_get_intrinsic_aspect_ratio (self->paintable);
};

static void
gtk_scaler_paintable_init (GdkPaintableInterface *iface)
{
  iface->snapshot = gtk_scaler_paintable_snapshot;
  iface->get_current_image = gtk_scaler_paintable_get_current_image;
  iface->get_flags = gtk_scaler_paintable_get_flags;
  iface->get_intrinsic_width = gtk_scaler_paintable_get_intrinsic_width;
  iface->get_intrinsic_height = gtk_scaler_paintable_get_intrinsic_height;
  iface->get_intrinsic_aspect_ratio = gtk_scaler_paintable_get_intrinsic_aspect_ratio;
}

G_DEFINE_TYPE_EXTENDED (GtkScaler, gtk_scaler, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
                                               gtk_scaler_paintable_init))

static void
gtk_scaler_dispose (GObject *object)
{
  GtkScaler *self = GTK_SCALER (object);

  if (self->paintable)
    {
      const guint flags = gdk_paintable_get_flags (self->paintable);

      if ((flags & GDK_PAINTABLE_STATIC_CONTENTS) == 0)
        g_signal_handlers_disconnect_by_func (self->paintable, gdk_paintable_invalidate_contents, self);

      if ((flags & GDK_PAINTABLE_STATIC_SIZE) == 0)
        g_signal_handlers_disconnect_by_func (self->paintable, gdk_paintable_invalidate_size, self);

      g_clear_object (&self->paintable);
    }

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

static void
gtk_scaler_class_init (GtkScalerClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->dispose = gtk_scaler_dispose;
}

static void
gtk_scaler_init (GtkScaler *self)
{
  self->scale_factor = 1.0;
}

GdkPaintable *
gtk_scaler_new (GdkPaintable *paintable,
                double        scale_factor)
{
  GtkScaler *self;
  guint flags;

  g_return_val_if_fail (GDK_IS_PAINTABLE (paintable), NULL);
  g_return_val_if_fail (scale_factor > 0.0, NULL);

  self = g_object_new (GTK_TYPE_SCALER, NULL);

  self->paintable = g_object_ref (paintable);
  flags = gdk_paintable_get_flags (paintable);

  if ((flags & GDK_PAINTABLE_STATIC_CONTENTS) == 0)
    g_signal_connect_swapped (paintable, "invalidate-contents", G_CALLBACK (gdk_paintable_invalidate_contents), self);

  if ((flags & GDK_PAINTABLE_STATIC_SIZE) == 0)
    g_signal_connect_swapped (paintable, "invalidate-size", G_CALLBACK (gdk_paintable_invalidate_size), self);

  self->scale_factor = scale_factor;

  return GDK_PAINTABLE (self);
}