summaryrefslogtreecommitdiff
path: root/gtk/gtkfixedlayout.c
blob: 1cf67c3ba39a0fd85e4f564f3ae55216ef85f23a (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
359
360
361
362
363
364
365
366
367
368
369
370
/* gtkfixedlayout.c: Fixed positioning layout manager
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * Copyright 2019 GNOME Foundation
 *
 * 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 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/>.
 */

/**
 * GtkFixedLayout:
 *
 * `GtkFixedLayout` is a layout manager which can place child widgets
 * at fixed positions.
 *
 * Most applications should never use this layout manager; fixed positioning
 * and sizing requires constant recalculations on where children need to be
 * positioned and sized. Other layout managers perform this kind of work
 * internally so that application developers don't need to do it. Specifically,
 * widgets positioned in a fixed layout manager will need to take into account:
 *
 * - Themes, which may change widget sizes.
 *
 * - Fonts other than the one you used to write the app will of course
 *   change the size of widgets containing text; keep in mind that
 *   users may use a larger font because of difficulty reading the
 *   default, or they may be using a different OS that provides different
 *   fonts.
 *
 * - Translation of text into other languages changes its size. Also,
 *   display of non-English text will use a different font in many
 *   cases.
 *
 * In addition, `GtkFixedLayout` does not pay attention to text direction and
 * thus may produce unwanted results if your app is run under right-to-left
 * languages such as Hebrew or Arabic. That is: normally GTK will order
 * containers appropriately depending on the text direction, e.g. to put labels
 * to the right of the thing they label when using an RTL language;
 * `GtkFixedLayout` won't be able to do that for you.
 *
 * Finally, fixed positioning makes it kind of annoying to add/remove UI
 * elements, since you have to reposition all the other  elements. This is a
 * long-term maintenance problem for your application.
 */

/**
 * GtkFixedLayoutChild:
 *
 * `GtkLayoutChild` subclass for children in a `GtkFixedLayout`.
 */

#include "config.h"

#include "gtkfixedlayout.h"

#include "gtkintl.h"
#include "gtklayoutchild.h"
#include "gtkprivate.h"
#include "gtkwidgetprivate.h"

#include <graphene-gobject.h>

struct _GtkFixedLayout
{
  GtkLayoutManager parent_instance;
};

struct _GtkFixedLayoutChild
{
  GtkLayoutChild parent_instance;

  GskTransform *transform;
};

enum
{
  PROP_CHILD_TRANSFORM = 1,

  N_CHILD_PROPERTIES
};

static GParamSpec *child_props[N_CHILD_PROPERTIES];

G_DEFINE_TYPE (GtkFixedLayoutChild, gtk_fixed_layout_child, GTK_TYPE_LAYOUT_CHILD)

static void
gtk_fixed_layout_child_set_property (GObject      *gobject,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  GtkFixedLayoutChild *self = GTK_FIXED_LAYOUT_CHILD (gobject);

  switch (prop_id)
    {
    case PROP_CHILD_TRANSFORM:
      gtk_fixed_layout_child_set_transform (self, g_value_get_boxed (value));
      break;

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

static void
gtk_fixed_layout_child_get_property (GObject    *gobject,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  GtkFixedLayoutChild *self = GTK_FIXED_LAYOUT_CHILD (gobject);

  switch (prop_id)
    {
    case PROP_CHILD_TRANSFORM:
      g_value_set_boxed (value, &self->transform);
      break;

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

static void
gtk_fixed_layout_child_finalize (GObject *gobject)
{
  GtkFixedLayoutChild *self = GTK_FIXED_LAYOUT_CHILD (gobject);

  gsk_transform_unref (self->transform);

  G_OBJECT_CLASS (gtk_fixed_layout_child_parent_class)->finalize (gobject);
}

static void
gtk_fixed_layout_child_class_init (GtkFixedLayoutChildClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = gtk_fixed_layout_child_set_property;
  gobject_class->get_property = gtk_fixed_layout_child_get_property;
  gobject_class->finalize = gtk_fixed_layout_child_finalize;

  /**
   * GtkFixedLayoutChild:transform: (attributes org.gtk.Property.get=gtk_fixed_layout_child_get_transform org.gtk.Property.set=gtk_fixed_layout_child_set_transform)
   *
   * The transform of the child.
   */
  child_props[PROP_CHILD_TRANSFORM] =
    g_param_spec_boxed ("transform",
                        P_("transform"),
                        P_("The transform of a child of a fixed layout"),
                        GSK_TYPE_TRANSFORM,
                        G_PARAM_READWRITE |
                        G_PARAM_STATIC_STRINGS |
                        G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (gobject_class, N_CHILD_PROPERTIES, child_props);
}

static void
gtk_fixed_layout_child_init (GtkFixedLayoutChild *self)
{
}

/**
 * gtk_fixed_layout_child_set_transform: (attributes org.gtk.Method.set_property=transform)
 * @child: a `GtkFixedLayoutChild`
 * @transform: a `GskTransform`
 *
 * Sets the transformation of the child of a `GtkFixedLayout`.
 */
void
gtk_fixed_layout_child_set_transform (GtkFixedLayoutChild *child,
                                      GskTransform        *transform)
{
  GtkLayoutManager *layout;

  g_return_if_fail (GTK_IS_FIXED_LAYOUT_CHILD (child));

  gsk_transform_unref (child->transform);
  child->transform = gsk_transform_ref (transform);

  layout = gtk_layout_child_get_layout_manager (GTK_LAYOUT_CHILD (child));
  gtk_layout_manager_layout_changed (layout);

  g_object_notify_by_pspec (G_OBJECT (child), child_props[PROP_CHILD_TRANSFORM]);
}

/**
 * gtk_fixed_layout_child_get_transform:
 * @child: a `GtkFixedLayoutChild`
 *
 * Retrieves the transformation of the child.
 *
 * Returns: (transfer none) (nullable): a `GskTransform`
 */
GskTransform *
gtk_fixed_layout_child_get_transform (GtkFixedLayoutChild *child)
{
  g_return_val_if_fail (GTK_IS_FIXED_LAYOUT_CHILD (child), NULL);

  return child->transform;
}

G_DEFINE_TYPE (GtkFixedLayout, gtk_fixed_layout, GTK_TYPE_LAYOUT_MANAGER)

static GtkSizeRequestMode
gtk_fixed_layout_get_request_mode (GtkLayoutManager *layout_manager,
                                   GtkWidget        *widget)
{
  return GTK_SIZE_REQUEST_CONSTANT_SIZE;
}

static void
gtk_fixed_layout_measure (GtkLayoutManager *layout_manager,
                          GtkWidget        *widget,
                          GtkOrientation    orientation,
                          int               for_size,
                          int              *minimum,
                          int              *natural,
                          int              *minimum_baseline,
                          int              *natural_baseline)
{
  GtkFixedLayoutChild *child_info;
  GtkWidget *child;
  int minimum_size = 0;
  int natural_size = 0;

  for (child = _gtk_widget_get_first_child (widget);
       child != NULL;
       child = _gtk_widget_get_next_sibling (child))
    {
      int child_min = 0, child_nat = 0;
      int child_min_opp = 0, child_nat_opp = 0;
      graphene_rect_t min_rect, nat_rect;

      if (!gtk_widget_should_layout (child))
        continue;

      child_info = GTK_FIXED_LAYOUT_CHILD (gtk_layout_manager_get_layout_child (layout_manager, child));

      gtk_widget_measure (child, orientation, -1,
                          &child_min, &child_nat,
                          NULL, NULL);
      gtk_widget_measure (child, OPPOSITE_ORIENTATION (orientation), -1,
                          &child_min_opp, &child_nat_opp,
                          NULL, NULL);

      min_rect.origin.x = min_rect.origin.y = 0;
      nat_rect.origin.x = nat_rect.origin.y = 0;
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          min_rect.size.width = child_min;
          min_rect.size.height = child_min_opp;
          nat_rect.size.width = child_nat;
          nat_rect.size.height = child_nat_opp;
        }
      else
       {
          min_rect.size.width = child_min_opp;
          min_rect.size.height = child_min;
          nat_rect.size.width = child_nat_opp;
          nat_rect.size.height = child_nat;
       }

      gsk_transform_transform_bounds (child_info->transform, &min_rect, &min_rect);
      gsk_transform_transform_bounds (child_info->transform, &nat_rect, &nat_rect);

      if (orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          minimum_size = MAX (minimum_size, min_rect.origin.x + min_rect.size.width);
          natural_size = MAX (natural_size, nat_rect.origin.x + nat_rect.size.width);
        }
      else
        {
          minimum_size = MAX (minimum_size, min_rect.origin.y + min_rect.size.height);
          natural_size = MAX (natural_size, nat_rect.origin.y + nat_rect.size.height);
        }
    }

  if (minimum != NULL)
    *minimum = minimum_size;
  if (natural != NULL)
    *natural = natural_size;
}

static void
gtk_fixed_layout_allocate (GtkLayoutManager *layout_manager,
                           GtkWidget        *widget,
                           int               width,
                           int               height,
                           int               baseline)
{
  GtkFixedLayoutChild *child_info;
  GtkWidget *child;

  for (child = _gtk_widget_get_first_child (widget);
       child != NULL;
       child = _gtk_widget_get_next_sibling (child))
    {
      GtkRequisition child_req;

      if (!gtk_widget_should_layout (child))
        continue;

      child_info = GTK_FIXED_LAYOUT_CHILD (gtk_layout_manager_get_layout_child (layout_manager, child));
      gtk_widget_get_preferred_size (child, &child_req, NULL);

      gtk_widget_allocate (child,
                           child_req.width,
                           child_req.height,
                           -1,
                           gsk_transform_ref (child_info->transform));
    }
}

static GtkLayoutChild *
gtk_fixed_layout_create_layout_child (GtkLayoutManager *manager,
                                      GtkWidget        *widget,
                                      GtkWidget        *for_child)
{
  return g_object_new (GTK_TYPE_FIXED_LAYOUT_CHILD,
                       "layout-manager", manager,
                       "child-widget", for_child,
                       NULL);
}

static void
gtk_fixed_layout_class_init (GtkFixedLayoutClass *klass)
{
  GtkLayoutManagerClass *layout_class = GTK_LAYOUT_MANAGER_CLASS (klass);

  layout_class->layout_child_type = GTK_TYPE_FIXED_LAYOUT_CHILD;

  layout_class->get_request_mode = gtk_fixed_layout_get_request_mode;
  layout_class->measure = gtk_fixed_layout_measure;
  layout_class->allocate = gtk_fixed_layout_allocate;
  layout_class->create_layout_child = gtk_fixed_layout_create_layout_child;
}

static void
gtk_fixed_layout_init (GtkFixedLayout *self)
{
}

/**
 * gtk_fixed_layout_new:
 *
 * Creates a new `GtkFixedLayout`.
 *
 * Returns: the newly created `GtkFixedLayout`
 */
GtkLayoutManager *
gtk_fixed_layout_new (void)
{
  return g_object_new (GTK_TYPE_FIXED_LAYOUT, NULL);
}