summaryrefslogtreecommitdiff
path: root/src/cheese-grid.c
blob: 97938da305b539f5b854da268f8eb160c1043293 (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
/* 
 * Copyright (C) 2009 Filippo Argiolas
 *
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include "cheese-grid.h"

#define ROWS 3
#define COLS 3
#define N_ACTORS ROWS*COLS

typedef struct _CheeseGridActorData CheeseGridActorData;

static void cheese_grid_dispose             (GObject *object);
static void cheese_grid_finalize            (GObject *object);

static void cheese_grid_finalize            (GObject *object);

static void clutter_container_iface_init  (ClutterContainerIface *iface);

static void cheese_grid_real_add            (ClutterContainer *container,
                                             ClutterActor     *actor);
static void cheese_grid_real_remove         (ClutterContainer *container,
                                             ClutterActor     *actor);
static void cheese_grid_real_foreach        (ClutterContainer *container,
                                             ClutterCallback   callback,
                                             gpointer          user_data);
static void cheese_grid_real_raise          (ClutterContainer *container,
                                             ClutterActor     *actor,
                                             ClutterActor     *sibling);
static void cheese_grid_real_lower          (ClutterContainer *container,
                                             ClutterActor     *actor,
                                             ClutterActor     *sibling);
static void
cheese_grid_real_sort_depth_order (ClutterContainer *container);

static void
cheese_grid_free_actor_data (gpointer data);

static void cheese_grid_paint (ClutterActor *actor);

static void cheese_grid_pick (ClutterActor *actor,
                              const ClutterColor *color);

static void
cheese_grid_get_preferred_width (ClutterActor *self,
                                 ClutterUnit for_height,
                                 ClutterUnit *min_width_p,
                                 ClutterUnit *natural_width_p);

static void
cheese_grid_get_preferred_height (ClutterActor *self,
                                  ClutterUnit for_width,
                                  ClutterUnit *min_height_p,
                                  ClutterUnit *natural_height_p);

static void cheese_grid_allocate (ClutterActor *self,
                                  const ClutterActorBox *box,
                                  gboolean absolute_origin_changed);

G_DEFINE_TYPE_WITH_CODE (CheeseGrid, cheese_grid,
                         CLUTTER_TYPE_ACTOR,
                         G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_CONTAINER,
                                                clutter_container_iface_init));

#define CHEESE_GRID_GET_PRIVATE(obj)                            \
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHEESE_TYPE_GRID,        \
                                CheeseGridPrivate))

struct _CheeseGridPrivate
{
  ClutterUnit for_height,  for_width;
  ClutterUnit pref_width,  pref_height;
  ClutterUnit alloc_width, alloc_height;

  gboolean    absolute_origin_changed;
  GHashTable *hash_table;
  GList      *list;

  ClutterUnit max_extent_a;
  ClutterUnit max_extent_b;
};

enum
{
  PROP_0,
};

struct _CheeseGridActorData
{
  gboolean    xpos_set,   ypos_set;
  ClutterUnit xpos,       ypos;
  ClutterUnit pref_width, pref_height;
};

static void
cheese_grid_class_init (CheeseGridClass *klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  ClutterActorClass *actor_class = (ClutterActorClass *) klass;

  gobject_class->dispose = cheese_grid_dispose;
  gobject_class->finalize = cheese_grid_finalize;

  actor_class->paint                = cheese_grid_paint;
  actor_class->pick                 = cheese_grid_pick;
  actor_class->get_preferred_width  = cheese_grid_get_preferred_width;
  actor_class->get_preferred_height = cheese_grid_get_preferred_height;
  actor_class->allocate             = cheese_grid_allocate;

  g_type_class_add_private (klass, sizeof (CheeseGridPrivate));
}


static void
clutter_container_iface_init (ClutterContainerIface *iface)
{
  iface->add              = cheese_grid_real_add;
  iface->remove           = cheese_grid_real_remove;
  iface->foreach          = cheese_grid_real_foreach;
  iface->raise            = cheese_grid_real_raise;
  iface->lower            = cheese_grid_real_lower;
  iface->sort_depth_order = cheese_grid_real_sort_depth_order;
}

static void
cheese_grid_init (CheeseGrid *self)
{
  CheeseGridPrivate *priv;

  self->priv = priv = CHEESE_GRID_GET_PRIVATE (self);

  /* do not unref in the hashtable, the reference is for now kept by the list
   * (double bookkeeping sucks)
   */
  priv->hash_table
    = g_hash_table_new_full (g_direct_hash,
                             g_direct_equal,
                             NULL,
                             cheese_grid_free_actor_data);
}

static void
cheese_grid_dispose (GObject *object)
{
  CheeseGrid *self = (CheeseGrid *) object;
  CheeseGridPrivate *priv;
 
  priv = self->priv;

  /* Destroy all of the children. This will cause them to be removed
     from the container and unparented */
  clutter_container_foreach (CLUTTER_CONTAINER (object),
                             (ClutterCallback) clutter_actor_destroy,
                             NULL);

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

static void
cheese_grid_finalize (GObject *object)
{
  CheeseGrid *self = (CheeseGrid *) object;
  CheeseGridPrivate *priv = self->priv;
  
  g_hash_table_destroy (priv->hash_table);

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

static void
cheese_grid_free_actor_data (gpointer data)
{
  g_slice_free (CheeseGridActorData, data);
}

ClutterActor *
cheese_grid_new (void)
{
  ClutterActor *self = g_object_new (CHEESE_TYPE_GRID, NULL);

  return self;
}

static void
cheese_grid_real_add (ClutterContainer *container,
                      ClutterActor     *actor)
{
  CheeseGridPrivate *priv;
  CheeseGridActorData *data;

  g_return_if_fail (CHEESE_IS_GRID (container));

  priv = CHEESE_GRID (container)->priv;

  g_object_ref (actor);

  clutter_actor_set_parent (actor, CLUTTER_ACTOR (container));

  data = g_slice_alloc0 (sizeof (CheeseGridActorData));

  priv->list = g_list_append (priv->list, actor);
  g_hash_table_insert (priv->hash_table, actor, data);

  g_signal_emit_by_name (container, "actor-added", actor);

  clutter_actor_queue_relayout (CLUTTER_ACTOR (container));

  g_object_unref (actor);
}

static void
cheese_grid_real_remove (ClutterContainer *container,
                         ClutterActor     *actor)
{
  CheeseGrid *layout = CHEESE_GRID (container);
  CheeseGridPrivate *priv = layout->priv;

  g_object_ref (actor);
  
  if (g_hash_table_remove (priv->hash_table, actor))
  {
    clutter_actor_unparent (actor);

    clutter_actor_queue_relayout (CLUTTER_ACTOR (layout));

    g_signal_emit_by_name (container, "actor-removed", actor);

    if (CLUTTER_ACTOR_IS_VISIBLE (CLUTTER_ACTOR (layout)))
      clutter_actor_queue_redraw (CLUTTER_ACTOR (layout));
  }
  priv->list = g_list_remove (priv->list, actor);

  g_object_unref (actor);
}

static void
cheese_grid_real_foreach (ClutterContainer *container,
                          ClutterCallback callback,
                          gpointer user_data)
{
  CheeseGrid *layout = CHEESE_GRID (container);
  CheeseGridPrivate *priv = layout->priv;

  g_list_foreach (priv->list, (GFunc) callback, user_data);
}

static void
cheese_grid_real_raise (ClutterContainer *container,
                        ClutterActor *actor,
                        ClutterActor *sibling)
{
  /* STUB */
}

static void
cheese_grid_real_lower (ClutterContainer *container,
                        ClutterActor *actor,
                        ClutterActor *sibling)
{
  /* STUB */
}

static void
cheese_grid_real_sort_depth_order (ClutterContainer *container)
{
  /* STUB */
}

static void
cheese_grid_paint (ClutterActor *actor)
{
  CheeseGrid *layout = (CheeseGrid *) actor;
  CheeseGridPrivate *priv = layout->priv;
  GList *child_item;

  for (child_item = priv->list;
       child_item != NULL;
       child_item = child_item->next)
  {
    ClutterActor *child = child_item->data;

    g_assert (child != NULL);

    if (CLUTTER_ACTOR_IS_VISIBLE (child))
      clutter_actor_paint (child);
  }

}

static void
cheese_grid_pick (ClutterActor *actor,
                  const ClutterColor *color)
{
  /* Chain up so we get a bounding box pained (if we are reactive) */
  CLUTTER_ACTOR_CLASS (cheese_grid_parent_class)->pick (actor, color);

  /* Just forward to the paint call which in turn will trigger
   * the child actors also getting 'picked'.
   */
  if (CLUTTER_ACTOR_IS_VISIBLE (actor))
    cheese_grid_paint (actor);
}

static void
cheese_grid_get_preferred_width (ClutterActor *self,
                                 ClutterUnit for_height,
                                 ClutterUnit *min_width_p,
                                 ClutterUnit *natural_width_p)
{
  CheeseGrid *layout = (CheeseGrid *) self;
  CheeseGridPrivate *priv = layout->priv;
  ClutterUnit natural_width;
  ClutterActor *parent = clutter_actor_get_parent (self);

//  natural_width = clutter_actor_get_width (parent);
  natural_width = CLUTTER_UNITS_FROM_INT (600);
  
  if (min_width_p)
    *min_width_p = natural_width;
  if (natural_width_p)
    *natural_width_p = natural_width;

  priv->pref_width = natural_width;
}

static void
cheese_grid_get_preferred_height (ClutterActor *self,
                                  ClutterUnit for_width,
                                  ClutterUnit *min_height_p,
                                  ClutterUnit *natural_height_p)
{
  CheeseGrid *layout = (CheeseGrid *) self;
  CheeseGridPrivate *priv = layout->priv;
  ClutterUnit natural_height;
  ClutterActor *parent = clutter_actor_get_parent (self);

//  natural_height = clutter_actor_get_height (parent);
  natural_height = CLUTTER_UNITS_FROM_INT (200);

  priv->for_width = for_width;
  priv->pref_height = natural_height;

  if (min_height_p)
    *min_height_p = natural_height;
  if (natural_height_p)
    *natural_height_p = natural_height;
}

ClutterActor *
cheese_grid_get_actor_for_sink (CheeseGrid *self,
                                GstElement *element)
{
  CheeseGridPrivate *priv = CHEESE_GRID_GET_PRIVATE (self);
  GList *iter;

  for (iter = priv->list; iter; iter = iter->next)
  {
    GObject *child = G_OBJECT (iter->data);
    GstElement *sink;
    g_object_get (child, "sink", &sink, NULL);
    if (sink == element) {
//      g_print ("found actor for: %s\n", GST_ELEMENT_NAME (element));
      return CLUTTER_ACTOR (child);
    }
  }
  return NULL;
}

static void
cheese_grid_allocate (ClutterActor          *self,
                      const ClutterActorBox *box,
                      gboolean               absolute_origin_changed)
{
  CheeseGrid *layout = (CheeseGrid *) self;
  CheeseGridPrivate *priv = layout->priv;

  ClutterUnit current_a;
  ClutterUnit current_b;
  ClutterUnit next_b;
  ClutterUnit agap;
  ClutterUnit bgap;

  gboolean homogenous_a;
  gboolean homogenous_b;
  gdouble  aalign;
  gdouble  balign;

  current_a = current_b = next_b = 0;

  GList *iter;

  /* chain up to set actor->allocation */
  CLUTTER_ACTOR_CLASS (cheese_grid_parent_class)
    ->allocate (self, box, absolute_origin_changed);

  priv->alloc_width = box->x2 - box->x1;
  priv->alloc_height = box->y2 - box->y1;
  priv->absolute_origin_changed = absolute_origin_changed;

  /* Make sure we have calculated the preferred size */
  /* what does this do? */
  clutter_actor_get_preferred_size (self, NULL, NULL, NULL, NULL);

  priv->max_extent_a = 0;
  priv->max_extent_b = 0;

  gint i = 0;
  gint j = 0;

  for (iter = priv->list; iter; iter = iter->next)
  {
    ClutterActor *child = iter->data;
    ClutterUnit natural_width;
    ClutterUnit natural_height;
    

    /* each child will get as much space as they require */
    clutter_actor_get_preferred_size (CLUTTER_ACTOR (child),
                                      NULL, NULL,
                                      &natural_width, &natural_height);
    if (natural_width > priv->max_extent_a)
      priv->max_extent_a = natural_width;
    if (natural_height > priv->max_extent_b)
      priv->max_extent_b = natural_width;

    ClutterUnit ratio = natural_width/natural_height;
    ClutterUnit alloc_ratio = priv->alloc_width/priv->alloc_height;

    ClutterUnit child_width = priv->alloc_width / 3.0;
    ClutterUnit child_height = priv->alloc_height / 3.0;
    ClutterActorBox child_box;

    if (alloc_ratio < ratio) {
      child_box.x1 = child_width * i;
      child_box.x2 = child_box.x1 + child_width;
    
      child_box.y1 = child_height * j + (child_height - child_width/ratio)/2.0;
      child_box.y2 = child_box.y1 + (child_width/ratio);
    }
    else {
      child_box.x1 = child_width * i + (child_width - child_height*ratio)/2.0;
      child_box.x2 = child_box.x1 + child_height*ratio;
    
      child_box.y1 = child_height * j;
      child_box.y2 = child_box.y1 + child_height;
    }
    
    /* update the allocation */
    clutter_actor_allocate (CLUTTER_ACTOR (child),
                            &child_box,
                            absolute_origin_changed);
    i++;
    if (i == 3) {
      i = 0;
      j++;
    }
  }
}