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>
25 #include "empathy-ui-utils.h"
26 #include "empathy-utils.h"
28 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySearchBar)
30 G_DEFINE_TYPE (EmpathySearchBar, empathy_search_bar, GTK_TYPE_BOX);
32 typedef struct _EmpathySearchBarPriv EmpathySearchBarPriv;
33 struct _EmpathySearchBarPriv
35 EmpathyThemeAdium *chat_view;
37 GtkWidget *search_entry;
39 GtkWidget *search_match_case;
41 GtkWidget *search_match_case_toolitem;
43 GtkWidget *search_close;
44 GtkWidget *search_previous;
45 GtkWidget *search_next;
46 GtkWidget *search_not_found;
50 empathy_search_bar_new (EmpathyThemeAdium *view)
52 EmpathySearchBar *self = g_object_new (EMPATHY_TYPE_SEARCH_BAR, NULL);
54 GET_PRIV (self)->chat_view = view;
56 return GTK_WIDGET (self);
60 empathy_search_bar_update_buttons (EmpathySearchBar *self,
64 gboolean can_go_forward = FALSE;
65 gboolean can_go_backward = FALSE;
67 EmpathySearchBarPriv* priv = GET_PRIV (self);
69 /* update previous / next buttons */
70 empathy_theme_adium_find_abilities (priv->chat_view, search, match_case,
71 &can_go_backward, &can_go_forward);
73 gtk_widget_set_sensitive (priv->search_previous,
74 can_go_backward && !EMP_STR_EMPTY (search));
75 gtk_widget_set_sensitive (priv->search_next,
76 can_go_forward && !EMP_STR_EMPTY (search));
80 empathy_search_bar_update (EmpathySearchBar *self)
84 EmpathySearchBarPriv *priv = GET_PRIV (self);
86 search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
87 match_case = gtk_toggle_button_get_active (
88 GTK_TOGGLE_BUTTON (priv->search_match_case));
90 /* highlight & search */
91 empathy_theme_adium_highlight (priv->chat_view, search, match_case);
93 /* update the buttons */
94 empathy_search_bar_update_buttons (self, search, match_case);
100 empathy_search_bar_show (EmpathySearchBar *self)
102 EmpathySearchBarPriv *priv = GET_PRIV (self);
104 /* update the highlighting and buttons */
105 empathy_search_bar_update (self);
107 /* grab the focus to the search entry */
108 gtk_widget_grab_focus (priv->search_entry);
110 gtk_widget_show (GTK_WIDGET (self));
114 empathy_search_bar_hide (EmpathySearchBar *self)
116 EmpathySearchBarPriv *priv = GET_PRIV (self);
118 empathy_theme_adium_highlight (priv->chat_view, "", FALSE);
119 gtk_widget_hide (GTK_WIDGET (self));
121 /* give the focus back to the focus-chain with the chat view */
122 gtk_widget_grab_focus (GTK_WIDGET (priv->chat_view));
126 empathy_search_bar_search (EmpathySearchBar *self,
133 EmpathySearchBarPriv *priv;
135 priv = GET_PRIV (self);
137 search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
138 match_case = gtk_toggle_button_get_active (
139 GTK_TOGGLE_BUTTON (priv->search_match_case));
141 /* highlight & search */
142 empathy_theme_adium_highlight (priv->chat_view, search, match_case);
145 found = empathy_theme_adium_find_next (priv->chat_view,
152 found = empathy_theme_adium_find_previous (priv->chat_view,
158 /* (don't) display the not found label */
159 gtk_widget_set_visible (priv->search_not_found,
160 !(found || EMP_STR_EMPTY (search)));
162 /* update the buttons */
163 empathy_search_bar_update_buttons (self, search, match_case);
169 empathy_search_bar_close_cb (GtkButton *button,
172 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (user_data));
176 empathy_search_bar_entry_changed (GtkEditable *entry,
179 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, TRUE);
183 empathy_search_bar_key_pressed (GtkWidget *widget,
187 if (event->keyval == GDK_KEY_Escape)
189 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (widget));
196 empathy_search_bar_next_cb (GtkButton *button,
199 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), TRUE, FALSE);
203 empathy_search_bar_previous_cb (GtkButton *button,
206 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, FALSE);
210 empathy_search_bar_match_case_toggled (GtkButton *button,
213 empathy_search_bar_update (EMPATHY_SEARCH_BAR (user_data));
217 empathy_search_bar_match_case_menu_toggled (GtkWidget *check,
220 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
223 match_case = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (check));
225 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->search_match_case),
230 empathy_searchbar_create_menu_proxy_cb (GtkToolItem *toolitem,
233 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
234 GtkWidget *checkbox_menu;
237 checkbox_menu = gtk_check_menu_item_new_with_mnemonic (_("_Match case"));
238 match_case = gtk_toggle_button_get_active (
239 GTK_TOGGLE_BUTTON (priv->search_match_case));
240 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox_menu),
243 g_signal_connect (checkbox_menu, "toggled",
244 G_CALLBACK (empathy_search_bar_match_case_menu_toggled), user_data);
246 gtk_tool_item_set_proxy_menu_item (toolitem, "menu-proxy",
253 empathy_search_bar_init (EmpathySearchBar * self)
258 EmpathySearchBarPriv *priv;
260 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_SEARCH_BAR,
261 EmpathySearchBarPriv);
265 filename = empathy_file_lookup ("empathy-search-bar.ui", "libempathy-gtk");
266 gui = empathy_builder_get_file (filename,
267 "search_widget", &internal,
268 "search_close", &priv->search_close,
269 "search_entry", &priv->search_entry,
270 "search_previous", &priv->search_previous,
271 "search_next", &priv->search_next,
272 "search_not_found", &priv->search_not_found,
273 "search_match_case", &priv->search_match_case,
277 /* Add the signals */
278 empathy_builder_connect (gui, self,
279 "search_close", "clicked", empathy_search_bar_close_cb,
280 "search_entry", "changed", empathy_search_bar_entry_changed,
281 "search_previous", "clicked", empathy_search_bar_previous_cb,
282 "search_next", "clicked", empathy_search_bar_next_cb,
283 "search_match_case", "toggled", empathy_search_bar_match_case_toggled,
284 "search_match_case_toolitem", "create-menu-proxy", empathy_searchbar_create_menu_proxy_cb,
287 g_signal_connect (G_OBJECT (self), "key-press-event",
288 G_CALLBACK (empathy_search_bar_key_pressed), NULL);
290 gtk_box_pack_start (GTK_BOX (self), internal, TRUE, TRUE, 0);
291 gtk_widget_show_all (internal);
292 gtk_widget_hide (priv->search_not_found);
293 g_object_unref (gui);
297 empathy_search_bar_class_init (EmpathySearchBarClass *class)
299 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
301 g_type_class_add_private (gobject_class, sizeof (EmpathySearchBarPriv));
305 empathy_search_bar_paste_clipboard (EmpathySearchBar *self)
307 EmpathySearchBarPriv *priv = GET_PRIV (self);
309 gtk_editable_paste_clipboard (GTK_EDITABLE (priv->search_entry));