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