summaryrefslogtreecommitdiff
path: root/demos/gtk-demo/gtkthrobber.c
blob: c12df51b62fe05eab58739063dfcdc2b916e4e1d (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
/*
 * 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 "gtkthrobber.h"

#include "gtk.h"

struct _GtkThrobber
{
  GObject parent_instance;

  GdkFrameClock *clock;
  GdkTexture *texture;
  guint clock_tick_id;

  float min, max, scale, delta;
  float delta2, hot;

  GdkDrag *drag;
};

struct _GtkThrobberClass
{
  GObjectClass parent_class;
};

static void
gtk_throbber_paintable_snapshot (GdkPaintable *paintable,
                               GdkSnapshot  *snapshot,
                               double        width,
                               double        height)
{
  GtkThrobber *self = GTK_THROBBER (paintable);

  gtk_snapshot_append_texture (snapshot, self->texture, &GRAPHENE_RECT_INIT (0, 0, width, height));
}

static int
gtk_throbber_paintable_get_intrinsic_width (GdkPaintable *paintable)
{
  GtkThrobber *self = GTK_THROBBER (paintable);

  return gdk_texture_get_width (self->texture) * self->scale;
}

static int
gtk_throbber_paintable_get_intrinsic_height (GdkPaintable *paintable)
{
  GtkThrobber *self = GTK_THROBBER (paintable);

  return gdk_texture_get_height (self->texture) * self->scale;
}

static void
gtk_throbber_paintable_init (GdkPaintableInterface *iface)
{
  iface->snapshot = gtk_throbber_paintable_snapshot;
  iface->get_intrinsic_width = gtk_throbber_paintable_get_intrinsic_width;
  iface->get_intrinsic_height = gtk_throbber_paintable_get_intrinsic_height;
}

G_DEFINE_TYPE_EXTENDED (GtkThrobber, gtk_throbber, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
                                               gtk_throbber_paintable_init))

static void
gtk_throbber_dispose (GObject *object)
{
  GtkThrobber *self = GTK_THROBBER (object);

  gdk_frame_clock_end_updating (self->clock);
  g_signal_handler_disconnect (self->clock, self->clock_tick_id);
  self->clock_tick_id = 0;

  g_clear_object (&self->clock);
  g_clear_object (&self->texture);
  g_clear_object (&self->drag);

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

static void
gtk_throbber_class_init (GtkThrobberClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->dispose = gtk_throbber_dispose;
}

static void
gtk_throbber_init (GtkThrobber *self)
{
}

static void
on_frame_clock_update (GdkFrameClock *clock,
                       GtkThrobber   *self)
{
  self->scale += self->delta;
  if (self->scale >= self->max)
    {
      self->scale = self->max;
      self->delta = - self->delta;
    }
  else if (self->scale <= self->min)
    {
      self->scale = self->min;
      self->delta = - self->delta;
    }

  self->hot += self->delta2;
  if (self->hot >= 1.)
    {
      self->hot = 1.;
      self->delta2 = - self->delta2;
    }
  else if (self->hot <= 0.)
    {
      self->hot = 0.;
      self->delta2 = - self->delta2;
    }

  gdk_paintable_invalidate_size (GDK_PAINTABLE (self));

  gdk_drag_set_hotspot (self->drag,
                        gdk_texture_get_width (self->texture) * self->scale * self->hot,
                        gdk_texture_get_height (self->texture) * self->scale * self->hot);
}


GdkPaintable *
gtk_throbber_new (GdkFrameClock *clock,
                  const char    *resource_path,
                  float          min,
                  float          max,
                  GdkDrag       *drag)
{
  GtkThrobber *self;

  self = g_object_new (GTK_TYPE_THROBBER, NULL);

  self->clock = g_object_ref (clock);
  self->texture = gdk_texture_new_from_resource (resource_path);
  self->drag = g_object_ref (drag);
  self->min = min;
  self->max = max;
  self->scale = min;
  self->delta = (max - min) / (15 * 16);
  self->delta2 = 1. / (10 * 16);
  self->hot = 0.;

  self->clock_tick_id = g_signal_connect (self->clock, "update",
                                          G_CALLBACK (on_frame_clock_update), self);

  gdk_frame_clock_begin_updating (self->clock);

  return GDK_PAINTABLE (self);
}