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 GdkRectangle *background_area,
64 GdkRectangle *cell_area,
65 GdkRectangle *expose_area,
66 GtkCellRendererState flags);
67 static void cell_renderer_text_update_text (EmpathyCellRendererText *cell,
81 G_DEFINE_TYPE (EmpathyCellRendererText, empathy_cell_renderer_text, GTK_TYPE_CELL_RENDERER_TEXT);
84 empathy_cell_renderer_text_class_init (EmpathyCellRendererTextClass *klass)
86 GObjectClass *object_class;
87 GtkCellRendererClass *cell_class;
90 object_class = G_OBJECT_CLASS (klass);
91 cell_class = GTK_CELL_RENDERER_CLASS (klass);
93 object_class->finalize = cell_renderer_text_finalize;
95 object_class->get_property = cell_renderer_text_get_property;
96 object_class->set_property = cell_renderer_text_set_property;
98 cell_class->get_size = cell_renderer_text_get_size;
99 cell_class->render = cell_renderer_text_render;
101 spec = g_param_spec_string ("name", "Name", "Contact name", NULL,
102 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
103 g_object_class_install_property (object_class, PROP_NAME, spec);
105 spec = g_param_spec_uint ("presence-type", "TpConnectionPresenceType",
106 "The contact's presence type",
107 0, G_MAXUINT, /* Telepathy enum, can be extended */
108 TP_CONNECTION_PRESENCE_TYPE_UNKNOWN,
109 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
110 g_object_class_install_property (object_class, PROP_PRESENCE_TYPE,
113 spec = g_param_spec_string ("status", "Status message",
114 "Contact's custom status message", NULL,
115 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
116 g_object_class_install_property (object_class, PROP_STATUS, spec);
118 spec = g_param_spec_boolean ("is-group", "Is group",
119 "Whether this cell is a group", FALSE,
120 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
121 g_object_class_install_property (object_class, PROP_IS_GROUP, spec);
123 spec = g_param_spec_boolean ("compact", "Compact",
124 "TRUE to show the status alongside the contact name;"
125 "FALSE to show it on its own line",
126 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
127 g_object_class_install_property (object_class, PROP_COMPACT, spec);
129 g_type_class_add_private (object_class, sizeof (EmpathyCellRendererTextPriv));
133 empathy_cell_renderer_text_init (EmpathyCellRendererText *cell)
135 EmpathyCellRendererTextPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
136 EMPATHY_TYPE_CELL_RENDERER_TEXT, EmpathyCellRendererTextPriv);
140 "ellipsize", PANGO_ELLIPSIZE_END,
143 priv->name = g_strdup ("");
144 priv->status = g_strdup ("");
145 priv->compact = FALSE;
149 cell_renderer_text_finalize (GObject *object)
151 EmpathyCellRendererText *cell;
152 EmpathyCellRendererTextPriv *priv;
154 cell = EMPATHY_CELL_RENDERER_TEXT (object);
155 priv = GET_PRIV (cell);
158 g_free (priv->status);
160 (G_OBJECT_CLASS (empathy_cell_renderer_text_parent_class)->finalize) (object);
164 cell_renderer_text_get_property (GObject *object,
169 EmpathyCellRendererText *cell;
170 EmpathyCellRendererTextPriv *priv;
172 cell = EMPATHY_CELL_RENDERER_TEXT (object);
173 priv = GET_PRIV (cell);
177 g_value_set_string (value, priv->name);
179 case PROP_PRESENCE_TYPE:
180 g_value_set_uint (value, priv->presence_type);
183 g_value_set_string (value, priv->status);
186 g_value_set_boolean (value, priv->is_group);
189 g_value_set_boolean (value, priv->compact);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
198 cell_renderer_text_set_property (GObject *object,
203 EmpathyCellRendererText *cell;
204 EmpathyCellRendererTextPriv *priv;
207 cell = EMPATHY_CELL_RENDERER_TEXT (object);
208 priv = GET_PRIV (cell);
213 str = g_value_get_string (value);
214 priv->name = g_strdup (str ? str : "");
215 g_strdelimit (priv->name, "\n\r\t", ' ');
216 priv->is_valid = FALSE;
218 case PROP_PRESENCE_TYPE:
219 priv->presence_type = g_value_get_uint (value);
220 priv->is_valid = FALSE;
223 g_free (priv->status);
224 str = g_value_get_string (value);
225 priv->status = g_strdup (str ? str : "");
226 g_strdelimit (priv->status, "\n\r\t", ' ');
227 priv->is_valid = FALSE;
230 priv->is_group = g_value_get_boolean (value);
231 priv->is_valid = FALSE;
234 priv->compact = g_value_get_boolean (value);
235 priv->is_valid = FALSE;
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
244 cell_renderer_text_get_size (GtkCellRenderer *cell,
246 GdkRectangle *cell_area,
252 EmpathyCellRendererText *celltext;
253 EmpathyCellRendererTextPriv *priv;
255 celltext = EMPATHY_CELL_RENDERER_TEXT (cell);
256 priv = GET_PRIV (cell);
258 /* Only update if not already valid so we get the right size. */
259 cell_renderer_text_update_text (celltext, widget, priv->is_selected);
261 (GTK_CELL_RENDERER_CLASS (empathy_cell_renderer_text_parent_class)->get_size) (cell, widget,
268 cell_renderer_text_render (GtkCellRenderer *cell,
271 GdkRectangle *background_area,
272 GdkRectangle *cell_area,
273 GdkRectangle *expose_area,
274 GtkCellRendererState flags)
276 EmpathyCellRendererText *celltext;
278 celltext = EMPATHY_CELL_RENDERER_TEXT (cell);
280 cell_renderer_text_update_text (celltext,
282 (flags & GTK_CELL_RENDERER_SELECTED));
284 (GTK_CELL_RENDERER_CLASS (empathy_cell_renderer_text_parent_class)->render) (
293 cell_renderer_text_update_text (EmpathyCellRendererText *cell,
297 EmpathyCellRendererTextPriv *priv;
298 PangoAttrList *attr_list;
299 PangoAttribute *attr_color, *attr_size;
303 priv = GET_PRIV (cell);
305 if (priv->is_valid && priv->is_selected == selected) {
309 if (priv->is_group) {
312 "weight", PANGO_WEIGHT_BOLD,
319 priv->is_selected = selected;
320 priv->is_valid = TRUE;
324 style = gtk_widget_get_style (widget);
326 attr_list = pango_attr_list_new ();
328 attr_size = pango_attr_size_new (pango_font_description_get_size (style->font_desc) / 1.2);
329 attr_size->start_index = strlen (priv->name) + 1;
330 attr_size->end_index = -1;
331 pango_attr_list_insert (attr_list, attr_size);
336 color = style->text_aa[GTK_STATE_NORMAL];
338 attr_color = pango_attr_foreground_new (color.red, color.green, color.blue);
339 attr_color->start_index = attr_size->start_index;
340 attr_color->end_index = -1;
341 pango_attr_list_insert (attr_list, attr_color);
345 if (EMP_STR_EMPTY (priv->status)) {
346 str = g_strdup (priv->name);
348 str = g_strdup_printf ("%s %s", priv->name, priv->status);
351 const gchar *status = priv->status;
353 if (EMP_STR_EMPTY (priv->status)) {
354 status = empathy_presence_get_default_message (priv->presence_type);
357 str = g_strdup_printf ("%s\n%s", priv->name, status);
362 "weight", PANGO_WEIGHT_NORMAL,
364 "attributes", attr_list,
370 pango_attr_list_unref (attr_list);
372 priv->is_selected = selected;
373 priv->is_valid = TRUE;
377 empathy_cell_renderer_text_new (void)
379 return g_object_new (EMPATHY_TYPE_CELL_RENDERER_TEXT, NULL);