1 /* Copyright (C) 2004 Red Hat, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with the Gnome Library; see the file COPYING.LIB. If not,
15 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA.
21 #include "eggfindbar.h"
23 #include <glib/gi18n.h>
25 #include <gdk/gdkkeysyms.h>
29 struct _EggFindBarPrivate
33 GtkToolItem *next_button;
34 GtkToolItem *previous_button;
35 GtkToolItem *status_separator;
36 GtkToolItem *status_item;
37 GtkToolItem *case_button;
39 GtkWidget *find_entry;
40 GtkWidget *status_label;
42 gulong set_focus_handler;
43 guint case_sensitive : 1;
46 #define EGG_FIND_BAR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EGG_TYPE_FIND_BAR, EggFindBarPrivate))
54 static void egg_find_bar_finalize (GObject *object);
55 static void egg_find_bar_get_property (GObject *object,
59 static void egg_find_bar_set_property (GObject *object,
63 static void egg_find_bar_show (GtkWidget *widget);
64 static void egg_find_bar_hide (GtkWidget *widget);
65 static void egg_find_bar_grab_focus (GtkWidget *widget);
67 G_DEFINE_TYPE (EggFindBar, egg_find_bar, GTK_TYPE_TOOLBAR);
78 static guint find_bar_signals[LAST_SIGNAL] = { 0 };
81 egg_find_bar_class_init (EggFindBarClass *klass)
83 GObjectClass *object_class;
84 GtkWidgetClass *widget_class;
85 GtkBindingSet *binding_set;
87 egg_find_bar_parent_class = g_type_class_peek_parent (klass);
89 object_class = (GObjectClass *)klass;
90 widget_class = (GtkWidgetClass *)klass;
92 object_class->set_property = egg_find_bar_set_property;
93 object_class->get_property = egg_find_bar_get_property;
95 object_class->finalize = egg_find_bar_finalize;
97 widget_class->show = egg_find_bar_show;
98 widget_class->hide = egg_find_bar_hide;
100 widget_class->grab_focus = egg_find_bar_grab_focus;
102 find_bar_signals[NEXT] =
103 g_signal_new ("next",
104 G_OBJECT_CLASS_TYPE (object_class),
106 G_STRUCT_OFFSET (EggFindBarClass, next),
108 g_cclosure_marshal_VOID__VOID,
110 find_bar_signals[PREVIOUS] =
111 g_signal_new ("previous",
112 G_OBJECT_CLASS_TYPE (object_class),
114 G_STRUCT_OFFSET (EggFindBarClass, previous),
116 g_cclosure_marshal_VOID__VOID,
118 find_bar_signals[CLOSE] =
119 g_signal_new ("close",
120 G_OBJECT_CLASS_TYPE (object_class),
121 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
122 G_STRUCT_OFFSET (EggFindBarClass, close),
124 g_cclosure_marshal_VOID__VOID,
126 find_bar_signals[SCROLL] =
127 g_signal_new ("scroll",
128 G_OBJECT_CLASS_TYPE (object_class),
129 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
130 G_STRUCT_OFFSET (EggFindBarClass, scroll),
132 g_cclosure_marshal_VOID__ENUM,
134 GTK_TYPE_SCROLL_TYPE);
137 * EggFindBar:search_string:
139 * The current string to search for. NULL or empty string
140 * both mean no current string.
143 g_object_class_install_property (object_class,
145 g_param_spec_string ("search_string",
147 _("The name of the string to be found"),
152 * EggFindBar:case_sensitive:
154 * TRUE for a case sensitive search.
157 g_object_class_install_property (object_class,
159 g_param_spec_boolean ("case_sensitive",
161 _("TRUE for a case sensitive search"),
165 /* Style properties */
166 gtk_widget_class_install_style_property (widget_class,
167 g_param_spec_boxed ("all_matches_color",
168 _("Highlight color"),
169 _("Color of highlight for all matches"),
173 gtk_widget_class_install_style_property (widget_class,
174 g_param_spec_boxed ("current_match_color",
176 _("Color of highlight for the current match"),
180 g_type_class_add_private (object_class, sizeof (EggFindBarPrivate));
182 binding_set = gtk_binding_set_by_class (klass);
184 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
187 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
189 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_BACKWARD);
191 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
193 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_FORWARD);
197 egg_find_bar_emit_next (EggFindBar *find_bar)
199 g_signal_emit (find_bar, find_bar_signals[NEXT], 0);
203 egg_find_bar_emit_previous (EggFindBar *find_bar)
205 g_signal_emit (find_bar, find_bar_signals[PREVIOUS], 0);
209 next_clicked_callback (GtkButton *button,
212 EggFindBar *find_bar = EGG_FIND_BAR (data);
214 egg_find_bar_emit_next (find_bar);
218 previous_clicked_callback (GtkButton *button,
221 EggFindBar *find_bar = EGG_FIND_BAR (data);
223 egg_find_bar_emit_previous (find_bar);
227 case_sensitive_toggled_callback (GtkCheckButton *button,
230 EggFindBar *find_bar = EGG_FIND_BAR (data);
232 egg_find_bar_set_case_sensitive (find_bar,
233 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)));
237 entry_activate_callback (GtkEntry *entry,
240 EggFindBar *find_bar = EGG_FIND_BAR (data);
242 if (find_bar->priv->search_string != NULL)
243 egg_find_bar_emit_next (find_bar);
247 entry_changed_callback (GtkEntry *entry,
250 EggFindBar *find_bar = EGG_FIND_BAR (data);
253 /* paranoid strdup because set_search_string() sets
256 text = g_strdup (gtk_entry_get_text (entry));
258 egg_find_bar_set_search_string (find_bar, text);
264 set_focus_cb (GtkWidget *window,
268 GtkWidget *wbar = GTK_WIDGET (bar);
270 while (widget != NULL && widget != wbar)
272 widget = widget->parent;
275 /* if widget == bar, the new focus widget is in the bar, so we
280 g_signal_emit (bar, find_bar_signals[CLOSE], 0);
285 egg_find_bar_init (EggFindBar *find_bar)
287 EggFindBarPrivate *priv;
289 GtkWidget *alignment;
295 priv = EGG_FIND_BAR_GET_PRIVATE (find_bar);
297 find_bar->priv = priv;
298 priv->search_string = NULL;
300 gtk_toolbar_set_style (GTK_TOOLBAR (find_bar), GTK_TOOLBAR_BOTH_HORIZ);
303 item = gtk_tool_item_new ();
304 box = gtk_hbox_new (FALSE, 12);
306 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 0.0);
307 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 2, 2);
309 label = gtk_label_new_with_mnemonic (_("Find:"));
311 priv->find_entry = gtk_entry_new ();
312 gtk_entry_set_width_chars (GTK_ENTRY (priv->find_entry), 32);
313 gtk_entry_set_max_length (GTK_ENTRY (priv->find_entry), 512);
314 gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->find_entry);
317 arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
318 priv->previous_button = gtk_tool_button_new (arrow, Q_("Find Previous"));
319 gtk_tool_item_set_is_important (priv->previous_button, TRUE);
320 #if GTK_CHECK_VERSION (2, 11, 5)
321 gtk_widget_set_tooltip_text (GTK_WIDGET (priv->previous_button),
322 _("Find previous occurrence of the search string"));
324 gtk_tool_item_set_tooltip (priv->previous_button, GTK_TOOLBAR (find_bar)->tooltips,
325 _("Find previous occurrence of the search string"),
330 arrow = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
331 priv->next_button = gtk_tool_button_new (arrow, Q_("Find Next"));
332 gtk_tool_item_set_is_important (priv->next_button, TRUE);
333 #if GTK_CHECK_VERSION (2, 11, 5)
334 gtk_widget_set_tooltip_text (GTK_WIDGET (priv->next_button),
335 _("Find next occurrence of the search string"));
337 gtk_tool_item_set_tooltip (priv->next_button, GTK_TOOLBAR (find_bar)->tooltips,
338 _("Find next occurrence of the search string"),
343 priv->status_separator = gtk_separator_tool_item_new();
346 priv->case_button = gtk_toggle_tool_button_new ();
347 g_object_set (G_OBJECT (priv->case_button), "label", _("C_ase Sensitive"), NULL);
348 gtk_tool_item_set_is_important (priv->case_button, TRUE);
349 #if GTK_CHECK_VERSION (2, 11, 5)
350 gtk_widget_set_tooltip_text (GTK_WIDGET (priv->case_button),
351 _("Toggle case sensitive search"));
353 gtk_tool_item_set_tooltip (priv->case_button, GTK_TOOLBAR (find_bar)->tooltips,
354 _("Toggle case sensitive search"),
358 priv->status_item = gtk_tool_item_new();
359 gtk_tool_item_set_expand (priv->status_item, TRUE);
360 priv->status_label = gtk_label_new (NULL);
361 gtk_label_set_ellipsize (GTK_LABEL (priv->status_label),
362 PANGO_ELLIPSIZE_END);
363 gtk_misc_set_alignment (GTK_MISC (priv->status_label), 0.0, 0.5);
366 g_signal_connect (priv->find_entry, "changed",
367 G_CALLBACK (entry_changed_callback),
369 g_signal_connect (priv->find_entry, "activate",
370 G_CALLBACK (entry_activate_callback),
372 g_signal_connect (priv->next_button, "clicked",
373 G_CALLBACK (next_clicked_callback),
375 g_signal_connect (priv->previous_button, "clicked",
376 G_CALLBACK (previous_clicked_callback),
378 g_signal_connect (priv->case_button, "toggled",
379 G_CALLBACK (case_sensitive_toggled_callback),
382 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
383 gtk_box_pack_start (GTK_BOX (box), priv->find_entry, TRUE, TRUE, 0);
384 gtk_container_add (GTK_CONTAINER (alignment), box);
385 gtk_container_add (GTK_CONTAINER (item), alignment);
386 gtk_toolbar_insert (GTK_TOOLBAR (find_bar), item, -1);
387 gtk_toolbar_insert (GTK_TOOLBAR (find_bar), priv->previous_button, -1);
388 gtk_toolbar_insert (GTK_TOOLBAR (find_bar), priv->next_button, -1);
389 gtk_toolbar_insert (GTK_TOOLBAR (find_bar), priv->case_button, -1);
390 gtk_toolbar_insert (GTK_TOOLBAR (find_bar), priv->status_separator, -1);
391 gtk_container_add (GTK_CONTAINER (priv->status_item), priv->status_label);
392 gtk_toolbar_insert (GTK_TOOLBAR (find_bar), priv->status_item, -1);
394 /* don't show status separator/label until they are set */
396 gtk_widget_show_all (GTK_WIDGET (item));
397 gtk_widget_show_all (GTK_WIDGET (priv->next_button));
398 gtk_widget_show_all (GTK_WIDGET (priv->previous_button));
399 gtk_widget_show (priv->status_label);
403 egg_find_bar_finalize (GObject *object)
405 EggFindBar *find_bar = EGG_FIND_BAR (object);
406 EggFindBarPrivate *priv = (EggFindBarPrivate *)find_bar->priv;
408 g_free (priv->search_string);
410 G_OBJECT_CLASS (egg_find_bar_parent_class)->finalize (object);
414 egg_find_bar_set_property (GObject *object,
419 EggFindBar *find_bar = EGG_FIND_BAR (object);
423 case PROP_SEARCH_STRING:
424 egg_find_bar_set_search_string (find_bar, g_value_get_string (value));
426 case PROP_CASE_SENSITIVE:
427 egg_find_bar_set_case_sensitive (find_bar, g_value_get_boolean (value));
430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
436 egg_find_bar_get_property (GObject *object,
441 EggFindBar *find_bar = EGG_FIND_BAR (object);
442 EggFindBarPrivate *priv = (EggFindBarPrivate *)find_bar->priv;
446 case PROP_SEARCH_STRING:
447 g_value_set_string (value, priv->search_string);
449 case PROP_CASE_SENSITIVE:
450 g_value_set_boolean (value, priv->case_sensitive);
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459 egg_find_bar_show (GtkWidget *widget)
461 EggFindBar *bar = EGG_FIND_BAR (widget);
462 EggFindBarPrivate *priv = bar->priv;
464 GTK_WIDGET_CLASS (egg_find_bar_parent_class)->show (widget);
466 if (priv->set_focus_handler == 0)
470 toplevel = gtk_widget_get_toplevel (widget);
472 priv->set_focus_handler =
473 g_signal_connect (toplevel, "set-focus",
474 G_CALLBACK (set_focus_cb), bar);
479 egg_find_bar_hide (GtkWidget *widget)
481 EggFindBar *bar = EGG_FIND_BAR (widget);
482 EggFindBarPrivate *priv = bar->priv;
484 if (priv->set_focus_handler != 0)
488 toplevel = gtk_widget_get_toplevel (widget);
490 g_signal_handlers_disconnect_by_func
491 (toplevel, (void (*)) G_CALLBACK (set_focus_cb), bar);
492 priv->set_focus_handler = 0;
495 GTK_WIDGET_CLASS (egg_find_bar_parent_class)->hide (widget);
499 egg_find_bar_grab_focus (GtkWidget *widget)
501 EggFindBar *find_bar = EGG_FIND_BAR (widget);
502 EggFindBarPrivate *priv = find_bar->priv;
504 gtk_widget_grab_focus (priv->find_entry);
510 * Creates a new #EggFindBar.
512 * Returns: a newly created #EggFindBar
517 egg_find_bar_new (void)
519 EggFindBar *find_bar;
521 find_bar = g_object_new (EGG_TYPE_FIND_BAR, NULL);
523 return GTK_WIDGET (find_bar);
527 * egg_find_bar_set_search_string:
529 * Sets the string that should be found/highlighted in the document.
530 * Empty string is converted to NULL.
535 egg_find_bar_set_search_string (EggFindBar *find_bar,
536 const char *search_string)
538 EggFindBarPrivate *priv;
540 g_return_if_fail (EGG_IS_FIND_BAR (find_bar));
542 priv = (EggFindBarPrivate *)find_bar->priv;
544 g_object_freeze_notify (G_OBJECT (find_bar));
546 if (priv->search_string != search_string)
550 old = priv->search_string;
552 if (search_string && *search_string == '\0')
553 search_string = NULL;
555 /* Only update if the string has changed; setting the entry
556 * will emit changed on the entry which will re-enter
557 * this function, but we'll handle that fine with this
560 if ((old && search_string == NULL) ||
561 (old == NULL && search_string) ||
562 (old && search_string &&
563 strcmp (old, search_string) != 0))
567 priv->search_string = g_strdup (search_string);
570 gtk_entry_set_text (GTK_ENTRY (priv->find_entry),
571 priv->search_string ?
572 priv->search_string :
575 not_empty = (search_string == NULL) ? FALSE : TRUE;
577 gtk_widget_set_sensitive (GTK_WIDGET (find_bar->priv->next_button), not_empty);
578 gtk_widget_set_sensitive (GTK_WIDGET (find_bar->priv->previous_button), not_empty);
580 g_object_notify (G_OBJECT (find_bar),
585 g_object_thaw_notify (G_OBJECT (find_bar));
590 * egg_find_bar_get_search_string:
592 * Gets the string that should be found/highlighted in the document.
594 * Returns: the string
599 egg_find_bar_get_search_string (EggFindBar *find_bar)
601 EggFindBarPrivate *priv;
603 g_return_val_if_fail (EGG_IS_FIND_BAR (find_bar), NULL);
605 priv = find_bar->priv;
607 return priv->search_string ? priv->search_string : "";
611 * egg_find_bar_set_case_sensitive:
613 * Sets whether the search is case sensitive
618 egg_find_bar_set_case_sensitive (EggFindBar *find_bar,
619 gboolean case_sensitive)
621 EggFindBarPrivate *priv;
623 g_return_if_fail (EGG_IS_FIND_BAR (find_bar));
625 priv = (EggFindBarPrivate *)find_bar->priv;
627 g_object_freeze_notify (G_OBJECT (find_bar));
629 case_sensitive = case_sensitive != FALSE;
631 if (priv->case_sensitive != case_sensitive)
633 priv->case_sensitive = case_sensitive;
635 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->case_button),
636 priv->case_sensitive);
638 g_object_notify (G_OBJECT (find_bar),
642 g_object_thaw_notify (G_OBJECT (find_bar));
646 * egg_find_bar_get_case_sensitive:
648 * Gets whether the search is case sensitive
650 * Returns: TRUE if it's case sensitive
655 egg_find_bar_get_case_sensitive (EggFindBar *find_bar)
657 EggFindBarPrivate *priv;
659 g_return_val_if_fail (EGG_IS_FIND_BAR (find_bar), FALSE);
661 priv = (EggFindBarPrivate *)find_bar->priv;
663 return priv->case_sensitive;
667 get_style_color (EggFindBar *find_bar,
668 const char *style_prop_name,
671 GdkColor *style_color;
673 gtk_widget_ensure_style (GTK_WIDGET (find_bar));
674 gtk_widget_style_get (GTK_WIDGET (find_bar),
675 "color", &style_color, NULL);
678 *color = *style_color;
679 gdk_color_free (style_color);
684 * egg_find_bar_get_all_matches_color:
686 * Gets the color to use to highlight all the
692 egg_find_bar_get_all_matches_color (EggFindBar *find_bar,
695 GdkColor found_color = { 0, 0, 0, 0x0f0f };
697 get_style_color (find_bar, "all_matches_color", &found_color);
699 *color = found_color;
703 * egg_find_bar_get_current_match_color:
705 * Gets the color to use to highlight the match
706 * we're currently on.
711 egg_find_bar_get_current_match_color (EggFindBar *find_bar,
714 GdkColor found_color = { 0, 0, 0, 0xffff };
716 get_style_color (find_bar, "current_match_color", &found_color);
718 *color = found_color;
722 * egg_find_bar_set_status_text:
724 * Sets some text to display if there's space; typical text would
725 * be something like "5 results on this page" or "No results"
727 * @text: the text to display
732 egg_find_bar_set_status_text (EggFindBar *find_bar,
735 EggFindBarPrivate *priv;
737 g_return_if_fail (EGG_IS_FIND_BAR (find_bar));
739 priv = (EggFindBarPrivate *)find_bar->priv;
741 gtk_label_set_text (GTK_LABEL (priv->status_label), text);
742 g_object_set (priv->status_separator, "visible", text != NULL && *text != '\0', NULL);
743 g_object_set (priv->status_item, "visible", text != NULL && *text !='\0', NULL);