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
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
* A container that respects the aspect ratio of its child
*
* Copyright 2010, 2011 Intel Corporation.
* Copyright 2012, Red Hat, Inc.
*
* Based upon mx-aspect-frame.c
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
* Boston, MA 02111-1307, USA.
*/
#include <math.h>
#include "totem-aspect-frame.h"
G_DEFINE_TYPE (TotemAspectFrame, totem_aspect_frame, CLUTTER_TYPE_ACTOR)
#define ASPECT_FRAME_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), \
TOTEM_TYPE_ASPECT_FRAME, \
TotemAspectFramePrivate))
enum
{
PROP_0,
PROP_EXPAND,
};
struct _TotemAspectFramePrivate
{
guint expand : 1;
gdouble rotation;
};
static void
totem_aspect_frame_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
TotemAspectFrame *frame = TOTEM_ASPECT_FRAME (object);
switch (property_id)
{
case PROP_EXPAND:
g_value_set_boolean (value, totem_aspect_frame_get_expand (frame));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
totem_aspect_frame_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
switch (property_id)
{
case PROP_EXPAND:
totem_aspect_frame_set_expand (TOTEM_ASPECT_FRAME (object),
g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
totem_aspect_frame_dispose (GObject *object)
{
G_OBJECT_CLASS (totem_aspect_frame_parent_class)->dispose (object);
}
static void
totem_aspect_frame_finalize (GObject *object)
{
G_OBJECT_CLASS (totem_aspect_frame_parent_class)->finalize (object);
}
static void
totem_aspect_frame_get_preferred_width (ClutterActor *actor,
gfloat for_height,
gfloat *min_width_p,
gfloat *nat_width_p)
{
gboolean override;
if (for_height >= 0)
override = FALSE;
else
g_object_get (G_OBJECT (actor), "natural-height-set", &override, NULL);
if (override)
g_object_get (G_OBJECT (actor), "natural-height", &for_height, NULL);
CLUTTER_ACTOR_CLASS (totem_aspect_frame_parent_class)->
get_preferred_width (actor, for_height, min_width_p, nat_width_p);
}
static void
totem_aspect_frame_get_preferred_height (ClutterActor *actor,
gfloat for_width,
gfloat *min_height_p,
gfloat *nat_height_p)
{
gboolean override;
if (for_width >= 0)
override = FALSE;
else
g_object_get (G_OBJECT (actor), "natural-width-set", &override, NULL);
if (override)
g_object_get (G_OBJECT (actor), "natural-width", &for_width, NULL);
CLUTTER_ACTOR_CLASS (totem_aspect_frame_parent_class)->
get_preferred_height (actor, for_width, min_height_p, nat_height_p);
}
static void
totem_aspect_frame_set_rotation_internal (TotemAspectFrame *frame,
gdouble rotation,
gboolean animate)
{
ClutterActor *actor;
gfloat w, h, center_x, center_y;
gdouble scale;
g_return_if_fail (TOTEM_IS_ASPECT_FRAME (frame));
g_object_get (G_OBJECT (frame),
"width", &w,
"height", &h,
NULL);
/* So that the larger side fits the smaller side
* FIXME: check the angle instead */
if (rotation == 90.0 || rotation == 270.0)
{
if (w > h)
scale = h / (double) w;
else
scale = w / (double) h;
}
else
{
scale = 1.0;
}
center_x = w * 0.5f;
center_y = h * 0.5f;
actor = CLUTTER_ACTOR (frame);
if (animate)
{
clutter_actor_save_easing_state (actor);
clutter_actor_set_easing_duration (actor, 500);
}
/* FIXME: When animated, make sure that we go in the right direction,
* otherwise we'll spin in the wrong direction going back to 0 from 270 */
clutter_actor_set_rotation (actor, CLUTTER_Z_AXIS, rotation, center_x, center_y, 0);
clutter_actor_set_scale_full (actor, scale, scale, center_x, center_y);
if (animate)
clutter_actor_restore_easing_state (actor);
}
static void
totem_aspect_frame_allocate (ClutterActor *actor,
const ClutterActorBox *box,
ClutterAllocationFlags flags)
{
ClutterActor *child;
ClutterActorBox child_box;
gfloat aspect, child_aspect, width, height, box_width, box_height;
gdouble rotation;
TotemAspectFramePrivate *priv = TOTEM_ASPECT_FRAME (actor)->priv;
CLUTTER_ACTOR_CLASS (totem_aspect_frame_parent_class)->
allocate (actor, box, flags);
child = clutter_actor_get_child_at_index (actor, 0);
if (!child)
return;
box_width = box->x2 - box->x1;
box_height = box->y2 - box->y1;
clutter_actor_get_preferred_size (child, NULL, NULL, &width, &height);
if (width <= 0.0f || height <= 0.0f)
return;
aspect = box_width / box_height;
child_aspect = width / height;
if ((aspect < child_aspect) ^ priv->expand)
{
width = box_width;
height = box_width / child_aspect;
}
else
{
height = box_height;
width = box_height * child_aspect;
}
child_box.x1 = (box_width - width) / 2;
child_box.y1 = (box_height - height) / 2;
child_box.x2 = child_box.x1 + width;
child_box.y2 = child_box.y1 + height;
clutter_actor_allocate (child, &child_box, flags);
clutter_actor_save_easing_state (child);
clutter_actor_set_easing_duration (child, 0);
/* FIXME: We should swap height and width if the actor is on its side */
rotation = totem_aspect_frame_get_rotation (TOTEM_ASPECT_FRAME (actor));
totem_aspect_frame_set_rotation_internal (TOTEM_ASPECT_FRAME (actor),
rotation,
FALSE);
}
static void
totem_aspect_frame_paint (ClutterActor *actor)
{
ClutterActor *child;
TotemAspectFramePrivate *priv = TOTEM_ASPECT_FRAME (actor)->priv;
child = clutter_actor_get_child_at_index (actor, 0);
if (!child)
return;
if (priv->expand)
{
gfloat width, height;
clutter_actor_get_size (actor, &width, &height);
cogl_clip_push_rectangle (0.0, 0.0, width, height);
clutter_actor_paint (child);
cogl_clip_pop ();
}
else
clutter_actor_paint (child);
}
static void
totem_aspect_frame_pick (ClutterActor *actor,
const ClutterColor *color)
{
ClutterActorBox box;
ClutterActor *child;
TotemAspectFramePrivate *priv = TOTEM_ASPECT_FRAME (actor)->priv;
clutter_actor_get_allocation_box (actor, &box);
cogl_set_source_color4ub (color->red, color->green,
color->blue, color->alpha);
cogl_rectangle (box.x1, box.y1, box.x2, box.y2);
child = clutter_actor_get_child_at_index (actor, 0);
if (!child)
return;
if (priv->expand)
{
cogl_clip_push_rectangle (0.0, 0.0, box.x2 - box.x1, box.y2 - box.y1);
clutter_actor_paint (child);
cogl_clip_pop ();
}
else
clutter_actor_paint (child);
}
static void
totem_aspect_frame_class_init (TotemAspectFrameClass *klass)
{
GParamSpec *pspec;
GObjectClass *object_class = G_OBJECT_CLASS (klass);
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
g_type_class_add_private (klass, sizeof (TotemAspectFramePrivate));
object_class->get_property = totem_aspect_frame_get_property;
object_class->set_property = totem_aspect_frame_set_property;
object_class->dispose = totem_aspect_frame_dispose;
object_class->finalize = totem_aspect_frame_finalize;
actor_class->get_preferred_width = totem_aspect_frame_get_preferred_width;
actor_class->get_preferred_height = totem_aspect_frame_get_preferred_height;
actor_class->allocate = totem_aspect_frame_allocate;
actor_class->paint = totem_aspect_frame_paint;
actor_class->pick = totem_aspect_frame_pick;
pspec = g_param_spec_boolean ("expand",
"Expand",
"Fill the allocated area with the child and "
"clip off the excess.",
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_EXPAND, pspec);
}
static void
totem_aspect_frame_init (TotemAspectFrame *self)
{
self->priv = ASPECT_FRAME_PRIVATE (self);
}
ClutterActor *
totem_aspect_frame_new (void)
{
return g_object_new (TOTEM_TYPE_ASPECT_FRAME, NULL);
}
void
totem_aspect_frame_set_expand (TotemAspectFrame *frame, gboolean expand)
{
TotemAspectFramePrivate *priv;
g_return_if_fail (TOTEM_IS_ASPECT_FRAME (frame));
priv = frame->priv;
if (priv->expand != expand)
{
ClutterActor *child;
priv->expand = expand;
child = clutter_actor_get_child_at_index (CLUTTER_ACTOR (frame), 0);
if (child)
{
/* Duration will be reset in _allocate() */
clutter_actor_save_easing_state (child);
clutter_actor_set_easing_duration (child, 500);
clutter_actor_queue_relayout (CLUTTER_ACTOR (frame));
}
g_object_notify (G_OBJECT (frame), "expand");
}
}
gboolean
totem_aspect_frame_get_expand (TotemAspectFrame *frame)
{
g_return_val_if_fail (TOTEM_IS_ASPECT_FRAME (frame), FALSE);
return frame->priv->expand;
}
void
totem_aspect_frame_set_child (TotemAspectFrame *frame,
ClutterActor *child)
{
g_return_if_fail (TOTEM_IS_ASPECT_FRAME (frame));
clutter_actor_add_child (CLUTTER_ACTOR (frame), child);
}
void
totem_aspect_frame_set_rotation (TotemAspectFrame *frame,
gdouble rotation)
{
g_return_if_fail (TOTEM_IS_ASPECT_FRAME (frame));
rotation = fmod (rotation, 360.0);
g_debug ("Setting rotation to '%lf'", rotation);
frame->priv->rotation = rotation;
totem_aspect_frame_set_rotation_internal (frame, rotation, TRUE);
}
gdouble
totem_aspect_frame_get_rotation (TotemAspectFrame *frame)
{
gdouble rotation;
g_return_val_if_fail (TOTEM_IS_ASPECT_FRAME (frame), 0.0);
rotation = fmod (frame->priv->rotation, 360.0);
g_debug ("Got rotation %lf", rotation);
return rotation;
}
|