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