Updated Traditional Chinese translation(Hong Kong and Taiwan)
[empathy-mirror.git] / libempathy-gtk / empathy-search-bar.c
blobd23380d4d84c7670a8ca957c1d303b656b777e36
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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
20 #include "config.h"
22 #include <glib.h>
23 #include <glib-object.h>
24 #include <glib/gi18n-lib.h>
25 #include <gtk/gtk.h>
26 #include <gdk/gdkkeysyms.h>
28 #include <libempathy/empathy-utils.h>
30 #include "empathy-chat-view.h"
31 #include "empathy-search-bar.h"
32 #include "empathy-ui-utils.h"
34 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySearchBar)
36 G_DEFINE_TYPE (EmpathySearchBar, empathy_search_bar, GTK_TYPE_BOX);
38 typedef struct _EmpathySearchBarPriv EmpathySearchBarPriv;
39 struct _EmpathySearchBarPriv
41 EmpathyChatView *chat_view;
43 GtkWidget *search_entry;
45 GtkWidget *search_match_case;
47 GtkWidget *search_match_case_toolitem;
49 GtkWidget *search_close;
50 GtkWidget *search_previous;
51 GtkWidget *search_next;
52 GtkWidget *search_not_found;
55 GtkWidget *
56 empathy_search_bar_new (EmpathyChatView *view)
58 EmpathySearchBar *self = g_object_new (EMPATHY_TYPE_SEARCH_BAR, NULL);
60 GET_PRIV (self)->chat_view = view;
62 return GTK_WIDGET (self);
65 static void
66 empathy_search_bar_update_buttons (EmpathySearchBar *self,
67 gchar *search,
68 gboolean match_case)
70 gboolean can_go_forward = FALSE;
71 gboolean can_go_backward = FALSE;
73 EmpathySearchBarPriv* priv = GET_PRIV (self);
75 /* update previous / next buttons */
76 empathy_chat_view_find_abilities (priv->chat_view, search, match_case,
77 &can_go_backward, &can_go_forward);
79 gtk_widget_set_sensitive (priv->search_previous,
80 can_go_backward && !EMP_STR_EMPTY (search));
81 gtk_widget_set_sensitive (priv->search_next,
82 can_go_forward && !EMP_STR_EMPTY (search));
85 static void
86 empathy_search_bar_update (EmpathySearchBar *self)
88 gchar *search;
89 gboolean match_case;
90 EmpathySearchBarPriv *priv = GET_PRIV (self);
92 search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
93 match_case = gtk_toggle_button_get_active (
94 GTK_TOGGLE_BUTTON (priv->search_match_case));
96 /* highlight & search */
97 empathy_chat_view_highlight (priv->chat_view, search, match_case);
99 /* update the buttons */
100 empathy_search_bar_update_buttons (self, search, match_case);
102 g_free (search);
105 void
106 empathy_search_bar_show (EmpathySearchBar *self)
108 EmpathySearchBarPriv *priv = GET_PRIV (self);
110 /* update the highlighting and buttons */
111 empathy_search_bar_update (self);
113 /* grab the focus to the search entry */
114 gtk_widget_grab_focus (priv->search_entry);
116 gtk_widget_show (GTK_WIDGET (self));
119 void
120 empathy_search_bar_hide (EmpathySearchBar *self)
122 EmpathySearchBarPriv *priv = GET_PRIV (self);
124 empathy_chat_view_highlight (priv->chat_view, "", FALSE);
125 gtk_widget_hide (GTK_WIDGET (self));
127 /* give the focus back to the focus-chain with the chat view */
128 gtk_widget_grab_focus (GTK_WIDGET (priv->chat_view));
131 static void
132 empathy_search_bar_search (EmpathySearchBar *self,
133 gboolean next,
134 gboolean new_search)
136 gchar *search;
137 gboolean found;
138 gboolean match_case;
139 EmpathySearchBarPriv *priv;
141 priv = GET_PRIV (self);
143 search = gtk_editable_get_chars (GTK_EDITABLE(priv->search_entry), 0, -1);
144 match_case = gtk_toggle_button_get_active (
145 GTK_TOGGLE_BUTTON (priv->search_match_case));
147 /* highlight & search */
148 empathy_chat_view_highlight (priv->chat_view, search, match_case);
149 if (next)
151 found = empathy_chat_view_find_next (priv->chat_view,
152 search,
153 new_search,
154 match_case);
156 else
158 found = empathy_chat_view_find_previous (priv->chat_view,
159 search,
160 new_search,
161 match_case);
164 /* (don't) display the not found label */
165 gtk_widget_set_visible (priv->search_not_found,
166 !(found || EMP_STR_EMPTY (search)));
168 /* update the buttons */
169 empathy_search_bar_update_buttons (self, search, match_case);
171 g_free (search);
174 static void
175 empathy_search_bar_close_cb (GtkButton *button,
176 gpointer user_data)
178 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (user_data));
181 static void
182 empathy_search_bar_entry_changed (GtkEditable *entry,
183 gpointer user_data)
185 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, TRUE);
188 static gboolean
189 empathy_search_bar_key_pressed (GtkWidget *widget,
190 GdkEventKey *event,
191 gpointer user_data)
193 if (event->keyval == GDK_KEY_Escape)
195 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (widget));
196 return TRUE;
198 return FALSE;
201 static void
202 empathy_search_bar_next_cb (GtkButton *button,
203 gpointer user_data)
205 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), TRUE, FALSE);
208 static void
209 empathy_search_bar_previous_cb (GtkButton *button,
210 gpointer user_data)
212 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data), FALSE, FALSE);
215 static void
216 empathy_search_bar_match_case_toggled (GtkButton *button,
217 gpointer user_data)
219 empathy_search_bar_update (EMPATHY_SEARCH_BAR (user_data));
222 static void
223 empathy_search_bar_match_case_menu_toggled (GtkWidget *check,
224 gpointer user_data)
226 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
227 gboolean match_case;
229 match_case = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (check));
231 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->search_match_case),
232 match_case);
235 static gboolean
236 empathy_searchbar_create_menu_proxy_cb (GtkToolItem *toolitem,
237 gpointer user_data)
239 EmpathySearchBarPriv* priv = GET_PRIV ( EMPATHY_SEARCH_BAR (user_data));
240 GtkWidget *checkbox_menu;
241 gboolean match_case;
243 checkbox_menu = gtk_check_menu_item_new_with_mnemonic (_("_Match case"));
244 match_case = gtk_toggle_button_get_active (
245 GTK_TOGGLE_BUTTON (priv->search_match_case));
246 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox_menu),
247 match_case);
249 g_signal_connect (checkbox_menu, "toggled",
250 G_CALLBACK (empathy_search_bar_match_case_menu_toggled), user_data);
252 gtk_tool_item_set_proxy_menu_item (toolitem, "menu-proxy",
253 checkbox_menu);
255 return TRUE;
258 static void
259 empathy_search_bar_init (EmpathySearchBar * self)
261 gchar *filename;
262 GtkBuilder *gui;
263 GtkWidget *internal;
264 EmpathySearchBarPriv *priv;
266 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_SEARCH_BAR,
267 EmpathySearchBarPriv);
269 self->priv = priv;
271 filename = empathy_file_lookup ("empathy-search-bar.ui", "libempathy-gtk");
272 gui = empathy_builder_get_file (filename,
273 "search_widget", &internal,
274 "search_close", &priv->search_close,
275 "search_entry", &priv->search_entry,
276 "search_previous", &priv->search_previous,
277 "search_next", &priv->search_next,
278 "search_not_found", &priv->search_not_found,
279 "search_match_case", &priv->search_match_case,
280 NULL);
281 g_free (filename);
283 /* Add the signals */
284 empathy_builder_connect (gui, self,
285 "search_close", "clicked", empathy_search_bar_close_cb,
286 "search_entry", "changed", empathy_search_bar_entry_changed,
287 "search_previous", "clicked", empathy_search_bar_previous_cb,
288 "search_next", "clicked", empathy_search_bar_next_cb,
289 "search_match_case", "toggled", empathy_search_bar_match_case_toggled,
290 "search_match_case_toolitem", "create-menu-proxy", empathy_searchbar_create_menu_proxy_cb,
291 NULL);
293 g_signal_connect (G_OBJECT (self), "key-press-event",
294 G_CALLBACK (empathy_search_bar_key_pressed), NULL);
296 gtk_box_pack_start (GTK_BOX (self), internal, TRUE, TRUE, 0);
297 gtk_widget_show_all (internal);
298 gtk_widget_hide (priv->search_not_found);
299 g_object_unref (gui);
302 static void
303 empathy_search_bar_class_init (EmpathySearchBarClass *class)
305 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
307 g_type_class_add_private (gobject_class, sizeof (EmpathySearchBarPriv));
310 void
311 empathy_search_bar_paste_clipboard (EmpathySearchBar *self)
313 EmpathySearchBarPriv *priv = GET_PRIV (self);
315 gtk_editable_paste_clipboard (GTK_EDITABLE (priv->search_entry));