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 GPtrArray *stripped_words;
44 } EmpathyLiveSearchPriv;
60 static guint signals[LAST_SIGNAL];
62 static void live_search_hook_widget_destroy_cb (GtkWidget *object,
68 * Returns a stripped version of @ch, removing any case, accentuation
69 * mark, or any special mark on it.
72 stripped_char (gunichar ch)
79 utype = g_unichar_type (ch);
83 case G_UNICODE_CONTROL:
84 case G_UNICODE_FORMAT:
85 case G_UNICODE_UNASSIGNED:
86 case G_UNICODE_NON_SPACING_MARK:
87 case G_UNICODE_COMBINING_MARK:
88 case G_UNICODE_ENCLOSING_MARK:
91 case G_UNICODE_PRIVATE_USE:
92 case G_UNICODE_SURROGATE:
93 case G_UNICODE_LOWERCASE_LETTER:
94 case G_UNICODE_MODIFIER_LETTER:
95 case G_UNICODE_OTHER_LETTER:
96 case G_UNICODE_TITLECASE_LETTER:
97 case G_UNICODE_UPPERCASE_LETTER:
98 case G_UNICODE_DECIMAL_NUMBER:
99 case G_UNICODE_LETTER_NUMBER:
100 case G_UNICODE_OTHER_NUMBER:
101 case G_UNICODE_CONNECT_PUNCTUATION:
102 case G_UNICODE_DASH_PUNCTUATION:
103 case G_UNICODE_CLOSE_PUNCTUATION:
104 case G_UNICODE_FINAL_PUNCTUATION:
105 case G_UNICODE_INITIAL_PUNCTUATION:
106 case G_UNICODE_OTHER_PUNCTUATION:
107 case G_UNICODE_OPEN_PUNCTUATION:
108 case G_UNICODE_CURRENCY_SYMBOL:
109 case G_UNICODE_MODIFIER_SYMBOL:
110 case G_UNICODE_MATH_SYMBOL:
111 case G_UNICODE_OTHER_SYMBOL:
112 case G_UNICODE_LINE_SEPARATOR:
113 case G_UNICODE_PARAGRAPH_SEPARATOR:
114 case G_UNICODE_SPACE_SEPARATOR:
116 ch = g_unichar_tolower (ch);
117 decomp = g_unicode_canonical_decomposition (ch, &dlen);
129 append_word (GPtrArray **word_array,
134 if (*word_array == NULL)
135 *word_array = g_ptr_array_new_with_free_func (g_free);
136 g_ptr_array_add (*word_array, g_string_free (*word, FALSE));
142 empathy_live_search_strip_utf8_string (const gchar *string)
144 GPtrArray *word_array = NULL;
145 GString *word = NULL;
148 if (EMP_STR_EMPTY (string))
151 for (p = string; *p != '\0'; p = g_utf8_next_char (p))
155 /* Make the char lower-case, remove its accentuation marks, and ignore it
156 * if it is just unicode marks */
157 sc = stripped_char (g_utf8_get_char (p));
161 /* If it is not alpha-num, it is separator between words */
162 if (!g_unichar_isalnum (sc))
164 append_word (&word_array, &word);
168 /* It is alpha-num, append this char to current word, or start new word */
170 word = g_string_new (NULL);
171 g_string_append_unichar (word, sc);
174 append_word (&word_array, &word);
180 live_search_match_prefix (const gchar *string,
184 const gchar *prefix_p;
185 gboolean next_word = FALSE;
187 if (prefix == NULL || prefix[0] == 0)
190 if (EMP_STR_EMPTY (string))
194 for (p = string; *p != '\0'; p = g_utf8_next_char (p))
198 /* Make the char lower-case, remove its accentuation marks, and ignore it
199 * if it is just unicode marks */
200 sc = stripped_char (g_utf8_get_char (p));
204 /* If we want to go to next word, ignore alpha-num chars */
205 if (next_word && g_unichar_isalnum (sc))
209 /* Ignore word separators */
210 if (!g_unichar_isalnum (sc))
213 /* If this char does not match prefix_p, go to next word and start again
214 * from the beginning of prefix */
215 if (sc != g_utf8_get_char (prefix_p))
222 /* prefix_p match, verify to next char. If this was the last of prefix,
223 * it means it completely machted and we are done. */
224 prefix_p = g_utf8_next_char (prefix_p);
225 if (*prefix_p == '\0')
233 empathy_live_search_match_words (const gchar *string,
241 for (i = 0; i < words->len; i++)
242 if (!live_search_match_prefix (string, g_ptr_array_index (words, i)))
249 fire_key_navigation_sig (EmpathyLiveSearch *self,
254 g_signal_emit (self, signals[KEYNAV], 0, event, &ret);
259 live_search_entry_key_pressed_cb (GtkEntry *entry,
263 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
265 /* if esc key pressed, hide the search */
266 if (event->keyval == GDK_KEY_Escape)
268 gtk_widget_hide (GTK_WIDGET (self));
272 /* emit key navigation signal, so other widgets can respond to it properly */
273 if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down
274 || event->keyval == GDK_KEY_Page_Up || event->keyval == GDK_KEY_Page_Down)
276 return fire_key_navigation_sig (self, event);
279 if (event->keyval == GDK_KEY_Home || event->keyval == GDK_KEY_End ||
280 event->keyval == GDK_KEY_space)
282 /* If the live search is visible, the entry should catch the Home/End
283 * and space events */
284 if (!gtk_widget_get_visible (GTK_WIDGET (self)))
286 return fire_key_navigation_sig (self, event);
294 live_search_text_changed (GtkEntry *entry,
297 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
298 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
301 text = gtk_entry_get_text (entry);
303 if (EMP_STR_EMPTY (text))
304 gtk_widget_hide (GTK_WIDGET (self));
306 gtk_widget_show (GTK_WIDGET (self));
308 if (priv->stripped_words != NULL)
309 g_ptr_array_unref (priv->stripped_words);
311 priv->stripped_words = empathy_live_search_strip_utf8_string (text);
313 g_object_notify (G_OBJECT (self), "text");
317 live_search_close_pressed (GtkEntry *entry,
318 GtkEntryIconPosition icon_pos,
322 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
324 gtk_widget_hide (GTK_WIDGET (self));
328 live_search_key_press_event_cb (GtkWidget *widget,
332 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
333 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
337 /* dont forward this event to the entry, else the event is consumed by the
338 * entry and does not close the window */
339 if (!gtk_widget_get_visible (GTK_WIDGET (self)) &&
340 event->keyval == GDK_KEY_Escape)
343 /* do not show the search if CTRL and/or ALT are pressed with a key
344 * this is needed, because otherwise the CTRL + F accel would not work,
345 * because the entry consumes it */
346 if (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK) ||
347 event->keyval == GDK_KEY_Control_L ||
348 event->keyval == GDK_KEY_Control_R)
351 /* dont forward the up/down and Page Up/Down arrow keys to the entry,
352 * they are needed for navigation in the treeview and are not needed in
353 * the search entry */
354 if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down ||
355 event->keyval == GDK_KEY_Page_Up || event->keyval == GDK_KEY_Page_Down)
358 if (event->keyval == GDK_KEY_Home || event->keyval == GDK_KEY_End ||
359 event->keyval == GDK_KEY_space)
361 /* Home/End and space keys have to be forwarded to the entry only if
362 * the live search is visible (to move the cursor inside the entry). */
363 if (!gtk_widget_get_visible (GTK_WIDGET (self)))
367 /* realize the widget if it is not realized yet */
368 gtk_widget_realize (priv->search_entry);
369 if (!gtk_widget_has_focus (priv->search_entry))
371 gtk_widget_grab_focus (priv->search_entry);
372 gtk_editable_set_position (GTK_EDITABLE (priv->search_entry), -1);
375 /* forward the event to the search entry */
376 new_event = gdk_event_copy ((GdkEvent *) event);
377 ret = gtk_widget_event (priv->search_entry, new_event);
378 gdk_event_free (new_event);
384 live_search_entry_activate_cb (GtkEntry *entry,
385 EmpathyLiveSearch *self)
387 g_signal_emit (self, signals[ACTIVATE], 0);
391 live_search_release_hook_widget (EmpathyLiveSearch *self)
393 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
395 /* remove old handlers if old source was not null */
396 if (priv->hook_widget != NULL)
398 g_signal_handlers_disconnect_by_func (priv->hook_widget,
399 live_search_key_press_event_cb, self);
400 g_signal_handlers_disconnect_by_func (priv->hook_widget,
401 live_search_hook_widget_destroy_cb, self);
402 g_object_unref (priv->hook_widget);
403 priv->hook_widget = NULL;
408 live_search_hook_widget_destroy_cb (GtkWidget *object,
411 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
413 /* unref the hook widget and hide search */
414 gtk_widget_hide (GTK_WIDGET (self));
415 live_search_release_hook_widget (self);
419 live_search_dispose (GObject *obj)
421 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
423 live_search_release_hook_widget (self);
425 if (G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose != NULL)
426 G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose (obj);
430 live_search_finalize (GObject *obj)
432 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
433 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
435 if (priv->stripped_words != NULL)
436 g_ptr_array_unref (priv->stripped_words);
438 if (G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize != NULL)
439 G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize (obj);
443 live_search_get_property (GObject *object,
448 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
452 case PROP_HOOK_WIDGET:
453 g_value_set_object (value, empathy_live_search_get_hook_widget (self));
456 g_value_set_string (value, empathy_live_search_get_text (self));
459 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
465 live_search_set_property (GObject *object,
470 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
473 case PROP_HOOK_WIDGET:
474 empathy_live_search_set_hook_widget (self, g_value_get_object (value));
477 empathy_live_search_set_text (self, g_value_get_string (value));
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
486 live_search_unmap (GtkWidget *widget)
488 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
489 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
491 GTK_WIDGET_CLASS (empathy_live_search_parent_class)->unmap (widget);
493 /* unmap can happen if a parent gets hidden, in that case we want to hide
494 * the live search as well, so when it gets mapped again, the live search
496 gtk_widget_hide (widget);
498 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
499 gtk_widget_grab_focus (priv->hook_widget);
503 live_search_show (GtkWidget *widget)
505 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
506 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
508 if (!gtk_widget_has_focus (priv->search_entry))
509 gtk_widget_grab_focus (priv->search_entry);
511 GTK_WIDGET_CLASS (empathy_live_search_parent_class)->show (widget);
515 live_search_grab_focus (GtkWidget *widget)
517 EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
518 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
520 if (!gtk_widget_has_focus (priv->search_entry))
522 gtk_widget_grab_focus (priv->search_entry);
523 gtk_editable_set_position (GTK_EDITABLE (priv->search_entry), -1);
528 empathy_live_search_class_init (EmpathyLiveSearchClass *klass)
530 GObjectClass *object_class = (GObjectClass *) klass;
531 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
532 GParamSpec *param_spec;
534 object_class->finalize = live_search_finalize;
535 object_class->dispose = live_search_dispose;
536 object_class->get_property = live_search_get_property;
537 object_class->set_property = live_search_set_property;
539 widget_class->unmap = live_search_unmap;
540 widget_class->show = live_search_show;
541 widget_class->grab_focus = live_search_grab_focus;
543 signals[ACTIVATE] = g_signal_new ("activate",
544 G_TYPE_FROM_CLASS (object_class),
548 g_cclosure_marshal_generic,
551 signals[KEYNAV] = g_signal_new ("key-navigation",
552 G_TYPE_FROM_CLASS (object_class),
555 g_signal_accumulator_true_handled, NULL,
556 g_cclosure_marshal_generic,
557 G_TYPE_BOOLEAN, 1, GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
559 param_spec = g_param_spec_object ("hook-widget", "Live Search Hook Widget",
560 "The live search catches key-press-events on this widget",
561 GTK_TYPE_WIDGET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
562 g_object_class_install_property (object_class, PROP_HOOK_WIDGET,
565 param_spec = g_param_spec_string ("text", "Live Search Text",
566 "The text of the live search entry",
567 "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
568 g_object_class_install_property (object_class, PROP_TEXT, param_spec);
570 g_type_class_add_private (klass, sizeof (EmpathyLiveSearchPriv));
574 empathy_live_search_init (EmpathyLiveSearch *self)
576 EmpathyLiveSearchPriv *priv =
577 G_TYPE_INSTANCE_GET_PRIVATE ((self), EMPATHY_TYPE_LIVE_SEARCH,
578 EmpathyLiveSearchPriv);
580 gtk_widget_set_no_show_all (GTK_WIDGET (self), TRUE);
582 priv->search_entry = gtk_entry_new ();
583 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
584 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLOSE);
585 gtk_entry_set_icon_activatable (GTK_ENTRY (priv->search_entry),
586 GTK_ENTRY_ICON_SECONDARY, TRUE);
587 gtk_entry_set_icon_sensitive (GTK_ENTRY (priv->search_entry),
588 GTK_ENTRY_ICON_SECONDARY, TRUE);
589 gtk_widget_show (priv->search_entry);
591 gtk_box_pack_start (GTK_BOX (self), priv->search_entry, TRUE, TRUE, 0);
593 g_signal_connect (priv->search_entry, "icon_release",
594 G_CALLBACK (live_search_close_pressed), self);
595 g_signal_connect (priv->search_entry, "changed",
596 G_CALLBACK (live_search_text_changed), self);
597 g_signal_connect (priv->search_entry, "key-press-event",
598 G_CALLBACK (live_search_entry_key_pressed_cb), self);
599 g_signal_connect (priv->search_entry, "activate",
600 G_CALLBACK (live_search_entry_activate_cb), self);
602 priv->hook_widget = NULL;
608 empathy_live_search_new (GtkWidget *hook)
610 g_return_val_if_fail (hook == NULL || GTK_IS_WIDGET (hook), NULL);
612 return g_object_new (EMPATHY_TYPE_LIVE_SEARCH,
620 empathy_live_search_get_hook_widget (EmpathyLiveSearch *self)
622 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
624 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
626 return priv->hook_widget;
630 empathy_live_search_set_hook_widget (EmpathyLiveSearch *self,
633 EmpathyLiveSearchPriv *priv;
635 g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
636 g_return_if_fail (hook == NULL || GTK_IS_WIDGET (hook));
638 priv = GET_PRIV (self);
640 /* release the actual widget */
641 live_search_release_hook_widget (self);
643 /* connect handlers if new source is not null */
646 priv->hook_widget = g_object_ref (hook);
647 g_signal_connect (priv->hook_widget, "key-press-event",
648 G_CALLBACK (live_search_key_press_event_cb),
650 g_signal_connect (priv->hook_widget, "destroy",
651 G_CALLBACK (live_search_hook_widget_destroy_cb),
657 empathy_live_search_get_text (EmpathyLiveSearch *self)
659 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
661 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
663 return gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
667 empathy_live_search_set_text (EmpathyLiveSearch *self,
670 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
672 g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
673 g_return_if_fail (text != NULL);
675 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), text);
679 * empathy_live_search_match:
680 * @self: a #EmpathyLiveSearch
681 * @string: a string where to search, must be valid UTF-8.
683 * Search if one of the words in @string string starts with the current text
686 * Searching for "aba" in "Abasto" will match, searching in "Moraba" will not,
687 * and searching in "A tool (abacus)" will do.
689 * The match is not case-sensitive, and regardless of the accentuation marks.
691 * Returns: %TRUE if a match is found, %FALSE otherwise.
695 empathy_live_search_match (EmpathyLiveSearch *self,
698 EmpathyLiveSearchPriv *priv;
700 g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), FALSE);
702 priv = GET_PRIV (self);
704 return empathy_live_search_match_words (string, priv->stripped_words);
708 empathy_live_search_match_string (const gchar *string,
714 words = empathy_live_search_strip_utf8_string (prefix);
715 match = empathy_live_search_match_words (string, words);
717 g_ptr_array_unref (words);
723 empathy_live_search_get_words (EmpathyLiveSearch *self)
725 EmpathyLiveSearchPriv *priv = GET_PRIV (self);
727 return priv->stripped_words;