1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * Copyright (C) 2010 Thomas Meire <blackskad@gmail.com>
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.
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.
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
21 #include <glib-object.h>
24 #include <libempathy/empathy-utils.h>
26 #include "empathy-chat-view.h"
27 #include "empathy-search-bar.h"
28 #include "empathy-ui-utils.h"
30 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySearchBar)
32 G_DEFINE_TYPE (EmpathySearchBar, empathy_search_bar, GTK_TYPE_BIN);
34 typedef struct _EmpathySearchBarPriv EmpathySearchBarPriv;
35 struct _EmpathySearchBarPriv
37 EmpathyChatView *chat_view;
39 GtkWidget *search_entry;
41 GtkWidget *search_match_case;
43 GtkWidget *search_close;
44 GtkWidget *search_previous;
45 GtkWidget *search_next;
46 GtkWidget *search_not_found;
50 empathy_search_bar_new (EmpathyChatView *view)
52 EmpathySearchBar *self = g_object_new (EMPATHY_TYPE_SEARCH_BAR, NULL);
54 GET_PRIV (self)->chat_view = view;
56 return GTK_WIDGET (self);
60 empathy_search_bar_size_request (GtkWidget *widget,
61 GtkRequisition *requisition)
66 bin = GTK_BIN (widget);
67 child = gtk_bin_get_child (bin);
69 if (child && gtk_widget_get_visible (child))
71 GtkRequisition child_requisition;
73 gtk_widget_size_request (child, &child_requisition);
75 requisition->width = child_requisition.width;
76 requisition->height = child_requisition.height;
81 empathy_search_bar_size_allocate (GtkWidget *widget,
82 GtkAllocation *allocation)
86 GtkAllocation child_allocation;
88 bin = GTK_BIN (widget);
89 child = gtk_bin_get_child (bin);
91 gtk_widget_set_allocation (widget, allocation);
93 if (child && gtk_widget_get_visible (child))
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);
100 gtk_widget_size_allocate (child, &child_allocation);
105 empathy_search_bar_update_buttons (EmpathySearchBar *self,
108 gboolean can_go_forward = FALSE;
109 gboolean can_go_backward = FALSE;
111 EmpathySearchBarPriv* priv = GET_PRIV (self);
113 /* update previous / next buttons */
114 empathy_chat_view_find_abilities (priv->chat_view, search,
115 &can_go_backward, &can_go_forward);
117 gtk_widget_set_sensitive (priv->search_previous,
118 can_go_backward && !EMP_STR_EMPTY (search));
119 gtk_widget_set_sensitive (priv->search_next,
120 can_go_forward && !EMP_STR_EMPTY (search));
124 empathy_search_bar_show (EmpathySearchBar *self)
127 EmpathySearchBarPriv *priv = GET_PRIV (self);
129 search = gtk_editable_get_chars (GTK_EDITABLE (priv->search_entry), 0, -1);
130 empathy_chat_view_highlight (priv->chat_view, search, TRUE);
131 empathy_search_bar_update_buttons (self, search);
133 /* grab the focus to the search entry */
134 gtk_widget_grab_focus (priv->search_entry);
136 gtk_widget_show (GTK_WIDGET (self));
141 empathy_search_bar_close_cb (GtkButton *button,
144 EmpathySearchBarPriv *priv = GET_PRIV (user_data);
146 empathy_chat_view_highlight (priv->chat_view, "", FALSE);
147 gtk_widget_hide (GTK_WIDGET (user_data));
149 /* give the focus back to the focus-chain with the chat view */
150 gtk_widget_grab_focus (GTK_WIDGET (priv->chat_view));
154 empathy_search_bar_search (EmpathySearchBar *self,
161 EmpathySearchBarPriv *priv;
163 priv = GET_PRIV (self);
165 search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
166 match_case = gtk_toggle_button_get_active (
167 GTK_TOGGLE_BUTTON (priv->search_match_case));
169 /* highlight & search */
170 // TODO: add case parameter
171 empathy_chat_view_highlight (priv->chat_view, search, match_case);
174 found = empathy_chat_view_find_next (priv->chat_view, search, new_search);
178 found = empathy_chat_view_find_previous (priv->chat_view, search, new_search);
181 /* (don't) display the not found label */
182 gtk_widget_set_visible (priv->search_not_found,
183 !(found || EMP_STR_EMPTY (search)));
185 /* update the buttons */
186 empathy_search_bar_update_buttons (self, search);
192 empathy_search_bar_entry_changed (GtkEditable *entry,
195 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, TRUE);
199 empathy_search_bar_next_cb (GtkButton *button,
202 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), TRUE, FALSE);
206 empathy_search_bar_previous_cb (GtkButton *button,
209 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, FALSE);
213 empathy_search_bar_init (EmpathySearchBar * self)
218 EmpathySearchBarPriv *priv;
220 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_SEARCH_BAR,
221 EmpathySearchBarPriv);
225 filename = empathy_file_lookup ("empathy-search-bar.ui", "libempathy-gtk");
226 gui = empathy_builder_get_file (filename,
227 "search_widget", &internal,
228 "search_close", &priv->search_close,
229 "search_entry", &priv->search_entry,
230 "search_previous", &priv->search_previous,
231 "search_next", &priv->search_next,
232 "search_not_found", &priv->search_not_found,
233 "search_match_case", &priv->search_match_case,
237 /* Add the signals */
238 empathy_builder_connect (gui, self,
239 "search_close", "clicked", empathy_search_bar_close_cb,
240 "search_entry", "changed", empathy_search_bar_entry_changed,
241 "search_previous", "clicked", empathy_search_bar_previous_cb,
242 "search_next", "clicked", empathy_search_bar_next_cb,
245 gtk_container_add (GTK_CONTAINER (self), internal);
246 gtk_widget_show_all (internal);
247 gtk_widget_hide (priv->search_not_found);
248 g_object_unref (gui);
252 empathy_search_bar_class_init (EmpathySearchBarClass *class)
254 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
255 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
257 g_type_class_add_private (gobject_class, sizeof (EmpathySearchBarPriv));
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;