1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3
* st-texture-frame.h: Expandible texture actor
5
* Copyright 2007 OpenedHand
6
* Copyright 2009 Intel Corporation.
8
* This program is free software; you can redistribute it and/or modify it
9
* under the terms and conditions of the GNU Lesser General Public License,
10
* version 2.1, as published by the Free Software Foundation.
12
* This program is distributed in the hope it will be useful, but WITHOUT ANY
13
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17
* You should have received a copy of the GNU Lesser General Public License
18
* along with this program; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20
* Boston, MA 02111-1307, USA.
25
* SECTION:st-texture-frame
26
* @short_description: Stretch a texture to fit the entire allocation
36
#include <cogl/cogl.h>
38
#include "st-texture-frame.h"
39
#include "st-private.h"
53
G_DEFINE_TYPE (StTextureFrame, st_texture_frame, CLUTTER_TYPE_ACTOR);
55
#define ST_TEXTURE_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ST_TYPE_TEXTURE_FRAME, StTextureFramePrivate))
57
struct _StTextureFramePrivate
59
ClutterTexture *parent_texture;
68
st_texture_frame_get_preferred_width (ClutterActor *self,
71
gfloat *natural_width_p)
73
StTextureFramePrivate *priv = ST_TEXTURE_FRAME (self)->priv;
75
if (G_UNLIKELY (priv->parent_texture == NULL))
85
ClutterActorClass *klass;
87
/* by directly querying the parent texture's class implementation
88
* we are going around any override mechanism the parent texture
89
* might have in place, and we ask directly for the original
92
klass = CLUTTER_ACTOR_GET_CLASS (priv->parent_texture);
93
klass->get_preferred_width (CLUTTER_ACTOR (priv->parent_texture),
101
st_texture_frame_get_preferred_height (ClutterActor *self,
103
gfloat *min_height_p,
104
gfloat *natural_height_p)
106
StTextureFramePrivate *priv = ST_TEXTURE_FRAME (self)->priv;
108
if (G_UNLIKELY (priv->parent_texture == NULL))
113
if (natural_height_p)
114
*natural_height_p = 0;
118
ClutterActorClass *klass;
120
/* by directly querying the parent texture's class implementation
121
* we are going around any override mechanism the parent texture
122
* might have in place, and we ask directly for the original
125
klass = CLUTTER_ACTOR_GET_CLASS (priv->parent_texture);
126
klass->get_preferred_height (CLUTTER_ACTOR (priv->parent_texture),
134
st_texture_frame_paint (ClutterActor *self)
136
StTextureFramePrivate *priv = ST_TEXTURE_FRAME (self)->priv;
137
CoglHandle cogl_texture = COGL_INVALID_HANDLE;
138
CoglHandle cogl_material = COGL_INVALID_HANDLE;
139
ClutterActorBox box = { 0, };
140
gfloat width, height;
141
gfloat tex_width, tex_height;
143
gfloat tx1, ty1, tx2, ty2;
146
/* no need to paint stuff if we don't have a texture */
147
if (G_UNLIKELY (priv->parent_texture == NULL))
150
/* parent texture may have been hidden, so need to make sure it gets
153
if (!CLUTTER_ACTOR_IS_REALIZED (priv->parent_texture))
154
clutter_actor_realize (CLUTTER_ACTOR (priv->parent_texture));
156
cogl_texture = clutter_texture_get_cogl_texture (priv->parent_texture);
157
if (cogl_texture == COGL_INVALID_HANDLE)
159
cogl_material = clutter_texture_get_cogl_material (priv->parent_texture);
160
if (cogl_material == COGL_INVALID_HANDLE)
163
tex_width = cogl_texture_get_width (cogl_texture);
164
tex_height = cogl_texture_get_height (cogl_texture);
166
clutter_actor_get_allocation_box (self, &box);
167
width = box.x2 - box.x1;
168
height = box.y2 - box.y1;
170
tx1 = priv->left / tex_width;
171
tx2 = (tex_width - priv->right) / tex_width;
172
ty1 = priv->top / tex_height;
173
ty2 = (tex_height - priv->bottom) / tex_height;
175
ex = width - priv->right;
177
ex = priv->right; /* FIXME ? */
179
ey = height - priv->bottom;
181
ey = priv->bottom; /* FIXME ? */
183
opacity = clutter_actor_get_paint_opacity (self);
185
/* Paint using the parent texture's material. It should already have
186
the cogl texture set as the first layer */
187
/* NB: for correct blending we need set a preumultiplied color here: */
188
cogl_material_set_color4ub (cogl_material,
189
opacity, opacity, opacity, opacity);
190
cogl_set_source (cogl_material);
193
GLfloat rectangles[] =
195
/* top left corner */
196
0, 0, priv->left, priv->top,
201
priv->left, 0, ex, priv->top,
206
ex, 0, width, priv->top,
211
0, priv->top, priv->left, ey,
216
priv->left, priv->top, ex, ey,
221
ex, priv->top, width, ey,
226
0, ey, priv->left, height,
231
priv->left, ey, ex, height,
236
ex, ey, width, height,
241
cogl_rectangles_with_texture_coords (rectangles, 9);
246
st_texture_frame_set_frame_internal (StTextureFrame *frame,
252
StTextureFramePrivate *priv = frame->priv;
253
GObject *gobject = G_OBJECT (frame);
254
gboolean changed = FALSE;
256
g_object_freeze_notify (gobject);
258
if (priv->top != top)
261
g_object_notify (gobject, "top");
265
if (priv->right != right)
268
g_object_notify (gobject, "right");
272
if (priv->bottom != bottom)
274
priv->bottom = bottom;
275
g_object_notify (gobject, "bottom");
279
if (priv->left != left)
282
g_object_notify (gobject, "left");
286
if (changed && CLUTTER_ACTOR_IS_VISIBLE (frame))
287
clutter_actor_queue_redraw (CLUTTER_ACTOR (frame));
289
g_object_thaw_notify (gobject);
293
st_texture_frame_set_property (GObject *gobject,
298
StTextureFrame *frame = ST_TEXTURE_FRAME (gobject);
299
StTextureFramePrivate *priv = frame->priv;
303
case PROP_PARENT_TEXTURE:
304
st_texture_frame_set_parent_texture (frame,
305
g_value_get_object (value));
309
st_texture_frame_set_frame_internal (frame,
310
g_value_get_float (value),
317
st_texture_frame_set_frame_internal (frame,
319
g_value_get_float (value),
325
st_texture_frame_set_frame_internal (frame,
328
g_value_get_float (value),
333
st_texture_frame_set_frame_internal (frame,
337
g_value_get_float (value));
341
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
347
st_texture_frame_get_property (GObject *gobject,
352
StTextureFramePrivate *priv = ST_TEXTURE_FRAME (gobject)->priv;
356
case PROP_PARENT_TEXTURE:
357
g_value_set_object (value, priv->parent_texture);
361
g_value_set_float (value, priv->left);
365
g_value_set_float (value, priv->top);
369
g_value_set_float (value, priv->right);
373
g_value_set_float (value, priv->bottom);
377
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
383
st_texture_frame_dispose (GObject *gobject)
385
StTextureFramePrivate *priv = ST_TEXTURE_FRAME (gobject)->priv;
387
if (priv->parent_texture)
389
g_object_unref (priv->parent_texture);
390
priv->parent_texture = NULL;
393
G_OBJECT_CLASS (st_texture_frame_parent_class)->dispose (gobject);
397
st_texture_frame_class_init (StTextureFrameClass *klass)
399
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
400
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
403
g_type_class_add_private (gobject_class, sizeof (StTextureFramePrivate));
405
actor_class->get_preferred_width =
406
st_texture_frame_get_preferred_width;
407
actor_class->get_preferred_height =
408
st_texture_frame_get_preferred_height;
409
actor_class->paint = st_texture_frame_paint;
411
gobject_class->set_property = st_texture_frame_set_property;
412
gobject_class->get_property = st_texture_frame_get_property;
413
gobject_class->dispose = st_texture_frame_dispose;
415
pspec = g_param_spec_object ("parent-texture",
417
"The parent ClutterTexture",
418
CLUTTER_TYPE_TEXTURE,
421
g_object_class_install_property (gobject_class, PROP_PARENT_TEXTURE, pspec);
423
pspec = g_param_spec_float ("left",
429
g_object_class_install_property (gobject_class, PROP_LEFT, pspec);
431
pspec = g_param_spec_float ("top",
437
g_object_class_install_property (gobject_class, PROP_TOP, pspec);
439
pspec = g_param_spec_float ("bottom",
445
g_object_class_install_property (gobject_class, PROP_BOTTOM, pspec);
447
pspec = g_param_spec_float ("right",
453
g_object_class_install_property (gobject_class, PROP_RIGHT, pspec);
457
st_texture_frame_init (StTextureFrame *self)
459
StTextureFramePrivate *priv;
461
self->priv = priv = ST_TEXTURE_FRAME_GET_PRIVATE (self);
465
* st_texture_frame_new:
466
* @texture: a #ClutterTexture or %NULL
467
* @left: left margin preserving its content
468
* @top: top margin preserving its content
469
* @right: right margin preserving its content
470
* @bottom: bottom margin preserving its content
472
* A #StTextureFrame is a specialized texture that efficiently clones
473
* an area of the given @texture while keeping preserving portions of the
476
* A #StTextureFrame can be used to make a rectangular texture fit a
477
* given size without stretching its borders.
479
* Return value: the newly created #StTextureFrame
482
st_texture_frame_new (ClutterTexture *texture,
488
g_return_val_if_fail (texture == NULL || CLUTTER_IS_TEXTURE (texture), NULL);
490
return g_object_new (ST_TYPE_TEXTURE_FRAME,
491
"parent-texture", texture,
500
* st_texture_frame_get_parent_texture:
501
* @frame: A #StTextureFrame
503
* Return the texture used by the #StTextureFrame
505
* Returns: (transfer none): a #ClutterTexture owned by the #StTextureFrame
508
st_texture_frame_get_parent_texture (StTextureFrame *frame)
510
g_return_val_if_fail (ST_IS_TEXTURE_FRAME (frame), NULL);
512
return frame->priv->parent_texture;
516
* st_texture_frame_set_parent_texture:
517
* @frame: A #StTextureFrame
518
* @texture: A #ClutterTexture
520
* Set the #ClutterTexture used by this #StTextureFrame
524
st_texture_frame_set_parent_texture (StTextureFrame *frame,
525
ClutterTexture *texture)
527
StTextureFramePrivate *priv;
528
gboolean was_visible;
530
g_return_if_fail (ST_IS_TEXTURE_FRAME (frame));
531
g_return_if_fail (texture == NULL || CLUTTER_IS_TEXTURE (texture));
535
was_visible = CLUTTER_ACTOR_IS_VISIBLE (frame);
537
if (priv->parent_texture == texture)
540
if (priv->parent_texture)
542
g_object_unref (priv->parent_texture);
543
priv->parent_texture = NULL;
546
clutter_actor_hide (CLUTTER_ACTOR (frame));
551
priv->parent_texture = g_object_ref_sink (texture);
553
if (was_visible && CLUTTER_ACTOR_IS_VISIBLE (priv->parent_texture))
554
clutter_actor_show (CLUTTER_ACTOR (frame));
557
clutter_actor_queue_relayout (CLUTTER_ACTOR (frame));
559
g_object_notify (G_OBJECT (frame), "parent-texture");
563
* st_texture_frame_set_frame:
564
* @frame: A #StTextureFrame
565
* @top: width of the top slice
566
* @right: width of the right slice
567
* @bottom: width of the bottom slice
568
* @left: width of the left slice
570
* Set the slice lines of the specified frame. The slices are calculated as
571
* widths from the edge of the frame.
575
st_texture_frame_set_frame (StTextureFrame *frame,
581
g_return_if_fail (ST_IS_TEXTURE_FRAME (frame));
583
st_texture_frame_set_frame_internal (frame, top, right, bottom, left);
587
* st_texture_frame_get_frame:
588
* @frame: A #StTextureFrame
589
* @top: width of the top slice
590
* @right: width of the right slice
591
* @bottom: width of the bottom slice
592
* @left: width of the left slice
594
* Retrieve the current slice lines from the specified frame.
598
st_texture_frame_get_frame (StTextureFrame *frame,
604
StTextureFramePrivate *priv;
606
g_return_if_fail (ST_IS_TEXTURE_FRAME (frame));
614
*right = priv->right;
617
*bottom = priv->bottom;