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