1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 * Copyright (C) 2005 Novell, Inc.
5 * Nautilus is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * Nautilus 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; see the file COPYING. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Anders Carlsson <andersca@imendio.com>
25 #include "nautilus-search-bar.h"
27 #include <glib/gi18n.h>
28 #include <eel/eel-gtk-macros.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtkalignment.h>
31 #include <gtk/gtkbindings.h>
32 #include <gtk/gtkbutton.h>
33 #include <gtk/gtkentry.h>
34 #include <gtk/gtkframe.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtklabel.h>
38 struct NautilusSearchBarDetails
{
40 gboolean entry_borrowed
;
49 static guint signals
[LAST_SIGNAL
];
51 static void nautilus_search_bar_class_init (NautilusSearchBarClass
*class);
52 static void nautilus_search_bar_init (NautilusSearchBar
*bar
);
54 EEL_CLASS_BOILERPLATE (NautilusSearchBar
,
60 finalize (GObject
*object
)
62 NautilusSearchBar
*bar
;
64 bar
= NAUTILUS_SEARCH_BAR (object
);
66 g_free (bar
->details
);
68 EEL_CALL_PARENT (G_OBJECT_CLASS
, finalize
, (object
));
72 nautilus_search_bar_class_init (NautilusSearchBarClass
*class)
74 GObjectClass
*gobject_class
;
75 GtkBindingSet
*binding_set
;
77 gobject_class
= G_OBJECT_CLASS (class);
78 gobject_class
->finalize
= finalize
;
81 g_signal_new ("activate",
82 G_TYPE_FROM_CLASS (class),
84 G_STRUCT_OFFSET (NautilusSearchBarClass
, activate
),
86 g_cclosure_marshal_VOID__VOID
,
90 g_signal_new ("cancel",
91 G_TYPE_FROM_CLASS (class),
92 G_SIGNAL_RUN_LAST
| G_SIGNAL_ACTION
,
93 G_STRUCT_OFFSET (NautilusSearchBarClass
, cancel
),
95 g_cclosure_marshal_VOID__VOID
,
98 binding_set
= gtk_binding_set_by_class (class);
99 gtk_binding_entry_add_signal (binding_set
, GDK_Escape
, 0, "cancel", 0);
103 entry_has_text (NautilusSearchBar
*bar
)
107 text
= gtk_entry_get_text (GTK_ENTRY (bar
->details
->entry
));
109 return text
!= NULL
&& text
[0] != '\0';
113 entry_activate_cb (GtkWidget
*entry
, NautilusSearchBar
*bar
)
115 if (entry_has_text (bar
) && !bar
->details
->entry_borrowed
) {
116 g_signal_emit (bar
, signals
[ACTIVATE
], 0);
122 nautilus_search_bar_init (NautilusSearchBar
*bar
)
124 GtkWidget
*alignment
;
128 bar
->details
= g_new0 (NautilusSearchBarDetails
, 1);
130 gtk_event_box_set_visible_window (GTK_EVENT_BOX (bar
), FALSE
);
132 alignment
= gtk_alignment_new (0.5, 0.5,
134 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment
),
136 gtk_widget_show (alignment
);
137 gtk_container_add (GTK_CONTAINER (bar
), alignment
);
139 hbox
= gtk_hbox_new (FALSE
, 6);
140 gtk_widget_show (hbox
);
141 gtk_container_add (GTK_CONTAINER (alignment
), hbox
);
143 label
= gtk_label_new (_("Search:"));
144 gtk_widget_show (label
);
146 gtk_box_pack_start (GTK_BOX (hbox
), label
, FALSE
, FALSE
, 0);
148 bar
->details
->entry
= gtk_entry_new ();
149 gtk_box_pack_start (GTK_BOX (hbox
), bar
->details
->entry
, TRUE
, TRUE
, 0);
151 g_signal_connect (bar
->details
->entry
, "activate",
152 G_CALLBACK (entry_activate_cb
), bar
);
154 gtk_widget_show (bar
->details
->entry
);
158 nautilus_search_bar_borrow_entry (NautilusSearchBar
*bar
)
160 GtkBindingSet
*binding_set
;
162 bar
->details
->entry_borrowed
= TRUE
;
164 binding_set
= gtk_binding_set_by_class (G_OBJECT_GET_CLASS (bar
));
165 gtk_binding_entry_clear (binding_set
, GDK_Escape
, 0);
166 return bar
->details
->entry
;
170 nautilus_search_bar_return_entry (NautilusSearchBar
*bar
)
172 GtkBindingSet
*binding_set
;
174 bar
->details
->entry_borrowed
= FALSE
;
176 binding_set
= gtk_binding_set_by_class (G_OBJECT_GET_CLASS (bar
));
177 gtk_binding_entry_add_signal (binding_set
, GDK_Escape
, 0, "cancel", 0);
181 nautilus_search_bar_new (void)
185 bar
= g_object_new (NAUTILUS_TYPE_SEARCH_BAR
, NULL
);
191 nautilus_search_bar_get_query (NautilusSearchBar
*bar
)
193 const char *query_text
;
194 NautilusQuery
*query
;
196 query_text
= gtk_entry_get_text (GTK_ENTRY (bar
->details
->entry
));
198 /* Empty string is a NULL query */
199 if (query_text
&& query_text
[0] == '\0') {
203 query
= nautilus_query_new ();
204 nautilus_query_set_text (query
, query_text
);
210 nautilus_search_bar_grab_focus (NautilusSearchBar
*bar
)
212 gtk_widget_grab_focus (bar
->details
->entry
);
216 nautilus_search_bar_clear (NautilusSearchBar
*bar
)
218 gtk_entry_set_text (GTK_ENTRY (bar
->details
->entry
), "");