aboutsummaryrefslogtreecommitdiffstats
path: root/lib/widgets/ephy-search-entry.c
blob: 2d031877425e30da563d16c9502d89b9175728af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* 
 *  Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gtk/gtklabel.h>
#include <glib/gi18n.h>
#include <string.h>

#include "ephy-search-entry.h"

static void ephy_search_entry_class_init (EphySearchEntryClass *klass);
static void ephy_search_entry_init (EphySearchEntry *entry);

#define EPHY_SEARCH_ENTRY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_SEARCH_ENTRY, EphySearchEntryPrivate))

struct EphySearchEntryPrivate
{
    gboolean clearing;

    guint timeout;
};

enum
{
    SEARCH,
    LAST_SIGNAL
};

static GObjectClass *parent_class = NULL;

static guint ephy_search_entry_signals[LAST_SIGNAL] = { 0 };

GType
ephy_search_entry_get_type (void)
{
    static GType ephy_search_entry_type = 0;

    if (ephy_search_entry_type == 0)
    {
        static const GTypeInfo our_info =
        {
            sizeof (EphySearchEntryClass),
            NULL,
            NULL,
            (GClassInitFunc) ephy_search_entry_class_init,
            NULL,
            NULL,
            sizeof (EphySearchEntry),
            0,
            (GInstanceInitFunc) ephy_search_entry_init
        };

        ephy_search_entry_type = g_type_register_static (GTK_TYPE_ENTRY,
                                     "EphySearchEntry",
                                     &our_info, 0);
    }

    return ephy_search_entry_type;
}

static void
ephy_search_entry_class_init (EphySearchEntryClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    ephy_search_entry_signals[SEARCH] =
        g_signal_new ("search",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EphySearchEntryClass, search),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__STRING,
                  G_TYPE_NONE,
                  1,
                  G_TYPE_STRING);

    g_type_class_add_private (object_class, sizeof (EphySearchEntryPrivate));
}

static gboolean
ephy_search_entry_timeout_cb (EphySearchEntry *entry)
{
    g_signal_emit (G_OBJECT (entry), ephy_search_entry_signals[SEARCH], 0,
               gtk_entry_get_text (GTK_ENTRY (entry)));
    entry->priv->timeout = 0;

    return FALSE;
}

static void
ephy_search_entry_changed_cb (GtkEditable *editable,
                  EphySearchEntry *entry)
{
    if (entry->priv->clearing == TRUE)
        return;

    if (entry->priv->timeout != 0)
    {
        g_source_remove (entry->priv->timeout);
        entry->priv->timeout = 0;
    }

    entry->priv->timeout = g_timeout_add (300, (GSourceFunc) ephy_search_entry_timeout_cb, entry);
}

static void
ephy_search_entry_destroy_cb (GtkEditable *editable,
                  EphySearchEntry *entry)
{
    if (entry->priv->timeout)
    {
        g_source_remove (entry->priv->timeout);
        entry->priv->timeout = 0;
    }
}

static void
ephy_search_entry_init (EphySearchEntry *entry)
{
    entry->priv = EPHY_SEARCH_ENTRY_GET_PRIVATE (entry);

    entry->priv->clearing = FALSE;
    entry->priv->timeout = 0;

    g_signal_connect (G_OBJECT (entry),
              "destroy",
              G_CALLBACK (ephy_search_entry_destroy_cb),
              entry);
    g_signal_connect (G_OBJECT (entry),
              "changed",
              G_CALLBACK (ephy_search_entry_changed_cb),
              entry);
}

GtkWidget *
ephy_search_entry_new (void)
{
    GtkWidget *entry;

    entry = GTK_WIDGET (g_object_new (EPHY_TYPE_SEARCH_ENTRY,
                      NULL));

    return entry;
}

void
ephy_search_entry_clear (EphySearchEntry *entry)
{
    if (entry->priv->timeout != 0)
    {
        g_source_remove (entry->priv->timeout);
        entry->priv->timeout = 0;
    }

    entry->priv->clearing = TRUE;

    gtk_entry_set_text (GTK_ENTRY (entry), "");

    entry->priv->clearing = FALSE;
}