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"
33 #include "empathy-gtk-marshal.h"
35 G_DEFINE_TYPE (EmpathyLiveSearch, empathy_live_search, GTK_TYPE_HBOX)
37 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLiveSearch)
41 GtkWidget *search_entry;
42 GtkWidget *hook_widget;
44 GPtrArray *stripped_words;
45 } EmpathyLiveSearchPriv;
61 static guint signals[LAST_SIGNAL];
63 static void live_search_hook_widget_destroy_cb (GtkObject *object,
69 * Returns a stripped version of @ch, removing any case, accentuation
70 * mark, or any special mark on it.
73 stripped_char (gunichar ch)
80 utype = g_unichar_type (ch);
84 case G_UNICODE_CONTROL:
85 case G_UNICODE_FORMAT:
86 case G_UNICODE_UNASSIGNED:
87 case G_UNICODE_NON_SPACING_MARK:
88 case G_UNICODE_COMBINING_MARK:
89 case G_UNICODE_ENCLOSING_MARK:
93 ch = g_unichar_tolower (ch);
94 decomp = g_unicode_canonical_decomposition (ch, &dlen);
106 append_word (GPtrArray **word_array,
111 if (*word_array == NULL)
112 *word_array = g_ptr_array_new_with_free_func (g_free);
113 g_ptr_array_add (*word_array, g_string_free (*word, FALSE));
119 strip_utf8_string (const gchar *string)
121 GPtrArray *word_array = NULL;
122 GString *word = NULL;
125 if (EMP_STR_EMPTY (string))
128 for (p = string; *p != '\0'; p = g_utf8_next_char (p))
132 /* Make the char lower-case, remove its accentuation marks, and ignore it
133 * if it is just unicode marks */
134 sc = stripped_char (g_utf8_get_char (p));
138 /* If it is not alpha-num, it is separator between words */
139 if (!g_unichar_isalnum (sc))
141 append_word (&word_array, &word);
145 /* It is alpha-num, append this char to current word, or start new word */
147 word = g_string_new (NULL);
148 g_string_append_unichar (word, sc);
151 append_word (&word_array, &word);
157 live_search_entry_key_pressed_cb (GtkEntry *entry,
161 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
164 /* if esc key pressed, hide the search */
165 if (event->keyval == GDK_Escape)
167 gtk_widget_hide (GTK_WIDGET (self));
171 /* emit key navigation signal, so other widgets can respond to it properly */
172 if (event->keyval == GDK_Up || event->keyval == GDK_Down
173 || event->keyval == GDK_Left || event->keyval == GDK_Right)
175 g_signal_emit (self, signals[KEYNAV], 0, event, &ret);
183 live_search_text_changed (GtkEntry *entry,
186 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
187 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
190 text = gtk_entry_get_text (entry);
192 if (EMP_STR_EMPTY (text))
193 gtk_widget_hide (GTK_WIDGET (self));
195 gtk_widget_show (GTK_WIDGET (self));
197 if (priv->stripped_words != NULL)
198 g_ptr_array_unref (priv->stripped_words);
200 priv->stripped_words = strip_utf8_string (text);
202 g_object_notify (G_OBJECT (self), "text");
206 live_search_close_pressed (GtkEntry *entry,
207 GtkEntryIconPosition icon_pos,
211 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
213 gtk_widget_hide (GTK_WIDGET (self));
217 live_search_key_press_event_cb (GtkWidget *widget,
221 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
222 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
226 /* dont forward this event to the entry, else the event is consumed by the
227 * entry and does not close the window */
228 if (!gtk_widget_get_visible (GTK_WIDGET (self)) &&
229 event->keyval == GDK_Escape)
232 /* do not show the search if CTRL and/or ALT are pressed with a key
233 * this is needed, because otherwise the CTRL + F accel would not work,
234 * because the entry consumes it */
235 if (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK) ||
236 event->keyval == GDK_Control_L ||
237 event->keyval == GDK_Control_R)
240 /* dont forward the up and down arrow keys to the entry, they are needed for
241 * navigation in the treeview and are not needed in the search entry */
242 if (event->keyval == GDK_Up || event->keyval == GDK_Down)
245 /* realize the widget if it is not realized yet */
246 gtk_widget_realize (priv->search_entry);
247 if (!gtk_widget_has_focus (priv->search_entry))
249 gtk_widget_grab_focus (priv->search_entry);
250 gtk_editable_set_position (GTK_EDITABLE (priv->search_entry), -1);
253 /* forward the event to the search entry */
254 new_event = gdk_event_copy ((GdkEvent *) event);
255 ret = gtk_widget_event (priv->search_entry, new_event);
256 gdk_event_free (new_event);
262 live_search_entry_activate_cb (GtkEntry *entry,
263 EmpathyLiveSearch *self)
265 g_signal_emit (self, signals[ACTIVATE], 0);
269 live_search_release_hook_widget (EmpathyLiveSearch *self)
271 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
273 /* remove old handlers if old source was not null */
274 if (priv->hook_widget != NULL)
276 g_signal_handlers_disconnect_by_func (priv->hook_widget,
277 live_search_key_press_event_cb, self);
278 g_signal_handlers_disconnect_by_func (priv->hook_widget,
279 live_search_hook_widget_destroy_cb, self);
280 g_object_unref (priv->hook_widget);
281 priv->hook_widget = NULL;
286 live_search_hook_widget_destroy_cb (GtkObject *object,
289 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
291 /* unref the hook widget and hide search */
292 gtk_widget_hide (GTK_WIDGET (self));
293 live_search_release_hook_widget (self);
297 live_search_dispose (GObject *obj)
299 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
301 live_search_release_hook_widget (self);
303 if (G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose != NULL)
304 G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose (obj);
308 live_search_finalize (GObject *obj)
310 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
311 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
313 if (priv->stripped_words != NULL)
314 g_ptr_array_unref (priv->stripped_words);
316 if (G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize != NULL)
317 G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize (obj);
321 live_search_get_property (GObject *object,
326 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
330 case PROP_HOOK_WIDGET:
331 g_value_set_object (value, empathy_live_search_get_hook_widget (self));
334 g_value_set_string (value, empathy_live_search_get_text (self));
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
343 live_search_set_property (GObject *object,
348 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
351 case PROP_HOOK_WIDGET:
352 empathy_live_search_set_hook_widget (self, g_value_get_object (value));
355 empathy_live_search_set_text (self, g_value_get_string (value));
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
364 live_search_hide (GtkWidget *widget)
366 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
367 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
369 GTK_WIDGET_CLASS (empathy_live_search_parent_class)->hide (widget);
371 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
372 gtk_widget_grab_focus (priv->hook_widget);
376 live_search_show (GtkWidget *widget)
378 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
379 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
381 if (!gtk_widget_has_focus (priv->search_entry))
382 gtk_widget_grab_focus (priv->search_entry);
384 GTK_WIDGET_CLASS (empathy_live_search_parent_class)->show (widget);
388 live_search_grab_focus (GtkWidget *widget)
390 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
391 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
393 if (!gtk_widget_has_focus (priv->search_entry))
395 gtk_widget_grab_focus (priv->search_entry);
396 gtk_editable_set_position (GTK_EDITABLE (priv->search_entry), -1);
401 empathy_live_search_class_init (EmpathyLiveSearchClass *klass)
403 GObjectClass *object_class = (GObjectClass *) klass;
404 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
405 GParamSpec *param_spec;
407 object_class->finalize = live_search_finalize;
408 object_class->dispose = live_search_dispose;
409 object_class->get_property = live_search_get_property;
410 object_class->set_property = live_search_set_property;
412 widget_class->hide = live_search_hide;
413 widget_class->show = live_search_show;
414 widget_class->grab_focus = live_search_grab_focus;
416 signals[ACTIVATE] = g_signal_new ("activate",
417 G_TYPE_FROM_CLASS (object_class),
421 g_cclosure_marshal_VOID__VOID,
424 signals[KEYNAV] = g_signal_new ("key-navigation",
425 G_TYPE_FROM_CLASS (object_class),
428 g_signal_accumulator_true_handled, NULL,
429 _empathy_gtk_marshal_BOOLEAN__BOXED,
430 G_TYPE_BOOLEAN, 1, GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
432 param_spec = g_param_spec_object ("hook-widget", "Live Searchs Hook Widget",
433 "The live search catches key-press-events on this widget",
434 GTK_TYPE_WIDGET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
435 g_object_class_install_property (object_class, PROP_HOOK_WIDGET,
438 param_spec = g_param_spec_string ("text", "Live Search Text",
439 "The text of the live search entry",
440 "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
441 g_object_class_install_property (object_class, PROP_TEXT, param_spec);
443 g_type_class_add_private (klass, sizeof (EmpathyLiveSearchPriv));
447 empathy_live_search_init (EmpathyLiveSearch *self)
449 EmpathyLiveSearchPriv *priv =
450 G_TYPE_INSTANCE_GET_PRIVATE ((self), EMPATHY_TYPE_LIVE_SEARCH,
451 EmpathyLiveSearchPriv);
453 gtk_widget_set_no_show_all (GTK_WIDGET (self), TRUE);
455 priv->search_entry = gtk_entry_new ();
456 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
457 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLOSE);
458 gtk_entry_set_icon_activatable (GTK_ENTRY (priv->search_entry),
459 GTK_ENTRY_ICON_SECONDARY, TRUE);
460 gtk_entry_set_icon_sensitive (GTK_ENTRY (priv->search_entry),
461 GTK_ENTRY_ICON_SECONDARY, TRUE);
462 gtk_widget_show (priv->search_entry);
464 gtk_box_pack_start (GTK_BOX (self), priv->search_entry, TRUE, TRUE, 0);
466 g_signal_connect (priv->search_entry, "icon_release",
467 G_CALLBACK (live_search_close_pressed), self);
468 g_signal_connect (priv->search_entry, "changed",
469 G_CALLBACK (live_search_text_changed), self);
470 g_signal_connect (priv->search_entry, "key-press-event",
471 G_CALLBACK (live_search_entry_key_pressed_cb), self);
472 g_signal_connect (priv->search_entry, "activate",
473 G_CALLBACK (live_search_entry_activate_cb), self);
475 priv->hook_widget = NULL;
481 empathy_live_search_new (GtkWidget *hook)
483 g_return_val_if_fail (hook == NULL || GTK_IS_WIDGET (hook), NULL);
485 return g_object_new (EMPATHY_TYPE_LIVE_SEARCH,
493 empathy_live_search_get_hook_widget (EmpathyLiveSearch *self)
495 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
497 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
499 return priv->hook_widget;
503 empathy_live_search_set_hook_widget (EmpathyLiveSearch *self,
506 EmpathyLiveSearchPriv *priv;
508 g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
509 g_return_if_fail (hook == NULL || GTK_IS_WIDGET (hook));
511 priv = GET_PRIV (self);
513 /* release the actual widget */
514 live_search_release_hook_widget (self);
516 /* connect handlers if new source is not null */
519 priv->hook_widget = g_object_ref (hook);
520 g_signal_connect (priv->hook_widget, "key-press-event",
521 G_CALLBACK (live_search_key_press_event_cb),
523 g_signal_connect (priv->hook_widget, "destroy",
524 G_CALLBACK (live_search_hook_widget_destroy_cb),
530 empathy_live_search_get_text (EmpathyLiveSearch *self)
532 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
534 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
536 return gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
540 empathy_live_search_set_text (EmpathyLiveSearch *self,
543 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
545 g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
546 g_return_if_fail (text != NULL);
548 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), text);
552 live_search_match_prefix (const gchar *string,
556 const gchar *prefix_p;
557 gboolean next_word = FALSE;
559 if (prefix == NULL || prefix[0] == 0)
562 if (EMP_STR_EMPTY (string))
566 for (p = string; *p != '\0'; p = g_utf8_next_char (p))
570 /* Make the char lower-case, remove its accentuation marks, and ignore it
571 * if it is just unicode marks */
572 sc = stripped_char (g_utf8_get_char (p));
576 /* If we want to go to next word, ignore alpha-num chars */
577 if (next_word && g_unichar_isalnum (sc))
581 /* Ignore word separators */
582 if (!g_unichar_isalnum (sc))
585 /* If this char does not match prefix_p, go to next word and start again
586 * from the beginning of prefix */
587 if (sc != g_utf8_get_char (prefix_p))
594 /* prefix_p match, verify to next char. If this was the last of prefix,
595 * it means it completely machted and we are done. */
596 prefix_p = g_utf8_next_char (prefix_p);
597 if (*prefix_p == '\0')
605 live_search_match_words (const gchar *string,
613 for (i = 0; i < words->len; i++)
614 if (!live_search_match_prefix (string, g_ptr_array_index (words, i)))
621 * empathy_live_search_match:
622 * @self: a #EmpathyLiveSearch
623 * @string: a string where to search, must be valid UTF-8.
625 * Search if one of the words in @string string starts with the current text
628 * Searching for "aba" in "Abasto" will match, searching in "Moraba" will not,
629 * and searching in "A tool (abacus)" will do.
631 * The match is not case-sensitive, and regardless of the accentuation marks.
633 * Returns: %TRUE if a match is found, %FALSE otherwise.
637 empathy_live_search_match (EmpathyLiveSearch *self,
640 EmpathyLiveSearchPriv *priv;
642 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), FALSE);
644 priv = GET_PRIV (self);
646 return live_search_match_words (string, priv->stripped_words);
650 empathy_live_search_match_string (const gchar *string,
656 words = strip_utf8_string (prefix);
657 match = live_search_match_words (string, words);
659 g_ptr_array_unref (words);