Debian Codegraph @

clutter/deprecated/clutter-rectangle.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Authored By Matthew Allum <[email protected]>
7 *
8 * Copyright (C) 2006 OpenedHand
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 *
23 *
24 */
25
26 /**
27 * SECTION:clutter-rectangle
28 * @short_description: An actor that displays a simple rectangle.
29 *
30 * #ClutterRectangle is a #ClutterActor which draws a simple filled rectangle.
31 *
32 * #ClutterRectangle is deprecated since Clutter 1.10. If you want an actor
33 * painting a solid color, you can replace it with #ClutterActor and set the
34 * #ClutterActor:background-color property to the desired #ClutterColor. If
35 * you are drawing more complex shapes, use #ClutterCanvas to draw using the
36 * Cairo 2D API instead.
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #define CLUTTER_DISABLE_DEPRECATION_WARNINGS
44 #include "deprecated/clutter-rectangle.h"
45 #include "deprecated/clutter-actor.h"
46
47 #include "clutter-actor-private.h"
48 #include "clutter-color.h"
49 #include "clutter-debug.h"
50 #include "clutter-main.h"
51 #include "clutter-private.h"
52
53 #include "cogl/cogl.h"
54
55 struct _ClutterRectanglePrivate
56 {
57 ClutterColor color;
58 ClutterColor border_color;
59
60 guint border_width;
61
62 guint has_border : 1;
63 };
64
65 enum
66 {
67 PROP_0,
68
69 PROP_COLOR,
70 PROP_BORDER_COLOR,
71 PROP_BORDER_WIDTH,
72 PROP_HAS_BORDER
73
74 /* FIXME: Add gradient, rounded corner props etc */
75 };
76
77 static const ClutterColor default_color = { 255, 255, 255, 255 };
78 static const ClutterColor default_border_color = { 0, 0, 0, 255 };
79
80 G_DEFINE_TYPE_WITH_PRIVATE (ClutterRectangle, clutter_rectangle, CLUTTER_TYPE_ACTOR)
81
82 static void
83 clutter_rectangle_paint (ClutterActor *self)
84 {
85 ClutterRectanglePrivate *priv = CLUTTER_RECTANGLE (self)->priv;
86 ClutterGeometry geom;
87 guint8 tmp_alpha;
88
89 CLUTTER_NOTE (PAINT,
90 "painting rect '%s'",
91 clutter_actor_get_name (self) ? clutter_actor_get_name (self)
92 : "unknown");
93 clutter_actor_get_allocation_geometry (self, &geom);
94
95 if (priv->has_border)
96 {
97 /* We paint the border and the content only if the rectangle
98 * is big enough to show them
99 */
100 if ((priv->border_width * 2) < geom.width &&
101 (priv->border_width * 2) < geom.height)
102 {
103 /* compute the composited opacity of the actor taking into
104 * account the opacity of the color set by the user
105 */
106 tmp_alpha = clutter_actor_get_paint_opacity (self)
107 * priv->border_color.alpha
108 / 255;
109
110 /* paint the border */
111 cogl_set_source_color4ub (priv->border_color.red,
112 priv->border_color.green,
113 priv->border_color.blue,
114 tmp_alpha);
115
116 /* this sucks, but it's the only way to make a border */
117 cogl_rectangle (priv->border_width, 0,
118 geom.width,
119 priv->border_width);
120
121 cogl_rectangle (geom.width - priv->border_width,
122 priv->border_width,
123 geom.width,
124 geom.height);
125
126 cogl_rectangle (0, geom.height - priv->border_width,
127 geom.width - priv->border_width,
128 geom.height);
129
130 cogl_rectangle (0, 0,
131 priv->border_width,
132 geom.height - priv->border_width);
133
134 tmp_alpha = clutter_actor_get_paint_opacity (self)
135 * priv->color.alpha
136 / 255;
137
138 /* now paint the rectangle */
139 cogl_set_source_color4ub (priv->color.red,
140 priv->color.green,
141 priv->color.blue,
142 tmp_alpha);
143
144 cogl_rectangle (priv->border_width, priv->border_width,
145 geom.width - priv->border_width,
146 geom.height - priv->border_width);
147 }
148 else
149 {
150 /* Otherwise, we draw a rectangle with the same color
151 * as the border, since we can only fit that into the
152 * allocation.
153 */
154 tmp_alpha = clutter_actor_get_paint_opacity (self)
155 * priv->border_color.alpha
156 / 255;
157
158 cogl_set_source_color4ub (priv->border_color.red,
159 priv->border_color.green,
160 priv->border_color.blue,
161 tmp_alpha);
162
163 cogl_rectangle (0, 0, geom.width, geom.height);
164 }
165 }
166 else
167 {
168 /* compute the composited opacity of the actor taking into
169 * account the opacity of the color set by the user
170 */
171 tmp_alpha = clutter_actor_get_paint_opacity (self)
172 * priv->color.alpha
173 / 255;
174
175 cogl_set_source_color4ub (priv->color.red,
176 priv->color.green,
177 priv->color.blue,
178 tmp_alpha);
179
180 cogl_rectangle (0, 0, geom.width, geom.height);
181 }
182 }
183
184 static gboolean
185 clutter_rectangle_get_paint_volume (ClutterActor *self,
186 ClutterPaintVolume *volume)
187 {
188 return _clutter_actor_set_default_paint_volume (self,
189 CLUTTER_TYPE_RECTANGLE,
190 volume);
191 }
192
193 static gboolean
194 clutter_rectangle_has_overlaps (ClutterActor *self)
195 {
196 /* Rectangles never need an offscreen redirect because there are
197 never any overlapping primitives */
198 return FALSE;
199 }
200
201 static void
202 clutter_rectangle_set_property (GObject *object,
203 guint prop_id,
204 const GValue *value,
205 GParamSpec *pspec)
206 {
207 ClutterRectangle *rectangle = CLUTTER_RECTANGLE(object);
208
209 switch (prop_id)
210 {
211 case PROP_COLOR:
212 clutter_rectangle_set_color (rectangle, clutter_value_get_color (value));
213 break;
214 case PROP_BORDER_COLOR:
215 clutter_rectangle_set_border_color (rectangle,
216 clutter_value_get_color (value));
217 break;
218 case PROP_BORDER_WIDTH:
219 clutter_rectangle_set_border_width (rectangle,
220 g_value_get_uint (value));
221 break;
222 case PROP_HAS_BORDER:
223 rectangle->priv->has_border = g_value_get_boolean (value);
224 break;
225 default:
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 break;
228 }
229 }
230
231 static void
232 clutter_rectangle_get_property (GObject *object,
233 guint prop_id,
234 GValue *value,
235 GParamSpec *pspec)
236 {
237 ClutterRectanglePrivate *priv = CLUTTER_RECTANGLE(object)->priv;
238
239 switch (prop_id)
240 {
241 case PROP_COLOR:
242 clutter_value_set_color (value, &priv->color);
243 break;
244 case PROP_BORDER_COLOR:
245 clutter_value_set_color (value, &priv->border_color);
246 break;
247 case PROP_BORDER_WIDTH:
248 g_value_set_uint (value, priv->border_width);
249 break;
250 case PROP_HAS_BORDER:
251 g_value_set_boolean (value, priv->has_border);
252 break;
253 default:
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 break;
256 }
257 }
258
259
260 static void
261 clutter_rectangle_finalize (GObject *object)
262 {
263 G_OBJECT_CLASS (clutter_rectangle_parent_class)->finalize (object);
264 }
265
266 static void
267 clutter_rectangle_dispose (GObject *object)
268 {
269 G_OBJECT_CLASS (clutter_rectangle_parent_class)->dispose (object);
270 }
271
272
273 static void
274 clutter_rectangle_class_init (ClutterRectangleClass *klass)
275 {
276 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
277 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
278 GParamSpec *pspec;
279
280 actor_class->paint = clutter_rectangle_paint;
281 actor_class->get_paint_volume = clutter_rectangle_get_paint_volume;
282 actor_class->has_overlaps = clutter_rectangle_has_overlaps;
283
284 gobject_class->finalize = clutter_rectangle_finalize;
285 gobject_class->dispose = clutter_rectangle_dispose;
286 gobject_class->set_property = clutter_rectangle_set_property;
287 gobject_class->get_property = clutter_rectangle_get_property;
288
289 /**
290 * ClutterRectangle:color:
291 *
292 * The color of the rectangle.
293 */
294 pspec = clutter_param_spec_color ("color",
295 P_("Color"),
296 P_("The color of the rectangle"),
297 &default_color,
298 CLUTTER_PARAM_READWRITE);
299 g_object_class_install_property (gobject_class, PROP_COLOR, pspec);
300
301 /**
302 * ClutterRectangle:border-color:
303 *
304 * The color of the border of the rectangle.
305 *
306 * Since: 0.2
307 */
308 pspec = clutter_param_spec_color ("border-color",
309 P_("Border Color"),
310 P_("The color of the border of the rectangle"),
311 &default_border_color,
312 CLUTTER_PARAM_READWRITE);
313 g_object_class_install_property (gobject_class, PROP_BORDER_COLOR, pspec);
314
315 /**
316 * ClutterRectangle:border-width:
317 *
318 * The width of the border of the rectangle, in pixels.
319 *
320 * Since: 0.2
321 */
322 g_object_class_install_property (gobject_class,
323 PROP_BORDER_WIDTH,
324 g_param_spec_uint ("border-width",
325 P_("Border Width"),
326 P_("The width of the border of the rectangle"),
327 0, G_MAXUINT,
328 0,
329 CLUTTER_PARAM_READWRITE));
330 /**
331 * ClutterRectangle:has-border:
332 *
333 * Whether the #ClutterRectangle should be displayed with a border.
334 *
335 * Since: 0.2
336 */
337 g_object_class_install_property (gobject_class,
338 PROP_HAS_BORDER,
339 g_param_spec_boolean ("has-border",
340 P_("Has Border"),
341 P_("Whether the rectangle should have a border"),
342 FALSE,
343 CLUTTER_PARAM_READWRITE));
344 }
345
346 static void
347 clutter_rectangle_init (ClutterRectangle *self)
348 {
349 ClutterRectanglePrivate *priv;
350
351 self->priv = priv = clutter_rectangle_get_instance_private (self);
352
353 priv->color = default_color;
354 priv->border_color = default_border_color;
355
356 priv->border_width = 0;
357
358 priv->has_border = FALSE;
359 }
360
361 /**
362 * clutter_rectangle_new:
363 *
364 * Creates a new #ClutterActor with a rectangular shape.
365 *
366 * Return value: a new #ClutterRectangle
367 *
368 * Deprecated: 1.10: Use clutter_actor_new() instead
369 */
370 ClutterActor*
371 clutter_rectangle_new (void)
372 {
373 return g_object_new (CLUTTER_TYPE_RECTANGLE, NULL);
374 }
375
376 /**
377 * clutter_rectangle_new_with_color:
378 * @color: a #ClutterColor
379 *
380 * Creates a new #ClutterActor with a rectangular shape
381 * and of the given @color.
382 *
383 * Return value: a new #ClutterRectangle
384 *
385 * Deprecated: 1.10: Use clutter_actor_new() and
386 * clutter_actor_set_background_color() instead
387 */
388 ClutterActor *
389 clutter_rectangle_new_with_color (const ClutterColor *color)
390 {
391 return g_object_new (CLUTTER_TYPE_RECTANGLE,
392 "color", color,
393 NULL);
394 }
395
396 /**
397 * clutter_rectangle_get_color:
398 * @rectangle: a #ClutterRectangle
399 * @color: (out caller-allocates): return location for a #ClutterColor
400 *
401 * Retrieves the color of @rectangle.
402 *
403 * Deprecated: 1.10: Use #ClutterActor and clutter_actor_get_background_color()
404 * instead
405 */
406 void
407 clutter_rectangle_get_color (ClutterRectangle *rectangle,
408 ClutterColor *color)
409 {
410 ClutterRectanglePrivate *priv;
411
412 g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
413 g_return_if_fail (color != NULL);
414
415 priv = rectangle->priv;
416
417 color->red = priv->color.red;
418 color->green = priv->color.green;
419 color->blue = priv->color.blue;
420 color->alpha = priv->color.alpha;
421 }
422
423 /**
424 * clutter_rectangle_set_color:
425 * @rectangle: a #ClutterRectangle
426 * @color: a #ClutterColor
427 *
428 * Sets the color of @rectangle.
429 *
430 * Deprecated: 1.10: Use #ClutterActor and clutter_actor_set_background_color()
431 * instead
432 */
433 void
434 clutter_rectangle_set_color (ClutterRectangle *rectangle,
435 const ClutterColor *color)
436 {
437 ClutterRectanglePrivate *priv;
438
439 g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
440 g_return_if_fail (color != NULL);
441
442 g_object_ref (rectangle);
443
444 priv = rectangle->priv;
445
446 priv->color.red = color->red;
447 priv->color.green = color->green;
448 priv->color.blue = color->blue;
449 priv->color.alpha = color->alpha;
450
451 #if 0
452 /* FIXME - appears to be causing border to always get drawn */
453 if (clutter_color_equal (&priv->color, &priv->border_color))
454 priv->has_border = FALSE;
455 else
456 priv->has_border = TRUE;
457 #endif
458
459 clutter_actor_queue_redraw (CLUTTER_ACTOR (rectangle));
460
461 g_object_notify (G_OBJECT (rectangle), "color");
462 g_object_notify (G_OBJECT (rectangle), "has-border");
463 g_object_unref (rectangle);
464 }
465
466 /**
467 * clutter_rectangle_get_border_width:
468 * @rectangle: a #ClutterRectangle
469 *
470 * Gets the width (in pixels) of the border used by @rectangle
471 *
472 * Return value: the border's width
473 *
474 * Since: 0.2
475 *
476 * Deprecated: 1.10: Use #ClutterActor and a #ClutterCanvas content
477 * to draw the border using Cairo
478 */
479 guint
480 clutter_rectangle_get_border_width (ClutterRectangle *rectangle)
481 {
482 g_return_val_if_fail (CLUTTER_IS_RECTANGLE (rectangle), 0);
483
484 return rectangle->priv->border_width;
485 }
486
487 /**
488 * clutter_rectangle_set_border_width:
489 * @rectangle: a #ClutterRectangle
490 * @width: the width of the border
491 *
492 * Sets the width (in pixel) of the border used by @rectangle.
493 * A @width of 0 will unset the border.
494 *
495 * Since: 0.2
496 *
497 * Deprecated: 1.10: Use #ClutterActor and a #ClutterCanvas content
498 * to draw the border using Cairo
499 */
500 void
501 clutter_rectangle_set_border_width (ClutterRectangle *rectangle,
502 guint width)
503 {
504 ClutterRectanglePrivate *priv;
505
506 g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
507 priv = rectangle->priv;
508
509 if (priv->border_width != width)
510 {
511 g_object_ref (rectangle);
512
513 priv->border_width = width;
514
515 if (priv->border_width != 0)
516 priv->has_border = TRUE;
517 else
518 priv->has_border = FALSE;
519
520 clutter_actor_queue_redraw (CLUTTER_ACTOR (rectangle));
521
522 g_object_notify (G_OBJECT (rectangle), "border-width");
523 g_object_notify (G_OBJECT (rectangle), "has-border");
524 g_object_unref (rectangle);
525 }
526 }
527
528 /**
529 * clutter_rectangle_get_border_color:
530 * @rectangle: a #ClutterRectangle
531 * @color: (out caller-allocates): return location for a #ClutterColor
532 *
533 * Gets the color of the border used by @rectangle and places
534 * it into @color.
535 *
536 * Since: 0.2
537 *
538 * Deprecated: 1.10: Use #ClutterActor and a #ClutterCanvas to draw
539 * the border with Cairo
540 */
541 void
542 clutter_rectangle_get_border_color (ClutterRectangle *rectangle,
543 ClutterColor *color)
544 {
545 ClutterRectanglePrivate *priv;
546
547 g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
548 g_return_if_fail (color != NULL);
549
550 priv = rectangle->priv;
551
552 color->red = priv->border_color.red;
553 color->green = priv->border_color.green;
554 color->blue = priv->border_color.blue;
555 color->alpha = priv->border_color.alpha;
556 }
557
558 /**
559 * clutter_rectangle_set_border_color:
560 * @rectangle: a #ClutterRectangle
561 * @color: the color of the border
562 *
563 * Sets the color of the border used by @rectangle using @color
564 *
565 * Deprecated: 1.10: Use #ClutterActor and a #ClutterCanvas to draw
566 * the border with Cairo
567 */
568 void
569 clutter_rectangle_set_border_color (ClutterRectangle *rectangle,
570 const ClutterColor *color)
571 {
572 ClutterRectanglePrivate *priv;
573
574 g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
575 g_return_if_fail (color != NULL);
576
577 priv = rectangle->priv;
578
579 if (priv->border_color.red != color->red ||
580 priv->border_color.green != color->green ||
581 priv->border_color.blue != color->blue ||
582 priv->border_color.alpha != color->alpha)
583 {
584 g_object_ref (rectangle);
585
586 priv->border_color.red = color->red;
587 priv->border_color.green = color->green;
588 priv->border_color.blue = color->blue;
589 priv->border_color.alpha = color->alpha;
590
591 if (clutter_color_equal (&priv->color, &priv->border_color))
592 priv->has_border = FALSE;
593 else
594 priv->has_border = TRUE;
595
596 clutter_actor_queue_redraw (CLUTTER_ACTOR (rectangle));
597
598 g_object_notify (G_OBJECT (rectangle), "border-color");
599 g_object_notify (G_OBJECT (rectangle), "has-border");
600 g_object_unref (rectangle);
601 }
602 }