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
23 #include <glib-object.h>
24 #include <glib/gi18n-lib.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
;
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
);
66 empathy_search_bar_update_buttons (EmpathySearchBar
*self
,
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
));
86 empathy_search_bar_update (EmpathySearchBar
*self
)
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
);
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
));
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
));
132 empathy_search_bar_search (EmpathySearchBar
*self
,
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
);
151 found
= empathy_chat_view_find_next (priv
->chat_view
,
158 found
= empathy_chat_view_find_previous (priv
->chat_view
,
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
);
175 empathy_search_bar_close_cb (GtkButton
*button
,
178 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (user_data
));
182 empathy_search_bar_entry_changed (GtkEditable
*entry
,
185 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data
), FALSE
, TRUE
);
189 empathy_search_bar_key_pressed (GtkWidget
*widget
,
193 if (event
->keyval
== GDK_KEY_Escape
)
195 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (widget
));
202 empathy_search_bar_next_cb (GtkButton
*button
,
205 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data
), TRUE
, FALSE
);
209 empathy_search_bar_previous_cb (GtkButton
*button
,
212 empathy_search_bar_search (EMPATHY_SEARCH_BAR (user_data
), FALSE
, FALSE
);
216 empathy_search_bar_match_case_toggled (GtkButton
*button
,
219 empathy_search_bar_update (EMPATHY_SEARCH_BAR (user_data
));
223 empathy_search_bar_match_case_menu_toggled (GtkWidget
*check
,
226 EmpathySearchBarPriv
* priv
= GET_PRIV ( EMPATHY_SEARCH_BAR (user_data
));
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
),
236 empathy_searchbar_create_menu_proxy_cb (GtkToolItem
*toolitem
,
239 EmpathySearchBarPriv
* priv
= GET_PRIV ( EMPATHY_SEARCH_BAR (user_data
));
240 GtkWidget
*checkbox_menu
;
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
),
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",
259 empathy_search_bar_init (EmpathySearchBar
* self
)
264 EmpathySearchBarPriv
*priv
;
266 priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
, EMPATHY_TYPE_SEARCH_BAR
,
267 EmpathySearchBarPriv
);
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
,
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
,
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
);
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
));
311 empathy_search_bar_paste_clipboard (EmpathySearchBar
*self
)
313 EmpathySearchBarPriv
*priv
= GET_PRIV (self
);
315 gtk_editable_paste_clipboard (GTK_EDITABLE (priv
->search_entry
));