]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-search-bar.c
018a068abdc349dd0765195238cbed311c736136
[empathy.git] / libempathy-gtk / empathy-search-bar.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * Copyright (C) 2010 Thomas Meire <blackskad@gmail.com>
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21
22 #include <glib/gi18n-lib.h>
23
24 #include <libempathy/empathy-utils.h>
25
26 #include "empathy-search-bar.h"
27 #include "empathy-ui-utils.h"
28
29 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySearchBar)
30
31 G_DEFINE_TYPE (EmpathySearchBar, empathy_search_bar, GTK_TYPE_BOX);
32
33 typedef struct _EmpathySearchBarPriv EmpathySearchBarPriv;
34 struct _EmpathySearchBarPriv
35 {
36   EmpathyThemeAdium *chat_view;
37
38   GtkWidget *search_entry;
39
40   GtkWidget *search_match_case;
41
42   GtkWidget *search_match_case_toolitem;
43
44   GtkWidget *search_close;
45   GtkWidget *search_previous;
46   GtkWidget *search_next;
47   GtkWidget *search_not_found;
48 };
49
50 GtkWidget *
51 empathy_search_bar_new (EmpathyThemeAdium *view)
52 {
53   EmpathySearchBar *self = g_object_new (EMPATHY_TYPE_SEARCH_BAR, NULL);
54
55   GET_PRIV (self)->chat_view = view;
56
57   return GTK_WIDGET (self);
58 }
59
60 static void
61 empathy_search_bar_update_buttons (EmpathySearchBar *self,
62     gchar *search,
63     gboolean match_case)
64 {
65   gboolean can_go_forward = FALSE;
66   gboolean can_go_backward = FALSE;
67
68   EmpathySearchBarPriv* priv = GET_PRIV (self);
69
70   /* update previous / next buttons */
71   empathy_theme_adium_find_abilities (priv->chat_view, search, match_case,
72       &can_go_backward, &can_go_forward);
73
74   gtk_widget_set_sensitive (priv->search_previous,
75       can_go_backward && !EMP_STR_EMPTY (search));
76   gtk_widget_set_sensitive (priv->search_next,
77       can_go_forward && !EMP_STR_EMPTY (search));
78 }
79
80 static void
81 empathy_search_bar_update (EmpathySearchBar *self)
82 {
83   gchar *search;
84   gboolean match_case;
85   EmpathySearchBarPriv *priv = GET_PRIV (self);
86
87   search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
88   match_case = gtk_toggle_button_get_active (
89       GTK_TOGGLE_BUTTON (priv->search_match_case));
90
91   /* highlight & search */
92   empathy_theme_adium_highlight (priv->chat_view, search, match_case);
93
94   /* update the buttons */
95   empathy_search_bar_update_buttons (self, search, match_case);
96
97   g_free (search);
98 }
99
100 void
101 empathy_search_bar_show (EmpathySearchBar *self)
102 {
103   EmpathySearchBarPriv *priv = GET_PRIV (self);
104
105   /* update the highlighting and buttons */
106   empathy_search_bar_update (self);
107
108   /* grab the focus to the search entry */
109   gtk_widget_grab_focus (priv->search_entry);
110
111   gtk_widget_show (GTK_WIDGET (self));
112 }
113
114 void
115 empathy_search_bar_hide (EmpathySearchBar *self)
116 {
117   EmpathySearchBarPriv *priv = GET_PRIV (self);
118
119   empathy_theme_adium_highlight (priv->chat_view, "", FALSE);
120   gtk_widget_hide (GTK_WIDGET (self));
121
122   /* give the focus back to the focus-chain with the chat view */
123   gtk_widget_grab_focus (GTK_WIDGET (priv->chat_view));
124 }
125
126 static void
127 empathy_search_bar_search (EmpathySearchBar *self,
128     gboolean next,
129     gboolean new_search)
130 {
131   gchar *search;
132   gboolean found;
133   gboolean match_case;
134   EmpathySearchBarPriv *priv;
135
136   priv = GET_PRIV (self);
137
138   search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
139   match_case = gtk_toggle_button_get_active (
140       GTK_TOGGLE_BUTTON (priv->search_match_case));
141
142   /* highlight & search */
143   empathy_theme_adium_highlight (priv->chat_view, search, match_case);
144   if (next)
145     {
146       found = empathy_theme_adium_find_next (priv->chat_view,
147           search,
148           new_search,
149           match_case);
150     }
151   else
152     {
153       found = empathy_theme_adium_find_previous (priv->chat_view,
154           search,
155           new_search,
156           match_case);
157     }
158
159   /* (don't) display the not found label */
160   gtk_widget_set_visible (priv->search_not_found,
161       !(found || EMP_STR_EMPTY (search)));
162
163   /* update the buttons */
164   empathy_search_bar_update_buttons (self, search, match_case);
165
166   g_free (search);
167 }
168
169 static void
170 empathy_search_bar_close_cb (GtkButton *button,
171     gpointer user_data)
172 {
173   empathy_search_bar_hide (EMPATHY_SEARCH_BAR (user_data));
174 }
175
176 static void
177 empathy_search_bar_entry_changed (GtkEditable *entry,
178     gpointer user_data)
179 {
180   empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, TRUE);
181 }
182
183 static gboolean
184 empathy_search_bar_key_pressed (GtkWidget   *widget,
185     GdkEventKey *event,
186     gpointer user_data)
187 {
188   if (event->keyval == GDK_KEY_Escape)
189     {
190       empathy_search_bar_hide (EMPATHY_SEARCH_BAR (widget));
191       return TRUE;
192     }
193   return FALSE;
194 }
195
196 static void
197 empathy_search_bar_next_cb (GtkButton *button,
198     gpointer user_data)
199 {
200   empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), TRUE, FALSE);
201 }
202
203 static void
204 empathy_search_bar_previous_cb (GtkButton *button,
205     gpointer user_data)
206 {
207   empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, FALSE);
208 }
209
210 static void
211 empathy_search_bar_match_case_toggled (GtkButton *button,
212     gpointer user_data)
213 {
214   empathy_search_bar_update (EMPATHY_SEARCH_BAR (user_data));
215 }
216
217 static void
218 empathy_search_bar_match_case_menu_toggled (GtkWidget *check,
219     gpointer user_data)
220 {
221   EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
222   gboolean match_case;
223
224   match_case = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (check));
225
226   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->search_match_case),
227       match_case);
228 }
229
230 static gboolean
231 empathy_searchbar_create_menu_proxy_cb (GtkToolItem *toolitem,
232     gpointer user_data)
233 {
234   EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
235   GtkWidget *checkbox_menu;
236   gboolean match_case;
237
238   checkbox_menu = gtk_check_menu_item_new_with_mnemonic (_("_Match case"));
239   match_case = gtk_toggle_button_get_active (
240       GTK_TOGGLE_BUTTON (priv->search_match_case));
241   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox_menu),
242       match_case);
243
244   g_signal_connect (checkbox_menu, "toggled",
245       G_CALLBACK (empathy_search_bar_match_case_menu_toggled), user_data);
246
247   gtk_tool_item_set_proxy_menu_item (toolitem, "menu-proxy",
248       checkbox_menu);
249
250   return TRUE;
251 }
252
253 static void
254 empathy_search_bar_init (EmpathySearchBar * self)
255 {
256   gchar *filename;
257   GtkBuilder *gui;
258   GtkWidget *internal;
259   EmpathySearchBarPriv *priv;
260
261   priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_SEARCH_BAR,
262       EmpathySearchBarPriv);
263
264   self->priv = priv;
265
266   filename = empathy_file_lookup ("empathy-search-bar.ui", "libempathy-gtk");
267   gui = empathy_builder_get_file (filename,
268       "search_widget", &internal,
269       "search_close", &priv->search_close,
270       "search_entry", &priv->search_entry,
271       "search_previous", &priv->search_previous,
272       "search_next", &priv->search_next,
273       "search_not_found", &priv->search_not_found,
274       "search_match_case", &priv->search_match_case,
275       NULL);
276   g_free (filename);
277
278   /* Add the signals */
279   empathy_builder_connect (gui, self,
280       "search_close", "clicked", empathy_search_bar_close_cb,
281       "search_entry", "changed", empathy_search_bar_entry_changed,
282       "search_previous", "clicked", empathy_search_bar_previous_cb,
283       "search_next", "clicked", empathy_search_bar_next_cb,
284       "search_match_case", "toggled", empathy_search_bar_match_case_toggled,
285       "search_match_case_toolitem", "create-menu-proxy", empathy_searchbar_create_menu_proxy_cb,
286       NULL);
287
288   g_signal_connect (G_OBJECT (self), "key-press-event",
289       G_CALLBACK (empathy_search_bar_key_pressed), NULL);
290
291   gtk_box_pack_start (GTK_BOX (self), internal, TRUE, TRUE, 0);
292   gtk_widget_show_all (internal);
293   gtk_widget_hide (priv->search_not_found);
294   g_object_unref (gui);
295 }
296
297 static void
298 empathy_search_bar_class_init (EmpathySearchBarClass *class)
299 {
300   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
301
302   g_type_class_add_private (gobject_class, sizeof (EmpathySearchBarPriv));
303 }
304
305 void
306 empathy_search_bar_paste_clipboard (EmpathySearchBar *self)
307 {
308   EmpathySearchBarPriv *priv = GET_PRIV (self);
309
310   gtk_editable_paste_clipboard (GTK_EDITABLE (priv->search_entry));
311 }