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