2008-05-05 Paolo Borelli <pborelli@katamail.com>
[nautilus.git] / src / nautilus-search-bar.c
blob7d32f43f1e8f7c7c0cf8e96178bf2f08da1f4b17
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
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>
24 #include <config.h>
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 {
39 GtkWidget *entry;
40 gboolean entry_borrowed;
43 enum {
44 ACTIVATE,
45 CANCEL,
46 LAST_SIGNAL
47 };
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,
55 nautilus_search_bar,
56 GTK_TYPE_EVENT_BOX)
59 static void
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));
71 static void
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;
80 signals[ACTIVATE] =
81 g_signal_new ("activate",
82 G_TYPE_FROM_CLASS (class),
83 G_SIGNAL_RUN_LAST,
84 G_STRUCT_OFFSET (NautilusSearchBarClass, activate),
85 NULL, NULL,
86 g_cclosure_marshal_VOID__VOID,
87 G_TYPE_NONE, 0);
89 signals[CANCEL] =
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),
94 NULL, NULL,
95 g_cclosure_marshal_VOID__VOID,
96 G_TYPE_NONE, 0);
98 binding_set = gtk_binding_set_by_class (class);
99 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0, "cancel", 0);
102 static gboolean
103 entry_has_text (NautilusSearchBar *bar)
105 const char *text;
107 text = gtk_entry_get_text (GTK_ENTRY (bar->details->entry));
109 return text != NULL && text[0] != '\0';
112 static void
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);
121 static void
122 nautilus_search_bar_init (NautilusSearchBar *bar)
124 GtkWidget *alignment;
125 GtkWidget *hbox;
126 GtkWidget *label;
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,
133 1.0, 1.0);
134 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
135 0, 0, 6, 6);
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);
157 GtkWidget *
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;
169 void
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);
180 GtkWidget *
181 nautilus_search_bar_new (void)
183 GtkWidget *bar;
185 bar = g_object_new (NAUTILUS_TYPE_SEARCH_BAR, NULL);
187 return bar;
190 NautilusQuery *
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') {
200 return NULL;
203 query = nautilus_query_new ();
204 nautilus_query_set_text (query, query_text);
206 return query;
209 void
210 nautilus_search_bar_grab_focus (NautilusSearchBar *bar)
212 gtk_widget_grab_focus (bar->details->entry);
215 void
216 nautilus_search_bar_clear (NautilusSearchBar *bar)
218 gtk_entry_set_text (GTK_ENTRY (bar->details->entry), "");