gtklayoutmanager.c 18.2 KB
Newer Older
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* gtklayoutmanager.c: Layout manager base class
 * Copyright 2018  The 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.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/>.
 *
 * Author: Emmanuele Bassi
 */

/**
Matthias Clasen's avatar
Matthias Clasen committed
21
 * GtkLayoutManager:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
22
 *
23
 * Layout managers are delegate classes that handle the preferred size
Matthias Clasen's avatar
Matthias Clasen committed
24
 * and the allocation of a widget.
25
 *
Matthias Clasen's avatar
Matthias Clasen committed
26
 * You typically subclass `GtkLayoutManager` if you want to implement a
Emmanuele Bassi's avatar
Emmanuele Bassi committed
27
28
 * layout policy for the children of a widget, or if you want to determine
 * the size of a widget depending on its contents.
29
 *
Matthias Clasen's avatar
Matthias Clasen committed
30
31
32
 * Each `GtkWidget` can only have a `GtkLayoutManager` instance associated
 * to it at any given time; it is possible, though, to replace the layout
 * manager instance using [method@Gtk.Widget.set_layout_manager].
33
34
35
36
 *
 * ## Layout properties
 *
 * A layout manager can expose properties for controlling the layout of
Matthias Clasen's avatar
Matthias Clasen committed
37
38
 * each child, by creating an object type derived from [class@Gtk.LayoutChild]
 * and installing the properties on it as normal `GObject` properties.
39
 *
Matthias Clasen's avatar
Matthias Clasen committed
40
41
42
 * Each `GtkLayoutChild` instance storing the layout properties for a
 * specific child is created through the [method@Gtk.LayoutManager.get_layout_child]
 * method; a `GtkLayoutManager` controls the creation of its `GtkLayoutChild`
43
44
45
 * instances by overriding the GtkLayoutManagerClass.create_layout_child()
 * virtual function. The typical implementation should look like:
 *
Matthias Clasen's avatar
Matthias Clasen committed
46
 * ```c
47
48
49
50
51
52
53
54
55
56
 * static GtkLayoutChild *
 * create_layout_child (GtkLayoutManager *manager,
 *                      GtkWidget        *container,
 *                      GtkWidget        *child)
 * {
 *   return g_object_new (your_layout_child_get_type (),
 *                        "layout-manager", manager,
 *                        "child-widget", child,
 *                        NULL);
 * }
Matthias Clasen's avatar
Matthias Clasen committed
57
 * ```
58
 *
Matthias Clasen's avatar
Matthias Clasen committed
59
60
61
62
63
64
 * The [property@Gtk.LayoutChild:layout-manager] and
 * [property@Gtk.LayoutChild:child-widget] properties
 * on the newly created `GtkLayoutChild` instance are mandatory. The
 * `GtkLayoutManager` will cache the newly created `GtkLayoutChild` instance
 * until the widget is removed from its parent, or the parent removes the
 * layout manager.
65
 *
Matthias Clasen's avatar
Matthias Clasen committed
66
67
68
69
70
 * Each `GtkLayoutManager` instance creating a `GtkLayoutChild` should use
 * [method@Gtk.LayoutManager.get_layout_child] every time it needs to query
 * the layout properties; each `GtkLayoutChild` instance should call
 * [method@Gtk.LayoutManager.layout_changed] every time a property is
 * updated, in order to queue a new size measuring and allocation.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
71
72
73
74
 */

#include "config.h"

75
#include "gtklayoutmanagerprivate.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
76
#include "gtklayoutchild.h"
77
#include "gtkwidgetprivate.h"
78
#include "gtknative.h"
79
#include "gtkpopover.h"
80
#include "gtktexthandleprivate.h"
81
#include "gtktooltipwindowprivate.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#ifdef G_ENABLE_DEBUG
#define LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED(m,method)   G_STMT_START {  \
        GObject *_obj = G_OBJECT (m);                                   \
        g_warning ("Layout managers of type %s do not implement "       \
                   "the GtkLayoutManager::%s method",                   \
                   G_OBJECT_TYPE_NAME (_obj),                           \
                   #method);                           } G_STMT_END
#else
#define LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED(m,method)
#endif

typedef struct {
  GtkWidget *widget;
96
  GtkRoot *root;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
97
98
99

  /* HashTable<Widget, LayoutChild> */
  GHashTable *layout_children;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
100
101
102
103
} GtkLayoutManagerPrivate;

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkLayoutManager, gtk_layout_manager, G_TYPE_OBJECT)

104
105
106
107
108
109
110
111
112
113
static void
gtk_layout_manager_real_root (GtkLayoutManager *manager)
{
}

static void
gtk_layout_manager_real_unroot (GtkLayoutManager *manager)
{
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
114
115
116
117
static GtkSizeRequestMode
gtk_layout_manager_real_get_request_mode (GtkLayoutManager *manager,
                                          GtkWidget        *widget)
{
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
  int hfw = 0, wfh = 0;
  GtkWidget *child;

  for (child = _gtk_widget_get_first_child (widget);
       child != NULL;
       child = _gtk_widget_get_next_sibling (child))
    {
      GtkSizeRequestMode res = gtk_widget_get_request_mode (child);

      switch (res)
        {
        case GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH:
          hfw += 1;
          break;

        case GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT:
          wfh += 1;
          break;

        case GTK_SIZE_REQUEST_CONSTANT_SIZE:
        default:
          break;
        }
    }

 if (hfw == 0 && wfh == 0)
   return GTK_SIZE_REQUEST_CONSTANT_SIZE;

 return hfw > wfh ? GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH
                  : GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
}

static void
gtk_layout_manager_real_measure (GtkLayoutManager *manager,
                                 GtkWidget        *widget,
                                 GtkOrientation    orientation,
                                 int               for_size,
                                 int              *minimum,
                                 int              *natural,
                                 int              *baseline_minimum,
                                 int              *baseline_natural)
{
  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, measure);

  if (minimum != NULL)
    *minimum = 0;

  if (natural != NULL)
    *natural = 0;

  if (baseline_minimum != NULL)
    *baseline_minimum = 0;

  if (baseline_natural != NULL)
    *baseline_natural = 0;
}

static void
gtk_layout_manager_real_allocate (GtkLayoutManager *manager,
                                  GtkWidget        *widget,
                                  int               width,
                                  int               height,
                                  int               baseline)
{
  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, allocate);
}

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
static GtkLayoutChild *
gtk_layout_manager_real_create_layout_child (GtkLayoutManager *manager,
                                             GtkWidget        *widget,
                                             GtkWidget        *child)
{
  GtkLayoutManagerClass *manager_class = GTK_LAYOUT_MANAGER_GET_CLASS (manager);

  if (manager_class->layout_child_type == G_TYPE_INVALID)
    {
      LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, create_layout_child);
      return NULL;
    }

  return g_object_new (manager_class->layout_child_type,
                       "layout-manager", manager,
200
                       "child-widget", child,
201
202
203
                       NULL);
}

204
205
206
207
208
209
210
211
212
213
214
static void
gtk_layout_manager_finalize (GObject *gobject)
{
  GtkLayoutManager *self = GTK_LAYOUT_MANAGER (gobject);
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (self);

  g_clear_pointer (&priv->layout_children, g_hash_table_unref);

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

Emmanuele Bassi's avatar
Emmanuele Bassi committed
215
216
217
static void
gtk_layout_manager_class_init (GtkLayoutManagerClass *klass)
{
218
219
220
221
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gtk_layout_manager_finalize;

Emmanuele Bassi's avatar
Emmanuele Bassi committed
222
223
224
  klass->get_request_mode = gtk_layout_manager_real_get_request_mode;
  klass->measure = gtk_layout_manager_real_measure;
  klass->allocate = gtk_layout_manager_real_allocate;
225
  klass->create_layout_child = gtk_layout_manager_real_create_layout_child;
226
227
  klass->root = gtk_layout_manager_real_root;
  klass->unroot = gtk_layout_manager_real_unroot;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
228
229
230
231
232
233
234
235
236
}

static void
gtk_layout_manager_init (GtkLayoutManager *self)
{
}

/*< private >
 * gtk_layout_manager_set_widget:
Matthias Clasen's avatar
Matthias Clasen committed
237
238
 * @layout_manager: a `GtkLayoutManager`
 * @widget: (nullable): a `GtkWidget`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
239
 *
240
 * Sets a back pointer from @widget to @layout_manager.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
241
242
243
244
245
246
247
 */
void
gtk_layout_manager_set_widget (GtkLayoutManager *layout_manager,
                               GtkWidget        *widget)
{
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (layout_manager);

248
249
250
251
252
253
254
255
256
257
  if (widget != NULL && priv->widget != NULL)
    {
      g_critical ("The layout manager %p of type %s is already in use "
                  "by widget %p '%s', and cannot be used by widget %p '%s'",
                  layout_manager, G_OBJECT_TYPE_NAME (layout_manager),
                  priv->widget, gtk_widget_get_name (priv->widget),
                  widget, gtk_widget_get_name (widget));
      return;
    }

Emmanuele Bassi's avatar
Emmanuele Bassi committed
258
  priv->widget = widget;
259
260
261
262
263
264
265

  if (widget != NULL)
    gtk_layout_manager_set_root (layout_manager, gtk_widget_get_root (widget));
}

/*< private >
 * gtk_layout_manager_set_root:
Matthias Clasen's avatar
Matthias Clasen committed
266
267
 * @layout_manager: a i`GtkLayoutManager`
 * @root: (nullable): a `GtkWidget` implementing `GtkRoot`
268
269
270
 *
 * Sets a back pointer from @root to @layout_manager.
 *
Matthias Clasen's avatar
Matthias Clasen committed
271
272
273
 * This function is called by `GtkWidget` when getting rooted and unrooted,
 * and will call `GtkLayoutManagerClass.root()` or `GtkLayoutManagerClass.unroot()`
 * depending on whether @root is a `GtkWidget` or %NULL.
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
 */
void
gtk_layout_manager_set_root (GtkLayoutManager *layout_manager,
                             GtkRoot          *root)
{
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (layout_manager);
  GtkRoot *old_root = priv->root;

  priv->root = root;

  if (old_root != root)
    {
      if (priv->root != NULL)
        GTK_LAYOUT_MANAGER_GET_CLASS (layout_manager)->root (layout_manager);
      else
        GTK_LAYOUT_MANAGER_GET_CLASS (layout_manager)->unroot (layout_manager);
    }
Emmanuele Bassi's avatar
Emmanuele Bassi committed
291
292
293
294
}

/**
 * gtk_layout_manager_measure:
Matthias Clasen's avatar
Matthias Clasen committed
295
296
 * @manager: a `GtkLayoutManager`
 * @widget: the `GtkWidget` using @manager
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
 * @orientation: the orientation to measure
 * @for_size: Size for the opposite of @orientation; for instance, if
 *   the @orientation is %GTK_ORIENTATION_HORIZONTAL, this is the height
 *   of the widget; if the @orientation is %GTK_ORIENTATION_VERTICAL, this
 *   is the width of the widget. This allows to measure the height for the
 *   given width, and the width for the given height. Use -1 if the size
 *   is not known
 * @minimum: (out) (optional): the minimum size for the given size and
 *   orientation
 * @natural: (out) (optional): the natural, or preferred size for the
 *   given size and orientation
 * @minimum_baseline: (out) (optional): the baseline position for the
 *   minimum size
 * @natural_baseline: (out) (optional): the baseline position for the
 *   natural size
Emmanuele Bassi's avatar
Emmanuele Bassi committed
312
 *
313
314
 * Measures the size of the @widget using @manager, for the
 * given @orientation and size.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
315
 *
Matthias Clasen's avatar
Matthias Clasen committed
316
 * See the [class@Gtk.Widget] documentation on layout management for
317
 * more details.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
318
319
320
321
322
323
324
325
326
327
328
329
 */
void
gtk_layout_manager_measure (GtkLayoutManager *manager,
                            GtkWidget        *widget,
                            GtkOrientation    orientation,
                            int               for_size,
                            int              *minimum,
                            int              *natural,
                            int              *minimum_baseline,
                            int              *natural_baseline)
{
  GtkLayoutManagerClass *klass;
330
331
332
333
334
  int min_size = 0;
  int nat_size = 0;
  int min_baseline = -1;
  int nat_baseline = -1;

Emmanuele Bassi's avatar
Emmanuele Bassi committed
335
336
337
338
339
340
341
342

  g_return_if_fail (GTK_IS_LAYOUT_MANAGER (manager));
  g_return_if_fail (GTK_IS_WIDGET (widget));

  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);

  klass->measure (manager, widget, orientation,
                  for_size,
343
344
345
346
347
348
349
350
351
352
353
354
355
356
                  &min_size, &nat_size,
                  &min_baseline, &nat_baseline);

  if (minimum)
    *minimum = min_size;

  if (natural)
    *natural = nat_size;

  if (minimum_baseline)
    *minimum_baseline = min_baseline;

  if (natural_baseline)
    *natural_baseline = nat_baseline;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
357
358
}

359
360
361
362
363
364
365
366
367
static void
allocate_native_children (GtkWidget *widget)
{
  GtkWidget *child;

  for (child = _gtk_widget_get_first_child (widget);
       child != NULL;
       child = _gtk_widget_get_next_sibling (child))
    {
368
369
      if (GTK_IS_POPOVER (child))
        gtk_popover_present (GTK_POPOVER (child));
370
371
      else if (GTK_IS_TEXT_HANDLE (child))
        gtk_text_handle_present (GTK_TEXT_HANDLE (child));
372
373
      else if (GTK_IS_TOOLTIP_WINDOW (child))
        gtk_tooltip_window_present (GTK_TOOLTIP_WINDOW (child));
374
      else if (GTK_IS_NATIVE (child))
375
376
        g_warning ("Unable to present a to the layout manager unknown auxiliary child surface widget type %s",
                   G_OBJECT_TYPE_NAME (child));
377
378
379
    }
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
380
381
/**
 * gtk_layout_manager_allocate:
Matthias Clasen's avatar
Matthias Clasen committed
382
383
 * @manager: a `GtkLayoutManager`
 * @widget: the `GtkWidget` using @manager
384
385
 * @width: the new width of the @widget
 * @height: the new height of the @widget
Emmanuele Bassi's avatar
Emmanuele Bassi committed
386
 * @baseline: the baseline position of the @widget, or -1
Emmanuele Bassi's avatar
Emmanuele Bassi committed
387
 *
Matthias Clasen's avatar
Matthias Clasen committed
388
 * Assigns the given @width, @height, and @baseline to
389
390
 * a @widget, and computes the position and sizes of the children of
 * the @widget using the layout management policy of @manager.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
391
392
393
394
395
396
397
398
399
400
401
402
 */
void
gtk_layout_manager_allocate (GtkLayoutManager *manager,
                             GtkWidget        *widget,
                             int               width,
                             int               height,
                             int               baseline)
{
  GtkLayoutManagerClass *klass;

  g_return_if_fail (GTK_IS_LAYOUT_MANAGER (manager));
  g_return_if_fail (GTK_IS_WIDGET (widget));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
403
  g_return_if_fail (baseline >= -1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
404

405
406
  allocate_native_children (widget);

Emmanuele Bassi's avatar
Emmanuele Bassi committed
407
408
409
410
411
412
413
  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);

  klass->allocate (manager, widget, width, height, baseline);
}

/**
 * gtk_layout_manager_get_request_mode:
Matthias Clasen's avatar
Matthias Clasen committed
414
 * @manager: a `GtkLayoutManager`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
415
 *
416
 * Retrieves the request mode of @manager.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
417
 *
Matthias Clasen's avatar
Matthias Clasen committed
418
 * Returns: a `GtkSizeRequestMode`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
419
420
 */
GtkSizeRequestMode
Emmanuele Bassi's avatar
Emmanuele Bassi committed
421
gtk_layout_manager_get_request_mode (GtkLayoutManager *manager)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
422
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
423
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (manager);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
424
425
426
  GtkLayoutManagerClass *klass;

  g_return_val_if_fail (GTK_IS_LAYOUT_MANAGER (manager), GTK_SIZE_REQUEST_CONSTANT_SIZE);
427
  g_return_val_if_fail (priv->widget != NULL, GTK_SIZE_REQUEST_CONSTANT_SIZE);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
428
429
430

  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);

Emmanuele Bassi's avatar
Emmanuele Bassi committed
431
  return klass->get_request_mode (manager, priv->widget);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
432
433
}

434
435
/**
 * gtk_layout_manager_get_widget:
Matthias Clasen's avatar
Matthias Clasen committed
436
 * @manager: a `GtkLayoutManager`
437
 *
Matthias Clasen's avatar
Matthias Clasen committed
438
 * Retrieves the `GtkWidget` using the given `GtkLayoutManager`.
439
 *
Matthias Clasen's avatar
Matthias Clasen committed
440
 * Returns: (transfer none) (nullable): a `GtkWidget`
441
442
443
444
445
446
447
448
449
450
451
 */
GtkWidget *
gtk_layout_manager_get_widget (GtkLayoutManager *manager)
{
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (manager);

  g_return_val_if_fail (GTK_IS_LAYOUT_MANAGER (manager), NULL);

  return priv->widget;
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
452
453
/**
 * gtk_layout_manager_layout_changed:
Matthias Clasen's avatar
Matthias Clasen committed
454
 * @manager: a `GtkLayoutManager`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
455
 *
Matthias Clasen's avatar
Matthias Clasen committed
456
 * Queues a resize on the `GtkWidget` using @manager, if any.
457
 *
Matthias Clasen's avatar
Matthias Clasen committed
458
459
 * This function should be called by subclasses of `GtkLayoutManager`
 * in response to changes to their layout management policies.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
460
461
462
463
464
465
 */
void
gtk_layout_manager_layout_changed (GtkLayoutManager *manager)
{
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (manager);

466
467
  g_return_if_fail (GTK_IS_LAYOUT_MANAGER (manager));

Emmanuele Bassi's avatar
Emmanuele Bassi committed
468
469
470
  if (priv->widget != NULL)
    gtk_widget_queue_resize (priv->widget);
}
Emmanuele Bassi's avatar
Emmanuele Bassi committed
471

472
473
/*< private >
 * gtk_layout_manager_remove_layout_child:
Matthias Clasen's avatar
Matthias Clasen committed
474
475
 * @manager: a `GtkLayoutManager`
 * @widget: a `GtkWidget`
476
 *
Matthias Clasen's avatar
Matthias Clasen committed
477
478
 * Removes the `GtkLayoutChild` associated with @widget from the
 * given `GtkLayoutManager`, if any is set.
479
480
481
482
 */
void
gtk_layout_manager_remove_layout_child (GtkLayoutManager *manager,
                                        GtkWidget        *widget)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
483
{
484
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (manager);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
485
486
487
488
489
490
491
492
493

  if (priv->layout_children != NULL)
    {
      g_hash_table_remove (priv->layout_children, widget);
      if (g_hash_table_size (priv->layout_children) == 0)
        g_clear_pointer (&priv->layout_children, g_hash_table_unref);
    }
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
494
495
/**
 * gtk_layout_manager_get_layout_child:
Matthias Clasen's avatar
Matthias Clasen committed
496
497
 * @manager: a `GtkLayoutManager`
 * @child: a `GtkWidget`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
498
 *
Matthias Clasen's avatar
Matthias Clasen committed
499
500
 * Retrieves a `GtkLayoutChild` instance for the `GtkLayoutManager`,
 * creating one if necessary.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
501
502
 *
 * The @child widget must be a child of the widget using @manager.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
503
 *
Matthias Clasen's avatar
Matthias Clasen committed
504
505
506
 * The `GtkLayoutChild` instance is owned by the `GtkLayoutManager`,
 * and is guaranteed to exist as long as @child is a child of the
 * `GtkWidget` using the given `GtkLayoutManager`.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
507
 *
Matthias Clasen's avatar
Matthias Clasen committed
508
 * Returns: (transfer none): a `GtkLayoutChild`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
509
510
511
 */
GtkLayoutChild *
gtk_layout_manager_get_layout_child (GtkLayoutManager *manager,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
512
                                     GtkWidget        *child)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
513
514
515
516
517
518
{
  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (manager);
  GtkLayoutChild *res;
  GtkWidget *parent;

  g_return_val_if_fail (GTK_IS_LAYOUT_MANAGER (manager), NULL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
519
  g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
520

521
  parent = _gtk_widget_get_parent (child);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
522
523
524
525
526
527
528
  g_return_val_if_fail (parent != NULL, NULL);

  if (priv->widget != parent)
    {
      g_critical ("The parent %s %p of the widget %s %p does not "
                  "use the given layout manager of type %s %p",
                  gtk_widget_get_name (parent), parent,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
529
                  gtk_widget_get_name (child), child,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
530
531
532
533
                  G_OBJECT_TYPE_NAME (manager), manager);
      return NULL;
    }

Emmanuele Bassi's avatar
Emmanuele Bassi committed
534
535
536
537
538
539
540
541
  if (priv->layout_children == NULL)
    {
      priv->layout_children = g_hash_table_new_full (NULL, NULL,
                                                     NULL,
                                                     (GDestroyNotify) g_object_unref);
    }

  res = g_hash_table_lookup (priv->layout_children, child);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
542
543
544
545
546
547
548
549
550
551
552
  if (res != NULL)
    {
      /* If the LayoutChild instance is stale, and refers to another
       * layout manager, then we simply ask the LayoutManager to
       * replace it, as it means the layout manager for the parent
       * widget was replaced
       */
      if (gtk_layout_child_get_layout_manager (res) == manager)
        return res;
    }

Emmanuele Bassi's avatar
Emmanuele Bassi committed
553
  res = GTK_LAYOUT_MANAGER_GET_CLASS (manager)->create_layout_child (manager, parent, child);
554
555
556
557
558
559
560
  if (res == NULL)
    {
      g_critical ("The layout manager of type %s %p does not create "
                  "GtkLayoutChild instances",
                  G_OBJECT_TYPE_NAME (manager), manager);
      return NULL;
    }
Emmanuele Bassi's avatar
Emmanuele Bassi committed
561

562
  g_assert (g_type_is_a (G_OBJECT_TYPE (res), GTK_TYPE_LAYOUT_CHILD));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
563
  g_hash_table_insert (priv->layout_children, child, res);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
564
565
566

  return res;
}