2 * Copyright (C) 2010 Collabora Ltd.
3 * Copyright (C) 2007-2010 Nokia Corporation.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Felix Kaser <felix.kaser@collabora.co.uk>
20 * Xavier Claessens <xavier.claessens@collabora.co.uk>
21 * Claudio Saavedra <csaavedra@igalia.com>
28 #include <gdk/gdkkeysyms.h>
30 #include <libempathy/empathy-utils.h>
32 #include "empathy-live-search.h"
34 G_DEFINE_TYPE (EmpathyLiveSearch, empathy_live_search, GTK_TYPE_HBOX)
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLiveSearch)
40 GtkWidget *search_entry;
41 GtkWidget *hook_widget;
43 gunichar *text_stripped;
44 } EmpathyLiveSearchPriv;
53 static void live_search_hook_widget_destroy_cb (GtkObject *object,
59 * Returns a stripped version of @ch, removing any case, accentuation
60 * mark, or any special mark on it.
63 stripped_char (gunichar ch)
70 utype = g_unichar_type (ch);
74 case G_UNICODE_CONTROL:
75 case G_UNICODE_FORMAT:
76 case G_UNICODE_UNASSIGNED:
77 case G_UNICODE_COMBINING_MARK:
81 ch = g_unichar_tolower (ch);
82 decomp = g_unicode_canonical_decomposition (ch, &dlen);
94 strip_utf8_string (const gchar *string)
100 if (EMP_STR_EMPTY (string))
103 ret = g_malloc (sizeof (gunichar) * (strlen (string) + 1));
106 for (p = string; *p != '\0'; p = g_utf8_next_char (p))
110 sc = stripped_char (g_utf8_get_char (p));
121 live_search_entry_key_pressed_cb (GtkEntry *entry,
125 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
127 /* if esc key pressed, hide the search */
128 if (event->keyval == GDK_Escape)
130 gtk_widget_hide (GTK_WIDGET (self));
138 live_search_text_changed (GtkEntry *entry,
141 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
142 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
145 text = gtk_entry_get_text (entry);
147 if (EMP_STR_EMPTY (text))
148 gtk_widget_hide (GTK_WIDGET (self));
150 gtk_widget_show (GTK_WIDGET (self));
152 g_free (priv->text_stripped);
153 priv->text_stripped = strip_utf8_string (text);
154 g_object_notify (G_OBJECT (self), "text");
158 live_search_close_pressed (GtkEntry *entry,
159 GtkEntryIconPosition icon_pos,
163 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
165 gtk_widget_hide (GTK_WIDGET (self));
169 live_search_key_press_event_cb (GtkWidget *widget,
173 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
174 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
178 /* dont forward this event to the entry, else the event is consumed by the
179 * entry and does not close the window */
180 if (!gtk_widget_get_visible (GTK_WIDGET (self)) &&
181 event->keyval == GDK_Escape)
184 /* do not show the search if CTRL and/or ALT are pressed with a key
185 * this is needed, because otherwise the CTRL + F accel would not work,
186 * because the entry consumes it */
187 if (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK) ||
188 event->keyval == GDK_Control_L ||
189 event->keyval == GDK_Control_R)
192 /* realize the widget if it is not realized yet */
193 gtk_widget_realize (priv->search_entry);
194 if (!gtk_widget_has_focus (priv->search_entry))
196 gtk_widget_grab_focus (priv->search_entry);
197 gtk_editable_set_position (GTK_EDITABLE (priv->search_entry), -1);
200 /* forward the event to the search entry */
201 new_event = gdk_event_copy ((GdkEvent *) event);
202 ret = gtk_widget_event (priv->search_entry, new_event);
203 gdk_event_free (new_event);
209 live_search_release_hook_widget (EmpathyLiveSearch *self)
211 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
213 /* remove old handlers if old source was not null */
214 if (priv->hook_widget != NULL)
216 g_signal_handlers_disconnect_by_func (priv->hook_widget,
217 live_search_key_press_event_cb, self);
218 g_signal_handlers_disconnect_by_func (priv->hook_widget,
219 live_search_hook_widget_destroy_cb, self);
220 g_object_unref (priv->hook_widget);
221 priv->hook_widget = NULL;
226 live_search_hook_widget_destroy_cb (GtkObject *object,
229 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
231 /* unref the hook widget and hide search */
232 live_search_release_hook_widget (self);
233 gtk_widget_hide (GTK_WIDGET (self));
237 live_search_dispose (GObject *obj)
239 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
241 live_search_release_hook_widget (self);
243 if (G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose != NULL)
244 G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose (obj);
248 live_search_finalize (GObject *obj)
250 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
251 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
253 g_free (priv->text_stripped);
255 if (G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize != NULL)
256 G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize (obj);
260 live_search_get_property (GObject *object,
265 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
269 case PROP_HOOK_WIDGET:
270 g_value_set_object (value, empathy_live_search_get_hook_widget (self));
273 g_value_set_string (value, empathy_live_search_get_text (self));
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
282 live_search_set_property (GObject *object,
287 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
290 case PROP_HOOK_WIDGET:
291 empathy_live_search_set_hook_widget (self, g_value_get_object (value));
294 empathy_live_search_set_text (self, g_value_get_string (value));
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
303 live_search_hide (GtkWidget *widget)
305 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
306 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
308 GTK_WIDGET_CLASS (empathy_live_search_parent_class)->hide (widget);
310 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
311 gtk_widget_grab_focus (priv->hook_widget);
315 live_search_show (GtkWidget *widget)
317 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
318 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
320 if (!gtk_widget_has_focus (priv->search_entry))
321 gtk_widget_grab_focus (priv->search_entry);
323 GTK_WIDGET_CLASS (empathy_live_search_parent_class)->show (widget);
327 live_search_grab_focus (GtkWidget *widget)
329 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
330 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
332 if (!gtk_widget_has_focus (priv->search_entry))
333 gtk_widget_grab_focus (priv->search_entry);
337 empathy_live_search_class_init (EmpathyLiveSearchClass *klass)
339 GObjectClass *object_class = (GObjectClass *) klass;
340 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
341 GParamSpec *param_spec;
343 object_class->finalize = live_search_finalize;
344 object_class->dispose = live_search_dispose;
345 object_class->get_property = live_search_get_property;
346 object_class->set_property = live_search_set_property;
348 widget_class->hide = live_search_hide;
349 widget_class->show = live_search_show;
350 widget_class->grab_focus = live_search_grab_focus;
352 param_spec = g_param_spec_object ("hook-widget", "Live Searchs Hook Widget",
353 "The live search catches key-press-events on this widget",
354 GTK_TYPE_WIDGET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
355 g_object_class_install_property (object_class, PROP_HOOK_WIDGET,
358 param_spec = g_param_spec_string ("text", "Live Search Text",
359 "The text of the live search entry",
360 "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
361 g_object_class_install_property (object_class, PROP_TEXT, param_spec);
363 g_type_class_add_private (klass, sizeof (EmpathyLiveSearchPriv));
367 empathy_live_search_init (EmpathyLiveSearch *self)
369 EmpathyLiveSearchPriv *priv =
370 G_TYPE_INSTANCE_GET_PRIVATE ((self), EMPATHY_TYPE_LIVE_SEARCH,
371 EmpathyLiveSearchPriv);
373 gtk_widget_set_no_show_all (GTK_WIDGET (self), TRUE);
375 priv->search_entry = gtk_entry_new ();
376 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
377 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLOSE);
378 gtk_entry_set_icon_activatable (GTK_ENTRY (priv->search_entry),
379 GTK_ENTRY_ICON_SECONDARY, TRUE);
380 gtk_entry_set_icon_sensitive (GTK_ENTRY (priv->search_entry),
381 GTK_ENTRY_ICON_SECONDARY, TRUE);
382 gtk_widget_show (priv->search_entry);
384 gtk_box_pack_start (GTK_BOX (self), priv->search_entry, TRUE, TRUE, 0);
386 g_signal_connect (priv->search_entry, "icon_release",
387 G_CALLBACK (live_search_close_pressed), self);
388 g_signal_connect (priv->search_entry, "changed",
389 G_CALLBACK (live_search_text_changed), self);
390 g_signal_connect (priv->search_entry, "key-press-event",
391 G_CALLBACK (live_search_entry_key_pressed_cb), self);
393 priv->hook_widget = NULL;
399 empathy_live_search_new (GtkWidget *hook)
401 g_return_val_if_fail (hook == NULL || GTK_IS_WIDGET (hook), NULL);
403 return g_object_new (EMPATHY_TYPE_LIVE_SEARCH,
411 empathy_live_search_get_hook_widget (EmpathyLiveSearch *self)
413 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
415 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
417 return priv->hook_widget;
421 empathy_live_search_set_hook_widget (EmpathyLiveSearch *self,
424 EmpathyLiveSearchPriv *priv;
426 g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
427 g_return_if_fail (hook == NULL || GTK_IS_WIDGET (hook));
429 priv = GET_PRIV (self);
431 /* release the actual widget */
432 live_search_release_hook_widget (self);
434 /* connect handlers if new source is not null */
437 priv->hook_widget = g_object_ref (hook);
438 g_signal_connect (priv->hook_widget, "key-press-event",
439 G_CALLBACK (live_search_key_press_event_cb),
441 g_signal_connect (priv->hook_widget, "destroy",
442 G_CALLBACK (live_search_hook_widget_destroy_cb),
448 empathy_live_search_get_text (EmpathyLiveSearch *self)
450 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
452 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
454 return gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
458 empathy_live_search_set_text (EmpathyLiveSearch *self,
461 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
463 g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
464 g_return_if_fail (text != NULL);
466 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), text);
470 live_search_match_string (const gchar *string,
471 const gunichar *prefix)
475 if (prefix == NULL || prefix[0] == 0)
478 if (EMP_STR_EMPTY (string))
481 for (p = string; *p != '\0'; p = g_utf8_next_char (p))
485 /* Search the start of the word (skip non alpha-num chars) */
486 while (*p != '\0' && !g_unichar_isalnum (g_utf8_get_char (p)))
487 p = g_utf8_next_char (p);
489 /* Check if this word match prefix */
494 sc = stripped_char (g_utf8_get_char (p));
497 /* If the char does not match, stop */
501 /* The char matched. If it was the last of prefix, stop */
502 if (prefix[++i] == 0)
506 p = g_utf8_next_char (p);
509 /* This word didn't match, go to next one (skip alpha-num chars) */
510 while (*p != '\0' && g_unichar_isalnum (g_utf8_get_char (p)))
511 p = g_utf8_next_char (p);
521 * empathy_live_search_match:
522 * @self: a #EmpathyLiveSearch
523 * @string: a string where to search, must be valid UTF-8.
525 * Search if one of the words in @string string starts with the current text
528 * Searching for "aba" in "Abasto" will match, searching in "Moraba" will not,
529 * and searching in "A tool (abacus)" will do.
531 * The match is not case-sensitive, and regardless of the accentuation marks.
533 * Returns: %TRUE if a match is found, %FALSE otherwise.
537 empathy_live_search_match (EmpathyLiveSearch *self,
540 EmpathyLiveSearchPriv *priv;
542 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), FALSE);
544 priv = GET_PRIV (self);
546 return live_search_match_string (string, priv->text_stripped);
550 empathy_live_search_match_string (const gchar *string,
556 stripped = strip_utf8_string (prefix);
557 match = live_search_match_string (string, stripped);