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