2
* Copyright (c) 2010 Intel, Inc.
4
* The Control Center is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by the
6
* Free Software Foundation; either version 2 of the License, or (at your
7
* option) any later version.
9
* The Control Center is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with the Control Center; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Author: Thomas Wood <thos@gnome.org>
21
#include "shell-search-renderer.h"
24
G_DEFINE_TYPE (ShellSearchRenderer, shell_search_renderer, GTK_TYPE_CELL_RENDERER)
26
#define SEARCH_RENDERER_PRIVATE(o) \
27
(G_TYPE_INSTANCE_GET_PRIVATE ((o), SHELL_TYPE_SEARCH_RENDERER, ShellSearchRendererPrivate))
29
struct _ShellSearchRendererPrivate
47
shell_search_renderer_get_property (GObject *object,
55
case PROP_SEARCH_TARGET:
56
case PROP_SEARCH_STRING:
61
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
66
shell_search_renderer_set_property (GObject *object,
71
ShellSearchRendererPrivate *priv = SHELL_SEARCH_RENDERER (object)->priv;
77
priv->title = g_value_dup_string (value);
80
case PROP_SEARCH_TARGET:
81
g_free (priv->search_target);
82
priv->search_target = g_value_dup_string (value);
85
case PROP_SEARCH_STRING:
86
g_free (priv->search_string);
87
priv->search_string = g_value_dup_string (value);
91
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
96
shell_search_renderer_dispose (GObject *object)
98
ShellSearchRendererPrivate *priv = SHELL_SEARCH_RENDERER (object)->priv;
102
g_object_unref (priv->layout);
106
G_OBJECT_CLASS (shell_search_renderer_parent_class)->dispose (object);
110
shell_search_renderer_finalize (GObject *object)
112
ShellSearchRendererPrivate *priv = SHELL_SEARCH_RENDERER (object)->priv;
116
g_free (priv->title);
120
if (priv->search_target)
122
g_free (priv->search_target);
123
priv->search_target = NULL;
126
if (priv->search_string)
128
g_free (priv->search_string);
129
priv->search_string = NULL;
132
G_OBJECT_CLASS (shell_search_renderer_parent_class)->finalize (object);
136
shell_search_renderer_set_layout (ShellSearchRenderer *cell, GtkWidget *widget)
138
gchar *display_string;
139
ShellSearchRendererPrivate *priv = cell->priv;
140
gchar *needle, *haystack;
145
priv->layout = pango_layout_new (gtk_widget_get_pango_context (widget));
146
pango_layout_set_ellipsize (priv->layout, PANGO_ELLIPSIZE_END);
149
full_string = priv->search_target;
151
if (priv->search_string != NULL)
152
needle = g_utf8_casefold (priv->search_string, -1);
155
haystack = g_utf8_casefold (full_string, -1);
157
/* clear any previous attributes */
158
pango_layout_set_attributes (priv->layout, NULL);
160
if (priv->search_string && priv->title
161
&& (strstr (haystack, needle)))
164
gchar *lead, *trail, *leaddot;
170
count = strlen (needle);
171
start = full_string + (strstr (haystack, needle) - haystack);
173
lead = MAX (start - CONTEXT, full_string);
174
trail = start + count;
176
if (lead == full_string)
181
match = g_strndup (start, count);
182
lead = g_strndup (lead, start - lead);
184
display_string = g_markup_printf_escaped ("%s\n"
185
"<small>%s%s<b>%s</b>%s</small>",
186
priv->title, leaddot, lead,
193
display_string = g_markup_escape_text (priv->title, -1);
196
pango_layout_set_markup (priv->layout, display_string, -1);
197
g_free (display_string);
203
shell_search_renderer_get_size (GtkCellRenderer *cell,
205
const GdkRectangle *cell_area,
211
ShellSearchRendererPrivate *priv = SHELL_SEARCH_RENDERER (cell)->priv;
214
shell_search_renderer_set_layout (SHELL_SEARCH_RENDERER (cell), widget);
216
pango_layout_set_width (priv->layout, PANGO_SCALE * 180);
217
pango_layout_get_pixel_extents (priv->layout, NULL, &rect);
219
if (width) *width = rect.width;
220
if (height) *height = rect.height;
222
if (x_offset) *x_offset = 0;
223
if (y_offset) *y_offset = 0;
228
shell_search_renderer_render (GtkCellRenderer *cell,
231
const GdkRectangle *background_area,
232
const GdkRectangle *cell_area,
233
GtkCellRendererState flags)
235
ShellSearchRendererPrivate *priv = SHELL_SEARCH_RENDERER (cell)->priv;
238
shell_search_renderer_set_layout (SHELL_SEARCH_RENDERER (cell), widget);
240
pango_layout_get_pixel_extents (priv->layout, NULL, &rect);
242
cairo_move_to (cr, cell_area->x, cell_area->y);
244
/* FIXME: get the correct colour from the theme */
245
cairo_set_source_rgb (cr, 0, 0, 0);
247
pango_cairo_layout_path (cr, priv->layout);
252
shell_search_renderer_class_init (ShellSearchRendererClass *klass)
255
GObjectClass *object_class = G_OBJECT_CLASS (klass);
256
GtkCellRendererClass *cell_renderer = GTK_CELL_RENDERER_CLASS (klass);
258
g_type_class_add_private (klass, sizeof (ShellSearchRendererPrivate));
260
object_class->get_property = shell_search_renderer_get_property;
261
object_class->set_property = shell_search_renderer_set_property;
262
object_class->dispose = shell_search_renderer_dispose;
263
object_class->finalize = shell_search_renderer_finalize;
265
cell_renderer->get_size = shell_search_renderer_get_size;
266
cell_renderer->render = shell_search_renderer_render;
268
pspec = g_param_spec_string ("title",
272
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
273
g_object_class_install_property (object_class, PROP_TITLE, pspec);
275
pspec = g_param_spec_string ("search-target",
277
"The string that will be searched",
279
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
280
g_object_class_install_property (object_class, PROP_SEARCH_TARGET, pspec);
282
pspec = g_param_spec_string ("search-string",
284
"Current search string",
286
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
287
g_object_class_install_property (object_class, PROP_SEARCH_STRING, pspec);
291
shell_search_renderer_init (ShellSearchRenderer *self)
293
self->priv = SEARCH_RENDERER_PRIVATE (self);
296
ShellSearchRenderer *
297
shell_search_renderer_new (void)
299
return g_object_new (SHELL_TYPE_SEARCH_RENDERER, NULL);