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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2001-2007 Philip Withnall <philip@tecnocode.co.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* The Totem project hereby grant permission for non-gpl compatible GStreamer
* plugins to be used and distributed together with GStreamer and Totem. This
* permission are above and beyond the permissions granted by the GPL license
* Totem is covered by.
*
* Monday 7th February 2005: Christian Schaller: Add exception clause.
* See license_change file for details.
*
*/
#include "config.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <string.h>
#include "totem.h"
#include "totem-cell-renderer-video.h"
#include "totem-private.h"
struct _TotemCellRendererVideoPrivate {
gboolean dispose_has_run;
gchar *title;
GdkPixbuf *thumbnail;
PangoAlignment alignment;
gboolean use_placeholder;
};
#define TOTEM_CELL_RENDERER_VIDEO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TOTEM_TYPE_CELL_RENDERER_VIDEO, TotemCellRendererVideoPrivate))
enum {
PROP_THUMBNAIL = 1,
PROP_TITLE,
PROP_ALIGNMENT,
PROP_USE_PLACEHOLDER
};
static void totem_cell_renderer_video_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
static void totem_cell_renderer_video_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
static void totem_cell_renderer_video_dispose (GObject *object);
static void totem_cell_renderer_video_get_size (GtkCellRenderer *cell, GtkWidget *widget, GdkRectangle *cell_area, gint *x_offset, gint *y_offset, gint *width, gint *height);
static void totem_cell_renderer_video_render (GtkCellRenderer *cell, GdkDrawable *window, GtkWidget *widget, GdkRectangle *background_area, GdkRectangle *cell_area, GdkRectangle *expose_area, GtkCellRendererState flags);
G_DEFINE_TYPE (TotemCellRendererVideo, totem_cell_renderer_video, GTK_TYPE_CELL_RENDERER)
/**
* totem_cell_renderer_video_new:
* @use_placeholder: if %TRUE, use a placeholder thumbnail
*
* Creates a new #TotemCellRendererVideo with its :use-placeholder
* property set to @use_placeholder. If @use_placeholder is %TRUE,
* the renderer will display a generic placeholder thumbnail if a
* proper one is not available.
*
* Return value: a new #TotemCellRendererVideo
**/
TotemCellRendererVideo *
totem_cell_renderer_video_new (gboolean use_placeholder)
{
return g_object_new (TOTEM_TYPE_CELL_RENDERER_VIDEO,
"use-placeholder", use_placeholder,
NULL);
}
static void
totem_cell_renderer_video_class_init (TotemCellRendererVideoClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkCellRendererClass *renderer_class = GTK_CELL_RENDERER_CLASS (klass);
g_type_class_add_private (klass, sizeof (TotemCellRendererVideoPrivate));
object_class->set_property = totem_cell_renderer_video_set_property;
object_class->get_property = totem_cell_renderer_video_get_property;
object_class->dispose = totem_cell_renderer_video_dispose;
renderer_class->get_size = totem_cell_renderer_video_get_size;
renderer_class->render = totem_cell_renderer_video_render;
g_object_class_install_property (object_class, PROP_THUMBNAIL,
g_param_spec_object ("thumbnail", NULL, NULL,
GDK_TYPE_PIXBUF, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_TITLE,
g_param_spec_string ("title", NULL, NULL,
_("Unknown video"), G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_ALIGNMENT,
g_param_spec_enum ("alignment", NULL, NULL,
PANGO_TYPE_ALIGNMENT,
PANGO_ALIGN_CENTER,
G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_USE_PLACEHOLDER,
g_param_spec_boolean ("use-placeholder", NULL, NULL,
FALSE, G_PARAM_READWRITE));
}
static void
totem_cell_renderer_video_init (TotemCellRendererVideo *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, TOTEM_TYPE_CELL_RENDERER_VIDEO, TotemCellRendererVideoPrivate);
self->priv->dispose_has_run = FALSE;
self->priv->title = NULL;
self->priv->thumbnail = NULL;
self->priv->alignment = PANGO_ALIGN_CENTER;
self->priv->use_placeholder = FALSE;
/* Make sure we're in the right mode */
g_object_set ((gpointer) self, "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
}
static void
totem_cell_renderer_video_dispose (GObject *object)
{
TotemCellRendererVideo *self = TOTEM_CELL_RENDERER_VIDEO (object);
/* Make sure we only run once */
if (self->priv->dispose_has_run)
return;
self->priv->dispose_has_run = TRUE;
g_free (self->priv->title);
if (self->priv->thumbnail != NULL)
g_object_unref (self->priv->thumbnail);
/* Chain up to the parent class */
G_OBJECT_CLASS (totem_cell_renderer_video_parent_class)->dispose (object);
}
static void
totem_cell_renderer_video_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
TotemCellRendererVideoPrivate *priv = TOTEM_CELL_RENDERER_VIDEO_GET_PRIVATE (object);
switch (property_id)
{
case PROP_THUMBNAIL:
if (priv->thumbnail != NULL)
g_object_unref (priv->thumbnail);
priv->thumbnail = (GdkPixbuf*) g_value_dup_object (value);
break;
case PROP_TITLE:
g_free (priv->title);
priv->title = g_strdup (g_value_get_string (value));
break;
case PROP_ALIGNMENT:
priv->alignment = g_value_get_enum (value);
break;
case PROP_USE_PLACEHOLDER:
priv->use_placeholder = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
totem_cell_renderer_video_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
TotemCellRendererVideoPrivate *priv = TOTEM_CELL_RENDERER_VIDEO_GET_PRIVATE (object);
switch (property_id)
{
case PROP_THUMBNAIL:
g_value_set_object (value, G_OBJECT (priv->thumbnail));
break;
case PROP_TITLE:
g_value_set_string (value, priv->title);
break;
case PROP_ALIGNMENT:
g_value_set_enum (value, priv->alignment);
break;
case PROP_USE_PLACEHOLDER:
g_value_set_boolean (value, priv->use_placeholder);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *cell_area,
GdkRectangle *draw_area,
GdkRectangle *title_area,
GdkRectangle *thumbnail_area)
{
TotemCellRendererVideoPrivate *priv = TOTEM_CELL_RENDERER_VIDEO_GET_PRIVATE (cell);
guint pixbuf_width = 0;
guint pixbuf_height = 0;
guint title_width, title_height;
guint calc_width, calc_height;
PangoContext *context;
PangoFontMetrics *metrics;
PangoFontDescription *font_desc;
/* Calculate thumbnail dimensions */
if (priv->thumbnail != NULL) {
pixbuf_width = gdk_pixbuf_get_width (priv->thumbnail);
pixbuf_height = gdk_pixbuf_get_height (priv->thumbnail);
} else if (priv->use_placeholder == TRUE) {
gint width, height; /* Sort out signedness weirdness (damn GTK+) */
if (gtk_icon_size_lookup (GTK_ICON_SIZE_DIALOG, &width, &height) == TRUE) {
pixbuf_width = width;
pixbuf_height = height;
} else {
pixbuf_width = cell->width;
pixbuf_height = cell->width;
}
}
/* Calculate title dimensions */
font_desc = pango_font_description_copy_static (widget->style->font_desc);
if (priv->thumbnail != NULL)
pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
context = gtk_widget_get_pango_context (widget);
metrics = pango_context_get_metrics (context,
font_desc,
pango_context_get_language (context));
if (cell_area)
title_width = cell_area->width;
else
title_width = cell->width;
title_height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
pango_font_metrics_get_descent (metrics));
pango_font_metrics_unref (metrics);
pango_font_description_free (font_desc);
/* Calculate the total final size */
calc_width = cell->xpad * 2 + MAX (pixbuf_width, title_width);
calc_height = cell->ypad * 3 + pixbuf_height + title_height;
if (draw_area) {
if (cell_area && calc_width > 0 && calc_height > 0) {
draw_area->x = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
(1.0 - cell->xalign) : cell->xalign) *
(cell_area->width - calc_width));
draw_area->x = MAX (draw_area->x, 0);
draw_area->y = (cell->yalign * (cell_area->height - calc_height));
draw_area->y = MAX (draw_area->y, 0);
} else {
draw_area->x = 0;
draw_area->y = 0;
}
draw_area->width = calc_width;
draw_area->height = calc_height;
/*if (cell_area) {
g_message ("Cell area: X: %i, Y: %i, W: %i, H: %i", cell_area->x, cell_area->y, cell_area->width, cell_area->height);
g_message ("X-align: %f, Y-align: %f", cell->xalign, cell->yalign);
}
g_message ("Draw area: X: %i, Y: %i, W: %i, H: %i", draw_area->x, draw_area->y, draw_area->width, draw_area->height);*/
if (title_area) {
if (cell_area) {
title_area->width = cell_area->width;
title_area->x = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ? 1.0 : 0.0;
} else {
title_area->width = calc_width;
title_area->x = draw_area->x;
}
title_area->height = title_height;
if (pixbuf_height > 0)
title_area->y = draw_area->y + pixbuf_height + cell->ypad;
else
title_area->y = draw_area->y;
/*g_message ("Title area: X: %i, Y: %i, W: %i, H: %i", title_area->x, title_area->y, title_area->width, title_area->height);*/
}
if (pixbuf_height > 0 && thumbnail_area) {
thumbnail_area->x = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
(1.0 - cell->xalign) : cell->xalign) *
(cell_area->width - pixbuf_width));
thumbnail_area->x = MAX (thumbnail_area->x, 0);
thumbnail_area->y = draw_area->y;
thumbnail_area->width = cell->xpad * 2 + pixbuf_width;
thumbnail_area->height = pixbuf_height;
/*g_message ("Thumbnail area: X: %i, Y: %i, W: %i, H: %i", thumbnail_area->x, thumbnail_area->y, thumbnail_area->width, thumbnail_area->height);*/
}
/*g_message ("---");*/
}
}
static void
totem_cell_renderer_video_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *cell_area,
gint *x_offset,
gint *y_offset,
gint *width,
gint *height)
{
GdkRectangle draw_area;
get_size (cell, widget, cell_area, &draw_area, NULL, NULL);
if (x_offset)
*x_offset = draw_area.x;
if (y_offset)
*y_offset = draw_area.y;
if (width)
*width = draw_area.width;
if (height)
*height = draw_area.height;
}
static void
totem_cell_renderer_video_render (GtkCellRenderer *cell,
GdkDrawable *window,
GtkWidget *widget,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GdkRectangle *expose_area,
GtkCellRendererState flags)
{
TotemCellRendererVideoPrivate *priv = TOTEM_CELL_RENDERER_VIDEO_GET_PRIVATE (cell);
GdkPixbuf *pixbuf;
GdkRectangle draw_rect;
GdkRectangle draw_area;
GdkRectangle title_area;
GdkRectangle thumbnail_area;
cairo_t *cr;
PangoLayout *layout;
PangoFontDescription *desc;
GtkStateType state;
get_size (cell, widget, cell_area, &draw_area, &title_area, &thumbnail_area);
draw_area.x += cell_area->x + cell->xpad;
draw_area.y += cell_area->y + cell->ypad;
draw_area.width -= cell->xpad * 2;
draw_area.height -= cell->ypad * 2;
if (!gdk_rectangle_intersect (cell_area, &draw_area, &draw_rect) ||
!gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
return;
/* Sort out the thumbnail */
if (priv->thumbnail != NULL)
pixbuf = priv->thumbnail;
else if (priv->use_placeholder == TRUE)
pixbuf = gtk_widget_render_icon (widget,
GTK_STOCK_MISSING_IMAGE,
GTK_ICON_SIZE_DIALOG,
NULL);
else
pixbuf = NULL;
if (cell->is_expander)
return;
/* Sort out the title */
if (!cell->sensitive) {
state = GTK_STATE_INSENSITIVE;
} else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED) {
if (GTK_WIDGET_HAS_FOCUS (widget))
state = GTK_STATE_SELECTED;
else
state = GTK_STATE_ACTIVE;
} else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT) {
state = GTK_STATE_PRELIGHT;
} else {
if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE)
state = GTK_STATE_INSENSITIVE;
else
state = GTK_STATE_NORMAL;
}
/* Draw the title */
layout = gtk_widget_create_pango_layout (widget, priv->title);
desc = pango_font_description_copy_static (widget->style->font_desc);
if (pixbuf != NULL)
pango_font_description_set_weight (desc, PANGO_WEIGHT_BOLD);
pango_layout_set_font_description (layout, desc);
pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
pango_layout_set_width (layout, title_area.width * PANGO_SCALE);
pango_layout_set_alignment (layout, priv->alignment);
gtk_paint_layout (widget->style,
window,
state,
TRUE,
expose_area,
widget,
"cellrenderervideotitle",
cell_area->x + title_area.x + cell->xpad,
cell_area->y + title_area.y + cell->ypad,
layout);
pango_font_description_free (desc);
g_object_unref (layout);
/* Draw the thumbnail */
if (pixbuf != NULL) {
cr = gdk_cairo_create (window);
gdk_cairo_set_source_pixbuf (cr, pixbuf, cell_area->x + thumbnail_area.x + cell->xpad, cell_area->y + thumbnail_area.y + cell->ypad);
gdk_cairo_rectangle (cr, &draw_rect);
cairo_fill (cr);
cairo_destroy (cr);
}
}
|