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