]> Pileus Git - ~andy/gtk/blob - gtk/gtksearchentry.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtksearchentry.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2012 Red Hat, Inc.
3  *
4  * Authors:
5  * - Bastien Nocera <bnocera@redhat.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 2012.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26  */
27
28 #include "config.h"
29
30 #include "gtksearchentry.h"
31
32 /**
33  * SECTION:gtksearchentry
34  * @Short_description: An entry which shows a search icon
35  * @Title: GtkSearchEntry
36  *
37  * #GtkSearchEntry is a subclass of #GtkEntry that has
38  * been tailored for use as a search entry.
39  *
40  * It will show an inactive symbolic "find" icon when the
41  * search entry is empty, and a symbolic "clear" icon when
42  * there is text. Clicking on the "clear" icon will empty
43  * the search entry.
44  *
45  * Note that the search/clear icon is shown using a secondary
46  * icon, and thus does not work if you are using the secondary
47  * icon position for some other purpose.
48  *
49  * Since: 3.6
50  */
51
52 G_DEFINE_TYPE (GtkSearchEntry, gtk_search_entry, GTK_TYPE_ENTRY)
53
54 static void
55 gtk_search_entry_class_init (GtkSearchEntryClass *klass)
56 {
57 }
58
59 static void
60 search_entry_clear_cb (GtkEntry *entry,
61                        gpointer  user_data)
62 {
63   gtk_entry_set_text (entry, "");
64 }
65
66 static void
67 search_entry_changed_cb (GtkEntry *entry,
68                          gpointer  user_data)
69 {
70   const char *str, *icon_name;
71   gboolean active;
72
73   str = gtk_entry_get_text (entry);
74
75   if (str == NULL || *str == '\0')
76     {
77       icon_name = NULL;
78       active = FALSE;
79     }
80   else
81     {
82       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
83         icon_name = "edit-clear-rtl-symbolic";
84       else
85         icon_name = "edit-clear-symbolic";
86       active = TRUE;
87     }
88
89   g_object_set (entry,
90                 "secondary-icon-name", icon_name,
91                 "secondary-icon-activatable", active,
92                 "secondary-icon-sensitive", active,
93                 NULL);
94 }
95
96 static void
97 gtk_search_entry_init (GtkSearchEntry *entry)
98 {
99   g_signal_connect (entry, "changed",
100                     G_CALLBACK (search_entry_changed_cb), NULL);
101   g_signal_connect (entry, "icon-release",
102                     G_CALLBACK (search_entry_clear_cb), NULL);
103
104   g_object_set (entry,
105                 "primary-icon-name", "edit-find-symbolic",
106                 "primary-icon-activatable", FALSE,
107                 "primary-icon-sensitive", FALSE,
108                 NULL);
109
110   search_entry_changed_cb (GTK_ENTRY (entry), NULL);
111 }
112
113 /**
114  * gtk_search_entry_new:
115  *
116  * Creates a #GtkSearchEntry, with a find icon when the search field is
117  * empty, and a clear icon when it isn't.
118  *
119  * Return value: a new #GtkSearchEntry
120  *
121  * Since: 3.6
122  */
123 GtkWidget *
124 gtk_search_entry_new (void)
125 {
126   return GTK_WIDGET (g_object_new (GTK_TYPE_SEARCH_ENTRY, NULL));
127 }