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