summaryrefslogtreecommitdiff
path: root/gtk/gtkpropertylookuplistmodel.c
blob: 0c0dc4ea9151f203d4bb9acf2f1ffb341fcc3e3b (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * 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>
 */


/*
 * SECTION:gtkpropertylookuplistmodel
 * @Short_description: a List model for property lookups
 * @Title: GtkPropertyLookupListModel
 * @See_also: #GListModel
 *
 * #GtkPropertyLookupListModel is a #GListModel implementation that takes an
 * object and a property and then recursively looks up the next element using
 * the property on the previous object.
 *
 * For example, one could use this list model with the GtkWidget:parent property
 * to get a list of a widgets and all its ancestors.
 **/

#include "config.h"

#include "gtkpropertylookuplistmodelprivate.h"

#include "gtkintl.h"
#include "gtkprivate.h"

enum {
  PROP_0,
  PROP_ITEM_TYPE,
  PROP_OBJECT,
  PROP_PROPERTY,
  NUM_PROPERTIES
};

struct _GtkPropertyLookupListModel
{
  GObject parent_instance;

  GType item_type;
  char *property;
  GPtrArray *items; /* list of items - lazily expanded if there's a NULL sentinel */
};

struct _GtkPropertyLookupListModelClass
{
  GObjectClass parent_class;
};

static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };

static GType
gtk_property_lookup_list_model_get_item_type (GListModel *list)
{
  GtkPropertyLookupListModel *self = GTK_PROPERTY_LOOKUP_LIST_MODEL (list);

  return self->item_type;
}

static gboolean
gtk_property_lookup_list_model_is_infinite (GtkPropertyLookupListModel *self)
{
  if (self->items->len == 0)
    return FALSE;

  return g_ptr_array_index (self->items, self->items->len - 1) == NULL;
}

static void
gtk_property_lookup_list_model_notify_cb (GObject                    *object,
                                          GParamSpec                 *pspec,
                                          GtkPropertyLookupListModel *self);

static guint
gtk_property_lookup_list_model_clear (GtkPropertyLookupListModel *self,
                                      guint                       remaining)
{
  guint i;

  for (i = remaining; i < self->items->len; i++)
    {
      gpointer object = g_ptr_array_index (self->items, i);
      if (object == NULL)
        break;

      g_signal_handlers_disconnect_by_func (object, gtk_property_lookup_list_model_notify_cb, self);
      g_object_unref (object);
    }

  /* keeps the sentinel, yay! */
  g_ptr_array_remove_range (self->items, remaining, i - remaining);

  return i - remaining;
}

static guint
gtk_property_lookup_list_model_append (GtkPropertyLookupListModel *self,
                                       guint                       n_items)
{
  gpointer last, next;
  guint i, start;

  g_assert (self->items->len > 0);
  g_assert (!gtk_property_lookup_list_model_is_infinite (self));

  last = g_ptr_array_index (self->items, self->items->len - 1);
  start = self->items->len;
  for (i = start; i < n_items; i++)
    {
      g_object_get (last, self->property, &next, NULL);
      if (next == NULL)
        return i - start;
      
      g_signal_connect_closure_by_id (next,
                                      g_signal_lookup ("notify", G_OBJECT_TYPE (next)),
                                      g_quark_from_static_string (self->property),
                                      g_cclosure_new (G_CALLBACK (gtk_property_lookup_list_model_notify_cb), G_OBJECT (self), NULL),
                                      FALSE);

      g_ptr_array_add (self->items, next);
      last = next;
    }

  return i - start;
}

static void
gtk_property_lookup_list_model_ensure (GtkPropertyLookupListModel *self,
                                       guint                       n_items)
{
  if (!gtk_property_lookup_list_model_is_infinite (self))
    return;

  if (self->items->len - 1 >= n_items)
    return;
  
  /* remove NULL sentinel */
  g_ptr_array_remove_index (self->items, self->items->len - 1);

  if (self->items->len == 0)
    return;

  if (gtk_property_lookup_list_model_append (self, n_items) == n_items)
    {
      /* readd NULL sentinel */
      g_ptr_array_add (self->items, NULL);
    }
}

static void
gtk_property_lookup_list_model_notify_cb (GObject                    *object,
                                          GParamSpec                 *pspec,
                                          GtkPropertyLookupListModel *self)
{
  guint position, removed, added;

  if (!g_ptr_array_find (self->items, object, &position))
    {
      /* can only happen if we forgot to disconnect a signal handler */
      g_assert_not_reached ();
    }
  /* we found the position of the item that notified, but the first change
   * is its child.
   */
  position++;

  removed = gtk_property_lookup_list_model_clear (self, position);

  if (!gtk_property_lookup_list_model_is_infinite (self))
    added = gtk_property_lookup_list_model_append (self, G_MAXUINT);
  else
    added = 0;

  if (removed > 0 || added > 0)
    g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
}

static guint
gtk_property_lookup_list_model_get_n_items (GListModel *list)
{
  GtkPropertyLookupListModel *self = GTK_PROPERTY_LOOKUP_LIST_MODEL (list);

  gtk_property_lookup_list_model_ensure (self, G_MAXUINT);

  return self->items->len;
}

static gpointer
gtk_property_lookup_list_model_get_item (GListModel *list,
                                         guint       position)
{
  GtkPropertyLookupListModel *self = GTK_PROPERTY_LOOKUP_LIST_MODEL (list);

  gtk_property_lookup_list_model_ensure (self, position + 1);

  if (position >= self->items->len)
    return NULL;

  return g_object_ref (g_ptr_array_index (self->items, position));
}

static void
gtk_property_lookup_list_model_list_model_init (GListModelInterface *iface)
{
  iface->get_item_type = gtk_property_lookup_list_model_get_item_type;
  iface->get_n_items = gtk_property_lookup_list_model_get_n_items;
  iface->get_item = gtk_property_lookup_list_model_get_item;
}

G_DEFINE_TYPE_WITH_CODE (GtkPropertyLookupListModel, gtk_property_lookup_list_model,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_property_lookup_list_model_list_model_init))

static gboolean
check_pspec (GType       type,
             GParamSpec *pspec)
{
  if (pspec == NULL)
    return FALSE;

  if (!G_IS_PARAM_SPEC_OBJECT (pspec))
    return FALSE;

  if (!g_type_is_a (G_PARAM_SPEC (pspec)->value_type, type))
    return FALSE;

  return TRUE;
}

static gboolean
lookup_pspec (GType       type,
              const char *name)
{
  gboolean result;

  if (g_type_is_a (type, G_TYPE_INTERFACE))
    {
      gpointer iface;
      GParamSpec *pspec;

      iface = g_type_default_interface_ref (type);
      pspec = g_object_interface_find_property (iface, name);
      result = check_pspec (type, pspec);
      g_type_default_interface_unref (iface);
    }
  else
    {
      GObjectClass *klass;
      GParamSpec *pspec;

      klass = g_type_class_ref (type);
      g_return_val_if_fail (klass != NULL, FALSE);
      pspec = g_object_class_find_property (klass, name);
      result = check_pspec (type, pspec);
      g_type_class_unref (klass);
    }

  return result;
}

static void
gtk_property_lookup_list_model_set_property (GObject      *object,
                                             guint         prop_id,
                                             const GValue *value,
                                             GParamSpec   *pspec)
{
  GtkPropertyLookupListModel *self = GTK_PROPERTY_LOOKUP_LIST_MODEL (object);

  switch (prop_id)
    {
    case PROP_ITEM_TYPE:
      self->item_type = g_value_get_gtype (value);
      g_return_if_fail (self->item_type != 0);
      break;

    case PROP_OBJECT:
      gtk_property_lookup_list_model_set_object (self, g_value_get_object (value));
      break;

    case PROP_PROPERTY:
      self->property = g_value_dup_string (value);
      break;

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

  if (self->property && self->item_type &&
      !lookup_pspec (self->item_type, self->property))
    {
      g_critical ("type %s has no property named \"%s\"", g_type_name (self->item_type), self->property);
    }
}

static void 
gtk_property_lookup_list_model_get_property (GObject     *object,
                                             guint        prop_id,
                                             GValue      *value,
                                             GParamSpec  *pspec)
{
  GtkPropertyLookupListModel *self = GTK_PROPERTY_LOOKUP_LIST_MODEL (object);

  switch (prop_id)
    {
    case PROP_ITEM_TYPE:
      g_value_set_gtype (value, self->item_type);
      break;

    case PROP_OBJECT:
      g_value_set_object (value, gtk_property_lookup_list_model_get_object (self));
      break;

    case PROP_PROPERTY:
      g_value_set_object (value, self->property);
      break;

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

static void
gtk_property_lookup_list_model_dispose (GObject *object)
{
  GtkPropertyLookupListModel *self = GTK_PROPERTY_LOOKUP_LIST_MODEL (object);

  gtk_property_lookup_list_model_clear (self, 0);

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

static void
gtk_property_lookup_list_model_finalize (GObject *object)
{
  GtkPropertyLookupListModel *self = GTK_PROPERTY_LOOKUP_LIST_MODEL (object);

  g_ptr_array_unref (self->items);
  g_free (self->property);

  G_OBJECT_CLASS (gtk_property_lookup_list_model_parent_class)->finalize (object);
}

static void
gtk_property_lookup_list_model_class_init (GtkPropertyLookupListModelClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = gtk_property_lookup_list_model_get_property;
  object_class->set_property = gtk_property_lookup_list_model_set_property;
  object_class->dispose = gtk_property_lookup_list_model_dispose;
  object_class->finalize = gtk_property_lookup_list_model_finalize;

  /**
   * GtkPropertyLookupListModel:item-type:
   *
   * The #GType for elements of this object
   */
  properties[PROP_ITEM_TYPE] =
      g_param_spec_gtype ("item-type",
                          P_("Item type"),
                          P_("The type of elements of this object"),
                          G_TYPE_OBJECT,
                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * GtkPropertyLookupListModel:property:
   *
   * Name of the property used for lookups
   */
  properties[PROP_PROPERTY] =
      g_param_spec_string ("property",
                           P_("type"),
                           P_("Name of the property used for lookups"),
                           NULL,
                           GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * GtkPropertyLookupListModel:object:
   *
   * The root object
   */
  properties[PROP_OBJECT] =
      g_param_spec_object ("object",
                           P_("Object"),
                           P_("The root object"),
                           G_TYPE_LIST_MODEL,
                           GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}

static void
gtk_property_lookup_list_model_init (GtkPropertyLookupListModel *self)
{
  self->items = g_ptr_array_new ();
  /* add sentinel */
  g_ptr_array_add (self->items, NULL);
}

GtkPropertyLookupListModel *
gtk_property_lookup_list_model_new (GType       item_type,
                                    const char *property_name)
{
  g_return_val_if_fail (g_type_is_a (item_type, G_TYPE_OBJECT), NULL);
  g_return_val_if_fail (property_name != NULL, NULL);

  return g_object_new (GTK_TYPE_PROPERTY_LOOKUP_LIST_MODEL,
                       "item-type", item_type,
                       "property", property_name,
                       NULL);
}

void
gtk_property_lookup_list_model_set_object (GtkPropertyLookupListModel *self,
                                           gpointer                    object)
{
  guint removed, added;

  g_return_if_fail (GTK_IS_PROPERTY_LOOKUP_LIST_MODEL (self));

  if (object)
    {
      if (self->items->len != 0 &&
          g_ptr_array_index (self->items, 0) == object)
        return;

      removed = gtk_property_lookup_list_model_clear (self, 0);

      g_ptr_array_insert (self->items, 0, g_object_ref (object));
      g_signal_connect_closure_by_id (object,
                                      g_signal_lookup ("notify", G_OBJECT_TYPE (object)),
                                      g_quark_from_static_string (self->property),
                                      g_cclosure_new (G_CALLBACK (gtk_property_lookup_list_model_notify_cb), G_OBJECT (self), NULL),
                                      FALSE);
      added = 1;
      if (!gtk_property_lookup_list_model_is_infinite (self))
        added += gtk_property_lookup_list_model_append (self, G_MAXUINT);
    }
  else
    {
      if (self->items->len == 0 ||
          g_ptr_array_index (self->items, 0) == NULL)
        return;

      removed = gtk_property_lookup_list_model_clear (self, 0);
      added = 0;
    }

  g_assert (removed != 0 || added != 0);

  g_list_model_items_changed (G_LIST_MODEL (self), 0, removed, added);
}

gpointer
gtk_property_lookup_list_model_get_object (GtkPropertyLookupListModel *self)
{
  g_return_val_if_fail (GTK_IS_PROPERTY_LOOKUP_LIST_MODEL (self), NULL);

  if (self->items->len == 0)
    return NULL;
  
  return g_ptr_array_index (self->items, 0);
}