1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
6 * anjuta is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "anjuta-entry.h"
23 * SECTION:anjuta-entry
24 * @short_description: #GtkEntry subclass that displays help text with a button
25 * to clear the entry's contents.
26 * @include: libanjuta/anjuta-entry.h
28 * AnjutaEntry is a version of a #GtkEntry that displays some text, in
29 * a lighter color, that describes what is to be entered into it. There is also
30 * a button on the left to clear the entry's content quickly. AnjutaEntry is
31 * similar to the serach boxes used in Evolution and Glade, but is more generic
32 * can can be used in almost any situation.
48 struct _AnjutaEntryPriv
50 gboolean showing_help_text
;
54 G_DEFINE_TYPE (AnjutaEntry
, anjuta_entry
, GTK_TYPE_ENTRY
);
57 anjuta_entry_set_mode (AnjutaEntry
*self
, AnjutaEntryMode mode
)
61 style
= gtk_widget_get_style (GTK_WIDGET (self
));
65 case ANJUTA_ENTRY_NORMAL
:
66 /* Remove the help text from the widget */
67 if (self
->priv
->showing_help_text
)
68 gtk_entry_set_text (GTK_ENTRY (self
), "");
70 gtk_widget_modify_text (GTK_WIDGET (self
), GTK_STATE_NORMAL
, NULL
);
72 self
->priv
->showing_help_text
= FALSE
;
75 case ANJUTA_ENTRY_HELP
:
76 if (self
->priv
->help_text
)
77 gtk_entry_set_text (GTK_ENTRY (self
), self
->priv
->help_text
);
79 gtk_entry_set_text (GTK_ENTRY (self
), "");
81 gtk_widget_modify_text (GTK_WIDGET (self
),
83 &(style
->text
[GTK_STATE_INSENSITIVE
]));
85 self
->priv
->showing_help_text
= TRUE
;
93 /* It's probably terrible practice for a subclass to be listening to the
94 * parent' class's signals, but for some reason the icon release signal
95 * doesn't have a virtual method pointer in the GtkEntry class structure */
97 anjuta_entry_icon_release (GtkEntry
*entry
, GtkEntryIconPosition icon_pos
,
98 GdkEvent
*event
, gpointer user_data
)
100 if (icon_pos
== GTK_ENTRY_ICON_SECONDARY
)
101 gtk_entry_set_text (entry
, "");
105 anjuta_entry_init (AnjutaEntry
*self
)
107 self
->priv
= g_new0 (AnjutaEntryPriv
, 1);
109 gtk_entry_set_icon_from_stock (GTK_ENTRY (self
), GTK_ENTRY_ICON_SECONDARY
,
111 gtk_entry_set_icon_activatable (GTK_ENTRY (self
), GTK_ENTRY_ICON_SECONDARY
,
114 g_signal_connect (G_OBJECT (self
), "icon-release",
115 G_CALLBACK (anjuta_entry_icon_release
),
118 anjuta_entry_set_mode (self
, ANJUTA_ENTRY_HELP
);
122 anjuta_entry_finalize (GObject
*object
)
126 self
= ANJUTA_ENTRY (object
);
128 g_free (self
->priv
->help_text
);
131 G_OBJECT_CLASS (anjuta_entry_parent_class
)->finalize (object
);
135 anjuta_entry_set_property (GObject
*object
, guint prop_id
, const GValue
*value
,
140 g_return_if_fail (ANJUTA_IS_ENTRY (object
));
142 self
= ANJUTA_ENTRY (object
);
147 g_free (self
->priv
->help_text
);
149 self
->priv
->help_text
= g_value_dup_string (value
);
151 /* Update the display */
152 if (self
->priv
->showing_help_text
)
154 if (self
->priv
->help_text
)
156 gtk_entry_set_text (GTK_ENTRY (self
),
157 self
->priv
->help_text
);
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
168 anjuta_entry_get_property (GObject
*object
, guint prop_id
, GValue
*value
,
173 g_return_if_fail (ANJUTA_IS_ENTRY (object
));
175 self
= ANJUTA_ENTRY (object
);
180 g_value_set_string (value
, self
->priv
->help_text
);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
189 anjuta_entry_focus_in_event (GtkWidget
*widget
, GdkEventFocus
*event
)
193 self
= ANJUTA_ENTRY (widget
);
195 if (self
->priv
->showing_help_text
)
196 anjuta_entry_set_mode (self
, ANJUTA_ENTRY_NORMAL
);
198 return GTK_WIDGET_CLASS (anjuta_entry_parent_class
)->focus_in_event (widget
, event
);
202 anjuta_entry_focus_out_event (GtkWidget
*widget
, GdkEventFocus
*event
)
207 self
= ANJUTA_ENTRY (widget
);
208 text
= gtk_entry_get_text (GTK_ENTRY (widget
));
210 if (text
== NULL
|| text
[0] == '\0')
211 anjuta_entry_set_mode (self
, ANJUTA_ENTRY_HELP
);
213 return GTK_WIDGET_CLASS (anjuta_entry_parent_class
)->focus_out_event (widget
, event
);
217 anjuta_entry_class_init (AnjutaEntryClass
*klass
)
219 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
220 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS (klass
);
222 object_class
->finalize
= anjuta_entry_finalize
;
223 object_class
->set_property
= anjuta_entry_set_property
;
224 object_class
->get_property
= anjuta_entry_get_property
;
225 widget_class
->focus_in_event
= anjuta_entry_focus_in_event
;
226 widget_class
->focus_out_event
= anjuta_entry_focus_out_event
;
229 * AnjutaEntry::help-text:
231 * Text that should be displayed when the entry is empty. This text should
232 * briefly describe what the user should enter.
234 g_object_class_install_property (object_class
,
236 g_param_spec_string ("help-text",
238 _("Text to show the user what to enter into the entry"),
240 G_PARAM_READABLE
| G_PARAM_WRITABLE
));
246 * Creates a new AnjutaEntry.
249 anjuta_entry_new (void)
251 return g_object_new (ANJUTA_TYPE_ENTRY
, NULL
);
255 * anjuta_entry_get_text:
256 * @self: An AnjutaEntry
258 * Returns the contents of the entry. If the entry is empty, the help text will
259 * be displayed and an empty string will be returned.
262 anjuta_entry_get_text (AnjutaEntry
*self
)
264 return (self
->priv
->showing_help_text
) ?
265 "" : gtk_entry_get_text (GTK_ENTRY (self
)) ;
269 * anjuta_entry_dup_text:
270 * @self: An AnjutaEntry
272 * Returns a copy of the contents of the entry. If the entry is empty, the
273 * returned string will be empty. The returned string must be freed when no
277 anjuta_entry_dup_text (AnjutaEntry
*self
)
279 return g_strdup (anjuta_entry_get_text (self
));
283 * anjuta_entry_set_text:
284 * @self: An AnjutaEntry
285 * @text: The new text
287 * Sets the text on the entry, showing the help text if the text is empty.
290 anjuta_entry_set_text (AnjutaEntry
*self
, const gchar
*text
)
292 if (text
!= NULL
&& text
[0] != '\0')
293 anjuta_entry_set_mode (self
, ANJUTA_ENTRY_NORMAL
);
295 anjuta_entry_set_mode (self
, ANJUTA_ENTRY_HELP
);
297 gtk_entry_set_text (GTK_ENTRY (self
), text
);
301 * anjuta_entry_is_showing_help_text:
302 * @self: An AnjutaEntry
304 * Returns whether the entry is showing its help text. In practice, if this
305 * method returns %TRUE, it means that the user has not entered anything.
308 anjuta_entry_is_showing_help_text (AnjutaEntry
*self
)
310 return self
->priv
->showing_help_text
;