Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / lib / widgets / ephy-search-entry.c
blobfc6b760accbefb9af03712518287f1eaa003e057
1 /*
2 * Copyright © 2002 Jorn Baayen <jorn@nl.linux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * $Id: ephy-search-entry.c 6952 2007-03-11 19:42:02Z chpe $
21 #include "config.h"
23 #include <gtk/gtklabel.h>
24 #include <glib/gi18n.h>
25 #include <string.h>
27 #include "ephy-search-entry.h"
29 static void ephy_search_entry_class_init (EphySearchEntryClass *klass);
30 static void ephy_search_entry_init (EphySearchEntry *entry);
32 #define EPHY_SEARCH_ENTRY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_SEARCH_ENTRY, EphySearchEntryPrivate))
34 struct _EphySearchEntryPrivate
36 gboolean clearing;
38 guint timeout;
41 enum
43 SEARCH,
44 LAST_SIGNAL
47 static GObjectClass *parent_class = NULL;
49 static guint ephy_search_entry_signals[LAST_SIGNAL] = { 0 };
51 GType
52 ephy_search_entry_get_type (void)
54 static GType type = 0;
56 if (G_UNLIKELY (type == 0))
58 const GTypeInfo our_info =
60 sizeof (EphySearchEntryClass),
61 NULL,
62 NULL,
63 (GClassInitFunc) ephy_search_entry_class_init,
64 NULL,
65 NULL,
66 sizeof (EphySearchEntry),
68 (GInstanceInitFunc) ephy_search_entry_init
71 type = g_type_register_static (GTK_TYPE_ENTRY,
72 "EphySearchEntry",
73 &our_info, 0);
76 return type;
79 static void
80 ephy_search_entry_class_init (EphySearchEntryClass *klass)
82 GObjectClass *object_class = G_OBJECT_CLASS (klass);
84 parent_class = g_type_class_peek_parent (klass);
86 ephy_search_entry_signals[SEARCH] =
87 g_signal_new ("search",
88 G_OBJECT_CLASS_TYPE (object_class),
89 G_SIGNAL_RUN_LAST,
90 G_STRUCT_OFFSET (EphySearchEntryClass, search),
91 NULL, NULL,
92 g_cclosure_marshal_VOID__STRING,
93 G_TYPE_NONE,
95 G_TYPE_STRING);
97 g_type_class_add_private (object_class, sizeof (EphySearchEntryPrivate));
100 static gboolean
101 ephy_search_entry_timeout_cb (EphySearchEntry *entry)
103 g_signal_emit (G_OBJECT (entry), ephy_search_entry_signals[SEARCH], 0,
104 gtk_entry_get_text (GTK_ENTRY (entry)));
105 entry->priv->timeout = 0;
107 return FALSE;
110 static void
111 ephy_search_entry_changed_cb (GtkEditable *editable,
112 EphySearchEntry *entry)
114 if (entry->priv->clearing == TRUE)
115 return;
117 if (entry->priv->timeout != 0)
119 g_source_remove (entry->priv->timeout);
120 entry->priv->timeout = 0;
123 entry->priv->timeout = g_timeout_add (300, (GSourceFunc) ephy_search_entry_timeout_cb, entry);
126 static void
127 ephy_search_entry_destroy_cb (GtkEditable *editable,
128 EphySearchEntry *entry)
130 if (entry->priv->timeout)
132 g_source_remove (entry->priv->timeout);
133 entry->priv->timeout = 0;
137 static void
138 ephy_search_entry_init (EphySearchEntry *entry)
140 entry->priv = EPHY_SEARCH_ENTRY_GET_PRIVATE (entry);
142 g_signal_connect (G_OBJECT (entry),
143 "destroy",
144 G_CALLBACK (ephy_search_entry_destroy_cb),
145 entry);
146 g_signal_connect (G_OBJECT (entry),
147 "changed",
148 G_CALLBACK (ephy_search_entry_changed_cb),
149 entry);
152 GtkWidget *
153 ephy_search_entry_new (void)
155 GtkWidget *entry;
157 entry = GTK_WIDGET (g_object_new (EPHY_TYPE_SEARCH_ENTRY,
158 NULL));
160 return entry;
163 void
164 ephy_search_entry_clear (EphySearchEntry *entry)
166 if (entry->priv->timeout != 0)
168 g_source_remove (entry->priv->timeout);
169 entry->priv->timeout = 0;
172 entry->priv->clearing = TRUE;
174 gtk_entry_set_text (GTK_ENTRY (entry), "");
176 entry->priv->clearing = FALSE;