]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-search-bar.c
Add search bar in chat text views (#585168)
[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
24 #include <libempathy/empathy-utils.h>
25
26 #include "empathy-chat-view.h"
27 #include "empathy-search-bar.h"
28 #include "empathy-ui-utils.h"
29
30 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySearchBar)
31
32 G_DEFINE_TYPE (EmpathySearchBar, empathy_search_bar, GTK_TYPE_BIN);
33
34 typedef struct _EmpathySearchBarPriv EmpathySearchBarPriv;
35 struct _EmpathySearchBarPriv
36 {
37   EmpathyChatView *chat_view;
38
39   GtkWidget *search_entry;
40   gchar *last_search;
41
42   GtkWidget *search_close;
43   GtkWidget *search_previous;
44   GtkWidget *search_next;
45   GtkWidget *search_not_found;
46 };
47
48 GtkWidget *
49 empathy_search_bar_new (EmpathyChatView *view)
50 {
51   EmpathySearchBar *self = g_object_new (EMPATHY_TYPE_SEARCH_BAR, NULL);
52
53   GET_PRIV (self)->chat_view = view;
54
55   return GTK_WIDGET (self);
56 }
57
58 static void
59 empathy_search_bar_size_request (GtkWidget *widget,
60     GtkRequisition *requisition)
61 {
62   GtkBin *bin;
63   GtkWidget *child;
64
65   bin = GTK_BIN (widget);
66   child = gtk_bin_get_child (bin);
67
68   if (child && gtk_widget_get_visible (child))
69     {
70       GtkRequisition child_requisition;
71
72       gtk_widget_size_request (child, &child_requisition);
73
74       requisition->width = child_requisition.width;
75       requisition->height = child_requisition.height;
76     }
77 }
78
79 static void
80 empathy_search_bar_size_allocate (GtkWidget *widget,
81     GtkAllocation *allocation)
82 {
83   GtkBin *bin;
84   GtkWidget *child;
85   GtkAllocation child_allocation;
86
87   bin = GTK_BIN (widget);
88   child = gtk_bin_get_child (bin);
89
90   gtk_widget_set_allocation (widget, allocation);
91
92   if (child && gtk_widget_get_visible (child))
93     {
94       child_allocation.x = allocation->x;
95       child_allocation.y = allocation->y;
96       child_allocation.width = MAX (allocation->width, 0);
97       child_allocation.height = MAX (allocation->height, 0);
98
99       gtk_widget_size_allocate (child, &child_allocation);
100     }
101 }
102
103 static void
104 empathy_search_bar_finalize (GObject *object)
105 {
106   EmpathySearchBarPriv *priv = GET_PRIV (object);
107
108   g_free (priv->last_search);
109 }
110
111 static void
112 empathy_search_bar_update_buttons (EmpathySearchBar *self)
113 {
114   gboolean can_go_forward = FALSE;
115   gboolean can_go_backward = FALSE;
116
117   EmpathySearchBarPriv* priv = GET_PRIV (self);
118
119   /* update previous / next buttons */
120   if (priv->last_search)
121     {
122       empathy_chat_view_find_abilities (priv->chat_view, priv->last_search,
123           &can_go_backward, &can_go_forward);
124     }
125
126   gtk_widget_set_sensitive (priv->search_previous,
127       can_go_backward && !EMP_STR_EMPTY (priv->last_search));
128   gtk_widget_set_sensitive (priv->search_next,
129       can_go_forward && !EMP_STR_EMPTY (priv->last_search));
130 }
131
132 void
133 empathy_search_bar_show (EmpathySearchBar *self)
134 {
135   EmpathySearchBarPriv *priv = GET_PRIV (self);
136
137   if (priv->last_search)
138     {
139       /* make sure we have an up to date last_search */
140       g_free (priv->last_search);
141       priv->last_search = gtk_editable_get_chars (
142           GTK_EDITABLE (priv->search_entry), 0, -1);
143     }
144   empathy_chat_view_highlight (priv->chat_view, priv->last_search);
145   empathy_search_bar_update_buttons (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
154 static void
155 empathy_search_bar_close_cb (GtkButton *button,
156     gpointer user_data)
157 {
158   EmpathySearchBarPriv *priv = GET_PRIV (user_data);
159
160   empathy_chat_view_highlight (priv->chat_view, "");
161   gtk_widget_hide (GTK_WIDGET (user_data));
162
163   /* give the focus back to the focus-chain with the chat view */
164   gtk_widget_grab_focus (GTK_WIDGET (priv->chat_view));
165 }
166
167 static void
168 empathy_search_bar_entry_changed (GtkEditable *entry,
169     gpointer user_data)
170 {
171   EmpathySearchBarPriv *priv;
172   gboolean found;
173   gchar *search;
174
175   priv = GET_PRIV (user_data);
176
177   search = gtk_editable_get_chars (entry, 0, -1);
178
179   found = empathy_chat_view_find_previous (priv->chat_view, search, TRUE);
180   gtk_widget_set_visible (priv->search_not_found,
181       !(found || EMP_STR_EMPTY (search)));
182
183   empathy_chat_view_highlight (priv->chat_view, search);
184
185   g_free (priv->last_search);
186   priv->last_search = search;
187
188   empathy_search_bar_update_buttons (EMPATHY_SEARCH_BAR (user_data));
189 }
190
191 static void
192 empathy_search_bar_next_cb (GtkButton *button,
193     gpointer user_data)
194 {
195   EmpathySearchBarPriv *priv = GET_PRIV (user_data);
196
197   empathy_chat_view_find_next (priv->chat_view, priv->last_search, FALSE);
198   empathy_search_bar_update_buttons (EMPATHY_SEARCH_BAR (user_data));
199 }
200
201 static void
202 empathy_search_bar_previous_cb (GtkButton *button,
203     gpointer user_data)
204 {
205   EmpathySearchBarPriv *priv = GET_PRIV (user_data);
206
207   empathy_chat_view_find_previous (priv->chat_view, priv->last_search, FALSE);
208   empathy_search_bar_update_buttons (EMPATHY_SEARCH_BAR (user_data));
209 }
210
211 static void
212 empathy_search_bar_init (EmpathySearchBar * self)
213 {
214   gchar *filename;
215   GtkBuilder *gui;
216   GtkWidget *internal;
217   EmpathySearchBarPriv *priv;
218
219   priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_SEARCH_BAR,
220       EmpathySearchBarPriv);
221
222   self->priv = priv;
223
224   filename = empathy_file_lookup ("empathy-search-bar.ui", "libempathy-gtk");
225   gui = empathy_builder_get_file (filename,
226       "search_widget", &internal,
227       "search_close", &priv->search_close,
228       "search_entry", &priv->search_entry,
229       "search_previous", &priv->search_previous,
230       "search_next", &priv->search_next,
231       "search_not_found", &priv->search_not_found,
232       NULL);
233   g_free (filename);
234
235   /* Add the signals */
236   empathy_builder_connect (gui, self,
237       "search_close", "clicked", empathy_search_bar_close_cb,
238       "search_entry", "changed", empathy_search_bar_entry_changed,
239       "search_previous", "clicked", empathy_search_bar_previous_cb,
240       "search_next", "clicked", empathy_search_bar_next_cb,
241       NULL);
242
243   gtk_container_add (GTK_CONTAINER (self), internal);
244   gtk_widget_show_all (internal);
245   gtk_widget_hide (priv->search_not_found);
246   g_object_unref (gui);
247 }
248
249 static void
250 empathy_search_bar_class_init (EmpathySearchBarClass *class)
251 {
252   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
253   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
254
255   gobject_class->finalize = empathy_search_bar_finalize;
256
257   g_type_class_add_private (gobject_class, sizeof (EmpathySearchBarPriv));
258
259   /* Neither GtkBin nor GtkContainer seems to do this for us :( */
260   widget_class->size_request = empathy_search_bar_size_request;
261   widget_class->size_allocate = empathy_search_bar_size_allocate;
262 }
263