1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
4 * Copyright (C) 2010 Collabora Ltd
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
28 #include <libempathy/empathy-utils.h>
29 #include "empathy-cell-renderer-text.h"
31 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererText)
34 TpConnectionPresenceType presence_type;
42 } EmpathyCellRendererTextPriv;
44 static void cell_renderer_text_finalize (GObject *object);
45 static void cell_renderer_text_get_property (GObject *object,
49 static void cell_renderer_text_set_property (GObject *object,
53 static void cell_renderer_text_get_size (GtkCellRenderer *cell,
55 GdkRectangle *cell_area,
60 static void cell_renderer_text_render (GtkCellRenderer *cell,
63 const GdkRectangle *background_area,
64 const GdkRectangle *cell_area,
65 GtkCellRendererState flags);
66 static void cell_renderer_text_update_text (EmpathyCellRendererText *cell,
80 G_DEFINE_TYPE (EmpathyCellRendererText, empathy_cell_renderer_text, GTK_TYPE_CELL_RENDERER_TEXT);
83 empathy_cell_renderer_text_class_init (EmpathyCellRendererTextClass *klass)
85 GObjectClass *object_class;
86 GtkCellRendererClass *cell_class;
89 object_class = G_OBJECT_CLASS (klass);
90 cell_class = GTK_CELL_RENDERER_CLASS (klass);
92 object_class->finalize = cell_renderer_text_finalize;
94 object_class->get_property = cell_renderer_text_get_property;
95 object_class->set_property = cell_renderer_text_set_property;
97 cell_class->get_size = cell_renderer_text_get_size;
98 cell_class->render = cell_renderer_text_render;
100 spec = g_param_spec_string ("name", "Name", "Contact name", NULL,
101 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
102 g_object_class_install_property (object_class, PROP_NAME, spec);
104 spec = g_param_spec_uint ("presence-type", "TpConnectionPresenceType",
105 "The contact's presence type",
106 0, G_MAXUINT, /* Telepathy enum, can be extended */
107 TP_CONNECTION_PRESENCE_TYPE_UNKNOWN,
108 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
109 g_object_class_install_property (object_class, PROP_PRESENCE_TYPE,
112 spec = g_param_spec_string ("status", "Status message",
113 "Contact's custom status message", NULL,
114 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
115 g_object_class_install_property (object_class, PROP_STATUS, spec);
117 spec = g_param_spec_boolean ("is-group", "Is group",
118 "Whether this cell is a group", FALSE,
119 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
120 g_object_class_install_property (object_class, PROP_IS_GROUP, spec);
122 spec = g_param_spec_boolean ("compact", "Compact",
123 "TRUE to show the status alongside the contact name;"
124 "FALSE to show it on its own line",
125 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
126 g_object_class_install_property (object_class, PROP_COMPACT, spec);
128 g_type_class_add_private (object_class, sizeof (EmpathyCellRendererTextPriv));
132 empathy_cell_renderer_text_init (EmpathyCellRendererText *cell)
134 EmpathyCellRendererTextPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
135 EMPATHY_TYPE_CELL_RENDERER_TEXT, EmpathyCellRendererTextPriv);
139 "ellipsize", PANGO_ELLIPSIZE_END,
142 priv->name = g_strdup ("");
143 priv->status = g_strdup ("");
144 priv->compact = FALSE;
148 cell_renderer_text_finalize (GObject *object)
150 EmpathyCellRendererText *cell;
151 EmpathyCellRendererTextPriv *priv;
153 cell = EMPATHY_CELL_RENDERER_TEXT (object);
154 priv = GET_PRIV (cell);
157 g_free (priv->status);
159 (G_OBJECT_CLASS (empathy_cell_renderer_text_parent_class)->finalize) (object);
163 cell_renderer_text_get_property (GObject *object,
168 EmpathyCellRendererText *cell;
169 EmpathyCellRendererTextPriv *priv;
171 cell = EMPATHY_CELL_RENDERER_TEXT (object);
172 priv = GET_PRIV (cell);
176 g_value_set_string (value, priv->name);
178 case PROP_PRESENCE_TYPE:
179 g_value_set_uint (value, priv->presence_type);
182 g_value_set_string (value, priv->status);
185 g_value_set_boolean (value, priv->is_group);
188 g_value_set_boolean (value, priv->compact);
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
197 cell_renderer_text_set_property (GObject *object,
202 EmpathyCellRendererText *cell;
203 EmpathyCellRendererTextPriv *priv;
206 cell = EMPATHY_CELL_RENDERER_TEXT (object);
207 priv = GET_PRIV (cell);
212 str = g_value_get_string (value);
213 priv->name = g_strdup (str ? str : "");
214 g_strdelimit (priv->name, "\n\r\t", ' ');
215 priv->is_valid = FALSE;
217 case PROP_PRESENCE_TYPE:
218 priv->presence_type = g_value_get_uint (value);
219 priv->is_valid = FALSE;
222 g_free (priv->status);
223 str = g_value_get_string (value);
224 priv->status = g_strdup (str ? str : "");
225 g_strdelimit (priv->status, "\n\r\t", ' ');
226 priv->is_valid = FALSE;
229 priv->is_group = g_value_get_boolean (value);
230 priv->is_valid = FALSE;
233 priv->compact = g_value_get_boolean (value);
234 priv->is_valid = FALSE;
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
243 cell_renderer_text_get_size (GtkCellRenderer *cell,
245 GdkRectangle *cell_area,
251 EmpathyCellRendererText *celltext;
252 EmpathyCellRendererTextPriv *priv;
254 celltext = EMPATHY_CELL_RENDERER_TEXT (cell);
255 priv = GET_PRIV (cell);
257 /* Only update if not already valid so we get the right size. */
258 cell_renderer_text_update_text (celltext, widget, priv->is_selected);
260 (GTK_CELL_RENDERER_CLASS (empathy_cell_renderer_text_parent_class)->get_size) (cell, widget,
267 cell_renderer_text_render (GtkCellRenderer *cell,
270 const GdkRectangle *background_area,
271 const GdkRectangle *cell_area,
272 GtkCellRendererState flags)
274 EmpathyCellRendererText *celltext;
276 celltext = EMPATHY_CELL_RENDERER_TEXT (cell);
278 cell_renderer_text_update_text (celltext,
280 (flags & GTK_CELL_RENDERER_SELECTED));
282 (GTK_CELL_RENDERER_CLASS (empathy_cell_renderer_text_parent_class)->render) (
291 cell_renderer_text_update_text (EmpathyCellRendererText *cell,
295 EmpathyCellRendererTextPriv *priv;
296 PangoAttrList *attr_list;
297 PangoAttribute *attr_color, *attr_size;
301 priv = GET_PRIV (cell);
303 if (priv->is_valid && priv->is_selected == selected) {
307 if (priv->is_group) {
310 "weight", PANGO_WEIGHT_BOLD,
317 priv->is_selected = selected;
318 priv->is_valid = TRUE;
322 style = gtk_widget_get_style (widget);
324 attr_list = pango_attr_list_new ();
326 attr_size = pango_attr_size_new (pango_font_description_get_size (style->font_desc) / 1.2);
327 attr_size->start_index = strlen (priv->name) + 1;
328 attr_size->end_index = -1;
329 pango_attr_list_insert (attr_list, attr_size);
334 color = style->text_aa[GTK_STATE_NORMAL];
336 attr_color = pango_attr_foreground_new (color.red, color.green, color.blue);
337 attr_color->start_index = attr_size->start_index;
338 attr_color->end_index = -1;
339 pango_attr_list_insert (attr_list, attr_color);
343 if (EMP_STR_EMPTY (priv->status)) {
344 str = g_strdup (priv->name);
346 str = g_strdup_printf ("%s %s", priv->name, priv->status);
349 const gchar *status = priv->status;
351 if (EMP_STR_EMPTY (priv->status)) {
352 status = empathy_presence_get_default_message (priv->presence_type);
356 str = g_strdup (priv->name);
358 str = g_strdup_printf ("%s\n%s", priv->name, status);
363 "weight", PANGO_WEIGHT_NORMAL,
365 "attributes", attr_list,
371 pango_attr_list_unref (attr_list);
373 priv->is_selected = selected;
374 priv->is_valid = TRUE;
378 empathy_cell_renderer_text_new (void)
380 return g_object_new (EMPATHY_TYPE_CELL_RENDERER_TEXT, NULL);