call-window: properly remove the fs element notifiers
[empathy-mirror.git] / libempathy-gtk / empathy-search-bar.c
blob3383b9a12b2bb545a0898df54baaa6dae940a53d
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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
20 #include "config.h"
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;
51 GtkWidget *
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);
61 static void
62 empathy_search_bar_update_buttons (EmpathySearchBar *self,
63 gchar *search,
64 gboolean match_case)
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));
81 static void
82 empathy_search_bar_update (EmpathySearchBar *self)
84 gchar *search;
85 gboolean match_case;
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);
98 g_free (search);
101 void
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));
115 void
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));
127 static void
128 empathy_search_bar_search (EmpathySearchBar *self,
129 gboolean next,
130 gboolean new_search)
132 gchar *search;
133 gboolean found;
134 gboolean match_case;
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);
145 if (next)
147 found = empathy_theme_adium_find_next (priv->chat_view,
148 search,
149 new_search,
150 match_case);
152 else
154 found = empathy_theme_adium_find_previous (priv->chat_view,
155 search,
156 new_search,
157 match_case);
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);
167 g_free (search);
170 static void
171 empathy_search_bar_close_cb (GtkButton *button,
172 gpointer user_data)
174 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (user_data));
177 static void
178 empathy_search_bar_entry_changed (GtkEditable *entry,
179 gpointer user_data)
181 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, TRUE);
184 static gboolean
185 empathy_search_bar_key_pressed (GtkWidget *widget,
186 GdkEventKey *event,
187 gpointer user_data)
189 if (event->keyval == GDK_KEY_Escape)
191 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (widget));
192 return TRUE;
194 return FALSE;
197 static void
198 empathy_search_bar_next_cb (GtkButton *button,
199 gpointer user_data)
201 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), TRUE, FALSE);
204 static void
205 empathy_search_bar_previous_cb (GtkButton *button,
206 gpointer user_data)
208 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, FALSE);
211 static void
212 empathy_search_bar_match_case_toggled (GtkButton *button,
213 gpointer user_data)
215 empathy_search_bar_update (EMPATHY_SEARCH_BAR (user_data));
218 static void
219 empathy_search_bar_match_case_menu_toggled (GtkWidget *check,
220 gpointer user_data)
222 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
223 gboolean match_case;
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),
228 match_case);
231 static gboolean
232 empathy_searchbar_create_menu_proxy_cb (GtkToolItem *toolitem,
233 gpointer user_data)
235 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
236 GtkWidget *checkbox_menu;
237 gboolean match_case;
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),
243 match_case);
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",
249 checkbox_menu);
251 return TRUE;
254 static void
255 empathy_search_bar_init (EmpathySearchBar * self)
257 gchar *filename;
258 GtkBuilder *gui;
259 GtkWidget *internal;
260 EmpathySearchBarPriv *priv;
262 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_SEARCH_BAR,
263 EmpathySearchBarPriv);
265 self->priv = priv;
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,
276 NULL);
277 g_free (filename);
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,
287 NULL);
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);
298 static void
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));
306 void
307 empathy_search_bar_paste_clipboard (EmpathySearchBar *self)
309 EmpathySearchBarPriv *priv = GET_PRIV (self);
311 gtk_editable_paste_clipboard (GTK_EDITABLE (priv->search_entry));