1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * Copyright (C) 2010 Thomas Meire <blackskad@gmail.com>
5 * The code contained in this file is free software; you can redistribute
6 * it and/or modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either version
8 * 2.1 of the License, or (at your option) any later version.
10 * This file 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 code; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "empathy-search-bar.h"
23 #include <glib/gi18n-lib.h>
24 #include <tp-account-widgets/tpaw-builder.h>
25 #include <tp-account-widgets/tpaw-utils.h>
27 #include "empathy-ui-utils.h"
28 #include "empathy-utils.h"
30 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySearchBar)
32 G_DEFINE_TYPE (EmpathySearchBar, empathy_search_bar, GTK_TYPE_BOX);
34 typedef struct _EmpathySearchBarPriv EmpathySearchBarPriv;
35 struct _EmpathySearchBarPriv
37 EmpathyThemeAdium *chat_view;
39 GtkWidget *search_entry;
41 GtkWidget *search_match_case;
43 GtkWidget *search_match_case_toolitem;
45 GtkWidget *search_close;
46 GtkWidget *search_previous;
47 GtkWidget *search_next;
48 GtkWidget *search_not_found;
52 empathy_search_bar_new (EmpathyThemeAdium *view)
54 EmpathySearchBar *self = g_object_new (EMPATHY_TYPE_SEARCH_BAR, NULL);
56 GET_PRIV (self)->chat_view = view;
58 return GTK_WIDGET (self);
62 empathy_search_bar_update_buttons (EmpathySearchBar *self,
66 gboolean can_go_forward = FALSE;
67 gboolean can_go_backward = FALSE;
69 EmpathySearchBarPriv* priv = GET_PRIV (self);
71 /* update previous / next buttons */
72 empathy_theme_adium_find_abilities (priv->chat_view, search, match_case,
73 &can_go_backward, &can_go_forward);
75 gtk_widget_set_sensitive (priv->search_previous,
76 can_go_backward && !TPAW_STR_EMPTY (search));
77 gtk_widget_set_sensitive (priv->search_next,
78 can_go_forward && !TPAW_STR_EMPTY (search));
82 empathy_search_bar_update (EmpathySearchBar *self)
86 EmpathySearchBarPriv *priv = GET_PRIV (self);
88 search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
89 match_case = gtk_toggle_button_get_active (
90 GTK_TOGGLE_BUTTON (priv->search_match_case));
92 /* highlight & search */
93 empathy_theme_adium_highlight (priv->chat_view, search, match_case);
95 /* update the buttons */
96 empathy_search_bar_update_buttons (self, search, match_case);
102 empathy_search_bar_show (EmpathySearchBar *self)
104 EmpathySearchBarPriv *priv = GET_PRIV (self);
106 /* update the highlighting and buttons */
107 empathy_search_bar_update (self);
109 /* grab the focus to the search entry */
110 gtk_widget_grab_focus (priv->search_entry);
112 gtk_widget_show (GTK_WIDGET (self));
116 empathy_search_bar_hide (EmpathySearchBar *self)
118 EmpathySearchBarPriv *priv = GET_PRIV (self);
120 empathy_theme_adium_highlight (priv->chat_view, "", FALSE);
121 gtk_widget_hide (GTK_WIDGET (self));
123 /* give the focus back to the focus-chain with the chat view */
124 gtk_widget_grab_focus (GTK_WIDGET (priv->chat_view));
128 empathy_search_bar_search (EmpathySearchBar *self,
135 EmpathySearchBarPriv *priv;
137 priv = GET_PRIV (self);
139 search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
140 match_case = gtk_toggle_button_get_active (
141 GTK_TOGGLE_BUTTON (priv->search_match_case));
143 /* highlight & search */
144 empathy_theme_adium_highlight (priv->chat_view, search, match_case);
147 found = empathy_theme_adium_find_next (priv->chat_view,
154 found = empathy_theme_adium_find_previous (priv->chat_view,
160 /* (don't) display the not found label */
161 gtk_widget_set_visible (priv->search_not_found,
162 !(found || TPAW_STR_EMPTY (search)));
164 /* update the buttons */
165 empathy_search_bar_update_buttons (self, search, match_case);
171 empathy_search_bar_close_cb (GtkButton *button,
174 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (user_data));
178 empathy_search_bar_entry_changed (GtkEditable *entry,
181 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, TRUE);
185 empathy_search_bar_key_pressed (GtkWidget *widget,
189 if (event->keyval == GDK_KEY_Escape)
191 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (widget));
198 empathy_search_bar_next_cb (GtkButton *button,
201 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), TRUE, FALSE);
205 empathy_search_bar_previous_cb (GtkButton *button,
208 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, FALSE);
212 empathy_search_bar_match_case_toggled (GtkButton *button,
215 empathy_search_bar_update (EMPATHY_SEARCH_BAR (user_data));
219 empathy_search_bar_match_case_menu_toggled (GtkWidget *check,
222 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
225 match_case = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (check));
227 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->search_match_case),
232 empathy_searchbar_create_menu_proxy_cb (GtkToolItem *toolitem,
235 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
236 GtkWidget *checkbox_menu;
239 checkbox_menu = gtk_check_menu_item_new_with_mnemonic (_("_Match case"));
240 match_case = gtk_toggle_button_get_active (
241 GTK_TOGGLE_BUTTON (priv->search_match_case));
242 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox_menu),
245 g_signal_connect (checkbox_menu, "toggled",
246 G_CALLBACK (empathy_search_bar_match_case_menu_toggled), user_data);
248 gtk_tool_item_set_proxy_menu_item (toolitem, "menu-proxy",
255 empathy_search_bar_init (EmpathySearchBar * self)
260 EmpathySearchBarPriv *priv;
262 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_SEARCH_BAR,
263 EmpathySearchBarPriv);
267 filename = empathy_file_lookup ("empathy-search-bar.ui", "libempathy-gtk");
268 gui = tpaw_builder_get_file (filename,
269 "search_widget", &internal,
270 "search_close", &priv->search_close,
271 "search_entry", &priv->search_entry,
272 "search_previous", &priv->search_previous,
273 "search_next", &priv->search_next,
274 "search_not_found", &priv->search_not_found,
275 "search_match_case", &priv->search_match_case,
279 /* Add the signals */
280 tpaw_builder_connect (gui, self,
281 "search_close", "clicked", empathy_search_bar_close_cb,
282 "search_entry", "changed", empathy_search_bar_entry_changed,
283 "search_previous", "clicked", empathy_search_bar_previous_cb,
284 "search_next", "clicked", empathy_search_bar_next_cb,
285 "search_match_case", "toggled", empathy_search_bar_match_case_toggled,
286 "search_match_case_toolitem", "create-menu-proxy", empathy_searchbar_create_menu_proxy_cb,
289 g_signal_connect (G_OBJECT (self), "key-press-event",
290 G_CALLBACK (empathy_search_bar_key_pressed), NULL);
292 gtk_box_pack_start (GTK_BOX (self), internal, TRUE, TRUE, 0);
293 gtk_widget_show_all (internal);
294 gtk_widget_hide (priv->search_not_found);
295 g_object_unref (gui);
299 empathy_search_bar_class_init (EmpathySearchBarClass *class)
301 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
303 g_type_class_add_private (gobject_class, sizeof (EmpathySearchBarPriv));
307 empathy_search_bar_paste_clipboard (EmpathySearchBar *self)
309 EmpathySearchBarPriv *priv = GET_PRIV (self);
311 gtk_editable_paste_clipboard (GTK_EDITABLE (priv->search_entry));