2
// Copyright © 2011 Avi Romanoff, Allen Lowe, Maxwell Barvian
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 3 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, see <http://www.gnu.org/licenses/>.
17
// Code from Granite https://launchpad.net/granite
23
namespace Lsc.Widgets {
25
public class HintedEntry : Gtk.Entry {
27
public string hint_string {
29
return placeholder_text;
32
placeholder_text = value;
36
public HintedEntry (string hint_string) {
37
this.hint_string = hint_string;
41
public class SearchBar : HintedEntry {
43
private bool is_searching = true;
44
private uint timeout_id = 0;
47
* This value handles how much time (in ms) should pass
48
* after the user stops typing. By default it is set
51
public int pause_delay { get; set; default = 300; }
54
* text_changed () signal is emitted after a short delay,
55
* which depends on pause_delay.
56
* If you need a synchronous signal without any delay,
57
* use changed () method.
59
public signal void text_changed_pause (string text);
61
public SearchBar (string hint_string) {
65
set_icon_from_stock (EntryIconPosition.PRIMARY, Stock.FIND);
67
// Signals and callbacks
68
changed.connect (manage_icon);
69
changed.connect_after (on_changed);
70
focus_in_event.connect (on_focus_in);
71
focus_out_event.connect (on_focus_out);
72
icon_press.connect (on_icon_press);
75
protected new void hint () {
77
set_icon_from_stock (Gtk.EntryIconPosition.SECONDARY, null);
80
private bool on_focus_in () {
88
private bool on_focus_out () {
89
if (get_text () == "") {
96
private void manage_icon () {
98
set_icon_from_gicon (EntryIconPosition.SECONDARY, new ThemedIcon.with_default_fallbacks ("edit-clear-symbolic"));
100
set_icon_from_stock (EntryIconPosition.SECONDARY, null);
103
private void on_icon_press (EntryIconPosition position) {
104
if (position == EntryIconPosition.SECONDARY) {
105
is_searching = false;
107
set_icon_from_stock (position, null);
111
is_searching = false;
116
private void on_changed () {
119
Source.remove (timeout_id);
121
timeout_id = Timeout.add (pause_delay, emit_text_changed);
125
private bool emit_text_changed () {
127
var terms = get_text ();
128
text_changed_pause (terms); // Emit signal
130
return Source.remove (timeout_id);