summaryrefslogtreecommitdiff
path: root/gtk/gtknative.c
blob: 45ad317cbf33e5f2481abc578215c92184061568 (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
/*
 * Copyright © 2019 Red Hat, Inc.
 *
 * 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: Matthias Clasen <mclasen@redhat.com>
 */

#include "config.h"

#include "gtknativeprivate.h"
#include "gtkwidgetprivate.h"
#include "gdk/gdk-private.h"
#include "gtkprivate.h"
#include "gtkintl.h"
#include "gtkcssnodeprivate.h"

typedef struct _GtkNativePrivate
{
  gulong update_handler_id;
  gulong layout_handler_id;
} GtkNativePrivate;

static GQuark quark_gtk_native_private;

/**
 * SECTION:gtknative
 * @Title: GtkNative
 * @Short_description: Interface for widgets having surfaces
 * @See_also: #GtkRoot
 *
 * #GtkNative is the interface implemented by all widgets that can provide
 * a GdkSurface for widgets to render on.
 *
 * The obvious example of a #GtkNative is #GtkWindow.
 */

G_DEFINE_INTERFACE (GtkNative, gtk_native, GTK_TYPE_WIDGET)

static GskRenderer *
gtk_native_default_get_renderer (GtkNative *self)
{
  return NULL;
}

static void
gtk_native_default_get_surface_transform (GtkNative *self,
                                          double    *x,
                                          double    *y)
{
  *x = 0;
  *y = 0;
}

static void
gtk_native_default_check_resize (GtkNative *self)
{
}

static void
gtk_native_default_layout (GtkNative *self,
                           int        width,
                           int        height)
{
}

static void
gtk_native_default_init (GtkNativeInterface *iface)
{
  iface->get_renderer = gtk_native_default_get_renderer;
  iface->get_surface_transform = gtk_native_default_get_surface_transform;
  iface->check_resize = gtk_native_default_check_resize;
  iface->layout = gtk_native_default_layout;

  quark_gtk_native_private = g_quark_from_static_string ("gtk-native-private");
}

static void
frame_clock_update_cb (GdkFrameClock *clock,
                       GtkNative     *native)
{
  if (GTK_IS_ROOT (native))
    gtk_css_node_validate (gtk_widget_get_css_node (GTK_WIDGET (native)));
}

static void
gtk_native_layout (GtkNative *self,
                   int        width,
                   int        height)
{
  return GTK_NATIVE_GET_IFACE (self)->layout (self, width, height);
}

static void
surface_layout_cb (GdkSurface *surface,
                   int         width,
                   int         height,
                   GtkNative  *native)
{
  gtk_native_layout (native, width, height);

  if (gtk_widget_needs_allocate (GTK_WIDGET (native)))
    gtk_native_queue_relayout (native);
}

static void
verify_priv_unrealized (gpointer user_data)
{
  GtkNativePrivate *priv = user_data;

  g_warn_if_fail (priv->update_handler_id == 0);
  g_warn_if_fail (priv->layout_handler_id == 0);

  g_free (priv);
}

void
gtk_native_realize (GtkNative *self)
{
  GdkSurface *surface;
  GdkFrameClock *clock;
  GtkNativePrivate *priv;

  g_return_if_fail (g_object_get_qdata (G_OBJECT (self),
                                        quark_gtk_native_private) == NULL);

  surface = gtk_native_get_surface (self);
  clock = gdk_surface_get_frame_clock (surface);
  g_return_if_fail (clock != NULL);

  priv = g_new0 (GtkNativePrivate, 1);
  priv->update_handler_id = g_signal_connect_after (clock, "update",
                                              G_CALLBACK (frame_clock_update_cb),
                                              self);
  priv->layout_handler_id = g_signal_connect (surface, "layout",
                                              G_CALLBACK (surface_layout_cb),
                                              self);
  g_object_set_qdata_full (G_OBJECT (self),
                           quark_gtk_native_private,
                           priv,
                           verify_priv_unrealized);
}

void
gtk_native_unrealize (GtkNative *self)
{
  GtkNativePrivate *priv;
  GdkSurface *surface;
  GdkFrameClock *clock;

  priv = g_object_get_qdata (G_OBJECT (self), quark_gtk_native_private);
  g_return_if_fail (priv != NULL);

  surface = gtk_native_get_surface (self);
  clock = gdk_surface_get_frame_clock (surface);
  g_return_if_fail (clock != NULL);

  g_clear_signal_handler (&priv->update_handler_id, clock);
  g_clear_signal_handler (&priv->layout_handler_id, surface);

  g_object_set_qdata (G_OBJECT (self), quark_gtk_native_private, NULL);
}

/**
 * gtk_native_get_surface:
 * @self: a #GtkNative
 *
 * Returns the surface of this #GtkNative.
 *
 * Returns: (transfer none): the surface of @self
 */
GdkSurface *
gtk_native_get_surface (GtkNative *self)
{
  g_return_val_if_fail (GTK_IS_NATIVE (self), NULL);

  return GTK_NATIVE_GET_IFACE (self)->get_surface (self);
}

/**
 * gtk_native_get_renderer:
 * @self: a #GtkNative
 *
 * Returns the renderer that is used for this #GtkNative.
 *
 * Returns: (transfer none): the renderer for @self
 */
GskRenderer *
gtk_native_get_renderer (GtkNative *self)
{
  g_return_val_if_fail (GTK_IS_NATIVE (self), NULL);

  return GTK_NATIVE_GET_IFACE (self)->get_renderer (self);
}

/**
 * gtk_native_get_surface_transform:
 * @self: a @GtkNative
 * @x: (out): return location for the x coordinate
 * @y: (out): return location for the y coordinate
 *
 * Retrieves the surface transform of @self. This is the translation
 * from @self's surface coordinates into @self's widget coordinates.
 */
void
gtk_native_get_surface_transform (GtkNative *self,
                                  double    *x,
                                  double    *y)
{
  g_return_if_fail (GTK_IS_NATIVE (self));
  g_return_if_fail (x != NULL);
  g_return_if_fail (y != NULL);

  return GTK_NATIVE_GET_IFACE (self)->get_surface_transform (self, x, y);
}

/**
 * gtk_native_check_resize:
 * @self: a #GtkNative
 *
 * Reposition and resize a #GtkNative.
 *
 * Widgets need to call this function on their attached
 * native widgets when they receive a new size allocation.
 */
void
gtk_native_check_resize (GtkNative *self)
{
  g_return_if_fail (GTK_IS_NATIVE (self));

  GTK_NATIVE_GET_IFACE (self)->check_resize (self);
}

/**
 * gtk_native_get_for_surface:
 * @surface: a #GdkSurface
 *
 * Finds the GtkNative associated with the surface.
 *
 * Returns: (transfer none): the #GtkNative that is associated with @surface
 */
GtkNative *
gtk_native_get_for_surface (GdkSurface *surface)
{
  GtkWidget *widget;

  widget = (GtkWidget *)gdk_surface_get_widget (surface);

  if (widget && GTK_IS_NATIVE (widget))
    return GTK_NATIVE (widget);

  return NULL;
}

void
gtk_native_queue_relayout (GtkNative *self)
{
  GtkWidget *widget = GTK_WIDGET (self);
  GdkSurface *surface;
  GdkFrameClock *clock;

  surface = gtk_widget_get_surface (widget);
  clock = gtk_widget_get_frame_clock (widget);
  if (clock == NULL)
    return;

  gdk_frame_clock_request_phase (clock, GDK_FRAME_CLOCK_PHASE_UPDATE);
  gdk_surface_request_layout (surface);
}